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/texture.js | 149 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 js/lib/rdge/texture.js (limited to 'js/lib/rdge/texture.js') diff --git a/js/lib/rdge/texture.js b/js/lib/rdge/texture.js new file mode 100644 index 00000000..f9b3d4c3 --- /dev/null +++ b/js/lib/rdge/texture.js @@ -0,0 +1,149 @@ +/* +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(+) (limited to 'js/lib/rdge/texture.js') 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 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/texture.js | 134 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 107 insertions(+), 27 deletions(-) (limited to 'js/lib/rdge/texture.js') diff --git a/js/lib/rdge/texture.js b/js/lib/rdge/texture.js index b349ee62..41c9a54a 100644 --- a/js/lib/rdge/texture.js +++ b/js/lib/rdge/texture.js @@ -10,7 +10,7 @@ var Material = require("js/lib/rdge/materials/material").Material; // Class GLTexture // GL representation of a texture. /////////////////////////////////////////////////////////////////////// -function Texture( dstWorld ) +function Texture( dstWorld, texMapName, wrap, mips ) { /////////////////////////////////////////////////////////////////////// // Instance variables @@ -18,12 +18,24 @@ function Texture( dstWorld ) 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._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; /////////////////////////////////////////////////////////////////////// @@ -43,7 +55,35 @@ function Texture( dstWorld ) // Methods /////////////////////////////////////////////////////////////////////// - this.loadFromFile = function( texMapName, wrap, mips ) + 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; + + // 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.loadFromFile = function() { var tex = this._texture; this._srcCanvas = null; @@ -51,9 +91,9 @@ function Texture( dstWorld ) // 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 texMapName = this._texMapName; + var wrap = this._wrap; + var mips = this._mips; var dstWorld = this.getDstWorld(); if (dstWorld) @@ -69,23 +109,15 @@ function Texture( dstWorld ) } var __texCounter = 0; - this.loadFromCanvas = function( srcCanvas, wrap, mips ) + this.loadFromCanvas = function() { - this._srcCanvas = srcCanvas; + var srcCanvas = this._srcCanvas; + var wrap = this._wrap; + var mips = this._mips; this._texMapName = "GLTexture_" + __texCounter; __texCounter++; - // set default values for wrap and mips - if (wrap === undefined) - wrap = "REPEAT"; - if (mips === undefined) - mips = true; - - // we animate only if the source is an animated GLWorld - if (srcCanvas.elementModel && srcCanvas.elementModel.shapeModel && srcCanvas.elementModel.shapeModel.GLWorld) - this._isAnimated = srcCanvas.elementModel.shapeModel.GLWorld._hasAnimatedMaterials; - // create the texture var world = this.getDstWorld(); tex = world.getGLContext().createTexture(); @@ -128,13 +160,11 @@ function Texture( dstWorld ) var width = srcCanvas.width, height = srcCanvas.height; if (!this.isPowerOfTwo(width) || !this.isPowerOfTwo(height)) { - width = this.nextHighestPowerOfTwo( width ); - height = this.nextHighestPowerOfTwo( height ); - //width = 64; height = 64; + width = this.nextLowerPowerOfTwo( width ); + height = this.nextLowerPowerOfTwo( height ); } // create a canvas to be used as the image for the texture map - var doc = srcCanvas.ownerDocument; var renderCanvas = this._renderCanvas; if (!renderCanvas) { @@ -197,6 +227,56 @@ function Texture( dstWorld ) } 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; + + if (elt.children) + { + var nKids = elt.children.length; + 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 From b6a8f72f670a8edee35554a4ca3a0618c526d651 Mon Sep 17 00:00:00 2001 From: hwc487 Date: Thu, 5 Apr 2012 05:30:55 -0700 Subject: Re-added taper and twist-vert materials. --- js/lib/rdge/texture.js | 45 ++------------------------------------------- 1 file changed, 2 insertions(+), 43 deletions(-) (limited to 'js/lib/rdge/texture.js') diff --git a/js/lib/rdge/texture.js b/js/lib/rdge/texture.js index 2e76f2c0..d2f66efa 100644 --- a/js/lib/rdge/texture.js +++ b/js/lib/rdge/texture.js @@ -216,23 +216,7 @@ function Texture( dstWorld, texMapName, wrap, mips ) 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); - } - } - */ + // copy the source canvas to the context to be used in the texture renderCtx.drawImage(srcCanvas, 0, 0); ///////////////// @@ -240,7 +224,7 @@ function Texture( dstWorld, texMapName, wrap, mips ) renderer.commitTexture( tex ); return tex; -} + } this.isPowerOfTwo = function(x) { @@ -278,31 +262,6 @@ function Texture( dstWorld, texMapName, wrap, mips ) } } - /* - 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