ProfileGeometry.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * ProfileGeometry.js
  3. *
  4. * @author realor
  5. */
  6. import * as THREE from '../lib/three.module.js'
  7. class ProfileGeometry extends THREE.BufferGeometry {
  8. /* a ProfileGeometry is an BufferGeometry that holds a 2D path */
  9. constructor(path, divisions = 24) {
  10. super()
  11. this.path = path // THREE.Path or THREE.Shape
  12. this.type = 'ProfileGeometry'
  13. this.divisions = divisions
  14. this.updateBuffers()
  15. }
  16. isClosed() {
  17. return this.path instanceof THREE.Shape
  18. }
  19. updateBuffers() {
  20. const path = this.path
  21. const closed = path instanceof THREE.Shape
  22. const points = path.getPoints(this.divisions) // if closed, last === first
  23. const positions = []
  24. function addPoints(points) {
  25. let length = points.length - 1
  26. for (let i = 0; i < length; i++) {
  27. let vertex = points[i]
  28. let nextVertex = points[i + 1]
  29. positions.push(vertex.x, vertex.y, 0)
  30. positions.push(nextVertex.x, nextVertex.y, 0)
  31. }
  32. }
  33. addPoints(points)
  34. if (closed) {
  35. const pointsHoles = path.getPointsHoles(this.divisions)
  36. for (let pointsHole of pointsHoles) {
  37. addPoints(pointsHole)
  38. }
  39. }
  40. this.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3))
  41. }
  42. }
  43. export { ProfileGeometry }