aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge/materials/uber-material.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib/rdge/materials/uber-material.js')
-rwxr-xr-xjs/lib/rdge/materials/uber-material.js108
1 files changed, 71 insertions, 37 deletions
diff --git a/js/lib/rdge/materials/uber-material.js b/js/lib/rdge/materials/uber-material.js
index e94458cc..9d0cc564 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
7var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser; 7var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser;
8var Material = require("js/lib/rdge/materials/material").Material; 8var Material = require("js/lib/rdge/materials/material").Material;
9var Texture = require("js/lib/rdge/texture").Texture;
9 10
10var UberMaterial = function UberMaterial() { 11var 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,34 @@ 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;
255 261
256 if ((value == null) || (value.length == 0)) { 262 if ((value == null) || (value.length == 0)) {
257 if (this._useDiffuseMap) { 263 if (this._useDiffuseMap) {
258 this._useDiffuseMap = false; 264 this._useDiffuseMap = false;
259 this.rebuildShader(); 265 this._diffuseTexture = undefined;
260 } 266 this.rebuildShader();
261 } else { 267 }
262 if (!this._useDiffuseMap) { 268 } else {
263 this._useDiffuseMap = true; 269 if (!this._useDiffuseMap) {
264 this.rebuildShader(); 270 this._useDiffuseMap = true;
265 } else { 271 this.rebuildShader();
266 var material = this._materialNode; 272 } else {
267 if (material) { 273 var material = this._materialNode;
268 var technique = material.shaderProgram.defaultTechnique; 274 if (material) {
269 var renderer = RDGE.globals.engine.getContext().renderer; 275 var technique = material.shaderProgram.defaultTechnique;
270 if (renderer && technique) { 276 var renderer = RDGE.globals.engine.getContext().renderer;
271 var tex = renderer.getTextureByName(value, caps.diffuseMap.wrap); 277 if (renderer && technique) {
272 this.registerTexture(tex); 278 this._diffuseTexture = new Texture( this.getWorld(), value, caps.diffuseMap.wrap );
273 technique.s_diffuseMap.set(tex); 279 var tex = this._diffuseTexture.getTexture();
274 } 280 technique.s_diffuseMap.set( tex );
275 } 281 }
276 } 282 }
277 } 283 }
278 }; 284 }
285 };
279 286
280 this.updateSpecularMap = function () { 287 this.updateSpecularMap = function () {
281 var value = this._propValues["specularMap"]; 288 var value = this._propValues["specularMap"];
@@ -335,9 +342,13 @@ var UberMaterial = function UberMaterial() {
335 342
336 // duplcate method requirde 343 // duplcate method requirde
337 this.dup = function () { 344 this.dup = function () {
338 // allocate a new uber material
339 var newMat = new UberMaterial();
340 345
346 // get the current values;
347 var propNames = [], propValues = [], propTypes = [], propLabels = [];
348 this.getAllProperties(propNames, propValues, propTypes, propLabels);
349
350 // allocate a new material
351 var newMat = new UberMaterial();
341 newMat._useDiffuseMap = this._useDiffuseMap; 352 newMat._useDiffuseMap = this._useDiffuseMap;
342 newMat._useEnvironmentMap = this._useEnvironmentMap; 353 newMat._useEnvironmentMap = this._useEnvironmentMap;
343 newMat._useLights = this._useLights; 354 newMat._useLights = this._useLights;
@@ -345,9 +356,7 @@ var UberMaterial = function UberMaterial() {
345 newMat._useSpecularMap = this._useSpecularMap; 356 newMat._useSpecularMap = this._useSpecularMap;
346 newMat.rebuildShader(); 357 newMat.rebuildShader();
347 358
348 // copy over the current values; 359 // copy over the current values;
349 var propNames = [], propValues = [], propTypes = [], propLabels = [];
350 this.getAllProperties(propNames, propValues, propTypes, propLabels);
351 var n = propNames.length; 360 var n = propNames.length;
352 for (var i = 0; i < n; i++) 361 for (var i = 0; i < n; i++)
353 newMat.setProperty(propNames[i], propValues[i]); 362 newMat.setProperty(propNames[i], propValues[i]);
@@ -367,7 +376,32 @@ var UberMaterial = function UberMaterial() {
367 this._materialNode.setShader(this._shader); 376 this._materialNode.setShader(this._shader);
368 }; 377 };
369 378
370 this.importJSON = function (jObj) { 379 this.update = function()
380 {
381 var material = this._materialNode;
382 if (material)
383 {
384 var technique = material.shaderProgram.defaultTechnique;
385 var renderer = RDGE.globals.engine.getContext().renderer;
386 if (renderer && technique)
387 {
388 if (this._diffuseTexture && this._diffuseTexture.isAnimated())
389 {
390 this._diffuseTexture.render();
391 technique.s_diffuseMap.set( this._diffuseTexture.getTexture() );
392 }
393 }
394 }
395 }
396
397 this.isAnimated = function()
398 {
399 var anim = (this._diffuseTexture && this._diffuseTexture.isAnimated());
400 return anim;
401 }
402
403 this.importJSON = function (jObj)
404 {
371 if (this.getShaderName() != jObj.material) throw new Error("ill-formed material"); 405 if (this.getShaderName() != jObj.material) throw new Error("ill-formed material");
372 this.setName(jObj.name); 406 this.setName(jObj.name);
373 407
@@ -682,9 +716,9 @@ var UberMaterial = function UberMaterial() {
682 716
683 var renderer = RDGE.globals.engine.getContext().renderer; 717 var renderer = RDGE.globals.engine.getContext().renderer;
684 if (this._useDiffuseMap) { 718 if (this._useDiffuseMap) {
685 var tex = renderer.getTextureByName(caps.diffuseMap.texture, caps.diffuseMap.wrap, caps.diffuseMap.mips); 719 this._diffuseTexture = new Texture( this.getWorld(), caps.diffuseMap.texture, caps.diffuseMap.wrap, caps.diffuseMap.mips );
686 this.registerTexture(tex); 720 var tex = this._diffuseTexture.getTexture();
687 technique.s_diffuseMap.set(tex); 721 technique.s_diffuseMap.set( tex );
688 } 722 }
689 723
690 if (this._useNormalMap) { 724 if (this._useNormalMap) {