aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge
diff options
context:
space:
mode:
authorhwc4872012-04-13 07:08:42 -0700
committerhwc4872012-04-13 07:08:42 -0700
commit331b2ad4d602016f9bb3d03be759fd81fed50c10 (patch)
tree6bd56180f319d400881356626f32919a4d6b1f63 /js/lib/rdge
parent1fa8516bcd0fb0277c13a7081b5a51ce041d3496 (diff)
downloadninja-331b2ad4d602016f9bb3d03be759fd81fed50c10.tar.gz
Cloud Material
Diffstat (limited to 'js/lib/rdge')
-rw-r--r--js/lib/rdge/materials/cloud-material.js118
1 files changed, 72 insertions, 46 deletions
diff --git a/js/lib/rdge/materials/cloud-material.js b/js/lib/rdge/materials/cloud-material.js
index cec00696..126751c6 100644
--- a/js/lib/rdge/materials/cloud-material.js
+++ b/js/lib/rdge/materials/cloud-material.js
@@ -121,14 +121,21 @@ var CloudMaterial = function CloudMaterial() {
121 var canvasID = "__canvas__"; 121 var canvasID = "__canvas__";
122 //this._srcCanvas = doc.createElement(canvasID); 122 //this._srcCanvas = doc.createElement(canvasID);
123 this._srcCanvas = NJUtils.makeNJElement("canvas", canvasID, "shape", {"data-RDGE-id": NJUtils.generateRandom()}, true); 123 this._srcCanvas = NJUtils.makeNJElement("canvas", canvasID, "shape", {"data-RDGE-id": NJUtils.generateRandom()}, true);
124 var dstCanvas = this.getWorld().getCanvas(),
125 srcCanvas = this._srcCanvas;
126 srcCanvas.width = dstCanvas.width;
127 srcCanvas.height = dstCanvas.height;
128
129 // save the current RDGE context
130 var saveContext = g_Engine.getContext();
124 131
125 // build a world to do the rendering 132 // build a world to do the rendering
126 this._srcWorld = new GLWorld( this._srcCanvas, true ); 133 this._srcWorld = new GLWorld( this._srcCanvas, true, true );
127 var srcWorld = this._srcWorld; 134 var srcWorld = this._srcWorld;
128 this._srcCanvas.__GLWorld = srcWorld; 135 this._srcCanvas.__GLWorld = srcWorld;
129 136
130 // build the geometry 137 // build the geometry
131 var prim = this.buildGeometry(); 138 var prim = this.buildGeometry( srcWorld, srcCanvas.width, srcCanvas.height );
132 139
133 // set up the shader 140 // set up the shader
134 this._shader = new jshader(); 141 this._shader = new jshader();
@@ -149,6 +156,12 @@ var CloudMaterial = function CloudMaterial() {
149 t.u_surfaceAlpha.set( [this._surfaceAlpha] ); 156 t.u_surfaceAlpha.set( [this._surfaceAlpha] );
150 t.u_zmin.set( [this._zmin] ); 157 t.u_zmin.set( [this._zmin] );
151 t.u_zmax.set( [this._zmax] ); 158 t.u_zmax.set( [this._zmax] );
159
160 var wrap = 'REPEAT', mips = true;
161 var texMapName = this._propValues[this._propNames[0]];
162 var tex = srcWorld.renderer.getTextureByName(texMapName, wrap, mips );
163 if (tex)
164 srcWorld.textureToLoad( tex );
152 } 165 }
153 } 166 }
154 167
@@ -160,16 +173,25 @@ var CloudMaterial = function CloudMaterial() {
160 173
161 // create the texture 174 // create the texture
162 var wrap = 'REPEAT', mips = true; 175 var wrap = 'REPEAT', mips = true;
176 this._srcWorld._hasAnimatedMaterials = true; // hack to make the texture think this world is animated
163 this._glTex = new Texture( world, this._srcCanvas, wrap, mips ); 177 this._glTex = new Texture( world, this._srcCanvas, wrap, mips );
164 178
165 // set the shader values in the shader 179 // set the shader values in the shader
166 this.updateTexture(); 180 this.updateTexture();
167 this.update( 0 ); 181 this.update( 0 );
182
183 // restore the previous RDGE context
184 g_Engine.setContext( saveContext.id );
168 }; 185 };
169 186
170 this.updateTexture = function() { 187 this.updateTexture = function() {
171 var material = this._materialNode; 188 var material = this._materialNode;
172 if (material) { 189 if (material)
190 {
191 // save the current context
192 var saveContext = g_Engine.getContext();
193 g_Engine.setContext( this._srcCanvas.rdgeid );
194
173 var technique = material.shaderProgram['default']; 195 var technique = material.shaderProgram['default'];
174 var renderer = g_Engine.getContext().renderer; 196 var renderer = g_Engine.getContext().renderer;
175 if (renderer && technique) { 197 if (renderer && technique) {
@@ -185,8 +207,22 @@ var CloudMaterial = function CloudMaterial() {
185 } 207 }
186 208
187 } 209 }
210
211 g_Engine.setContext( saveContext.id );
188 } 212 }
189 }; 213 };
214
215 this.updateTextures = function()
216 {
217 if (this._glTex)
218 {
219 if (!this._glTex.isAnimated())
220 {
221 this._glTex.render();
222 this.updateTexture();
223 }
224 }
225 }
190 226
191 this.update = function( time ) 227 this.update = function( time )
192 { 228 {
@@ -215,12 +251,43 @@ var CloudMaterial = function CloudMaterial() {
215 } 251 }
216 }; 252 };
217 253
218 this.buildGeometry = function() 254 this.buildGeometry = function(world, canvasWidth, canvasHeight)
219 { 255 {
220 var RectangleGeometry = require("js/lib/geom/rectangle").RectangleGeometry; 256 var RectangleGeometry = require("js/lib/geom/rectangle").RectangleGeometry;
221 257
222 RectangleGeometry.init(); 258 RectangleGeometry.init();
223 259
260 // get the normalized device coordinates (NDC) for
261 // all position and dimensions.
262 var vpw = world.getViewportWidth(), vph = world.getViewportHeight();
263 var xNDC = 0.0/vpw, yNDC = 0.0/vph,
264 xFillNDC = canvasWidth/vpw, yFillNDC = canvasHeight/vph;
265
266 var aspect = world.getAspect();
267 var zn = world.getZNear(), zf = world.getZFar();
268 var t = zn * Math.tan(world.getFOV() * Math.PI / 360.0),
269 b = -t,
270 r = aspect*t,
271 l = -r;
272
273 // calculate the object coordinates from their NDC coordinates
274 var z = -world.getViewDistance();
275
276 // get the position of the origin
277 var x = -z*(r-l)/(2.0*zn)*xNDC,
278 y = -z*(t-b)/(2.0*zn)*yNDC;
279
280 // get the x and y fill
281 var xFill = -z*(r-l)/(2.0*zn)*xFillNDC,
282 yFill = -z*(t-b)/(2.0*zn)*yFillNDC;
283
284
285 //this.createFill([x,y], 2*xFill, 2*yFill, tlRadius, blRadius, brRadius, trRadius, fillMaterial);
286 var ctr = [x,y], width = 2*xFill, height = 2*yFill;
287 var prim = RectangleGeometry.create( ctr, width, height );
288 return prim;
289
290 /*
224 var verts = [], 291 var verts = [],
225 normals = [ [0,0,1], [0,0,1], [0,0,1], [0,0,1] ], 292 normals = [ [0,0,1], [0,0,1], [0,0,1], [0,0,1] ],
226 uvs = [ [0,0], [1,0], [1,1], [0,1] ]; 293 uvs = [ [0,0], [1,0], [1,1], [0,1] ];
@@ -242,6 +309,7 @@ var CloudMaterial = function CloudMaterial() {
242 } 309 }
243 310
244 return RectangleGeometry.buildPrimitive(); 311 return RectangleGeometry.buildPrimitive();
312 */
245 }; 313 };
246 314
247 // JSON export 315 // JSON export
@@ -270,48 +338,6 @@ var CloudMaterial = function CloudMaterial() {
270 throw new Error( "could not import material: " + jObj ); 338 throw new Error( "could not import material: " + jObj );
271 } 339 }
272 } 340 }
273
274
275 this.export = function() {
276 // every material needs the base type and instance name
277 var exportStr = "material: " + this.getShaderName() + "\n";
278 exportStr += "name: " + this.getName() + "\n";
279
280 var world = this.getWorld();
281 if (!world)
282 throw new Error( "no world in material.export, " + this.getName() );
283
284 var texMapName = this._propValues[this._propNames[0]];
285 exportStr += "texture: " +texMapName + "\n";
286
287 // every material needs to terminate like this
288 exportStr += "endMaterial\n";
289
290 return exportStr;
291 };
292
293 this.import = function( importStr ) {
294 var pu = new MaterialParser( importStr );
295 var material = pu.nextValue( "material: " );
296 if (material != this.getShaderName()) throw new Error( "ill-formed material" );
297 this.setName( pu.nextValue( "name: ") );
298
299 var rtnStr;
300 try {
301 this._propValues[this._propNames[0]] = pu.nextValue( "texture: " );
302
303 var endKey = "endMaterial\n";
304 var index = importStr.indexOf( endKey );
305 index += endKey.length;
306 rtnStr = importStr.substr( index );
307 }
308 catch (e)
309 {
310 throw new Error( "could not import material: " + importStr );
311 }
312
313 return rtnStr;
314 }
315}; 341};
316 342
317/////////////////////////////////////////////////////////////////////////////////////// 343///////////////////////////////////////////////////////////////////////////////////////