2025-07-31 13:38:50 +05:30
|
|
|
import { useEffect } from "react";
|
2025-06-10 15:28:23 +05:30
|
|
|
import { useThree } from "@react-three/fiber";
|
2025-07-31 13:38:50 +05:30
|
|
|
import * as THREE from 'three';
|
|
|
|
|
import { PerspectiveCamera, OrthographicCamera, CameraControls } from '@react-three/drei';
|
2025-06-10 15:28:23 +05:30
|
|
|
import { useParams } from "react-router-dom";
|
2025-07-31 13:38:50 +05:30
|
|
|
import * as CONSTANTS from '../../../types/world/worldConstants';
|
|
|
|
|
import { getCamera } from "../../../services/factoryBuilder/camera/getCameraApi";
|
2025-06-23 09:37:53 +05:30
|
|
|
import { getUserData } from "../../../functions/getUserData";
|
2025-07-31 13:38:50 +05:30
|
|
|
import { useToggleView } from "../../../store/builder/store";
|
2025-06-10 15:28:23 +05:30
|
|
|
|
|
|
|
|
export default function SwitchView() {
|
2025-07-31 13:38:50 +05:30
|
|
|
const { toggleView } = useToggleView();
|
|
|
|
|
const { controls } = useThree();
|
|
|
|
|
const { projectId } = useParams();
|
|
|
|
|
const { organization } = getUserData();
|
2025-06-10 15:28:23 +05:30
|
|
|
|
2025-07-31 13:38:50 +05:30
|
|
|
useEffect(() => {
|
|
|
|
|
if (toggleView && controls) {
|
|
|
|
|
(controls as any).mouseButtons.left = CONSTANTS.twoDimension.leftMouse;
|
|
|
|
|
(controls as any).mouseButtons.right = CONSTANTS.twoDimension.rightMouse;
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
getCamera(organization, localStorage.getItem('userId')!, projectId).then((data) => {
|
|
|
|
|
if (data && data.position && data.target) {
|
|
|
|
|
(controls as CameraControls)?.setPosition(data.position.x, data.position.y, data.position.z);
|
|
|
|
|
(controls as CameraControls)?.setTarget(data.target.x, data.target.y, data.target.z);
|
|
|
|
|
} else {
|
|
|
|
|
(controls as CameraControls)?.setPosition(...CONSTANTS.threeDimension.defaultPosition);
|
|
|
|
|
(controls as CameraControls)?.setTarget(...CONSTANTS.threeDimension.defaultTarget);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
echo.error("Failed to retrieve camera position or target");
|
|
|
|
|
(controls as CameraControls)?.setPosition(...CONSTANTS.threeDimension.defaultPosition);
|
|
|
|
|
(controls as CameraControls)?.setTarget(...CONSTANTS.threeDimension.defaultTarget);
|
|
|
|
|
}
|
2025-06-10 15:28:23 +05:30
|
|
|
|
2025-07-31 13:38:50 +05:30
|
|
|
if (controls) {
|
|
|
|
|
(controls as any).mouseButtons.left = CONSTANTS.threeDimension.leftMouse;
|
|
|
|
|
(controls as any).mouseButtons.right = CONSTANTS.threeDimension.rightMouse;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, [toggleView, controls]);
|
2025-06-10 15:28:23 +05:30
|
|
|
|
2025-07-31 13:38:50 +05:30
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{toggleView ? (
|
|
|
|
|
<OrthographicCamera
|
|
|
|
|
makeDefault
|
|
|
|
|
position={CONSTANTS.twoDimension.defaultPosition}
|
|
|
|
|
zoom={10}
|
|
|
|
|
near={0.01}
|
|
|
|
|
far={1000}
|
|
|
|
|
onUpdate={(self) => self.lookAt(new THREE.Vector3(...CONSTANTS.twoDimension.defaultTarget))}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<PerspectiveCamera
|
|
|
|
|
makeDefault
|
|
|
|
|
fov={75}
|
|
|
|
|
position={CONSTANTS.threeDimension.defaultPosition}
|
|
|
|
|
near={0.01}
|
|
|
|
|
far={1000}
|
|
|
|
|
onUpdate={(self) => self.lookAt(new THREE.Vector3(...CONSTANTS.threeDimension.defaultTarget))}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
);
|
2025-06-10 15:28:23 +05:30
|
|
|
}
|