diff options
Diffstat (limited to 'js/lib/rdge/materials/bump-metal-material.js')
-rwxr-xr-x | js/lib/rdge/materials/bump-metal-material.js | 168 |
1 files changed, 23 insertions, 145 deletions
diff --git a/js/lib/rdge/materials/bump-metal-material.js b/js/lib/rdge/materials/bump-metal-material.js index 27d5793c..4604a89d 100755 --- a/js/lib/rdge/materials/bump-metal-material.js +++ b/js/lib/rdge/materials/bump-metal-material.js | |||
@@ -6,6 +6,8 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot | |||
6 | 6 | ||
7 | var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser; | 7 | var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser; |
8 | var Material = require("js/lib/rdge/materials/material").Material; | 8 | var Material = require("js/lib/rdge/materials/material").Material; |
9 | var Texture = require("js/lib/rdge/texture").Texture; | ||
10 | |||
9 | /////////////////////////////////////////////////////////////////////// | 11 | /////////////////////////////////////////////////////////////////////// |
10 | // Class GLMaterial | 12 | // Class GLMaterial |
11 | // RDGE representation of a material. | 13 | // RDGE representation of a material. |
@@ -17,181 +19,58 @@ var BumpMetalMaterial = function BumpMetalMaterial() { | |||
17 | this._name = "BumpMetalMaterial"; | 19 | this._name = "BumpMetalMaterial"; |
18 | this._shaderName = "bumpMetal"; | 20 | this._shaderName = "bumpMetal"; |
19 | 21 | ||
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 | 22 | ||
32 | this.getShaderName = function() { | 23 | this._defaultDiffuseTexture = "assets/images/metal.png"; |
33 | return this._shaderName; | 24 | this._defaultSpecularTexture = "assets/images/silver.png"; |
34 | }; | 25 | this._defaultNormalTexture = "assets/images/normalMap.png"; |
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 | 26 | ||
48 | }; | 27 | // array textures indexed by shader uniform name |
28 | this._glTextures = []; | ||
49 | 29 | ||
50 | this.getDiffuseTexture = function() { return this._propValues[this._propNames[1]] ? this._propValues[this._propNames[1]].slice() : null }; | 30 | this._speed = 1.0; |
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 | 31 | ||
32 | /////////////////////////////////////////////////////////////////////// | ||
33 | // Property Accessors | ||
34 | /////////////////////////////////////////////////////////////////////// | ||
59 | this.isAnimated = function() { return true; }; | 35 | this.isAnimated = function() { return true; }; |
36 | this.getShaderDef = function() { return bumpMetalMaterialDef; }; | ||
60 | 37 | ||
61 | /////////////////////////////////////////////////////////////////////// | 38 | /////////////////////////////////////////////////////////////////////// |
62 | // Material Property Accessors | 39 | // Material Property Accessors |
63 | /////////////////////////////////////////////////////////////////////// | 40 | /////////////////////////////////////////////////////////////////////// |
64 | this._propNames = ["lightDiff", "diffuseTexture", "normalMap", "specularTexture"]; | 41 | this._propNames = ["u_light0Diff", "u_colMap", "u_normalMap", "u_glowMap" ]; |
65 | this._propLabels = ["Diffuse Color", "Diffuse Map", "Bump Map", "Specular Map"]; | 42 | this._propLabels = ["Diffuse Color", "Diffuse Map", "Bump Map", "Specular Map" ]; |
66 | this._propTypes = ["color", "file", "file", "file"]; | 43 | this._propTypes = ["color", "file", "file", "file" ]; |
67 | this._propValues = []; | 44 | this._propValues = []; |
68 | 45 | ||
69 | this._propValues[ this._propNames[0] ] = this._lightDiff.slice(0); | 46 | this._propValues[ this._propNames[0] ] = [0.3, 0.3, 0.3, 1.0]; |
70 | this._propValues[ this._propNames[1] ] = this._diffuseTexture.slice(0); | 47 | this._propValues[ this._propNames[1] ] = this._defaultDiffuseTexture.slice(0); |
71 | this._propValues[ this._propNames[2] ] = this._normalTexture.slice(0); | 48 | this._propValues[ this._propNames[2] ] = this._defaultNormalTexture.slice(0); |
72 | this._propValues[ this._propNames[3] ] = this._specularTexture.slice(0); | 49 | this._propValues[ this._propNames[3] ] = this._defaultSpecularTexture.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") return; | ||
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 | 50 | ||
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 | 51 | ||
101 | /////////////////////////////////////////////////////////////////////// | 52 | /////////////////////////////////////////////////////////////////////// |
102 | // Methods | 53 | // Methods |
103 | /////////////////////////////////////////////////////////////////////// | 54 | /////////////////////////////////////////////////////////////////////// |
104 | // duplcate method requirde | ||
105 | this.dup = function() { return new BumpMetalMaterial(); }; | ||
106 | 55 | ||
107 | this.init = function( world ) | 56 | this.init = function( world ) |
108 | { | 57 | { |
109 | // save the world | 58 | // save the world |
110 | if (world) { | 59 | if (world) { |
111 | this.setWorld( world ); | 60 | this.setWorld( world ); |
112 | } | 61 | } |
113 | 62 | ||
114 | // set up the shader | 63 | // set up the shader |
115 | this._shader = new RDGE.jshader(); | 64 | this._shader = new RDGE.jshader(); |
116 | this._shader.def = bumpMetalMaterialDef; | 65 | this._shader.def = bumpMetalMaterialDef; |
117 | this._shader.init(); | 66 | this._shader.init(); |
118 | this._shader['default'].u_light0Diff.set( this.getLightDiff() ); | ||
119 | 67 | ||
120 | // set up the material node | 68 | // set up the material node |
121 | this._materialNode = RDGE.createMaterialNode( this.getShaderName() + "_" + world.generateUniqueNodeID() ); | 69 | this._materialNode = RDGE.createMaterialNode( this.getShaderName() + "_" + world.generateUniqueNodeID() ); |
122 | this._materialNode.setShader(this._shader); | 70 | this._materialNode.setShader(this._shader); |
123 | 71 | ||
124 | // set some image maps | 72 | this.setShaderValues(); |
125 | this.updateTexture(1); | 73 | this.update(0); |
126 | this.updateTexture(2); | ||
127 | this.updateTexture(3); | ||
128 | }; | ||
129 | |||
130 | this.updateTexture = function( index ) | ||
131 | { | ||
132 | var material = this._materialNode; | ||
133 | if (material) | ||
134 | { | ||
135 | var technique = material.shaderProgram['default']; | ||
136 | var renderer = RDGE.globals.engine.getContext().renderer; | ||
137 | if (renderer && technique) | ||
138 | { | ||
139 | var texMapName = this._propValues[this._propNames[index]]; | ||
140 | var wrap = 'REPEAT', mips = true; | ||
141 | var tex = this.loadTexture( texMapName, wrap, mips ); | ||
142 | |||
143 | if (tex) | ||
144 | { | ||
145 | switch (index) | ||
146 | { | ||
147 | case 1: technique.u_colMap.set( tex ); break; | ||
148 | case 2: technique.u_normalMap.set( tex ); break; | ||
149 | case 3: technique.u_glowMap.set( tex ); break; | ||
150 | default: console.log( "invalid map index in BumpMetalMaterial, " + index ); | ||
151 | } | ||
152 | } | ||
153 | } | ||
154 | } | ||
155 | }; | ||
156 | |||
157 | this.exportJSON = function() | ||
158 | { | ||
159 | var jObj = | ||
160 | { | ||
161 | 'material' : this.getShaderName(), | ||
162 | 'name' : this.getName(), | ||
163 | 'lightDiff' : this.getLightDiff(), | ||
164 | 'diffuseTexture' : this.getDiffuseTexture(), | ||
165 | 'specularTexture' : this.getSpecularTexture(), | ||
166 | 'normalMap' : this.getNormalTexture() | ||
167 | }; | ||
168 | |||
169 | return jObj; | ||
170 | }; | ||
171 | |||
172 | this.importJSON = function( jObj ) | ||
173 | { | ||
174 | if (this.getShaderName() != jObj.material) throw new Error( "ill-formed material" ); | ||
175 | this.setName( jObj.name ); | ||
176 | |||
177 | try | ||
178 | { | ||
179 | var lightDiff = jObj.lightDiff, | ||
180 |