CircularSectorBuilder.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * CircularSectorBuilder.js
  3. *
  4. * @author realor
  5. */
  6. import { CircleBuilder } from './CircleBuilder.js'
  7. import { ObjectBuilder } from './ObjectBuilder.js'
  8. import { ProfileBuilder } from './ProfileBuilder.js'
  9. import { ProfileGeometry } from '../core/ProfileGeometry.js'
  10. import * as THREE from '../lib/three.module.js'
  11. class CircularSectorBuilder extends CircleBuilder {
  12. constructor(radius = 1, angle = 180, segments = 32) {
  13. super(radius, segments)
  14. this.angle = angle
  15. }
  16. performBuild(profile) {
  17. if (this.angle < 0) throw 'Unsupported negative angle'
  18. const shape = new THREE.Shape()
  19. if (this.angle >= 360) {
  20. this.drawCircle(shape, this.radius, this.segments)
  21. } else {
  22. this.drawSector(shape, this.radius, this.angle, this.segments)
  23. }
  24. profile.updateGeometry(new ProfileGeometry(shape))
  25. return true
  26. }
  27. drawSector(path, radius = 1, angle = 180, segments = 32) {
  28. const steps = Math.ceil((segments * angle) / 360)
  29. const angleRad = THREE.MathUtils.degToRad(angle)
  30. const incr = angleRad / steps
  31. path.moveTo(0, 0)
  32. for (let i = 0; i <= steps; i++) {
  33. let rad = incr * i
  34. path.lineTo(radius * Math.cos(rad), radius * Math.sin(rad))
  35. }
  36. path.closePath()
  37. }
  38. copy(source) {
  39. this.radius = source.radius
  40. this.segments = source.segments
  41. this.angle = source.angle
  42. return this
  43. }
  44. }
  45. ObjectBuilder.addClass(CircularSectorBuilder)
  46. export { CircularSectorBuilder }