aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge/materials/uber-material.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib/rdge/materials/uber-material.js')
-rwxr-xr-xjs/lib/rdge/materials/uber-material.js185
1 files changed, 181 insertions, 4 deletions
diff --git a/js/lib/rdge/materials/uber-material.js b/js/lib/rdge/materials/uber-material.js
index 91f43754..0868e3e9 100755
--- a/js/lib/rdge/materials/uber-material.js
+++ b/js/lib/rdge/materials/uber-material.js
@@ -483,6 +483,187 @@ var UberMaterial = function UberMaterial() {
483 this.rebuildShader(); 483 this.rebuildShader();
484 } 484 }
485 485
486 this.importJSON = function( jObj )
487 {
488 if (this.getShaderName() != jObj.material) throw new Error( "ill-formed material" );
489 this.setName( jObj.name );
490
491 if (jObj.materialProps)
492 {
493 var ambientColor = jObj.materialProps.ambientColor; this.setProperty( "ambientColor", ambientColor );
494 var diffuseColor = jObj.materialProps.diffuseColor; this.setProperty( "diffuseColor", diffuseColor );
495 var specularColor = jObj.materialProps.specularColor; this.setProperty( "specularColor", specularColor );
496 var specularPower = jObj.materialProps.specularPower; this.setProperty( "specularPower", specularPower );
497 }
498
499 var lightArray = jObj.lights;
500 if (lightArray)
501 {
502 this._lights = [];
503 for (var i=0; i<this._MAX_LIGHTS; i++)
504 {
505 var lightObj = lightArray[i];
506 if (lightObj)
507 {
508 var type = lightObj['light'+i];
509 if (type)
510 {
511 var light = new Object;
512 switch (type)
513 {
514 case "directional":
515 light.direction = lightObj['light' + i + 'Dir'];
516 break;
517
518 case "spot":
519 light.position = lightObj['light' + i + 'Pos'];
520 light['spotInnerCutoff'] = lightObj['light' + i + 'OuterSpotCutoff'];
521 light['spotOuterCutoff'] = lightObj['light' + i + 'InnerSpotCutoff'];
522 break;
523
524 case "point":
525 light.position = lightObj['light' + i + 'Pos'];
526 light.attenuation = lightObj['light' + i + 'Attenuation'];
527 break;
528
529 default:
530 throw new Error( "unrecognized light type on import: " + type );
531 break;
532 }
533
534 // common to all lights
535 light.diffuseColor = lightObj['light' + i + 'Color'];
536 light.specularColor = lightObj['light' + i + 'SpecularColor'];
537
538 // push the light
539 this._lights.push( light );
540 }
541 else
542 this._lights[i] = 'undefined';
543 }
544 }
545
546 if (this._lights.length > 0)
547 {
548 this._ubershaderCaps.lighting =
549 {
550 'light0' : this._lights[0],
551 'light1' : this._lights[1],
552 'light2' : this._lights[2],
553 'light3' : this._lights[3]
554 }
555 }
556 }
557
558 var diffuseMap = jObj['diffuseMap'];
559 if(diffuseMap)
560 this.setProperty( "diffuseMap", diffuseMap );
561
562 var normalMap = jObj['normalMap'];
563 if(normalMap)
564 this.setProperty( "normalMap", normalMap );
565
566 var specularMap = jObj['specularMap'];
567 if(specularMap)
568 this.setProperty( "specularMap", specularMap );
569
570 var environmentMap = jObj['environmentMap'];
571 if(environmentMap)
572 {
573 this.setProperty( "environmentMap", environmentMap );
574 this.setProperty( "environmentAmount", jObj['environmentAmount'] );
575 }
576
577 this.rebuildShader();
578 }
579
580 this.exportJSON = function()
581 {
582 // we will be needing the world. Make sure it is there
583 var world = this.getWorld();
584 if (!world)
585 throw new Error( "no world in material.export, " + this.getName() );
586
587 // every material needs the base type and instance name
588 var caps = this._ubershaderCaps;
589 var jObj =
590 {
591 'material' : this.getShaderName(),
592 'name' : this.getName()
593 };
594
595 // export the material properties
596 if (typeof caps.material != 'undefined')
597 {
598 jObj.materialProps =
599 {
600 'ambientColor' : this._ambientColor,
601 'diffuseColor' : this._diffuseColor,
602 'specularColor' : this._specularColor,
603 'specularPower' : this._specularPower
604 };
605
606 }
607
608 if (typeof caps.lighting != 'undefined')
609 {
610 var lightArray = [];
611 for (var i=0; i<this._MAX_LIGHTS; i++)
612 {
613 var light = caps.lighting["light" + i];
614 if( typeof light != "undefined")
615 {
616 var lightObj = {}
617 lightObj['light'+i] = light.type;
618
619 // output the light secific data
620 if (light.type === 'directional')
621 {
622 lightObj['light' + i + 'Dir'] = light['direction'];
623 }
624 else if (light.type === 'spot')
625 {
626 lightObj['light' + i + 'Pos'] = light['position'];
627 lightObj['light' + i + 'SpotInnerCutoff'] = light['spotInnerCutoff'];
628 lightObj['light' + i + 'SpotOuterCutoff'] = light['spotOuterCutoff'];
629 }
630 else // light.type === 'point'
631 {
632 lightObj['light' + i + 'Pos'] = (light['position'] || [ 0, 0, 0 ]);
633 lightObj['light' + i + 'Attenuation'] = (light['attenuation'] || [ 1, 0, 0 ]);
634 }
635
636 // common to all lights
637 lightObj['light' + i + 'Color'] = (light['diffuseColor'] || [ 1,1,1,1 ]);
638 lightObj['light' + i + 'SpecularColor'] = (light['specularColor'] || [ 1, 1, 1, 1 ]);
639
640 lightArray.push( lightObj );
641 }
642 }
643
644 if (lightArray.length > 0)
645 jObj.lights = lightArray;
646 }
647
648 if(typeof caps.diffuseMap != 'undefined')
649 jObj['diffuseMap'] = caps.diffuseMap.texture;
650
651 if(typeof caps.normalMap != 'undefined')
652 jObj['normalMap'] = caps.normalMap.texture;
653
654 if(typeof caps.specularMap != 'undefined')
655 jObj['specularMap'] = caps.specularMap.texture;
656
657 if(typeof caps.environmentMap != 'undefined')
658 {
659 jObj['environmentMap'] = caps.environmentMap.texture;
660 jObj['environmentAmount'] = caps.environmentMap.envReflection;
661 }
662
663 return jObj;
664 }
665
666
486 this.export = function() 667 this.export = function()
487 { 668 {
488 // every material needs the base type and instance name 669 // every material needs the base type and instance name
@@ -539,10 +720,6 @@ var UberMaterial = function UberMaterial() {
539 } 720 }
540 } 721 }
541 722
542// this._diffuseMapOb = { 'texture' : 'assets/images/rocky-diffuse.jpg', 'wrap' : 'REPEAT' };
543// this._normalMapOb = { 'texture' : 'assets/images/rocky-normal.jpg', 'wrap' : 'REPEAT' };
544// this._specularMapOb = { 'texture' : 'assets/images/rocky-spec.jpg', 'wrap' : 'REPEAT' };
545// this._environmentMapOb = { 'texture' : 'assets/images/silver.png', 'wrap' : 'CLAMP', 'envReflection' : this._environmentAmount };
546 var world = this.getWorld(); 723 var world = this.getWorld();
547 if (!world) 724 if (!world)
548 throw new Error( "no world in material.export, " + this.getName() ); 725 throw new Error( "no world in material.export, " + this.getName() );