Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import { useCallback, useMemo } from 'react';
import { useApp } from '@playcanvas/react/hooks';
import { Vec3 } from 'playcanvas';
export const useCameraPosition = () => {
const app = useApp();
const setCamera = useCallback(
(focus: [number, number, number], position?: [number, number, number], horizontalNdcBias = 0) => {
const camera = app.root.findByName('camera');
// @ts-expect-error - scripts are added dynamically to the camera entity
const controls = camera?.script?.cameraControls ?? camera?.script?.walkthroughCamera;
if (controls && camera) {
// The extra bias arg is only honored by WalkthroughCamera; CameraControls ignores it.
controls.reset(new Vec3(focus), position ? new Vec3(position) : camera.getPosition(), horizontalNdcBias);
}
},
[app]
);
const getCameraState = useCallback(() => {
const camera = app.root.findByName('camera');
// @ts-expect-error - scripts are added dynamically to the camera entity
const controls = camera?.script?.cameraControls ?? camera?.script?.walkthroughCamera;
if (camera && controls) {
const position = camera.getPosition();
const focusPoint = controls?.focusPoint;
return {
position: [position.x, position.y, position.z] as [number, number, number],
focus: [focusPoint.x, focusPoint.y, focusPoint.z] as [number, number, number],
};
}
return null;
}, [app]);
return useMemo(() => ({ setCamera, getCameraState }), [setCamera, getCameraState]);
};
|