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.js130
1 files changed, 94 insertions, 36 deletions
diff --git a/js/lib/rdge/materials/uber-material.js b/js/lib/rdge/materials/uber-material.js
index e94458cc..bc2ba340 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,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"];
@@ -335,9 +366,13 @@ var UberMaterial = function UberMaterial() {
335 366
336 // duplcate method requirde 367 // duplcate method requirde
337 this.dup = function () { 368 this.dup = function () {
338 // allocate a new uber material
339 var newMat = new UberMaterial();
340 369
370 // get the current values;
371 var propNames = [], propValues = [], propTypes = [], propLabels = [];
372 this.getAllProperties(propNames, propValues, propTypes, propLabels);
373
374 // allocate a new material
375 var newMat = new UberMaterial();
341 newMat._useDiffuseMap = this._useDiffuseMap; 376 newMat._useDiffuseMap = this._useDiffuseMap;
342 newMat._useEnvironmentMap = this._useEnvironmentMap; 377 newMat._useEnvironmentMap = this._useEnvironmentMap;
343 newMat._useLights = this._useLights; 378 newMat._useLights = this._useLights;
@@ -345,9 +380,7 @@ var UberMaterial = function UberMaterial() {
345 newMat._useSpecularMap = this._useSpecularMap; 380 newMat._useSpecularMap = this._useSpecularMap;
346 newMat.rebuildShader(); 381 newMat.rebuildShader();
347 382
348 // copy over the current values; 383 // copy over the current values;
349 var propNames = [], propValues = [], propTypes = [], propLabels = [];
350 this.getAllProperties(propNames, propValues, propTypes, propLabels);
351 var n = propNames.length; 384 var n = propNames.length;
352 for (var i = 0; i < n; i++) 385 for (var i = 0; i < n; i++)
353 newMat.setProperty(propNames[i], propValues[i]); 386 newMat.setProperty(propNames[i], propValues[i]);
@@ -367,7 +400,32 @@ var UberMaterial = function UberMaterial() {
367 this._materialNode.setShader(this._shader); 400 this._materialNode.setShader(this._shader);
368 }; 401 };
369 402
370 this.importJSON = function (jObj) { 403 this.update = function()
404 {
405 var material = this._materialNode;
406 if (material)
407 {
408 var technique = material.shaderProgram.defaultTechnique;
409 var renderer = RDGE.globals.engine.getContext().renderer;
410 if (renderer && technique)
411 {
412 if (this._diffuseTexture && this._diffuseTexture.isAnimated())
413 {
414 this._diffuseTexture.render();
415 technique.s_diffuseMap.set( this._diffuseTexture.getTexture() );
416 }
417 }
418 }
419 }
420
421 this.isAnimated = function()
422 {
423 var anim = (this._diffuseTexture && this._diffuseTexture.isAnimated());
424 return anim;
425 }
426
427 this.importJSON = function (jObj)
428 {
371 if (this.getShaderName() != jObj.material) throw new Error("ill-formed material"); 429 if (this.getShaderName() != jObj.material) throw new Error("ill-formed material");
372 this.setName(jObj.name); 430 this.setName(jObj.name);
373 431
@@ -682,9 +740,9 @@ var UberMaterial = function UberMaterial() {
682 740
683 var renderer = RDGE.globals.engine.getContext().renderer; 741 var renderer = RDGE.globals.engine.getContext().renderer;
684 if (this._useDiffuseMap) { 742 if (this._useDiffuseMap) {
685 var tex = renderer.getTextureByName(caps.diffuseMap.texture, caps.diffuseMap.wrap, caps.diffuseMap.mips); 743 this._diffuseTexture = new Texture( this.getWorld(), caps.diffuseMap.texture, caps.diffuseMap.wrap, caps.diffuseMap.mips );
686 this.registerTexture(tex); 744 var tex = this._diffuseTexture.getTexture();
687 technique.s_diffuseMap.set(tex); 745 technique.s_diffuseMap.set( tex );
688 } 746 }
689 747
690 if (this._useNormalMap) { 748 if (this._useNormalMap) {