47 lines
1.6 KiB
TypeScript
47 lines
1.6 KiB
TypeScript
|
|
import * as THREE from 'three';
|
||
|
|
|
||
|
|
interface SetCameraViewProps {
|
||
|
|
controls: any;
|
||
|
|
camera: THREE.Camera;
|
||
|
|
position: THREE.Vector3 | { x: number; y: number; z: number };
|
||
|
|
rotation: THREE.Euler | { x: number; y: number; z: number };
|
||
|
|
username?: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export default async function setCameraView({
|
||
|
|
controls,
|
||
|
|
camera,
|
||
|
|
position,
|
||
|
|
rotation,
|
||
|
|
username,
|
||
|
|
}: SetCameraViewProps) {
|
||
|
|
if (!controls || !camera) return;
|
||
|
|
|
||
|
|
// Normalize position
|
||
|
|
const newPosition = position instanceof THREE.Vector3
|
||
|
|
? position
|
||
|
|
: new THREE.Vector3(position.x, position.y, position.z);
|
||
|
|
|
||
|
|
// Normalize rotation
|
||
|
|
const newRotation = rotation instanceof THREE.Euler
|
||
|
|
? rotation
|
||
|
|
: new THREE.Euler(rotation.x, rotation.y, rotation.z);
|
||
|
|
|
||
|
|
// Update camera position and rotation
|
||
|
|
// camera.position.copy(newPosition);
|
||
|
|
// camera.rotation.copy(newRotation);
|
||
|
|
|
||
|
|
// If your controls need to update the target, you can optionally adjust it too
|
||
|
|
if (controls.setTarget) {
|
||
|
|
// Setting a basic target slightly forward from new position based on rotation
|
||
|
|
const cameraDirection = new THREE.Vector3(0, 0, -1).applyEuler(newRotation);
|
||
|
|
const targetPosition = new THREE.Vector3().copy(newPosition).add(cameraDirection);
|
||
|
|
|
||
|
|
// controls.setTarget(targetPosition.x, targetPosition.y, targetPosition.z);
|
||
|
|
controls?.setLookAt(...newPosition.toArray(), newPosition.x, 0, newPosition.z, true);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Optionally you can log
|
||
|
|
console.log(`Camera view updated by ${username ?? 'unknown user'}`);
|
||
|
|
}
|