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