@margoengine/voxel-edit 0.0.0-stage → 1.0.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 (64) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +445 -2
  4. package/dist/animation.d.ts +50 -0
  5. package/dist/animation.js +88 -0
  6. package/dist/assist.d.ts +24 -0
  7. package/dist/assist.js +52 -0
  8. package/dist/brush-gpu.d.ts +62 -0
  9. package/dist/bytes.d.ts +55 -0
  10. package/dist/collab.d.ts +77 -0
  11. package/dist/collab.js +159 -0
  12. package/dist/edit.d.ts +30 -0
  13. package/dist/edit.js +361 -0
  14. package/dist/export.d.ts +58 -0
  15. package/dist/export.js +52 -0
  16. package/dist/file-colors-DHT5vrzW.js +55 -0
  17. package/dist/file-colors.d.ts +16 -0
  18. package/dist/format-plist.d.ts +4 -0
  19. package/dist/formats-extra.d.ts +20 -0
  20. package/dist/formats-extra.js +335 -0
  21. package/dist/formats.d.ts +96 -0
  22. package/dist/formats.js +924 -0
  23. package/dist/generate.d.ts +63 -0
  24. package/dist/generate.js +103 -0
  25. package/dist/gox.d.ts +60 -0
  26. package/dist/gpu.d.ts +74 -0
  27. package/dist/gpu.js +167 -0
  28. package/dist/grid-B2V0TGLV.js +120 -0
  29. package/dist/grid.d.ts +75 -0
  30. package/dist/history.d.ts +65 -0
  31. package/dist/history.js +75 -0
  32. package/dist/index.d.ts +5 -0
  33. package/dist/index.js +631 -0
  34. package/dist/layout.d.ts +76 -0
  35. package/dist/library.d.ts +50 -0
  36. package/dist/library.js +89 -0
  37. package/dist/mesh.d.ts +128 -0
  38. package/dist/mesh.js +365 -0
  39. package/dist/model.d.ts +173 -0
  40. package/dist/pick.d.ts +34 -0
  41. package/dist/pick.js +122 -0
  42. package/dist/raster.d.ts +89 -0
  43. package/dist/rig.d.ts +88 -0
  44. package/dist/rig.js +216 -0
  45. package/dist/select.d.ts +50 -0
  46. package/dist/select.js +94 -0
  47. package/dist/shaders-BaY4gcm9.js +234 -0
  48. package/dist/shaders.d.ts +116 -0
  49. package/dist/shaders.js +1 -0
  50. package/dist/slices.d.ts +75 -0
  51. package/dist/smooth.d.ts +43 -0
  52. package/dist/smooth.js +212 -0
  53. package/dist/sparse.d.ts +239 -0
  54. package/dist/sparse.js +646 -0
  55. package/dist/stamp.d.ts +88 -0
  56. package/dist/vmax.d.ts +48 -0
  57. package/dist/vox-resolve.d.ts +64 -0
  58. package/dist/vox-scene.d.ts +51 -0
  59. package/dist/vox.d.ts +30 -0
  60. package/dist/vox.js +684 -0
  61. package/dist/voxelize.d.ts +70 -0
  62. package/dist/voxelize.js +249 -0
  63. package/dist/vxm.d.ts +54 -0
  64. package/package.json +130 -4
@@ -0,0 +1,239 @@
1
+ import type { MutableVec3, Vec3 } from '@margoengine/math';
2
+ import { type VoxelWorld } from '@margoengine/voxel';
3
+ import type { OwnedBuffer } from './bytes.js';
4
+ import { type GridHit, type VoxelGrid } from './grid.js';
5
+ /** One cell change: local x, y, z and the new palette index (0 empties the cell). */
6
+ export type VoxelEdit = readonly [x: number, y: number, z: number, index: number];
7
+ /** Memory and work budgets of a sparse grid, checked before an edit is kept. Each may be raised to 4x its default. */
8
+ export interface SparseLimits {
9
+ /** Chunks (32 x 32 x 32 cells each) the grid may hold. Default 4,096. */
10
+ maxChunks?: number;
11
+ /** Bytes of chunk storage. Default 134,217,728 (128 MiB). */
12
+ maxBytes?: number;
13
+ /** Cell edits in one `edit` call. Default 1,048,576. */
14
+ maxEdits?: number;
15
+ /** Cells in one dense window (`readWindow`, `fromGrid`) or traversal. Default 16,777,216. */
16
+ maxWindowCells?: number;
17
+ }
18
+ /** Options of {@link SparseGrid.empty}. */
19
+ export interface SparseOptions extends SparseLimits {
20
+ /** Engine cell of local cell (0, 0, 0), for {@link SparseGrid.toWorld}. Default [0, 0, 0]. */
21
+ origin?: Vec3;
22
+ /**
23
+ * The cells `origin + local cell` may reach. `'world'` (the default) is a `VoxelWorld`'s range: Y from -32,768 to
24
+ * 32,767, X and Z within 32-bit integers; such a grid saves as version 1. `'wide'` allows any engine cell from
25
+ * -(2^53 - 1) to 2^53 - 1 on every axis (whole numbers stay exact); a grid that leaves the world range saves as
26
+ * version 2. Either way the cell count must be a safe integer, so a cell index (`x + size[0] * (z + size[2] * y)`,
27
+ * the Float64Array selections of `./edit`) is exact.
28
+ */
29
+ envelope?: 'world' | 'wide';
30
+ }
31
+ /** A dense copy of part of a sparse grid. */
32
+ export interface GridWindow extends VoxelGrid {
33
+ /** Local cell of the window's (0, 0, 0). */
34
+ readonly origin: Vec3;
35
+ }
36
+ /** An undo token from {@link SparseGrid.edit}; pass it to {@link SparseGrid.restore}. */
37
+ export interface SparseDelta {
38
+ /** Cells the edit changed. */
39
+ readonly changed: number;
40
+ /** Chunk bytes the before and after snapshots of the touched chunks hold. */
41
+ readonly bytes: number;
42
+ }
43
+ /** What a sparse edit returns. An edit that changes nothing returns the same grid. */
44
+ export interface SparseEdit {
45
+ /** The new snapshot; the old one is unchanged and shares every untouched chunk. */
46
+ grid: SparseGrid;
47
+ /** Undo token. */
48
+ delta: SparseDelta;
49
+ }
50
+ /** Statistics of a sparse grid. */
51
+ export interface SparseStats {
52
+ /** Filled cells. */
53
+ filled: number;
54
+ /** Chunks held (empty chunks are dropped). */
55
+ chunks: number;
56
+ /** Bytes of chunk storage. */
57
+ bytes: number;
58
+ }
59
+ /**
60
+ * The saved form of a sparse grid ({@link SparseGrid.serialize}): plain JSON. `format` stays `'margo-sparse-volume'`
61
+ * so files written before the rename still load. `version` is 1 while the grid's cells stay in the `'world'`
62
+ * envelope ({@link SparseOptions.envelope}) and 2 past it; the fields are the same, and readers before version 2
63
+ * refuse such a file instead of misplacing it.
64
+ */
65
+ export interface SparseGridData {
66
+ format: 'margo-sparse-volume';
67
+ version: 1 | 2;
68
+ size: MutableVec3;
69
+ origin: MutableVec3;
70
+ /** Packed chunks in @margoengine/voxel's palette layout. */
71
+ chunks: {
72
+ position: MutableVec3;
73
+ palette: number[];
74
+ bits: 0 | 1 | 2 | 4 | 8;
75
+ words: number[];
76
+ }[];
77
+ }
78
+ /**
79
+ * Packed chunks as typed arrays, chunk `i`'s parts in order: its position (chunk coordinates, cell / 32) at
80
+ * `positions[3 i ...]`, its `bits` per cell, `paletteSizes[i]` palette entries and `1024 * bits[i]` words, the layout
81
+ * of {@link SparseGridData} chunks.
82
+ */
83
+ export interface SparseChunkTable {
84
+ positions: Float64Array;
85
+ /** 0, 1, 2, 4 or 8 per chunk. */
86
+ bits: Uint8Array;
87
+ /** 1 to 2^bits (at most 256) per chunk. */
88
+ paletteSizes: Uint16Array;
89
+ /** Every chunk's palette entries (0 to 255), one after another. */
90
+ palettes: Uint8Array;
91
+ /** Every chunk's cell words, one after another. */
92
+ words: Uint32Array;
93
+ }
94
+ /**
95
+ * A sparse grid as transferable typed arrays ({@link SparseGrid.snapshot}): each array owns its whole buffer and
96
+ * nothing else refers to it, so `postMessage(snapshot, snapshotBuffers(snapshot))` moves it to a worker without a
97
+ * copy. {@link SparseGrid.fromSnapshot} rebuilds the grid.
98
+ */
99
+ export interface SparseSnapshot {
100
+ format: 'margo-sparse-snapshot';
101
+ version: 1;
102
+ size: MutableVec3;
103
+ origin: MutableVec3;
104
+ chunks: SparseChunkTable;
105
+ }
106
+ /**
107
+ * The chunks one edit touched, before and after it ({@link SparseGrid.deltaSnapshot}), as transferable typed arrays:
108
+ * `before` and `after` list the same positions in the same order, and a chunk that is absent on one side is listed
109
+ * there with bits 0 and the single palette entry 0. {@link SparseGrid.applyDeltaSnapshot} replays it.
110
+ */
111
+ export interface SparseDeltaSnapshot {
112
+ format: 'margo-sparse-delta';
113
+ version: 1;
114
+ size: MutableVec3;
115
+ origin: MutableVec3;
116
+ /** Cells the edit changed. */
117
+ changed: number;
118
+ before: SparseChunkTable;
119
+ after: SparseChunkTable;
120
+ }
121
+ /**
122
+ * An immutable sparse voxel grid for models far larger than memory would hold densely: only chunks with filled cells
123
+ * exist, so a 1,000,000-wide grid with two filled cells holds two chunks. Each edit returns a new snapshot that copies
124
+ * only the chunks it touched. Palette indexes and the cell layout are those of {@link VoxelGrid}.
125
+ */
126
+ export declare class SparseGrid {
127
+ #private;
128
+ /** Cells along x, y and z. */
129
+ readonly size: Vec3;
130
+ /** Engine cell of local (0, 0, 0). */
131
+ readonly origin: Vec3;
132
+ private constructor();
133
+ /** An empty grid of `size`; allocates no chunks, however large. */
134
+ static empty(size: Vec3, options?: SparseOptions): SparseGrid;
135
+ /** A sparse copy of a dense grid (at most `maxWindowCells` cells); palette indexes are kept exactly. */
136
+ static fromGrid(grid: VoxelGrid, options?: SparseOptions): SparseGrid;
137
+ /**
138
+ * Decodes {@link SparseGridData} (untrusted input is fine: every count, word and index is checked first). Version 1
139
+ * data must keep its cells in the `'world'` envelope and version 2 data must leave it, as `serialize` writes them.
140
+ */
141
+ static deserialize(data: unknown, limits?: SparseLimits): SparseGrid;
142
+ /**
143
+ * Rebuilds a grid from {@link SparseSnapshot} (untrusted input is fine, as for {@link SparseGrid.deserialize}). The
144
+ * grid copies what it keeps, so the snapshot's buffers may be reused, transferred again or dropped afterwards.
145
+ */
146
+ static fromSnapshot(snapshot: SparseSnapshot, limits?: SparseLimits): SparseGrid;
147
+ /**
148
+ * The chunks the edit that returned `delta` touched, before and after it, as a transferable
149
+ * {@link SparseDeltaSnapshot} (new buffers nothing else refers to). A worker holding the grid before the edit
150
+ * replays it with {@link SparseGrid.applyDeltaSnapshot}.
151
+ */
152
+ static deltaSnapshot(delta: SparseDelta): SparseDeltaSnapshot;
153
+ /**
154
+ * The chunk storage the edit that returned `delta` added or replaced, on both sides: what an undo step keeps alive
155
+ * beyond the snapshots it shares chunks with. Pass the result to a `ByteLedger` from the package root.
156
+ */
157
+ static deltaBuffers(delta: SparseDelta): OwnedBuffer[];
158
+ /**
159
+ * Each held chunk's storage, with a token every snapshot sharing that chunk reports too; the bytes add up to
160
+ * `stats.bytes`. A `ByteLedger` from the package root counts shared chunks once.
161
+ */
162
+ ownedBuffers(): OwnedBuffer[];
163
+ /** Filled cells, chunks and bytes. */
164
+ get stats(): SparseStats;
165
+ /** The box of filled cells, `max` exclusive, or null when the grid is empty. Visits held chunks only. */
166
+ get bounds(): {
167
+ min: MutableVec3;
168
+ max: MutableVec3;
169
+ } | null;
170
+ /** The palette index at local cell (x, y, z); integer cells outside the grid read as air (0). */
171
+ get(x: number, y: number, z: number): number;
172
+ /**
173
+ * Applies `edits` in order as one transaction (a later edit of the same cell wins) and returns the new snapshot.
174
+ * Nothing is kept when any edit is invalid or a budget is exceeded.
175
+ */
176
+ edit(edits: Iterable<VoxelEdit>, limits?: SparseLimits): SparseEdit;
177
+ /** The snapshot before or after the edit that returned `delta`; this grid must be one of the two. */
178
+ restore(delta: SparseDelta, side: 'before' | 'after'): SparseGrid;
179
+ /** Every filled cell as [x, y, z, index], chunk by chunk (y, then z, then x); empty chunks are never visited. */
180
+ filled(): IterableIterator<VoxelEdit>;
181
+ /** Each held chunk as a dense window of at most 32 x 32 x 32 cells, owned by the caller. */
182
+ windows(): IterableIterator<GridWindow>;
183
+ /** A dense copy of the box at `origin` of `size` (at most `maxWindowCells` cells) inside the grid. */
184
+ readWindow(origin: Vec3, size: Vec3, limits?: SparseLimits): GridWindow;
185
+ /** Replaces the box a window covers, air cells included, as one undoable edit. */
186
+ writeWindow(window: GridWindow, limits?: SparseLimits): SparseEdit;
187
+ /**
188
+ * Casts a ray from `origin` along `direction` (local cells, any length but not zero) and returns the first filled
189
+ * cell within `maxDistance`, visiting only chunks the ray crosses. A ray starting in a filled cell hits it at
190
+ * distance 0 with a zero normal.
191
+ */
192
+ raycast(origin: Vec3, direction: Vec3, maxDistance?: number): GridHit | null;
193
+ /**
194
+ * The grid as a transferable {@link SparseSnapshot}: new buffers that nothing else refers to, owned by the caller.
195
+ * Transferring them (see {@link snapshotBuffers}) leaves this grid unchanged.
196
+ */
197
+ snapshot(): SparseSnapshot;
198
+ /**
199
+ * Replays a {@link SparseDeltaSnapshot} on this grid as one undoable edit: `'after'` (the default) moves this grid
200
+ * from the delta's before chunks to its after chunks, `'before'` undoes it. This grid must hold exactly the chunks
201
+ * the delta starts from (a MargoError with code `state` otherwise: apply deltas in order); nothing changes when a
202
+ * check fails. The result copies what it keeps, as {@link SparseGrid.fromSnapshot} does.
203
+ */
204
+ applyDeltaSnapshot(data: SparseDeltaSnapshot, side?: 'before' | 'after', limits?: SparseLimits): SparseEdit;
205
+ /**
206
+ * The grid as {@link SparseGridData}: plain arrays, safe to `JSON.stringify`. Version 1 while every cell stays in the
207
+ * `'world'` envelope, version 2 past it.
208
+ */
209
+ serialize(limits?: SparseLimits): SparseGridData;
210
+ /**
211
+ * Writes every filled cell into a @margoengine/voxel world at `origin + cell`, the palette index as the block id.
212
+ * The world must have air at all those cells and a Y range covering them; nothing is written when a check fails.
213
+ */
214
+ toWorld(world: VoxelWorld): void;
215
+ }
216
+ /** Options of {@link editWindow}. */
217
+ export interface WindowEditOptions extends SparseLimits {
218
+ /** Local cell of the box's lower corner. */
219
+ origin: Vec3;
220
+ /** Cells along x, y and z. */
221
+ size: Vec3;
222
+ /**
223
+ * `'fill'` fills every cell, `'erase'` empties them, `'paint'` recolours filled ones, `'dilate'` fills air next to a
224
+ * filled cell, `'erode'` empties filled cells next to air. Dilate and erode read neighbours outside the box but
225
+ * write only inside it.
226
+ */
227
+ operation: 'fill' | 'erase' | 'paint' | 'dilate' | 'erode';
228
+ /** Palette index for fill and paint (required) and dilate (default: the filled neighbour's index), 1-255. */
229
+ color?: number;
230
+ }
231
+ /** Applies one operation to a box of a sparse grid as a single undoable edit. */
232
+ export declare function editWindow(grid: SparseGrid, options: WindowEditOptions): SparseEdit;
233
+ /**
234
+ * {@link SparseGrid.fromSnapshot} for several snapshots with one `maxBytes` budget across all of them (each grid's
235
+ * chunk bytes count against what the next may use); the other limits apply to each grid.
236
+ */
237
+ export declare function restoreSnapshots(snapshots: readonly SparseSnapshot[], limits?: SparseLimits): SparseGrid[];
238
+ /** The distinct buffers of a snapshot or delta snapshot: the transfer list for `postMessage`. */
239
+ export declare function snapshotBuffers(data: SparseSnapshot | SparseDeltaSnapshot): ArrayBuffer[];