diff options
Diffstat (limited to 'js/lib/rdge/materials/bump-metal-material.js')
-rwxr-xr-x | js/lib/rdge/materials/bump-metal-material.js | 314 |
1 files changed, 314 insertions, 0 deletions
diff --git a/js/lib/rdge/materials/bump-metal-material.js b/js/lib/rdge/materials/bump-metal-material.js new file mode 100755 index 00000000..61126952 --- /dev/null +++ b/js/lib/rdge/materials/bump-metal-material.js | |||
@@ -0,0 +1,314 @@ | |||
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 | var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser; | ||
8 | var Material = require("js/lib/rdge/materials/material").Material; | ||
9 | /////////////////////////////////////////////////////////////////////// | ||
10 | // Class GLMaterial | ||
11 | // RDGE representation of a material. | ||
12 | /////////////////////////////////////////////////////////////////////// | ||
13 | var BumpMetalMaterial = function BumpMetalMaterial() { | ||
14 | /////////////////////////////////////////////////////////////////////// | ||
15 | // Instance variables | ||
16 | /////////////////////////////////////////////////////////////////////// | ||
17 | this._name = "BumpMetalMaterial"; | ||
18 | this._shaderName = "bumpMetal"; | ||
19 | |||
20 | this._lightDiff = [0.3, 0.3, 0.3, 1.0]; | ||
21 | this._diffuseTexture = "assets/images/metal.png"; | ||
22 | this._specularTexture = "assets/images/silver.png"; | ||
23 | this._normalTexture = "assets/images/normalMap.png"; | ||
24 | |||
25 | /////////////////////////////////////////////////////////////////////// | ||
26 | // Property Accessors | ||
27 | /////////////////////////////////////////////////////////////////////// | ||
28 | this.getName = function() { | ||
29 | return this._name; | ||
30 | }; | ||
31 | |||
32 | this.getShaderName = function() { | ||
33 | return this._shaderName; | ||
34 | }; | ||
35 | |||
36 | this.getLightDiff = function() { | ||
37 | return this._lightDiff; | ||
38 | }; | ||
39 | |||
40 | this.setLightDiff = function(ld) { | ||
41 | this._lightDiff = ld; | ||
42 | // Bad property name. Commenting for now | ||
43 | |||
44 | if (this._shader && this._shader['default']){ | ||
45 | this._shader['default'].u_light0Diff.set( ld ); | ||
46 | } | ||
47 | |||
48 | }; | ||
49 | |||
50 | this.getDiffuseTexture = function() { return this._propValues[this._propNames[1]] ? this._propValues[this._propNames[1]].slice() : null }; | ||
51 | this.setDiffuseTexture = function(m) { this._propValues[this._propNames[1]] = m ? m.slice(0) : null; this.updateTexture(1); }; | ||
52 | |||
53 | this.getNormalTexture = function() { return this._propValues[this._propNames[2]] ? this._propValues[this._propNames[2]].slice() : null }; | ||
54 | this.setNormalTexture = function(m) { this._propValues[this._propNames[2]] = m ? m.slice(0) : null; this.updateTexture(2); }; | ||
55 | |||
56 | this.getSpecularTexture = function() { return this._propValues[this._propNames[3]] ? this._propValues[this._propNames[3]].slice() : null }; | ||
57 | this.setSpecularTexture = function(m) { this._propValues[this._propNames[3]] = m ? m.slice(0) : null; this.updateTexture(3); }; | ||
58 | |||
59 | this.isAnimated = function() { return true; }; | ||
60 | |||
61 | /////////////////////////////////////////////////////////////////////// | ||
62 | // Material Property Accessors | ||
63 | /////////////////////////////////////////////////////////////////////// | ||
64 | this._propNames = ["lightDiff", "diffuseTexture", "normalMap", "specularTexture"]; | ||
65 | this._propLabels = ["Diffuse Color", "Diffuse Map", "Bump Map", "Specular Map"]; | ||
66 | this._propTypes = ["color", "file", "file", "file"]; | ||
67 | this._propValues = []; | ||
68 | |||
69 | this._propValues[ this._propNames[0] ] = this._lightDiff.slice(0); | ||
70 | this._propValues[ this._propNames[1] ] = this._diffuseTexture.slice(0); | ||
71 | this._propValues[ this._propNames[2] ] = this._normalTexture.slice(0); | ||
72 | this._propValues[ this._propNames[3] ] = this._specularTexture.slice(0); | ||
73 | |||
74 | // TODO - shader techniques are not all named the same, i.e., FlatMaterial uses "colorMe" and BrickMaterial uses "default" | ||
75 | this.setProperty = function( prop, value ) | ||
76 | { | ||
77 | // every material should do something with the "color" property | ||
78 | if (prop === "color") prop = "lightDiff"; | ||
79 | |||
80 | // make sure we have legitimate imput | ||
81 | var ok = this.validateProperty( prop, value ); | ||
82 | if (!ok) | ||
83 | { | ||
84 | console.log( "invalid property in Bump Metal Materia;" + prop + " : " + value ); | ||
85 | return; | ||
86 | } | ||
87 | |||
88 | switch (prop) | ||
89 | { | ||
90 | case "lightDiff": this.setLightDiff( value ); break; | ||
91 | case "diffuseTexture": this.setDiffuseTexture( value ); break; | ||
92 | case "specularTexture": this.setSpecularTexture( value ); break; | ||
93 | case "normalMap": this.setNormalTexture( value ); break; | ||
94 | |||
95 | default: | ||
96 | console.log( "invalid property to Bump Metal Material: " + prop + ", value: " + value ); | ||
97 | break; | ||
98 | } | ||
99 | }; | ||
100 | |||
101 | /////////////////////////////////////////////////////////////////////// | ||
102 | // Methods | ||
103 | /////////////////////////////////////////////////////////////////////// | ||
104 | // duplcate method requirde | ||
105 | this.dup = function() { return new BumpMetalMaterial(); }; | ||
106 | |||
107 | this.init = function( world ) | ||
108 | { | ||
109 | // save the world | ||
110 | if (world) this.setWorld( world ); | ||
111 | |||
112 | // set up the shader | ||
113 | this._shader = new jshader(); | ||
114 | this._shader.def = bumpMetalMaterialDef; | ||
115 | this._shader.init(); | ||
116 | this._shader['default'].u_light0Diff.set( this.getLightDiff() ); | ||
117 | |||
118 | // set up the material node | ||
119 | this._materialNode = createMaterialNode( this.getShaderName() ); | ||
120 | this._materialNode.setShader(this._shader); | ||
121 | |||
122 | // set some image maps | ||
123 | this.updateTexture(1); | ||
124 | this.updateTexture(2); | ||
125 | this.updateTexture(3); | ||
126 | }; | ||
127 | |||
128 | this.updateTexture = function( index ) | ||
129 | { | ||
130 | var material = this._materialNode; | ||
131 | if (material) | ||
132 | { | ||
133 | var technique = material.shaderProgram['default']; | ||
134 | var renderer = g_Engine.getContext().renderer; | ||
135 | if (renderer && technique) | ||
136 | { | ||
137 | var texMapName = this._propValues[this._propNames[index]]; | ||
138 | var wrap = 'REPEAT', mips = true; | ||
139 | var tex = this.loadTexture( texMapName, wrap, mips ); | ||
140 | |||
141 | if (tex) | ||
142 | { | ||
143 | switch (index) | ||
144 | { | ||
145 | case 1: technique.u_colMap.set( tex ); break; | ||
146 | case 2: technique.u_normalMap.set( tex ); break; | ||
147 | case 3: technique.u_glowMap.set( tex ); break; | ||
148 | default: console.log( "invalid map index in BumpMetalMaterial, " + index ); | ||
149 | } | ||
150 | } | ||
151 | } | ||
152 | } | ||
153 | }; | ||
154 | |||
155 | this.export = function() | ||
156 | { | ||
157 | // every material needs the base type and instance name | ||
158 | var exportStr = "material: " + this.getShaderName() + "\n"; | ||
159 | exportStr += "name: " + this.getName() + "\n"; | ||
160 | |||
161 | exportStr += "lightDiff: " + this.getLightDiff() + "\n"; | ||
162 | exportStr += "diffuseTexture: " + this.getDiffuseTexture() + "\n"; | ||
163 | exportStr += "specularTexture: " + this.getSpecularTexture() + "\n"; | ||
164 | exportStr += "normalMap: " + this.getNormalTexture() + "\n"; | ||
165 | |||
166 | // every material needs to terminate like this | ||
167 | exportStr += "endMaterial\n"; | ||
168 | |||
169 | return exportStr; | ||
170 | }; | ||
171 | |||
172 | this.import = function( importStr ) | ||
173 | { | ||
174 | var pu = new MaterialParser( importStr ); | ||
175 | var material = pu.nextValue( "material: " ); | ||
176 | if (material != this.getShaderName()) throw new Error( "ill-formed material" ); | ||
177 | this.setName( pu.nextValue( "name: ") ); | ||
178 | |||
179 | var rtnStr; | ||
180 | try | ||
181 | { | ||
182 | var lightDiff = eval( "[" + pu.nextValue( "lightDiff: " ) + "]" ), | ||
183 | dt = pu.nextValue( "diffuseTexture: " ), | ||
184 | st = pu.nextValue( "specularTexture: " ), | ||
185 | nt = pu.nextValue( "normalMap: " ); | ||
186 | |||
187 | this.setProperty( "lightDiff", lightDiff); | ||
188 | this.setProperty( "diffuseTexture", dt ); | ||
189 | this.setProperty( "specularTexture", st ); | ||
190 | this.setProperty( "normalMap", nt ); | ||
191 | |||
192 | var endKey = "endMaterial\n"; | ||
193 | var index = importStr.indexOf( endKey ); | ||
194 | index += endKey.length; | ||
195 | rtnStr = importStr.substr( index ); | ||
196 | } | ||
197 | catch (e) | ||
198 | { | ||
199 | throw new Error( "could not import material: " + importStr ); | ||
200 | } | ||
201 | |||
202 | return rtnStr; | ||
203 | }; | ||
204 | }; | ||
205 | |||
206 | /////////////////////////////////////////////////////////////////////////////////////// | ||
207 | // RDGE shader | ||
208 | |||
209 | // shader spec (can also be loaded from a .JSON file, or constructed at runtime) | ||
210 | var bumpMetalMaterialDef = bumpMetalShaderDef = | ||
211 | { | ||
212 | 'shaders': | ||
213 | { | ||
214 | // this shader is being referenced by file | ||
215 | 'defaultVShader':"assets/shaders/test_vshader.glsl", | ||
216 | 'defaultFShader':"assets/shaders/test |