CylinderDistortionComponent.re.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import * as RE from 'rogue-engine'
  2. import * as THREE from 'three'
  3. export default class CylinderDistortionComponent extends RE.Component {
  4. awake() {
  5. this.distort()
  6. }
  7. start() {
  8. }
  9. update() {
  10. }
  11. distort() {
  12. const positions = (this.object3d as any).geometry.getAttribute("position")
  13. const normals = (this.object3d as any).geometry.getAttribute("normal")
  14. const uv = (this.object3d as any).geometry.getAttribute("uv")
  15. const pos = new THREE.Vector3(0,0,0)
  16. const norm = new THREE.Vector3(0,0,0)
  17. const uvPoint = new THREE.Vector2(0,0)
  18. const alteredPosition = new THREE.Vector3(0,0,0)
  19. for (let i = 0; i < (positions.array.length / positions.itemSize); i++) {
  20. const offset = i * positions.itemSize
  21. pos.set(positions.array[offset], positions.array[offset + 1], positions.array[offset + 2])
  22. norm.set(normals.array[offset], normals.array[offset + 1], normals.array[offset + 2])
  23. const uvOffset = i * uv.itemSize
  24. uvPoint.set(uv.array[uvOffset], uv.array[uvOffset + 1])
  25. alteredPosition.set(pos.x, pos.y, pos.z)
  26. const modifier = 1-Math.cos(pos.y / 12)
  27. alteredPosition.add(norm.multiplyScalar((12 * modifier) * Math.random()))
  28. positions.array[offset] = alteredPosition.x
  29. positions.array[offset + 1] = alteredPosition.y
  30. positions.array[offset + 2] = alteredPosition.z
  31. }
  32. }
  33. }
  34. RE.registerComponent(CylinderDistortionComponent);