diff options
Diffstat (limited to 'js/lib/rdge/materials/uber-material.js')
-rwxr-xr-x | js/lib/rdge/materials/uber-material.js | 124 |
1 files changed, 81 insertions, 43 deletions
diff --git a/js/lib/rdge/materials/uber-material.js b/js/lib/rdge/materials/uber-material.js index e94458cc..febf765f 100755 --- a/js/lib/rdge/materials/uber-material.js +++ b/js/lib/rdge/materials/uber-material.js | |||
@@ -6,12 +6,13 @@ 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; | ||
9 | 10 | ||
10 | var UberMaterial = function UberMaterial() { | 11 | var UberMaterial = function UberMaterial() { |
11 | /////////////////////////////////////////////////////////////////////// | 12 | /////////////////////////////////////////////////////////////////////// |
12 | // Instance variables | 13 | // Instance variables |
13 | /////////////////////////////////////////////////////////////////////// | 14 | /////////////////////////////////////////////////////////////////////// |
14 | this._name = "UberMaterial"; | 15 | this._name = "Uber"; |
15 | this._shaderName = "uber"; | 16 | this._shaderName = "uber"; |
16 | this.getShaderName = function () { return this._shaderName; }; | 17 | this.getShaderName = function () { return this._shaderName; }; |
17 | 18 | ||
@@ -23,7 +24,8 @@ var UberMaterial = function UberMaterial() { | |||
23 | this._environmentAmount = 0.2; // 0 .. 1 | 24 | this._environmentAmount = 0.2; // 0 .. 1 |
24 | 25 | ||
25 | // set the default maps | 26 | // set the default maps |
26 | this._diffuseMapOb = { 'texture': 'assets/images/rocky-diffuse.jpg', 'wrap': 'REPEAT' }; | 27 | this._diffuseMapOb = { 'texture' : 'assets/images/rocky-diffuse.jpg', 'wrap' : 'REPEAT' }; |
28 | //this._diffuseMapOb = { 'texture' : 'texture', 'wrap' : 'REPEAT' }; | ||
27 | this._normalMapOb = { 'texture': 'assets/images/rocky-normal.jpg', 'wrap': 'REPEAT' }; | 29 | this._normalMapOb = { 'texture': 'assets/images/rocky-normal.jpg', 'wrap': 'REPEAT' }; |
28 | this._specularMapOb = { 'texture': 'assets/images/rocky-spec.jpg', 'wrap': 'REPEAT' }; | 30 | this._specularMapOb = { 'texture': 'assets/images/rocky-spec.jpg', 'wrap': 'REPEAT' }; |
29 | this._environmentMapOb = { 'texture': 'assets/images/silver.png', 'wrap': 'CLAMP', 'envReflection': this._environmentAmount }; | 31 | this._environmentMapOb = { 'texture': 'assets/images/silver.png', 'wrap': 'CLAMP', 'envReflection': this._environmentAmount }; |
@@ -34,8 +36,14 @@ var UberMaterial = function UberMaterial() { | |||
34 | this._useEnvironmentMap = true; | 36 | this._useEnvironmentMap = true; |
35 | this._useLights = [true, true, true, true]; | 37 | this._useLights = [true, true, true, true]; |
36 | 38 | ||
39 | // these are the abstracted texture objects - defined where they are set | ||
40 | this._diffuseTexture; | ||
41 | |||
37 | this._MAX_LIGHTS = 4; | 42 | this._MAX_LIGHTS = 4; |
38 | 43 | ||
44 | // array textures indexed by shader uniform name | ||
45 | this._glTextures = []; | ||
46 | |||
39 | /////////////////////////////////////////////////////////////////////// | 47 | /////////////////////////////////////////////////////////////////////// |
40 | // Material Property Accessors | 48 | // Material Property Accessors |
41 | /////////////////////////////////////////////////////////////////////// | 49 | /////////////////////////////////////////////////////////////////////// |
@@ -175,6 +183,7 @@ var UberMaterial = function UberMaterial() { | |||
175 | this.updateAmbientColor = function () { | 183 | this.updateAmbientColor = function () { |
176 | this._ambientColor = this._propValues['ambientColor'].slice(0); | 184 | this._ambientColor = this._propValues['ambientColor'].slice(0); |
177 | var material = this._materialNode; | 185 | var material = this._materialNode; |
186 | //console.log( "ambient color: " + this._ambientColor ); | ||
178 | if (material) { | 187 | if (material) { |
179 | var technique = material.shaderProgram.defaultTechnique; | 188 | var technique = material.shaderProgram.defaultTechnique; |
180 | technique.u_ambientColor.set(this._ambientColor); | 189 | technique.u_ambientColor.set(this._ambientColor); |
@@ -188,6 +197,7 @@ var UberMaterial = function UberMaterial() { | |||
188 | if (material) { | 197 | if (material) { |
189 | var technique = material.shaderProgram.defaultTechnique; | 198 | var technique = material.shaderProgram.defaultTechnique; |
190 | technique.u_diffuseColor.set(this._diffuseColor); | 199 | technique.u_diffuseColor.set(this._diffuseColor); |
200 | this.getWorld().restartRenderLoop(); | ||
191 | } | 201 | } |
192 | }; | 202 | }; |
193 | 203 | ||
@@ -240,42 +250,43 @@ var UberMaterial = function UberMaterial() { | |||
240 | var technique = material.shaderProgram.defaultTechnique; | 250 | var technique = material.shaderProgram.defaultTechnique; |
241 | var renderer = RDGE.globals.engine.getContext().renderer; | 251 | var renderer = RDGE.globals.engine.getContext().renderer; |
242 | if (renderer && technique) { | 252 | if (renderer && technique) { |
243 | var tex = renderer.getTextureByName(value, caps.environmentMap.wrap); | 253 | var tex = renderer.getTextureByName(value, this._ubershaderCaps.environmentMap.wrap); |
244 | this.registerTexture(tex); | 254 | this.registerTexture(tex); |
245 | technique.s_environmentMap.set(tex); | 255 | technique.s_envMap.set(tex); |
246 | } | 256 | } |
247 | } | 257 | } |
248 | } | 258 | } |
249 | } | 259 | } |
250 | }; | 260 | }; |
251 | 261 | ||
252 | this.updateDiffuseMap = function (value) { | 262 | this.updateDiffuseMap = function(value) { |
253 | var value = this._propValues["diffuseMap"]; | 263 | var value = this._propValues[ "diffuseMap" ]; |
254 | this._diffuseMapOb.texture = value; | 264 | this._diffuseMapOb.texture = value; |
255 | 265 | ||
256 | if ((value == null) || (value.length == 0)) { | 266 | if ((value == null) || (value.length == 0)) { |
257 | if (this._useDiffuseMap) { | 267 | if (this._useDiffuseMap) { |
258 | this._useDiffuseMap = false; | 268 | this._useDiffuseMap = false; |
259 | this.rebuildShader(); | 269 | this._diffuseTexture = undefined; |
260 | } | 270 | this.rebuildShader(); |
261 | } else { | 271 | } |
262 | if (!this._useDiffuseMap) { | 272 | } else { |
263 | this._useDiffuseMap = true; | 273 | if (!this._useDiffuseMap) { |
264 | this.rebuildShader(); | 274 | this._useDiffuseMap = true; |
265 | } else { | 275 | this.rebuildShader(); |
266 | var material = this._materialNode; | 276 | } else { |
267 | if (material) { | 277 | var material = this._materialNode; |
268 | var technique = material.shaderProgram.defaultTechnique; | 278 | if (material) { |
269 | var renderer = RDGE.globals.engine.getContext().renderer; | 279 | var technique = material.shaderProgram.defaultTechnique; |
270 | if (renderer && technique) { | 280 | var renderer = RDGE.globals.engine.getContext().renderer; |
271 | var tex = renderer.getTextureByName(value, caps.diffuseMap.wrap); | 281 | if (renderer && technique) { |
272 | this.registerTexture(tex); | 282 | this._diffuseTexture = new Texture( this.getWorld(), value, this._ubershaderCaps.diffuseMap.wrap ); |
273 | technique.s_diffuseMap.set(tex); | 283 | var tex = this._diffuseTexture.getTexture(); |
274 | } | 284 | technique.s_diffuseMap.set( tex ); |
275 | } | 285 | } |
276 | } | 286 | } |
277 | } | 287 | } |
278 | }; | 288 | } |
289 | }; | ||
279 | 290 | ||
280 | this.updateSpecularMap = function () { | 291 | this.updateSpecularMap = function () { |
281 | var value = this._propValues["specularMap"]; | 292 | var value = this._propValues["specularMap"]; |
@@ -296,7 +307,7 @@ var UberMaterial = function UberMaterial() { | |||
296 | var technique = material.shaderProgram.defaultTechnique; | 307 | var technique = material.shaderProgram.defaultTechnique; |
297 | var renderer = RDGE.globals.engine.getContext().renderer; | 308 | var renderer = RDGE.globals.engine.getContext().renderer; |
298 | if (renderer && technique) { | 309 | if (renderer && technique) { |
299 | var tex = renderer.getTextureByName(value, caps.specularMap.wrap); | 310 | var tex = renderer.getTextureByName(value, this._ubershaderCaps.specularMap.wrap); |
300 | this.registerTexture(tex); | 311 | this.registerTexture(tex); |
301 | technique.s_specularMap.set(tex); | 312 | technique.s_specularMap.set(tex); |
302 | } | 313 | } |
@@ -324,7 +335,7 @@ var UberMaterial = function UberMaterial() { | |||
324 | var technique = material.shaderProgram.defaultTechnique; | 335 | var technique = material.shaderProgram.defaultTechnique; |
325 | var renderer = RDGE.globals.engine.getContext().renderer; | 336 | var renderer = RDGE.globals.engine.getContext().renderer; |
326 | if (renderer && technique) { | 337 | if (renderer && technique) { |
327 | var tex = renderer.getTextureByName(value, caps.normalMap.wrap); | 338 | var tex = renderer.getTextureByName(value, this._ubershaderCaps.normalMap.wrap); |
328 | this.registerTexture(tex); | 339 | this.registerTexture(tex); |
329 | technique.s_normalMap.set(tex); | 340 | technique.s_normalMap.set(tex); |
330 | } | 341 | } |
@@ -335,9 +346,13 @@ var UberMaterial = function UberMaterial() { | |||
335 | 346 | ||
336 | // duplcate method requirde | 347 | // duplcate method requirde |
337 | this.dup = function () { | 348 | this.dup = function () { |
338 | // allocate a new uber material | ||
339 | var newMat = new UberMaterial(); | ||
340 | 349 | ||
350 | // get the current values; | ||
351 | var propNames = [], propValues = [], propTypes = [], propLabels = []; | ||
352 | this.getAllProperties(propNames, propValues, propTypes, propLabels); | ||
353 | |||
354 | // allocate a new material | ||
355 | var newMat = new UberMaterial(); | ||
341 | newMat._useDiffuseMap = this._useDiffuseMap; | 356 | newMat._useDiffuseMap = this._useDiffuseMap; |
342 | newMat._useEnvironmentMap = this._useEnvironmentMap; | 357 | newMat._useEnvironmentMap = this._useEnvironmentMap; |
343 | newMat._useLights = this._useLights; | 358 | newMat._useLights = this._useLights; |
@@ -345,9 +360,7 @@ var UberMaterial = function UberMaterial() { | |||
345 | newMat._useSpecularMap = this._useSpecularMap; | 360 | newMat._useSpecularMap = this._useSpecularMap; |
346 | newMat.rebuildShader(); | 361 | newMat.rebuildShader(); |
347 | 362 | ||
348 | // copy over the current values; | 363 | // copy over the current values; |
349 | var propNames = [], propValues = [], propTypes = [], propLabels = []; | ||
350 | this.getAllProperties(propNames, propValues, propTypes, propLabels); | ||
351 | var n = propNames.length; | 364 | var n = propNames.length; |
352 | for (var i = 0; i < n; i++) | 365 | for (var i = 0; i < n; i++) |
353 | newMat.setProperty(propNames[i], propValues[i]); | 366 | newMat.setProperty(propNames[i], propValues[i]); |
@@ -367,7 +380,32 @@ var UberMaterial = function UberMaterial() { | |||
367 | this._materialNode.setShader(this._shader); | 380 | this._materialNode.setShader(this._shader); |
368 | }; | 381 | }; |
369 | 382 | ||
370 | this.importJSON = function (jObj) { | 383 | this.update = function() |
384 | { | ||
385 | var material = this._materialNode; | ||
386 | if (material) | ||
387 | { | ||
388 | var technique = material.shaderProgram.defaultTechnique; | ||
389 | var renderer = RDGE.globals.engine.getContext().renderer; | ||
390 | if (renderer && technique) | ||
391 | { | ||
392 | if (this._diffuseTexture && this._diffuseTexture.isAnimated()) | ||
393 | { | ||
394 | this._diffuseTexture.render(); | ||
395 | technique.s_diffuseMap.set( this._diffuseTexture.getTexture() ); | ||
396 |