CircleGeometry.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { Geometry } from '../core/Geometry.js';
  2. import { BufferGeometry } from '../core/BufferGeometry.js';
  3. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  4. import { Vector3 } from '../math/Vector3.js';
  5. import { Vector2 } from '../math/Vector2.js';
  6. // CircleGeometry
  7. class CircleGeometry extends Geometry {
  8. constructor( radius, segments, thetaStart, thetaLength ) {
  9. super();
  10. this.type = 'CircleGeometry';
  11. this.parameters = {
  12. radius: radius,
  13. segments: segments,
  14. thetaStart: thetaStart,
  15. thetaLength: thetaLength
  16. };
  17. this.fromBufferGeometry( new CircleBufferGeometry( radius, segments, thetaStart, thetaLength ) );
  18. this.mergeVertices();
  19. }
  20. }
  21. // CircleBufferGeometry
  22. class CircleBufferGeometry extends BufferGeometry {
  23. constructor( radius, segments, thetaStart, thetaLength ) {
  24. super();
  25. this.type = 'CircleBufferGeometry';
  26. this.parameters = {
  27. radius: radius,
  28. segments: segments,
  29. thetaStart: thetaStart,
  30. thetaLength: thetaLength
  31. };
  32. radius = radius || 1;
  33. segments = segments !== undefined ? Math.max( 3, segments ) : 8;
  34. thetaStart = thetaStart !== undefined ? thetaStart : 0;
  35. thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2;
  36. // buffers
  37. const indices = [];
  38. const vertices = [];
  39. const normals = [];
  40. const uvs = [];
  41. // helper variables
  42. const vertex = new Vector3();
  43. const uv = new Vector2();
  44. // center point
  45. vertices.push( 0, 0, 0 );
  46. normals.push( 0, 0, 1 );
  47. uvs.push( 0.5, 0.5 );
  48. for ( let s = 0, i = 3; s <= segments; s ++, i += 3 ) {
  49. const segment = thetaStart + s / segments * thetaLength;
  50. // vertex
  51. vertex.x = radius * Math.cos( segment );
  52. vertex.y = radius * Math.sin( segment );
  53. vertices.push( vertex.x, vertex.y, vertex.z );
  54. // normal
  55. normals.push( 0, 0, 1 );
  56. // uvs
  57. uv.x = ( vertices[ i ] / radius + 1 ) / 2;
  58. uv.y = ( vertices[ i + 1 ] / radius + 1 ) / 2;
  59. uvs.push( uv.x, uv.y );
  60. }
  61. // indices
  62. for ( let i = 1; i <= segments; i ++ ) {
  63. indices.push( i, i + 1, 0 );
  64. }
  65. // build geometry
  66. this.setIndex( indices );
  67. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  68. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  69. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  70. }
  71. }
  72. export { CircleGeometry, CircleBufferGeometry };