RingGeometry.js 3.4 KB

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