aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/RDGE/Materials/BumpMetalMaterial.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/helper-classes/RDGE/Materials/BumpMetalMaterial.js')
-rw-r--r--js/helper-classes/RDGE/Materials/BumpMetalMaterial.js270
1 files changed, 270 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/Materials/BumpMetalMaterial.js b/js/helper-classes/RDGE/Materials/BumpMetalMaterial.js
new file mode 100644
index 00000000..0aa3ee78
--- /dev/null
+++ b/js/helper-classes/RDGE/Materials/BumpMetalMaterial.js
@@ -0,0 +1,270 @@
1/* <copyright>
2 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4 (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5 </copyright> */
6
7
8///////////////////////////////////////////////////////////////////////
9// Class GLMaterial
10// RDGE representation of a material.
11///////////////////////////////////////////////////////////////////////
12function BumpMetalMaterial()
13{
14 // initialize the inherited members
15 this.inheritedFrom = GLMaterial;
16 this.inheritedFrom();
17
18 ///////////////////////////////////////////////////////////////////////
19 // Instance variables
20 ///////////////////////////////////////////////////////////////////////
21 this._name = "BumpMetalMaterial";
22 this._shaderName = "bumpMetal";
23
24 this._lightDiff = [0.3, 0.3, 0.3, 1.0];
25 this._diffuseTexture = "metal";
26 this._specularTexture = "silver";
27 this._normalTexture = "normalMap";
28
29 ///////////////////////////////////////////////////////////////////////
30 // Property Accessors
31 ///////////////////////////////////////////////////////////////////////
32 this.getName = function() { return this._name; }
33 this.getShaderName = function() { return this._shaderName; }
34
35 this.getLightDiff = function() { return this._lightDiff; }
36 this.setLightDiff = function(ld) { this._lightDiff = ld;
37 if (this._shader && this._shader.default)
38 this._shader.default.u_light0Diff.set( ld ); }
39
40 this.getDiffuseTexture = function() { return this._diffuseTexture; }
41 this.setDiffuseTexture = function(dt) { this._diffuseTexture = dt;
42 if (this._materialNode) this._materialNode.setDiffuseTexture( dt ); }
43
44 this.getSpecularTexture = function() { return this._specularTexture; }
45 this.setSpecularTexture = function(st) { this._specularTexture = st;
46 if (this._materialNode) this._materialNode.setSpecularTexture( st ); }
47
48 this.getNormalTexture = function() { return this._normalTexture; }
49 this.setNormalTexture = function(nt) { this._normalTexture = nt;
50 if (this._materialNode) this._materialNode.setNormalTexture( nt ); }
51
52 ///////////////////////////////////////////////////////////////////////
53 // Material Property Accessors
54 ///////////////////////////////////////////////////////////////////////
55 this._propNames = ["lightDiff", "diffuseMap", "normalMap", "specularMap"];
56 this._propLabels = ["Diffuse Color", "Diffuse Map", "Bump Map", "Specular Map"];
57 this._propTypes = ["color", "file", "file", "file"];
58 this._propValues = [];
59
60 this._propValues[ this._propNames[0] ] = this._lightDiff.slice(0);
61 this._propValues[ this._propNames[1] ] = this._diffuseTexture.slice(0);
62 this._propValues[ this._propNames[2] ] = this._specularTexture.slice(0);
63 this._propValues[ this._propNames[3] ] = this._specularTexture.slice(0);
64
65 // TODO - shader techniques are not all named the same, i.e., FlatMaterial uses "colorMe" and BrickMaterial uses "default"
66 this.setProperty = function( prop, value )
67 {
68 // every material should do something with the "color" property
69 if (prop === "color") prop = "lightDiff";
70
71 // make sure we have legitimate imput
72 var ok = this.validateProperty( prop, value );
73 if (!ok)
74 {
75 console.log( "invalid property in Bump Metal Materia;" + prop + " : " + value );
76 return;
77 }
78
79 switch (prop)
80 {
81 case "lightDiff": this.setLightDiff( value ); break;
82 case "diffuseTexture": this.setDiffuseTexture( value ); break;
83 case "specularTexture": this.setSpecularTexture( value ); break;
84 case "normalMap": this.setNormalTexture( value ); break;
85
86 default:
87 console.log( "invalid property to Bump Metal Material: " + prop + ", value: " + value );
88 break;
89 }
90 }
91
92 ///////////////////////////////////////////////////////////////////////
93 // Methods
94 ///////////////////////////////////////////////////////////////////////
95 // duplcate method requirde
96 this.dup = function() { return new BumpMetalMaterial(); }
97
98 this.init = function()
99 {
100 // set up the shader
101 this._shader = new jshader();
102 this._shader.def = bumpMetalMaterialDef;
103 this._shader.init();
104 this._shader.default.u_light0Diff.set( this.getLightDiff() );
105
106 // set up the material node
107 this._materialNode = createMaterialNode( this.getShaderName() );
108 this._materialNode.setShader(this._shader);
109
110 // set some image maps
111 this._materialNode.setDiffuseTexture( this.getDiffuseTexture() );
112 this._materialNode.setSpecTexture( this.getSpecularTexture() );
113 this._materialNode.setNormalTexture( this.getNormalTexture() );
114 }
115
116 this.export = function()
117 {
118 // every material needs the base type and instance name
119 var exportStr = "material: " + this.getShaderName() + "\n";
120 exportStr += "name: " + this.getName() + "\n";
121
122 exportStr += "lightDiff: " + this.getLightDiff() + "\n";
123 exportStr += "diffuseTexture: " + this.getDiffuseTexture() + "\n";
124 exportStr += "specularTexture: " + this.getSpecularTexture() + "\n";
125 exportStr += "normalTexture: " + this.getNormalTexture() + "\n";
126
127 // every material needs to terminate like this
128 exportStr += "endMaterial\n";
129
130 return exportStr;
131 }
132
133 this.import = function( importStr )
134 {
135 var pu = new ParseUtils( importStr );
136 var material = pu.nextValue( "material: " );
137 if (material != this.getShaderName()) throw new Error( "ill-formed material" );
138 this.setName( pu.nextValue( "name: ") );
139
140 var rtnStr;
141 try
142 {
143 var lightDiff = eval( "[" + pu.nextValue( "lightDiff: " ) + "]" ),
144 dt = pu.nextValue( "diffuseTexture: " ),
145 st = pu.nextValue( "specularTexture: " ),
146 nt = pu.nextValue( "normalTexture: " );
147
148 this.setProperty( "lightDiff", lightDif);
149 this.setProperty( "diffuseTexture", dt );
150 this.setProperty( "specularTexture", st );
151 this.setProperty( "normalTexture", nt );
152
153 var endKey = "endMaterial\n";
154 var index = importStr.indexOf( endKey );
155 index += endKey.length;
156 rtnStr = importStr.substr( index );
157 }
158 catch (e)
159 {
160 throw new Error( "could not import material: " + importStr );
161 }
162
163 return rtnStr;
164 }
165}
166
167///////////////////////////////////////////////////////////////////////////////////////
168// RDGE shader
169
170// shader spec (can also be loaded from a .JSON file, or constructed at runtime)
171var bumpMetalMaterialDef =
172bumpMetalShaderDef =
173{
174 'shaders':
175 {
176 // this shader is being referenced by file
177 'defaultVShader':"assets/shaders/test_vshader.glsl",
178 'defaultFShader':"assets/shaders/test_fshader.glsl",
179
180 // this shader is inline
181 'dirLightVShader': "\
182 uniform mat4 u_mvMatrix;\
183 uniform mat4 u_normalMatrix;\
184 uniform mat4 u_projMatrix;\
185 uniform mat4 u_worldMatrix;\
186 attribute vec3 a_pos;\
187 attribute vec3 a_nrm;\
188 varying vec3 vNormal;\
189 varying vec3 vPos;\
190 void main() {\
191 vNormal.xyz = (u_normalMatrix*vec4(a_nrm, 0.0)).xyz;\
192 gl_Position = u_projMatrix * u_mvMatrix * vec4(a_pos,1.0);\
193 vPos = (u_worldMatrix * vec4(a_pos,1.0)).xyz;\
194 }",
195 'dirLightFShader': "\
196 precision highp float;\
197 uniform vec4 u_light1Diff;\
198 uniform vec3 u_light1Pos;\
199 uniform vec4 u_light2Diff;\
200 uniform vec3 u_light2Pos;\
201 varying vec3 vNormal;\
202 varying vec3 vPos;\
203 void main() {\
204 vec3 light1 = vec3(u_light1Pos.x - vPos.x, u_light1Pos.y - vPos.y, u_light1Pos.z - vPos.z);\
205 vec3 light2 = vec3(u_light2Pos.x - vPos.x, u_light2Pos.y - vPos.y, u_light2Pos.z - vPos.z);\