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 --- assets/canvas-runtime.js | 185 ++++++++++++++++++++++++++++++--- js/document/html-document.js | 2 + js/lib/rdge/materials/uber-material.js | 12 +-- 3 files changed, 176 insertions(+), 23 deletions(-) diff --git a/assets/canvas-runtime.js b/assets/canvas-runtime.js index 51c1de1f..fd823f35 100644 --- a/assets/canvas-runtime.js +++ b/assets/canvas-runtime.js @@ -504,20 +504,7 @@ function RuntimeGeomObj() /////////////////////////////////////////////////////////////////////// // Methods - /////////////////////////////////////////////////////////////////////// - this.makeStrokeMaterial = function() - { - } - - this.makeFillMaterial = function() - { - } - - - this.render = function() - { - } - + /////////////////////////////////////////////////////////////////////// this.addChild = function( child ) { if (!this._children) this._children = []; @@ -573,9 +560,10 @@ function RuntimeGeomObj() this._materials.push( mat ); } - var endIndex = importStr.indexOf( "endMaterial\n" ); + var endKey = "endMaterial\n"; + var endIndex = importStr.indexOf( endKey ); if (endIndex < 0) break; - importStr = importStr.substr( endIndex ); + importStr = importStr.substr( endIndex + endKey.length ); } } @@ -1369,8 +1357,88 @@ function RuntimeUberMaterial() this.inheritedFrom = RuntimeMaterial; this.inheritedFrom(); + this._MAX_LIGHTS = 4; + this.init = function( ) { + var material = this._materialNode; + if (material) + { + var technique = material.shaderProgram.defaultTechnique; + var renderer = g_Engine.getContext().renderer; + if (renderer && technique) + { + if (this._shader && this._shader.defaultTechnique) + { + if (this._ambientColor && technique.u_ambientColor) technique.u_ambientColor.set(this._ambientColor ); + if (this._diffuseColor && technique.u_diffuseColor ) technique.u_diffuseColor.set(this._diffuseColor ); + if (this._specularColor && technique.u_specularColor) technique.u_specularColor.set(this._specularColor); + if (this._specularPower && technique.u_specularPower) technique.u_specularPower.set([this._specularPower]); + + if (this._lights) + { + for(var i = 0; i < 4; ++i) + { + var light = this._lights[i]; + if (light) + { + if(light.type == 'directional') + { + technique['u_light'+i+'Dir'].set( light.direction || [ 0, 0, 1 ]); + } + else if(light.type == 'spot') + { + technique['u_light'+i+'Atten'].set(light.attenuation || [ 1,0,0 ]); + technique['u_light'+i+'Pos'].set(light.position || [ 0, 0, 0 ]); + technique['u_light'+i+'Spot'].set([ Math.cos( ( light.spotInnerCutoff || 45.0 ) * deg2Rad ), + Math.cos( ( light.spotOuterCutoff || 90.0 ) * deg2Rad )]); + } + else + { + technique['u_light'+i+'Pos'].set(light.position || [ 0, 0, 0 ]); + technique['u_light'+i+'Atten'].set(light.attenuation || [ 1,0,0 ]); + } + + // set the common light properties + technique['u_light'+i+'Color'].set(light.diffuseColor || [ 1,1,1,1 ]); + technique['u_light'+i+'Specular'].set(light.specularColor || [ 1, 1, 1, 1 ]); + } + } + } + + // currently not exported + var uvTransform = [ 2.0, 0, 0, 0, 0, 2.0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1]; + technique.u_uvMatrix.set(uvTransform); + + var renderer = g_Engine.getContext().renderer; + if (this._diffuseMap) + { + var tex = renderer.getTextureByName(this._diffuseMap, 'REPEAT'); + technique.s_diffuseMap.set( tex ); + } + + if (this._normalMap) + { + var tex = renderer.getTextureByName(this._normalMap, 'REPEAT'); + technique.s_normalMap.set( tex ); + } + + if (this._specularMap) + { + var tex = renderer.getTextureByName(this._specularMap, 'REPEAT'); + technique.s_specMap.set( tex ); + } + + if(this._environmentMap) + { + var tex = renderer.getTextureByName(this._environmentMap, 'CLAMP'); + technique.s_envMap.set( tex ); + if (this._environmentAmount) + technique.u_envReflection.set([ this._environmentAmount ] ); + } + } + } + } } this.update = function( time ) @@ -1379,6 +1447,89 @@ function RuntimeUberMaterial() 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.slice( 0, index ); + + var matProps = getPropertyFromString( "materialProps: ", importStr ); + if (matProps) + { + this._ambientColor = eval( "[" + getPropertyFromString("ambientColor: ", importStr) + ']' ); + this._diffuseColor = eval( "[" + getPropertyFromString( "diffuseColor: ", importStr) + ']' ); + this._specularColor = eval( "[" + getPropertyFromString( "specularColor: ", importStr) + ']' ); + this._specularPower = Number( getPropertyFromString( "specularPower: ", importStr) ); + } + + var lightProps = getPropertyFromString( "lightProps: ", importStr ); + if (lightProps) + { + this._lights = []; + var lightStr; + for (var i=0; i= 0) { @@ -244,6 +245,7 @@ exports.HTMLDocument = Montage.create(TextDocument, { } } } + // */ } } } 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