@margoengine/voxel 0.0.0-stage → 0.1.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 (90) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +1671 -3
  4. package/dist/bevel.d.ts +79 -0
  5. package/dist/bevel.js +453 -0
  6. package/dist/blocks-BabtpQT-.js +179 -0
  7. package/dist/blocks.d.ts +220 -0
  8. package/dist/border.d.ts +9 -0
  9. package/dist/chunk-BCoaKSn4.js +158 -0
  10. package/dist/chunk.d.ts +112 -0
  11. package/dist/codec-ZZFj1xvt.js +410 -0
  12. package/dist/codec.d.ts +125 -0
  13. package/dist/codec.js +1 -0
  14. package/dist/debris.d.ts +114 -0
  15. package/dist/debris.js +387 -0
  16. package/dist/faces-DUR7LhrN.js +146 -0
  17. package/dist/faces.d.ts +7 -0
  18. package/dist/fluids.d.ts +74 -0
  19. package/dist/fluids.js +226 -0
  20. package/dist/generate.d.ts +78 -0
  21. package/dist/generate.js +99 -0
  22. package/dist/gi.d.ts +76 -0
  23. package/dist/gi.js +264 -0
  24. package/dist/gpu-mesher.d.ts +77 -0
  25. package/dist/gpu-mesher.js +461 -0
  26. package/dist/granular-mpm.d.ts +32 -0
  27. package/dist/granular-mpm.js +46 -0
  28. package/dist/index.d.ts +6 -0
  29. package/dist/index.js +4 -0
  30. package/dist/light-rgb.d.ts +71 -0
  31. package/dist/light-rgb.js +287 -0
  32. package/dist/light-worker.d.ts +84 -0
  33. package/dist/light-worker.js +110 -0
  34. package/dist/light.d.ts +108 -0
  35. package/dist/light.js +332 -0
  36. package/dist/light.worker.d.ts +1 -0
  37. package/dist/lightmap.d.ts +50 -0
  38. package/dist/lightmap.js +68 -0
  39. package/dist/lod.d.ts +87 -0
  40. package/dist/lod.js +182 -0
  41. package/dist/mesher.d.ts +214 -0
  42. package/dist/mesher.js +539 -0
  43. package/dist/mesher.worker.d.ts +1 -0
  44. package/dist/model-data-D_rIKJCQ.js +41 -0
  45. package/dist/model-data.d.ts +21 -0
  46. package/dist/model-mesh-C4Gi6bMM.js +122 -0
  47. package/dist/model-mesh.d.ts +21 -0
  48. package/dist/model-surface.d.ts +5 -0
  49. package/dist/models.d.ts +74 -0
  50. package/dist/models.js +308 -0
  51. package/dist/objects.d.ts +73 -0
  52. package/dist/objects.js +152 -0
  53. package/dist/octants-C_Ic7gdn.js +10 -0
  54. package/dist/octants.d.ts +16 -0
  55. package/dist/origin-DsMelUxs.js +23 -0
  56. package/dist/origin.d.ts +7 -0
  57. package/dist/physics.d.ts +147 -0
  58. package/dist/physics.js +486 -0
  59. package/dist/query.d.ts +166 -0
  60. package/dist/query.js +305 -0
  61. package/dist/render-post.d.ts +6 -0
  62. package/dist/render.d.ts +296 -0
  63. package/dist/render.js +1613 -0
  64. package/dist/shared/borrowed.d.ts +44 -0
  65. package/dist/smooth-CEwHkXli.js +968 -0
  66. package/dist/smooth-clip.d.ts +24 -0
  67. package/dist/smooth-lod.d.ts +118 -0
  68. package/dist/smooth-lod.js +244 -0
  69. package/dist/smooth.d.ts +171 -0
  70. package/dist/smooth.js +1 -0
  71. package/dist/stream.d.ts +201 -0
  72. package/dist/stream.js +402 -0
  73. package/dist/terrain.worker.d.ts +1 -0
  74. package/dist/textures.d.ts +86 -0
  75. package/dist/ticks.d.ts +126 -0
  76. package/dist/ticks.js +239 -0
  77. package/dist/unit-light-Ch7roJgB.js +7 -0
  78. package/dist/vox.d.ts +237 -0
  79. package/dist/vox.js +780 -0
  80. package/dist/wind-data.d.ts +13 -0
  81. package/dist/wind.d.ts +68 -0
  82. package/dist/wind.js +170 -0
  83. package/dist/worker-DlGRxlc4.js +44 -0
  84. package/dist/worker-pool-sPyC6jCE.js +93 -0
  85. package/dist/workers/light.js +1 -0
  86. package/dist/workers/mesher.js +1 -0
  87. package/dist/workers/terrain.js +1 -0
  88. package/dist/world-CelTlft2.js +153 -0
  89. package/dist/world.d.ts +117 -0
  90. package/package.json +175 -6
package/dist/vox.js ADDED
@@ -0,0 +1,780 @@
1
+ import { i as e, t } from "./world-CelTlft2.js";
2
+ //#region src/vox.ts
3
+ var n = /* @__PURE__ */ new Map([
4
+ ["PACK", "pack"],
5
+ ["SIZE", "models"],
6
+ ["nTRN", "nodes"],
7
+ ["nGRP", "nodes"],
8
+ ["nSHP", "nodes"],
9
+ ["LAYR", "layers"],
10
+ ["RGBA", "palette"],
11
+ ["MATL", "materials"]
12
+ ]), r = {
13
+ transform: "nTRN",
14
+ group: "nGRP",
15
+ shape: "nSHP"
16
+ }, i = /* @__PURE__ */ new Uint8Array(), a = [
17
+ 1,
18
+ 0,
19
+ 0,
20
+ 0,
21
+ 1,
22
+ 0,
23
+ 0,
24
+ 0,
25
+ 1
26
+ ], o = (() => {
27
+ let e = /* @__PURE__ */ new Uint8Array(1024), t = [
28
+ 255,
29
+ 204,
30
+ 153,
31
+ 102,
32
+ 51,
33
+ 0
34
+ ], n = [
35
+ 238,
36
+ 221,
37
+ 187,
38
+ 170,
39
+ 136,
40
+ 119,
41
+ 85,
42
+ 68,
43
+ 34,
44
+ 17
45
+ ], r = 1;
46
+ for (let n of t) for (let i of t) for (let a of t) n | i | a && e.set([
47
+ n,
48
+ i,
49
+ a,
50
+ 255
51
+ ], 4 * r++);
52
+ for (let t of [
53
+ [
54
+ 1,
55
+ 0,
56
+ 0
57
+ ],
58
+ [
59
+ 0,
60
+ 1,
61
+ 0
62
+ ],
63
+ [
64
+ 0,
65
+ 0,
66
+ 1
67
+ ],
68
+ [
69
+ 1,
70
+ 1,
71
+ 1
72
+ ]
73
+ ]) for (let i of n) e.set([
74
+ i * t[0],
75
+ i * t[1],
76
+ i * t[2],
77
+ 255
78
+ ], 4 * r++);
79
+ return e;
80
+ })(), s = (e) => typeof e == "number" && (e | 0) === e, c = (e, t, n, r) => typeof t === n ? /* @__PURE__ */ RangeError(`${e} ${String(t)} is invalid; pass ${r}`) : /* @__PURE__ */ TypeError(`${e} must be a ${n}; pass ${r}`), l = {
81
+ read: (e) => e,
82
+ write(e, t) {
83
+ if (typeof e != "string") throw TypeError(`${t} must be a string`);
84
+ return e;
85
+ }
86
+ }, u = {
87
+ read: (e) => e !== "0",
88
+ write(e, t) {
89
+ if (typeof e != "boolean") throw TypeError(`${t} must be true or false`);
90
+ return e ? "1" : "0";
91
+ }
92
+ }, d = {
93
+ read: (e) => Number.isFinite(Number(e)) ? Number(e) : void 0,
94
+ write(e, t) {
95
+ if (typeof e != "number" || !Number.isFinite(e)) throw c(t, e, "number", "a finite number");
96
+ return String(e);
97
+ }
98
+ }, f = {
99
+ read: (e) => s(Number(e)) && Number(e) >= 0 ? Number(e) : void 0,
100
+ write(e, t) {
101
+ if (!s(e) || e < 0) throw c(t, e, "number", "an integer frame index from 0");
102
+ return String(e);
103
+ }
104
+ }, p = {
105
+ read: (e) => e.startsWith("_") ? e.slice(1) : e,
106
+ write: (e, t) => `_${l.write(e, t)}`
107
+ }, m = {
108
+ read(e) {
109
+ let t = e.trim().split(/\s+/).map(Number);
110
+ return t.length === 3 && t.every(s) ? t : void 0;
111
+ },
112
+ write(e, t) {
113
+ if (!Array.isArray(e) || e.length !== 3 || !e.every(s)) throw RangeError(`${t} must be 3 32-bit integers [x, y, z]`);
114
+ return e.join(" ");
115
+ }
116
+ };
117
+ function h(e) {
118
+ let t = e & 3, n = e >> 2 & 3;
119
+ if (t > 2 || n > 2 || t === n) return;
120
+ let r = [
121
+ 0,
122
+ 0,
123
+ 0,
124
+ 0,
125
+ 0,
126
+ 0,
127
+ 0,
128
+ 0,
129
+ 0
130
+ ];
131
+ return r[t] = e & 16 ? -1 : 1, r[3 + n] = e & 32 ? -1 : 1, r[9 - t - n] = e & 64 ? -1 : 1, r;
132
+ }
133
+ function g(e, t) {
134
+ let n = e, r = [];
135
+ if (Array.isArray(n) && n.length === 9) for (let e = 0; e < 3; e++) {
136
+ let t = n.slice(3 * e, 3 * e + 3), i = t.findIndex((e) => e === 1 || e === -1);
137
+ if (i < 0 || r.includes(i) || t.some((e, t) => t !== i && e !== 0)) break;
138
+ r.push(i);
139
+ }
140
+ if (r.length !== 3) throw RangeError(`${t} must be a row-major 3 x 3 signed permutation (each row and column one 1 or -1)`);
141
+ return r[0] | r[1] << 2 | (n[r[0]] < 0 ? 16 : 0) | (n[3 + r[1]] < 0 ? 32 : 0) | (n[6 + r[2]] < 0 ? 64 : 0);
142
+ }
143
+ var _ = {
144
+ read: (e) => s(Number(e)) && (Number(e) & 255) === Number(e) ? h(Number(e)) : void 0,
145
+ write: (e, t) => String(g(e, t))
146
+ }, v = [[
147
+ "name",
148
+ "_name",
149
+ l
150
+ ], [
151
+ "hidden",
152
+ "_hidden",
153
+ u
154
+ ]], y = [
155
+ [
156
+ "rotation",
157
+ "_r",
158
+ _
159
+ ],
160
+ [
161
+ "translation",
162
+ "_t",
163
+ m
164
+ ],
165
+ [
166
+ "frame",
167
+ "_f",
168
+ f
169
+ ]
170
+ ], b = [[
171
+ "frame",
172
+ "_f",
173
+ f
174
+ ]], x = [[
175
+ "type",
176
+ "_type",
177
+ p
178
+ ], ...[
179
+ "weight",
180
+ "rough",
181
+ "spec",
182
+ "ior",
183
+ "att",
184
+ "flux",
185
+ "plastic"
186
+ ].map((e) => [
187
+ e,
188
+ `_${e}`,
189
+ d
190
+ ])], S = new TextDecoder("utf-8", {
191
+ fatal: !0,
192
+ ignoreBOM: !0
193
+ }), C = new TextDecoder("windows-1252"), w = new TextEncoder(), T = !1, E = (e) => {
194
+ if (T = !1, e.length <= 64 && e.every((e) => e < 128)) return String.fromCharCode(...e);
195
+ try {
196
+ return S.decode(e);
197
+ } catch {
198
+ return T = !0, C.decode(e);
199
+ }
200
+ }, D, O = (e) => {
201
+ D ??= new Map(Array.from(C.decode(Uint8Array.from({ length: 256 }, (e, t) => t)), (e, t) => [e.codePointAt(0), t]));
202
+ let t = new Uint8Array(e.length);
203
+ for (let n = 0; n < e.length; n++) {
204
+ let r = D.get(e.charCodeAt(n));
205
+ if (r === void 0) return;
206
+ t[n] = r;
207
+ }
208
+ return t;
209
+ }, k = /* @__PURE__ */ new WeakMap(), A = (e) => /* @__PURE__ */ RangeError(`Vox file ${e}; pass a complete .vox file as written by MagicaVoxel or writeVox`), j = (e) => JSON.stringify(e.length > 40 ? `${e.slice(0, 40)}...` : e);
210
+ function M(e) {
211
+ let t = e instanceof ArrayBuffer ? new Uint8Array(e) : e;
212
+ if (!(t instanceof Uint8Array)) throw TypeError("parseVox expects the file bytes as a Uint8Array or ArrayBuffer");
213
+ let r = new DataView(t.buffer, t.byteOffset, t.byteLength), i = (e) => String.fromCharCode(t[e], t[e + 1], t[e + 2], t[e + 3]);
214
+ if (t.length < 20 || i(0) !== "VOX " || i(8) !== "MAIN") throw TypeError("Bytes are not a .vox file (no \"VOX \" header and MAIN chunk); pass a MagicaVoxel .vox file");
215
+ if (r.getInt32(12, !0)) throw A("MAIN chunk has content bytes");
216
+ if (r.getInt32(16, !0) !== t.length - 20) throw A(`MAIN chunk declares ${r.getInt32(16, !0)} bytes of chunks but ${t.length - 20} follow`);
217
+ let a = {
218
+ version: r.getInt32(4, !0),
219
+ models: [],
220
+ palette: o.slice(),
221
+ materials: [],
222
+ nodes: [],
223
+ layers: [],
224
+ chunks: []
225
+ }, s = a.chunks, c = 20, l = -1, u = !1, d = "", f = 0, p = 0, m = () => `chunk ${d} at byte ${c}`, h = () => {
226
+ if (t.length - c < 12) throw A(`is truncated: a chunk header at byte ${c} needs 12 bytes`);
227
+ let e = r.getInt32(c + 4, !0), n = r.getInt32(c + 8, !0);
228
+ if (e < 0 || n < 0 || e + n > t.length - c - 12) throw A(`is truncated: chunk ${j(i(c))} at byte ${c} declares ${e} + ${n} bytes, ${t.length - c - 12} remain`);
229
+ return [
230
+ i(c),
231
+ e,
232
+ n
233
+ ];
234
+ }, g = (e) => {
235
+ if (e > p - f) throw A(`${m()} is truncated at byte ${f}`);
236
+ }, _ = () => (g(4), f += 4, r.getInt32(f - 4, !0)), S = (e, t, n) => {
237
+ let r = _();
238
+ if (r < e || r > (p - f) / t) throw A(`${m()} has ${r} ${n}, ${r < e ? `fewer than ${e}` : "more than its bytes hold"}`);
239
+ return r;
240
+ }, C = () => {
241
+ let e = _();
242
+ if (e < 0) throw A(`${m()} has a string of ${e} bytes`);
243
+ return g(e), f += e, E(t.subarray(f - e, f));
244
+ }, w = () => {
245
+ let e = /* @__PURE__ */ new Map(), t;
246
+ for (let n = S(0, 8, "attributes"); n--;) {
247
+ let n = C(), r = T;
248
+ if (e.has(n)) throw A(`${m()} has attribute ${j(n)} twice`);
249
+ let i = C();
250
+ e.set(n, i), (r || T) && (t ??= /* @__PURE__ */ new Map()).set(n, {
251
+ key: r,
252
+ value: T ? i : void 0
253
+ });
254
+ }
255
+ return t && k.set(e, t), e;
256
+ }, D = (e, t, n, r) => {
257
+ for (let [i, a, o] of n) {
258
+ let n = t.get(a), s = n === void 0 ? void 0 : o.read(n);
259
+ if (n !== void 0 && s === void 0) throw A(`${r} attribute ${a} ${j(n)} is invalid`);
260
+ s !== void 0 && (e[i] = s);
261
+ }
262
+ return e;
263
+ };
264
+ for (; c < t.length;) {
265
+ let [e, i, o] = h();
266
+ if (d = e, f = c + 12, p = f + i, d === "MAIN" || d === "XYZI") throw A(d === "MAIN" ? `has a second MAIN chunk at byte ${c}` : `has an XYZI chunk at byte ${c} without a SIZE before it`);
267
+ if (!n.has(d)) s.push({
268
+ id: d,
269
+ content: t.slice(f, p),
270
+ children: t.slice(p, p + o)
271
+ }), c = p + o;
272
+ else {
273
+ if (o) throw A(`${m()} has child chunks`);
274
+ if (d === "PACK") {
275
+ if (l >= 0) throw A(`has a second PACK chunk at byte ${c}`);
276
+ if (l = _(), l < 0) throw A(`${m()} counts ${l} models`);
277
+ } else if (d === "SIZE") {
278
+ let e = [
279
+ _(),
280
+ _(),
281
+ _()
282
+ ];
283
+ if (!e.every((e) => e >= 1 && e <= 256)) throw A(`${m()} has size ${e.join(" x ")}, not 1-256 per axis`);
284
+ if (f !== p) throw A(`${m()} has ${p - f} extra bytes`);
285
+ let n = c;
286
+ c = p;
287
+ let [i, o, s] = c < t.length ? h() : [
288
+ "",
289
+ 0,
290
+ 0
291
+ ];
292
+ if (i !== "XYZI") throw A(`chunk SIZE at byte ${n} is not followed by its XYZI chunk`);
293
+ let l = o >= 4 ? r.getInt32(c + 12, !0) : -1;
294
+ if (s || l < 0 || o !== 4 + 4 * l) throw A(`chunk XYZI at byte ${c} has ${o} content bytes, not 4 + 4 per voxel (and no children)`);
295
+ let u = t.slice(c + 16, c + 16 + 4 * l);
296
+ for (let t = 0; t < u.length; t += 4) if (u[t] >= e[0] || u[t + 1] >= e[1] || u[t + 2] >= e[2] || !u[t + 3]) throw A(`voxel ${t / 4} of model ${a.models.length} (${u.subarray(t, t + 4).join(", ")}) is outside ${e.join(" x ")} or has palette index 0`);
297
+ a.models.push({
298
+ size: e,
299
+ voxels: u
300
+ }), d = "SIZE", f = p = c + 12 + o;
301
+ } else if (d === "RGBA") {
302
+ if (u) throw A(`has a second RGBA chunk at byte ${c}`);
303
+ g(1024), u = !0;
304
+ for (let e = 0; e < 256; e++) a.palette.set(t.subarray(f + 4 * e, f + 4 * e + 4), 4 * (e + 1 & 255));
305
+ f += 1024;
306
+ } else if (d === "MATL") {
307
+ let e = {
308
+ id: _(),
309
+ attributes: w()
310
+ };
311
+ a.materials.push(D(e, e.attributes, x, `material ${e.id}`));
312
+ } else if (d === "LAYR") {
313
+ let e = {
314
+ id: _(),
315
+ attributes: w()
316
+ };
317
+ _(), a.layers.push(D(e, e.attributes, v, `layer ${e.id}`));
318
+ } else {
319
+ let e = _(), t = w(), n;
320
+ if (d === "nTRN") {
321
+ let r = _();
322
+ _(), n = {
323
+ id: e,
324
+ type: "transform",
325
+ child: r,
326
+ layer: _(),
327
+ frames: Array.from({ length: S(1, 4, "frames") }, (t, n) => {
328
+ let r = w();
329
+ return D({ attributes: r }, r, y, `node ${e} frame ${n}`);
330
+ }),
331
+ attributes: t
332
+ };
333
+ } else n = d === "nGRP" ? {
334
+ id: e,
335
+ type: "group",
336
+ children: Array.from({ length: S(0, 4, "children") }, _),
337
+ attributes: t
338
+ } : {
339
+ id: e,
340
+ type: "shape",
341
+ models: Array.from({ length: S(1, 8, "models") }, (t, n) => {
342
+ let r = _(), i = w();
343
+ return D({
344
+ model: r,
345
+ attributes: i
346
+ }, i, b, `node ${e} model ${n}`);
347
+ }),
348
+ attributes: t
349
+ };
350
+ a.nodes.push(D(n, t, v, `node ${e}`));
351
+ }
352
+ if (f !== p) throw A(`${m()} has ${p - f} bytes after its content`);
353
+ s.push({ id: d }), c = p;
354
+ }
355
+ }
356
+ if (l >= 0 && l !== a.models.length) throw A(`PACK counts ${l} models but ${a.models.length} follow`);
357
+ return N(a.nodes, a.models.length, A), a;
358
+ }
359
+ function N(e, t, n) {
360
+ let r = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map();
361
+ for (let t of e) {
362
+ if (r.has(t.id)) throw n(`has two nodes with id ${t.id}`);
363
+ r.set(t.id, t);
364
+ }
365
+ let a = (e, t, a) => {
366
+ let o = r.get(t);
367
+ if (!o || o.type === "transform" !== a) throw n(`node ${e} has child ${t}, which is not a ${a ? "transform" : "group or shape"} node`);
368
+ if (i.has(t)) throw n(`node ${t} has two parents, ${i.get(t)} and ${e}`);
369
+ i.set(t, e);
370
+ };
371
+ for (let r of e) if (r.type === "transform") a(r.id, r.child, !1);
372
+ else if (r.type === "group") for (let e of r.children) a(r.id, e, !0);
373
+ else for (let { model: e } of r.models) if (!(Number.isInteger(e) && e >= 0 && e < t)) throw n(`has shape node ${r.id} showing model ${e} of ${t}`);
374
+ let o = /* @__PURE__ */ new Set();
375
+ for (let t of e) {
376
+ let r = [];
377
+ for (let a = t.id; a !== void 0 && !o.has(a); a = i.get(a)) if (r.push(a), r.length > e.length) throw n(`has node ${t.id} on a cycle of parents`);
378
+ for (let e of r) o.add(e);
379
+ }
380
+ }
381
+ function P(e, t = {}) {
382
+ let { maxBytes: n = 1048576, maxEntries: r = 4096 } = t;
383
+ for (let [e, t] of [["maxBytes", n], ["maxEntries", r]]) if (!Number.isSafeInteger(t) || t < 0) throw RangeError(`Vox render limit ${e} ${t} is invalid; pass an integer >= 0`);
384
+ if (!e || typeof e != "object") throw TypeError("readVoxRenderDictionaries needs the scene parseVox returns");
385
+ let a = e.chunks ?? [];
386
+ if (!Array.isArray(a)) throw TypeError("Vox scene chunks must be an array, as parseVox returns");
387
+ let o = {
388
+ objects: [],
389
+ cameras: [],
390
+ diagnostics: []
391
+ };
392
+ return a.forEach((e, t) => {
393
+ let a = e?.id;
394
+ if (a !== "rOBJ" && a !== "rCAM") return;
395
+ let s = (e, n) => o.diagnostics.push({
396
+ kind: e,
397
+ id: a,
398
+ chunk: t,
399
+ message: `${a} chunk ${t} ${n}`
400
+ }), c = e.content instanceof Uint8Array ? e.content : i;
401
+ if (c.length > n) {
402
+ s("unsupported", `has ${c.length} bytes, over maxBytes ${n}; it is kept, not read`);
403
+ return;
404
+ }
405
+ let l = new DataView(c.buffer, c.byteOffset, c.byteLength), u = 0, d = (e) => /* @__PURE__ */ RangeError(`${e} at byte ${u}`), f = () => {
406
+ if (c.length - u < 4) throw d("is truncated");
407
+ return u += 4, l.getInt32(u - 4, !0);
408
+ }, p = () => {
409
+ let e = f();
410
+ if (e < 0 || e > c.length - u) throw d(`has a string of ${e} bytes past its content`);
411
+ return u += e, E(c.subarray(u - e, u));
412
+ };
413
+ try {
414
+ let t = a === "rCAM" ? f() : 0, n = f();
415
+ if (n < 0 || n > (c.length - u) / 8) throw d(`counts ${n} attributes, more than its bytes hold`);
416
+ if (n > r) {
417
+ s("unsupported", `counts ${n} attributes, over maxEntries ${r}; it is kept, not read`);
418
+ return;
419
+ }
420
+ let i = /* @__PURE__ */ new Map();
421
+ for (let e = n; e--;) {
422
+ let e = p();
423
+ if (i.has(e)) throw d(`has attribute ${j(e)} twice`);
424
+ i.set(e, p());
425
+ }
426
+ if (u !== c.length) throw d(`has ${c.length - u} bytes after its dictionary`);
427
+ a === "rOBJ" ? o.objects.push(i) : o.cameras.push({
428
+ id: t,
429
+ attributes: i
430
+ }), e.children?.length && s("unsupported", `has ${e.children.length} bytes of child chunks; they are kept, not read`);
431
+ } catch (e) {
432
+ if (!(e instanceof RangeError)) throw e;
433
+ s("malformed", `${e.message}; it is kept and gives no dictionary`);
434
+ }
435
+ }), o;
436
+ }
437
+ var F = class {
438
+ constructor() {
439
+ this.bytes = /* @__PURE__ */ new Uint8Array(4096), this.view = new DataView(this.bytes.buffer), this.at = 0;
440
+ }
441
+ room(e) {
442
+ if (this.at + e <= this.bytes.length) return;
443
+ if (this.at + e > 2147483647) throw I("is over the format's 2 GiB");
444
+ let t = new Uint8Array(Math.min(2147483647, Math.max(2 * this.bytes.length, this.at + e)));
445
+ t.set(this.bytes.subarray(0, this.at)), this.bytes = t, this.view = new DataView(t.buffer);
446
+ }
447
+ push(e) {
448
+ this.room(e.length), this.bytes.set(e, this.at), this.at += e.length;
449
+ }
450
+ int(...e) {
451
+ this.room(4 * e.length);
452
+ for (let t of e) this.view.setInt32(this.at, t, !0), this.at += 4;
453
+ }
454
+ text(e, t = !1) {
455
+ if (e.length > 64 || /[^\0-\x7f]/.test(e)) {
456
+ let n = t && O(e) || w.encode(e);
457
+ this.int(n.length), this.push(n);
458
+ } else {
459
+ this.int(e.length), this.room(e.length);
460
+ for (let t = 0; t < e.length; t++) this.bytes[this.at++] = e.charCodeAt(t);
461
+ }
462
+ }
463
+ dict(e) {
464
+ let t = k.get(e);
465
+ this.int(e.size);
466
+ for (let [n, r] of e) {
467
+ let e = t?.get(n);
468
+ this.text(n, e?.key), this.text(r, e?.value === r);
469
+ }
470
+ }
471
+ open(e) {
472
+ this.room(4);
473
+ for (let t = 0; t < 4; t++) this.bytes[this.at++] = e.charCodeAt(t);
474
+ return this.int(0, 0), this.at - 8;
475
+ }
476
+ close(e, t = i) {
477
+ this.view.setInt32(e, this.at - e - 8, !0), this.push(t), this.view.setInt32(e + 4, t.length, !0);
478
+ }
479
+ }, I = (e) => /* @__PURE__ */ RangeError(`Vox scene ${e}; fix the scene first`), L = (e, t) => {
480
+ if (!s(e)) throw c(t, e, "number", "a 32-bit integer");
481
+ return e;
482
+ }, R = (e, t, n = 0) => {
483
+ if (!Array.isArray(e) || e.length < n) throw TypeError(`${t} must be an array${n ? ` of at least ${n}` : ""}`);
484
+ return e;
485
+ };
486
+ function z(e, t, n) {
487
+ if (!e || typeof e != "object") throw TypeError(`${n} must be an object`);
488
+ let r = e, i = r.attributes;
489
+ if (i !== void 0 && !(i instanceof Map)) throw TypeError(`${n} attributes must be a Map of strings`);
490
+ let a = new Map(i), o = i && k.get(i);
491
+ o && k.set(a, o);
492
+ for (let [e, t] of a) if (typeof e != "string" || typeof t != "string") throw TypeError(`${n} attributes must be strings`);
493
+ for (let [e, i, o] of t) {
494
+ let t = r[e];
495
+ if (t === void 0) {
496
+ a.delete(i);
497
+ continue;
498
+ }
499
+ let s = o.write(t, `${n} ${e}`), c = a.get(i), l = c === void 0 ? void 0 : o.read(c);
500
+ (l === void 0 || o.write(l, "") !== s) && a.set(i, s);
501
+ }
502
+ return a;
503
+ }
504
+ function B(e, t) {
505
+ let n = R(e, "Vox scene nodes");
506
+ for (let e of n) {
507
+ if (!e || typeof e != "object" || !Object.hasOwn(r, e.type)) throw TypeError("Vox scene node type must be 'transform', 'group' or 'shape'");
508
+ let t = `Vox scene node ${L(e.id, "Vox scene node id")}`;
509
+ if (e.type === "transform") {
510
+ L(e.child, `${t} child`), L(e.layer, `${t} layer`);
511
+ for (let n of R(e.frames, `${t} frames`, 1)) if (!n || typeof n != "object") throw TypeError(`${t} frames must be objects`);
512
+ } else if (e.type === "group") for (let n of R(e.children, `${t} children`)) L(n, `${t} child`);
513
+ else for (let n of R(e.models, `${t} models`, 1)) if (!n || typeof n != "object") throw TypeError(`${t} models must be {model} objects`);
514
+ }
515
+ return N(n, t, I), n;
516
+ }
517
+ function V(e, t) {
518
+ if (!e || typeof e != "object") throw TypeError(`${t} must be {size, voxels}`);
519
+ let { size: n, voxels: r } = e;
520
+ if (!Array.isArray(n) || n.length !== 3 || !n.every((e) => (e & 511) === e && e >= 1 && e <= 256)) throw RangeError(`${t} size must be 3 integers in 1-256`);
521
+ if (!(r instanceof Uint8Array) || r.length % 4) throw TypeError(`${t} voxels must be a Uint8Array of [x, y, z, palette index] quadruples`);
522
+ for (let e = 0; e < r.length; e += 4) if (r[e] >= n[0] || r[e + 1] >= n[1] || r[e + 2] >= n[2] || !r[e + 3]) throw RangeError(`${t} voxel ${e / 4} is outside the size or has palette index 0; keep x, y, z below size and indices 1-255`);
523
+ }
524
+ function H(e) {
525
+ if (!e || typeof e != "object") throw TypeError("writeVox expects a VoxScene, such as parseVox() returns");
526
+ let t = L(e.version, "Vox scene version"), a = R(e.models, "Vox scene models"), s = B(e.nodes, a.length), c = R(e.layers, "Vox scene layers"), l = R(e.materials, "Vox scene materials"), u = e.chunks === void 0 ? [] : R(e.chunks, "Vox scene chunks");
527
+ G(e.palette);
528
+ let d = new F(), f = {
529
+ models: 0,
530
+ nodes: 0,
531
+ layers: 0,
532
+ materials: 0,
533
+ palette: 0,
534
+ pack: 0
535
+ }, p = {
536
+ models: a.length,
537
+ nodes: s.length,
538
+ layers: c.length,
539
+ materials: l.length,
540
+ palette: 1,
541
+ pack: 0
542
+ };
543
+ d.push(U("VOX ")), d.int(t), d.push(U("MAIN")), d.int(0, 0);
544
+ let m = (t) => {
545
+ let n = f[t]++;
546
+ if (t === "models") {
547
+ let e = a[n];
548
+ V(e, `Vox scene model ${n}`);
549
+ let t = d.open("SIZE");
550
+ d.int(...e.size), d.close(t), t = d.open("XYZI"), d.int(e.voxels.length / 4), d.push(e.voxels), d.close(t);
551
+ } else if (t === "nodes") {
552
+ let e = s[n], t = `Vox scene node ${e.id}`, i = d.open(r[e.type]);
553
+ if (d.int(e.id), d.dict(z(e, v, t)), e.type === "transform") d.int(e.child, -1, e.layer, e.frames.length), e.frames.forEach((e, n) => d.dict(z(e, y, `${t} frame ${n}`)));
554
+ else if (e.type === "group") {
555
+ d.int(e.children.length);
556
+ for (let t of e.children) d.int(t);
557
+ } else d.int(e.models.length), e.models.forEach((e, n) => {
558
+ d.int(e.model), d.dict(z(e, b, `${t} model ${n}`));
559
+ });
560
+ d.close(i);
561
+ } else if (t === "layers" || t === "materials") {
562
+ let e = t === "layers" ? c[n] : l[n], r = `Vox scene ${t === "layers" ? "layer" : "material"} ${n}`, i = z(e, t === "layers" ? v : x, r), a = d.open(t === "layers" ? "LAYR" : "MATL");
563
+ d.int(L(e.id, `${r} id`)), d.dict(i), t === "layers" && d.int(-1), d.close(a);
564
+ } else if (t === "palette") {
565
+ let t = d.open("RGBA");
566
+ for (let t = 0; t < 256; t++) d.push(e.palette.subarray(4 * (t + 1 & 255), 4 * (t + 1 & 255) + 4));
567
+ d.close(t);
568
+ } else {
569
+ let e = d.open("PACK");
570
+ d.int(a.length), d.close(e);
571
+ }
572
+ }, h = /* @__PURE__ */ new Map();
573
+ u.forEach((e, t) => {
574
+ let r = n.get(e?.id);
575
+ r && h.set(r, t);
576
+ }), u.forEach((e, t) => {
577
+ let r = n.get(e?.id);
578
+ if (r) {
579
+ if (e.content || e.children) throw TypeError(`Vox scene chunk ${e.id} is written from the scene's fields; drop its content and children`);
580
+ if (r === "pack" || r === "palette") {
581
+ if (f[r]) throw I(`chunks list ${e.id} twice`);
582
+ m(r);
583
+ } else if (f[r] < p[r] && m(r), t === h.get(r)) for (; f[r] < p[r];) m(r);
584
+ } else {
585
+ let { id: n, content: r = i, children: a = i } = e ?? {};
586
+ if (typeof n != "string" || n.length !== 4 || /[^\0-\xff]/.test(n) || n === "MAIN" || n === "XYZI") throw TypeError(`Vox scene chunk ${t} id ${JSON.stringify(n)} must be 4 Latin-1 characters other than MAIN and XYZI`);
587
+ if (!(r instanceof Uint8Array) || !(a instanceof Uint8Array)) throw TypeError(`Vox scene chunk ${n} content and children must be Uint8Arrays`);
588
+ let o = d.open(n);
589
+ d.push(r), d.close(o, a);
590
+ }
591
+ });
592
+ for (let t of [
593
+ "models",
594
+ "nodes",
595
+ "layers",
596
+ "palette",
597
+ "materials"
598
+ ]) if (!(t === "palette" && (f.palette || e.chunks && W(e.palette, o)))) for (; f[t] < p[t];) m(t);
599
+ return d.view.setInt32(16, d.at - 20, !0), d.bytes.slice(0, d.at);
600
+ }
601
+ var U = (e) => Uint8Array.from(e, (e) => e.charCodeAt(0)), W = (e, t) => e.length === t.length && e.every((e, n) => e === t[n]), G = (e) => {
602
+ if (!(e instanceof Uint8Array) || e.length !== 1024) throw TypeError("Vox scene palette must be a Uint8Array of 1024 bytes (256 RGBA colours)");
603
+ }, K = (e, t) => Array.from({ length: 9 }, (n, r) => e[r - r % 3] * t[r % 3] + e[r - r % 3 + 1] * t[3 + r % 3] + e[r - r % 3 + 2] * t[6 + r % 3]);
604
+ function q(e, t, n) {
605
+ let r = {
606
+ model: e,
607
+ source: [],
608
+ sign: [],
609
+ base: []
610
+ };
611
+ for (let [i, a] of [
612
+ [0, !1],
613
+ [2, !1],
614
+ [1, !0]
615
+ ]) {
616
+ let o = [
617
+ 0,
618
+ 1,
619
+ 2
620
+ ].find((e) => t[3 * i + e] !== 0) ?? 0, s = t[3 * i + o] < 0, c = e.size[o], l = n[i] - Math.floor(c / 2) + (s ? c - 1 : 0);
621
+ r.source.push(o), r.sign.push((s ? -1 : 1) * (a ? -1 : 1)), r.base.push(a ? -1 - l : l);
622
+ }
623
+ return r;
624
+ }
625
+ function J(e) {
626
+ let t = R(e.models, "Vox scene models"), n = B(e.nodes, t.length);
627
+ if (t.forEach((e, t) => V(e, `Vox scene model ${t}`)), !n.length) return t.length ? [q(t[0], a, [
628
+ 0,
629
+ 0,
630
+ 0
631
+ ])] : [];
632
+ let r = new Map(n.map((e) => [e.id, e])), i = new Set(n.flatMap((e) => e.type === "transform" ? [e.child] : e.type === "group" ? e.children : [])), o = new Set(R(e.layers, "Vox scene layers").flatMap((e) => e?.hidden ? [e.id] : [])), s = [], c = n.filter((e) => !i.has(e.id)).map((e) => [
633
+ e,
634
+ a,
635
+ [
636
+ 0,
637
+ 0,
638
+ 0
639
+ ]
640
+ ]);
641
+ for (let e = c.pop(); e; e = c.pop()) {
642
+ let [n, i, l] = e;
643
+ if (!n.hidden) {
644
+ if (n.type === "transform") {
645
+ if (o.has(n.layer)) continue;
646
+ let e = n.frames[0], t = e.rotation ?? a, s = e.translation ?? [
647
+ 0,
648
+ 0,
649
+ 0
650
+ ];
651
+ g(t, `Vox scene node ${n.id} rotation`), m.write(s, `Vox scene node ${n.id} translation`);
652
+ let u = r.get(n.child), d = [
653
+ 0,
654
+ 1,
655
+ 2
656
+ ].map((e) => i[3 * e] * s[0] + i[3 * e + 1] * s[1] + i[3 * e + 2] * s[2] + l[e]);
657
+ c.push([
658
+ u,
659
+ K(i, t),
660
+ d
661
+ ]);
662
+ } else if (n.type === "group") for (let e = n.children.length; e--;) c.push([
663
+ r.get(n.children[e]),
664
+ i,
665
+ l
666
+ ]);
667
+ else s.push(q(t[n.models[0].model], i, l));
668
+ }
669
+ }
670
+ return s;
671
+ }
672
+ function Y(e, n, r, i) {
673
+ if (!(e instanceof t)) throw TypeError("pasteVoxModel needs a VoxelWorld to write into");
674
+ if (!Array.isArray(r) || r.length !== 3 || !r.every((e) => typeof e == "number")) throw TypeError("pasteVoxModel at must be a cell [x, y, z]");
675
+ if (!r.every((e) => s(e))) throw RangeError(`pasteVoxModel at [${r.join(", ")}] must be 3 32-bit integers; floor it`);
676
+ if (!n || typeof n != "object") throw TypeError("pasteVoxModel model must be a VoxModel or a VoxScene");
677
+ let o = "models" in n ? n : void 0;
678
+ if (!o && i === void 0) throw TypeError("pasteVoxModel needs a mapping (palette index -> block state) for a bare model; pass one, or pass the scene");
679
+ if (i !== void 0 && (!i || typeof i != "object" || !Number.isInteger(i.length))) throw TypeError("pasteVoxModel mapping must be an array of block states indexed by palette index");
680
+ o ? i === void 0 && G(o.palette) : V(n, "Vox model");
681
+ let c = o ? J(o) : [q(n, a, [
682
+ 0,
683
+ 0,
684
+ 0
685
+ ])], l = /* @__PURE__ */ new Uint8Array(256), u = [
686
+ Infinity,
687
+ Infinity,
688
+ Infinity
689
+ ], d = [
690
+ -Infinity,
691
+ -Infinity,
692
+ -Infinity
693
+ ];
694
+ for (let e of c) {
695
+ for (let t = 3; t < e.model.voxels.length; t += 4) l[e.model.voxels[t]] = 1;
696
+ for (let t = 0; t < 3; t++) {
697
+ let n = e.sign[t] * (e.model.size[e.source[t]] - 1);
698
+ u[t] = Math.min(u[t], e.base[t] + Math.min(0, n)), d[t] = Math.max(d[t], e.base[t] + Math.max(0, n));
699
+ }
700
+ }
701
+ if (!c.length) return 0;
702
+ let f = r.map((e, t) => e - u[t]);
703
+ if (!s(d[0] + f[0]) || !s(d[2] + f[2]) || d[1] + f[1] > e.maxY || r[1] < e.minY) throw RangeError(`Vox paste box [${r.join(", ")}]..[${d.map((e, t) => e + f[t]).join(", ")}] leaves the world (y ${e.minY}..${e.maxY}); move it`);
704
+ let p = /* @__PURE__ */ new Uint32Array(256);
705
+ for (let t = 1; t < 256; t++) {
706
+ if (!l[t] || i === void 0) continue;
707
+ let n = i[t];
708
+ if (n >>> 0 !== n || (n & 65535) >= e.blocks.table.length) throw RangeError(`pasteVoxModel mapping[${t}] ${n} is not a block state of world.blocks; define the type first`);
709
+ p[t] = n;
710
+ }
711
+ if (i === void 0) {
712
+ let t = o.palette, n = new Map(e.blocks.table.map((e) => [e.name, e.id])), r = new Map(o.materials?.map((e) => [e.id, e]));
713
+ for (let i = 1; i < 256; i++) {
714
+ if (!l[i]) continue;
715
+ let a = Array.from(t.subarray(4 * i, 4 * i + 3), (e) => e.toString(16).padStart(2, "0")).join(""), o = r.get(i), s = o ? Q(o) : {}, c = [`vox:${a}`, ...Object.entries(s).map(([e, t]) => `${e}=${t}`)].join(":");
716
+ p[i] = n.get(c) ?? e.blocks.define(c, {
717
+ ...s,
718
+ color: `#${a}`
719
+ }), n.set(c, p[i]);
720
+ }
721
+ }
722
+ return Z(e, c, f, p);
723
+ }
724
+ function X(e, t) {
725
+ let n = [
726
+ Infinity,
727
+ Infinity,
728
+ Infinity,
729
+ -Infinity,
730
+ -Infinity,
731
+ -Infinity
732
+ ];
733
+ for (let r of e) for (let e = 0; e < 3; e++) {
734
+ let i = r.sign[e] * (r.model.size[r.source[e]] - 1), a = r.base[e] + t[e] + Math.min(0, i), o = r.base[e] + t[e] + Math.max(0, i);
735
+ n[e] = Math.min(n[e], a >> 5), n[e + 3] = Math.max(n[e + 3], o >> 5);
736
+ }
737
+ return n;
738
+ }
739
+ function Z(t, n, r, i) {
740
+ let [a, o, s, c, l, u] = X(n, r), d = c - a + 1, f = l - o + 1, p = u - s + 1, m = d * f * p;
741
+ if (n.length > 1 && m > n.reduce((e, t) => e + t.model.voxels.length, 0) + 4096) {
742
+ let e = 0;
743
+ for (let a of n) e += Z(t, [a], r, i);
744
+ return e;
745
+ }
746
+ let h = n.reduce((e, t) => e + t.model.voxels.length / 4, 0), g = new Int32Array(h), _ = new Uint16Array(h), v = new Uint8Array(h), y = new Uint32Array(m + 1), b = 0;
747
+ for (let { model: e, source: t, sign: c, base: l } of n) {
748
+ let n = e.voxels, u = t[0], d = t[1], m = t[2], h = c[0] < 0 ? -1 : 0, x = c[1] < 0 ? -1 : 0, S = c[2] < 0 ? -1 : 0, C = l[0] + r[0], w = l[1] + r[1], T = l[2] + r[2];
749
+ for (let e = 0; e < n.length; e += 4, b++) {
750
+ let t = n[e + 3];
751
+ if (!i[t]) {
752
+ g[b] = -1;
753
+ continue;
754
+ }
755
+ let r = C + ((n[e + u] ^ h) - h), c = w + ((n[e + d] ^ x) - x), l = T + ((n[e + m] ^ S) - S), E = (((r >> 5) - a) * f + (c >> 5) - o) * p + (l >> 5) - s;
756
+ g[b] = E, _[b] = r & 31 | (l & 31) << 5 | (c & 31) << 10, v[b] = t, y[E + 1]++;
757
+ }
758
+ }
759
+ for (let e = 0; e < m; e++) y[e + 1] += y[e];
760
+ if (!y[m]) return 0;
761
+ let x = new Uint16Array(y[m]), S = new Uint8Array(y[m]);
762
+ for (let e = 0; e < h; e++) {
763
+ let t = g[e];
764
+ if (t < 0) continue;
765
+ let n = y[t]++;
766
+ x[n] = _[e], S[n] = v[e];
767
+ }
768
+ let C = 0;
769
+ for (let n = 0, r = 0; n < m; r = y[n++]) y[n] !== r && (C += e(t, a + Math.floor(n / (f * p)), o + Math.floor(n / p) % f, s + n % p, x, S, i, r, y[n]));
770
+ return C;
771
+ }
772
+ function Q(e) {
773
+ let t = e.type ?? "diffuse", n = e.weight ?? 1, r = (t) => {
774
+ let n = Number(e.attributes?.get(t));
775
+ return Number.isFinite(n) ? Math.min(1, Math.max(0, n)) : void 0;
776
+ }, i = r("_trans") ?? r("_alpha") ?? (t === "glass" ? n : void 0), a = {};
777
+ return e.rough !== void 0 && (t === "metal" || t === "glass" || t === "blend") && (a.roughness = Math.min(1, Math.max(0, e.rough))), (t === "metal" || t === "blend") && (a.metalness = r("_metal") ?? n), t === "emit" && (a.emissive = n * (1 + Math.max(0, e.flux ?? 0))), (t === "glass" || t === "blend") && i && (a.layer = "translucent", a.transmission = i), t === "media" && (a.layer = "translucent", a.scatter = n), a;
778
+ }
779
+ //#endregion
780
+ export { M as parseVox, Y as pasteVoxModel, P as readVoxRenderDictionaries, Q as voxBlockOptions, H as writeVox };