RingGeometry.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * @author Kaleb Murphy
  3. * @author Mugen87 / https://github.com/Mugen87
  4. */
  5. import { Geometry } from '../core/Geometry.js';
  6. import { BufferGeometry } from '../core/BufferGeometry.js';
  7. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  8. import { Vector2 } from '../math/Vector2.js';
  9. import { Vector3 } from '../math/Vector3.js';
  10. // RingGeometry
  11. function RingGeometry( innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength ) {
  12. Geometry.call( this );
  13. this.type = 'RingGeometry';
  14. this.parameters = {
  15. innerRadius: innerRadius,
  16. outerRadius: outerRadius,
  17. thetaSegments: thetaSegments,
  18. phiSegments: phiSegments,
  19. thetaStart: thetaStart,
  20. thetaLength: thetaLength
  21. };
  22. this.fromBufferGeometry( new RingBufferGeometry( innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength ) );
  23. this.mergeVertices();
  24. }
  25. RingGeometry.prototype = Object.create( Geometry.prototype );
  26. RingGeometry.prototype.constructor = RingGeometry;
  27. // RingBufferGeometry
  28. function RingBufferGeometry( innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength ) {
  29. BufferGeometry.call( this );
  30. this.type = 'RingBufferGeometry';
  31. this.parameters = {
  32. innerRadius: innerRadius,
  33. outerRadius: outerRadius,
  34. thetaSegments: thetaSegments,
  35. phiSegments: phiSegments,
  36. thetaStart: thetaStart,
  37. thetaLength: thetaLength
  38. };
  39. innerRadius = innerRadius || 0.5;
  40. outerRadius = outerRadius || 1;
  41. thetaStart = thetaStart !== undefined ? thetaStart : 0;
  42. thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2;
  43. thetaSegments = thetaSegments !== undefined ? Math.max( 3, thetaSegments ) : 8;
  44. phiSegments = phiSegments !== undefined ? Math.max( 1, phiSegments ) : 1;
  45. // buffers
  46. var indices = [];
  47. var vertices = [];
  48. var normals = [];
  49. var uvs = [];
  50. // some helper variables
  51. var segment;
  52. var radius = innerRadius;
  53. var radiusStep = ( ( outerRadius - innerRadius ) / phiSegments );
  54. var vertex = new Vector3();
  55. var uv = new Vector2();
  56. var j, i;
  57. // generate vertices, normals and uvs
  58. for ( j = 0; j <= phiSegments; j ++ ) {
  59. for ( i = 0; i <= thetaSegments; i ++ ) {
  60. // values are generate from the inside of the ring to the outside
  61. segment = thetaStart + i / thetaSegments * thetaLength;
  62. // vertex
  63. vertex.x = radius * Math.cos( segment );
  64. vertex.y = radius * Math.sin( segment );
  65. vertices.push( vertex.x, vertex.y, vertex.z );
  66. // normal
  67. normals.push( 0, 0, 1 );
  68. // uv
  69. uv.x = ( vertex.x / outerRadius + 1 ) / 2;
  70. uv.y = ( vertex.y / outerRadius + 1 ) / 2;
  71. uvs.push( uv.x, uv.y );
  72. }
  73. // increase the radius for next row of vertices
  74. radius += radiusStep;
  75. }
  76. // indices
  77. for ( j = 0; j < phiSegments; j ++ ) {
  78. var thetaSegmentLevel = j * ( thetaSegments + 1 );
  79. for ( i = 0; i < thetaSegments; i ++ ) {
  80. segment = i + thetaSegmentLevel;
  81. var a = segment;
  82. var b = segment + thetaSegments + 1;
  83. var c = segment + thetaSegments + 2;
  84. var d = segment + 1;
  85. // faces
  86. indices.push( a, b, d );
  87. indices.push( b, c, d );
  88. }
  89. }
  90. // build geometry
  91. this.setIndex( indices );
  92. this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  93. this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  94. this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  95. }
  96. RingBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
  97. RingBufferGeometry.prototype.constructor = RingBufferGeometry;
  98. export { RingGeometry, RingBufferGeometry };