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
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { B as SectionResult, K as Range, c as ColorRule, f as DistanceFieldDesc, n as AddMeshOptions, z as SectionPlane } from "./scene-like-B_3qm0u6.js";
|
|
2
|
+
//#region src/worker/protocol.d.ts
|
|
3
|
+
/** A mesh, field or section id allocated on the main side. Unique per kind. */
|
|
4
|
+
type ClientId = number;
|
|
5
|
+
/** A {@link ColorRule} naming its field by client id. */
|
|
6
|
+
type ColorRuleWire = Omit<ColorRule, "field"> & {
|
|
7
|
+
field: ClientId;
|
|
8
|
+
};
|
|
9
|
+
/** A {@link DistanceFieldDesc} naming its meshes by client id, its targets as a list. */
|
|
10
|
+
type FieldDescWire = Omit<DistanceFieldDesc, "on" | "to"> & {
|
|
11
|
+
on: ClientId;
|
|
12
|
+
to: ClientId[];
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* A section description by client ids. For `op: "add"`, `plane` and `meshes`
|
|
16
|
+
* are required; for `op: "set"`, each field present is applied (plane, then
|
|
17
|
+
* meshes, then fill).
|
|
18
|
+
*/
|
|
19
|
+
interface SectionDescWire {
|
|
20
|
+
plane?: SectionPlane;
|
|
21
|
+
meshes?: ClientId[];
|
|
22
|
+
fill?: boolean;
|
|
23
|
+
}
|
|
24
|
+
/** A {@link SectionResult}; its typed arrays are transferred. */
|
|
25
|
+
type SectionResultWire = SectionResult;
|
|
26
|
+
/**
|
|
27
|
+
* A request. `rev` is the main side's revision, increasing per request.
|
|
28
|
+
* `key` names the state the request targets (see {@link keyOf} for the
|
|
29
|
+
* defaults); requests whose kind is "last wins" ({@link isLastWins}) are
|
|
30
|
+
* dropped when a later queued request with the same key supersedes them.
|
|
31
|
+
*/
|
|
32
|
+
type Request = {
|
|
33
|
+
kind: "addMesh";
|
|
34
|
+
rev: number;
|
|
35
|
+
id: ClientId;
|
|
36
|
+
/** `Float64Array` bytes when `f64`, else `Float32Array` bytes: world positions. */
|
|
37
|
+
positions: ArrayBuffer;
|
|
38
|
+
f64: boolean;
|
|
39
|
+
/** `Uint32Array` bytes. */
|
|
40
|
+
indices: ArrayBuffer;
|
|
41
|
+
/** `mode` is ignored: the Worker scene is always `"eager"`. */
|
|
42
|
+
options: AddMeshOptions;
|
|
43
|
+
/**
|
|
44
|
+
* Add in place, in the batch's own result (a restart's replay), never
|
|
45
|
+
* in the background: by default a large mesh is built in the
|
|
46
|
+
* background and lands with a later result or `update` message.
|
|
47
|
+
*/
|
|
48
|
+
inline?: boolean;
|
|
49
|
+
key?: string;
|
|
50
|
+
} | {
|
|
51
|
+
kind: "removeMesh" | "compact";
|
|
52
|
+
rev: number;
|
|
53
|
+
id: ClientId;
|
|
54
|
+
key?: string;
|
|
55
|
+
} | {
|
|
56
|
+
kind: "setPlacement";
|
|
57
|
+
rev: number;
|
|
58
|
+
id: ClientId;
|
|
59
|
+
rotation: number[];
|
|
60
|
+
translation: number[];
|
|
61
|
+
key: string;
|
|
62
|
+
} | {
|
|
63
|
+
kind: "drag";
|
|
64
|
+
rev: number;
|
|
65
|
+
id: ClientId;
|
|
66
|
+
phase: "begin" | "move" | "end" | "cancel";
|
|
67
|
+
/** For `move`, and optionally `end` (a last move before ending). */
|
|
68
|
+
rotation?: number[];
|
|
69
|
+
translation?: number[];
|
|
70
|
+
key: string;
|
|
71
|
+
} | {
|
|
72
|
+
kind: "edit" | "repair";
|
|
73
|
+
rev: number;
|
|
74
|
+
id: ClientId;
|
|
75
|
+
/** `EditOp[]` for `edit`; `RepairOp` or `RepairOp[]` for `repair`. */
|
|
76
|
+
payload: unknown;
|
|
77
|
+
/** When set, the result (an `EditResult` or the repair report(s)) is replied under it. */
|
|
78
|
+
qid?: number;
|
|
79
|
+
key?: string;
|
|
80
|
+
} | {
|
|
81
|
+
kind: "undo" | "redo";
|
|
82
|
+
rev: number;
|
|
83
|
+
key?: string;
|
|
84
|
+
} |
|
|
85
|
+
/** Drops every undo and redo step (the end of a `restart()` replay). */
|
|
86
|
+
{
|
|
87
|
+
kind: "clearHistory";
|
|
88
|
+
rev: number;
|
|
89
|
+
key?: string;
|
|
90
|
+
} | {
|
|
91
|
+
kind: "coloring";
|
|
92
|
+
rev: number;
|
|
93
|
+
id: ClientId;
|
|
94
|
+
rule: ColorRuleWire | null;
|
|
95
|
+
key: string;
|
|
96
|
+
} |
|
|
97
|
+
/**
|
|
98
|
+
* Turns on the mirror of the colours' values (`views(mesh).values()`):
|
|
99
|
+
* until it, results carry no value bytes. The next result then carries every
|
|
100
|
+
* coloured mesh's colours and values whole.
|
|
101
|
+
*/
|
|
102
|
+
{
|
|
103
|
+
kind: "values";
|
|
104
|
+
rev: number;
|
|
105
|
+
} |
|
|
106
|
+
/**
|
|
107
|
+
* A field `add` may carry the key of the display it is built for (such as
|
|
108
|
+
* `display:<meshId>`). When the request with that key it feeds (the next
|
|
109
|
+
* queued last-wins one) is superseded before the build starts, the build is
|
|
110
|
+
* abandoned: no status is reported, and the field is built when a later
|
|
111
|
+
* request first needs it (a colouring, `fetchFieldValues`, `fieldStatus`).
|
|
112
|
+
*/
|
|
113
|
+
{
|
|
114
|
+
kind: "field";
|
|
115
|
+
rev: number;
|
|
116
|
+
field: ClientId;
|
|
117
|
+
op: "add" | "remove";
|
|
118
|
+
desc?: FieldDescWire;
|
|
119
|
+
key?: string;
|
|
120
|
+
} | {
|
|
121
|
+
kind: "section";
|
|
122
|
+
rev: number;
|
|
123
|
+
section: ClientId;
|
|
124
|
+
op: "add" | "set" | "remove";
|
|
125
|
+
desc?: SectionDescWire;
|
|
126
|
+
key?: string;
|
|
127
|
+
} | {
|
|
128
|
+
kind: "query";
|
|
129
|
+
rev: number;
|
|
130
|
+
qid: number;
|
|
131
|
+
name: string;
|
|
132
|
+
args: unknown;
|
|
133
|
+
} | {
|
|
134
|
+
kind: "fetchFieldValues";
|
|
135
|
+
rev: number;
|
|
136
|
+
qid: number;
|
|
137
|
+
field: ClientId;
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* One mesh's change record (a `MeshChange` of the engine's scene) with
|
|
141
|
+
* client ids and the bytes of its dirty ranges.
|
|
142
|
+
*
|
|
143
|
+
* `bytes` holds, per buffer, every dirty range's elements concatenated in
|
|
144
|
+
* range order (positions and normals: 3 `f32` per vertex slot; colours: 4
|
|
145
|
+
* bytes per vertex slot; values: 1 `f32` per vertex slot, over the colours'
|
|
146
|
+
* ranges, once a `values` request turned them on; indices: 3 `u32` per triangle slot), or the whole buffer when the
|
|
147
|
+
* ranges are `"all"`. `null` for a buffer that is off (normals, colours and
|
|
148
|
+
* values), and for every buffer of a removed mesh.
|
|
149
|
+
*/
|
|
150
|
+
interface MeshChangeWire {
|
|
151
|
+
id: ClientId;
|
|
152
|
+
added: boolean;
|
|
153
|
+
removed: boolean;
|
|
154
|
+
placementChanged: boolean;
|
|
155
|
+
capacityChanged: boolean;
|
|
156
|
+
positions: Range[] | "all";
|
|
157
|
+
normals: Range[] | "all";
|
|
158
|
+
colors: Range[] | "all";
|
|
159
|
+
indices: Range[] | "all";
|
|
160
|
+
fields: ClientId[];
|
|
161
|
+
sections: ClientId[];
|
|
162
|
+
/** Slot counts after the change; 0 for a removed mesh. */
|
|
163
|
+
vertexSlots: number;
|
|
164
|
+
triangleSlots: number;
|
|
165
|
+
bytes: {
|
|
166
|
+
positions: ArrayBuffer | null;
|
|
167
|
+
normals: ArrayBuffer | null;
|
|
168
|
+
colors: ArrayBuffer | null;
|
|
169
|
+
values: ArrayBuffer | null;
|
|
170
|
+
indices: ArrayBuffer | null;
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
/** An error crossing the boundary: a `FastMeshError` code and message. */
|
|
174
|
+
interface ErrorWire {
|
|
175
|
+
code: string;
|
|
176
|
+
message: string;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* What the engine posts after each processed batch: one per batch received,
|
|
180
|
+
* in order (a batch whose requests were all dropped still gets one).
|
|
181
|
+
*/
|
|
182
|
+
interface Result {
|
|
183
|
+
/** The highest revision of the batch processed (or dropped as superseded or abandoned). */
|
|
184
|
+
rev: number;
|
|
185
|
+
/** Change records, ranges applied in order; never drop them. */
|
|
186
|
+
changes: MeshChangeWire[];
|
|
187
|
+
/** Placement and `matrix4` (column-major) of every mesh added or moved. */
|
|
188
|
+
placements: {
|
|
189
|
+
id: ClientId;
|
|
190
|
+
rotation: number[];
|
|
191
|
+
translation: number[];
|
|
192
|
+
matrix4: number[];
|
|
193
|
+
}[];
|
|
194
|
+
/**
|
|
195
|
+
* Every changed section entry. `result` is `null` once the entry is gone:
|
|
196
|
+
* the section was removed, no longer cuts the mesh, or the mesh was
|
|
197
|
+
* removed (one entry per section that cut it).
|
|
198
|
+
*/
|
|
199
|
+
sections: {
|
|
200
|
+
section: ClientId;
|
|
201
|
+
mesh: ClientId;
|
|
202
|
+
result: SectionResultWire | null;
|
|
203
|
+
}[];
|
|
204
|
+
history: {
|
|
205
|
+
canUndo: boolean;
|
|
206
|
+
canRedo: boolean;
|
|
207
|
+
};
|
|
208
|
+
/**
|
|
209
|
+
* The outcome of every keyed request run in this batch, in order (a
|
|
210
|
+
* request dropped as superseded, or an abandoned field build, has none).
|
|
211
|
+
*/
|
|
212
|
+
status: {
|
|
213
|
+
key: string;
|
|
214
|
+
rev: number;
|
|
215
|
+
status: "ready" | "error";
|
|
216
|
+
error?: ErrorWire;
|
|
217
|
+
}[];
|
|
218
|
+
replies: {
|
|
219
|
+
qid: number;
|
|
220
|
+
ok: boolean;
|
|
221
|
+
value?: unknown;
|
|
222
|
+
error?: ErrorWire;
|
|
223
|
+
}[];
|
|
224
|
+
/**
|
|
225
|
+
* The colouring requests applied in this batch (mesh, revision), in order:
|
|
226
|
+
* the colours and values in `changes` are made by these rules. A rejected
|
|
227
|
+
* or superseded colouring has none. (Their `display:` statuses may come
|
|
228
|
+
* later, with the batch that completes the fields.)
|
|
229
|
+
*/
|
|
230
|
+
colorings: {
|
|
231
|
+
id: ClientId;
|
|
232
|
+
rev: number;
|
|
233
|
+
}[];
|
|
234
|
+
/**
|
|
235
|
+
* The engine stopped: the WebAssembly instance trapped, or packing the
|
|
236
|
+
* result failed. The whole batch failed: its changes are lost, and every
|
|
237
|
+
* status and reply in this result is an `INSTANCE_POISONED` error (the
|
|
238
|
+
* message names the cause). Later batches fail the same way without
|
|
239
|
+
* running. Recreate the Worker.
|
|
240
|
+
*/
|
|
241
|
+
poisoned?: boolean;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* The first message to a Worker, before any batch: how to start the engine.
|
|
245
|
+
* `threads` is the pool size asked for (`undefined`: the Worker's default,
|
|
246
|
+
* see `defaultThreads`); `debug` asks for the development warnings.
|
|
247
|
+
* `crashChannel` (threaded engines only) names the `BroadcastChannel` the
|
|
248
|
+
* pool's threads report a crash on: the engine Worker is then blocked and
|
|
249
|
+
* cannot, so the scene listens there itself (see {@link PoolCrash}).
|
|
250
|
+
*/
|
|
251
|
+
interface InitMessage {
|
|
252
|
+
kind: "init";
|
|
253
|
+
threads?: number;
|
|
254
|
+
debug: boolean;
|
|
255
|
+
crashChannel?: string;
|
|
256
|
+
/** `WorkerSceneOptions.backgroundAddTriangles`, when set. */
|
|
257
|
+
backgroundAddTriangles?: number;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* What a Worker posts: a {@link Result} per batch, after `{ kind: "ready" }`
|
|
261
|
+
* once the engine runs, or `{ kind: "fatal" }` when it could not start. The
|
|
262
|
+
* ready message reports the threads the engine computes with (1 when
|
|
263
|
+
* single-threaded) and, in development, why it is single-threaded when more
|
|
264
|
+
* were asked for (`warning`, for the main side to log once per scene).
|
|
265
|
+
* `probeFailed` says this browser cannot run the threaded engine at all: the
|
|
266
|
+
* scene then asks its later Workers, and the page's later scenes, for one
|
|
267
|
+
* thread.
|
|
268
|
+
* `{ kind: "fallback" }` replaces the ready message when the threaded pool
|
|
269
|
+
* did not start: the Worker gives back the batches it received (later ones
|
|
270
|
+
* follow, one message each) and expects to be replaced by a single-threaded
|
|
271
|
+
* Worker, which gets them. `{ kind: "update" }` carries a result that
|
|
272
|
+
* belongs to no batch: a mesh added in the background landed while the
|
|
273
|
+
* Worker was idle (its frame, and the add's status); its `rev` is that of
|
|
274
|
+
* the last batch result.
|
|
275
|
+
*/
|
|
276
|
+
type WorkerMessage = Result | {
|
|
277
|
+
kind: "update";
|
|
278
|
+
result: Result;
|
|
279
|
+
} | {
|
|
280
|
+
kind: "ready";
|
|
281
|
+
threads?: number;
|
|
282
|
+
warning?: string;
|
|
283
|
+
probeFailed?: true;
|
|
284
|
+
} | {
|
|
285
|
+
kind: "fallback";
|
|
286
|
+
batches: Request[][];
|
|
287
|
+
warning?: string;
|
|
288
|
+
} | {
|
|
289
|
+
kind: "fatal";
|
|
290
|
+
error: ErrorWire;
|
|
291
|
+
};
|
|
292
|
+
//#endregion
|
|
293
|
+
export { WorkerMessage as a, Result as i, InitMessage as n, Request as r, ClientId as t };
|
package/dist/r3f.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { W as MeshHandle } from "./scene-like-B_3qm0u6.js";
|
|
2
|
+
import { t as SceneStore } from "./store-DNnHN33A.js";
|
|
3
|
+
import { n as BoundMesh, t as BindMeshOptions } from "./mesh-B16gV3oX.js";
|
|
4
|
+
import * as THREE from "three";
|
|
5
|
+
import { JSX } from "react";
|
|
6
|
+
import { EventHandlers } from "@react-three/fiber";
|
|
7
|
+
//#region src/r3f/index.d.ts
|
|
8
|
+
/**
|
|
9
|
+
* Calls `store.commitFrame()` once per rendered R3F frame, however many
|
|
10
|
+
* components call this hook for the same store, and invalidates the canvas
|
|
11
|
+
* when the store or the scene changes, so `frameloop="demand"` renders the
|
|
12
|
+
* change. Use it inside a `<Canvas>`; `<FastMesh>` calls it, so an app that
|
|
13
|
+
* draws through `<FastMesh>` needs no other driver. Do not also call
|
|
14
|
+
* `store.commitFrame()` yourself.
|
|
15
|
+
*
|
|
16
|
+
* The first frame switches the store to frame flushing (declarative changes
|
|
17
|
+
* wait for the next frame); when the store's last driver unmounts, the store
|
|
18
|
+
* goes back to flushing them on a microtask.
|
|
19
|
+
*
|
|
20
|
+
* @param store - The store to drive; defaults to the nearest `SceneProvider`'s.
|
|
21
|
+
* @throws `INVALID_ARGUMENT` without a `store` outside a provider.
|
|
22
|
+
*/
|
|
23
|
+
export declare function useFrameDriver(store?: SceneStore): void;
|
|
24
|
+
/** Props of {@link FastMesh}: R3F's pointer events are passed through to the mesh. */
|
|
25
|
+
export interface FastMeshProps extends EventHandlers {
|
|
26
|
+
/** The scene mesh to draw; added through the store (`store.addMesh`). */
|
|
27
|
+
mesh: MeshHandle;
|
|
28
|
+
/** The store; defaults to the nearest `SceneProvider`'s. */
|
|
29
|
+
store?: SceneStore;
|
|
30
|
+
/**
|
|
31
|
+
* The material (see `BindMeshOptions.material`): give each `<FastMesh>` its
|
|
32
|
+
* own, and dispose it yourself. Changing it rebinds the mesh.
|
|
33
|
+
*/
|
|
34
|
+
material?: BindMeshOptions["material"];
|
|
35
|
+
/** The colour while the mesh has no colouring (see `BindMeshOptions.color`). */
|
|
36
|
+
color?: THREE.ColorRepresentation;
|
|
37
|
+
/**
|
|
38
|
+
* How a colouring is drawn (see `BindMeshOptions.shading`): `"vertex"` (the
|
|
39
|
+
* default) or `"fragment"`. Changing it rebinds the mesh.
|
|
40
|
+
*/
|
|
41
|
+
shading?: BindMeshOptions["shading"];
|
|
42
|
+
/**
|
|
43
|
+
* Fragment shading's edge softness in distance units (see
|
|
44
|
+
* `BindMeshOptions.edgeSoftness`); 0 (the default) is a hard edge. Changing
|
|
45
|
+
* it never rebinds. NaN, an infinite or a negative width draws as 0, with a
|
|
46
|
+
* console warning (`bindMesh` would throw).
|
|
47
|
+
*/
|
|
48
|
+
edgeSoftness?: number;
|
|
49
|
+
/**
|
|
50
|
+
* Called with the binding once the mesh is bound, and with `null` when that
|
|
51
|
+
* binding is disposed (unmount, removal, or a rebind, which then delivers
|
|
52
|
+
* the new one). Pass it to `bindSection` or `attachTransformControls`.
|
|
53
|
+
* `bindSection` clips the materials it sees when it is called, so bind the
|
|
54
|
+
* section again when a new binding arrives. A new callback never rebinds.
|
|
55
|
+
*/
|
|
56
|
+
onBind?: (bound: BoundMesh | null) => void;
|
|
57
|
+
}
|
|
58
|
+
export declare function FastMesh(props: FastMeshProps): JSX.Element;
|
|
59
|
+
//#endregion
|
package/dist/r3f.js
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { t as FastMeshError } from "./core-Cke_kZ2e.js";
|
|
2
|
+
import { t as bindMesh } from "./mesh-B8pae8WA.js";
|
|
3
|
+
import { s as meshState } from "./snapshot-Cf6NgDl7.js";
|
|
4
|
+
import { r as setFrameLoop } from "./store-D3ErV6o-.js";
|
|
5
|
+
import { c as useStoreSelector, i as useScene, l as StoreContext, t as SceneProvider } from "./react-CTADh3NN.js";
|
|
6
|
+
import * as THREE from "three";
|
|
7
|
+
import { createElement, useCallback, useContext, useLayoutEffect, useRef, useState } from "react";
|
|
8
|
+
import { useFrame, useThree } from "@react-three/fiber";
|
|
9
|
+
//#region src/r3f/index.ts
|
|
10
|
+
const drivers = /* @__PURE__ */ new WeakMap();
|
|
11
|
+
/**
|
|
12
|
+
* Calls `store.commitFrame()` once per rendered R3F frame, however many
|
|
13
|
+
* components call this hook for the same store, and invalidates the canvas
|
|
14
|
+
* when the store or the scene changes, so `frameloop="demand"` renders the
|
|
15
|
+
* change. Use it inside a `<Canvas>`; `<FastMesh>` calls it, so an app that
|
|
16
|
+
* draws through `<FastMesh>` needs no other driver. Do not also call
|
|
17
|
+
* `store.commitFrame()` yourself.
|
|
18
|
+
*
|
|
19
|
+
* The first frame switches the store to frame flushing (declarative changes
|
|
20
|
+
* wait for the next frame); when the store's last driver unmounts, the store
|
|
21
|
+
* goes back to flushing them on a microtask.
|
|
22
|
+
*
|
|
23
|
+
* @param store - The store to drive; defaults to the nearest `SceneProvider`'s.
|
|
24
|
+
* @throws `INVALID_ARGUMENT` without a `store` outside a provider.
|
|
25
|
+
*/
|
|
26
|
+
function useFrameDriver(store) {
|
|
27
|
+
const fromContext = useContext(StoreContext);
|
|
28
|
+
const target = store ?? fromContext;
|
|
29
|
+
if (target === null) throw new FastMeshError("INVALID_ARGUMENT", "useFrameDriver() needs a store or a <SceneProvider> above it");
|
|
30
|
+
const invalidate = useThree((s) => s.invalidate);
|
|
31
|
+
const [token] = useState(() => ({}));
|
|
32
|
+
useLayoutEffect(() => {
|
|
33
|
+
let found = drivers.get(target);
|
|
34
|
+
if (found === void 0) {
|
|
35
|
+
found = {
|
|
36
|
+
tokens: [],
|
|
37
|
+
canvases: /* @__PURE__ */ new Map()
|
|
38
|
+
};
|
|
39
|
+
drivers.set(target, found);
|
|
40
|
+
}
|
|
41
|
+
const d = found;
|
|
42
|
+
d.tokens.push(token);
|
|
43
|
+
let subscribed = d.canvases.get(invalidate);
|
|
44
|
+
if (subscribed === void 0) {
|
|
45
|
+
const offStore = target.subscribe(() => invalidate());
|
|
46
|
+
const offScene = target.scene.onChanges((changes) => {
|
|
47
|
+
if (changes.length > 0) invalidate();
|
|
48
|
+
});
|
|
49
|
+
subscribed = {
|
|
50
|
+
count: 0,
|
|
51
|
+
off: () => {
|
|
52
|
+
offStore();
|
|
53
|
+
offScene();
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
d.canvases.set(invalidate, subscribed);
|
|
57
|
+
}
|
|
58
|
+
const canvas = subscribed;
|
|
59
|
+
canvas.count++;
|
|
60
|
+
return () => {
|
|
61
|
+
const at = d.tokens.indexOf(token);
|
|
62
|
+
if (at !== -1) d.tokens.splice(at, 1);
|
|
63
|
+
if (--canvas.count === 0) {
|
|
64
|
+
d.canvases.delete(invalidate);
|
|
65
|
+
canvas.off();
|
|
66
|
+
}
|
|
67
|
+
if (d.tokens.length === 0) setFrameLoop(target, false);
|
|
68
|
+
};
|
|
69
|
+
}, [
|
|
70
|
+
target,
|
|
71
|
+
token,
|
|
72
|
+
invalidate
|
|
73
|
+
]);
|
|
74
|
+
useFrame(() => {
|
|
75
|
+
if (drivers.get(target)?.tokens[0] === token) target.commitFrame();
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Draws a scene mesh: binds it with `bindMesh` on mount and disposes the
|
|
80
|
+
* binding on unmount (or when the mesh is removed, after which it renders
|
|
81
|
+
* nothing). The mesh's applied `opacity` and `visible` follow the store in
|
|
82
|
+
* the frame that applies them; colouring follows the scene. Drives the
|
|
83
|
+
* store's frames with {@link useFrameDriver}. Use it inside a `<Canvas>`.
|
|
84
|
+
*
|
|
85
|
+
* The mesh must be added through the store (`store.addMesh`): a mesh added
|
|
86
|
+
* straight to `store.scene` is not in the store, and renders nothing.
|
|
87
|
+
*
|
|
88
|
+
* @throws `INVALID_ARGUMENT` without a `store` outside a `SceneProvider`.
|
|
89
|
+
*/
|
|
90
|
+
/**
|
|
91
|
+
* `width` when it is a finite number `>= 0` (what `setEdgeSoftness` takes),
|
|
92
|
+
* else 0 with a warning: a bad prop (a slider's NaN, say) draws hard edges
|
|
93
|
+
* rather than throwing inside the canvas.
|
|
94
|
+
*/
|
|
95
|
+
function usableSoftness(width) {
|
|
96
|
+
if (typeof width === "number" && Number.isFinite(width) && width >= 0) return width;
|
|
97
|
+
console.warn(`fast-mesh-wasm: <FastMesh edgeSoftness={${String(width)}}> is not a finite number >= 0; using 0.`);
|
|
98
|
+
return 0;
|
|
99
|
+
}
|
|
100
|
+
function FastMesh(props) {
|
|
101
|
+
const body = createElement(FastMeshBody, props);
|
|
102
|
+
return props.store === void 0 ? body : createElement(SceneProvider, { store: props.store }, body);
|
|
103
|
+
}
|
|
104
|
+
function FastMeshBody(props) {
|
|
105
|
+
const { mesh, store: _store, material, color, shading, edgeSoftness = 0, onBind, ...events } = props;
|
|
106
|
+
const store = useScene();
|
|
107
|
+
useFrameDriver(store);
|
|
108
|
+
const present = useStoreSelector(useCallback((s) => s.meshes.has(mesh), [mesh]));
|
|
109
|
+
const colorKey = color === void 0 ? void 0 : new THREE.Color(color).getHex();
|
|
110
|
+
const [bound, setBound] = useState(null);
|
|
111
|
+
const onBindRef = useRef(onBind);
|
|
112
|
+
onBindRef.current = onBind;
|
|
113
|
+
const softness = usableSoftness(edgeSoftness);
|
|
114
|
+
const softnessRef = useRef(softness);
|
|
115
|
+
softnessRef.current = softness;
|
|
116
|
+
useLayoutEffect(() => {
|
|
117
|
+
if (!present) return;
|
|
118
|
+
const options = {
|
|
119
|
+
material,
|
|
120
|
+
shading,
|
|
121
|
+
edgeSoftness: softnessRef.current
|
|
122
|
+
};
|
|
123
|
+
const b = bindMesh(store.scene, mesh, colorKey === void 0 ? options : {
|
|
124
|
+
...options,
|
|
125
|
+
color: colorKey
|
|
126
|
+
});
|
|
127
|
+
let last;
|
|
128
|
+
const apply = () => {
|
|
129
|
+
const d = meshState(store.getSnapshot(), mesh).display;
|
|
130
|
+
if (d !== last) {
|
|
131
|
+
last = d;
|
|
132
|
+
b.setOpacity(d.opacity);
|
|
133
|
+
b.object.visible = d.visible;
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
apply();
|
|
137
|
+
const off = store.subscribe(apply);
|
|
138
|
+
setBound(b);
|
|
139
|
+
onBindRef.current?.(b);
|
|
140
|
+
return () => {
|
|
141
|
+
off();
|
|
142
|
+
b.dispose();
|
|
143
|
+
setBound((current) => current === b ? null : current);
|
|
144
|
+
onBindRef.current?.(null);
|
|
145
|
+
};
|
|
146
|
+
}, [
|
|
147
|
+
store,
|
|
148
|
+
mesh,
|
|
149
|
+
present,
|
|
150
|
+
material,
|
|
151
|
+
colorKey,
|
|
152
|
+
shading
|
|
153
|
+
]);
|
|
154
|
+
useLayoutEffect(() => {
|
|
155
|
+
bound?.setEdgeSoftness(softness);
|
|
156
|
+
}, [bound, softness]);
|
|
157
|
+
if (bound === null) return null;
|
|
158
|
+
return createElement("primitive", {
|
|
159
|
+
object: bound.object,
|
|
160
|
+
dispose: null,
|
|
161
|
+
...events
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
//#endregion
|
|
165
|
+
export { FastMesh, useFrameDriver };
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { t as FastMeshError } from "./core-Cke_kZ2e.js";
|
|
2
|
+
import { l as sectionState, n as EMPTY_SNAPSHOT, s as meshState } from "./snapshot-Cf6NgDl7.js";
|
|
3
|
+
import { createContext, createElement, useCallback, useContext, useMemo, useRef, useSyncExternalStore } from "react";
|
|
4
|
+
//#region src/react/context.ts
|
|
5
|
+
/** The store a `SceneProvider` provides, or `null` outside one. */
|
|
6
|
+
const StoreContext = createContext(null);
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src/react/index.ts
|
|
9
|
+
/** Provides `store` to the hooks below it. */
|
|
10
|
+
function SceneProvider(props) {
|
|
11
|
+
return createElement(StoreContext.Provider, { value: props.store }, props.children);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The store of the nearest {@link SceneProvider}.
|
|
15
|
+
*
|
|
16
|
+
* @throws `INVALID_ARGUMENT` outside a provider.
|
|
17
|
+
*/
|
|
18
|
+
function useScene() {
|
|
19
|
+
const store = useContext(StoreContext);
|
|
20
|
+
if (store === null) throw new FastMeshError("INVALID_ARGUMENT", "useScene() needs a <SceneProvider> above it");
|
|
21
|
+
return store;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Selects a slice of the store's snapshot; the component re-renders only when
|
|
25
|
+
* the slice changes (by `isEqual`, `Object.is` by default). The selection is
|
|
26
|
+
* cached per snapshot and selector, and a new selection equal to the
|
|
27
|
+
* previous one keeps the previous value, so a selector may build new objects
|
|
28
|
+
* and may be an inline function. The server renders `EMPTY_SNAPSHOT`.
|
|
29
|
+
*/
|
|
30
|
+
function useStoreSelector(selector, isEqual = Object.is) {
|
|
31
|
+
const store = useScene();
|
|
32
|
+
const selectorRef = useRef(selector);
|
|
33
|
+
const isEqualRef = useRef(isEqual);
|
|
34
|
+
selectorRef.current = selector;
|
|
35
|
+
isEqualRef.current = isEqual;
|
|
36
|
+
const [get, getServer] = useMemo(() => {
|
|
37
|
+
const memo = (read) => {
|
|
38
|
+
const m = { has: false };
|
|
39
|
+
return () => {
|
|
40
|
+
const snap = read();
|
|
41
|
+
const sel = selectorRef.current;
|
|
42
|
+
if (m.has && snap === m.snap && sel === m.selector) return m.value;
|
|
43
|
+
const next = sel(snap);
|
|
44
|
+
m.snap = snap;
|
|
45
|
+
m.selector = sel;
|
|
46
|
+
if (!m.has || !isEqualRef.current(m.value, next)) {
|
|
47
|
+
m.value = next;
|
|
48
|
+
m.has = true;
|
|
49
|
+
}
|
|
50
|
+
return m.value;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
return [memo(store.getSnapshot), memo(() => EMPTY_SNAPSHOT)];
|
|
54
|
+
}, [store]);
|
|
55
|
+
return useSyncExternalStore(store.subscribe, get, getServer);
|
|
56
|
+
}
|
|
57
|
+
const merged = /* @__PURE__ */ new WeakMap();
|
|
58
|
+
function mergeDisplay(state) {
|
|
59
|
+
let out = merged.get(state);
|
|
60
|
+
if (out === void 0) {
|
|
61
|
+
out = Object.freeze({
|
|
62
|
+
...state,
|
|
63
|
+
...state.display
|
|
64
|
+
});
|
|
65
|
+
merged.set(state, out);
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* A mesh's display and its setter (`store.setDisplay`). The toggle-button
|
|
71
|
+
* pattern is `pressed = state.desired.coloring !== null`, with
|
|
72
|
+
* `busy = state.status === "computing"`. A mesh not in the store (removed,
|
|
73
|
+
* or rendered on the server) reads as `REMOVED_MESH_STATE`: `"idle"`,
|
|
74
|
+
* default display. The setter throws `UNKNOWN_MESH` for such a mesh.
|
|
75
|
+
*/
|
|
76
|
+
function useMeshDisplay(mesh) {
|
|
77
|
+
const store = useScene();
|
|
78
|
+
return [useStoreSelector(useCallback((s) => mergeDisplay(meshState(s, mesh)), [mesh])), useCallback((patch) => store.setDisplay(mesh, patch), [store, mesh])];
|
|
79
|
+
}
|
|
80
|
+
const selectHistory = (s) => s.history;
|
|
81
|
+
/** Undo and redo, with whether each is available. The object and its actions keep their identity while history is unchanged; the actions are stable per store. */
|
|
82
|
+
function useHistory() {
|
|
83
|
+
const store = useScene();
|
|
84
|
+
const history = useStoreSelector(selectHistory);
|
|
85
|
+
const undo = useCallback(() => store.undo(), [store]);
|
|
86
|
+
const redo = useCallback(() => store.redo(), [store]);
|
|
87
|
+
return useMemo(() => ({
|
|
88
|
+
canUndo: history.canUndo,
|
|
89
|
+
canRedo: history.canRedo,
|
|
90
|
+
undo,
|
|
91
|
+
redo
|
|
92
|
+
}), [
|
|
93
|
+
history,
|
|
94
|
+
undo,
|
|
95
|
+
redo
|
|
96
|
+
]);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* A section's state (the applied plane, meshes and fill, `desired` and
|
|
100
|
+
* `status`) and its setter (`store.setSection`). A section not in the store
|
|
101
|
+
* reads as `REMOVED_SECTION_STATE` (`"idle"`, no meshes); the setter throws
|
|
102
|
+
* `UNKNOWN_SECTION` for it.
|
|
103
|
+
*/
|
|
104
|
+
function useSection(section) {
|
|
105
|
+
const store = useScene();
|
|
106
|
+
return [useStoreSelector(useCallback((s) => sectionState(s, section), [section])), useCallback((patch) => store.setSection(section, patch), [store, section])];
|
|
107
|
+
}
|
|
108
|
+
const selectStatus = (s) => s.error === void 0 ? { status: s.status } : {
|
|
109
|
+
status: s.status,
|
|
110
|
+
error: s.error
|
|
111
|
+
};
|
|
112
|
+
const sameStatus = (a, b) => a.status === b.status && a.error === b.error;
|
|
113
|
+
/** The overall status: `"error"` (with the first error) when anything failed, else `"computing"` while anything is pending. */
|
|
114
|
+
function useSceneStatus() {
|
|
115
|
+
return useStoreSelector(selectStatus, sameStatus);
|
|
116
|
+
}
|
|
117
|
+
const selectSelection = (s) => s.selection;
|
|
118
|
+
/** The selected mesh (or `null`) and the function that selects one (`store.select`). */
|
|
119
|
+
function useSelection() {
|
|
120
|
+
const store = useScene();
|
|
121
|
+
return [useStoreSelector(selectSelection), useCallback((mesh) => store.select(mesh), [store])];
|
|
122
|
+
}
|
|
123
|
+
//#endregion
|
|
124
|
+
export { useSceneStatus as a, useStoreSelector as c, useScene as i, StoreContext as l, useHistory as n, useSection as o, useMeshDisplay as r, useSelection as s, SceneProvider as t };
|