aboutsummaryrefslogtreecommitdiff
path: root/assets/canvas-runtime.js
diff options
context:
space:
mode:
Diffstat (limited to 'assets/canvas-runtime.js')
-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 ee9f24a4..b2db2cbd 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 }
@@ -694,9 +724,17 @@ NinjaCvsRt.RuntimeRectangle = function ()
694 var blRad = this._blRadius; 724 var blRad = this._blRadius;
695 var brRad = this._brRadius; 725 var brRad = this._brRadius;
696 726
727 var world = this.getWorld();
728 var vpw = world.getViewportWidth(), vph = world.getViewportHeight();
729 var cop = [0.5*vpw, 0.5*vph, 0.0];
730 var xCtr = cop[0] + this._xOffset, yCtr = cop[1] - this._yOffset;
731 var xLeft = xCtr - 0.5*this._width, yTop = yCtr - 0.5*this._height;
732 var xDist = cop[0] - xLeft, yDist = cop[1] - yTop;
733 var xOff = 0.5*vpw - xDist, yOff = 0.5*vph - yDist;
734
697 if ((tlRad <= 0) && (blRad <= 0) && (brRad <= 0) && (trRad <= 0)) 735 if ((tlRad <= 0) && (blRad <= 0) && (brRad <= 0) && (trRad <= 0))
698 { 736 {
699 ctx.rect(pt[0], pt[1], width - 2*inset, height - 2*inset); 737 ctx.rect(pt[0]+xOff, pt[1]+yOff, width - 2*inset, height - 2*inset);
700 } 738 }
701 else 739 else
702 { 740 {
@@ -705,53 +743,53 @@ NinjaCvsRt.RuntimeRectangle = function ()
705 if (rad < 0) rad = 0; 743 if (rad < 0) rad = 0;
706 pt[1] += rad; 744 pt[1] += rad;
707 if (Math.abs(rad) < 0.001) pt[1] = inset; 745 if (Math.abs(rad) < 0.001) pt[1] = inset;
708 ctx.moveTo( pt[0], pt[1] ); 746 ctx.moveTo( pt[0]+xOff, pt[1]+yOff );
709 747
710 // get the bottom left point 748 // get the bottom left point
711 pt = [inset, height - inset]; 749 pt = [inset, height - inset];
712 rad = blRad - inset; 750 rad = blRad - inset;
713 if (rad < 0) rad = 0; 751 if (rad < 0) rad = 0;
714 pt[1] -= rad; 752 pt[1] -= rad;
715 ctx.lineTo( pt[0], pt[1] ); 753 ctx.lineTo( pt[0]+xOff, pt[1]+yOff );
716 754
717 // get the bottom left curve 755 // get the bottom left curve
718 if (rad > 0.001) 756 if (rad > 0.001)
719 ctx.quadraticCurveTo( inset, height-inset, inset+rad, height-inset ); 757 ctx.quadraticCurveTo( inset+xOff, height-inset+yOff, inset+rad+xOff, height-inset+yOff );
720 758
721 // do the bottom of the rectangle 759 // do the bottom of the rectangle
722 pt = [width - inset, height - inset]; 760 pt = [width - inset, height - inset];
723 rad = brRad - inset; 761 rad = brRad - inset;
724 if (rad < 0) rad = 0; 762 if (rad < 0) rad = 0;
725 pt[0] -= rad; 763 pt[0] -= rad;
726 ctx.lineTo( pt[0], pt[1] ); 764 ctx.lineTo( pt[0]+xOff, pt[1]+yOff );
727 765
728 // get the bottom right arc 766 // get the bottom right arc
729 if (rad > 0.001) 767 if (rad > 0.001)
730 ctx.quadraticCurveTo( width-inset, height-inset, width-inset, height-inset-rad ); 768 ctx.quadraticCurveTo( width-inset+xOff, height-inset+yOff, width-inset+xOff, height-inset-rad+yOff );
731 769
732 // get the right of the rectangle 770 // get the right of the rectangle
733 pt = [width - inset, inset]; 771 pt = [width - inset, inset];
734 rad = trRad - inset; 772 rad = trRad - inset;
735 if (rad < 0) rad = 0; 773 if (rad < 0) rad = 0;
736 pt[1] += rad; 774 pt[1] += rad;
737 ctx.lineTo( pt[0], pt[1] ); 775 ctx.lineTo( pt[0]+xOff, pt[1]+yOff );
738 776
739 // do the top right corner 777 // do the top right corner
740 if (rad > 0.001) 778 if (rad > 0.001)
741 ctx.quadraticCurveTo( width-inset, inset, width-inset-rad, inset ); 779 ctx.quadraticCurveTo( width-inset+xOff, inset+