diff options
Diffstat (limited to 'js/lib/geom/geom-obj.js')
-rwxr-xr-x | js/lib/geom/geom-obj.js | 123 |
1 files changed, 103 insertions, 20 deletions
diff --git a/js/lib/geom/geom-obj.js b/js/lib/geom/geom-obj.js index c5880843..fb1cb246 100755 --- a/js/lib/geom/geom-obj.js +++ b/js/lib/geom/geom-obj.js | |||
@@ -138,22 +138,61 @@ var GeomObj = function GLGeomObj() { | |||
138 | // Methods | 138 | // Methods |
139 | /////////////////////////////////////////////////////////////////////// | 139 | /////////////////////////////////////////////////////////////////////// |
140 | this.setMaterialColor = function(c, type) { | 140 | this.setMaterialColor = function(c, type) { |
141 | if (type == "fill") { | 141 | var i = 0, |
142 | this._fillColor = c.slice(0); | 142 | nMats = 0; |
143 | if(c.gradientMode) { | ||
144 | // Gradient support | ||
145 | if (this._materialArray && this._materialTypeArray) { | ||
146 | nMats = this._materialArray.length; | ||
147 | } | ||
148 | |||
149 | var stops = [], | ||
150 | colors = c.color; | ||
151 | |||
152 | var len = colors.length; | ||
153 | // TODO - Current shaders only support 4 color stops | ||
154 | if(len > 4) { | ||
155 | len = 4; | ||
156 | } | ||
157 | |||
158 | for(var n=0; n<len; n++) { | ||
159 | var position = colors[n].position/100; | ||
160 | var cs = colors[n].value; | ||
161 | var stop = [cs.r/255, cs.g/255, cs.b/255, cs.a]; | ||
162 | stops.push(stop); | ||
163 | |||
164 | if (nMats === this._materialTypeArray.length) { | ||
165 | for (i=0; i<nMats; i++) { | ||
166 | if (this._materialTypeArray[i] == type) { | ||
167 | this._materialArray[i].setProperty( "color"+(n+1), stop.slice(0) ); | ||
168 | this._materialArray[i].setProperty( "colorStop"+(n+1), position ); | ||
169 | } | ||
170 | } | ||
171 | } | ||
172 | } | ||
173 | if (type === "fill") { | ||
174 | this._fillColor = c; | ||
175 | } else { | ||
176 | this._strokeColor = c; | ||
177 | } | ||
143 | } else { | 178 | } else { |
144 | this._strokeColor = c.slice(0); | 179 | if (type === "fill") { |
145 | } | 180 | this._fillColor = c.slice(0); |
181 | } else { | ||
182 | this._strokeColor = c.slice(0); | ||
183 | } | ||
146 | 184 | ||
147 | if (this._materialArray && this._materialTypeArray) { | 185 | if (this._materialArray && this._materialTypeArray) { |
148 | var nMats = this._materialArray.length; | 186 | nMats = this._materialArray.length; |
149 | if (nMats === this._materialTypeArray.length) { | 187 | if (nMats === this._materialTypeArray.length) { |
150 | for (var i=0; i<nMats; i++) { | 188 | for (i=0; i<nMats; i++) { |
151 | if (this._materialTypeArray[i] == type) { | 189 | if (this._materialTypeArray[i] == type) { |
152 | this._materialArray[i].setProperty( "color", c.slice(0) ); | 190 | this._materialArray[i].setProperty( "color", c.slice(0) ); |
191 | } | ||
153 | } | 192 | } |
154 | } | 193 | } |
155 | } | 194 | } |
156 | } | 195 | } |
157 | 196 | ||
158 | var world = this.getWorld(); | 197 | var world = this.getWorld(); |
159 | if (world) { | 198 | if (world) { |
@@ -171,14 +210,15 @@ var GeomObj = function GLGeomObj() { | |||
171 | 210 | ||
172 | if (strokeMaterial) { | 211 | if (strokeMaterial) { |
173 | strokeMaterial.init( this.getWorld() ); | 212 | strokeMaterial.init( this.getWorld() ); |
174 | if(this._strokeColor) { | ||
175 | strokeMaterial.setProperty("color", this._strokeColor); | ||
176 | } | ||
177 | } | 213 | } |
178 | 214 | ||
179 | this._materialArray.push( strokeMaterial ); | 215 | this._materialArray.push( strokeMaterial ); |
180 | this._materialTypeArray.push( "stroke" ); | 216 | this._materialTypeArray.push( "stroke" ); |
181 | 217 | ||
218 | if(this._strokeColor) { | ||
219 | this.setStrokeColor(this._strokeColor); | ||
220 | } | ||
221 | |||
182 | return strokeMaterial; | 222 | return strokeMaterial; |
183 | }; | 223 | }; |
184 | 224 | ||
@@ -192,15 +232,15 @@ var GeomObj = function GLGeomObj() { | |||
192 | 232 | ||
193 | if (fillMaterial) { | 233 | if (fillMaterial) { |
194 | fillMaterial.init( this.getWorld() ); | 234 | fillMaterial.init( this.getWorld() ); |
195 | //if(!this.getFillMaterial() && this._fillColor) | ||
196 | if (this._fillColor) { | ||
197 | fillMaterial.setProperty("color", this._fillColor); | ||
198 | } | ||
199 | } | 235 | } |
200 | 236 | ||
201 | this._materialArray.push( fillMaterial ); | 237 | this._materialArray.push( fillMaterial ); |
202 | this._materialTypeArray.push( "fill" ); | 238 | this._materialTypeArray.push( "fill" ); |
203 | 239 | ||
240 | if (this._fillColor) { | ||
241 | this.setFillColor(this._fillColor); | ||
242 | } | ||
243 | |||
204 | return fillMaterial; | 244 | return fillMaterial; |
205 | }; | 245 | }; |
206 | 246 | ||
@@ -265,6 +305,49 @@ var GeomObj = function GLGeomObj() { | |||
265 | return rtnStr; | 305 | return rtnStr; |
266 | }; | 306 | }; |
267 | 307 | ||
308 | // Gradient stops for rgba(255,0,0,1) at 0%; rgba(0,255,0,1) at 33%; rgba(0,0,255,1) at 100% will return | ||
309 | // 255,0,0,1@0;0,255,0,1@33;0,0,255,1@100 | ||
310 | this.gradientToString = function(colors) { | ||
311 | var rtnStr = ""; | ||
312 | if(colors && colors.length) { | ||
313 | var c = colors[0], | ||
314 | len = colors.length; | ||
315 | |||
316 | rtnStr += String(c.value.r + "," + c.value.g + "," + c.value.b + "," + c.value.a + "@" + c.position); | ||
317 | for(var i=1; i<len; i++) { | ||
318 | c = colors[i]; | ||
319 | rtnStr += ";" + String(c.value.r + "," + c.value.g + "," + c.value.b + "," + c.value.a + "@" + c.position); | ||
320 | } | ||
321 | } | ||
322 | return rtnStr; | ||
323 | }; | ||
324 | |||
325 | // Given a gradientStr "255,0,0,1@0;0,255,0,1@33;0,0,255,1@100" will return: | ||
326 | // colors array [{position:0, value:{r:255, g:0, b:0, a:1}}, | ||
327 | // {position:33, value:{r:0, g:255, b:0, a:1}}, | ||
328 | // {position:100, value:{r:0, g:0, b:255, a:1}} | ||
329 | // ] | ||
330 | this.stringToGradient = function(gradientStr) { | ||
331 | var rtnArr = []; | ||
332 | |||
333 | var i, | ||
334 | len, | ||
335 | stops, | ||
336 | stop, | ||
337 | c; | ||
338 | |||
339 | stops = gradientStr.split(";"); | ||
340 | len = stops.length; | ||
341 | for(i=0; i<len; i++) | ||
342 | { | ||
343 | stop = stops[i].split("@"); | ||
344 | c = stop[0].split(","); | ||
345 | rtnArr.push({ position: Number(stop[1]), value:{r:Number(c[0]), g:Number(c[1]), b:Number(c[2]), a:Number(c[3])} }); | ||
346 | } | ||
347 | |||
348 | return rtnArr; | ||
349 | }; | ||
350 | |||
268 | /* | 351 | /* |
269 | this.export = function() { | 352 | this.export = function() { |
270 | var rtnStr; | 353 | var rtnStr; |