diff options
Diffstat (limited to 'js/lib/geom/geom-obj.js')
-rwxr-xr-x | js/lib/geom/geom-obj.js | 289 |
1 files changed, 269 insertions, 20 deletions
diff --git a/js/lib/geom/geom-obj.js b/js/lib/geom/geom-obj.js index c5880843..f1aa9f8a 100755 --- a/js/lib/geom/geom-obj.js +++ b/js/lib/geom/geom-obj.js | |||
@@ -5,6 +5,7 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot | |||
5 | </copyright> */ | 5 | </copyright> */ |
6 | 6 | ||
7 | var MaterialsModel = require("js/models/materials-model").MaterialsModel; | 7 | var MaterialsModel = require("js/models/materials-model").MaterialsModel; |
8 | |||
8 | /////////////////////////////////////////////////////////////////////// | 9 | /////////////////////////////////////////////////////////////////////// |
9 | // Class GLGeomObj | 10 | // Class GLGeomObj |
10 | // Super class for all geometry classes | 11 | // Super class for all geometry classes |
@@ -18,6 +19,7 @@ var GeomObj = function GLGeomObj() { | |||
18 | this.GEOM_TYPE_LINE = 3; | 19 | this.GEOM_TYPE_LINE = 3; |
19 | this.GEOM_TYPE_PATH = 4; | 20 | this.GEOM_TYPE_PATH = 4; |
20 | this.GEOM_TYPE_CUBIC_BEZIER = 5; | 21 | this.GEOM_TYPE_CUBIC_BEZIER = 5; |
22 | this.GEOM_TYPE_BRUSH_STROKE = 6; | ||
21 | this.GEOM_TYPE_UNDEFINED = -1; | 23 | this.GEOM_TYPE_UNDEFINED = -1; |
22 | 24 | ||
23 | // Needed for calculating dashed/dotted strokes | 25 | // Needed for calculating dashed/dotted strokes |
@@ -138,22 +140,61 @@ var GeomObj = function GLGeomObj() { | |||
138 | // Methods | 140 | // Methods |
139 | /////////////////////////////////////////////////////////////////////// | 141 | /////////////////////////////////////////////////////////////////////// |
140 | this.setMaterialColor = function(c, type) { | 142 | this.setMaterialColor = function(c, type) { |
141 | if (type == "fill") { | 143 | var i = 0, |
142 | this._fillColor = c.slice(0); | 144 | nMats = 0; |
145 | if(c.gradientMode) { | ||
146 | // Gradient support | ||
147 | if (this._materialArray && this._materialTypeArray) { | ||
148 | nMats = this._materialArray.length; | ||
149 | } | ||
150 | |||
151 | var stops = [], | ||
152 | colors = c.color; | ||
153 | |||
154 | var len = colors.length; | ||
155 | // TODO - Current shaders only support 4 color stops | ||
156 | if(len > 4) { | ||
157 | len = 4; | ||
158 | } | ||
159 | |||
160 | for(var n=0; n<len; n++) { | ||
161 | var position = colors[n].position/100; | ||
162 | var cs = colors[n].value; | ||
163 | var stop = [cs.r/255, cs.g/255, cs.b/255, cs.a]; | ||
164 | stops.push(stop); | ||
165 | |||
166 | if (nMats === this._materialTypeArray.length) { | ||
167 | for (i=0; i<nMats; i++) { | ||
168 | if (this._materialTypeArray[i] == type) { | ||
169 | this._materialArray[i].setProperty( "color"+(n+1), stop.slice(0) ); | ||
170 | this._materialArray[i].setProperty( "colorStop"+(n+1), position ); | ||
171 | } | ||
172 | } | ||
173 | } | ||
174 | } | ||
175 | if (type === "fill") { | ||
176 | this._fillColor = c; | ||
177 | } else { | ||
178 | this._strokeColor = c; | ||
179 | } | ||
143 | } else { | 180 | } else { |
144 | this._strokeColor = c.slice(0); | 181 | if (type === "fill") { |
145 | } | 182 | this._fillColor = c.slice(0); |
183 | } else { | ||
184 | this._strokeColor = c.slice(0); | ||
185 | } | ||
146 | 186 | ||
147 | if (this._materialArray && this._materialTypeArray) { | 187 | if (this._materialArray && this._materialTypeArray) { |
148 | var nMats = this._materialArray.length; | 188 | nMats = this._materialArray.length; |
149 | if (nMats === this._materialTypeArray.length) { | 189 | if (nMats === this._materialTypeArray.length) { |
150 | for (var i=0; i<nMats; i++) { | 190 | for (i=0; i<nMats; i++) { |
151 | if (this._materialTypeArray[i] == type) { | 191 | if (this._materialTypeArray[i] == type) { |
152 | this._materialArray[i].setProperty( "color", c.slice(0) ); | 192 | this._materialArray[i].setProperty( "color", c.slice(0) ); |
193 | } | ||
153 | } | 194 | } |
154 | } | 195 | } |
155 | } | 196 | } |
156 | } | 197 | } |
157 | 198 | ||
158 | var world = this.getWorld(); | 199 | var world = this.getWorld(); |
159 | if (world) { | 200 | if (world) { |
@@ -171,14 +212,15 @@ var GeomObj = function GLGeomObj() { | |||
171 | 212 | ||
172 | if (strokeMaterial) { | 213 | if (strokeMaterial) { |
173 | strokeMaterial.init( this.getWorld() ); | 214 | strokeMaterial.init( this.getWorld() ); |
174 | if(this._strokeColor) { | ||
175 | strokeMaterial.setProperty("color", this._strokeColor); | ||
176 | } | ||
177 | } | 215 | } |
178 | 216 | ||
179 | this._materialArray.push( strokeMaterial ); | 217 | this._materialArray.push( strokeMaterial ); |
180 | this._materialTypeArray.push( "stroke" ); | 218 | this._materialTypeArray.push( "stroke" ); |
181 | 219 | ||
220 | if(this._strokeColor) { | ||
221 | this.setStrokeColor(this._strokeColor); | ||
222 | } | ||
223 | |||
182 | return strokeMaterial; | 224 | return strokeMaterial; |
183 | }; | 225 | }; |
184 | 226 | ||
@@ -192,18 +234,182 @@ var GeomObj = function GLGeomObj() { | |||
192 | 234 | ||
193 | if (fillMaterial) { | 235 | if (fillMaterial) { |
194 | fillMaterial.init( this.getWorld() ); | 236 | fillMaterial.init( this.getWorld() ); |
195 | //if(!this.getFillMaterial() && this._fillColor) | ||
196 | if (this._fillColor) { | ||
197 | fillMaterial.setProperty("color", this._fillColor); | ||
198 | } | ||
199 | } | 237 | } |
200 | 238 | ||
201 | this._materialArray.push( fillMaterial ); | 239 | this._materialArray.push( fillMaterial ); |
202 | this._materialTypeArray.push( "fill" ); | 240 | this._materialTypeArray.push( "fill" ); |
203 | 241 | ||
242 | if (this._fillColor) { | ||
243 | this.setFillColor(this._fillColor); | ||
244 | } | ||
245 | |||
204 | return fillMaterial; | 246 | return fillMaterial; |
205 | }; | 247 | }; |
206 | 248 | ||
249 | this.exportMaterialsJSON = function() | ||
250 | { | ||
251 | var jObj; | ||
252 | if (this._materialArray && this._materialNodeArray && this.getWorld().isWebGL()) | ||
253 | { | ||
254 | var nMats = this._materialArray.length; | ||
255 | if (nMats > 0) | ||
256 | { | ||
257 | var arr = []; | ||
258 | |||
259 | for (var i=0; i<nMats; i++) | ||
260 | { | ||
261 | var matObj = | ||
262 | { | ||
263 | 'materialNodeName' : this._materialNodeArray[i].name, | ||
264 | 'material' : this._materialArray[i].exportJSON(), | ||
265 | 'type' : this._materialTypeArray[i] | ||
266 | } | ||
267 | arr.push( matObj ); | ||
268 | } | ||
269 | |||
270 | jObj = | ||
271 | { | ||
272 | 'nMaterials' : nMats, | ||
273 | 'materials' : arr | ||
274 | }; | ||
275 | } | ||
276 | } | ||
277 | |||
278 | return jObj; | ||
279 | } | ||
280 | |||
281 | this.importMaterialsJSON = function( jObj ) | ||
282 | { | ||
283 | this._materialArray = []; | ||
284 | this._materialTypeArray = []; | ||
285 | |||
286 | if (!jObj) return; | ||
287 | |||
288 | var nMaterials = jObj.nMaterials; | ||
289 | var matArray = jObj.materials; | ||
290 | for (var i=0; i<nMaterials; i++) | ||
291 | { | ||
292 | var mat; | ||
293 | var matObj = matArray[i].material; | ||
294 | var shaderName = matObj.material; | ||
295 | switch (shaderName) | ||
296 | { | ||
297 | case "flat": | ||
298 | case "radialGradient": | ||
299 | case "linearGradient": | ||
300 | case "bumpMetal": | ||
301 | case "uber": | ||
302 | case "plasma": | ||
303 | case "deform": | ||
304 | case "water": | ||
305 | case "paris": | ||
306 | case "raiders": | ||
307 | case "tunnel": | ||
308 | case "reliefTunnel": | ||
309 | case "squareTunnel": | ||
310 | case "twist": | ||
311 | case "fly": | ||
312 | case "julia": | ||
313 | case "mandel": | ||
314 | case "star": | ||
315 | case "zinvert": | ||
316 | case "keleidoscope": | ||
317 | case "radialBlur": | ||
318 | case "pulse": | ||
319 | mat = MaterialsModel.getMaterialByShader( shaderName ); | ||
320 | if (mat) mat = mat.dup(); | ||
321 | break; | ||
322 | |||
323 | default: | ||
324 | console.log( "material type: " + shaderName + " is not supported" ); | ||
325 | break; | ||
326 | } | ||
327 | |||
328 | if (mat) | ||
329 | { | ||
330 | mat.importJSON( matObj ); | ||
331 | this._materialArray.push( mat ); | ||
332 | this._materialTypeArray.push( matObj.type ); | ||
333 | var type = matArray[i].type; | ||
334 | if (type == "fill") this._fillMaterial = mat; | ||
335 | else this._strokeMaterial = mat; | ||
336 | } | ||
337 | } | ||
338 | } | ||
339 | |||
340 | this.exportMaterials = function() | ||
341 | { | ||
342 | var rtnStr = ""; | ||
343 | if (this._materialArray && this._materialNodeArray) | ||
344 | { | ||
345 | var nMats = this._materialArray.length; | ||
346 | rtnStr += "nMaterials: " + nMats + "\n"; | ||