SphereGeometry.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { Geometry } from '../core/Geometry.js';
  2. import { BufferGeometry } from '../core/BufferGeometry.js';
  3. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  4. import { Vector3 } from '../math/Vector3.js';
  5. // SphereGeometry
  6. function SphereGeometry( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) {
  7. Geometry.call( this );
  8. this.type = 'SphereGeometry';
  9. this.parameters = {
  10. radius: radius,
  11. widthSegments: widthSegments,
  12. heightSegments: heightSegments,
  13. phiStart: phiStart,
  14. phiLength: phiLength,
  15. thetaStart: thetaStart,
  16. thetaLength: thetaLength
  17. };
  18. this.fromBufferGeometry( new SphereBufferGeometry( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) );
  19. this.mergeVertices();
  20. }
  21. SphereGeometry.prototype = Object.create( Geometry.prototype );
  22. SphereGeometry.prototype.constructor = SphereGeometry;
  23. // SphereBufferGeometry
  24. function SphereBufferGeometry( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) {
  25. BufferGeometry.call( this );
  26. this.type = 'SphereBufferGeometry';
  27. this.parameters = {
  28. radius: radius,
  29. widthSegments: widthSegments,
  30. heightSegments: heightSegments,
  31. phiStart: phiStart,
  32. phiLength: phiLength,
  33. thetaStart: thetaStart,
  34. thetaLength: thetaLength
  35. };
  36. radius = radius || 1;
  37. widthSegments = Math.max( 3, Math.floor( widthSegments ) || 8 );
  38. heightSegments = Math.max( 2, Math.floor( heightSegments ) || 6 );
  39. phiStart = phiStart !== undefined ? phiStart : 0;
  40. phiLength = phiLength !== undefined ? phiLength : Math.PI * 2;
  41. thetaStart = thetaStart !== undefined ? thetaStart : 0;
  42. thetaLength = thetaLength !== undefined ? thetaLength : Math.PI;
  43. const thetaEnd = Math.min( thetaStart + thetaLength, Math.PI );
  44. let index = 0;
  45. const grid = [];
  46. const vertex = new Vector3();
  47. const normal = new Vector3();
  48. // buffers
  49. const indices = [];
  50. const vertices = [];
  51. const normals = [];
  52. const uvs = [];
  53. // generate vertices, normals and uvs
  54. for ( let iy = 0; iy <= heightSegments; iy ++ ) {
  55. const verticesRow = [];
  56. const v = iy / heightSegments;
  57. // special case for the poles
  58. let uOffset = 0;
  59. if ( iy == 0 && thetaStart == 0 ) {
  60. uOffset = 0.5 / widthSegments;
  61. } else if ( iy == heightSegments && thetaEnd == Math.PI ) {
  62. uOffset = - 0.5 / widthSegments;
  63. }
  64. for ( let ix = 0; ix <= widthSegments; ix ++ ) {
  65. const u = ix / widthSegments;
  66. // vertex
  67. vertex.x = - radius * Math.cos( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
  68. vertex.y = radius * Math.cos( thetaStart + v * thetaLength );
  69. vertex.z = radius * Math.sin( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
  70. vertices.push( vertex.x, vertex.y, vertex.z );
  71. // normal
  72. normal.copy( vertex ).normalize();
  73. normals.push( normal.x, normal.y, normal.z );
  74. // uv
  75. uvs.push( u + uOffset, 1 - v );
  76. verticesRow.push( index ++ );
  77. }
  78. grid.push( verticesRow );
  79. }
  80. // indices
  81. for ( let iy = 0; iy < heightSegments; iy ++ ) {
  82. for ( let ix = 0; ix < widthSegments; ix ++ ) {
  83. const a = grid[ iy ][ ix + 1 ];
  84. const b = grid[ iy ][ ix ];
  85. const c = grid[ iy + 1 ][ ix ];
  86. const d = grid[ iy + 1 ][ ix + 1 ];
  87. if ( iy !== 0 || thetaStart > 0 ) indices.push( a, b, d );
  88. if ( iy !== heightSegments - 1 || thetaEnd < Math.PI ) indices.push( b, c, d );
  89. }
  90. }
  91. // build geometry
  92. this.setIndex( indices );
  93. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  94. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  95. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  96. }
  97. SphereBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
  98. SphereBufferGeometry.prototype.constructor = SphereBufferGeometry;
  99. export { SphereGeometry, SphereBufferGeometry };