SphereGeometry.js 4.0 KB

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