@margoengine/voxel 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +1671 -3
- package/dist/bevel.d.ts +79 -0
- package/dist/bevel.js +453 -0
- package/dist/blocks-BabtpQT-.js +179 -0
- package/dist/blocks.d.ts +220 -0
- package/dist/border.d.ts +9 -0
- package/dist/chunk-BCoaKSn4.js +158 -0
- package/dist/chunk.d.ts +112 -0
- package/dist/codec-ZZFj1xvt.js +410 -0
- package/dist/codec.d.ts +125 -0
- package/dist/codec.js +1 -0
- package/dist/debris.d.ts +114 -0
- package/dist/debris.js +387 -0
- package/dist/faces-DUR7LhrN.js +146 -0
- package/dist/faces.d.ts +7 -0
- package/dist/fluids.d.ts +74 -0
- package/dist/fluids.js +226 -0
- package/dist/generate.d.ts +78 -0
- package/dist/generate.js +99 -0
- package/dist/gi.d.ts +76 -0
- package/dist/gi.js +264 -0
- package/dist/gpu-mesher.d.ts +77 -0
- package/dist/gpu-mesher.js +461 -0
- package/dist/granular-mpm.d.ts +32 -0
- package/dist/granular-mpm.js +46 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +4 -0
- package/dist/light-rgb.d.ts +71 -0
- package/dist/light-rgb.js +287 -0
- package/dist/light-worker.d.ts +84 -0
- package/dist/light-worker.js +110 -0
- package/dist/light.d.ts +108 -0
- package/dist/light.js +332 -0
- package/dist/light.worker.d.ts +1 -0
- package/dist/lightmap.d.ts +50 -0
- package/dist/lightmap.js +68 -0
- package/dist/lod.d.ts +87 -0
- package/dist/lod.js +182 -0
- package/dist/mesher.d.ts +214 -0
- package/dist/mesher.js +539 -0
- package/dist/mesher.worker.d.ts +1 -0
- package/dist/model-data-D_rIKJCQ.js +41 -0
- package/dist/model-data.d.ts +21 -0
- package/dist/model-mesh-C4Gi6bMM.js +122 -0
- package/dist/model-mesh.d.ts +21 -0
- package/dist/model-surface.d.ts +5 -0
- package/dist/models.d.ts +74 -0
- package/dist/models.js +308 -0
- package/dist/objects.d.ts +73 -0
- package/dist/objects.js +152 -0
- package/dist/octants-C_Ic7gdn.js +10 -0
- package/dist/octants.d.ts +16 -0
- package/dist/origin-DsMelUxs.js +23 -0
- package/dist/origin.d.ts +7 -0
- package/dist/physics.d.ts +147 -0
- package/dist/physics.js +486 -0
- package/dist/query.d.ts +166 -0
- package/dist/query.js +305 -0
- package/dist/render-post.d.ts +6 -0
- package/dist/render.d.ts +296 -0
- package/dist/render.js +1613 -0
- package/dist/shared/borrowed.d.ts +44 -0
- package/dist/smooth-CEwHkXli.js +968 -0
- package/dist/smooth-clip.d.ts +24 -0
- package/dist/smooth-lod.d.ts +118 -0
- package/dist/smooth-lod.js +244 -0
- package/dist/smooth.d.ts +171 -0
- package/dist/smooth.js +1 -0
- package/dist/stream.d.ts +201 -0
- package/dist/stream.js +402 -0
- package/dist/terrain.worker.d.ts +1 -0
- package/dist/textures.d.ts +86 -0
- package/dist/ticks.d.ts +126 -0
- package/dist/ticks.js +239 -0
- package/dist/unit-light-Ch7roJgB.js +7 -0
- package/dist/vox.d.ts +237 -0
- package/dist/vox.js +780 -0
- package/dist/wind-data.d.ts +13 -0
- package/dist/wind.d.ts +68 -0
- package/dist/wind.js +170 -0
- package/dist/worker-DlGRxlc4.js +44 -0
- package/dist/worker-pool-sPyC6jCE.js +93 -0
- package/dist/workers/light.js +1 -0
- package/dist/workers/mesher.js +1 -0
- package/dist/workers/terrain.js +1 -0
- package/dist/world-CelTlft2.js +153 -0
- package/dist/world.d.ts +117 -0
- package/package.json +175 -6
package/dist/gi.js
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const i=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { t as e } from "./unit-light-Ch7roJgB.js";
|
|
5
|
+
import { n as t, r as n } from "./origin-DsMelUxs.js";
|
|
6
|
+
import { MargoError as r } from "@margoengine/math/errors";
|
|
7
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
8
|
+
//#region src/gi.ts
|
|
9
|
+
var a = 46, o = 64, s = 4096 * o, c = "\nconst S = 64u;\nstruct Params { origin: vec4i, sun: vec4f, radiance: vec4f, frame: vec4u, settings: vec4f }\n@group(0) @binding(0) var<storage, read> cells: array<u32>;\n@group(0) @binding(1) var<storage, read> albedo: array<vec4f>;\n@group(0) @binding(2) var<uniform> P: Params;\n@group(0) @binding(3) var<storage, read_write> acc: array<vec4f>;\n@group(0) @binding(4) var shR: texture_storage_3d<rgba16float, write>;\n@group(0) @binding(5) var shG: texture_storage_3d<rgba16float, write>;\n@group(0) @binding(6) var shB: texture_storage_3d<rgba16float, write>;\n@group(0) @binding(7) var sunTex: texture_storage_3d<rgba16float, write>;\n\n\nfn cell(c: vec3i) -> u32 {\n if (any(c < vec3i(0)) || any(c >= vec3i(i32(S)))) { return 0u; }\n return cells[u32(c.x) + u32(c.z) * S + u32(c.y) * S * S];\n}\nfn solid(c: vec3i) -> bool { return (cell(c) & 65536u) != 0u; }\n\nfn hash(x: u32) -> u32 {\n var h = x * 747796405u + 2891336453u;\n h = ((h >> ((h >> 28u) + 4u)) ^ h) * 277803737u;\n return (h >> 22u) ^ h;\n}\nfn rand(seed: ptr<function, u32>) -> f32 { *seed = hash(*seed); return f32(*seed >> 8u) / 16777216.0; }\n\nstruct Hit { cell: vec3i, normal: vec3f, hit: bool }\n\nfn trace(o: vec3f, d: vec3f, range: f32) -> Hit {\n var c = vec3i(floor(o));\n let step = vec3i(sign(d));\n let inv = 1.0 / max(abs(d), vec3f(1e-6));\n var next = (select(vec3f(c) - o, vec3f(c) + 1.0 - o, d > vec3f(0.0))) * sign(d) * inv;\n next = max(next, vec3f(0.0));\n var h: Hit; h.hit = false;\n var t = 0.0;\n for (var i = 0; i < 200; i++) {\n var axis = 0;\n if (next.y < next.x && next.y <= next.z) { axis = 1; } else if (next.z < next.x && next.z < next.y) { axis = 2; }\n t = next[axis];\n if (t > range) { return h; }\n c[axis] += step[axis];\n next[axis] += inv[axis];\n if (any(c < vec3i(-1)) || any(c > vec3i(i32(S)))) { return h; }\n if (solid(c)) {\n h.hit = true; h.cell = c; h.normal = vec3f(0.0); h.normal[axis] = -f32(step[axis]);\n return h;\n }\n }\n return h;\n}\n\n\nfn toSun(seed: ptr<function, u32>) -> vec3f {\n let l = P.sun.xyz;\n let up = select(vec3f(0.0, 1.0, 0.0), vec3f(1.0, 0.0, 0.0), abs(l.y) > 0.9);\n let a = normalize(cross(up, l)); let b = cross(l, a);\n let cosMax = cos(P.settings.y);\n let z = 1.0 - rand(seed) * (1.0 - cosMax); let r = sqrt(max(0.0, 1.0 - z * z)); let phi = rand(seed) * 6.2831853;\n return normalize(a * r * cos(phi) + b * r * sin(phi) + l * z);\n}\n\n@compute @workgroup_size(64)\nfn gather(@builtin(global_invocation_id) g: vec3u) {\n if (g.x >= P.frame.y) { return; }\n let i = (g.x * P.frame.w + P.frame.z) % (S * S * S);\n let c = vec3i(i32(i % S), i32(i / (S * S)), i32((i / S) % S));\n let base = i * 4u;\n if (solid(c)) {\n acc[base] = vec4f(0.0); acc[base + 1u] = vec4f(0.0); acc[base + 2u] = vec4f(0.0); acc[base + 3u] = vec4f(0.0);\n } else {\n var seed = hash(i ^ hash(P.frame.x));\n let o = vec3f(c) + vec3f(0.5);\n let sunUp = P.sun.y > 0.0;\n\n var visible = 0.0;\n if (sunUp && !trace(o, toSun(&seed), P.settings.z * 4.0).hit) { visible = 1.0; }\n\n var r = vec4f(0.0); var gg = vec4f(0.0); var b = vec4f(0.0);\n let rays = u32(P.settings.w);\n for (var k = 0u; k < rays; k++) {\n let z = 1.0 - 2.0 * rand(&seed); let s = sqrt(max(0.0, 1.0 - z * z)); let phi = rand(&seed) * 6.2831853;\n let d = vec3f(s * cos(phi), z, s * sin(phi));\n let h = trace(o, d, P.settings.z);\n if (!h.hit || !sunUp) { continue; }\n let l = P.sun.xyz;\n let cosine = dot(h.normal, l);\n if (cosine <= 0.0) { continue; }\n let front = vec3f(h.cell) + vec3f(0.5) + h.normal * 0.501;\n if (trace(front, toSun(&seed), P.settings.z * 4.0).hit) { continue; }\n let a = albedo[cell(h.cell) & 65535u].rgb;\n\n let L = a * P.radiance.rgb * cosine / 3.14159265;\n\n let y = vec4f(0.282095, 0.488603 * d.y, 0.488603 * d.z, 0.488603 * d.x);\n r += L.r * y; gg += L.g * y; b += L.b * y;\n }\n let scale = 12.5663706 / f32(rays);\n let blend = P.settings.x;\n acc[base] = mix(acc[base], r * scale, blend);\n acc[base + 1u] = mix(acc[base + 1u], gg * scale, blend);\n acc[base + 2u] = mix(acc[base + 2u], b * scale, blend);\n acc[base + 3u] = mix(acc[base + 3u], vec4f(visible, 1.0, 0.0, 0.0), blend);\n }\n\n let w = vec3u(((c + P.origin.xyz) % vec3i(i32(S)) + vec3i(i32(S))) % vec3i(i32(S)));\n let open = select(1.0, 0.0, solid(c));\n textureStore(shR, w, acc[base] * open);\n textureStore(shG, w, acc[base + 1u] * open);\n textureStore(shB, w, acc[base + 2u] * open);\n textureStore(sunTex, w, vec4f(acc[base + 3u].x * open, open, 0.0, 0.0));\n}\n", l = (e) => `
|
|
10
|
+
@group(${e}) @binding(${a}) var voxelGiR:texture_3d<f32>;
|
|
11
|
+
@group(${e}) @binding(47) var voxelGiG:texture_3d<f32>;
|
|
12
|
+
@group(${e}) @binding(48) var voxelGiB:texture_3d<f32>;
|
|
13
|
+
@group(${e}) @binding(49) var voxelGiSunVolume:texture_3d<f32>;
|
|
14
|
+
@group(${e}) @binding(50) var voxelGiSampler:sampler;
|
|
15
|
+
struct VoxelGiFrame {corner:vec4f,wrap:vec4f}
|
|
16
|
+
@group(${e}) @binding(51) var<uniform> voxelGiWindow:VoxelGiFrame;
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
fn voxelGiAt(p:vec3f,n:vec3f)->vec4f {
|
|
21
|
+
let q=p+n*0.5;let l=q-voxelGiWindow.corner.xyz;
|
|
22
|
+
if(any(l<vec3f(0.5))||any(l>vec3f(63.5))){return vec4f(-1.0);}
|
|
23
|
+
return vec4f((q+voxelGiWindow.wrap.xyz)/64.0,1.0);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
fn voxelGiLight(p:vec3f,n:vec3f,base:vec3f)->vec3f {
|
|
28
|
+
let at=voxelGiAt(p,n);if(at.w<0.0){return vec3f(0.0);}
|
|
29
|
+
let open=textureSampleLevel(voxelGiSunVolume,voxelGiSampler,at.xyz,0.0).y;if(open<0.002){return vec3f(0.0);}
|
|
30
|
+
let y=vec4f(3.14159265*0.282095,2.0943951*0.488603*n.y,2.0943951*0.488603*n.z,2.0943951*0.488603*n.x);
|
|
31
|
+
let e=vec3f(dot(textureSampleLevel(voxelGiR,voxelGiSampler,at.xyz,0.0),y),dot(textureSampleLevel(voxelGiG,voxelGiSampler,at.xyz,0.0),y),dot(textureSampleLevel(voxelGiB,voxelGiSampler,at.xyz,0.0),y));
|
|
32
|
+
return base*max(e/open,vec3f(0.0))/3.14159265*voxelGiWindow.corner.w;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
fn voxelGiSun(p:vec3f,n:vec3f)->f32 {
|
|
36
|
+
let at=voxelGiAt(p,n);if(at.w<0.0){return 1.0;}
|
|
37
|
+
let t=textureSampleLevel(voxelGiSunVolume,voxelGiSampler,at.xyz,0.0);
|
|
38
|
+
if(t.y<0.002){return 1.0;}
|
|
39
|
+
return clamp(t.x/t.y,0.0,1.0);
|
|
40
|
+
}`, u = (e) => Array.isArray(e) && e.length === 3 && e.every((e) => typeof e == "number" && e >= 0 && e <= 1), d = class d {
|
|
41
|
+
#e;
|
|
42
|
+
#t;
|
|
43
|
+
#n;
|
|
44
|
+
#r;
|
|
45
|
+
#i;
|
|
46
|
+
#a;
|
|
47
|
+
#o;
|
|
48
|
+
#s;
|
|
49
|
+
#c;
|
|
50
|
+
#l = !0;
|
|
51
|
+
#u = 0;
|
|
52
|
+
#d = 0;
|
|
53
|
+
#f = 0;
|
|
54
|
+
#p = -1;
|
|
55
|
+
#m = /* @__PURE__ */ new Map();
|
|
56
|
+
#h = [];
|
|
57
|
+
#g = !1;
|
|
58
|
+
constructor(e, t) {
|
|
59
|
+
this.#e = this.owner = e, this.world = t.world, this.#t = t.albedo ?? {}, this.strength = t.strength ?? 1, this.#n = t.softness ?? .05, this.#r = t.cells ?? 32768, this.#i = t.rays ?? 4, this.#a = t.range ?? 32;
|
|
60
|
+
let n = () => {
|
|
61
|
+
this.#l = !0;
|
|
62
|
+
};
|
|
63
|
+
this.#h.push(this.world.onChange(n), this.world.onChunk(n));
|
|
64
|
+
}
|
|
65
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
66
|
+
if (typeof e?.createMesh != "function") throw TypeError("VoxelGI.attach expects an Engine");
|
|
67
|
+
if (typeof t?.world?.getChunk != "function" || typeof t.world.onChange != "function") throw TypeError("VoxelGI world must be a VoxelWorld");
|
|
68
|
+
let n = (e, n, r, i = !1) => {
|
|
69
|
+
let a = t[e];
|
|
70
|
+
if (a !== void 0 && (typeof a != "number" || !(a >= n && a <= r) || i && !Number.isInteger(a))) throw RangeError(`VoxelGI ${e} is ${a}; pass ${i ? "a whole number " : ""}from ${n} to ${r}`);
|
|
71
|
+
};
|
|
72
|
+
n("strength", 0, 8), n("softness", 0, .5), n("cells", 4096, s, !0), n("rays", 1, 16, !0), n("range", 4, 64, !0);
|
|
73
|
+
for (let [e, n] of Object.entries(t.albedo ?? {})) if (!u(n)) throw RangeError(`VoxelGI albedo of "${e}" must be [r, g, b] in 0..1`);
|
|
74
|
+
if (!e.gpu) throw new r("unsupported", "VoxelGI needs the WebGPU backend; create the engine with WebGPU");
|
|
75
|
+
return new d(e, t);
|
|
76
|
+
}
|
|
77
|
+
add(e) {
|
|
78
|
+
let t = e.addLighting(this);
|
|
79
|
+
return this.#s = e, () => {
|
|
80
|
+
t(), this.#s === e && (this.#s = void 0);
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
get origin() {
|
|
84
|
+
return this.#c && [...this.#c];
|
|
85
|
+
}
|
|
86
|
+
update() {
|
|
87
|
+
if (this.#g) throw new r("disposed", "VoxelGI is disposed; attach a new one");
|
|
88
|
+
if (typeof this.strength != "number" || !(this.strength >= 0 && this.strength <= 8)) throw RangeError(`VoxelGI strength is ${String(this.strength)}; pass from 0 to 8`);
|
|
89
|
+
this.#x();
|
|
90
|
+
let i = t(this.#s?.origin, "VoxelGI"), a = this.#o ??= this.#_(), c = this.#e.camera.position, l = [
|
|
91
|
+
0,
|
|
92
|
+
1,
|
|
93
|
+
2
|
|
94
|
+
].map((e) => Math.round((c[e] + i[e]) / 32) * 32 - 32);
|
|
95
|
+
(!this.#c || l.some((e, t) => e !== this.#c[t])) && (this.#c = l, this.#l = !0, this.#d = 0, this.#f = 0), this.world.blocks.table.length !== this.#p && this.#y(a), this.#l && this.#b(a);
|
|
96
|
+
let { device: u } = a, d = this.#e.lighting, f = this.#r, p = /* @__PURE__ */ new ArrayBuffer(80), m = new Int32Array(p), h = new Uint32Array(p), g = new Float32Array(p);
|
|
97
|
+
m.set(l, 0), e(g, 4, d.direction), g.set(d.radiance, 8), h.set([
|
|
98
|
+
this.#u++,
|
|
99
|
+
f,
|
|
100
|
+
this.#f,
|
|
101
|
+
40503
|
|
102
|
+
], 12), g.set([
|
|
103
|
+
this.#d < s ? 1 : 1 / 8,
|
|
104
|
+
this.#n,
|
|
105
|
+
this.#a,
|
|
106
|
+
this.#i
|
|
107
|
+
], 16), u.queue.writeBuffer(a.params, 0, p), u.queue.writeBuffer(a.window, 0, Float32Array.of(l[0] - i[0], l[1] - i[1], l[2] - i[2], this.strength, n(i[0], o), n(i[1], o), n(i[2], o), 0));
|
|
108
|
+
let _ = u.createCommandEncoder(), v = _.beginComputePass();
|
|
109
|
+
return v.setPipeline(a.pipeline), v.setBindGroup(0, a.group), v.dispatchWorkgroups(Math.ceil(f / 64)), v.end(), u.queue.submit([_.finish()]), this.#f = (this.#f + f * 40503) % s, this.#d += f, f;
|
|
110
|
+
}
|
|
111
|
+
#_() {
|
|
112
|
+
let e = i(this.#e).gpu, t = e.createShaderModule({
|
|
113
|
+
label: "margoengine voxel gi",
|
|
114
|
+
code: c
|
|
115
|
+
}), n = e.createComputePipeline({
|
|
116
|
+
label: "margoengine voxel gi",
|
|
117
|
+
layout: "auto",
|
|
118
|
+
compute: {
|
|
119
|
+
module: t,
|
|
120
|
+
entryPoint: "gather"
|
|
121
|
+
}
|
|
122
|
+
}), r = (t, n) => e.createBuffer({
|
|
123
|
+
label: "margoengine voxel gi",
|
|
124
|
+
size: t,
|
|
125
|
+
usage: n
|
|
126
|
+
}), l = r(s * 4, GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST), u = r(16, GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST), d = r(80, GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST), f = r(s * 64, GPUBufferUsage.STORAGE), p = [
|
|
127
|
+
0,
|
|
128
|
+
1,
|
|
129
|
+
2,
|
|
130
|
+
3
|
|
131
|
+
].map(() => e.createTexture({
|
|
132
|
+
label: "margoengine voxel gi",
|
|
133
|
+
dimension: "3d",
|
|
134
|
+
size: [
|
|
135
|
+
o,
|
|
136
|
+
o,
|
|
137
|
+
o
|
|
138
|
+
],
|
|
139
|
+
format: "rgba16float",
|
|
140
|
+
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.STORAGE_BINDING
|
|
141
|
+
})), m = e.createSampler({
|
|
142
|
+
magFilter: "linear",
|
|
143
|
+
minFilter: "linear",
|
|
144
|
+
addressModeU: "repeat",
|
|
145
|
+
addressModeV: "repeat",
|
|
146
|
+
addressModeW: "repeat"
|
|
147
|
+
}), h = r(32, GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST), g = {
|
|
148
|
+
device: e,
|
|
149
|
+
pipeline: n,
|
|
150
|
+
cells: l,
|
|
151
|
+
albedo: u,
|
|
152
|
+
params: d,
|
|
153
|
+
acc: f,
|
|
154
|
+
textures: p,
|
|
155
|
+
sampler: m,
|
|
156
|
+
window: h,
|
|
157
|
+
group: void 0,
|
|
158
|
+
entries: [
|
|
159
|
+
...p.map((e, t) => ({
|
|
160
|
+
binding: a + t,
|
|
161
|
+
resource: e.createView()
|
|
162
|
+
})),
|
|
163
|
+
{
|
|
164
|
+
binding: 50,
|
|
165
|
+
resource: m
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
binding: 51,
|
|
169
|
+
resource: { buffer: h }
|
|
170
|
+
}
|
|
171
|
+
]
|
|
172
|
+
};
|
|
173
|
+
return this.#v(g), g;
|
|
174
|
+
}
|
|
175
|
+
#v(e) {
|
|
176
|
+
e.group = e.device.createBindGroup({
|
|
177
|
+
layout: e.pipeline.getBindGroupLayout(0),
|
|
178
|
+
entries: [
|
|
179
|
+
{
|
|
180
|
+
binding: 0,
|
|
181
|
+
resource: { buffer: e.cells }
|
|
182
|
+
},
|
|
183
|
+
{
|
|
184
|
+
binding: 1,
|
|
185
|
+
resource: { buffer: e.albedo }
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
binding: 2,
|
|
189
|
+
resource: { buffer: e.params }
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
binding: 3,
|
|
193
|
+
resource: { buffer: e.acc }
|
|
194
|
+
},
|
|
195
|
+
...e.textures.map((e, t) => ({
|
|
196
|
+
binding: 4 + t,
|
|
197
|
+
resource: e.createView()
|
|
198
|
+
}))
|
|
199
|
+
]
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
#y(e) {
|
|
203
|
+
let t = this.world.blocks.table, n = new Float32Array(Math.max(1, t.length) * 4);
|
|
204
|
+
t.forEach((e, t) => {
|
|
205
|
+
n.set(this.#t[e.name] ?? [
|
|
206
|
+
.5,
|
|
207
|
+
.5,
|
|
208
|
+
.5
|
|
209
|
+
], t * 4), n[t * 4 + 3] = 1;
|
|
210
|
+
}), e.albedo.size < n.byteLength && (e.albedo.destroy(), e.albedo = e.device.createBuffer({
|
|
211
|
+
label: "margoengine voxel gi",
|
|
212
|
+
size: n.byteLength,
|
|
213
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
|
|
214
|
+
}), this.#v(e)), e.device.queue.writeBuffer(e.albedo, 0, n), this.#p = t.length, this.#l = !0;
|
|
215
|
+
}
|
|
216
|
+
#b(e) {
|
|
217
|
+
let [t, n, r] = this.#c, i = this.world.blocks.table.map((e) => e.layer === "opaque" && e.shape === "cube" ? 65536 : 0), a = new Uint32Array(s);
|
|
218
|
+
for (let e = 0; e < 2; e++) for (let s = 0; s < 2; s++) for (let c = 0; c < 2; c++) {
|
|
219
|
+
let l = this.world.getChunk(t / 32 + c, n / 32 + e, r / 32 + s);
|
|
220
|
+
if (l) for (let t = 0; t < 32; t++) for (let n = 0; n < 32; n++) for (let r = 0, u = c * 32 + (s * 32 + n) * o + (e * 32 + t) * o * o; r < 32; r++, u++) {
|
|
221
|
+
let e = l.get(r, t, n) & 65535;
|
|
222
|
+
a[u] = e | (i[e] ?? 0);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
e.device.queue.writeBuffer(e.cells, 0, a), this.#l = !1;
|
|
226
|
+
}
|
|
227
|
+
pbr(e) {
|
|
228
|
+
if (!this.#x() || this.#g) return;
|
|
229
|
+
let t = this.#m.get(e);
|
|
230
|
+
return t || (t = {
|
|
231
|
+
declarations: l(e),
|
|
232
|
+
light: "voxelGiLight(i.position,s.n,s.base.rgb)",
|
|
233
|
+
direct: (e) => `(${e}*voxelGiSun(i.position,s.n))`
|
|
234
|
+
}, this.#m.set(e, t)), t;
|
|
235
|
+
}
|
|
236
|
+
bindings() {
|
|
237
|
+
return this.#x()?.entries ?? [];
|
|
238
|
+
}
|
|
239
|
+
#x() {
|
|
240
|
+
return this.#o && this.#o.device !== i(this.#e).gpu && (this.#S(this.#o), this.#o = void 0, this.#c = void 0, this.#p = -1, this.#u = 0), this.#o;
|
|
241
|
+
}
|
|
242
|
+
dispose() {
|
|
243
|
+
if (!this.#g) {
|
|
244
|
+
this.#g = !0;
|
|
245
|
+
for (let e of this.#h) e();
|
|
246
|
+
this.#o && this.#S(this.#o), this.#o = void 0;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
#S(e) {
|
|
250
|
+
for (let t of e.textures) t.destroy();
|
|
251
|
+
for (let t of [
|
|
252
|
+
e.cells,
|
|
253
|
+
e.albedo,
|
|
254
|
+
e.params,
|
|
255
|
+
e.acc,
|
|
256
|
+
e.window
|
|
257
|
+
]) t.destroy();
|
|
258
|
+
}
|
|
259
|
+
[Symbol.dispose]() {
|
|
260
|
+
this.dispose();
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
//#endregion
|
|
264
|
+
export { d as VoxelGI };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { Vec3 } from '@margoengine/math';
|
|
3
|
+
import { type ChunkMesh } from './mesher.js';
|
|
4
|
+
import type { VoxelWorld } from './world.js';
|
|
5
|
+
/** A chunk mesher a {@link VoxelRenderer} can use instead of its worker pool: `VoxelRendererOptions.mesher`. */
|
|
6
|
+
export interface GpuVoxelMesherOptions {
|
|
7
|
+
/** Most chunks meshed in one dispatch. Each takes about 3.5 MB of GPU scratch. Default 8, from 1 to 32. */
|
|
8
|
+
batch?: number;
|
|
9
|
+
}
|
|
10
|
+
/** Counts of chunks meshed, from {@link GpuVoxelMesher.stats}. */
|
|
11
|
+
export interface GpuVoxelMesherStats {
|
|
12
|
+
/** Chunks meshed in compute shaders. */
|
|
13
|
+
gpu: number;
|
|
14
|
+
/** Chunks meshed on this thread by meshChunk: slabs or stairs within a cell of them. */
|
|
15
|
+
cpu: number;
|
|
16
|
+
/** Chunks with nothing to draw, answered without the GPU. */
|
|
17
|
+
empty: number;
|
|
18
|
+
/** Dispatches run. */
|
|
19
|
+
batches: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Meshes chunks in WebGPU compute shaders: the same quads, in the same order, as {@link meshChunk} on the CPU (see
|
|
23
|
+
* `ChunkMesh` for the format), for chunks of cubes, cross blocks and liquids. Calls made together go out in batches of
|
|
24
|
+
* up to `batch` chunks per dispatch, nearest first, so a large edit remeshes in a few GPU passes instead of a worker
|
|
25
|
+
* job per chunk. A chunk with a slab or stair within a cell of it is meshed by meshChunk on this thread. The page still
|
|
26
|
+
* packs each chunk and its shell (as for the worker mesher) and finds which chunk faces see each other.
|
|
27
|
+
*
|
|
28
|
+
* The mesher holds GPU objects of the device it was made with. When that device is lost its jobs wait, and
|
|
29
|
+
* {@link GpuVoxelMesher.setDevice} moves it to the device the engine restored, where they run. A `VoxelRenderer` given
|
|
30
|
+
* the mesher does this from its own restore hook; a page using the mesher alone calls it after `engine.rebootDevice()`.
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* const mesher = new GpuVoxelMesher(engine.gpu!);
|
|
34
|
+
* const voxels = VoxelRenderer.attach(engine, { world, textures, mesher });
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare class GpuVoxelMesher {
|
|
38
|
+
#private;
|
|
39
|
+
/** Most chunks per dispatch. */
|
|
40
|
+
readonly batch: number;
|
|
41
|
+
/** Chunks meshed so far, by path. */
|
|
42
|
+
readonly stats: GpuVoxelMesherStats;
|
|
43
|
+
/**
|
|
44
|
+
* @param device The WebGPU device: `engine.gpu`.
|
|
45
|
+
* @throws TypeError when `device` is not a GPUDevice.
|
|
46
|
+
* @throws RangeError when `batch` is not a whole number from 1 to 32, or the device cannot bind its scratch.
|
|
47
|
+
*/
|
|
48
|
+
constructor(device: GPUDevice, options?: GpuVoxelMesherOptions);
|
|
49
|
+
/**
|
|
50
|
+
* Moves the mesher to `device`, such as the one `engine.rebootDevice()` restored: its pipelines and scratch are made
|
|
51
|
+
* again there on the next batch, and the jobs that waited for it run. `VoxelRenderer` calls it for the mesher it was
|
|
52
|
+
* given. Calling it with the device in use does nothing.
|
|
53
|
+
* @throws TypeError when `device` is not a GPUDevice.
|
|
54
|
+
* @throws RangeError when `device` cannot bind this mesher's `batch` scratch.
|
|
55
|
+
* @throws MargoError `disposed` after {@link GpuVoxelMesher.dispose}.
|
|
56
|
+
*/
|
|
57
|
+
setDevice(device: GPUDevice): void;
|
|
58
|
+
/** Compiles the shaders and makes the scratch now rather than with the first chunks. */
|
|
59
|
+
warm(): void;
|
|
60
|
+
/**
|
|
61
|
+
* Meshes chunk `key` of `world` with the next batch, as `VoxelMesher.mesh` does in a worker: resolves the chunk's
|
|
62
|
+
* {@link ChunkMesh}, or null when the job was cancelled ({@link GpuVoxelMesher.cancel}, dispose) or superseded by a
|
|
63
|
+
* newer call for the key, and when the chunk is not loaded once its batch starts. Rejects when the GPU work fails.
|
|
64
|
+
* @param priority Lower is meshed sooner, such as the chunk's distance to the camera. Default 0.
|
|
65
|
+
* @param eye The camera's world position: the translucent quads come back sorted from it (`sortQuads`).
|
|
66
|
+
* @param border Also list the shell's cube faces in `ChunkMesh.border`, read on this thread. Default false.
|
|
67
|
+
* @throws TypeError when `world` is not a VoxelWorld, `key` is not a string or `eye` is not three numbers.
|
|
68
|
+
* @throws RangeError when `key` is not three chunk coordinates, or `priority` or `eye` is not finite.
|
|
69
|
+
* @throws MargoError `disposed` after {@link GpuVoxelMesher.dispose}.
|
|
70
|
+
*/
|
|
71
|
+
mesh(world: VoxelWorld, key: string, priority?: number, eye?: Vec3, border?: boolean): Promise<ChunkMesh | null>;
|
|
72
|
+
/** Drops the pending job of chunk `key`, if any: its `mesh` promise resolves null. */
|
|
73
|
+
cancel(key: string): void;
|
|
74
|
+
/** Cancels every pending job and frees the GPU scratch. */
|
|
75
|
+
dispose(): void;
|
|
76
|
+
[Symbol.dispose](): void;
|
|
77
|
+
}
|