/*
This file contains proprietary software owned by Motorola Mobility, Inc.
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
*/
var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser;
var Material = require("js/lib/rdge/materials/material").Material;
var Texture = require("js/lib/rdge/texture").Texture;
///////////////////////////////////////////////////////////////////////
// Class GLMaterial
// RDGE representation of a material.
///////////////////////////////////////////////////////////////////////
var BumpMetalMaterial = function BumpMetalMaterial() {
///////////////////////////////////////////////////////////////////////
// Instance variables
///////////////////////////////////////////////////////////////////////
this._name = "BumpMetalMaterial";
this._shaderName = "bumpMetal";
this._lightDiff = [0.3, 0.3, 0.3, 1.0];
//this._diffuseTexture = "assets/images/metal.png";
this._diffuseTexture = "texture";
this._diffuseWorld = null; // the world that the texture is derived from (if there is one).
this._diffuseTextureObj = null;
this._specularTexture = "assets/images/silver.png";
this._normalTexture = "assets/images/normalMap.png";
///////////////////////////////////////////////////////////////////////
// Property Accessors
///////////////////////////////////////////////////////////////////////
this.getName = function() {
return this._name;
};
this.getShaderName = function() {
return this._shaderName;
};
this.getLightDiff = function() {
return this._lightDiff;
};
this.setLightDiff = function(ld) {
this._lightDiff = ld;
// Bad property name. Commenting for now
if (this._shader && this._shader['default']){
this._shader['default'].u_light0Diff.set( ld );
}
};
this.getDiffuseTexture = function() { return this._propValues[this._propNames[1]] ? this._propValues[this._propNames[1]].slice() : null };
this.setDiffuseTexture = function(m) { this._propValues[this._propNames[1]] = m ? m.slice(0) : null; this.updateTexture(1); };
this.getNormalTexture = function() { return this._propValues[this._propNames[2]] ? this._propValues[this._propNames[2]].slice() : null };
this.setNormalTexture = function(m) { this._propValues[this._propNames[2]] = m ? m.slice(0) : null; this.updateTexture(2); };
this.getSpecularTexture = function() { return this._propValues[this._propNames[3]] ? this._propValues[this._propNames[3]].slice() : null };
this.setSpecularTexture = function(m) { this._propValues[this._propNames[3]] = m ? m.slice(0) : null; this.updateTexture(3); };
this.isAnimated = function() { return true; };
///////////////////////////////////////////////////////////////////////
// Material Property Accessors
///////////////////////////////////////////////////////////////////////
this._propNames = ["lightDiff", "diffuseTexture", "normalMap", "specularTexture"];
this._propLabels = ["Diffuse Color", "Diffuse Map", "Bump Map", "Specular Map"];
this._propTypes = ["color", "file", "file", "file"];
this._propValues = [];
this._propValues[ this._propNames[0] ] = this._lightDiff.slice(0);
this._propValues[ this._propNames[1] ] = this._diffuseTexture.slice(0);
this._propValues[ this._propNames[2] ] = this._normalTexture.slice(0);
this._propValues[ this._propNames[3] ] = this._specularTexture.slice(0);
// TODO - shader techniques are not all named the same, i.e., FlatMaterial uses "colorMe" and BrickMaterial uses "default"
this.setProperty = function( prop, value )
{
// every material should do something with the "color" property
if (prop === "color") return;
// make sure we have legitimate imput
var ok = this.validateProperty( prop, value );
if (!ok)
{
console.log( "invalid property in Bump Metal Materia;" + prop + " : " + value );
return;
}
switch (prop)
{
case "lightDiff": this.setLightDiff( value ); break;
case "diffuseTexture": this.setDiffuseTexture( value ); break;
case "specularTexture": this.setSpecularTexture( value ); break;
case "normalMap": this.setNormalTexture( value ); break;
default:
console.log( "invalid property to Bump Metal Material: " + prop + ", value: " + value );
break;
}
};
///////////////////////////////////////////////////////////////////////
// Methods
///////////////////////////////////////////////////////////////////////
// duplcate method requirde
this.dup = function() { return new BumpMetalMaterial(); };
this.init = function( world )
{
// save the world
if (world) this.setWorld( world );
// set up the shader
this._shader = new jshader();
this._shader.def = bumpMetalMaterialDef;
this._shader.init();
this._shader['default'].u_light0Diff.set( this.getLightDiff() );
// set up the material node
this._materialNode = createMaterialNode( this.getShaderName() + "_" + world.generateUniqueNodeID() );
this._materialNode.setShader(this._shader);
// DEBUG CODE
this.initWorldTextures();
// set some image maps
this.updateTexture(1);
this.updateTexture(2);
this.updateTexture(3);
};
this.initWorldTextures = function()
{
// find the world with the given id
var viewUtils = require("js/helper-classes/3D/view-utils").ViewUtils;
var root = viewUtils.application.ninja.currentDocument.documentRoot;
this._diffuseWorld = this.findWorld( this._diffuseTexture, root );
if (this._diffuseWorld)
{
var world = this.getWorld();
var tex = new Texture( world );
this._diffuseTextureObj = tex;
tex.loadFromCanvas( world.getCanvas() );
}
}
this.findWorld = function( id, elt )
{
if (elt.id && elt.id === id)
{
if (elt.elementModel && elt.elementModel.shapeModel && elt.elementModel.shapeModel.GLWorld)
{
var world = elt.elementModel.shapeModel.GLWorld;
return world;
}
}
if (elt.children)
{
var nKids = elt.children.length;
for (var i=0; i