SphereGeometry.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { BufferGeometry } from '../core/BufferGeometry.js';
  2. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  3. import { Vector3 } from '../math/Vector3.js';
  4. class SphereGeometry extends BufferGeometry {
  5. constructor( radius = 1, widthSegments = 32, heightSegments = 16, phiStart = 0, phiLength = Math.PI * 2, thetaStart = 0, thetaLength = Math.PI ) {
  6. super();
  7. this.type = 'SphereGeometry';
  8. this.parameters = {
  9. radius: radius,
  10. widthSegments: widthSegments,
  11. heightSegments: heightSegments,
  12. phiStart: phiStart,
  13. phiLength: phiLength,
  14. thetaStart: thetaStart,
  15. thetaLength: thetaLength
  16. };
  17. widthSegments = Math.max( 3, Math.floor( widthSegments ) );
  18. heightSegments = Math.max( 2, Math.floor( heightSegments ) );
  19. const thetaEnd = Math.min( thetaStart + thetaLength, Math.PI );
  20. let index = 0;
  21. const grid = [];
  22. const vertex = new Vector3();
  23. const normal = new Vector3();
  24. // buffers
  25. const indices = [];
  26. const vertices = [];
  27. const normals = [];
  28. const uvs = [];
  29. // generate vertices, normals and uvs
  30. for ( let iy = 0; iy <= heightSegments; iy ++ ) {
  31. const verticesRow = [];
  32. const v = iy / heightSegments;
  33. // special case for the poles
  34. let uOffset = 0;
  35. if ( iy == 0 && thetaStart == 0 ) {
  36. uOffset = 0.5 / widthSegments;
  37. } else if ( iy == heightSegments && thetaEnd == Math.PI ) {
  38. uOffset = - 0.5 / widthSegments;
  39. }
  40. for ( let ix = 0; ix <= widthSegments; ix ++ ) {
  41. const u = ix / widthSegments;
  42. // vertex
  43. vertex.x = - radius * Math.cos( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
  44. vertex.y = radius * Math.cos( thetaStart + v * thetaLength );
  45. vertex.z = radius * Math.sin( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
  46. vertices.push( vertex.x, vertex.y, vertex.z );
  47. // normal
  48. normal.copy( vertex ).normalize();
  49. normals.push( normal.x, normal.y, normal.z );
  50. // uv
  51. uvs.push( u + uOffset, 1 - v );
  52. verticesRow.push( index ++ );
  53. }
  54. grid.push( verticesRow );
  55. }
  56. // indices
  57. for ( let iy = 0; iy < heightSegments; iy ++ ) {
  58. for ( let ix = 0; ix < widthSegments; ix ++ ) {
  59. const a = grid[ iy ][ ix + 1 ];
  60. const b = grid[ iy ][ ix ];
  61. const c = grid[ iy + 1 ][ ix ];
  62. const d = grid[ iy + 1 ][ ix + 1 ];
  63. if ( iy !== 0 || thetaStart > 0 ) indices.push( a, b, d );
  64. if ( iy !== heightSegments - 1 || thetaEnd < Math.PI ) indices.push( b, c, d );
  65. }
  66. }
  67. // build geometry
  68. this.setIndex( indices );
  69. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  70. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  71. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  72. }
  73. static fromJSON( data ) {
  74. return new SphereGeometry( data.radius, data.widthSegments, data.heightSegments, data.phiStart, data.phiLength, data.thetaStart, data.thetaLength );
  75. }
  76. }
  77. export { SphereGeometry, SphereGeometry as SphereBufferGeometry };
粤ICP备19079148号