@margoengine/voxel-edit 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.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +446 -3
- 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 +127 -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-CcSeamtF.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 +132 -6
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { c as e, l as t, o as n, p as r, s as i } from "./grid-B2V0TGLV.js";
|
|
2
|
+
import { MargoError as a } from "@margoengine/math/errors";
|
|
3
|
+
//#region src/brush-gpu.ts
|
|
4
|
+
var o = "struct Info {\n size: vec4<i32>,\n count: u32,\n every: u32,\n pass_index: u32,\n seed: u32,\n tool_size: vec4<i32>,\n tool_offset: vec4<i32>,\n params: array<vec4<f32>, 4>,\n}\n@group(0) @binding(0) var<uniform> info: Info;\n@group(0) @binding(1) var<storage, read> source_cells: array<u32>;\n@group(0) @binding(2) var<storage, read_write> result_cells: array<u32>;\n@group(0) @binding(3) var<storage, read> selected_cells: array<u32>;\n@group(0) @binding(4) var<storage, read> tool_cells: array<u32>;\n@group(0) @binding(5) var<storage, read> lookup_table: array<u32, 256>;\nfn grid_size() -> vec3<i32> { return info.size.xyz; }\nfn inside(q: vec3<i32>) -> bool { return all(q >= vec3<i32>(0)) && all(q < info.size.xyz); }\nfn cell_index(q: vec3<i32>) -> i32 { return q.x + info.size.x * (q.z + info.size.z * q.y); }\nfn cell(q: vec3<i32>) -> u32 {\n if (!inside(q)) { return 0u; }\n let i = u32(cell_index(q));\n return (source_cells[i >> 2u] >> ((i & 3u) * 8u)) & 255u;\n}\nfn tool(q: vec3<i32>) -> u32 {\n let t = q - info.tool_offset.xyz;\n if (info.tool_size.w == 0 || any(t < vec3<i32>(0)) || any(t >= info.tool_size.xyz)) { return 0u; }\n let i = u32(t.x + info.tool_size.x * (t.z + info.tool_size.z * t.y));\n return (tool_cells[i >> 2u] >> ((i & 3u) * 8u)) & 255u;\n}\nfn lookup(i: u32) -> u32 { return lookup_table[min(i, 255u)]; }\nfn face_offset(d: i32) -> vec3<i32> {\n switch d {\n case 0: { return vec3<i32>(-1, 0, 0); }\n case 1: { return vec3<i32>(1, 0, 0); }\n case 2: { return vec3<i32>(0, -1, 0); }\n case 3: { return vec3<i32>(0, 1, 0); }\n case 4: { return vec3<i32>(0, 0, -1); }\n default: { return vec3<i32>(0, 0, 1); }\n }\n}\nfn param(i: u32) -> f32 {\n let j = min(i, 15u);\n return info.params[j >> 2u][j & 3u];\n}\nfn pass_number() -> u32 { return info.pass_index; }\nfn hash_u32(x: u32) -> u32 {\n var h = x * 747796405u + 2891336453u;\n h = ((h >> ((h >> 28u) + 4u)) ^ h) * 277803737u;\n return (h >> 22u) ^ h;\n}\nfn hash(q: vec3<i32>, salt: u32) -> f32 {\n let h = hash_u32(bitcast<u32>(q.x) ^ hash_u32(bitcast<u32>(q.y) ^ hash_u32(bitcast<u32>(q.z) ^ hash_u32(salt ^ info.seed))));\n return f32(h >> 8u) / 16777216.0;\n}\nfn noise(x: vec3<f32>) -> f32 {\n let c = floor(x);\n let f = x - c;\n let u = f * f * (3.0 - 2.0 * f);\n let i = vec3<i32>(c);\n let a = mix(hash(i, 0u), hash(i + vec3<i32>(1, 0, 0), 0u), u.x);\n let b = mix(hash(i + vec3<i32>(0, 1, 0), 0u), hash(i + vec3<i32>(1, 1, 0), 0u), u.x);\n let d = mix(hash(i + vec3<i32>(0, 0, 1), 0u), hash(i + vec3<i32>(1, 0, 1), 0u), u.x);\n let e = mix(hash(i + vec3<i32>(0, 1, 1), 0u), hash(i + vec3<i32>(1, 1, 1), 0u), u.x);\n return mix(mix(a, b, u.y), mix(d, e, u.y), u.z);\n}\n", s = "\n@compute @workgroup_size(64)\nfn brush_main(@builtin(global_invocation_id) id: vec3<u32>, @builtin(num_workgroups) groups: vec3<u32>) {\n let w = id.x + id.y * groups.x * 64u;\n if (w * 4u >= info.count) { return; }\n let row = info.size.x * info.size.z;\n var out = 0u;\n for (var k = 0u; k < 4u; k++) {\n let i = w * 4u + k;\n if (i >= info.count) { break; }\n var v = (source_cells[w] >> (k * 8u)) & 255u;\n if (info.every == 1u || ((selected_cells[w] >> (k * 8u)) & 255u) != 0u) {\n let n = i32(i);\n let y = n / row;\n let z = (n - y * row) / info.size.x;\n v = min(brush(vec3<i32>(n - y * row - z * info.size.x, y, z), v), 255u);\n }\n out |= v << (k * 8u);\n }\n result_cells[w] = out;\n}\n", c = o.split("\n").length - 1, l = /* @__PURE__ */ new WeakMap();
|
|
5
|
+
function u(e) {
|
|
6
|
+
if (typeof e != "string" || !/\bfn\s+brush\s*\(/.test(e)) throw TypeError("Shader brush code must be WGSL defining fn brush(p: vec3<i32>, value: u32) -> u32; pass one");
|
|
7
|
+
return o + e + s;
|
|
8
|
+
}
|
|
9
|
+
var d = (e) => typeof e?.createShaderModule == "function" && typeof e?.createComputePipeline == "function";
|
|
10
|
+
async function f(e, t, n) {
|
|
11
|
+
e.pushErrorScope("validation");
|
|
12
|
+
let r = e.createShaderModule({
|
|
13
|
+
label: n,
|
|
14
|
+
code: u(t)
|
|
15
|
+
}), i = await r.getCompilationInfo(), o = await e.popErrorScope(), s = i.messages.filter((e) => e.type === "error");
|
|
16
|
+
if (o && !s.length) throw new a("failed", `Shader brush module is invalid: ${o.message}; fix the WGSL`);
|
|
17
|
+
if (s.length) {
|
|
18
|
+
let e = s[0], t = e.lineNum - c;
|
|
19
|
+
throw new a("failed", `Shader brush code does not compile: ${e.message} (${t > 0 ? `line ${t} of the brush code` : "in the prelude"}); fix the WGSL`);
|
|
20
|
+
}
|
|
21
|
+
let l = (e, t) => ({
|
|
22
|
+
binding: e,
|
|
23
|
+
visibility: GPUShaderStage.COMPUTE,
|
|
24
|
+
buffer: { type: t }
|
|
25
|
+
}), d = e.createBindGroupLayout({
|
|
26
|
+
label: n,
|
|
27
|
+
entries: [
|
|
28
|
+
l(0, "uniform"),
|
|
29
|
+
l(1, "read-only-storage"),
|
|
30
|
+
l(2, "storage"),
|
|
31
|
+
l(3, "read-only-storage"),
|
|
32
|
+
l(4, "read-only-storage"),
|
|
33
|
+
l(5, "read-only-storage")
|
|
34
|
+
]
|
|
35
|
+
});
|
|
36
|
+
return e.createComputePipelineAsync({
|
|
37
|
+
label: n,
|
|
38
|
+
layout: e.createPipelineLayout({
|
|
39
|
+
label: n,
|
|
40
|
+
bindGroupLayouts: [d]
|
|
41
|
+
}),
|
|
42
|
+
compute: {
|
|
43
|
+
module: r,
|
|
44
|
+
entryPoint: "brush_main"
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
function p(t, n, r, a) {
|
|
49
|
+
let { selection: o, passes: s = 1, params: c = [], seed: l = 0, tool: u, toolOffset: d = [
|
|
50
|
+
0,
|
|
51
|
+
0,
|
|
52
|
+
0
|
|
53
|
+
], table: f } = t;
|
|
54
|
+
if (!Number.isInteger(s) || s < 1 || s > 1024) throw RangeError(`${a} passes is ${s}; pass a whole number from 1 to 1024`);
|
|
55
|
+
if (!Array.isArray(c) || c.length > 16 || !c.every((e) => typeof e == "number" && Number.isFinite(e))) throw TypeError(`${a} params must be up to 16 finite numbers; pass an array such as [0.1, 0.5, 3]`);
|
|
56
|
+
if (!Number.isInteger(l) || l < 0 || l > 4294967295) throw RangeError(`${a} seed is ${l}; pass a whole number from 0 to 4294967295`);
|
|
57
|
+
if (u !== void 0 && e(u, r, `${a} tool`), i(d, `${a} toolOffset`), f !== void 0) {
|
|
58
|
+
if (typeof f?.length != "number" || f.length !== 256) throw TypeError(`${a} table must hold 256 numbers; pass a Uint8Array(256)`);
|
|
59
|
+
for (let e = 0; e < 256; e++) if (!Number.isInteger(f[e]) || f[e] < 0 || f[e] > 255) throw RangeError(`${a} table[${e}] is ${f[e]}; pass whole numbers 0 to 255`);
|
|
60
|
+
}
|
|
61
|
+
let p = null;
|
|
62
|
+
if (o !== void 0) {
|
|
63
|
+
if (typeof o?.length != "number") throw TypeError(`${a} selection must be cell indexes; pass a Float64Array`);
|
|
64
|
+
p = new Uint8Array(Math.ceil(n / 4) * 4);
|
|
65
|
+
for (let e = 0; e < o.length; e++) {
|
|
66
|
+
let t = o[e];
|
|
67
|
+
if (!Number.isInteger(t) || t < 0 || t >= n) throw RangeError(`${a} selection holds ${t}, not a cell index of this grid; pass 0 to ${n - 1}`);
|
|
68
|
+
p[t] = 1;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
passes: s,
|
|
73
|
+
params: c,
|
|
74
|
+
seed: l,
|
|
75
|
+
mask: p,
|
|
76
|
+
tool: u ?? null,
|
|
77
|
+
toolOffset: d,
|
|
78
|
+
table: f ?? null
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function m(e, t, n) {
|
|
82
|
+
let r = Math.ceil(t / 4) * 4;
|
|
83
|
+
if (r > e.limits.maxStorageBufferBindingSize) throw RangeError(`${n} grid of ${t} cells needs ${r} bytes, over this device's ${e.limits.maxStorageBufferBindingSize}; split the grid`);
|
|
84
|
+
return r;
|
|
85
|
+
}
|
|
86
|
+
var h = () => GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST, g = class {
|
|
87
|
+
#e;
|
|
88
|
+
#t;
|
|
89
|
+
#n;
|
|
90
|
+
#r = 0;
|
|
91
|
+
#i;
|
|
92
|
+
#a;
|
|
93
|
+
#o = !1;
|
|
94
|
+
constructor(e, t, n) {
|
|
95
|
+
let r = t.voxels.length;
|
|
96
|
+
this.#e = e, this.size = [
|
|
97
|
+
t.size[0],
|
|
98
|
+
t.size[1],
|
|
99
|
+
t.size[2]
|
|
100
|
+
], this.count = r, this.#t = m(e, r, n), this.#n = [0, 1].map((t) => e.createBuffer({
|
|
101
|
+
label: `${n} cells ${t}`,
|
|
102
|
+
size: this.#t,
|
|
103
|
+
usage: h()
|
|
104
|
+
})), this.#i = e.createBuffer({
|
|
105
|
+
label: `${n} info`,
|
|
106
|
+
size: 128,
|
|
107
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
108
|
+
}), this.#a = e.createBuffer({
|
|
109
|
+
label: `${n} none`,
|
|
110
|
+
size: 1024,
|
|
111
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
|
|
112
|
+
}), this.write(t.voxels);
|
|
113
|
+
}
|
|
114
|
+
get buffer() {
|
|
115
|
+
return this.#n[this.#r];
|
|
116
|
+
}
|
|
117
|
+
write(e) {
|
|
118
|
+
let t = new Uint8Array(this.#t);
|
|
119
|
+
t.set(e), this.#e.queue.writeBuffer(this.#n[this.#r], 0, t);
|
|
120
|
+
}
|
|
121
|
+
run(e, t) {
|
|
122
|
+
let n = this.#e, r = this.#t / 4, i = Math.ceil(r / 64), a = Math.min(i, 65535), o = Math.ceil(i / a), s = [], c = (e, t) => {
|
|
123
|
+
let r = n.createBuffer({
|
|
124
|
+
label: t,
|
|
125
|
+
size: Math.max(16, Math.ceil(e.byteLength / 4) * 4),
|
|
126
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
|
|
127
|
+
}), i = new Uint8Array(r.size);
|
|
128
|
+
return i.set(new Uint8Array(e.buffer, e.byteOffset, e.byteLength)), n.queue.writeBuffer(r, 0, i), s.push(r), r;
|
|
129
|
+
}, l = t.mask ? c(t.mask, "brush selection") : this.#a, u = t.tool ? (m(n, t.tool.voxels.length, "Brush tool"), c(t.tool.voxels, "brush tool")) : this.#a, d = t.table ? c(Uint32Array.from(t.table), "brush table") : this.#a, f = /* @__PURE__ */ new ArrayBuffer(128), p = new Int32Array(f), h = new Uint32Array(f), g = new Float32Array(f);
|
|
130
|
+
p.set(this.size, 0), h[4] = this.count, h[5] = +!t.mask, h[7] = t.seed, t.tool && (p.set(t.tool.size, 8), p[11] = 1), p.set(t.toolOffset, 12), g.set(t.params, 16);
|
|
131
|
+
let _ = e.getBindGroupLayout(0), v = [0, 1].map((e) => n.createBindGroup({
|
|
132
|
+
layout: _,
|
|
133
|
+
entries: [
|
|
134
|
+
{
|
|
135
|
+
binding: 0,
|
|
136
|
+
resource: { buffer: this.#i }
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
binding: 1,
|
|
140
|
+
resource: { buffer: this.#n[e] }
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
binding: 2,
|
|
144
|
+
resource: { buffer: this.#n[1 - e] }
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
binding: 3,
|
|
148
|
+
resource: { buffer: l }
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
binding: 4,
|
|
152
|
+
resource: { buffer: u }
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
binding: 5,
|
|
156
|
+
resource: { buffer: d }
|
|
157
|
+
}
|
|
158
|
+
]
|
|
159
|
+
}));
|
|
160
|
+
for (let r = 0; r < t.passes; r++) {
|
|
161
|
+
h[6] = r, n.queue.writeBuffer(this.#i, 0, f);
|
|
162
|
+
let t = n.createCommandEncoder({ label: "brush pass" }), i = t.beginComputePass();
|
|
163
|
+
i.setPipeline(e), i.setBindGroup(0, v[this.#r]), i.dispatchWorkgroups(a, o), i.end(), n.queue.submit([t.finish()]), this.#r = 1 - this.#r;
|
|
164
|
+
}
|
|
165
|
+
s.length && n.queue.onSubmittedWorkDone().then(() => {
|
|
166
|
+
for (let e of s) e.destroy();
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
async read(e) {
|
|
170
|
+
let t = this.#e, n = t.createBuffer({
|
|
171
|
+
label: "brush read",
|
|
172
|
+
size: this.#t,
|
|
173
|
+
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST
|
|
174
|
+
}), r = t.createCommandEncoder({ label: "brush read" });
|
|
175
|
+
try {
|
|
176
|
+
return r.copyBufferToBuffer(this.#n[this.#r], 0, n, 0, this.#t), t.queue.submit([r.finish()]), await n.mapAsync(GPUMapMode.READ), e?.throwIfAborted(), new Uint8Array(n.getMappedRange().slice(0, this.count));
|
|
177
|
+
} finally {
|
|
178
|
+
n.destroy();
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
destroy() {
|
|
182
|
+
if (!this.#o) {
|
|
183
|
+
this.#o = !0;
|
|
184
|
+
for (let e of [
|
|
185
|
+
...this.#n,
|
|
186
|
+
this.#i,
|
|
187
|
+
this.#a
|
|
188
|
+
]) e.destroy();
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}, _ = {
|
|
192
|
+
grow: "\nfn brush(p: vec3<i32>, value: u32) -> u32 {\n if (value != 0u) { return value; }\n var best = 0u;\n var first = 2147483647;\n for (var d = 0; d < 6; d++) {\n let q = p + face_offset(d);\n let v = cell(q);\n if (v != 0u && cell_index(q) < first) { first = cell_index(q); best = v; }\n }\n if (best != 0u && param(0) >= 1.0) { return u32(param(0)); }\n return best;\n}",
|
|
193
|
+
erode: "\nfn brush(p: vec3<i32>, value: u32) -> u32 {\n if (value == 0u) { return 0u; }\n for (var d = 0; d < 6; d++) { if (cell(p + face_offset(d)) == 0u) { return 0u; } }\n return value;\n}",
|
|
194
|
+
noise: "\nfn brush(p: vec3<i32>, value: u32) -> u32 {\n if (noise((vec3<f32>(p) + 0.5) * param(0)) > param(1)) { return u32(param(2)); }\n return value;\n}",
|
|
195
|
+
smooth: "\nfn brush(p: vec3<i32>, value: u32) -> u32 {\n var filled = 0;\n for (var z = -1; z <= 1; z++) {\n for (var y = -1; y <= 1; y++) {\n for (var x = -1; x <= 1; x++) { if (cell(p + vec3<i32>(x, y, z)) != 0u) { filled++; } }\n }\n }\n if (filled < 14) { return 0u; }\n if (value != 0u) { return value; }\n var best = 0u;\n var most = 0;\n for (var z = -1; z <= 1; z++) {\n for (var y = -1; y <= 1; y++) {\n for (var x = -1; x <= 1; x++) {\n let v = cell(p + vec3<i32>(x, y, z));\n if (v == 0u) { continue; }\n var n = 0;\n for (var c = -1; c <= 1; c++) {\n for (var b = -1; b <= 1; b++) {\n for (var a = -1; a <= 1; a++) { if (cell(p + vec3<i32>(a, b, c)) == v) { n++; } }\n }\n }\n if (n > most || (n == most && v < best)) { most = n; best = v; }\n }\n }\n }\n return best;\n}"
|
|
196
|
+
}, v = class i {
|
|
197
|
+
#e;
|
|
198
|
+
#t;
|
|
199
|
+
#n = !1;
|
|
200
|
+
constructor(e, t, n) {
|
|
201
|
+
this.#e = e, this.code = t, this.#t = n, l.set(this, {
|
|
202
|
+
device: e,
|
|
203
|
+
pipeline: n
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
static async create(e, t, n = {}) {
|
|
207
|
+
if (!d(e)) throw TypeError("ShaderBrush.create needs a GPUDevice; pass (await adapter.requestDevice())");
|
|
208
|
+
u(t), n?.signal?.throwIfAborted();
|
|
209
|
+
let r = n?.label ?? "shader brush";
|
|
210
|
+
if (typeof r != "string") throw TypeError("ShaderBrush label must be a string; pass a name for GPU debugging");
|
|
211
|
+
let a = await f(e, t, r);
|
|
212
|
+
return n?.signal?.throwIfAborted(), new i(e, t, a);
|
|
213
|
+
}
|
|
214
|
+
async run(i, o = {}) {
|
|
215
|
+
if (this.#n) throw new a("disposed", "ShaderBrush is disposed; create a new one");
|
|
216
|
+
t(o, "ShaderBrush.run");
|
|
217
|
+
let s = n(o), c = p(o, e(i, s), s, "ShaderBrush"), l = o.signal;
|
|
218
|
+
l?.throwIfAborted();
|
|
219
|
+
let u = new g(this.#e, i, "ShaderBrush");
|
|
220
|
+
try {
|
|
221
|
+
return u.run(this.#t, c), r(i, await u.read(l), 0);
|
|
222
|
+
} finally {
|
|
223
|
+
u.destroy();
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
dispose() {
|
|
227
|
+
this.#n = !0, l.delete(this);
|
|
228
|
+
}
|
|
229
|
+
[Symbol.dispose]() {
|
|
230
|
+
this.dispose();
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
//#endregion
|
|
234
|
+
export { l as a, d as c, p as i, _ as n, u as o, g as r, f as s, v as t };
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { Vec3 } from '@margoengine/math';
|
|
3
|
+
import { brushSource } from './brush-gpu.js';
|
|
4
|
+
import { type GridEdit, type GridLimits, type VoxelGrid } from './grid.js';
|
|
5
|
+
export { brushSource };
|
|
6
|
+
/** Options of {@link ShaderBrush.run}. */
|
|
7
|
+
export interface ShaderBrushRunOptions extends GridLimits {
|
|
8
|
+
/** Cell indexes the brush runs on (any order, repeats allowed); the rest are copied. Default every cell. */
|
|
9
|
+
selection?: ArrayLike<number>;
|
|
10
|
+
/** Passes, 1 to 1,024; each reads the result of the one before. Default 1. */
|
|
11
|
+
passes?: number;
|
|
12
|
+
/** Up to 16 numbers the code reads as `param(0)` to `param(15)` (f32). Default all 0. */
|
|
13
|
+
params?: readonly number[];
|
|
14
|
+
/** Integer 0 to 2^32 - 1 the code's `hash` and `noise` mix in, so one brush gives different patterns. Default 0. */
|
|
15
|
+
seed?: number;
|
|
16
|
+
/** A second grid the code reads with `tool(q)` (its index at base cell q, 0 outside it). Default none. */
|
|
17
|
+
tool?: VoxelGrid;
|
|
18
|
+
/** Where the tool's cell (0, 0, 0) sits in the grid, in whole cells. Default [0, 0, 0]. */
|
|
19
|
+
toolOffset?: Vec3;
|
|
20
|
+
/** 256 numbers 0 to 255 the code reads with `lookup(i)`, such as a palette remap. Default all 0. */
|
|
21
|
+
table?: ArrayLike<number>;
|
|
22
|
+
/** Aborts the run before the GPU work is read back; the run then rejects with the signal's reason. */
|
|
23
|
+
signal?: AbortSignal;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Ready-made brush code. `grow` and `erode` give exactly `dilateGrid` and `erodeGrid` with all six directions (grow
|
|
27
|
+
* takes the colour of the first neighbour in grid order, or `param(0)` when it is 1 to 255); `noise` fills cells where
|
|
28
|
+
* value noise at `p * param(0)` exceeds `param(1)` with colour `param(2)`; `smooth` sets each cell to the filled or empty
|
|
29
|
+
* state of most of its 26 neighbours and itself (filled cells taking the most common neighbour colour, the lowest on a
|
|
30
|
+
* tie).
|
|
31
|
+
*/
|
|
32
|
+
export declare const brushes: {
|
|
33
|
+
readonly grow: `
|
|
34
|
+
fn brush(p: vec3<i32>, value: u32) -> u32 {
|
|
35
|
+
if (value != 0u) { return value; }
|
|
36
|
+
var best = 0u;
|
|
37
|
+
var first = 2147483647;
|
|
38
|
+
for (var d = 0; d < 6; d++) {
|
|
39
|
+
let q = p + face_offset(d);
|
|
40
|
+
let v = cell(q);
|
|
41
|
+
if (v != 0u && cell_index(q) < first) { first = cell_index(q); best = v; }
|
|
42
|
+
}
|
|
43
|
+
if (best != 0u && param(0) >= 1.0) { return u32(param(0)); }
|
|
44
|
+
return best;
|
|
45
|
+
}`;
|
|
46
|
+
readonly erode: `
|
|
47
|
+
fn brush(p: vec3<i32>, value: u32) -> u32 {
|
|
48
|
+
if (value == 0u) { return 0u; }
|
|
49
|
+
for (var d = 0; d < 6; d++) { if (cell(p + face_offset(d)) == 0u) { return 0u; } }
|
|
50
|
+
return value;
|
|
51
|
+
}`;
|
|
52
|
+
readonly noise: `
|
|
53
|
+
fn brush(p: vec3<i32>, value: u32) -> u32 {
|
|
54
|
+
if (noise((vec3<f32>(p) + 0.5) * param(0)) > param(1)) { return u32(param(2)); }
|
|
55
|
+
return value;
|
|
56
|
+
}`;
|
|
57
|
+
readonly smooth: `
|
|
58
|
+
fn brush(p: vec3<i32>, value: u32) -> u32 {
|
|
59
|
+
var filled = 0;
|
|
60
|
+
for (var z = -1; z <= 1; z++) {
|
|
61
|
+
for (var y = -1; y <= 1; y++) {
|
|
62
|
+
for (var x = -1; x <= 1; x++) { if (cell(p + vec3<i32>(x, y, z)) != 0u) { filled++; } }
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (filled < 14) { return 0u; }
|
|
66
|
+
if (value != 0u) { return value; }
|
|
67
|
+
var best = 0u;
|
|
68
|
+
var most = 0;
|
|
69
|
+
for (var z = -1; z <= 1; z++) {
|
|
70
|
+
for (var y = -1; y <= 1; y++) {
|
|
71
|
+
for (var x = -1; x <= 1; x++) {
|
|
72
|
+
let v = cell(p + vec3<i32>(x, y, z));
|
|
73
|
+
if (v == 0u) { continue; }
|
|
74
|
+
var n = 0;
|
|
75
|
+
for (var c = -1; c <= 1; c++) {
|
|
76
|
+
for (var b = -1; b <= 1; b++) {
|
|
77
|
+
for (var a = -1; a <= 1; a++) { if (cell(p + vec3<i32>(a, b, c)) == v) { n++; } }
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (n > most || (n == most && v < best)) { most = n; best = v; }
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return best;
|
|
85
|
+
}`;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* A compiled shader brush: user WGSL defining `fn brush(p: vec3<i32>, value: u32) -> u32`, run on the GPU for every
|
|
89
|
+
* selected cell of a grid. `p` is the cell, `value` its palette index (0 air); the return value (clamped to 255) is the
|
|
90
|
+
* cell's new index. The code may call `cell(q)` (index at any cell, 0 outside), `inside(q)`, `cell_index(q)`,
|
|
91
|
+
* `grid_size()`, `face_offset(0..5)` (-x, +x, -y, +y, -z, +z), `param(i)`, `pass_number()`, `tool(q)`, `lookup(i)`,
|
|
92
|
+
* `hash(q, salt)` (0 to 1) and `noise(x)` (value noise 0 to 1). Every cell of a pass reads the grid as it was before that pass.
|
|
93
|
+
*/
|
|
94
|
+
export declare class ShaderBrush {
|
|
95
|
+
#private;
|
|
96
|
+
readonly code: string;
|
|
97
|
+
private constructor();
|
|
98
|
+
/**
|
|
99
|
+
* Compiles `code` on `device`; `label` (default 'shader brush') names its GPU objects in debuggers and errors.
|
|
100
|
+
* @throws TypeError when `device` is not a GPUDevice or `code` defines no `fn brush`.
|
|
101
|
+
* @throws MargoError `'failed'` when the code does not compile, naming the line of `code` and WGSL's message.
|
|
102
|
+
*/
|
|
103
|
+
static create(device: GPUDevice, code: string, options?: {
|
|
104
|
+
signal?: AbortSignal;
|
|
105
|
+
label?: string;
|
|
106
|
+
}): Promise<ShaderBrush>;
|
|
107
|
+
/**
|
|
108
|
+
* Runs the brush over `grid` (unchanged) and resolves to the new grid, the cells that changed and `clipped` 0.
|
|
109
|
+
* @throws TypeError or RangeError for a bad grid or option, or a grid larger than the device's storage binding limit.
|
|
110
|
+
* @throws MargoError `'disposed'` after dispose(); rejects with the signal's reason when aborted.
|
|
111
|
+
*/
|
|
112
|
+
run(grid: VoxelGrid, options?: ShaderBrushRunOptions): Promise<GridEdit>;
|
|
113
|
+
/** Lets the pipeline go; `run` then throws. Calling it again does nothing. */
|
|
114
|
+
dispose(): void;
|
|
115
|
+
[Symbol.dispose](): void;
|
|
116
|
+
}
|
package/dist/shaders.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { t as ShaderBrush, o as brushSource, n as brushes } from "./shaders-CcSeamtF.js";
|
package/dist/slices.d.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { MutableVec3 } from '@margoengine/math';
|
|
2
|
+
import { type GridLimits, type VoxelGrid } from './grid.js';
|
|
3
|
+
/** Decoded RGBA pixels, 4 bytes per pixel, rows top to bottom (an `ImageData`'s layout). */
|
|
4
|
+
export interface SlicePixels {
|
|
5
|
+
/** Pixels per row, a positive integer. */
|
|
6
|
+
width: number;
|
|
7
|
+
/** Rows, a positive integer. */
|
|
8
|
+
height: number;
|
|
9
|
+
/** `width * height * 4` bytes: red, green, blue, alpha. */
|
|
10
|
+
data: Uint8Array | Uint8ClampedArray;
|
|
11
|
+
}
|
|
12
|
+
/** How {@link stackSlices} reads the frames and turns them into cells. */
|
|
13
|
+
export interface SliceOptions extends GridLimits {
|
|
14
|
+
/** Frame width in pixels; must divide the image width. */
|
|
15
|
+
frameWidth: number;
|
|
16
|
+
/** Frame height in pixels; must divide the image height. */
|
|
17
|
+
frameHeight: number;
|
|
18
|
+
/** Frames per row of the sheet, from 1 to `width / frameWidth`. Default `width / frameWidth`. */
|
|
19
|
+
columns?: number;
|
|
20
|
+
/** Frames to stack, read row by row (left to right, then down), from 1 to `columns * rows`. Default all. */
|
|
21
|
+
frameCount?: number;
|
|
22
|
+
/**
|
|
23
|
+
* The axis frames stack along. `'y'` (default): frame pixel (column, row) lands at (column, depth, row), so image
|
|
24
|
+
* rows run along +z. `'z'`: (column, frameHeight - 1 - row, depth), the image upright. `'x'`: (depth,
|
|
25
|
+
* frameHeight - 1 - row, column).
|
|
26
|
+
*/
|
|
27
|
+
axis?: 'x' | 'y' | 'z';
|
|
28
|
+
/** Frame 0 at the top (true, default: depth `frameCount - 1 - frame`) or at the bottom (false: depth `frame`). */
|
|
29
|
+
topToBottom?: boolean;
|
|
30
|
+
/** Pixels with alpha below this, 1 to 255, are air. Default 1 (only fully transparent pixels are air). */
|
|
31
|
+
alphaThreshold?: number;
|
|
32
|
+
/**
|
|
33
|
+
* The palette policy. Without it, each distinct RGBA colour kept gets the next index from 1 in reading order and
|
|
34
|
+
* more than 255 colours are refused. With a 1,024-byte RGBA palette (index i at `4 * i`), each colour takes the
|
|
35
|
+
* first index from 1 with exactly its bytes, and a colour it lacks is refused. Nothing is ever approximated.
|
|
36
|
+
*/
|
|
37
|
+
palette?: Uint8Array;
|
|
38
|
+
}
|
|
39
|
+
/** The checked arithmetic of a slice layout ({@link sliceLayout}). */
|
|
40
|
+
export interface SliceLayout {
|
|
41
|
+
frameWidth: number;
|
|
42
|
+
frameHeight: number;
|
|
43
|
+
columns: number;
|
|
44
|
+
rows: number;
|
|
45
|
+
frameCount: number;
|
|
46
|
+
/** `columns * rows`. */
|
|
47
|
+
availableFrames: number;
|
|
48
|
+
axis: 'x' | 'y' | 'z';
|
|
49
|
+
topToBottom: boolean;
|
|
50
|
+
alphaThreshold: number;
|
|
51
|
+
/** The grid's size. */
|
|
52
|
+
size: MutableVec3;
|
|
53
|
+
/** The grid's cell count. */
|
|
54
|
+
cells: number;
|
|
55
|
+
}
|
|
56
|
+
/** What {@link stackSlices} returns. */
|
|
57
|
+
export interface SliceStack {
|
|
58
|
+
grid: VoxelGrid;
|
|
59
|
+
/** 1,024 RGBA bytes, index i at `4 * i`; index 0 and unused indexes are 0 unless a palette was given. */
|
|
60
|
+
palette: Uint8Array;
|
|
61
|
+
/** Distinct indexes the grid uses. */
|
|
62
|
+
colors: number;
|
|
63
|
+
layout: SliceLayout;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Checks a slice layout against an image of `width` x `height` pixels without reading or allocating anything: frame
|
|
67
|
+
* sizes divide the image, counts fit the sheet, and the image and the grid stay within `maxCells`.
|
|
68
|
+
*/
|
|
69
|
+
export declare function sliceLayout(width: number, height: number, options: SliceOptions): SliceLayout;
|
|
70
|
+
/**
|
|
71
|
+
* Stacks sprite frames into an indexed grid, Y up: frame `f` of the sheet becomes one layer along `axis`, its kept
|
|
72
|
+
* pixels cells with their exact RGBA colour's palette index and transparent pixels air (empty frames keep their
|
|
73
|
+
* layer). Every colour is checked against the palette policy before the grid is allocated.
|
|
74
|
+
*/
|
|
75
|
+
export declare function stackSlices(pixels: SlicePixels, options: SliceOptions): SliceStack;
|
package/dist/smooth.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Vec3 } from '@margoengine/math';
|
|
2
|
+
import { type GridLimits, type VoxelGrid } from './grid.js';
|
|
3
|
+
import type { GridMesh, MeshStats } from './mesh.js';
|
|
4
|
+
/** Options of {@link smoothGrid}. */
|
|
5
|
+
export interface SmoothGridOptions extends GridLimits {
|
|
6
|
+
/** Relaxation passes moving each vertex toward its neighbours' mean, always inside its net cell, 0 to 64. Default 2. */
|
|
7
|
+
iterations?: number;
|
|
8
|
+
/** Box blur radius in cells over the occupancy, 0 to 8. Rounder shapes; parts thinner than `2 * blur + 1` fade. Default 0. */
|
|
9
|
+
blur?: number;
|
|
10
|
+
/** Point subtracted from every vertex, in cells (the model's pivot). Default [0, 0, 0]. */
|
|
11
|
+
pivot?: Vec3;
|
|
12
|
+
/** Most quads, 0 to 1,000,000. Default 1,000,000. */
|
|
13
|
+
maxQuads?: number;
|
|
14
|
+
}
|
|
15
|
+
/** Counts of a {@link smoothGrid} call. */
|
|
16
|
+
export interface SmoothStats extends MeshStats {
|
|
17
|
+
/** Distinct surface points. Each quad still has its own 4 mesh vertices, so `vertices` is `4 * quads`. */
|
|
18
|
+
surfaceVertices: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Mesh a grid into a smooth surface with Surface Nets: one point per net cell the surface passes through, one quad per
|
|
22
|
+
* filled-to-air cell pair. Normals follow the density gradient and each quad takes the palette index of the nearest
|
|
23
|
+
* filled voxel. The result has the {@link GridMesh} shape, so `partitionMesh` and `gridToExportSource` take it. The grid
|
|
24
|
+
* is read, never changed. Cells touching only along an edge or corner share a surface point there.
|
|
25
|
+
*
|
|
26
|
+
* @param grid The voxels.
|
|
27
|
+
* @param options Relaxation, blur, pivot and limits.
|
|
28
|
+
* @returns The mesh, in cells minus the pivot, and its counts.
|
|
29
|
+
* @throws RangeError when the grid or quad count passes its limit or an option is out of range.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { smoothGrid } from '@margoengine/voxel-edit/smooth';
|
|
34
|
+
*
|
|
35
|
+
* const grid = { size: [2, 2, 2] as const, voxels: new Uint8Array(8).fill(1) };
|
|
36
|
+
* const { mesh, stats } = smoothGrid(grid, { iterations: 4 });
|
|
37
|
+
* console.log(mesh.positions.length, stats.quads);
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare function smoothGrid(grid: VoxelGrid, options?: SmoothGridOptions): {
|
|
41
|
+
mesh: GridMesh;
|
|
42
|
+
stats: SmoothStats;
|
|
43
|
+
};
|
package/dist/smooth.js
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { c as e, d as t, g as n, l as r, o as i } from "./grid-B2V0TGLV.js";
|
|
2
|
+
//#region src/smooth.ts
|
|
3
|
+
var a = 1e6;
|
|
4
|
+
function o(o, c = {}) {
|
|
5
|
+
r(c, "smoothGrid");
|
|
6
|
+
let { iterations: l = 2, blur: u = 0, pivot: d = [
|
|
7
|
+
0,
|
|
8
|
+
0,
|
|
9
|
+
0
|
|
10
|
+
], maxQuads: f = a } = c;
|
|
11
|
+
if (!Number.isInteger(l) || l < 0 || l > 64) throw RangeError("iterations is out of range; pass an integer from 0 to 64");
|
|
12
|
+
if (!Number.isInteger(u) || u < 0 || u > 8) throw RangeError("blur is out of range; pass an integer from 0 to 8");
|
|
13
|
+
if (!Number.isInteger(f) || f < 0 || f > a) throw RangeError(`maxQuads is out of range; pass an integer from 0 to ${a}`);
|
|
14
|
+
t(d, "pivot");
|
|
15
|
+
let p = i(c);
|
|
16
|
+
e(o, p);
|
|
17
|
+
let [m, h, g] = o.size, { voxels: _ } = o, v = m + 2, y = h + 2, b = g + 2, x = new Float32Array(n([
|
|
18
|
+
v,
|
|
19
|
+
y,
|
|
20
|
+
b
|
|
21
|
+
], p, "Padded grid")), S = v, C = v * b, w = 0;
|
|
22
|
+
for (let e = 0, t = 0; e < h; e++) for (let n = 0; n < g; n++) for (let r = 0; r < m; r++, t++) _[t] && (w++, x[r + 1 + S * (n + 1) + C * (e + 1)] = 1);
|
|
23
|
+
u && s(x, [
|
|
24
|
+
v,
|
|
25
|
+
y,
|
|
26
|
+
b
|
|
27
|
+
], u);
|
|
28
|
+
let T = m + 1, E = h + 1, D = g + 1, O = new Int32Array(T * E * D).fill(-1), k = [], A = [], j = [
|
|
29
|
+
0,
|
|
30
|
+
1,
|
|
31
|
+
S,
|
|
32
|
+
S + 1,
|
|
33
|
+
C,
|
|
34
|
+
C + 1,
|
|
35
|
+
C + S,
|
|
36
|
+
C + S + 1
|
|
37
|
+
], M = [
|
|
38
|
+
1,
|
|
39
|
+
4,
|
|
40
|
+
2
|
|
41
|
+
];
|
|
42
|
+
for (let e = 0, t = 0; e < E; e++) for (let n = 0; n < D; n++) for (let r = 0; r < T; r++, t++) {
|
|
43
|
+
let i = r + S * n + C * e, a = 0;
|
|
44
|
+
for (let e = 0; e < 8; e++) x[i + j[e]] >= .5 && (a |= 1 << e);
|
|
45
|
+
if (a === 0 || a === 255) continue;
|
|
46
|
+
let o = [
|
|
47
|
+
0,
|
|
48
|
+
0,
|
|
49
|
+
0
|
|
50
|
+
], s = [
|
|
51
|
+
0,
|
|
52
|
+
0,
|
|
53
|
+
0
|
|
54
|
+
], c = 0;
|
|
55
|
+
for (let e = 0; e < 8; e++) {
|
|
56
|
+
let t = x[i + j[e]];
|
|
57
|
+
for (let n = 0; n < 3; n++) {
|
|
58
|
+
let r = e & M[n];
|
|
59
|
+
if (s[n] += r ? t : -t, r || !(a & 1 << e) == !(a & 1 << (e | M[n]))) continue;
|
|
60
|
+
let l = (.5 - t) / (x[i + j[e | M[n]]] - t);
|
|
61
|
+
c++;
|
|
62
|
+
for (let t = 0; t < 3; t++) o[t] += (e & M[t] ? 1 : 0) + (t === n ? l : 0);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
O[t] = k.length / 3, k.push(r - .5 + o[0] / c, e - .5 + o[1] / c, n - .5 + o[2] / c), A.push(0 - s[0], 0 - s[1], 0 - s[2]);
|
|
66
|
+
}
|
|
67
|
+
let N = k.length / 3, P = [], F = [], I = (e, t, n) => O[e + T * (n + D * t)], L = [
|
|
68
|
+
1,
|
|
69
|
+
C,
|
|
70
|
+
S
|
|
71
|
+
], R = (e, t, n) => {
|
|
72
|
+
if (e >= 0 && t >= 0 && n >= 0 && e < m && t < h && n < g && _[e + m * (n + g * t)]) return _[e + m * (n + g * t)];
|
|
73
|
+
let r = 0, i = Infinity;
|
|
74
|
+
for (let a = Math.max(0, t - u); a <= Math.min(h - 1, t + u); a++) for (let o = Math.max(0, n - u); o <= Math.min(g - 1, n + u); o++) for (let s = Math.max(0, e - u); s <= Math.min(m - 1, e + u); s++) {
|
|
75
|
+
let c = _[s + m * (o + g * a)], l = (s - e) ** 2 + (a - t) ** 2 + (o - n) ** 2;
|
|
76
|
+
c && l < i && (i = l, r = c);
|
|
77
|
+
}
|
|
78
|
+
return r;
|
|
79
|
+
};
|
|
80
|
+
for (let e = 0; e < y; e++) for (let t = 0; t < b; t++) for (let n = 0; n < v; n++) {
|
|
81
|
+
let r = n + S * t + C * e, i = x[r] >= .5;
|
|
82
|
+
for (let a = 0; a < 3; a++) {
|
|
83
|
+
let o = [
|
|
84
|
+
n,
|
|
85
|
+
e,
|
|
86
|
+
t
|
|
87
|
+
];
|
|
88
|
+
if (o[a] + 1 >= [
|
|
89
|
+
v,
|
|
90
|
+
y,
|
|
91
|
+
b
|
|
92
|
+
][a] || i === x[r + L[a]] >= .5) continue;
|
|
93
|
+
if (P.length / 4 >= f) throw RangeError(`The mesh passes maxQuads (${f}); raise maxQuads or pass a smaller grid`);
|
|
94
|
+
let s = (a + 1) % 3, c = (a + 2) % 3, l = (r, i) => {
|
|
95
|
+
let a = [
|
|
96
|
+
n,
|
|
97
|
+
e,
|
|
98
|
+
t
|
|
99
|
+
];
|
|
100
|
+
return a[s] += r - 1, a[c] += i - 1, I(a[0], a[1], a[2]);
|
|
101
|
+
}, u = [
|
|
102
|
+
l(0, 0),
|
|
103
|
+
l(1, 0),
|
|
104
|
+
l(1, 1),
|
|
105
|
+
l(0, 1)
|
|
106
|
+
];
|
|
107
|
+
i || u.reverse(), P.push(...u);
|
|
108
|
+
let d = i ? o : [
|
|
109
|
+
n,
|
|
110
|
+
e,
|
|
111
|
+
t
|
|
112
|
+
].map((e, t) => t === a ? e + 1 : e);
|
|
113
|
+
F.push(R(d[0] - 1, d[1] - 1, d[2] - 1));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
let z = F.length, B = Float64Array.from(k);
|
|
117
|
+
if (l) {
|
|
118
|
+
let e = new Float64Array(N * 3), t = new Uint32Array(N);
|
|
119
|
+
for (let n = 0; n < l; n++) {
|
|
120
|
+
e.fill(0), t.fill(0);
|
|
121
|
+
for (let n = 0; n < z; n++) for (let r = 0; r < 4; r++) {
|
|
122
|
+
let i = P[4 * n + r], a = P[4 * n + (r + 1) % 4];
|
|
123
|
+
for (let t = 0; t < 3; t++) e[3 * i + t] += B[3 * a + t], e[3 * a + t] += B[3 * i + t];
|
|
124
|
+
t[i]++, t[a]++;
|
|
125
|
+
}
|
|
126
|
+
let n = new Float64Array(N * 3);
|
|
127
|
+
for (let r = 0; r < N; r++) for (let i = 0; i < 3; i++) {
|
|
128
|
+
let a = Math.round(k[3 * r + i]);
|
|
129
|
+
n[3 * r + i] = Math.min(a + .5, Math.max(a - .5, e[3 * r + i] / t[r]));
|
|
130
|
+
}
|
|
131
|
+
B = n;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
let V = new Float64Array(N * 3);
|
|
135
|
+
for (let e = 0; e < z; e++) {
|
|
136
|
+
let [t, n, r, i] = [
|
|
137
|
+
0,
|
|
138
|
+
1,
|
|
139
|
+
2,
|
|
140
|
+
3
|
|
141
|
+
].map((t) => 3 * P[4 * e + t]), a = B[r] - B[t], o = B[r + 1] - B[t + 1], s = B[r + 2] - B[t + 2], c = B[i] - B[n], l = B[i + 1] - B[n + 1], u = B[i + 2] - B[n + 2], d = o * u - s * l, f = s * c - a * u, p = a * l - o * c;
|
|
142
|
+
for (let e of [
|
|
143
|
+
t,
|
|
144
|
+
n,
|
|
145
|
+
r,
|
|
146
|
+
i
|
|
147
|
+
]) V[e] += d, V[e + 1] += f, V[e + 2] += p;
|
|
148
|
+
}
|
|
149
|
+
let H = new Float32Array(z * 12), U = new Float32Array(z * 12), W = new Uint32Array(z * 6), G = [
|
|
150
|
+
Infinity,
|
|
151
|
+
Infinity,
|
|
152
|
+
Infinity
|
|
153
|
+
], K = [
|
|
154
|
+
-Infinity,
|
|
155
|
+
-Infinity,
|
|
156
|
+
-Infinity
|
|
157
|
+
];
|
|
158
|
+
for (let e = 0; e < z; e++) {
|
|
159
|
+
W.set([
|
|
160
|
+
4 * e,
|
|
161
|
+
4 * e + 1,
|
|
162
|
+
4 * e + 2,
|
|
163
|
+
4 * e,
|
|
164
|
+
4 * e + 2,
|
|
165
|
+
4 * e + 3
|
|
166
|
+
], 6 * e);
|
|
167
|
+
for (let t = 0; t < 4; t++) {
|
|
168
|
+
let n = 3 * P[4 * e + t], r = 3 * (4 * e + t), i = Math.hypot(A[n], A[n + 1], A[n + 2]) ? A : V, a = Math.hypot(i[n], i[n + 1], i[n + 2]) || 1;
|
|
169
|
+
for (let e = 0; e < 3; e++) {
|
|
170
|
+
let t = B[n + e] - d[e];
|
|
171
|
+
H[r + e] = t, U[r + e] = i[n + e] / a, H[r + e] < G[e] && (G[e] = H[r + e]), H[r + e] > K[e] && (K[e] = H[r + e]);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (!z) for (let e = 0; e < 3; e++) G[e] = K[e] = 0;
|
|
176
|
+
return {
|
|
177
|
+
mesh: {
|
|
178
|
+
positions: H,
|
|
179
|
+
normals: U,
|
|
180
|
+
ao: /* @__PURE__ */ new Float32Array(),
|
|
181
|
+
indices: W,
|
|
182
|
+
paletteIndexes: Uint8Array.from(F),
|
|
183
|
+
min: G,
|
|
184
|
+
max: K
|
|
185
|
+
},
|
|
186
|
+
stats: {
|
|
187
|
+
voxels: w,
|
|
188
|
+
quads: z,
|
|
189
|
+
triangles: z * 2,
|
|
190
|
+
vertices: z * 4,
|
|
191
|
+
surfaceVertices: N
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
function s(e, t, n) {
|
|
196
|
+
let [r, i, a] = t, o = 2 * n + 1, s = [
|
|
197
|
+
1,
|
|
198
|
+
r * a,
|
|
199
|
+
r
|
|
200
|
+
], c = new Float32Array(Math.max(r, i, a));
|
|
201
|
+
for (let r = 0; r < 3; r++) {
|
|
202
|
+
let i = t[r === 2 ? 2 : r], a = s[r], l = (r + 1) % 3, u = (r + 2) % 3, d = t[l], f = t[u];
|
|
203
|
+
for (let t = 0; t < d; t++) for (let r = 0; r < f; r++) {
|
|
204
|
+
let d = t * s[l] + r * s[u], f = 0;
|
|
205
|
+
for (let t = 0; t <= Math.min(n, i - 1); t++) f += e[d + t * a];
|
|
206
|
+
for (let t = 0; t < i; t++) c[t] = f / o, t + n + 1 < i && (f += e[d + (t + n + 1) * a]), t - n >= 0 && (f -= e[d + (t - n) * a]);
|
|
207
|
+
for (let t = 0; t < i; t++) e[d + t * a] = c[t];
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
//#endregion
|
|
212
|
+
export { o as smoothGrid };
|