feat: controls

This commit is contained in:
TrèsAbhi committed 2024-01-03 12:21:30 -06:00
1 parent ff66d99fc6
commit b745ad2fa7
2 files changed
+35

No files matched your search

@@ -0,0 +1,24 @@
import { OrbitControls } from '@react-three/drei';
import { useThree } from '@react-three/fiber';
import { RefObject, useEffect, useRef } from 'react';
import { Box3, Object3D } from 'three';
import { OrbitControls as OrbitControlsClass } from 'three-stdlib';
interface ControlsProps {
enabled: boolean;
fit: RefObject<Object3D>;
}
export function Controls({ enabled, fit }: ControlsProps) {
const camera = useThree((state) => state.camera);
const orbitControls = useRef<OrbitControlsClass>(null);
useEffect(() => {
const box = new Box3().setFromObject(fit.current!);
orbitControls.current?.target.set(0, (box.min.y + box.max.y) / 2, 0);
camera.position.set(-5, (box.min.y + box.max.y) / 2, -15);
}, [fit.current]);
return <OrbitControls ref={orbitControls} enabled={enabled} />;
}