Skip to content

Commit

Permalink
Merge pull request #73 from cgtuebingen/feature/20-fun-camera-positions
Browse files Browse the repository at this point in the history
Teleportation to fun locations
  • Loading branch information
Courtsilius authored Jun 29, 2024
2 parents ba037f7 + 09477fb commit de1aeab
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/components/CanvasLayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
StatsGl
} from '@react-three/drei';
import { FirstPersonControls } from './FirstPersonControls.tsx'
import TeleportControls from './TeleportControls.tsx';

import { Splat } from './Splat.tsx';
import { Leva, useControls } from 'leva';
Expand All @@ -17,6 +18,8 @@ const CanvasLayer = () => {
const handleOverlayEnter = () => setIsPointerLocked(false);
const handleOverlayLeave = () => setIsPointerLocked(true);

const teleportControlsRef = useRef();

useEffect(() => {
const checkFileExists = async () => {
try {
Expand All @@ -43,17 +46,30 @@ const CanvasLayer = () => {

const splatOptions = useControls('Admin Panel', options);

const handleTeleport = () => {
if (teleportControlsRef.current) {
// Test cooordinates
// (posX, posY, posZ, lookAtX, lookAtY, lookAtZ)
teleportControlsRef.current.teleport(0, 0, 0, 0, 2, 1);
// here you teleprot to pos 0,0,0 and look at pos 0,2,1
}
};


return (
<div className="absolute w-full h-full">
<div onMouseEnter={handleOverlayEnter} onMouseLeave={handleOverlayLeave}>
<Leva oneLineLabels />
<button onClick={handleTeleport} style={{ margin: '0px 450px', position: 'absolute', zIndex: 10 }}>
Teleport
</button>
</div>
<Canvas>
<StatsGl />
<ambientLight />
<pointLight position={[0, 0, 0]} />
<FirstPersonControls speed={splatOptions.speed} />
<TeleportControls ref={teleportControlsRef} />
{isPointerLocked && <PointerLockControls />}
{splatExists &&
<Splat position={[0, 2, 1]} src="splat.splat" /> }
Expand Down
31 changes: 31 additions & 0 deletions app/components/TeleportControls.tsx
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;

0 comments on commit de1aeab

Please sign in to comment.