From 818582d389f504c915be0c9052fafa33e3e76c92 Mon Sep 17 00:00:00 2001 From: hwc487 Date: Wed, 7 Mar 2012 16:48:48 -0800 Subject: File IO --- js/lib/rdge/materials/bump-metal-material.js | 4 +- js/lib/rdge/materials/deform-material.js | 2 +- js/lib/rdge/materials/flat-material.js | 41 +- js/lib/rdge/materials/fly-material.js | 2 +- js/lib/rdge/materials/julia-material.js | 2 +- js/lib/rdge/materials/keleidoscope-material.js | 2 +- js/lib/rdge/materials/linear-gradient-material.js | 2 +- js/lib/rdge/materials/mandel-material.js | 2 +- js/lib/rdge/materials/plasma-material.js | 7 +- js/lib/rdge/materials/pulse-material.js | 7 +- js/lib/rdge/materials/radial-blur-material.js | 2 +- js/lib/rdge/materials/radial-gradient-material.js | 7 +- js/lib/rdge/materials/relief-tunnel-material.js | 2 +- js/lib/rdge/materials/square-tunnel-material.js | 2 +- js/lib/rdge/materials/star-material.js | 2 +- js/lib/rdge/materials/taper-material.js | 6 +- js/lib/rdge/materials/tunnel-material.js | 2 +- js/lib/rdge/materials/twist-material.js | 2 +- js/lib/rdge/materials/twist-vert-material.js | 6 +- js/lib/rdge/materials/uber-material.js | 71 ++- js/lib/rdge/materials/water-material.js | 2 +- js/lib/rdge/materials/z-invert-material.js | 2 +- js/lib/rdge/runtime/CanvasDataManager.js | 83 +++ js/lib/rdge/runtime/GLRuntime.js | 357 ++++++++++++ js/lib/rdge/runtime/RuntimeGeomObj.js | 630 ++++++++++++++++++++++ js/lib/rdge/runtime/RuntimeMaterial.js | 317 +++++++++++ 26 files changed, 1520 insertions(+), 44 deletions(-) create mode 100644 js/lib/rdge/runtime/CanvasDataManager.js create mode 100644 js/lib/rdge/runtime/GLRuntime.js create mode 100644 js/lib/rdge/runtime/RuntimeGeomObj.js create mode 100644 js/lib/rdge/runtime/RuntimeMaterial.js (limited to 'js/lib/rdge') diff --git a/js/lib/rdge/materials/bump-metal-material.js b/js/lib/rdge/materials/bump-metal-material.js index 61126952..67b16371 100755 --- a/js/lib/rdge/materials/bump-metal-material.js +++ b/js/lib/rdge/materials/bump-metal-material.js @@ -75,7 +75,7 @@ var BumpMetalMaterial = function BumpMetalMaterial() { this.setProperty = function( prop, value ) { // every material should do something with the "color" property - if (prop === "color") prop = "lightDiff"; + if (prop === "color") return; // make sure we have legitimate imput var ok = this.validateProperty( prop, value ); @@ -116,7 +116,7 @@ var BumpMetalMaterial = function BumpMetalMaterial() { this._shader['default'].u_light0Diff.set( this.getLightDiff() ); // set up the material node - this._materialNode = createMaterialNode( this.getShaderName() ); + this._materialNode = createMaterialNode( this.getShaderName() + "_" + world.generateUniqueNodeID() ); this._materialNode.setShader(this._shader); // set some image maps diff --git a/js/lib/rdge/materials/deform-material.js b/js/lib/rdge/materials/deform-material.js index 1e6af6ac..dfd89d12 100644 --- a/js/lib/rdge/materials/deform-material.js +++ b/js/lib/rdge/materials/deform-material.js @@ -60,7 +60,7 @@ var DeformMaterial = function DeformMaterial() { this._shader.init(); // set up the material node - this._materialNode = createMaterialNode("deformMaterial"); + this._materialNode = createMaterialNode("deformMaterial" + "_" + world.generateUniqueNodeID()); this._materialNode.setShader(this._shader); this._time = 0; diff --git a/js/lib/rdge/materials/flat-material.js b/js/lib/rdge/materials/flat-material.js index be8bf2b8..97e4f646 100755 --- a/js/lib/rdge/materials/flat-material.js +++ b/js/lib/rdge/materials/flat-material.js @@ -36,19 +36,27 @@ var FlatMaterial = function FlatMaterial() { // duplcate method requirde this.dup = function() { return new FlatMaterial(); } ; - this.init = function() + this.init = function( world ) { - // set up the shader - this._shader = new jshader(); - this._shader.def = flatShaderDef; - this._shader.init(); - - // set the defaults - this._shader.colorMe.color.set( this.getColor() ); - - // set up the material node - this._materialNode = createMaterialNode("flatMaterial"); - this._materialNode.setShader(this._shader); + // save the world + if (world) + { + this.setWorld( world ); + + // set up the shader + this._shader = new jshader(); + this._shader.def = flatShaderDef; + this._shader.init(); + + // set the defaults + this._shader.colorMe.color.set( this.getColor() ); + + // set up the material node + this._materialNode = createMaterialNode("flatMaterial_" + world.generateUniqueNodeID() ); + this._materialNode.setShader(this._shader); + } + else + throw new Error( "GLWorld not supplied to material initialization" ); }; @@ -73,15 +81,12 @@ var FlatMaterial = function FlatMaterial() { }; /////////////////////////////////////////////////////////////////////// - this.export = function() { + this.export = function() + { // this function should be overridden by subclasses var exportStr = "material: " + this.getShaderName() + "\n"; exportStr += "name: " + this.getName() + "\n"; - - if (this._shader) - exportStr += "color: " + String(this._shader.colorMe.color) + "\n"; - else - exportStr += "color: " + this.getColor() + "\n"; + exportStr += "color: " + String(this._propValues["color"]) + "\n"; exportStr += "endMaterial\n"; return exportStr; diff --git a/js/lib/rdge/materials/fly-material.js b/js/lib/rdge/materials/fly-material.js index 8eadb3ab..bd92ecd8 100644 --- a/js/lib/rdge/materials/fly-material.js +++ b/js/lib/rdge/materials/fly-material.js @@ -53,7 +53,7 @@ var FlyMaterial = function FlyMaterial() { this._shader.init(); // set up the material node - this._materialNode = createMaterialNode("flyMaterial"); + this._materialNode = createMaterialNode("flyMaterial" + "_" + world.generateUniqueNodeID()); this._materialNode.setShader(this._shader); this._time = 0; diff --git a/js/lib/rdge/materials/julia-material.js b/js/lib/rdge/materials/julia-material.js index 07536f33..976dfad3 100644 --- a/js/lib/rdge/materials/julia-material.js +++ b/js/lib/rdge/materials/julia-material.js @@ -55,7 +55,7 @@ var JuliaMaterial = function JuliaMaterial() { this._shader.init(); // set up the material node - this._materialNode = createMaterialNode("juliaMaterial"); + this._materialNode = createMaterialNode("juliaMaterial" + "_" + world.generateUniqueNodeID()); this._materialNode.setShader(this._shader); this._time = 0; diff --git a/js/lib/rdge/materials/keleidoscope-material.js b/js/lib/rdge/materials/keleidoscope-material.js index 3ef5d613..16d1ccfa 100644 --- a/js/lib/rdge/materials/keleidoscope-material.js +++ b/js/lib/rdge/materials/keleidoscope-material.js @@ -60,7 +60,7 @@ var KeleidoscopeMaterial = function KeleidoscopeMaterial() { this._shader.init(); // set up the material node - this._materialNode = createMaterialNode("keleidoscopeMaterial"); + this._materialNode = createMaterialNode("keleidoscopeMaterial" + "_" + world.generateUniqueNodeID()); this._materialNode.setShader(this._shader); this._time = 0; diff --git a/js/lib/rdge/materials/linear-gradient-material.js b/js/lib/rdge/materials/linear-gradient-material.js index 2c52c67d..8e05e23d 100755 --- a/js/lib/rdge/materials/linear-gradient-material.js +++ b/js/lib/rdge/materials/linear-gradient-material.js @@ -212,7 +212,7 @@ var LinearGradientMaterial = function LinearGradientMaterial() { this._shader.init(); // set up the material node - this._materialNode = createMaterialNode( this.getShaderName() ); + this._materialNode = createMaterialNode( this.getShaderName() + "_" + world.generateUniqueNodeID() ); this._materialNode.setShader(this._shader); // send the current values to the shader diff --git a/js/lib/rdge/materials/mandel-material.js b/js/lib/rdge/materials/mandel-material.js index 07e009a2..d9f00383 100644 --- a/js/lib/rdge/materials/mandel-material.js +++ b/js/lib/rdge/materials/mandel-material.js @@ -66,7 +66,7 @@ var MandelMaterial = function MandelMaterial() { this._shader.init(); // set up the material node - this._materialNode = createMaterialNode("mandelMaterial"); + this._materialNode = createMaterialNode("mandelMaterial" + "_" + world.generateUniqueNodeID()); this._materialNode.setShader(this._shader); this._time = 0; diff --git a/js/lib/rdge/materials/plasma-material.js b/js/lib/rdge/materials/plasma-material.js index 9ecc2b48..62d0db70 100644 --- a/js/lib/rdge/materials/plasma-material.js +++ b/js/lib/rdge/materials/plasma-material.js @@ -54,7 +54,10 @@ var PlasmaMaterial = function PlasmaMaterial() { return new PlasmaMaterial(); }; - this.init = function() { + this.init = function( world) + { + this.setWorld( world ); + // set up the shader this._shader = new jshader(); this._shader.def = plasmaShaderDef; @@ -66,7 +69,7 @@ var PlasmaMaterial = function PlasmaMaterial() { this.setProperty( "color", [this._time, 0, 0, 1] ); // set up the material node - this._materialNode = createMaterialNode("plasmaMaterial"); + this._materialNode = createMaterialNode("plasmaMaterial" + "_" + world.generateUniqueNodeID()); this._materialNode.setShader(this._shader); }; diff --git a/js/lib/rdge/materials/pulse-material.js b/js/lib/rdge/materials/pulse-material.js index 63cab2f4..1e3137eb 100644 --- a/js/lib/rdge/materials/pulse-material.js +++ b/js/lib/rdge/materials/pulse-material.js @@ -101,7 +101,7 @@ var PulseMaterial = function PulseMaterial() { this._shader.init(); // set up the material node - this._materialNode = createMaterialNode("pulseMaterial"); + this._materialNode = createMaterialNode("pulseMaterial" + "_" + world.generateUniqueNodeID()); this._materialNode.setShader(this._shader); this._time = 0; @@ -178,6 +178,9 @@ var PulseMaterial = function PulseMaterial() { // every material needs the base type and instance name var exportStr = "material: " + this.getShaderName() + "\n"; exportStr += "name: " + this.getName() + "\n"; + + var texMapName = this._propValues[this._propNames[0]]; + exportStr += "texture: " + texMapName + "\n"; // every material needs to terminate like this exportStr += "endMaterial\n"; @@ -193,6 +196,8 @@ var PulseMaterial = function PulseMaterial() { var rtnStr; try { + this._propValues[this._propNames[0]] = pu.nextValue( "texture: " ); + var endKey = "endMaterial\n"; var index = importStr.indexOf( endKey ); index += endKey.length; diff --git a/js/lib/rdge/materials/radial-blur-material.js b/js/lib/rdge/materials/radial-blur-material.js index 41e68182..89116af4 100644 --- a/js/lib/rdge/materials/radial-blur-material.js +++ b/js/lib/rdge/materials/radial-blur-material.js @@ -94,7 +94,7 @@ var RadialBlurMaterial = function RadialBlurMaterial() { this._shader.init(); // set up the material node - this._materialNode = createMaterialNode("radialBlurMaterial"); + this._materialNode = createMaterialNode("radialBlurMaterial" + "_" + world.generateUniqueNodeID()); this._materialNode.setShader(this._shader); this._time = 0; diff --git a/js/lib/rdge/materials/radial-gradient-material.js b/js/lib/rdge/materials/radial-gradient-material.js index 13be50f5..d19b44b7 100755 --- a/js/lib/rdge/materials/radial-gradient-material.js +++ b/js/lib/rdge/materials/radial-gradient-material.js @@ -193,14 +193,17 @@ var RadialGradientMaterial = function RadialGradientMaterial() { return new RadialGradientMaterial(); }; - this.init = function() { + this.init = function( world ) + { + this.setWorld( world ); + // set up the shader this._shader = new jshader(); this._shader.def = radialGradientMaterialDef; this._shader.init(); // set up the material node - this._materialNode = createMaterialNode("radialGradientMaterial"); + this._materialNode = createMaterialNode("radialGradientMaterial" + "_" + world.generateUniqueNodeID()); this._materialNode.setShader(this._shader); // set the shader values in the shader diff --git a/js/lib/rdge/materials/relief-tunnel-material.js b/js/lib/rdge/materials/relief-tunnel-material.js index 20290895..52c40543 100644 --- a/js/lib/rdge/materials/relief-tunnel-material.js +++ b/js/lib/rdge/materials/relief-tunnel-material.js @@ -54,7 +54,7 @@ var ReliefTunnelMaterial = function ReliefTunnelMaterial() { this._shader.init(); // set up the material node - this._materialNode = createMaterialNode("reliefTunnelMaterial"); + this._materialNode = createMaterialNode("reliefTunnelMaterial" + "_" + world.generateUniqueNodeID()); this._materialNode.setShader(this._shader); this._time = 0; diff --git a/js/lib/rdge/materials/square-tunnel-material.js b/js/lib/rdge/materials/square-tunnel-material.js index b5cbff30..9264872f 100644 --- a/js/lib/rdge/materials/square-tunnel-material.js +++ b/js/lib/rdge/materials/square-tunnel-material.js @@ -55,7 +55,7 @@ var SquareTunnelMaterial = function SquareTunnelMaterial() { this._shader.init(); // set up the material node - this._materialNode = createMaterialNode("squareTunnelMaterial"); + this._materialNode = createMaterialNode("squareTunnelMaterial" + "_" + world.generateUniqueNodeID()); this._materialNode.setShader(this._shader); this._time = 0; diff --git a/js/lib/rdge/materials/star-material.js b/js/lib/rdge/materials/star-material.js index 9c0f3172..d7d797e4 100644 --- a/js/lib/rdge/materials/star-material.js +++ b/js/lib/rdge/materials/star-material.js @@ -55,7 +55,7 @@ var StarMaterial = function StarMaterial() { this._shader.init(); // set up the material node - this._materialNode = createMaterialNode("starMaterial"); + this._materialNode = createMaterialNode("starMaterial" + "_" + world.generateUniqueNodeID()); this._materialNode.setShader(this._shader); this._time = 0; diff --git a/js/lib/rdge/materials/taper-material.js b/js/lib/rdge/materials/taper-material.js index eeb08aec..bd89f08f 100644 --- a/js/lib/rdge/materials/taper-material.js +++ b/js/lib/rdge/materials/taper-material.js @@ -42,8 +42,10 @@ function TaperMaterial() // duplcate method requirde this.dup = function() { return new TaperMaterial(); } - this.init = function() + this.init = function( world ) { + this.setWorld( world ); + // set up the shader this._shader = new jshader(); this._shader.def = taperShaderDef; @@ -53,7 +55,7 @@ function TaperMaterial() this._shader.colorMe.color.set( this.getColor() ); // set up the material node - this._materialNode = createMaterialNode("taperMaterial"); + this._materialNode = createMaterialNode("taperMaterial" + "_" + world.generateUniqueNodeID()); this._materialNode.setShader(this._shader); // initialize the taper properties diff --git a/js/lib/rdge/materials/tunnel-material.js b/js/lib/rdge/materials/tunnel-material.js index fe787d16..fe0fbd51 100644 --- a/js/lib/rdge/materials/tunnel-material.js +++ b/js/lib/rdge/materials/tunnel-material.js @@ -56,7 +56,7 @@ var TunnelMaterial = function TunnelMaterial() { this._shader.init(); // set up the material node - this._materialNode = createMaterialNode("tunnelMaterial"); + this._materialNode = createMaterialNode("tunnelMaterial" + "_" + world.generateUniqueNodeID()); this._materialNode.setShader(this._shader); this._time = 0; diff --git a/js/lib/rdge/materials/twist-material.js b/js/lib/rdge/materials/twist-material.js index eb562e99..163c8a77 100644 --- a/js/lib/rdge/materials/twist-material.js +++ b/js/lib/rdge/materials/twist-material.js @@ -56,7 +56,7 @@ var TwistMaterial = function TwistMaterial() { this._shader.init(); // set up the material node - this._materialNode = createMaterialNode("twistMaterial"); + this._materialNode = createMaterialNode("twistMaterial" + "_" + world.generateUniqueNodeID()); this._materialNode.setShader(this._shader); this._time = 0; diff --git a/js/lib/rdge/materials/twist-vert-material.js b/js/lib/rdge/materials/twist-vert-material.js index c2573071..05172a1b 100644 --- a/js/lib/rdge/materials/twist-vert-material.js +++ b/js/lib/rdge/materials/twist-vert-material.js @@ -46,8 +46,10 @@ function TwistVertMaterial() // duplcate method requirde this.dup = function() { return new TwistVertMaterial(); } - this.init = function() + this.init = function( world ) { + this.setWorld( world ); + // set up the shader this._shader = new jshader(); this._shader.def = twistVertShaderDef; @@ -57,7 +59,7 @@ function TwistVertMaterial() this._shader.twistMe.color.set( this.getColor() ); // set up the material node - this._materialNode = createMaterialNode("twistVertMaterial"); + this._materialNode = createMaterialNode("twistVertMaterial" + "_" + world.generateUniqueNodeID()); this._materialNode.setShader(this._shader); // initialize the twist vert properties diff --git a/js/lib/rdge/materials/uber-material.js b/js/lib/rdge/materials/uber-material.js index 6bc35d51..0964c9c4 100755 --- a/js/lib/rdge/materials/uber-material.js +++ b/js/lib/rdge/materials/uber-material.js @@ -363,10 +363,79 @@ var UberMaterial = function UberMaterial() { this._shader = this.buildUberShader( this._ubershaderCaps ); // set up the material node - this._materialNode = createMaterialNode("uberMaterial"); + this._materialNode = createMaterialNode("uberMaterial" + "_" + world.generateUniqueNodeID()); this._materialNode.setShader(this._shader); }; + this.import = function( importStr ) + { + // limit the key searches to this material + var endKey = "endMaterial\n"; + var index = importStr.indexOf( endKey ); + index += endKey.length; + importStr = importStr.substr( index ); + } + + this.export = function() + { + // every material needs the base type and instance name + var exportStr = "material: " + this.getShaderName() + "\n"; + exportStr += "name: " + this.getName() + "\n"; + + var caps = this._ubershaderCaps; + + // export the material properties + if (typeof caps.material != 'undefined') + { + exportStr += "material: true\n"; + exportStr += "ambientColor: " + caps.material.ambientColor + "\n"; + exportStr += "diffuseColor: " + caps.material.diffuseColor + "\n"; + exportStr += "specularColor: " + caps.material.specularColor + "\n"; + exportStr += "specularPower: " + caps.material.specularPower + "\n"; + } + + if (typeof caps.lighting != 'undefined') + { + var light = caps.lighting['light' + i]; + var t; + for (var i=0; i +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 GeomObj = require("js/lib/geom/geom-obj").GeomObj; +var ShapePrimitive = require("js/lib/geom/shape-primitive").ShapePrimitive; +var MaterialsModel = require("js/models/materials-model").MaterialsModel; +var GLRuntime = require("js/lib/rdge/runtime/GLRuntime").GLRuntime; + +/////////////////////////////////////////////////////////////////////// +// Class ShapeRuntime +// Manages runtime shape display +/////////////////////////////////////////////////////////////////////// +var CanvasDataManager = function CanvasDataManager() +{ + this.loadGLData = function(root, valueArray, NinjaUtils) + { + var value = valueArray; + var nWorlds = value.length; + for (var i=0; i= 0) + { + var endIndex = importStr.indexOf( "\n", startIndex ); + if (endIndex > 0) + { + var id = importStr.substring( startIndex+4, endIndex ); + var canvas = this.findCanvasWithID( id, root ); + if (canvas) + { + var rt = new GLRuntime( canvas, importStr ); + } + } + } + } + } + + this.collectGLData = function( elt, dataArray ) + { + if (elt.elementModel && elt.elementModel.shapeModel && elt.elementModel.shapeModel.GLWorld) + { + var data = elt.elementModel.shapeModel.GLWorld.export( true ); + dataArray.push( data ); + } + + if (elt.children) + { + var nKids = elt.children.length; + for (var i=0; i +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 GeomObj = require("js/lib/geom/geom-obj").GeomObj, + RuntimeGeomObj = require("js/lib/rdge/runtime/RuntimeGeomObj"), + RuntimeRectangle = RuntimeGeomObj.RuntimeRectangle, + RuntimeOval = RuntimeGeomObj.RuntimeOval, + getPropertyFromString = require("js/lib/rdge/runtime/RuntimeGeomObj").getPropertyFromString; + +/////////////////////////////////////////////////////////////////////// +// Class GLRuntime +// Manages runtime fora WebGL canvas +/////////////////////////////////////////////////////////////////////// +var GLRuntime = function GLRuntime( canvas, importStr ) +{ + /////////////////////////////////////////////////////////////////////// + // Instance variables + /////////////////////////////////////////////////////////////////////// + this._canvas = canvas; + this._context = null; + this._importStr = importStr; + + this.renderer = null; + this.myScene = null; + this.light = null; + this.light2 = null; + this._rootNode = null; + + this._firstRender = true; + this._initialized = false; + + this._useWebGL = false; + + // view parameters + this._fov = 45.0; + this._zNear = 0.1; + this._zFar = 100.0; + this._viewDist = 5.0; + + this._aspect = canvas.width/canvas.height; + + this._geomRoot; + + // all "live" materials + this._materials = []; + + /////////////////////////////////////////////////////////////////////// + // accessors + /////////////////////////////////////////////////////////////////////// + this.getZNear = function() { return this._zNear; } + this.getZFar = function() { return this._zFar; } + this.getFOV = function() { return this._fov; } + this.getAspect = function() { return this._aspect; } + this.getViewDistance = function() { return this._viewDist; } + + this.get2DContext = function() { return this._context; } + + this.getViewportWidth = function() { return this._canvas.width; } + this.getViewportHeight = function() { return this._canvas.height; } + + /////////////////////////////////////////////////////////////////////// + // accessors + /////////////////////////////////////////////////////////////////////// + this.loadScene = function() + { + // parse the data + // the GL runtime must start with a "sceneData: " + var index = importStr.indexOf( "scenedata: " ); + if (index >= 0) + { + this._useWebGL = true; + + var rdgeStr = importStr.substr( index+11 ); + var endIndex = rdgeStr.indexOf( "endscene\n" ); + if (endIndex < 0) throw new Error( "ill-formed WebGL data" ); + var len = endIndex - index + 11; + rdgeStr = rdgeStr.substr( 0, endIndex ); + + this.myScene.importJSON( rdgeStr ); + this.importObjects( importStr ); + this.linkMaterials( this._geomRoot ); + this.initMaterials(); + } + else + { + this._context = this._canvas.getContext( "2d" ); + this.importObjects( importStr ); + this.render(); + } + } + + this.init = function() + { + var ctx1 = g_Engine.ctxMan.handleToObject(this._canvas.rdgeCtxHandle), + ctx2 = g_Engine.getContext(); + if (ctx1 != ctx2) console.log( "***** different contexts *****" ); + this.renderer = ctx1.renderer; + + // create a camera, set its perspective, and then point it at the origin + var cam = new camera(); + this._camera = cam; + cam.setPerspective(this.getFOV(), this.getAspect(), this.getZNear(), this.getZFar()); + cam.setLookAt([0, 0, this.getViewDistance()], [0, 0, 0], vec3.up()); + + // make this camera the active camera + this.renderer.cameraManager().setActiveCamera(cam); + + // change clear color + this.renderer.setClearColor([1.0, 1.0, 1.0, 0.0]); + + // create an empty scene graph + this.myScene = new SceneGraph(); + + // load the scene graph data + this.loadScene(); + + // Add the scene to the engine - necessary if you want the engine to draw for you + var name = "myScene" + this._canvas.getAttribute( "data-RDGE-id" ); + g_Engine.AddScene(name, this.myScene); + + this._initialized = true; + } + + // main code for handling user interaction and updating the scene + this.update = function(dt) + { + if (this._initialized) + { + if (!dt) dt = 0.2; + + dt = 0.01; // use our own internal throttle + this.elapsed += dt; + + // changed the global position uniform of light 0, another way to change behavior of a light + rdgeGlobalParameters.u_light0Pos.set( [5*Math.cos(this.elapsed), 5*Math.sin(this.elapsed), 20]); + + // orbit the light nodes around the boxes + //this.light.setPosition([1.2*Math.cos(this.elapsed*2.0), 1.2*Math.sin(this.elapsed*2.0), 1.2*Math.cos(this.elapsed*2.0)]); + //this.light2.setPosition([-1.2*Math.cos(this.elapsed*2.0), 1.2*Math.sin(this.elapsed*2.0), -1.2*Math.cos(this.elapsed)]); + + this.updateMaterials(); + + // now update all the nodes in the scene + this.myScene.update(dt); + } + } + + this.updateMaterials = function() + { + var nMats = this._materials.length; + for (var i=0; i= 0) + { + // update the string to the current object + importStr = importStr.substr( index+7 ); + + // read the next object + var obj = this.importObject( importStr, parent ); + + // determine if we have children + var endIndex = importStr.indexOf( "ENDOBJECT\n" ), + childIndex = importStr.indexOf( "OBJECT\n" ); + if (endIndex < 0) throw new Error( "ill-formed object data" ); + if ((childIndex >= 0) && (childIndex < endIndex)) + { + importStr = importStr.substr( childIndex + 7 ); + importStr = this.importObjects( importStr, obj ); + endIndex = importStr.indexOf( "ENDOBJECT\n" ) + } + + // remove the string for the object(s) just created + importStr = importStr.substr( endIndex ); + + // get the location of the next object + index = importStr.indexOf( "OBJECT\n", endIndex ); + } + + return importStr; + } + + this.importObject = function( objStr, parent ) + { + var type = Number( getPropertyFromString( "type: ", objStr ) ); + + var obj; + switch (type) + { + case 1: + obj = new RuntimeRectangle(); + obj.import( objStr, parent ); + break; + + case 2: // circle + obj = new RuntimeOval(); + obj.import( objStr, parent ); + break; + + case 3: // line + obj = new RuntimeLine(); + obj.import( objStr, parent ); + break; + + default: + throw new Error( "Attempting to load unrecognized object type: " + type ); + break; + } + + if (obj) + this.addObject( obj ); + + return obj; + } + + this.addObject = function( obj, parent ) + { + if (!obj) return; + obj.setWorld( this ); + + if (parent == null) + this._geomRoot = obj; + else + parent.addChild( obj ); + } + + this.linkMaterials = function( obj ) + { + if (!obj) return; + + // get the array of materials from the object + var matArray = obj._materials; + var nMats = matArray.length; + for (var i=0; i= 0); + if (this._useWebGL) + { + var id = canvas.getAttribute( "data-RDGE-id" ); + canvas.rdgeid = id; + g_Engine.registerCanvas(canvas, this); + RDGEStart( canvas ); + } + else + { + this.loadScene(); + } +} + + +if (typeof exports === "object") { + exports.GLRuntime = GLRuntime; +} + + + diff --git a/js/lib/rdge/runtime/RuntimeGeomObj.js b/js/lib/rdge/runtime/RuntimeGeomObj.js new file mode 100644 index 00000000..5b212dce --- /dev/null +++ b/js/lib/rdge/runtime/RuntimeGeomObj.js @@ -0,0 +1,630 @@ + +/* +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 RuntimeMaterial = require("js/lib/rdge/runtime/RuntimeMaterial"); +var RuntimeFlatMaterial = RuntimeMaterial.RuntimeFlatMaterial, + RuntimeRadialGradientMaterial = RuntimeMaterial.RuntimeRadialGradientMaterial, + RuntimeLinearGradientMaterial = RuntimeMaterial.RuntimeLinearGradientMaterial, + RuntimeBumpMetalMaterial = RuntimeMaterial.RuntimeBumpMetalMaterial, + RuntimeUberMaterial = RuntimeMaterial.RuntimeUberMaterial, + RuntimePulseMaterial = RuntimeMaterial.RuntimePulseMaterial; + + + +/////////////////////////////////////////////////////////////////////// +// Class RuntimeGeomObj +// Super class for all geometry classes +/////////////////////////////////////////////////////////////////////// +var RuntimeGeomObj = function RuntimeGeomObj() +{ + /////////////////////////////////////////////////////////////////////// + // Constants + /////////////////////////////////////////////////////////////////////// + this.GEOM_TYPE_RECTANGLE = 1; + this.GEOM_TYPE_CIRCLE = 2; + this.GEOM_TYPE_LINE = 3; + this.GEOM_TYPE_PATH = 4; + this.GEOM_TYPE_CUBIC_BEZIER = 5; + this.GEOM_TYPE_UNDEFINED = -1; + + /////////////////////////////////////////////////////////////////////// + // Instance variables + /////////////////////////////////////////////////////////////////////// + this._children; + + // stroke and fill colors + this._strokeColor = [0,0,0,0]; + this._fillColor = [0,0,0,0]; + + // array of materials + this._materials = []; + + /////////////////////////////////////////////////////////////////////// + // Property accessors + /////////////////////////////////////////////////////////////////////// + + this.geomType = function() { return this.GEOM_TYPE_UNDEFINED; } + + this.setWorld = function(w) { this._world = w; } + this.getWorld = function() { return this._world; } + + /////////////////////////////////////////////////////////////////////// + // Methods + /////////////////////////////////////////////////////////////////////// + this.makeStrokeMaterial = function() + { + } + + this.makeFillMaterial = function() + { + } + + + this.render = function() + { + } + + this.addChild = function( child ) + { + if (!this._children) this._children = []; + this._children.push( child ); + } + + this.import = function() + { + } + + this.importMaterials = function(importStr) + { + var nMaterials = Number( getPropertyFromString( "nMaterials: ", importStr ) ); + for (var i=0; i= 0.001) + { + var scale = len/sum; + rtnVec = []; + for (var i=0; i= 0) + rtnStr = rtnStr.substr(0, index); + + return rtnStr; +} + +/////////////////////////////////////////////////////////////////////// +// Class RuntimeRectangle +/////////////////////////////////////////////////////////////////////// +var RuntimeRectangle = function RuntimeRectangle() +{ + // inherit the members of RuntimeGeomObj + this.inheritedFrom = RuntimeGeomObj; + this.inheritedFrom(); + + this.import = function( importStr ) + { + this._xOffset = Number( getPropertyFromString( "xoff: ", importStr ) ); + this._yOffset = Number( getPropertyFromString( "yoff: ", importStr ) ); + this._width = Number( getPropertyFromString( "width: ", importStr ) ); + this._height = Number( getPropertyFromString( "height: ", importStr ) ); + this._strokeWidth = Number( getPropertyFromString( "strokeWidth: ", importStr ) ); + this._innerRadius = Number( getPropertyFromString( "innerRadius: ", importStr ) ); + this._strokeStyle = Number( getPropertyFromString( "strokeStyle: ", importStr ) ); + var strokeMaterialName = getPropertyFromString( "strokeMat: ", importStr ); + var fillMaterialName = getPropertyFromString( "fillMat: ", importStr ); + this._strokeStyle = getPropertyFromString( "strokeStyle: ", importStr ); + this._fillColor = eval( "[" + getPropertyFromString( "fillColor: ", importStr ) + "]" ); + this._strokeColor = eval( "[" + getPropertyFromString( "strokeColor: ", importStr ) + "]" ); + this._tlRadius = Number( getPropertyFromString( "tlRadius: ", importStr ) ); + this._trRadius = Number( getPropertyFromString( "trRadius: ", importStr ) ); + this._blRadius = Number( getPropertyFromString( "blRadius: ", importStr ) ); + this._brRadius = Number( getPropertyFromString( "brRadius: ", importStr ) ); + + this.importMaterials( importStr ); + } + + this.renderPath = function( inset, ctx ) + { + // various declarations + var pt, rad, ctr, startPt, bPts; + var width = Math.round(this._width), + height = Math.round(this._height); + + pt = [inset, inset]; // top left corner + + var tlRad = this._tlRadius; //top-left radius + var trRad = this._trRadius; + var blRad = this._blRadius; + var brRad = this._brRadius; + + if ((tlRad <= 0) && (blRad <= 0) && (brRad <= 0) && (trRad <= 0)) + { + ctx.rect(pt[0], pt[1], width - 2*inset, height - 2*inset); + } + else + { + // get the top left point + rad = tlRad - inset; + if (rad < 0) rad = 0; + pt[1] += rad; + if (Math.abs(rad) < 0.001) pt[1] = inset; + ctx.moveTo( pt[0], pt[1] ); + + // get the bottom left point + pt = [inset, height - inset]; + rad = blRad - inset; + if (rad < 0) rad = 0; + pt[1] -= rad; + ctx.lineTo( pt[0], pt[1] ); + + // get the bottom left curve + if (rad > 0.001) + ctx.quadraticCurveTo( inset, height-inset, inset+rad, height-inset ); + + // do the bottom of the rectangle + pt = [width - inset, height - inset]; + rad = brRad - inset; + if (rad < 0) rad = 0; + pt[0] -= rad; + ctx.lineTo( pt[0], pt[1] ); + + // get the bottom right arc + if (rad > 0.001) + ctx.quadraticCurveTo( width-inset, height-inset, width-inset, height-inset-rad ); + + // get the right of the rectangle + pt = [width - inset, inset]; + rad = trRad - inset; + if (rad < 0) rad = 0; + pt[1] += rad; + ctx.lineTo( pt[0], pt[1] ); + + // do the top right corner + if (rad > 0.001) + ctx.quadraticCurveTo( width-inset, inset, width-inset-rad, inset ); + + // do the top of the rectangle + pt = [inset, inset] + rad = tlRad - inset; + if (rad < 0) rad = 0; + pt[0] += rad; + ctx.lineTo( pt[0], pt[1] ); + + // do the top left corner + if (rad > 0.001) + ctx.quadraticCurveTo( inset, inset, inset, inset+rad ); + else + ctx.lineTo( inset, 2*inset ); + } + } + + this.render = function() + { + // get the world + var world = this.getWorld(); + if (!world) throw( "null world in rectangle render" ); + + // get the context + var ctx = world.get2DContext(); + if (!ctx) return; + + // get some dimensions + var lw = this._strokeWidth; + var w = world.getViewportWidth(), + h = world.getViewportHeight(); + + // render the fill + ctx.beginPath(); + if (this._fillColor) + { + var c = "rgba(" + 255*this._fillColor[0] + "," + 255*this._fillColor[1] + "," + 255*this._fillColor[2] + "," + this._fillColor[3] + ")"; + ctx.fillStyle = c; + + ctx.lineWidth = lw; + var inset = Math.ceil( lw ) + 0.5; + this.renderPath( inset, ctx ); + ctx.fill(); + ctx.closePath(); + } + + // render the stroke + ctx.beginPath(); + if (this._strokeColor) + { + var c = "rgba(" + 255*this._strokeColor[0] + "," + 255*this._strokeColor[1] + "," + 255*this._strokeColor[2] + "," + this._strokeColor[3] + ")"; + ctx.strokeStyle = c; + + ctx.lineWidth = lw; + var inset = Math.ceil( 0.5*lw ) + 0.5; + this.renderPath( inset, ctx ); + ctx.stroke(); + ctx.closePath(); + } + } +} + +/////////////////////////////////////////////////////////////////////// +// Class RuntimeOval +/////////////////////////////////////////////////////////////////////// +var RuntimeOval = function RuntimeOval() +{ + // inherit the members of RuntimeGeomObj + this.inheritedFrom = RuntimeGeomObj; + this.inheritedFrom(); + + this.import = function( importStr ) + { + this._xOffset = Number( getPropertyFromString( "xoff: ", importStr ) ); + this._yOffset = Number( getPropertyFromString( "yoff: ", importStr ) ); + this._width = Number( getPropertyFromString( "width: ", importStr ) ); + this._height = Number( getPropertyFromString( "height: ", importStr ) ); + this._strokeWidth = Number( getPropertyFromString( "strokeWidth: ", importStr ) ); + this._innerRadius = Number( getPropertyFromString( "innerRadius: ", importStr ) ); + this._strokeStyle = getPropertyFromString( "strokeStyle: ", importStr ); + var strokeMaterialName = getPropertyFromString( "strokeMat: ", importStr ); + var fillMaterialName = getPropertyFromString( "fillMat: ", importStr ); + this._fillColor = eval( "[" + getPropertyFromString( "fillColor: ", importStr ) + "]" ); + this._strokeColor = eval( "[" + getPropertyFromString( "strokeColor: ", importStr ) + "]" ); + + this.importMaterials( importStr ); + } + + this.render = function() + { + // get the world + var world = this.getWorld(); + if (!world) throw( "null world in buildBuffers" ); + + // get the context + var ctx = world.get2DContext(); + if (!ctx) return; + + // declare some variables + var p0, p1; + var x0, y1, x1, y1; + + // create the matrix + var lineWidth = this._strokeWidth; + var innerRad = this._innerRadius; + var xScale = 0.5*this._width - lineWidth, + yScale = 0.5*this._height - lineWidth; + + // translate + var xCtr = 0.5*world.getViewportWidth() + this._xOffset, + yCtr = 0.5*world.getViewportHeight() + this._yOffset; + var mat = Matrix.create( [ + [ xScale, 0.0, 0.0, xCtr], + [ 0.0, yScale, 0.0, yCtr], + [ 0.0, 0.0, 1.0, 0.0], + [ 0.0, 0.0, 0.0, 1.0] + ] ); + + // get a bezier representation of the circle + var bezPts = this.circularArcToBezier( [0,0,0], [1,0,0], 2.0*Math.PI ); + if (bezPts) + { + var n = bezPts.length; + + // set up the fill style + ctx.beginPath(); + ctx.lineWidth = 0; + if (this._fillColor) + { + var c = "rgba(" + 255*this._fillColor[0] + "," + 255*this._fillColor[1] + "," + 255*this._fillColor[2] + "," + this._fillColor[3] + ")"; + ctx.fillStyle = c; + + // draw the fill + ctx.beginPath(); + var p = this.transformPoint( bezPts[0], mat ); + ctx.moveTo( p[0], p[1] ); + var index = 1; + while (index < n) + { + p0 = this.transformPoint( bezPts[index], mat ); + p1 = this.transformPoint( bezPts[index+1], mat ); + + x0 = p0[0]; y0 = p0[1]; + x1 = p1[0]; y1 = p1[1]; + ctx.quadraticCurveTo( x0, y0, x1, y1 ); + index += 2; + } + + if ( innerRad > 0.001) + { + xScale = 0.5*innerRad*this._width; + yScale = 0.5*innerRad*this._height; + mat[0] = xScale; + mat[5] = yScale; + + // get the bezier points + var bezPts = this.circularArcToBezier( Vector.create([0,0,0]), Vector.create([1,0,0]), -2.0*Math.PI ); + if (bezPts) + { + var n = bezPts.length; + p = this.transformPoint( bezPts[0], mat ); + ctx.moveTo( p[0], p[1] ); + index = 1; + while (index < n) + { + p0 = this.transformPoint( bezPts[index], mat ); + p1 = this.transformPoint( bezPts[index+1], mat ); + + var x0 = p0[0], y0 = p0[1], + x1 = p1[0], y1 = p1[1]; + ctx.quadraticCurveTo( x0, y0, x1, y1 ); + index += 2; + } + } + } + + // fill the path + ctx.fill(); + } + + // calculate the stroke matrix + xScale = 0.5*this._width - 0.5*lineWidth; + yScale = 0.5*this._height - 0.5*lineWidth; + mat[0] = xScale; + mat[5] = yScale; + + // set up the stroke style + ctx.beginPath(); + ctx.lineWidth = lineWidth; + if (this._strokeColor) + { + var c = "rgba(" + 255*this._strokeColor[0] + "," + 255*this._strokeColor[1] + "," + 255*this._strokeColor[2] + "," + this._strokeColor[3] + ")"; + ctx.strokeStyle = c; + + // draw the stroke + p = this.transformPoint( bezPts[0], mat ); + ctx.moveTo( p[0], p[1] ); + index = 1; + while (index < n) + { + var p0 = this.transformPoint( bezPts[index], mat ); + var p1 = this.transformPoint( bezPts[index+1], mat ); + + var x0 = p0[0], y0 = p0[1], + x1 = p1[0], y1 = p1[1]; + ctx.quadraticCurveTo( x0, y0, x1, y1 ); + index += 2; + } + + if (innerRad > 0.01) + { + // calculate the stroke matrix + xScale = 0.5*innerRad*this._width - 0.5*lineWidth; + yScale = 0.5*innerRad*this._height - 0.5*lineWidth; + mat[0] = xScale; + mat[5] = yScale; + + // draw the stroke + p = this.transformPoint( bezPts[0], mat ); + ctx.moveTo( p[0], p[1] ); + index = 1; + while (index < n) + { + var p0 = this.transformPoint( bezPts[index], mat ); + var p1 = this.transformPoint( bezPts[index+1], mat ); + + var x0 = p0[0], y0 = p0[1], + x1 = p1[0], y1 = p1[1]; + ctx.quadraticCurveTo( x0, y0, x1, y1 ); + index += 2; + } + } + + // render the stroke + ctx.stroke(); + } + } + } + + /////////////////////////////////////////////////////////////////////// + // this function returns the quadratic Bezier approximation to the specified + // circular arc. The input can be 2D or 3D, determined by the minimum dimension + // of the center and start point. + // includedAngle is in radians, can be positiveor negative + this.circularArcToBezier= function( ctr_, startPt_, includedAngle ) + { + var dimen = 3; + var ctr = ctr_.slice(); + var startPt = startPt_.slice(); + + // make sure the start point is good + var pt = this.vecSubtract(dimen, startPt, ctr); + var rad = this.vecMag(dimen, pt); + + if ((dimen != 3) || (rad <= 0) || (includedAngle === 0)) + { + if (dimen != 3) console.log( "circularArcToBezier works for 3 dimensional points only. Was " + dimen ); + return [ startPt.slice(0), startPt.slice(0), startPt.slice(0) ]; + } + + // determine the number of segments. 45 degree span maximum. + var nSegs = Math.ceil( Math.abs(includedAngle)/(0.25*Math.PI) ); + if (nSegs <= 0) return [ startPt.slice(0), startPt.slice(0), startPt.slice(0) ]; + var dAngle = includedAngle/nSegs; + + // determine the length of the center control point from the circle center + var cs = Math.cos( 0.5*Math.abs(dAngle) ), sn = Math.sin( 0.5*Math.abs(dAngle) ); + var c = rad*sn; + var h = c*sn/cs; + var d = rad*cs + h; + + var rtnPts = [ this.vecAdd(dimen, pt, ctr) ]; + var rotMat = Matrix.RotationZ( dAngle ); + for ( var i=0; i +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 RuntimeGeomObjDict = require("js/lib/rdge/runtime/RuntimeGeomObj"); +//var getPropertyFromString = RuntimeGeomObjDict.getPropertyFromString; + +//var GeomObj = require("js/lib/geom/geom-obj").GeomObj, + //MaterialsModel = require("js/models/materials-model").MaterialsModel, + //CanvasDataManager = require("js/lib/rdge/runtime/CanvasDataManager"), + //RuntimeGeomObj = require("js/lib/rdge/runtime/RuntimeGeomObj"), + //RuntimeRectangle = RuntimeGeomObj.RuntimeRectangle, + //RuntimeOval = RuntimeGeomObj.RuntimeOval, + //getPropertyFromString = require("js/lib/rdge/runtime/RuntimeGeomObj").getPropertyFromString; + +/////////////////////////////////////////////////////////////////////// +// Class RuntimeMaterial +// Runtime representation of a material. +/////////////////////////////////////////////////////////////////////// +var RuntimeMaterial = function RuntimeMaterial( world ) +{ + /////////////////////////////////////////////////////////////////////// + // Instance variables + /////////////////////////////////////////////////////////////////////// + this._name = "GLMaterial"; + this._shaderName = "undefined"; + + // variables for animation speed + this._time = 0.0; + this._dTime = 0.01; + + // RDGE variables + this._shader; + this._materialNode; + + /////////////////////////////////////////////////////////////////////// + // Property Accessors + /////////////////////////////////////////////////////////////////////// + + // a material can be animated or not. default is not. + // Any material needing continuous rendering should override this method + this.isAnimated = function() { return false; } + + /////////////////////////////////////////////////////////////////////// + // Methods + /////////////////////////////////////////////////////////////////////// + this.init = function() + { + } + + this.update = function( time ) + { + } + + this.getPropertyFromString = function( prop, str ) + { + var index = str.indexOf( prop ); + if (index < 0) throw new Error( "property " + prop + " not found in string: " + str); + + var rtnStr = str.substr( index+prop.length ); + index = rtnStr.indexOf( "\n" ); + if (index >= 0) + rtnStr = rtnStr.substr(0, index); + + return rtnStr; + } + +} + +function RuntimeFlatMaterial() +{ + // inherit the members of RuntimeMaterial + this.inheritedFrom = RuntimeMaterial; + this.inheritedFrom(); + + this._name = "FlatMaterial"; + this._shaderName = "flat"; + + // assign a default color + this._color = [1,0,0,1]; + + this.import = function( importStr ) + { + var colorStr = this.getPropertyFromString( "color: ", importStr ); + if (colorStr) + this._color = eval( "[" + colorStr + "]" ); + }; + + + this.init = function() + { + if (this._shader) + { + this._shader.colorMe["color"].set( this._color ); + } + } +} + +function RuntimePulseMaterial() +{ + // inherit the members of RuntimeMaterial + this.inheritedFrom = RuntimeMaterial; + this.inheritedFrom(); + + this._name = "PulseMaterial"; + this._shaderName = "pulse"; + + this._texMap = 'assets/images/cubelight.png'; + + this.isAnimated = function() { return true; } + + + this.import = function( importStr ) + { + this._texMap = this.getPropertyFromString( "texture: ", importStr ); + } + + this.init = function() + { + var material = this._materialNode; + if (material) + { + var technique = material.shaderProgram.default; + var renderer = g_Engine.getContext().renderer; + if (renderer && technique) + { + if (this._shader && this._shader.default) + { + var res = [ renderer.vpWidth, renderer.vpHeight ]; + technique.u_resolution.set( res ); + + var wrap = 'REPEAT', mips = true; + var tex = renderer.getTextureByName(this._texMap, wrap, mips ); + if (tex) + technique.u_tex0.set( tex ); + + this._shader.default.u_time.set( [this._time] ); + } + } + } + } + + // several materials inherit from pulse. + // they may share this update method + this.update = function( time ) + { + var material = this._materialNode; + if (material) + { + var technique = material.shaderProgram.default; + var renderer = g_Engine.getContext().renderer; + if (renderer && technique) + { + if (this._shader && this._shader.default) + this._shader.default.u_time.set( [this._time] ); + this._time += this._dTime; + if (this._time > 200.0) this._time = 0.0; + } + } + } +} + +function RuntimeRadialGradientMaterial() +{ + // inherit the members of RuntimeMaterial + this.inheritedFrom = RuntimeMaterial; + this.inheritedFrom(); + + this._name = "RadialGradientMaterial"; + this._shaderName = "radialGradient"; + + // setup default values + this._color1 = [1,0,0,1]; this._colorStop1 = 0.0; + this._color2 = [0,1,0,1]; this._colorStop2 = 0.3; + this._color3 = [0,1,0,1]; this._colorStop3 = 0.6; + this._color4 = [0,1,0,1]; this._colorStop4 = 1.0; + + this.init = function() + { + var material = this._materialNode; + if (material) + { + var technique = material.shaderProgram.default; + var renderer = g_Engine.getContext().renderer; + if (renderer && technique) + { + if (this._shader && this._shader.default) + { + this._shader.default.u_color1.set( this._color1 ); + this._shader.default.u_color2.set( this._color2 ); + this._shader.default.u_color3.set( this._color3 ); + this._shader.default.u_color4.set( this._color4 ); + + this._shader.default.u_colorStop1.set( [this._colorStop1] ); + this._shader.default.u_colorStop2.set( [this._colorStop2] ); + this._shader.default.u_colorStop3.set( [this._colorStop3] ); + this._shader.default.u_colorStop4.set( [this._colorStop4] ); + + if (this._angle !== undefined) + this._shader.default.u_cos_sin_angle.set([Math.cos(this._angle), Math.sin(this._angle)]); + } + } + } + } + + this.import = function( importStr ) + { + var colorStr; + colorStr = this.getPropertyFromString( "color1: ", importStr ); + this._color1 = eval( "[" + colorStr + "]" ); + colorStr = this.getPropertyFromString( "color2: ", importStr ); + this._color2 = eval( "[" + colorStr + "]" ); + colorStr = this.getPropertyFromString( "color3: ", importStr ); + this._color3 = eval( "[" + colorStr + "]" ); + colorStr = this.getPropertyFromString( "color4: ", importStr ); + this._color4 = eval( "[" + colorStr + "]" ); + + this._colorStop1 = Number( this.getPropertyFromString( "colorStop1: ", importStr ) ); + this._colorStop2 = Number( this.getPropertyFromString( "colorStop2: ", importStr ) ); + this._colorStop3 = Number( this.getPropertyFromString( "colorStop3: ", importStr ) ); + this._colorStop4 = Number( this.getPropertyFromString( "colorStop4: ", importStr ) ); + + if (this._angle !== undefined) + this._angle = this.getPropertyFromString( "angle: ", importStr ); + } + +} + +function RuntimeLinearGradientMaterial() +{ + // inherit the members of RuntimeMaterial + this.inheritedFrom = RuntimeRadialGradientMaterial; + this.inheritedFrom(); + + this._name = "LinearGradientMaterial"; + this._shaderName = "linearGradient"; + + // the only difference between linear & radial gradient is the existance of an angle for linear. + this._angle = 0.0; +} + +function RuntimeBumpMetalMaterial() +{ + // inherit the members of RuntimeMaterial + this.inheritedFrom = RuntimeMaterial; + this.inheritedFrom(); + + this._name = "BumpMetalMaterial"; + this._shaderName = "bumpMetal"; + + this._lightDiff = [0.3, 0.3, 0.3, 1.0]; + this._diffuseTexture = "assets/images/metal.png"; + this._specularTexture = "assets/images/silver.png"; + this._normalTexture = "assets/images/normalMap.png"; + + this.import = function( importStr ) + { + this._lightDiff = eval( "[" + this.getPropertyFromString( "lightDiff: ", importStr ) + "]" ); + this._diffuseTexture = this.getPropertyFromString( "diffuseTexture: ", importStr ); + this._specularTexture = this.getPropertyFromString( "specularTexture: ", importStr ); + this._normalTexture = this.getPropertyFromString( "normalMap: ", importStr ); + } + + this.init = function() + { + var material = this._materialNode; + if (material) + { + var technique = material.shaderProgram.default; + var renderer = g_Engine.getContext().renderer; + if (renderer && technique) + { + if (this._shader && this._shader.default) + { + technique.u_light0Diff.set( this._lightDiff ); + + var tex; + var wrap = 'REPEAT', mips = true; + if (this._diffuseTexture) + { + tex = renderer.getTextureByName(this._diffuseTexture, wrap, mips ); + if (tex) technique.u_colMap.set( tex ); + + } + if (this._normalTexture) + { + tex = renderer.getTextureByName(this._normalTexture, wrap, mips ); + if (tex) technique.u_normalMap.set( tex ); + } + if (this._specularTexture) + { + tex = renderer.getTextureByName(this._specularTexture, wrap, mips ); + technique.u_glowMap.set( tex ); + } + } + } + } + } +} + +function RuntimeUberMaterial() +{ +} + + +if (typeof exports === "object") +{ + exports.RuntimeMaterial = RuntimeMaterial; + exports.RuntimeFlatMaterial = RuntimeFlatMaterial; + exports.RuntimePulseMaterial = RuntimePulseMaterial; + exports.RuntimeRadialGradientMaterial = RuntimeRadialGradientMaterial; + exports.RuntimeLinearGradientMaterial = RuntimeLinearGradientMaterial; + exports.RuntimeBumpMetalMaterial = RuntimeBumpMetalMaterial; + exports.RuntimeUberMaterial = RuntimeUberMaterial; +} -- cgit v1.2.3