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,265 @@
1
+ // Bevel: each selected line of a closed surface becomes a rounded (or, with one segment, flat) strip, like Blender's Ctrl+B.
2
+ // The line's two faces are cut back by `width` (measured across each face), and a strip of `segments` rows joins the cuts. Where several
3
+ // beveled lines meet, a patch fills the corner. Returns a new mesh (faces stay CCW from outside) and where the old points went.
4
+ import { add, sub, scale, cross, dot, len, norm } from './vec.js';
5
+ const newell = (pts) => {
6
+ const n = [0, 0, 0];
7
+ for (let k = 0; k < pts.length; k++) {
8
+ const a = pts[k];
9
+ const b = pts[(k + 1) % pts.length];
10
+ n[0] += (a[1] - b[1]) * (a[2] + b[2]);
11
+ n[1] += (a[2] - b[2]) * (a[0] + b[0]);
12
+ n[2] += (a[0] - b[0]) * (a[1] + b[1]);
13
+ }
14
+ return n;
15
+ };
16
+ /** A ring that goes out along a path and comes back the same way (x y x) has a spike: the spike is cut off. */
17
+ const despike = (ring) => {
18
+ const r = [...ring];
19
+ for (let again = true; again && r.length > 2;) {
20
+ again = false;
21
+ for (let i = 0; i < r.length; i++) {
22
+ if (r[(i + r.length - 1) % r.length] === r[(i + 1) % r.length]) {
23
+ r.splice(i, 1);
24
+ r.splice(i % r.length, 1);
25
+ again = true;
26
+ break;
27
+ }
28
+ }
29
+ }
30
+ return r.filter((x, i) => x !== r[(i + 1) % r.length]);
31
+ };
32
+ export const bevel = (mesh, lines, width, segments = 1) => {
33
+ if (!(width > 0))
34
+ throw new Error('the bevel width must be positive');
35
+ if (!Number.isInteger(segments) || segments < 1)
36
+ throw new Error('the bevel needs 1 or more segments');
37
+ const P0 = mesh.positions;
38
+ const F = mesh.faces;
39
+ const half = new Map(); // "a,b" -> the face holding the half-line a -> b
40
+ F.forEach((f, fi) => f.v.forEach((a, k) => half.set(`${a},${f.v[(k + 1) % f.v.length]}`, fi)));
41
+ const key = (a, b) => (a < b ? `${a},${b}` : `${b},${a}`);
42
+ const beveled = new Set();
43
+ for (const [a, b] of lines)
44
+ if (half.has(`${a},${b}`) && half.has(`${b},${a}`))
45
+ beveled.add(key(a, b));
46
+ if (!beveled.size)
47
+ throw new Error('no line to bevel: a line needs a face on both sides');
48
+ const isB = (a, b) => beveled.has(key(a, b));
49
+ const verts = new Set();
50
+ for (const k of beveled)
51
+ for (const v of k.split(','))
52
+ verts.add(Number(v));
53
+ const fans = new Map();
54
+ for (const v of verts) {
55
+ const into = new Map();
56
+ F.forEach((f, fi) => {
57
+ const k = f.v.indexOf(v);
58
+ if (k >= 0)
59
+ into.set(f.v[(k + f.v.length - 1) % f.v.length], { f: fi, q: f.v[(k + 1) % f.v.length] });
60
+ });
61
+ const first = [...into.keys()][0];
62
+ const ns = [first];
63
+ const fs = [];
64
+ let n = first;
65
+ for (let guard = 0; guard < into.size + 1; guard++) {
66
+ const c = into.get(n);
67
+ if (!c)
68
+ throw new Error('the bevel needs a closed surface round the ends of the lines');
69
+ fs.push(c.f);
70
+ n = c.q;
71
+ if (n === first)
72
+ break;
73
+ ns.push(n);
74
+ }
75
+ if (n !== first || fs.length !== into.size)
76
+ throw new Error('the bevel needs a closed surface round the ends of the lines');
77
+ fans.set(v, { ns, fs });
78
+ }
79
+ // the corner of every face at every beveled point sits on the offset lines: P = v + w / sin * (b_i u_(i+1) + b_(i+1) u_i)
80
+ const geo = new Map();
81
+ let limit = Infinity;
82
+ for (const [v, { ns }] of fans) {
83
+ const k = ns.length;
84
+ const u = ns.map((n) => norm(sub(P0[n], P0[v])));
85
+ const l = ns.map((n) => len(sub(P0[n], P0[v])));
86
+ const b = ns.map((n) => (isB(v, n) ? 1 : 0));
87
+ const sin = ns.map((_, i) => len(cross(u[i], u[(i + 1) % k])));
88
+ for (let i = 0; i < k; i++) {
89
+ const j = (i + 1) % k;
90
+ if (!b[i] && !b[j])
91
+ continue;
92
+ if (sin[i] < 1e-3)
93
+ throw new Error('a line ends where a face goes straight on: bevel from another line');
94
+ if (b[i])
95
+ limit = Math.min(limit, 0.45 * sin[i] * l[j]);
96
+ if (b[j])
97
+ limit = Math.min(limit, 0.45 * sin[i] * l[i]);
98
+ }
99
+ geo.set(v, { u, sin, b, l });
100
+ }
101
+ const w = Math.min(width, limit);
102
+ const scaleOf = Math.max(...P0.map((p) => Math.hypot(...p)), 1e-9);
103
+ const P = P0.map((p) => [...p]);
104
+ const reg = new Map();
105
+ const pt = (p) => {
106
+ const k = p.map((c) => Math.round(c / (scaleOf * 1e-9))).join(',');
107
+ const have = reg.get(k);
108
+ if (have !== undefined)
109
+ return have;
110
+ P.push(p);
111
+ reg.set(k, P.length - 1);
112
+ return P.length - 1;
113
+ };
114
+ const C = new Map(); // C.get(v)[i]: the corner point of face i at v
115
+ for (const [v, { ns }] of fans) {
116
+ const { u, sin, b } = geo.get(v);
117
+ const k = ns.length;
118
+ C.set(v, ns.map((_, i) => {
119
+ const j = (i + 1) % k;
120
+ if (!b[i] && !b[j])
121
+ return v;
122
+ const off = scale(add(scale(u[j], b[i]), scale(u[i], b[j])), w / sin[i]);
123
+ return pt(add(P0[v], off));
124
+ }));
125
+ }
126
+ // the profile of each line at each end: from the face holding the half-line v -> n to the other face (n >= 1 rows)
127
+ const profile = new Map();
128
+ for (const [v, { ns }] of fans) {
129
+ const k = ns.length;
130
+ const cs = C.get(v);
131
+ for (let i = 0; i < k; i++) {
132
+ if (!isB(v, ns[i]))
133
+ continue;
134
+ const A = cs[(i + k - 1) % k];
135
+ const B = cs[i];
136
+ const chain = [A];
137
+ for (let s = 1; s < segments; s++) {
138
+ const t = s / segments;
139
+ const p = add(add(scale(P[A], (1 - t) * (1 - t)), scale(P0[v], 2 * t * (1 - t))), scale(P[B], t * t));
140
+ chain.push(pt(p));
141
+ }
142
+ chain.push(B);
143
+ profile.set(`${v}>${ns[i]}`, chain);
144
+ }
145
+ }
146
+ const faceNormal = (fi) => norm(newell(F[fi].v.map((i) => P0[i])));
147
+ const out = [];
148
+ const addFace = (v, want, from) => {
149
+ const ids = v.filter((x, i) => x !== v[(i + 1) % v.length]);
150
+ if (new Set(ids).size < 3)
151
+ return;
152
+ const n = newell(ids.map((i) => P[i]));
153
+ const f = F[from];
154
+ out.push({ v: dot(n, want) < 0 ? [...ids].reverse() : ids, ...(f.color ? { color: f.color } : {}), ...(f.mat ? { mat: f.mat } : {}) });
155
+ };
156
+ // the strips
157
+ for (const k of beveled) {
158
+ const [a, b] = k.split(',').map(Number);
159
+ const ca = profile.get(`${a}>${b}`); // from the face holding a -> b
160
+ const cb = [...profile.get(`${b}>${a}`)].reverse(); // began at the face holding b -> a: turned round, it too begins at the face of a -> b
161
+ const want = add(faceNormal(half.get(`${a},${b}`)), faceNormal(half.get(`${b},${a}`)));
162
+ for (let s = 0; s < segments; s++)
163
+ addFace([ca[s], ca[s + 1], cb[s + 1], cb[s]], want, half.get(`${a},${b}`));
164
+ }
165
+ // the corner patches, and the one face that takes a corner in (a point with a single untouched face is cut off)
166
+ const replace = new Map(); // "face,v" -> the points that stand where v was
167
+ for (const [v, { ns, fs }] of fans) {
168
+ const { b } = geo.get(v);
169
+ const k = ns.length;
170
+ const cs = C.get(v);
171
+ const ring = [];
172
+ for (let i = 0; i < k; i++) {
173
+ ring.push(cs[i]);
174
+ const j = (i + 1) % k;
175
+ if (b[j])
176
+ ring.push(...profile.get(`${v}>${ns[j]}`).slice(1, -1));
177
+ }
178
+ const loop = despike(ring.filter((x, i) => x !== ring[(i + 1) % ring.length]));
179
+ const untouched = ns.map((_, i) => i).filter((i) => !b[i] && !b[(i + 1) % k]);
180
+ if (untouched.length === 1) {
181
+ const at = loop.indexOf(v);
182
+ const around = [...loop.slice(at + 1), ...loop.slice(0, at)];
183
+ replace.set(`${fs[untouched[0]]},${v}`, around.reverse());
184
+ continue;
185
+ }
186
+ const want = fs.reduce((t, f) => add(t, faceNormal(f)), [0, 0, 0]);
187
+ if (new Set(loop).size < 3)
188
+ continue;
189
+ const nrm = norm(newell(loop.map((i) => P[i])));
190
+ let convex = true;
191
+ for (let i = 0; i < loop.length; i++) {
192
+ const p = P[loop[i]];
193
+ const q = P[loop[(i + 1) % loop.length]];
194
+ const r = P[loop[(i + 2) % loop.length]];
195
+ if (dot(cross(sub(q, p), sub(r, q)), nrm) < -1e-9 * scaleOf * scaleOf)
196
+ convex = false;
197
+ }
198
+ const from = fs[0];
199
+ if (convex)
200
+ addFace(loop, want, from);
201
+ else {
202
+ // the whole ring is turned the right way once: a sliver triangle has no reliable normal of its own
203
+ const ccw = dot(newell(loop.map((i) => P[i])), want) < 0 ? [...loop].reverse() : loop;
204
+ const hub = ccw.includes(v) ? ccw.indexOf(v) : 0;
205
+ const f = F[from];
206
+ for (let i = 1; i + 1 < ccw.length; i++) {
207
+ const t = [ccw[hub], ccw[(hub + i) % ccw.length], ccw[(hub + i + 1) % ccw.length]];
208
+ if (new Set(t).size === 3)
209
+ out.push({ v: t, ...(f.color ? { color: f.color } : {}), ...(f.mat ? { mat: f.mat } : {}) });
210
+ }
211
+ }
212
+ }
213
+ // the old faces, with their corners at beveled points replaced
214
+ const dist = (p, v) => len(sub(P[p], P0[v]));
215
+ const faces = F.map((f, fi) => {
216
+ const list = [];
217
+ for (const x of f.v) {
218
+ const fan = fans.get(x);
219
+ if (!fan) {
220
+ list.push(x);
221
+ continue;
222
+ }
223
+ const taken = replace.get(`${fi},${x}`);
224
+ if (taken) {
225
+ list.push(...taken);
226
+ continue;
227
+ }
228
+ const { ns } = fan;
229
+ const k = ns.length;
230
+ const i = fan.fs.indexOf(fi);
231
+ const cs = C.get(x);
232
+ const { b } = geo.get(x);
233
+ const mine = cs[i];
234
+ const d = dist(mine, x);
235
+ const before = cs[(i + k - 1) % k];
236
+ const after = cs[(i + 1) % k];
237
+ if (!b[i] && before !== mine && dist(before, x) > d + 1e-12)
238
+ list.push(before);
239
+ list.push(mine);
240
+ if (!b[(i + 1) % k] && after !== mine && dist(after, x) > d + 1e-12)
241
+ list.push(after);
242
+ }
243
+ return { ...f, v: list.filter((p, i) => p !== list[(i + 1) % list.length]) };
244
+ });
245
+ // drop the points nothing uses any more
246
+ const all = [...faces, ...out];
247
+ const used = new Set();
248
+ for (const f of all)
249
+ for (const i of f.v)
250
+ used.add(i);
251
+ const remapAll = new Map();
252
+ const positions = [];
253
+ P.forEach((p, i) => {
254
+ if (used.has(i)) {
255
+ remapAll.set(i, positions.length);
256
+ positions.push(p);
257
+ }
258
+ });
259
+ const result = { positions, faces: all.map((f) => ({ ...f, v: f.v.map((i) => remapAll.get(i)) })) };
260
+ const remap = new Map();
261
+ for (let i = 0; i < P0.length; i++)
262
+ if (remapAll.has(i))
263
+ remap.set(i, remapAll.get(i));
264
+ return { mesh: result, remap, width: w, lines: beveled.size };
265
+ };
@@ -0,0 +1,76 @@
1
+ // Four named views, all looking at the mesh bounding-box center.
2
+ // front / side / top are orthographic (same scale), persp is a perspective view.
3
+ // A view can also be a custom spec: { dir, up, ortho, name? } where `dir` is the
4
+ // direction from the target towards the eye.
5
+ import { add, cross, dot, norm, scale, plus, minus, times } from './vec.js';
6
+ import { bounds } from './mesh.js';
7
+ export const VIEW_ORDER = ['front', 'side', 'top', 'persp'];
8
+ const FOV = (35 * Math.PI) / 180;
9
+ const VIEWS = {
10
+ front: { dir: [0, 0, 1], up: [0, 1, 0], ortho: true }, // eye on +z
11
+ side: { dir: [1, 0, 0], up: [0, 1, 0], ortho: true }, // eye on +x
12
+ top: { dir: [0, 1, 0], up: [0, 0, -1], ortho: true }, // eye on +y, -z is screen-up
13
+ persp: { dir: norm([1, 0.8, 1.2]), up: [0, 1, 0], ortho: false },
14
+ };
15
+ /**
16
+ * `fit` = { center, radius, dist? } overrides the target and framing (perspective views only use `dist`). Use it when the
17
+ * mesh moves (e.g. rotates) so the camera keeps looking at the same spot.
18
+ */
19
+ export const makeCamera = (view, mesh, size, fit) => {
20
+ const spec = typeof view === 'string' ? VIEWS[view] : view;
21
+ if (!spec)
22
+ throw new Error(`unknown view: ${view}`);
23
+ const name = typeof view === 'string' ? view : (view.name ?? 'custom');
24
+ const ortho = Boolean(spec.ortho);
25
+ const { center, radius } = fit ?? bounds(mesh);
26
+ const r0 = radius || 1;
27
+ const dist = ortho ? r0 * 4 : (fit?.dist ?? (r0 * 1.25) / Math.sin(FOV / 2));
28
+ const fov = fit?.fov ?? FOV; // fit.dist: eye distance override (a first-person eye sits almost on the center)
29
+ const eye = plus(center)(times(dist)(norm(spec.dir)));
30
+ const f = norm(minus(eye)(center)); // forward
31
+ const r = norm(cross(f, spec.up)); // right
32
+ const u = cross(r, f); // up
33
+ const k = ortho ? ((size / 2) * 0.8) / r0 : size / 2 / Math.tan(fov / 2);
34
+ return { name, eye, f, r, u, ortho, k, size };
35
+ };
36
+ /**
37
+ * Turntable rotation as a camera: the view you get from rotate(mesh, { yaw, pitch })
38
+ * with a fixed camera, but the mesh stays where it is. Picks and marks are then
39
+ * in object space.
40
+ */
41
+ export const orbit = ({ yaw = 0, pitch = 0, roll = 0, ortho = false, name = 'orbit' } = {}) => {
42
+ const cy = Math.cos(yaw);
43
+ const sy = Math.sin(yaw);
44
+ const cp = Math.cos(pitch);
45
+ const sp = Math.sin(pitch);
46
+ const dir = [-sy * cp, sp, cy * cp];
47
+ const up = [sy * sp, cp, -cy * sp];
48
+ if (!roll)
49
+ return { name, ortho, dir, up };
50
+ // roll: the up vector turns round the view axis towards the right (positive: the camera banks right, the picture turns left)
51
+ const right = cross(scale(dir, -1), up);
52
+ const c = Math.cos(roll), s = Math.sin(roll);
53
+ return { name, ortho, dir, up: [up[0] * c + right[0] * s, up[1] * c + right[1] * s, up[2] * c + right[2] * s] };
54
+ };
55
+ /** Ray through panel pixel (x, y), the inverse of project(): { origin, dir } in world space, dir is a unit vector. */
56
+ export const ray = (cam, x, y) => {
57
+ const nx = (x - cam.size / 2) / cam.k;
58
+ const ny = -(y - cam.size / 2) / cam.k;
59
+ const lateral = plus(scale(cam.u, ny))(scale(cam.r, nx));
60
+ return cam.ortho
61
+ ? { origin: add(cam.eye, lateral), dir: cam.f }
62
+ : { origin: cam.eye, dir: norm(plus(lateral)(cam.f)) };
63
+ };
64
+ /** World point -> { x, y } in panel pixels (y down) and depth along the view direction. */
65
+ export const project = (cam, p) => {
66
+ const d = minus(cam.eye)(p);
67
+ const cx = dot(d, cam.r);
68
+ const cy = dot(d, cam.u);
69
+ const depth = dot(d, cam.f);
70
+ const w = cam.ortho ? 1 : depth;
71
+ return {
72
+ x: cam.size / 2 + (cx / w) * cam.k,
73
+ y: cam.size / 2 - (cy / w) * cam.k,
74
+ depth,
75
+ };
76
+ };