Open History to see the last commit
1import * as THREE from "three";
2import { UNIT, type Position } from "../shared/types";
3
4export const CURSOR_TTL = 3000;
5
6// Track real scene surfaces, independently of placement snapping and drag handles.
7export function installCursorTracking(
8 canvas: HTMLCanvasElement,
9 camera: THREE.Camera,
10 scene: THREE.Scene,
11 onCursor: (cursor: Position | null) => void,
12 keys: Window = window,
13 page: Document = document,
14) {
15 const raycaster = new THREE.Raycaster();
16 const pointer = new THREE.Vector2();
17 let location: { x: number; y: number } | null = null;
18 let dirty = true;
19 const cameraPose = new THREE.Matrix4();
20 const projection = new THREE.Matrix4();
21 let viewport = "";
22 let signature = "";
23 const emit = (point: Position | null) => {
24 const next = JSON.stringify(point);
25 if (next === signature) return;
26 signature = next;
27 onCursor(point);
28 };
29 const clear = () => {
30 location = null;
31 emit(null);
32 };
33 const move = (e: PointerEvent) => {
34 if (e.pointerType === "touch") return clear();
35 const rect = canvas.getBoundingClientRect();
36 if (
37 e.clientX < rect.left ||
38 e.clientX > rect.right ||
39 e.clientY < rect.top ||
40 e.clientY > rect.bottom
41 )
42 return clear();
43 location = { x: e.clientX, y: e.clientY };
44 dirty = true;
45 };
46 const visibility = () => {
47 if (page.visibilityState !== "visible") clear();
48 };
49 canvas.addEventListener("pointermove", move, true);
50 canvas.addEventListener("pointerdown", move, true);
51 canvas.addEventListener("pointerleave", clear);
52 canvas.addEventListener("pointercancel", clear);
53 keys.addEventListener("blur", clear);
54 page.addEventListener("visibilitychange", visibility);
55 return {
56 clear,
57 invalidate() {
58 dirty = true;
59 },
60 update() {
61 if (!location) return;
62 const rect = canvas.getBoundingClientRect();
63 if (!rect.width || !rect.height) return clear();
64 const nextViewport = `${rect.left}/${rect.top}/${rect.width}/${rect.height}`;
65 if (
66 !dirty &&
67 viewport === nextViewport &&
68 cameraPose.equals(camera.matrixWorld) &&
69 projection.equals((camera as THREE.PerspectiveCamera).projectionMatrix)
70 )
71 return;
72 dirty = false;
73 viewport = nextViewport;
74 cameraPose.copy(camera.matrixWorld);
75 projection.copy((camera as THREE.PerspectiveCamera).projectionMatrix);
76 pointer.set(
77 ((location.x - rect.left) / rect.width) * 2 - 1,
78 1 - ((location.y - rect.top) / rect.height) * 2,
79 );
80 raycaster.setFromCamera(pointer, camera);
81 // Restrict picking to bricks and the board; ghosts, handles and cursors
82 // must never steal the point being shared.
83 const surfaces: THREE.Object3D[] = [];
84 scene.traverseVisible((object) => {
85 if (object.userData.partAt || object.userData.cursorSurface)
86 surfaces.push(object);
87 });
88 const hit = raycaster
89 .intersectObjects(surfaces, false)
90 .find(
91 (hit) =>
92 hit.object.userData.cursorSurface ||
93 hit.object.userData.partAt?.(hit.instanceId ?? -1),
94 );
95 emit(
96 hit
97 ? {
98 x: Math.round(hit.point.x * UNIT * 100) / 100 || 0,
99 y: Math.round(hit.point.y * UNIT * 100) / 100 || 0,
100 z: Math.round(hit.point.z * UNIT * 100) / 100 || 0,
101 }
102 : null,
103 );
104 },
105 dispose() {
106 clear();
107 canvas.removeEventListener("pointermove", move, true);
108 canvas.removeEventListener("pointerdown", move, true);
109 canvas.removeEventListener("pointerleave", clear);
110 canvas.removeEventListener("pointercancel", clear);
111 keys.removeEventListener("blur", clear);
112 page.removeEventListener("visibilitychange", visibility);
113 },
114 };
115}