DirectionalLightShadowFixForMobileCamera.re.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. private _normalBias = 0.04;
  7. get light() {
  8. return this.object3d as THREE.DirectionalLight
  9. }
  10. get camera() {
  11. return RE.App.currentScene.getObjectByProperty("uuid", RE.App.activeCamera) as THREE.Camera
  12. }
  13. get shadow() {
  14. return this.light.shadow;
  15. }
  16. @RE.props.num()
  17. get normalBias() {
  18. return this._normalBias;
  19. }
  20. set normalBias(value: number) {
  21. this._normalBias = value;
  22. if (this.shadow) { this.shadow.normalBias = value; }
  23. }
  24. awake() {
  25. }
  26. start() {
  27. this.initialOffset.copy(this.light.position)
  28. this.initialOffset.sub(new THREE.Vector3(0, 0, 0))
  29. this.updatePositions()
  30. this.light.target = this.camera
  31. }
  32. update() {
  33. this.updatePositions()
  34. }
  35. updatePositions() {
  36. this.currentOffset.copy(this.initialOffset).add(this.camera.position)
  37. this.light.position.set(this.currentOffset.x, this.currentOffset.y, this.currentOffset.z)
  38. if(this.shadow) {
  39. this.shadow.camera.position.set(this.currentOffset.x, this.currentOffset.y, this.currentOffset.z)
  40. }
  41. }
  42. }
  43. RE.registerComponent(DirectionalLightShadowFixForMobileCamera);