diff options
Diffstat (limited to 'js/lib/rdge/materials/uber-material.js')
-rwxr-xr-x | js/lib/rdge/materials/uber-material.js | 283 |
1 files changed, 150 insertions, 133 deletions
diff --git a/js/lib/rdge/materials/uber-material.js b/js/lib/rdge/materials/uber-material.js index 416cb980..7c7dced4 100755 --- a/js/lib/rdge/materials/uber-material.js +++ b/js/lib/rdge/materials/uber-material.js | |||
@@ -45,11 +45,11 @@ var UberMaterial = function UberMaterial() { | |||
45 | this._diffuseColor = [1.0, 1.0, 1.0, 1.0]; | 45 | this._diffuseColor = [1.0, 1.0, 1.0, 1.0]; |
46 | this._specularColor = [1.0, 1.0, 1.0, 1.0]; | 46 | this._specularColor = [1.0, 1.0, 1.0, 1.0]; |
47 | this._specularPower = 32.0; | 47 | this._specularPower = 32.0; |
48 | this._environmentAmount = 0.2; // 0 .. 1 | 48 | this._environmentAmount = 0.2; // 0 .. 1 |
49 | 49 | ||
50 | // set the default maps | 50 | // set the default maps |
51 | this._diffuseMapOb = { 'texture' : 'assets/images/rocky-diffuse.jpg', 'wrap' : 'REPEAT' }; | 51 | this._diffuseMapOb = { 'texture' : 'assets/images/rocky-diffuse.jpg', 'wrap' : 'REPEAT' }; |
52 | //this._diffuseMapOb = { 'texture' : 'texture', 'wrap' : 'REPEAT' }; | 52 | //this._diffuseMapOb = { 'texture' : 'texture', 'wrap' : 'REPEAT' }; |
53 | this._normalMapOb = { 'texture': 'assets/images/rocky-normal.jpg', 'wrap': 'REPEAT' }; | 53 | this._normalMapOb = { 'texture': 'assets/images/rocky-normal.jpg', 'wrap': 'REPEAT' }; |
54 | this._specularMapOb = { 'texture': 'assets/images/rocky-spec.jpg', 'wrap': 'REPEAT' }; | 54 | this._specularMapOb = { 'texture': 'assets/images/rocky-spec.jpg', 'wrap': 'REPEAT' }; |
55 | this._environmentMapOb = { 'texture': 'assets/images/silver.png', 'wrap': 'CLAMP', 'envReflection': this._environmentAmount }; | 55 | this._environmentMapOb = { 'texture': 'assets/images/silver.png', 'wrap': 'CLAMP', 'envReflection': this._environmentAmount }; |
@@ -86,6 +86,23 @@ var UberMaterial = function UberMaterial() { | |||
86 | this._propValues[this._propNames[7]] = this._environmentMapOb['texture']; | 86 | this._propValues[this._propNames[7]] = this._environmentMapOb['texture']; |
87 | this._propValues[this._propNames[8]] = this._environmentMapOb['envReflection']; | 87 | this._propValues[this._propNames[8]] = this._environmentMapOb['envReflection']; |
88 | 88 | ||
89 | this.resetToDefault = function() | ||
90 | { | ||
91 | this._propValues[this._propNames[0]] = this._ambientColor.slice(0); | ||
92 | this._propValues[this._propNames[1]] = this._diffuseColor.slice(0); | ||
93 | this._propValues[this._propNames[2]] = this._specularColor.slice(0); | ||
94 | this._propValues[this._propNames[3]] = this._specularPower; | ||
95 | this._propValues[this._propNames[4]] = this._diffuseMapOb['texture']; | ||
96 | this._propValues[this._propNames[5]] = this._normalMapOb['texture']; | ||
97 | this._propValues[this._propNames[6]] = this._specularMapOb['texture']; | ||
98 | this._propValues[this._propNames[7]] = this._environmentMapOb['texture']; | ||
99 | this._propValues[this._propNames[8]] = this._environmentMapOb['envReflection']; | ||
100 | |||
101 | var nProps = this._propNames.length; | ||
102 | for (var i=0; i<nProps; i++) | ||
103 | this.setProperty( this._propNames[i], this._propValues[this._propNames[i]] ); | ||
104 | }; | ||
105 | |||
89 | this.setProperty = function (prop, value) { | 106 | this.setProperty = function (prop, value) { |
90 | if (prop == "color") prop = "ambientColor"; | 107 | if (prop == "color") prop = "ambientColor"; |
91 | var valid = this.validateProperty(prop, value); | 108 | var valid = this.validateProperty(prop, value); |
@@ -140,86 +157,86 @@ var UberMaterial = function UberMaterial() { | |||
140 | // define the 4 lights | 157 | // define the 4 lights |
141 | this._lights = [ | 158 | this._lights = [ |
142 | 159 | ||
143 | { | 160 | { |
144 | 'type': 'point', // can be 'directional', 'point' or 'spot' | 161 | 'type': 'point', // can be 'directional', 'point' or 'spot' |
145 | 'spotInnerCutoff': 14.0, // fragments in the inner cutoff 'cone' are full intensity. | 162 | 'spotInnerCutoff': 14.0, // fragments in the inner cutoff 'cone' are full intensity. |
146 | 'spotOuterCutoff': 15.0, // fragments outside the outer cutoff 'cone' are unlit. | 163 | 'spotOuterCutoff': 15.0, // fragments outside the outer cutoff 'cone' are unlit. |
147 | 'position': [8.0, 2.0, 8.0], // light position; ignored for directional lights | 164 | 'position': [8.0, 2.0, 8.0], // light position; ignored for directional lights |
148 | 'direction': [-1.0, -1.0, -1.0], // light direction; ignored for point lights | 165 | 'direction': [-1.0, -1.0, -1.0], // light direction; ignored for point lights |
149 | 'attenuation': [1.0, 0.025, 0.00125], // light attenuation; constant, linear, quadratic | 166 | 'attenuation': [1.0, 0.025, 0.00125], // light attenuation; constant, linear, quadratic |
150 | 'diffuseColor': [1.0, 0.5, 0.5, 1.0], // diffuse light color | 167 | 'diffuseColor': [1.0, 0.5, 0.5, 1.0], // diffuse light color |
151 | 'specularColor': [1.0, 1.0, 1.0, 1.0] // specular light color | 168 | 'specularColor': [1.0, 1.0, 1.0, 1.0] // specular light color |
152 | }, | 169 | }, |
153 | { | 170 | { |
154 | 'type': 'point', | 171 | 'type': 'point', |
155 | 'spotInnerCutoff': 9.0, | 172 | 'spotInnerCutoff': 9.0, |
156 | 'spotOuterCutoff': 20.0, | 173 | 'spotOuterCutoff': 20.0, |
157 | 'position': [-8.0, 2.0, 8.0], | 174 | 'position': [-8.0, 2.0, 8.0], |
158 | 'direction': [1.0, -1.0, -1.0], | 175 | 'direction': [1.0, -1.0, -1.0], |
159 | 'attenuation': [1.0, 0.025, 0.00125], | 176 | 'attenuation': [1.0, 0.025, 0.00125], |
160 | 'diffuseColor': [0.5, 1.0, 0.5, 1.0], | 177 | 'diffuseColor': [0.5, 1.0, 0.5, 1.0], |
161 | 'specularColor': [1.0, 1.0, 1.0, 1.0] | 178 | 'specularColor': [1.0, 1.0, 1.0, 1.0] |
162 | }, | 179 | }, |
163 | { | 180 | { |
164 | 'type': 'point', | 181 | 'type': 'point', |
165 | 'spotInnerCutoff': 9.0, | 182 | 'spotInnerCutoff': 9.0, |
166 | 'spotOuterCutoff': 20.0, | 183 | 'spotOuterCutoff': 20.0, |
167 | 'position': [-8.0, 2.0, -8.0], | 184 | 'position': [-8.0, 2.0, -8.0], |
168 | 'direction': [1.0, -1.0, 1.0], | 185 | 'direction': [1.0, -1.0, 1.0], |
169 | 'attenuation': [1.0, 0.25, 0.0125], | 186 | 'attenuation': [1.0, 0.25, 0.0125], |
170 | 'diffuseColor': [0.5, 0.5, 1.0, 1.0], | 187 | 'diffuseColor': [0.5, 0.5, 1.0, 1.0], |
171 | 'specularColor': [1.0, 1.0, 1.0, 1.0] | 188 | 'specularColor': [1.0, 1.0, 1.0, 1.0] |
172 | }, | 189 | }, |
173 | { | 190 | { |
174 | 'type': 'point', | 191 | 'type': 'point', |
175 | 'spotInnerCutoff': 9.0, | 192 | 'spotInnerCutoff': 9.0, |
176 | 'spotOuterCutoff': 20.0, | 193 | 'spotOuterCutoff': 20.0, |
177 | 'position': [8.0, 4.0, -8.0], | 194 | 'position': [8.0, 4.0, -8.0], |
178 | 'direction': [-1.0, -1.0, 1.0], | 195 | 'direction': [-1.0, -1.0, 1.0], |
179 | 'attenuation': [1.0, 0.25, 0.0125], | 196 | 'attenuation': [1.0, 0.25, 0.0125], |
180 | 'diffuseColor': [1.0, 1.0, 0.5, 1.0], | 197 | 'diffuseColor': [1.0, 1.0, 0.5, 1.0], |
181 | 'specularColor': [1.0, 1.0, 1.0, 1.0] | 198 | 'specularColor': [1.0, 1.0, 1.0, 1.0] |
182 | } | 199 | } |
183 | ]; | 200 | ]; |
184 | 201 | ||
185 | this._ubershaderCaps = | 202 | this._ubershaderCaps = |
186 | { | 203 | { |
187 | // ubershader material properties. | 204 | // ubershader material properties. |
188 | 'material': { | 205 | 'material': { |
189 | 'ambientColor': this._ambientColor, // material ambient color | 206 | 'ambientColor': this._ambientColor, // material ambient color |
190 | 'diffuseColor': this._diffuseColor, // material diffuse color | 207 | 'diffuseColor': this._diffuseColor, // material diffuse color |
191 | 'specularColor': this._specularColor, // material specular color | 208 | 'specularColor': this._specularColor, // material specular color |
192 | 'specularPower': this._specularPower // material specular power (shininess) | 209 | 'specularPower': this._specularPower // material specular power (shininess) |
193 | }, | 210 | }, |
194 | 211 | ||
195 | // ubershader supports up to four lights. | 212 | // ubershader supports up to four lights. |
196 | 'lighting': { | 213 | 'lighting': { |
197 | 'light0': this._lights[0], | 214 | 'light0': this._lights[0], |
198 | 'light1': this._lights[1], | 215 | 'light1': this._lights[1], |
199 | 'light2': this._lights[2], | 216 | 'light2': this._lights[2], |
200 | 'light3': this._lights[3] | 217 | 'light3': this._lights[3] |
201 | }, | 218 | }, |
202 | 219 | ||
203 | // uvTransform can be used to scale or offset the texture coordinates. | 220 | // uvTransform can be used to scale or offset the texture coordinates. |
204 | 'uvTransform': [2.0, 0, 0, 0, 0, 2.0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1], | 221 | 'uvTransform': [2.0, 0, 0, 0, 0, 2.0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1], |
205 | 222 | ||
206 | // optional diffuse map | 223 | // optional diffuse map |
207 | 'diffuseMap': this._diffuseMapOb, | 224 | 'diffuseMap': this._diffuseMapOb, |
208 | 225 | ||
209 | // optional normal map | 226 | // optional normal map |
210 | 'normalMap': this._normalMapOb, | 227 | 'normalMap': this._normalMapOb, |
211 | 228 | ||
212 | // optional specular map | 229 | // optional specular map |
213 | 'specularMap': this._specularMapOb, | 230 | 'specularMap': this._specularMapOb, |
214 | 231 | ||
215 | // optional environment map | 232 | // optional environment map |
216 | 'environmentMap': this._environmentMapOb | 233 | 'environmentMap': this._environmentMapOb |
217 | }; | 234 | }; |
218 | 235 | ||
219 | this.updateAmbientColor = function () { | 236 | this.updateAmbientColor = function () { |
220 | this._ambientColor = this._propValues['ambientColor'].slice(0); | 237 | this._ambientColor = this._propValues['ambientColor'].slice(0); |
221 | var material = this._materialNode; | 238 | var material = this._materialNode; |
222 | //console.log( "ambient color: " + this._ambientColor ); | 239 | //console.log( "ambient color: " + this._ambientColor ); |
223 | if (material) { | 240 | if (material) { |
224 | var technique = material.shaderProgram.defaultTechnique; | 241 | var technique = material.shaderProgram.defaultTechnique; |
225 | technique.u_ambientColor.set(this._ambientColor); | 242 | technique.u_ambientColor.set(this._ambientColor); |
@@ -295,34 +312,34 @@ var UberMaterial = function UberMaterial() { | |||
295 | } | 312 | } |
296 | }; | 313 | }; |
297 | 314 | ||
298 | this.updateDiffuseMap = function(value) { | 315 | this.updateDiffuseMap = function(value) { |
299 | var value = this._propValues[ "diffuseMap" ]; | 316 | var value = this._propValues[ "diffuseMap" ]; |
300 | this._diffuseMapOb.texture = value; | 317 | this._diffuseMapOb.texture = value; |
301 | 318 | ||
302 | if ((value == null) || (value.length == 0)) { | 319 | if ((value == null) || (value.length == 0)) { |
303 | if (this._useDiffuseMap) { | 320 | if (this._useDiffuseMap) { |
304 | this._useDiffuseMap = false; | 321 | this._useDiffuseMap = false; |
305 | this._diffuseTexture = undefined; | 322 | this._diffuseTexture = undefined; |
306 | this.rebuildShader(); | 323 | this.rebuildShader(); |
307 | } | 324 | } |
308 | } else { | 325 | } else { |
309 | if (!this._useDiffuseMap) { | 326 | if (!this._useDiffuseMap) { |
310 | this._useDiffuseMap = true; | 327 | this._useDiffuseMap = true; |
311 | this.rebuildShader(); | 328 | this.rebuildShader(); |
312 | } else { | 329 | } else { |
313 | var material = this._materialNode; |