open in new window

Demos : Parsing Collada

Code:

<html>
<head>
     <script type="text/javascript" src="http://lib.ivank.net/ivank.js"></script>
     <script type="text/javascript" src="http://k3d.ivank.net/K3D.js"></script>
     <script type="text/javascript">
          
          function Start()
          {
               K3D.load("sport_car/models/sport_car.dae", loaded);		// loading file ...
          }
          
          function loaded(data)
          {
               var m = K3D.parse.fromCollada(data);	// done !
               console.log(m);
               
               var stage = new Stage("c");  
               var s = new Sprite(); 
               stage.addChild(s);
               
               s.x = stage.stageWidth/2; 
               s.y = stage.stageHeight/2;
               s.z = 400;
               s.scaleX = s.scaleY = s.scaleZ = 2.5;
               s.rotationX = 90;
               
               //  my model uses several images
               var imgs = {};
               for(var n in m.images) imgs[n] = new BitmapData("sport_car/models/" + m.images[n]);
               
               for(var i=0; i<m.geometries.length; i++)
               {
                    var g = m.geometries[i];
                    for(var j=0; j<g.triangles.length; j++)
                    {
                    	var t = g.triangles[j];
                    	
                    	var mat = m.materials[t.material];
                    	var eff = m.effects[mat];
                    	if(eff.surface == null) continue;	// skip meshes without texture
                    	
                    	var verts = K3D.edit.unwrap(t.i_VERTEX  , g.sources[t.s_VERTEX  ], 3);
                    	var uvt   = K3D.edit.unwrap(t.i_TEXCOORD, g.sources[t.s_TEXCOORD], 2);
                    	for(var k=1; k<uvt.length; k+=2) uvt[k] = 1 - uvt[k];
                    	var inds  = [];
                    	for(var k=0; k<t.i_VERTEX.length; k++) inds.push(k);
                    	
                    	s.graphics.beginBitmapFill(imgs[eff.surface]);
                    	s.graphics.drawTriangles3D(verts, inds, uvt);
                    }
               }
               
               stage.addEventListener(Event.ENTER_FRAME, 
                    function(e) {   s.rotationY += 0.01*(stage.mouseX - stage.stageWidth/2);  
                                    s.rotationX += 0.01*(stage.mouseY - stage.stageHeight/2); } );
          }
     </script>
</head>
<body onload="Start();"><canvas id="c"></canvas></body>
</html>