From 818582d389f504c915be0c9052fafa33e3e76c92 Mon Sep 17 00:00:00 2001 From: hwc487 Date: Wed, 7 Mar 2012 16:48:48 -0800 Subject: File IO --- js/lib/rdge/materials/uber-material.js | 71 +++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) (limited to 'js/lib/rdge/materials/uber-material.js') diff --git a/js/lib/rdge/materials/uber-material.js b/js/lib/rdge/materials/uber-material.js index 6bc35d51..0964c9c4 100755 --- a/js/lib/rdge/materials/uber-material.js +++ b/js/lib/rdge/materials/uber-material.js @@ -363,10 +363,79 @@ var UberMaterial = function UberMaterial() { this._shader = this.buildUberShader( this._ubershaderCaps ); // set up the material node - this._materialNode = createMaterialNode("uberMaterial"); + this._materialNode = createMaterialNode("uberMaterial" + "_" + world.generateUniqueNodeID()); this._materialNode.setShader(this._shader); }; + this.import = function( importStr ) + { + // limit the key searches to this material + var endKey = "endMaterial\n"; + var index = importStr.indexOf( endKey ); + index += endKey.length; + importStr = importStr.substr( index ); + } + + this.export = function() + { + // every material needs the base type and instance name + var exportStr = "material: " + this.getShaderName() + "\n"; + exportStr += "name: " + this.getName() + "\n"; + + var caps = this._ubershaderCaps; + + // export the material properties + if (typeof caps.material != 'undefined') + { + exportStr += "material: true\n"; + exportStr += "ambientColor: " + caps.material.ambientColor + "\n"; + exportStr += "diffuseColor: " + caps.material.diffuseColor + "\n"; + exportStr += "specularColor: " + caps.material.specularColor + "\n"; + exportStr += "specularPower: " + caps.material.specularPower + "\n"; + } + + if (typeof caps.lighting != 'undefined') + { + var light = caps.lighting['light' + i]; + var t; + for (var i=0; i */ +var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser; var Material = require("js/lib/rdge/materials/material").Material; var UberMaterial = function UberMaterial() { @@ -375,19 +376,111 @@ var UberMaterial = function UberMaterial() { var endKey = "endMaterial\n"; var index = importStr.indexOf( endKey ); index += endKey.length; - importStr = importStr.substr( index ); + importStr = importStr.slice( 0, index ); var pu = new MaterialParser( importStr ); var matProps = pu.nextValue( "materialProps: " ); if (matProps) { - var ambientColor = Number( pu.nextValue( "ambientColor: " )); this.setProperty( "ambientColor", ambientColor ); - var diffuseColor = Number( pu.nextValue( "diffuseColor: " )); this.setProperty( "diffuseColor", diffuseColor ); - var specularColor = Number( pu.nextValue( "specularColor: " )); this.setProperty( "specularColor", specularColor ); - var specularPower = Number( pu.nextValue( "specularPower: " )); this.setProperty( "specularPower", specularPower ); + var ambientColor = eval( "[" + pu.nextValue("ambientColor: ") + ']' ); this.setProperty( "ambientColor", ambientColor ); + var diffuseColor = eval( "[" + pu.nextValue( "diffuseColor: ") + ']' ); this.setProperty( "diffuseColor", diffuseColor ); + var specularColor = eval( "[" + pu.nextValue( "specularColor: ") + ']' ); this.setProperty( "specularColor", specularColor ); + var specularPower = eval( "[" + pu.nextValue( "specularPower: ") + ']' ); this.setProperty( "specularPower", specularPower ); } - var lightProps = pu.nextValue( "theLights" ); + var lightProps = pu.nextValue( "lightProps: " ); + if (lightProps) + { + this._lights = []; + var lightStr; + for (var i=0; i 0) + { + this._ubershaderCaps.lighting = + { + 'light0' : this._lights[0], + 'light1' : this._lights[1], + 'light2' : this._lights[2], + 'light3' : this._lights[3] + } + } + } + + var diffuseMap = pu.nextValue( "diffuseMap: " ) + if(diffuseMap) + this.setProperty( "diffuseMap", diffuseMap ); + + var normalMap = pu.nextValue( "normalMap: " ); + if(normalMap) + this.setProperty( "normalMap", normalMap ); + + var specularMap = pu.nextValue( "specularMap: " ); + if(specularMap) + this.setProperty( "specularMap", specularMap ); + + var environmentMap = pu.nextValue( "environmentMap: " ); + if(environmentMap) + { + this.setProperty( "environmentMap", environmentMap ); + this.setProperty( "environmentAmount", Number( pu.nextValue( "envReflection" ) ) ); + } + + this.rebuildShader(); } this.export = function() @@ -428,20 +521,18 @@ var UberMaterial = function UberMaterial() { else if (light.type === 'spot') { exportStr += 'light' + i + 'Pos: ' + light['position'] + '\n'; - - var deg2Rad = Math.PI / 180; - exportStr += 'light' + i + 'Spot: ' + [ Math.cos( ( light['spotInnerCutoff'] || 45.0 ) * deg2Rad ), - Math.cos( ( light['spotOuterCutoff'] || 90.0 ) * deg2Rad )] + '\n'; + exportStr += 'light' + i + 'SpotInnerCutoff: ' + light['spotInnerCutoff'] + '\n'; + exportStr += 'light' + i + 'SpotOuterCutoff: ' + light['spotOuterCutoff'] + '\n'; } else // light.type === 'point' { - exportStr += 'light' + i + 'Pos: ' + (light['position'] || [ 0, 0, 0 ]) ; - exportStr += 'light' + i + 'Attenuation: ' + (light['attenuation'] || [ 1, 0, 0 ]) ; + exportStr += 'light' + i + 'Pos: ' + (light['position'] || [ 0, 0, 0 ]) + '\n'; + exportStr += 'light' + i + 'Attenuation: ' + (light['attenuation'] || [ 1, 0, 0 ]) + '\n'; } // common to all lights - exportStr += 'light' + i + 'Color: ' + light['diffuseColor'] || [ 1,1,1,1 ] + '\n'; - exportStr += 'light' + i + 'SpecularColor: ' + light['specularColor'] || [ 1, 1, 1, 1 ] + '\n'; + exportStr += 'light' + i + 'Color: ' + (light['diffuseColor'] || [ 1,1,1,1 ]) + '\n'; + exportStr += 'light' + i + 'SpecularColor: ' + (light['specularColor'] || [ 1, 1, 1, 1 ]) + '\n'; exportStr += "endlight\n"; } @@ -466,7 +557,10 @@ var UberMaterial = function UberMaterial() { exportStr += "specularMap: " + caps.specularMap.texture + "\n"; if(typeof caps.environmentMap != 'undefined') + { exportStr += "environmentMap: " + caps.environmentMap.texture + "\n"; + exportStr += "envReflection: " + caps.environmentMap.envReflection + "\n"; + } // every material needs to terminate like this exportStr += "endMaterial\n"; -- cgit v1.2.3 From 57d4a82977a1f0e809511fe894886f88581d9615 Mon Sep 17 00:00:00 2001 From: hwc487 Date: Wed, 14 Mar 2012 16:22:22 -0700 Subject: Corrections for Uber shader IO --- js/lib/rdge/materials/uber-material.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'js/lib/rdge/materials/uber-material.js') diff --git a/js/lib/rdge/materials/uber-material.js b/js/lib/rdge/materials/uber-material.js index 8ec5b05c..91f43754 100755 --- a/js/lib/rdge/materials/uber-material.js +++ b/js/lib/rdge/materials/uber-material.js @@ -477,7 +477,7 @@ var UberMaterial = function UberMaterial() { if(environmentMap) { this.setProperty( "environmentMap", environmentMap ); - this.setProperty( "environmentAmount", Number( pu.nextValue( "envReflection" ) ) ); + this.setProperty( "environmentAmount", Number( pu.nextValue( "environmentAmount" ) ) ); } this.rebuildShader(); @@ -495,10 +495,10 @@ var UberMaterial = function UberMaterial() { if (typeof caps.material != 'undefined') { exportStr += "materialProps: true\n"; - exportStr += "ambientColor: " + caps.material.ambientColor + "\n"; - exportStr += "diffuseColor: " + caps.material.diffuseColor + "\n"; - exportStr += "specularColor: " + caps.material.specularColor + "\n"; - exportStr += "specularPower: " + caps.material.specularPower + "\n"; + exportStr += "ambientColor: " + this._ambientColor + "\n"; + exportStr += "diffuseColor: " + this._diffuseColor + "\n"; + exportStr += "specularColor: " + this._specularColor + "\n"; + exportStr += "specularPower: " + this._specularPower + "\n"; } if (typeof caps.lighting != 'undefined') @@ -559,7 +559,7 @@ var UberMaterial = function UberMaterial() { if(typeof caps.environmentMap != 'undefined') { exportStr += "environmentMap: " + caps.environmentMap.texture + "\n"; - exportStr += "envReflection: " + caps.environmentMap.envReflection + "\n"; + exportStr += "environmentAmount: " + caps.environmentMap.envReflection + "\n"; } // every material needs to terminate like this -- cgit v1.2.3 From fdeed8051c3af538d28ca3bc599121cea483c22c Mon Sep 17 00:00:00 2001 From: Valerio Virgillito Date: Thu, 22 Mar 2012 15:47:56 -0700 Subject: Squashed commit of the following GL integration Signed-off-by: Valerio Virgillito --- js/lib/rdge/materials/uber-material.js | 180 ++++++++++++++++++++++++++++++++- 1 file changed, 176 insertions(+), 4 deletions(-) (limited to 'js/lib/rdge/materials/uber-material.js') 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() { this.rebuildShader(); } + this.importJSON = function( jObj ) + { + if (this.getShaderName() != jObj.material) throw new Error( "ill-formed material" ); + this.setName( jObj.name ); + + if (jObj.materialProps) + { + var ambientColor = jObj.materialProps.ambientColor; this.setProperty( "ambientColor", ambientColor ); + var diffuseColor = jObj.materialProps.diffuseColor; this.setProperty( "diffuseColor", diffuseColor ); + var specularColor = jObj.materialProps.specularColor; this.setProperty( "specularColor", specularColor ); + var specularPower = jObj.materialProps.specularPower; this.setProperty( "specularPower", specularPower ); + } + + var lightArray = jObj.lights; + if (lightArray) + { + this._lights = []; + for (var i=0; i 0) + { + this._ubershaderCaps.lighting = + { + 'light0' : this._lights[0], + 'light1' : this._lights[1], + 'light2' : this._lights[2], + 'light3' : this._lights[3] + } + } + } + + var diffuseMap = jObj['diffuseMap']; + if(diffuseMap) + this.setProperty( "diffuseMap", diffuseMap ); + + var normalMap = jObj['normalMap']; + if(normalMap) + this.setProperty( "normalMap", normalMap ); + + var specularMap = jObj['specularMap']; + if(specularMap) + this.setProperty( "specularMap", specularMap ); + + var environmentMap = jObj['environmentMap']; + if(environmentMap) + { + this.setProperty( "environmentMap", environmentMap ); + this.setProperty( "environmentAmount", jObj['environmentAmount'] ); + } + + this.rebuildShader(); + } + + this.exportJSON = function() + { + // every material needs the base type and instance name + var caps = this._ubershaderCaps; + var jObj = + { + 'material' : this.getShaderName(), + 'name' : this.getName() + }; + + // export the material properties + if (typeof caps.material != 'undefined') + { + jObj.materialProps = + { + 'ambientColor' : this._ambientColor, + 'diffuseColor' : this._diffuseColor, + 'specularColor' : this._specularColor, + 'specularPower' : this._specularPower + }; + + } + + if (typeof caps.lighting != 'undefined') + { + var lightArray = []; + for (var i=0; i 0) + jObj.lights = lightArray; + } + + if(typeof caps.diffuseMap != 'undefined') + jObj['diffuseMap'] = caps.diffuseMap.texture; + + if(typeof caps.normalMap != 'undefined') + jObj['normalMap'] = caps.normalMap.texture; + + if(typeof caps.specularMap != 'undefined') + jObj['specularMap'] = caps.specularMap.texture; + + if(typeof caps.environmentMap != 'undefined') + { + jObj['environmentMap'] = caps.environmentMap.texture; + jObj['environmentAmount'] = caps.environmentMap.envReflection; + } + + return jObj; + } + + this.export = function() { // every material needs the base type and instance name @@ -539,10 +715,6 @@ var UberMaterial = function UberMaterial() { } } -// this._diffuseMapOb = { 'texture' : 'assets/images/rocky-diffuse.jpg', 'wrap' : 'REPEAT' }; -// this._normalMapOb = { 'texture' : 'assets/images/rocky-normal.jpg', 'wrap' : 'REPEAT' }; -// this._specularMapOb = { 'texture' : 'assets/images/rocky-spec.jpg', 'wrap' : 'REPEAT' }; -// this._environmentMapOb = { 'texture' : 'assets/images/silver.png', 'wrap' : 'CLAMP', 'envReflection' : this._environmentAmount }; var world = this.getWorld(); if (!world) throw new Error( "no world in material.export, " + this.getName() ); -- cgit v1.2.3