diff options
Diffstat (limited to 'js/lib/rdge/materials/uber-material.js')
-rwxr-xr-x | js/lib/rdge/materials/uber-material.js | 180 |
1 files changed, 176 insertions, 4 deletions
diff --git a/js/lib/rdge/materials/uber-material.js b/js/lib/rdge/materials/uber-material.js index 91f43754..c1d1913c 100755 --- a/js/lib/rdge/materials/uber-material.js +++ b/js/lib/rdge/materials/uber-material.js | |||
@@ -483,6 +483,182 @@ 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 | // every material needs the base type and instance name | ||
583 | var caps = this._ubershaderCaps; | ||
584 | var jObj = | ||
585 | { | ||
586 | 'material' : this.getShaderName(), | ||
587 | 'name' : this.getName() | ||
588 | }; | ||
589 | |||
590 | // export the material properties | ||
591 | if (typeof caps.material != 'undefined') | ||
592 | { | ||
593 | jObj.materialProps = | ||
594 | { | ||
595 | 'ambientColor' : this._ambientColor, | ||
596 | 'diffuseColor' : this._diffuseColor, | ||
597 | 'specularColor' : this._specularColor, | ||
598 | 'specularPower' : this._specularPower | ||
599 | }; | ||
600 | |||
601 | } | ||
602 | |||
603 | if (typeof caps.lighting != 'undefined') | ||
604 | { | ||
605 | var lightArray = []; | ||
606 | for (var i=0; i<this._MAX_LIGHTS; i++) | ||
607 | { | ||
608 | var light = caps.lighting["light" + i]; | ||
609 | if( typeof light != "undefined") | ||
610 | { | ||
611 | var lightObj = {} | ||
612 | lightObj['light'+i] = light.type; | ||
613 | |||
614 | // output the light secific data | ||
615 | if (light.type === 'directional') | ||
616 | { | ||
617 | lightObj['light' + i + 'Dir'] = light['direction']; | ||
618 | } | ||
619 | else if (light.type === 'spot') | ||
620 | { | ||
621 | lightObj['light' + i + 'Pos'] = light['position']; | ||
622 | lightObj['light' + i + 'SpotInnerCutoff'] = light['spotInnerCutoff']; | ||
623 | lightObj['light' + i + 'SpotOuterCutoff'] = light['spotOuterCutoff']; | ||
624 | } | ||
625 | else // light.type === 'point' | ||
626 | { | ||
627 | lightObj['light' + i + 'Pos'] = (light['position'] || [ 0, 0, 0 ]); | ||
628 | lightObj['light' + i + 'Attenuation'] = (light['attenuation'] || [ 1, 0, 0 ]); | ||
629 | } | ||
630 | |||
631 | // common to all lights | ||
632 | lightObj['light' + i + 'Color'] = (light['diffuseColor'] || [ 1,1,1,1 ]); | ||
633 | lightObj['light' + i + 'SpecularColor'] = (light['specularColor'] || [ 1, 1, 1, 1 ]); | ||
634 | |||
635 | lightArray.push( lightObj ); | ||
636 | } | ||
637 | } | ||
638 | |||
639 | if (lightArray.length > 0) | ||
640 | jObj.lights = lightArray; | ||
641 | } | ||
642 | |||
643 | if(typeof caps.diffuseMap != 'undefined') | ||
644 | jObj['diffuseMap'] = caps.diffuseMap.texture; | ||
645 | |||
646 | if(typeof caps.normalMap != 'undefined') | ||
647 | jObj['normalMap'] = caps.normalMap.texture; | ||
648 | |||
649 | if(typeof caps.specularMap != 'undefined') | ||
650 | jObj['specularMap'] = caps.specularMap.texture; | ||
651 | |||
652 | if(typeof caps.environmentMap != 'undefined') | ||
653 | { | ||
654 | jObj['environmentMap'] = caps.environmentMap.texture; | ||
655 | jObj['environmentAmount'] = caps.environmentMap.envReflection; | ||
656 | } | ||
657 | |||
658 | return jObj; | ||
659 | } | ||
660 | |||
661 | |||
486 | this.export = function() | 662 | this.export = function() |
487 | { | 663 | { |
488 | // every material needs the base type and instance name | 664 | // every material needs the base type and instance name |
@@ -539,10 +715,6 @@ var UberMaterial = function UberMaterial() { | |||
539 | } | 715 | } |
540 | } | 716 | } |
541 | 717 | ||
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(); | 718 | var world = this.getWorld(); |
547 | if (!world) | 719 | if (!world) |
548 | throw new Error( "no world in material.export, " + this.getName() ); | 720 | throw new Error( "no world in material.export, " + this.getName() ); |