import { Camera } from '@babylonjs/core/Cameras/camera.js' ; import { Scene } from '@babylonjs/core/scene.js' ; import { Nullable } from '@babylonjs/core/types.js' ; import { useContext , useEffect , useRef } from 'react' ; import { SceneContext } from './scene' ; param createCameraFn param autoAttach param noPreventDefault param useCtrlForPanning param panningMoustButton export const useCamera = < T extends Camera > ( createCameraFn , autoAttach = true , noPreventDefault = true ) : Nullable < T > => { const { scene } = useContext ( SceneContext ) ; const cameraRef = useRef < Nullable < T > > ( null ) ; useEffect ( ( ) => { if ( scene === null ) { console . warn ( 'cannot create camera (scene not ready)' ) ; return ; } const camera = createCameraFn ( scene ) ; if ( autoAttach === true ) { const canvas = scene . getEngine ( ) ! . getRenderingCanvas ( ) ! ; camera . attachControl ( canvas , noPreventDefault ) ; } cameraRef . current = camera ; return ( ) => { if ( autoAttach === true ) { const canvas = scene . getEngine ( ) ! . getRenderingCanvas ( ) ! ; camera . detachControl ( canvas ) ; } camera . dispose ( ) ; } ; } , [ scene ] ) ; return cameraRef . current ; } ;