@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
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { MutableVec3 } from '@margoengine/math';
|
|
2
|
+
import { type GridLimits, type VoxelGrid } from './grid.js';
|
|
3
|
+
/** An RGBA8 image, the shape of `ImageData`: `width * height * 4` bytes, row 0 first (glTF's v = 0). */
|
|
4
|
+
export interface VoxelizeTexture {
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
data: Uint8Array | Uint8ClampedArray;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Where voxel colours come from. `vertex` holds RGB or RGBA per vertex, bytes in a `Uint8Array` and 0 to 1 otherwise.
|
|
11
|
+
* `texture` is read at nearest texel through `uvs` (two per vertex, repeating). Values reach the palette unconverted
|
|
12
|
+
* and alpha is ignored.
|
|
13
|
+
*/
|
|
14
|
+
export type VoxelizeColors = {
|
|
15
|
+
vertex: ArrayLike<number>;
|
|
16
|
+
} | {
|
|
17
|
+
uvs: ArrayLike<number>;
|
|
18
|
+
texture: VoxelizeTexture;
|
|
19
|
+
};
|
|
20
|
+
/** Options of {@link voxelizeMesh}. Pass exactly one of `voxelSize` and `size`. */
|
|
21
|
+
export interface VoxelizeOptions extends GridLimits {
|
|
22
|
+
/** Edge of one cubic cell, in mesh units. */
|
|
23
|
+
voxelSize?: number;
|
|
24
|
+
/** Cells along the mesh's longest axis, a positive integer; the other axes follow with cubic cells. */
|
|
25
|
+
size?: number;
|
|
26
|
+
/** `'surface'` fills every cell a triangle touches; `'solid'` every cell whose centre is inside. Default `'surface'`. */
|
|
27
|
+
fill?: 'surface' | 'solid';
|
|
28
|
+
/** Colour source. Default none: every filled cell is index 1, white. */
|
|
29
|
+
colors?: VoxelizeColors;
|
|
30
|
+
/** Most palette colours, 1 to 255. Default 255. */
|
|
31
|
+
paletteSize?: number;
|
|
32
|
+
/** Stops the work; the promise rejects with the signal's reason. */
|
|
33
|
+
signal?: AbortSignal;
|
|
34
|
+
}
|
|
35
|
+
/** What {@link voxelizeMesh} returns. Cell (x, y, z) covers `origin + [x, y, z] * voxelSize` to one `voxelSize` above. */
|
|
36
|
+
export interface VoxelizedMesh {
|
|
37
|
+
/** The voxels, Y up as in the mesh. */
|
|
38
|
+
grid: VoxelGrid;
|
|
39
|
+
/** 256 RGBA8 colours, index i at `4 * i`. */
|
|
40
|
+
palette: Uint8Array;
|
|
41
|
+
/** Mesh position of the grid's low corner: the low corner of the mesh's bounds. */
|
|
42
|
+
origin: MutableVec3;
|
|
43
|
+
/** Edge of one cell, in mesh units. */
|
|
44
|
+
voxelSize: number;
|
|
45
|
+
/** True when the mesh held more colours than `paletteSize`, so median cut merged some. */
|
|
46
|
+
colorsReduced: boolean;
|
|
47
|
+
/** With `'solid'`, x rows crossing the mesh an odd number of times: a hole in it. Their last crossing is ignored. */
|
|
48
|
+
openRows: number;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Turn a triangle mesh into a voxel grid and palette, yielding to the event loop while it works. The surface fill uses
|
|
52
|
+
* the triangle-box separating axis test; the solid fill counts crossings along each x row through cell centres, so a
|
|
53
|
+
* closed mesh stays exact even when a ray meets a shared edge.
|
|
54
|
+
*
|
|
55
|
+
* @param positions xyz per vertex.
|
|
56
|
+
* @param indices Three vertex indexes per triangle, or null for one triangle per three vertices.
|
|
57
|
+
* @param options Cell size, fill, colours, limits and abort.
|
|
58
|
+
* @returns The grid, its palette and where it sits.
|
|
59
|
+
* @throws RangeError when the mesh is empty or malformed or the grid would pass `maxCells`.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* import { voxelizeMesh } from '@margoengine/voxel-edit/voxelize';
|
|
64
|
+
*
|
|
65
|
+
* const positions = new Float32Array([0, 0, 0, 1, 0, 0, 0, 1, 0]);
|
|
66
|
+
* const { grid, palette } = await voxelizeMesh(positions, [0, 1, 2], { size: 16 });
|
|
67
|
+
* console.log(grid.size, palette.length);
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export declare function voxelizeMesh(positions: ArrayLike<number>, indices: ArrayLike<number> | null, options: VoxelizeOptions): Promise<VoxelizedMesh>;
|
package/dist/voxelize.js
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { g as e, l as t, o as n } from "./grid-B2V0TGLV.js";
|
|
2
|
+
//#region src/voxelize.ts
|
|
3
|
+
var r = [
|
|
4
|
+
16,
|
|
5
|
+
8,
|
|
6
|
+
0
|
|
7
|
+
];
|
|
8
|
+
async function i(r, i, m) {
|
|
9
|
+
t(m, "voxelizeMesh");
|
|
10
|
+
let { signal: h, colors: g, fill: _ = "surface", paletteSize: v = 255, voxelSize: y, size: b } = m;
|
|
11
|
+
if (h?.throwIfAborted(), _ !== "surface" && _ !== "solid") throw TypeError("fill is unknown; pass 'surface' or 'solid'");
|
|
12
|
+
if (!Number.isInteger(v) || v < 1 || v > 255) throw RangeError("paletteSize is out of range; pass an integer from 1 to 255");
|
|
13
|
+
if (y === void 0 == (b === void 0)) throw TypeError("voxelizeMesh needs one cell size; pass voxelSize or size, not both");
|
|
14
|
+
let x = r.length / 3;
|
|
15
|
+
if (!Number.isInteger(x)) throw RangeError("positions do not hold xyz triples; pass 3 per vertex");
|
|
16
|
+
let S = (i ? i.length : x) / 3;
|
|
17
|
+
if (!Number.isInteger(S) || S === 0) throw RangeError("The mesh holds no whole triangles; pass 3 indexes per triangle");
|
|
18
|
+
let C = (e, t) => i ? i[3 * e + t] : 3 * e + t, w = [
|
|
19
|
+
Infinity,
|
|
20
|
+
Infinity,
|
|
21
|
+
Infinity
|
|
22
|
+
], T = [
|
|
23
|
+
-Infinity,
|
|
24
|
+
-Infinity,
|
|
25
|
+
-Infinity
|
|
26
|
+
];
|
|
27
|
+
for (let e = 0; e < S * 3; e++) {
|
|
28
|
+
let t = i ? i[e] : e;
|
|
29
|
+
if (!Number.isInteger(t) || t < 0 || t >= x) throw RangeError(`Index ${t} is outside the ${x} vertices; fix the indices`);
|
|
30
|
+
for (let e = 0; e < 3; e++) {
|
|
31
|
+
let n = r[3 * t + e];
|
|
32
|
+
if (!Number.isFinite(n)) throw RangeError(`Vertex ${t} is not finite; pass finite positions`);
|
|
33
|
+
n < w[e] && (w[e] = n), n > T[e] && (T[e] = n);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
let E = Math.max(T[0] - w[0], T[1] - w[1], T[2] - w[2]);
|
|
37
|
+
if (b !== void 0 && (!Number.isSafeInteger(b) || b < 1)) throw RangeError("size is not a cell count; pass a positive integer");
|
|
38
|
+
if (y !== void 0 && !(y > 0 && Number.isFinite(y))) throw RangeError("voxelSize is not positive; pass a positive finite number");
|
|
39
|
+
let D = y ?? (E / b || 1), O = [
|
|
40
|
+
0,
|
|
41
|
+
1,
|
|
42
|
+
2
|
|
43
|
+
].map((e) => {
|
|
44
|
+
let t = (T[e] - w[e]) / D;
|
|
45
|
+
return Math.max(1, Math.ceil(t - 1e-6 * Math.max(1, t)));
|
|
46
|
+
}), k = e(O, n(m), "Voxelized grid"), [A, j, M] = O, N = new Uint8Array(k), P = new Float64Array(S * 9);
|
|
47
|
+
for (let e = 0; e < S; e++) for (let t = 0; t < 3; t++) for (let n = 0; n < 3; n++) P[9 * e + 3 * t + n] = (r[3 * C(e, t) + n] - w[n]) / D;
|
|
48
|
+
let F = g ? f(g, x, C) : null, I = F ? new Uint32Array(k) : null, L = F ? new Float32Array(k) : null, R = 0, z = performance.now(), B = async () => {
|
|
49
|
+
performance.now() - z < 16 || (await new Promise((e) => setTimeout(e)), h?.throwIfAborted(), z = performance.now());
|
|
50
|
+
};
|
|
51
|
+
if (_ === "surface" || F) {
|
|
52
|
+
let e = /* @__PURE__ */ new Float64Array(60);
|
|
53
|
+
for (let t = 0; t < S; t++) {
|
|
54
|
+
t & 255 || await B(), s(P, 9 * t, e);
|
|
55
|
+
let n = [
|
|
56
|
+
0,
|
|
57
|
+
1,
|
|
58
|
+
2
|
|
59
|
+
].map((e) => Math.floor(a(P, 9 * t, e))), r = [
|
|
60
|
+
0,
|
|
61
|
+
1,
|
|
62
|
+
2
|
|
63
|
+
].map((e) => Math.min(O[e] - 1, Math.floor(o(P, 9 * t, e))));
|
|
64
|
+
for (let e = 0; e < 3; e++) n[e] = Math.min(n[e], O[e] - 1);
|
|
65
|
+
for (let i = n[1]; i <= r[1]; i++) for (let a = n[2]; a <= r[2]; a++) {
|
|
66
|
+
++R & 4095 || await B();
|
|
67
|
+
for (let o = n[0]; o <= r[0]; o++) {
|
|
68
|
+
if (!c(e, o + .5, i + .5, a + .5)) continue;
|
|
69
|
+
let n = o + A * (a + M * i);
|
|
70
|
+
if (_ === "surface" && (N[n] = 1), F && I && L) {
|
|
71
|
+
let e = d(P, t, o + .5, i + .5, a + .5, F);
|
|
72
|
+
(!I[n] || e.distance < L[n]) && (I[n] = e.packed, L[n] = e.distance);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
let V = 0;
|
|
79
|
+
if (_ === "solid") {
|
|
80
|
+
let e = /* @__PURE__ */ new Map();
|
|
81
|
+
for (let t = 0; t < S; t++) {
|
|
82
|
+
t & 255 || await B();
|
|
83
|
+
let n = 9 * t, r = Math.max(0, Math.ceil(a(P, n, 1) - .5)), i = Math.min(j - 1, Math.floor(o(P, n, 1) - .5)), s = Math.max(0, Math.ceil(a(P, n, 2) - .5)), c = Math.min(M - 1, Math.floor(o(P, n, 2) - .5));
|
|
84
|
+
for (let a = r; a <= i; a++) for (let r = s; r <= c; r++) {
|
|
85
|
+
if (!l(P, n, a + .5, r + .5)) continue;
|
|
86
|
+
let i = r + M * a, o = e.get(i);
|
|
87
|
+
o || e.set(i, o = []), o.push([u(P, n, a + .5, r + .5), t]);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
for (let [t, n] of e) {
|
|
91
|
+
++R & 255 || await B(), n.sort((e, t) => e[0] - t[0]), n.length & 1 && V++;
|
|
92
|
+
let e = t % M, r = (t - e) / M;
|
|
93
|
+
for (let t = 0; t + 1 < n.length; t += 2) {
|
|
94
|
+
let i = n[t], a = n[t + 1], o = Math.min(A - 1, Math.ceil(a[0] - .5) - 1);
|
|
95
|
+
for (let t = Math.max(0, Math.ceil(i[0] - .5)); t <= o; t++) {
|
|
96
|
+
let n = t + A * (e + M * r);
|
|
97
|
+
N[n] = 1, F && I && !I[n] && (I[n] = d(P, (t + .5 - i[0] <= a[0] - t - .5 ? i : a)[1], t + .5, r + .5, e + .5, F).packed);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
let H = /* @__PURE__ */ new Uint8Array(1024), U = !1;
|
|
103
|
+
if (I) {
|
|
104
|
+
let e = /* @__PURE__ */ new Map();
|
|
105
|
+
for (let t = 0; t < k; t++) N[t] && e.set(I[t], (e.get(I[t]) ?? 0) + 1);
|
|
106
|
+
U = e.size > v;
|
|
107
|
+
let t = p(e, v, H);
|
|
108
|
+
for (let e = 0; e < k; e++) N[e] && (N[e] = t.get(I[e]));
|
|
109
|
+
} else H.fill(255, 4, 8);
|
|
110
|
+
return h?.throwIfAborted(), {
|
|
111
|
+
grid: {
|
|
112
|
+
size: O,
|
|
113
|
+
voxels: N
|
|
114
|
+
},
|
|
115
|
+
palette: H,
|
|
116
|
+
origin: w,
|
|
117
|
+
voxelSize: D,
|
|
118
|
+
colorsReduced: U,
|
|
119
|
+
openRows: V
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
function a(e, t, n) {
|
|
123
|
+
return Math.min(e[t + n], e[t + 3 + n], e[t + 6 + n]);
|
|
124
|
+
}
|
|
125
|
+
function o(e, t, n) {
|
|
126
|
+
return Math.max(e[t + n], e[t + 3 + n], e[t + 6 + n]);
|
|
127
|
+
}
|
|
128
|
+
function s(e, t, n) {
|
|
129
|
+
let r = [
|
|
130
|
+
e[t + 3] - e[t],
|
|
131
|
+
e[t + 6] - e[t + 3],
|
|
132
|
+
e[t] - e[t + 6]
|
|
133
|
+
], i = [
|
|
134
|
+
e[t + 4] - e[t + 1],
|
|
135
|
+
e[t + 7] - e[t + 4],
|
|
136
|
+
e[t + 1] - e[t + 7]
|
|
137
|
+
], a = [
|
|
138
|
+
e[t + 5] - e[t + 2],
|
|
139
|
+
e[t + 8] - e[t + 5],
|
|
140
|
+
e[t + 2] - e[t + 8]
|
|
141
|
+
], o = 0, s = (r, i, a) => {
|
|
142
|
+
let s = r * e[t] + i * e[t + 1] + a * e[t + 2], c = r * e[t + 3] + i * e[t + 4] + a * e[t + 5], l = r * e[t + 6] + i * e[t + 7] + a * e[t + 8];
|
|
143
|
+
n[o] = r, n[o + 1] = i, n[o + 2] = a, n[o + 3] = Math.min(s, c, l), n[o + 4] = Math.max(s, c, l), n[o + 5] = (Math.abs(r) + Math.abs(i) + Math.abs(a)) / 2, o += 6;
|
|
144
|
+
};
|
|
145
|
+
for (let e = 0; e < 3; e++) s(0, -a[e], i[e]), s(a[e], 0, -r[e]), s(-i[e], r[e], 0);
|
|
146
|
+
s(i[0] * a[1] - a[0] * i[1], a[0] * r[1] - r[0] * a[1], r[0] * i[1] - i[0] * r[1]);
|
|
147
|
+
}
|
|
148
|
+
function c(e, t, n, r) {
|
|
149
|
+
for (let i = 0; i < 60; i += 6) {
|
|
150
|
+
let a = e[i] * t + e[i + 1] * n + e[i + 2] * r;
|
|
151
|
+
if (e[i + 3] - a > e[i + 5] || e[i + 4] - a < -e[i + 5]) return !1;
|
|
152
|
+
}
|
|
153
|
+
return !0;
|
|
154
|
+
}
|
|
155
|
+
function l(e, t, n, r) {
|
|
156
|
+
let i = !1;
|
|
157
|
+
for (let a = 0; a < 3; a++) {
|
|
158
|
+
let o = t + 3 * a, s = t + 3 * ((a + 1) % 3);
|
|
159
|
+
(e[o + 1] > e[s + 1] || e[o + 1] === e[s + 1] && e[o + 2] > e[s + 2]) && ([o, s] = [s, o]);
|
|
160
|
+
let c = e[o + 2], l = e[s + 2];
|
|
161
|
+
c <= r != l <= r && e[o + 1] + (r - c) * (e[s + 1] - e[o + 1]) / (l - c) > n && (i = !i);
|
|
162
|
+
}
|
|
163
|
+
return i;
|
|
164
|
+
}
|
|
165
|
+
function u(e, t, n, r) {
|
|
166
|
+
let i = e[t + 4] - e[t + 1], s = e[t + 5] - e[t + 2], c = e[t + 7] - e[t + 1], l = e[t + 8] - e[t + 2], u = i * l - s * c, d = n - e[t + 1], f = r - e[t + 2], p = u ? (d * l - f * c) / u : 1 / 3, m = u ? (f * i - d * s) / u : 1 / 3, h = e[t] + p * (e[t + 3] - e[t]) + m * (e[t + 6] - e[t]);
|
|
167
|
+
return Math.min(o(e, t, 0), Math.max(a(e, t, 0), h));
|
|
168
|
+
}
|
|
169
|
+
function d(e, t, n, r, i, a) {
|
|
170
|
+
let o = 9 * t, s = e[o + 3] - e[o], c = e[o + 4] - e[o + 1], l = e[o + 5] - e[o + 2], u = e[o + 6] - e[o], d = e[o + 7] - e[o + 1], f = e[o + 8] - e[o + 2], p = n - e[o], m = r - e[o + 1], h = i - e[o + 2], g = s * s + c * c + l * l, _ = s * u + c * d + l * f, v = u * u + d * d + f * f, y = p * s + m * c + h * l, b = p * u + m * d + h * f, x = g * v - _ * _, S = x ? (v * y - _ * b) / x : 0, C = x ? (g * b - _ * y) / x : 0, w = 1 - S - C;
|
|
171
|
+
w = Math.max(0, w), S = Math.max(0, S), C = Math.max(0, C);
|
|
172
|
+
let T = w + S + C;
|
|
173
|
+
w /= T, S /= T, C /= T;
|
|
174
|
+
let E = S * s + C * u - p, D = S * c + C * d - m, O = S * l + C * f - h;
|
|
175
|
+
return {
|
|
176
|
+
packed: a(t, w, S, C),
|
|
177
|
+
distance: E * E + D * D + O * O
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
function f(e, t, n) {
|
|
181
|
+
if (typeof e != "object" || !e) throw TypeError("colors is not a colour source; pass {vertex} or {uvs, texture}");
|
|
182
|
+
let r = (e, t, n) => (4278190080 | e << 16 | t << 8 | n) >>> 0;
|
|
183
|
+
if ("vertex" in e) {
|
|
184
|
+
let i = e.vertex, a = i.length / t, o = i instanceof Uint8Array || i instanceof Uint8ClampedArray ? 1 : 255;
|
|
185
|
+
if (a !== 3 && a !== 4) throw RangeError(`colors.vertex holds ${i.length} values; pass 3 or 4 per vertex`);
|
|
186
|
+
let s = (e, t, r, s, c) => {
|
|
187
|
+
let l = t * i[a * n(e, 0) + c] + r * i[a * n(e, 1) + c] + s * i[a * n(e, 2) + c];
|
|
188
|
+
return Math.min(255, Math.max(0, Math.round(l * o)));
|
|
189
|
+
};
|
|
190
|
+
return (e, t, n, i) => r(s(e, t, n, i, 0), s(e, t, n, i, 1), s(e, t, n, i, 2));
|
|
191
|
+
}
|
|
192
|
+
let { uvs: i, texture: a } = e;
|
|
193
|
+
if (i?.length !== t * 2) throw RangeError("colors.uvs does not match the mesh; pass 2 per vertex");
|
|
194
|
+
let { width: o, height: s, data: c } = a ?? {};
|
|
195
|
+
if (!Number.isSafeInteger(o) || !Number.isSafeInteger(s) || o < 1 || s < 1 || c?.length !== 4 * o * s) throw RangeError("colors.texture is not RGBA8; pass width, height and width * height * 4 bytes");
|
|
196
|
+
let l = (e, t, r, a, o) => {
|
|
197
|
+
let s = t * i[2 * n(e, 0) + o] + r * i[2 * n(e, 1) + o] + a * i[2 * n(e, 2) + o];
|
|
198
|
+
return s - Math.floor(s);
|
|
199
|
+
};
|
|
200
|
+
return (e, t, n, i) => {
|
|
201
|
+
let a = Math.min(o - 1, Math.floor(l(e, t, n, i, 0) * o)), u = Math.min(s - 1, Math.floor(l(e, t, n, i, 1) * s)), d = 4 * (a + o * u);
|
|
202
|
+
return r(c[d], c[d + 1], c[d + 2]);
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
function p(e, t, n) {
|
|
206
|
+
let i = (e) => {
|
|
207
|
+
let t = 0, n = 0;
|
|
208
|
+
for (let i of r) {
|
|
209
|
+
let r = 255, a = 0;
|
|
210
|
+
for (let [t] of e) {
|
|
211
|
+
let e = t >>> i & 255;
|
|
212
|
+
e < r && (r = e), e > a && (a = e);
|
|
213
|
+
}
|
|
214
|
+
a - r > t && (t = a - r, n = i);
|
|
215
|
+
}
|
|
216
|
+
return {
|
|
217
|
+
box: e,
|
|
218
|
+
range: t,
|
|
219
|
+
channel: n
|
|
220
|
+
};
|
|
221
|
+
}, a = [...e].sort((e, t) => e[0] - t[0]), o = a.length ? [i(a)] : [];
|
|
222
|
+
for (; o.length < t;) {
|
|
223
|
+
let e = 0;
|
|
224
|
+
for (let t = 1; t < o.length; t++) o[t].range > o[e].range && (e = t);
|
|
225
|
+
if (!o[e]?.range) break;
|
|
226
|
+
let { box: t, channel: n } = o[e];
|
|
227
|
+
t.sort((e, t) => (e[0] >>> n & 255) - (t[0] >>> n & 255));
|
|
228
|
+
let r = 0;
|
|
229
|
+
for (let [, e] of t) r += e;
|
|
230
|
+
let a = 0, s = 0;
|
|
231
|
+
for (; a < t.length - 2 && s + t[a][1] < r / 2;) s += t[a++][1];
|
|
232
|
+
let c = a + 1;
|
|
233
|
+
o[e] = i(t.slice(0, c)), o.push(i(t.slice(c)));
|
|
234
|
+
}
|
|
235
|
+
let s = /* @__PURE__ */ new Map();
|
|
236
|
+
return o.forEach(({ box: e }, t) => {
|
|
237
|
+
let i = 0, a = [
|
|
238
|
+
0,
|
|
239
|
+
0,
|
|
240
|
+
0
|
|
241
|
+
];
|
|
242
|
+
for (let [n, o] of e) i += o, r.forEach((e, t) => {
|
|
243
|
+
a[t] += o * (n >>> e & 255);
|
|
244
|
+
}), s.set(n, t + 1);
|
|
245
|
+
n.set([...a.map((e) => Math.round(e / i)), 255], 4 * (t + 1));
|
|
246
|
+
}), s;
|
|
247
|
+
}
|
|
248
|
+
//#endregion
|
|
249
|
+
export { i as voxelizeMesh };
|
package/dist/vxm.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { Vec3 } from '@margoengine/math';
|
|
2
|
+
import type { VoxelFile, VoxelFileModel } from './formats.js';
|
|
3
|
+
import { type GridLimits } from './grid.js';
|
|
4
|
+
/** One model of a `.vxm` file. */
|
|
5
|
+
export interface VXMModel extends VoxelFileModel {
|
|
6
|
+
/** The model's `visible` flag (version 12 on); true before. */
|
|
7
|
+
visible: boolean;
|
|
8
|
+
}
|
|
9
|
+
/** What {@link parseVXM} returns. */
|
|
10
|
+
export interface VXMFile extends VoxelFile {
|
|
11
|
+
/** Models in file order (one before version 12), all of one size, each at [0, 0, 0]. */
|
|
12
|
+
models: VXMModel[];
|
|
13
|
+
/** The file's version, 3 to 13. */
|
|
14
|
+
version: number;
|
|
15
|
+
/** The pivot as the file stores it: a fraction of the size on each axis. [0.5, 0, 0.5] before version 5. */
|
|
16
|
+
pivot: Vec3;
|
|
17
|
+
/** 256 flags, 1 where that palette index is an emissive material (version 4 on). */
|
|
18
|
+
emissive: Uint8Array;
|
|
19
|
+
}
|
|
20
|
+
/** A model for {@link writeVXM}: any {@link VoxelFileModel}, with an optional `visible` (default true). */
|
|
21
|
+
export interface VXMWriteModel extends VoxelFileModel {
|
|
22
|
+
visible?: boolean;
|
|
23
|
+
}
|
|
24
|
+
/** Options of {@link writeVXM}. */
|
|
25
|
+
export interface VXMWriteOptions {
|
|
26
|
+
/** The pivot as a fraction of the size on each axis. Default [0.5, 0, 0.5]: centred in x and z, at the bottom. */
|
|
27
|
+
pivot?: Vec3;
|
|
28
|
+
/** Palette indices whose material emits light (any non-zero entry of a 256-entry array). Default none. */
|
|
29
|
+
emissive?: ArrayLike<number>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Reads a VoxEdit `.vxm` of version 3 to 13: every model with its name, the materials as the palette (material i is
|
|
33
|
+
* palette index i + 1, BGRA in the file) and the pivot. The baked mesh and the full palettes are skipped. Cells run Z
|
|
34
|
+
* fastest, then Y, then X; file cell (x, y, z) lands in grid cell (size x - 1 - x, y, z), the mirror existing files
|
|
35
|
+
* need, with Y up. A run naming a material the file lacks is air. Bytes after the models are not read.
|
|
36
|
+
*
|
|
37
|
+
* Only versions 4 and 12 were checked against real files; the others follow the specification alone.
|
|
38
|
+
*
|
|
39
|
+
* @throws TypeError when the bytes are not a `.vxm`; RangeError on a cut-off or damaged file, an unknown version or
|
|
40
|
+
* past `maxCells` across all models.
|
|
41
|
+
*/
|
|
42
|
+
export declare function parseVXM(bytes: Uint8Array | ArrayBuffer, limits?: GridLimits): VXMFile;
|
|
43
|
+
/**
|
|
44
|
+
* Writes a VoxEdit `.vxm` of version 12 (`VXMC`): every model (at most 255, all of one size; `position` is not stored),
|
|
45
|
+
* palette index i as material i - 1 up to the highest index used, no baked mesh, and the full palettes VoxEdit keeps
|
|
46
|
+
* (unused entries magenta). Cells are written in the order and mirror {@link parseVXM} reads, so a round trip gives
|
|
47
|
+
* back the same grids and colours.
|
|
48
|
+
*
|
|
49
|
+
* Whether VoxEdit itself opens a file without a baked mesh is not known; other tools that read `.vxm` do.
|
|
50
|
+
*
|
|
51
|
+
* @throws RangeError when the palette is not 1,024 bytes, the models differ in size or number over 255, or a name
|
|
52
|
+
* holds a NUL byte or is over 1,023 bytes.
|
|
53
|
+
*/
|
|
54
|
+
export declare function writeVXM(models: VXMWriteModel | readonly VXMWriteModel[], palette: Uint8Array, options?: VXMWriteOptions): Uint8Array;
|
package/package.json
CHANGED
|
@@ -1,6 +1,132 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@margoengine/voxel-edit",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Editing voxel models for editors and tools: dense and sparse grids, modeling operations, brushes and selections, picking, export meshing, .vox scene editing and frame animation.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"margoengine",
|
|
7
|
+
"webgpu",
|
|
8
|
+
"3d",
|
|
9
|
+
"voxel",
|
|
10
|
+
"edit"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://margoengine.com",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/scottcampbelldata/margoengine/issues"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/scottcampbelldata/margoengine.git",
|
|
19
|
+
"directory": "packages/voxel-edit"
|
|
20
|
+
},
|
|
21
|
+
"license": "Apache-2.0",
|
|
22
|
+
"author": "Scott Campbell",
|
|
23
|
+
"type": "module",
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"import": "./dist/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./edit": {
|
|
31
|
+
"types": "./dist/edit.d.ts",
|
|
32
|
+
"import": "./dist/edit.js"
|
|
33
|
+
},
|
|
34
|
+
"./pick": {
|
|
35
|
+
"types": "./dist/pick.d.ts",
|
|
36
|
+
"import": "./dist/pick.js"
|
|
37
|
+
},
|
|
38
|
+
"./sparse": {
|
|
39
|
+
"types": "./dist/sparse.d.ts",
|
|
40
|
+
"import": "./dist/sparse.js"
|
|
41
|
+
},
|
|
42
|
+
"./mesh": {
|
|
43
|
+
"types": "./dist/mesh.d.ts",
|
|
44
|
+
"import": "./dist/mesh.js"
|
|
45
|
+
},
|
|
46
|
+
"./vox": {
|
|
47
|
+
"types": "./dist/vox.d.ts",
|
|
48
|
+
"import": "./dist/vox.js"
|
|
49
|
+
},
|
|
50
|
+
"./animation": {
|
|
51
|
+
"types": "./dist/animation.d.ts",
|
|
52
|
+
"import": "./dist/animation.js"
|
|
53
|
+
},
|
|
54
|
+
"./history": {
|
|
55
|
+
"types": "./dist/history.d.ts",
|
|
56
|
+
"import": "./dist/history.js"
|
|
57
|
+
},
|
|
58
|
+
"./export": {
|
|
59
|
+
"types": "./dist/export.d.ts",
|
|
60
|
+
"import": "./dist/export.js"
|
|
61
|
+
},
|
|
62
|
+
"./formats": {
|
|
63
|
+
"types": "./dist/formats.d.ts",
|
|
64
|
+
"import": "./dist/formats.js"
|
|
65
|
+
},
|
|
66
|
+
"./assist": {
|
|
67
|
+
"types": "./dist/assist.d.ts",
|
|
68
|
+
"import": "./dist/assist.js"
|
|
69
|
+
},
|
|
70
|
+
"./library": {
|
|
71
|
+
"types": "./dist/library.d.ts",
|
|
72
|
+
"import": "./dist/library.js"
|
|
73
|
+
},
|
|
74
|
+
"./smooth": {
|
|
75
|
+
"types": "./dist/smooth.d.ts",
|
|
76
|
+
"import": "./dist/smooth.js"
|
|
77
|
+
},
|
|
78
|
+
"./voxelize": {
|
|
79
|
+
"types": "./dist/voxelize.d.ts",
|
|
80
|
+
"import": "./dist/voxelize.js"
|
|
81
|
+
},
|
|
82
|
+
"./shaders": {
|
|
83
|
+
"types": "./dist/shaders.d.ts",
|
|
84
|
+
"import": "./dist/shaders.js"
|
|
85
|
+
},
|
|
86
|
+
"./rig": {
|
|
87
|
+
"types": "./dist/rig.d.ts",
|
|
88
|
+
"import": "./dist/rig.js"
|
|
89
|
+
},
|
|
90
|
+
"./gpu": {
|
|
91
|
+
"types": "./dist/gpu.d.ts",
|
|
92
|
+
"import": "./dist/gpu.js"
|
|
93
|
+
},
|
|
94
|
+
"./collab": {
|
|
95
|
+
"types": "./dist/collab.d.ts",
|
|
96
|
+
"import": "./dist/collab.js"
|
|
97
|
+
},
|
|
98
|
+
"./generate": {
|
|
99
|
+
"types": "./dist/generate.d.ts",
|
|
100
|
+
"import": "./dist/generate.js"
|
|
101
|
+
},
|
|
102
|
+
"./select": {
|
|
103
|
+
"types": "./dist/select.d.ts",
|
|
104
|
+
"import": "./dist/select.js"
|
|
105
|
+
},
|
|
106
|
+
"./formats-extra": {
|
|
107
|
+
"types": "./dist/formats-extra.d.ts",
|
|
108
|
+
"import": "./dist/formats-extra.js"
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
"files": [
|
|
112
|
+
"dist",
|
|
113
|
+
"LICENSE",
|
|
114
|
+
"NOTICE"
|
|
115
|
+
],
|
|
116
|
+
"publishConfig": {
|
|
117
|
+
"access": "public"
|
|
118
|
+
},
|
|
119
|
+
"dependencies": {
|
|
120
|
+
"@margoengine/decoders": "^1.0.0",
|
|
121
|
+
"@margoengine/math": "^1.0.0",
|
|
122
|
+
"@margoengine/voxel": "^1.0.0",
|
|
123
|
+
"@margoengine/loaders": "^1.0.0"
|
|
124
|
+
},
|
|
125
|
+
"scripts": {
|
|
126
|
+
"build": "node ../../scripts/build-optional.js voxel-edit",
|
|
127
|
+
"test": "npm run build && node --test tests/*.test.js",
|
|
128
|
+
"test:gpu": "cd ../.. && node packages/voxel-edit/tests/shaders-browser-check.mjs && node packages/voxel-edit/tests/gpu-browser-check.mjs",
|
|
129
|
+
"prepack": "npm run build",
|
|
130
|
+
"test:browser:generate": "cd ../.. && node packages/voxel-edit/tests/generate-browser-check.mjs"
|
|
131
|
+
}
|
|
132
|
+
}
|