-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(scene): fix transform controls being clickable
- Loading branch information
Showing
4 changed files
with
76 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 29 additions & 8 deletions
37
packages/scene-composer/src/hooks/useOverwriteRaycaster.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,38 @@ | ||
import { useCallback } from 'react'; | ||
import { useCallback, useEffect, useRef } from 'react'; | ||
import { computeMikkTSpaceTangents } from 'three/examples/jsm/utils/BufferGeometryUtils'; | ||
|
||
const useOverwriteRaycaster: (object: THREE.Object3D, raycaster?: () => void) => () => void = ( | ||
object: THREE.Object3D, | ||
const useOverwriteRaycaster: ( | ||
object: THREE.Object3D | undefined, | ||
raycaster?: () => void, | ||
) => { | ||
) => [() => void, () => void] = (object: THREE.Object3D | undefined, raycaster?: () => void) => { | ||
const originalRaycastMap = useRef({}); | ||
|
||
useEffect(() => { | ||
if (object) { | ||
object.traverse((o: THREE.Object3D) => { | ||
originalRaycastMap.current[o.uuid] = o.raycast; | ||
}); | ||
} | ||
}, [object]); | ||
|
||
const overwriteRaycaster = useCallback(() => { | ||
const raycastOverride = raycaster ? raycaster : () => {}; | ||
object.traverse((object: THREE.Object3D) => { | ||
object.raycast = raycastOverride; | ||
}); | ||
if (object) { | ||
object.traverse((o: THREE.Object3D) => { | ||
o.raycast = raycastOverride; | ||
}); | ||
} | ||
}, [object, raycaster]); | ||
|
||
const restoreRaycaster = useCallback(() => { | ||
if (object) { | ||
object.traverse((o: THREE.Object3D) => { | ||
o.raycast = originalRaycastMap.current[o.uuid]; | ||
}); | ||
} | ||
}, [object]); | ||
|
||
return overwriteRaycaster; | ||
return [overwriteRaycaster, restoreRaycaster]; | ||
}; | ||
|
||
export default useOverwriteRaycaster; |