aboutsummaryrefslogtreecommitdiff
path: root/assets
diff options
context:
space:
mode:
authorValerio Virgillito2012-04-30 14:49:49 -0700
committerValerio Virgillito2012-04-30 14:49:49 -0700
commit6b1a6994d98a18b45016b97ac8d81483109a586c (patch)
treecda8a39f8813fb5c0f9b4fb1d37007e6ec971128 /assets
parentc8fac64d4ad8ac18744a17a5e0f0d17204355fd6 (diff)
parent48e442977ef1630e0f664265a7142320b55d7d20 (diff)
downloadninja-6b1a6994d98a18b45016b97ac8d81483109a586c.tar.gz
Merge pull request #182 from ericmueller/Canvas-interaction
Canvas interaction
Diffstat (limited to 'assets')
-rw-r--r--assets/canvas-runtime.js110
1 files changed, 74 insertions, 36 deletions
diff --git a/assets/canvas-runtime.js b/assets/canvas-runtime.js
index ce2a15de..104c22cc 100644
--- a/assets/canvas-runtime.js
+++ b/assets/canvas-runtime.js
@@ -116,7 +116,8 @@ NinjaCvsRt.GLRuntime = function ( canvas, jObj, assetPath )
116 116
117 this._aspect = canvas.width/canvas.height; 117 this._aspect = canvas.width/canvas.height;
118 118
119 this._geomRoot = null; 119 //this._geomRoot = null;
120 this._rootChildren = [];
120 121
121 // all "live" materials 122 // all "live" materials
122 this._materials = []; 123 this._materials = [];
@@ -152,26 +153,36 @@ NinjaCvsRt.GLRuntime = function ( canvas, jObj, assetPath )
152 this.loadScene = function() 153 this.loadScene = function()
153 { 154 {
154 var jObj = this._jObj; 155 var jObj = this._jObj;
155 if (!jObj.children || (jObj.children.length != 1)) 156 if (!jObj.children) // || (jObj.children.length != 1))
156 throw new Error( "ill-formed JSON for runtime load: " + jObj ); 157 throw new Error( "ill-formed JSON for runtime load: " + jObj );
157 var root = jObj.children[0]; 158 var nChildren = jObj.children.length;
158 159
159 // parse the data 160 // parse the data
161 var child;
160 if (jObj.scenedata) 162 if (jObj.scenedata)
161 { 163 {
162 this._useWebGL = true; 164 this._useWebGL = true;
163 165
164 var rdgeStr = jObj.scenedata; 166 var rdgeStr = jObj.scenedata;
165 this.myScene.importJSON( rdgeStr ); 167 this.myScene.importJSON( rdgeStr );
166 this.importObjects( root ); 168 for (var i=0; i<nChildren; i++)
167 this.linkMaterials( this._geomRoot ); 169 {
170 child = jObj.children[i];
171 this.importObjects( child );
172 }
173 //this.linkMaterials( this._geomRoot );
174 this.linkMaterials( this._rootChildren );
168 this.initMaterials(); 175 this.initMaterials();
169 this.linkLights(); 176 this.linkLights();
170 } 177 }
171 else 178 else
172 { 179 {
173 this._context = this._canvas.getContext( "2d" ); 180 this._context = this._canvas.getContext( "2d" );
174 this.importObjects( root ); 181 for (var i=0; i<nChildren; i++)
182 {
183 child = jObj.children[i];
184 this.importObjects( child );
185 }
175 this.render(); 186 this.render();
176 } 187 }
177 }; 188 };
@@ -324,7 +335,10 @@ NinjaCvsRt.GLRuntime = function ( canvas, jObj, assetPath )
324 obj.setWorld( this ); 335 obj.setWorld( this );
325 336
326 if (parent == null) 337 if (parent == null)
327 this._geomRoot = obj; 338 {
339 //this._geomRoot = obj;
340 this._rootChildren.push( obj );
341 }
328 else 342 else
329 parent.addChild( obj ); 343 parent.addChild( obj );
330 }; 344 };
@@ -339,24 +353,31 @@ NinjaCvsRt.GLRuntime = function ( canvas, jObj, assetPath )
339 } 353 }
340 }; 354 };
341 355
342 this.linkMaterials = function( obj ) 356 this.linkMaterials = function( objArray )
343 { 357 {
344 if (!obj) return; 358 if (!objArray) return;
345 359
346 // get the array of materials from the object 360 for (var i=0; i<objArray.length; i++)
347 var matArray = obj._materials;
348 var nMats = matArray.length;
349 for (var i=0; i<nMats; i++)
350 { 361 {
351 var mat = matArray[i]; 362 var obj = objArray[i];
352 var nodeName = mat._materialNodeName; 363
353 var matNode = this.findMaterialNode( nodeName, this.myScene.scene ); 364 // get the array of materials from the object
354 if (matNode) 365 var matArray = obj._materials;
366 var nMats = matArray.length;
367 for (var j=0; j<nMats; j++)
355 { 368 {
356 mat._materialNode = matNode; 369 var mat = matArray[j];
357 mat._shader = matNode.shaderProgram; 370 var nodeName = mat._materialNodeName;
358 this._materials.push( mat ); 371 var matNode = this.findMaterialNode( nodeName, this.myScene.scene );
372 if (matNode)
373 {
374 mat._materialNode = matNode;
375 mat._shader = matNode.shaderProgram;
376 this._materials.push( mat );
377 }
359 } 378 }
379
380 this.linkMaterials( obj.children );
360 } 381 }
361 }; 382 };
362 383
@@ -394,15 +415,24 @@ NinjaCvsRt.GLRuntime = function ( canvas, jObj, assetPath )
394 415
395 this.render = function( obj ) 416 this.render = function( obj )
396 { 417 {
397 if (!obj) obj = this._geomRoot; 418 //if (!obj) obj = this._geomRoot;
398 obj.render(); 419 //obj.render();
420
421 var children;
422 if (obj)
423 {
424 obj.render();
425 children = obj.children;
426 }
427 else
428 children = this._rootChildren;
399 429
400 if (obj.children) 430 if (children)
401 { 431 {
402 var nKids = obj.children.length; 432 var nKids = children.length;
403 for (var i=0; i<nKids; i++) 433 for (var i=0; i<nKids; i++)
404 { 434 {
405 var child = obj.children[i]; 435 var child = children[i];
406 if (child) 436 if (child)
407 this.render( child ); 437 this.render( child );
408 } 438 }
@@ -702,9 +732,17 @@ NinjaCvsRt.RuntimeRectangle = function ()
702 if (brRad > minDimen) brRad = minDimen; 732 if (brRad > minDimen) brRad = minDimen;
703 if (trRad > minDimen) trRad = minDimen; 733 if (trRad > minDimen) trRad = minDimen;
704 734
735 var world = this.getWorld();
736 var vpw = world.getViewportWidth(), vph = world.getViewportHeight();
737 var cop = [0.5*vpw, 0.5*vph, 0.0];
738 var xCtr = cop[0] + this._xOffset, yCtr = cop[1] - this._yOffset;
739 var xLeft = xCtr - 0.5*this._width, yTop = yCtr - 0.5*this._height;
740 var xDist = cop[0] - xLeft, yDist = cop[1] - yTop;
741 var xOff = 0.5*vpw - xDist, yOff = 0.5*vph - yDist;
742
705 if ((tlRad <= 0) && (blRad <= 0) && (brRad <= 0) && (trRad <= 0)) 743 if ((tlRad <= 0) && (blRad <= 0) && (brRad <= 0) && (trRad <= 0))
706 { 744 {
707 ctx.rect(pt[0], pt[1], width - 2*inset, height - 2*inset); 745 ctx.rect(pt[0]+xOff, pt[1]+yOff, width - 2*inset, height - 2*inset);
708 } 746 }
709 else 747 else
710 { 748 {
@@ -713,53 +751,53 @@ NinjaCvsRt.RuntimeRectangle = function ()
713 if (rad < 0) rad = 0; 751 if (rad < 0) rad = 0;
714 pt[1] += rad; 752 pt[1] += rad;
715 if (Math.abs(rad) < 0.001) pt[1] = inset; 753 if (Math.abs(rad) < 0.001) pt[1] = inset;
716 ctx.moveTo( pt[0], pt[1] ); 754 ctx.moveTo( pt[0]+xOff, pt[1]+yOff );
717 755
718 // get the bottom left point 756 // get the bottom left point
719 pt = [inset, height - inset]; 757 pt = [inset, height - inset];
720 rad = blRad - inset; 758 rad = blRad - inset;
721 if (rad < 0) rad = 0; 759 if (rad < 0) rad = 0;
722 pt[1] -= rad; 760 pt[1] -= rad;
723 ctx.lineTo( pt[0], pt[1] ); 761 ctx.lineTo( pt[0]+xOff, pt[1]+yOff );
724 762
725 // get the bottom left curve 763 // get the bottom left curve
726 if (rad > 0.001) 764 if (rad > 0.001)
727 ctx.quadraticCurveTo( inset, height-inset, inset+rad, height-inset ); 765 ctx.quadraticCurveTo( inset+xOff, height-inset+yOff, inset+rad+xOff, height-inset+yOff );
728 766
729 // do the bottom of the rectangle 767 // do the bottom of the rectangle
730 pt = [width - inset, height - inset]; 768 pt = [width - inset, height - inset];
731 rad = brRad - inset; 769 rad = brRad - inset;
732 if (rad < 0) rad = 0; 770 if (rad < 0) rad = 0;
733 pt[0] -= rad; 771 pt[0] -= rad;
734 ctx.lineTo( pt[0], pt[1] ); 772 ctx.lineTo( pt[0]+xOff, pt[1]+yOff );
735 773
736 // get the bottom right arc 774 // get the bottom right arc
737 if (rad > 0.001) 775 if (rad > 0.001)
738 ctx.quadraticCurveTo( width-inset, height-inset, width-inset, height-inset-rad ); 776 ctx.quadraticCurveTo( width-inset+xOff, height-inset+yOff, width-inset+xOff, height-inset-rad+yOff );
739 777
740 // get the right of the rectangle 778 // get the right of the rectangle
741 pt = [width - inset, inset]; 779 pt = [width - inset, inset];
742 rad = trRad - inset; 780 rad = trRad - inset;
743 if (rad < 0) rad = 0; 781 if (rad < 0) rad = 0;
744 pt[1] += rad; 782 pt[1] += rad;
745 ctx.lineTo( pt[0], pt[1] ); 783 ctx.lineTo( pt[0]+xOff, pt[1]+yOff );