diff options
author | Pushkar Joshi | 2012-05-31 12:20:03 -0700 |
---|---|---|
committer | Pushkar Joshi | 2012-05-31 12:20:03 -0700 |
commit | 4bc52365b1f81a386c3c59fd7c6ad874bb387cb5 (patch) | |
tree | c5b17297aba04bd54a59cf6214d29486e089031b /js/lib/rdge/materials/radial-gradient-material.js | |
parent | 75a862d305bc4502e22bc5aa65fa271143b8cf6c (diff) | |
parent | 6042bdc5f2aada4412912fd01602d32c9088dc26 (diff) | |
download | ninja-4bc52365b1f81a386c3c59fd7c6ad874bb387cb5.tar.gz |
Merge branch 'master' into pentool
Diffstat (limited to 'js/lib/rdge/materials/radial-gradient-material.js')
-rwxr-xr-x | js/lib/rdge/materials/radial-gradient-material.js | 70 |
1 files changed, 62 insertions, 8 deletions
diff --git a/js/lib/rdge/materials/radial-gradient-material.js b/js/lib/rdge/materials/radial-gradient-material.js index dd40d31d..b0b63cee 100755 --- a/js/lib/rdge/materials/radial-gradient-material.js +++ b/js/lib/rdge/materials/radial-gradient-material.js | |||
@@ -6,6 +6,7 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot | |||
6 | 6 | ||
7 | var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser; | 7 | var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser; |
8 | var Material = require("js/lib/rdge/materials/material").Material; | 8 | var Material = require("js/lib/rdge/materials/material").Material; |
9 | var ShapePrimitive = require("js/lib/geom/shape-primitive").ShapePrimitive; | ||
9 | 10 | ||
10 | var RadialGradientMaterial = function RadialGradientMaterial() { | 11 | var RadialGradientMaterial = function RadialGradientMaterial() { |
11 | /////////////////////////////////////////////////////////////////////// | 12 | /////////////////////////////////////////////////////////////////////// |
@@ -24,6 +25,8 @@ var RadialGradientMaterial = function RadialGradientMaterial() { | |||
24 | this._colorStop4 = 1.0; | 25 | this._colorStop4 = 1.0; |
25 | // this._colorCount = 4; | 26 | // this._colorCount = 4; |
26 | 27 | ||
28 | this._textureTransform = [1,0,0, 0,1,0, 0,0,1]; | ||
29 | |||
27 | /////////////////////////////////////////////////////////////////////// | 30 | /////////////////////////////////////////////////////////////////////// |
28 | // Property Accessors | 31 | // Property Accessors |
29 | /////////////////////////////////////////////////////////////////////// | 32 | /////////////////////////////////////////////////////////////////////// |
@@ -143,9 +146,9 @@ var RadialGradientMaterial = function RadialGradientMaterial() { | |||
143 | /////////////////////////////////////////////////////////////////////// | 146 | /////////////////////////////////////////////////////////////////////// |
144 | // Material Property Accessors | 147 | // Material Property Accessors |
145 | /////////////////////////////////////////////////////////////////////// | 148 | /////////////////////////////////////////////////////////////////////// |
146 | this._propNames = ["color1", "color2", "color3", "color4", "colorStop1", "colorStop2", "colorStop3", "colorStop4", "angle"]; | 149 | this._propNames = ["color1", "color2", "color3", "color4", "colorStop1", "colorStop2", "colorStop3", "colorStop4" ]; |
147 | this._propLabels = ["Color 1", "Color 2", "Color 3", "Color 4", "Color Stop 1", "Color Stop 2", "Color Stop 3", "Color Stop 4", "Angle"]; | 150 | this._propLabels = ["Color 1", "Color 2", "Color 3", "Color 4", "Color Stop 1", "Color Stop 2", "Color Stop 3", "Color Stop 4" ]; |
148 | this._propTypes = ["color", "color", "color", "color", "float", "float", "float", "float", "float"]; | 151 | this._propTypes = ["color", "color", "color", "color", "float", "float", "float", "float" ]; |
149 | this._propValues = []; | 152 | this._propValues = []; |
150 | 153 | ||
151 | this._propValues[this._propNames[0]] = this._color1.slice(0); | 154 | this._propValues[this._propNames[0]] = this._color1.slice(0); |
@@ -188,9 +191,21 @@ var RadialGradientMaterial = function RadialGradientMaterial() { | |||
188 | // Methods | 191 | // Methods |
189 | /////////////////////////////////////////////////////////////////////// | 192 | /////////////////////////////////////////////////////////////////////// |
190 | // duplcate method requirde | 193 | // duplcate method requirde |
191 | this.dup = function () { | 194 | this.dup = function () { |
192 | return new RadialGradientMaterial(); | 195 | // allocate a new material |
193 | }; | 196 | var newMat = new RadialGradientMaterial(); |
197 | |||
198 | // copy over the current values; | ||
199 | var propNames = [], propValues = [], propTypes = [], propLabels = []; | ||
200 | this.getAllProperties( propNames, propValues, propTypes, propLabels); | ||
201 | var n = propNames.length; | ||
202 | for (var i=0; i<n; i++) { | ||
203 | newMat.setProperty( propNames[i], propValues[i] ); | ||
204 | } | ||
205 | newMat._textureTransform = this._textureTransform.slice(); | ||
206 | |||
207 | return newMat; | ||
208 | }; | ||
194 | 209 | ||
195 | this.init = function (world) { | 210 | this.init = function (world) { |
196 | this.setWorld(world); | 211 | this.setWorld(world); |
@@ -231,9 +246,45 @@ var RadialGradientMaterial = function RadialGradientMaterial() { | |||
231 | this._shader['default'].u_colorStop3.set([s]); | 246 | this._shader['default'].u_colorStop3.set([s]); |
232 | s = this.getColorStop4(); | 247 | s = this.getColorStop4(); |
233 | this._shader['default'].u_colorStop4.set([s]); | 248 | this._shader['default'].u_colorStop4.set([s]); |
249 | |||
250 | this._shader['default'].u_texTransform.set( this._textureTransform ); | ||
234 | } | 251 | } |
235 | }; | 252 | }; |
236 | 253 | ||
254 | this.fitToPrimitive = function( prim ) | ||
255 | { | ||
256 | var bounds = ShapePrimitive.getBounds( prim ); | ||
257 | if (bounds) | ||
258 | { | ||
259 | var dx = Math.abs( bounds[3] - bounds[0] ), | ||
260 | dy = Math.abs( bounds[4] - bounds[1] ); | ||
261 | if (dy == 0) dy = 1.0; | ||
262 | if (dx == 0) dx = 1.0; | ||
263 | var xScale = 2.0, yScale = 2.0; | ||
264 | if (dx > dy) | ||
265 | yScale *= dy/dx; | ||
266 | else | ||
267 | xScale *= dx/dy; | ||
268 | |||
269 | // build the matrix - the translation to the origin, the scale, | ||
270 | // and the translation back to the center (hard coded at (0.5, 0.5) for now). | ||
271 | // the matrix is build directly instead of with matrix multiplications | ||
272 | // for efficiency, not to mention that the multiplication function does | ||
273 | // not exist for mat3's. | ||
274 | // the matrix as laid out below looks transposed - order is columnwise. | ||
275 | var xCtr = 0.5, yCtr = 0.5; | ||
276 | this._textureTransform = [ | ||
277 | xScale, 0.0, 0.0, | ||
278 | 0.0, yScale, 0.0, | ||
279 | xCtr*(1-xScale), yCtr*(1 - yScale), 1.0 | ||
280 | ]; | ||
281 | |||
282 | if (this._shader && this._shader['default']) | ||
283 | this._shader['default'].u_texTransform.set( this._textureTransform ); | ||
284 | |||
285 | } | ||
286 | }; | ||
287 | |||
237 | this.exportJSON = function () { | 288 | this.exportJSON = function () { |
238 | var jObj = | 289 | var jObj = |
239 | { | 290 | { |
@@ -247,7 +298,8 @@ var RadialGradientMaterial = function RadialGradientMaterial() { | |||
247 | 'colorStop1': this.getColorStop1(), | 298 | 'colorStop1': this.getColorStop1(), |
248 | 'colorStop2': this.getColorStop2(), | 299 | 'colorStop2': this.getColorStop2(), |
249 | 'colorStop3': this.getColorStop3(), | 300 | 'colorStop3': this.getColorStop3(), |
250 | 'colorStop4': this.getColorStop4() | 301 | 'colorStop4': this.getColorStop4(), |
302 | 'textureTransform': this._textureTransform | ||
251 | }; | 303 | }; |
252 | 304 | ||
253 | return jObj; | 305 | return jObj; |
@@ -266,6 +318,7 @@ var RadialGradientMaterial = function RadialGradientMaterial() { | |||
266 | colorStop2 = jObj.colorStop2, | 318 | colorStop2 = jObj.colorStop2, |
267 | colorStop3 = jObj.colorStop3, | 319 | colorStop3 = jObj.colorStop3, |
268 | colorStop4 = jObj.colorStop4; | 320 | colorStop4 = jObj.colorStop4; |
321 | this._textureTransform = jObj.textureTransform; | ||
269 | 322 | ||
270 | this.setProperty("color1", color1); | 323 | this.setProperty("color1", color1); |
271 | this.setProperty("color2", color2); | 324 | this.setProperty("color2", color2); |
@@ -316,7 +369,8 @@ var radialGradientMaterialDef = | |||
316 | 'u_colorStop1': { 'type': 'float' }, | 369 | 'u_colorStop1': { 'type': 'float' }, |
317 | 'u_colorStop2': { 'type': 'float' }, | 370 | 'u_colorStop2': { 'type': 'float' }, |
318 | 'u_colorStop3': { 'type': 'float' }, | 371 | 'u_colorStop3': { 'type': 'float' }, |
319 | 'u_colorStop4': { 'type': 'float' } | 372 | 'u_colorStop4': { 'type': 'float' }, |
373 | 'u_texTransform': { 'type' : 'mat3' } | ||
320 | //'u_colorCount': {'type' : 'int' } | 374 | //'u_colorCount': {'type' : 'int' } |
321 | }, | 375 | }, |
322 | 376 | ||