3draft 26.11.1013 → 26.11.3001
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 +89 -0
- package/README.md +71 -144
- package/dist/camera.d.ts +29 -0
- package/dist/camera.js +68 -0
- package/dist/check.d.ts +24 -0
- package/dist/check.js +83 -0
- package/dist/csg.d.ts +10 -0
- package/dist/csg.js +239 -0
- package/dist/edit.d.ts +23 -0
- package/dist/edit.js +48 -0
- package/dist/gpu.d.ts +77 -0
- package/dist/gpu.js +339 -0
- package/dist/index.d.ts +33 -0
- package/{src → dist}/index.js +14 -14
- package/dist/kernel-bytes.d.ts +1 -0
- package/dist/kernel-bytes.js +2 -0
- package/dist/kernel.d.ts +14 -0
- package/dist/kernel.js +81 -0
- package/dist/material.d.ts +31 -0
- package/dist/material.js +17 -0
- package/dist/mesh.d.ts +40 -0
- package/dist/mesh.js +130 -0
- package/dist/order.d.ts +22 -0
- package/dist/order.js +125 -0
- package/dist/pick.d.ts +18 -0
- package/dist/pick.js +82 -0
- package/dist/place.d.ts +23 -0
- package/{src → dist}/place.js +7 -13
- package/dist/svg.d.ts +37 -0
- package/dist/svg.js +239 -0
- package/dist/types.d.ts +57 -0
- package/dist/types.js +2 -0
- package/dist/vec.d.ts +14 -0
- package/dist/vec.js +22 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +7 -0
- package/package.json +18 -9
- package/src/camera.js +0 -75
- package/src/check.js +0 -79
- package/src/edit.js +0 -47
- package/src/gpu.js +0 -252
- package/src/index.d.ts +0 -209
- package/src/mesh.js +0 -127
- package/src/order.js +0 -94
- package/src/pick.js +0 -83
- package/src/svg.js +0 -119
- package/src/vec.js +0 -16
- package/src/version.js +0 -8
package/dist/mesh.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
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
|
+
positions.push(...m.positions);
|
|
37
|
+
for (const f of m.faces)
|
|
38
|
+
faces.push({ ...f, v: f.v.map((i) => i + offset) });
|
|
39
|
+
}
|
|
40
|
+
return { positions, faces };
|
|
41
|
+
};
|
|
42
|
+
/** Bounding box center and bounding sphere radius around that center. */
|
|
43
|
+
export const bounds = (mesh) => {
|
|
44
|
+
if (!mesh.positions.length)
|
|
45
|
+
return { center: [0, 0, 0], radius: 0 };
|
|
46
|
+
const min = [Infinity, Infinity, Infinity];
|
|
47
|
+
const max = [-Infinity, -Infinity, -Infinity];
|
|
48
|
+
for (const p of mesh.positions) {
|
|
49
|
+
for (let a = 0; a < 3; a++) {
|
|
50
|
+
if (p[a] < min[a])
|
|
51
|
+
min[a] = p[a];
|
|
52
|
+
if (p[a] > max[a])
|
|
53
|
+
max[a] = p[a];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const center = [(min[0] + max[0]) / 2, (min[1] + max[1]) / 2, (min[2] + max[2]) / 2];
|
|
57
|
+
let radius = 0;
|
|
58
|
+
for (const p of mesh.positions)
|
|
59
|
+
radius = Math.max(radius, len(sub(p, center)));
|
|
60
|
+
return { center, radius };
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Turntable rotation: yaw around Y first, then pitch around X (radians),
|
|
64
|
+
* both around `center` (default: bounding-box center). Faces are shared.
|
|
65
|
+
*/
|
|
66
|
+
export const rotate = (mesh, { yaw = 0, pitch = 0 } = {}, center = bounds(mesh).center) => {
|
|
67
|
+
const cy = Math.cos(yaw);
|
|
68
|
+
const sy = Math.sin(yaw);
|
|
69
|
+
const cp = Math.cos(pitch);
|
|
70
|
+
const sp = Math.sin(pitch);
|
|
71
|
+
const positions = mesh.positions.map((p) => {
|
|
72
|
+
const x = p[0] - center[0];
|
|
73
|
+
const y = p[1] - center[1];
|
|
74
|
+
const z = p[2] - center[2];
|
|
75
|
+
const x1 = cy * x + sy * z;
|
|
76
|
+
const z1 = -sy * x + cy * z;
|
|
77
|
+
return [x1 + center[0], cp * y - sp * z1 + center[1], sp * y + cp * z1 + center[2]];
|
|
78
|
+
});
|
|
79
|
+
return { positions, faces: mesh.faces };
|
|
80
|
+
};
|
|
81
|
+
/** Face normal (not normalized) by Newell's method; points CCW => outward. */
|
|
82
|
+
export const faceNormal = (pts) => {
|
|
83
|
+
const n = [0, 0, 0];
|
|
84
|
+
for (let i = 0; i < pts.length; i++) {
|
|
85
|
+
const a = pts[i];
|
|
86
|
+
const b = pts[(i + 1) % pts.length];
|
|
87
|
+
n[0] += (a[1] - b[1]) * (a[2] + b[2]);
|
|
88
|
+
n[1] += (a[2] - b[2]) * (a[0] + b[0]);
|
|
89
|
+
n[2] += (a[0] - b[0]) * (a[1] + b[1]);
|
|
90
|
+
}
|
|
91
|
+
return n;
|
|
92
|
+
};
|
|
93
|
+
/** Mean of the face points. */
|
|
94
|
+
export const faceCenter = (pts) => {
|
|
95
|
+
const s = pts.reduce((acc, p) => add(acc, p), [0, 0, 0]);
|
|
96
|
+
return [s[0] / pts.length, s[1] / pts.length, s[2] / pts.length];
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Prism from a convex footprint: `polygon` is [[x, z], ...] on the ground plane (any
|
|
100
|
+
* winding), it is extruded up from `y` by `height`. Faces: top, bottom, then one
|
|
101
|
+
* quad per footprint edge, all CCW from outside.
|
|
102
|
+
*/
|
|
103
|
+
export const extrude = ({ polygon, height = 1, y = 0, color = '#c8c8c8' }) => {
|
|
104
|
+
if (!Array.isArray(polygon) || polygon.length < 3)
|
|
105
|
+
throw new Error('polygon needs at least 3 [x, z] points');
|
|
106
|
+
if (!(height > 0))
|
|
107
|
+
throw new Error('height must be positive');
|
|
108
|
+
let pts = polygon.map((p) => [p[0], p[1]]);
|
|
109
|
+
const n = pts.length;
|
|
110
|
+
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
|
|
111
|
+
let area = 0;
|
|
112
|
+
for (let i = 0; i < n; i++)
|
|
113
|
+
area += pts[i][1] * pts[(i + 1) % n][0] - pts[i][0] * pts[(i + 1) % n][1];
|
|
114
|
+
if (Math.abs(area) < 1e-12)
|
|
115
|
+
throw new Error('polygon has no area');
|
|
116
|
+
if (area < 0)
|
|
117
|
+
pts = pts.reverse();
|
|
118
|
+
for (let i = 0; i < n; i++) {
|
|
119
|
+
if (turn(pts[i], pts[(i + 1) % n], pts[(i + 2) % n]) < -1e-12)
|
|
120
|
+
throw new Error('polygon must be convex');
|
|
121
|
+
}
|
|
122
|
+
const positions = [...pts.map(([x, z]) => [x, y, z]), ...pts.map(([x, z]) => [x, y + height, z])];
|
|
123
|
+
const ring = [...Array(n).keys()];
|
|
124
|
+
const faces = [
|
|
125
|
+
{ v: ring.map((i) => n + i), color },
|
|
126
|
+
{ v: [...ring].reverse(), color },
|
|
127
|
+
...ring.map((i) => ({ v: [i, (i + 1) % n, n + ((i + 1) % n), n + i], color })),
|
|
128
|
+
];
|
|
129
|
+
return { positions, faces };
|
|
130
|
+
};
|
package/dist/order.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Vec3 } from './types.js';
|
|
2
|
+
export interface ScreenPoint {
|
|
3
|
+
x: number;
|
|
4
|
+
y: number;
|
|
5
|
+
}
|
|
6
|
+
/** One projected face: see paintOrder(). */
|
|
7
|
+
export interface PaintItem {
|
|
8
|
+
i: number;
|
|
9
|
+
world: Vec3[];
|
|
10
|
+
screen: ScreenPoint[];
|
|
11
|
+
n: Vec3;
|
|
12
|
+
p: Vec3;
|
|
13
|
+
eye: 1 | -1;
|
|
14
|
+
depth: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Items need: { i, world, screen, n, p, eye, depth }
|
|
18
|
+
* world face points in world space screen the same, projected { x, y }
|
|
19
|
+
* n, p unit normal and a point on it eye +1 if the face faces the camera, else -1
|
|
20
|
+
* depth mean depth, used for ties i face index, used for ties
|
|
21
|
+
*/
|
|
22
|
+
export declare const paintOrder: <T extends PaintItem>(items: T[]) => T[];
|
package/dist/order.js
ADDED
|
@@ -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
|
+
};
|
package/dist/pick.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Camera, Hit, Mesh, Vec3 } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The nearest surface under panel pixel (x, y), or null.
|
|
4
|
+
* { face, distance, point, normal, front, color }
|
|
5
|
+
* `normal` is a unit vector facing the camera; `front` tells whether the face
|
|
6
|
+
* itself faces the camera (always true while `cull` is on). Faces are assumed
|
|
7
|
+
* convex and planar.
|
|
8
|
+
*/
|
|
9
|
+
export declare const pick: (mesh: Mesh, cam: Camera, x: number, y: number, { cull }?: {
|
|
10
|
+
cull?: boolean;
|
|
11
|
+
}) => Hit | null;
|
|
12
|
+
/**
|
|
13
|
+
* A target cross lying flat on the picked surface (perpendicular to its
|
|
14
|
+
* normal): four arms with a gap in the middle and a ring, as 3D polylines.
|
|
15
|
+
* It is aligned to the first edge of the face, so it turns with the object.
|
|
16
|
+
* `radius` is the ring radius in world units.
|
|
17
|
+
*/
|
|
18
|
+
export declare const crosshair: (mesh: Mesh, hit: Hit, radius?: number) => Vec3[][];
|
package/dist/pick.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
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 { ray } from './camera.js';
|
|
7
|
+
const EPS = 1e-9;
|
|
8
|
+
// Moller-Trumbore ray/triangle test. Distance along the ray, or null.
|
|
9
|
+
const hitTriangle = (o, d, a, b, c) => {
|
|
10
|
+
const e1 = minus(a)(b);
|
|
11
|
+
const e2 = minus(a)(c);
|
|
12
|
+
const h = cross(d, e2);
|
|
13
|
+
const det = dot(e1, h);
|
|
14
|
+
if (Math.abs(det) < EPS)
|
|
15
|
+
return null;
|
|
16
|
+
const inv = 1 / det;
|
|
17
|
+
const s = minus(a)(o);
|
|
18
|
+
const u = inv * dot(s, h);
|
|
19
|
+
if (u < 0 || u > 1)
|
|
20
|
+
return null;
|
|
21
|
+
const q = cross(s, e1);
|
|
22
|
+
const v = inv * dot(d, q);
|
|
23
|
+
if (v < 0 || u + v > 1)
|
|
24
|
+
return null;
|
|
25
|
+
const t = inv * dot(e2, q);
|
|
26
|
+
return t > EPS ? t : null;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* The nearest surface under panel pixel (x, y), or null.
|
|
30
|
+
* { face, distance, point, normal, front, color }
|
|
31
|
+
* `normal` is a unit vector facing the camera; `front` tells whether the face
|
|
32
|
+
* itself faces the camera (always true while `cull` is on). Faces are assumed
|
|
33
|
+
* convex and planar.
|
|
34
|
+
*/
|
|
35
|
+
export const pick = (mesh, cam, x, y, { cull = true } = {}) => {
|
|
36
|
+
const { origin, dir } = ray(cam, x, y);
|
|
37
|
+
let best = null;
|
|
38
|
+
mesh.faces.forEach((face, i) => {
|
|
39
|
+
const pts = face.v.map((k) => mesh.positions[k]);
|
|
40
|
+
const outward = norm(faceNormal(pts));
|
|
41
|
+
const front = dot(outward, dir) < 0;
|
|
42
|
+
if (cull && !front)
|
|
43
|
+
return;
|
|
44
|
+
for (let j = 1; j < pts.length - 1; j++) {
|
|
45
|
+
const t = hitTriangle(origin, dir, pts[0], pts[j], pts[j + 1]);
|
|
46
|
+
if (t !== null && (!best || t < best.distance)) {
|
|
47
|
+
best = {
|
|
48
|
+
face: i,
|
|
49
|
+
distance: t,
|
|
50
|
+
point: plus(origin)(times(t)(dir)),
|
|
51
|
+
normal: front ? outward : times(-1)(outward),
|
|
52
|
+
front,
|
|
53
|
+
color: face.color,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
return best;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* A target cross lying flat on the picked surface (perpendicular to its
|
|
62
|
+
* normal): four arms with a gap in the middle and a ring, as 3D polylines.
|
|
63
|
+
* It is aligned to the first edge of the face, so it turns with the object.
|
|
64
|
+
* `radius` is the ring radius in world units.
|
|
65
|
+
*/
|
|
66
|
+
export const crosshair = (mesh, hit, radius = 0.1) => {
|
|
67
|
+
const [a, b] = mesh.faces[hit.face].v;
|
|
68
|
+
const t1 = norm(minus(mesh.positions[a])(mesh.positions[b]));
|
|
69
|
+
const t2 = norm(cross(hit.normal, t1));
|
|
70
|
+
const at = (p, q) => plus(hit.point)(plus(scale(t2, q * radius))(scale(t1, p * radius)));
|
|
71
|
+
const ring = Array.from({ length: 33 }, (_, i) => {
|
|
72
|
+
const angle = (i / 32) * 2 * Math.PI;
|
|
73
|
+
return at(Math.cos(angle), Math.sin(angle));
|
|
74
|
+
});
|
|
75
|
+
return [
|
|
76
|
+
[at(-1.7, 0), at(-0.4, 0)],
|
|
77
|
+
[at(0.4, 0), at(1.7, 0)],
|
|
78
|
+
[at(0, -1.7), at(0, -0.4)],
|
|
79
|
+
[at(0, 0.4), at(0, 1.7)],
|
|
80
|
+
ring,
|
|
81
|
+
];
|
|
82
|
+
};
|
package/dist/place.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Axes, Hit, Mesh, Vec3 } from './types.js';
|
|
2
|
+
export interface StandOptions {
|
|
3
|
+
/** Square base edge (default 0.15). */
|
|
4
|
+
base?: number;
|
|
5
|
+
/** Height (default 3 x base). */
|
|
6
|
+
height?: number;
|
|
7
|
+
color?: string;
|
|
8
|
+
}
|
|
9
|
+
/** Where and how an instanced renderer draws the prism. */
|
|
10
|
+
export interface Frame {
|
|
11
|
+
size: Vec3;
|
|
12
|
+
at: Vec3;
|
|
13
|
+
axes: Axes;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A square prism standing on the picked surface: its base lies on the surface,
|
|
17
|
+
* centered at the hit point, and it rises along the surface normal. Its sides
|
|
18
|
+
* line up with the first edge of the face. The height is `3 * base` unless given.
|
|
19
|
+
* Returns a new mesh; merge() it into the scene.
|
|
20
|
+
*/
|
|
21
|
+
export declare const standOn: (mesh: Mesh, hit: Hit, { base, height, color }?: StandOptions) => Mesh & {
|
|
22
|
+
frame: Frame;
|
|
23
|
+
};
|
package/{src → dist}/place.js
RENAMED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
// Putting things down on surfaces.
|
|
2
|
-
|
|
3
|
-
import { add, cross, norm, scale, sub } from './vec.js';
|
|
2
|
+
import { cross, norm, plus, minus, times } from './vec.js';
|
|
4
3
|
import { box } from './mesh.js';
|
|
5
|
-
|
|
6
4
|
/**
|
|
7
5
|
* A square prism standing on the picked surface: its base lies on the surface,
|
|
8
6
|
* centered at the hit point, and it rises along the surface normal. Its sides
|
|
@@ -10,14 +8,10 @@ import { box } from './mesh.js';
|
|
|
10
8
|
* Returns a new mesh; merge() it into the scene.
|
|
11
9
|
*/
|
|
12
10
|
export const standOn = (mesh, hit, { base = 0.15, height = 3 * base, color = '#c8c8c8' } = {}) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
at: add(hit.point, scale(ey, height / 2)),
|
|
20
|
-
axes: [ex, ey, ez],
|
|
21
|
-
color,
|
|
22
|
-
});
|
|
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
|
|
23
17
|
};
|
package/dist/svg.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Palette } from './material.js';
|
|
2
|
+
import type { Fit, Mark, Mesh, ViewName, ViewSpec } from './types.js';
|
|
3
|
+
export interface RenderOptions {
|
|
4
|
+
/** Panel size in px (default 400). */
|
|
5
|
+
size?: number;
|
|
6
|
+
/** Drop back-facing faces (default true). */
|
|
7
|
+
cull?: boolean;
|
|
8
|
+
/** Draw the vertices as circles (default true). */
|
|
9
|
+
points?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Send only what can be seen (default false). Faces are first painted into a small id buffer, far to
|
|
12
|
+
* near; a face none of whose pixels survives is covered by nearer ones and is left out, and so is a
|
|
13
|
+
* point no visible face reaches. The picture stays the same, the SVG gets much smaller.
|
|
14
|
+
*/
|
|
15
|
+
occlude?: boolean;
|
|
16
|
+
/** Named materials (`face.mat`). The SVG shows their color and alpha; metal, rough and emit are for the GPU view. */
|
|
17
|
+
palette?: Palette;
|
|
18
|
+
stroke?: string;
|
|
19
|
+
pointColor?: string;
|
|
20
|
+
background?: string;
|
|
21
|
+
/** Keep the camera target/framing fixed while the mesh moves. */
|
|
22
|
+
fit?: Fit;
|
|
23
|
+
/** 3D polylines drawn on top, e.g. the surface cursor. */
|
|
24
|
+
marks?: Mark[];
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* One standalone SVG for a single view: 'front' | 'side' | 'top' | 'persp',
|
|
28
|
+
* or a custom { dir, up, ortho, name? } spec.
|
|
29
|
+
*/
|
|
30
|
+
export declare const renderView: (mesh: Mesh, view: ViewName | ViewSpec, opts?: RenderOptions) => string;
|
|
31
|
+
/** The four views one after another: [{ name, svg }, ...]. */
|
|
32
|
+
export declare const renderViews: (mesh: Mesh, opts?: RenderOptions) => {
|
|
33
|
+
name: ViewName;
|
|
34
|
+
svg: string;
|
|
35
|
+
}[];
|
|
36
|
+
/** All four views on one 2x2 sheet: front, side / top, persp. */
|
|
37
|
+
export declare const renderSheet: (mesh: Mesh, opts?: RenderOptions) => string;
|