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 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 168 insertions(+), 17 deletions(-) (limited to 'assets/canvas-runtime.js') 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