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,83 @@
|
|
|
1
|
+
// Model checks an AI can read as text: is the mesh closed, are the faces CCW from
|
|
2
|
+
// outside, planar and convex (picking and the painter assume that).
|
|
3
|
+
import { sub, dot, cross, len, norm } from './vec.js';
|
|
4
|
+
import { faceNormal, bounds } from './mesh.js';
|
|
5
|
+
/**
|
|
6
|
+
* { vertices, faces, closed, openEdges, volume, inside_out, degenerateFaces,
|
|
7
|
+
* nonPlanarFaces, nonConvexFaces, ok, notes }
|
|
8
|
+
* `volume` and `inside_out` only mean something for a closed mesh.
|
|
9
|
+
*/
|
|
10
|
+
export const validate = (mesh) => {
|
|
11
|
+
const { positions, faces } = mesh;
|
|
12
|
+
const size = bounds(mesh).radius || 1;
|
|
13
|
+
const degenerate = [];
|
|
14
|
+
const nonPlanar = [];
|
|
15
|
+
const nonConvex = [];
|
|
16
|
+
const directed = new Map();
|
|
17
|
+
let volume = 0;
|
|
18
|
+
faces.forEach((face, i) => {
|
|
19
|
+
const pts = face.v.map((k) => positions[k]);
|
|
20
|
+
const m = pts.length;
|
|
21
|
+
const n = faceNormal(pts);
|
|
22
|
+
const area2 = len(n);
|
|
23
|
+
if (area2 < 1e-12 * size * size) {
|
|
24
|
+
degenerate.push(i);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const unit = norm(n);
|
|
28
|
+
if (Math.max(...pts.map((p) => Math.abs(dot(sub(p, pts[0]), unit)))) > 1e-6 * size)
|
|
29
|
+
nonPlanar.push(i);
|
|
30
|
+
for (let j = 0; j < m; j++) {
|
|
31
|
+
const a = pts[j];
|
|
32
|
+
const b = pts[(j + 1) % m];
|
|
33
|
+
const c = pts[(j + 2) % m];
|
|
34
|
+
if (dot(cross(sub(b, a), sub(c, b)), unit) < -1e-9 * size * size) {
|
|
35
|
+
nonConvex.push(i);
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
for (let j = 0; j < m; j++) {
|
|
40
|
+
const key = `${face.v[j]},${face.v[(j + 1) % m]}`;
|
|
41
|
+
directed.set(key, (directed.get(key) ?? 0) + 1);
|
|
42
|
+
}
|
|
43
|
+
for (let j = 1; j < m - 1; j++)
|
|
44
|
+
volume += dot(pts[0], cross(pts[j], pts[j + 1])) / 6;
|
|
45
|
+
});
|
|
46
|
+
let open = 0;
|
|
47
|
+
let repeated = 0;
|
|
48
|
+
for (const [key, count] of directed) {
|
|
49
|
+
const [a, b] = key.split(',');
|
|
50
|
+
if (count > 1)
|
|
51
|
+
repeated += 1;
|
|
52
|
+
if (!directed.has(`${b},${a}`))
|
|
53
|
+
open += 1;
|
|
54
|
+
}
|
|
55
|
+
const closed = faces.length > 0 && open === 0 && repeated === 0;
|
|
56
|
+
const insideOut = closed && volume < 0;
|
|
57
|
+
const notes = [];
|
|
58
|
+
if (degenerate.length)
|
|
59
|
+
notes.push(`degenerate faces (no area): ${degenerate.join(', ')}`);
|
|
60
|
+
if (nonPlanar.length)
|
|
61
|
+
notes.push(`non-planar faces: ${nonPlanar.join(', ')}`);
|
|
62
|
+
if (nonConvex.length)
|
|
63
|
+
notes.push(`non-convex faces (picking and painting assume convex): ${nonConvex.join(', ')}`);
|
|
64
|
+
if (repeated)
|
|
65
|
+
notes.push(`${repeated} edge(s) are used twice in the same direction: faces are wound inconsistently`);
|
|
66
|
+
if (open)
|
|
67
|
+
notes.push(`${open} open edge(s): the mesh is not closed (fine for a surface, e.g. a missing bottom)`);
|
|
68
|
+
if (insideOut)
|
|
69
|
+
notes.push('the faces point inward: reverse the vertex order of every face');
|
|
70
|
+
return {
|
|
71
|
+
vertices: positions.length,
|
|
72
|
+
faces: faces.length,
|
|
73
|
+
closed,
|
|
74
|
+
openEdges: open,
|
|
75
|
+
volume: closed ? Math.round(volume * 1e9) / 1e9 : null,
|
|
76
|
+
inside_out: insideOut,
|
|
77
|
+
degenerateFaces: degenerate,
|
|
78
|
+
nonPlanarFaces: nonPlanar,
|
|
79
|
+
nonConvexFaces: nonConvex,
|
|
80
|
+
ok: !degenerate.length && !nonPlanar.length && !nonConvex.length && !repeated && !insideOut,
|
|
81
|
+
notes,
|
|
82
|
+
};
|
|
83
|
+
};
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
// Boolean operations on closed solids: union, subtract (a - b) and intersect.
|
|
2
|
+
// A BSP tree per solid (after Evan Wallace's csg.js): polygons are cut by the planes of the other solid
|
|
3
|
+
// and the pieces inside or outside are kept, so every result face stays planar and convex, which is what
|
|
4
|
+
// the rest of the engine assumes. The result is welded and its T-junctions are closed, so it is a closed
|
|
5
|
+
// solid again and can go into the next boolean. Faces keep the color and material of the face they came from;
|
|
6
|
+
// a face cut into a solid by the other one takes the color of the cutting face.
|
|
7
|
+
import { triangulate } from './triangulate.js';
|
|
8
|
+
import { sub, dot, cross, len } from './vec.js';
|
|
9
|
+
import { faceNormal, bounds } from './mesh.js';
|
|
10
|
+
import { validate } from './check.js';
|
|
11
|
+
let CSG_EPS = 1e-6; // set per operation, relative to the size of the two solids
|
|
12
|
+
const unit = (v) => {
|
|
13
|
+
const l = len(v) || 1;
|
|
14
|
+
return [v[0] / l, v[1] / l, v[2] / l];
|
|
15
|
+
};
|
|
16
|
+
// The BSP needs convex polygons: a concave face (an L, a V, a sketch outline) goes in as the triangles of its ear clipping.
|
|
17
|
+
const polysOf = (mesh) => mesh.faces.flatMap((f) => {
|
|
18
|
+
const pts = f.v.map((k) => mesh.positions[k]);
|
|
19
|
+
const n = unit(faceNormal(pts));
|
|
20
|
+
if (len(faceNormal(pts)) < 1e-12)
|
|
21
|
+
return [];
|
|
22
|
+
const tris = triangulate(pts);
|
|
23
|
+
if (tris.every(([a, b, c], j) => a === 0 && b === j + 1 && c === j + 2))
|
|
24
|
+
return [{ pts, n, w: dot(n, pts[0]), color: f.color, mat: f.mat }];
|
|
25
|
+
return tris.map((t) => ({ pts: t.map((i) => pts[i]), n, w: dot(n, pts[0]), color: f.color, mat: f.mat }));
|
|
26
|
+
});
|
|
27
|
+
const flip = (p) => ({ ...p, pts: [...p.pts].reverse(), n: [-p.n[0], -p.n[1], -p.n[2]], w: -p.w });
|
|
28
|
+
/** Cuts `p` by the plane into the four buckets. */
|
|
29
|
+
const splitPoly = (plane, p, cf, cb, front, back) => {
|
|
30
|
+
const COPLANAR = 0;
|
|
31
|
+
const FRONT = 1;
|
|
32
|
+
const BACK = 2;
|
|
33
|
+
const SPANNING = 3;
|
|
34
|
+
let type = 0;
|
|
35
|
+
const types = p.pts.map((q) => {
|
|
36
|
+
const t = dot(plane.n, q) - plane.w;
|
|
37
|
+
const k = t < -CSG_EPS ? BACK : t > CSG_EPS ? FRONT : COPLANAR;
|
|
38
|
+
type |= k;
|
|
39
|
+
return k;
|
|
40
|
+
});
|
|
41
|
+
if (type === COPLANAR)
|
|
42
|
+
(dot(plane.n, p.n) > 0 ? cf : cb).push(p);
|
|
43
|
+
else if (type === FRONT)
|
|
44
|
+
front.push(p);
|
|
45
|
+
else if (type === BACK)
|
|
46
|
+
back.push(p);
|
|
47
|
+
else if (type === SPANNING) {
|
|
48
|
+
const f = [];
|
|
49
|
+
const b = [];
|
|
50
|
+
for (let i = 0; i < p.pts.length; i++) {
|
|
51
|
+
const j = (i + 1) % p.pts.length;
|
|
52
|
+
const ti = types[i];
|
|
53
|
+
const tj = types[j];
|
|
54
|
+
const vi = p.pts[i];
|
|
55
|
+
const vj = p.pts[j];
|
|
56
|
+
if (ti !== BACK)
|
|
57
|
+
f.push(vi);
|
|
58
|
+
if (ti !== FRONT)
|
|
59
|
+
b.push(vi);
|
|
60
|
+
if ((ti | tj) === SPANNING) {
|
|
61
|
+
const t = (plane.w - dot(plane.n, vi)) / dot(plane.n, sub(vj, vi));
|
|
62
|
+
const v = [vi[0] + (vj[0] - vi[0]) * t, vi[1] + (vj[1] - vi[1]) * t, vi[2] + (vj[2] - vi[2]) * t];
|
|
63
|
+
f.push(v);
|
|
64
|
+
b.push(v);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (f.length >= 3)
|
|
68
|
+
front.push({ ...p, pts: f });
|
|
69
|
+
if (b.length >= 3)
|
|
70
|
+
back.push({ ...p, pts: b });
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
const build = (node, polys) => {
|
|
74
|
+
if (!polys.length)
|
|
75
|
+
return;
|
|
76
|
+
if (!node.plane)
|
|
77
|
+
node.plane = { n: polys[0].n, w: polys[0].w };
|
|
78
|
+
const front = [];
|
|
79
|
+
const back = [];
|
|
80
|
+
for (const p of polys)
|
|
81
|
+
splitPoly(node.plane, p, node.polys, node.polys, front, back);
|
|
82
|
+
if (front.length)
|
|
83
|
+
build((node.front ??= { plane: null, front: null, back: null, polys: [] }), front);
|
|
84
|
+
if (back.length)
|
|
85
|
+
build((node.back ??= { plane: null, front: null, back: null, polys: [] }), back);
|
|
86
|
+
};
|
|
87
|
+
const tree = (polys) => {
|
|
88
|
+
const node = { plane: null, front: null, back: null, polys: [] };
|
|
89
|
+
build(node, polys);
|
|
90
|
+
return node;
|
|
91
|
+
};
|
|
92
|
+
const invert = (node) => {
|
|
93
|
+
node.polys = node.polys.map(flip);
|
|
94
|
+
if (node.plane)
|
|
95
|
+
node.plane = { n: [-node.plane.n[0], -node.plane.n[1], -node.plane.n[2]], w: -node.plane.w };
|
|
96
|
+
invert0(node);
|
|
97
|
+
};
|
|
98
|
+
const invert0 = (node) => {
|
|
99
|
+
if (node.front)
|
|
100
|
+
invert(node.front);
|
|
101
|
+
if (node.back)
|
|
102
|
+
invert(node.back);
|
|
103
|
+
[node.front, node.back] = [node.back, node.front];
|
|
104
|
+
};
|
|
105
|
+
/** The parts of `polys` that are outside the solid of `node`. */
|
|
106
|
+
const clipPolys = (node, polys) => {
|
|
107
|
+
if (!node.plane)
|
|
108
|
+
return [...polys];
|
|
109
|
+
let front = [];
|
|
110
|
+
let back = [];
|
|
111
|
+
for (const p of polys)
|
|
112
|
+
splitPoly(node.plane, p, front, back, front, back);
|
|
113
|
+
front = node.front ? clipPolys(node.front, front) : front;
|
|
114
|
+
back = node.back ? clipPolys(node.back, back) : [];
|
|
115
|
+
return [...front, ...back];
|
|
116
|
+
};
|
|
117
|
+
const clipTo = (node, other) => {
|
|
118
|
+
node.polys = clipPolys(other, node.polys);
|
|
119
|
+
if (node.front)
|
|
120
|
+
clipTo(node.front, other);
|
|
121
|
+
if (node.back)
|
|
122
|
+
clipTo(node.back, other);
|
|
123
|
+
};
|
|
124
|
+
const all = (node) => [...node.polys, ...(node.front ? all(node.front) : []), ...(node.back ? all(node.back) : [])];
|
|
125
|
+
/** Polygons to a welded mesh: shared vertices, no sliver faces, T-junctions closed. */
|
|
126
|
+
const toMesh = (polys, scale) => {
|
|
127
|
+
const cell = scale * 1e-5;
|
|
128
|
+
const ids = new Map();
|
|
129
|
+
const positions = [];
|
|
130
|
+
const weld = (p) => {
|
|
131
|
+
// look in the neighbouring cells too, so a point on a cell border still finds its twin
|
|
132
|
+
const base = p.map((c) => Math.round(c / cell));
|
|
133
|
+
for (let dx = -1; dx <= 1; dx++)
|
|
134
|
+
for (let dy = -1; dy <= 1; dy++)
|
|
135
|
+
for (let dz = -1; dz <= 1; dz++) {
|
|
136
|
+
const hit = ids.get(`${base[0] + dx},${base[1] + dy},${base[2] + dz}`);
|
|
137
|
+
if (hit !== undefined && len(sub(positions[hit], p)) <= cell * 1.5)
|
|
138
|
+
return hit;
|
|
139
|
+
}
|
|
140
|
+
ids.set(base.join(','), positions.length);
|
|
141
|
+
positions.push(p);
|
|
142
|
+
return positions.length - 1;
|
|
143
|
+
};
|
|
144
|
+
let faces = polys.flatMap((p) => {
|
|
145
|
+
const v = [];
|
|
146
|
+
for (const q of p.pts) {
|
|
147
|
+
const k = weld(q);
|
|
148
|
+
if (v[v.length - 1] !== k)
|
|
149
|
+
v.push(k);
|
|
150
|
+
}
|
|
151
|
+
if (v.length > 1 && v[0] === v[v.length - 1])
|
|
152
|
+
v.pop();
|
|
153
|
+
if (v.length < 3)
|
|
154
|
+
return [];
|
|
155
|
+
// slivers stay for now: their vertices still give the neighbours' edges something to join to (see below)
|
|
156
|
+
return [{ v, color: p.color, mat: p.mat }];
|
|
157
|
+
});
|
|
158
|
+
// T-junctions: a vertex lying on the edge of a neighbouring face gets inserted into that edge
|
|
159
|
+
const used = new Set(faces.flatMap((f) => f.v));
|
|
160
|
+
const verts = [...used];
|
|
161
|
+
if (verts.length * faces.length < 6e7) {
|
|
162
|
+
const tol = scale * 1e-5 * 3;
|
|
163
|
+
faces = faces.map((f) => {
|
|
164
|
+
const out = [];
|
|
165
|
+
for (let i = 0; i < f.v.length; i++) {
|
|
166
|
+
const a = f.v[i];
|
|
167
|
+
const b = f.v[(i + 1) % f.v.length];
|
|
168
|
+
out.push(a);
|
|
169
|
+
const pa = positions[a];
|
|
170
|
+
const pb = positions[b];
|
|
171
|
+
const d = sub(pb, pa);
|
|
172
|
+
const dl = len(d);
|
|
173
|
+
if (dl < tol * 4)
|
|
174
|
+
continue;
|
|
175
|
+
const on = [];
|
|
176
|
+
for (const k of verts) {
|
|
177
|
+
if (k === a || k === b || f.v.includes(k))
|
|
178
|
+
continue; // never twice in one face
|
|
179
|
+
const pk = positions[k];
|
|
180
|
+
const t = dot(sub(pk, pa), d) / (dl * dl);
|
|
181
|
+
if (t <= 0 || t >= 1)
|
|
182
|
+
continue;
|
|
183
|
+
if (len(cross(sub(pk, pa), d)) / dl <= tol)
|
|
184
|
+
on.push({ t, k });
|
|
185
|
+
}
|
|
186
|
+
on.sort((x, y) => x.t - y.t);
|
|
187
|
+
for (const { k } of on)
|
|
188
|
+
out.push(k);
|
|
189
|
+
}
|
|
190
|
+
return { ...f, v: out };
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
// Now the slivers can go: a face with no area (its corners are in a line, the neighbours already have the extra vertices)
|
|
194
|
+
// closes up around it. Dropping one before the T-junctions are done would leave a hole (an open edge) behind.
|
|
195
|
+
const flat = 1e-9 * scale * scale;
|
|
196
|
+
faces = faces.filter((f) => len(faceNormal(f.v.map((k) => positions[k]))) >= flat);
|
|
197
|
+
return { positions, faces };
|
|
198
|
+
};
|
|
199
|
+
const closedSolid = (name, m) => {
|
|
200
|
+
const v = validate(m);
|
|
201
|
+
if (!v.closed)
|
|
202
|
+
throw new Error(`boolean needs closed solids: ${name} has ${v.openEdges} open edge(s)`);
|
|
203
|
+
if (v.inside_out)
|
|
204
|
+
throw new Error(`boolean needs outward faces: ${name} is inside out`);
|
|
205
|
+
};
|
|
206
|
+
const subMesh = (mesh, idx) => {
|
|
207
|
+
const map = new Map();
|
|
208
|
+
const positions = [];
|
|
209
|
+
const faces = idx.map((i) => {
|
|
210
|
+
const f = mesh.faces[i];
|
|
211
|
+
return { ...f, v: f.v.map((k) => (map.has(k) ? map.get(k) : (map.set(k, positions.length), positions.push(mesh.positions[k]), positions.length - 1))) };
|
|
212
|
+
});
|
|
213
|
+
return { ...mesh, positions, faces };
|
|
214
|
+
};
|
|
215
|
+
/** The connected parts of a mesh as separate meshes (each with its own vertex list), in face order of their first face. */
|
|
216
|
+
export const splitParts = (mesh) => health(mesh)
|
|
217
|
+
.parts.map((p) => p.faces)
|
|
218
|
+
.sort((a, b) => a[0] - b[0])
|
|
219
|
+
.map((faces) => subMesh(mesh, faces));
|
|
220
|
+
/** Splits a mesh into connected parts and says which of them are closed solids (the health check). */
|
|
221
|
+
export const health = (mesh) => {
|
|
222
|
+
const n = mesh.faces.length;
|
|
223
|
+
const root = Array.from({ length: n }, (_, i) => i);
|
|
224
|
+
const find = (i) => (root[i] === i ? i : (root[i] = find(root[i])));
|
|
225
|
+
const seen = new Map();
|
|
226
|
+
mesh.faces.forEach((f, i) => f.v.forEach((v, k) => {
|
|
227
|
+
const w = f.v[(k + 1) % f.v.length];
|
|
228
|
+
const key = v < w ? `${v},${w}` : `${w},${v}`;
|
|
229
|
+
const j = seen.get(key);
|
|
230
|
+
if (j === undefined)
|
|
231
|
+
seen.set(key, i);
|
|
232
|
+
else
|
|
233
|
+
root[find(i)] = find(j);
|
|
234
|
+
}));
|
|
235
|
+
const groups = new Map();
|
|
236
|
+
for (let i = 0; i < n; i++)
|
|
237
|
+
groups.set(find(i), [...(groups.get(find(i)) ?? []), i]);
|
|
238
|
+
const parts = [...groups.values()].map((faces) => {
|
|
239
|
+
const v = validate(subMesh(mesh, faces));
|
|
240
|
+
return { faces, closed: v.closed, insideOut: v.inside_out, openEdges: v.openEdges };
|
|
241
|
+
});
|
|
242
|
+
const usable = parts.filter((p) => p.closed && !p.insideOut);
|
|
243
|
+
const usableFaces = usable.reduce((t, p) => t + p.faces.length, 0);
|
|
244
|
+
return { parts, usableFaces, skippedFaces: n - usableFaces, hasSolid: usable.length > 0, allSolid: usable.length === parts.length && parts.length > 0 };
|
|
245
|
+
};
|
|
246
|
+
const append = (base, extra) => {
|
|
247
|
+
const o = base.positions.length;
|
|
248
|
+
return { ...base, positions: [...base.positions, ...extra.positions], faces: [...base.faces, ...extra.faces.map((f) => ({ ...f, v: f.v.map((k) => k + o) }))] };
|
|
249
|
+
};
|
|
250
|
+
/**
|
|
251
|
+
* Boolean of two closed solids (outward CCW faces): 'union', 'subtract' (a minus b) or 'intersect'.
|
|
252
|
+
* Throws with a reason when an input is open or inside out. With `strict: false` the parts that are not closed solids are
|
|
253
|
+
* left out of the calculation instead (see `health`): a union keeps them as they are, a subtract keeps those of `a`,
|
|
254
|
+
* an intersect drops them; it throws only when `a` has no closed part at all.
|
|
255
|
+
*/
|
|
256
|
+
export const boolean = (a, b, op, { strict = true } = {}) => {
|
|
257
|
+
if (strict) {
|
|
258
|
+
closedSolid('a', a);
|
|
259
|
+
closedSolid('b', b);
|
|
260
|
+
return run(a, b, op);
|
|
261
|
+
}
|
|
262
|
+
const ha = health(a);
|
|
263
|
+
const hb = health(b);
|
|
264
|
+
if (!ha.hasSolid)
|
|
265
|
+
throw new Error('boolean: the model has no closed part (nothing to calculate with)');
|
|
266
|
+
if (!hb.hasSolid)
|
|
267
|
+
throw new Error('boolean: the second model has no closed part');
|
|
268
|
+
const pick = (m, h, closed) => subMesh(m, h.parts.filter((p) => (p.closed && !p.insideOut) === closed).flatMap((p) => p.faces).sort((x, y) => x - y));
|
|
269
|
+
let out = run(pick(a, ha, true), pick(b, hb, true), op);
|
|
270
|
+
if (ha.skippedFaces)
|
|
271
|
+
out = append(out, pick(a, ha, false));
|
|
272
|
+
if (op === 'union' && hb.skippedFaces)
|
|
273
|
+
out = append(out, pick(b, hb, false));
|
|
274
|
+
return out;
|
|
275
|
+
};
|
|
276
|
+
// A result is sound when it is a closed, outward solid with consistent winding (and the inputs were).
|
|
277
|
+
const sound = (m) => {
|
|
278
|
+
if (!m.faces.length)
|
|
279
|
+
return true; // nothing is a fine result (disjoint intersect)
|
|
280
|
+
const v = validate(m);
|
|
281
|
+
return v.closed && !v.inside_out && !v.notes.some((n) => /wound inconsistently/.test(n));
|
|
282
|
+
};
|
|
283
|
+
const shiftBy = (m, d) => ({ ...m, positions: m.positions.map((p) => [p[0] + d[0], p[1] + d[1], p[2] + d[2]]) });
|
|
284
|
+
/**
|
|
285
|
+
* BSP booleans hit numerical trouble when faces are (nearly) coincident or a cut passes within a hair of a vertex; the
|
|
286
|
+
* result then comes out with a crack or a flipped face. A solid must stay a solid, so a result that is not sound is
|
|
287
|
+
* thrown away and the operation is repeated with the second operand moved by a tiny amount (a few tries, growing).
|
|
288
|
+
* When no try gives a sound solid it throws, so the caller keeps the original.
|
|
289
|
+
*/
|
|
290
|
+
const run = (a, b, op) => {
|
|
291
|
+
const first = run0(a, b, op);
|
|
292
|
+
if (sound(first) || !sound(a) || !sound(b))
|
|
293
|
+
return first;
|
|
294
|
+
const size = Math.max(bounds(a).radius, bounds(b).radius, 1e-9);
|
|
295
|
+
for (let k = 1; k <= 12; k++) {
|
|
296
|
+
const mag = size * 1e-4 * k;
|
|
297
|
+
const d = [Math.sin(k * 12.9898) * mag, Math.sin(k * 78.233) * mag, Math.sin(k * 37.719) * mag];
|
|
298
|
+
const r = run0(a, shiftBy(b, d), op);
|
|
299
|
+
if (sound(r))
|
|
300
|
+
return r;
|
|
301
|
+
}
|
|
302
|
+
throw new Error('boolean: the result would not be a closed solid (numerical trouble); the operands were left alone');
|
|
303
|
+
};
|
|
304
|
+
const run0 = (a, b, op) => {
|
|
305
|
+
const sa = bounds(a);
|
|
306
|
+
const sb = bounds(b);
|
|
307
|
+
const scale = Math.max(sa.radius, sb.radius, 1e-9) * 2 + len(sub(sa.center, sb.center));
|
|
308
|
+
CSG_EPS = scale * 1e-9 * 1000;
|
|
309
|
+
const A = tree(polysOf(a));
|
|
310
|
+
const B = tree(polysOf(b));
|
|
311
|
+
if (op === 'union') {
|
|
312
|
+
clipTo(A, B);
|
|
313
|
+
clipTo(B, A);
|
|
314
|
+
invert(B);
|
|
315
|
+
clipTo(B, A);
|
|
316
|
+
invert(B);
|
|
317
|
+
build(A, all(B));
|
|
318
|
+
}
|
|
319
|
+
else if (op === 'subtract') {
|
|
320
|
+
invert(A);
|
|
321
|
+
clipTo(A, B);
|
|
322
|
+
clipTo(B, A);
|
|
323
|
+
invert(B);
|
|
324
|
+
clipTo(B, A);
|
|
325
|
+
invert(B);
|
|
326
|
+
build(A, all(B));
|
|
327
|
+
invert(A);
|
|
328
|
+
}
|
|
329
|
+
else {
|
|
330
|
+
invert(A);
|
|
331
|
+
clipTo(B, A);
|
|
332
|
+
invert(B);
|
|
333
|
+
clipTo(A, B);
|
|
334
|
+
clipTo(B, A);
|
|
335
|
+
build(A, all(B));
|
|
336
|
+
invert(A);
|
|
337
|
+
}
|
|
338
|
+
return toMesh(all(A), scale);
|
|
339
|
+
};
|
|
340
|
+
export const union = (a, b) => boolean(a, b, 'union');
|
|
341
|
+
export const subtract = (a, b) => boolean(a, b, 'subtract');
|
|
342
|
+
export const intersect = (a, b) => boolean(a, b, 'intersect');
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Shaping an existing mesh: the topology edits a modeller does by hand.
|
|
2
|
+
// Both functions return a NEW mesh; faces stay CCW from outside and closed meshes stay closed.
|
|
3
|
+
import { add, sub, scale as mul, norm } from './vec.js';
|
|
4
|
+
import { faceNormal, faceCenter } from './mesh.js';
|
|
5
|
+
/**
|
|
6
|
+
* Extrude a face: it is replaced (same index) by a copy moved `distance` along its
|
|
7
|
+
* normal and scaled by `scale` about its center, and one quad per edge joins the two.
|
|
8
|
+
* distance > 0, scale 1 a prism out of the face
|
|
9
|
+
* distance > 0, scale < 1 a taper (roof, spire, frustum)
|
|
10
|
+
* distance 0, scale < 1 an inset (a panel, a window frame)
|
|
11
|
+
* distance < 0 a dent into the solid (a niche, a door recess)
|
|
12
|
+
* The new quads are appended after the existing faces; `color` recolors the cap and
|
|
13
|
+
* the quads, otherwise they keep the face color.
|
|
14
|
+
* Returns { mesh, cap, sides } with the index of the cap face and the new quad indices.
|
|
15
|
+
*/
|
|
16
|
+
export const extrudeFace = (mesh, index, { distance = 0, scale = 1, color } = {}) => {
|
|
17
|
+
const face = mesh.faces[index];
|
|
18
|
+
if (!face)
|
|
19
|
+
throw new Error(`no face ${index}: this mesh has ${mesh.faces.length} faces`);
|
|
20
|
+
if (!(scale > 0))
|
|
21
|
+
throw new Error('scale must be positive');
|
|
22
|
+
if (distance === 0 && scale === 1)
|
|
23
|
+
throw new Error('nothing to do: give a distance or a scale other than 1');
|
|
24
|
+
const pts = face.v.map((k) => mesh.positions[k]);
|
|
25
|
+
const n = norm(faceNormal(pts));
|
|
26
|
+
const c = faceCenter(pts);
|
|
27
|
+
const moved = pts.map((p) => add(add(c, mul(sub(p, c), scale)), mul(n, distance)));
|
|
28
|
+
const base = mesh.positions.length;
|
|
29
|
+
const m = pts.length;
|
|
30
|
+
const tint = color ?? face.color;
|
|
31
|
+
const faces = mesh.faces.map((f, i) => (i === index ? { ...f, v: moved.map((_, j) => base + j), color: tint } : f));
|
|
32
|
+
const sides = [];
|
|
33
|
+
for (let i = 0; i < m; i++) {
|
|
34
|
+
sides.push(faces.length);
|
|
35
|
+
faces.push({ v: [face.v[i], face.v[(i + 1) % m], base + ((i + 1) % m), base + i], color: tint });
|
|
36
|
+
}
|
|
37
|
+
return { mesh: { positions: [...mesh.positions, ...moved], faces }, cap: index, sides };
|
|
38
|
+
};
|
|
39
|
+
/** Move the vertices at `indices` by `delta` [dx, dy, dz]. Faces keep their vertices, so faces may stop being planar: check with validate(). */
|
|
40
|
+
export const moveVertices = (mesh, indices, delta) => {
|
|
41
|
+
const set = new Set();
|
|
42
|
+
for (const i of indices) {
|
|
43
|
+
if (!Number.isInteger(i) || i < 0 || i >= mesh.positions.length)
|
|
44
|
+
throw new Error(`vertex ${i} does not exist: this mesh has ${mesh.positions.length} vertices`);
|
|
45
|
+
set.add(i);
|
|
46
|
+
}
|
|
47
|
+
return { ...mesh, positions: mesh.positions.map((p, i) => (set.has(i) ? add(p, delta) : p)) };
|
|
48
|
+
};
|