fast-mesh-wasm 0.6.0 → 0.8.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/README.md +43 -3
- package/dist/client-CJWOKQMM.d.ts +664 -0
- package/dist/{client-BrTp2eMp.js → client-DQADH6KL.js} +1893 -109
- package/dist/{engine-DjdY3GxK.js → engine-D2124Vml.js} +995 -52
- package/dist/io.d.ts +205 -2
- package/dist/io.js +3063 -9
- package/dist/{mesh-DIWD2F5-.js → mesh-BtWBMRMO.js} +2 -1
- package/dist/{mesh-BsqIofgT.d.ts → mesh-DTm2Fnqa.d.ts} +3 -2
- package/dist/node.d.ts +4 -2
- package/dist/node.js +4 -3
- package/dist/primitives-6T0eQiSm.d.ts +157 -0
- package/dist/primitives-wasm-C30Bk4ut.js +3827 -0
- package/dist/primitives-wasm-d9m2vikb.d.ts +43 -0
- package/dist/{protocol-Ci9LLiwO.js → protocol-Bvjmvlb3.js} +38 -3
- package/dist/protocol-Da0kmXuF.d.ts +743 -0
- package/dist/r3f.d.ts +3 -3
- package/dist/r3f.js +3 -3
- package/dist/{react-DzrtrzZy.js → react-DIuy5dyR.js} +12 -3
- package/dist/react.d.ts +6 -3
- package/dist/react.js +1 -1
- package/dist/scene-like-Bx7lZKif.d.ts +4808 -0
- package/dist/{store-DIQmxF_s.js → store-BxzcXy9y.js} +431 -65
- package/dist/{store-BfC6kkf5.d.ts → store-LM3iU8E4.d.ts} +110 -11
- package/dist/store.d.ts +3 -3
- package/dist/store.js +1 -1
- package/dist/three.d.ts +13 -11
- package/dist/three.js +63 -18
- package/dist/views-DPMbo0Sl.js +3147 -0
- package/dist/wasm/fast_mesh_wasm.d.ts +548 -8
- package/dist/wasm/fast_mesh_wasm.js +2100 -89
- package/dist/wasm/fast_mesh_wasm_bg.wasm +0 -0
- package/dist/wasm/fast_mesh_wasm_bg.wasm.d.ts +95 -0
- package/dist/wasm-threads/fast_mesh_wasm.d.ts +548 -8
- package/dist/wasm-threads/fast_mesh_wasm.js +2101 -90
- 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/wave6-wasm-Bt4vTlKz.js +146 -0
- package/dist/web.d.ts +4 -2
- package/dist/web.js +4 -3
- package/dist/worker-engine.d.ts +20 -2
- package/dist/worker-engine.js +1 -1
- package/dist/worker-entry.js +1 -1
- package/dist/worker.d.ts +1 -1
- package/dist/worker.js +1 -1
- package/package.json +1 -1
- package/dist/api-4kz45-Mq.js +0 -63
- package/dist/client-Ds09vLkN.d.ts +0 -336
- package/dist/protocol-Cb23HelE.d.ts +0 -327
- package/dist/scene-D0HNTwoo.js +0 -2216
- package/dist/scene-like-CADk1vfb.d.ts +0 -1755
- package/dist/views-B00kdV0R.js +0 -214
package/README.md
CHANGED
|
@@ -208,6 +208,36 @@ colour(gap);
|
|
|
208
208
|
if (scene.canUndo()) scene.undo();
|
|
209
209
|
scene.redo();
|
|
210
210
|
|
|
211
|
+
// Adds and removals are permanent unless asked otherwise: an `undoable` one is
|
|
212
|
+
// an undo step (or part of the open one), and the mesh comes back with the
|
|
213
|
+
// same handle, data, placement and display. Nothing is copied.
|
|
214
|
+
const copy = scene.addMesh(upperPositions, upperIndices, { undoable: true });
|
|
215
|
+
scene.undo(); // `copy` is gone: scene.has(copy) === false, scene.isRestorable(copy) === true
|
|
216
|
+
scene.redo(); // and back
|
|
217
|
+
scene.removeMesh(copy, { undoable: true });
|
|
218
|
+
scene.undo();
|
|
219
|
+
|
|
220
|
+
// The history holds at most 256 MiB, or what you say here (or pass to
|
|
221
|
+
// `new Scene({ historyBudget })`); past it the oldest steps go. An action
|
|
222
|
+
// larger than all of it, like the removal of a huge mesh, is permanent.
|
|
223
|
+
scene.setHistoryBudget(1024 ** 3);
|
|
224
|
+
scene.onPermanent(({ lost, gone }) => {
|
|
225
|
+
if (lost === "undo") console.log("cannot be undone", gone);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
// State of your own can be undone in order with the meshes: change it, then
|
|
229
|
+
// record how to take that back and make it again. `bytes` counts against the
|
|
230
|
+
// budget, and `drop` is called once the history lets go of the entry.
|
|
231
|
+
let title = "Draft";
|
|
232
|
+
title = "Final";
|
|
233
|
+
scene.recordEntry({
|
|
234
|
+
undo: () => { title = "Draft"; },
|
|
235
|
+
redo: () => { title = "Final"; },
|
|
236
|
+
drop: () => console.log("the title's change is permanent"),
|
|
237
|
+
bytes: 20,
|
|
238
|
+
});
|
|
239
|
+
scene.undo(); // title === "Draft"
|
|
240
|
+
|
|
211
241
|
// Queries are in world space.
|
|
212
242
|
const hit = scene.raycast([0, 0, 50], [0, 0, -1]); // nearest hit over all meshes, or null
|
|
213
243
|
const closest = scene.meshDistance(upper, lower); // { distance, a, b, ... }
|
|
@@ -308,10 +338,11 @@ const values = await scene.fetchFieldValues(gap); // `fieldValues` on Scene
|
|
|
308
338
|
- **Wide-range colouring during a drag.** After each batch the Worker recomputes the fields the batch made due in small steps, and drops the recompute when a newer move, `setPlacement`, edit, repair, compact, undo/redo or removal of either mesh is already queued; other requests wait for it. A pose's colours are applied all at once, never partly. A drag move does not drop a recompute under way: it finishes and is shown, and the newest pose follows. During a sustained drag the colours therefore update about once per recompute, each update exactly one pose, and when the drag ends (`end()`) the last pose's colours arrive within about one recompute (measured before this default changed: one at 1M, but about 1.5 at 10M when most of the mesh is in range): the end drops a recompute kept for an older pose. A pointer that only pauses, without ending the drag, may wait up to two recomputes. Edits, undo/redo, removals and cancels always drop a stale recompute. `fetchFieldValues()` and `fieldStatus()` first bring every due field up to date in one go, which cannot be dropped, so do not poll them during a drag. To keep a field too wide for this live, see `setFieldInteraction` below. On two touching 1M-triangle meshes with `maxDistance` 8, a recompute takes from about 0.07 s to 0.7 s depending on how much of the mesh is in range, and about ten times that at 10M; the Worker is never blocked for long, since a recompute runs in steps: about 12 ms at a time at 1M, and 14 to 18 ms for field builds and edits at 10M (see [docs/benchmarks.md](docs/benchmarks.md)). These are single-threaded figures; on a cross-origin isolated page the threaded engine makes a recompute 1.4 to 4.5 times faster (see [Multithreading (COOP/COEP)](#multithreading-coopcoep)). A contact range (a small `maxDistance`) keeps up with the pointer.
|
|
309
339
|
- **Interactions.** `scene.setFieldInteraction(points)` starts a gesture and `setFieldInteraction(null)` ends it (on `Scene` too). During one, a field with more than `points` finite values updates about `points` vertices closest to the contact per update, those around an edit and a run of others; the rest keep their values and colours, out of date by up to how far the meshes moved. **During an interaction `fetchFieldValues()`, `fieldValues()`, `fieldNearest()` and the colours can hold those stale values, and `fieldStatus()` still says `current: true`:** bringing a field up to date means only the tier's update then. Read values you rely on after ending it: the end recomputes every vertex it left out of date, in steps on the Worker, and the values and colours are then those of an exact update. A gesture that changed nothing costs nothing.
|
|
310
340
|
- **Queries and anything that returns data** (`raycast`, `closestPoint`, `meshDistance`, `intersects`, `findContacts`, `analyze`, `measure`, `register`, `edit`, `repair`, `fetchFieldValues`, `fieldStatus`, `sectionGap`) return promises. Once a frame loop runs they go out with the next `commitFrame()` (or after 100 ms if no frame comes); before the first `commitFrame()` they are sent on a microtask. For latency-critical picking, keep a light picking copy in a main-thread `Scene`.
|
|
311
|
-
- **
|
|
341
|
+
- **Entries of your own.** `scene.recordEntry({ undo, redo, drop, bytes, coalesce })` puts state of the application into the undo history, in order with mesh edits. Its functions run on this thread, when the result that undid, redid or dropped it is applied: after the mirrors, before the promises of the same result and the next `commitFrame()`. An entry recorded while an `undo()` is in flight comes after it in the history, so its `undo` is called before the undone entry's and its `redo` after: every function finds the application as the history has it. `restart()` clears the history and drops every entry.
|
|
342
|
+
- **Errors.** A failed state change shows on its key (`status(key).error`, a `FastMeshError` with the same `code` as on `Scene`); a failed promise rejects with it. After a trap inside the Worker every call fails with `INSTANCE_POISONED`, also when it happens on one of the multithreaded engine's pool threads (a panic or a trap in work split across threads); a few kinds of pool-thread failure still cannot be detected (see [Multithreading (COOP/COEP)](#multithreading-coopcoep)). `await scene.restart()` starts a new Worker and replays the scene from the main thread's mirrors: meshes (compacted: slot ids change, handles do not), placements, fields, colourings and sections. History is cleared (a mesh only undo or redo could have brought back is gone for good), and edits or queries that had not finished fail and must be re-issued.
|
|
312
343
|
- **Not in the Worker scene:** user-opened steps (`beginStep`/`endStep`/`cancelStep`), Lazy and Manual modes (it is always eager) and `takeChanges` (use `onChanges` or the return value of `commitFrame()`). `terminate()` stops the Worker.
|
|
313
344
|
- **Large meshes.** `addMesh` copies its inputs on the main thread (they are then transferred to the Worker) and, without an `origin` option, scans the positions for the bounding-box centre. That takes about 9 ms for 1M triangles and about 63 ms for 10M (see [docs/benchmarks.md](docs/benchmarks.md)); passing `origin` skips the scan.
|
|
314
|
-
- **Large meshes are added in the background.** A mesh of 65,536 triangles or more is built in steps inside the Worker, its upload, store build, normals and the copy of its buffers for the main thread included (about 1.1 s at 1M and 12 s at 10M, single-threaded) while the requests sent after it that do not need it run: toggles, edits, drags and queries of other meshes are answered within milliseconds instead of waiting. A request that names the new mesh (or a field not built yet that is measured on or to it), adds or removes a mesh, or queries every mesh (`raycast`, `closestPoint` without a mesh, `findContacts` without pairs) waits for it and finishes it first (in one call on a threaded engine, in steps that still yield on a single-threaded one), so the engine's state is the same as running the requests in order. Replies can come in another order, though: until the mesh lands, `status("mesh:<id>")` reads `"computing"` and its views are empty; its buffers arrive with that status turning `"ready"`, so a reply to an unrelated request sent after the add can arrive first. `createWorkerScene({ backgroundAddTriangles })` sets the threshold; `Infinity` turns background adds off (every add then comes back with its own batch's result).
|
|
345
|
+
- **Large meshes are added in the background.** A mesh of 65,536 triangles or more is built in steps inside the Worker, its upload, store build, normals and the copy of its buffers for the main thread included (about 1.1 s at 1M and 12 s at 10M, single-threaded) while the requests sent after it that do not need it run: toggles, edits, drags and queries of other meshes are answered within milliseconds instead of waiting. A request that names the new mesh (or a field not built yet that is measured on or to it), adds or removes a mesh, or queries every mesh (`raycast`, `closestPoint` without a mesh, `findContacts` without pairs) waits for it and finishes it first (in one call on a threaded engine, in steps that still yield on a single-threaded one), so the engine's state is the same as running the requests in order. Replies can come in another order, though: until the mesh lands, `status("mesh:<id>")` reads `"computing"` and its views are empty; its buffers arrive with that status turning `"ready"`, so a reply to an unrelated request sent after the add can arrive first. `createWorkerScene({ backgroundAddTriangles })` sets the threshold; `Infinity` turns background adds off (every add then comes back with its own batch's result). An `undoable` add is never in the background: it lands in the step it was made in.
|
|
315
346
|
|
|
316
347
|
**Bundlers.** The Worker script is a separate file, `worker-entry.js`, loaded with `new Worker(new URL("./worker-entry.js", import.meta.url), { type: "module" })`. Vite and other bundlers that understand this pattern pick it up and emit it with the `.wasm` it loads: [`examples/webgpu`](examples/webgpu) uses the default with Vite, in both `vite build` and `vite dev` (open it with `?worker`). If your setup does not handle the pattern, serve the package's `dist/` folder (the Worker script loads the files next to it) and pass the script's URL, or create the Worker yourself:
|
|
317
348
|
|
|
@@ -536,7 +567,7 @@ export function App() {
|
|
|
536
567
|
}
|
|
537
568
|
```
|
|
538
569
|
|
|
539
|
-
**The store.** `store.getSnapshot()` returns a frozen `SceneSnapshot` (meshes, sections, history, selection and an overall status), replaced on every change, with unchanged parts shared between versions. `store.subscribe(listener)` notifies after each new snapshot: at once for an action, and once per task for the scene's events (on a microtask; `getSnapshot()` is up to date before then). An update re-derives only the meshes and sections it touches. What still grows with the scene is one copy of the snapshot's mesh map per change: a native `Map` copy, about 45 µs at 1500 meshes. Actions mirror the scene: `addMesh`, `removeMesh`, `setPlacement`, `drag`, `edit`, `repair`, `undo`, `redo`, `addSection`, `removeSection` and `restart` (Worker only). The declarative ones are new:
|
|
570
|
+
**The store.** `store.getSnapshot()` returns a frozen `SceneSnapshot` (meshes, sections, history, selection and an overall status), replaced on every change, with unchanged parts shared between versions. `store.subscribe(listener)` notifies after each new snapshot: at once for an action, and once per task for the scene's events (on a microtask; `getSnapshot()` is up to date before then). An update re-derives only the meshes and sections it touches. What still grows with the scene is one copy of the snapshot's mesh map per change: a native `Map` copy, about 45 µs at 1500 meshes. The snapshot follows undoable adds and removals: an undone add leaves it, and a mesh that is back has the display and sections it had, with the colourings measured to it applied again (`store.isRestorable(mesh)` tells whether undo or redo can still bring one back). Actions mirror the scene: `addMesh`, `removeMesh`, `setPlacement`, `drag`, `edit`, `repair`, `undo`, `redo`, `addSection`, `removeSection` and `restart` (Worker only). `createSceneStore(scene, { historyBudget })` and `store.setHistoryBudget(bytes)` set the history's memory budget, and `store.onPermanent(listener)` tells of an action too large for it; `store.recordEntry(entry)` puts state of your own into the history. The declarative ones are new:
|
|
540
571
|
|
|
541
572
|
- `setDisplay(mesh, { coloring?, opacity?, visible? })`. A `coloring` colours the mesh by its distance to `distanceTo`: one mesh, or a list of them (the smallest signed distance over them; see [Contact groups](#contact-groups-fields-to-several-meshes)). The store owns the distance field behind it: one per coloured mesh, named `fast-mesh-store:<n>`, kept while the colouring is off, reused when it comes back with the same `distanceTo` (as a set: the order does not matter), `maxDistance` and `sign`, and replaced only when one of them changes. `opacity` (in [0, 1], default 1) and `visible` (default true) live only in the store; the engine never sees them. Display changes never enter the undo history.
|
|
542
573
|
- `setContactGroup(id, { meshes, pairs?, coloring } | null)` colours a group of meshes by contact. Each member gets `coloring` with its partners as `distanceTo`: the members it is paired with in `pairs` (either way round), or every other member with `pairs: "all"` (the default). `coloring` is a `DistanceColoring` without `distanceTo`, so any preset spreads into it. Moving a member recomputes only its own field and its partners'. Setting the group again updates the members; a member dropped from it, or all of them with `null`, is uncoloured and its store-owned field removed. A member the app has coloured itself since (with `setDisplay`) is left alone by those changes. A removed mesh leaves its groups: the others keep their remaining partners, and a member left with none is uncoloured. The colouring is checked when the group is set (`INVALID_ARGUMENT`). Groups are not in the snapshot: the members' `display` and `desired` colourings show them.
|
|
@@ -611,6 +642,15 @@ Meshes must be added through the store (`store.addMesh`). A mesh added straight
|
|
|
611
642
|
| `init(input?)` | `Promise<void>` | Loads the WebAssembly module. Required once in browsers; resolves immediately in Node.js and Bun. Safe to call repeatedly. `input` optionally overrides where the `.wasm` file comes from (`InitInput`). |
|
|
612
643
|
| `computeVertexNormals(positions, indices)` | `Float32Array` | Smooth, area-weighted per-vertex normals. Same length as `positions`. |
|
|
613
644
|
| `computeBounds(positions)` | `Bounds` | Plain object `{ min, max, center, size }`, each a `[x, y, z]` array. |
|
|
645
|
+
| `generatePrimitive(shape)` | `PrimitiveMesh` | A box, cylinder, cone, UV sphere, icosphere, torus, arrow, n-gon prism or plane as `{ positions, indices }` (`Float32Array`, `Uint32Array`): centred on the origin, welded, closed (except the plane) and wound outward. `shape` is `{ kind, ...options }`; every option has a default (see `PrimitiveShape`). |
|
|
646
|
+
| `generateHeightfield(field)` | `PrimitiveMesh` | The relief of a grid of heights as `{ positions, indices }`: closed, outward-wound, a vertex per sample at exactly its height and a flat base. `field` is `{ heights, width, height, pixelSize, base }`, heights row by row from the top left. |
|
|
647
|
+
|
|
648
|
+
`generatePrimitive` is a pure function, not a scene operation: it stores nothing and records no history, so it runs wherever the module is initialised (the main thread, or any Worker of yours). A `WorkerScene` has `scene.generatePrimitive(shape)`, which runs the same generator in the scene's Worker and transfers the buffers back, so a page on the Worker scene needs no wasm of its own. Add the result with `addMesh(positions, indices)`.
|
|
649
|
+
|
|
650
|
+
```ts
|
|
651
|
+
const { positions, indices } = generatePrimitive({ kind: "torus", majorRadius: 2, minorRadius: 0.5, segments: 96 });
|
|
652
|
+
const mesh = scene.addMesh(positions, indices);
|
|
653
|
+
```
|
|
614
654
|
|
|
615
655
|
All functions throw an `Error` with a readable message when a buffer length is not a multiple of 3 or an index is out of range, and when called before `init()` has finished in the browser.
|
|
616
656
|
|