Guest User

CopperLicht:: SphereSceneNode

a guest
Jan 14th, 2012
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //--------------------------------------------------------------------------------------------------------------------------        
  2. //                                      CL3D.MeshSceneNode.updateNormals()
  3. //--------------------------------------------------------------------------------------------------------------------------
  4.  
  5.         CL3D.MeshBuffer.prototype.updateNormals = function() {
  6.             var buffer = this;
  7.             var counter = new Array();
  8.             //reset normals
  9.             for (var i=0; i<buffer.Vertices.length; i++) {
  10.                 buffer.Vertices[i].Normal.X = 0;
  11.                 buffer.Vertices[i].Normal.Y = 0;
  12.                 buffer.Vertices[i].Normal.Z = 0;
  13.                 counter[i] = 0;
  14.             }
  15.             //calculate face normals
  16.             for (var f=0; f<buffer.Indices.length; f+=3) {
  17.                 var a = buffer.Indices[f + 0];
  18.                 var b = buffer.Indices[f + 1];
  19.                 var c = buffer.Indices[f + 2];
  20.                 var v0 = buffer.Vertices[a];
  21.                 var v1 = buffer.Vertices[b];
  22.                 var v2 = buffer.Vertices[c];
  23.  
  24.                 var ax = v1.Pos.X - v0.Pos.X;
  25.                 var ay = v1.Pos.Y - v0.Pos.Y;
  26.                 var az = v1.Pos.Z - v0.Pos.Z;
  27.                 var bx = v2.Pos.X - v1.Pos.X;
  28.                 var by = v2.Pos.Y - v1.Pos.Y;
  29.                 var bz = v2.Pos.Z - v1.Pos.Z;
  30.                
  31.                 var nx = ( ay * bz ) - ( az * by );
  32.                 var ny = ( az * bx ) - ( ax * bz );
  33.                 var nz = ( ax * by ) - ( ay * bx );
  34.                 var ns = Math.sqrt( nx*nx + ny*ny + nz*nz );
  35.        
  36.                 if (ns > 0) {
  37.                     nx = nx / ns;
  38.                     ny = ny / ns;
  39.                     nz = nz / ns;
  40.                 }
  41.                
  42.                 buffer.Vertices[a].Normal.X += nx; buffer.Vertices[a].Normal.Y += ny; buffer.Vertices[a].Normal.Z += nz;
  43.                 buffer.Vertices[b].Normal.X += nx; buffer.Vertices[b].Normal.Y += ny; buffer.Vertices[b].Normal.Z += nz;
  44.                 buffer.Vertices[c].Normal.X += nx; buffer.Vertices[c].Normal.Y += ny; buffer.Vertices[c].Normal.Z += nz;
  45.                 counter[a] ++;
  46.                 counter[b] ++;
  47.                 counter[c] ++;
  48.                
  49.             }
  50.  
  51.             for (var i=0; i<counter.length; i++) {
  52.                 if (counter[i] < 1) continue;
  53.                 buffer.Vertices[i].Normal.X /= counter[i];
  54.                 buffer.Vertices[i].Normal.Y /= counter[i];
  55.                 buffer.Vertices[i].Normal.Z /= counter[i];
  56.             }
  57.                        
  58.             buffer.update();
  59.         }
  60.  
  61. //--------------------------------------------------------------------------------------------------------------------------        
  62. //                                      CL3D.SphereSceneNode
  63. //--------------------------------------------------------------------------------------------------------------------------        
  64.  
  65. CL3D.SphereSceneNode = function (size, segments) {
  66.     if (size == null) {
  67.         size = 10;
  68.     }
  69.     if (segments == null) {
  70.         segments = 8;
  71.     }
  72.     this.OwnedMesh = new CL3D.Mesh();
  73.     var c = new CL3D.MeshBuffer();
  74.     this.OwnedMesh.AddMeshBuffer(c);
  75.  
  76.     this.addVertex = function(x,y,z,u,v) {
  77.         return c.Vertices.push( this.createVertex(x,y,z,u,v,0,0,0,0) ) - 1;
  78.     }
  79.     this.addTriangle = function(m,n,o) {
  80.         c.Indices.push(m);
  81.         c.Indices.push(n);
  82.         c.Indices.push(o);
  83.     }
  84.    
  85.     if ((segments>=2) || (segments<=100)) {
  86.    
  87.         var div=(360.0/(segments*2.0));
  88.         var height=1.0;
  89.         var upos=1.0;
  90.         var udiv=(1.0/(segments*2.0));
  91.         var vdiv=(1.0/segments);
  92.         var RotAngle=90;
  93.  
  94.         if (segments==2) { // diamond shape - no center strips
  95.        
  96.             for ( var i=1; i<=(segments*2); i++ ) {
  97.                 var np=this.addVertex(0.0,height,0.0,upos-(udiv/2.0),0);//northpole
  98.                 var sp=this.addVertex(0.0,-height,0.0,upos-(udiv/2.0),1);//southpole
  99.                 var XPos=-Math.cos(0.0175*RotAngle);
  100.                 var ZPos=Math.sin(0.0175*RotAngle);
  101.                 var v0=this.addVertex(XPos,0,ZPos,upos,0.5);
  102.                 RotAngle=RotAngle+div;
  103.                 if ( RotAngle>=360.0 ) RotAngle=RotAngle-360.0;
  104.                 XPos=-Math.cos(0.0175*RotAngle);
  105.                 ZPos=Math.sin(0.0175*RotAngle);
  106.                 upos=upos-udiv;
  107.                 var v1=this.addVertex(XPos,0,ZPos,upos,0.5);
  108.                 this.addTriangle(np,v0,v1);
  109.                 this.addTriangle(v1,v0,sp);
  110.             }
  111.            
  112.         } else { // have center strips now
  113.        
  114.             // poles first
  115.             for (var i=1;i<=(segments*2); i++) {
  116.            
  117.                 var np=this.addVertex(0.0,height,0.0,upos-(udiv/2.0),0);//northpole
  118.                 var sp=this.addVertex(0.0,-height,0.0,upos-(udiv/2.0),1);//southpole
  119.                
  120.                 var YPos=Math.cos(0.0175*div);
  121.                
  122.                 var XPos=-Math.cos(0.0175*RotAngle)*(Math.sin(0.0175*div));
  123.                 var ZPos=Math.sin(0.0175*RotAngle)*(Math.sin(0.0175*div));
  124.                
  125.                 var v0t=this.addVertex(XPos,YPos,ZPos,upos,vdiv);
  126.                 var v0b=this.addVertex(XPos,-YPos,ZPos,upos,1-vdiv);
  127.                
  128.                 RotAngle=RotAngle+div;
  129.                
  130.                 XPos=-Math.cos(0.0175*RotAngle)*(Math.sin(0.0175*div));
  131.                 ZPos=Math.sin(0.0175*RotAngle)*(Math.sin(0.0175*div));
  132.                
  133.                 upos=upos-udiv;
  134.  
  135.                 var v1t=this.addVertex(XPos,YPos,ZPos,upos,vdiv);
  136.                 var v1b=this.addVertex(XPos,-YPos,ZPos,upos,1-vdiv);
  137.                
  138.                 this.addTriangle(np,v0t,v1t);
  139.                 this.addTriangle(v1b,v0b,sp);  
  140.                
  141.             }
  142.            
  143.             // center strips
  144.  
  145.             upos=1.0;
  146.             RotAngle=90;
  147.             for ( var i=1 ; i<=(segments*2); i++) {
  148.            
  149.                 var mult=1;
  150.                 var YPos=Math.cos(0.0175*div*(mult));
  151.                 var YPos2=Math.cos(0.0175*div*(mult+1.0));
  152.                 var Thisvdiv=vdiv
  153.                 for ( var j=1 ; j<=(segments-2); j++ ) {
  154.                    
  155.                     var XPos=-Math.cos(0.0175*RotAngle)*(Math.sin(0.0175*div*(mult)));
  156.                     var ZPos=Math.sin(0.0175*RotAngle)*(Math.sin(0.0175*div*(mult)));
  157.  
  158.                     var XPos2=-Math.cos(0.0175*RotAngle)*(Math.sin(0.0175*div*(mult+1.0)));
  159.                     var ZPos2=Math.sin(0.0175*RotAngle)*(Math.sin(0.0175*div*(mult+1.0)));
  160.                                
  161.                     var v0t=this.addVertex(XPos,YPos,ZPos,upos,Thisvdiv);
  162.                     var v0b=this.addVertex(XPos2,YPos2,ZPos2,upos,Thisvdiv+vdiv);
  163.                
  164.                     var tempRotAngle=RotAngle+div;
  165.                
  166.                     XPos=-Math.cos(0.0175*tempRotAngle)*(Math.sin(0.0175*div*(mult)));
  167.                     ZPos=Math.sin(0.0175*tempRotAngle)*(Math.sin(0.0175*div*(mult)));
  168.                    
  169.                     XPos2=-Math.cos(0.0175*tempRotAngle)*(Math.sin(0.0175*div*(mult+1.0)));
  170.                     ZPos2=Math.sin(0.0175*tempRotAngle)*(Math.sin(0.0175*div*(mult+1.0)));         
  171.                
  172.                     var temp_upos=upos-udiv;
  173.  
  174.                     var v1t=this.addVertex(XPos,YPos,ZPos,temp_upos,Thisvdiv);
  175.                     var v1b=this.addVertex(XPos2,YPos2,ZPos2,temp_upos,Thisvdiv+vdiv);
  176.                                    
  177.                     this.addTriangle(v1t,v0t,v0b);
  178.                     this.addTriangle(v1b,v1t,v0b);
  179.                    
  180.                     Thisvdiv=Thisvdiv+vdiv;
  181.                     mult=mult+1;
  182.                     YPos=Math.cos(0.0175*div*(mult));
  183.                     YPos2=Math.cos(0.0175*div*(mult+1.0));
  184.                
  185.                 }
  186.                 upos=upos-udiv;
  187.                 RotAngle=RotAngle+div;
  188.             }
  189.  
  190.             c.updateNormals();
  191.         }
  192.     }
  193.  
  194.     for (var d = 0; d < c.Vertices.length; ++d) {
  195.         var a = c.Vertices[d].Pos;
  196.         a.multiplyThisWithScal(size);
  197.         //a.X -= size * 0.5;
  198.         //a.Y -= size * 0.5;
  199.         //a.Z -= size * 0.5
  200.     }
  201.     c.update();
  202.     c.recalculateBoundingBox();
  203.     this.OwnedMesh.Box = c.Box.clone();
  204.     this.init()
  205. };
  206. CL3D.SphereSceneNode.prototype = new CL3D.MeshSceneNode();
  207. CL3D.SphereSceneNode.prototype.createVertex = function (g, f, e, h, i, j, d, c, b) {
  208.     var a = new CL3D.Vertex3D(true);
  209.     a.Pos.X = g;
  210.     a.Pos.Y = f;
  211.     a.Pos.Z = e;
  212.     a.Normal.X = d;
  213.     a.Normal.Y = c;
  214.     a.Normal.Z = b;
  215.     a.TCoords.X = h;
  216.     a.TCoords.Y = i;
  217.     return a
  218. };
  219. CL3D.SphereSceneNode.prototype.createClone = function (a) {
  220.     var b = new CL3D.SphereSceneNode();
  221.     this.cloneMembers(b, a);
  222.     b.OwnedMesh = this.OwnedMesh;
  223.     b.ReadonlyMaterials = this.ReadonlyMaterials;
  224.     b.DoesCollision = this.DoesCollision;
  225.     if (this.Box) {
  226.         b.Box = this.Box.clone()
  227.     }
  228.     return b
  229. };
  230. //----------
Advertisement
Add Comment
Please, Sign In to add comment