aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge/texture.js
diff options
context:
space:
mode:
authorhwc4872012-03-27 04:53:27 -0700
committerhwc4872012-03-27 04:53:27 -0700
commitd7269673dc1f5caf6df3765c6b669d3d1a93bdb1 (patch)
treef49f92e2a69befbd16d6b39721e7986908c6a14d /js/lib/rdge/texture.js
parent7fbc78522cc5b5e3df471d9f9e53fdde58f83892 (diff)
downloadninja-d7269673dc1f5caf6df3765c6b669d3d1a93bdb1.tar.gz
Integrated texture wrapper into pulse and bump-metal materials.
Diffstat (limited to 'js/lib/rdge/texture.js')
-rw-r--r--js/lib/rdge/texture.js134
1 files changed, 107 insertions, 27 deletions
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;
10// Class GLTexture 10// Class GLTexture
11// GL representation of a texture. 11// GL representation of a texture.
12/////////////////////////////////////////////////////////////////////// 12///////////////////////////////////////////////////////////////////////
13function Texture( dstWorld ) 13function Texture( dstWorld, texMapName, wrap, mips )
14{ 14{
15 /////////////////////////////////////////////////////////////////////// 15 ///////////////////////////////////////////////////////////////////////
16 // Instance variables 16 // Instance variables
@@ -18,12 +18,24 @@ function Texture( dstWorld )
18 this._texture; 18 this._texture;
19 19
20 // texture attributes 20 // texture attributes
21 this._texMapName; 21 this._texMapName = texMapName.slice();
22 this._wrap; 22
23 this._mips; 23 // set default values for wrap and mips
24 24 if (wrap === undefined)
25 this._srcCanvas; // the canvas generating the texture map. 25 wrap = "REPEAT";
26 this._dstWorld; // the world that will use the texture map 26 if (mips === undefined)
27 mips = true;
28 this._wrap = wrap;
29 this._mips = mips;
30
31 // the canvas generating the texture map (if there is one)
32 this._srcCanvas;
33 this._srcWorld;
34
35 // cache whether or not the source is animated
36 this._isAnimated = false;
37
38 // the destination world that will use the texture map
27 this._dstWorld = dstWorld; 39 this._dstWorld = dstWorld;
28 40
29 /////////////////////////////////////////////////////////////////////// 41 ///////////////////////////////////////////////////////////////////////
@@ -43,7 +55,35 @@ function Texture( dstWorld )
43 // Methods 55 // Methods
44 /////////////////////////////////////////////////////////////////////// 56 ///////////////////////////////////////////////////////////////////////
45 57
46 this.loadFromFile = function( texMapName, wrap, mips ) 58 this.init = function()
59 {
60 // determine if the source is a canvas or an image file
61 var viewUtils = require("js/helper-classes/3D/view-utils").ViewUtils;
62 var root = viewUtils.application.ninja.currentDocument.documentRoot;
63 var srcCanvas = this.findCanvas( this._texMapName, root );
64 if (srcCanvas)
65 {
66 this._srcCanvas = srcCanvas;
67 if (srcCanvas.elementModel && srcCanvas.elementModel.shapeModel && srcCanvas.elementModel.shapeModel.GLWorld)
68 {
69 this._srcWorld = srcCanvas.elementModel.shapeModel.GLWorld;
70
71 // check if the source is animated
72 if (srcCanvas.elementModel && srcCanvas.elementModel.shapeModel && srcCanvas.elementModel.shapeModel.GLWorld)
73 this._isAnimated = this._srcWorld._hasAnimatedMaterials;
74 }
75
76 this.loadFromCanvas();
77 }
78 else
79 {
80 this.loadFromFile();
81 }
82
83
84 }
85
86 this.loadFromFile = function()
47 { 87 {
48 var tex = this._texture; 88 var tex = this._texture;
49 this._srcCanvas = null; 89 this._srcCanvas = null;
@@ -51,9 +91,9 @@ function Texture( dstWorld )
51 // only load if something has changed 91 // only load if something has changed
52 if (this._texMapName !== texMapName) // does RDGE allow us to change wrap or mips? 92 if (this._texMapName !== texMapName) // does RDGE allow us to change wrap or mips?
53 { 93 {
54 this._texMapName = texMapName.slice(); 94 var texMapName = this._texMapName;
55 this._wrap = wrap; 95 var wrap = this._wrap;
56 this._mips = mips; 96 var mips = this._mips;
57 97
58 var dstWorld = this.getDstWorld(); 98 var dstWorld = this.getDstWorld();
59 if (dstWorld) 99 if (dstWorld)
@@ -69,23 +109,15 @@ function Texture( dstWorld )
69 } 109 }
70 110
71 var __texCounter = 0; 111 var __texCounter = 0;
72 this.loadFromCanvas = function( srcCanvas, wrap, mips ) 112 this.loadFromCanvas = function()
73 { 113 {
74 this._srcCanvas = srcCanvas; 114 var srcCanvas = this._srcCanvas;
115 var wrap = this._wrap;
116 var mips = this._mips;
75 117
76 this._texMapName = "GLTexture_" + __texCounter; 118 this._texMapName = "GLTexture_" + __texCounter;
77 __texCounter++; 119 __texCounter++;
78 120
79 // set default values for wrap and mips
80 if (wrap === undefined)
81 wrap = "REPEAT";
82 if (mips === undefined)
83 mips = true;
84
85 // we animate only if the source is an animated GLWorld
86 if (srcCanvas.elementModel && srcCanvas.elementModel.shapeModel && srcCanvas.elementModel.shapeModel.GLWorld)
87 this._isAnimated = srcCanvas.elementModel.shapeModel.GLWorld._hasAnimatedMaterials;
88
89 // create the texture 121 // create the texture
90 var world = this.getDstWorld(); 122 var world = this.getDstWorld();
91 tex = world.getGLContext().createTexture(); 123 tex = world.getGLContext().createTexture();
@@ -128,13 +160,11 @@ function Texture( dstWorld )
128 var width = srcCanvas.width, height = srcCanvas.height; 160 var width = srcCanvas.width, height = srcCanvas.height;
129 if (!this.isPowerOfTwo(width) || !this.isPowerOfTwo(height)) 161 if (!this.isPowerOfTwo(width) || !this.isPowerOfTwo(height))
130 { 162 {
131 width = this.nextHighestPowerOfTwo( width ); 163 width = this.nextLowerPowerOfTwo( width );
132 height = this.nextHighestPowerOfTwo( height ); 164 height = this.nextLowerPowerOfTwo( height );
133 //width = 64; height = 64;
134 } 165 }
135 166
136 // create a canvas to be used as the image for the texture map 167 // create a canvas to be used as the image for the texture map
137 var doc = srcCanvas.ownerDocument;
138 var renderCanvas = this._renderCanvas; 168 var renderCanvas = this._renderCanvas;
139 if (!renderCanvas) 169 if (!renderCanvas)
140 { 170 {
@@ -197,6 +227,56 @@ function Texture( dstWorld )
197 } 227 }
198 return x + 1; 228 return x + 1;
199 } 229 }
230
231 this.nextLowerPowerOfTwo = function(x)
232 {
233 return this.nextHighestPowerOfTwo(x) >> 1;
234 }
235
236 this.findCanvas = function( id, elt )
237 {
238 if (elt.id && elt.id === id)
239 return elt;
240
241 if (elt.children)
242 {
243 var nKids = elt.children.length;
244 for (var i=0; i<nKids; i++)
245 {
246 var child = elt.children[i];
247 var canvas = this.findCanvas( id, child );
248 if (canvas) return canvas;
249 }
250 }
251 }
252
253 /*
254 this.findWorld = function( id, elt )
255 {
256 if (elt.id && elt.id === id)
257 {
258 if (elt.elementModel && elt.elementModel.shapeModel && elt.elementModel.shapeModel.GLWorld)
259 {
260 var world = elt.elementModel.shapeModel.GLWorld;
261 return world;
262 }
263 }
264
265 if (elt.children)
266 {
267 var nKids = elt.children.length;
268 for (var i=0; i<nKids; i++)
269 {
270 var child = elt.children[i];
271 var world = this.findWorld( id, child );
272 if (world) return world;
273 }
274 }
275 }
276 */
277
278 // initialize the object
279 this.init();
200} 280}
201 281
202if (typeof exports === "object") { 282if (typeof exports === "object") {