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.js269
1 files changed, 179 insertions, 90 deletions
diff --git a/js/lib/rdge/materials/radial-gradient-material.js b/js/lib/rdge/materials/radial-gradient-material.js
index 6a5a9e9a..f99b78de 100755
--- a/js/lib/rdge/materials/radial-gradient-material.js
+++ b/js/lib/rdge/materials/radial-gradient-material.js
@@ -1,24 +1,25 @@
1/* <copyright> 1/* <copyright>
2Copyright (c) 2012, Motorola Mobility, Inc 2Copyright (c) 2012, Motorola Mobility LLC.
3All Rights Reserved. 3All Rights Reserved.
4BSD License.
5 4
6Redistribution and use in source and binary forms, with or without 5Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met: 6modification, are permitted provided that the following conditions are met:
8 7
9 - Redistributions of source code must retain the above copyright notice, 8* Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer. 9 this list of conditions and the following disclaimer.
11 - Redistributions in binary form must reproduce the above copyright 10
12 notice, this list of conditions and the following disclaimer in the 11* Redistributions in binary form must reproduce the above copyright notice,
13 documentation and/or other materials provided with the distribution. 12 this list of conditions and the following disclaimer in the documentation
14 - Neither the name of Motorola Mobility nor the names of its contributors 13 and/or other materials provided with the distribution.
15 may be used to endorse or promote products derived from this software 14
16 without specific prior written permission. 15* Neither the name of Motorola Mobility LLC nor the names of its
16 contributors may be used to endorse or promote products derived from this
17 software without specific prior written permission.
17 18
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
@@ -47,17 +48,17 @@ var RadialGradientMaterial = function RadialGradientMaterial() {
47 this._defaultColorStop2 = 0.3; 48 this._defaultColorStop2 = 0.3;
48 this._defaultColorStop3 = 0.6; 49 this._defaultColorStop3 = 0.6;
49 this._defaultColorStop4 = 1.0; 50 this._defaultColorStop4 = 1.0;
50 // this._defaultColorCount = 4; 51 // this._defaultColorCount = 4;
51 52
52 this._textureTransform = [1,0,0, 0,1,0, 0,0,1]; 53 this._textureTransform = [1,0,0, 0,1,0, 0,0,1];
53 54
54 /////////////////////////////////////////////////////////////////////// 55 ///////////////////////////////////////////////////////////////////////
55 // Property Accessors 56 // Property Accessors
56 /////////////////////////////////////////////////////////////////////// 57 ///////////////////////////////////////////////////////////////////////
57
58 58
59 this.isAnimated = function () { return false; }; 59
60 this.getShaderDef = function() { return radialGradientMaterialDef; }; 60 this.isAnimated = function () { return false; };
61 this.getShaderDef = function() { return radialGradientMaterialDef; };
61 62
62 /////////////////////////////////////////////////////////////////////// 63 ///////////////////////////////////////////////////////////////////////
63 // Material Property Accessors 64 // Material Property Accessors
@@ -95,49 +96,137 @@ var RadialGradientMaterial = function RadialGradientMaterial() {
95 this._materialNode.setShader(this._shader); 96 this._materialNode.setShader(this._shader);
96 97
97 // set the shader values in the shader 98 // set the shader values in the shader
98 this._shader['default'].u_texTransform.set( this._textureTransform ); 99 this._shader['default'].u_texTransform.set( this._textureTransform );
99 this.setShaderValues(); 100 this.setShaderValues();
100 }; 101 };
101 102
102 this.fitToPrimitive = function( prim ) 103 this.resetToDefault = function()
104 {
105 this._propValues[this._propNames[0]] = this._defaultColor1.slice(0);
106 this._propValues[this._propNames[1]] = this._defaultColor2.slice(0);
107 this._propValues[this._propNames[2]] = this._defaultColor3.slice(0);
108 this._propValues[this._propNames[3]] = this._defaultColor4.slice(0);
109
110 this._propValues[this._propNames[4]] = this._defaultColorStop1;
111 this._propValues[this._propNames[5]] = this._defaultColorStop2;
112 this._propValues[this._propNames[6]] = this._defaultColorStop3;
113 this._propValues[this._propNames[7]] = this._defaultColorStop4;
114
115 var nProps = this._propNames.length;
116 for (var i=0; i<nProps; i++) {
117 this.setProperty( this._propNames[i], this._propValues[this._propNames[i]] );
118 }
119 };
120
121 this.fitToBounds = function( bounds )
103 { 122 {
104 var bounds = ShapePrimitive.getBounds( prim ); 123 if (bounds)
105 if (bounds) 124 {
125 var dx = Math.abs( bounds[3] - bounds[0] ),
126 dy = Math.abs( bounds[4] - bounds[1] );
127 if (dy == 0) dy = 1.0;
128 if (dx == 0) dx = 1.0;
129 var xScale = 2.0, yScale = 2.0;
130 if (dx > dy)
131 yScale *= dy/dx;
132 else
133 xScale *= dx/dy;
134
135 // build the matrix - the translation to the origin, the scale,
136 // and the translation back to the center (hard coded at (0.5, 0.5) for now).
137 // the matrix is build directly instead of with matrix multiplications
138 // for efficiency, not to mention that the multiplication function does
139 // not exist for mat3's.
140 // the matrix as laid out below looks transposed - order is columnwise.
141 var xCtr = 0.5, yCtr = 0.5;
142 this._textureTransform = [
143 xScale, 0.0, 0.0,
144 0.0, yScale, 0.0,
145 xCtr*(1-xScale), yCtr*(1 - yScale), 1.0
146 ];
147
148 if (this._shader && this._shader['default'])
149 this._shader['default'].u_texTransform.set( this._textureTransform );
150
151 }
152 };
153
154 this.fitToPrimitiveArray = function( primArray )
155 {
156 if (!primArray) return;
157 var nPrims = primArray.length;
158 if (nPrims == 0) return;
159 var bounds = ShapePrimitive.getBounds( primArray[0] );
160 for (var i=1; i<nPrims; i++)
106 { 161 {
107 var dx = Math.abs( bounds[3] - bounds[0] ), 162 var prim = primArray[i];
108 dy = Math.abs( bounds[4] - bounds[1] ); 163 var b2 = ShapePrimitive.getBounds( prim );
109 if (dy == 0) dy = 1.0; 164
110 if (dx == 0) dx = 1.0; 165 // [xMin, yMin, zMin, xMax, yMax, zMax]
111 var xScale = 2.0, yScale = 2.0; 166 if (b2[0] < bounds[0]) bounds[0] = b2[0];
112 if (dx > dy) 167 if (b2[1] < bounds[1]) bounds[1] = b2[1];
113 yScale *= dy/dx; 168 if (b2[2] < bounds[2]) bounds[2] = b2[2];
114 else
115 xScale *= dx/dy;
116
117 // build the matrix - the translation to the origin, the scale,
118 // and the translation back to the center (hard coded at (0.5, 0.5) for now).
119 // the matrix is build directly instead of with matrix multiplications
120 // for efficiency, not to mention that the multiplication function does
121 // not exist for mat3's.
122 // the matrix as laid out below looks transposed - order is columnwise.
123 var xCtr = 0.5, yCtr = 0.5;
124 this._textureTransform = [
125 xScale, 0.0, 0.0,
126 0.0, yScale, 0.0,
127 xCtr*(1-xScale), yCtr*(1 - yScale), 1.0
128 ];
129
130 if (this._shader && this._shader['default'])
131 this._shader['default'].u_texTransform.set( this._textureTransform );
132 169
170 if (b2[3] > bounds[3]) bounds[3] = b2[3];
171 if (b2[4] > bounds[4]) bounds[4] = b2[4];
172 if (b2[5] > bounds[5]) bounds[5] = b2[5];
133 } 173 }
174
175 this.fitToBounds( bounds );
134 }; 176 };
135 177
136 this.customExport = function( jObj ) 178 this.fitToPrimitive = function( prim )
137 { 179 {
138 jObj.u_texTransform = this._textureTransform.slice(); 180 var bounds = ShapePrimitive.getBounds( prim );
139 return jObj; 181 this.fitToBounds( bounds );
140 } 182 };
183
184 this.customExport = function( jObj )
185 {
186 jObj.u_texTransform = this._textureTransform.slice();
187 return jObj;
188 };
189
190 // Only Linear Gradient and Radial Gradient have gradient data.
191 this.gradientType = "radial";
192
193 this.getGradientData = function() {
194 var angle = Math.round(this._angle*180/Math.PI),
195 color,
196 colorStr,
197 css = "-webkit-gradient(linear, " + angle + "deg";
198
199 // TODO - Angle is not supported in -webkit-gradient syntax, so just default to across:
200 css = '-webk