SphereGeometry.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. var thetaEnd = thetaStart + thetaLength;
  49. var ix, iy;
  50. var index = 0;
  51. var grid = [];
  52. var vertex = new Vector3();
  53. var normal = new Vector3();
  54. // buffers
  55. var indices = [];
  56. var vertices = [];
  57. var normals = [];
  58. var uvs = [];
  59. // generate vertices, normals and uvs
  60. for ( iy = 0; iy <= heightSegments; iy ++ ) {
  61. var verticesRow = [];
  62. var v = iy / heightSegments;
  63. // special case for the poles
  64. var uOffset = ( iy == 0 ) ? 0.5 / widthSegments : ( ( iy == heightSegments ) ? - 0.5 / widthSegments : 0 );
  65. for ( ix = 0; ix <= widthSegments; ix ++ ) {
  66. var 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 ( iy = 0; iy < heightSegments; iy ++ ) {
  83. for ( ix = 0; ix < widthSegments; ix ++ ) {
  84. var a = grid[ iy ][ ix + 1 ];
  85. var b = grid[ iy ][ ix ];
  86. var c = grid[ iy + 1 ][ ix ];
  87. var 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.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  95. this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  96. this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  97. }
  98. SphereBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
  99. SphereBufferGeometry.prototype.constructor = SphereBufferGeometry;
  100. export { SphereGeometry, SphereBufferGeometry };