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.js109
1 files changed, 108 insertions, 1 deletions
diff --git a/js/lib/rdge/materials/uber-material.js b/js/lib/rdge/materials/uber-material.js
index 6bc35d51..d120ffa1 100755
--- a/js/lib/rdge/materials/uber-material.js
+++ b/js/lib/rdge/materials/uber-material.js
@@ -33,6 +33,8 @@ var UberMaterial = function UberMaterial() {
33 this._useEnvironmentMap = true; 33 this._useEnvironmentMap = true;
34 this._useLights = [true, true, true, true]; 34 this._useLights = [true, true, true, true];
35 35
36 this._MAX_LIGHTS = 4;
37
36 /////////////////////////////////////////////////////////////////////// 38 ///////////////////////////////////////////////////////////////////////
37 // Material Property Accessors 39 // Material Property Accessors
38 /////////////////////////////////////////////////////////////////////// 40 ///////////////////////////////////////////////////////////////////////
@@ -363,10 +365,115 @@ var UberMaterial = function UberMaterial() {
363 this._shader = this.buildUberShader( this._ubershaderCaps ); 365 this._shader = this.buildUberShader( this._ubershaderCaps );
364 366
365 // set up the material node 367 // set up the material node
366 this._materialNode = createMaterialNode("uberMaterial"); 368 this._materialNode = createMaterialNode("uberMaterial" + "_" + world.generateUniqueNodeID());
367 this._materialNode.setShader(this._shader); 369 this._materialNode.setShader(this._shader);
368 }; 370 };
369 371
372 this.import = function( importStr )
373 {
374 // limit the key searches to this material
375 var endKey = "endMaterial\n";
376 var index = importStr.indexOf( endKey );
377 index += endKey.length;
378 importStr = importStr.substr( index );
379 var pu = new MaterialParser( importStr );
380
381 var matProps = pu.nextValue( "materialProps: " );
382 if (matProps)
383 {
384 var ambientColor = Number( pu.nextValue( "ambientColor: " )); this.setProperty( "ambientColor", ambientColor );
385 var diffuseColor = Number( pu.nextValue( "diffuseColor: " )); this.setProperty( "diffuseColor", diffuseColor );
386 var specularColor = Number( pu.nextValue( "specularColor: " )); this.setProperty( "specularColor", specularColor );
387 var specularPower = Number( pu.nextValue( "specularPower: " )); this.setProperty( "specularPower", specularPower );
388 }
389
390 var lightProps = pu.nextValue( "theLights" );
391 }
392
393 this.export = function()
394 {
395 // every material needs the base type and instance name
396 var exportStr = "material: " + this.getShaderName() + "\n";
397 exportStr += "name: " + this.getName() + "\n";
398
399 var caps = this._ubershaderCaps;
400
401 // export the material properties
402 if (typeof caps.material != 'undefined')
403 {
404 exportStr += "materialProps: true\n";
405 exportStr += "ambientColor: " + caps.material.ambientColor + "\n";
406 exportStr += "diffuseColor: " + caps.material.diffuseColor + "\n";
407 exportStr += "specularColor: " + caps.material.specularColor + "\n";
408 exportStr += "specularPower: " + caps.material.specularPower + "\n";
409 }
410
411 if (typeof caps.lighting != 'undefined')
412 {
413 exportStr += "lightProps: true\n";
414
415 var light = caps.lighting['light' + i];
416 for (var i=0; i<this._MAX_LIGHTS; i++)
417 {
418 var light = caps.lighting["light" + i];
419 if( typeof light != "undefined")
420 {
421 exportStr += "light" + i + ': ' + light.type + "\n";
422
423 // output the light secific data
424 if (light.type === 'directional')
425 {
426 exportStr += 'light' + i + 'Dir: ' + light['direction'] + '\n';
427 }
428 else if (light.type === 'spot')
429 {
430 exportStr += 'light' + i + 'Pos: ' + light['position'] + '\n';
431
432 var deg2Rad = Math.PI / 180;
433 exportStr += 'light' + i + 'Spot: ' + [ Math.cos( ( light['spotInnerCutoff'] || 45.0 ) * deg2Rad ),
434 Math.cos( ( light['spotOuterCutoff'] || 90.0 ) * deg2Rad )] + '\n';
435 }
436 else // light.type === 'point'
437 {
438 exportStr += 'light' + i + 'Pos: ' + (light['position'] || [ 0, 0, 0 ]) ;
439 exportStr += 'light' + i + 'Attenuation: ' + (light['attenuation'] || [ 1, 0, 0 ]) ;
440 }
441
442 // common to all lights
443 exportStr += 'light' + i + 'Color: ' + light['diffuseColor'] || [ 1,1,1,1 ] + '\n';
444 exportStr += 'light' + i + 'SpecularColor: ' + light['specularColor'] || [ 1, 1, 1, 1 ] + '\n';
445
446 exportStr += "endlight\n";
447 }
448 }
449 }
450
451// this._diffuseMapOb = { 'texture' : 'assets/images/rocky-diffuse.jpg', 'wrap' : 'REPEAT' };
452// this._normalMapOb = { 'texture' : 'assets/images/rocky-normal.jpg', 'wrap' : 'REPEAT' };
453// this._specularMapOb = { 'texture' : 'assets/images/rocky-spec.jpg', 'wrap' : 'REPEAT' };
454// this._environmentMapOb = { 'texture' : 'assets/images/silver.png', 'wrap' : 'CLAMP', 'envReflection' : this._environmentAmount };
455 var world = this.getWorld();
456 if (!world)
457 throw new Error( "no world in material.export, " + this.getName() );
458
459 if(typeof caps.diffuseMap != 'undefined')
460 exportStr += "diffuseMap: " + world.cleansePath(caps.diffuseMap.texture) + "\n";
461
462 if(typeof caps.normalMap != 'undefined')
463 exportStr += "normalMap: " + world.cleansePath(caps.normalMap.texture) + "\n";
464
465 if(typeof caps.specularMap != 'undefined')
466 exportStr += "specularMap: " + world.cleansePath(caps.specularMap.texture) + "\n";
467
468 if(typeof caps.environmentMap != 'undefined')
469 exportStr += "environmentMap: " + world.cleansePath(caps.environmentMap.texture) + "\n";
470
471 // every material needs to terminate like this
472 exportStr += "endMaterial\n";
473
474 return exportStr;
475 }
476
370 this.buildUberShader = function(caps) 477 this.buildUberShader = function(caps)
371 { 478 {
372 var preproc = ""; 479 var preproc = "";