import React, { useRef, useEffect } from "react"; import { useThree } from "@react-three/fiber"; import { PerspectiveCamera, OrthographicCamera } from "@react-three/drei"; import * as THREE from "three"; import { useStore } from "../store/store.ts"; const CameraControllerInner: React.FC = () => { const cameraView = useStore((state) => state.cameraView); const { set, size } = useThree(); const perspectiveRef = useRef(null); const orthoRef = useRef(null); useEffect(() => { if (cameraView === "perspective") { if (perspectiveRef.current) { set({ camera: perspectiveRef.current }); } return; } if (orthoRef.current) { const camera = orthoRef.current; const aspect = size.width / size.height; const frustum = 150; camera.left = -frustum * aspect; camera.right = frustum * aspect; camera.top = frustum; camera.bottom = -frustum; camera.near = 0.1; camera.far = 2000; switch (cameraView) { case "ortho-top": camera.position.set(0, 500, 0); camera.lookAt(0, 0, 0); break; case "ortho-front": camera.position.set(0, 50, -500); camera.lookAt(0, 50, 0); break; case "ortho-side": camera.position.set(-500, 50, 0); camera.lookAt(0, 50, 0); break; } camera.updateProjectionMatrix(); set({ camera: camera }); } }, [cameraView, set, size]); return ( <> ); }; export const CameraController = React.memo(CameraControllerInner); CameraController.displayName = "CameraController";