@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/edit.js
ADDED
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
import { c as e, d as t, i as n, l as r, n as i, o as a, p as o, r as s, s as c, u as l } from "./grid-B2V0TGLV.js";
|
|
2
|
+
import { SparseGrid as u } from "./sparse.js";
|
|
3
|
+
//#region src/raster.ts
|
|
4
|
+
var d = 67108864, f = 1048576;
|
|
5
|
+
function p(e, t, n) {
|
|
6
|
+
if (r(t, n), c(e, `${n} size`), e.some((e) => e < 1)) throw RangeError(`${n} size has an empty axis; pass sizes of at least 1`);
|
|
7
|
+
let i = t.outside ?? "refuse", o = a(t), s = /* @__PURE__ */ new Set(), [l, u, d] = e;
|
|
8
|
+
if (i !== "refuse" && i !== "clip") throw TypeError(`${n} outside is unknown; pass 'refuse' or 'clip'`);
|
|
9
|
+
return {
|
|
10
|
+
put(e, t, r) {
|
|
11
|
+
if (e < 0 || t < 0 || r < 0 || e >= l || t >= u || r >= d) {
|
|
12
|
+
if (i === "clip") return;
|
|
13
|
+
throw RangeError(`${n} reaches cell ${e}, ${t}, ${r} outside the grid; move it or pass outside 'clip'`);
|
|
14
|
+
}
|
|
15
|
+
let a = e + l * (r + d * t);
|
|
16
|
+
if (!s.has(a) && s.size >= o) throw RangeError(`${n} covers over ${o} cells; draw less or raise maxCells`);
|
|
17
|
+
s.add(a);
|
|
18
|
+
},
|
|
19
|
+
span(t, n, r) {
|
|
20
|
+
let a = Math.ceil(t), o = Math.floor(n);
|
|
21
|
+
return i === "clip" ? [Math.max(0, a), Math.min(e[r] - 1, o)] : [a, o];
|
|
22
|
+
},
|
|
23
|
+
result: () => Float64Array.from(s)
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function m(e, t) {
|
|
27
|
+
if (typeof e != "number" || !Number.isFinite(e) || e < 0) throw RangeError(`${t} must be finite and 0 or more; pass one`);
|
|
28
|
+
return e;
|
|
29
|
+
}
|
|
30
|
+
function h(e, t) {
|
|
31
|
+
if (e.reduce((e, t) => e * Math.max(0, t), 1) > d) throw RangeError(`${t} spans over ${d} cells of bounding box; draw it in parts`);
|
|
32
|
+
}
|
|
33
|
+
function g(e, t, n) {
|
|
34
|
+
let [r, i, a] = t, [o, s, c] = n, [l, u] = e.span(i - s, i + s, 1), [d, f] = e.span(a - c, a + c, 2), [p, m] = e.span(r - o, r + o, 0);
|
|
35
|
+
for (let t = l; t <= u; t++) for (let n = d; n <= f; n++) for (let l = p; l <= m; l++) ((l - r) / o) ** 2 + ((t - i) / s) ** 2 + ((n - a) / c) ** 2 <= 1 && e.put(l, t, n);
|
|
36
|
+
}
|
|
37
|
+
function _(e, n) {
|
|
38
|
+
let r = p(e, n, "ellipsoidCells");
|
|
39
|
+
if (t(n.center, "ellipsoidCells center"), t(n.radii, "ellipsoidCells radii"), n.radii.some((e) => e <= 0)) throw RangeError("ellipsoidCells radii must be positive; pass three");
|
|
40
|
+
return h(n.radii.map((e) => 2 * e + 1), "ellipsoidCells"), g(r, n.center, n.radii), r.result();
|
|
41
|
+
}
|
|
42
|
+
function v(e, t, n, r, i, a) {
|
|
43
|
+
let o = (t + 1) % 3, s = (t + 2) % 3, c = [
|
|
44
|
+
0,
|
|
45
|
+
0,
|
|
46
|
+
0
|
|
47
|
+
];
|
|
48
|
+
for (let l = Math.ceil(-i / 2); l <= Math.floor(i / 2); l++) for (let i = Math.ceil(-r); i <= r; i++) for (let u = Math.ceil(-r); u <= r; u++) {
|
|
49
|
+
let d = i * i + u * u;
|
|
50
|
+
d > r * r || a !== void 0 && d <= a * a || (c[0] = n[0], c[1] = n[1], c[2] = n[2], c[t] += l, c[o] += i, c[s] += u, e(Math.round(c[0]), Math.round(c[1]), Math.round(c[2])));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function y(e, n) {
|
|
54
|
+
let r = p(e, n, "cylinderCells"), i = s(n.axis, "cylinderCells axis");
|
|
55
|
+
t(n.center, "cylinderCells center");
|
|
56
|
+
let a = m(n.radius, "cylinderCells radius"), o = m(n.length, "cylinderCells length"), c = n.inner === void 0 ? void 0 : m(n.inner, "cylinderCells inner");
|
|
57
|
+
return h([
|
|
58
|
+
2 * a + 1,
|
|
59
|
+
2 * a + 1,
|
|
60
|
+
o + 1
|
|
61
|
+
], "cylinderCells"), v(r.put, i, n.center, a, o, c), r.result();
|
|
62
|
+
}
|
|
63
|
+
function b(e, n) {
|
|
64
|
+
let r = p(e, n, "ringCells");
|
|
65
|
+
t(n.center, "ringCells center");
|
|
66
|
+
let i = m(n.outer, "ringCells outer"), a = m(n.inner, "ringCells inner"), o = m(n.thickness, "ringCells thickness");
|
|
67
|
+
return h([
|
|
68
|
+
2 * i + 1,
|
|
69
|
+
2 * i + 1,
|
|
70
|
+
o + 1
|
|
71
|
+
], "ringCells"), v(r.put, 2, n.center, i, o, a), r.result();
|
|
72
|
+
}
|
|
73
|
+
function x(e, n) {
|
|
74
|
+
let r = p(e, n, "thickLineCells"), { from: i, to: a } = n, o = m(n.halfWidth ?? 0, "thickLineCells halfWidth"), s = n.steps ?? "ceil";
|
|
75
|
+
if (t(i, "thickLineCells from"), t(a, "thickLineCells to"), s !== "ceil" && s !== "span") throw TypeError("thickLineCells steps is unknown; pass 'ceil' or 'span'");
|
|
76
|
+
let c = Math.max(Math.abs(i[0] - a[0]), Math.abs(i[1] - a[1]), Math.abs(i[2] - a[2])), l = s === "ceil" ? Math.ceil(c) : c;
|
|
77
|
+
if (l > f) throw RangeError(`thickLineCells takes over ${f} steps; draw it in parts`);
|
|
78
|
+
h([
|
|
79
|
+
2 * o + 1,
|
|
80
|
+
2 * o + 1,
|
|
81
|
+
2 * o + 1
|
|
82
|
+
], "thickLineCells");
|
|
83
|
+
for (let e = 0; e <= l; e++) {
|
|
84
|
+
let t = [
|
|
85
|
+
0,
|
|
86
|
+
1,
|
|
87
|
+
2
|
|
88
|
+
].map((t) => Math.round(i[t] + (a[t] - i[t]) * e / Math.max(1, l))), [n, s] = r.span(t[1] - o, t[1] + o, 1), [c, u] = r.span(t[2] - o, t[2] + o, 2), [d, f] = r.span(t[0] - o, t[0] + o, 0);
|
|
89
|
+
for (let e = n; e <= s; e++) for (let t = c; t <= u; t++) for (let n = d; n <= f; n++) r.put(n, e, t);
|
|
90
|
+
}
|
|
91
|
+
return r.result();
|
|
92
|
+
}
|
|
93
|
+
function S(e, n) {
|
|
94
|
+
let r = p(e, n, "taperedLineCells"), { from: i, to: a } = n, o = n.radius ?? 1, s = n.endRadius ?? o, c = n.minRadius ?? .6, l = n.samplesPerCell ?? 1.4;
|
|
95
|
+
if (t(i, "taperedLineCells from"), t(a, "taperedLineCells to"), !Number.isFinite(o) || !Number.isFinite(s)) throw RangeError("taperedLineCells radius and endRadius must be finite; pass numbers");
|
|
96
|
+
if (!(c > 0 && Number.isFinite(c)) || !(l > 0 && Number.isFinite(l))) throw RangeError("taperedLineCells minRadius and samplesPerCell must be positive and finite; pass numbers");
|
|
97
|
+
let u = Math.hypot(a[0] - i[0], a[1] - i[1], a[2] - i[2]), d = Math.ceil(u * l), m = Math.max(c, Math.abs(o), Math.abs(s));
|
|
98
|
+
if (d > f) throw RangeError(`taperedLineCells takes over ${f} steps; draw it in parts`);
|
|
99
|
+
h([
|
|
100
|
+
2 * m + 1,
|
|
101
|
+
2 * m + 1,
|
|
102
|
+
2 * m + 1
|
|
103
|
+
], "taperedLineCells");
|
|
104
|
+
for (let e = 0; e <= d; e++) {
|
|
105
|
+
let t = e / Math.max(1, d), n = Math.max(c, o + (s - o) * t);
|
|
106
|
+
g(r, [
|
|
107
|
+
i[0] + (a[0] - i[0]) * t,
|
|
108
|
+
i[1] + (a[1] - i[1]) * t,
|
|
109
|
+
i[2] + (a[2] - i[2]) * t
|
|
110
|
+
], [
|
|
111
|
+
n,
|
|
112
|
+
n,
|
|
113
|
+
n
|
|
114
|
+
]);
|
|
115
|
+
}
|
|
116
|
+
return r.result();
|
|
117
|
+
}
|
|
118
|
+
//#endregion
|
|
119
|
+
//#region src/stamp.ts
|
|
120
|
+
function C(e, t) {
|
|
121
|
+
r(t, "mirrorCells"), c(t.size, "mirrorCells size");
|
|
122
|
+
let [n, i, o] = t.size, s = t.mirror, u = t.side ?? 1, d = a(t);
|
|
123
|
+
if (n < 1 || i < 1 || o < 1) throw RangeError("mirrorCells size has an empty axis; pass sizes of at least 1");
|
|
124
|
+
if (!Array.isArray(s) || s.length !== 3 || !s.every((e) => typeof e == "boolean")) throw TypeError("mirrorCells mirror is not three booleans; pass [x, y, z]");
|
|
125
|
+
if (!Number.isInteger(u) || u < 1 || u > 255) throw RangeError("mirrorCells side must be an integer from 1 to 255; pass one");
|
|
126
|
+
let f = -Math.floor((u - 1) / 2), p = /* @__PURE__ */ new Map(), m;
|
|
127
|
+
for (let t of e) {
|
|
128
|
+
if (!Array.isArray(t) || t.length !== 3 && t.length !== 4) throw TypeError("mirrorCells sample is not [x, y, z] or [x, y, z, index]; pass one");
|
|
129
|
+
if (m === void 0) m = t.length === 4;
|
|
130
|
+
else if (m !== (t.length === 4)) throw TypeError("mirrorCells samples mix [x, y, z] and [x, y, z, index]; pass one kind");
|
|
131
|
+
c(t.slice(0, 3), "mirrorCells sample"), m && l(t[3], "mirrorCells sample index", 0);
|
|
132
|
+
let [e, r, a] = t, h = m ? t[3] : 0;
|
|
133
|
+
for (let t = f; t < f + u; t++) for (let c = f; c < f + u; c++) for (let l = f; l < f + u; l++) {
|
|
134
|
+
let u = e + l, f = r + t, m = a + c, g = s[0] ? [u, n - 1 - u] : [u], _ = s[1] ? [f, i - 1 - f] : [f], v = s[2] ? [m, o - 1 - m] : [m];
|
|
135
|
+
for (let e of g) for (let t of _) for (let r of v) {
|
|
136
|
+
if (e < 0 || t < 0 || r < 0 || e >= n || t >= i || r >= o) continue;
|
|
137
|
+
let a = e + n * (r + o * t);
|
|
138
|
+
if (!p.has(a) && p.size >= d) throw RangeError(`mirrorCells covers over ${d} cells; stamp less or raise maxCells`);
|
|
139
|
+
p.set(a, h);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return m ? {
|
|
144
|
+
cells: Float64Array.from(p.keys()),
|
|
145
|
+
values: Uint8Array.from(p.values())
|
|
146
|
+
} : { cells: Float64Array.from(p.keys()) };
|
|
147
|
+
}
|
|
148
|
+
function w(e, t, n, i) {
|
|
149
|
+
r(n, "stampCells");
|
|
150
|
+
let { operation: a, color: o, values: s, selection: c } = n;
|
|
151
|
+
if (a !== "fill" && a !== "paint" && a !== "erase") throw TypeError("stampCells operation is unknown; pass 'fill', 'paint' or 'erase'");
|
|
152
|
+
if (o !== void 0 && l(o, "stampCells color"), s !== void 0 && (!(s instanceof Uint8Array) || s.length !== t.length)) throw RangeError("stampCells values must be a Uint8Array with one index per cell; pass mirrorCells values");
|
|
153
|
+
if (a !== "erase" && o === void 0 && s === void 0) throw TypeError(`stampCells ${a} needs a color or values; pass one`);
|
|
154
|
+
if (c !== void 0 && typeof c?.has != "function") throw TypeError("stampCells selection has no has(index); pass a Set of cell indexes");
|
|
155
|
+
let u = e[0] * e[1] * e[2], d = /* @__PURE__ */ new Map();
|
|
156
|
+
for (let e = 0; e < t.length; e++) {
|
|
157
|
+
let n = t[e];
|
|
158
|
+
if (!Number.isSafeInteger(n) || n < 0 || n >= u) throw RangeError(`stampCells cell ${n} is not a cell index of the grid; pass indexes from mirrorCells`);
|
|
159
|
+
if (c && !c.has(n)) continue;
|
|
160
|
+
let r = d.get(n) ?? i(n);
|
|
161
|
+
if (a === "paint" && !i(n)) continue;
|
|
162
|
+
let l = a === "erase" ? 0 : o ?? s[e];
|
|
163
|
+
l !== r && d.set(n, l);
|
|
164
|
+
}
|
|
165
|
+
return d;
|
|
166
|
+
}
|
|
167
|
+
function T(t, r, a) {
|
|
168
|
+
if (t instanceof u) {
|
|
169
|
+
let e = w(t.size, r, a, (e) => t.get(...n(t.size, e)));
|
|
170
|
+
return t.edit([...e].map(([e, r]) => [...n(t.size, e), r]));
|
|
171
|
+
}
|
|
172
|
+
e(t, i, "stampCells target");
|
|
173
|
+
let s = w(t.size, r, a, (e) => t.voxels[e]), c = t.voxels.slice();
|
|
174
|
+
for (let [e, t] of s) c[e] = t;
|
|
175
|
+
return o(t, c, 0);
|
|
176
|
+
}
|
|
177
|
+
function E(t, s, d) {
|
|
178
|
+
r(d, "pasteCells");
|
|
179
|
+
let f = a(d), p = t instanceof u;
|
|
180
|
+
p || e(t, i, "pasteCells target"), c(d.offset, "pasteCells offset");
|
|
181
|
+
for (let e of [d.targetKeys, d.clipKeys]) if (!Array.isArray(e) || e.length !== 256 || e.some((e) => typeof e != "string")) throw TypeError("pasteCells keys must be 256 strings per palette; pass targetKeys and clipKeys");
|
|
182
|
+
let [m, h, g] = t.size, [_, v, y] = d.offset, b = [], x = 0, S = 0;
|
|
183
|
+
for (let e of s) {
|
|
184
|
+
if (++x > f) throw RangeError(`pasteCells clip holds over ${f} cells; raise maxCells`);
|
|
185
|
+
if (!Array.isArray(e) || e.length !== 4) throw TypeError("pasteCells clip cell is not [x, y, z, index]; pass cells as four-number arrays");
|
|
186
|
+
c(e.slice(0, 3), "pasteCells clip cell"), l(e[3], "pasteCells clip index");
|
|
187
|
+
let t = e[0] + _, n = e[1] + v, r = e[2] + y;
|
|
188
|
+
t < 0 || n < 0 || r < 0 || t >= m || n >= h || r >= g ? S++ : b.push([t + m * (r + g * n), e[3]]);
|
|
189
|
+
}
|
|
190
|
+
let C = new Set(b.map(([e]) => e)), w = /* @__PURE__ */ new Set();
|
|
191
|
+
if (p) for (let [e, n, r, i] of t.filled()) C.has(e + m * (r + g * n)) || w.add(i);
|
|
192
|
+
else for (let e = 0; e < t.voxels.length; e++) t.voxels[e] && !C.has(e) && w.add(t.voxels[e]);
|
|
193
|
+
let T = [...d.targetKeys], E = /* @__PURE__ */ new Uint8Array(256), D = [], O = 0, k = 0, A = [];
|
|
194
|
+
for (let e of new Set(b.map(([, e]) => e))) {
|
|
195
|
+
let t = d.clipKeys[e], n = T[e] === t ? e : T.findIndex((e, n) => n > 0 && e === t);
|
|
196
|
+
n < 1 ? A.push(e) : (w.add(n), E[e] = n);
|
|
197
|
+
}
|
|
198
|
+
for (let e of A) {
|
|
199
|
+
let t = d.clipKeys[e], n = T.findIndex((e, n) => n > 0 && e === t);
|
|
200
|
+
if (n > 0) {
|
|
201
|
+
E[e] = n;
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
O++;
|
|
205
|
+
let r = 1;
|
|
206
|
+
for (; r < 256 && w.has(r);) r++;
|
|
207
|
+
r !== 256 && (k++, T[r] = t, D.push({
|
|
208
|
+
from: e,
|
|
209
|
+
to: r
|
|
210
|
+
}), w.add(r), E[e] = r);
|
|
211
|
+
}
|
|
212
|
+
if (O > k) return {
|
|
213
|
+
status: "overflow",
|
|
214
|
+
needed: O,
|
|
215
|
+
free: k
|
|
216
|
+
};
|
|
217
|
+
let j = {
|
|
218
|
+
status: "pasted",
|
|
219
|
+
paletteMap: E,
|
|
220
|
+
assignments: D,
|
|
221
|
+
selection: Float64Array.from(C),
|
|
222
|
+
clipped: S
|
|
223
|
+
};
|
|
224
|
+
if (p) return {
|
|
225
|
+
...j,
|
|
226
|
+
...t.edit(b.map(([e, r]) => [...n(t.size, e), E[r]]))
|
|
227
|
+
};
|
|
228
|
+
let M = t.voxels.slice();
|
|
229
|
+
for (let [e, t] of b) M[e] = E[t];
|
|
230
|
+
return {
|
|
231
|
+
...j,
|
|
232
|
+
...o(t, M, S)
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
//#endregion
|
|
236
|
+
//#region src/edit.ts
|
|
237
|
+
var D = 4096, O = 16777216, k = 128;
|
|
238
|
+
function A(e, n) {
|
|
239
|
+
return t(e, n), [
|
|
240
|
+
Math.round(e[0]),
|
|
241
|
+
Math.round(e[1]),
|
|
242
|
+
Math.round(e[2])
|
|
243
|
+
];
|
|
244
|
+
}
|
|
245
|
+
function j(e, t) {
|
|
246
|
+
let n = A(e, "lineCells start"), r = A(t, "lineCells end"), i = [
|
|
247
|
+
r[0] - n[0],
|
|
248
|
+
r[1] - n[1],
|
|
249
|
+
r[2] - n[2]
|
|
250
|
+
], a = Math.max(Math.abs(i[0]), Math.abs(i[1]), Math.abs(i[2]));
|
|
251
|
+
if (a > D) throw RangeError(`lineCells spans ${a} cells, over ${D}; draw it in parts`);
|
|
252
|
+
return a ? Array.from({ length: a + 1 }, (e, t) => [
|
|
253
|
+
Math.round(n[0] + i[0] * t / a),
|
|
254
|
+
Math.round(n[1] + i[1] * t / a),
|
|
255
|
+
Math.round(n[2] + i[2] * t / a)
|
|
256
|
+
]) : [n];
|
|
257
|
+
}
|
|
258
|
+
function M(e, t, n = {}) {
|
|
259
|
+
let r = A(e, "boxCells corner"), i = A(t, "boxCells corner"), a = [
|
|
260
|
+
Math.min(r[0], i[0]),
|
|
261
|
+
Math.min(r[1], i[1]),
|
|
262
|
+
Math.min(r[2], i[2])
|
|
263
|
+
], o = [
|
|
264
|
+
Math.max(r[0], i[0]),
|
|
265
|
+
Math.max(r[1], i[1]),
|
|
266
|
+
Math.max(r[2], i[2])
|
|
267
|
+
], s = (o[0] - a[0] + 1) * (o[1] - a[1] + 1) * (o[2] - a[2] + 1), c = n.hollow ?? !1;
|
|
268
|
+
if (typeof c != "boolean") throw TypeError("boxCells hollow is not a boolean; pass true or false");
|
|
269
|
+
if (s > O) throw RangeError(`boxCells covers ${s} cells, over ${O}; use a smaller box`);
|
|
270
|
+
let l = [];
|
|
271
|
+
for (let e = a[1]; e <= o[1]; e++) for (let t = a[2]; t <= o[2]; t++) for (let n = a[0]; n <= o[0]; n++) (!c || n === a[0] || n === o[0] || e === a[1] || e === o[1] || t === a[2] || t === o[2]) && l.push([
|
|
272
|
+
n,
|
|
273
|
+
e,
|
|
274
|
+
t
|
|
275
|
+
]);
|
|
276
|
+
return l;
|
|
277
|
+
}
|
|
278
|
+
function N(e, t) {
|
|
279
|
+
let n = A(e, "sphereCells center");
|
|
280
|
+
if (!Number.isFinite(t) || t < 0 || t > k) throw RangeError(`sphereCells radius must be 0 to ${k}; pass one in range`);
|
|
281
|
+
let r = Math.ceil(t), i = t * t, a = [];
|
|
282
|
+
for (let e = -r; e <= r; e++) for (let t = -r; t <= r; t++) for (let o = -r; o <= r; o++) o * o + e * e + t * t <= i && a.push([
|
|
283
|
+
n[0] + o,
|
|
284
|
+
n[1] + e,
|
|
285
|
+
n[2] + t
|
|
286
|
+
]);
|
|
287
|
+
return a;
|
|
288
|
+
}
|
|
289
|
+
var P = [
|
|
290
|
+
[
|
|
291
|
+
-1,
|
|
292
|
+
0,
|
|
293
|
+
0
|
|
294
|
+
],
|
|
295
|
+
[
|
|
296
|
+
1,
|
|
297
|
+
0,
|
|
298
|
+
0
|
|
299
|
+
],
|
|
300
|
+
[
|
|
301
|
+
0,
|
|
302
|
+
-1,
|
|
303
|
+
0
|
|
304
|
+
],
|
|
305
|
+
[
|
|
306
|
+
0,
|
|
307
|
+
1,
|
|
308
|
+
0
|
|
309
|
+
],
|
|
310
|
+
[
|
|
311
|
+
0,
|
|
312
|
+
0,
|
|
313
|
+
-1
|
|
314
|
+
],
|
|
315
|
+
[
|
|
316
|
+
0,
|
|
317
|
+
0,
|
|
318
|
+
1
|
|
319
|
+
]
|
|
320
|
+
];
|
|
321
|
+
function F(t, n, r, i, o, s = P) {
|
|
322
|
+
if (typeof r != "object" || !r) throw TypeError(`${i} limits are not an object; pass {maxCells}`);
|
|
323
|
+
let l = a(r);
|
|
324
|
+
c(n, `${i} seed`);
|
|
325
|
+
let d = t instanceof u;
|
|
326
|
+
d || e(t, l);
|
|
327
|
+
let [f, p, m] = t.size, h = f * m, g = d ? void 0 : t.voxels, _ = (e, n, r) => e < 0 || n < 0 || r < 0 || e >= f || n >= p || r >= m ? 0 : g ? g[e + f * (r + m * n)] : t.get(e, n, r), [v, y, b] = n;
|
|
328
|
+
if (v < 0 || y < 0 || b < 0 || v >= f || y >= p || b >= m || !o(_, v, y, b)) return /* @__PURE__ */ new Float64Array();
|
|
329
|
+
if (d && !_(v, y, b) && f * p * m > l) throw RangeError(`${i} would flood the air of a grid larger than maxCells; select within a window instead`);
|
|
330
|
+
let x = g ? new Uint8Array(g.length) : void 0, S = g ? void 0 : /* @__PURE__ */ new Set(), C = v + f * (b + m * y), w = [C];
|
|
331
|
+
x ? x[C] = 1 : S.add(C);
|
|
332
|
+
for (let e = 0; e < w.length; e++) {
|
|
333
|
+
let t = w[e], n = Math.floor(t / h), r = t - n * h, a = Math.floor(r / f), c = r - a * f;
|
|
334
|
+
for (let [e, t, r] of s) {
|
|
335
|
+
let s = c + e, u = n + t, d = a + r;
|
|
336
|
+
if (s < 0 || u < 0 || d < 0 || s >= f || u >= p || d >= m) continue;
|
|
337
|
+
let h = s + f * (d + m * u);
|
|
338
|
+
if (!(x ? x[h] : S.has(h)) && o(_, s, u, d)) {
|
|
339
|
+
if (w.length >= l) throw RangeError(`${i} exceeds maxCells (${l}); select less or raise maxCells`);
|
|
340
|
+
x ? x[h] = 1 : S.add(h), w.push(h);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return Float64Array.from(w);
|
|
345
|
+
}
|
|
346
|
+
function I(e, t, n = {}) {
|
|
347
|
+
let r = -1;
|
|
348
|
+
return F(e, t, n, "floodCells", (e, t, n, i) => {
|
|
349
|
+
let a = e(t, n, i);
|
|
350
|
+
return r < 0 && (r = a), a === r;
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
function L(e, t, n = {}) {
|
|
354
|
+
return F(e, t, n, "connectedCells", (e, t, n, r) => e(t, n, r) !== 0);
|
|
355
|
+
}
|
|
356
|
+
function R(e, t, n, r = {}) {
|
|
357
|
+
if (!Array.isArray(n) || n.length !== 3 || !Array.from(n).every(Number.isInteger) || Math.abs(n[0]) + Math.abs(n[1]) + Math.abs(n[2]) !== 1) throw RangeError("faceCells normal is not a unit axis vector; pass e.g. [0, 1, 0]");
|
|
358
|
+
return F(e, t, r, "faceCells", (e, t, r, i) => e(t, r, i) !== 0 && !e(t + n[0], r + n[1], i + n[2]), P.filter((e) => e.every((e, t) => !e || !n[t])));
|
|
359
|
+
}
|
|
360
|
+
//#endregion
|
|
361
|
+
export { M as boxCells, L as connectedCells, y as cylinderCells, _ as ellipsoidCells, R as faceCells, I as floodCells, j as lineCells, C as mirrorCells, E as pasteCells, b as ringCells, N as sphereCells, T as stampCells, S as taperedLineCells, x as thickLineCells };
|
package/dist/export.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { type GridMesh } from './mesh.js';
|
|
2
|
+
import type { VoxelGrid } from './grid.js';
|
|
3
|
+
/** Palette material written onto one primitive. */
|
|
4
|
+
export interface ExportMaterial {
|
|
5
|
+
/** Palette index this material belongs to. */
|
|
6
|
+
index: number;
|
|
7
|
+
/** Linear RGB base colour. */
|
|
8
|
+
color: [number, number, number];
|
|
9
|
+
/** Optional metalness, 0 to 1. */
|
|
10
|
+
metalness?: number;
|
|
11
|
+
/** Optional roughness, 0 to 1. */
|
|
12
|
+
roughness?: number;
|
|
13
|
+
}
|
|
14
|
+
/** One material's triangles, the shape the exporters' `ExportPrimitive` reads. */
|
|
15
|
+
export interface VoxelExportPrimitive {
|
|
16
|
+
/** Triangles in cells: xyz positions, unit normals and indices. */
|
|
17
|
+
geometry: {
|
|
18
|
+
positions: Float32Array;
|
|
19
|
+
normals: Float32Array;
|
|
20
|
+
indices: Uint32Array;
|
|
21
|
+
/** Per-vertex ambient occlusion as rgb, present when the mesh was built with ambient occlusion. */
|
|
22
|
+
colors?: Float32Array;
|
|
23
|
+
};
|
|
24
|
+
/** Named `palette-<index>`; linear RGBA colour, roughness and metalness from the matching {@link ExportMaterial}. */
|
|
25
|
+
material: {
|
|
26
|
+
name: string;
|
|
27
|
+
color: [number, number, number, number];
|
|
28
|
+
roughness: number;
|
|
29
|
+
metalness: number;
|
|
30
|
+
doubleSided: boolean;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A plain data scene. `nodes` is what `exportGLB`, `exportUSDZ` and `exportOBJ` require.
|
|
35
|
+
* `mesh` is the mesh the primitives were split from, hard cubic unless one was passed.
|
|
36
|
+
*/
|
|
37
|
+
export interface VoxelExportSource {
|
|
38
|
+
/** One node holding every primitive. */
|
|
39
|
+
nodes: {
|
|
40
|
+
name?: string;
|
|
41
|
+
primitives: VoxelExportPrimitive[];
|
|
42
|
+
}[];
|
|
43
|
+
/** The quad mesh the primitives were split from. */
|
|
44
|
+
mesh: GridMesh;
|
|
45
|
+
/** The scene name, `options.name` or `'voxels'`. */
|
|
46
|
+
name?: string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Mesh a grid and split it by palette so `exportGLB(gridToExportSource(...))` writes one primitive per material.
|
|
50
|
+
* Vertex colors, when ambient occlusion is on, are the corner AO so a viewer darkens those corners.
|
|
51
|
+
*/
|
|
52
|
+
export declare function gridToExportSource(grid: VoxelGrid, materials: readonly ExportMaterial[], options?: {
|
|
53
|
+
name?: string;
|
|
54
|
+
ambientOcclusion?: boolean;
|
|
55
|
+
smoothNormals?: boolean;
|
|
56
|
+
/** A mesh already built from the grid, such as `smoothGrid`'s, used in place of the hard cubic one. */
|
|
57
|
+
mesh?: GridMesh;
|
|
58
|
+
}): VoxelExportSource;
|
package/dist/export.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { meshGrid as e, partitionMesh as t } from "./mesh.js";
|
|
2
|
+
//#region src/export.ts
|
|
3
|
+
function n(n, r, i = {}) {
|
|
4
|
+
let a = i.name ?? "voxels", o = i.mesh ?? e(n, {
|
|
5
|
+
ambientOcclusion: i.ambientOcclusion,
|
|
6
|
+
smoothNormals: i.smoothNormals
|
|
7
|
+
}).mesh, s = new Map(r.map((e) => [e.index, e]));
|
|
8
|
+
return {
|
|
9
|
+
nodes: [{
|
|
10
|
+
name: a,
|
|
11
|
+
primitives: t(o).map((e) => {
|
|
12
|
+
let t = s.get(e.paletteIndex), n = t?.color ?? [
|
|
13
|
+
1,
|
|
14
|
+
1,
|
|
15
|
+
1
|
|
16
|
+
], r;
|
|
17
|
+
if (e.ao.length) {
|
|
18
|
+
r = new Float32Array(e.ao.length * 3);
|
|
19
|
+
for (let t = 0; t < e.ao.length; t++) r.set([
|
|
20
|
+
e.ao[t],
|
|
21
|
+
e.ao[t],
|
|
22
|
+
e.ao[t]
|
|
23
|
+
], t * 3);
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
geometry: {
|
|
27
|
+
positions: e.positions,
|
|
28
|
+
normals: e.normals,
|
|
29
|
+
indices: e.indices,
|
|
30
|
+
...r ? { colors: r } : {}
|
|
31
|
+
},
|
|
32
|
+
material: {
|
|
33
|
+
name: `palette-${e.paletteIndex}`,
|
|
34
|
+
color: [
|
|
35
|
+
n[0],
|
|
36
|
+
n[1],
|
|
37
|
+
n[2],
|
|
38
|
+
1
|
|
39
|
+
],
|
|
40
|
+
roughness: t?.roughness ?? 1,
|
|
41
|
+
metalness: t?.metalness ?? 0,
|
|
42
|
+
doubleSided: !1
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
})
|
|
46
|
+
}],
|
|
47
|
+
mesh: o,
|
|
48
|
+
name: a
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
export { n as gridToExportSource };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { g as e } from "./grid-B2V0TGLV.js";
|
|
2
|
+
//#region src/file-colors.ts
|
|
3
|
+
var t = 4096, n = (e, t) => {
|
|
4
|
+
if (e instanceof ArrayBuffer) return new Uint8Array(e);
|
|
5
|
+
if (e instanceof Uint8Array) return e;
|
|
6
|
+
throw TypeError(`${t} needs a Uint8Array or ArrayBuffer; pass the file's bytes`);
|
|
7
|
+
}, r = (e) => {
|
|
8
|
+
throw RangeError(`${e} ends early; the file is cut off, export it again`);
|
|
9
|
+
}, i = class {
|
|
10
|
+
constructor(e) {
|
|
11
|
+
this.limit = e, this.models = [], this.used = 0;
|
|
12
|
+
}
|
|
13
|
+
add(n, r, i, a) {
|
|
14
|
+
if (this.models.length >= 4096) throw RangeError(`${a} holds over ${t} models; split the file`);
|
|
15
|
+
this.used += e(n, this.limit - this.used, `${a} size`);
|
|
16
|
+
let o = new Uint32Array(n[0] * n[1] * n[2]);
|
|
17
|
+
return this.models.push({
|
|
18
|
+
name: i,
|
|
19
|
+
position: r,
|
|
20
|
+
size: n,
|
|
21
|
+
cells: o
|
|
22
|
+
}), o;
|
|
23
|
+
}
|
|
24
|
+
finish() {
|
|
25
|
+
let e = /* @__PURE__ */ new Set();
|
|
26
|
+
for (let { cells: t } of this.models) for (let n of t) n && e.add(n);
|
|
27
|
+
let t = 0, n = (e) => e;
|
|
28
|
+
for (; new Set([...e].map(n)).size > 255;) {
|
|
29
|
+
let e = ++t, r = 1 << e - 1, i = (t) => t === 255 ? 255 : t >> e << e | r;
|
|
30
|
+
n = (e) => (i(e & 255) | i(e >>> 8 & 255) << 8 | i(e >>> 16 & 255) << 16 | i(e >>> 24) << 24) >>> 0;
|
|
31
|
+
}
|
|
32
|
+
let r = /* @__PURE__ */ new Uint8Array(1024), i = /* @__PURE__ */ new Map();
|
|
33
|
+
return {
|
|
34
|
+
models: this.models.map(({ name: e, position: t, size: a, cells: o }) => {
|
|
35
|
+
let s = new Uint8Array(o.length);
|
|
36
|
+
return o.forEach((e, t) => {
|
|
37
|
+
if (!e) return;
|
|
38
|
+
let a = n(e), o = i.get(a);
|
|
39
|
+
o === void 0 && (o = i.size + 1, i.set(a, o), new DataView(r.buffer).setUint32(o * 4, a, !0)), s[t] = o;
|
|
40
|
+
}), {
|
|
41
|
+
...e === void 0 ? {} : { name: e },
|
|
42
|
+
position: t,
|
|
43
|
+
grid: {
|
|
44
|
+
size: a,
|
|
45
|
+
voxels: s
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}),
|
|
49
|
+
palette: r,
|
|
50
|
+
colorsReduced: t > 0
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
}, a = (e, t, n, r) => (e | t << 8 | n << 16 | r << 24) >>> 0;
|
|
54
|
+
//#endregion
|
|
55
|
+
export { n as a, r as i, t as n, a as r, i as t };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Vec3 } from '@margoengine/math';
|
|
2
|
+
import type { VoxelFile } from './formats.js';
|
|
3
|
+
/** @internal Most models one file may hold. */
|
|
4
|
+
export declare const MAX_MODELS = 4096;
|
|
5
|
+
export declare const view: (bytes: unknown, subject: string) => Uint8Array;
|
|
6
|
+
export declare const short: (subject: string) => never;
|
|
7
|
+
/** Turns RGBA cells (0 is air) into grids sharing one palette, coarsening colours only when over 255 are used. */
|
|
8
|
+
export declare class Colors {
|
|
9
|
+
private readonly limit;
|
|
10
|
+
private readonly models;
|
|
11
|
+
private used;
|
|
12
|
+
constructor(limit: number);
|
|
13
|
+
add(size: Vec3, position: Vec3, name: string | undefined, subject: string): Uint32Array;
|
|
14
|
+
finish(): VoxelFile;
|
|
15
|
+
}
|
|
16
|
+
export declare const rgba: (r: number, g: number, b: number, a: number) => number;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Vec3 } from '@margoengine/math';
|
|
2
|
+
import type { VoxelFile } from './formats.js';
|
|
3
|
+
import { type GridLimits } from './grid.js';
|
|
4
|
+
export { parseVMAX, type VMAXFile, type VMAXNode, type VMAXModel, type VMAXOptions } from './vmax.js';
|
|
5
|
+
/** One KV6 model with the floating-point pivot retained in Y-up cell coordinates. */
|
|
6
|
+
export interface KV6File extends VoxelFile {
|
|
7
|
+
pivot: Vec3;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Reads KV6 surface records and implicit solid column interiors. File X stays X,
|
|
11
|
+
* downward Z becomes upward Y, file Y becomes Z. BGRA colour words are opaque;
|
|
12
|
+
* the fourth byte is lighting metadata, not alpha. Position is the negative pivot.
|
|
13
|
+
*/
|
|
14
|
+
export declare function parseKV6(input: Uint8Array | ArrayBuffer, limits?: GridLimits): KV6File;
|
|
15
|
+
/**
|
|
16
|
+
* Reads Cube World `.cub`: LE X/Y/Z sizes then RGB triples, X fastest,
|
|
17
|
+
* Y next and Z last; black is air. The file and grid are both Y up.
|
|
18
|
+
* CUB has no pivot or object placement, and no alpha channel.
|
|
19
|
+
*/
|
|
20
|
+
export declare function parseCUB(input: Uint8Array | ArrayBuffer, limits?: GridLimits): VoxelFile;
|