diff options
-rwxr-xr-x | js/lib/rdge/materials/bump-metal-material.js | 40 | ||||
-rw-r--r-- | js/lib/rdge/texture.js | 149 |
2 files changed, 188 insertions, 1 deletions
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 | |||
6 | 6 | ||
7 | var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser; | 7 | var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser; |
8 | var Material = require("js/lib/rdge/materials/material").Material; | 8 | var Material = require("js/lib/rdge/materials/material").Material; |
9 | var Texture = require("js/lib/rdge/texture").Texture; | ||
10 | |||
9 | /////////////////////////////////////////////////////////////////////// | 11 | /////////////////////////////////////////////////////////////////////// |
10 | // Class GLMaterial | 12 | // Class GLMaterial |
11 | // RDGE representation of a material. | 13 | // RDGE representation of a material. |
@@ -18,7 +20,9 @@ var BumpMetalMaterial = function BumpMetalMaterial() { | |||
18 | this._shaderName = "bumpMetal"; | 20 | this._shaderName = "bumpMetal"; |
19 | 21 | ||
20 | this._lightDiff = [0.3, 0.3, 0.3, 1.0]; | 22 | this._lightDiff = [0.3, 0.3, 0.3, 1.0]; |
21 | this._diffuseTexture = "assets/images/metal.png"; | 23 | //this._diffuseTexture = "assets/images/metal.png"; |
24 | this._diffuseTexture = "texture"; | ||
25 | this._diffuseWorld = null; // the world that the texture is derived from (if there is one). | ||
22 | this._specularTexture = "assets/images/silver.png"; | 26 | this._specularTexture = "assets/images/silver.png"; |
23 | this._normalTexture = "assets/images/normalMap.png"; | 27 | this._normalTexture = "assets/images/normalMap.png"; |
24 | 28 | ||
@@ -119,12 +123,46 @@ var BumpMetalMaterial = function BumpMetalMaterial() { | |||
119 | this._materialNode = createMaterialNode( this.getShaderName() + "_" + world.generateUniqueNodeID() ); | 123 | this._materialNode = createMaterialNode( this.getShaderName() + "_" + world.generateUniqueNodeID() ); |
120 | this._materialNode.setShader(this._shader); | 124 | this._materialNode.setShader(this._shader); |
121 | 125 | ||
126 | // DEBUG CODE | ||
127 | this.initWorldTextures(); | ||
128 | |||
122 | // set some image maps | 129 | // set some image maps |
123 | this.updateTexture(1); | 130 | this.updateTexture(1); |
124 | this.updateTexture(2); | 131 | this.updateTexture(2); |
125 | this.updateTexture(3); | 132 | this.updateTexture(3); |
126 | }; | 133 | }; |
127 | 134 | ||
135 | this.initWorldTextures = function() | ||
136 | { | ||
137 | // find the world with the given id | ||
138 | var viewUtils = require("js/helper-classes/3D/view-utils").ViewUtils; | ||
139 | var root = viewUtils.application.ninja.currentDocument.documentRoot; | ||
140 | this._diffuseWorld = this.findWorld( this._diffuseTexture, root ); | ||
141 | } | ||
142 | |||
143 | this.findWorld = function( id, elt ) | ||
144 | { | ||
145 | if (elt.id && elt.id === id) | ||
146 | { | ||
147 | if (elt.eltModel && elt.elementModel.shapeModel && elt.elementModel.shapeModel.GLWorld) | ||
148 | { | ||
149 | var world = elt.elementModel.shapeModel.GLWorld; | ||
150 | return world; | ||
151 | } | ||
152 | } | ||
153 | |||
154 | if (elt.children) | ||
155 | { | ||
156 | var nKids = elt.children.length; | ||
157 | for (var i=0; i<nKids; i++) | ||
158 | { | ||
159 | var child = elt.children[i]; | ||
160 | var world = this.findWorld( id, child ); | ||
161 | if (world) return world; | ||
162 | } | ||
163 | } | ||
164 | } | ||
165 | |||
128 | this.updateTexture = function( index ) | 166 | this.updateTexture = function( index ) |
129 | { | 167 | { |
130 | var material = this._materialNode; | 168 | var material = this._materialNode; |
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 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | var Material = require("js/lib/rdge/materials/material").Material; | ||
8 | |||
9 | /////////////////////////////////////////////////////////////////////// | ||
10 | // Class GLTexture | ||
11 | // GL representation of a texture. | ||
12 | /////////////////////////////////////////////////////////////////////// | ||
13 | function Texture( dstWorld ) | ||
14 | { | ||
15 | /////////////////////////////////////////////////////////////////////// | ||
16 | // Instance variables | ||
17 | /////////////////////////////////////////////////////////////////////// | ||
18 | this._texture; | ||
19 | |||
20 | // texture attributes | ||
21 | this._texMapName; | ||
22 | this._wrap; | ||
23 | this._mips; | ||
24 | |||
25 | this._srcCanvas; // the canvas generating the texture map. | ||
26 | this._dstWorld; // the world that will use the texture map | ||
27 | this._dstWorld = dstWorld; | ||
28 | |||
29 | /////////////////////////////////////////////////////////////////////// | ||
30 | // Property Accessors | ||
31 | /////////////////////////////////////////////////////////////////////// | ||
32 | this.getTexture = function() { return this._texture; } | ||
33 | |||
34 | this.setSrcWorld = function(w) { this._srcWorld = w; } | ||
35 | this.getSrcWorld = function() { return this._srcWorld; } | ||
36 | |||
37 | this.setDstWorld = function(w) { this._dstWorld = w; } | ||
38 | this.getDstWorld = function() { return this._dstWorld; } | ||
39 | |||
40 | /////////////////////////////////////////////////////////////////////// | ||
41 | // Methods | ||
42 | /////////////////////////////////////////////////////////////////////// | ||
43 | |||
44 | this.loadFromFile = function( texMapName, wrap, mips ) | ||
45 | { | ||
46 | var tex = this._texture; | ||
47 | this._srcCanvas = null; | ||
48 | |||
49 | // only load if something has changed | ||
50 | if (this._texMapName !== texMapName) // does RDGE allow us to change wrap or mips? | ||
51 | { | ||
52 | this._texMapName = texMapName.slice(); | ||
53 | this._wrap = wrap; | ||
54 | this._mips = mips; | ||
55 | |||
56 | var dstWorld = this.getDstWorld(); | ||
57 | if (dstWorld) | ||
58 | { | ||
59 | var renderer = dstWorld.getRenderer(); | ||
60 | tex = renderer.getTextureByName(texMapName, wrap, mips ); | ||
61 | this._texture = tex; | ||
62 | dstWorld.textureToLoad( tex ); | ||
63 | } | ||
64 | } | ||
65 | |||
66 | return tex; | ||
67 | } | ||
68 | |||
69 | this.loadFromCanvas = function( srcCanvas, wrap, mips ) | ||
70 | { | ||
71 | this._texMapName = "GLTexture_" + this.texCounter; | ||
72 | this.texCounter++; | ||
73 | |||
74 | //if (elt.elementModel && elt.elementModel.shapeModel && elt.elementModel.shapeModel.GLWorld) | ||
75 | var world = this.getDstWorld(); | ||
76 | var renderer = world.getRenderer(); | ||
77 | |||
78 | var imageData; | ||
79 | var width = srcCanvas.width, height = srcCanvas.height; | ||
80 | width = 128; height = 64; // some even power of 2 for now... | ||
81 | |||
82 | // create a canvas to be used as the image for the texture map | ||
83 | var doc = srcCanvas.ownerDocument; | ||
84 | var dstCanvas = doc.createElement("canvas"); | ||
85 | dstCanvas.width = width; | ||
86 | dstCanvas.height = height; | ||
87 | var dstCtx = dstCanvas.getContext("2d"); | ||
88 | |||
89 | var tex; | ||
90 | var srcCtx = srcCanvas.getContext("2d"); | ||
91 | if (srcCtx) | ||
92 | { | ||
93 | tex = renderer.getTextureByName(this._texMapName, wrap, mips ); | ||
94 | imageData = srcCtx.getImageData( 0, 0, width, height ); | ||
95 | dstCtx.putImageData( imageData, 0, 0 ); | ||
96 | } | ||
97 | else | ||
98 | { | ||
99 | tex = renderer.getTextureByName(this._texMapName, wrap, mips ); | ||
100 | //tex = world.getGLContext().createTexture(); | ||
101 | tex.image = new Image; | ||
102 | tex.wrap = wrap; | ||
103 | tex.mips = mips; | ||
104 | |||
105 | srcCtx = srcCanvas.getContext("experimental-webgl"); | ||
106 | if (srcCtx) | ||
107 | { | ||
108 | // var data = new Uint8Array(width * height * 4); | ||
109 | // srcCtx.readPixels(0, 0, width, height, srcCtx.RGBA, srcCtx.UNSIGNED_BYTE, data); | ||
110 | // console.log( "pixel 0: " + data[width+0] + ", " + data[width+1] + ", " + data[width+2] + ", " + data[width+3] ); | ||
111 | // | ||
112 | // //imageData.data = data; | ||
113 | // imageData = dstCtx.createImageData(width, height); | ||
114 | // var nBytes = width*height*4; | ||
115 | // for (var i=0; i<nBytes; i++) | ||
116 | // imageData.data[i] = data[i]; | ||
117 | // dstCtx.putImageData( imageData, 0, 0 ); | ||
118 | |||
119 | dstCtx.drawImage(srcCanvas, 0, 0); | ||
120 | } | ||
121 | } | ||
122 | |||
123 | |||
124 | ///////////////// | ||
125 | tex.image = dstCanvas; | ||
126 | |||
127 | this._texture = tex; | ||
128 | return tex; | ||
129 | } | ||
130 | |||
131 | this.findPreviousWorld = function() | ||
132 | { | ||
133 | var prevWorld; | ||
134 | for ( var w in _worldStack ) | ||
135 | { | ||
136 | world = _worldStack[w]; | ||
137 | if (world == this.getWorld()) return prevWorld; | ||
138 | prevWorld = world; | ||
139 | } | ||
140 | } | ||
141 | |||
142 | var texCounter = 0; | ||
143 | } | ||
144 | |||
145 | if (typeof exports === "object") { | ||
146 | exports.Texture = Texture; | ||
147 | } | ||
148 | |||
149 | |||