SphereGeometry.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. */
  4. THREE.SphereGeometry = function ( radius, segmentsWidth, segmentsHeight, phiStart, phiLength, thetaStart, thetaLength ) {
  5. THREE.Geometry.call( this );
  6. var radius = radius || 50;
  7. var segmentsX = segmentsWidth || 8;
  8. var segmentsY = segmentsHeight || 6;
  9. var phiStart = phiStart != undefined ? phiStart : 0;
  10. var phiLength = phiLength != undefined ? phiLength : Math.PI * 2;
  11. var thetaStart = thetaStart != undefined ? thetaStart : 0;
  12. var thetaLength = thetaLength != undefined ? thetaLength : Math.PI;
  13. var x, y, vertices = [], uvs = [];
  14. for ( y = 0; y <= segmentsY; y ++ ) {
  15. var verticesRow = [];
  16. var uvsRow = [];
  17. for ( x = 0; x <= segmentsX; x ++ ) {
  18. var u = x / segmentsX;
  19. var v = y / segmentsY;
  20. var xpos = - radius * Math.cos( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
  21. var ypos = radius * Math.cos( thetaStart + v * thetaLength );
  22. var zpos = radius * Math.sin( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
  23. this.vertices.push( new THREE.Vertex( new THREE.Vector3( xpos, ypos, zpos ) ) );
  24. verticesRow.push( this.vertices.length - 1 );
  25. uvsRow.push( new THREE.UV( u, v ) );
  26. }
  27. vertices.push( verticesRow );
  28. uvs.push( uvsRow );
  29. }
  30. for ( y = 0; y < segmentsY; y ++ ) {
  31. for ( x = 0; x < segmentsX; x ++ ) {
  32. var v1 = vertices[ y ][ x + 1 ];
  33. var v2 = vertices[ y ][ x ];
  34. var v3 = vertices[ y + 1 ][ x ];
  35. var v4 = vertices[ y + 1 ][ x + 1 ];
  36. var n1 = this.vertices[ v1 ].position.clone().normalize();
  37. var n2 = this.vertices[ v2 ].position.clone().normalize();
  38. var n3 = this.vertices[ v3 ].position.clone().normalize();
  39. var n4 = this.vertices[ v4 ].position.clone().normalize();
  40. var uv1 = uvs[ y ][ x + 1 ].clone();
  41. var uv2 = uvs[ y ][ x ].clone();
  42. var uv3 = uvs[ y + 1 ][ x ].clone();
  43. var uv4 = uvs[ y + 1 ][ x + 1 ].clone();
  44. if ( Math.abs( this.vertices[ v1 ].position.y ) == radius ) {
  45. this.faces.push( new THREE.Face3( v1, v3, v4, [ n1, n3, n4 ] ) );
  46. this.faceVertexUvs[ 0 ].push( [ uv1, uv3, uv4 ] );
  47. } else if ( Math.abs( this.vertices[ v3 ].position.y ) == radius ) {
  48. this.faces.push( new THREE.Face3( v1, v2, v3, [ n1, n2, n3 ] ) );
  49. this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv3 ] );
  50. } else {
  51. this.faces.push( new THREE.Face4( v1, v2, v3, v4, [ n1, n2, n3, n4 ] ) );
  52. this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv3, uv4 ] );
  53. }
  54. }
  55. }
  56. this.computeCentroids();
  57. this.computeFaceNormals();
  58. this.boundingSphere = { radius: radius };
  59. };
  60. THREE.SphereGeometry.prototype = new THREE.Geometry();
  61. THREE.SphereGeometry.prototype.constructor = THREE.SphereGeometry;