DirectionalLightShadowFixForMobileCamera.re.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import * as RE from 'rogue-engine';
  2. import * as THREE from 'three'
  3. export default class DirectionalLightShadowFixForMobileCamera extends RE.Component {
  4. initialOffset: THREE.Vector3 = new THREE.Vector3()
  5. currentOffset: THREE.Vector3 = new THREE.Vector3()
  6. getCamera(): THREE.Camera {
  7. return RE.App.currentScene.getObjectByProperty("uuid", RE.App.activeCamera) as THREE.Camera
  8. }
  9. getDirectionalLight(): THREE.DirectionalLight {
  10. return this.object3d as THREE.DirectionalLight
  11. }
  12. awake() {
  13. }
  14. start() {
  15. const directionalLight: THREE.DirectionalLight = this.getDirectionalLight()
  16. this.initialOffset.copy(new THREE.Vector3(0, 0, 0))
  17. this.initialOffset.add(directionalLight.position).normalize().multiplyScalar(100)
  18. directionalLight.target = this.getCamera()
  19. this.updatePositions()
  20. if (directionalLight.shadow) {
  21. directionalLight.shadow.normalBias = 0.04;
  22. }
  23. }
  24. update() {
  25. this.updatePositions()
  26. }
  27. updatePositions() {
  28. const directionalLight: THREE.DirectionalLight = this.getDirectionalLight()
  29. this.currentOffset.copy(this.initialOffset).add(this.getCamera().position)
  30. directionalLight.position.set(this.currentOffset.x, this.currentOffset.y, this.currentOffset.z)
  31. if(directionalLight.shadow) {
  32. directionalLight.shadow.camera.position.set(this.currentOffset.x, this.currentOffset.y, this.currentOffset.z)
  33. }
  34. }
  35. }
  36. RE.registerComponent(DirectionalLightShadowFixForMobileCamera);