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.
- package/APP.md +40 -0
- package/Dockerfile +4 -0
- package/MCP.md +149 -0
- package/PLAYER.md +136 -0
- package/README.md +35 -2
- package/api/mcp.mjs +31 -0
- package/api/store.mjs +158 -0
- package/bin/spi3der.mjs +20 -0
- package/emulator.html +17 -0
- package/engine/8fa668821759/bevel.js +265 -0
- package/engine/8fa668821759/camera.js +76 -0
- package/engine/8fa668821759/character.js +1019 -0
- package/engine/8fa668821759/check.js +83 -0
- package/engine/8fa668821759/csg.js +342 -0
- package/engine/8fa668821759/edit.js +48 -0
- package/engine/8fa668821759/galaxy.js +265 -0
- package/engine/8fa668821759/gl.js +798 -0
- package/engine/8fa668821759/gpu.js +1356 -0
- package/engine/8fa668821759/index.js +48 -0
- package/engine/8fa668821759/instances.js +41 -0
- package/engine/8fa668821759/joints.js +355 -0
- package/engine/8fa668821759/kernel-bytes.js +2 -0
- package/engine/8fa668821759/kernel.js +82 -0
- package/engine/8fa668821759/lathe.js +87 -0
- package/engine/8fa668821759/material.js +19 -0
- package/engine/8fa668821759/mesh.js +131 -0
- package/engine/8fa668821759/order.js +125 -0
- package/engine/8fa668821759/pick.js +85 -0
- package/engine/8fa668821759/place.js +17 -0
- package/engine/8fa668821759/planet.js +376 -0
- package/engine/8fa668821759/rig.js +417 -0
- package/engine/8fa668821759/saw.js +121 -0
- package/engine/8fa668821759/sky.js +428 -0
- package/engine/8fa668821759/svg.js +239 -0
- package/engine/8fa668821759/text.js +231 -0
- package/engine/8fa668821759/triangulate.js +72 -0
- package/engine/8fa668821759/types.js +2 -0
- package/engine/8fa668821759/vec.js +22 -0
- package/engine/8fa668821759/version.js +7 -0
- package/engine/8fa668821759/vrtsh.js +354 -0
- package/engine/8fa668821759/wire.js +346 -0
- package/favicon.svg +15 -0
- package/help.md +289 -0
- package/icons/apple-touch-icon.png +0 -0
- package/icons/favicon-32.png +0 -0
- package/icons/icon-192.png +0 -0
- package/icons/icon-512.png +0 -0
- package/icons/icon-maskable-512.png +0 -0
- package/icons/icon-maskable.svg +13 -0
- package/icons/icon.svg +15 -0
- package/index.html +13730 -0
- package/manifest.webmanifest +37 -0
- package/mcp/engine/bevel.js +265 -0
- package/mcp/engine/camera.js +76 -0
- package/mcp/engine/character.js +1019 -0
- package/mcp/engine/check.js +83 -0
- package/mcp/engine/csg.js +342 -0
- package/mcp/engine/edit.js +48 -0
- package/mcp/engine/galaxy.js +265 -0
- package/mcp/engine/gl.js +798 -0
- package/mcp/engine/gpu.js +1356 -0
- package/mcp/engine/index.js +48 -0
- package/mcp/engine/instances.js +41 -0
- package/mcp/engine/joints.js +355 -0
- package/mcp/engine/kernel-bytes.js +2 -0
- package/mcp/engine/kernel.js +82 -0
- package/mcp/engine/lathe.js +87 -0
- package/mcp/engine/material.js +19 -0
- package/mcp/engine/mesh.js +131 -0
- package/mcp/engine/order.js +125 -0
- package/mcp/engine/pick.js +85 -0
- package/mcp/engine/place.js +17 -0
- package/mcp/engine/planet.js +376 -0
- package/mcp/engine/rig.js +417 -0
- package/mcp/engine/saw.js +121 -0
- package/mcp/engine/sky.js +428 -0
- package/mcp/engine/svg.js +239 -0
- package/mcp/engine/text.js +231 -0
- package/mcp/engine/triangulate.js +72 -0
- package/mcp/engine/types.js +2 -0
- package/mcp/engine/vec.js +22 -0
- package/mcp/engine/version.js +7 -0
- package/mcp/engine/vrtsh.js +354 -0
- package/mcp/engine/wire.js +346 -0
- package/mcp/http.js +292 -0
- package/mcp/live.js +95 -0
- package/mcp/package.json +10 -0
- package/mcp/scene.js +1027 -0
- package/mcp/serve.js +87 -0
- package/mcp/server.js +97 -0
- package/media/face-of-spider.png +0 -0
- package/media/spider-paint-2015.png +0 -0
- package/media/spider-web.png +0 -0
- package/models/deepsky-lark.scene.json +1 -0
- package/models/deepsky.scene.json +1 -0
- package/models/globe.scene.json +1 -0
- package/models/lark-4.scene.json +1 -0
- package/models/lark.game.json +14 -0
- package/models/spider-build-v2.md +215 -0
- package/models/spider-build-v2.mjs +355 -0
- package/models/spider-build-v2.schema.json +1001 -0
- package/nginx.conf +12 -0
- package/package.json +45 -4
- package/player.html +274 -0
- package/sky.html +133 -0
- package/sky.md +285 -0
- package/src/demo.js +92 -0
- package/src/emulator/devices.js +22 -0
- package/src/emulator/xr.js +362 -0
- package/src/math.js +77 -0
- package/sw.js +37 -0
- package/xr-compare.html +332 -0
- package/xr-test.html +248 -0
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// Flat SVG preview: points, edges, faces. No shading, no textures.
|
|
2
|
+
// Faces are painted far to near (see order.js), back faces culled. The polygon stroke
|
|
3
|
+
// is the edge layer, so hidden edges stay hidden. Output is deterministic.
|
|
4
|
+
import { dot, norm, minus } from './vec.js';
|
|
5
|
+
import { faceNormal } from './mesh.js';
|
|
6
|
+
import { paintOrder } from './order.js';
|
|
7
|
+
import { VIEW_ORDER, makeCamera, project } from './camera.js';
|
|
8
|
+
import { surfaceOf } from './material.js';
|
|
9
|
+
/**
|
|
10
|
+
* Depth-tested scanline fill of one polygon into an id buffer. `w` grows towards the eye (1/depth in a
|
|
11
|
+
* perspective view, -depth in an orthographic one) and is linear across a planar face on the screen.
|
|
12
|
+
* A pixel that ties with what is there (coplanar faces) keeps both ids alive for the painter's order.
|
|
13
|
+
*/
|
|
14
|
+
const fill = (ids, zb, size, pts, w, id, alive, solid = true) => {
|
|
15
|
+
const test = (x, y, v) => {
|
|
16
|
+
if (x < 0 || y < 0 || x >= size || y >= size)
|
|
17
|
+
return;
|
|
18
|
+
const at = y * size + x;
|
|
19
|
+
const old = zb[at];
|
|
20
|
+
const tie = Math.abs(v - old) <= 1e-5 * Math.abs(old);
|
|
21
|
+
if (!solid) {
|
|
22
|
+
// a see-through face is visible where nothing nearer hides it, and hides nothing itself
|
|
23
|
+
if (v > old || tie)
|
|
24
|
+
alive.add(id);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (v > old || tie) {
|
|
28
|
+
if (tie && ids[at] >= 0)
|
|
29
|
+
alive.add(ids[at]);
|
|
30
|
+
zb[at] = Math.max(v, old);
|
|
31
|
+
ids[at] = id;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
// the outline too: a face thinner than a pixel still shows as a line
|
|
35
|
+
for (let a = 0; a < pts.length; a++) {
|
|
36
|
+
const b = (a + 1) % pts.length;
|
|
37
|
+
const n = Math.ceil(Math.max(Math.abs(pts[b].x - pts[a].x), Math.abs(pts[b].y - pts[a].y)));
|
|
38
|
+
for (let i = 0; i <= n; i++) {
|
|
39
|
+
const t = n ? i / n : 0;
|
|
40
|
+
test(Math.floor(pts[a].x + t * (pts[b].x - pts[a].x)), Math.floor(pts[a].y + t * (pts[b].y - pts[a].y)), w[a] + t * (w[b] - w[a]));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
let lo = Infinity;
|
|
44
|
+
let hi = -Infinity;
|
|
45
|
+
for (const p of pts)
|
|
46
|
+
(lo = Math.min(lo, p.y), (hi = Math.max(hi, p.y)));
|
|
47
|
+
const y0 = Math.max(0, Math.ceil(lo - 0.5));
|
|
48
|
+
const y1 = Math.min(size - 1, Math.floor(hi - 0.5));
|
|
49
|
+
const cross = [];
|
|
50
|
+
for (let y = y0; y <= y1; y++) {
|
|
51
|
+
const yc = y + 0.5;
|
|
52
|
+
cross.length = 0;
|
|
53
|
+
for (let a = 0; a < pts.length; a++) {
|
|
54
|
+
const b = (a + 1) % pts.length;
|
|
55
|
+
const p = pts[a];
|
|
56
|
+
const q = pts[b];
|
|
57
|
+
if ((p.y <= yc && q.y > yc) || (q.y <= yc && p.y > yc)) {
|
|
58
|
+
const t = (yc - p.y) / (q.y - p.y);
|
|
59
|
+
cross.push({ x: p.x + t * (q.x - p.x), w: w[a] + t * (w[b] - w[a]) });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
cross.sort((a, b) => a.x - b.x);
|
|
63
|
+
for (let k = 0; k + 1 < cross.length; k += 2) {
|
|
64
|
+
const l = cross[k];
|
|
65
|
+
const r = cross[k + 1];
|
|
66
|
+
const x0 = Math.max(0, Math.ceil(l.x - 0.5));
|
|
67
|
+
const x1 = Math.min(size - 1, Math.floor(r.x - 0.5));
|
|
68
|
+
const span = r.x - l.x || 1;
|
|
69
|
+
for (let x = x0; x <= x1; x++) {
|
|
70
|
+
const v = l.w + ((x + 0.5 - l.x) / span) * (r.w - l.w);
|
|
71
|
+
test(x, y, v);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const defaults = {
|
|
77
|
+
size: 400,
|
|
78
|
+
cull: true,
|
|
79
|
+
points: true,
|
|
80
|
+
stroke: '#1d1d1f',
|
|
81
|
+
pointColor: '#d6336c',
|
|
82
|
+
background: '#fafafa',
|
|
83
|
+
};
|
|
84
|
+
const num = (n) => String(Math.round(n * 100) / 100 + 0); // +0 turns -0 into 0
|
|
85
|
+
const esc = (s) => String(s).replace(/[&<>"]/g, (c) => `&#${c.charCodeAt(0)};`);
|
|
86
|
+
const panel = (mesh, view, o, x = 0, y = 0) => {
|
|
87
|
+
const cam = makeCamera(view, mesh, o.size, o.fit);
|
|
88
|
+
const name = esc(cam.name);
|
|
89
|
+
const proj = mesh.positions.map((p) => project(cam, p));
|
|
90
|
+
const items = [];
|
|
91
|
+
const NEAR = 0.02; // perspective: nearer than this is cut off
|
|
92
|
+
const keep = -8; // faces entirely off the panel (by more than a stroke) are not drawn
|
|
93
|
+
const seen = (pts) => !pts.every((p) => p.x < keep) && !pts.every((p) => p.y < keep) && !pts.every((p) => p.x > o.size - keep) && !pts.every((p) => p.y > o.size - keep);
|
|
94
|
+
mesh.faces.forEach((face, i) => {
|
|
95
|
+
let world = face.v.map((k) => mesh.positions[k]);
|
|
96
|
+
let screen = face.v.map((k) => proj[k]);
|
|
97
|
+
const dir = cam.ortho ? cam.f : minus(cam.eye)(world[0]);
|
|
98
|
+
const normal = faceNormal(world);
|
|
99
|
+
const facing = dot(normal, dir) < 0;
|
|
100
|
+
if (o.cull && !facing)
|
|
101
|
+
return; // back face
|
|
102
|
+
if (cam.ortho) {
|
|
103
|
+
if (screen.some((s) => s.depth <= 0.01))
|
|
104
|
+
return; // behind the camera
|
|
105
|
+
}
|
|
106
|
+
else if (screen.some((s) => s.depth < NEAR)) {
|
|
107
|
+
// cut the face at the near plane, like a camera does
|
|
108
|
+
if (screen.every((s) => s.depth < NEAR))
|
|
109
|
+
return;
|
|
110
|
+
const cut = [];
|
|
111
|
+
for (let a = 0; a < world.length; a++) {
|
|
112
|
+
const b = (a + 1) % world.length;
|
|
113
|
+
const da = screen[a].depth - NEAR;
|
|
114
|
+
const db = screen[b].depth - NEAR;
|
|
115
|
+
if (da >= 0)
|
|
116
|
+
cut.push(world[a]);
|
|
117
|
+
if (da * db < 0) {
|
|
118
|
+
const t = da / (da - db);
|
|
119
|
+
cut.push(world[a].map((c, k) => c + (world[b][k] - c) * t));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (cut.length < 3)
|
|
123
|
+
return;
|
|
124
|
+
world = cut;
|
|
125
|
+
screen = world.map((p) => project(cam, p));
|
|
126
|
+
}
|
|
127
|
+
if (!seen(screen))
|
|
128
|
+
return; // outside the panel
|
|
129
|
+
const surface = surfaceOf(face, o.palette);
|
|
130
|
+
items.push({
|
|
131
|
+
i,
|
|
132
|
+
vs: face.v,
|
|
133
|
+
world,
|
|
134
|
+
screen,
|
|
135
|
+
n: norm(normal),
|
|
136
|
+
p: world[0],
|
|
137
|
+
eye: facing ? 1 : -1,
|
|
138
|
+
color: surface.color,
|
|
139
|
+
alpha: surface.alpha,
|
|
140
|
+
depth: screen.reduce((s, p) => s + p.depth, 0) / screen.length,
|
|
141
|
+
pts: screen.map((p) => `${num(p.x)},${num(p.y)}`).join(' '),
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
// Occlusion: a depth buffer finds the faces that own a pixel; only those go on to the painter's order.
|
|
145
|
+
let idBuf = null;
|
|
146
|
+
let visible = items;
|
|
147
|
+
if (o.occlude) {
|
|
148
|
+
const buf = new Int32Array(o.size * o.size).fill(-1);
|
|
149
|
+
const zb = new Float32Array(o.size * o.size).fill(-Infinity);
|
|
150
|
+
const alive = new Set();
|
|
151
|
+
items.forEach((it, idx) => {
|
|
152
|
+
const w = it.screen.map((p) => (cam.ortho ? -p.depth : 1 / p.depth));
|
|
153
|
+
fill(buf, zb, o.size, it.screen, w, idx, alive, it.alpha >= 1);
|
|
154
|
+
});
|
|
155
|
+
for (const id of buf)
|
|
156
|
+
if (id >= 0)
|
|
157
|
+
alive.add(id);
|
|
158
|
+
const remap = new Map(); // item index -> index among the visible faces
|
|
159
|
+
visible = items.filter((_, idx) => alive.has(idx) && (remap.set(idx, remap.size), true));
|
|
160
|
+
idBuf = buf.map((id) => (id < 0 ? -1 : (remap.get(id) ?? -1)));
|
|
161
|
+
}
|
|
162
|
+
let ordered = paintOrder(visible); // far to near
|
|
163
|
+
if (idBuf) {
|
|
164
|
+
// ids point into `visible`; the painter's order permutes it
|
|
165
|
+
const at = new Map();
|
|
166
|
+
visible.forEach((it, n) => at.set(it, n));
|
|
167
|
+
const perm = new Int32Array(visible.length);
|
|
168
|
+
ordered.forEach((it, n) => (perm[at.get(it)] = n));
|
|
169
|
+
idBuf = idBuf.map((id) => (id < 0 ? -1 : perm[id]));
|
|
170
|
+
}
|
|
171
|
+
// A point is drawn right after the nearest painted face that holds it, so
|
|
172
|
+
// faces painted later (closer to the camera) still cover it.
|
|
173
|
+
const last = new Map();
|
|
174
|
+
ordered.forEach((it, idx) => it.vs.forEach((k) => last.set(k, idx)));
|
|
175
|
+
// a point is visible when a visible face that holds it owns a pixel at (or right next to) it
|
|
176
|
+
const reached = (k) => {
|
|
177
|
+
const px = Math.round(proj[k].x);
|
|
178
|
+
const py = Math.round(proj[k].y);
|
|
179
|
+
for (let dy = -1; dy <= 1; dy++) {
|
|
180
|
+
for (let dx = -1; dx <= 1; dx++) {
|
|
181
|
+
const x = px + dx;
|
|
182
|
+
const y = py + dy;
|
|
183
|
+
if (x < 0 || y < 0 || x >= o.size || y >= o.size)
|
|
184
|
+
continue;
|
|
185
|
+
const id = idBuf[y * o.size + x];
|
|
186
|
+
if (id >= 0 && ordered[id].vs.includes(k))
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return false;
|
|
191
|
+
};
|
|
192
|
+
const body = ordered.flatMap((it, idx) => {
|
|
193
|
+
const polygon = `<polygon data-face="${it.i}" data-depth="${num(it.depth)}" points="${it.pts}" ` +
|
|
194
|
+
`fill="${it.color}"${it.alpha < 1 ? ` fill-opacity="${num(it.alpha)}"` : ''} stroke="${o.stroke}" stroke-width="1.5" stroke-linejoin="round"/>`;
|
|
195
|
+
const dots = o.points
|
|
196
|
+
? it.vs
|
|
197
|
+
.filter((k) => last.get(k) === idx && (cam.ortho || proj[k].depth >= NEAR) && (!idBuf || reached(k)))
|
|
198
|
+
.sort((a, b) => a - b)
|
|
199
|
+
.map((k) => `<circle data-v="${k}" cx="${num(proj[k].x)}" cy="${num(proj[k].y)}" r="3" fill="${o.pointColor}"/>`)
|
|
200
|
+
: [];
|
|
201
|
+
return [polygon, ...dots];
|
|
202
|
+
});
|
|
203
|
+
// 3D polylines (e.g. a surface cursor) drawn on top of everything else.
|
|
204
|
+
const marks = (o.marks ?? []).flatMap((m) => m.lines.flatMap((line) => {
|
|
205
|
+
const pts = line.map((p) => project(cam, p));
|
|
206
|
+
if (pts.some((p) => p.depth <= 0.01))
|
|
207
|
+
return [];
|
|
208
|
+
return [
|
|
209
|
+
`<polyline data-mark="${esc(m.name ?? 'mark')}" points="${pts.map((p) => `${num(p.x)},${num(p.y)}`).join(' ')}" ` +
|
|
210
|
+
`fill="none" stroke="${m.color ?? '#0b7285'}" stroke-width="${m.width ?? 2}" ` +
|
|
211
|
+
`stroke-linecap="round" stroke-linejoin="round"${m.opacity !== undefined && m.opacity < 1 ? ` stroke-opacity="${m.opacity}"` : ''}/>`,
|
|
212
|
+
];
|
|
213
|
+
}));
|
|
214
|
+
return [
|
|
215
|
+
`<g data-view="${name}" transform="translate(${x} ${y})">`,
|
|
216
|
+
`<rect width="${o.size}" height="${o.size}" fill="${o.background}" stroke="#cfcfd4"/>`,
|
|
217
|
+
`<text x="10" y="20" font-family="monospace" font-size="13" fill="#555">${name}</text>`,
|
|
218
|
+
...body,
|
|
219
|
+
...marks,
|
|
220
|
+
'</g>',
|
|
221
|
+
].join('\n');
|
|
222
|
+
};
|
|
223
|
+
const root = (w, h, body) => `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${w} ${h}" width="${w}" height="${h}">\n${body}\n</svg>\n`;
|
|
224
|
+
/**
|
|
225
|
+
* One standalone SVG for a single view: 'front' | 'side' | 'top' | 'persp',
|
|
226
|
+
* or a custom { dir, up, ortho, name? } spec.
|
|
227
|
+
*/
|
|
228
|
+
export const renderView = (mesh, view, opts = {}) => {
|
|
229
|
+
const o = { ...defaults, ...opts };
|
|
230
|
+
return root(o.size, o.size, panel(mesh, view, o));
|
|
231
|
+
};
|
|
232
|
+
/** The four views one after another: [{ name, svg }, ...]. */
|
|
233
|
+
export const renderViews = (mesh, opts = {}) => VIEW_ORDER.map((name) => ({ name, svg: renderView(mesh, name, opts) }));
|
|
234
|
+
/** All four views on one 2x2 sheet: front, side / top, persp. */
|
|
235
|
+
export const renderSheet = (mesh, opts = {}) => {
|
|
236
|
+
const o = { ...defaults, ...opts };
|
|
237
|
+
const body = VIEW_ORDER.map((name, i) => panel(mesh, name, o, (i % 2) * o.size, Math.floor(i / 2) * o.size)).join('\n');
|
|
238
|
+
return root(o.size * 2, o.size * 2, body);
|
|
239
|
+
};
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
// Text as a model: a minimalist, blocky stroke font drawn on a 2 x 4 grid, every stroke a straight wooden wedge-simple bar.
|
|
2
|
+
// The stroke is 20 % of the letter height (and so is the depth the letters stand out from the face).
|
|
3
|
+
// Glyphs are data: lists of poly-lines; a poly-line of one point is a dot. Unknown characters are skipped, lower case is upper case.
|
|
4
|
+
import { add, cross, dot, norm, scale, sub } from './vec.js';
|
|
5
|
+
import { box, faceNormal, merge } from './mesh.js';
|
|
6
|
+
import { health, union } from './csg.js';
|
|
7
|
+
const G = (...lines) => lines;
|
|
8
|
+
const O_ = [[0, 0], [0, 4], [2, 4], [2, 0], [0, 0]];
|
|
9
|
+
/** The font: x 0..2, y 0..4 (the centre line of the strokes); accents rise above 4. */
|
|
10
|
+
export const GLYPHS = {
|
|
11
|
+
A: G([[0, 0], [0, 4], [2, 4], [2, 0]], [[0, 2], [2, 2]]),
|
|
12
|
+
B: G(O_, [[0, 2], [2, 2]]),
|
|
13
|
+
C: G([[2, 4], [0, 4], [0, 0], [2, 0]]),
|
|
14
|
+
D: G([[0, 0], [0, 4], [1, 4], [2, 3], [2, 1], [1, 0], [0, 0]]),
|
|
15
|
+
E: G([[2, 4], [0, 4], [0, 0], [2, 0]], [[0, 2], [1.5, 2]]),
|
|
16
|
+
F: G([[2, 4], [0, 4], [0, 0]], [[0, 2], [1.5, 2]]),
|
|
17
|
+
G: G([[2, 4], [0, 4], [0, 0], [2, 0], [2, 2], [1, 2]]),
|
|
18
|
+
H: G([[0, 0], [0, 4]], [[2, 0], [2, 4]], [[0, 2], [2, 2]]),
|
|
19
|
+
I: G([[0, 4], [2, 4]], [[1, 4], [1, 0]], [[0, 0], [2, 0]]),
|
|
20
|
+
J: G([[2, 4], [2, 0], [0, 0], [0, 1]]),
|
|
21
|
+
K: G([[0, 0], [0, 4]], [[2, 4], [0, 2], [2, 0]]),
|
|
22
|
+
L: G([[0, 4], [0, 0], [2, 0]]),
|
|
23
|
+
M: G([[0, 0], [0, 4], [1, 2], [2, 4], [2, 0]]),
|
|
24
|
+
N: G([[0, 0], [0, 4], [2, 0], [2, 4]]),
|
|
25
|
+
O: G(O_),
|
|
26
|
+
P: G([[0, 0], [0, 4], [2, 4], [2, 2], [0, 2]]),
|
|
27
|
+
Q: G(O_, [[1, 1], [2, 0]]),
|
|
28
|
+
R: G([[0, 0], [0, 4], [2, 4], [2, 2], [0, 2]], [[1, 2], [2, 0]]),
|
|
29
|
+
S: G([[2, 4], [0, 4], [0, 2], [2, 2], [2, 0], [0, 0]]),
|
|
30
|
+
T: G([[0, 4], [2, 4]], [[1, 4], [1, 0]]),
|
|
31
|
+
U: G([[0, 4], [0, 0], [2, 0], [2, 4]]),
|
|
32
|
+
V: G([[0, 4], [1, 0], [2, 4]]),
|
|
33
|
+
W: G([[0, 4], [0.5, 0], [1, 2], [1.5, 0], [2, 4]]),
|
|
34
|
+
X: G([[0, 4], [2, 0]], [[0, 0], [2, 4]]),
|
|
35
|
+
Y: G([[0, 4], [1, 2], [2, 4]], [[1, 2], [1, 0]]),
|
|
36
|
+
Z: G([[0, 4], [2, 4], [0, 0], [2, 0]]),
|
|
37
|
+
'0': G(O_, [[0, 0], [2, 4]]),
|
|
38
|
+
'1': G([[0, 3], [1, 4], [1, 0]]),
|
|
39
|
+
'2': G([[0, 4], [2, 4], [2, 2], [0, 2], [0, 0], [2, 0]]),
|
|
40
|
+
'3': G([[0, 4], [2, 4], [2, 0], [0, 0]], [[0, 2], [2, 2]]),
|
|
41
|
+
'4': G([[0, 4], [0, 2], [2, 2]], [[2, 4], [2, 0]]),
|
|
42
|
+
'5': G([[2, 4], [0, 4], [0, 2], [2, 2], [2, 0], [0, 0]]),
|
|
43
|
+
'6': G([[2, 4], [0, 4], [0, 0], [2, 0], [2, 2], [0, 2]]),
|
|
44
|
+
'7': G([[0, 4], [2, 4], [0, 0]]),
|
|
45
|
+
'8': G(O_, [[0, 2], [2, 2]]),
|
|
46
|
+
'9': G([[2, 2], [0, 2], [0, 4], [2, 4], [2, 0], [0, 0]]),
|
|
47
|
+
'.': G([[1, 0]]),
|
|
48
|
+
',': G([[1, 0], [0.5, -1]]),
|
|
49
|
+
':': G([[1, 0]], [[1, 2.5]]),
|
|
50
|
+
'!': G([[1, 4], [1, 1.5]], [[1, 0]]),
|
|
51
|
+
'?': G([[0, 4], [2, 4], [2, 2], [1, 2], [1, 1]], [[1, 0]]),
|
|
52
|
+
'-': G([[0, 2], [2, 2]]),
|
|
53
|
+
_: G([[0, 0], [2, 0]]),
|
|
54
|
+
'+': G([[0, 2], [2, 2]], [[1, 3], [1, 1]]),
|
|
55
|
+
'/': G([[0, 0], [2, 4]]),
|
|
56
|
+
"'": G([[1, 4], [1, 3]]),
|
|
57
|
+
' ': [],
|
|
58
|
+
};
|
|
59
|
+
// Hungarian (and other) accents: a letter plus a mark above it
|
|
60
|
+
const ACUTE = G([[0.7, 5.4], [1.3, 6.4]]);
|
|
61
|
+
const DOUBLE = G([[0, 5.4], [0.6, 6.4]], [[1.4, 5.4], [2, 6.4]]);
|
|
62
|
+
const DOTS = G([[0, 5.6]], [[2, 5.6]]);
|
|
63
|
+
const ACCENTS = {
|
|
64
|
+
Á: ['A', ACUTE], É: ['E', ACUTE], Í: ['I', ACUTE], Ó: ['O', ACUTE], Ú: ['U', ACUTE],
|
|
65
|
+
Ö: ['O', DOTS], Ü: ['U', DOTS], Ő: ['O', DOUBLE], Ű: ['U', DOUBLE],
|
|
66
|
+
};
|
|
67
|
+
/** Grid units: the centre line is 4 high, the stroke 1: the letter is 5 high, so the stroke is 20 % of it. */
|
|
68
|
+
export const STROKE = 0.2;
|
|
69
|
+
const CELL_H = 5;
|
|
70
|
+
const ADVANCE = 4; // 3 wide letter + 1 gap
|
|
71
|
+
const SPACE = 3;
|
|
72
|
+
/** The frame of a face: it starts in the lower left corner (up = world up on the face; on a floor or ceiling: away from the viewer), and its height is the face's height. */
|
|
73
|
+
export const faceFrame = (mesh, face) => {
|
|
74
|
+
const f = mesh.faces[face];
|
|
75
|
+
const n = norm(faceNormal(f.v.map((i) => mesh.positions[i])));
|
|
76
|
+
let up = sub([0, 1, 0], scale(n, dot([0, 1, 0], n)));
|
|
77
|
+
if (Math.hypot(...up) < 1e-3)
|
|
78
|
+
up = sub([0, 0, -1], scale(n, dot([0, 0, -1], n)));
|
|
79
|
+
up = norm(up);
|
|
80
|
+
const right = norm(cross(up, n));
|
|
81
|
+
let lo = Infinity, hi = -Infinity, left = Infinity;
|
|
82
|
+
for (const i of f.v) {
|
|
83
|
+
const p = mesh.positions[i];
|
|
84
|
+
const u = dot(p, up);
|
|
85
|
+
lo = Math.min(lo, u);
|
|
86
|
+
hi = Math.max(hi, u);
|
|
87
|
+
left = Math.min(left, dot(p, right));
|
|
88
|
+
}
|
|
89
|
+
const p0 = mesh.positions[f.v[0]];
|
|
90
|
+
// the point of the face plane with the lowest up and the lowest right
|
|
91
|
+
const origin = add(add(scale(right, left), scale(up, lo)), scale(n, dot(p0, n)));
|
|
92
|
+
return { origin, right, up, normal: n, height: Math.max(1e-6, hi - lo) };
|
|
93
|
+
};
|
|
94
|
+
const strokesOf = (ch) => {
|
|
95
|
+
const up = ch.toUpperCase();
|
|
96
|
+
const acc = ACCENTS[up];
|
|
97
|
+
if (acc)
|
|
98
|
+
return [{ at: 0, lines: GLYPHS[acc[0]] }, { at: 0, lines: acc[1] }];
|
|
99
|
+
const g = GLYPHS[up];
|
|
100
|
+
return g ? [{ at: 0, lines: g }] : [];
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Faces that lie in one plane and touch along an edge become one face (the boolean cuts the big sides into pieces; a letter wants
|
|
104
|
+
* plain sides). Only when the result stays convex and the two have the same material. `edge(u, v)`, when given, must accept every
|
|
105
|
+
* shared edge of a pair (the viewer's mirror joins only along its plane).
|
|
106
|
+
*/
|
|
107
|
+
export const mergeFlat = (mesh, { edge } = {}) => {
|
|
108
|
+
const P = mesh.positions;
|
|
109
|
+
let faces = mesh.faces.map((f) => ({ ...f, n: norm(faceNormal(f.v.map((i) => P[i]))) }));
|
|
110
|
+
const tryMerge = (a, b) => {
|
|
111
|
+
if (a.mat !== b.mat || a.color !== b.color || dot(a.n, b.n) < 1 - 1e-9)
|
|
112
|
+
return null;
|
|
113
|
+
const bAt = new Map();
|
|
114
|
+
b.v.forEach((v, i) => bAt.set(`${v},${b.v[(i + 1) % b.v.length]}`, i));
|
|
115
|
+
let shared = 0;
|
|
116
|
+
let start = -1;
|
|
117
|
+
a.v.forEach((u, i) => {
|
|
118
|
+
const w = a.v[(i + 1) % a.v.length];
|
|
119
|
+
if (bAt.has(`${w},${u}`)) {
|
|
120
|
+
shared++;
|
|
121
|
+
start = i;
|
|
122
|
+
if (edge && !edge(u, w))
|
|
123
|
+
shared = -Infinity;
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
if (!(shared > 0))
|
|
127
|
+
return null;
|
|
128
|
+
// the shared edge u -> v of a (v -> u in b): a from v round to u, then b from u round to v
|
|
129
|
+
const u = a.v[start];
|
|
130
|
+
const v = a.v[(start + 1) % a.v.length];
|
|
131
|
+
const ai = (start + 1) % a.v.length;
|
|
132
|
+
const A = a.v.map((_, k) => a.v[(ai + k) % a.v.length]); // v ... u
|
|
133
|
+
const bi = bAt.get(`${v},${u}`) + 1; // u in b
|
|
134
|
+
const B = b.v.map((_, k) => b.v[(bi + k) % b.v.length]); // u ... v
|
|
135
|
+
const out = [...A, ...B.slice(1, -1)];
|
|
136
|
+
// spikes: a vertex between two copies of the same neighbour (several shared edges in a row)
|
|
137
|
+
for (let again = true; again && out.length > 2;) {
|
|
138
|
+
again = false;
|
|
139
|
+
for (let i = 0; i < out.length; i++) {
|
|
140
|
+
if (out[(i + out.length - 1) % out.length] === out[(i + 1) % out.length]) {
|
|
141
|
+
out.splice(i, 1);
|
|
142
|
+
out.splice(i % out.length, 1);
|
|
143
|
+
again = true;
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (out.length !== a.v.length + b.v.length - 2 * shared || new Set(out).size !== out.length)
|
|
149
|
+
return null;
|
|
150
|
+
// the engine's faces are convex (picking and painting assume it): a merge that would bend a face is not made
|
|
151
|
+
for (let i = 0; i < out.length; i++) {
|
|
152
|
+
const p0 = P[out[i]];
|
|
153
|
+
const p1 = P[out[(i + 1) % out.length]];
|
|
154
|
+
const p2 = P[out[(i + 2) % out.length]];
|
|
155
|
+
if (dot(cross(sub(p1, p0), sub(p2, p1)), a.n) < -1e-9)
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
return out;
|
|
159
|
+
};
|
|
160
|
+
for (let again = true; again;) {
|
|
161
|
+
again = false;
|
|
162
|
+
outer: for (let i = 0; i < faces.length; i++) {
|
|
163
|
+
for (let j = i + 1; j < faces.length; j++) {
|
|
164
|
+
const m = tryMerge(faces[i], faces[j]);
|
|
165
|
+
if (!m)
|
|
166
|
+
continue;
|
|
167
|
+
faces = faces.filter((_, k) => k !== j);
|
|
168
|
+
faces[i] = { ...faces[i], v: m };
|
|
169
|
+
again = true;
|
|
170
|
+
break outer;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return { positions: P, faces: faces.map(({ n: _n, ...f }) => f) };
|
|
175
|
+
};
|
|
176
|
+
/** The bars of one letter fused into plain closed solids (no bar showing through another); if the fusing fails, the loose bars. */
|
|
177
|
+
const solid = (bars) => {
|
|
178
|
+
if (bars.length < 2)
|
|
179
|
+
return merge(...bars);
|
|
180
|
+
try {
|
|
181
|
+
let m = bars[0];
|
|
182
|
+
for (const b of bars.slice(1))
|
|
183
|
+
m = union(m, b);
|
|
184
|
+
const h = health(m);
|
|
185
|
+
if (h.allSolid && h.skippedFaces === 0)
|
|
186
|
+
return mergeFlat(m);
|
|
187
|
+
}
|
|
188
|
+
catch {
|
|
189
|
+
/* fall through */
|
|
190
|
+
}
|
|
191
|
+
return merge(...bars);
|
|
192
|
+
};
|
|
193
|
+
/** The text as one mesh: every stroke a bar of stroke × stroke section standing out of the face by the stroke width. */
|
|
194
|
+
export const textMesh = (text, frame, { mat, color = '#c8c8c8' } = {}) => {
|
|
195
|
+
const s = frame.height / CELL_H; // world units per grid unit
|
|
196
|
+
const letters = [];
|
|
197
|
+
let x = 0;
|
|
198
|
+
const half = 0.5; // the outer edge of the letter starts half a stroke before the centre line
|
|
199
|
+
for (const ch of text) {
|
|
200
|
+
if (ch === ' ') {
|
|
201
|
+
x += SPACE;
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
const bars = [];
|
|
205
|
+
for (const part of strokesOf(ch)) {
|
|
206
|
+
for (const line of part.lines) {
|
|
207
|
+
const pts = line.length === 1 ? [line[0], line[0]] : line;
|
|
208
|
+
for (let i = 0; i + 1 < pts.length; i++) {
|
|
209
|
+
const [ax, ay] = pts[i];
|
|
210
|
+
const [bx, by] = pts[i + 1];
|
|
211
|
+
const dx = bx - ax;
|
|
212
|
+
const dy = by - ay;
|
|
213
|
+
const len = Math.hypot(dx, dy);
|
|
214
|
+
const dir = len ? add(scale(frame.right, dx / len), scale(frame.up, dy / len)) : frame.right;
|
|
215
|
+
const ez = frame.normal;
|
|
216
|
+
const ey = cross(ez, dir);
|
|
217
|
+
const cx = x + half + (ax + bx) / 2;
|
|
218
|
+
const cy = half + (ay + by) / 2;
|
|
219
|
+
const at = add(add(add(frame.origin, scale(frame.right, cx * s)), scale(frame.up, cy * s)), scale(ez, (1 * s) / 2));
|
|
220
|
+
const bar = box({ size: [(len + 1) * s, s, s], at, axes: [dir, ey, ez], color });
|
|
221
|
+
if (mat)
|
|
222
|
+
bar.faces.forEach((f) => (f.mat = mat));
|
|
223
|
+
bars.push(bar);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
letters.push(solid(bars));
|
|
228
|
+
x += ADVANCE;
|
|
229
|
+
}
|
|
230
|
+
return merge(...letters);
|
|
231
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Triangles of one polygon face. A convex polygon (and anything with 3 or 4 well-behaved corners) is a plain fan from its first
|
|
2
|
+
// corner; a concave one (an L, a V, a star: an outline that was drawn or filled by hand) is cut by ear clipping, so no triangle ever
|
|
3
|
+
// lies outside the outline. The triangles keep the winding of the polygon.
|
|
4
|
+
import { faceNormal } from './mesh.js';
|
|
5
|
+
const fan = (n) => Array.from({ length: Math.max(0, n - 2) }, (_, j) => [0, j + 1, j + 2]);
|
|
6
|
+
/** Indices into `pts`, three per triangle, `pts.length - 2` triangles. */
|
|
7
|
+
export const triangulate = (pts) => {
|
|
8
|
+
const n = pts.length;
|
|
9
|
+
if (n <= 3)
|
|
10
|
+
return fan(n);
|
|
11
|
+
const nrm = faceNormal(pts);
|
|
12
|
+
const big = Math.max(Math.abs(nrm[0]), Math.abs(nrm[1]), Math.abs(nrm[2]));
|
|
13
|
+
if (big < 1e-12)
|
|
14
|
+
return fan(n); // no plane to speak of: nothing better than the fan
|
|
15
|
+
// the plane is dropped to 2D along the normal's biggest axis; `sign` keeps the polygon counter-clockwise there
|
|
16
|
+
const axis = big === Math.abs(nrm[0]) ? 0 : big === Math.abs(nrm[1]) ? 1 : 2;
|
|
17
|
+
const [iu, iv] = axis === 0 ? [1, 2] : axis === 1 ? [2, 0] : [0, 1];
|
|
18
|
+
const sign = nrm[axis] >= 0 ? 1 : -1;
|
|
19
|
+
const P = pts.map((p) => [p[iu] * sign, p[iv]]);
|
|
20
|
+
const turn = (a, b, c) => (P[b][0] - P[a][0]) * (P[c][1] - P[a][1]) - (P[b][1] - P[a][1]) * (P[c][0] - P[a][0]);
|
|
21
|
+
let scale = 0;
|
|
22
|
+
for (const p of P)
|
|
23
|
+
scale = Math.max(scale, Math.abs(p[0]), Math.abs(p[1]));
|
|
24
|
+
const eps = 1e-12 * scale * scale;
|
|
25
|
+
let convex = true;
|
|
26
|
+
for (let i = 0; i < n && convex; i++)
|
|
27
|
+
if (turn(i, (i + 1) % n, (i + 2) % n) < -eps)
|
|
28
|
+
convex = false;
|
|
29
|
+
if (convex)
|
|
30
|
+
return fan(n);
|
|
31
|
+
const inside = (p, a, b, c) => {
|
|
32
|
+
// on or in the triangle (a, b, c), which is counter-clockwise; corners at the same place as a, b or c do not count
|
|
33
|
+
for (const q of [a, b, c])
|
|
34
|
+
if (Math.abs(P[p][0] - P[q][0]) <= 1e-12 * scale && Math.abs(P[p][1] - P[q][1]) <= 1e-12 * scale)
|
|
35
|
+
return false;
|
|
36
|
+
return turn(a, b, p) >= -eps && turn(b, c, p) >= -eps && turn(c, a, p) >= -eps;
|
|
37
|
+
};
|
|
38
|
+
const rest = pts.map((_, i) => i);
|
|
39
|
+
const out = [];
|
|
40
|
+
let guard = 0;
|
|
41
|
+
while (rest.length > 3 && guard++ < n * n) {
|
|
42
|
+
let clipped = false;
|
|
43
|
+
for (let k = 0; k < rest.length; k++) {
|
|
44
|
+
const a = rest[(k + rest.length - 1) % rest.length];
|
|
45
|
+
const b = rest[k];
|
|
46
|
+
const c = rest[(k + 1) % rest.length];
|
|
47
|
+
if (turn(a, b, c) <= eps)
|
|
48
|
+
continue; // a reflex (or flat) corner is not an ear
|
|
49
|
+
if (rest.some((p) => p !== a && p !== b && p !== c && inside(p, a, b, c)))
|
|
50
|
+
continue;
|
|
51
|
+
out.push([a, b, c]);
|
|
52
|
+
rest.splice(k, 1);
|
|
53
|
+
clipped = true;
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
if (!clipped) {
|
|
57
|
+
// a self-touching or degenerate outline: take the flattest corner off so the loop always ends
|
|
58
|
+
let k = 0;
|
|
59
|
+
let best = -Infinity;
|
|
60
|
+
for (let i = 0; i < rest.length; i++) {
|
|
61
|
+
const t = turn(rest[(i + rest.length - 1) % rest.length], rest[i], rest[(i + 1) % rest.length]);
|
|
62
|
+
if (t > best)
|
|
63
|
+
((best = t), (k = i));
|
|
64
|
+
}
|
|
65
|
+
out.push([rest[(k + rest.length - 1) % rest.length], rest[k], rest[(k + 1) % rest.length]]);
|
|
66
|
+
rest.splice(k, 1);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (rest.length === 3)
|
|
70
|
+
out.push([rest[0], rest[1], rest[2]]);
|
|
71
|
+
return out;
|
|
72
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Tiny vec3 helpers on plain [x, y, z] arrays.
|
|
2
|
+
// The curried ones (plus, minus, times) are made for pipelines: `b |> minus(a) |> norm`.
|
|
3
|
+
export const add = (a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
|
|
4
|
+
export const sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
|
|
5
|
+
export const scale = (a, s) => [a[0] * s, a[1] * s, a[2] * s];
|
|
6
|
+
export const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
7
|
+
export const cross = (a, b) => [
|
|
8
|
+
a[1] * b[2] - a[2] * b[1],
|
|
9
|
+
a[2] * b[0] - a[0] * b[2],
|
|
10
|
+
a[0] * b[1] - a[1] * b[0],
|
|
11
|
+
];
|
|
12
|
+
export const len = (a) => Math.hypot(a[0], a[1], a[2]);
|
|
13
|
+
export const norm = (a) => {
|
|
14
|
+
const l = len(a);
|
|
15
|
+
return l ? scale(a, 1 / l) : [0, 0, 0];
|
|
16
|
+
};
|
|
17
|
+
/** `a |> plus(b)` is a + b. */
|
|
18
|
+
export const plus = (b) => (a) => add(a, b);
|
|
19
|
+
/** `a |> minus(b)` is a - b. */
|
|
20
|
+
export const minus = (b) => (a) => sub(a, b);
|
|
21
|
+
/** `a |> times(s)` is a * s. */
|
|
22
|
+
export const times = (s) => (a) => scale(a, s);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Versions are date based and stored as semver YY.M.DNNN (see scripts/bump.js).
|
|
2
|
+
// This gives the readable form: '26.11.1001' -> '26.11.01.001'.
|
|
3
|
+
export const displayVersion = (version) => {
|
|
4
|
+
const [yy, mm, p] = version.split('.').map(Number);
|
|
5
|
+
const pad = (n, w) => String(n).padStart(w, '0');
|
|
6
|
+
return `${pad(yy, 2)}.${pad(mm, 2)}.${pad(Math.floor(p / 1000), 2)}.${pad(p % 1000, 3)}`;
|
|
7
|
+
};
|