spi3der 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/APP.md +40 -0
  2. package/Dockerfile +4 -0
  3. package/MCP.md +149 -0
  4. package/PLAYER.md +136 -0
  5. package/README.md +35 -2
  6. package/api/mcp.mjs +31 -0
  7. package/api/store.mjs +158 -0
  8. package/bin/spi3der.mjs +20 -0
  9. package/emulator.html +17 -0
  10. package/engine/8fa668821759/bevel.js +265 -0
  11. package/engine/8fa668821759/camera.js +76 -0
  12. package/engine/8fa668821759/character.js +1019 -0
  13. package/engine/8fa668821759/check.js +83 -0
  14. package/engine/8fa668821759/csg.js +342 -0
  15. package/engine/8fa668821759/edit.js +48 -0
  16. package/engine/8fa668821759/galaxy.js +265 -0
  17. package/engine/8fa668821759/gl.js +798 -0
  18. package/engine/8fa668821759/gpu.js +1356 -0
  19. package/engine/8fa668821759/index.js +48 -0
  20. package/engine/8fa668821759/instances.js +41 -0
  21. package/engine/8fa668821759/joints.js +355 -0
  22. package/engine/8fa668821759/kernel-bytes.js +2 -0
  23. package/engine/8fa668821759/kernel.js +82 -0
  24. package/engine/8fa668821759/lathe.js +87 -0
  25. package/engine/8fa668821759/material.js +19 -0
  26. package/engine/8fa668821759/mesh.js +131 -0
  27. package/engine/8fa668821759/order.js +125 -0
  28. package/engine/8fa668821759/pick.js +85 -0
  29. package/engine/8fa668821759/place.js +17 -0
  30. package/engine/8fa668821759/planet.js +376 -0
  31. package/engine/8fa668821759/rig.js +417 -0
  32. package/engine/8fa668821759/saw.js +121 -0
  33. package/engine/8fa668821759/sky.js +428 -0
  34. package/engine/8fa668821759/svg.js +239 -0
  35. package/engine/8fa668821759/text.js +231 -0
  36. package/engine/8fa668821759/triangulate.js +72 -0
  37. package/engine/8fa668821759/types.js +2 -0
  38. package/engine/8fa668821759/vec.js +22 -0
  39. package/engine/8fa668821759/version.js +7 -0
  40. package/engine/8fa668821759/vrtsh.js +354 -0
  41. package/engine/8fa668821759/wire.js +346 -0
  42. package/favicon.svg +15 -0
  43. package/help.md +289 -0
  44. package/icons/apple-touch-icon.png +0 -0
  45. package/icons/favicon-32.png +0 -0
  46. package/icons/icon-192.png +0 -0
  47. package/icons/icon-512.png +0 -0
  48. package/icons/icon-maskable-512.png +0 -0
  49. package/icons/icon-maskable.svg +13 -0
  50. package/icons/icon.svg +15 -0
  51. package/index.html +13730 -0
  52. package/manifest.webmanifest +37 -0
  53. package/mcp/engine/bevel.js +265 -0
  54. package/mcp/engine/camera.js +76 -0
  55. package/mcp/engine/character.js +1019 -0
  56. package/mcp/engine/check.js +83 -0
  57. package/mcp/engine/csg.js +342 -0
  58. package/mcp/engine/edit.js +48 -0
  59. package/mcp/engine/galaxy.js +265 -0
  60. package/mcp/engine/gl.js +798 -0
  61. package/mcp/engine/gpu.js +1356 -0
  62. package/mcp/engine/index.js +48 -0
  63. package/mcp/engine/instances.js +41 -0
  64. package/mcp/engine/joints.js +355 -0
  65. package/mcp/engine/kernel-bytes.js +2 -0
  66. package/mcp/engine/kernel.js +82 -0
  67. package/mcp/engine/lathe.js +87 -0
  68. package/mcp/engine/material.js +19 -0
  69. package/mcp/engine/mesh.js +131 -0
  70. package/mcp/engine/order.js +125 -0
  71. package/mcp/engine/pick.js +85 -0
  72. package/mcp/engine/place.js +17 -0
  73. package/mcp/engine/planet.js +376 -0
  74. package/mcp/engine/rig.js +417 -0
  75. package/mcp/engine/saw.js +121 -0
  76. package/mcp/engine/sky.js +428 -0
  77. package/mcp/engine/svg.js +239 -0
  78. package/mcp/engine/text.js +231 -0
  79. package/mcp/engine/triangulate.js +72 -0
  80. package/mcp/engine/types.js +2 -0
  81. package/mcp/engine/vec.js +22 -0
  82. package/mcp/engine/version.js +7 -0
  83. package/mcp/engine/vrtsh.js +354 -0
  84. package/mcp/engine/wire.js +346 -0
  85. package/mcp/http.js +292 -0
  86. package/mcp/live.js +95 -0
  87. package/mcp/package.json +10 -0
  88. package/mcp/scene.js +1027 -0
  89. package/mcp/serve.js +87 -0
  90. package/mcp/server.js +97 -0
  91. package/media/face-of-spider.png +0 -0
  92. package/media/spider-paint-2015.png +0 -0
  93. package/media/spider-web.png +0 -0
  94. package/models/deepsky-lark.scene.json +1 -0
  95. package/models/deepsky.scene.json +1 -0
  96. package/models/globe.scene.json +1 -0
  97. package/models/lark-4.scene.json +1 -0
  98. package/models/lark.game.json +14 -0
  99. package/models/spider-build-v2.md +215 -0
  100. package/models/spider-build-v2.mjs +355 -0
  101. package/models/spider-build-v2.schema.json +1001 -0
  102. package/nginx.conf +12 -0
  103. package/package.json +45 -4
  104. package/player.html +274 -0
  105. package/sky.html +133 -0
  106. package/sky.md +285 -0
  107. package/src/demo.js +92 -0
  108. package/src/emulator/devices.js +22 -0
  109. package/src/emulator/xr.js +362 -0
  110. package/src/math.js +77 -0
  111. package/sw.js +37 -0
  112. package/xr-compare.html +332 -0
  113. package/xr-test.html +248 -0
@@ -0,0 +1,354 @@
1
+ // VRTSH, "ViRTuSH": The Solution is at Hand. A hand-held UI for VR with bare hands (WebXR hand tracking).
2
+ //
3
+ // Each hand always has one function, whatever the wearer is doing or whoever they are playing. On the back of each hand sit
4
+ // the icons of the functions it can take; the other hand's index fingertip pokes one to choose it. The chosen one stands in
5
+ // front of the back of the hand, over the fingers, bigger, tilted toward the eyes; it is only a sign, it does not react. What a function does is up to the program: VRTSH
6
+ // reads the hands, keeps the choice, tells the gestures (fist, pinch, pointing, click, a fist opened) and lays the icons out.
7
+ // It knows nothing of a scene or a renderer: positions are in the room's reference space (metres, y up).
8
+ export const TSH_JOINTS = [
9
+ 'wrist',
10
+ 'thumb-metacarpal', 'thumb-phalanx-proximal', 'thumb-phalanx-distal', 'thumb-tip',
11
+ ...['index-finger', 'middle-finger', 'ring-finger', 'pinky-finger'].flatMap((f) => ['metacarpal', 'phalanx-proximal', 'phalanx-intermediate', 'phalanx-distal', 'tip'].map((k) => `${f}-${k}`)),
12
+ ];
13
+ const FINGERS = ['index-finger', 'middle-finger', 'ring-finger', 'pinky-finger'];
14
+ /** The bones to draw a hand with: chains of joint names from the wrist to each tip. */
15
+ export const TSH_BONES = [
16
+ ['wrist', 'thumb-metacarpal', 'thumb-phalanx-proximal', 'thumb-phalanx-distal', 'thumb-tip'],
17
+ ...FINGERS.map((f) => ['wrist', `${f}-metacarpal`, `${f}-phalanx-proximal`, `${f}-phalanx-intermediate`, `${f}-phalanx-distal`, `${f}-tip`]),
18
+ ];
19
+ const add = (a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
20
+ const sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
21
+ const mul = (a, k) => [a[0] * k, a[1] * k, a[2] * k];
22
+ const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
23
+ const cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
24
+ const len = (a) => Math.hypot(a[0], a[1], a[2]);
25
+ const unit = (a) => {
26
+ const l = len(a) || 1;
27
+ return [a[0] / l, a[1] / l, a[2] / l];
28
+ };
29
+ const mid = (a, b) => [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2, (a[2] + b[2]) / 2];
30
+ const lerp = (a, b, k) => [a[0] + (b[0] - a[0]) * k, a[1] + (b[1] - a[1]) * k, a[2] + (b[2] - a[2]) * k];
31
+ /** Read both hands from a WebXR frame (joints in `space`). Hands that are not tracked are left out. */
32
+ export const readHands = (session, frame, space) => {
33
+ const out = {};
34
+ if (!frame?.getJointPose || !space)
35
+ return out;
36
+ for (const src of session.inputSources) {
37
+ const side = src.handedness;
38
+ if (!src.hand || (side !== 'left' && side !== 'right'))
39
+ continue;
40
+ const joints = {};
41
+ let wrist = null, palm;
42
+ for (const name of TSH_JOINTS) {
43
+ const js = src.hand.get?.(name);
44
+ if (!js)
45
+ continue;
46
+ let pose = null;
47
+ try {
48
+ pose = frame.getJointPose(js, space);
49
+ }
50
+ catch { }
51
+ if (!pose)
52
+ continue;
53
+ const m = pose.transform.matrix;
54
+ joints[name] = [m[12], m[13], m[14]];
55
+ if (name === 'wrist')
56
+ wrist = Float32Array.from(m);
57
+ if (name === 'middle-finger-metacarpal')
58
+ palm = Float32Array.from(m);
59
+ }
60
+ if (wrist && joints['index-finger-tip'] && joints['thumb-tip'] && joints['middle-finger-phalanx-proximal'])
61
+ out[side] = { joints, wrist, palm };
62
+ }
63
+ return out;
64
+ };
65
+ // a smoothing that takes big jumps at once: it is for the tracking's tremble, not for real moves
66
+ const ease = (old, now, k, jump) => (old && len(sub(now, old)) < jump ? lerp(old, now, k) : now);
67
+ /** A hand's gestures, with slack so they do not flicker. `mem` keeps the hand's state between frames. */
68
+ export const handGestures = (side, h, mem, now) => {
69
+ const j = h.joints, w = j.wrist;
70
+ const ratio = (f) => {
71
+ const t = j[`${f}-tip`], k = j[`${f}-phalanx-proximal`];
72
+ return t && k ? len(sub(t, w)) / Math.max(1e-3, len(sub(k, w))) : 9;
73
+ };
74
+ const curls = FINGERS.map(ratio);
75
+ const curl = curls.reduce((a, b) => a + b, 0) / curls.length;
76
+ const others = Math.max(curls[1], curls[2], curls[3]);
77
+ // pointing: the index straight, the other three curled
78
+ mem.point = mem.point ? curls[0] > 1.5 && others < 1.35 : curls[0] > 1.7 && others < 1.15;
79
+ mem.aim = mem.aim ? curls[0] > 1.5 : curls[0] > 1.7;
80
+ // the thumb on the index: its tip near the index between the knuckle and the last joint (the tip itself is a pinch)
81
+ {
82
+ const p0 = j['index-finger-phalanx-proximal'], p1 = j['index-finger-phalanx-distal'] ?? j['index-finger-phalanx-intermediate'];
83
+ let gap = 9;
84
+ if (p0 && p1) {
85
+ const seg = sub(p1, p0), l2 = Math.max(1e-9, dot(seg, seg));
86
+ const t = Math.max(0, Math.min(1, dot(sub(j['thumb-tip'], p0), seg) / l2));
87
+ gap = len(sub(j['thumb-tip'], add(p0, mul(seg, t))));
88
+ }
89
+ mem.thumbOn = mem.aim && (mem.thumbOn ? gap < 0.04 : gap < 0.025);
90
+ }
91
+ // the index and the ring finger side by side: both straight, their directions within 20° (let go past 30°)
92
+ {
93
+ const dirOf = (f) => {
94
+ const k = j[`${f}-phalanx-proximal`], t = j[`${f}-tip`];
95
+ return k && t ? unit(sub(t, k)) : null;
96
+ };
97
+ const di = dirOf('index-finger'), dr = dirOf('ring-finger');
98
+ const cosA = di && dr ? dot(di, dr) : -1;
99
+ const ringStraight = mem.pair ? curls[2] > 1.45 : curls[2] > 1.6;
100
+ mem.pair = mem.aim && ringStraight && (mem.pair ? cosA > Math.cos((30 * Math.PI) / 180) : cosA > Math.cos((20 * Math.PI) / 180));
101
+ if (mem.aim && curls[2] < 1.3)
102
+ mem.aloneAt = now; // (the index alone: the ring finger bent)
103
+ }
104
+ mem.fist = !mem.point && (mem.fist ? curl < 1.35 : curl < 1.1);
105
+ const pinchGap = len(sub(j['thumb-tip'], j['index-finger-tip']));
106
+ mem.pinch = !mem.fist && !mem.point && (mem.pinch ? pinchGap < 0.035 : pinchGap < 0.018);
107
+ // the click while pointing: the thumb tucked against the middle finger
108
+ const midF = j['middle-finger-phalanx-intermediate'] ?? j['middle-finger-phalanx-proximal'];
109
+ const tuck = len(sub(j['thumb-tip'], midF));
110
+ mem.click = mem.point && (mem.click ? tuck < 0.045 : tuck < 0.03);
111
+ mem.palm = ease(mem.palm, mid(w, j['middle-finger-phalanx-proximal']), 0.5, 0.03);
112
+ // the back of the hand: across the palm (index → pinky) × along it, signed by the hand (left and right are mirrored)
113
+ const a = j['index-finger-metacarpal'] ?? j['index-finger-phalanx-proximal'], b = j['pinky-finger-metacarpal'] ?? j['pinky-finger-phalanx-proximal'];
114
+ let back = a && b ? unit(cross(sub(a, w), sub(b, w))) : [0, 1, 0];
115
+ if (side === 'right')
116
+ back = mul(back, -1);
117
+ mem.back = unit(ease(mem.back, back, 0.35, 0.5));
118
+ const forward = unit(sub(j['middle-finger-phalanx-proximal'], w));
119
+ const ip = j['index-finger-phalanx-proximal'], it = j['index-finger-tip'];
120
+ mem.rayFrom = ip ? ease(mem.rayFrom, ip, 0.5, 0.03) : undefined;
121
+ mem.rayDir = ip ? unit(ease(mem.rayDir, unit(sub(it, ip)), 0.4, 0.5)) : undefined;
122
+ if (mem.fist)
123
+ mem.fistSince ??= now;
124
+ else
125
+ mem.fistSince = undefined;
126
+ return {
127
+ fist: mem.fist, pinch: mem.pinch, point: mem.point, click: mem.click, aim: mem.aim, thumbOn: mem.thumbOn, pair: mem.pair,
128
+ palm: mem.palm, back: mem.back, forward,
129
+ rayFrom: mem.rayFrom ?? null, rayDir: mem.rayDir ?? null,
130
+ fistFor: mem.fistSince !== undefined ? (now - mem.fistSince) / 1000 : 0,
131
+ };
132
+ };
133
+ /**
134
+ * A VRTSH: the functions of each hand and the choice between them. Call `update` every frame with the hands (readHands) and
135
+ * the head's pose; it returns each hand's function, gestures and icons, and what happened (a choice, a fist closed or opened).
136
+ */
137
+ export const createTsh = (opts = {}) => {
138
+ const fns = { left: opts.left ?? [], right: opts.right ?? [] };
139
+ const chosen = { left: fns.left[0] ?? null, right: fns.right[0] ?? null };
140
+ const mem = { left: {}, right: {} };
141
+ const poking = { left: null, right: null };
142
+ // a two-handed skill borrows the other hand: what that hand had before, to give it back
143
+ const lent = { left: null, right: null };
144
+ const other = (side) => (side === 'left' ? 'right' : 'left');
145
+ const take = (side, fn) => {
146
+ const o = other(side);
147
+ const was = chosen[side];
148
+ chosen[side] = fn;
149
+ if (fn.both) {
150
+ if (was && !was.both)
151
+ lent[side] = was;
152
+ if (!chosen[o]?.both)
153
+ lent[o] = chosen[o];
154
+ chosen[o] = fn;
155
+ }
156
+ else if (was?.both && chosen[o] === was) {
157
+ chosen[o] = lent[o] ?? fns[o][0] ?? null;
158
+ lent[o] = lent[side] = null;
159
+ }
160
+ };
161
+ const wasPinch = { left: false, right: false };
162
+ const wasFist = { left: 0, right: 0 };
163
+ const wasClick = { left: false, right: false };
164
+ const wasPoint = { left: false, right: false };
165
+ const wasThumb = { left: false, right: false };
166
+ const wasPair = { left: false, right: false };
167
+ const columns = opts.columns ?? 4, cell = opts.cell ?? 0.024, lift = opts.lift ?? 0.018;
168
+ const bigSize = opts.bigSize ?? 0.05, bigAhead = opts.bigAhead ?? 0.1, bigOut = opts.bigOut ?? 0.045, reach = opts.reach ?? 0.013;
169
+ /** where the icons of a hand sit: a grid over the back of the hand, the chosen one big in front of it, tilted to the eyes */
170
+ // The icons hang on the WRIST's tracked pose only: the back of the hand, the way the fingers point and the palm's middle are
171
+ // kept in the wrist's own frame, averaged slowly (they are the hand's shape, they do not change), and carried by the wrist.
172
+ // So another hand coming near (which can shake the finger joints' tracking) does not turn or shift them.
173
+ const onHand = { left: null, right: null };
174
+ const inWrist = (m, v, point) => {
175
+ const d = point ? [v[0] - m[12], v[1] - m[13], v[2] - m[14]] : v;
176
+ return [m[0] * d[0] + m[1] * d[1] + m[2] * d[2], m[4] * d[0] + m[5] * d[1] + m[6] * d[2], m[8] * d[0] + m[9] * d[1] + m[10] * d[2]];
177
+ };
178
+ const byWrist = (m, v, point) => {
179
+ const r = [m[0] * v[0] + m[4] * v[1] + m[8] * v[2], m[1] * v[0] + m[5] * v[1] + m[9] * v[2], m[2] * v[0] + m[6] * v[1] + m[10] * v[2]];
180
+ return point ? [r[0] + m[12], r[1] + m[13], r[2] + m[14]] : r;
181
+ };
182
+ const learnHand = (side, h, g) => {
183
+ const now = { back: inWrist(h.wrist, g.back, false), fwd: inWrist(h.wrist, g.forward, false), palm: inWrist(h.wrist, g.palm, true) };
184
+ const was = onHand[side];
185
+ // (a fist folds the fingers: learn from an open hand only, after the first look)
186
+ if (was && (g.fist || g.point))
187
+ return;
188
+ const k = was ? 0.01 : 1;
189
+ onHand[side] = was
190
+ ? { back: unit(lerp(was.back, now.back, k)), fwd: unit(lerp(was.fwd, now.fwd, k)), palm: lerp(was.palm, now.palm, k) }
191
+ : now;
192
+ };
193
+ // While the other hand's index comes to choose (its tip within `hold` of the grid), the grid stays where it was: the wrist's
194
+ // tracking trembles when the hands come close (one hides the other from the cameras), and a grid that moved under the finger
195
+ // chose the wrong icon. A real move of the hand (the wrist 6 cm away from where it was held) lets it go again.
196
+ const hold = opts.hold ?? 0.12;
197
+ const held = { left: null, right: null };
198
+ const tips = { left: null, right: null };
199
+ const layout = (side, g, h) => {
200
+ const list = fns[side];
201
+ if (list.length < 2)
202
+ return []; // (one function: nothing to choose)
203
+ const local = onHand[side];
204
+ const wp = [h.wrist[12], h.wrist[13], h.wrist[14]];
205
+ const last = held[side], tip = tips[side];
206
+ let z, palm, y, x;
207
+ if (last && tip && len(sub(tip, add(last.palm, mul(last.z, lift)))) < hold && len(sub(wp, last.wp)) < 0.06)
208
+ ({ palm, x, y, z } = last);
209
+ else {
210
+ z = local ? unit(byWrist(h.wrist, local.back, false)) : g.back;
211
+ const fwd = local ? byWrist(h.wrist, local.fwd, false) : g.forward;
212
+ palm = local ? byWrist(h.wrist, local.palm, true) : g.palm;
213
+ y = unit(sub(fwd, mul(z, dot(fwd, z))));
214
+ // x to the viewer's right when the back of the hand faces them (mirrored for the left hand's thumb side, which is fine)
215
+ x = cross(y, z);
216
+ held[side] = { palm, x, y, z, wp };
217
+ }
218
+ // the cells: fixed slots first, the rest in order (−1: the last cell of the last row, a corner, away from the middle)
219
+ let rows = Math.max(1, Math.ceil(list.length / columns));
220
+ for (const fn of list)
221
+ if (fn.slot !== undefined && fn.slot >= 0)
222
+ rows = Math.max(rows, Math.floor(fn.slot / columns) + 1);
223
+ const cells = new Map();
224
+ for (const fn of list)
225
+ if (fn.slot !== undefined)
226
+ cells.set(fn, fn.slot < 0 ? rows * columns + fn.slot : fn.slot);
227
+ let next = 0;
228
+ const taken = new Set(cells.values());
229
+ for (const fn of list) {
230
+ if (cells.has(fn))
231
+ continue;
232
+ while (taken.has(next))
233
+ next++;
234
+ cells.set(fn, next);
235
+ taken.add(next);
236
+ }
237
+ const centre = add(palm, mul(z, lift));
238
+ const icons = list.map((fn) => {
239
+ const i = cells.get(fn);
240
+ const c = i % columns, r = Math.floor(i / columns);
241
+ const at = add(centre, add(mul(x, (c - (columns - 1) / 2) * cell), mul(y, ((rows - 1) / 2 - r) * cell)));
242
+ return { fn, at, x, y, z, size: cell * 0.85, chosen: fn === chosen[side], hover: poking[side] === fn.id };
243
+ });
244
+ const fn = chosen[side];
245
+ if (fn) {
246
+ // the big one: in front of the back of the hand, over the fingers, leaning back toward the wrist by 40° (toward you when
247
+ // the hand is held up in front of you); fixed on the hand like the rest (a sign only: the choosing never looks at it)
248
+ const at = add(palm, add(mul(y, bigAhead), mul(z, bigOut)));
249
+ const lean = (40 * Math.PI) / 180;
250
+ const bz = unit(sub(mul(z, Math.cos(lean)), mul(y, Math.sin(lean))));
251
+ const by = unit(cross(bz, x));
252
+ icons.push({ fn, at, x: cross(by, bz), y: by, z: bz, size: bigSize, chosen: true, hover: false, big: true });
253
+ }
254
+ return icons;
255
+ };
256
+ const update = (hands, head, now) => {
257
+ const events = [];
258
+ const hp = head ? [head[12], head[13], head[14]] : null;
259
+ const state = {};
260
+ for (const side of ['left', 'right'])
261
+ tips[side] = hands[side === 'left' ? 'right' : 'left']?.joints['index-finger-tip'] ?? null;
262
+ for (const side of ['left', 'right']) {
263
+ const h = hands[side];
264
+ const g = h ? handGestures(side, h, mem[side], now) : null;
265
+ if (h && g)
266
+ learnHand(side, h, g);
267
+ state[side] = { seen: Boolean(h), fn: chosen[side], g, icons: h && g ? layout(side, g, h) : [] };
268
+ if (!g)
269
+ continue;
270
+ if (g.fist && !wasFist[side])
271
+ events.push({ type: 'fistClose', side });
272
+ if (!g.fist && wasFist[side])
273
+ events.push({ type: 'fistOpen', side, at: g.palm, heldFor: wasFist[side] });
274
+ wasFist[side] = g.fist ? Math.max(g.fistFor, 1e-3) : 0;
275
+ if (g.click && !wasClick[side])
276
+ events.push({ type: 'clickDown', side });
277
+ wasClick[side] = g.click;
278
+ if (g.point && !wasPoint[side])
279
+ events.push({ type: 'pointStart', side });
280
+ wasPoint[side] = g.point;
281
+ if (g.thumbOn && !wasThumb[side])
282
+ events.push({ type: 'thumbDown', side });
283
+ wasThumb[side] = g.thumbOn;
284
+ // (only after aiming with the index alone in the last 1.5 s: an open hand that just moves about does not count)
285
+ if (g.pair && !wasPair[side] && now - (mem[side].aloneAt ?? -1e9) < 1500)
286
+ events.push({ type: 'pairStart', side });
287
+ wasPair[side] = g.pair;
288
+ if (g.pinch && !wasPinch[side])
289
+ events.push({ type: 'pinchStart', side });
290
+ wasPinch[side] = g.pinch;
291
+ }
292
+ // choosing: the other hand's index fingertip on an icon (once, until it leaves)
293
+ for (const side of ['left', 'right']) {
294
+ const other = side === 'left' ? 'right' : 'left';
295
+ const tip = hands[other]?.joints['index-finger-tip'];
296
+ const icons = state[side].icons.filter((ic) => !ic.big);
297
+ if (!tip || !icons.length) {
298
+ poking[side] = null;
299
+ continue;
300
+ }
301
+ let near = null, best = Infinity;
302
+ for (const ic of icons) {
303
+ const d = len(sub(tip, ic.at));
304
+ if (d < best)
305
+ ((best = d), (near = ic));
306
+ }
307
+ if (near && best < reach) {
308
+ if (poking[side] !== near.fn.id) {
309
+ poking[side] = near.fn.id;
310
+ if (chosen[side] !== near.fn) {
311
+ take(side, near.fn);
312
+ events.push({ type: 'choose', side, fn: near.fn });
313
+ for (const sd of ['left', 'right'])
314
+ if (state[sd].g && hands[sd])
315
+ state[sd] = { ...state[sd], fn: chosen[sd], icons: layout(sd, state[sd].g, hands[sd]) };
316
+ }
317
+ }
318
+ for (const ic of state[side].icons)
319
+ ic.hover = !ic.big && ic.fn.id === near.fn.id;
320
+ }
321
+ else if (best > reach * 2)
322
+ poking[side] = null;
323
+ }
324
+ return { left: state.left, right: state.right, events };
325
+ };
326
+ return {
327
+ update,
328
+ /** the function a hand has now */
329
+ fn: (side) => chosen[side],
330
+ /** give a hand a function by id (or the first of its list) */
331
+ choose: (side, id) => {
332
+ const fn = fns[side].find((f) => f.id === id) ?? fns[other(side)].find((f) => f.id === id && f.both);
333
+ if (fn)
334
+ take(side, fn);
335
+ return fn ?? null;
336
+ },
337
+ /** end a two-handed skill: both hands get back what they had before it */
338
+ release: () => {
339
+ for (const side of ['left', 'right']) {
340
+ if (!chosen[side]?.both)
341
+ continue;
342
+ chosen[side] = lent[side] ?? fns[side].find((f) => !f.both) ?? null;
343
+ lent[side] = null;
344
+ }
345
+ },
346
+ /** change a hand's list (the choice stays if it is still there) */
347
+ setFunctions: (side, list) => {
348
+ fns[side] = list;
349
+ if (!list.includes(chosen[side]) && !chosen[side]?.both)
350
+ chosen[side] = list.find((f) => f.id === chosen[side]?.id) ?? list[0] ?? null;
351
+ },
352
+ functions: (side) => fns[side],
353
+ };
354
+ };