11import { Events , RenderProps , RootState , createEvents as createFiberEvents } from "@react-three/fiber" ;
2- import { PerspectiveCamera } from "three" ;
2+ import { Matrix4 } from "three" ;
33import { UseBoundStore } from "zustand" ;
44
55type DomEvent = PointerEvent | MouseEvent | WheelEvent ;
@@ -17,17 +17,12 @@ const DOM_EVENTS = {
1717 onLostPointerCapture : [ "lostpointercapture" , true ] ,
1818} as const ;
1919
20- const identity = [ 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 ]
20+ const projByViewInv = new Matrix4 ( )
2121
2222/** ThreeLayer event manager for MapLibre and Mapbox */
2323export function createEvents ( ) : RenderProps < HTMLCanvasElement > [ "events" ] {
2424 return ( store : UseBoundStore < RootState > ) => {
2525 const { handlePointer } = createFiberEvents ( store ) ;
26- const rayCamera = new PerspectiveCamera ( ) ;
27- rayCamera . position . z = 5
28- rayCamera . matrixAutoUpdate = false ;
29- rayCamera . matrixWorldAutoUpdate = false ;
30- rayCamera . lookAt ( 0 , 0 , 0 )
3126
3227 return {
3328 priority : 1 ,
@@ -38,21 +33,13 @@ export function createEvents(): RenderProps<HTMLCanvasElement>["events"] {
3833 state . pointer . x = ( event . offsetX / state . size . width ) * 2 - 1 ;
3934 state . pointer . y = 1 - ( event . offsetY / state . size . height ) * 2 ;
4035
41- rayCamera . copy ( state . camera as PerspectiveCamera ) ;
42-
43- const projByViewInv = rayCamera . userData . projByViewInv || identity ;
44-
45- rayCamera . matrix . identity ( ) ;
46- rayCamera . matrixWorld . identity ( ) ;
47- rayCamera . matrixWorldInverse . copy ( rayCamera . matrixWorld ) . invert ( ) ;
48- rayCamera . projectionMatrixInverse . fromArray ( projByViewInv )
49- rayCamera . projectionMatrix . fromArray ( rayCamera . userData . projByView )
36+ if ( state . camera . userData . projByViewInv ) projByViewInv . fromArray ( state . camera . userData . projByViewInv ) ;
5037
5138 state . raycaster . camera = state . camera ;
52- state . raycaster . ray . origin . setScalar ( 0 ) . applyMatrix4 ( rayCamera . projectionMatrixInverse ) ;
39+ state . raycaster . ray . origin . setScalar ( 0 ) . applyMatrix4 ( projByViewInv ) ;
5340 state . raycaster . ray . direction
5441 . set ( state . pointer . x , state . pointer . y , 1 )
55- . applyMatrix4 ( rayCamera . projectionMatrixInverse )
42+ . applyMatrix4 ( projByViewInv )
5643 . sub ( state . raycaster . ray . origin )
5744 . normalize ( ) ;
5845 } ,
0 commit comments