diff options
Diffstat (limited to 'js/lib/rdge/texture.js')
-rw-r--r-- | js/lib/rdge/texture.js | 527 |
1 files changed, 261 insertions, 266 deletions
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; | |||
12 | /////////////////////////////////////////////////////////////////////// | 12 | /////////////////////////////////////////////////////////////////////// |
13 | function Texture( dstWorld, texMapName, wrap, mips ) | 13 | function Texture( dstWorld, texMapName, wrap, mips ) |
14 | { | 14 | { |
15 | /////////////////////////////////////////////////////////////////////// | 15 | /////////////////////////////////////////////////////////////////////// |
16 | // Instance variables | 16 | // Instance variables |
17 | /////////////////////////////////////////////////////////////////////// | 17 | /////////////////////////////////////////////////////////////////////// |
18 | this._texture; | 18 | this._texture; |
19 | 19 | ||
20 | // texture attributes | 20 | // texture attributes |
21 | this._texMapName = texMapName.slice(); | 21 | this._texMapName = texMapName.slice(); |
22 | 22 | ||
23 | // set default values for wrap and mips | 23 | // set default values for wrap and mips |
24 | if (wrap === undefined) | 24 | if (wrap === undefined) |
25 | wrap = "REPEAT"; | 25 | wrap = "REPEAT"; |
26 | if (mips === undefined) | 26 | if (mips === undefined) |
27 | mips = true; | 27 | mips = true; |
28 | this._wrap = wrap; | 28 | this._wrap = wrap; |
29 | this._mips = mips; | 29 | this._mips = mips; |
30 | 30 | ||
31 | // the canvas generating the texture map (if there is one) | 31 | // the canvas generating the texture map (if there is one) |
32 | this._srcCanvas; | 32 | this._srcCanvas; |
33 | this._srcWorld; | 33 | this._srcWorld; |
34 | 34 | ||
35 | // cache whether or not the source is animated | 35 | // cache whether or not the source is animated |
36 | this._isAnimated = false; | 36 | this._isAnimated = false; |
37 | 37 | ||
38 | // the destination world that will use the texture map | 38 | // the destination world that will use the texture map |
39 | this._dstWorld = dstWorld; | 39 | this._dstWorld = dstWorld; |
40 | 40 | ||
41 | /////////////////////////////////////////////////////////////////////// | 41 | /////////////////////////////////////////////////////////////////////// |
42 | // Property Accessors | 42 | // Property Accessors |
43 | /////////////////////////////////////////////////////////////////////// | 43 | /////////////////////////////////////////////////////////////////////// |
44 | this.getTexture = function() { return this._texture; } | 44 | this.getTexture = function() { return this._texture; } |
45 | 45 | ||
46 | this.setSrcWorld = function(w) { this._srcWorld = w; } | 46 | this.setSrcWorld = function(w) { this._srcWorld = w; } |
47 | this.getSrcWorld = function() { return this._srcWorld; } | 47 | this.getSrcWorld = function() { return this._srcWorld; } |
48 | 48 | ||
49 | this.setDstWorld = function(w) { this._dstWorld = w; } | 49 | this.setDstWorld = function(w) { this._dstWorld = w; } |
50 | this.getDstWorld = function() { return this._dstWorld; } | 50 | this.getDstWorld = function() { return this._dstWorld; } |
51 | 51 | ||
52 | this.isAnimated = function() { return this._isAnimated; } | 52 | this.isAnimated = function() { return this._isAnimated; } |
53 | 53 | ||
54 | /////////////////////////////////////////////////////////////////////// | 54 | /////////////////////////////////////////////////////////////////////// |
55 | // Methods | 55 | // Methods |
56 | /////////////////////////////////////////////////////////////////////// | 56 | /////////////////////////////////////////////////////////////////////// |
57 | 57 | ||
58 | this.init = function() | 58 | this.init = function() |
59 | { | 59 | { |
60 | // determine if the source is a canvas or an image file | 60 | // determine if the source is a canvas or an image file |
61 | var viewUtils = require("js/helper-classes/3D/view-utils").ViewUtils; | 61 | var viewUtils = require("js/helper-classes/3D/view-utils").ViewUtils; |
62 | var root = viewUtils.application.ninja.currentDocument.documentRoot; | 62 | var root = viewUtils.application.ninja.currentDocument.documentRoot; |
63 | var srcCanvas = this.findCanvas( this._texMapName, root ); | 63 | var srcCanvas = this.findCanvas( this._texMapName, root ); |
64 | if (srcCanvas) | 64 | if (srcCanvas) |
65 | { | 65 | { |
66 | this._srcCanvas = srcCanvas; | 66 | this._srcCanvas = srcCanvas; |
67 | if (srcCanvas.elementModel && srcCanvas.elementModel.shapeModel && srcCanvas.elementModel.shapeModel.GLWorld) | 67 | if (srcCanvas.elementModel && srcCanvas.elementModel.shapeModel && srcCanvas.elementModel.shapeModel.GLWorld) |
68 | { | 68 | { |
69 | this._srcWorld = srcCanvas.elementModel.shapeModel.GLWorld; | 69 | this._srcWorld = srcCanvas.elementModel.shapeModel.GLWorld; |
70 | 70 | ||
71 | // add a notifier to the world | 71 | // add a notifier to the world |
72 | this._srcWorld.addListener( this, this.worldCallback, { srcWorld: this._srcWorld } ); | 72 | this._srcWorld.addListener( this, this.worldCallback, { srcWorld: this._srcWorld } ); |
73 | 73 | ||
74 | // check if the source is animated | 74 | // check if the source is animated |
75 | if (srcCanvas.elementModel && srcCanvas.elementModel.shapeModel && srcCanvas.elementModel.shapeModel.GLWorld) | 75 | if (srcCanvas.elementModel && srcCanvas.elementModel.shapeModel && srcCanvas.elementModel.shapeModel.GLWorld) |
76 | this._isAnimated = this._srcWorld._hasAnimatedMaterials; | 76 | this._isAnimated = this._srcWorld._hasAnimatedMaterials; |
77 | } | 77 | } |
78 | 78 | ||
79 | this.loadFromCanvas(); | 79 | this.loadFromCanvas(); |
80 | } | 80 | } |
81 | else | 81 | else |
82 | { | 82 | { |
83 | this.loadFromFile(); | 83 | this.loadFromFile(); |
84 | } | 84 | } |
85 | } | 85 | } |
86 | 86 | ||
87 | this.worldCallback = function( type, callbackObj, calleeData, callerData ) | 87 | this.worldCallback = function( type, callbackObj, calleeData, callerData ) |
88 | { | 88 | { |
89 | console.log( "texture callback, type: " + type ); | 89 | console.log( "texture callback, type: " + type ); |
90 | if (calleeData.srcWorld) | 90 | if (calleeData.srcWorld) |
91 | { | 91 | { |
92 | var srcWorld = calleeData.srcWorld; | 92 | var srcWorld = calleeData.srcWorld; |
93 | var notifier = srcWorld._notifier; | 93 | var notifier = srcWorld._notifier; |
94 | var texture = this.callbackObj; | 94 | var texture = this.callbackObj; |
95 | if (texture) | 95 | if (texture) |
96 | { | 96 | { |
97 | switch (type) | 97 | switch (type) |
98 | { | 98 | { |
99 | case notifier.OBJECT_DELETE: | 99 | case notifier.OBJECT_DELETE: |
100 | break; | 100 | break; |
101 | 101 | ||
102 | case notifier.OBJECT_REINSTANTIATE: | 102 | case notifier.OBJECT_REINSTANTIATE: |
103 | break; | 103 | break; |
104 | 104 | ||
105 | case notifier.OBJECT_CHANGE: | 105 | case notifier.OBJECT_CHANGE: |
106 | if (!texture.isAnimated()) | 106 | if (!texture.isAnimated()) |
107 | texture.rerender(); | 107 | texture.rerender(); |
108 | texture.getDstWorld().restartRenderLoop(); | 108 | texture.getDstWorld().restartRenderLoop(); |
109 | break; | 109 | break; |
110 | 110 | ||
111 | default: | 111 | default: |
112 | throw new Exception( "unrecognized texture callback type: " + type ); | 112 | throw new Exception( "unrecognized texture callback type: " + type ); |
113 | break; | 113 | break; |
114 | } | 114 | } |
115 | } | 115 | } |
116 | } | 116 | } |
117 | } | 117 | } |
118 | 118 | ||
119 | this.loadFromFile = function() | 119 | this.loadFromFile = function() |
120 | { | 120 | { |
121 | var tex = this._texture; | 121 | var tex = this._texture; |
122 | this._srcCanvas = null; | 122 | this._srcCanvas = null; |
123 | 123 | ||
124 | // only load if something has changed | 124 | // only load if something has changed |
125 | if (this._texMapName !== texMapName) // does RDGE allow us to change wrap or mips? | 125 | if (this._texMapName !== texMapName) // does RDGE allow us to change wrap or mips? |
126 | { | 126 | { |
127 | var texMapName = this._texMapName; | 127 | var texMapName = this._texMapName; |
128 | var wrap = this._wrap; | 128 | var wrap = this._wrap; |
129 | var mips = this._mips; | 129 | var mips = this._mips; |
130 | 130 | ||
131 | var dstWorld = this.getDstWorld(); | 131 | var dstWorld = this.getDstWorld(); |
132 | if (dstWorld) | 132 | if (dstWorld) |
133 | { | 133 | { |
134 | var renderer = dstWorld.getRenderer(); | 134 | var renderer = dstWorld.getRenderer(); |
135 | tex = renderer.getTextureByName(texMapName, wrap, mips ); | 135 | tex = renderer.getTextureByName(texMapName, wrap, mips ); |
136 | this._texture = tex; | 136 | this._texture = tex; |
137 | dstWorld.textureToLoad( tex ); | 137 | dstWorld.textureToLoad( tex ); |
138 | } | 138 | } |
139 | } | 139 | } |
140 | 140 | ||
141 | return tex; | 141 | return tex; |
142 | } | 142 | } |
143 | 143 | ||
144 | var __texCounter = 0; | 144 | var __texCounter = 0; |
145 | this.loadFromCanvas = function() | 145 | this.loadFromCanvas = function() |
146 | { | 146 | { |
147 | var srcCanvas = this._srcCanvas; | 147 | var srcCanvas = this._srcCanvas; |
148 | var wrap = this._wrap; | 148 | var wrap = this._wrap; |
149 | var mips = this._mips; | 149 | var mips = this._mips; |
150 | 150 | ||
151 | this._texMapName = "GLTexture_" + __texCounter; | 151 | this._texMapName = "GLTexture_" + __texCounter; |
152 | __texCounter++; | 152 | __texCounter++; |
153 | 153 | ||
154 | // create the texture | 154 | // create the texture |
155 | var world = this.getDstWorld(); | 155 | var world = this.getDstWorld(); |
156 | tex = world.getGLContext().createTexture(); | 156 | tex = world.getGLContext().createTexture(); |
157 | this._texture = tex; | 157 | this._texture = tex; |
158 | tex.texparams = new _texparams(wrap, mips); // defined in renderer.js | 158 | tex.texparams = new _texparams(wrap, mips); // defined in renderer.js |
159 | tex.image = new Image; | 159 | tex.image = new Image; |
160 | 160 | ||
161 | // create the canvas and context to render into | 161 | // create the canvas and context to render into |
162 | var doc = srcCanvas.ownerDocument; | 162 | var doc = srcCanvas.ownerDocument; |
163 | this._renderCanvas = doc.createElement("canvas"); | 163 | this._renderCanvas = doc.createElement("canvas"); |
164 | 164 | ||
165 | // cache whether this is a 2D canvas or 3D canvas | 165 | // cache whether this is a 2D canvas or 3D canvas |
166 | var srcCtx = srcCanvas.getContext("2d"); |