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,131 @@
|
|
|
1
|
+
// Mesh = { positions: [[x, y, z], ...], faces: [{ v: [i, j, k, ...], color }] }
|
|
2
|
+
// Faces are CCW when seen from outside. No normals, UVs or textures yet.
|
|
3
|
+
import { add, len, scale, sub } from './vec.js';
|
|
4
|
+
/** Box: 8 points, 12 edges, 6 quad faces. Axis-aligned unless `axes` turns it. */
|
|
5
|
+
export const box = ({ size = [1, 1, 1], at = [0, 0, 0], color = '#c8c8c8', axes } = {}) => {
|
|
6
|
+
const [hx, hy, hz] = size.map((s) => s / 2);
|
|
7
|
+
// `axes` = [ex, ey, ez], an orthonormal right-handed frame (default: x, y, z)
|
|
8
|
+
const [ex, ey, ez] = axes ?? [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
|
|
9
|
+
const at3 = (x, y, z) => add(at, add(add(scale(ex, x), scale(ey, y)), scale(ez, z)));
|
|
10
|
+
const positions = [
|
|
11
|
+
at3(-hx, -hy, -hz), // 0
|
|
12
|
+
at3(hx, -hy, -hz), // 1
|
|
13
|
+
at3(hx, hy, -hz), // 2
|
|
14
|
+
at3(-hx, hy, -hz), // 3
|
|
15
|
+
at3(-hx, -hy, hz), // 4
|
|
16
|
+
at3(hx, -hy, hz), // 5
|
|
17
|
+
at3(hx, hy, hz), // 6
|
|
18
|
+
at3(-hx, hy, hz), // 7
|
|
19
|
+
];
|
|
20
|
+
const faces = [
|
|
21
|
+
[4, 5, 6, 7], // +z
|
|
22
|
+
[1, 0, 3, 2], // -z
|
|
23
|
+
[1, 2, 6, 5], // +x
|
|
24
|
+
[0, 4, 7, 3], // -x
|
|
25
|
+
[3, 7, 6, 2], // +y
|
|
26
|
+
[0, 1, 5, 4], // -y
|
|
27
|
+
].map((v) => ({ v, color }));
|
|
28
|
+
return { positions, faces };
|
|
29
|
+
};
|
|
30
|
+
/** Concatenate meshes into one, re-indexing the faces. */
|
|
31
|
+
export const merge = (...meshes) => {
|
|
32
|
+
const positions = [];
|
|
33
|
+
const faces = [];
|
|
34
|
+
for (const m of meshes) {
|
|
35
|
+
const offset = positions.length;
|
|
36
|
+
for (const q of m.positions)
|
|
37
|
+
positions.push(q); // (no spread: a big mesh would overflow the call stack)
|
|
38
|
+
for (const f of m.faces)
|
|
39
|
+
faces.push({ ...f, v: f.v.map((i) => i + offset) });
|
|
40
|
+
}
|
|
41
|
+
return { positions, faces };
|
|
42
|
+
};
|
|
43
|
+
/** Bounding box center and bounding sphere radius around that center. */
|
|
44
|
+
export const bounds = (mesh) => {
|
|
45
|
+
if (!mesh.positions.length)
|
|
46
|
+
return { center: [0, 0, 0], radius: 0 };
|
|
47
|
+
const min = [Infinity, Infinity, Infinity];
|
|
48
|
+
const max = [-Infinity, -Infinity, -Infinity];
|
|
49
|
+
for (const p of mesh.positions) {
|
|
50
|
+
for (let a = 0; a < 3; a++) {
|
|
51
|
+
if (p[a] < min[a])
|
|
52
|
+
min[a] = p[a];
|
|
53
|
+
if (p[a] > max[a])
|
|
54
|
+
max[a] = p[a];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const center = [(min[0] + max[0]) / 2, (min[1] + max[1]) / 2, (min[2] + max[2]) / 2];
|
|
58
|
+
let radius = 0;
|
|
59
|
+
for (const p of mesh.positions)
|
|
60
|
+
radius = Math.max(radius, len(sub(p, center)));
|
|
61
|
+
return { center, radius };
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Turntable rotation: yaw around Y first, then pitch around X (radians),
|
|
65
|
+
* both around `center` (default: bounding-box center). Faces are shared.
|
|
66
|
+
*/
|
|
67
|
+
export const rotate = (mesh, { yaw = 0, pitch = 0 } = {}, center = bounds(mesh).center) => {
|
|
68
|
+
const cy = Math.cos(yaw);
|
|
69
|
+
const sy = Math.sin(yaw);
|
|
70
|
+
const cp = Math.cos(pitch);
|
|
71
|
+
const sp = Math.sin(pitch);
|
|
72
|
+
const positions = mesh.positions.map((p) => {
|
|
73
|
+
const x = p[0] - center[0];
|
|
74
|
+
const y = p[1] - center[1];
|
|
75
|
+
const z = p[2] - center[2];
|
|
76
|
+
const x1 = cy * x + sy * z;
|
|
77
|
+
const z1 = -sy * x + cy * z;
|
|
78
|
+
return [x1 + center[0], cp * y - sp * z1 + center[1], sp * y + cp * z1 + center[2]];
|
|
79
|
+
});
|
|
80
|
+
return { positions, faces: mesh.faces };
|
|
81
|
+
};
|
|
82
|
+
/** Face normal (not normalized) by Newell's method; points CCW => outward. */
|
|
83
|
+
export const faceNormal = (pts) => {
|
|
84
|
+
const n = [0, 0, 0];
|
|
85
|
+
for (let i = 0; i < pts.length; i++) {
|
|
86
|
+
const a = pts[i];
|
|
87
|
+
const b = pts[(i + 1) % pts.length];
|
|
88
|
+
n[0] += (a[1] - b[1]) * (a[2] + b[2]);
|
|
89
|
+
n[1] += (a[2] - b[2]) * (a[0] + b[0]);
|
|
90
|
+
n[2] += (a[0] - b[0]) * (a[1] + b[1]);
|
|
91
|
+
}
|
|
92
|
+
return n;
|
|
93
|
+
};
|
|
94
|
+
/** Mean of the face points. */
|
|
95
|
+
export const faceCenter = (pts) => {
|
|
96
|
+
const s = pts.reduce((acc, p) => add(acc, p), [0, 0, 0]);
|
|
97
|
+
return [s[0] / pts.length, s[1] / pts.length, s[2] / pts.length];
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Prism from a convex footprint: `polygon` is [[x, z], ...] on the ground plane (any
|
|
101
|
+
* winding), it is extruded up from `y` by `height`. Faces: top, bottom, then one
|
|
102
|
+
* quad per footprint edge, all CCW from outside.
|
|
103
|
+
*/
|
|
104
|
+
export const extrude = ({ polygon, height = 1, y = 0, color = '#c8c8c8' }) => {
|
|
105
|
+
if (!Array.isArray(polygon) || polygon.length < 3)
|
|
106
|
+
throw new Error('polygon needs at least 3 [x, z] points');
|
|
107
|
+
if (!(height > 0))
|
|
108
|
+
throw new Error('height must be positive');
|
|
109
|
+
let pts = polygon.map((p) => [p[0], p[1]]);
|
|
110
|
+
const n = pts.length;
|
|
111
|
+
const turn = (a, b, c) => (b[1] - a[1]) * (c[0] - b[0]) - (b[0] - a[0]) * (c[1] - b[1]); // y of edge cross, + is CCW from above
|
|
112
|
+
let area = 0;
|
|
113
|
+
for (let i = 0; i < n; i++)
|
|
114
|
+
area += pts[i][1] * pts[(i + 1) % n][0] - pts[i][0] * pts[(i + 1) % n][1];
|
|
115
|
+
if (Math.abs(area) < 1e-12)
|
|
116
|
+
throw new Error('polygon has no area');
|
|
117
|
+
if (area < 0)
|
|
118
|
+
pts = pts.reverse();
|
|
119
|
+
for (let i = 0; i < n; i++) {
|
|
120
|
+
if (turn(pts[i], pts[(i + 1) % n], pts[(i + 2) % n]) < -1e-12)
|
|
121
|
+
throw new Error('polygon must be convex');
|
|
122
|
+
}
|
|
123
|
+
const positions = [...pts.map(([x, z]) => [x, y, z]), ...pts.map(([x, z]) => [x, y + height, z])];
|
|
124
|
+
const ring = [...Array(n).keys()];
|
|
125
|
+
const faces = [
|
|
126
|
+
{ v: ring.map((i) => n + i), color },
|
|
127
|
+
{ v: [...ring].reverse(), color },
|
|
128
|
+
...ring.map((i) => ({ v: [i, (i + 1) % n, n + ((i + 1) % n), n + i], color })),
|
|
129
|
+
];
|
|
130
|
+
return { positions, faces };
|
|
131
|
+
};
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// Back-to-front painting order, Newell-Sancha style.
|
|
2
|
+
//
|
|
3
|
+
// Faces that overlap on screen are ordered by plane tests: face A can be painted
|
|
4
|
+
// after face B when A lies wholly on the eye side of B's plane, or when B lies
|
|
5
|
+
// wholly behind A's plane. The resulting "paint this first" relations are then
|
|
6
|
+
// sorted topologically. Centroid depth only breaks ties, and orders faces that
|
|
7
|
+
// cannot be compared (intersecting solids, cycles) as well as before.
|
|
8
|
+
import { dot, minus } from './vec.js';
|
|
9
|
+
const PLANE_EPS = 1e-6; // plane test tolerance, world units
|
|
10
|
+
const PX = 0.01; // overlaps thinner than this are treated as touching
|
|
11
|
+
// Do two convex screen polygons overlap by more than a sliver? (separating axis test)
|
|
12
|
+
const overlaps = (a, b) => {
|
|
13
|
+
for (const poly of [a, b]) {
|
|
14
|
+
for (let i = 0; i < poly.length; i++) {
|
|
15
|
+
const p = poly[i];
|
|
16
|
+
const q = poly[(i + 1) % poly.length];
|
|
17
|
+
const nx = q.y - p.y;
|
|
18
|
+
const ny = p.x - q.x;
|
|
19
|
+
const length = Math.hypot(nx, ny);
|
|
20
|
+
if (!length)
|
|
21
|
+
continue;
|
|
22
|
+
let minA = Infinity;
|
|
23
|
+
let maxA = -Infinity;
|
|
24
|
+
let minB = Infinity;
|
|
25
|
+
let maxB = -Infinity;
|
|
26
|
+
for (const v of a) {
|
|
27
|
+
const d = (v.x * nx + v.y * ny) / length;
|
|
28
|
+
if (d < minA)
|
|
29
|
+
minA = d;
|
|
30
|
+
if (d > maxA)
|
|
31
|
+
maxA = d;
|
|
32
|
+
}
|
|
33
|
+
for (const v of b) {
|
|
34
|
+
const d = (v.x * nx + v.y * ny) / length;
|
|
35
|
+
if (d < minB)
|
|
36
|
+
minB = d;
|
|
37
|
+
if (d > maxB)
|
|
38
|
+
maxB = d;
|
|
39
|
+
}
|
|
40
|
+
if (maxA <= minB + PX || maxB <= minA + PX)
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return true;
|
|
45
|
+
};
|
|
46
|
+
// Signed distances of points to a face's plane, positive on the eye side.
|
|
47
|
+
const distances = (points, face) => points.map((v) => dot(face.n, minus(face.p)(v)) * face.eye);
|
|
48
|
+
/**
|
|
49
|
+
* Items need: { i, world, screen, n, p, eye, depth }
|
|
50
|
+
* world face points in world space screen the same, projected { x, y }
|
|
51
|
+
* n, p unit normal and a point on it eye +1 if the face faces the camera, else -1
|
|
52
|
+
* depth mean depth, used for ties i face index, used for ties
|
|
53
|
+
*/
|
|
54
|
+
export const paintOrder = (items) => {
|
|
55
|
+
const count = items.length;
|
|
56
|
+
const after = items.map(() => []);
|
|
57
|
+
const waiting = new Array(count).fill(0);
|
|
58
|
+
const link = (first, second) => {
|
|
59
|
+
after[first].push(second);
|
|
60
|
+
waiting[second] += 1;
|
|
61
|
+
};
|
|
62
|
+
// Screen bounding boxes, swept along x: only faces whose boxes overlap get the exact tests.
|
|
63
|
+
const box = items.map(({ screen }) => {
|
|
64
|
+
let x0 = Infinity;
|
|
65
|
+
let x1 = -Infinity;
|
|
66
|
+
let y0 = Infinity;
|
|
67
|
+
let y1 = -Infinity;
|
|
68
|
+
for (const { x, y } of screen) {
|
|
69
|
+
if (x < x0)
|
|
70
|
+
x0 = x;
|
|
71
|
+
if (x > x1)
|
|
72
|
+
x1 = x;
|
|
73
|
+
if (y < y0)
|
|
74
|
+
y0 = y;
|
|
75
|
+
if (y > y1)
|
|
76
|
+
y1 = y;
|
|
77
|
+
}
|
|
78
|
+
return { x0, x1, y0, y1 };
|
|
79
|
+
});
|
|
80
|
+
const byX = items.map((_, k) => k).sort((a, b) => box[a].x0 - box[b].x0 || a - b);
|
|
81
|
+
for (let s = 0; s < count; s++) {
|
|
82
|
+
for (let t = s + 1; t < count; t++) {
|
|
83
|
+
const [i, j] = byX[s] < byX[t] ? [byX[s], byX[t]] : [byX[t], byX[s]];
|
|
84
|
+
const bi = box[byX[s]];
|
|
85
|
+
const bj = box[byX[t]];
|
|
86
|
+
if (bj.x0 >= bi.x1 - PX)
|
|
87
|
+
break; // every later box starts further right
|
|
88
|
+
if (bj.y0 >= bi.y1 - PX || bi.y0 >= bj.y1 - PX)
|
|
89
|
+
continue;
|
|
90
|
+
const A = items[i];
|
|
91
|
+
const B = items[j];
|
|
92
|
+
if (!overlaps(A.screen, B.screen))
|
|
93
|
+
continue;
|
|
94
|
+
const a = distances(A.world, B); // A against B's plane
|
|
95
|
+
const b = distances(B.world, A); // B against A's plane
|
|
96
|
+
const aAfterB = a.every((d) => d >= -PLANE_EPS) || b.every((d) => d <= PLANE_EPS);
|
|
97
|
+
const bAfterA = b.every((d) => d >= -PLANE_EPS) || a.every((d) => d <= PLANE_EPS);
|
|
98
|
+
if (aAfterB && !bAfterA)
|
|
99
|
+
link(j, i);
|
|
100
|
+
else if (bAfterA && !aAfterB)
|
|
101
|
+
link(i, j);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const farther = (x, y) => items[x].depth > items[y].depth || (items[x].depth === items[y].depth && items[x].i < items[y].i);
|
|
105
|
+
const done = new Array(count).fill(false);
|
|
106
|
+
const out = [];
|
|
107
|
+
while (out.length < count) {
|
|
108
|
+
let best = -1;
|
|
109
|
+
for (let k = 0; k < count; k++) {
|
|
110
|
+
if (!done[k] && waiting[k] === 0 && (best < 0 || farther(k, best)))
|
|
111
|
+
best = k;
|
|
112
|
+
}
|
|
113
|
+
if (best < 0) {
|
|
114
|
+
// a cycle: take the farthest face that is left
|
|
115
|
+
for (let k = 0; k < count; k++)
|
|
116
|
+
if (!done[k] && (best < 0 || farther(k, best)))
|
|
117
|
+
best = k;
|
|
118
|
+
}
|
|
119
|
+
done[best] = true;
|
|
120
|
+
out.push(items[best]);
|
|
121
|
+
for (const k of after[best])
|
|
122
|
+
waiting[k] -= 1;
|
|
123
|
+
}
|
|
124
|
+
return out;
|
|
125
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Surface sensing: what is under a screen pixel, and a marker that lies on it.
|
|
2
|
+
// Picking casts a real ray against the face geometry, so it is exact even where
|
|
3
|
+
// the painter-sorted drawing is not.
|
|
4
|
+
import { cross, dot, norm, plus, minus, times, scale } from './vec.js';
|
|
5
|
+
import { faceNormal } from './mesh.js';
|
|
6
|
+
import { triangulate } from './triangulate.js';
|
|
7
|
+
import { ray } from './camera.js';
|
|
8
|
+
// Parallel means relative to the triangle's own size: a star 40 ly away has 1e-10 ly edges (det ~ 1e-20), so an absolute
|
|
9
|
+
// epsilon would call every face of a real-scale planet parallel. det^2 < PAR^2 |e1|^2 |e2|^2 (d is a unit vector).
|
|
10
|
+
const PAR2 = 1e-24;
|
|
11
|
+
// Moller-Trumbore ray/triangle test. Distance along the ray, or null.
|
|
12
|
+
const hitTriangle = (o, d, a, b, c) => {
|
|
13
|
+
const e1 = minus(a)(b);
|
|
14
|
+
const e2 = minus(a)(c);
|
|
15
|
+
const h = cross(d, e2);
|
|
16
|
+
const det = dot(e1, h);
|
|
17
|
+
if (det * det <= PAR2 * dot(e1, e1) * dot(e2, e2))
|
|
18
|
+
return null;
|
|
19
|
+
const inv = 1 / det;
|
|
20
|
+
const s = minus(a)(o);
|
|
21
|
+
const u = inv * dot(s, h);
|
|
22
|
+
if (u < 0 || u > 1)
|
|
23
|
+
return null;
|
|
24
|
+
const q = cross(s, e1);
|
|
25
|
+
const v = inv * dot(d, q);
|
|
26
|
+
if (v < 0 || u + v > 1)
|
|
27
|
+
return null;
|
|
28
|
+
const t = inv * dot(e2, q);
|
|
29
|
+
return t > 0 ? t : null; // (in front of the eye at any scale)
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* The nearest surface under panel pixel (x, y), or null.
|
|
33
|
+
* { face, distance, point, normal, front, color }
|
|
34
|
+
* `normal` is a unit vector facing the camera; `front` tells whether the face
|
|
35
|
+
* itself faces the camera (always true while `cull` is on). Faces are assumed
|
|
36
|
+
* convex and planar.
|
|
37
|
+
*/
|
|
38
|
+
export const pick = (mesh, cam, x, y, { cull = true } = {}) => {
|
|
39
|
+
const { origin, dir } = ray(cam, x, y);
|
|
40
|
+
let best = null;
|
|
41
|
+
mesh.faces.forEach((face, i) => {
|
|
42
|
+
const pts = face.v.map((k) => mesh.positions[k]);
|
|
43
|
+
const outward = norm(faceNormal(pts));
|
|
44
|
+
const front = dot(outward, dir) < 0;
|
|
45
|
+
if (cull && !front)
|
|
46
|
+
return;
|
|
47
|
+
for (const [a, b, c] of triangulate(pts)) {
|
|
48
|
+
const t = hitTriangle(origin, dir, pts[a], pts[b], pts[c]);
|
|
49
|
+
if (t !== null && (!best || t < best.distance)) {
|
|
50
|
+
best = {
|
|
51
|
+
face: i,
|
|
52
|
+
distance: t,
|
|
53
|
+
point: plus(origin)(times(t)(dir)),
|
|
54
|
+
normal: front ? outward : times(-1)(outward),
|
|
55
|
+
front,
|
|
56
|
+
color: face.color,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
return best;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* A target cross lying flat on the picked surface (perpendicular to its
|
|
65
|
+
* normal): four arms with a gap in the middle and a ring, as 3D polylines.
|
|
66
|
+
* It is aligned to the first edge of the face, so it turns with the object.
|
|
67
|
+
* `radius` is the ring radius in world units.
|
|
68
|
+
*/
|
|
69
|
+
export const crosshair = (mesh, hit, radius = 0.1) => {
|
|
70
|
+
const [a, b] = mesh.faces[hit.face].v;
|
|
71
|
+
const t1 = norm(minus(mesh.positions[a])(mesh.positions[b]));
|
|
72
|
+
const t2 = norm(cross(hit.normal, t1));
|
|
73
|
+
const at = (p, q) => plus(hit.point)(plus(scale(t2, q * radius))(scale(t1, p * radius)));
|
|
74
|
+
const ring = Array.from({ length: 33 }, (_, i) => {
|
|
75
|
+
const angle = (i / 32) * 2 * Math.PI;
|
|
76
|
+
return at(Math.cos(angle), Math.sin(angle));
|
|
77
|
+
});
|
|
78
|
+
return [
|
|
79
|
+
[at(-1.7, 0), at(-0.4, 0)],
|
|
80
|
+
[at(0.4, 0), at(1.7, 0)],
|
|
81
|
+
[at(0, -1.7), at(0, -0.4)],
|
|
82
|
+
[at(0, 0.4), at(0, 1.7)],
|
|
83
|
+
ring,
|
|
84
|
+
];
|
|
85
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Putting things down on surfaces.
|
|
2
|
+
import { cross, norm, plus, minus, times } from './vec.js';
|
|
3
|
+
import { box } from './mesh.js';
|
|
4
|
+
/**
|
|
5
|
+
* A square prism standing on the picked surface: its base lies on the surface,
|
|
6
|
+
* centered at the hit point, and it rises along the surface normal. Its sides
|
|
7
|
+
* line up with the first edge of the face. The height is `3 * base` unless given.
|
|
8
|
+
* Returns a new mesh; merge() it into the scene.
|
|
9
|
+
*/
|
|
10
|
+
export const standOn = (mesh, hit, { base = 0.15, height = 3 * base, color = '#c8c8c8' } = {}) => {
|
|
11
|
+
const [a, b] = mesh.faces[hit.face].v;
|
|
12
|
+
const ex = norm(minus(mesh.positions[a])(mesh.positions[b]));
|
|
13
|
+
const ey = hit.normal;
|
|
14
|
+
const ez = cross(ex, ey);
|
|
15
|
+
const frame = { size: [base, height, base], at: plus(hit.point)(times(height / 2)(ey)), axes: [ex, ey, ez] };
|
|
16
|
+
return { ...box({ ...frame, color }), frame }; // frame: what an instanced renderer needs
|
|
17
|
+
};
|