aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge/materials/radial-gradient-material.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib/rdge/materials/radial-gradient-material.js')
-rwxr-xr-xjs/lib/rdge/materials/radial-gradient-material.js64
1 files changed, 59 insertions, 5 deletions
diff --git a/js/lib/rdge/materials/radial-gradient-material.js b/js/lib/rdge/materials/radial-gradient-material.js
index 083b7371..67a85041 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
7var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser; 7var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser;
8var Material = require("js/lib/rdge/materials/material").Material; 8var Material = require("js/lib/rdge/materials/material").Material;
9var ShapePrimitive = require("js/lib/geom/shape-primitive").ShapePrimitive;
9 10
10var RadialGradientMaterial = function RadialGradientMaterial() { 11var 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 ///////////////////////////////////////////////////////////////////////
@@ -191,9 +194,21 @@ var RadialGradientMaterial = function RadialGradientMaterial() {
191 // Methods 194 // Methods
192 /////////////////////////////////////////////////////////////////////// 195 ///////////////////////////////////////////////////////////////////////
193 // duplcate method requirde 196 // duplcate method requirde
194 this.dup = function () { 197 this.dup = function () {
195 return new RadialGradientMaterial(); 198 // allocate a new material
196 }; 199 var newMat = new RadialGradientMaterial();
200
201 // copy over the current values;
202 var propNames = [], propValues = [], propTypes = [], propLabels = [];
203 this.getAllProperties( propNames, propValues, propTypes, propLabels);
204 var n = propNames.length;
205 for (var i=0; i<n; i++) {
206 newMat.setProperty( propNames[i], propValues[i] );
207 }
208 newMat._textureTransform = this._textureTransform.slice();
209
210 return newMat;
211 };
197 212
198 this.init = function (world) { 213 this.init = function (world) {
199 this.setWorld(world); 214 this.setWorld(world);
@@ -234,9 +249,45 @@ var RadialGradientMaterial = function RadialGradientMaterial() {
234 this._shader['default'].u_colorStop3.set([s]); 249 this._shader['default'].u_colorStop3.set([s]);
235 s = this.getColorStop4(); 250 s = this.getColorStop4();
236 this._shader['default'].u_colorStop4.set([s]); 251 this._shader['default'].u_colorStop4.set([s]);
252
253 this._shader['default'].u_texTransform.set( this._textureTransform );
237 } 254 }
238 }; 255 };
239 256
257 this.fitToPrimitive = function( prim )
258 {
259 var bounds = ShapePrimitive.getBounds( prim );
260 if (bounds)
261 {
262 var dx = Math.abs( bounds[3] - bounds[0] ),
263 dy = Math.abs( bounds[4] - bounds[1] );
264 if (dy == 0) dy = 1.0;
265 if (dx == 0) dx = 1.0;
266 var xScale = 2.0, yScale = 2.0;
267 if (dx > dy)
268 yScale *= dy/dx;
269 else
270 xScale *= dx/dy;
271
272 // build the matrix - the translation to the origin, the scale,
273 // and the translation back to the center (hard coded at (0.5, 0.5) for now).
274 // the matrix is build directly instead of with matrix multiplications
275 // for efficiency, not to mention that the multiplication function does
276 // not exist for mat3's.
277 // the matrix as laid out below looks transposed - order is columnwise.
278 var xCtr = 0.5, yCtr = 0.5;
279 this._textureTransform = [
280 xScale, 0.0, 0.0,
281 0.0, yScale, 0.0,
282 xCtr*(1-xScale), yCtr*(1 - yScale), 1.0
283 ];
284
285 if (this._shader && this._shader['default'])
286 this._shader['default'].u_texTransform.set( this._textureTransform );
287
288 }
289 };
290
240 this.exportJSON = function () { 291 this.exportJSON = function () {
241 var jObj = 292 var jObj =
242 { 293 {
@@ -250,7 +301,8 @@ var RadialGradientMaterial = function RadialGradientMaterial() {
250 'colorStop1': this.getColorStop1(), 301 'colorStop1': this.getColorStop1(),
251 'colorStop2': this.getColorStop2(), 302 'colorStop2': this.getColorStop2(),
252 'colorStop3': this.getColorStop3(), 303 'colorStop3': this.getColorStop3(),
253 'colorStop4': this.getColorStop4() 304 'colorStop4': this.getColorStop4(),
305 'textureTransform': this._textureTransform
254 }; 306 };
255 307
256 return jObj; 308 return jObj;
@@ -269,6 +321,7 @@ var RadialGradientMaterial = function RadialGradientMaterial() {
269 colorStop2 = jObj.colorStop2, 321 colorStop2 = jObj.colorStop2,
270 colorStop3 = jObj.colorStop3, 322 colorStop3 = jObj.colorStop3,
271 colorStop4 = jObj.colorStop4; 323 colorStop4 = jObj.colorStop4;
324 this._textureTransform = jObj.textureTransform;
272 325
273 this.setProperty("color1", color1); 326 this.setProperty("color1", color1);
274 this.setProperty("color2", color2); 327 this.setProperty("color2", color2);
@@ -319,7 +372,8 @@ var radialGradientMaterialDef =
319 'u_colorStop1': { 'type': 'float' }, 372 'u_colorStop1': { 'type': 'float' },
320 'u_colorStop2': { 'type': 'float' }, 373 'u_colorStop2': { 'type': 'float' },
321 'u_colorStop3': { 'type': 'float' }, 374 'u_colorStop3': { 'type': 'float' },
322 'u_colorStop4': { 'type': 'float' } 375 'u_colorStop4': { 'type': 'float' },
376 'u_texTransform': { 'type' : 'mat3' }
323 //'u_colorCount': {'type' : 'int' } 377 //'u_colorCount': {'type' : 'int' }
324 }, 378 },
325 379