fast-mesh-wasm 0.3.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 CHANGED
@@ -6,6 +6,8 @@
6
6
 
7
7
  Fast triangle mesh utilities for JavaScript and TypeScript, written in Rust and compiled to WebAssembly.
8
8
 
9
+ **[Live demo](https://yoondonggeun.github.io/fast-mesh-wasm/)**: the [showcase app](examples/showcase) in your browser.
10
+
9
11
  - Works in browsers (Vite, webpack, Next.js), Node.js and Bun
10
12
  - Installs the same way with npm, pnpm and bun. No native build step, no install scripts, zero dependencies
11
13
  - Full TypeScript types and inline docs, generated from the Rust source
@@ -292,7 +294,7 @@ const values = await scene.fetchFieldValues(gap); // `fieldValues` on Scene
292
294
  - **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, and edits or queries that had not finished fail and must be re-issued.
293
295
  - **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.
294
296
  - **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.
295
- - **Large meshes are added in the background.** A mesh of 65,536 triangles or more is built in steps inside the Worker (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).
297
+ - **Large meshes are added in the background.** A mesh of 65,536 triangles or more is built in steps inside the Worker, its 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).
296
298
 
297
299
  **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:
298
300
 
@@ -1,5 +1,5 @@
1
1
  import { s as setMemory, t as FastMeshError } from "./core-Cke_kZ2e.js";
2
- import { i as useWasmScene, n as fieldSteps, r as meshAdds, t as Scene } from "./scene-BG3EEyfw.js";
2
+ import { i as useWasmScene, n as fieldSteps, r as meshAdds, t as Scene } from "./scene-BFd6nYRe.js";
3
3
  import { a as supersedes, n as isLastWins, r as keyOf, t as isInit } from "./protocol-CAUYm1gd.js";
4
4
  /**
5
5
  * A module importing a shared memory `e.m` (1 page, at most 2), exporting
@@ -340,6 +340,8 @@ const BACKGROUND_ADD_TRIANGLES = 65536;
340
340
  const ADD_STEP_WORK = 1e5;
341
341
  /** A step budget that computes a whole unit at once. */
342
342
  const ALL_POINTS = 4294967295;
343
+ /** Bytes of a background add's render buffers copied per unit of {@link EngineOptions.addStepWork}. */
344
+ const PACK_BYTES_PER_UNIT = 64;
343
345
  const now = typeof performance !== "undefined" && typeof performance.now === "function" ? () => performance.now() : () => Date.now();
344
346
  /**
345
347
  * Runs the engine on `port` until the instance traps. The WebAssembly module
@@ -518,6 +520,8 @@ var Batch = class {
518
520
  };
519
521
  /** Marks the end of a received batch in the queue: its result is posted there. */
520
522
  const END = Symbol("end of batch");
523
+ /** Whether every byte of a {@link Packed} copy is copied. */
524
+ const packedAll = (p) => p !== void 0 && p.done === p.positions.byteLength + (p.normals?.byteLength ?? 0) + p.indices.byteLength;
521
525
  /**
522
526
  * Yields to the message loop, so batches sent meanwhile are received:
523
527
  * `setImmediate` where there is one (Node: a chain of `MessageChannel` ticks
@@ -548,10 +552,17 @@ var Engine = class {
548
552
  #meshAdds = meshAdds(this.#scene);
549
553
  /** Meshes being added in the background, in arrival order. */
550
554
  #adds = [];
555
+ /**
556
+ * Landed background adds whose render buffers were copied out before:
557
+ * their record in the next frame sends these copies, unless a request
558
+ * changed the mesh since (see {@link Engine.#unpack}).
559
+ */
560
+ #packed = /* @__PURE__ */ new Map();
551
561
  #backgroundAdd;
552
562
  #addStepWork;
553
563
  /** More than one thread: a waiting request finishes adds in one call. */
554
564
  #threaded;
565
+ #keepCopies;
555
566
  /** The revision of the last result posted, for `update` messages. */
556
567
  #lastRev = 0;
557
568
  /** A received batch is being run: its first request started, its result is not posted yet. */
@@ -636,6 +647,7 @@ var Engine = class {
636
647
  const work = Math.floor(options.addStepWork ?? 1e5);
637
648
  this.#addStepWork = work >= 1 ? Math.min(work, ALL_POINTS) : ADD_STEP_WORK;
638
649
  this.#threaded = (options.threads ?? 1) > 1;
650
+ this.#keepCopies = options.keepCopies ?? false;
639
651
  this.#steps.setDefer(true);
640
652
  }
641
653
  /**
@@ -743,6 +755,7 @@ var Engine = class {
743
755
  const key = keyOf(req);
744
756
  if (this.#superseded(req, key) || this.#abandoned(req, key)) return true;
745
757
  if (this.#adds.length > 0 && this.#dependsOnAdds(req) && !await this.#finishAdds(req)) return false;
758
+ this.#unpack(req);
746
759
  try {
747
760
  if (req.kind === "addMesh" && this.#inBackground(req)) {
748
761
  this.#beginAdd(req);
@@ -858,15 +871,22 @@ var Engine = class {
858
871
  }
859
872
  }
860
873
  /**
861
- * Advances the oldest background add by one step and, with `land`, lands
862
- * it once built (landing changes the scene, which would drop an open
863
- * field step unit: the unit's loop steps without landing). A build that
864
- * fails is dropped, with an error status for its add. `false` once
865
- * poisoned.
874
+ * Advances the oldest background add by one step: of its build (BVH and
875
+ * normals), or once built, of the copy of its render buffers
876
+ * ({@link Engine.#pack}); with `land`, lands it once copied (landing
877
+ * changes the scene, which would drop an open field step unit: the
878
+ * unit's loop steps without landing; copying changes nothing). A build
879
+ * that fails is dropped, with an error status for its add. Without
880
+ * `copy`, a built add lands at once and the frame copies its buffers.
881
+ * `false` once poisoned.
866
882
  */
867
- async #advanceAdd(land = true, failed) {
883
+ async #advanceAdd(land = true, failed, copy = true) {
868
884
  const a = this.#adds[0];
869
885
  if (a === void 0) return true;
886
+ if (a.built && (!copy || a.uncopied === true) && land) {
887
+ a.packed = void 0;
888
+ return this.#land(a, failed);
889
+ }
870
890
  if (!a.built) {
871
891
  try {
872
892
  a.built = this.#meshAdds.step(a.token, this.#addStepWork);
@@ -877,8 +897,83 @@ var Engine = class {
877
897
  kind: "addMeshStep",
878
898
  id: a.id
879
899
  });
900
+ } else if (!packedAll(a.packed) && a.uncopied !== true) {
901
+ try {
902
+ this.#pack(a);
903
+ } catch (e) {
904
+ if (isPoison(e)) return this.#addFailed(a, e, false, failed);
905
+ a.packed = void 0;
906
+ a.uncopied = true;
907
+ }
908
+ this.#onChunk?.({
909
+ kind: "addMeshPack",
910
+ id: a.id
911
+ });
880
912
  }
881
- return a.built && land ? this.#land(a, failed) : true;
913
+ return (packedAll(a.packed) || a.uncopied === true) && land ? this.#land(a, failed) : true;
914
+ }
915
+ /**
916
+ * Whether `req` may change the positions, normals or triangles of the
917
+ * mesh `id`: an edit, repair, compaction or removal of it, or any undo
918
+ * or redo.
919
+ */
920
+ #changesMesh(req, id) {
921
+ switch (req.kind) {
922
+ case "edit":
923
+ case "repair":
924
+ case "compact":
925
+ case "removeMesh": return req.id === id;
926
+ case "undo":
927
+ case "redo": return true;
928
+ default: return false;
929
+ }
930
+ }
931
+ /**
932
+ * Copies about {@link EngineOptions.addStepWork} × 64 more bytes of a
933
+ * built add's render buffers (positions, normals, indices) out of wasm
934
+ * memory, so that the frame it lands in sends them instead of copying
935
+ * the whole mesh then. The build's buffers are the added mesh's, moved
936
+ * into the scene unchanged, so the copies are what the frame would copy,
937
+ * bit for bit, unless a request changes the mesh first
938
+ * ({@link Engine.#unpack}).
939
+ */
940
+ #pack(a) {
941
+ const views = this.#meshAdds.buffers(a.token);
942
+ const p = a.packed ??= {
943
+ positions: new ArrayBuffer(views.positions.byteLength),
944
+ normals: views.normals === null ? null : new ArrayBuffer(views.normals.byteLength),
945
+ indices: new ArrayBuffer(views.indices.byteLength),
946
+ done: 0
947
+ };
948
+ const parts = [
949
+ [views.positions, p.positions],
950
+ [views.normals, p.normals],
951
+ [views.indices, p.indices]
952
+ ];
953
+ let budget = this.#addStepWork * PACK_BYTES_PER_UNIT;
954
+ let start = 0;
955
+ for (const [view, out] of parts) {
956
+ if (view === null || out === null) continue;
957
+ const end = start + out.byteLength;
958
+ if (budget > 0 && p.done < end) {
959
+ const from = p.done - start;
960
+ const n = Math.min(out.byteLength - from, budget);
961
+ new Uint8Array(out, from, n).set(new Uint8Array(view.buffer, view.byteOffset + from, n));
962
+ p.done += n;
963
+ budget -= n;
964
+ }
965
+ start = end;
966
+ }
967
+ }
968
+ /**
969
+ * Forgets the copied-out buffers of a landed mesh that `req` may change
970
+ * (an edit, repair or compaction of it, an undo or redo), to free them
971
+ * early. Correctness does not depend on it: the copies are sent only
972
+ * while the mesh's content stamp is the one it landed with.
973
+ */
974
+ #unpack(req) {
975
+ if (this.#packed.size === 0 || this.#keepCopies) return;
976
+ for (const h of [...this.#packed.keys()]) if (this.#changesMesh(req, this.#meshId(h))) this.#packed.delete(h);
882
977
  }
883
978
  /**
884
979
  * Finishes every background add, in arrival order, for `failed`, the
@@ -887,7 +982,9 @@ var Engine = class {
887
982
  * what is left of its BVH at once with the parallel one-call build
888
983
  * (several times faster than steps). Single-threaded the two cost the
889
984
  * same, so it keeps stepping, yielding (to receive only) between steps,
890
- * so that newer batches still arrive and coalesce.
985
+ * so that newer batches still arrive and coalesce; it skips the copy of
986
+ * an add's buffers when `failed` changes that mesh (the frame copies
987
+ * them anyway).
891
988
  */
892
989
  async #finishAdds(failed) {
893
990
  while (this.#adds.length > 0) {
@@ -895,7 +992,8 @@ var Engine = class {
895
992
  if (!this.#land(this.#adds[0], failed)) return false;
896
993
  continue;
897
994
  }
898
- if (!await this.#advanceAdd(true, failed)) return false;
995
+ const copy = failed === void 0 || !this.#changesMesh(failed, this.#adds[0].id);
996
+ if (!await this.#advanceAdd(true, failed, copy)) return false;
899
997
  if (this.#adds.length > 0) await this.#maybeYield();
900
998
  }
901
999
  return true;
@@ -915,6 +1013,12 @@ var Engine = class {
915
1013
  }
916
1014
  this.#meshes.set(a.id, h);
917
1015
  this.#meshIds.set(h, a.id);
1016
+ if (packedAll(a.packed)) try {
1017
+ a.packed.stamp = this.#meshAdds.stamp(h);
1018
+ this.#packed.set(h, a.packed);
1019
+ } catch (e) {
1020
+ if (isPoison(e)) return this.#addFailed(a, e, true, failed);
1021
+ }
918
1022
  this.#onChunk?.({
919
1023
  kind: "addMesh",
920
1024
  id: a.id
@@ -1724,6 +1828,7 @@ var Engine = class {
1724
1828
  canUndo: s.canUndo(),
1725
1829
  canRedo: s.canRedo()
1726
1830
  };
1831
+ this.#packed.clear();
1727
1832
  for (const h of this.#removed.splice(0)) this.#meshIds.delete(h);
1728
1833
  for (const h of this.#removedFields.splice(0)) this.#fieldIds.delete(h);
1729
1834
  for (const h of this.#removedSections.splice(0)) this.#sectionIds.delete(h);
@@ -1802,11 +1907,15 @@ var Engine = class {
1802
1907
  if (c.removed) return wire;
1803
1908
  wire.vertexSlots = this.#scene.vertexSlots(c.mesh);
1804
1909
  wire.triangleSlots = this.#scene.triangleSlots(c.mesh);
1910
+ let packed = c.added ? this.#packed.get(c.mesh) : void 0;
1911
+ if (packed !== void 0 && packed.stamp !== this.#meshAdds.stamp(c.mesh)) packed = void 0;
1805
1912
  const views = this.#scene.views(c.mesh);
1806
1913
  for (const name of Object.keys(STRIDE)) {
1807
1914
  const view = name === "values" && !this.#mirrorValues ? null : views[name]?.() ?? null;
1808
1915
  if (view !== null) {
1809
- const bytes = copyRanges(view, rangesOf(c, name), STRIDE[name]);
1916
+ const ranges = rangesOf(c, name);
1917
+ const copy = name === "positions" || name === "normals" || name === "indices" ? packed?.[name] : void 0;
1918
+ const bytes = ranges === "all" && copy != null && copy.byteLength === view.byteLength ? copy : copyRanges(view, ranges, STRIDE[name]);
1810
1919
  wire.bytes[name] = bytes;
1811
1920
  b.transfer.push(bytes);
1812
1921
  }
package/dist/node.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { t as FastMeshError } from "./core-Cke_kZ2e.js";
2
2
  import { n as computeVertexNormals, r as markInitialized, t as computeBounds } from "./api-BUy7FRx7.js";
3
- import { t as Scene } from "./scene-BG3EEyfw.js";
3
+ import { t as Scene } from "./scene-BFd6nYRe.js";
4
4
  import { initSync } from "./wasm/fast_mesh_wasm.js";
5
5
  import { readFileSync } from "node:fs";
6
6
  //#region src/node.ts
@@ -1097,10 +1097,23 @@ var Scene = class {
1097
1097
  return call(() => s.begin_add_mesh(p, i, origin, mode, options.normals ?? true));
1098
1098
  },
1099
1099
  step: (token, budget) => call(() => scene.#scene().add_mesh_step(token, budget)),
1100
+ buffers: (token) => {
1101
+ const [pp, pl, np, nl, ip, il] = call(() => scene.#peek().add_mesh_buffers(token));
1102
+ const memory = wasmMemory().buffer;
1103
+ return {
1104
+ positions: new Float32Array(memory, pp, pl),
1105
+ normals: np === 0 ? null : new Float32Array(memory, np, nl),
1106
+ indices: new Uint32Array(memory, ip, il)
1107
+ };
1108
+ },
1100
1109
  finish: (token) => {
1101
1110
  const id = call(() => scene.#scene().finish_add_mesh(token));
1102
1111
  return scene.#meshHandle(id[0], id[1]);
1103
1112
  },
1113
+ stamp: (mesh) => {
1114
+ const { index, generation } = scene.#id(mesh);
1115
+ return call(() => scene.#peek().content_stamp(index, generation));
1116
+ },
1104
1117
  drop: (token) => call(() => scene.#peek().drop_add_mesh(token))
1105
1118
  });
1106
1119
  }
@@ -32,6 +32,16 @@ export class WasmScene {
32
32
  * the identity. Nothing is added on error.
33
33
  */
34
34
  add_mesh(positions: Float64Array, indices: Uint32Array, origin: Float64Array | null | undefined, mode: number, normals: boolean): Uint32Array;
35
+ /**
36
+ * The render buffers of a mesh being added in steps, as
37
+ * `[positions_ptr, positions_len, normals_ptr, normals_len,
38
+ * indices_ptr, indices_len]` (as the `*_ptr` / `*_len` methods give
39
+ * them once it is added; normals 0, 0 until built or when off). The
40
+ * added mesh keeps these addresses and contents until it changes, so
41
+ * they can be copied out before it lands. Valid until the next call
42
+ * that may allocate.
43
+ */
44
+ add_mesh_buffers(build: number): Uint32Array;
35
45
  /**
36
46
  * Builds about `budget` more units of a mesh being added (see
37
47
  * `MeshBuild::step`); `true` once its BVH is built. A failed build is
@@ -108,6 +118,11 @@ export class WasmScene {
108
118
  * edits leave the history (placements stay).
109
119
  */
110
120
  compact(index: number, generation: number): void;
121
+ /**
122
+ * The mesh's content stamp (`Scene::content_stamp`): it goes up
123
+ * whenever the mesh's positions, triangles or normals change.
124
+ */
125
+ content_stamp(index: number, generation: number): number;
111
126
  /**
112
127
  * Drops a mesh being added in steps (nothing was added).
113
128
  */
@@ -408,6 +423,7 @@ export interface InitOutput {
408
423
  readonly wasmscene_add_distance_field: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number];
409
424
  readonly wasmscene_add_distance_field_deferred: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number];
410
425
  readonly wasmscene_add_mesh: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => [number, number, number, number];
426
+ readonly wasmscene_add_mesh_buffers: (a: number, b: number) => [number, number, number, number];
411
427
  readonly wasmscene_add_mesh_step: (a: number, b: number, c: number) => [number, number, number];
412
428
  readonly wasmscene_add_section: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number];
413
429
  readonly wasmscene_analyze: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number];
@@ -423,6 +439,7 @@ export interface InitOutput {
423
439
  readonly wasmscene_colors_len: (a: number, b: number, c: number) => [number, number, number];
424
440
  readonly wasmscene_colors_ptr: (a: number, b: number, c: number) => [number, number, number];
425
441
  readonly wasmscene_compact: (a: number, b: number, c: number) => [number, number];
442
+ readonly wasmscene_content_stamp: (a: number, b: number, c: number) => [number, number, number];
426
443
  readonly wasmscene_drop_add_mesh: (a: number, b: number) => void;
427
444
  readonly wasmscene_edit: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number, number];
428
445
  readonly wasmscene_end_step: (a: number) => [number, number];
@@ -91,6 +91,26 @@ export class WasmScene {
91
91
  wasm.__wbindgen_free(ret[0], ret[1] * 4, 4);
92
92
  return v4;
93
93
  }
94
+ /**
95
+ * The render buffers of a mesh being added in steps, as
96
+ * `[positions_ptr, positions_len, normals_ptr, normals_len,
97
+ * indices_ptr, indices_len]` (as the `*_ptr` / `*_len` methods give
98
+ * them once it is added; normals 0, 0 until built or when off). The
99
+ * added mesh keeps these addresses and contents until it changes, so
100
+ * they can be copied out before it lands. Valid until the next call
101
+ * that may allocate.
102
+ * @param {number} build
103
+ * @returns {Uint32Array}
104
+ */
105
+ add_mesh_buffers(build) {
106
+ const ret = wasm.wasmscene_add_mesh_buffers(this.__wbg_ptr, build);
107
+ if (ret[3]) {
108
+ throw takeFromExternrefTable0(ret[2]);
109
+ }
110
+ var v1 = getArrayU32FromWasm0(ret[0], ret[1]).slice();
111
+ wasm.__wbindgen_free(ret[0], ret[1] * 4, 4);
112
+ return v1;
113
+ }
94
114
  /**
95
115
  * Builds about `budget` more units of a mesh being added (see
96
116
  * `MeshBuild::step`); `true` once its BVH is built. A failed build is
@@ -302,6 +322,20 @@ export class WasmScene {
302
322
  throw takeFromExternrefTable0(ret[0]);
303
323
  }
304
324
  }
325
+ /**
326
+ * The mesh's content stamp (`Scene::content_stamp`): it goes up
327
+ * whenever the mesh's positions, triangles or normals change.
328
+ * @param {number} index
329
+ * @param {number} generation
330
+ * @returns {number}
331
+ */
332
+ content_stamp(index, generation) {
333
+ const ret = wasm.wasmscene_content_stamp(this.__wbg_ptr, index, generation);
334
+ if (ret[2]) {
335
+ throw takeFromExternrefTable0(ret[1]);
336
+ }
337
+ return ret[0];
338
+ }
305
339
  /**
306
340
  * Drops a mesh being added in steps (nothing was added).
307
341
  * @param {number} build
Binary file
@@ -8,6 +8,7 @@ export const wasmscene_abandon_field_step: (a: number) => void;
8
8
  export const wasmscene_add_distance_field: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number];
9
9
  export const wasmscene_add_distance_field_deferred: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number];
10
10
  export const wasmscene_add_mesh: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => [number, number, number, number];
11
+ export const wasmscene_add_mesh_buffers: (a: number, b: number) => [number, number, number, number];
11
12
  export const wasmscene_add_mesh_step: (a: number, b: number, c: number) => [number, number, number];
12
13
  export const wasmscene_add_section: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number];
13
14
  export const wasmscene_analyze: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number];
@@ -23,6 +24,7 @@ export const wasmscene_closest_point_on: (a: number, b: number, c: number, d: nu
23
24
  export const wasmscene_colors_len: (a: number, b: number, c: number) => [number, number, number];
24
25
  export const wasmscene_colors_ptr: (a: number, b: number, c: number) => [number, number, number];
25
26
  export const wasmscene_compact: (a: number, b: number, c: number) => [number, number];
27
+ export const wasmscene_content_stamp: (a: number, b: number, c: number) => [number, number, number];
26
28
  export const wasmscene_drop_add_mesh: (a: number, b: number) => void;
27
29
  export const wasmscene_edit: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number, number];
28
30
  export const wasmscene_end_step: (a: number) => [number, number];
@@ -32,6 +32,16 @@ export class WasmScene {
32
32
  * the identity. Nothing is added on error.
33
33
  */
34
34
  add_mesh(positions: Float64Array, indices: Uint32Array, origin: Float64Array | null | undefined, mode: number, normals: boolean): Uint32Array;
35
+ /**
36
+ * The render buffers of a mesh being added in steps, as
37
+ * `[positions_ptr, positions_len, normals_ptr, normals_len,
38
+ * indices_ptr, indices_len]` (as the `*_ptr` / `*_len` methods give
39
+ * them once it is added; normals 0, 0 until built or when off). The
40
+ * added mesh keeps these addresses and contents until it changes, so
41
+ * they can be copied out before it lands. Valid until the next call
42
+ * that may allocate.
43
+ */
44
+ add_mesh_buffers(build: number): Uint32Array;
35
45
  /**
36
46
  * Builds about `budget` more units of a mesh being added (see
37
47
  * `MeshBuild::step`); `true` once its BVH is built. A failed build is
@@ -108,6 +118,11 @@ export class WasmScene {
108
118
  * edits leave the history (placements stay).
109
119
  */
110
120
  compact(index: number, generation: number): void;
121
+ /**
122
+ * The mesh's content stamp (`Scene::content_stamp`): it goes up
123
+ * whenever the mesh's positions, triangles or normals change.
124
+ */
125
+ content_stamp(index: number, generation: number): number;
111
126
  /**
112
127
  * Drops a mesh being added in steps (nothing was added).
113
128
  */
@@ -431,6 +446,7 @@ export interface InitOutput {
431
446
  readonly wasmscene_add_distance_field: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number];
432
447
  readonly wasmscene_add_distance_field_deferred: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number];
433
448
  readonly wasmscene_add_mesh: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => [number, number, number, number];
449
+ readonly wasmscene_add_mesh_buffers: (a: number, b: number) => [number, number, number, number];
434
450
  readonly wasmscene_add_mesh_step: (a: number, b: number, c: number) => [number, number, number];
435
451
  readonly wasmscene_add_section: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number];
436
452
  readonly wasmscene_analyze: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number];
@@ -446,6 +462,7 @@ export interface InitOutput {
446
462
  readonly wasmscene_colors_len: (a: number, b: number, c: number) => [number, number, number];
447
463
  readonly wasmscene_colors_ptr: (a: number, b: number, c: number) => [number, number, number];
448
464
  readonly wasmscene_compact: (a: number, b: number, c: number) => [number, number];
465
+ readonly wasmscene_content_stamp: (a: number, b: number, c: number) => [number, number, number];
449
466
  readonly wasmscene_drop_add_mesh: (a: number, b: number) => void;
450
467
  readonly wasmscene_edit: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number, number];
451
468
  readonly wasmscene_end_step: (a: number) => [number, number];
@@ -94,6 +94,26 @@ export class WasmScene {
94
94
  wasm.__wbindgen_free(ret[0], ret[1] * 4, 4);
95
95
  return v4;
96
96
  }
97
+ /**
98
+ * The render buffers of a mesh being added in steps, as
99
+ * `[positions_ptr, positions_len, normals_ptr, normals_len,
100
+ * indices_ptr, indices_len]` (as the `*_ptr` / `*_len` methods give
101
+ * them once it is added; normals 0, 0 until built or when off). The
102
+ * added mesh keeps these addresses and contents until it changes, so
103
+ * they can be copied out before it lands. Valid until the next call
104
+ * that may allocate.
105
+ * @param {number} build
106
+ * @returns {Uint32Array}
107
+ */
108
+ add_mesh_buffers(build) {
109
+ const ret = wasm.wasmscene_add_mesh_buffers(this.__wbg_ptr, build);
110
+ if (ret[3]) {
111
+ throw takeFromExternrefTable0(ret[2]);
112
+ }
113
+ var v1 = getArrayU32FromWasm0(ret[0], ret[1]).slice();
114
+ wasm.__wbindgen_free(ret[0], ret[1] * 4, 4);
115
+ return v1;
116
+ }
97
117
  /**
98
118
  * Builds about `budget` more units of a mesh being added (see
99
119
  * `MeshBuild::step`); `true` once its BVH is built. A failed build is
@@ -305,6 +325,20 @@ export class WasmScene {
305
325
  throw takeFromExternrefTable0(ret[0]);
306
326
  }
307
327
  }
328
+ /**
329
+ * The mesh's content stamp (`Scene::content_stamp`): it goes up
330
+ * whenever the mesh's positions, triangles or normals change.
331
+ * @param {number} index
332
+ * @param {number} generation
333
+ * @returns {number}
334
+ */
335
+ content_stamp(index, generation) {
336
+ const ret = wasm.wasmscene_content_stamp(this.__wbg_ptr, index, generation);
337
+ if (ret[2]) {
338
+ throw takeFromExternrefTable0(ret[1]);
339
+ }
340
+ return ret[0];
341
+ }
308
342
  /**
309
343
  * Drops a mesh being added in steps (nothing was added).
310
344
  * @param {number} build
@@ -9,6 +9,7 @@ export const wasmscene_abandon_field_step: (a: number) => void;
9
9
  export const wasmscene_add_distance_field: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number];
10
10
  export const wasmscene_add_distance_field_deferred: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number];
11
11
  export const wasmscene_add_mesh: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => [number, number, number, number];
12
+ export const wasmscene_add_mesh_buffers: (a: number, b: number) => [number, number, number, number];
12
13
  export const wasmscene_add_mesh_step: (a: number, b: number, c: number) => [number, number, number];
13
14
  export const wasmscene_add_section: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number];
14
15
  export const wasmscene_analyze: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number];
@@ -24,6 +25,7 @@ export const wasmscene_closest_point_on: (a: number, b: number, c: number, d: nu
24
25
  export const wasmscene_colors_len: (a: number, b: number, c: number) => [number, number, number];
25
26
  export const wasmscene_colors_ptr: (a: number, b: number, c: number) => [number, number, number];
26
27
  export const wasmscene_compact: (a: number, b: number, c: number) => [number, number];
28
+ export const wasmscene_content_stamp: (a: number, b: number, c: number) => [number, number, number];
27
29
  export const wasmscene_drop_add_mesh: (a: number, b: number) => void;
28
30
  export const wasmscene_edit: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number, number];
29
31
  export const wasmscene_end_step: (a: number) => [number, number];
package/dist/web.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { t as FastMeshError } from "./core-Cke_kZ2e.js";
2
2
  import { n as computeVertexNormals, r as markInitialized, t as computeBounds } from "./api-BUy7FRx7.js";
3
- import { t as Scene } from "./scene-BG3EEyfw.js";
3
+ import { t as Scene } from "./scene-BFd6nYRe.js";
4
4
  import wasmInit from "./wasm/fast_mesh_wasm.js";
5
5
  //#region src/web.ts
6
6
  let ready;
@@ -133,8 +133,8 @@ export interface EngineOptions {
133
133
  * field's client id), every step of a field add's build
134
134
  * (`"fieldBuildStep"`, with the id) and every such build abandoned for a
135
135
  * superseded display (`"fieldBuildAbandon"`), every background add started
136
- * (`"addMeshBegin"`), stepped (`"addMeshStep"`) and landed (`"addMesh"`),
137
- * with the mesh's id, every operation of a repair
136
+ * (`"addMeshBegin"`), stepped (`"addMeshStep"`), copied out
137
+ * (`"addMeshPack"`) and landed (`"addMesh"`), with the mesh's id, every operation of a repair
138
138
  * list run in steps (`"repairOp"`), every field step (`"fieldStep"`) and
139
139
  * every abandoned field step unit (`"fieldStepAbandon"`).
140
140
  */
@@ -181,10 +181,11 @@ export interface EngineOptions {
181
181
  */
182
182
  backgroundAddTriangles?: number;
183
183
  /**
184
- * The work of one step of a background add's BVH build
185
- * ({@link ADD_STEP_WORK} by default): a triangle's box, or a triangle
186
- * visited per tree level. Clamped to 1 to 2^32 − 1; NaN or less than 1
187
- * keeps the default.
184
+ * The work of one step of a background add ({@link ADD_STEP_WORK} by
185
+ * default): a triangle's box, or a triangle visited per tree level, of
186
+ * its BVH build; 4 triangles or 16 vertices of its normals; 64 bytes of
187
+ * the copy of its render buffers. Clamped to 1 to 2^32 − 1; NaN or less
188
+ * than 1 keeps the default.
188
189
  */
189
190
  addStepWork?: number;
190
191
  /**
@@ -195,6 +196,12 @@ export interface EngineOptions {
195
196
  * with yields, which costs the same, so newer batches still arrive.
196
197
  */
197
198
  threads?: number;
199
+ /**
200
+ * Test-only: never drop a landed add's copied-out buffers early (see
201
+ * {@link Engine.#unpack}), so that only the check at packing time guards
202
+ * them.
203
+ */
204
+ keepCopies?: boolean;
198
205
  }
199
206
  /** The default {@link EngineOptions.yieldMs}. */
200
207
  export declare const YIELD_MS = 4;
@@ -1 +1 @@
1
- export { t as ADD_STEP_WORK, n as BACKGROUND_ADD_TRIANGLES, r as STALE_GUARD_MS, i as STEP_POINTS, a as YIELD_MS, c as loadEngine, u as probeSharedGrowth, o as runEngine, s as serveEngine, l as startEngine } from "./engine-B-ZEzR1c.js";
1
+ export { t as ADD_STEP_WORK, n as BACKGROUND_ADD_TRIANGLES, r as STALE_GUARD_MS, i as STEP_POINTS, a as YIELD_MS, c as loadEngine, u as probeSharedGrowth, o as runEngine, s as serveEngine, l as startEngine } from "./engine-BpnthBYQ.js";
@@ -1,4 +1,4 @@
1
- import { l as startEngine, s as serveEngine } from "./engine-B-ZEzR1c.js";
1
+ import { l as startEngine, s as serveEngine } from "./engine-BpnthBYQ.js";
2
2
  //#region src/worker/entry.ts
3
3
  const scope = globalThis;
4
4
  serveEngine({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-mesh-wasm",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Fast triangle mesh utilities for JavaScript, written in Rust and compiled to WebAssembly",
5
5
  "keywords": [
6
6
  "mesh",