@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.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +445 -2
- package/dist/animation.d.ts +50 -0
- package/dist/animation.js +88 -0
- package/dist/assist.d.ts +24 -0
- package/dist/assist.js +52 -0
- package/dist/brush-gpu.d.ts +62 -0
- package/dist/bytes.d.ts +55 -0
- package/dist/collab.d.ts +77 -0
- package/dist/collab.js +159 -0
- package/dist/edit.d.ts +30 -0
- package/dist/edit.js +361 -0
- package/dist/export.d.ts +58 -0
- package/dist/export.js +52 -0
- package/dist/file-colors-DHT5vrzW.js +55 -0
- package/dist/file-colors.d.ts +16 -0
- package/dist/format-plist.d.ts +4 -0
- package/dist/formats-extra.d.ts +20 -0
- package/dist/formats-extra.js +335 -0
- package/dist/formats.d.ts +96 -0
- package/dist/formats.js +924 -0
- package/dist/generate.d.ts +63 -0
- package/dist/generate.js +103 -0
- package/dist/gox.d.ts +60 -0
- package/dist/gpu.d.ts +74 -0
- package/dist/gpu.js +167 -0
- package/dist/grid-B2V0TGLV.js +120 -0
- package/dist/grid.d.ts +75 -0
- package/dist/history.d.ts +65 -0
- package/dist/history.js +75 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +631 -0
- package/dist/layout.d.ts +76 -0
- package/dist/library.d.ts +50 -0
- package/dist/library.js +89 -0
- package/dist/mesh.d.ts +128 -0
- package/dist/mesh.js +365 -0
- package/dist/model.d.ts +173 -0
- package/dist/pick.d.ts +34 -0
- package/dist/pick.js +122 -0
- package/dist/raster.d.ts +89 -0
- package/dist/rig.d.ts +88 -0
- package/dist/rig.js +216 -0
- package/dist/select.d.ts +50 -0
- package/dist/select.js +94 -0
- package/dist/shaders-BaY4gcm9.js +234 -0
- package/dist/shaders.d.ts +116 -0
- package/dist/shaders.js +1 -0
- package/dist/slices.d.ts +75 -0
- package/dist/smooth.d.ts +43 -0
- package/dist/smooth.js +212 -0
- package/dist/sparse.d.ts +239 -0
- package/dist/sparse.js +646 -0
- package/dist/stamp.d.ts +88 -0
- package/dist/vmax.d.ts +48 -0
- package/dist/vox-resolve.d.ts +64 -0
- package/dist/vox-scene.d.ts +51 -0
- package/dist/vox.d.ts +30 -0
- package/dist/vox.js +684 -0
- package/dist/voxelize.d.ts +70 -0
- package/dist/voxelize.js +249 -0
- package/dist/vxm.d.ts +54 -0
- package/package.json +130 -4
package/dist/vox.js
ADDED
|
@@ -0,0 +1,684 @@
|
|
|
1
|
+
import { c as e, l as t, n, o as r, t as i } from "./grid-B2V0TGLV.js";
|
|
2
|
+
import { SparseGrid as a } from "./sparse.js";
|
|
3
|
+
import { MargoError as o, Matrix4 as s } from "@margoengine/math";
|
|
4
|
+
//#region src/vox-resolve.ts
|
|
5
|
+
var c = [
|
|
6
|
+
1,
|
|
7
|
+
0,
|
|
8
|
+
0,
|
|
9
|
+
0,
|
|
10
|
+
1,
|
|
11
|
+
0,
|
|
12
|
+
0,
|
|
13
|
+
0,
|
|
14
|
+
1
|
|
15
|
+
];
|
|
16
|
+
function l(e) {
|
|
17
|
+
let t = e.frame ?? 0;
|
|
18
|
+
if (!Number.isSafeInteger(t) || t < 0) throw RangeError("A .vox key frame is not a whole number from 0; fix the key");
|
|
19
|
+
return t;
|
|
20
|
+
}
|
|
21
|
+
function u(e, t) {
|
|
22
|
+
let n = e[0], r;
|
|
23
|
+
for (let i of e) {
|
|
24
|
+
let e = l(i);
|
|
25
|
+
e < l(n) && (n = i), e <= t && (!r || e > l(r)) && (r = i);
|
|
26
|
+
}
|
|
27
|
+
return r ?? n;
|
|
28
|
+
}
|
|
29
|
+
function d(e = c) {
|
|
30
|
+
if (!Array.isArray(e) || e.length !== 9 || e.some((e) => e !== 0 && e !== 1 && e !== -1) || [
|
|
31
|
+
0,
|
|
32
|
+
1,
|
|
33
|
+
2
|
|
34
|
+
].some((t) => Math.abs(e[t * 3]) + Math.abs(e[t * 3 + 1]) + Math.abs(e[t * 3 + 2]) !== 1 || Math.abs(e[t]) + Math.abs(e[3 + t]) + Math.abs(e[6 + t]) !== 1)) throw RangeError("A .vox rotation is not a row-major signed axis permutation; fix the key");
|
|
35
|
+
return e;
|
|
36
|
+
}
|
|
37
|
+
var f = (e) => e.type === "transform" ? [e.child] : e.type === "group" ? e.children : [];
|
|
38
|
+
function p(e) {
|
|
39
|
+
if (typeof e != "object" || !e || !Array.isArray(e.nodes) || !Array.isArray(e.models)) throw TypeError("A .vox scene is missing nodes or models; pass parseVox output");
|
|
40
|
+
let t = new Map(e.nodes.map((e) => [e.id, e])), n = /* @__PURE__ */ new Set();
|
|
41
|
+
if (t.size !== e.nodes.length) throw RangeError("The .vox scene repeats a node id; give each node its own");
|
|
42
|
+
for (let r of e.nodes) {
|
|
43
|
+
for (let e of f(r)) {
|
|
44
|
+
if (!t.has(e)) throw RangeError(`.vox node ${r.id} links missing node ${e}; fix the link`);
|
|
45
|
+
if (n.has(e)) throw RangeError(`.vox node ${e} has two parents; share models instead`);
|
|
46
|
+
n.add(e);
|
|
47
|
+
}
|
|
48
|
+
if (r.type === "transform") {
|
|
49
|
+
if (!Array.isArray(r.frames) || !r.frames.length) throw RangeError(`.vox transform ${r.id} has no keys; give it one`);
|
|
50
|
+
for (let e of r.frames) {
|
|
51
|
+
l(e), d(e.rotation);
|
|
52
|
+
let t = e.translation;
|
|
53
|
+
if (t && (!Array.isArray(t) || t.length !== 3 || t.some((e) => !Number.isSafeInteger(e)))) throw RangeError(`.vox transform ${r.id} has a translation that is not three whole numbers; export the model with whole-voxel translations`);
|
|
54
|
+
}
|
|
55
|
+
} else if (r.type === "shape") {
|
|
56
|
+
if (!Array.isArray(r.models) || !r.models.length) throw RangeError(`.vox shape ${r.id} has no model keys; give it one`);
|
|
57
|
+
for (let t of r.models) if (l(t), !Number.isInteger(t.model) || t.model < 0 || t.model >= e.models.length) throw RangeError(`.vox shape ${r.id} uses missing model ${t.model}; point it at an existing model`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
let r = e.nodes.filter((e) => !n.has(e.id)), i = /* @__PURE__ */ new Set(), a = [...r];
|
|
61
|
+
for (; a.length;) {
|
|
62
|
+
let e = a.pop();
|
|
63
|
+
i.add(e.id);
|
|
64
|
+
for (let n of f(e)) a.push(t.get(n));
|
|
65
|
+
}
|
|
66
|
+
if (i.size !== t.size) throw RangeError("The .vox node links form a cycle; fix the links");
|
|
67
|
+
return {
|
|
68
|
+
nodes: t,
|
|
69
|
+
roots: r
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function m(e, t, n, r) {
|
|
73
|
+
if (!Array.isArray(e) || e.length !== 3 || e.some((e) => !Number.isInteger(e) || e < 1 || e > 256)) throw RangeError("A .vox model size is not 1 to 256 cells per axis; fix the model");
|
|
74
|
+
let i = [
|
|
75
|
+
[
|
|
76
|
+
1,
|
|
77
|
+
0,
|
|
78
|
+
0
|
|
79
|
+
],
|
|
80
|
+
[
|
|
81
|
+
0,
|
|
82
|
+
0,
|
|
83
|
+
-1
|
|
84
|
+
],
|
|
85
|
+
[
|
|
86
|
+
0,
|
|
87
|
+
1,
|
|
88
|
+
0
|
|
89
|
+
]
|
|
90
|
+
], a = i.map((e, t) => [
|
|
91
|
+
0,
|
|
92
|
+
1,
|
|
93
|
+
2
|
|
94
|
+
].map((e) => i[e][t])), o = [
|
|
95
|
+
0,
|
|
96
|
+
1,
|
|
97
|
+
2
|
|
98
|
+
].map((e) => [
|
|
99
|
+
0,
|
|
100
|
+
1,
|
|
101
|
+
2
|
|
102
|
+
].map((n) => t[e * 3] * i[0][n] + t[e * 3 + 1] * i[1][n] + t[e * 3 + 2] * i[2][n])), s = [
|
|
103
|
+
0,
|
|
104
|
+
1,
|
|
105
|
+
2
|
|
106
|
+
].map((i) => {
|
|
107
|
+
let a = [
|
|
108
|
+
0,
|
|
109
|
+
1,
|
|
110
|
+
2
|
|
111
|
+
].find((e) => t[i * 3 + e] !== 0), o = t[i * 3 + a], s = Math.floor(e[a] / 2), c = r === "box-v1" ? -s + (o < 0 ? e[a] : 0) : -o * s;
|
|
112
|
+
return t[i * 3 + 1] * e[1] + n[i] + c;
|
|
113
|
+
}), c = Array(16).fill(0);
|
|
114
|
+
for (let e = 0; e < 3; e++) {
|
|
115
|
+
for (let t = 0; t < 3; t++) c[t * 4 + e] = a[e][0] * o[0][t] + a[e][1] * o[1][t] + a[e][2] * o[2][t];
|
|
116
|
+
c[12 + e] = a[e][0] * s[0] + a[e][1] * s[1] + a[e][2] * s[2];
|
|
117
|
+
}
|
|
118
|
+
return c[15] = 1, c.map((e) => e || 0);
|
|
119
|
+
}
|
|
120
|
+
function h(e, t) {
|
|
121
|
+
if (typeof t != "object" || !t) throw TypeError("resolveVoxScene options are missing; pass {centering: 'box-v1' | 'pivot-v1', frame?}");
|
|
122
|
+
let n = t.frame ?? 0, r = t.centering;
|
|
123
|
+
if (r !== "box-v1" && r !== "pivot-v1") throw TypeError("resolveVoxScene centering is unknown; pass 'box-v1' or 'pivot-v1'");
|
|
124
|
+
if (!Number.isSafeInteger(n) || n < 0) throw RangeError("resolveVoxScene frame is not a whole number from 0; pass one");
|
|
125
|
+
let { nodes: i, roots: a } = p(e), o = new Map((e.layers ?? []).map((e) => [e.id, e])), s = [], l = a.map((e) => ({
|
|
126
|
+
node: e,
|
|
127
|
+
rotation: c,
|
|
128
|
+
translation: [
|
|
129
|
+
0,
|
|
130
|
+
0,
|
|
131
|
+
0
|
|
132
|
+
],
|
|
133
|
+
visible: !0,
|
|
134
|
+
transforms: [],
|
|
135
|
+
layers: []
|
|
136
|
+
})).reverse();
|
|
137
|
+
for (; l.length;) {
|
|
138
|
+
let t = l.pop(), a = t.node, c = {
|
|
139
|
+
...t,
|
|
140
|
+
visible: t.visible && !a.hidden,
|
|
141
|
+
name: a.name ?? t.name
|
|
142
|
+
};
|
|
143
|
+
if (a.type === "transform") {
|
|
144
|
+
let e = u(a.frames, n), r = d(e.rotation), s = e.translation ?? [
|
|
145
|
+
0,
|
|
146
|
+
0,
|
|
147
|
+
0
|
|
148
|
+
], f = t.rotation;
|
|
149
|
+
c.rotation = [
|
|
150
|
+
0,
|
|
151
|
+
1,
|
|
152
|
+
2
|
|
153
|
+
].flatMap((e) => [
|
|
154
|
+
0,
|
|
155
|
+
1,
|
|
156
|
+
2
|
|
157
|
+
].map((t) => f[e * 3] * r[t] + f[e * 3 + 1] * r[3 + t] + f[e * 3 + 2] * r[6 + t])), c.translation = [
|
|
158
|
+
0,
|
|
159
|
+
1,
|
|
160
|
+
2
|
|
161
|
+
].map((e) => f[e * 3] * s[0] + f[e * 3 + 1] * s[1] + f[e * 3 + 2] * s[2] + t.translation[e]), c.transforms = [...t.transforms, a.id], a.layer >= 0 && !t.layers.includes(a.layer) && (c.layers = [...t.layers, a.layer]), c.visible &&= !o.get(a.layer)?.hidden, l.push({
|
|
162
|
+
...c,
|
|
163
|
+
node: i.get(a.child)
|
|
164
|
+
});
|
|
165
|
+
} else if (a.type === "group") for (let e = a.children.length - 1; e >= 0; e--) l.push({
|
|
166
|
+
...c,
|
|
167
|
+
node: i.get(a.children[e])
|
|
168
|
+
});
|
|
169
|
+
else {
|
|
170
|
+
let t = u(a.models, n).model;
|
|
171
|
+
s.push({
|
|
172
|
+
shapeId: a.id,
|
|
173
|
+
model: t,
|
|
174
|
+
...c.name === void 0 ? {} : { name: c.name },
|
|
175
|
+
visible: c.visible,
|
|
176
|
+
transformIds: c.transforms,
|
|
177
|
+
layerIds: c.layers,
|
|
178
|
+
rotation: c.rotation.map((e) => e || 0),
|
|
179
|
+
translation: c.translation.map((e) => e || 0),
|
|
180
|
+
matrix: m(e.models[t].size, c.rotation, c.translation, r)
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return s;
|
|
185
|
+
}
|
|
186
|
+
function g(e) {
|
|
187
|
+
if (typeof e != "object" || !e || !Array.isArray(e.nodes)) throw TypeError("A .vox scene is missing nodes; pass parseVox output");
|
|
188
|
+
let t = Infinity, n = -Infinity;
|
|
189
|
+
for (let r of e.nodes) for (let e of r.type === "transform" ? r.frames : r.type === "shape" ? r.models : []) {
|
|
190
|
+
let r = l(e);
|
|
191
|
+
t = Math.min(t, r), n = Math.max(n, r);
|
|
192
|
+
}
|
|
193
|
+
return Number.isFinite(t) ? {
|
|
194
|
+
startFrame: t,
|
|
195
|
+
endFrame: n
|
|
196
|
+
} : void 0;
|
|
197
|
+
}
|
|
198
|
+
//#endregion
|
|
199
|
+
//#region src/vox-scene.ts
|
|
200
|
+
var _ = -2147483648, v = 2147483647, y = 16384, b = 16384, x = 4096, S = 4096, C = 1024, w = [
|
|
201
|
+
1,
|
|
202
|
+
0,
|
|
203
|
+
0,
|
|
204
|
+
0,
|
|
205
|
+
1,
|
|
206
|
+
0,
|
|
207
|
+
0,
|
|
208
|
+
0,
|
|
209
|
+
1
|
|
210
|
+
], T = (e) => e.frame ?? 0, E = (e, t) => T(e) - T(t);
|
|
211
|
+
function D(e) {
|
|
212
|
+
if (typeof e != "string" || !e.trim() || e.trim().length > 200) throw RangeError("A .vox name must have 1 to 200 characters; pass a shorter or nonempty name");
|
|
213
|
+
return e.trim();
|
|
214
|
+
}
|
|
215
|
+
function O(e, t) {
|
|
216
|
+
if (!Number.isInteger(e) || e < 0 || e > v) throw RangeError(`${t} is not a .vox id; pass an integer from 0 to ${v}`);
|
|
217
|
+
}
|
|
218
|
+
function k(e) {
|
|
219
|
+
if (typeof e != "object" || !e || Object.keys(e).some((e) => e !== "name" && e !== "hidden")) throw TypeError("A .vox node patch holds only name and hidden; pass {name?, hidden?}");
|
|
220
|
+
if (e.hidden !== void 0 && typeof e.hidden != "boolean") throw TypeError("A .vox node patch hidden is not a boolean; pass true or false");
|
|
221
|
+
return {
|
|
222
|
+
...e.name === void 0 ? {} : { name: D(e.name) },
|
|
223
|
+
...e.hidden === void 0 ? {} : { hidden: e.hidden }
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
function A(e, t) {
|
|
227
|
+
return t.name !== void 0 && t.name !== e.name || t.hidden !== void 0 && t.hidden !== (e.hidden ?? !1);
|
|
228
|
+
}
|
|
229
|
+
function j(e, t) {
|
|
230
|
+
let n = new Map(e.attributes);
|
|
231
|
+
return t.name !== void 0 && n.set("_name", t.name), t.hidden !== void 0 && n.set("_hidden", t.hidden ? "1" : "0"), {
|
|
232
|
+
...e,
|
|
233
|
+
...t,
|
|
234
|
+
attributes: n
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
function M(e) {
|
|
238
|
+
if (typeof e != "object" || !e || !Array.isArray(e.nodes) || !Array.isArray(e.models)) throw TypeError("A .vox scene is missing nodes or models; pass parseVox output");
|
|
239
|
+
if (e.nodes.length > y) throw RangeError(`The .vox scene has over ${y} nodes; edit a smaller scene`);
|
|
240
|
+
let t = /* @__PURE__ */ new Map(), n = /* @__PURE__ */ new Map(), r = 0;
|
|
241
|
+
for (let n of e.nodes) {
|
|
242
|
+
if (O(n.id, "A .vox node id"), t.has(n.id)) throw TypeError(`The .vox scene has node id ${n.id} twice; give each node its own id`);
|
|
243
|
+
t.set(n.id, n);
|
|
244
|
+
let i = n.type === "transform" ? n.frames : n.type === "shape" ? n.models : void 0;
|
|
245
|
+
if (!i) continue;
|
|
246
|
+
if (!Array.isArray(i) || !i.length || i.length > x) throw RangeError(`.vox node ${n.id} needs 1 to ${x} keys; fix its track`);
|
|
247
|
+
r += i.length;
|
|
248
|
+
let a = /* @__PURE__ */ new Set();
|
|
249
|
+
for (let e of i) {
|
|
250
|
+
if (O(T(e), `A key frame of .vox node ${n.id}`), a.has(T(e))) throw TypeError(`.vox node ${n.id} has duplicate (ambiguous) keys at frame ${T(e)}; keep one`);
|
|
251
|
+
a.add(T(e));
|
|
252
|
+
}
|
|
253
|
+
if (n.type === "shape") {
|
|
254
|
+
for (let t of n.models) if (!Number.isInteger(t.model) || t.model < 0 || t.model >= e.models.length) throw RangeError(`.vox shape ${n.id} uses missing model ${t.model}; point it at an existing model`);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (r > b) throw RangeError(`The .vox scene has over ${b} keys; remove some`);
|
|
258
|
+
for (let r of e.nodes) {
|
|
259
|
+
let e = r.type === "transform" ? [r.child] : r.type === "group" ? r.children : [];
|
|
260
|
+
for (let i of e) {
|
|
261
|
+
let e = t.get(i);
|
|
262
|
+
if (!e) throw TypeError(`.vox node ${r.id} links missing node ${i}; fix the link`);
|
|
263
|
+
if (r.type === "group" != (e.type === "transform")) throw TypeError(".vox groups hold transforms and transforms hold groups or shapes; fix the links");
|
|
264
|
+
if (n.has(i)) throw TypeError(`.vox node ${i} has two parents; share models instead of nodes`);
|
|
265
|
+
n.set(i, r.id);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
for (let t of e.nodes) {
|
|
269
|
+
let e = /* @__PURE__ */ new Set();
|
|
270
|
+
for (let r = t.id; r !== void 0; r = n.get(r)) {
|
|
271
|
+
if (e.has(r)) throw TypeError("The .vox node links form a cycle; fix the links");
|
|
272
|
+
if (e.size >= C) throw RangeError(`The .vox hierarchy is over ${C} levels deep; flatten it`);
|
|
273
|
+
e.add(r);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return {
|
|
277
|
+
nodes: t,
|
|
278
|
+
parents: n
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
function N(e, t, n) {
|
|
282
|
+
O(t, `A .vox ${n} id`);
|
|
283
|
+
let r = e.nodes.get(t);
|
|
284
|
+
if (!r || r.type !== n) throw RangeError(`.vox node ${t} is not a ${n}; pass the id of one`);
|
|
285
|
+
return r;
|
|
286
|
+
}
|
|
287
|
+
function P(e, t) {
|
|
288
|
+
return {
|
|
289
|
+
...e,
|
|
290
|
+
nodes: e.nodes.map((e) => e.id === t.id ? t : e)
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
function F(e) {
|
|
294
|
+
return e.nodes.reduce((e, t) => e + (t.type === "transform" ? t.frames.length : t.type === "shape" ? t.models.length : 0), 0);
|
|
295
|
+
}
|
|
296
|
+
function I(e, t) {
|
|
297
|
+
if (F(e) + t > b) throw RangeError(`The .vox scene would pass ${b} keys; remove some first`);
|
|
298
|
+
}
|
|
299
|
+
function L(e, t) {
|
|
300
|
+
let n = [...e].sort(E), r = n[0];
|
|
301
|
+
for (let e of n) T(e) <= t && (r = e);
|
|
302
|
+
return r;
|
|
303
|
+
}
|
|
304
|
+
function R(e, t) {
|
|
305
|
+
let n = e.nodes.reduce((e, t) => Math.max(e, t.id + 1), 0);
|
|
306
|
+
if (e.nodes.length + t > y || n + t - 1 > v) throw RangeError(`The .vox scene has no room for ${t} more nodes (ids are int32); remove some first`);
|
|
307
|
+
return Array.from({ length: t }, (e, t) => n + t);
|
|
308
|
+
}
|
|
309
|
+
function z(e, t) {
|
|
310
|
+
M(e);
|
|
311
|
+
let n = D(t);
|
|
312
|
+
if (e.layers.length >= S) throw RangeError(`The .vox scene has ${S} layers; remove one first`);
|
|
313
|
+
let r = e.layers.reduce((e, t) => Math.max(e, t.id + 1), 0);
|
|
314
|
+
return O(r, "The new .vox layer id"), {
|
|
315
|
+
...e,
|
|
316
|
+
layers: [...e.layers, {
|
|
317
|
+
id: r,
|
|
318
|
+
name: n,
|
|
319
|
+
hidden: !1,
|
|
320
|
+
attributes: /* @__PURE__ */ new Map([["_name", n], ["_hidden", "0"]])
|
|
321
|
+
}]
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
function B(e, t, n) {
|
|
325
|
+
M(e), O(t, "A .vox layer id");
|
|
326
|
+
let r = k(n), i = e.layers.find((e) => e.id === t);
|
|
327
|
+
if (!i) throw RangeError(`.vox layer ${t} does not exist; pass an existing layer id`);
|
|
328
|
+
return A(i, r) ? {
|
|
329
|
+
...e,
|
|
330
|
+
layers: e.layers.map((e) => e.id === t ? j(i, r) : e)
|
|
331
|
+
} : e;
|
|
332
|
+
}
|
|
333
|
+
function V(e, t, n) {
|
|
334
|
+
let r = M(e);
|
|
335
|
+
O(t, "A .vox node id");
|
|
336
|
+
let i = k(n), a = r.nodes.get(t);
|
|
337
|
+
if (!a) throw RangeError(`.vox node ${t} does not exist; pass an existing node id`);
|
|
338
|
+
return A(a, i) ? P(e, j(a, i)) : e;
|
|
339
|
+
}
|
|
340
|
+
function H(e, t, n) {
|
|
341
|
+
let r = N(M(e), t, "transform");
|
|
342
|
+
if (n !== -1 && (O(n, "A .vox layer id"), !e.layers.some((e) => e.id === n))) throw RangeError(`.vox layer ${n} does not exist; pass an existing layer id or -1`);
|
|
343
|
+
return r.layer === n ? e : P(e, {
|
|
344
|
+
...r,
|
|
345
|
+
layer: n
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
function U(e, t, n, r) {
|
|
349
|
+
let i = N(M(e), t, "transform");
|
|
350
|
+
if (O(n, "A .vox key frame"), typeof r != "object" || !r || Object.keys(r).some((e) => e !== "translation" && e !== "rotation") || r.translation === void 0 && r.rotation === void 0) throw TypeError("A .vox transform key needs translation or rotation; pass {translation?, rotation?}");
|
|
351
|
+
let a = r.translation;
|
|
352
|
+
if (a !== void 0 && (!Array.isArray(a) || a.length !== 3 || !Array.from(a).every((e) => Number.isInteger(e) && e >= _ && e <= v))) throw RangeError("A .vox translation must be three int32 integers; round it to whole cells");
|
|
353
|
+
let o = r.rotation;
|
|
354
|
+
if (o !== void 0 && !J(o)) throw RangeError("A .vox rotation must be a row-major signed axis permutation; pass quarter turns only");
|
|
355
|
+
let s = i.frames.find((e) => T(e) === n);
|
|
356
|
+
if (!s) {
|
|
357
|
+
if (i.frames.length >= x) throw RangeError(`.vox node ${t} has ${x} keys; remove one first`);
|
|
358
|
+
I(e, 1);
|
|
359
|
+
}
|
|
360
|
+
let c = s ?? L(i.frames, n), l = [...a ?? c.translation ?? [
|
|
361
|
+
0,
|
|
362
|
+
0,
|
|
363
|
+
0
|
|
364
|
+
]], u = [...o ?? c.rotation ?? w];
|
|
365
|
+
if (s && l.every((e, t) => e === (s.translation?.[t] ?? 0)) && u.every((e, t) => e === (s.rotation ?? w)[t])) return e;
|
|
366
|
+
let d = {
|
|
367
|
+
...c,
|
|
368
|
+
frame: n,
|
|
369
|
+
translation: l,
|
|
370
|
+
rotation: u,
|
|
371
|
+
attributes: new Map(c.attributes)
|
|
372
|
+
};
|
|
373
|
+
return P(e, {
|
|
374
|
+
...i,
|
|
375
|
+
frames: [...i.frames.filter((e) => T(e) !== n), d].sort(E)
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
function ee(e, t, n) {
|
|
379
|
+
let r = N(M(e), t, "transform");
|
|
380
|
+
if (O(n, "A .vox key frame"), !r.frames.some((e) => T(e) === n)) return e;
|
|
381
|
+
if (r.frames.length === 1) throw new o("state", `.vox node ${t} keeps at least one pose; set that key instead of deleting it`);
|
|
382
|
+
return P(e, {
|
|
383
|
+
...r,
|
|
384
|
+
frames: r.frames.filter((e) => T(e) !== n)
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
function te(e, t, n, r) {
|
|
388
|
+
let i = N(M(e), t, "shape");
|
|
389
|
+
if (O(n, "A .vox key frame"), !Number.isInteger(r) || r < 0 || r >= e.models.length) throw RangeError(`.vox model ${r} does not exist; pass 0 to ${e.models.length - 1}`);
|
|
390
|
+
let a = i.models.find((e) => T(e) === n);
|
|
391
|
+
if (a?.model === r) return e;
|
|
392
|
+
if (!a) {
|
|
393
|
+
if (i.models.length >= x) throw RangeError(`.vox shape ${t} has ${x} keys; remove one first`);
|
|
394
|
+
I(e, 1);
|
|
395
|
+
}
|
|
396
|
+
let o = a ?? L(i.models, n), s = {
|
|
397
|
+
...o,
|
|
398
|
+
frame: n,
|
|
399
|
+
model: r,
|
|
400
|
+
attributes: new Map(o.attributes)
|
|
401
|
+
};
|
|
402
|
+
return P(e, {
|
|
403
|
+
...i,
|
|
404
|
+
models: [...i.models.filter((e) => T(e) !== n), s].sort(E)
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
function ne(e, t, n) {
|
|
408
|
+
let r = N(M(e), t, "shape");
|
|
409
|
+
if (O(n, "A .vox key frame"), !r.models.some((e) => T(e) === n)) return e;
|
|
410
|
+
if (r.models.length === 1) throw new o("state", `.vox shape ${t} keeps at least one key; point it at another model instead`);
|
|
411
|
+
return P(e, {
|
|
412
|
+
...r,
|
|
413
|
+
models: r.models.filter((e) => T(e) !== n)
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
function W(e, t, n) {
|
|
417
|
+
return {
|
|
418
|
+
id: e,
|
|
419
|
+
type: "transform",
|
|
420
|
+
name: n,
|
|
421
|
+
hidden: !1,
|
|
422
|
+
layer: -1,
|
|
423
|
+
child: t,
|
|
424
|
+
frames: [{
|
|
425
|
+
frame: 0,
|
|
426
|
+
translation: [
|
|
427
|
+
0,
|
|
428
|
+
0,
|
|
429
|
+
0
|
|
430
|
+
],
|
|
431
|
+
rotation: [...w]
|
|
432
|
+
}],
|
|
433
|
+
attributes: /* @__PURE__ */ new Map([["_name", n]])
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
function G(e, t, n, r) {
|
|
437
|
+
let i = {
|
|
438
|
+
...e,
|
|
439
|
+
nodes: [...e.nodes.map((e) => e.id === t.id ? {
|
|
440
|
+
...t,
|
|
441
|
+
children: [...t.children, n]
|
|
442
|
+
} : e), ...r]
|
|
443
|
+
};
|
|
444
|
+
return M(i), i;
|
|
445
|
+
}
|
|
446
|
+
function K(e, t, n) {
|
|
447
|
+
let r = N(M(e), t, "group"), i = D(n);
|
|
448
|
+
I(e, 1);
|
|
449
|
+
let [a, o] = R(e, 2);
|
|
450
|
+
return {
|
|
451
|
+
scene: G(e, r, a, [W(a, o, i), {
|
|
452
|
+
id: o,
|
|
453
|
+
type: "group",
|
|
454
|
+
children: []
|
|
455
|
+
}]),
|
|
456
|
+
transformId: a,
|
|
457
|
+
groupId: o
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
function q(e, t, n, r) {
|
|
461
|
+
let i = N(M(e), t, "group"), a = D(r);
|
|
462
|
+
if (!Number.isInteger(n) || n < 0 || n >= e.models.length) throw RangeError(`.vox model ${n} does not exist; pass 0 to ${e.models.length - 1}`);
|
|
463
|
+
I(e, 2);
|
|
464
|
+
let [o, s] = R(e, 2);
|
|
465
|
+
return {
|
|
466
|
+
scene: G(e, i, o, [W(o, s, a), {
|
|
467
|
+
id: s,
|
|
468
|
+
type: "shape",
|
|
469
|
+
models: [{
|
|
470
|
+
frame: 0,
|
|
471
|
+
model: n
|
|
472
|
+
}]
|
|
473
|
+
}]),
|
|
474
|
+
transformId: o,
|
|
475
|
+
shapeId: s
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
function J(e) {
|
|
479
|
+
if (!Array.isArray(e) || e.length !== 9 || !Array.from(e).every((e) => e === 0 || e === 1 || e === -1)) return !1;
|
|
480
|
+
for (let t = 0; t < 3; t++) if (Math.abs(e[t * 3]) + Math.abs(e[t * 3 + 1]) + Math.abs(e[t * 3 + 2]) !== 1 || Math.abs(e[t]) + Math.abs(e[3 + t]) + Math.abs(e[6 + t]) !== 1) return !1;
|
|
481
|
+
return !0;
|
|
482
|
+
}
|
|
483
|
+
function Y(e, t) {
|
|
484
|
+
let n = [];
|
|
485
|
+
for (let r = t; r !== void 0; r = e.parents.get(r)) n.push(e.nodes.get(r));
|
|
486
|
+
return n.reverse();
|
|
487
|
+
}
|
|
488
|
+
function X(e, t) {
|
|
489
|
+
let n = new s();
|
|
490
|
+
for (let r of e) {
|
|
491
|
+
if (r.type !== "transform") continue;
|
|
492
|
+
let e = L(r.frames, t), i = e.rotation ?? w, a = e.translation ?? [
|
|
493
|
+
0,
|
|
494
|
+
0,
|
|
495
|
+
0
|
|
496
|
+
];
|
|
497
|
+
n.multiply(new s().set(i[0], i[1], i[2], a[0], i[3], i[4], i[5], a[1], i[6], i[7], i[8], a[2], 0, 0, 0, 1));
|
|
498
|
+
}
|
|
499
|
+
return n;
|
|
500
|
+
}
|
|
501
|
+
function Z(e, t) {
|
|
502
|
+
return t.some((t) => t.hidden || t.type === "transform" && e.layers.some((e) => e.id === t.layer && e.hidden));
|
|
503
|
+
}
|
|
504
|
+
function re(e, t, n) {
|
|
505
|
+
let r = M(e), i = N(r, t, "transform"), a = N(r, n, "group"), s = r.parents.get(t);
|
|
506
|
+
if (s === n) return e;
|
|
507
|
+
if (s === void 0) throw new o("state", "The .vox root transform cannot move; move one of its children");
|
|
508
|
+
let c = r.nodes.get(s), l = Y(r, t), u = Y(r, n);
|
|
509
|
+
if (u.some((e) => e.id === t)) throw new o("state", `.vox group ${n} is inside node ${t}; moving there would make a cycle`);
|
|
510
|
+
let d = Z(e, l);
|
|
511
|
+
if (!d && Z(e, u)) throw new o("state", `.vox group ${n} is hidden; show it before moving visible node ${t} into it`);
|
|
512
|
+
let f = [.../* @__PURE__ */ new Set([0, ...[...l, ...u].flatMap((e) => e.type === "transform" ? e.frames.map(T) : [])])].sort((e, t) => e - t);
|
|
513
|
+
if (f.length > x) throw RangeError(`Keeping .vox node ${t} in place needs over ${x} keys`);
|
|
514
|
+
I(e, f.length - i.frames.length);
|
|
515
|
+
let p = f.map((e) => {
|
|
516
|
+
let n = X(u, e).invert().multiply(X(l, e)).elements, r = [
|
|
517
|
+
n[0],
|
|
518
|
+
n[4],
|
|
519
|
+
n[8],
|
|
520
|
+
n[1],
|
|
521
|
+
n[5],
|
|
522
|
+
n[9],
|
|
523
|
+
n[2],
|
|
524
|
+
n[6],
|
|
525
|
+
n[10]
|
|
526
|
+
].map((e) => e === 0 ? 0 : e), a = [
|
|
527
|
+
n[12],
|
|
528
|
+
n[13],
|
|
529
|
+
n[14]
|
|
530
|
+
];
|
|
531
|
+
if (!J(r) || !a.every((e) => Number.isInteger(e) && e >= _ && e <= v)) throw RangeError(`Keeping .vox node ${t} in place needs a non-axis rotation or fractional move`);
|
|
532
|
+
let o = L(i.frames, e);
|
|
533
|
+
return {
|
|
534
|
+
...o,
|
|
535
|
+
frame: e,
|
|
536
|
+
rotation: r,
|
|
537
|
+
translation: a,
|
|
538
|
+
attributes: new Map(o.attributes)
|
|
539
|
+
};
|
|
540
|
+
}), m = {
|
|
541
|
+
...d && !Z(e, u) ? j(i, { hidden: !0 }) : i,
|
|
542
|
+
frames: p
|
|
543
|
+
};
|
|
544
|
+
return {
|
|
545
|
+
...e,
|
|
546
|
+
nodes: e.nodes.map((e) => e.id === t ? m : e.id === s ? {
|
|
547
|
+
...c,
|
|
548
|
+
children: c.children.filter((e) => e !== t)
|
|
549
|
+
} : e.id === n ? {
|
|
550
|
+
...a,
|
|
551
|
+
children: [...a.children, t]
|
|
552
|
+
} : e)
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
function ie(e, t) {
|
|
556
|
+
let n = M(e), r = N(n, t, "transform"), i = n.parents.get(t);
|
|
557
|
+
if (i === void 0) throw new o("state", "The .vox root transform cannot be deleted; delete a child");
|
|
558
|
+
let a = n.nodes.get(i), s = /* @__PURE__ */ new Set(), c = [r.id];
|
|
559
|
+
for (; c.length;) {
|
|
560
|
+
let e = c.pop(), t = n.nodes.get(e);
|
|
561
|
+
s.add(e), t.type === "group" ? c.push(...t.children) : t.type === "transform" && c.push(t.child);
|
|
562
|
+
}
|
|
563
|
+
return {
|
|
564
|
+
...e,
|
|
565
|
+
nodes: e.nodes.filter((e) => !s.has(e.id)).map((e) => e.id === i ? {
|
|
566
|
+
...a,
|
|
567
|
+
children: a.children.filter((e) => e !== t)
|
|
568
|
+
} : e)
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
//#endregion
|
|
572
|
+
//#region src/vox.ts
|
|
573
|
+
function Q(e, t, n) {
|
|
574
|
+
if (!Array.isArray(e) || e.length !== 3) throw TypeError(`${n} is not [x, y, z]; pass three sizes`);
|
|
575
|
+
if (!e.every((e) => Number.isInteger(e) && e >= 1 && e <= 256)) throw RangeError(`${n} must be 1 to 256 cells per axis, the .vox limit; resize the model`);
|
|
576
|
+
let a = e[0] * e[1] * e[2], o = r(t, i);
|
|
577
|
+
if (a > o) throw RangeError(`${n} exceeds maxCells (${o}); raise maxCells`);
|
|
578
|
+
return [
|
|
579
|
+
e[0],
|
|
580
|
+
e[1],
|
|
581
|
+
e[2]
|
|
582
|
+
];
|
|
583
|
+
}
|
|
584
|
+
function $(e, t = {}) {
|
|
585
|
+
if (typeof e != "object" || !e) throw TypeError("gridFromVox model is missing; pass a VoxModel");
|
|
586
|
+
let [n, r, i] = Q(e.size, t, "gridFromVox size"), a = e.voxels;
|
|
587
|
+
if (!(a instanceof Uint8Array) || a.length % 4) throw RangeError("gridFromVox voxels are not x, y, z, index records; pass a Uint8Array of 4-byte records");
|
|
588
|
+
for (let e = 0; e < a.length; e += 4) {
|
|
589
|
+
if (a[e] >= n || a[e + 1] >= r || a[e + 2] >= i) throw RangeError(`gridFromVox record ${e / 4} lies outside the model size; fix the record or the size`);
|
|
590
|
+
if (!a[e + 3]) throw RangeError(`gridFromVox record ${e / 4} has palette index 0; use 1 to 255`);
|
|
591
|
+
}
|
|
592
|
+
let o = [
|
|
593
|
+
n,
|
|
594
|
+
i,
|
|
595
|
+
r
|
|
596
|
+
], s = new Uint8Array(n * r * i);
|
|
597
|
+
for (let e = 0; e < a.length; e += 4) s[a[e] + n * (r - 1 - a[e + 1] + r * a[e + 2])] = a[e + 3];
|
|
598
|
+
return {
|
|
599
|
+
size: o,
|
|
600
|
+
voxels: s
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
function ae(t, n, r = {}) {
|
|
604
|
+
let i = t instanceof a, [o, s, c] = Q(t?.size, r, "voxFromGrid size");
|
|
605
|
+
i || e(t, o * s * c);
|
|
606
|
+
let l = i ? (e, n, r) => t.get(e, n, r) : (e, n, r) => t.voxels[e + o * (r + c * n)];
|
|
607
|
+
if (n && n.size[0] === o && n.size[1] === c && n.size[2] === s) {
|
|
608
|
+
let e = $(n, r).voxels, t = !0;
|
|
609
|
+
for (let n = 0, r = 0; t && n < s; n++) for (let i = 0; t && i < c; i++) for (let a = 0; a < o; a++, r++) e[r] !== l(a, n, i) && (t = !1);
|
|
610
|
+
if (t) return n;
|
|
611
|
+
}
|
|
612
|
+
let u = [];
|
|
613
|
+
if (i) for (let [e, n, r, i] of t.filled()) u.push(e, c - 1 - r, n, i);
|
|
614
|
+
else for (let e = 0; e < s; e++) for (let n = 0; n < c; n++) for (let r = 0; r < o; r++) {
|
|
615
|
+
let i = t.voxels[r + o * (n + c * e)];
|
|
616
|
+
i && u.push(r, c - 1 - n, e, i);
|
|
617
|
+
}
|
|
618
|
+
return {
|
|
619
|
+
size: [
|
|
620
|
+
o,
|
|
621
|
+
c,
|
|
622
|
+
s
|
|
623
|
+
],
|
|
624
|
+
voxels: Uint8Array.from(u)
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
function oe(i, o = {}) {
|
|
628
|
+
t(o, "voxTiles");
|
|
629
|
+
let s = o.tileSize ?? 256, c = o.maxTiles ?? 4096, l = r(o);
|
|
630
|
+
if (!Number.isInteger(s) || s < 1 || s > 256) throw RangeError("voxTiles tileSize must be an integer from 1 to 256; pass one");
|
|
631
|
+
if (!Number.isInteger(c) || c < 1 || c > 65536) throw RangeError("voxTiles maxTiles must be an integer from 1 to 65,536; pass one");
|
|
632
|
+
let u = i instanceof a;
|
|
633
|
+
u || e(i, n, "voxTiles grid");
|
|
634
|
+
let [d, , f] = i.size, p = u ? () => i.filled() : function* () {
|
|
635
|
+
let e = i.voxels;
|
|
636
|
+
for (let t = 0; t < e.length; t++) if (e[t]) {
|
|
637
|
+
let n = Math.floor(t / d);
|
|
638
|
+
yield [
|
|
639
|
+
t - n * d,
|
|
640
|
+
Math.floor(n / f),
|
|
641
|
+
n % f,
|
|
642
|
+
e[t]
|
|
643
|
+
];
|
|
644
|
+
}
|
|
645
|
+
}, m = /* @__PURE__ */ new Map(), h = (e, t, n) => `${Math.floor(e / s)},${Math.floor(t / s)},${Math.floor(n / s)}`;
|
|
646
|
+
for (let [e, t, n] of p()) {
|
|
647
|
+
let r = h(e, t, n), i = m.get(r);
|
|
648
|
+
if (!i) {
|
|
649
|
+
if (m.size >= c) throw RangeError(`voxTiles needs over ${c} tiles; raise maxTiles or split the grid`);
|
|
650
|
+
m.set(r, i = {
|
|
651
|
+
min: [
|
|
652
|
+
e,
|
|
653
|
+
t,
|
|
654
|
+
n
|
|
655
|
+
],
|
|
656
|
+
max: [
|
|
657
|
+
e + 1,
|
|
658
|
+
t + 1,
|
|
659
|
+
n + 1
|
|
660
|
+
]
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
let a = [
|
|
664
|
+
e,
|
|
665
|
+
t,
|
|
666
|
+
n
|
|
667
|
+
];
|
|
668
|
+
for (let e = 0; e < 3; e++) a[e] < i.min[e] && (i.min[e] = a[e]), a[e] >= i.max[e] && (i.max[e] = a[e] + 1);
|
|
669
|
+
}
|
|
670
|
+
let g = [...m.entries()].sort(([, e], [, t]) => e.min[1] - t.min[1] || e.min[2] - t.min[2] || e.min[0] - t.min[0]), _ = g.map(([, e]) => e.max.map((t, n) => t - e.min[n])), v = 0;
|
|
671
|
+
for (let [e, t, n] of _) if (v += e * t * n, v > l) throw RangeError(`voxTiles tiles hold over ${l} cells; raise maxCells or split the grid`);
|
|
672
|
+
let y = g.map(([, e], t) => ({
|
|
673
|
+
origin: e.min,
|
|
674
|
+
size: _[t],
|
|
675
|
+
voxels: new Uint8Array(_[t][0] * _[t][1] * _[t][2])
|
|
676
|
+
})), b = new Map(g.map(([e], t) => [e, y[t]]));
|
|
677
|
+
for (let [e, t, n, r] of p()) {
|
|
678
|
+
let { origin: i, size: a, voxels: o } = b.get(h(e, t, n));
|
|
679
|
+
o[e - i[0] + a[0] * (n - i[2] + a[2] * (t - i[1]))] = r;
|
|
680
|
+
}
|
|
681
|
+
return y;
|
|
682
|
+
}
|
|
683
|
+
//#endregion
|
|
684
|
+
export { K as addVoxGroup, q as addVoxInstance, z as addVoxLayer, ie as deleteVoxNode, ne as deleteVoxShapeKey, ee as deleteVoxTransformKey, $ as gridFromVox, re as moveVoxNode, h as resolveVoxScene, H as setVoxNodeLayer, te as setVoxShapeKey, U as setVoxTransformKey, B as updateVoxLayer, V as updateVoxNode, g as voxFrameRange, ae as voxFromGrid, oe as voxTiles };
|