Skip to content

Commit bd62ef9

Browse files
authored
simplify raycaster calculations
1 parent 1815100 commit bd62ef9

File tree

5 files changed

+17
-24
lines changed

5 files changed

+17
-24
lines changed

.changeset/strange-walls-repeat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-three-map": patch
3+
---
4+
5+
asdsd

src/core/create-events.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Events, RenderProps, RootState, createEvents as createFiberEvents } from "@react-three/fiber";
2-
import { PerspectiveCamera } from "three";
2+
import { Matrix4 } from "three";
33
import { UseBoundStore } from "zustand";
44

55
type 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 */
2323
export 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
},

stories/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"react": "^18.2.0",
1616
"react-dom": "^18.2.0",
1717
"react-map-gl": "^7.1.2",
18-
"react-three-map": "^0.3.5",
18+
"react-three-map": "^0.4.1",
1919
"three": "^0.154.0",
2020
"three-stdlib": "^2.23.13",
2121
"web-ifc-three": "^0.0.125"

stories/src/multi-coordinates.stories.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function Default() {
3737
longitude={green[0]}
3838
latitude={green[1]}
3939
>
40-
<MyBox position={[0, 1, 0]} color="green" />
40+
<MyBox position={[-2, 1, 0]} color="green" />
4141
</Coordinates>
4242
<Coordinates
4343
longitude={purple[0]}
@@ -53,6 +53,7 @@ const MyBox = ({ position, color }: { position: Vector3, color: ColorRepresentat
5353

5454
return <Box
5555
position={position}
56+
onClick={() => hover(!hovered)}
5657
onPointerOver={() => hover(true)}
5758
onPointerOut={() => hover(false)}
5859
scale={hovered ? 1.5 : 1}

stories/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3473,10 +3473,10 @@ react-refresh@^0.14.0:
34733473
resolved "https://registry.yarnpkg.com/react-refresh/-/react-refresh-0.14.0.tgz#4e02825378a5f227079554d4284889354e5f553e"
34743474
integrity sha512-wViHqhAd8OHeLS/IRMJjTSDHF3U9eWi62F/MledQGPdJGDhodXJ9PBLNGr6WWL7qlH12Mt3TyTpbS+hGXMjCzQ==
34753475

3476-
react-three-map@^0.3.5:
3477-
version "0.3.5"
3478-
resolved "https://registry.yarnpkg.com/react-three-map/-/react-three-map-0.3.5.tgz#ca35fba24a81cc84b8252b5d607be390be069b95"
3479-
integrity sha512-BVji1OgyEf7RxMWuYWuOl3k3Ro1wxJ+NZ+tBtWbzl1jp5QIKkQnOZ+cJOKA5XFr/6OMi2xI8yY9g2WFp2TlMtA==
3476+
react-three-map@^0.4.1:
3477+
version "0.4.1"
3478+
resolved "https://registry.yarnpkg.com/react-three-map/-/react-three-map-0.4.1.tgz#4849aba8d907f1a0782c269f623f81675d144fbb"
3479+
integrity sha512-ctVVZwhkDH33yjM2r8zQ2URXxK3OimuJJgwTDJBEmuhRMTwqqseGvPPC/enqXhI2KWeGmUKySSeonpDq2LjB4Q==
34803480

34813481
react-use-measure@^2.1.1:
34823482
version "2.1.1"

0 commit comments

Comments
 (0)