From eb59a523258cad3351cba9bf8de986e90a8e5b1c Mon Sep 17 00:00:00 2001 From: hwc487 Date: Wed, 21 Mar 2012 15:17:58 -0700 Subject: Added material library data to the canvas data. --- js/document/html-document.js | 33 ++- js/lib/rdge/materials/bump-metal-material.js | 4 - js/lib/rdge/materials/cloud-material.js | 300 ++++++++++++++++++++++++++ js/lib/rdge/materials/plasma-material.js | 1 - js/lib/rdge/materials/pulse-material.js | 4 - js/lib/rdge/materials/radial-blur-material.js | 4 - js/lib/rdge/materials/uber-material.js | 5 - js/models/materials-model.js | 53 +++-- 8 files changed, 355 insertions(+), 49 deletions(-) create mode 100644 js/lib/rdge/materials/cloud-material.js (limited to 'js') diff --git a/js/document/html-document.js b/js/document/html-document.js index 8b765501..3d109fdb 100755 --- a/js/document/html-document.js +++ b/js/document/html-document.js @@ -9,7 +9,8 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot var Montage = require("montage/core/core").Montage, TextDocument = require("js/document/text-document").TextDocument, NJUtils = require("js/lib/NJUtils").NJUtils, - GLWorld = require("js/lib/drawing/world").World; + GLWorld = require("js/lib/drawing/world").World, + MaterialsModel = require("js/models/materials-model").MaterialsModel; //////////////////////////////////////////////////////////////////////// // exports.HTMLDocument = Montage.create(TextDocument, { @@ -193,7 +194,8 @@ exports.HTMLDocument = Montage.create(TextDocument, { var elt = this.iframe.contentWindow.document.getElementById("UserContent"); // if (elt) { - this._glData = []; + var matLib = MaterialsModel.exportMaterials(); + this._glData = [matLib]; this.collectGLData(elt, this._glData ); } else { this._glData = null @@ -231,12 +233,27 @@ exports.HTMLDocument = Montage.create(TextDocument, { } else { - var startIndex = importStr.indexOf( "id: " ); - if (startIndex >= 0) { - var endIndex = importStr.indexOf( "\n", startIndex ); - if (endIndex > 0) - id = importStr.substring( startIndex+4, endIndex ); - } + // at this point the data could be either the materials library or + // an old style world. We can determine which by converting the string + // to an object via JSON.parse. That operation will fail if the string + // is an old style world. + var matLibStr = 'materialLibrary;'; + index = importStr.indexOf( matLibStr ); + if (index == 0) + { + importStr = importStr.substr( matLibStr.length ); + var matLibObj = JSON.parse( importStr ); + MaterialsModel.importMaterials( matLibObj ); + } + else + { + var startIndex = importStr.indexOf( "id: " ); + if (startIndex >= 0) { + var endIndex = importStr.indexOf( "\n", startIndex ); + if (endIndex > 0) + id = importStr.substring( startIndex+4, endIndex ); + } + } } if (id != null) diff --git a/js/lib/rdge/materials/bump-metal-material.js b/js/lib/rdge/materials/bump-metal-material.js index 4a6e9ab8..2ef83227 100755 --- a/js/lib/rdge/materials/bump-metal-material.js +++ b/js/lib/rdge/materials/bump-metal-material.js @@ -154,10 +154,6 @@ var BumpMetalMaterial = function BumpMetalMaterial() { this.exportJSON = function() { - var world = this.getWorld(); - if (!world) - throw new Error( "no world in material.export, " + this.getName() ); - var jObj = { 'material' : this.getShaderName(), diff --git a/js/lib/rdge/materials/cloud-material.js b/js/lib/rdge/materials/cloud-material.js new file mode 100644 index 00000000..85088f91 --- /dev/null +++ b/js/lib/rdge/materials/cloud-material.js @@ -0,0 +1,300 @@ +/* + 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; +/////////////////////////////////////////////////////////////////////// +// Class GLMaterial +// RDGE representation of a material. +/////////////////////////////////////////////////////////////////////// +var CloudMaterial = function CloudMaterial() { + /////////////////////////////////////////////////////////////////////// + // Instance variables + /////////////////////////////////////////////////////////////////////// + this._name = "CloudMaterial"; + this._shaderName = "cloud"; + + this._texMap = 'assets/images/cloud2.jpg'; + this._diffuseColor = [0.5, 0.5, 0.5, 0.5]; + + this._time = 0.0; + this._dTime = 0.01; + + /////////////////////////////////////////////////////////////////////// + // Property Accessors + /////////////////////////////////////////////////////////////////////// + this.getName = function() { return this._name; }; + this.getShaderName = function() { return this._shaderName; }; + + this.getTextureMap = function() { return this._propValues[this._propNames[0]] ? this._propValues[this._propNames[0]].slice() : null }; + this.setTextureMap = function(m) { this._propValues[this._propNames[0]] = m ? m.slice(0) : null; this.updateTexture(); }; + + this.setDiffuseColor = function(c) { this._propValues[this._propNames[1]] = c.slice(0); this.updateColor(); }; + this.getDiffuseColor = function() { return this._propValues[this._propNames[1]] ? this._propValues[this._propNames[1]].slice() : null; }; + + this.isAnimated = function() { return true; }; + + /////////////////////////////////////////////////////////////////////// + // Material Property Accessors + /////////////////////////////////////////////////////////////////////// + this._propNames = ["texmap", "diffusecolor"]; + this._propLabels = ["Texture map", "Diffuse Color"]; + this._propTypes = ["file", "color"]; + this._propValues = []; + + this._propValues[ this._propNames[0] ] = this._texMap.slice(0); + this._propValues[ this._propNames[1] ] = this._diffuseColor.slice(); + + this.setProperty = function( prop, value ) + { + if (prop === 'color') prop = 'diffusecolor'; + + // make sure we have legitimate imput + var ok = this.validateProperty( prop, value ); + if (!ok) { + console.log( "invalid property in Radial Gradient Material:" + prop + " : " + value ); + } + + switch (prop) + { + case "texmap": + this.setTextureMap(value); + break; + + case "diffusecolor": + this.setDiffuseColor( value ); + break; + + case "color": + break; + } + }; + /////////////////////////////////////////////////////////////////////// + + + /////////////////////////////////////////////////////////////////////// + // Methods + /////////////////////////////////////////////////////////////////////// + // duplcate method requirde + this.dup = function( world ) + { + // save the world + if (world) this.setWorld( world ); + + // allocate a new uber material + var newMat = new CloudMaterial(); + + // copy over the current values; + var propNames = [], propValues = [], propTypes = [], propLabels = []; + this.getAllProperties( propNames, propValues, propTypes, propLabels); + var n = propNames.length; + for (var i=0; i 200.0) this._time = 0.0; + } + } + }; + + // JSON export + this.exportJSON = function() + { + var jObj = + { + 'material' : this.getShaderName(), + 'name' : this.getName(), + 'texture' : this._propValues[this._propNames[0]] + }; + + return jObj; + }; + + this.importJSON = function( jObj ) + { + if (this.getShaderName() != jObj.material) throw new Error( "ill-formed material" ); + this.setName( jObj.name ); + + try { + this._propValues[this._propNames[0]] = jObj.texture; + } + catch (e) + { + throw new Error( "could not import material: " + jObj ); + } + } + + + this.export = function() { + // every material needs the base type and instance name + var exportStr = "material: " + this.getShaderName() + "\n"; + exportStr += "name: " + this.getName() + "\n"; + + var world = this.getWorld(); + if (!world) + throw new Error( "no world in material.export, " + this.getName() ); + + var texMapName = this._propValues[this._propNames[0]]; + exportStr += "texture: " +texMapName + "\n"; + + // every material needs to terminate like this + exportStr += "endMaterial\n"; + + return exportStr; + }; + + this.import = function( importStr ) { + var pu = new MaterialParser( importStr ); + var material = pu.nextValue( "material: " ); + if (material != this.getShaderName()) throw new Error( "ill-formed material" ); + this.setName( pu.nextValue( "name: ") ); + + var rtnStr; + try { + this._propValues[this._propNames[0]] = pu.nextValue( "texture: " ); + + var endKey = "endMaterial\n"; + var index = importStr.indexOf( endKey ); + index += endKey.length; + rtnStr = importStr.substr( index ); + } + catch (e) + { + throw new Error( "could not import material: " + importStr ); + } + + return rtnStr; + } +}; + +/////////////////////////////////////////////////////////////////////////////////////// +// RDGE shader + +// shader spec (can also be loaded from a .JSON file, or constructed at runtime) +var cloudMaterialDef = +{'shaders': + { + 'defaultVShader':"assets/shaders/Cloud.vert.glsl", + 'defaultFShader':"assets/shaders/Cloud.frag.glsl" + }, + 'techniques': + { + 'default': + [ + { + 'vshader' : 'defaultVShader', + 'fshader' : 'defaultFShader', + // attributes + 'attributes' : + { + 'vert' : { 'type' : 'vec3' }, + 'normal' : { 'type' : 'vec3' }, + 'texcoord' : { 'type' : 'vec2' } + }, + // parameters + 'params' : + { + 'u_tex0': { 'type' : 'tex2d' }, + 'u_time' : { 'type' : 'float' }, + 'u_DiffuseColor' : { 'type' : 'vec4' } + }, + + // render states + 'states' : + { + 'depthEnable' : true, + 'offset':[1.0, 0.1] + } + } + ] + } +}; + + + + +CloudMaterial.prototype = new Material(); + +if (typeof exports === "object") { + exports.CloudMaterial = CloudMaterial; +} + diff --git a/js/lib/rdge/materials/plasma-material.js b/js/lib/rdge/materials/plasma-material.js index 316a5989..b04d8451 100644 --- a/js/lib/rdge/materials/plasma-material.js +++ b/js/lib/rdge/materials/plasma-material.js @@ -69,7 +69,6 @@ var PlasmaMaterial = function PlasmaMaterial() { this.exportJSON = function() { - var jObj = { 'material' : this.getShaderName(), diff --git a/js/lib/rdge/materials/pulse-material.js b/js/lib/rdge/materials/pulse-material.js index fd2cbe0b..e6be69b4 100644 --- a/js/lib/rdge/materials/pulse-material.js +++ b/js/lib/rdge/materials/pulse-material.js @@ -165,10 +165,6 @@ var PulseMaterial = function PulseMaterial() { // JSON export this.exportJSON = function() { - var world = this.getWorld(); - if (!world) - throw new Error( "no world in material.export, " + this.getName() ); - var jObj = { 'material' : this.getShaderName(), diff --git a/js/lib/rdge/materials/radial-blur-material.js b/js/lib/rdge/materials/radial-blur-material.js index d67a3d43..91eebcff 100644 --- a/js/lib/rdge/materials/radial-blur-material.js +++ b/js/lib/rdge/materials/radial-blur-material.js @@ -159,10 +159,6 @@ var RadialBlurMaterial = function RadialBlurMaterial() { this.exportJSON = function() { - var world = this.getWorld(); - if (!world) - throw new Error( "no world in material.export, " + this.getName() ); - var jObj = { 'material' : this.getShaderName(), diff --git a/js/lib/rdge/materials/uber-material.js b/js/lib/rdge/materials/uber-material.js index 0868e3e9..c1d1913c 100755 --- a/js/lib/rdge/materials/uber-material.js +++ b/js/lib/rdge/materials/uber-material.js @@ -579,11 +579,6 @@ var UberMaterial = function UberMaterial() { this.exportJSON = function() { - // we will be needing the world. Make sure it is there - var world = this.getWorld(); - if (!world) - throw new Error( "no world in material.export, " + this.getName() ); - // every material needs the base type and instance name var caps = this._ubershaderCaps; var jObj = diff --git a/js/models/materials-model.js b/js/models/materials-model.js index 9c692f45..291f7567 100755 --- a/js/models/materials-model.js +++ b/js/models/materials-model.js @@ -174,36 +174,47 @@ exports.MaterialsModel = Montage.create(Component, { }, exportMaterials: { - value: function() { - - var exportStr = "MaterialLibrary: \n"; - + value: function() + { + var matArray = []; var nMats = this._materials.length; - for (var i=0; i