aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge/materials/cloud-material.js
diff options
context:
space:
mode:
authorhwc4872012-04-15 05:16:34 -0700
committerhwc4872012-04-15 05:16:34 -0700
commit8992d9baf542135e910bb59328a592d9e330703a (patch)
tree268465b69fd37ec1dc33d304de959e79dc619ecd /js/lib/rdge/materials/cloud-material.js
parent331b2ad4d602016f9bb3d03be759fd81fed50c10 (diff)
downloadninja-8992d9baf542135e910bb59328a592d9e330703a.tar.gz
Cloud Material
Diffstat (limited to 'js/lib/rdge/materials/cloud-material.js')
-rw-r--r--js/lib/rdge/materials/cloud-material.js270
1 files changed, 185 insertions, 85 deletions
diff --git a/js/lib/rdge/materials/cloud-material.js b/js/lib/rdge/materials/cloud-material.js
index 126751c6..e2292c7a 100644
--- a/js/lib/rdge/materials/cloud-material.js
+++ b/js/lib/rdge/materials/cloud-material.js
@@ -8,6 +8,8 @@ var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialP
8var Material = require("js/lib/rdge/materials/material").Material; 8var Material = require("js/lib/rdge/materials/material").Material;
9var GLWorld = require("js/lib/drawing/world").World; 9var GLWorld = require("js/lib/drawing/world").World;
10var Texture = require("js/lib/rdge/texture").Texture; 10var Texture = require("js/lib/rdge/texture").Texture;
11var ElementMediator = require("js/mediators/element-mediator").ElementMediator;
12var TagTool = require("js/tools/TagTool").TagTool;
11 13
12/////////////////////////////////////////////////////////////////////// 14///////////////////////////////////////////////////////////////////////
13// Class GLMaterial 15// Class GLMaterial
@@ -20,7 +22,9 @@ var CloudMaterial = function CloudMaterial() {
20 this._name = "CloudMaterial"; 22 this._name = "CloudMaterial";
21 this._shaderName = "cloud"; 23 this._shaderName = "cloud";
22 24
23 this._texMap = 'assets/images/cloud10.png'; 25 //this._texMap = 'assets/images/cloud10.png';
26 this._texMap = 'assets/images/us_flag.png';
27 //this._texMap = 'assets/images/cubelight.png';
24 this._diffuseColor = [0.5, 0.5, 0.5, 0.5]; 28 this._diffuseColor = [0.5, 0.5, 0.5, 0.5];
25 29
26 // base size of cloud polygons. Random adjustments made to each quad 30 // base size of cloud polygons. Random adjustments made to each quad
@@ -31,7 +35,7 @@ var CloudMaterial = function CloudMaterial() {
31 35
32 // parameter initial values 36 // parameter initial values
33 this._time = 0.0; 37 this._time = 0.0;
34 this._surfaceAlpha = 1.0; 38 this._surfaceAlpha = 0.6;
35 this._zmin = 0.1; 39 this._zmin = 0.1;
36 this._zmax = 10.0; 40 this._zmax = 10.0;
37 41
@@ -111,80 +115,54 @@ var CloudMaterial = function CloudMaterial() {
111 115
112 // save the world 116 // save the world
113 if (world) this.setWorld( world ); 117 if (world) this.setWorld( world );
114 118 var dstWorld = world;
115 // this variable declared above is inherited set to a smaller delta.
116 // the cloud material runs a little faster
117 this._dTime = 0.01;
118 119
119 // create a canvas to render into 120 // create a canvas to render into
120 var doc = world.getCanvas().ownerDocument; 121 var dstCanvas = this.getWorld().getCanvas();
122 var doc = this.getWorld().getCanvas().ownerDocument;
121 var canvasID = "__canvas__"; 123 var canvasID = "__canvas__";
122 //this._srcCanvas = doc.createElement(canvasID); 124 //this._srcCanvas = doc.createElement(canvasID);
123 this._srcCanvas = NJUtils.makeNJElement("canvas", canvasID, "shape", {"data-RDGE-id": NJUtils.generateRandom()}, true); 125 this._srcCanvas = NJUtils.makeNJElement("canvas", canvasID, "shape", {"data-RDGE-id": NJUtils.generateRandom()}, true);
124 var dstCanvas = this.getWorld().getCanvas(), 126 srcCanvas = this._srcCanvas;
125 srcCanvas = this._srcCanvas;
126 srcCanvas.width = dstCanvas.width; 127 srcCanvas.width = dstCanvas.width;
127 srcCanvas.height = dstCanvas.height; 128 srcCanvas.height = dstCanvas.height;
128 129
129 // save the current RDGE context 130 //////////////////////////////////////////////////////////////////////////////////
130 var saveContext = g_Engine.getContext(); 131 // IS THIS NECESSARY??
131 132 //var elementModel = TagTool.makeElement(~~srcCanvas.width, ~~srcCanvas.height,
132 // build a world to do the rendering 133 // Matrix.I(4), [0,0,0], srcCanvas);
133 this._srcWorld = new GLWorld( this._srcCanvas, true, true ); 134 //ElementMediator.addElement(srcCanvas, elementModel.data, true);
134 var srcWorld = this._srcWorld; 135 //////////////////////////////////////////////////////////////////////////////////
135 this._srcCanvas.__GLWorld = srcWorld;
136 136
137 // build the geometry 137 // build the source.
138 var prim = this.buildGeometry( srcWorld, srcCanvas.width, srcCanvas.height ); 138 // the source being the world/canvas/geometry of the clouds.
139 // the source is used to create a texture map that is then used by
140 // the destimation.
141 this.buildSource();
139 142
140 // set up the shader 143 // set up the shader
141 this._shader = new jshader(); 144 this._shader = new jshader();
142 this._shader.def = cloudMaterialDef; 145 this._shader.def = cloudMapMaterialDef;
143 this._shader.init(); 146 this._shader.init();
144 147
145 // set up the material node 148 // set up the material node
146 this._materialNode = createMaterialNode("cloudMaterial" + "_" + world.generateUniqueNodeID()); 149 this._materialNode = createMaterialNode("cloudMapMaterial" + "_" + world.generateUniqueNodeID());
147 this._materialNode.setShader(this._shader); 150 this._materialNode.setShader(this._shader);
148 151
149 // initialize the shader uniforms 152 // initialize the time
150 this._time = 0; 153 this._time = 0;
151 if (this._shader && this._shader['default']) {
152 var t = this._shader['default'];
153 if (t)
154 {
155 t.u_time.set( [this._time] );
156 t.u_surfaceAlpha.set( [this._surfaceAlpha] );
157 t.u_zmin.set( [this._zmin] );
158 t.u_zmax.set( [this._zmax] );
159 154
160 var wrap = 'REPEAT', mips = true; 155 // create the texture to map the source cloud generation world/canvas to the destination
161 var texMapName = this._propValues[this._propNames[0]];
162 var tex = srcWorld.renderer.getTextureByName(texMapName, wrap, mips );
163 if (tex)
164 srcWorld.textureToLoad( tex );
165 }
166 }
167
168 // add the nodes to the tree
169 var trNode = createTransformNode("objRootNode_" + this._srcWorld._nodeCounter++);
170 srcWorld._rootNode.insertAsChild( trNode );
171 trNode.attachMeshNode(srcWorld.renderer.id + "_prim_" + srcWorld._nodeCounter++, prim);
172 trNode.attachMaterial( this._materialNode );
173
174 // create the texture
175 var wrap = 'REPEAT', mips = true; 156 var wrap = 'REPEAT', mips = true;
176 this._srcWorld._hasAnimatedMaterials = true; // hack to make the texture think this world is animated
177 this._glTex = new Texture( world, this._srcCanvas, wrap, mips ); 157 this._glTex = new Texture( world, this._srcCanvas, wrap, mips );
178 158
179 // set the shader values in the shader 159 // set the shader values in the shader
180 this.updateTexture(); 160 this.updateTexture();
181 this.update( 0 ); 161 this.update( 0 );
182
183 // restore the previous RDGE context
184 g_Engine.setContext( saveContext.id );
185 }; 162 };
186 163
187 this.updateTexture = function() { 164 this.updateTexture = function()
165 {
188 var material = this._materialNode; 166 var material = this._materialNode;
189 if (material) 167 if (material)
190 { 168 {
@@ -195,9 +173,7 @@ var CloudMaterial = function CloudMaterial() {
195 var technique = material.shaderProgram['default']; 173 var technique = material.shaderProgram['default'];
196 var renderer = g_Engine.getContext().renderer; 174 var renderer = g_Engine.getContext().renderer;
197 if (renderer && technique) { 175 if (renderer && technique) {
198 var texMapName = this._propValues[this._propNames[0]];
199 var wrap = 'REPEAT', mips = true; 176 var wrap = 'REPEAT', mips = true;
200 //var tex = this.loadTexture( texMapName, wrap, mips );
201 if (this._glTex) 177 if (this._glTex)
202 { 178 {
203 this._glTex.render(); 179 this._glTex.render();
@@ -205,9 +181,9 @@ var CloudMaterial = function CloudMaterial() {
205 if (tex) 181 if (tex)
206 technique.u_tex0.set( tex ); 182 technique.u_tex0.set( tex );
207 } 183 }
208
209 } 184 }
210 185
186 // restore the context
211 g_Engine.setContext( saveContext.id ); 187 g_Engine.setContext( saveContext.id );
212 } 188 }
213 }; 189 };
@@ -226,29 +202,100 @@ var CloudMaterial = function CloudMaterial() {
226 202
227 this.update = function( time ) 203 this.update = function( time )
228 { 204 {
205 var technique, renderer, tex;
206
207 // update the cloud map material
229 var material = this._materialNode; 208 var material = this._materialNode;
230 if (material) 209 if (material)
231 { 210 {
232 if (this._srcWorld) this._srcWorld.draw(); 211 technique = material.shaderProgram['default'];
212 renderer = g_Engine.getContext().renderer;
213 if (renderer && technique)
214 {
215 if (this._glTex)
216 {
217 this._glTex.render();
218 tex = this._glTex.getTexture();
219 technique.u_tex0.set( tex );
220 }
221 }
222 }
223 }
233 224
234 var technique = material.shaderProgram['default']; 225 // update the source material
235 var renderer = g_Engine.getContext().renderer; 226 material = this._srcMaterialNode;
236 if (renderer && technique) { 227 if (material)
228 {