diff options
Diffstat (limited to 'js/lib/rdge/materials/radial-gradient-material.js')
-rwxr-xr-x | js/lib/rdge/materials/radial-gradient-material.js | 126 |
1 files changed, 119 insertions, 7 deletions
diff --git a/js/lib/rdge/materials/radial-gradient-material.js b/js/lib/rdge/materials/radial-gradient-material.js index c9c2536f..6ce28ae4 100755 --- a/js/lib/rdge/materials/radial-gradient-material.js +++ b/js/lib/rdge/materials/radial-gradient-material.js | |||
@@ -1,7 +1,31 @@ | |||
1 | /* <copyright> | 1 | /* <copyright> |
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | 2 | Copyright (c) 2012, Motorola Mobility, Inc |
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | 3 | All Rights Reserved. |
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | 4 | BSD License. |
5 | |||
6 | Redistribution and use in source and binary forms, with or without | ||
7 | modification, are permitted provided that the following conditions are met: | ||
8 | |||
9 | - Redistributions of source code must retain the above copyright notice, | ||
10 | this list of conditions and the following disclaimer. | ||
11 | - Redistributions in binary form must reproduce the above copyright | ||
12 | notice, this list of conditions and the following disclaimer in the | ||
13 | documentation and/or other materials provided with the distribution. | ||
14 | - Neither the name of Motorola Mobility nor the names of its contributors | ||
15 | may be used to endorse or promote products derived from this software | ||
16 | without specific prior written permission. | ||
17 | |||
18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
22 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
23 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
24 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
25 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
26 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
27 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
28 | POSSIBILITY OF SUCH DAMAGE. | ||
5 | </copyright> */ | 29 | </copyright> */ |
6 | 30 | ||
7 | var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser; | 31 | var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser; |
@@ -75,9 +99,26 @@ var RadialGradientMaterial = function RadialGradientMaterial() { | |||
75 | this.setShaderValues(); | 99 | this.setShaderValues(); |
76 | }; | 100 | }; |
77 | 101 | ||
78 | this.fitToPrimitive = function( prim ) | 102 | this.resetToDefault = function() |
103 | { | ||
104 | this._propValues[this._propNames[0]] = this._defaultColor1.slice(0); | ||
105 | this._propValues[this._propNames[1]] = this._defaultColor2.slice(0); | ||
106 | this._propValues[this._propNames[2]] = this._defaultColor3.slice(0); | ||
107 | this._propValues[this._propNames[3]] = this._defaultColor4.slice(0); | ||
108 | |||
109 | this._propValues[this._propNames[4]] = this._defaultColorStop1; | ||
110 | this._propValues[this._propNames[5]] = this._defaultColorStop2; | ||
111 | this._propValues[this._propNames[6]] = this._defaultColorStop3; | ||
112 | this._propValues[this._propNames[7]] = this._defaultColorStop4; | ||
113 | |||
114 | var nProps = this._propNames.length; | ||
115 | for (var i=0; i<nProps; i++) { | ||
116 | this.setProperty( this._propNames[i], this._propValues[this._propNames[i]] ); | ||
117 | } | ||
118 | }; | ||
119 | |||
120 | this.fitToBounds = function( bounds ) | ||
79 | { | 121 | { |
80 | var bounds = ShapePrimitive.getBounds( prim ); | ||
81 | if (bounds) | 122 | if (bounds) |
82 | { | 123 | { |
83 | var dx = Math.abs( bounds[3] - bounds[0] ), | 124 | var dx = Math.abs( bounds[3] - bounds[0] ), |
@@ -109,11 +150,82 @@ var RadialGradientMaterial = function RadialGradientMaterial() { | |||
109 | } | 150 | } |
110 | }; | 151 | }; |
111 | 152 | ||
153 | this.fitToPrimitiveArray = function( primArray ) | ||
154 | { | ||
155 | if (!primArray) return; | ||
156 | var nPrims = primArray.length; | ||
157 | if (nPrims == 0) return; | ||
158 | var bounds = ShapePrimitive.getBounds( primArray[0] ); | ||
159 | for (var i=1; i<nPrims; i++) | ||
160 | { | ||
161 | var prim = primArray[i]; | ||
162 | var b2 = ShapePrimitive.getBounds( prim ); | ||
163 | |||
164 | // [xMin, yMin, zMin, xMax, yMax, zMax] | ||
165 | if (b2[0] < bounds[0]) bounds[0] = b2[0]; | ||
166 | if (b2[1] < bounds[1]) bounds[1] = b2[1]; | ||
167 | if (b2[2] < bounds[2]) bounds[2] = b2[2]; | ||
168 | |||
169 | if (b2[3] > bounds[3]) bounds[3] = b2[3]; | ||
170 | if (b2[4] > bounds[4]) bounds[4] = b2[4]; | ||
171 | if (b2[5] > bounds[5]) bounds[5] = b2[5]; | ||
172 | } | ||
173 | |||
174 | this.fitToBounds( bounds ); | ||
175 | }; | ||
176 | |||
177 | this.fitToPrimitive = function( prim ) | ||
178 | { | ||
179 | var bounds = ShapePrimitive.getBounds( prim ); | ||
180 | this.fitToBounds( bounds ); | ||
181 | }; | ||
182 | |||
112 | this.customExport = function( jObj ) | 183 | this.customExport = function( jObj ) |
113 | { | 184 | { |
114 | jObj.u_texTransform = this._textureTransform.slice(); | 185 | jObj.u_texTransform = this._textureTransform.slice(); |
115 | return jObj; | 186 | return jObj; |
116 | } | 187 | }; |
188 | |||
189 | // Only Linear Gradient and Radial Gradient have gradient data. | ||
190 | this.gradientType = "radial"; | ||
191 | |||
192 | this.getGradientData = function() { | ||
193 | var angle = Math.round(this._angle*180/Math.PI), | ||
194 | color, | ||
195 | colorStr, | ||
196 | css = "-webkit-gradient(linear, " + angle + "deg"; | ||
197 | |||
198 | // TODO - Angle is not supported in -webkit-gradient syntax, so just default to across: | ||
199 | css = '-webkit-radial-gradient(50% 50%, ellipse cover'; | ||
200 | |||
201 | // TODO - Also, Color Model requires from and to in the gradient string | ||
202 | for (var i=1; i < 5; i++) { | ||
203 | color = this.getProperty('u_color'+i); | ||
204 | colorStr = Math.round(color[0] * 255) + ', ' + Math.round(color[1] * 255) + ', ' + Math.round(color[2] * 255) + ', ' + Math.round(color[3] * 100); | ||
205 | css += ', rgba(' + colorStr + ') ' + Math.round(this.getProperty('u_colorStop'+i)*100) + '%'; | ||
206 | } | ||
207 | |||
208 | css += ')'; | ||
209 | |||
210 | return css; | ||
211 | }; | ||
212 | |||
213 | this.setGradientData = function(colors) { | ||
214 | var len = colors.length; | ||
215 | // TODO - Current shaders only support 4 color stops | ||
216 | if (len > 4) { | ||
217 | len = 4; | ||
218 | } | ||
219 | |||
220 | for (var n = 0; n < len; n++) { | ||
221 | var position = colors[n].position/100; | ||
222 | var cs = colors[n].value; | ||
223 | var stop = [cs.r/255, cs.g/255, cs.b/255, cs.a]; | ||
224 | |||
225 | this.setProperty("u_color" + (n + 1), stop.slice(0)); | ||
226 | this.setProperty("u_colorStop" + (n + 1), position); | ||
227 | } | ||
228 | }; | ||
117 | }; | 229 | }; |
118 | 230 | ||
119 | /////////////////////////////////////////////////////////////////////////////////////// | 231 | /////////////////////////////////////////////////////////////////////////////////////// |
@@ -170,4 +282,4 @@ RadialGradientMaterial.prototype = new Material(); | |||
170 | 282 | ||
171 | if (typeof exports === "object") { | 283 | if (typeof exports === "object") { |
172 | exports.RadialGradientMaterial = RadialGradientMaterial; | 284 | exports.RadialGradientMaterial = RadialGradientMaterial; |
173 | } \ No newline at end of file | 285 | } |