@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.
Files changed (90) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +1671 -3
  4. package/dist/bevel.d.ts +79 -0
  5. package/dist/bevel.js +453 -0
  6. package/dist/blocks-BabtpQT-.js +179 -0
  7. package/dist/blocks.d.ts +220 -0
  8. package/dist/border.d.ts +9 -0
  9. package/dist/chunk-BCoaKSn4.js +158 -0
  10. package/dist/chunk.d.ts +112 -0
  11. package/dist/codec-ZZFj1xvt.js +410 -0
  12. package/dist/codec.d.ts +125 -0
  13. package/dist/codec.js +1 -0
  14. package/dist/debris.d.ts +114 -0
  15. package/dist/debris.js +387 -0
  16. package/dist/faces-DUR7LhrN.js +146 -0
  17. package/dist/faces.d.ts +7 -0
  18. package/dist/fluids.d.ts +74 -0
  19. package/dist/fluids.js +226 -0
  20. package/dist/generate.d.ts +78 -0
  21. package/dist/generate.js +99 -0
  22. package/dist/gi.d.ts +76 -0
  23. package/dist/gi.js +264 -0
  24. package/dist/gpu-mesher.d.ts +77 -0
  25. package/dist/gpu-mesher.js +461 -0
  26. package/dist/granular-mpm.d.ts +32 -0
  27. package/dist/granular-mpm.js +46 -0
  28. package/dist/index.d.ts +6 -0
  29. package/dist/index.js +4 -0
  30. package/dist/light-rgb.d.ts +71 -0
  31. package/dist/light-rgb.js +287 -0
  32. package/dist/light-worker.d.ts +84 -0
  33. package/dist/light-worker.js +110 -0
  34. package/dist/light.d.ts +108 -0
  35. package/dist/light.js +332 -0
  36. package/dist/light.worker.d.ts +1 -0
  37. package/dist/lightmap.d.ts +50 -0
  38. package/dist/lightmap.js +68 -0
  39. package/dist/lod.d.ts +87 -0
  40. package/dist/lod.js +182 -0
  41. package/dist/mesher.d.ts +214 -0
  42. package/dist/mesher.js +539 -0
  43. package/dist/mesher.worker.d.ts +1 -0
  44. package/dist/model-data-D_rIKJCQ.js +41 -0
  45. package/dist/model-data.d.ts +21 -0
  46. package/dist/model-mesh-C4Gi6bMM.js +122 -0
  47. package/dist/model-mesh.d.ts +21 -0
  48. package/dist/model-surface.d.ts +5 -0
  49. package/dist/models.d.ts +74 -0
  50. package/dist/models.js +308 -0
  51. package/dist/objects.d.ts +73 -0
  52. package/dist/objects.js +152 -0
  53. package/dist/octants-C_Ic7gdn.js +10 -0
  54. package/dist/octants.d.ts +16 -0
  55. package/dist/origin-DsMelUxs.js +23 -0
  56. package/dist/origin.d.ts +7 -0
  57. package/dist/physics.d.ts +147 -0
  58. package/dist/physics.js +486 -0
  59. package/dist/query.d.ts +166 -0
  60. package/dist/query.js +305 -0
  61. package/dist/render-post.d.ts +6 -0
  62. package/dist/render.d.ts +296 -0
  63. package/dist/render.js +1613 -0
  64. package/dist/shared/borrowed.d.ts +44 -0
  65. package/dist/smooth-CEwHkXli.js +968 -0
  66. package/dist/smooth-clip.d.ts +24 -0
  67. package/dist/smooth-lod.d.ts +118 -0
  68. package/dist/smooth-lod.js +244 -0
  69. package/dist/smooth.d.ts +171 -0
  70. package/dist/smooth.js +1 -0
  71. package/dist/stream.d.ts +201 -0
  72. package/dist/stream.js +402 -0
  73. package/dist/terrain.worker.d.ts +1 -0
  74. package/dist/textures.d.ts +86 -0
  75. package/dist/ticks.d.ts +126 -0
  76. package/dist/ticks.js +239 -0
  77. package/dist/unit-light-Ch7roJgB.js +7 -0
  78. package/dist/vox.d.ts +237 -0
  79. package/dist/vox.js +780 -0
  80. package/dist/wind-data.d.ts +13 -0
  81. package/dist/wind.d.ts +68 -0
  82. package/dist/wind.js +170 -0
  83. package/dist/worker-DlGRxlc4.js +44 -0
  84. package/dist/worker-pool-sPyC6jCE.js +93 -0
  85. package/dist/workers/light.js +1 -0
  86. package/dist/workers/mesher.js +1 -0
  87. package/dist/workers/terrain.js +1 -0
  88. package/dist/world-CelTlft2.js +153 -0
  89. package/dist/world.d.ts +117 -0
  90. package/package.json +175 -6
package/dist/query.js ADDED
@@ -0,0 +1,305 @@
1
+ import { n as e } from "./octants-C_Ic7gdn.js";
2
+ import { t } from "./model-data-D_rIKJCQ.js";
3
+ //#region src/query.ts
4
+ var n = 1e-6, r = 1 / 120, i = .25, a = .2, o = (e) => 1 - Math.exp(-e * r), s = o(20), c = o(2), l = o(6), u = o(8), d = [
5
+ 0,
6
+ 0,
7
+ 0
8
+ ], f = /* @__PURE__ */ new Float64Array(6), p = !1, m = (e, t) => {
9
+ if (typeof e?.getBlock != "function" || !e.blocks?.table) throw TypeError(`${t} needs a VoxelWorld: pass the world to query`);
10
+ }, h = (e, t) => {
11
+ if (typeof e != "number") throw TypeError(`${t} must be a number`);
12
+ if (!Number.isFinite(e)) throw RangeError(`${t} is ${e}; pass a finite number`);
13
+ return e;
14
+ }, g = (e, t) => {
15
+ if (!e || typeof e != "object" || e.length !== 3) throw TypeError(`${t} must be an [x, y, z] array of numbers`);
16
+ let n = e;
17
+ return [
18
+ h(n[0], `${t} x`),
19
+ h(n[1], `${t} y`),
20
+ h(n[2], `${t} z`)
21
+ ];
22
+ }, _ = (e, t, n) => {
23
+ if (e === void 0) return n;
24
+ if (h(e, t) < 0) throw RangeError(`${t} ${e} is negative; pass a number >= 0`);
25
+ return e;
26
+ }, v = (e, t) => t.layer !== "invisible" && !t.liquid, y = (t, n) => t.shape === "slab" || t.shape === "stair" ? e(t.shape, n >>> 16) : 255, b = [0, 0];
27
+ function x(e, n, r, i, a, o, s, c) {
28
+ let l = Infinity, u = -1;
29
+ for (let d of t(e, n)) {
30
+ let e = -Infinity, t = Infinity, n = -1;
31
+ for (let o = 0; o < 3; o++) {
32
+ let s = r[o] + d.min[o], c = r[o] + d.max[o];
33
+ if (!a[o]) {
34
+ (i[o] < s || i[o] >= c) && (t = -Infinity);
35
+ continue;
36
+ }
37
+ let l = ((a[o] > 0 ? s : c) - i[o]) / a[o], u = ((a[o] > 0 ? c : s) - i[o]) / a[o];
38
+ l > e && (e = l, n = o), t = Math.min(t, u);
39
+ }
40
+ let f = Math.max(e, o);
41
+ t > f && f <= s && f < l && (l = f, u = e >= o ? n : c);
42
+ }
43
+ return l !== Infinity && (b[0] = l, b[1] = u, !0);
44
+ }
45
+ function S(e, t, n, r, i, a, o) {
46
+ let s = [
47
+ 0,
48
+ 0,
49
+ 0
50
+ ], c = [
51
+ 0,
52
+ 0,
53
+ 0
54
+ ];
55
+ for (let e = 0; e < 3; e++) {
56
+ let o = n[e] + r[e] * i - t[e], l = n[e] + r[e] * ((i + Math.min(a, i + 1e-9)) / 2) - t[e];
57
+ s[e] = +(o > .5 || o === .5 && (r[e] > 0 || r[e] === 0 && l >= .5)), c[e] = r[e] > 0 && !s[e] || r[e] < 0 && s[e] ? (t[e] + .5 - n[e]) / r[e] : Infinity;
58
+ }
59
+ let l = i;
60
+ for (;;) {
61
+ if (e >> (s[0] | s[1] << 1 | s[2] << 2) & 1) return b[0] = l, b[1] = o, !0;
62
+ if (l = Math.min(c[0], c[1], c[2]), !(l < a)) return !1;
63
+ o = c[0] === l ? 0 : c[1] === l ? 1 : 2, s[o] ^= 1, c[o] = Infinity;
64
+ }
65
+ }
66
+ var C = (e, t) => {
67
+ let n = Math.floor(e);
68
+ return t < 0 && n === e ? n - 1 : n;
69
+ }, w = (e, t, n) => n > 0 ? (e + 1 - t) / n : n < 0 ? (e - t) / n : Infinity;
70
+ function T(e, t, n, r, i = v) {
71
+ m(e, "raycastVoxels");
72
+ let [a, o, s] = g(t, "Ray origin"), [c, l, u] = g(n, "Ray direction"), d = Math.hypot(c, l, u);
73
+ if (!d) throw RangeError("Ray direction is [0, 0, 0]; pass a non-zero direction such as [0, 0, -1]");
74
+ if (h(r, "Ray max distance") < 0) throw RangeError(`Ray max distance ${r} is negative; pass a distance >= 0`);
75
+ if (typeof i != "function") throw TypeError("Ray filter must be a function (state, type) => boolean");
76
+ if (!(Math.abs(a) < 2 ** 31 && Math.abs(o) < 2 ** 31 && Math.abs(s) < 2 ** 31)) throw RangeError(`Ray origin (${a}, ${o}, ${s}) is outside the world's 32-bit cells; move it closer`);
77
+ let f = e.blocks.table, { minY: p, maxY: _ } = e, T = r / d, E = Math.sign(c), D = Math.sign(l), O = Math.sign(u), k = C(a, c), A = C(o, l), j = C(s, u), M = w(k, a, c), N = w(A, o, l), P = w(j, s, u), F = 0, I = 0;
78
+ for (c && a === (c > 0 ? k : k + 1) ? I = 1 : l && o === (l > 0 ? A : A + 1) ? I = 2 : u && s === (u > 0 ? j : j + 1) && (I = 4);;) {
79
+ if (A >= p && A <= _) {
80
+ let t = e.getBlock(k, A, j), n = t ? f[t & 65535] : void 0, r = n && i(t, n) ? y(n, t) : 0, p = I & 1 ? 0 : I & 2 ? 1 : I & 4 ? 2 : -1, m = F;
81
+ if (n?.model && r ? x(n, t, [
82
+ k,
83
+ A,
84
+ j
85
+ ], [
86
+ a,
87
+ o,
88
+ s
89
+ ], [
90
+ c,
91
+ l,
92
+ u
93
+ ], F, Math.min(M, N, P, T), p) ? (m = b[0], p = b[1]) : p = -2 : r !== 255 && r && S(r, [
94
+ k,
95
+ A,
96
+ j
97
+ ], [
98
+ a,
99
+ o,
100
+ s
101
+ ], [
102
+ c,
103
+ l,
104
+ u
105
+ ], F, Math.min(M, N, P, T), p) ? (m = b[0], p = b[1]) : r !== 255 && (p = -2), p > -2) {
106
+ let e = [
107
+ k,
108
+ A,
109
+ j
110
+ ], r = [
111
+ 0,
112
+ 0,
113
+ 0
114
+ ], i = [
115
+ a + c * m,
116
+ o + l * m,
117
+ s + u * m
118
+ ], f = [
119
+ c,
120
+ l,
121
+ u
122
+ ];
123
+ if (m === F && F) for (let t = 0; t < 3; t++) I & 1 << t && (i[t] = f[t] > 0 ? e[t] : e[t] + 1);
124
+ else m !== F && p >= 0 && !n?.model && (i[p] = e[p] + .5);
125
+ return p >= 0 && (r[p] = -Math.sign(f[p])), {
126
+ cell: e,
127
+ normal: r,
128
+ adjacent: [
129
+ k + r[0],
130
+ A + r[1],
131
+ j + r[2]
132
+ ],
133
+ point: i,
134
+ distance: m * d,
135
+ state: t
136
+ };
137
+ }
138
+ } else if (D === 0 || (D > 0 ? A > _ : A < p)) return null;
139
+ if (F = Math.min(M, N, P), F > T || (I = 0, M === F && (k += E, M = w(k, a, c), I |= 1), N === F && (A += D, N = w(A, o, l), I |= 2), P === F && (j += O, P = w(j, s, u), I |= 4), (k | 0) !== k || (j | 0) !== j)) return null;
140
+ }
141
+ }
142
+ function E(e, r, i, a) {
143
+ let o = e.blocks.table, s = +!i, c = i === 2 ? 1 : 2, l = [
144
+ 1,
145
+ 2,
146
+ 4
147
+ ], u = Math.floor(r[s] + n), f = Math.floor(r[c] + n), m = a > 0, h = m ? r[i + 3] : r[i], g = m ? Math.ceil(h - n) - 1 : Math.floor(h + n), _ = m ? Math.floor(h + a + n) : Math.ceil(h + a - n) - 1, v = d;
148
+ p = !1;
149
+ for (let d = g; m ? d <= _ : d >= _; d += m ? 1 : -1) {
150
+ v[i] = d;
151
+ for (let g = u; g < r[s + 3] - n; g++) for (let u = f; u < r[c + 3] - n; u++) {
152
+ v[s] = g, v[c] = u;
153
+ let f = e.getBlock(v[0], v[1], v[2]), _ = o[f & 65535];
154
+ if (!_?.solid) continue;
155
+ let b = _.model ? t(_, f) : void 0, x = y(_, f), S = x === 255;
156
+ for (let e = 0; e < (b ? b.length : S ? 1 : 8); e++) {
157
+ if (!b && !S && !(x >> e & 1)) continue;
158
+ let t = S ? 1 : .5, o = b?.[e], f = d + (o ? o.min[i] : !S && e & l[i] ? .5 : 0), _ = o ? d + o.max[i] : f + t, v = g + (o ? o.min[s] : !S && e & l[s] ? .5 : 0), y = u + (o ? o.min[c] : !S && e & l[c] ? .5 : 0);
159
+ if (v >= r[s + 3] - n || (o ? g + o.max[s] : v + t) <= r[s] + n || y >= r[c + 3] - n || (o ? u + o.max[c] : y + t) <= r[c] + n) continue;
160
+ let C = m ? f - h : _ - h;
161
+ (m ? f >= h - n && C <= a + n : _ <= h + n && C >= a - n) && (p = !0, a = m ? Math.min(a, C) : Math.max(a, C));
162
+ }
163
+ }
164
+ if (p) break;
165
+ }
166
+ return r[i] += a, r[i + 3] += a, a;
167
+ }
168
+ var D = (e, t, n, r) => r !== 0 && (E(e, t, n, r), p);
169
+ function O(e, t, n, r, i, a) {
170
+ a[1] = D(e, t, 1, r);
171
+ let o = r ? a[1] && r < 0 : (f.set(t), E(e, f, 1, 0), p);
172
+ return a[0] = D(e, t, 0, n), a[2] = D(e, t, 2, i), o;
173
+ }
174
+ function k(e, t) {
175
+ let r = e.blocks.table;
176
+ for (let i = Math.floor(t[0] + n); i < t[3] - n; i++) for (let a = Math.floor(t[1] + n); a < t[4] - n; a++) for (let o = Math.floor(t[2] + n); o < t[5] - n; o++) {
177
+ let t = e.getBlock(i, a, o), n = r[t & 65535];
178
+ if (n?.liquid || n?.waterlog && t >>> 31) return !0;
179
+ }
180
+ return !1;
181
+ }
182
+ function A(e, t, n) {
183
+ if (m(e, "sweepBox"), !t?.min || !t.max || typeof t.min != "object" || typeof t.max != "object") throw TypeError("sweepBox box must be {min: {x, y, z}, max: {x, y, z}}, such as a Box3");
184
+ let r = /* @__PURE__ */ new Float64Array(6);
185
+ for (let e = 0; e < 6; e++) {
186
+ let n = "xyz"[e % 3];
187
+ r[e] = h((e < 3 ? t.min : t.max)[n], `sweepBox box ${e < 3 ? "min" : "max"}.${n}`);
188
+ }
189
+ if (r[0] > r[3] || r[1] > r[4] || r[2] > r[5]) throw RangeError("sweepBox box is empty (a min exceeds its max); pass min <= max on every axis");
190
+ let [i, a, o] = g(n, "sweepBox motion"), s = [
191
+ !1,
192
+ !1,
193
+ !1
194
+ ], c = O(e, r, i, a, o, s);
195
+ return {
196
+ position: [
197
+ r[0],
198
+ r[1],
199
+ r[2]
200
+ ],
201
+ hit: s,
202
+ onGround: c
203
+ };
204
+ }
205
+ var j = class {
206
+ constructor(e, t = {}) {
207
+ if (this.position = [
208
+ 0,
209
+ 0,
210
+ 0
211
+ ], this.velocity = [
212
+ 0,
213
+ 0,
214
+ 0
215
+ ], this.renderPosition = [
216
+ 0,
217
+ 0,
218
+ 0
219
+ ], this.flying = !1, this.ground = !1, this.liquid = !1, this.pushing = !1, this.time = 0, this.previous = [
220
+ 0,
221
+ 0,
222
+ 0
223
+ ], this.last = [
224
+ 0,
225
+ 0,
226
+ 0
227
+ ], this.box = /* @__PURE__ */ new Float64Array(6), this.start = /* @__PURE__ */ new Float64Array(6), this.stepped = /* @__PURE__ */ new Float64Array(6), this.hits = [
228
+ !1,
229
+ !1,
230
+ !1
231
+ ], m(e, "VoxelCharacter"), !t || typeof t != "object") throw TypeError("VoxelCharacter options must be an object");
232
+ this.world = e;
233
+ let n = t.size === void 0 ? [
234
+ .6,
235
+ 1.8,
236
+ .6
237
+ ] : g(t.size, "VoxelCharacter size");
238
+ if (!(n[0] > 0 && n[1] > 0 && n[2] > 0)) throw RangeError(`VoxelCharacter size [${n}] must be positive on every axis, e.g. [0.6, 1.8, 0.6]`);
239
+ this.size = [
240
+ n[0],
241
+ n[1],
242
+ n[2]
243
+ ], this.stepHeight = _(t.stepHeight, "VoxelCharacter stepHeight", .6), this.gravity = _(t.gravity, "VoxelCharacter gravity", 32);
244
+ let r = t.speeds ?? {};
245
+ if (typeof r != "object") throw TypeError("VoxelCharacter speeds must be {walk, sprint, swim, fly, jump}");
246
+ this.speeds = {
247
+ walk: _(r.walk, "VoxelCharacter speeds.walk", 4.317),
248
+ sprint: _(r.sprint, "VoxelCharacter speeds.sprint", 5.612),
249
+ swim: _(r.swim, "VoxelCharacter speeds.swim", 2),
250
+ fly: _(r.fly, "VoxelCharacter speeds.fly", 11),
251
+ jump: _(r.jump, "VoxelCharacter speeds.jump", 9)
252
+ };
253
+ }
254
+ get onGround() {
255
+ return this.ground;
256
+ }
257
+ get inLiquid() {
258
+ return this.liquid;
259
+ }
260
+ update(e, t = {}) {
261
+ if (h(e, "VoxelCharacter update dt") < 0) throw RangeError(`VoxelCharacter update dt ${e} is negative; pass the frame time in seconds`);
262
+ if (!t || typeof t != "object") throw TypeError("VoxelCharacter update intent must be an object");
263
+ let [n, a, o] = t.direction === void 0 ? [
264
+ 0,
265
+ 0,
266
+ 0
267
+ ] : g(t.direction, "VoxelCharacter intent direction"), s = this.position, c = this.previous;
268
+ if (s[0] !== this.last[0] || s[1] !== this.last[1] || s[2] !== this.last[2]) {
269
+ g(s, "VoxelCharacter position");
270
+ for (let e = 0; e < 3; e++) c[e] = s[e];
271
+ }
272
+ for (this.time = Math.min(this.time + e, i); this.time + r * 1e-6 >= r; this.time -= r) {
273
+ for (let e = 0; e < 3; e++) c[e] = s[e];
274
+ this.step(t, n, a, o);
275
+ }
276
+ this.time < 0 && (this.time = 0);
277
+ let l = this.time / r;
278
+ for (let e = 0; e < 3; e++) this.renderPosition[e] = c[e] + (s[e] - c[e]) * l, this.last[e] = s[e];
279
+ }
280
+ step(e, t, i, o) {
281
+ let { world: d, box: f, start: p, size: m, velocity: h, speeds: g, hits: _ } = this, v = this.position;
282
+ f[0] = v[0] - m[0] / 2, f[1] = v[1], f[2] = v[2] - m[2] / 2, f[3] = v[0] + m[0] / 2, f[4] = v[1] + m[1], f[5] = v[2] + m[2] / 2;
283
+ let y = this.liquid = k(d, f);
284
+ if (this.flying || y) {
285
+ let n = i + +!!e.jump - +!!e.descend, s = (this.flying ? g.fly : g.swim) / Math.max(1, Math.hypot(t, n, o)), c = this.flying ? u : l;
286
+ h[0] += (t * s - h[0]) * c, h[1] += (n * s - h[1]) * c, h[2] += (o * s - h[2]) * c, this.flying || (h[1] -= this.gravity * a * r, e.jump && this.pushing && (h[1] = Math.max(h[1], g.jump)));
287
+ } else {
288
+ let n = (e.sprint ? g.sprint : g.walk) / Math.max(1, Math.hypot(t, o)), i = this.ground ? s : c;
289
+ h[0] += (t * n - h[0]) * i, h[2] += (o * n - h[2]) * i, e.jump && this.ground && (h[1] = g.jump), h[1] = Math.max(h[1] - this.gravity * r, -78.4);
290
+ }
291
+ let b = h[0] * r, x = h[1] * r, S = h[2] * r;
292
+ p.set(f);
293
+ let C = O(d, f, b, x, S, _);
294
+ if (this.stepHeight > 0 && (_[0] || _[2]) && (this.ground || C || y)) {
295
+ let e = this.stepped;
296
+ e.set(p);
297
+ let t = E(d, e, 1, this.stepHeight), r = D(d, e, 0, b), i = D(d, e, 2, S), a = x - t, o = D(d, e, 1, a) && a < 0;
298
+ (e[0] - p[0]) ** 2 + (e[2] - p[2]) ** 2 > (f[0] - p[0]) ** 2 + (f[2] - p[2]) ** 2 + n * n && (f.set(e), _[0] = r, _[1] = o, _[2] = i, C = o);
299
+ }
300
+ for (let e = 0; e < 3; e++) _[e] && (h[e] = 0);
301
+ this.ground = C, this.pushing = _[0] || _[2], v[0] = (f[0] + f[3]) / 2, v[1] = f[1], v[2] = (f[2] + f[5]) / 2;
302
+ }
303
+ };
304
+ //#endregion
305
+ export { j as VoxelCharacter, T as raycastVoxels, A as sweepBox };
@@ -0,0 +1,6 @@
1
+ /**
2
+ * A voxel beauty shader (quads or bevel triangles) turned into a post replay: the same surface, written out as view
3
+ * normal and depth (`gbuffer`), ambient light (`ambient`), both (`occlusion`) or a material capture (`capture`). With a
4
+ * lightmap, charted surfaces replay their baked ambient.
5
+ */
6
+ export declare function voxelPostShader(source: string, mode: 'gbuffer' | 'ambient' | 'occlusion' | 'capture', capture?: string): string;
@@ -0,0 +1,296 @@
1
+ /// <reference lib="esnext.disposable" preserve="true" />
2
+ import type { EngineKernel } from '@margoengine/core';
3
+ import { type Engine, type EnginePlugin, type FrameGraph, type PassContext } from '@margoengine/core/extension';
4
+ import type { Vec3 } from '@margoengine/math';
5
+ import type { VoxelBevel } from './bevel.js';
6
+ import { type ChunkMesh } from './mesher.js';
7
+ import type { VoxelLightmap } from './lightmap.js';
8
+ import { BlockTextures } from './textures.js';
9
+ import type { VoxelWorld } from './world.js';
10
+ export type { VoxelLightmap } from './lightmap.js';
11
+ export { type BlockPixels, BlockTextures, type BlockTexturesOptions } from './textures.js';
12
+ /** What {@link VoxelRendererOptions.mesher} needs: `VoxelMesher`'s methods. */
13
+ export interface VoxelChunkMesher {
14
+ /** `border` asks for `ChunkMesh.border`, which a bevel needs to chamfer the chunk seams; a mesher may ignore it. */
15
+ mesh(world: VoxelWorld, key: string, priority?: number, eye?: Vec3, border?: boolean): Promise<ChunkMesh | null>;
16
+ cancel(key: string): void;
17
+ warm?(): void;
18
+ /**
19
+ * A mesher holding GPU objects (`GpuVoxelMesher`) moves to the engine's restored device here: the renderer calls it
20
+ * from its own restore hook, before meshing every chunk again. A mesher without GPU objects leaves it out.
21
+ */
22
+ setDevice?(device: GPUDevice): void;
23
+ dispose(): void;
24
+ }
25
+ /** Options of {@link VoxelRenderer.attach}. */
26
+ export interface VoxelRendererOptions {
27
+ /** The world to draw. The renderer meshes the chunks in `world.dirty` (nearest first) and removes their keys. */
28
+ world: VoxelWorld;
29
+ /** Textures of the blocks: `BlockTypeOptions.textures` are layers of it. */
30
+ textures: BlockTextures;
31
+ /** Mesher workers, as `VoxelMesherOptions.workers`. Default: half the logical cores minus one, 1 to 4. */
32
+ workers?: number;
33
+ /**
34
+ * The chunk mesher to use instead of a pool of `workers`, such as `GpuVoxelMesher` of `@margoengine/voxel/gpu-mesher`:
35
+ * any object with `VoxelMesher`'s `mesh`, `cancel` and `dispose`. The renderer never disposes a mesher it was given;
36
+ * after a device restore it hands it the new device through `setDevice` (when the mesher has one) and meshes again.
37
+ */
38
+ mesher?: VoxelChunkMesher;
39
+ /**
40
+ * Most GPU bytes for quads (12 each, in pages of 64): the buffer starts at 4 MiB and doubles up to this. A mesh that
41
+ * does not fit waits until other chunks free pages, with {@link VoxelRendererStats.poolFull} true meanwhile. Default
42
+ * the device's limit: the smaller of maxStorageBufferBindingSize and maxBufferSize, in whole pages (128 MiB, about
43
+ * 11.18 million quads, on a default WebGPU device); at most that limit.
44
+ */
45
+ maxQuadBytes?: number;
46
+ /** Initial {@link VoxelRenderer.daylight}. Default 1. */
47
+ daylight?: number;
48
+ /** Cutout cubes (leaves) are see-through; false draws them solid. Cross blocks always cut out. Default true. */
49
+ fancyLeaves?: boolean;
50
+ /**
51
+ * A bevel from `voxelBevel(amount)` (`@margoengine/voxel/bevel`): each chunk's cube faces become a chamfered
52
+ * triangle mesh, lit, shadowed and replayed like the quads; crosses and liquids stay quads. Default none (flat quads).
53
+ */
54
+ bevel?: VoxelBevel;
55
+ /** Initial {@link VoxelRenderer.lightmap}. Default none. */
56
+ lightmap?: VoxelLightmap;
57
+ /**
58
+ * Blocks per cell of `world`: a power of two, 1 to 4,096. Above 1 the world is a coarse copy (a far level of
59
+ * `@margoengine/voxel/lod`): cell (x, y, z) covers blocks `scale` x to `scale` (x + 1) and so on, textures stretch
60
+ * over it, and the renderer takes no lighting effect, casts no shadow and joins no capture. Default 1.
61
+ */
62
+ scale?: number;
63
+ /**
64
+ * Origin rebasing for worlds far from zero: an object whose `origin` is the float64 world point (engine units) drawn
65
+ * at engine zero, such as a `FloatingOrigin` of `@margoengine/cameras/origin`; read every frame. The camera, the
66
+ * engine and lighting hooks work in engine space (world minus origin); chunks stay at their world cells. The GPU
67
+ * draws chunks relative to a base snapped to 1,024 cells near the origin, so every float32 value it reads stays
68
+ * within about 1,500 cells of zero wherever the world is. Default null (world and engine space are the same).
69
+ */
70
+ origin?: VoxelOrigin | null;
71
+ }
72
+ /** What `VoxelRendererOptions.origin` reads: the float64 world point drawn at engine zero. */
73
+ export interface VoxelOrigin {
74
+ readonly origin: ArrayLike<number>;
75
+ }
76
+ /**
77
+ * A lighting effect for {@link VoxelRenderer.addLighting}: an object with glTF PBR's lighting hooks (contract version 1),
78
+ * such as `Shadows`, `Clustered`, `Fog` or `EnvironmentLighting`. Their `add(voxels)` calls `addLighting` for you.
79
+ */
80
+ export interface VoxelRendererEffect {
81
+ /**
82
+ * WGSL hooks for bind group `group` (the renderer passes 1): `declarations` and any of `direct` (wraps the sun
83
+ * term), `light` (local lights), `ambient` (replaces the hemisphere ambient) and `output` (wraps the display colour).
84
+ * Undefined while the effect is not ready; the renderer asks every frame.
85
+ */
86
+ pbr(group: number): {
87
+ readonly declarations: string;
88
+ direct?(direct: string): string;
89
+ readonly light?: string;
90
+ readonly ambient?: string;
91
+ output?(color: string): string;
92
+ } | undefined;
93
+ /** Bind group entries of those hooks' bindings. */
94
+ bindings(): GPUBindGroupEntry[];
95
+ }
96
+ /** Optional quad/bevel vertex motion supplied by a feature entry such as voxel/wind. */
97
+ export interface VoxelVertexMotion {
98
+ /** Shader transform for beauty, surface capture and shadow passes. */
99
+ shader(code: string): string;
100
+ /**
101
+ * Additional group-zero bindings, from binding 11 upward. Called each frame with `time`, the renderer clock in
102
+ * seconds (float64), and `base`, the cell the shader's positions are relative to (multiples of 1,024 per axis; see
103
+ * `VoxelRendererOptions.origin`), to refresh live uniforms; without them, to read the current entries for a bind
104
+ * group. A position p in the shader is world cell p + base.
105
+ */
106
+ bindings(device: GPUDevice, time?: number, base?: readonly number[]): GPUBindGroupEntry[];
107
+ /** Maximum outward displacement in blocks, for conservative chunk bounds. */
108
+ readonly padding: number;
109
+ /** Changes when shader definitions change. */
110
+ readonly version: number;
111
+ }
112
+ /** Material capture contract shared with SSR and SSGI from `@margoengine/post`; neither package is required. */
113
+ export interface VoxelCapture {
114
+ /** PBR Surface capture declarations, including `MargoCapture` and `margoCapture(s,v,k)`. */
115
+ declarations(code: string): string;
116
+ /** Capture color targets in declaration location order. */
117
+ readonly targets: readonly GPUColorTargetState[];
118
+ /** Begins the owner's shared pass; the owner ends it after all registered sources draw. */
119
+ begin(): GPURenderPassEncoder;
120
+ }
121
+ /** What `Shadows` hands {@link VoxelRenderer.drawShadow}: one cascade's depth pass and light matrix. */
122
+ export interface VoxelShadowContext {
123
+ /** The frame's pass context; the renderer reads its `device`. */
124
+ readonly context: {
125
+ readonly device: GPUDevice;
126
+ };
127
+ /** The cascade's depth pass (depth32float, less-equal). */
128
+ readonly pass: GPURenderPassEncoder;
129
+ /** The cascade's light view-projection matrix (column-major, depth 0 to 1). */
130
+ readonly matrix: Float32Array;
131
+ /** Cascade index, 0 to 3. */
132
+ readonly cascade: number;
133
+ }
134
+ /** What a {@link VoxelRenderer} holds and drew in its last frame. */
135
+ export interface VoxelRendererStats {
136
+ /** Chunks with quads on the GPU. */
137
+ readonly chunks: number;
138
+ /** Of those, chunks in the camera frustum. */
139
+ readonly visible: number;
140
+ /** Quads on the GPU. With a bevel only crosses and liquids are quads. */
141
+ readonly quads: number;
142
+ /** Chamfer triangles on the GPU (with a bevel). */
143
+ readonly triangles: number;
144
+ /** GPU bytes of the quad pages in use (768 per page of 64 quads). */
145
+ readonly bytes: number;
146
+ /** Draw calls: one per visible chunk layer with quads or triangles. */
147
+ readonly draws: number;
148
+ /**
149
+ * True while the pipelines of the current composition (target, lightmap, lighting effects) compile in the
150
+ * background; meanwhile the largest compiled composition whose effects are all still added draws, or nothing on the
151
+ * first frames (`draws` is 0). The compositions without each effect are compiled ahead, a little per frame, so taking
152
+ * out one normally draws its final result at once.
153
+ */
154
+ readonly compiling: boolean;
155
+ /**
156
+ * Chunks still on their way to the GPU: changed and not yet sent to the mesher, being meshed in a worker, or meshed
157
+ * and waiting for their upload (about 4 MiB a frame). 0 once the frame shows every chunk the world holds. Wait for
158
+ * it rather than for a number of frames: how long meshing takes depends on the CPU.
159
+ */
160
+ readonly pending: number;
161
+ /**
162
+ * True while a meshed chunk waits because the quad pool is at `maxQuadBytes` and no run of free pages fits it: that
163
+ * chunk is not drawn and `pending` stays above 0 until chunks are removed (or the renderer is made again with a
164
+ * larger `maxQuadBytes`, up to the device limit). False otherwise.
165
+ */
166
+ readonly poolFull: boolean;
167
+ }
168
+ /**
169
+ * Draws a {@link VoxelWorld} with an engine: an engine plugin that meshes the world's dirty chunks in workers
170
+ * ({@link VoxelMesher}, nearest chunks first), keeps their quads on the GPU, culls chunks against the camera frustum on
171
+ * the CPU and draws opaque and cutout blocks before the sky (pass order 1) and translucent ones back to front after
172
+ * water (order 4.5). Lit by the engine's directional light (`engine.lighting`) and an ambient term, both scaled by the
173
+ * chunks' baked sky light, ambient occlusion and block light (attach `VoxelLighting` to compute it: unlit chunks are
174
+ * dark). Needs the WebGPU backend.
175
+ *
176
+ * ```ts
177
+ * const voxels = VoxelRenderer.attach(engine, { world, textures: await BlockTextures.load(urls) });
178
+ * world.setBlock(x, y, z, stone); // drawn within two frames
179
+ * voxels.stats.draws;
180
+ * voxels.dispose();
181
+ * ```
182
+ */
183
+ export declare class VoxelRenderer implements EnginePlugin {
184
+ #private;
185
+ /** Plugin name, unique per renderer. */
186
+ readonly name: string;
187
+ /** The world drawn. */
188
+ readonly world: VoxelWorld;
189
+ /** The textures drawn with. */
190
+ readonly textures: BlockTextures;
191
+ /** Live {@link VoxelRendererOptions.origin}; null for none. */
192
+ origin: VoxelOrigin | null;
193
+ private constructor();
194
+ /**
195
+ * Creates a renderer for `options.world` and adds it to `engine`.
196
+ * @throws TypeError when `world` is not a VoxelWorld, `textures` not BlockTextures, `lightmap` not a `voxelLightmap`
197
+ * result, `bevel` not a `voxelBevel` result, or an option has the wrong type.
198
+ * @throws RangeError for `maxQuadBytes` under 768 or over the device's maxStorageBufferBindingSize or maxBufferSize, `daylight` that is
199
+ * negative or not finite, or `workers` that is not a whole number of 1 or more.
200
+ * @throws MargoError `unsupported` without the WebGPU backend or Web Workers.
201
+ */
202
+ static attach(engine: EngineKernel, options: VoxelRendererOptions): VoxelRenderer;
203
+ /** Blocks per cell of the world (option `scale`): 1, or a far level's power of two. */
204
+ get scale(): number;
205
+ /** Scale of the sky light (ambient light from the open sky), 0 (night) to 1 (day) and above. Live. Default 1. */
206
+ get daylight(): number;
207
+ set daylight(value: number);
208
+ /**
209
+ * Linear colour of block light (torches, lava: `BlockTypeOptions.light`), 0 to 1 or above per channel. Live. Default
210
+ * warm white `[1, 0.82, 0.6]`.
211
+ */
212
+ get blockLightColor(): Vec3;
213
+ set blockLightColor(value: Vec3);
214
+ /** Chunks, quads, bytes and draw calls of the last frame. */
215
+ get stats(): VoxelRendererStats;
216
+ /** True after disposal, for optional post and lighting hosts. */
217
+ get disposed(): boolean;
218
+ /** Notifies an optional host when this renderer leaves; the returned unsubscribe and disposal are idempotent. */
219
+ onDispose(callback: () => void): () => void;
220
+ /** Sets the native PBR material capture hook used by SSR/SSGI. Returns an identity-safe remover. */
221
+ setCapture(hook: VoxelCapture): () => void;
222
+ /**
223
+ * Replays native quad materials for GTAO/SSAO (`gbuffer`, `ambient`, `occlusion`) or SSR/SSGI (`capture`). Opaque and
224
+ * cutout geometry keep the beauty shader's texture, alpha, liquid normals and lighting hooks; with a bevel the
225
+ * chamfer triangles replay the same way. Capture excludes translucent surfaces. `occlusion` needs the post owner's
226
+ * two-target external pass; external/capture passes remain open for their owner. GPU state is allocated only on the
227
+ * first replay, and pipelines compile asynchronously: until they are ready nothing is drawn. Returns true when the
228
+ * replay was recorded, false when there was nothing to draw yet (no chunks, or pipelines still compiling), so an
229
+ * owner that needs the replay in a frame can wait for it rather than for a number of frames.
230
+ */
231
+ draw(c: Extract<PassContext, {
232
+ backend: 'webgpu';
233
+ }>, mode: 'gbuffer' | 'ambient' | 'occlusion' | 'capture', color: GPUTextureView, depth: GPUTextureView, phase?: 'all' | 'opaque' | 'blend', external?: {
234
+ pass: GPURenderPassEncoder;
235
+ view: ArrayLike<number>;
236
+ depthCompare: GPUCompareFunction;
237
+ }): boolean;
238
+ /**
239
+ * Changes whenever the shadow map must be drawn again: chunks with opaque or cutout quads arrive, change or leave
240
+ * inside the last map's cascades, the textures or GPU buffers change, or the shadow pipelines become ready. `Shadows`
241
+ * redraws a `static` source's map only then (and when its cascades move).
242
+ */
243
+ get shadowVersion(): number;
244
+ /**
245
+ * Baked light from `voxelLightmap(options)` (`@margoengine/voxel/lightmap`): fragments on its charts take the atlas's
246
+ * light instead of the sun and ambient terms; emission still adds and the rest stay live. Undefined restores live
247
+ * lighting. Live: the shader is composed again and compiled in the background. The atlas texture is borrowed, never
248
+ * destroyed. A `(device) => texture` atlas is made once per device and survives a device restore; a plain texture is
249
+ * lost with its device: the renderer goes on with live lighting, reports it once through `engine.onError` (`'state'`,
250
+ * #borrowed-lost), and a new lightmap assigned here brings the baked light back.
251
+ * @throws TypeError for anything but a `voxelLightmap` result or undefined.
252
+ * @throws MargoError `disposed` after `dispose()`.
253
+ */
254
+ get lightmap(): VoxelLightmap | undefined;
255
+ set lightmap(lightmap: VoxelLightmap | undefined);
256
+ /**
257
+ * Lights the voxels with an effect's glTF PBR lighting hooks: `shadows.add(voxels, {static: true})`,
258
+ * `clustered.add(voxels)`, `fog.add(voxels)` and `environment.add(voxels)` call this. The shader is composed again
259
+ * and compiled in the background; until it is ready the previous one keeps drawing. Returns a remover: the frame after
260
+ * it draws without the removed effect's resources, which its owner may dispose right away. Once a composition is
261
+ * ready, those without each one effect and the one without any compile ahead, a pipeline a frame; while the wanted one
262
+ * is not compiled, the largest compiled composition whose effects are all still added draws (see `stats.compiling`).
263
+ * @throws TypeError when `effect` has no `pbr` and `bindings` functions.
264
+ * @throws MargoError `attached` when the effect belongs to another engine, `disposed` after `dispose()`.
265
+ */
266
+ addLighting(effect: VoxelRendererEffect): () => void;
267
+ /** Adds one optional vertex-motion feature. Returns its removal function; dispose that feature after removal. */
268
+ addVertexMotion(motion: VoxelVertexMotion): () => void;
269
+ /**
270
+ * `Shadows` calls this per cascade (add the renderer with `shadows.add(voxels, {static: true})`): the opaque and
271
+ * cutout chunks inside the cascade, culled on the CPU, into its depth pass. Translucent blocks cast no shadow. With a
272
+ * bevel the chamfer triangles cast. Returns true when the casters were recorded, false while there are none or the
273
+ * shadow pipelines still compile (asynchronously, on the first call).
274
+ */
275
+ drawShadow({ context, pass, matrix, cascade }: VoxelShadowContext): boolean;
276
+ /** @internal Engine plugin hook. */
277
+ setup(engine: Engine): void;
278
+ /** @internal Engine plugin hook: prepare (order -6), opaque and cutout (1), translucent then its depth (4.5). */
279
+ registerPasses(graph: FrameGraph): void;
280
+ /** @internal Engine plugin hook: the new device starts empty and every chunk is meshed again. */
281
+ /** Seconds on the renderer's clock (water waves, voxel motion, animated materials). */
282
+ get time(): number;
283
+ /**
284
+ * Moves the renderer's clock by `delta` seconds without drawing (a delta of 0 or less moves nothing). Each frame update
285
+ * runs it, so `seek` from `@margoengine/cutscene/seek` replays it exactly.
286
+ */
287
+ advanceTime(delta: number): void;
288
+ onPreUpdate(delta: number): void;
289
+ onDeviceRestored(): void;
290
+ /**
291
+ * Stops the workers, frees the GPU buffers and leaves the engine. The textures stay (dispose them yourself); their GPU
292
+ * copy is freed when no renderer draws with them.
293
+ */
294
+ dispose(): void;
295
+ [Symbol.dispose](): void;
296
+ }