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.js119
1 files changed, 77 insertions, 42 deletions
diff --git a/js/lib/rdge/materials/uber-material.js b/js/lib/rdge/materials/uber-material.js
index e94458cc..0fef78f6 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 ///////////////////////////////////////////////////////////////////////
@@ -175,6 +180,7 @@ var UberMaterial = function UberMaterial() {
175 this.updateAmbientColor = function () { 180 this.updateAmbientColor = function () {
176 this._ambientColor = this._propValues['ambientColor'].slice(0); 181 this._ambientColor = this._propValues['ambientColor'].slice(0);
177 var material = this._materialNode; 182 var material = this._materialNode;
183 console.log( "ambient color: " + this._ambientColor );
178 if (material) { 184 if (material) {
179 var technique = material.shaderProgram.defaultTechnique; 185 var technique = material.shaderProgram.defaultTechnique;
180 technique.u_ambientColor.set(this._ambientColor); 186 technique.u_ambientColor.set(this._ambientColor);
@@ -188,6 +194,7 @@ var UberMaterial = function UberMaterial() {
188 if (material) { 194 if (material) {
189 var technique = material.shaderProgram.defaultTechnique; 195 var technique = material.shaderProgram.defaultTechnique;
190 technique.u_diffuseColor.set(this._diffuseColor); 196 technique.u_diffuseColor.set(this._diffuseColor);
197 this.getWorld().restartRenderLoop();
191 } 198 }
192 }; 199 };
193 200
@@ -240,42 +247,43 @@ var UberMaterial = function UberMaterial() {
240 var technique = material.shaderProgram.defaultTechnique; 247 var technique = material.shaderProgram.defaultTechnique;
241 var renderer = RDGE.globals.engine.getContext().renderer; 248 var renderer = RDGE.globals.engine.getContext().renderer;
242 if (renderer && technique) { 249 if (renderer && technique) {
243 var tex = renderer.getTextureByName(value, caps.environmentMap.wrap); 250 var tex = renderer.getTextureByName(value, this._ubershaderCaps.environmentMap.wrap);
244 this.registerTexture(tex); 251 this.registerTexture(tex);
245 technique.s_environmentMap.set(tex); 252 technique.s_envMap.set(tex);
246 } 253 }
247 } 254 }
248 } 255 }
249 } 256 }
250 }; 257 };
251 258
252 this.updateDiffuseMap = function (value) { 259 this.updateDiffuseMap = function(value) {
253 var value = this._propValues["diffuseMap"]; 260 var value = this._propValues[ "diffuseMap" ];
254 this._diffuseMapOb.texture = value; 261 this._diffuseMapOb.texture = value;
255 262
256 if ((value == null) || (value.length == 0)) { 263 if ((value == null) || (value.length == 0)) {
257 if (this._useDiffuseMap) { 264 if (this._useDiffuseMap) {
258 this._useDiffuseMap = false; 265 this._useDiffuseMap = false;
259 this.rebuildShader(); 266 this._diffuseTexture = undefined;
260 } 267 this.rebuildShader();
261 } else { 268 }
262 if (!this._useDiffuseMap) { 269 } else {
263 this._useDiffuseMap = true; 270 if (!this._useDiffuseMap) {
264 this.rebuildShader(); 271 this._useDiffuseMap = true;
265 } else { 272 this.rebuildShader();
266 var material = this._materialNode; 273 } else {
267 if (material) { 274 var material = this._materialNode;
268 var technique = material.shaderProgram.defaultTechnique; 275 if (material) {
269 var renderer = RDGE.globals.engine.getContext().renderer; 276 var technique = material.shaderProgram.defaultTechnique;
270 if (renderer && technique) { 277 var renderer = RDGE.globals.engine.getContext().renderer;
271 var tex = renderer.getTextureByName(value, caps.diffuseMap.wrap); 278 if (renderer && technique) {
272 this.registerTexture(tex); 279 this._diffuseTexture = new Texture( this.getWorld(), value, this._ubershaderCaps.diffuseMap.wrap );
273 technique.s_diffuseMap.set(tex); 280 var tex = this._diffuseTexture.getTexture();
274 } 281 technique.s_diffuseMap.set( tex );
275 } 282 }
276 } 283 }
277 } 284 }
278 }; 285 }
286 };
279 287
280 this.updateSpecularMap = function () { 288 this.updateSpecularMap = function () {
281 var value = this._propValues["specularMap"]; 289 var value = this._propValues["specularMap"];
@@ -296,7 +304,7 @@ var UberMaterial = function UberMaterial() {
296 var technique = material.shaderProgram.defaultTechnique; 304 var technique = material.shaderProgram.defaultTechnique;
297 var renderer = RDGE.globals.engine.getContext().renderer; 305 var renderer = RDGE.globals.engine.getContext().renderer;
298 if (renderer && technique) { 306 if (renderer && technique) {
299 var tex = renderer.getTextureByName(value, caps.specularMap.wrap); 307 var tex = renderer.getTextureByName(value, this._ubershaderCaps.specularMap.wrap);
300 this.registerTexture(tex); 308 this.registerTexture(tex);
301 technique.s_specularMap.set(tex); 309 technique.s_specularMap.set(tex);
302 } 310 }
@@ -324,7 +332,7 @@ var UberMaterial = function UberMaterial() {
324 var technique = material.shaderProgram.defaultTechnique; 332 var technique = material.shaderProgram.defaultTechnique;
325 var renderer = RDGE.globals.engine.getContext().renderer; 333 var renderer = RDGE.globals.engine.getContext().renderer;
326 if (renderer && technique) { 334 if (renderer && technique) {
327 var tex = renderer.getTextureByName(value, caps.normalMap.wrap); 335 var tex = renderer.getTextureByName(value, this._ubershaderCaps.normalMap.wrap);
328 this.registerTexture(tex); 336 this.registerTexture(tex);
329 technique.s_normalMap.set(tex); 337 technique.s_normalMap.set(tex);
330 } 338 }
@@ -335,9 +343,13 @@ var UberMaterial = function UberMaterial() {
335 343
336 // duplcate method requirde 344 // duplcate method requirde
337 this.dup = function () { 345 this.dup = function () {
338 // allocate a new uber material
339 var newMat = new UberMaterial();
340 346
347 // get the current values;
348 var propNames = [], propValues = [], propTypes = [], propLabels = [];
349 this.getAllProperties(propNames, propValues, propTypes, propLabels);
350
351 // allocate a new material
352 var newMat = new UberMaterial();
341 newMat._useDiffuseMap = this._useDiffuseMap; 353 newMat._useDiffuseMap = this._useDiffuseMap;
342 newMat._useEnvironmentMap = this._useEnvironmentMap; 354 newMat._useEnvironmentMap = this._useEnvironmentMap;
343 newMat._useLights = this._useLights; 355 newMat._useLights = this._useLights;
@@ -345,9 +357,7 @@ var UberMaterial = function UberMaterial() {
345 newMat._useSpecularMap = this._useSpecularMap; 357 newMat._useSpecularMap = this._useSpecularMap;
346 newMat.rebuildShader(); 358 newMat.rebuildShader();
347 359
348 // copy over the current values; 360 // copy over the current values;
349 var propNames = [], propValues = [], propTypes = [], propLabels = [];
350 this.getAllProperties(propNames, propValues, propTypes, propLabels);
351 var n = propNames.length; 361 var n = propNames.length;
352 for (var i = 0; i < n; i++) 362 for (var i = 0; i < n; i++)
353 newMat.setProperty(propNames[i], propValues[i]); 363 newMat.setProperty(propNames[i], propValues[i]);
@@ -367,7 +377,32 @@ var UberMaterial = function UberMaterial() {
367 this._materialNode.setShader(this._shader); 377 this._materialNode.setShader(this._shader);
368 }; 378 };
369 379
370 this.importJSON = function (jObj) { 380 this.update = function()
381 {
382 var material = this._materialNode;
383 if (material)
384 {
385 var technique = material.shaderProgram.defaultTechnique;
386 var renderer = RDGE.globals.engine.getContext().renderer;
387 if (renderer && technique)
388 {
389 if (this._diffuseTexture && this._diffuseTexture.isAnimated())
390 {
391 this._diffuseTexture.render();
392 technique.s_diffuseMap.set( this._diffuseTexture.getTexture() );
393 }
394 }
395 }
396 }
397
398 this.isAnimated = function()
399 {
400 var anim = (this._diffuseTexture && this._diffuseTexture.isAnimated());
401 return anim;
402 }
403
404 this.importJSON = function (jObj)
405 {
371 if (this.getShaderName() != jObj.material) throw new Error("ill-formed material"); 406 if (this.getShaderName() != jObj.material) throw new Error("ill-formed material");
372 this.setName(jObj.name); 407 this.setName(jObj.name);
373