SphereGeometry.js 3.7 KB

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