fast-mesh-wasm 0.2.0 → 0.3.1
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/README.md +490 -1
- package/dist/{api-D0o2HjaU.js → api-BUy7FRx7.js} +11 -11
- package/dist/client-Bp9FRd70.js +2278 -0
- package/dist/client-m2zSdFlu.d.ts +305 -0
- package/dist/colormap-D_xH5hI5.js +303 -0
- package/dist/core-Cke_kZ2e.js +113 -0
- package/dist/core-D2X9GPfK.d.ts +15 -0
- package/dist/engine-BpnthBYQ.js +1927 -0
- package/dist/mesh-B16gV3oX.d.ts +130 -0
- package/dist/mesh-B8pae8WA.js +428 -0
- package/dist/node.d.ts +3 -2
- package/dist/node.js +5 -4
- package/dist/protocol-CAUYm1gd.js +68 -0
- package/dist/protocol-W9R-iGl5.d.ts +293 -0
- package/dist/r3f.d.ts +59 -0
- package/dist/r3f.js +165 -0
- package/dist/react-CTADh3NN.js +124 -0
- package/dist/react.d.ts +65 -0
- package/dist/react.js +1 -0
- package/dist/scene-BFd6nYRe.js +1256 -0
- package/dist/scene-like-B_3qm0u6.d.ts +925 -0
- package/dist/snapshot-Cf6NgDl7.js +104 -0
- package/dist/store-D3ErV6o-.js +1379 -0
- package/dist/store-DNnHN33A.d.ts +285 -0
- package/dist/store.d.ts +61 -0
- package/dist/store.js +105 -0
- package/dist/three.d.ts +118 -0
- package/dist/three.js +390 -0
- package/dist/views-BaZqBxlX.js +189 -0
- package/dist/wasm/fast_mesh_wasm.d.ts +475 -0
- package/dist/wasm/fast_mesh_wasm.js +1184 -0
- package/dist/wasm/fast_mesh_wasm_bg.wasm +0 -0
- package/dist/wasm/fast_mesh_wasm_bg.wasm.d.ts +76 -0
- package/dist/wasm-threads/fast_mesh_wasm.d.ts +557 -0
- package/dist/wasm-threads/fast_mesh_wasm.js +1547 -0
- package/dist/wasm-threads/fast_mesh_wasm_bg.wasm +0 -0
- package/dist/wasm-threads/fast_mesh_wasm_bg.wasm.d.ts +95 -0
- package/dist/wasm-threads/package.json +7 -0
- package/dist/wasm-threads/snippets/fast-mesh-wasm-ae14781a92c2789e/inline0.js +16 -0
- package/dist/wasm-threads/snippets/wasm-bindgen-rayon-38edf6e439f6d70d/src/workerHelpers.js +107 -0
- package/dist/web.d.ts +3 -2
- package/dist/web.js +4 -2
- package/dist/worker-engine.d.ts +255 -0
- package/dist/worker-engine.js +1 -0
- package/dist/worker-entry.d.ts +1 -0
- package/dist/worker-entry.js +11 -0
- package/dist/worker.d.ts +2 -0
- package/dist/worker.js +1 -0
- package/package.json +53 -4
- package/dist/api-HcskOwd7.d.ts +0 -43
package/dist/three.js
ADDED
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
import { t as FastMeshError } from "./core-Cke_kZ2e.js";
|
|
2
|
+
import { t as bindMesh } from "./mesh-B8pae8WA.js";
|
|
3
|
+
import * as THREE from "three";
|
|
4
|
+
//#region src/three/controls.ts
|
|
5
|
+
const defaultSchedule = (callback) => {
|
|
6
|
+
if (typeof requestAnimationFrame === "function") requestAnimationFrame(() => callback());
|
|
7
|
+
else setTimeout(callback, 16);
|
|
8
|
+
};
|
|
9
|
+
/** `R · v` for the rotation of a unit quaternion `[x, y, z, w]`, in f64. */
|
|
10
|
+
function rotate(q, v) {
|
|
11
|
+
const [x, y, z, w] = q;
|
|
12
|
+
const cx = y * v[2] - z * v[1];
|
|
13
|
+
const cy = z * v[0] - x * v[2];
|
|
14
|
+
const cz = x * v[1] - y * v[0];
|
|
15
|
+
return [
|
|
16
|
+
v[0] + 2 * (w * cx + y * cz - z * cy),
|
|
17
|
+
v[1] + 2 * (w * cy + z * cx - x * cz),
|
|
18
|
+
v[2] + 2 * (w * cz + x * cy - y * cx)
|
|
19
|
+
];
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Lets a TransformControls move a bound mesh: while the gizmo drags
|
|
23
|
+
* `bound.object`, its pose is sent to `scene.drag(bound.mesh)`, and the drag
|
|
24
|
+
* is ended (one undo step) when the gizmo lets go. Returns a function that
|
|
25
|
+
* detaches it (ending a drag in progress first).
|
|
26
|
+
*
|
|
27
|
+
* Pose and placement: `bound.object.matrix` is the scene mesh's
|
|
28
|
+
* `matrix4 = placement · translate(origin)`, so with placement `(R, t)` the
|
|
29
|
+
* object's quaternion is `R` and its position is `R · origin + t`. A pose is
|
|
30
|
+
* therefore sent as `{ rotation: quaternion, translation: position − R · origin }`,
|
|
31
|
+
* all in f64. The origin (fixed for the mesh) comes from the placement and
|
|
32
|
+
* matrix at the start of each drag: `origin = Rᵀ (m − t)`, with `m` the
|
|
33
|
+
* matrix's translation column.
|
|
34
|
+
*
|
|
35
|
+
* Placements are rigid: TransformControls' scale mode has no effect (the
|
|
36
|
+
* object's scale is kept at 1). The object must be a direct child of an
|
|
37
|
+
* untransformed parent (its matrix is a world matrix), as for `bindMesh`.
|
|
38
|
+
*
|
|
39
|
+
* The object's `position`, `quaternion` and `scale` are kept in step with its
|
|
40
|
+
* matrix (TransformControls reads them when a drag starts), so call this
|
|
41
|
+
* after `bindMesh` and before the gizmo grabs the object. While the gizmo
|
|
42
|
+
* drags, the object's matrix follows the gizmo, not the change stream (the
|
|
43
|
+
* scene lags by up to a frame); once the drag ends, the final placement is
|
|
44
|
+
* applied. When the drag is ended by the scene instead (`undo()` while
|
|
45
|
+
* dragging, for example), the object follows the scene again and the rest
|
|
46
|
+
* of the gesture is ignored. When the mesh is removed, the drag ends
|
|
47
|
+
* (no step is left open) and the attachment goes idle. A gesture that
|
|
48
|
+
* cannot start a drag (another drag is active) is ignored with a warning.
|
|
49
|
+
*/
|
|
50
|
+
function attachTransformControls(controls, scene, bound, options = {}) {
|
|
51
|
+
const schedule = options.schedule ?? defaultSchedule;
|
|
52
|
+
const { object, mesh } = bound;
|
|
53
|
+
let drag;
|
|
54
|
+
/** True from a drag's start until the gizmo lets go, even if the scene ended the drag. */
|
|
55
|
+
let grabbed = false;
|
|
56
|
+
/** The mesh origin during a drag: `Rᵀ (m − t)`. */
|
|
57
|
+
let origin = [
|
|
58
|
+
0,
|
|
59
|
+
0,
|
|
60
|
+
0
|
|
61
|
+
];
|
|
62
|
+
let pending;
|
|
63
|
+
let scheduled = false;
|
|
64
|
+
let attached = true;
|
|
65
|
+
/** The mesh was removed: no more scene calls about it. */
|
|
66
|
+
let gone = false;
|
|
67
|
+
let warned = false;
|
|
68
|
+
/** Puts the object where the scene has it and its pose in step. */
|
|
69
|
+
const syncFromScene = () => {
|
|
70
|
+
object.matrix.fromArray(scene.matrix4(mesh));
|
|
71
|
+
object.matrix.decompose(object.position, object.quaternion, object.scale);
|
|
72
|
+
object.matrixWorldNeedsUpdate = true;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Runs a scene call about the mesh; when the mesh turns out to be removed
|
|
76
|
+
* (`UNKNOWN_MESH`), marks it gone and returns false. Other errors propagate.
|
|
77
|
+
*/
|
|
78
|
+
const guarded = (run) => {
|
|
79
|
+
if (gone) return false;
|
|
80
|
+
try {
|
|
81
|
+
run();
|
|
82
|
+
return true;
|
|
83
|
+
} catch (error) {
|
|
84
|
+
if (error instanceof FastMeshError && error.code === "UNKNOWN_MESH") {
|
|
85
|
+
gone = true;
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
/** Lets go of the drag: ends it if the scene has not, drops the pending pose. */
|
|
92
|
+
const release = () => {
|
|
93
|
+
const d = drag;
|
|
94
|
+
drag = void 0;
|
|
95
|
+
pending = void 0;
|
|
96
|
+
if (d?.active) d.end();
|
|
97
|
+
};
|
|
98
|
+
/** The drag, if it is still ours and live. */
|
|
99
|
+
const live = () => {
|
|
100
|
+
if (drag !== void 0 && !drag.active) {
|
|
101
|
+
release();
|
|
102
|
+
guarded(syncFromScene);
|
|
103
|
+
}
|
|
104
|
+
return drag;
|
|
105
|
+
};
|
|
106
|
+
const flush = () => {
|
|
107
|
+
const d = live();
|
|
108
|
+
const pose = pending;
|
|
109
|
+
pending = void 0;
|
|
110
|
+
if (d === void 0 || pose === void 0) return;
|
|
111
|
+
const r = rotate(pose.rotation, origin);
|
|
112
|
+
if (!guarded(() => d.move({
|
|
113
|
+
rotation: pose.rotation,
|
|
114
|
+
translation: [
|
|
115
|
+
pose.position[0] - r[0],
|
|
116
|
+
pose.position[1] - r[1],
|
|
117
|
+
pose.position[2] - r[2]
|
|
118
|
+
]
|
|
119
|
+
}))) release();
|
|
120
|
+
};
|
|
121
|
+
const onFrame = () => {
|
|
122
|
+
scheduled = false;
|
|
123
|
+
if (attached) flush();
|
|
124
|
+
};
|
|
125
|
+
const begin = () => {
|
|
126
|
+
if (grabbed || gone || controls.object !== object) return;
|
|
127
|
+
try {
|
|
128
|
+
const p = scene.placement(mesh);
|
|
129
|
+
const m = scene.matrix4(mesh);
|
|
130
|
+
const r = p.rotation;
|
|
131
|
+
const d = [
|
|
132
|
+
m[12] - p.translation[0],
|
|
133
|
+
m[13] - p.translation[1],
|
|
134
|
+
m[14] - p.translation[2]
|
|
135
|
+
];
|
|
136
|
+
origin = [
|
|
137
|
+
r[0] * d[0] + r[3] * d[1] + r[6] * d[2],
|
|
138
|
+
r[1] * d[0] + r[4] * d[1] + r[7] * d[2],
|
|
139
|
+
r[2] * d[0] + r[5] * d[1] + r[8] * d[2]
|
|
140
|
+
];
|
|
141
|
+
drag = scene.drag(mesh);
|
|
142
|
+
grabbed = true;
|
|
143
|
+
} catch (error) {
|
|
144
|
+
if (!warned) {
|
|
145
|
+
warned = true;
|
|
146
|
+
console.warn("fast-mesh-wasm/three: cannot start a drag; the gesture is ignored.", error);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
const finish = () => {
|
|
151
|
+
if (!grabbed) {
|
|
152
|
+
if (controls.object === object) guarded(syncFromScene);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
grabbed = false;
|
|
156
|
+
try {
|
|
157
|
+
flush();
|
|
158
|
+
} finally {
|
|
159
|
+
release();
|
|
160
|
+
}
|
|
161
|
+
guarded(syncFromScene);
|
|
162
|
+
};
|
|
163
|
+
const onDraggingChanged = (event) => {
|
|
164
|
+
if (typeof event.value === "boolean" ? event.value : controls.dragging) begin();
|
|
165
|
+
else finish();
|
|
166
|
+
};
|
|
167
|
+
const onObjectChange = () => {
|
|
168
|
+
if (live() === void 0 || controls.object !== object) return;
|
|
169
|
+
object.scale.set(1, 1, 1);
|
|
170
|
+
object.updateMatrix();
|
|
171
|
+
const q = object.quaternion;
|
|
172
|
+
const p = object.position;
|
|
173
|
+
pending = {
|
|
174
|
+
rotation: [
|
|
175
|
+
q.x,
|
|
176
|
+
q.y,
|
|
177
|
+
q.z,
|
|
178
|
+
q.w
|
|
179
|
+
],
|
|
180
|
+
position: [
|
|
181
|
+
p.x,
|
|
182
|
+
p.y,
|
|
183
|
+
p.z
|
|
184
|
+
]
|
|
185
|
+
};
|
|
186
|
+
if (!scheduled) {
|
|
187
|
+
scheduled = true;
|
|
188
|
+
schedule(onFrame);
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
const unsubscribe = scene.onChanges((changes) => {
|
|
192
|
+
for (const c of changes) {
|
|
193
|
+
if (c.mesh !== mesh) continue;
|
|
194
|
+
if (c.removed) {
|
|
195
|
+
gone = true;
|
|
196
|
+
release();
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
if (!c.placementChanged) continue;
|
|
200
|
+
if (live() !== void 0) object.updateMatrix();
|
|
201
|
+
else object.matrix.decompose(object.position, object.quaternion, object.scale);
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
controls.addEventListener("dragging-changed", onDraggingChanged);
|
|
205
|
+
controls.addEventListener("objectChange", onObjectChange);
|
|
206
|
+
syncFromScene();
|
|
207
|
+
return () => {
|
|
208
|
+
if (!attached) return;
|
|
209
|
+
attached = false;
|
|
210
|
+
try {
|
|
211
|
+
finish();
|
|
212
|
+
} finally {
|
|
213
|
+
pending = void 0;
|
|
214
|
+
controls.removeEventListener("dragging-changed", onDraggingChanged);
|
|
215
|
+
controls.removeEventListener("objectChange", onObjectChange);
|
|
216
|
+
unsubscribe();
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
//#endregion
|
|
221
|
+
//#region src/three/section.ts
|
|
222
|
+
const DEFAULT_LINE = 0;
|
|
223
|
+
const DEFAULT_FILL = 8947848;
|
|
224
|
+
/**
|
|
225
|
+
* Line segment endpoints of a result's contours: consecutive point pairs,
|
|
226
|
+
* plus the last-to-first pair of a closed contour.
|
|
227
|
+
*/
|
|
228
|
+
function segments(result) {
|
|
229
|
+
let count = 0;
|
|
230
|
+
for (const c of result.contours) {
|
|
231
|
+
const n = c.points.length / 3;
|
|
232
|
+
count += n < 2 ? 0 : c.closed ? n : n - 1;
|
|
233
|
+
}
|
|
234
|
+
const out = new Float32Array(count * 6);
|
|
235
|
+
let at = 0;
|
|
236
|
+
for (const { points, closed } of result.contours) {
|
|
237
|
+
const n = points.length / 3;
|
|
238
|
+
if (n < 2) continue;
|
|
239
|
+
const last = closed ? n : n - 1;
|
|
240
|
+
for (let k = 0; k < last; k++) {
|
|
241
|
+
const next = (k + 1) % n;
|
|
242
|
+
out.set(points.subarray(3 * k, 3 * k + 3), at);
|
|
243
|
+
out.set(points.subarray(3 * next, 3 * next + 3), at + 3);
|
|
244
|
+
at += 6;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return out;
|
|
248
|
+
}
|
|
249
|
+
/** The result for a mesh, or `null` when the section or mesh is gone or the section does not cut it. */
|
|
250
|
+
function resultOf(scene, section, bound) {
|
|
251
|
+
try {
|
|
252
|
+
return scene.section(section, bound.mesh);
|
|
253
|
+
} catch (error) {
|
|
254
|
+
if (error instanceof FastMeshError && (error.code === "INVALID_ARGUMENT" || error.code === "UNKNOWN_MESH" || error.code === "UNKNOWN_SECTION")) return null;
|
|
255
|
+
throw error;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Draws a section of the scene and clips the bound meshes at its plane.
|
|
260
|
+
*
|
|
261
|
+
* The plane is added to each bound mesh's `material.clippingPlanes` (unless
|
|
262
|
+
* `clip` is `false`), which three applies only when the renderer has
|
|
263
|
+
* `renderer.localClippingEnabled = true`. A mesh with an array of materials
|
|
264
|
+
* has the plane added to each; a material shared by several bound meshes
|
|
265
|
+
* gets it once.
|
|
266
|
+
*
|
|
267
|
+
* The lines and fill of a bound mesh are rebuilt when a frame's
|
|
268
|
+
* `sectionsChanged` lists the section for it; a mesh the section does not
|
|
269
|
+
* cut (or that is removed) draws nothing. Results are shown as last
|
|
270
|
+
* computed: a `"lazy"` or `"manual"` mesh's lag until the scene computes
|
|
271
|
+
* them. The plane follows `Scene.setSectionPlane` on the next frame.
|
|
272
|
+
* When the section is removed, the drawing is emptied and stops following
|
|
273
|
+
* the scene, and the plane is taken off the materials.
|
|
274
|
+
*
|
|
275
|
+
* Like `bindMesh`, it only listens through {@link SceneLike.onChanges}: call
|
|
276
|
+
* `scene.commitFrame()` once per animation frame.
|
|
277
|
+
*/
|
|
278
|
+
function bindSection(scene, section, bound, options = {}) {
|
|
279
|
+
const group = new THREE.Group();
|
|
280
|
+
const plane = new THREE.Plane();
|
|
281
|
+
const lineMaterial = new THREE.LineBasicMaterial({ color: options.lineColor ?? DEFAULT_LINE });
|
|
282
|
+
const fillMaterial = new THREE.MeshBasicMaterial({
|
|
283
|
+
color: options.fillColor ?? DEFAULT_FILL,
|
|
284
|
+
side: THREE.DoubleSide
|
|
285
|
+
});
|
|
286
|
+
let last = "";
|
|
287
|
+
const updatePlane = (p) => {
|
|
288
|
+
const key = `${p.origin.join()},${p.normal.join()}`;
|
|
289
|
+
if (key === last) return;
|
|
290
|
+
last = key;
|
|
291
|
+
plane.setFromNormalAndCoplanarPoint(new THREE.Vector3(...p.normal), new THREE.Vector3(...p.origin));
|
|
292
|
+
};
|
|
293
|
+
updatePlane(scene.sectionPlane(section));
|
|
294
|
+
const entries = bound.map((b) => {
|
|
295
|
+
const lines = new THREE.LineSegments(new THREE.BufferGeometry(), lineMaterial);
|
|
296
|
+
const fill = new THREE.Mesh(new THREE.BufferGeometry(), fillMaterial);
|
|
297
|
+
lines.matrixAutoUpdate = false;
|
|
298
|
+
fill.matrixAutoUpdate = false;
|
|
299
|
+
group.add(lines, fill);
|
|
300
|
+
return {
|
|
301
|
+
bound: b,
|
|
302
|
+
lines,
|
|
303
|
+
fill
|
|
304
|
+
};
|
|
305
|
+
});
|
|
306
|
+
const rebuild = (e) => {
|
|
307
|
+
const result = resultOf(scene, section, e.bound);
|
|
308
|
+
const lines = new THREE.BufferGeometry();
|
|
309
|
+
const fill = new THREE.BufferGeometry();
|
|
310
|
+
if (result !== null) {
|
|
311
|
+
lines.setAttribute("position", new THREE.BufferAttribute(segments(result), 3));
|
|
312
|
+
if (result.fill !== void 0) {
|
|
313
|
+
fill.setAttribute("position", new THREE.BufferAttribute(result.fill.positions, 3));
|
|
314
|
+
fill.setIndex(new THREE.BufferAttribute(result.fill.indices, 1));
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
lines.computeBoundingSphere();
|
|
318
|
+
fill.computeBoundingSphere();
|
|
319
|
+
e.lines.geometry.dispose();
|
|
320
|
+
e.fill.geometry.dispose();
|
|
321
|
+
e.lines.geometry = lines;
|
|
322
|
+
e.fill.geometry = fill;
|
|
323
|
+
};
|
|
324
|
+
for (const e of entries) rebuild(e);
|
|
325
|
+
const clipped = [];
|
|
326
|
+
if (options.clip ?? true) for (const b of bound) {
|
|
327
|
+
const materials = b.object.material;
|
|
328
|
+
for (const material of Array.isArray(materials) ? materials : [materials]) {
|
|
329
|
+
if (clipped.some((c) => c.material === material)) continue;
|
|
330
|
+
const created = material.clippingPlanes === null;
|
|
331
|
+
material.clippingPlanes = [...material.clippingPlanes ?? [], plane];
|
|
332
|
+
material.needsUpdate = true;
|
|
333
|
+
clipped.push({
|
|
334
|
+
material,
|
|
335
|
+
created
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
/** Takes the plane off the materials it was added to. */
|
|
340
|
+
const unclip = () => {
|
|
341
|
+
for (const { material, created } of clipped.splice(0)) {
|
|
342
|
+
const rest = (material.clippingPlanes ?? []).filter((p) => p !== plane);
|
|
343
|
+
material.clippingPlanes = rest.length === 0 && created ? null : rest;
|
|
344
|
+
material.needsUpdate = true;
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
let unsubscribe;
|
|
348
|
+
const detach = () => {
|
|
349
|
+
unsubscribe?.();
|
|
350
|
+
unsubscribe = void 0;
|
|
351
|
+
};
|
|
352
|
+
unsubscribe = scene.onChanges((changes) => {
|
|
353
|
+
let plane;
|
|
354
|
+
try {
|
|
355
|
+
plane = scene.sectionPlane(section);
|
|
356
|
+
} catch (error) {
|
|
357
|
+
if (error instanceof FastMeshError && error.code === "UNKNOWN_SECTION") {
|
|
358
|
+
for (const e of entries) rebuild(e);
|
|
359
|
+
unclip();
|
|
360
|
+
detach();
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
throw error;
|
|
364
|
+
}
|
|
365
|
+
updatePlane(plane);
|
|
366
|
+
for (const c of changes) {
|
|
367
|
+
if (!c.removed && !c.sectionsChanged.includes(section)) continue;
|
|
368
|
+
for (const e of entries) if (e.bound.mesh === c.mesh) rebuild(e);
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
let disposed = false;
|
|
372
|
+
return {
|
|
373
|
+
group,
|
|
374
|
+
plane,
|
|
375
|
+
dispose() {
|
|
376
|
+
detach();
|
|
377
|
+
if (disposed) return;
|
|
378
|
+
disposed = true;
|
|
379
|
+
unclip();
|
|
380
|
+
for (const e of entries) {
|
|
381
|
+
e.lines.geometry.dispose();
|
|
382
|
+
e.fill.geometry.dispose();
|
|
383
|
+
}
|
|
384
|
+
lineMaterial.dispose();
|
|
385
|
+
fillMaterial.dispose();
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
//#endregion
|
|
390
|
+
export { attachTransformControls, bindMesh, bindSection };
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { c as wasmMemory, n as call, t as FastMeshError } from "./core-Cke_kZ2e.js";
|
|
2
|
+
import { n as DEFAULT_NAN, t as DEFAULT_BASE } from "./colormap-D_xH5hI5.js";
|
|
3
|
+
//#region src/handles.ts
|
|
4
|
+
/**
|
|
5
|
+
* A mesh in a {@link Scene}. Opaque: pass it to the scene's methods. The same
|
|
6
|
+
* mesh always gives the same handle object, so handles compare with `===`.
|
|
7
|
+
*/
|
|
8
|
+
var MeshHandle = class {};
|
|
9
|
+
/**
|
|
10
|
+
* A field in a {@link Scene}. Opaque and deduplicated like {@link MeshHandle}.
|
|
11
|
+
* The scene keeps every field handle it gave out for its own lifetime, even
|
|
12
|
+
* after the field is removed, so a removal in a change record names the same
|
|
13
|
+
* object you hold.
|
|
14
|
+
*/
|
|
15
|
+
var FieldHandle = class {};
|
|
16
|
+
/**
|
|
17
|
+
* A section in a {@link Scene}. Opaque and deduplicated like {@link MeshHandle},
|
|
18
|
+
* and kept for the scene's lifetime like {@link FieldHandle}.
|
|
19
|
+
*/
|
|
20
|
+
var SectionHandle = class {};
|
|
21
|
+
/** A quaternion as a row-major rotation matrix, normalised first, in f64. */
|
|
22
|
+
function quatToMat3(q) {
|
|
23
|
+
const n = Math.hypot(q[0], q[1], q[2], q[3]);
|
|
24
|
+
if (!(n > 0) || !Number.isFinite(n)) throw new FastMeshError("INVALID_ARGUMENT", "quaternion must be non-zero and finite");
|
|
25
|
+
const x = q[0] / n;
|
|
26
|
+
const y = q[1] / n;
|
|
27
|
+
const z = q[2] / n;
|
|
28
|
+
const w = q[3] / n;
|
|
29
|
+
return [
|
|
30
|
+
1 - 2 * (y * y + z * z),
|
|
31
|
+
2 * (x * y - z * w),
|
|
32
|
+
2 * (x * z + y * w),
|
|
33
|
+
2 * (x * y + z * w),
|
|
34
|
+
1 - 2 * (x * x + z * z),
|
|
35
|
+
2 * (y * z - x * w),
|
|
36
|
+
2 * (x * z - y * w),
|
|
37
|
+
2 * (y * z + x * w),
|
|
38
|
+
1 - 2 * (x * x + y * y)
|
|
39
|
+
];
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* A placement rotation as 9 numbers, row-major (a quaternion converted in
|
|
43
|
+
* f64). Internal: shared with the Worker client, not part of the public API.
|
|
44
|
+
*/
|
|
45
|
+
function rotationMatrix(rotation) {
|
|
46
|
+
if (rotation.length === 4) return quatToMat3(rotation);
|
|
47
|
+
if (rotation.length === 9) return Array.from(rotation);
|
|
48
|
+
throw new FastMeshError("INVALID_ARGUMENT", "rotation must be a 3×3 matrix (9 numbers) or a quaternion (4 numbers)");
|
|
49
|
+
}
|
|
50
|
+
/** A distance field's target or targets (`DistanceFieldDesc.to`), as a list. Internal. */
|
|
51
|
+
function targetsOf(to) {
|
|
52
|
+
return Array.isArray(to) ? to : [to];
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/rule.ts
|
|
56
|
+
const MAPS = {
|
|
57
|
+
turbo: 0,
|
|
58
|
+
viridis: 1,
|
|
59
|
+
coolwarm: 2,
|
|
60
|
+
grayscale: 3
|
|
61
|
+
};
|
|
62
|
+
const OUT_OF_RANGE = {
|
|
63
|
+
clamp: 0,
|
|
64
|
+
base: 1
|
|
65
|
+
};
|
|
66
|
+
function invalid(message) {
|
|
67
|
+
return new FastMeshError("INVALID_ARGUMENT", message);
|
|
68
|
+
}
|
|
69
|
+
/** The code of a string option in `table`, or `INVALID_ARGUMENT` naming `what`. */
|
|
70
|
+
function codeIn(table, value, what) {
|
|
71
|
+
if (!Object.hasOwn(table, value)) throw invalid(`${what} must be one of ${Object.keys(table).join(", ")}`);
|
|
72
|
+
return table[value];
|
|
73
|
+
}
|
|
74
|
+
/** A colour rule as the binding's `(map, rule)`: see `parse_rule` in the Rust crate. */
|
|
75
|
+
function encodeRule(rule) {
|
|
76
|
+
if (rule.range?.length !== 2) throw invalid("range must be [lo, hi]");
|
|
77
|
+
if (rule.center !== void 0 && Number.isNaN(rule.center)) throw invalid("color center must lie within the range");
|
|
78
|
+
const base = rule.base ?? DEFAULT_BASE;
|
|
79
|
+
const nan = rule.nan ?? DEFAULT_NAN;
|
|
80
|
+
if (base.length !== 4) throw invalid("base must have 4 channels");
|
|
81
|
+
if (nan.length !== 4) throw invalid("nan must have 4 channels");
|
|
82
|
+
const values = [
|
|
83
|
+
rule.range[0],
|
|
84
|
+
rule.range[1],
|
|
85
|
+
rule.center ?? NaN,
|
|
86
|
+
codeIn(OUT_OF_RANGE, rule.below ?? "clamp", "below"),
|
|
87
|
+
codeIn(OUT_OF_RANGE, rule.above ?? "base", "above"),
|
|
88
|
+
...base,
|
|
89
|
+
...nan
|
|
90
|
+
];
|
|
91
|
+
let map;
|
|
92
|
+
if (typeof rule.map === "string") map = codeIn(MAPS, rule.map, "map");
|
|
93
|
+
else if (typeof rule.map === "object" && rule.map !== null && Array.isArray(rule.map.stops)) {
|
|
94
|
+
map = 4;
|
|
95
|
+
for (const stop of rule.map.stops) {
|
|
96
|
+
if (!Array.isArray(stop) || stop.length !== 2) throw invalid("each stop must be [t, rgba]");
|
|
97
|
+
const [t, color] = stop;
|
|
98
|
+
if (typeof color !== "object" || color === null || color.length !== 4) throw invalid("stop colours must have 4 channels");
|
|
99
|
+
values.push(t, ...Array.from(color));
|
|
100
|
+
}
|
|
101
|
+
} else throw invalid("map must be \"turbo\", \"viridis\", \"coolwarm\", \"grayscale\" or { stops }");
|
|
102
|
+
return {
|
|
103
|
+
map,
|
|
104
|
+
values: Float32Array.from(values)
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* A deep, frozen copy of a colour rule (the field handle kept, not frozen):
|
|
109
|
+
* its range and colours, and a custom map's stops with their colours, so
|
|
110
|
+
* later changes to the caller's object do not leak in and callers of
|
|
111
|
+
* `coloring()` cannot change it. Colours given as typed arrays become plain
|
|
112
|
+
* arrays (a typed array cannot be frozen). Copies what it is given; the
|
|
113
|
+
* engine checks it.
|
|
114
|
+
*/
|
|
115
|
+
function copyRule(rule) {
|
|
116
|
+
const frozen = (x) => Array.isArray(x) || ArrayBuffer.isView(x) ? Object.freeze(Array.from(x)) : x;
|
|
117
|
+
const copy = { ...rule };
|
|
118
|
+
for (const [k, v] of Object.entries(copy)) if (k !== "field") copy[k] = frozen(v);
|
|
119
|
+
const stops = rule.map?.stops;
|
|
120
|
+
if (Array.isArray(stops)) copy.map = Object.freeze({ stops: Object.freeze(stops.map((stop) => Array.isArray(stop) ? Object.freeze(stop.map(frozen)) : stop)) });
|
|
121
|
+
return Object.freeze(copy);
|
|
122
|
+
}
|
|
123
|
+
//#endregion
|
|
124
|
+
//#region src/views.ts
|
|
125
|
+
/**
|
|
126
|
+
* Debug-mode check of kept views. The scene bumps the epoch on every call
|
|
127
|
+
* that may allocate or move buffers; a guarded view is a `Proxy` over the
|
|
128
|
+
* typed array that, on every access, warns (once per guard) when the epoch
|
|
129
|
+
* moved since the view was made, or the memory growth detached it.
|
|
130
|
+
*/
|
|
131
|
+
var ViewGuard = class {
|
|
132
|
+
#epoch = 0;
|
|
133
|
+
#warned = false;
|
|
134
|
+
/** A scene call that may allocate or move buffers is about to run. */
|
|
135
|
+
bump() {
|
|
136
|
+
this.#epoch++;
|
|
137
|
+
}
|
|
138
|
+
/** `view`, checked on every access. */
|
|
139
|
+
wrap(view) {
|
|
140
|
+
const born = this.#epoch;
|
|
141
|
+
const attached = view.byteLength > 0;
|
|
142
|
+
const check = () => {
|
|
143
|
+
if (!this.#warned && (this.#epoch !== born || attached && view.byteLength === 0)) {
|
|
144
|
+
this.#warned = true;
|
|
145
|
+
console.warn("fast-mesh-wasm: a render view was read after a scene call that may have moved or freed its buffer, so it may read other data or be empty. Views are only valid until the next scene call: re-acquire them right before use (scene.views(mesh).positions() etc.) and do not keep them.");
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
return new Proxy(view, {
|
|
149
|
+
get(target, key) {
|
|
150
|
+
check();
|
|
151
|
+
const value = Reflect.get(target, key, target);
|
|
152
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
153
|
+
},
|
|
154
|
+
set(target, key, value) {
|
|
155
|
+
check();
|
|
156
|
+
return Reflect.set(target, key, value, target);
|
|
157
|
+
},
|
|
158
|
+
has(target, key) {
|
|
159
|
+
check();
|
|
160
|
+
return Reflect.has(target, key);
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
/**
|
|
166
|
+
* The views of mesh `(index, generation)`. `scene` returns the live raw scene
|
|
167
|
+
* (it throws once the scene is freed); every getter goes through it.
|
|
168
|
+
*/
|
|
169
|
+
function meshViews(scene, index, generation, guard) {
|
|
170
|
+
const build = (make, ptr, len) => {
|
|
171
|
+
const s = scene();
|
|
172
|
+
const p = call(() => ptr(s));
|
|
173
|
+
if (p === 0) return null;
|
|
174
|
+
const view = make(wasmMemory().buffer, p, call(() => len(s)));
|
|
175
|
+
return guard === void 0 ? view : guard.wrap(view);
|
|
176
|
+
};
|
|
177
|
+
const f32 = (b, p, n) => new Float32Array(b, p, n);
|
|
178
|
+
const u8 = (b, p, n) => new Uint8Array(b, p, n);
|
|
179
|
+
const u32 = (b, p, n) => new Uint32Array(b, p, n);
|
|
180
|
+
return {
|
|
181
|
+
positions: () => build(f32, (s) => s.positions_ptr(index, generation), (s) => s.positions_len(index, generation)),
|
|
182
|
+
normals: () => build(f32, (s) => s.normals_ptr(index, generation), (s) => s.normals_len(index, generation)),
|
|
183
|
+
colors: () => build(u8, (s) => s.colors_ptr(index, generation), (s) => s.colors_len(index, generation)),
|
|
184
|
+
values: () => build(f32, (s) => s.values_ptr(index, generation), (s) => s.values_len(index, generation)),
|
|
185
|
+
indices: () => build(u32, (s) => s.indices_ptr(index, generation), (s) => s.indices_len(index, generation))
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
//#endregion
|
|
189
|
+
export { FieldHandle as a, rotationMatrix as c, encodeRule as i, targetsOf as l, meshViews as n, MeshHandle as o, copyRule as r, SectionHandle as s, ViewGuard as t };
|