diff options
Diffstat (limited to 'js/lib/rdge/materials/uber-material.js')
-rwxr-xr-x | js/lib/rdge/materials/uber-material.js | 118 |
1 files changed, 87 insertions, 31 deletions
diff --git a/js/lib/rdge/materials/uber-material.js b/js/lib/rdge/materials/uber-material.js index e94458cc..a2c782d8 100755 --- a/js/lib/rdge/materials/uber-material.js +++ b/js/lib/rdge/materials/uber-material.js | |||
@@ -6,6 +6,7 @@ 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 | /////////////////////////////////////////////////////////////////////// |
@@ -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,6 +36,9 @@ 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 | ||
39 | /////////////////////////////////////////////////////////////////////// | 44 | /////////////////////////////////////////////////////////////////////// |
@@ -188,6 +193,7 @@ var UberMaterial = function UberMaterial() { | |||
188 | if (material) { | 193 | if (material) { |
189 | var technique = material.shaderProgram.defaultTechnique; | 194 | var technique = material.shaderProgram.defaultTechnique; |
190 | technique.u_diffuseColor.set(this._diffuseColor); | 195 | technique.u_diffuseColor.set(this._diffuseColor); |
196 | this.getWorld().restartRenderLoop(); | ||
191 | } | 197 | } |
192 | }; | 198 | }; |
193 | 199 | ||
@@ -249,33 +255,58 @@ var UberMaterial = function UberMaterial() { | |||
249 | } | 255 | } |
250 | }; | 256 | }; |
251 | 257 | ||
252 | this.updateDiffuseMap = function (value) { | 258 | this.updateDiffuseMap = function(value) { |
253 | var value = this._propValues["diffuseMap"]; | 259 | var value = this._propValues[ "diffuseMap" ]; |
254 | this._diffuseMapOb.texture = value; | 260 | this._diffuseMapOb.texture = value; |
261 | |||
262 | if ((value == null) || (value.length == 0)) { | ||
263 | if (this._useDiffuseMap) { | ||
264 | this._useDiffuseMap = false; | ||
265 | this._diffuseTexture = undefined; | ||
266 | this.rebuildShader(); | ||
267 | } | ||
268 | } else { | ||
269 | if (!this._useDiffuseMap) { | ||
270 | this._useDiffuseMap = true; | ||
271 | this.rebuildShader(); | ||
272 | } else { | ||
273 | var material = this._materialNode; | ||
274 | if (material) { | ||
275 | var technique = material.shaderProgram.defaultTechnique; | ||
276 | var renderer = RDGE.globals.engine.getContext().renderer; | ||
277 | if (renderer && technique) { | ||
278 | this._diffuseTexture = new Texture( this.getWorld(), value, caps.diffuseMap.wrap ); | ||
279 | var tex = this._diffuseTexture.getTexture(); | ||
280 | technique.s_diffuseMap.set( tex ); | ||
281 | } | ||
282 | } | ||
283 | } | ||
284 | } | ||
285 | }; | ||
286 | |||
287 | |||
288 | this.updateTextures = function() | ||
289 | { | ||
290 | var material = this._materialNode; | ||
291 | if (material) | ||
292 | { | ||
293 | var technique = material.shaderProgram.defaultTechnique; | ||
294 | var renderer = RDGE_globals.engine.getContext().renderer; | ||
295 | if (renderer && technique) | ||
296 | { | ||
297 | if (this._diffuseTexture) | ||
298 | { | ||
299 | if (!this._diffuseTexture.isAnimated()) | ||
300 | { | ||
301 | this._diffuseTexture.render(); | ||
302 | var tex = this._diffuseTexture.getTexture(); | ||
303 | technique.s_diffuseMap.set( tex ); | ||
304 | } | ||
305 | } | ||
306 | } | ||
307 | } | ||
308 | } | ||
255 | 309 | ||
256 | if ((value == null) || (value.length == 0)) { | ||
257 | if (this._useDiffuseMap) { | ||
258 | this._useDiffuseMap = false; | ||
259 | this.rebuildShader(); | ||
260 | } | ||
261 | } else { | ||
262 | if (!this._useDiffuseMap) { | ||
263 | this._useDiffuseMap = true; | ||
264 | this.rebuildShader(); | ||
265 | } else { | ||
266 | var material = this._materialNode; | ||
267 | if (material) { | ||
268 | var technique = material.shaderProgram.defaultTechnique; | ||
269 | var renderer = RDGE.globals.engine.getContext().renderer; | ||
270 | if (renderer && technique) { | ||
271 | var tex = renderer.getTextureByName(value, caps.diffuseMap.wrap); | ||
272 | this.registerTexture(tex); | ||
273 | technique.s_diffuseMap.set(tex); | ||
274 | } | ||
275 | } | ||
276 | } | ||
277 | } | ||
278 | }; | ||
279 | 310 | ||
280 | this.updateSpecularMap = function () { | 311 | this.updateSpecularMap = function () { |
281 | var value = this._propValues["specularMap"]; | 312 | var value = this._propValues["specularMap"]; |
@@ -367,7 +398,32 @@ var UberMaterial = function UberMaterial() { | |||
367 | this._materialNode.setShader(this._shader); | 398 | this._materialNode.setShader(this._shader); |
368 | }; | 399 | }; |
369 | 400 | ||
370 | this.importJSON = function (jObj) { | 401 | this.update = function() |
402 | { | ||
403 | var material = this._materialNode; | ||
404 | if (material) | ||
405 | { | ||
406 | var technique = material.shaderProgram.defaultTechnique; | ||
407 | var renderer = RDGE.globals.engine.getContext().renderer; | ||
408 | if (renderer && technique) | ||
409 | { | ||
410 | if (this._diffuseTexture && this._diffuseTexture.isAnimated()) | ||
411 | { | ||
412 | this._diffuseTexture.render(); | ||
413 | technique.s_diffuseMap.set( this._diffuseTexture.getTexture() ); | ||
414 | } | ||
415 | } | ||
416 | } | ||
417 | } | ||
418 | |||
419 | this.isAnimated = function() | ||
420 | { | ||
421 | var anim = (this._diffuseTexture && this._diffuseTexture.isAnimated()); | ||
422 | return anim; | ||
423 | } | ||
424 | |||
425 | this.importJSON = function (jObj) | ||
426 | { | ||
371 | if (this.getShaderName() != jObj.material) throw new Error("ill-formed material"); | 427 | if (this.getShaderName() != jObj.material) throw new Error("ill-formed material"); |
372 | this.setName(jObj.name); | 428 | this.setName(jObj.name); |
373 | 429 | ||
@@ -682,9 +738,9 @@ var UberMaterial = function UberMaterial() { | |||
682 | 738 | ||
683 | var renderer = RDGE.globals.engine.getContext().renderer; | 739 | var renderer = RDGE.globals.engine.getContext().renderer; |
684 | if (this._useDiffuseMap) { | 740 | if (this._useDiffuseMap) { |
685 | var tex = renderer.getTextureByName(caps.diffuseMap.texture, caps.diffuseMap.wrap, caps.diffuseMap.mips); | 741 | this._diffuseTexture = new Texture( this.getWorld(), caps.diffuseMap.texture, caps.diffuseMap.wrap, caps.diffuseMap.mips ); |
686 | this.registerTexture(tex); | 742 | var tex = this._diffuseTexture.getTexture(); |
687 | technique.s_diffuseMap.set(tex); | 743 | technique.s_diffuseMap.set( tex ); |
688 | } | 744 | } |
689 | 745 | ||
690 | if (this._useNormalMap) { | 746 | if (this._useNormalMap) { |