fast-mesh-wasm 0.2.0 → 0.3.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.
Files changed (50) hide show
  1. package/README.md +488 -1
  2. package/dist/{api-D0o2HjaU.js → api-BUy7FRx7.js} +11 -11
  3. package/dist/client-Bp9FRd70.js +2278 -0
  4. package/dist/client-m2zSdFlu.d.ts +305 -0
  5. package/dist/colormap-D_xH5hI5.js +303 -0
  6. package/dist/core-Cke_kZ2e.js +113 -0
  7. package/dist/core-D2X9GPfK.d.ts +15 -0
  8. package/dist/engine-B-ZEzR1c.js +1818 -0
  9. package/dist/mesh-B16gV3oX.d.ts +130 -0
  10. package/dist/mesh-B8pae8WA.js +428 -0
  11. package/dist/node.d.ts +3 -2
  12. package/dist/node.js +5 -4
  13. package/dist/protocol-CAUYm1gd.js +68 -0
  14. package/dist/protocol-W9R-iGl5.d.ts +293 -0
  15. package/dist/r3f.d.ts +59 -0
  16. package/dist/r3f.js +165 -0
  17. package/dist/react-CTADh3NN.js +124 -0
  18. package/dist/react.d.ts +65 -0
  19. package/dist/react.js +1 -0
  20. package/dist/scene-BG3EEyfw.js +1243 -0
  21. package/dist/scene-like-B_3qm0u6.d.ts +925 -0
  22. package/dist/snapshot-Cf6NgDl7.js +104 -0
  23. package/dist/store-D3ErV6o-.js +1379 -0
  24. package/dist/store-DNnHN33A.d.ts +285 -0
  25. package/dist/store.d.ts +61 -0
  26. package/dist/store.js +105 -0
  27. package/dist/three.d.ts +118 -0
  28. package/dist/three.js +390 -0
  29. package/dist/views-BaZqBxlX.js +189 -0
  30. package/dist/wasm/fast_mesh_wasm.d.ts +458 -0
  31. package/dist/wasm/fast_mesh_wasm.js +1150 -0
  32. package/dist/wasm/fast_mesh_wasm_bg.wasm +0 -0
  33. package/dist/wasm/fast_mesh_wasm_bg.wasm.d.ts +74 -0
  34. package/dist/wasm-threads/fast_mesh_wasm.d.ts +540 -0
  35. package/dist/wasm-threads/fast_mesh_wasm.js +1513 -0
  36. package/dist/wasm-threads/fast_mesh_wasm_bg.wasm +0 -0
  37. package/dist/wasm-threads/fast_mesh_wasm_bg.wasm.d.ts +93 -0
  38. package/dist/wasm-threads/package.json +7 -0
  39. package/dist/wasm-threads/snippets/fast-mesh-wasm-ae14781a92c2789e/inline0.js +16 -0
  40. package/dist/wasm-threads/snippets/wasm-bindgen-rayon-38edf6e439f6d70d/src/workerHelpers.js +107 -0
  41. package/dist/web.d.ts +3 -2
  42. package/dist/web.js +4 -2
  43. package/dist/worker-engine.d.ts +248 -0
  44. package/dist/worker-engine.js +1 -0
  45. package/dist/worker-entry.d.ts +1 -0
  46. package/dist/worker-entry.js +11 -0
  47. package/dist/worker.d.ts +2 -0
  48. package/dist/worker.js +1 -0
  49. package/package.json +53 -4
  50. package/dist/api-HcskOwd7.d.ts +0 -43
@@ -0,0 +1,305 @@
1
+ import { $ as PlacementInput, A as RepairOp, B as SectionResult, D as RayHit, G as SectionHandle, L as SectionDesc, M as RepairReportOf, O as RaycastOptions, Q as Placement, R as SectionGap, S as MeshChange, U as FieldHandle, W as MeshHandle, Y as Indices, _ as FieldStatus, b as FindContactsOptions, c as ColorRule, f as DistanceFieldDesc, g as EditResult, i as AnalyzeOptions, it as Vec3, m as EditOp, n as AddMeshOptions, o as ClosestHit, p as DragSession, q as MeshViews, r as Analysis, s as ClosestPointOptions, t as SceneLike, tt as Positions64, u as Contact, w as MeshDistance, z as SectionPlane } from "./scene-like-B_3qm0u6.js";
2
+ import { t as FastMeshError } from "./core-D2X9GPfK.js";
3
+ import "./protocol-W9R-iGl5.js";
4
+ //#region src/worker/client.d.ts
5
+ /** What {@link WorkerScene} needs of a Worker; a browser `Worker` fits. */
6
+ interface WorkerLike {
7
+ postMessage(msg: unknown, transfer?: Transferable[]): void;
8
+ /** `"message"` events carry the message in `data`. */
9
+ addEventListener(type: "message" | "error" | "messageerror", cb: (e: any) => void): void;
10
+ terminate(): void;
11
+ }
12
+ /** Options of {@link createWorkerScene}. */
13
+ interface WorkerSceneOptions {
14
+ /**
15
+ * The Worker script, loaded as a module Worker. Defaults to the package's
16
+ * `worker-entry.js` next to this module, which bundlers pick up from the
17
+ * `new Worker(new URL(...), { type: "module" })` pattern.
18
+ */
19
+ workerUrl?: string | URL;
20
+ /** Creates the Worker instead (overrides `workerUrl`). */
21
+ createWorker?: () => WorkerLike;
22
+ /**
23
+ * Development checks, off by default: render views are then `Proxy`
24
+ * objects that warn once when read after a mirror they view was replaced
25
+ * (a mesh's slot count changed, a buffer turned on or off, the mesh was
26
+ * removed). See {@link MeshViews}. Also logs, once per scene, why the
27
+ * engine runs single-threaded when more threads were asked for (as in
28
+ * development builds, where `import.meta.env.DEV` is set).
29
+ */
30
+ debug?: boolean;
31
+ /**
32
+ * The threads the engine computes with: its WebAssembly runs on a pool of
33
+ * this many Workers sharing its memory. Defaults to the logical cores
34
+ * (`navigator.hardwareConcurrency`), at most 8; `1` runs the
35
+ * single-threaded engine. Threads need a cross-origin isolated page
36
+ * (COOP/COEP headers); elsewhere, or when the threaded engine fails to
37
+ * start, the scene runs single-threaded. {@link WorkerScene.threads} tells
38
+ * which. An integer, clamped to 1 to {@link MAX_THREADS}.
39
+ */
40
+ threads?: number;
41
+ /**
42
+ * Meshes of at least this many triangles are added in the background:
43
+ * the Worker builds them in steps while the requests sent after them
44
+ * that do not need them run (toggles, edits, drags, queries of other
45
+ * meshes), so those do not wait for a large add. A request that names
46
+ * the new mesh (or a field measured to it), adds or removes a mesh, or
47
+ * queries every mesh waits for it, so the engine's state is the same as
48
+ * in arrival order; but a reply to an unrelated request sent after the
49
+ * add can arrive before the mesh, whose status reads `"computing"` (and
50
+ * whose views are empty) until it lands. Defaults to 65,536; `Infinity`
51
+ * turns background adds off, so every add comes back with its own
52
+ * batch's result. A number, 0 or more.
53
+ */
54
+ backgroundAddTriangles?: number;
55
+ }
56
+ /** The state of a request key, from {@link WorkerScene.status}. */
57
+ interface KeyStatus {
58
+ /**
59
+ * `"computing"` while a request with this key is queued or running,
60
+ * `"ready"` or `"error"` once the latest one finished, `"idle"` for a key
61
+ * never used, or one of a removed mesh, field or section once its removal
62
+ * has settled (its state is then forgotten).
63
+ */
64
+ status: "idle" | "computing" | "ready" | "error";
65
+ /** The revision of the request the status is about (0 when idle). */
66
+ revision: number;
67
+ error?: FastMeshError;
68
+ }
69
+ /**
70
+ * Starts a Worker running the scene engine and resolves once it is ready.
71
+ * Rejects with `INSTANCE_POISONED` when the Worker could not load fast-mesh.
72
+ */
73
+ declare function createWorkerScene(options?: WorkerSceneOptions): Promise<WorkerScene>;
74
+ /**
75
+ * A scene whose engine runs in a Worker, from {@link createWorkerScene}. It
76
+ * mirrors `Scene` (always `"eager"`) with these differences:
77
+ *
78
+ * - Handles are returned synchronously; the Worker catches up in order.
79
+ * - State changes return `void` and are tracked by key through
80
+ * {@link WorkerScene.status}; calls that return data return promises.
81
+ * - Requests are sent once per frame by {@link WorkerScene.commitFrame},
82
+ * in call order, and "last wins" changes (colouring, section
83
+ * plane/meshes/fill, drag moves) superseded within the batch are dropped.
84
+ * Calls returning a promise wait for the next `commitFrame()` too, or are
85
+ * sent on their own after 100 ms when no frame comes;
86
+ * before the first `commitFrame()` (no frame loop) they are sent on the
87
+ * next microtask.
88
+ * - `views`, `placement`, `matrix4`, `section` and `sectionPlane` read
89
+ * main-thread mirrors, updated as results arrive and reported by the next
90
+ * `commitFrame()`. Views follow the same fresh-view rule as `Scene`: get
91
+ * them right before use. A mesh has 0 slots until its add is processed.
92
+ * - Drags are optimistic: `move` shows the pose at once, `end` and `cancel`
93
+ * are synchronous here, and the pose is held until the Worker has the drag.
94
+ * - {@link WorkerScene.restart} replaces the Worker and replays the scene
95
+ * into it (the way back from `INSTANCE_POISONED`); handles keep working.
96
+ * Unlike other promise calls, its replay is sent at once, not with the
97
+ * next `commitFrame()`.
98
+ */
99
+ declare class WorkerScene implements SceneLike {
100
+ #private;
101
+ /** Use {@link createWorkerScene}. */
102
+ private constructor();
103
+ /**
104
+ * Adds a mesh from world positions and returns its handle at once. The
105
+ * origin defaults to the bounding-box centre, computed here. The mesh has
106
+ * no slots until the Worker has added it; a failed add shows as an error
107
+ * on `status("mesh:<id>")`, and later calls on it fail with `UNKNOWN_MESH`.
108
+ *
109
+ * A mesh of {@link WorkerSceneOptions.backgroundAddTriangles} triangles or
110
+ * more (65,536 by default) is added in the background: requests sent after
111
+ * it that do not need it run meanwhile, so a reply to one of them can
112
+ * arrive before the mesh. Until it lands, `status("mesh:<id>")` reads
113
+ * `"computing"` and its views are empty; its buffers arrive with that
114
+ * status turning `"ready"`.
115
+ */
116
+ addMesh(positions: Positions64, indices: Indices, options?: Pick<AddMeshOptions, "origin" | "normals">): MeshHandle;
117
+ /**
118
+ * Removes a mesh, ending an active drag of it first. Later calls on it give
119
+ * `UNKNOWN_MESH`; its mirrors stay readable until the removal is delivered
120
+ * by `commitFrame()`.
121
+ */
122
+ removeMesh(mesh: MeshHandle): void;
123
+ /** Sets a mesh's placement (one undo step); `placement()` follows when the result arrives. */
124
+ setPlacement(mesh: MeshHandle, placement: PlacementInput): void;
125
+ /** The mesh's placement as mirrored (a drag's pose while it is held). */
126
+ placement(mesh: MeshHandle): Placement;
127
+ /** The column-major world matrix of the mesh's local render coordinates, as mirrored. */
128
+ matrix4(mesh: MeshHandle): Float64Array;
129
+ /**
130
+ * The mirrors of the mesh's render buffers, at full slot length. Each
131
+ * getter returns the current mirror; a mirror is replaced when the slot
132
+ * count changes or a buffer turns on or off, so get views right before use.
133
+ */
134
+ views(mesh: MeshHandle): MeshViews;
135
+ /**
136
+ * The rule the mirrored colours and values are made by (a frozen copy of
137
+ * the one given to `setColoring`, the same object until another rule is
138
+ * applied), or `null` without colouring. It follows the
139
+ * results: a new rule shows with the batch that applies it, a rejected one
140
+ * never, and a removed field's colouring goes when the removal is applied.
141
+ */
142
+ coloring(mesh: MeshHandle): ColorRule | null;
143
+ /**
144
+ * Starts an optimistic drag. `move` shows the pose here at once and sends
145
+ * at most one move per batch; `end` and `cancel` are synchronous here.
146
+ * `cancel` puts the start pose back and records nothing.
147
+ */
148
+ drag(mesh: MeshHandle): DragSession;
149
+ /** Edits a mesh's geometry (one undo step); resolves with the ids added. */
150
+ edit(mesh: MeshHandle, ops: readonly EditOp[]): Promise<EditResult>;
151
+ /** Compacts a mesh's slots (its slot counts change). */
152
+ compact(mesh: MeshHandle): void;
153
+ /** A report of the mesh's defects; see `Scene.analyze`. */
154
+ analyze(mesh: MeshHandle, options?: AnalyzeOptions): Promise<Analysis>;
155
+ /** Runs one repair operation, or a list as one transaction; see `Scene.repair`. */
156
+ repair<const O extends RepairOp>(mesh: MeshHandle, op: O): Promise<RepairReportOf<O>>;
157
+ repair<const O extends readonly RepairOp[]>(mesh: MeshHandle, ops: O): Promise<{ -readonly [K in keyof O]: RepairReportOf<O[K]>; }>;
158
+ /** Undoes the latest step; an active drag is ended first. */
159
+ undo(): void;
160
+ /** Redoes the latest undone step; an active drag is ended first. */
161
+ redo(): void;
162
+ /**
163
+ * The threads the engine computes with: more than 1 when the threaded
164
+ * engine runs (see {@link WorkerSceneOptions.threads}), 1 when
165
+ * single-threaded. As of the current Worker's start (1 until it is ready).
166
+ */
167
+ get threads(): number;
168
+ /** Whether there is a step to undo, as of the latest result. */
169
+ get canUndo(): boolean;
170
+ /** Whether there is a step to redo, as of the latest result. */
171
+ get canRedo(): boolean;
172
+ /** Adds a signed distance field; see `Scene.addDistanceField`. */
173
+ addDistanceField(desc: DistanceFieldDesc): FieldHandle;
174
+ /** Removes a field, and a colouring by it. */
175
+ removeField(field: FieldHandle): void;
176
+ /**
177
+ * Colours a mesh from one of its fields, or turns colouring off with
178
+ * `null`. Last wins: of several calls for a mesh in one frame, only the
179
+ * last is sent. Tracked by `status("display:<id>")`.
180
+ */
181
+ setColoring(mesh: MeshHandle, rule: ColorRule | null): void;
182
+ /** A copy of the field's values as of its last update, one per vertex slot. */
183
+ fetchFieldValues(field: FieldHandle): Promise<Float32Array>;
184
+ /**
185
+ * The field's nearest-target indices as of its last update (the index in
186
+ * its `to`, `0xffffffff` for none); see `Scene.fieldNearest`. A target
187
+ * removed since shifts the later indices down.
188
+ */
189
+ fetchFieldNearest(field: FieldHandle): Promise<Uint32Array>;
190
+ /** What the sign data found out about each of the field's targets; see `Scene.fieldTargetStatus`. */
191
+ fieldTargetStatus(field: FieldHandle): Promise<FieldStatus["sign"][]>;
192
+ /**
193
+ * Whether the field is current, and what its sign data found out about the
194
+ * targets together; see `Scene.fieldStatus`.
195
+ */
196
+ fieldStatus(field: FieldHandle): Promise<FieldStatus>;
197
+ /** Adds a plane section; its results arrive with the next results. */
198
+ addSection(desc: SectionDesc): SectionHandle;
199
+ /** Moves a section's plane; `sectionPlane()` returns it at once. Last wins per frame. */
200
+ setSectionPlane(section: SectionHandle, plane: SectionPlane): void;
201
+ /** Changes the meshes a section cuts. Last wins per frame. */
202
+ setSectionMeshes(section: SectionHandle, meshes: readonly MeshHandle[]): void;
203
+ /** Turns a section's fill on or off. Last wins per frame. */
204
+ setSectionFill(section: SectionHandle, fill: boolean): void;
205
+ /** Removes a section. Its handle then gives `UNKNOWN_SECTION`. */
206
+ removeSection(section: SectionHandle): void;
207
+ /**
208
+ * The section's latest result for a mesh as mirrored (empty and `"stale"`
209
+ * until the first arrives). Do not modify it.
210
+ *
211
+ * @throws `INVALID_ARGUMENT` when the section does not cut `mesh`.
212
+ */
213
+ section(section: SectionHandle, mesh: MeshHandle): SectionResult;
214
+ /** The section's plane as last set here, the normal normalised. */
215
+ sectionPlane(section: SectionHandle): SectionPlane;
216
+ /** The closest pair between two meshes' contours in a section; see `Scene.sectionGap`. */
217
+ sectionGap(section: SectionHandle, a: MeshHandle, b: MeshHandle): Promise<SectionGap | null>;
218
+ /** The nearest hit of a world ray over every mesh, or `null`; see `Scene.raycast`. */
219
+ raycast(origin: Readonly<Vec3>, direction: Readonly<Vec3>, options?: RaycastOptions): Promise<RayHit | null>;
220
+ /** The surface point nearest to a world point, or `null`; see `Scene.closestPoint`. */
221
+ closestPoint(point: Readonly<Vec3>, options?: ClosestPointOptions): Promise<ClosestHit | null>;
222
+ /** The distance between two meshes' surfaces, or `null`; see `Scene.meshDistance`. */
223
+ meshDistance(a: MeshHandle, b: MeshHandle, options?: {
224
+ maxDistance?: number;
225
+ }): Promise<MeshDistance | null>;
226
+ /** Whether two meshes intersect or touch as placed. */
227
+ intersects(a: MeshHandle, b: MeshHandle): Promise<boolean>;
228
+ /** Every mesh pair (or every pair of `pairs`) within `margin`; see `Scene.findContacts`. */
229
+ findContacts(options?: FindContactsOptions): Promise<Contact[]>;
230
+ /** Registers a listener for {@link WorkerScene.commitFrame}; returns the function that removes it. */
231
+ onChanges(listener: (changes: MeshChange[]) => void): () => void;
232
+ /**
233
+ * Call once per animation frame. Sends this frame's requests to the Worker
234
+ * as one batch, then delivers to every listener, and returns, the change
235
+ * records of the results received since the last call (already applied to
236
+ * the mirrors) plus the drags' pose changes made here. Every listener runs
237
+ * even if one throws; the first error is rethrown afterwards.
238
+ */
239
+ commitFrame(): MeshChange[];
240
+ /**
241
+ * The state of a request key: `mesh:<id>`, `placement:<id>`, `edit:<id>`,
242
+ * `display:<id>`, `field:<id>`, `section:<id>` (add and remove),
243
+ * `section:<id>:plane` / `:meshes` / `:fill`, `history`. A result sets
244
+ * `"ready"` or `"error"` only when no newer request for the key is pending.
245
+ * A status covers only requests made under its key: a drag or edit does
246
+ * not mark the `display:` or `section:` keys of the colours and sections
247
+ * that catch up after it. Returns a copy.
248
+ */
249
+ status(key: string): KeyStatus;
250
+ /**
251
+ * Registers a listener called (on a microtask) when any status or the
252
+ * undo/redo state changes; returns the function that removes it.
253
+ */
254
+ onStatus(listener: () => void): () => void;
255
+ /** The client id behind a handle, as used in status keys. */
256
+ idOf(handle: MeshHandle | FieldHandle | SectionHandle): number;
257
+ /**
258
+ * Stops the Worker, starts a new one and replays the scene into it; this
259
+ * is how a scene recovers after `INSTANCE_POISONED`. Resolves once the
260
+ * replay's results are applied (reported by the next `commitFrame()`).
261
+ *
262
+ * The replay is built here, from what the main thread has:
263
+ * - every mesh the engine had, re-added from its mirrors: live triangles
264
+ * only, compacted (slot ids change; handles do not), with its origin;
265
+ * - meshes whose add was never sent (still queued, or failed here after a
266
+ * poisoning), from their own data;
267
+ * - placements: the engine's, or, when the scene was not poisoned, the
268
+ * latest requested one (a `setPlacement` or drag not acknowledged yet);
269
+ * - fields, colourings and sections as last requested (removed ones are
270
+ * not re-created).
271
+ *
272
+ * History is cleared (`canUndo`/`canRedo` are false). The replay is sent
273
+ * at once, not with the next frame. Every replayed key is `"computing"`
274
+ * until the replay's results arrive, and so is the key of every pending
275
+ * declarative request (placement, drag, colouring, field, section), also
276
+ * one that failed after a poisoning (its call was rejected then): it
277
+ * settles with the replay (`"ready"`, or `"error"` when the replay failed
278
+ * for it), which applies the newest value; after a poisoning, placements
279
+ * replay the engine's pose. A colouring whose field cannot be replayed
280
+ * (its mesh was lost) is dropped, with `UNKNOWN_FIELD` on its display key.
281
+ * Removals count as done: field and section removal keys read `"idle"`
282
+ * at once (their state is forgotten), mesh removal keys after the next
283
+ * `commitFrame()` delivers the removal. When the replay fails as a whole
284
+ * (no Worker, it could not be posted, a trap), the scene is poisoned and
285
+ * the next `restart()` replays the same intent and settles those keys.
286
+ *
287
+ * What cannot be replayed surfaces as errors (`INSTANCE_POISONED`): edits,
288
+ * repairs, compaction, undo/redo and queries sent and not answered, or not
289
+ * sent yet (their promises reject), and meshes whose add was sent but
290
+ * never acknowledged (their buffers went to the old Worker: they stay
291
+ * failed, and calls on them give `UNKNOWN_MESH`). An active drag ends.
292
+ * Mirrors, section results included, stay readable throughout. Calling it
293
+ * again before it settles returns the same promise. When no Worker can be
294
+ * created, the promise rejects and the scene stays poisoned (a later
295
+ * `restart()` tries again).
296
+ */
297
+ restart(): Promise<void>;
298
+ /**
299
+ * Stops the Worker. Pending promises reject with `INVALID_ARGUMENT`; later
300
+ * calls throw it. Calling it again does nothing.
301
+ */
302
+ terminate(): void;
303
+ }
304
+ //#endregion
305
+ export { createWorkerScene as a, WorkerSceneOptions as i, WorkerLike as n, WorkerScene as r, KeyStatus as t };
@@ -0,0 +1,303 @@
1
+ //#region src/colormap.ts
2
+ /** The rule's `base` colour when it gives none. */
3
+ const DEFAULT_BASE = [
4
+ 200,
5
+ 200,
6
+ 200,
7
+ 255
8
+ ];
9
+ /** The rule's `nan` colour when it gives none. */
10
+ const DEFAULT_NAN = [
11
+ 128,
12
+ 128,
13
+ 128,
14
+ 255
15
+ ];
16
+ /**
17
+ * sRGB byte to linear, as a normalized `Uint16` (0 to 65535). The engine's
18
+ * colour maps are sRGB-encoded bytes; three treats vertex colours as linear
19
+ * working-space values and encodes its output to sRGB, so bytes passed
20
+ * through would be encoded twice and look washed out. Sixteen bits keep the
21
+ * dark tones that eight linear bits would band. The fragment shading
22
+ * linearises through the same table.
23
+ */
24
+ const SRGB_TO_LINEAR16 = (() => {
25
+ const table = /* @__PURE__ */ new Uint16Array(256);
26
+ for (let i = 0; i < 256; i++) {
27
+ const c = i / 255;
28
+ const linear = c <= .04045 ? c / 12.92 : ((c + .055) / 1.055) ** 2.4;
29
+ table[i] = Math.round(linear * 65535);
30
+ }
31
+ return table;
32
+ })();
33
+ /**
34
+ * The engine's built-in tables (RGB; alpha is 255), base64: Turbo, Viridis
35
+ * and Coolwarm as `color_of` samples them (the tests check them against the
36
+ * engine). Grayscale is `(i, i, i)`.
37
+ */
38
+ const TABLES = {
39
+ turbo: "IxcbJxooKxw0Lx4/MyBKNiNVOSVfOyhpPipyQCx7Qi+ERDGMRTSURzebSDmiSTypST6vSkG1SkS7S0bBS0nGS0zLS0/PSlHTSlTXSlfbSVnfSVziSF/lR2LoRmXqRWftRGrvQ23wQnDyQXL0QHX1P3j2Pnv3PX34O4D4OoP5OYb5OIj5N4v5NY75NJD4M5P4Mpb3MZj2MJv2L571LqD0LaPyLKXxK6jwKqruKq3tKa/rKLLqKLToJ7bmJ7nkJrviJr3gJcDeJcLcJcTaJcbXJcjVJcrTJc3RJc/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//ocD/osH/o8L+pcP+psT+qMX+qcb+qsf9rMj9rcn9rsn8sMr8scv8ssz7s837tc76ts76t8/5udD5utH4u9H4vNL3vtP2v9P2wNT1wdT0w9X0xNbzxdbyxtfxyNfxydjwytjvy9juzNntzdnsztrr0Nrq0drp0tvo09vn1Nvm1dvl1tzk19zj2Nzi2dzh2tzg293e3N3d3dzc3tzb39zZ4dvY4trW49rV5NnT5dnS5djR5tjP59fO6NbM6dbL6tXJ69TI69PG7NPF7dLD7tHC7tDA78+/78698M688c268cy48su38sq188m088iy9Mex9Mav9MWu9cSs9cOq9cGp9sCn9r+m9r6k9r2i97yh97qf97me97ic97eb97WZ97SX97OW97KU97CT96+R962Q96yO96uM96mL96iJ96aI9qWG9qOF9qKD9qCB9Z+A9Z1+9Zx99Jp79Jl69Jd485V385R18pJ08pBy8Y9x8Y1v8Itu8Ips74hr74Zp7oVo7YNm7YFl7H9j631i6nxg6npf6Xhd6HZc53Ra5nJZ5XBY5W9W5G1V42tT4mlS4WdR4GVP32NO3mFN3V9L3F1K21tJ2llH2FZG11RF1lJD1VBC1E5B00xA0Uk+0Ec9z0U8zkM7zEA5yz44yjs3yDk2xzY0xjQzxDEywy4xwSswwCgvvyUuvSIsvB4ruhoquRYptxEotgsntAQm"
42
+ };
43
+ const decoded = /* @__PURE__ */ new Map();
44
+ /** A built-in map's 256 RGB entries. */
45
+ function table(map) {
46
+ let t = decoded.get(map);
47
+ if (t === void 0) {
48
+ if (map === "grayscale") {
49
+ t = /* @__PURE__ */ new Uint8Array(768);
50
+ for (let i = 0; i < 256; i++) t.fill(i, 3 * i, 3 * i + 3);
51
+ } else {
52
+ const text = atob(TABLES[map]);
53
+ t = new Uint8Array(text.length);
54
+ for (let i = 0; i < text.length; i++) t[i] = text.charCodeAt(i);
55
+ }
56
+ decoded.set(map, t);
57
+ }
58
+ return t;
59
+ }
60
+ const f = Math.fround;
61
+ /** The shading data of a valid colour rule. */
62
+ function shadingOf(rule) {
63
+ const map = rule.map;
64
+ const builtin = typeof map === "string";
65
+ const count = builtin ? 256 : map.stops.length;
66
+ const width = Math.max(256, count);
67
+ const lut = new Float32Array(width * 4 * 4);
68
+ const row = (r, i) => (r * width + i) * 4;
69
+ if (builtin) {
70
+ const t = table(map);
71
+ for (let i = 0; i < 256; i++) lut.set([
72
+ t[3 * i],
73
+ t[3 * i + 1],
74
+ t[3 * i + 2],
75
+ 255
76
+ ], row(0, i));
77
+ const sum = [
78
+ 0,
79
+ 0,
80
+ 0,
81
+ 0
82
+ ];
83
+ for (let i = 0; i < 256; i++) {
84
+ if (i > 0) for (let k = 0; k < 4; k++) sum[k] += (lut[row(0, i - 1) + k] + lut[row(0, i) + k]) / 510;
85
+ lut.set(sum, row(2, i));
86
+ }
87
+ } else map.stops.forEach(([t, color], i) => {
88
+ lut.set(color, row(0, i));
89
+ lut[row(1, i)] = t;
90
+ });
91
+ for (let i = 0; i < 256; i++) lut[row(3, i)] = SRGB_TO_LINEAR16[i] / 65535;
92
+ return {
93
+ lut,
94
+ width,
95
+ count,
96
+ builtin,
97
+ range: [f(rule.range[0]), f(rule.range[1])],
98
+ center: rule.center === void 0 ? NaN : f(rule.center),
99
+ belowBase: (rule.below ?? "clamp") === "base",
100
+ aboveBase: (rule.above ?? "base") === "base",
101
+ base: rule.base ?? DEFAULT_BASE,
102
+ nan: rule.nan ?? DEFAULT_NAN
103
+ };
104
+ }
105
+ /**
106
+ * Writes the attribute of `values[from, to)` (see the notes at the top) into
107
+ * `out` from element `4 · from`: `(v · m, m, nan, far)` per vertex.
108
+ */
109
+ function encodeValues(values, out, from, to) {
110
+ for (let i = from; i < to; i++) {
111
+ const v = values[i];
112
+ const finite = Number.isFinite(v);
113
+ out[4 * i] = finite ? v : 0;
114
+ out[4 * i + 1] = finite ? 1 : 0;
115
+ out[4 * i + 2] = Number.isNaN(v) ? 1 : 0;
116
+ out[4 * i + 3] = v === Infinity ? 1 : v === -Infinity ? -1 : 0;
117
+ }
118
+ }
119
+ /** Declarations for the vertex shader: the attribute and its varying. */
120
+ const SHADING_VERTEX = `
121
+ attribute vec4 fmValue;
122
+ varying vec4 vFmValue;
123
+ `;
124
+ /**
125
+ * Declarations and functions for the fragment shader. `fmShade(vFmValue)`
126
+ * is the linear RGBA of the fragment; each function mirrors the reference
127
+ * function of the same name above (`fmColorAt` = `colorAt`, ...).
128
+ */
129
+ const SHADING_FRAGMENT = `
130
+ varying vec4 vFmValue;
131
+ uniform highp sampler2D fmLut;
132
+ uniform int fmCount;
133
+ uniform bool fmBuiltin;
134
+ uniform vec2 fmRange;
135
+ uniform float fmCenter;
136
+ uniform bool fmHasCenter;
137
+ uniform bool fmBelowBase;
138
+ uniform bool fmAboveBase;
139
+ uniform vec4 fmBase;
140
+ uniform vec4 fmNan;
141
+ uniform float fmSoftness;
142
+ uniform bool fmOn;
143
+
144
+ vec4 fmTexel(int r, int i) {
145
+ return texelFetch(fmLut, ivec2(i, r), 0);
146
+ }
147
+
148
+ vec4 fmLerp(vec4 a, vec4 b, float x) {
149
+ return a + (b - a) * x;
150
+ }
151
+
152
+ vec4 fmColorAt(float t) {
153
+ if (fmBuiltin) {
154
+ float x = t * 255.0;
155
+ int i = min(int(floor(x)), 254);
156
+ return fmLerp(fmTexel(0, i), fmTexel(0, i + 1), x - float(i));
157
+ }
158
+ int last = fmCount - 1;
159
+ if (t <= fmTexel(1, 0).r) {
160
+ return fmTexel(0, 0);
161
+ }
162
+ if (t >= fmTexel(1, last).r) {
163
+ return fmTexel(0, last);
164
+ }
165
+ for (int k = 1; k < fmCount; k++) {
166
+ float p1 = fmTexel(1, k).r;
167
+ if (t <= p1) {
168
+ float p0 = fmTexel(1, k - 1).r;
169
+ float span = p1 - p0;
170
+ return fmLerp(fmTexel(0, k - 1), fmTexel(0, k), span > 0.0 ? (t - p0) / span : 1.0);
171
+ }
172
+ }
173
+ return fmTexel(0, last);
174
+ }
175
+
176
+ vec4 fmRound(vec4 c) {
177
+ vec4 r = floor(c);
178
+ return clamp(r + step(0.5, c - r), 0.0, 255.0);
179
+ }
180
+
181
+ float fmT(float v) {
182
+ float lo = fmRange.x;
183
+ float hi = fmRange.y;
184
+ float c = fmCenter;
185
+ if (!fmHasCenter) {
186
+ return (v - lo) / (hi - lo);
187
+ }
188
+ if (v == c) {
189
+ return 0.5;
190
+ }
191
+ return v < c ? 0.5 * (v - lo) / (c - lo) : 0.5 + 0.5 * (v - c) / (hi - c);
192
+ }
193
+
194
+ vec4 fmExact(float v) {
195
+ vec4 c;
196
+ if (v < fmRange.x) {
197
+ c = fmBelowBase ? fmBase : fmColorAt(0.0);
198
+ } else if (v > fmRange.y) {
199
+ c = fmAboveBase ? fmBase : fmColorAt(1.0);
200
+ } else {
201
+ c = fmColorAt(clamp(fmT(v), 0.0, 1.0));
202
+ }
203
+ return fmRound(c);
204
+ }
205
+
206
+ vec4 fmPrefix(float t) {
207
+ int i = min(int(floor(t * 255.0)), 254);
208
+ float d = t - float(i) / 255.0;
209
+ vec4 a = fmTexel(0, i);
210
+ return fmTexel(2, i) + d * (a + 0.5 * d * 255.0 * (fmTexel(0, i + 1) - a));
211
+ }
212
+
213
+ vec4 fmIntegral(float t1, float t2) {
214
+ if (fmBuiltin && (t2 - t1) * 255.0 > 16.0) {
215
+ return fmPrefix(t2) - fmPrefix(t1);
216
+ }
217
+ vec4 sum = vec4(0.0);
218
+ float prev = t1;
219
+ vec4 cp = fmColorAt(t1);
220
+ if (fmBuiltin) {
221
+ int first = int(floor(t1 * 255.0)) + 1;
222
+ int last = int(ceil(t2 * 255.0)) - 1;
223
+ for (int j = 0; j <= 17; j++) {
224
+ int k = first + j;
225
+ if (k > last) {
226
+ break;
227
+ }
228
+ float p = float(k) / 255.0;
229
+ vec4 c = fmTexel(0, k);
230
+ sum += (p - prev) * (cp + c) * 0.5;
231
+ prev = p;
232
+ cp = c;
233
+ }
234
+ } else {
235
+ for (int k = 0; k < fmCount; k++) {
236
+ float p = fmTexel(1, k).r;
237
+ if (p > t1 && p < t2) {
238
+ vec4 c = fmTexel(0, k);
239
+ sum += (p - prev) * (cp + c) * 0.5;
240
+ prev = p;
241
+ cp = c;
242
+ }
243
+ }
244
+ }
245
+ return sum + (t2 - prev) * (cp + fmColorAt(t2)) * 0.5;
246
+ }
247
+
248
+ vec4 fmSoft(float v, float h) {
249
+ float lo = fmRange.x;
250
+ float hi = fmRange.y;
251
+ float c = fmCenter;
252
+ float a = v - h;
253
+ float b = v + h;
254
+ vec4 sum = max(0.0, min(b, lo) - a) * (fmBelowBase ? fmBase : fmColorAt(0.0));
255
+ sum += max(0.0, b - max(a, hi)) * (fmAboveBase ? fmBase : fmColorAt(1.0));
256
+ float ia = max(a, lo);
257
+ float ib = min(b, hi);
258
+ if (ib > ia) {
259
+ if (!fmHasCenter) {
260
+ sum += (hi - lo) * fmIntegral((ia - lo) / (hi - lo), (ib - lo) / (hi - lo));
261
+ } else {
262
+ float lowEnd = min(ib, c);
263
+ if (lowEnd > ia) {
264
+ sum += 2.0 * (c - lo) * fmIntegral(0.5 * (ia - lo) / (c - lo), 0.5 * (lowEnd - lo) / (c - lo));
265
+ }
266
+ float highStart = max(ia, c);
267
+ if (ib > highStart) {
268
+ sum += 2.0 * (hi - c) * fmIntegral(0.5 + 0.5 * (highStart - c) / (hi - c), 0.5 + 0.5 * (ib - c) / (hi - c));
269
+ }
270
+ }
271
+ }
272
+ return sum / (b - a);
273
+ }
274
+
275
+ vec4 fmLinear(vec4 c) {
276
+ vec4 o = vec4(0.0, 0.0, 0.0, c.a / 255.0);
277
+ for (int k = 0; k < 3; k++) {
278
+ float x = clamp(c[k], 0.0, 255.0);
279
+ int i = int(floor(x));
280
+ o[k] = mix(fmTexel(3, i).r, fmTexel(3, min(i + 1, 255)).r, x - float(i));
281
+ }
282
+ return o;
283
+ }
284
+
285
+ vec4 fmFar(float sign) {
286
+ bool base = sign >= 0.0 ? fmAboveBase : fmBelowBase;
287
+ return base ? fmBase : fmRound(fmColorAt(sign >= 0.0 ? 1.0 : 0.0));
288
+ }
289
+
290
+ vec4 fmShade(vec4 a) {
291
+ float w = a.y;
292
+ if (w >= 0.5) {
293
+ float v = a.x / w;
294
+ return fmLinear(fmSoftness > 0.0 ? fmSoft(v, 0.5 * fmSoftness) : fmExact(v));
295
+ }
296
+ if (a.z > 1.0 - w - a.z) {
297
+ return fmLinear(fmNan);
298
+ }
299
+ return fmLinear(fmFar(a.w));
300
+ }
301
+ `;
302
+ //#endregion
303
+ export { SRGB_TO_LINEAR16 as a, SHADING_VERTEX as i, DEFAULT_NAN as n, encodeValues as o, SHADING_FRAGMENT as r, shadingOf as s, DEFAULT_BASE as t };