Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //--------------------------------------------------------------------------------------------------------------------------
- // CL3D.MeshSceneNode.updateNormals()
- //--------------------------------------------------------------------------------------------------------------------------
- CL3D.MeshBuffer.prototype.updateNormals = function() {
- var buffer = this;
- var counter = new Array();
- //reset normals
- for (var i=0; i<buffer.Vertices.length; i++) {
- buffer.Vertices[i].Normal.X = 0;
- buffer.Vertices[i].Normal.Y = 0;
- buffer.Vertices[i].Normal.Z = 0;
- counter[i] = 0;
- }
- //calculate face normals
- for (var f=0; f<buffer.Indices.length; f+=3) {
- var a = buffer.Indices[f + 0];
- var b = buffer.Indices[f + 1];
- var c = buffer.Indices[f + 2];
- var v0 = buffer.Vertices[a];
- var v1 = buffer.Vertices[b];
- var v2 = buffer.Vertices[c];
- var ax = v1.Pos.X - v0.Pos.X;
- var ay = v1.Pos.Y - v0.Pos.Y;
- var az = v1.Pos.Z - v0.Pos.Z;
- var bx = v2.Pos.X - v1.Pos.X;
- var by = v2.Pos.Y - v1.Pos.Y;
- var bz = v2.Pos.Z - v1.Pos.Z;
- var nx = ( ay * bz ) - ( az * by );
- var ny = ( az * bx ) - ( ax * bz );
- var nz = ( ax * by ) - ( ay * bx );
- var ns = Math.sqrt( nx*nx + ny*ny + nz*nz );
- if (ns > 0) {
- nx = nx / ns;
- ny = ny / ns;
- nz = nz / ns;
- }
- buffer.Vertices[a].Normal.X += nx; buffer.Vertices[a].Normal.Y += ny; buffer.Vertices[a].Normal.Z += nz;
- buffer.Vertices[b].Normal.X += nx; buffer.Vertices[b].Normal.Y += ny; buffer.Vertices[b].Normal.Z += nz;
- buffer.Vertices[c].Normal.X += nx; buffer.Vertices[c].Normal.Y += ny; buffer.Vertices[c].Normal.Z += nz;
- counter[a] ++;
- counter[b] ++;
- counter[c] ++;
- }
- for (var i=0; i<counter.length; i++) {
- if (counter[i] < 1) continue;
- buffer.Vertices[i].Normal.X /= counter[i];
- buffer.Vertices[i].Normal.Y /= counter[i];
- buffer.Vertices[i].Normal.Z /= counter[i];
- }
- buffer.update();
- }
- //--------------------------------------------------------------------------------------------------------------------------
- // CL3D.SphereSceneNode
- //--------------------------------------------------------------------------------------------------------------------------
- CL3D.SphereSceneNode = function (size, segments) {
- if (size == null) {
- size = 10;
- }
- if (segments == null) {
- segments = 8;
- }
- this.OwnedMesh = new CL3D.Mesh();
- var c = new CL3D.MeshBuffer();
- this.OwnedMesh.AddMeshBuffer(c);
- this.addVertex = function(x,y,z,u,v) {
- return c.Vertices.push( this.createVertex(x,y,z,u,v,0,0,0,0) ) - 1;
- }
- this.addTriangle = function(m,n,o) {
- c.Indices.push(m);
- c.Indices.push(n);
- c.Indices.push(o);
- }
- if ((segments>=2) || (segments<=100)) {
- var div=(360.0/(segments*2.0));
- var height=1.0;
- var upos=1.0;
- var udiv=(1.0/(segments*2.0));
- var vdiv=(1.0/segments);
- var RotAngle=90;
- if (segments==2) { // diamond shape - no center strips
- for ( var i=1; i<=(segments*2); i++ ) {
- var np=this.addVertex(0.0,height,0.0,upos-(udiv/2.0),0);//northpole
- var sp=this.addVertex(0.0,-height,0.0,upos-(udiv/2.0),1);//southpole
- var XPos=-Math.cos(0.0175*RotAngle);
- var ZPos=Math.sin(0.0175*RotAngle);
- var v0=this.addVertex(XPos,0,ZPos,upos,0.5);
- RotAngle=RotAngle+div;
- if ( RotAngle>=360.0 ) RotAngle=RotAngle-360.0;
- XPos=-Math.cos(0.0175*RotAngle);
- ZPos=Math.sin(0.0175*RotAngle);
- upos=upos-udiv;
- var v1=this.addVertex(XPos,0,ZPos,upos,0.5);
- this.addTriangle(np,v0,v1);
- this.addTriangle(v1,v0,sp);
- }
- } else { // have center strips now
- // poles first
- for (var i=1;i<=(segments*2); i++) {
- var np=this.addVertex(0.0,height,0.0,upos-(udiv/2.0),0);//northpole
- var sp=this.addVertex(0.0,-height,0.0,upos-(udiv/2.0),1);//southpole
- var YPos=Math.cos(0.0175*div);
- var XPos=-Math.cos(0.0175*RotAngle)*(Math.sin(0.0175*div));
- var ZPos=Math.sin(0.0175*RotAngle)*(Math.sin(0.0175*div));
- var v0t=this.addVertex(XPos,YPos,ZPos,upos,vdiv);
- var v0b=this.addVertex(XPos,-YPos,ZPos,upos,1-vdiv);
- RotAngle=RotAngle+div;
- XPos=-Math.cos(0.0175*RotAngle)*(Math.sin(0.0175*div));
- ZPos=Math.sin(0.0175*RotAngle)*(Math.sin(0.0175*div));
- upos=upos-udiv;
- var v1t=this.addVertex(XPos,YPos,ZPos,upos,vdiv);
- var v1b=this.addVertex(XPos,-YPos,ZPos,upos,1-vdiv);
- this.addTriangle(np,v0t,v1t);
- this.addTriangle(v1b,v0b,sp);
- }
- // center strips
- upos=1.0;
- RotAngle=90;
- for ( var i=1 ; i<=(segments*2); i++) {
- var mult=1;
- var YPos=Math.cos(0.0175*div*(mult));
- var YPos2=Math.cos(0.0175*div*(mult+1.0));
- var Thisvdiv=vdiv
- for ( var j=1 ; j<=(segments-2); j++ ) {
- var XPos=-Math.cos(0.0175*RotAngle)*(Math.sin(0.0175*div*(mult)));
- var ZPos=Math.sin(0.0175*RotAngle)*(Math.sin(0.0175*div*(mult)));
- var XPos2=-Math.cos(0.0175*RotAngle)*(Math.sin(0.0175*div*(mult+1.0)));
- var ZPos2=Math.sin(0.0175*RotAngle)*(Math.sin(0.0175*div*(mult+1.0)));
- var v0t=this.addVertex(XPos,YPos,ZPos,upos,Thisvdiv);
- var v0b=this.addVertex(XPos2,YPos2,ZPos2,upos,Thisvdiv+vdiv);
- var tempRotAngle=RotAngle+div;
- XPos=-Math.cos(0.0175*tempRotAngle)*(Math.sin(0.0175*div*(mult)));
- ZPos=Math.sin(0.0175*tempRotAngle)*(Math.sin(0.0175*div*(mult)));
- XPos2=-Math.cos(0.0175*tempRotAngle)*(Math.sin(0.0175*div*(mult+1.0)));
- ZPos2=Math.sin(0.0175*tempRotAngle)*(Math.sin(0.0175*div*(mult+1.0)));
- var temp_upos=upos-udiv;
- var v1t=this.addVertex(XPos,YPos,ZPos,temp_upos,Thisvdiv);
- var v1b=this.addVertex(XPos2,YPos2,ZPos2,temp_upos,Thisvdiv+vdiv);
- this.addTriangle(v1t,v0t,v0b);
- this.addTriangle(v1b,v1t,v0b);
- Thisvdiv=Thisvdiv+vdiv;
- mult=mult+1;
- YPos=Math.cos(0.0175*div*(mult));
- YPos2=Math.cos(0.0175*div*(mult+1.0));
- }
- upos=upos-udiv;
- RotAngle=RotAngle+div;
- }
- c.updateNormals();
- }
- }
- for (var d = 0; d < c.Vertices.length; ++d) {
- var a = c.Vertices[d].Pos;
- a.multiplyThisWithScal(size);
- //a.X -= size * 0.5;
- //a.Y -= size * 0.5;
- //a.Z -= size * 0.5
- }
- c.update();
- c.recalculateBoundingBox();
- this.OwnedMesh.Box = c.Box.clone();
- this.init()
- };
- CL3D.SphereSceneNode.prototype = new CL3D.MeshSceneNode();
- CL3D.SphereSceneNode.prototype.createVertex = function (g, f, e, h, i, j, d, c, b) {
- var a = new CL3D.Vertex3D(true);
- a.Pos.X = g;
- a.Pos.Y = f;
- a.Pos.Z = e;
- a.Normal.X = d;
- a.Normal.Y = c;
- a.Normal.Z = b;
- a.TCoords.X = h;
- a.TCoords.Y = i;
- return a
- };
- CL3D.SphereSceneNode.prototype.createClone = function (a) {
- var b = new CL3D.SphereSceneNode();
- this.cloneMembers(b, a);
- b.OwnedMesh = this.OwnedMesh;
- b.ReadonlyMaterials = this.ReadonlyMaterials;
- b.DoesCollision = this.DoesCollision;
- if (this.Box) {
- b.Box = this.Box.clone()
- }
- return b
- };
- //----------
Advertisement
Add Comment
Please, Sign In to add comment