From 0f31002ca696c1ef303d2926a504afd27305e94f Mon Sep 17 00:00:00 2001 From: hwc487 Date: Wed, 4 Apr 2012 05:52:54 -0700 Subject: Added Flag material --- js/lib/rdge/materials/flag-material.js | 6 + js/lib/rdge/materials/material.js | 5 + js/lib/rdge/materials/twist-vert-material.js | 1 + js/lib/rdge/texture.js | 527 +++++++++++++-------------- 4 files changed, 273 insertions(+), 266 deletions(-) (limited to 'js/lib/rdge') diff --git a/js/lib/rdge/materials/flag-material.js b/js/lib/rdge/materials/flag-material.js index 77991a8c..8d4d1ee3 100644 --- a/js/lib/rdge/materials/flag-material.js +++ b/js/lib/rdge/materials/flag-material.js @@ -23,6 +23,8 @@ var FlagMaterial = function FlagMaterial() { this._defaultWaveWidth = 1.0; this._defaultWaveHeight = 1.0; + this._hasVertexDeformation = true; + /////////////////////////////////////////////////////////////////////// // Properties /////////////////////////////////////////////////////////////////////// @@ -37,6 +39,10 @@ var FlagMaterial = function FlagMaterial() { this._propValues[ this._propNames[1] ] = this._defaultWaveWidth; this._propValues[ this._propNames[2] ] = this._defaultWaveHeight; + // a material can be animated or not. default is not. + // Any material needing continuous rendering should override this method + this.isAnimated = function() { return true; }; + /////////////////////////////////////////////////////////////////////// // Methods /////////////////////////////////////////////////////////////////////// diff --git a/js/lib/rdge/materials/material.js b/js/lib/rdge/materials/material.js index 13251ce8..a81ca18f 100755 --- a/js/lib/rdge/materials/material.js +++ b/js/lib/rdge/materials/material.js @@ -38,6 +38,11 @@ var Material = function GLMaterial( world ) { this._shader = null; this._materialNode = null; + // vertex deformation variables + this._hasVertexDeformation = false; + this._vertexDeformationRange = [0, 0, 1, 1]; // (xMin, yMin, xMax, yMax) + this._vertexDeformationTolerance = 0.02; + /////////////////////////////////////////////////////////////////////// // Property Accessors /////////////////////////////////////////////////////////////////////// diff --git a/js/lib/rdge/materials/twist-vert-material.js b/js/lib/rdge/materials/twist-vert-material.js index 2d2cdcc5..802690a2 100644 --- a/js/lib/rdge/materials/twist-vert-material.js +++ b/js/lib/rdge/materials/twist-vert-material.js @@ -36,6 +36,7 @@ function TwistVertMaterial() this.getShaderName = function() { return this._shaderName; } this.isAnimated = function() { return true; } + this.hasVertexDeformation = function() { return this._hasVertexDeformation; } this._hasVertexDeformation = true; this._vertexDeformationTolerance = 0.02; // should be a property diff --git a/js/lib/rdge/texture.js b/js/lib/rdge/texture.js index 83509f80..2e76f2c0 100644 --- a/js/lib/rdge/texture.js +++ b/js/lib/rdge/texture.js @@ -12,264 +12,259 @@ var Material = require("js/lib/rdge/materials/material").Material; /////////////////////////////////////////////////////////////////////// function Texture( dstWorld, texMapName, wrap, mips ) { - /////////////////////////////////////////////////////////////////////// - // Instance variables - /////////////////////////////////////////////////////////////////////// - this._texture; - - // texture attributes - this._texMapName = texMapName.slice(); - - // set default values for wrap and mips - if (wrap === undefined) - wrap = "REPEAT"; - if (mips === undefined) - mips = true; - this._wrap = wrap; - this._mips = mips; + /////////////////////////////////////////////////////////////////////// + // Instance variables + /////////////////////////////////////////////////////////////////////// + this._texture; + + // texture attributes + this._texMapName = texMapName.slice(); + + // set default values for wrap and mips + if (wrap === undefined) + wrap = "REPEAT"; + if (mips === undefined) + mips = true; + this._wrap = wrap; + this._mips = mips; // the canvas generating the texture map (if there is one) - this._srcCanvas; - this._srcWorld; - - // cache whether or not the source is animated - this._isAnimated = false; - - // the destination 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; } - - this.isAnimated = function() { return this._isAnimated; } - - /////////////////////////////////////////////////////////////////////// - // Methods - /////////////////////////////////////////////////////////////////////// - - this.init = function() - { - // determine if the source is a canvas or an image file - var viewUtils = require("js/helper-classes/3D/view-utils").ViewUtils; - var root = viewUtils.application.ninja.currentDocument.documentRoot; - var srcCanvas = this.findCanvas( this._texMapName, root ); - if (srcCanvas) - { - this._srcCanvas = srcCanvas; - if (srcCanvas.elementModel && srcCanvas.elementModel.shapeModel && srcCanvas.elementModel.shapeModel.GLWorld) - { - this._srcWorld = srcCanvas.elementModel.shapeModel.GLWorld; - - // add a notifier to the world - this._srcWorld.addListener( this, this.worldCallback, { srcWorld: this._srcWorld } ); - - // check if the source is animated - if (srcCanvas.elementModel && srcCanvas.elementModel.shapeModel && srcCanvas.elementModel.shapeModel.GLWorld) - this._isAnimated = this._srcWorld._hasAnimatedMaterials; - } - - this.loadFromCanvas(); - } - else - { - this.loadFromFile(); - } - } - - this.worldCallback = function( type, callbackObj, calleeData, callerData ) - { - console.log( "texture callback, type: " + type ); - if (calleeData.srcWorld) - { - var srcWorld = calleeData.srcWorld; - var notifier = srcWorld._notifier; - var texture = this.callbackObj; - if (texture) - { - switch (type) - { - case notifier.OBJECT_DELETE: - break; - - case notifier.OBJECT_REINSTANTIATE: - break; - - case notifier.OBJECT_CHANGE: - if (!texture.isAnimated()) - texture.rerender(); - texture.getDstWorld().restartRenderLoop(); - break; - - default: - throw new Exception( "unrecognized texture callback type: " + type ); - break; - } - } - } - } - - this.loadFromFile = function() - { - 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? - { - var texMapName = this._texMapName; - var wrap = this._wrap; - var mips = this._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() - { - var srcCanvas = this._srcCanvas; - var wrap = this._wrap; - var mips = this._mips; - - this._texMapName = "GLTexture_" + __texCounter; - __texCounter++; - - // create the texture - var world = this.getDstWorld(); - tex = world.getGLContext().createTexture(); - this._texture = tex; - tex.texparams = new _texparams(wrap, mips); // defined in renderer.js - tex.image = new Image; - - // create the canvas and context to render into - var doc = srcCanvas.ownerDocument; - this._renderCanvas = doc.createElement("canvas"); - - // cache whether this is a 2D canvas or 3D canvas - var srcCtx = srcCanvas.getContext("2d"); - this._is3D = false; - if (!srcCtx) this._is3D = true; - - this.rerender(); - - return tex; - } - - this.rerender = function() - { - if (!this._srcCanvas) - { - console.log( " no source canvas in GLTexture.rerender" ); - return; - } - var srcCanvas = this._srcCanvas; - - var world = this.getDstWorld(); - if (!world) - { - console.log( "no world in GLTexture.rerender" ); - return; - } - var renderer = world.getRenderer(); - - var imageData; - var width = srcCanvas.width, height = srcCanvas.height; - if (!this.isPowerOfTwo(width) || !this.isPowerOfTwo(height)) - { - width = this.nextLowerPowerOfTwo( width ); - height = this.nextLowerPowerOfTwo( height ); - } - - // create a canvas to be used as the image for the texture map - var renderCanvas = this._renderCanvas; - if (!renderCanvas) - { - console.log( "no render canvas in GLTexture.rerender" ); - return; - } - renderCanvas.width = width; - renderCanvas.height = height; - var renderCtx = renderCanvas.getContext("2d"); - - // create the texture - var tex = this._texture; - if (!tex) - { - console.log( "no texture in GLTexture.rerender" ); - return; - } - - var srcCtx; - if (!this._is3D) - { - srcCtx = srcCanvas.getContext("2d"); - imageData = srcCtx.getImageData( 0, 0, width, height ); - renderCtx.putImageData( imageData, 0, 0 ); - } - else - { - srcCtx = srcCanvas.getContext("experimental-webgl"); - if (srcCtx) - { - renderCtx.drawImage(srcCanvas, 0, 0); -// var data = new Uint8Array(width * height * 4); -// srcCtx.readPixels(0, 0, width, height, srcCtx.RGBA, srcCtx.UNSIGNED_BYTE, data); -// imageData = renderCtx.createImageData(width, height); -// var nBytes = width*height*4; -// 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; + } - this.nextLowerPowerOfTwo = function(x) - { - return this.nextHighestPowerOfTwo(x) >> 1; - } - - this.findCanvas = function( id, elt ) - { - if (elt.id && elt.id === id) - return elt; + this.nextLowerPowerOfTwo = function(x) + { + return this.nextHighestPowerOfTwo(x) >> 1; + } + + this.findCanvas = function( id, elt ) + { + if (elt.id && elt.id === id) + return elt; if (elt.children) { @@ -278,22 +273,22 @@ function Texture( dstWorld, texMapName, wrap, mips ) { var child = elt.children[i]; var canvas = this.findCanvas( id, child ); - if (canvas) return canvas; + if (canvas) return canvas; } } } - /* - 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; - } - } + /* + 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) { @@ -302,7 +297,7 @@ function Texture( dstWorld, texMapName, wrap, mips ) { var child = elt.children[i]; var world = this.findWorld( id, child ); - if (world) return world; + if (world) return world; } } } @@ -313,7 +308,7 @@ function Texture( dstWorld, texMapName, wrap, mips ) } if (typeof exports === "object") { - exports.Texture = Texture; + exports.Texture = Texture; } -- cgit v1.2.3