CircleGeometry.js 1.9 KB

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