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 (limited to 'js/lib') 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