@margoengine/voxel 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +1670 -2
  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-BmXhcsFt.js +412 -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 +40 -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 +1616 -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 +173 -4
@@ -0,0 +1,461 @@
1
+ import { i as e, r as t } from "./octants-C_Ic7gdn.js";
2
+ import { blockMaterials as n, meshChunk as r, meshInput as i, sortQuads as a } from "./mesher.js";
3
+ import { MargoError as o } from "@margoengine/math/errors";
4
+ //#region src/border.ts
5
+ var s = 34, c = 1156, l = [
6
+ 0,
7
+ 0,
8
+ 1,
9
+ 1,
10
+ 2,
11
+ 2
12
+ ], u = [
13
+ 1,
14
+ -1,
15
+ c,
16
+ -1156,
17
+ s,
18
+ -34
19
+ ], d = [
20
+ 0,
21
+ 1,
22
+ 2,
23
+ 3,
24
+ 4,
25
+ 5
26
+ ].map((e) => {
27
+ let t = [
28
+ 1,
29
+ 2,
30
+ 4
31
+ ][l[e]], n = 0;
32
+ for (let r = 0; r < 8; r++) (r & t) === (e & 1 ? 0 : t) && (n |= 1 << r);
33
+ return n;
34
+ }), f = d.map((e, t) => d[t ^ 1]), p = /* @__PURE__ */ new WeakMap();
35
+ function m(e) {
36
+ let t = p.get(e);
37
+ if (t?.n === e.length) return t;
38
+ let n = e.length;
39
+ t = {
40
+ n,
41
+ layer: new Uint8Array(n),
42
+ opaque: new Uint8Array(n),
43
+ shape: new Uint8Array(n)
44
+ };
45
+ for (let r = 0; r < n; r++) {
46
+ let { layer: n, shape: i } = e[r], a = [
47
+ "cube",
48
+ "slab",
49
+ "stair"
50
+ ].indexOf(i);
51
+ t.shape[r] = a < 0 ? 3 : a, t.layer[r] = a < 0 ? 0 : [
52
+ "opaque",
53
+ "cutout",
54
+ "translucent"
55
+ ].indexOf(n) + 1, t.opaque[r] = +(n === "opaque");
56
+ }
57
+ return p.set(e, t), t;
58
+ }
59
+ var h;
60
+ function g({ chunk: n, shell: r, blocks: i }) {
61
+ let { layer: a, opaque: o, shape: p } = m(i), { palette: g, bits: _, words: v } = typeof n.pack == "function" ? n.pack() : n, y = (1 << _) - 1, b = h ??= new Uint32Array(s * c), x = (n) => {
62
+ let r = p[n & 65535];
63
+ return r === 0 ? 255 : r === 1 ? t(n >>> 16) : r === 2 ? e(n >>> 16) : 0;
64
+ };
65
+ for (let e = 0, t = 0, n = 0; e < 34; e++) for (let i = 0; i < 34; i++, t += s) if (e === 0 || e === 33 || i === 0 || i === 33) for (let e = 0; e < 34; e++) b[t + e] = r[n++];
66
+ else b[t] = r[n++], b[t + 33] = r[n++];
67
+ for (let e = 0; e < 32768; e++) {
68
+ let t = e * _;
69
+ b[1 + (e & 31) + ((e >> 5 & 31) + 1) * s + ((e >> 10) + 1) * c] = _ ? g[v[t >>> 5] >>> (t & 31) & y] : g[0];
70
+ }
71
+ let S = [], C = [
72
+ 0,
73
+ 0,
74
+ 0
75
+ ];
76
+ for (let e = -1; e <= 32; e++) for (let t = -1; t <= 32; t++) for (let n = -1; n <= 32; n++) {
77
+ n === 0 && e >= 0 && e < 32 && t >= 0 && t < 32 && (n = 32);
78
+ let r = n + 1 + (t + 1) * s + (e + 1) * c, i = b[r], p = i & 65535;
79
+ if (!a[p]) continue;
80
+ let m = x(i);
81
+ C[0] = n, C[1] = e, C[2] = t;
82
+ for (let i = 0; i < 6; i++) {
83
+ if ((i & 1 ? C[l[i]] < 0 : C[l[i]] > 31) || (m & d[i]) !== d[i]) continue;
84
+ let s = b[r + u[i]], c = s & 65535;
85
+ ((o[c] ? x(s) : 0) | (c === p ? x(s) : 0)) & f[i] || S.push(n + 1 | e + 1 << 6 | t + 1 << 12 | i << 18 | a[p] - 1 << 21);
86
+ }
87
+ }
88
+ return Uint32Array.from(S);
89
+ }
90
+ //#endregion
91
+ //#region src/gpu-mesher.ts
92
+ var _ = 39304, v = 32768, y = 192, b = 16, x = 64, S = 3, C = {
93
+ opaque: 0,
94
+ cutout: 1,
95
+ translucent: 2,
96
+ invisible: 3
97
+ }, w = {
98
+ cube: 0,
99
+ cross: 4,
100
+ liquid: 8,
101
+ slab: 76,
102
+ stair: 76,
103
+ model: 96,
104
+ object: 96
105
+ }, T = (e) => {
106
+ let t = 0;
107
+ for (let n = 0; n < 5; n++) if (e & 1 << n) for (let r = n + 1; r < 6; r++) e & 1 << r && (t |= 1 << n * 6 + r);
108
+ return t;
109
+ }, E = T(63), D = "\noverride CAP: u32 = 8u;\nconst V = 39304u; const SZ = 34u; const SY = 1156u; const BASE = 1191u;\n@group(0) @binding(0) var<storage, read> src: array<u32>;\n@group(0) @binding(1) var<storage, read> hdr: array<u32>;\n@group(0) @binding(2) var<storage, read_write> vol: array<u32>;\n@group(0) @binding(3) var<storage, read> tbl: array<u32>;\n@group(0) @binding(4) var<storage, read_write> slotq: array<u32>;\n@group(0) @binding(5) var<storage, read_write> aux: array<u32>;\n@group(0) @binding(6) var<storage, read_write> outq: array<u32>;\n\n\nfn slotCount(b: u32, slot: u32) -> u32 { return (b * 192u + slot) * 4u; }\nfn crossCount(b: u32, t: u32) -> u32 { return CAP * 768u + (b * 256u + t) * 4u; }\nfn crossOffset(b: u32, t: u32) -> u32 { return CAP * 1792u + (b * 256u + t) * 4u; }\nfn slotOffset(b: u32, slot: u32) -> u32 { return CAP * 2816u + (b * 192u + slot) * 4u; }\nfn totals(b: u32) -> u32 { return CAP * 3584u + b * 4u; }\nfn bases(b: u32) -> u32 { return CAP * 3588u + b * 4u; }\n\n\nfn state(b: u32, p: u32) -> u32 { return vol[b * 2u * V + p]; }\nfn info(b: u32, p: u32) -> u32 { return vol[b * 2u * V + V + p]; }\nfn occ(b: u32, p: u32) -> u32 { return (info(b, p) >> 12u) & 1u; }\n\nfn spread(b: u32, p: u32) -> u32 {\n let i = info(b, p);\n if (((i >> 12u) & 1u) != 0u) { return 0u; }\n return (((i >> 4u) & 15u) << 8u) | (i & 15u);\n}\nfn srcByte(at: u32, i: u32) -> u32 { return (src[at + (i >> 2u)] >> ((i & 3u) * 8u)) & 255u; }\n\n@compute @workgroup_size(256)\nfn unpack(@builtin(global_invocation_id) g: vec3u, @builtin(workgroup_id) w: vec3u) {\n let p = g.x; let b = w.y;\n if (p >= V) { return; }\n let m = b * 8u;\n let X = p % SZ; let Z = (p / SZ) % SZ; let Y = p / SY;\n var s: u32; var l: u32;\n if (X >= 1u && X <= 32u && Y >= 1u && Y <= 32u && Z >= 1u && Z <= 32u) {\n let i = (X - 1u) + (Z - 1u) * 32u + (Y - 1u) * 1024u;\n let bits = hdr[m];\n var slot = 0u;\n if (bits != 0u) {\n let bit = i * bits;\n slot = (src[hdr[m + 2u] + (bit >> 5u)] >> (bit & 31u)) & ((1u << bits) - 1u);\n }\n s = src[hdr[m + 1u] + slot];\n if (hdr[m + 6u] != 0u) { l = hdr[m + 7u]; } else { l = srcByte(hdr[m + 3u], i); }\n } else {\n\n var k: u32;\n if (Y == 0u || Y == 33u) {\n k = select(0u, 5380u, Y == 33u) + Z * 34u + X;\n } else {\n let lay0 = 1156u + (Y - 1u) * 132u;\n if (Z == 0u || Z == 33u) { k = lay0 + select(0u, 98u, Z == 33u) + X; }\n else { k = lay0 + 34u + (Z - 1u) * 2u + select(0u, 1u, X == 33u); }\n }\n s = src[hdr[m + 4u] + k];\n l = srcByte(hdr[m + 5u], k);\n }\n vol[b * 2u * V + p] = s;\n vol[b * 2u * V + V + p] = l | (tbl[(s & 0xffffu) * 8u] << 8u);\n}\n\nvar<workgroup> keys: array<u32, 1024>;\nvar<workgroup> lights: array<u32, 1024>;\n\n\nfn mean(n: u32, sum: u32) -> u32 { return (sum * 2u + n) / (n * 2u); }\n\nfn vertexLight(n: u32, sum: u32, own: u32) -> u32 {\n if (n == 0u) { return (own >> 4u) | ((own & 15u) << 4u); }\n return mean(n, sum >> 8u) | (mean(n, sum & 255u) << 4u);\n}\n\n@compute @workgroup_size(256)\nfn faces(@builtin(local_invocation_index) t: u32, @builtin(workgroup_id) w: vec3u) {\n let slot = w.x; let b = w.y; let d = slot / 32u; let s = slot % 32u;\n var NS = array<u32, 6>(1u, 1u, SY, SY, SZ, SZ);\n var US = array<u32, 6>(SZ, SZ, 1u, 1u, 1u, 1u);\n var VS = array<u32, 6>(SY, SY, SZ, SZ, SY, SY);\n let sN = NS[d]; let sU = US[d]; let sV = VS[d];\n let top = d == 2u; let side = d < 2u || d > 3u;\n for (var j = 0u; j < 4u; j++) {\n let m = t + j * 256u;\n keys[m] = 0u;\n let p = BASE + s * sN + (m & 31u) * sU + (m >> 5u) * sV;\n let f = info(b, p) >> 8u;\n if ((f & 32u) != 0u) { continue; }\n var q = p + sN;\n if ((d & 1u) != 0u) { q = p - sN; }\n let liquid = (f & 12u) == 8u;\n if (!liquid && occ(b, q) != 0u) { continue; }\n let st = state(b, p); let id = st & 0xffffu;\n let surface = liquid && d != 3u && (state(b, p + SY) & 0xffffu) != id;\n var drop = 0u;\n if (surface) { drop = (st >> 16u) & 7u; }\n if (occ(b, q) != 0u) {\n if (!top || drop == 0u) { continue; }\n } else if ((state(b, q) & 0xffffu) == id) {\n var below = (state(b, q) >> 16u) & 7u;\n if ((state(b, q + SY) & 0xffffu) == id) { below = 0u; }\n if (!side || !liquid || drop >= below) { continue; }\n }\n let a = occ(b, q - sU); let bb = occ(b, q + sU); let c = occ(b, q - sV); let e = occ(b, q + sV);\n var ao0 = 0u; var ao1 = 0u; var ao2 = 0u; var ao3 = 0u;\n var d0 = 0u; var d1 = 0u; var d2 = 0u; var d3 = 0u;\n if ((a & c) == 0u) { ao0 = 3u - a - c - occ(b, q - sU - sV); d0 = spread(b, q - sU - sV); }\n if ((bb & c) == 0u) { ao1 = 3u - bb - c - occ(b, q + sU - sV); d1 = spread(b, q + sU - sV); }\n if ((bb & e) == 0u) { ao2 = 3u - bb - e - occ(b, q + sU + sV); d2 = spread(b, q + sU + sV); }\n if ((a & e) == 0u) { ao3 = 3u - a - e - occ(b, q - sU + sV); d3 = spread(b, q - sU + sV); }\n let open = 1u - occ(b, q);\n let sq = spread(b, q); let sa = spread(b, q - sU); let sb = spread(b, q + sU);\n let sc = spread(b, q - sV); let se = spread(b, q + sV);\n let own = info(b, p) & 255u;\n keys[m] = tbl[id * 8u + 1u + d] | (ao0 << 12u) | (ao1 << 14u) | (ao2 << 16u) | (ao3 << 18u) |\n select(0u, 1u << 20u, surface) | (drop << 21u) | ((f & 3u) << 24u) | (1u << 31u);\n lights[m] = vertexLight(ao0 + open, sq + sa + sc + d0, own) |\n (vertexLight(ao1 + open, sq + sb + sc + d1, own) << 8u) |\n (vertexLight(ao2 + open, sq + sb + se + d2, own) << 16u) |\n (vertexLight(ao3 + open, sq + sa + se + d3, own) << 24u);\n }\n workgroupBarrier();\n if (t != 0u) { return; }\n\n\n var ATS = array<u32, 6>(0u, 0u, 5u, 5u, 10u, 10u);\n var ATU = array<u32, 6>(10u, 10u, 0u, 0u, 0u, 0u);\n var ATV = array<u32, 6>(5u, 5u, 10u, 10u, 5u, 5u);\n let base = (s << ATS[d]) | (d << 15u); let atU = ATU[d]; let atV = ATV[d];\n var count = array<u32, 3>(0u, 0u, 0u);\n var n = 0u;\n let region = (b * 192u + slot) * 4096u;\n for (var m = 0u; m < 1024u; m++) {\n let A = keys[m];\n if (A == 0u) { continue; }\n let u = m & 31u; let v = m >> 5u; let B = lights[m];\n let o0 = (A >> 12u) & 3u; let o1 = (A >> 14u) & 3u; let o2 = (A >> 16u) & 3u; let o3 = (A >> 18u) & 3u;\n let l0 = B & 255u; let l1 = (B >> 8u) & 255u; let l2 = (B >> 16u) & 255u; let l3 = B >> 24u;\n var width = 1u; var height = 1u;\n\n let single = ((A >> 24u) & 3u) == 2u;\n if (!single && o0 == o1 && o3 == o2 && l0 == l1 && l3 == l2) {\n loop {\n if (u + width >= 32u || keys[m + width] != A || lights[m + width] != B) { break; }\n width++;\n }\n }\n if (!single && o0 == o3 && o1 == o2 && l0 == l3 && l1 == l2) {\n loop {\n if (v + height >= 32u) { break; }\n var same = true;\n for (var k = m + (height << 5u); k < m + (height << 5u) + width; k++) {\n if (keys[k] != A || lights[k] != B) { same = false; break; }\n }\n if (!same) { break; }\n height++;\n }\n }\n for (var y = 0u; y < height; y++) {\n for (var k = m + (y << 5u); k < m + (y << 5u) + width; k++) { keys[k] = 0u; }\n }\n let flip = select(0u, 1u << 28u, o1 + o3 > o0 + o2);\n let lay = (A >> 24u) & 3u;\n let at = region + n * 4u;\n slotq[at] = base | (u << atU) | (v << atV) | ((width - 1u) << 18u) | ((height - 1u) << 23u) | flip |\n ((A >> 21u) << 29u);\n slotq[at + 1u] = A & 0x1fffffu;\n slotq[at + 2u] = B;\n slotq[at + 3u] = count[lay] | (lay << 30u);\n count[lay]++;\n n++;\n }\n let c = slotCount(b, slot);\n aux[c] = count[0]; aux[c + 1u] = count[1]; aux[c + 2u] = count[2]; aux[c + 3u] = n;\n}\n\n\nfn isCross(f: u32) -> bool { return (f & 32u) != 0u && (f & 12u) == 4u && (f & 3u) != 3u; }\n\n@compute @workgroup_size(256)\nfn crosses(@builtin(local_invocation_index) t: u32, @builtin(workgroup_id) w: vec3u) {\n let b = w.y;\n var count = array<u32, 3>(0u, 0u, 0u);\n for (var i = t * 128u; i < t * 128u + 128u; i++) {\n let f = info(b, BASE + (i & 31u) + ((i >> 5u) & 31u) * SZ + (i >> 10u) * SY) >> 8u;\n if (isCross(f)) { count[f & 3u] += 2u; }\n }\n let c = crossCount(b, t);\n aux[c] = count[0]; aux[c + 1u] = count[1]; aux[c + 2u] = count[2];\n}\n\n\n@compute @workgroup_size(1)\nfn offsets(@builtin(workgroup_id) w: vec3u) {\n let b = w.y;\n for (var l = 0u; l < 3u; l++) {\n var run = 0u;\n for (var t = 0u; t < 256u; t++) { aux[crossOffset(b, t) + l] = run; run += aux[crossCount(b, t) + l]; }\n for (var s = 0u; s < 192u; s++) { aux[slotOffset(b, s) + l] = run; run += aux[slotCount(b, s) + l]; }\n aux[totals(b) + l] = run;\n }\n}\n\n@compute @workgroup_size(256)\nfn place(@builtin(local_invocation_index) t: u32, @builtin(workgroup_id) w: vec3u) {\n let slot = w.x; let b = w.y;\n if (slot < 192u) {\n let region = (b * 192u + slot) * 4096u;\n for (var j = t; j < aux[slotCount(b, slot) + 3u]; j += 256u) {\n let at = region + j * 4u;\n let tag = slotq[at + 3u]; let lay = tag >> 30u;\n let o = (aux[bases(b) + lay] + aux[slotOffset(b, slot) + lay] + (tag & 0x3fffffffu)) * 3u;\n outq[o] = slotq[at]; outq[o + 1u] = slotq[at + 1u]; outq[o + 2u] = slotq[at + 2u];\n }\n return;\n }\n\n let c = crossOffset(b, t);\n var run = array<u32, 3>(aux[c], aux[c + 1u], aux[c + 2u]);\n for (var i = t * 128u; i < t * 128u + 128u; i++) {\n let p = BASE + (i & 31u) + ((i >> 5u) & 31u) * SZ + (i >> 10u) * SY;\n let f = info(b, p) >> 8u;\n if (!isCross(f)) { continue; }\n let lay = f & 3u; let l = info(b, p) & 255u;\n let cell = (i & 31u) | ((i >> 10u) << 5u) | (((i >> 5u) & 31u) << 10u);\n let w1 = tbl[(state(b, p) & 0xffffu) * 8u + 1u] | (0xffu << 12u);\n let w2 = ((l >> 4u) | ((l & 15u) << 4u)) * 0x01010101u;\n let o = (aux[bases(b) + lay] + run[lay]) * 3u;\n outq[o] = cell | (6u << 15u); outq[o + 1u] = w1; outq[o + 2u] = w2;\n outq[o + 3u] = cell | (7u << 15u); outq[o + 4u] = w1; outq[o + 5u] = w2;\n run[lay] += 2u;\n }\n}\n", O = /^(-?\d+),(-?\d+),(-?\d+)$/, k = (e) => e << 5 >> 5 === e, A = (e) => !!(e.opaque.length || e.cutout.length || e.translucent.length), j = class {
110
+ #e;
111
+ #t;
112
+ #n;
113
+ #r;
114
+ #i;
115
+ #a;
116
+ #o;
117
+ #s;
118
+ #c;
119
+ #l;
120
+ #u;
121
+ #d;
122
+ #f;
123
+ #p;
124
+ #m;
125
+ #h;
126
+ #g;
127
+ constructor(e, t = {}) {
128
+ this.stats = {
129
+ gpu: 0,
130
+ cpu: 0,
131
+ empty: 0,
132
+ batches: 0
133
+ }, this.#r = [], this.#u = /* @__PURE__ */ new Map(), this.#d = /* @__PURE__ */ new Set(), this.#f = !1, this.#p = !1, this.#m = 0, this.#h = !1, this.#g = !1;
134
+ let { batch: n = 8 } = t;
135
+ if (!Number.isInteger(n) || n < 1 || n > 32) throw RangeError(`GpuVoxelMesher batch is ${n}; pass a whole number from 1 to 32`);
136
+ M(e, n), this.#e = e, this.batch = n, this.#_(e);
137
+ }
138
+ setDevice(e) {
139
+ if (this.#S(), M(e, this.batch), e !== this.#e || this.#g) {
140
+ this.#v();
141
+ for (let e of this.#r.splice(0)) e.destroy();
142
+ this.#t = this.#c = this.#l = void 0, this.#n && (this.#n.buffer = void 0), this.#e = e, this.#g = !1, this.#_(e), this.#C();
143
+ }
144
+ }
145
+ #_(e) {
146
+ e.lost?.then(() => {
147
+ this.#e !== e || this.#h || (this.#g = !0, this.#v());
148
+ });
149
+ }
150
+ #v() {
151
+ for (let e of this.#d) e.cancelled || e.moved || (e.moved = !0, this.#u.has(e.key) || this.#u.set(e.key, {
152
+ ...e,
153
+ moved: !1,
154
+ order: this.#m++
155
+ }));
156
+ }
157
+ #y() {
158
+ if (this.#t) return;
159
+ let e = this.#e, t = e.createShaderModule({
160
+ label: "margoengine voxel gpu-mesher",
161
+ code: D
162
+ }), n = (n) => e.createComputePipeline({
163
+ label: `margoengine voxel gpu-mesher ${n}`,
164
+ layout: "auto",
165
+ compute: {
166
+ module: t,
167
+ entryPoint: n,
168
+ constants: { CAP: this.batch }
169
+ }
170
+ });
171
+ this.#t = {
172
+ unpack: n("unpack"),
173
+ faces: n("faces"),
174
+ crosses: n("crosses"),
175
+ offsets: n("offsets"),
176
+ place: n("place")
177
+ };
178
+ let r = GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC;
179
+ this.#i = this.#b(this.batch * 2 * _ * 4, r), this.#a = this.#b(this.batch * y * 4096 * 4, r), this.#o = this.#b(this.batch * 3592 * 4, r), this.#s = this.#b(this.batch * 32, r);
180
+ }
181
+ #b(e, t) {
182
+ let n = this.#e.createBuffer({
183
+ label: "margoengine voxel gpu-mesher",
184
+ size: e,
185
+ usage: t
186
+ });
187
+ return this.#r.push(n), n;
188
+ }
189
+ #x(e) {
190
+ e && (this.#r.splice(this.#r.indexOf(e), 1), e.destroy());
191
+ }
192
+ warm() {
193
+ this.#S(), this.#y();
194
+ }
195
+ mesh(e, t, n = 0, r, i = !1) {
196
+ if (this.#S(), typeof e?.getChunk != "function") throw TypeError("GpuVoxelMesher.mesh expects a VoxelWorld and a key");
197
+ if (typeof t != "string") throw TypeError("Chunk key must be a string; build it with chunkKey(cx, cy, cz)");
198
+ let a = O.exec(t)?.slice(1).map(Number);
199
+ if (!a?.every(k)) throw RangeError(`Chunk key "${t}" is not three chunk coordinates; build it with chunkKey(cx, cy, cz)`);
200
+ if (typeof n != "number" || !Number.isFinite(n)) throw RangeError(`GpuVoxelMesher.mesh priority is ${n}; pass a finite number`);
201
+ if (r !== void 0) {
202
+ if (!Array.isArray(r) && !ArrayBuffer.isView(r)) throw TypeError("GpuVoxelMesher.mesh eye must be [x, y, z]");
203
+ if (r.length !== 3 || ![
204
+ 0,
205
+ 1,
206
+ 2
207
+ ].every((e) => Number.isFinite(r[e]))) throw RangeError("GpuVoxelMesher.mesh eye must be three finite numbers");
208
+ }
209
+ let o = r && [
210
+ r[0] - a[0] * 32,
211
+ r[1] - a[1] * 32,
212
+ r[2] - a[2] * 32
213
+ ];
214
+ return this.cancel(t), new Promise((r, a) => {
215
+ this.#u.set(t, {
216
+ world: e,
217
+ key: t,
218
+ priority: n,
219
+ order: this.#m++,
220
+ eye: o,
221
+ border: i === !0,
222
+ resolve: r,
223
+ reject: a,
224
+ cancelled: !1
225
+ }), this.#C();
226
+ });
227
+ }
228
+ cancel(e) {
229
+ let t = this.#u.get(e);
230
+ t && (this.#u.delete(e), t.resolve(null));
231
+ for (let t of this.#d) t.key === e && (t.cancelled = !0);
232
+ }
233
+ #S() {
234
+ if (this.#h) throw new o("disposed", "GpuVoxelMesher is disposed; create a new GpuVoxelMesher");
235
+ }
236
+ #C() {
237
+ this.#f || this.#p || !this.#u.size || this.#h || this.#g || (this.#p = !0, queueMicrotask(() => {
238
+ this.#p = !1, !this.#f && this.#u.size && !this.#h && !this.#g && this.#w();
239
+ }));
240
+ }
241
+ async #w() {
242
+ this.#f = !0;
243
+ let e = this.#e, t = [];
244
+ try {
245
+ let e = [...this.#u.values()].sort((e, t) => e.priority - t.priority || e.order - t.order);
246
+ for (let n of e) {
247
+ if (t.length === this.batch) break;
248
+ this.#u.delete(n.key);
249
+ try {
250
+ let e = i(n.world, n.key);
251
+ if (!e) {
252
+ n.resolve(null);
253
+ continue;
254
+ }
255
+ if (t.length && e.blocks !== this.#n?.blocks) {
256
+ this.#u.set(n.key, n);
257
+ break;
258
+ }
259
+ let o = e.chunk, s = this.#T(e.blocks), c = 0;
260
+ for (let e of o.palette) c |= s[e & 65535];
261
+ for (let t of e.shell) c |= s[t & 65535];
262
+ let l = s[o.palette[0] & 65535];
263
+ if (!o.bits && (l & 3) === S) this.stats.empty++, n.resolve({
264
+ ...P(),
265
+ visibility: E
266
+ });
267
+ else if (c & x) {
268
+ this.stats.cpu++;
269
+ let t = r(e);
270
+ n.eye && a(t.translucent, n.eye), n.border && A(t) && (t.border = g(e)), n.resolve(t);
271
+ } else t.push({
272
+ job: n,
273
+ chunk: o,
274
+ shell: e.shell,
275
+ light: e.light,
276
+ visibility: F(o, s),
277
+ border: n.border ? g(e) : void 0
278
+ }), this.#d.add(n);
279
+ } catch (e) {
280
+ n.reject(e);
281
+ }
282
+ }
283
+ if (t.length) {
284
+ let e = await this.#E(t);
285
+ this.stats.gpu += t.length, this.stats.batches++, t.forEach(({ job: t, border: n }, r) => {
286
+ if (t.moved) return;
287
+ if (t.cancelled || this.#h) return t.resolve(null);
288
+ let i = e[r];
289
+ t.eye && a(i.translucent, t.eye), n && A(i) && (i.border = n), t.resolve(i);
290
+ });
291
+ }
292
+ } catch (n) {
293
+ e === this.#e && !this.#g && n?.name === "AbortError" && (this.#g = !0, this.#v());
294
+ for (let { job: e } of t) if (e.moved) continue;
295
+ else e.cancelled || this.#h ? e.resolve(null) : e.reject(n);
296
+ } finally {
297
+ for (let { job: e } of t) this.#d.delete(e);
298
+ this.#f = !1, this.#C();
299
+ }
300
+ }
301
+ #T(e) {
302
+ let t = this.#n;
303
+ if (t?.blocks === e && t.n === e.length) return t.flags;
304
+ let r = new Uint8Array(e.length), i = n(e), a = new Uint32Array(e.length * 8);
305
+ for (let t = 0; t < e.length; t++) {
306
+ let n = C[e[t]?.layer], o = w[e[t]?.shape];
307
+ if (n === void 0 || o === void 0) throw TypeError(`GpuVoxelMesher block type ${t} is not a block type; use the world's BlockTypes`);
308
+ r[t] = n | o | (n === 0 && o === 0 ? b : 0) | (n === 3 || o === 4 ? 32 : 0) | (e[t].vertexMotion ? x : 0), a[t * 8] = r[t];
309
+ for (let e = 0; e < 6; e++) a[t * 8 + 1 + e] = i[t * 6 + e];
310
+ }
311
+ return this.#x(t?.buffer), this.#n = t = {
312
+ blocks: e,
313
+ n: e.length,
314
+ flags: r,
315
+ data: a
316
+ }, r;
317
+ }
318
+ async #E(e) {
319
+ this.#y();
320
+ let t = this.#e, n = this.#t, r = this.#n, i = e.length, a = new Uint32Array(this.batch * 8), s = [];
321
+ r.buffer || (r.buffer = this.#b(r.data.byteLength, GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST), t.queue.writeBuffer(r.buffer, 0, r.data));
322
+ let c = 0, l = (e) => {
323
+ s.push(e);
324
+ let t = c;
325
+ return c += e.length, t;
326
+ }, u = (e) => {
327
+ let t = new Uint8Array(e.length + 3 & -4);
328
+ return t.set(e), new Uint32Array(t.buffer);
329
+ };
330
+ e.forEach(({ chunk: e, shell: t, light: n }, r) => {
331
+ let i = r * 8;
332
+ a[i] = e.bits, a[i + 1] = l(e.palette), a[i + 2] = l(e.words), typeof e.light == "number" ? (a[i + 6] = 1, a[i + 7] = e.light) : a[i + 3] = l(u(e.light)), a[i + 4] = l(t), a[i + 5] = l(u(n));
333
+ });
334
+ let d = new Uint32Array(Math.max(c, 1)), f = 0;
335
+ for (let e of s) d.set(e, (f += e.length) - e.length);
336
+ (!this.#c || this.#c.size < d.byteLength) && (this.#x(this.#c), this.#c = this.#b(Math.max(d.byteLength, 1 << 20), GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST)), t.pushErrorScope("validation"), t.queue.writeBuffer(this.#c, 0, d), t.queue.writeBuffer(this.#s, 0, a);
337
+ let p = {
338
+ unpack: [
339
+ 0,
340
+ 1,
341
+ 2,
342
+ 3
343
+ ],
344
+ faces: [
345
+ 2,
346
+ 3,
347
+ 4,
348
+ 5
349
+ ],
350
+ crosses: [2, 5],
351
+ offsets: [5],
352
+ place: [
353
+ 2,
354
+ 3,
355
+ 4,
356
+ 5,
357
+ 6
358
+ ]
359
+ }, m = (e, a, o, s) => {
360
+ let c = [
361
+ this.#c,
362
+ this.#s,
363
+ this.#i,
364
+ r.buffer,
365
+ this.#a,
366
+ this.#o,
367
+ s
368
+ ], l = n[a], u = e.beginComputePass();
369
+ u.setPipeline(l), u.setBindGroup(0, t.createBindGroup({
370
+ layout: l.getBindGroupLayout(0),
371
+ entries: p[a].map((e) => ({
372
+ binding: e,
373
+ resource: { buffer: c[e] }
374
+ }))
375
+ })), u.dispatchWorkgroups(o, i), u.end();
376
+ }, h = this.batch * 3584 * 4, g = t.createBuffer({
377
+ size: i * 16,
378
+ usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST
379
+ }), v = t.createCommandEncoder();
380
+ m(v, "unpack", Math.ceil(_ / 256)), m(v, "faces", y), m(v, "crosses", 1), m(v, "offsets", 1), v.copyBufferToBuffer(this.#o, h, g, 0, i * 16), t.queue.submit([v.finish()]);
381
+ let b;
382
+ try {
383
+ let [e] = await Promise.all([t.popErrorScope(), g.mapAsync(GPUMapMode.READ)]);
384
+ if (N(t, this.#e), e) throw new o("failed", `GpuVoxelMesher GPU work failed (${e.message}); mesh the chunk with meshChunk or VoxelMesher instead`);
385
+ b = new Uint32Array(g.getMappedRange().slice(0));
386
+ } finally {
387
+ g.destroy();
388
+ }
389
+ let x = new Uint32Array(i * 4), S = 0;
390
+ for (let e = 0; e < i; e++) for (let t = 0; t < 3; t++) x[e * 4 + t] = S, S += b[e * 4 + t];
391
+ let C = Math.max(S * 12, 16);
392
+ (!this.#l || this.#l.size < C) && (this.#x(this.#l), this.#l = this.#b(Math.max(C, 1 << 20), GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST)), t.queue.writeBuffer(this.#o, this.batch * 3588 * 4, x);
393
+ let w = t.createBuffer({
394
+ size: C,
395
+ usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST
396
+ });
397
+ v = t.createCommandEncoder(), m(v, "place", 193, this.#l), v.copyBufferToBuffer(this.#l, 0, w, 0, C), t.queue.submit([v.finish()]);
398
+ let T;
399
+ try {
400
+ await w.mapAsync(GPUMapMode.READ), N(t, this.#e), T = new Uint32Array(w.getMappedRange().slice(0));
401
+ } finally {
402
+ w.destroy();
403
+ }
404
+ return e.map(({ visibility: e }, t) => {
405
+ let n = (e) => T.slice(x[t * 4 + e] * 3, (x[t * 4 + e] + b[t * 4 + e]) * 3);
406
+ return {
407
+ opaque: n(0),
408
+ cutout: n(1),
409
+ translucent: n(2),
410
+ visibility: e
411
+ };
412
+ });
413
+ }
414
+ dispose() {
415
+ if (!this.#h) {
416
+ this.#h = !0;
417
+ for (let e of this.#u.values()) e.resolve(null);
418
+ this.#u.clear();
419
+ for (let e of this.#d) e.cancelled = !0;
420
+ for (let e of this.#r.splice(0)) e.destroy();
421
+ }
422
+ }
423
+ [Symbol.dispose]() {
424
+ this.dispose();
425
+ }
426
+ };
427
+ function M(e, t) {
428
+ if (typeof e?.createComputePipeline != "function") throw TypeError("GpuVoxelMesher expects a GPUDevice; pass engine.gpu of a WebGPU engine");
429
+ let n = t * y * 4096 * 4;
430
+ if (n > e.limits.maxStorageBufferBindingSize) throw RangeError(`GpuVoxelMesher batch ${t} needs ${n} bytes in one binding, over this device's limit; pass a smaller batch`);
431
+ }
432
+ function N(e, t) {
433
+ if (e !== t) throw new o("state", "GpuVoxelMesher moved to another device during a batch; its jobs run again there");
434
+ }
435
+ var P = () => ({
436
+ opaque: /* @__PURE__ */ new Uint32Array(),
437
+ cutout: /* @__PURE__ */ new Uint32Array(),
438
+ translucent: /* @__PURE__ */ new Uint32Array()
439
+ });
440
+ function F(e, t) {
441
+ let { palette: n, bits: r, words: i } = e;
442
+ if (!r) return t[n[0] & 65535] & b ? 0 : E;
443
+ let a = new Uint8Array(v), o = new Int32Array(v), s = (1 << r) - 1;
444
+ for (let e = 0; e < v; e++) {
445
+ let o = e * r;
446
+ a[e] = t[n[i[o >>> 5] >>> (o & 31) & s] & 65535] >> 4 & 1;
447
+ }
448
+ let c = 0;
449
+ for (let e = 0; e < v && c !== E; e++) {
450
+ if (a[e]) continue;
451
+ let t = 0, n = 0;
452
+ for (a[e] = 1, o[t++] = e; t;) {
453
+ let e = o[--t], r = e & 31, i = e >> 5 & 31, s = e >> 10;
454
+ r === 31 ? n |= 1 : a[e + 1] || (a[e + 1] = 1, o[t++] = e + 1), r === 0 ? n |= 2 : a[e - 1] || (a[e - 1] = 1, o[t++] = e - 1), s === 31 ? n |= 4 : a[e + 1024] || (a[e + 1024] = 1, o[t++] = e + 1024), s === 0 ? n |= 8 : a[e - 1024] || (a[e - 1024] = 1, o[t++] = e - 1024), i === 31 ? n |= 16 : a[e + 32] || (a[e + 32] = 1, o[t++] = e + 32), i === 0 ? n |= 32 : a[e - 32] || (a[e - 32] = 1, o[t++] = e - 32);
455
+ }
456
+ c |= T(n);
457
+ }
458
+ return c;
459
+ }
460
+ //#endregion
461
+ export { j as GpuVoxelMesher };
@@ -0,0 +1,40 @@
1
+ // @ts-ignore optional peer: its types apply when it is installed
2
+ import type { GranularMpmParams, GranularMpmSolver } from '@margoengine/physics/granular-mpm';
3
+ // @ts-ignore optional peer: its types apply when it is installed
4
+ import type { Cell, PhysicsWorld } from '@margoengine/physics/world';
5
+ import { type VoxelWorld } from './world.js';
6
+ /**
7
+ * One voxel that becomes granular material: its position in voxel coordinates, the expected block ID and the
8
+ * material table index (default 0).
9
+ */
10
+ export interface VoxelGranularCell {
11
+ /** Position of a loaded, solid cube in voxel coordinates. */
12
+ readonly position: Cell;
13
+ /** Expected block ID. Pick the voxel type representing the loose material. */
14
+ readonly block: number;
15
+ /** Index into the solver material table. Defaults to 0. */
16
+ readonly material?: number;
17
+ }
18
+ /**
19
+ * Options for turning voxels into granular points: the cells, voxel side in metres (default 1), samples per axis
20
+ * (default 2), voxel origin, physics cell and solver parameters.
21
+ */
22
+ export interface VoxelGranularOptions {
23
+ readonly cells: readonly VoxelGranularCell[];
24
+ /** Side length of a voxel, metres. Defaults to 1. */
25
+ readonly metresPerVoxel?: number;
26
+ /** Number of equal-volume samples along each axis of a cube. Defaults to 2. */
27
+ readonly samplesPerAxis?: number;
28
+ /** Voxel corner at the receiving cell's local origin. Defaults to [0, 0, 0]. */
29
+ readonly voxelOrigin?: Cell;
30
+ /** Floating-origin cell in the physics world. Defaults to [0, 0, 0]. */
31
+ readonly cell?: Cell;
32
+ /** Solver parameters. Grid spacing defaults to the voxel side divided by samplesPerAxis. */
33
+ readonly params?: Omit<GranularMpmParams, 'points'>;
34
+ }
35
+ /**
36
+ * Seeds a fresh, loaded MPM solver from the selected cells and consumes only those solids.
37
+ * Call at a solver step boundary. Invalid input or a failed solver registration leaves all source cells available.
38
+ * Observers receive ordinary voxel removals after the complete selection and the solver are committed.
39
+ */
40
+ export declare function transferVoxelGranular(voxels: VoxelWorld, physics: PhysicsWorld, solver: GranularMpmSolver, options: VoxelGranularOptions): void;
@@ -0,0 +1,46 @@
1
+ import { r as e } from "./blocks-BabtpQT-.js";
2
+ import { r as t } from "./world-CelTlft2.js";
3
+ import { MargoError as n } from "@margoengine/math/errors";
4
+ //#region src/granular-mpm.ts
5
+ function r(r, i, a, o) {
6
+ let { cells: s, metresPerVoxel: c = 1, samplesPerAxis: l = 2, voxelOrigin: u = [
7
+ 0,
8
+ 0,
9
+ 0
10
+ ], cell: d, params: f } = o, p = c / l, m = p * p * p;
11
+ if (!Number.isSafeInteger(l) || l < 1 || !(m > 0 && Number.isFinite(m))) throw new n("state", "Use a positive voxel size and a positive whole sample count with finite sample volume.");
12
+ if (!s.length || u.length !== 3 || !u.every(Number.isSafeInteger)) throw new n("state", "Select at least one loose-material cell and use three integer voxel origin coordinates.");
13
+ let h = s.length * l * l * l;
14
+ if (!Number.isSafeInteger(h) || h * 4012 >= 4294967296) throw new n("state", "Reduce the voxel selection or samples per axis to fit in the granular arena.");
15
+ let g = [], _ = /* @__PURE__ */ new Set();
16
+ for (let { position: [t, i, a], block: o } of s) {
17
+ let s = `${t},${i},${a}`, c = r.getBlock(t, i, a), l = r.blocks.table[e(c)];
18
+ if (!c || !l?.solid || l.liquid || l.shape !== "cube" || l.id !== o || _.has(s)) throw new n("state", `Select each matching, loaded solid cube once. Check voxel (${s}).`);
19
+ _.add(s), g.push([
20
+ t,
21
+ i,
22
+ a,
23
+ c
24
+ ]);
25
+ }
26
+ let v = [];
27
+ for (let [e, [t, n, r]] of g.entries()) {
28
+ let i = (t - u[0]) * c, a = (n - u[1]) * c, o = (r - u[2]) * c, d = s[e].material;
29
+ for (let e = 0; e < l; e++) for (let t = 0; t < l; t++) for (let n = 0; n < l; n++) v.push({
30
+ position: [
31
+ i + (n + .5) * p,
32
+ a + (t + .5) * p,
33
+ o + (e + .5) * p
34
+ ],
35
+ volume: m,
36
+ material: d
37
+ });
38
+ }
39
+ if (i.add(a, {
40
+ spacing: p,
41
+ ...f,
42
+ points: v
43
+ }, d), !t(r, g)) throw i.remove(a.id), new n("state", "Voxel sources changed during registration. Select the current blocks and load a fresh solver.");
44
+ }
45
+ //#endregion
46
+ export { r as transferVoxelGranular };
@@ -0,0 +1,6 @@
1
+ export type { BlockLayer, BlockShape, BlockType, BlockTypeOptions, StairCorner, StairFacing } from './blocks.js';
2
+ export { BlockTypes, blockData, blockId, blockState, connectStairs, doubleSlabState, shapeOctants, slabState, stairState, waterlogState, } from './blocks.js';
3
+ export type { PackedChunk } from './chunk.js';
4
+ export { CHUNK_SIZE, VoxelChunk } from './chunk.js';
5
+ export type { VoxelWorldOptions } from './world.js';
6
+ export { chunkKey, VoxelWorld } from './world.js';
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ import { a as e, c as t, d as n, i as r, l as i, n as a, o, r as s, t as c, u as l } from "./blocks-BabtpQT-.js";
2
+ import { n as u, t as d } from "./chunk-BCoaKSn4.js";
3
+ import { n as f, t as p } from "./world-CelTlft2.js";
4
+ export { c as BlockTypes, d as CHUNK_SIZE, u as VoxelChunk, p as VoxelWorld, a as blockData, s as blockId, r as blockState, f as chunkKey, e as connectStairs, o as doubleSlabState, t as shapeOctants, i as slabState, l as stairState, n as waterlogState };
@@ -0,0 +1,71 @@
1
+ import type { EngineKernel } from '@margoengine/core';
2
+ import type { Vec3 } from '@margoengine/math';
3
+ import { VoxelWorld } from './world.js';
4
+ /** Options of {@link VoxelColorLight.attach}. */
5
+ export interface VoxelColorLightOptions {
6
+ /** The world whose emitters light it. */
7
+ world: VoxelWorld;
8
+ /**
9
+ * Light colour per block type name, linear RGB 0..1: the channels' levels are the type's `light` times each
10
+ * channel, rounded. Types not named give white light (all three channels at `light`). Live: set a colour and the
11
+ * light floods again.
12
+ */
13
+ colors?: Readonly<Record<string, Vec3>>;
14
+ /** Cells flooded per {@link VoxelColorLight.update} per channel, as `VoxelLighting`'s budget. Default 4096. */
15
+ budget?: number;
16
+ /** Size in blocks of the lit box kept on the GPU around the camera: multiples of 32. Default [128, 64, 128]. */
17
+ extent?: readonly [number, number, number];
18
+ /** Scale of the coloured light, a finite number >= 0. Live (checked by each update). Default 1. */
19
+ strength?: number;
20
+ }
21
+ /**
22
+ * Coloured block light for a block world: red lava, blue lamps, green crystals. Attach it with the engine, add it to
23
+ * the world's `VoxelRenderer` with {@link VoxelColorLight.add}, call {@link VoxelColorLight.update} every frame, and set
24
+ * the renderer's `blockLightColor` to `[0, 0, 0]` so its single-channel block light does not add on top.
25
+ */
26
+ export declare class VoxelColorLight {
27
+ #private;
28
+ readonly world: VoxelWorld;
29
+ /** The engine whose device holds the light volume (a renderer of another engine refuses it). */
30
+ readonly owner: EngineKernel;
31
+ /** Scale of the coloured light, a finite number >= 0. Live; update() throws RangeError for any other value. */
32
+ strength: number;
33
+ /**
34
+ * @throws TypeError for a wrong engine, world or option type; RangeError for options out of range.
35
+ */
36
+ static attach(engine: EngineKernel, options: VoxelColorLightOptions): VoxelColorLight;
37
+ private constructor();
38
+ /**
39
+ * The light colour of a block type (by name). Setting one re-lights its emitters: their cells change level in each
40
+ * channel copy and flood again over the next updates.
41
+ * @throws RangeError for a colour outside 0..1 or a type that is not defined.
42
+ */
43
+ setColor(name: string, color: Vec3): void;
44
+ /** Red, green and blue block light levels (0..15) of a cell, as flooded so far. */
45
+ levels(x: number, y: number, z: number): [number, number, number];
46
+ /**
47
+ * Floods each channel up to `budget` cells, follows the camera and uploads the chunks whose light changed or that came
48
+ * into range. Call it once a frame. Returns the cells flooded.
49
+ * @throws MargoError `disposed` after `dispose()`.
50
+ * @throws RangeError when the renderer's origin holds a value that is not a finite number, or `strength` is not a
51
+ * finite number >= 0.
52
+ */
53
+ update(): number;
54
+ /**
55
+ * Adds the light to a `VoxelRenderer` (its `addLighting`) and follows that renderer's `origin` from then on: the lit
56
+ * box is placed around the camera's world chunk and the shading reads it in engine space. Returns the remover.
57
+ */
58
+ add(voxels: {
59
+ addLighting(effect: VoxelColorLight): () => void;
60
+ }): () => void;
61
+ /** The renderer's lighting hooks: the coloured light term. Undefined until the first `update()` made the volume. */
62
+ pbr(group: number): {
63
+ declarations: string;
64
+ light: string;
65
+ } | undefined;
66
+ /** Bind group entries of the hooks: the volume, its sampler and the window. */
67
+ bindings(): GPUBindGroupEntry[];
68
+ /** Stops following the world and frees the volume. */
69
+ dispose(): void;
70
+ [Symbol.dispose](): void;
71
+ }