diff options
Diffstat (limited to 'js/lib/rdge/materials/uber-material.js')
-rwxr-xr-x | js/lib/rdge/materials/uber-material.js | 389 |
1 files changed, 388 insertions, 1 deletions
diff --git a/js/lib/rdge/materials/uber-material.js b/js/lib/rdge/materials/uber-material.js index e2f86511..4ff3676a 100755 --- a/js/lib/rdge/materials/uber-material.js +++ b/js/lib/rdge/materials/uber-material.js | |||
@@ -4,6 +4,7 @@ | |||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | 4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. |
5 | </copyright> */ | 5 | </copyright> */ |
6 | 6 | ||
7 | var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser; | ||
7 | var Material = require("js/lib/rdge/materials/material").Material; | 8 | var Material = require("js/lib/rdge/materials/material").Material; |
8 | 9 | ||
9 | var UberMaterial = function UberMaterial() { | 10 | var UberMaterial = function UberMaterial() { |
@@ -33,6 +34,8 @@ var UberMaterial = function UberMaterial() { | |||
33 | this._useEnvironmentMap = true; | 34 | this._useEnvironmentMap = true; |
34 | this._useLights = [true, true, true, true]; | 35 | this._useLights = [true, true, true, true]; |
35 | 36 | ||
37 | this._MAX_LIGHTS = 4; | ||
38 | |||
36 | /////////////////////////////////////////////////////////////////////// | 39 | /////////////////////////////////////////////////////////////////////// |
37 | // Material Property Accessors | 40 | // Material Property Accessors |
38 | /////////////////////////////////////////////////////////////////////// | 41 | /////////////////////////////////////////////////////////////////////// |
@@ -363,10 +366,394 @@ var UberMaterial = function UberMaterial() { | |||
363 | this._shader = this.buildUberShader( this._ubershaderCaps ); | 366 | this._shader = this.buildUberShader( this._ubershaderCaps ); |
364 | 367 | ||
365 | // set up the material node | 368 | // set up the material node |
366 | this._materialNode = RDGE.createMaterialNode("uberMaterial"); | 369 | this._materialNode = RDGE.createMaterialNode("uberMaterial" + "_" + world.generateUniqueNodeID()); |
367 | this._materialNode.setShader(this._shader); | 370 | this._materialNode.setShader(this._shader); |
368 | }; | 371 | }; |
369 | 372 | ||
373 | this.import = function( importStr ) | ||
374 | { | ||
375 | // limit the key searches to this material | ||
376 | var endKey = "endMaterial\n"; | ||
377 | var index = importStr.indexOf( endKey ); | ||
378 | index += endKey.length; | ||
379 | importStr = importStr.slice( 0, index ); | ||
380 | var pu = new MaterialParser( importStr ); | ||
381 | |||
382 | var matProps = pu.nextValue( "materialProps: " ); | ||
383 | if (matProps) | ||
384 | { | ||
385 | var ambientColor = eval( "[" + pu.nextValue("ambientColor: ") + ']' ); this.setProperty( "ambientColor", ambientColor ); | ||
386 | var diffuseColor = eval( "[" + pu.nextValue( "diffuseColor: ") + ']' ); this.setProperty( "diffuseColor", diffuseColor ); | ||
387 | var specularColor = eval( "[" + pu.nextValue( "specularColor: ") + ']' ); this.setProperty( "specularColor", specularColor ); | ||
388 | var specularPower = eval( "[" + pu.nextValue( "specularPower: ") + ']' ); this.setProperty( "specularPower", specularPower ); | ||
389 | } | ||
390 | |||
391 | var lightProps = pu.nextValue( "lightProps: " ); | ||
392 | if (lightProps) | ||
393 | { | ||
394 | this._lights = []; | ||
395 | var lightStr; | ||
396 | for (var i=0; i<this._MAX_LIGHTS; i++) | ||
397 | { | ||
398 | var type = pu.nextValue( "light" + i + ": " ); | ||
399 | if (type) | ||
400 | { | ||
401 | var light = new Object; | ||
402 | switch (type) | ||
403 | { | ||
404 | case "directional": | ||
405 | lightStr = pu.nextValue( 'light' + i + 'Dir: '); | ||
406 | light.direction = eval( "[" + lightStr + "]" ); | ||
407 | break; | ||
408 | |||
409 | case "spot": | ||
410 | lightStr = pu.nextValue( 'light' + i + 'Pos: ' ); | ||
411 | light.position = eval( "[" + lightStr + "]" ); | ||
412 | |||
413 | lightStr = pu.nextValue( 'light' + i + 'OuterSpotCutoff: ' ); | ||
414 | light['spotInnerCutoff'] = Number( lightStr ); | ||
415 | |||
416 | lightStr = pu.nextValue( 'light' + i + 'InnerSpotCutoff: ' ); | ||
417 | light['spotOuterCutoff'] = Number( lightStr ); | ||
418 | break; | ||
419 | |||
420 | case "point": | ||
421 | lightStr = pu.nextValue( 'light' + i + 'Pos: ' ); | ||
422 | light.position = eval( "[" + lightStr + "]" ); | ||
423 | |||
424 | lightStr = pu.nextValue( 'light' + i + 'Attenuation: ' ); | ||
425 | light.attenuation = eval( "[" + lightStr + "]" ); | ||
426 | break; | ||
427 | |||
428 | default: | ||
429 | throw new Error( "unrecognized light type on import: " + type ); | ||
430 | break; | ||
431 | } | ||
432 | |||
433 | // common to all lights | ||
434 | light.diffuseColor = eval( "[" + pu.nextValue( 'light' + i + 'Color: ') + "]" ); | ||
435 | light.specularColor = eval( "[" + pu.nextValue( 'light' + i + 'SpecularColor: ') + "]" ); | ||
436 | |||
437 | // push the light | ||
438 | this._lights.push( light ); | ||
439 | } | ||
440 | else { | ||
441 | this._lights[i] = 'undefined'; | ||
442 | } | ||
443 | |||
444 | // advance to the next light | ||
445 | var endLightKey = "endMaterial\n"; | ||
446 | index = importStr.indexOf( endLightKey ); | ||
447 | if (index < 0) throw new Error( "ill-formed light encountered in import" ); | ||
448 | index += endLightKey.length; | ||
449 | importStr = importStr.slice( 0, index ); | ||
450 | |||
451 | } | ||
452 | |||
453 | if (this._lights.length > 0) | ||
454 | { | ||
455 | this._ubershaderCaps.lighting = | ||
456 | { | ||
457 | 'light0' : this._lights[0], | ||
458 | 'light1' : this._lights[1], | ||
459 | 'light2' : this._lights[2], | ||
460 | 'light3' : this._lights[3] | ||
461 | } | ||
462 | } | ||
463 | } | ||
464 | |||
465 | var diffuseMap = pu.nextValue( "diffuseMap: " ) | ||
466 | if(diffuseMap) { | ||
467 | this.setProperty( "diffuseMap", diffuseMap ); | ||
468 | } | ||
469 | |||
470 | var normalMap = pu.nextValue( "normalMap: " ); | ||
471 | if(normalMap) { | ||
472 | this.setProperty( "normalMap", normalMap ); | ||
473 | } | ||
474 | |||
475 | var specularMap = pu.nextValue( "specularMap: " ); | ||
476 | if(specularMap) { | ||
477 | this.setProperty( "specularMap", specularMap ); | ||
478 | } | ||
479 | |||
480 | var environmentMap = pu.nextValue( "environmentMap: " ); | ||
481 | if(environmentMap) | ||
482 | { | ||
483 | this.setProperty( "environmentMap", environmentMap ); | ||
484 | this.setProperty( "environmentAmount", Number( pu.nextValue( "environmentAmount" ) ) ); | ||
485 | } | ||
486 | |||
487 | this.rebuildShader(); | ||
488 | }; | ||
489 | |||
490 | this.importJSON = function( jObj ) | ||
491 | { | ||
492 | if (this.getShaderName() != jObj.material) throw new Error( "ill-formed material" ); | ||
493 | this.setName( jObj.name ); | ||
494 | |||
495 | if (jObj.materialProps) | ||
496 | { | ||
497 | var ambientColor = jObj.materialProps.ambientColor; this.setProperty( "ambientColor", ambientColor ); | ||
498 | var diffuseColor = jObj.materialProps.diffuseColor; this.setProperty( "diffuseColor", diffuseColor ); | ||
499 | var specularColor = jObj.materialProps.specularColor; this.setProperty( "specularColor", specularColor ); | ||
500 | var specularPower = jObj.materialProps.specularPower; this.setProperty( "specularPower", specularPower ); | ||
501 | } | ||
502 | |||
503 | var lightArray = jObj.lights; | ||
504 | if (lightArray) | ||
505 | { | ||
506 | this._lights = []; | ||
507 | for (var i=0; i<this._MAX_LIGHTS; i++) | ||
508 | { | ||
509 | var lightObj = lightArray[i]; | ||
510 | if (lightObj) | ||
511 | { | ||
512 | var type = lightObj['light'+i]; | ||
513 | if (type) | ||
514 | { | ||
515 | var light = new Object; | ||
516 | switch (type) | ||
517 | { | ||
518 | case "directional": | ||
519 | light.direction = lightObj['light' + i + 'Dir']; | ||
520 | break; | ||
521 | |||
522 | case "spot": | ||
523 | light.position = lightObj['light' + i + 'Pos']; | ||
524 | light['spotInnerCutoff'] = lightObj['light' + i + 'OuterSpotCutoff']; | ||
525 | light['spotOuterCutoff'] = lightObj['light' + i + 'InnerSpotCutoff']; | ||
526 | break; | ||
527 | |||
528 | case "point": | ||
529 | light.position = lightObj['light' + i + 'Pos']; | ||
530 | light.attenuation = lightObj['light' + i + 'Attenuation']; | ||
531 | break; | ||
532 | |||
533 | default: | ||
534 | throw new Error( "unrecognized light type on import: " + type ); | ||
535 | break; | ||
536 | } | ||
537 | |||
538 | // common to all lights | ||
539 | light.diffuseColor = lightObj['light' + i + 'Color']; | ||
540 | light.specularColor = lightObj['light' + i + 'SpecularColor']; | ||
541 | |||
542 | // push the light | ||
543 | this._lights.push( light ); | ||
544 | } | ||
545 | else | ||
546 | this._lights[i] = 'undefined'; | ||
547 | } | ||
548 | } | ||
549 | |||
550 | if (this._lights.length > 0) | ||
551 | { | ||