UNPKG

@threlte/core

Version:

A 3D framework for the web, built on top of Svelte and Three.js

44 lines (43 loc) 1.45 kB
import { fromStore } from 'svelte/store'; import { useThrelte } from '../../../context/compounds/useThrelte.js'; import { isInstanceOf } from '../../../utilities/index.js'; const defaultCameras = new Set(); export const useCamera = (getCamera, getManual, getMakeDefault) => { const { invalidate, size: sizeStore, camera: defaultCamera } = useThrelte(); const camera = $derived(getCamera()); const size = fromStore(sizeStore); $effect.pre(() => { if (!getMakeDefault()) { return; } const current = camera; defaultCameras.add(current); defaultCamera.set(current); invalidate(); return () => { defaultCameras.delete(current); if (defaultCameras.size === 0) { defaultCamera.set(undefined); invalidate(); } }; }); $effect.pre(() => { if (getManual()) { return; } const { width, height } = size.current; if (isInstanceOf(camera, 'PerspectiveCamera')) { camera.aspect = width / height; } else if (isInstanceOf(camera, 'OrthographicCamera')) { camera.left = width / -2; camera.right = width / 2; camera.top = height / 2; camera.bottom = height / -2; } camera.updateProjectionMatrix(); camera.updateMatrixWorld(); invalidate(); }); };