From 00cd98e49c959906d7c44bb6adcdef1f3d5148ae Mon Sep 17 00:00:00 2001 From: hwc487 Date: Thu, 22 Mar 2012 13:52:09 -0700 Subject: Merge branch 'master' of github.com:Motorola-Mobility/ninja-internal into Texture Conflicts: assets/shaders/plasma.frag.glsl js/helper-classes/RDGE/GLLine.js js/helper-classes/RDGE/MaterialsLibrary.js js/lib/drawing/world.js js/lib/geom/circle.js js/lib/geom/rectangle.js js/lib/rdge/materials/flat-material.js js/lib/rdge/materials/material.js js/panels/Materials/Materials.xml js/panels/Materials/materials-popup.reel/materials-popup.js js/preloader/Preloader.js --- js/lib/rdge/materials/bump-metal-material.js | 40 ++++++- js/lib/rdge/texture.js | 149 +++++++++++++++++++++++++++ 2 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 js/lib/rdge/texture.js diff --git a/js/lib/rdge/materials/bump-metal-material.js b/js/lib/rdge/materials/bump-metal-material.js index fa6f5300..fe8a9dd5 100755 --- a/js/lib/rdge/materials/bump-metal-material.js +++ b/js/lib/rdge/materials/bump-metal-material.js @@ -6,6 +6,8 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot 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. @@ -18,7 +20,9 @@ var BumpMetalMaterial = function BumpMetalMaterial() { this._shaderName = "bumpMetal"; this._lightDiff = [0.3, 0.3, 0.3, 1.0]; - this._diffuseTexture = "assets/images/metal.png"; + //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._specularTexture = "assets/images/silver.png"; this._normalTexture = "assets/images/normalMap.png"; @@ -119,12 +123,46 @@ var BumpMetalMaterial = function BumpMetalMaterial() { 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 ); + } + + this.findWorld = function( id, elt ) + { + if (elt.id && elt.id === id) + { + if (elt.eltModel && 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 +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 Material = require("js/lib/rdge/materials/material").Material; + +/////////////////////////////////////////////////////////////////////// +// Class GLTexture +// GL representation of a texture. +/////////////////////////////////////////////////////////////////////// +function Texture( dstWorld ) +{ + /////////////////////////////////////////////////////////////////////// + // Instance variables + /////////////////////////////////////////////////////////////////////// + this._texture; + + // texture attributes + this._texMapName; + this._wrap; + this._mips; + + this._srcCanvas; // the canvas generating the texture map. + this._dstWorld; // the world that will use the texture map + this._dstWorld = dstWorld; + + /////////////////////////////////////////////////////////////////////// + // Property Accessors + /////////////////////////////////////////////////////////////////////// + this.getTexture = function() { return this._texture; } + + this.setSrcWorld = function(w) { this._srcWorld = w; } + this.getSrcWorld = function() { return this._srcWorld; } + + this.setDstWorld = function(w) { this._dstWorld = w; } + this.getDstWorld = function() { return this._dstWorld; } + + /////////////////////////////////////////////////////////////////////// + // Methods + /////////////////////////////////////////////////////////////////////// + + this.loadFromFile = function( texMapName, wrap, mips ) + { + var tex = this._texture; + this._srcCanvas = null; + + // only load if something has changed + if (this._texMapName !== texMapName) // does RDGE allow us to change wrap or mips? + { + this._texMapName = texMapName.slice(); + this._wrap = wrap; + this._mips = mips; + + var dstWorld = this.getDstWorld(); + if (dstWorld) + { + var renderer = dstWorld.getRenderer(); + tex = renderer.getTextureByName(texMapName, wrap, mips ); + this._texture = tex; + dstWorld.textureToLoad( tex ); + } + } + + return tex; + } + + this.loadFromCanvas = function( srcCanvas, wrap, mips ) + { + this._texMapName = "GLTexture_" + this.texCounter; + this.texCounter++; + + //if (elt.elementModel && elt.elementModel.shapeModel && elt.elementModel.shapeModel.GLWorld) + var world = this.getDstWorld(); + var renderer = world.getRenderer(); + + var imageData; + var width = srcCanvas.width, height = srcCanvas.height; + width = 128; height = 64; // some even power of 2 for now... + + // create a canvas to be used as the image for the texture map + var doc = srcCanvas.ownerDocument; + var dstCanvas = doc.createElement("canvas"); + dstCanvas.width = width; + dstCanvas.height = height; + var dstCtx = dstCanvas.getContext("2d"); + + var tex; + var srcCtx = srcCanvas.getContext("2d"); + if (srcCtx) + { + tex = renderer.getTextureByName(this._texMapName, wrap, mips ); + imageData = srcCtx.getImageData( 0, 0, width, height ); + dstCtx.putImageData( imageData, 0, 0 ); + } + else + { + tex = renderer.getTextureByName(this._texMapName, wrap, mips ); + //tex = world.getGLContext().createTexture(); + tex.image = new Image; + tex.wrap = wrap; + tex.mips = mips; + + srcCtx = srcCanvas.getContext("experimental-webgl"); + if (srcCtx) + { +// var data = new Uint8Array(width * height * 4); +// srcCtx.readPixels(0, 0, width, height, srcCtx.RGBA, srcCtx.UNSIGNED_BYTE, data); +// console.log( "pixel 0: " + data[width+0] + ", " + data[width+1] + ", " + data[width+2] + ", " + data[width+3] ); +// +// //imageData.data = data; +// imageData = dstCtx.createImageData(width, height); +// var nBytes = width*height*4; +// for (var i=0; i> i; + } + return x + 1; + } } if (typeof exports === "object") { -- cgit v1.2.3 From 62f4327f9b83760e52a1f6bf1e689b1e0a780fbb Mon Sep 17 00:00:00 2001 From: hwc487 Date: Fri, 23 Mar 2012 17:15:28 -0700 Subject: textures --- js/lib/rdge/texture.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/js/lib/rdge/texture.js b/js/lib/rdge/texture.js index 28dc868a..c72864b8 100644 --- a/js/lib/rdge/texture.js +++ b/js/lib/rdge/texture.js @@ -207,6 +207,7 @@ function Texture( dstWorld ) width = this.nextHighestPowerOfTwo( width ); height = this.nextHighestPowerOfTwo( height ); } + width = 64; height = 64; // create a canvas to be used as the image for the texture map var doc = srcCanvas.ownerDocument; @@ -246,6 +247,10 @@ function Texture( dstWorld ) var nBytes = width*height*4; for (var i=0; i 1000) nPrint = 1000; + for (var i=0; i */ var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser; +var Texture = require("js/lib/rdge/texture").Texture; var Material = require("js/lib/rdge/materials/material").Material; /////////////////////////////////////////////////////////////////////// // Class GLMaterial // RDGE representation of a material. /////////////////////////////////////////////////////////////////////// -var PulseMaterial = function PulseMaterial() { +var PulseMaterial = function PulseMaterial() +{ + var MaterialLibrary = require("js/models/materials-model").MaterialsModel; + + // initialize the inherited members + this.inheritedFrom = Material; + this.inheritedFrom(); + /////////////////////////////////////////////////////////////////////// // Instance variables /////////////////////////////////////////////////////////////////////// this._name = "PulseMaterial"; this._shaderName = "pulse"; - this._texMap = 'assets/images/cubelight.png'; + //this._texMap = 'assets/images/cubelight.png'; + this._texMap = 'texture'; this._time = 0.0; this._dTime = 0.01; @@ -109,6 +118,16 @@ var PulseMaterial = function PulseMaterial() { this._shader['default'].u_time.set( [this._time] ); } + // check if the texture uses a canvas as the source + var viewUtils = require("js/helper-classes/3D/view-utils").ViewUtils; + var root = viewUtils.application.ninja.currentDocument.documentRoot; + var texMapName = this._propValues[this._propNames[0]]; + var texWorld = this.findWorld( texMapName, root ); + if (texWorld) + { + this._glTex = new Texture( this.getWorld() ); + this._glTex.loadFromCanvas( texWorld.getCanvas() ); + } // set the shader values in the shader this.updateTexture(); @@ -122,9 +141,18 @@ var PulseMaterial = function PulseMaterial() { var technique = material.shaderProgram['default']; var renderer = g_Engine.getContext().renderer; if (renderer && technique) { - var texMapName = this._propValues[this._propNames[0]]; var wrap = 'REPEAT', mips = true; - var tex = this.loadTexture( texMapName, wrap, mips ); + var tex; + if (this._glTex) + { + this._glTex.rerender(); + tex = this._glTex.getTexture(); + } + else + { + var texMapName = this._propValues[this._propNames[0]]; + tex = this.loadTexture( texMapName, wrap, mips ); + } if (tex) { technique.u_tex0.set( tex ); @@ -138,6 +166,9 @@ var PulseMaterial = function PulseMaterial() { var material = this._materialNode; if (material) { + if (this._glTex && this._glTex.isAnimated()) + this.updateTexture(); + var technique = material.shaderProgram['default']; var renderer = g_Engine.getContext().renderer; if (renderer && technique) { @@ -279,7 +310,8 @@ var pulseMaterialDef = } }; -PulseMaterial.prototype = new Material(); +// doing the inheritance here introtudes bugs. Local instance variables are overwritten in the base class +//PulseMaterial.prototype = new Material(); if (typeof exports === "object") { exports.PulseMaterial = PulseMaterial; diff --git a/js/lib/rdge/texture.js b/js/lib/rdge/texture.js index c72864b8..b349ee62 100644 --- a/js/lib/rdge/texture.js +++ b/js/lib/rdge/texture.js @@ -15,267 +15,188 @@ function Texture( dstWorld ) /////////////////////////////////////////////////////////////////////// // Instance variables /////////////////////////////////////////////////////////////////////// - this._texture; + this._texture; - // texture attributes - this._texMapName; - this._wrap; - this._mips; + // texture attributes + this._texMapName; + this._wrap; + this._mips; - this._srcCanvas; // the canvas generating the texture map. - this._dstWorld; // the world that will use the texture map - this._dstWorld = dstWorld; + this._srcCanvas; // the canvas generating the texture map. + this._dstWorld; // the world that will use the texture map + this._dstWorld = dstWorld; /////////////////////////////////////////////////////////////////////// // Property Accessors /////////////////////////////////////////////////////////////////////// - this.getTexture = function() { return this._texture; } + this.getTexture = function() { return this._texture; } - this.setSrcWorld = function(w) { this._srcWorld = w; } - this.getSrcWorld = function() { return this._srcWorld; } + this.setSrcWorld = function(w) { this._srcWorld = w; } + this.getSrcWorld = function() { return this._srcWorld; } - this.setDstWorld = function(w) { this._dstWorld = w; } - this.getDstWorld = function() { return this._dstWorld; } + this.setDstWorld = function(w) { this._dstWorld = w; } + this.getDstWorld = function() { return this._dstWorld; } + + this.isAnimated = function() { return this._isAnimated; } /////////////////////////////////////////////////////////////////////// // Methods /////////////////////////////////////////////////////////////////////// - this.loadFromFile = function( texMapName, wrap, mips ) - { - var tex = this._texture; - this._srcCanvas = null; - - // only load if something has changed - if (this._texMapName !== texMapName) // does RDGE allow us to change wrap or mips? - { - this._texMapName = texMapName.slice(); - this._wrap = wrap; - this._mips = mips; - - var dstWorld = this.getDstWorld(); - if (dstWorld) - { - var renderer = dstWorld.getRenderer(); - tex = renderer.getTextureByName(texMapName, wrap, mips ); - this._texture = tex; - dstWorld.textureToLoad( tex ); - } - } - - return tex; - } + this.loadFromFile = function( texMapName, wrap, mips ) + { + var tex = this._texture; + this._srcCanvas = null; + + // only load if something has changed + if (this._texMapName !== texMapName) // does RDGE allow us to change wrap or mips? + { + this._texMapName = texMapName.slice(); + this._wrap = wrap; + this._mips = mips; + + var dstWorld = this.getDstWorld(); + if (dstWorld) + { + var renderer = dstWorld.getRenderer(); + tex = renderer.getTextureByName(texMapName, wrap, mips ); + this._texture = tex; + dstWorld.textureToLoad( tex ); + } + } + + return tex; + } var __texCounter = 0; + this.loadFromCanvas = function( srcCanvas, wrap, mips ) + { + this._srcCanvas = srcCanvas; - /* - this.loadFromCanvas = function( srcCanvas, wrap, mips ) - { - this._texMapName = "GLTexture_" + __texCounter; - __texCounter++; - - //if (elt.elementModel && elt.elementModel.shapeModel && elt.elementModel.shapeModel.GLWorld) - var world = this.getDstWorld(); - var renderer = world.getRenderer(); + this._texMapName = "GLTexture_" + __texCounter; + __texCounter++; - // set default values for wrap and mips + // set default values for wrap and mips if (wrap === undefined) wrap = "REPEAT"; if (mips === undefined) mips = true; - var imageData; - var width = srcCanvas.width, height = srcCanvas.height; - width = 64; height = 64; // some even power of 2 for now... - - // create a canvas to be used as the image for the texture map - var doc = srcCanvas.ownerDocument; - var dstCanvas = doc.createElement("canvas"); - dstCanvas.width = width; - dstCanvas.height = height; - var dstCtx = dstCanvas.getContext("2d"); - - var tex; - var srcCtx = srcCanvas.getContext("2d"); - if (srcCtx) - { - tex = renderer.getTextureByName(this._texMapName, wrap, mips ); - imageData = srcCtx.getImageData( 0, 0, width, height ); - dstCtx.putImageData( imageData, 0, 0 ); - } - else - { - tex = renderer.getTextureByName(this._texMapName, wrap, mips ); - //tex = world.getGLContext().createTexture(); - tex.image = new Image; - tex.wrap = wrap; - tex.mips = mips; - - srcCtx = srcCanvas.getContext("experimental-webgl"); - if (srcCtx) - { -// var data = new Uint8Array(width * height * 4); -// srcCtx.readPixels(0, 0, width, height, srcCtx.RGBA, srcCtx.UNSIGNED_BYTE, data); -// console.log( "pixel 0: " + data[width+0] + ", " + data[width+1] + ", " + data[width+2] + ", " + data[width+3] ); -// -// //imageData.data = data; -// imageData = dstCtx.createImageData(width, height); -// var nBytes = width*height*4; -// for (var i=0; i 1000) nPrint = 1000; - for (var i=0; i> i; - } - return x + 1; - } + this.nextHighestPowerOfTwo = function(x) + { + --x; + for (var i = 1; i < 32; i <<= 1) { + x = x | x >> i; + } + return x + 1; + } } if (typeof exports === "object") { -- cgit v1.2.3 From d7269673dc1f5caf6df3765c6b669d3d1a93bdb1 Mon Sep 17 00:00:00 2001 From: hwc487 Date: Tue, 27 Mar 2012 04:53:27 -0700 Subject: Integrated texture wrapper into pulse and bump-metal materials. --- js/lib/rdge/materials/bump-metal-material.js | 89 +++++++++--------- js/lib/rdge/materials/material.js | 95 ------------------- js/lib/rdge/materials/pulse-material.js | 38 ++++---- js/lib/rdge/texture.js | 134 +++++++++++++++++++++------ 4 files changed, 171 insertions(+), 185 deletions(-) diff --git a/js/lib/rdge/materials/bump-metal-material.js b/js/lib/rdge/materials/bump-metal-material.js index a3ef9d37..0570e8ed 100755 --- a/js/lib/rdge/materials/bump-metal-material.js +++ b/js/lib/rdge/materials/bump-metal-material.js @@ -21,14 +21,14 @@ var BumpMetalMaterial = function BumpMetalMaterial() { 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._diffuseTexture = "assets/images/metal.png"; + //this._diffuseTexture = "texture"; this._specularTexture = "assets/images/silver.png"; this._normalTexture = "assets/images/normalMap.png"; + // keep the array of initialized textures + this._textures = []; + /////////////////////////////////////////////////////////////////////// // Property Accessors /////////////////////////////////////////////////////////////////////// @@ -55,13 +55,13 @@ var BumpMetalMaterial = function BumpMetalMaterial() { }; 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.setDiffuseTexture = function(m) { this._propValues[this._propNames[1]] = m ? m.slice(0) : null; this.initTexture(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.setNormalTexture = function(m) { this._propValues[this._propNames[2]] = m ? m.slice(0) : null; this.initTexture(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.setSpecularTexture = function(m) { this._propValues[this._propNames[3]] = m ? m.slice(0) : null; this.initTexture(3); }; this.isAnimated = function() { return true; }; @@ -127,29 +127,34 @@ var BumpMetalMaterial = function BumpMetalMaterial() { this._materialNode.setShader(this._shader); // DEBUG CODE - this.initWorldTextures(); - - // set some image maps - this.updateTexture(1); - this.updateTexture(2); - this.updateTexture(3); + this.initTextures(); }; - this.initWorldTextures = function() + this.initTexture = function( index ) { - // 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 dstWorld = this.getWorld(); + if (dstWorld) { - var world = this.getWorld(); - var tex = new Texture( world ); - this._diffuseTextureObj = tex; - tex.loadFromCanvas( this._diffuseWorld.getCanvas() ); + var texMapName = this._propValues[this._propNames[index]]; + var texture = new Texture( dstWorld, texMapName ); + this._textures[index] = texture; + this.updateTexture( index ); } } + this.initTextures = function() + { + var dstWorld = this.getWorld(); + if (dstWorld) + { + // find the world with the given id + for (var i=1; i<=3; i++) + { + this.initTexture( i ); + } + } + } + this.updateTexture = function( index ) { var material = this._materialNode; @@ -159,24 +164,24 @@ var BumpMetalMaterial = function BumpMetalMaterial() { var renderer = g_Engine.getContext().renderer; if (renderer && technique) { - var texMapName = this._propValues[this._propNames[index]]; - var wrap = 'REPEAT', mips = true; - var tex; - if ((index === 1) && this._diffuseTextureObj) - tex = this._diffuseTextureObj.getTexture(); - else - tex = this.loadTexture( texMapName, wrap, mips ); - - if (tex) - { - switch (index) - { - case 1: technique.u_colMap.set( tex ); break; - case 2: technique.u_normalMap.set( tex ); break; - case 3: technique.u_glowMap.set( tex ); break; - default: console.log( "invalid map index in BumpMetalMaterial, " + index ); - } - } + var glTex = this._textures[ index ]; + if (glTex) + { + if (glTex.isAnimated()) + glTex.rerender(); + + var tex = glTex.getTexture(); + if (tex) + { + switch (index) + { + case 1: technique.u_colMap.set( tex ); break; + case 2: technique.u_normalMap.set( tex ); break; + case 3: technique.u_glowMap.set( tex ); break; + default: console.log( "invalid map index in BumpMetalMaterial, " + index ); + } + } + } } } }; diff --git a/js/lib/rdge/materials/material.js b/js/lib/rdge/materials/material.js index 157fa7db..13251ce8 100755 --- a/js/lib/rdge/materials/material.js +++ b/js/lib/rdge/materials/material.js @@ -252,29 +252,6 @@ var Material = function GLMaterial( world ) { return tex; }; - 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> 1; + } + + this.findCanvas = function( id, elt ) + { + if (elt.id && elt.id === id) + return elt; + + if (elt.children) + { + var nKids = elt.children.length; + for (var i=0; i */ -var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser; var Texture = require("js/lib/rdge/texture").Texture; var Material = require("js/lib/rdge/materials/material").Material; /////////////////////////////////////////////////////////////////////// @@ -220,7 +219,7 @@ var PulseMaterial = function PulseMaterial() } } - + /* this.export = function() { // every material needs the base type and instance name var exportStr = "material: " + this.getShaderName() + "\n"; @@ -261,6 +260,7 @@ var PulseMaterial = function PulseMaterial() return rtnStr; } + */ }; /////////////////////////////////////////////////////////////////////////////////////// diff --git a/js/lib/rdge/materials/relief-tunnel-material.js b/js/lib/rdge/materials/relief-tunnel-material.js index 52c40543..24fc885f 100644 --- a/js/lib/rdge/materials/relief-tunnel-material.js +++ b/js/lib/rdge/materials/relief-tunnel-material.js @@ -6,8 +6,10 @@ var PulseMaterial = require("js/lib/rdge/materials/pulse-material").PulseMaterial; +var Texture = require("js/lib/rdge/texture").Texture; -var ReliefTunnelMaterial = function ReliefTunnelMaterial() { +var ReliefTunnelMaterial = function ReliefTunnelMaterial() +{ /////////////////////////////////////////////////////////////////////// // Instance variables /////////////////////////////////////////////////////////////////////// @@ -62,6 +64,9 @@ var ReliefTunnelMaterial = function ReliefTunnelMaterial() { this._shader['default'].u_time.set( [this._time] ); } + var texMapName = this._propValues[this._propNames[0]]; + this._glTex = new Texture( world, texMapName ); + // set the shader values in the shader this.updateTexture(); this.setResolution( [world.getViewportWidth(),world.getViewportHeight()] ); -- cgit v1.2.3 From 0a0aa4662ffa687dd049cc5a24e701cedf87253a Mon Sep 17 00:00:00 2001 From: hwc487 Date: Wed, 28 Mar 2012 05:44:38 -0700 Subject: added a notification procedure to worlds. --- js/lib/drawing/world.js | 71 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 2 deletions(-) diff --git a/js/lib/drawing/world.js b/js/lib/drawing/world.js index ebeeeb20..f077a5e2 100755 --- a/js/lib/drawing/world.js +++ b/js/lib/drawing/world.js @@ -78,6 +78,9 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) { // keep a counter for generating node names this._nodeCounter = 0; + // for sending notifications to listeners + this._notifier = new Notifier(); + /////////////////////////////////////////////////////////////////////// // Property accessors /////////////////////////////////////////////////////////////////////// @@ -435,6 +438,9 @@ World.prototype.updateObject = function (obj) { childTrNode.attachMeshNode(this.renderer.id + "_prim_" + this._nodeCounter++, prim); childTrNode.attachMaterial(materialNodes[i]); } + + // send a notification that the tree has changed + this._notifier.sendNotification( this._notifier.OBJECT_CHANGE ); }; World.prototype.addObject = function( obj ) { @@ -463,6 +469,9 @@ World.prototype.addObject = function( obj ) { obj.buildBuffers(); this.restartRenderLoop(); } + + // send a notification that the tree has changed + this._notifier.sendNotification( this._notifier.OBJECT_CHANGE ); } catch(e) { @@ -520,13 +529,21 @@ World.prototype.addIfNewObject = function (obj) { obj.buildBuffers(); this.restartRenderLoop(); } + + // send a notification that the tree has changed + this._notifier.sendNotification( this._notifier.OBJECT_CHANGE ); + } catch (e) { alert("Exception in GLWorld.addIfNewObject " + e); } }; -World.prototype.clearTree = function() { - if (this._useWebGL) { +World.prototype.clearTree = function() +{ + this._notifier.sendNotification( this._notifier.OBJECT_DELETE ); + + if (this._useWebGL) + { var root = this._rootNode; root.children = new Array(); g_Engine.unregisterCanvas( this._canvas.rdgeid ) @@ -1085,6 +1102,56 @@ World.prototype.importSubObject = function( objStr, parentNode ) { return trNode; }; +function Notifier() +{ + // notification types supported + this.OBJECT_DELETE = 1; + this.OBJECT_REANIMATE = 2; // the object has come back after a deletion - as in undo + this.OBJECT_CHANGE = 3; + + + // the array of listener objects + this._listenerArray = []; + + this.sendNotification = function( type, callerData ) + { + var n = this._listenerArray.length; + for (var i=0; i