-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from cgtuebingen/feature/20-fun-camera-positions
Teleportation to fun locations
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React, { useImperativeHandle, useState } from 'react'; | ||
import * as THREE from 'three'; | ||
import { useThree, useFrame } from '@react-three/fiber'; | ||
|
||
const TeleportControls = React.forwardRef((props, ref) => { | ||
const { camera } = useThree(); | ||
const [teleportData, setTeleportData] = useState<{ | ||
position: THREE.Vector3; | ||
lookAt: THREE.Vector3; | ||
} | null>(null); | ||
|
||
useImperativeHandle(ref, () => ({ | ||
teleport: (x, y, z, lookAtX, lookAtY, lookAtZ) => { | ||
const position = new THREE.Vector3(x, y, z); | ||
const lookAt = new THREE.Vector3(lookAtX, lookAtY, lookAtZ); | ||
setTeleportData({ position, lookAt }); | ||
} | ||
})); | ||
|
||
useFrame(() => { | ||
if (teleportData) { | ||
camera.position.copy(teleportData.position); | ||
camera.lookAt(teleportData.lookAt); | ||
setTeleportData(null); | ||
} | ||
}); | ||
|
||
return null; | ||
}); | ||
|
||
export default TeleportControls; |