diff options
author | Jose Antonio Marquez Russo | 2012-03-15 06:28:14 -0700 |
---|---|---|
committer | Jose Antonio Marquez Russo | 2012-03-15 06:28:14 -0700 |
commit | 67748b4fb19cd5fd210ca711c98d992371c4d2d3 (patch) | |
tree | ab86675822de48f16be1000e8c137ec7189fc3e4 /js/lib/rdge/materials/uber-material.js | |
parent | 089534a80c64226bf7d124ab9147afce386fdb5c (diff) | |
parent | 57d4a82977a1f0e809511fe894886f88581d9615 (diff) | |
download | ninja-67748b4fb19cd5fd210ca711c98d992371c4d2d3.tar.gz |
Merge pull request #34 from ericmueller/integration
Integration
Diffstat (limited to 'js/lib/rdge/materials/uber-material.js')
-rwxr-xr-x | js/lib/rdge/materials/uber-material.js | 130 |
1 files changed, 112 insertions, 18 deletions
diff --git a/js/lib/rdge/materials/uber-material.js b/js/lib/rdge/materials/uber-material.js index 655d8e2a..91f43754 100755 --- a/js/lib/rdge/materials/uber-material.js +++ b/js/lib/rdge/materials/uber-material.js | |||
@@ -4,6 +4,7 @@ | |||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | 4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. |
5 | </copyright> */ | 5 | </copyright> */ |
6 | 6 | ||
7 | var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser; | ||
7 | var Material = require("js/lib/rdge/materials/material").Material; | 8 | var Material = require("js/lib/rdge/materials/material").Material; |
8 | 9 | ||
9 | var UberMaterial = function UberMaterial() { | 10 | var UberMaterial = function UberMaterial() { |
@@ -375,19 +376,111 @@ var UberMaterial = function UberMaterial() { | |||
375 | var endKey = "endMaterial\n"; | 376 | var endKey = "endMaterial\n"; |
376 | var index = importStr.indexOf( endKey ); | 377 | var index = importStr.indexOf( endKey ); |
377 | index += endKey.length; | 378 | index += endKey.length; |
378 | importStr = importStr.substr( index ); | 379 | importStr = importStr.slice( 0, index ); |
379 | var pu = new MaterialParser( importStr ); | 380 | var pu = new MaterialParser( importStr ); |
380 | 381 | ||
381 | var matProps = pu.nextValue( "materialProps: " ); | 382 | var matProps = pu.nextValue( "materialProps: " ); |
382 | if (matProps) | 383 | if (matProps) |
383 | { | 384 | { |
384 | var ambientColor = Number( pu.nextValue( "ambientColor: " )); this.setProperty( "ambientColor", ambientColor ); | 385 | var ambientColor = eval( "[" + pu.nextValue("ambientColor: ") + ']' ); this.setProperty( "ambientColor", ambientColor ); |
385 | var diffuseColor = Number( pu.nextValue( "diffuseColor: " )); this.setProperty( "diffuseColor", diffuseColor ); | 386 | var diffuseColor = eval( "[" + pu.nextValue( "diffuseColor: ") + ']' ); this.setProperty( "diffuseColor", diffuseColor ); |
386 | var specularColor = Number( pu.nextValue( "specularColor: " )); this.setProperty( "specularColor", specularColor ); | 387 | var specularColor = eval( "[" + pu.nextValue( "specularColor: ") + ']' ); this.setProperty( "specularColor", specularColor ); |
387 | var specularPower = Number( pu.nextValue( "specularPower: " )); this.setProperty( "specularPower", specularPower ); | 388 | var specularPower = eval( "[" + pu.nextValue( "specularPower: ") + ']' ); this.setProperty( "specularPower", specularPower ); |
388 | } | 389 | } |
389 | 390 | ||
390 | var lightProps = pu.nextValue( "theLights" ); | 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 | |||
483 | this.rebuildShader(); | ||
391 | } | 484 | } |
392 | 485 | ||
393 | this.export = function() | 486 | this.export = function() |
@@ -402,10 +495,10 @@ var UberMaterial = function UberMaterial() { | |||
402 | if (typeof caps.material != 'undefined') | 495 | if (typeof caps.material != 'undefined') |
403 | { | 496 | { |
404 | exportStr += "materialProps: true\n"; | 497 | exportStr += "materialProps: true\n"; |
405 | exportStr += "ambientColor: " + caps.material.ambientColor + "\n"; | 498 | exportStr += "ambientColor: " + this._ambientColor + "\n"; |
406 | exportStr += "diffuseColor: " + caps.material.diffuseColor + "\n"; | 499 | exportStr += "diffuseColor: " + this._diffuseColor + "\n"; |
407 | exportStr += "specularColor: " + caps.material.specularColor + "\n"; | 500 | exportStr += "specularColor: " + this._specularColor + "\n"; |
408 | exportStr += "specularPower: " + caps.material.specularPower + "\n"; | 501 | exportStr += "specularPower: " + this._specularPower + "\n"; |
409 | } | 502 | } |
410 | 503 | ||
411 | if (typeof caps.lighting != 'undefined') | 504 | if (typeof caps.lighting != 'undefined') |
@@ -428,20 +521,18 @@ var UberMaterial = function UberMaterial() { | |||
428 | else if (light.type === 'spot') | 521 | else if (light.type === 'spot') |
429 | { | 522 | { |
430 | exportStr += 'light' + i + 'Pos: ' + light['position'] + '\n'; | 523 | exportStr += 'light' + i + 'Pos: ' + light['position'] + '\n'; |
431 | 524 | exportStr += 'light' + i + 'SpotInnerCutoff: ' + light['spotInnerCutoff'] + '\n'; | |
432 | var deg2Rad = Math.PI / 180; | 525 | exportStr += 'light' + i + 'SpotOuterCutoff: ' + light['spotOuterCutoff'] + '\n'; |
433 | exportStr += 'light' + i + 'Spot: ' + [ Math.cos( ( light['spotInnerCutoff'] || 45.0 ) * deg2Rad ), | ||
434 | Math.cos( ( light['spotOuterCutoff'] || 90.0 ) * deg2Rad )] + '\n'; | ||
435 | } | 526 | } |
436 | else // light.type === 'point' | 527 | else // light.type === 'point' |
437 | { | 528 | { |
438 | exportStr += 'light' + i + 'Pos: ' + (light['position'] || [ 0, 0, 0 ]) ; | 529 | exportStr += 'light' + i + 'Pos: ' + (light['position'] || [ 0, 0, 0 ]) + '\n'; |
439 | exportStr += 'light' + i + 'Attenuation: ' + (light['attenuation'] || [ 1, 0, 0 ]) ; | 530 | exportStr += 'light' + i + 'Attenuation: ' + (light['attenuation'] || [ 1, 0, 0 ]) + '\n'; |
440 | } | 531 | } |
441 | 532 | ||
442 | // common to all lights | 533 | // common to all lights |
443 | exportStr += 'light' + i + 'Color: ' + light['diffuseColor'] || [ 1,1,1,1 ] + '\n'; | 534 | exportStr += 'light' + i + 'Color: ' + (light['diffuseColor'] || [ 1,1,1,1 ]) + '\n'; |
444 | exportStr += 'light' + i + 'SpecularColor: ' + light['specularColor'] || [ 1, 1, 1, 1 ] + '\n'; | 535 | exportStr += 'light' + i + 'SpecularColor: ' + (light['specularColor'] || [ 1, 1, 1, 1 ]) + '\n'; |
445 | 536 | ||
446 | exportStr += "endlight\n"; | 537 | exportStr += "endlight\n"; |
447 | } | 538 | } |
@@ -466,7 +557,10 @@ var UberMaterial = function UberMaterial() { | |||
466 | exportStr += "specularMap: " + caps.specularMap.texture + "\n"; | 557 | exportStr += "specularMap: " + caps.specularMap.texture + "\n"; |
467 | 558 | ||
468 | if(typeof caps.environmentMap != 'undefined') | 559 | if(typeof caps.environmentMap != 'undefined') |
560 | { | ||
469 | exportStr += "environmentMap: " + caps.environmentMap.texture + "\n"; | 561 | exportStr += "environmentMap: " + caps.environmentMap.texture + "\n"; |
562 | exportStr += "environmentAmount: " + caps.environmentMap.envReflection + "\n"; | ||
563 | } | ||
470 | 564 | ||
471 | // every material needs to terminate like this | 565 | // every material needs to terminate like this |
472 | exportStr += "endMaterial\n"; | 566 | exportStr += "endMaterial\n"; |