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.js129
1 files changed, 84 insertions, 45 deletions
diff --git a/js/lib/rdge/materials/uber-material.js b/js/lib/rdge/materials/uber-material.js
index e94458cc..347e45d3 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
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 ///////////////////////////////////////////////////////////////////////
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,9 +307,9 @@ 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_specMap.set(tex);
302 } 313 }
303 } 314 }
304 } 315 }
@@ -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,23 +346,26 @@ 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;
344 newMat._useNormalMap = this._useNormalMap; 359 newMat._useNormalMap = this._useNormalMap;
345 newMat._useSpecularMap = this._useSpecularMap; 360 newMat._useSpecularMap = this._useSpecularMap;
346 newMat.rebuildShader();
347 361
348 // copy over the current values; 362 // copy over the current values;
349 var propNames = [], propValues = [], propTypes = [], propLabels = [];
350 this.getAllProperties(propNames, propValues, propTypes, propLabels);
351 var n = propNames.length; 363 var n = propNames.length;
352 for (var i = 0; i < n; i++) 364 for (var i = 0; i < n; i++)
353 newMat.setProperty(propNames[i], propValues[i]); 365 newMat.setProperty(propNames[i], propValues[i]);
354 366
367 newMat.rebuildShader();
368
355 return newMat; 369 return newMat;
356 }; 370 };
357 371
@@ -367,7 +381,32 @@ var UberMaterial = function UberMaterial() {
367 this._materialNode.setShader(this._shader); 381 this._materialNode.setShader(this._shader);
368 }; 382 };
369 383
370 this.importJSON = function (jObj) { 384 this.update = function()
385 {
386 var material = this._materialNode;
387 if (mater