diff options
author | hwc487 | 2012-04-02 10:23:28 -0700 |
---|---|---|
committer | hwc487 | 2012-04-02 10:23:28 -0700 |
commit | 19c975cfb16f1cccc9e9f86255575499315d421e (patch) | |
tree | f97824d2281c55e527032fbaca8d3c80998053cf /js/lib/rdge/materials | |
parent | 679a0746bedab9c5871a83a5fea6a06ac6a4f41c (diff) | |
download | ninja-19c975cfb16f1cccc9e9f86255575499315d421e.tar.gz |
texture abstraction
Diffstat (limited to 'js/lib/rdge/materials')
-rwxr-xr-x | js/lib/rdge/materials/uber-material.js | 232 |
1 files changed, 34 insertions, 198 deletions
diff --git a/js/lib/rdge/materials/uber-material.js b/js/lib/rdge/materials/uber-material.js index c1d1913c..ca244629 100755 --- a/js/lib/rdge/materials/uber-material.js +++ b/js/lib/rdge/materials/uber-material.js | |||
@@ -6,6 +6,7 @@ | |||
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 | /////////////////////////////////////////////////////////////////////// |
@@ -189,6 +194,7 @@ var UberMaterial = function UberMaterial() { | |||
189 | if (material) { | 194 | if (material) { |
190 | var technique = material.shaderProgram.defaultTechnique; | 195 | var technique = material.shaderProgram.defaultTechnique; |
191 | technique.u_diffuseColor.set(this._diffuseColor); | 196 | technique.u_diffuseColor.set(this._diffuseColor); |
197 | this.getWorld().restartRenderLoop(); | ||
192 | } | 198 | } |
193 | }; | 199 | }; |
194 | 200 | ||
@@ -258,6 +264,7 @@ var UberMaterial = function UberMaterial() { | |||
258 | if ((value == null) || (value.length == 0)) { | 264 | if ((value == null) || (value.length == 0)) { |
259 | if (this._useDiffuseMap) { | 265 | if (this._useDiffuseMap) { |
260 | this._useDiffuseMap = false; | 266 | this._useDiffuseMap = false; |
267 | this._diffuseTexture = undefined; | ||
261 | this.rebuildShader(); | 268 | this.rebuildShader(); |
262 | } | 269 | } |
263 | } else { | 270 | } else { |
@@ -270,8 +277,8 @@ var UberMaterial = function UberMaterial() { | |||
270 | var technique = material.shaderProgram.defaultTechnique; | 277 | var technique = material.shaderProgram.defaultTechnique; |
271 | var renderer = g_Engine.getContext().renderer; | 278 | var renderer = g_Engine.getContext().renderer; |
272 | if (renderer && technique) { | 279 | if (renderer && technique) { |
273 | var tex = renderer.getTextureByName(value, caps.diffuseMap.wrap); | 280 | this._diffuseTexture = new Texture( this.getWorld(), value, caps.diffuseMap.wrap ); |
274 | this.registerTexture( tex ); | 281 | var tex = this._diffuseTexture.getTexture(); |
275 | technique.s_diffuseMap.set( tex ); | 282 | technique.s_diffuseMap.set( tex ); |
276 | } | 283 | } |
277 | } | 284 | } |
@@ -370,118 +377,29 @@ var UberMaterial = function UberMaterial() { | |||
370 | this._materialNode.setShader(this._shader); | 377 | this._materialNode.setShader(this._shader); |
371 | }; | 378 | }; |
372 | 379 | ||
373 | this.import = function( importStr ) | 380 | this.update = function() |
374 | { | 381 | { |
375 | // limit the key searches to this material | 382 | var material = this._materialNode; |
376 | var endKey = "endMaterial\n"; | 383 | if (material) |
377 | var index = importStr.indexOf( endKey ); | 384 | { |
378 | index += endKey.length; | 385 | var technique = material.shaderProgram.defaultTechnique; |
379 | importStr = importStr.slice( 0, index ); | 386 | var renderer = g_Engine.getContext().renderer; |
380 | var pu = new MaterialParser( importStr ); | 387 | if (renderer && technique) |
381 | 388 | { | |
382 | var matProps = pu.nextValue( "materialProps: " ); | 389 | if (this._diffuseTexture && this._diffuseTexture.isAnimated()) |
383 | if (matProps) | 390 | { |
384 | { | 391 | this._diffuseTexture.rerender(); |
385 | var ambientColor = eval( "[" + pu.nextValue("ambientColor: ") + ']' ); this.setProperty( "ambientColor", ambientColor ); | 392 | technique.s_diffuseMap.set( this._diffuseTexture.getTexture() ); |
386 | var diffuseColor = eval( "[" + pu.nextValue( "diffuseColor: ") + ']' ); this.setProperty( "diffuseColor", diffuseColor ); | 393 | } |
387 | var specularColor = eval( "[" + pu.nextValue( "specularColor: ") + ']' ); this.setProperty( "specularColor", specularColor ); | 394 | } |
388 | var specularPower = eval( "[" + pu.nextValue( "specularPower: ") + ']' ); this.setProperty( "specularPower", specularPower ); | 395 | } |
389 | } | 396 | } |
390 | |||
391 | var lightProps = pu.nextValue( "lightProps: " ); | ||
392 | if (lightProps) | ||
393 | { | ||
394 | this._lights = []; | ||
395 | var lightStr; | ||
396 | for (var i=0; i<this._MAX_LIGHTS; i++) | ||
397 | { | ||
398 | var type = pu.nextValue( "light" + i + ": " ); | ||
399 | if (type) | ||
400 | { | ||
401 | var light = new Object; | ||
402 | switch (type) | ||
403 | { | ||
404 | case "directional": | ||
405 | lightStr = pu.nextValue( 'light' + i + 'Dir: '); | ||
406 | light.direction = eval( "[" + lightStr + "]" ); | ||
407 | break; | ||
408 | |||
409 | case "spot": | ||
410 | lightStr = pu.nextValue( 'light' + i + 'Pos: ' ); | ||
411 | light.position = eval( "[" + lightStr + "]" ); | ||
412 | |||
413 | lightStr = pu.nextValue( 'light' + i + 'OuterSpotCutoff: ' ); | ||
414 | light['spotInnerCutoff'] = Number( lightStr ); | ||
415 | |||
416 | lightStr = pu.nextValue( 'light' + i + 'InnerSpotCutoff: ' ); | ||
417 | light['spotOuterCutoff'] = Number( lightStr ); | ||
418 | break; | ||
419 | |||
420 | case "point": | ||
421 | lightStr = pu.nextValue( 'light' + i + 'Pos: ' ); | ||
422 | light.position = eval( "[" + lightStr + "]" ); | ||
423 | |||
424 | lightStr = pu.nextValue( 'light' + i + 'Attenuation: ' ); | ||
425 | light.attenuation = eval( "[" + lightStr + "]" ); | ||
426 | break; | ||
427 | |||
428 | default: | ||
429 | throw new Error( "unrecognized light type on import: " + type ); | ||
430 | break; | ||
431 | } | ||
432 | |||
433 | // common to all lights | ||
434 | light.diffuseColor = eval( "[" + pu.nextValue( 'light' + i + 'Color: ') + "]" ); | ||
435 | light.specularColor = eval( "[" + pu.nextValue( 'light' + i + 'SpecularColor: ') + "]" ); | ||
436 | |||
437 | // push the light | ||
438 | this._lights.push( light ); | ||
439 | } | ||
440 | else | ||
441 | this._lights[i] = 'undefined'; | ||
442 | |||
443 | // advance to the next light | ||
444 | var endLightKey = "endMaterial\n"; | ||
445 | index = importStr.indexOf( endLightKey ); | ||
446 | if (index < 0) throw new Error( "ill-formed light encountered in import" ); | ||
447 | index += endLightKey.length; | ||
448 | importStr = importStr.slice( 0, index ); | ||
449 | |||
450 | } | ||
451 | |||
452 | if (this._lights.length > 0) | ||
453 | { | ||
454 | this._ubershaderCaps.lighting = | ||
455 | { | ||
456 | 'light0' : this._lights[0], | ||
457 | 'light1' : this._lights[1], | ||
458 | 'light2' : this._lights[2], | ||
459 | 'light3' : this._lights[3] | ||
460 | } | ||
461 | } | ||
462 | } | ||
463 | |||
464 | var diffuseMap = pu.nextValue( "diffuseMap: " ) | ||
465 | if(diffuseMap) | ||
466 | this.setProperty( "diffuseMap", diffuseMap ); | ||
467 | |||
468 | var normalMap = pu.nextValue( "normalMap: " ); | ||
469 | if(normalMap) | ||
470 | this.setProperty( "normalMap", normalMap ); | ||
471 | |||
472 | var specularMap = pu.nextValue( "specularMap: " ); | ||
473 | if(specularMap) | ||
474 | this.setProperty( "specularMap", specularMap ); | ||
475 | |||
476 | var environmentMap = pu.nextValue( "environmentMap: " ); | ||
477 | if(environmentMap) | ||
478 | { | ||
479 | this.setProperty( "environmentMap", environmentMap ); | ||
480 | this.setProperty( "environmentAmount", Number( pu.nextValue( "environmentAmount" ) ) ); | ||
481 | } | ||
482 | 397 | ||
483 | this.rebuildShader(); | 398 | this.isAnimated = function() |
484 | } | 399 | { |
400 | var anim = (this._diffuseTexture && this._diffuseTexture.isAnimated()); | ||
401 | return anim; | ||
402 | } | ||
485 | 403 | ||
486 | this.importJSON = function( jObj ) |