@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
@@ -0,0 +1,13 @@
1
+ /** Prepared per-block vertex motion, created by defineWindBlock in the optional wind entry. */
2
+ export interface VoxelBlockMotion {
3
+ /** Foliage bends horizontally; liquid moves surface vertices vertically. */
4
+ readonly kind: 'foliage' | 'liquid';
5
+ /** Maximum displacement in blocks. */
6
+ readonly amplitude: number;
7
+ /** Angular phase change per second. */
8
+ readonly speed: number;
9
+ /** Spatial angular frequency in radians per block. */
10
+ readonly frequency: number;
11
+ /** Foliage root vertices remain fixed when true. */
12
+ readonly rooted: boolean;
13
+ }
package/dist/wind.d.ts ADDED
@@ -0,0 +1,68 @@
1
+ /// <reference lib="esnext.disposable" preserve="true" />
2
+ import type { BlockTypes, BlockTypeOptions } from './blocks.js';
3
+ import type { VoxelRenderer, VoxelVertexMotion } from './render.js';
4
+ import type { VoxelBlockMotion } from './wind-data.js';
5
+ export type { VoxelBlockMotion } from './wind-data.js';
6
+ /** Per-block foliage or liquid vertex animation. */
7
+ export interface VoxelWindBlockOptions {
8
+ /** Default foliage for non-liquids, liquid for liquid shapes. */
9
+ kind?: 'foliage' | 'liquid';
10
+ /** Maximum displacement in blocks, 0..0.5. Default 0.12 for foliage, 0.04 for liquid. */
11
+ amplitude?: number;
12
+ /** Angular speed, 0..100 radians/second. Default 1.6. */
13
+ speed?: number;
14
+ /** Angular spatial frequency, 0..100 radians/block. Default 1.3. */
15
+ frequency?: number;
16
+ /** Anchors foliage at the cell floor. Default true for crosses, false otherwise. */
17
+ rooted?: boolean;
18
+ }
19
+ /** Defines a block with prepared motion metadata; animated faces remain unit quads to keep shared wave edges joined. */
20
+ export declare function defineWindBlock(blocks: BlockTypes, name: string, options?: BlockTypeOptions, motion?: VoxelWindBlockOptions): number;
21
+ /** XZ wind direction/strength and optional explicit animation clock. */
22
+ export interface VoxelWindOptions {
23
+ /** XZ vector, default [1,0]. Length below 1 scales displacement, zero stops it; length above 1 is normalized. */
24
+ direction?: readonly [number, number];
25
+ /** Explicit seconds, in +/-1e6. Default renderer time; use undefined to return to that clock. */
26
+ time?: number;
27
+ }
28
+ /** CPU reference of the GPU motion, for tools and deterministic seam checks. Returns an offset, in blocks. */
29
+ export declare function sampleVoxelWind(motion: VoxelBlockMotion, position: readonly [number, number, number], cellFloor: number, time: number, direction?: readonly [number, number], surface?: boolean): [number, number, number];
30
+ /** GPU vertex motion for a VoxelRenderer, shared by beauty, surface capture and shadows. */
31
+ export declare class VoxelWind implements VoxelVertexMotion {
32
+ readonly renderer: VoxelRenderer;
33
+ private values;
34
+ private clock;
35
+ private speeds;
36
+ private base;
37
+ private speedsVersion;
38
+ private gpu?;
39
+ private readonly cache;
40
+ private count;
41
+ private paddingVersion;
42
+ private radius;
43
+ private dirty;
44
+ private disposed;
45
+ private readonly off;
46
+ private readonly offDispose;
47
+ constructor(renderer: VoxelRenderer, options?: VoxelWindOptions);
48
+ /** Live XZ wind; a copy is returned. */
49
+ get direction(): readonly [number, number];
50
+ set direction(value: readonly [number, number]);
51
+ /** Explicit clock, or undefined for the renderer clock. */
52
+ get time(): number | undefined;
53
+ set time(value: number | undefined);
54
+ private check;
55
+ /** Conservative bounds padding. */
56
+ get padding(): number;
57
+ /** Shader definition version; adding a block type refreshes material ids. */
58
+ get version(): number;
59
+ /** Renderer adapter: creates/restores one owned uniform buffer and uploads live direction/time. */
60
+ bindings(device: GPUDevice, time?: number, base?: readonly number[]): GPUBindGroupEntry[];
61
+ private speedTable;
62
+ /** Renderer adapter: composes motion with the existing voxel shader without changing static worlds. */
63
+ shader(code: string): string;
64
+ /** Removes motion before releasing its GPU uniform buffer. Block metadata and fine tessellation remain. */
65
+ dispose(): void;
66
+ /** Removes and releases the wind feature. */
67
+ [Symbol.dispose](): void;
68
+ }
package/dist/wind.js ADDED
@@ -0,0 +1,170 @@
1
+ import { MargoError as e } from "@margoengine/math/errors";
2
+ //#region src/wind.ts
3
+ var t = (e, t, n) => {
4
+ if (!Number.isFinite(e) || e < 0 || e > n) throw RangeError(`Voxel wind ${t} must be finite in 0..${n}; correct the option`);
5
+ return e;
6
+ };
7
+ function n(e, n, r = {}, i = {}) {
8
+ let a = i.kind ?? (r.shape === "liquid" ? "liquid" : "foliage");
9
+ if (a !== "foliage" && a !== "liquid") throw TypeError("Wind kind must be foliage or liquid; select one of those");
10
+ if (a === "liquid" && r.shape !== "liquid") throw TypeError("Liquid wind motion needs a liquid shape; pass {shape: \"liquid\"} or choose foliage motion");
11
+ if (r.shape === "model" || r.shape === "object") throw TypeError("Wind blocks use cube, cross, liquid, slab or stair shapes; use those shapes");
12
+ let o = i.rooted ?? r.shape === "cross";
13
+ if (typeof o != "boolean") throw TypeError("Wind rooted must be true or false; pass a boolean");
14
+ let s = {
15
+ kind: a,
16
+ rooted: o,
17
+ amplitude: t(i.amplitude ?? (a === "liquid" ? .04 : .12), "amplitude", .5),
18
+ speed: t(i.speed ?? 1.6, "speed", 100),
19
+ frequency: t(i.frequency ?? 1.3, "frequency", 100)
20
+ };
21
+ return e.define(n, {
22
+ ...r,
23
+ vertexMotion: s
24
+ });
25
+ }
26
+ function r(e, t, n, r, i = [1, 0], a = !0) {
27
+ if (!t.every(Number.isFinite) || !Number.isFinite(n) || !Number.isFinite(r) || i.length !== 2 || !i.every(Number.isFinite)) throw RangeError("Wind sample needs finite position, floor, time and XZ direction; pass finite numbers");
28
+ let o = Math.hypot(...i), s = Math.min(1, o), c = o ? i[0] / o : 0, l = o ? i[1] / o : 0, u = (t[0] * c + t[2] * l) * e.frequency + r * e.speed;
29
+ if (!s || !e.amplitude) return [
30
+ 0,
31
+ 0,
32
+ 0
33
+ ];
34
+ if (e.kind === "liquid") return [
35
+ 0,
36
+ a ? e.amplitude * s * (Math.sin(u) + .5 * Math.sin(1.7 * u + 1.2)) / 1.5 : 0,
37
+ 0
38
+ ];
39
+ let d = Math.max(0, Math.min(1, t[1] - n)), f = e.rooted ? d * d * (3 - 2 * d) : 1, p = e.amplitude * s * f * (.75 * Math.sin(u) + .25 * Math.sin(1.91 * u + .73));
40
+ return [
41
+ c * p || 0,
42
+ 0,
43
+ l * p || 0
44
+ ];
45
+ }
46
+ var i = class {
47
+ constructor(e, t = {}) {
48
+ this.renderer = e, this.values = new Float32Array([
49
+ 1,
50
+ 0,
51
+ 0,
52
+ 0,
53
+ 0,
54
+ 0,
55
+ 0,
56
+ 0
57
+ ]), this.speeds = [], this.base = [
58
+ 0,
59
+ 0,
60
+ 0
61
+ ], this.speedsVersion = -1, this.cache = /* @__PURE__ */ new Map(), this.count = -1, this.paddingVersion = -1, this.radius = 0, this.dirty = !0, this.disposed = !1, this.direction = t.direction ?? [1, 0], this.time = t.time, this.off = e.addVertexMotion(this), this.offDispose = e.onDispose(() => this.dispose());
62
+ }
63
+ get direction() {
64
+ return [this.values[0], this.values[1]];
65
+ }
66
+ set direction(e) {
67
+ if (this.check(), e?.length !== 2 || !e.every((e) => Number.isFinite(e) && Math.abs(e) <= 1e4)) throw RangeError("Wind direction must be two finite XZ components within +/-10000; pass [x,z]");
68
+ this.values[0] = e[0], this.values[1] = e[1], this.dirty = !0;
69
+ }
70
+ get time() {
71
+ return this.clock;
72
+ }
73
+ set time(e) {
74
+ if (this.check(), e !== void 0 && (!Number.isFinite(e) || Math.abs(e) > 1e6)) throw RangeError("Wind time must be finite within +/-1e6 seconds; pass time or undefined for renderer time");
75
+ this.clock = e, this.dirty = !0;
76
+ }
77
+ check() {
78
+ if (this.disposed) throw new e("disposed", "VoxelWind is disposed; create another VoxelWind");
79
+ }
80
+ get padding() {
81
+ return this.paddingVersion !== this.version && (this.paddingVersion = this.version, this.radius = this.renderer.world.blocks.table.reduce((e, t) => Math.max(e, t.vertexMotion?.amplitude ?? 0), 0)), this.radius;
82
+ }
83
+ get version() {
84
+ return this.renderer.world.blocks.table.length;
85
+ }
86
+ bindings(e, t, n) {
87
+ this.check(), n && (this.base = n);
88
+ let r = this.speedTable();
89
+ if (this.values.length !== 4 + 4 * Math.max(1, Math.ceil(r.length / 4))) {
90
+ let e = new Float32Array(4 + 4 * Math.max(1, Math.ceil(r.length / 4)));
91
+ e.set(this.values.subarray(0, 2)), this.values = e;
92
+ }
93
+ if (this.gpu?.device !== e || this.gpu.size !== this.values.byteLength) {
94
+ this.gpu?.buffer.destroy();
95
+ let t = this.values.byteLength;
96
+ this.gpu = {
97
+ device: e,
98
+ buffer: e.createBuffer({
99
+ size: t,
100
+ usage: 72
101
+ }),
102
+ size: t
103
+ }, this.dirty = !0;
104
+ }
105
+ let i = this.clock ?? t;
106
+ if (i !== void 0) {
107
+ let [e, , t] = this.base, n = Math.max(Math.fround(Math.hypot(this.values[0], this.values[1])), Math.fround(1e-6)), a = Math.fround(this.values[0] / n), o = Math.fround(this.values[1] / n);
108
+ for (let n = 0; n < r.length; n++) {
109
+ let [s, c] = r[n], l = i * s + (e * a + t * o) * Math.fround(c), u = Math.fround(l - Math.floor(l / (2 * Math.PI)) * 2 * Math.PI);
110
+ this.values[4 + n] !== u && (this.values[4 + n] = u, this.dirty = !0);
111
+ }
112
+ }
113
+ return this.dirty &&= (e.queue.writeBuffer(this.gpu.buffer, 0, this.values), !1), [{
114
+ binding: 11,
115
+ resource: { buffer: this.gpu.buffer }
116
+ }];
117
+ }
118
+ speedTable() {
119
+ if (this.speedsVersion !== this.version) {
120
+ this.speedsVersion = this.version, this.speeds = [];
121
+ for (let e of this.renderer.world.blocks.table) {
122
+ let t = e.layer === "invisible" ? void 0 : e.vertexMotion;
123
+ t && !this.speeds.some(([e, n]) => e === t.speed && n === t.frequency) && this.speeds.push([t.speed, t.frequency]);
124
+ }
125
+ }
126
+ return this.speeds;
127
+ }
128
+ shader(e) {
129
+ this.check(), this.count !== this.version && (this.count = this.version, this.cache.clear());
130
+ let t = this.cache.get(e);
131
+ if (t) return t;
132
+ let n = e;
133
+ if (e.includes("fn voxelPlace(")) {
134
+ if (n = n.replaceAll(/voxelPlace\(w0,w1,(c(?:\^1u)?|3u-c),t\)/g, (e, t) => `windPlace(w0,w1,${t},t,voxelOrigins[voxelPages[q>>6u]].xyz)`), e.includes("struct Out {@builtin(position) clip:vec4f,@location(0) @interpolate(flat) words:vec3u")) {
135
+ n = n.replace("@location(3) @interpolate(flat) maps:vec4u}", "@location(3) @interpolate(flat) maps:vec4u,@location(4) windBase:vec3f,@location(5) windWorld:vec3f}"), n = n.replace("var o:Out;o.clip=clip;", "var o:Out;o.windBase=origin.xyz+windBase(w0,w1,c,t);o.windWorld=origin.xyz+windPlace(w0,w1,c,t,origin.xyz);o.clip=clip;"), n = n.replace("i.position=voxelHit(o.clip.xy,n,k);let l=i.position-O;", "i.position=select(voxelHit(o.clip.xy,n,k),o.windWorld,windInfo(w.y&4095u).x>0.0);let l=select(i.position,o.windBase,windInfo(w.y&4095u).x>0.0)-O;"), n = n.replace("let dx=voxelTile(face,voxelHit(o.clip.xy+vec2f(1.0,0.0),n,k)-O,cell)-uv;let dy=voxelTile(face,voxelHit(o.clip.xy+vec2f(0.0,1.0),n,k)-O,cell)-uv;", "let dx=select(voxelTile(face,voxelHit(o.clip.xy+vec2f(1.0,0.0),n,k)-O,cell)-uv,dpdx(uv),windInfo(w.y&4095u).x>0.0);let dy=select(voxelTile(face,voxelHit(o.clip.xy+vec2f(0.0,1.0),n,k)-O,cell)-uv,dpdy(uv),windInfo(w.y&4095u).x>0.0);"), n = n.replace("let side=select(-1.0,1.0,front);", "let windN=cross(dpdx(o.windWorld),dpdy(o.windWorld));let side=select(-1.0,1.0,front);");
136
+ let e = "if(face==2u&&(w.y&0x100000u)!=0u){normal=voxelRipple(i.position.xz,f.waves)*side;}";
137
+ n = n.replace(e, e + "\n if(windInfo(w.y&4095u).x>0.0&&dot(windN,windN)>0.0){normal=normalize(windN)*select(-1.0,1.0,dot(windN,normal)>=0.0);}\n");
138
+ }
139
+ n += "\nfn windBase(w0:u32,w1:u32,c:u32,t:vec4f)->vec3f {var p=voxelPlace(w0,w1,c,t);let face=(w0>>15u)&7u;if(windInfo(w1&4095u).x==2.0&&(face==2u||(face!=3u&&face<6u&&c>=2u))){p.y-=voxelRise(w1,t);}return p;}\nfn windPlace(w0:u32,w1:u32,c:u32,t:vec4f,O:vec3f)->vec3f {let p=windBase(w0,w1,c,t);let face=(w0>>15u)&7u;let surface=(w1&0x100000u)!=0u&&(face==2u||(face!=3u&&face<6u&&c>=2u));return p+windOffset(O+p,O.y+f32((w0>>5u)&31u),w1&4095u,surface);}\n";
140
+ }
141
+ e.includes("o.clip=f.camera*vec4f(p,1.0)") && (n = n.replace("o.clip=f.camera*vec4f(p,1.0)", "o.clip=f.camera*vec4f(p+windOffset(p,floor(p.y),v.material&4095u,true),1.0)").replace("o.world=p;", "o.world=p+windOffset(p,floor(p.y),v.material&4095u,true);")), e.includes("shadow.matrix*vec4f(p,1.0)") && (n = n.replace("shadow.matrix*vec4f(p,1.0)", "shadow.matrix*vec4f(p+windOffset(p,floor(p.y),material&4095u,true),1.0)"));
142
+ let r = this.renderer.world.blocks.table, i = 0, a = "", o = "";
143
+ for (let e of r) if (e.layer !== "invisible") {
144
+ for (let t = 0; t < 6; t++) if (e.textures.indexOf(e.textures[t]) === t) {
145
+ let t = e.vertexMotion;
146
+ t && (a += `case ${i}u:{return vec4f(${t.kind === "liquid" ? "2.0" : "1.0"},${t.amplitude},${t.frequency},${this.speedTable().findIndex(([e, n]) => e === t.speed && n === t.frequency)});}\n`, t.rooted && (o += `case ${i}u:{return true;}\n`)), i++;
147
+ }
148
+ }
149
+ return n = `
150
+ struct WindFrame {data:vec4f,phase:array<vec4f,${Math.max(1, Math.ceil(this.speedTable().length / 4))}>}
151
+ @group(0) @binding(11) var<uniform> windFrame:WindFrame;
152
+ fn windInfo(m:u32)->vec4f {switch m {${a} default:{return vec4f(0.0);}}}
153
+ fn windRoot(m:u32)->bool {switch m {${o} default:{return false;}}}
154
+ fn windOffset(p:vec3f,floorY:f32,material:u32,surface:bool)->vec3f {
155
+ let config=windInfo(material);let magnitude=length(windFrame.data.xy);let direction=windFrame.data.xy/max(magnitude,0.000001);let strength=min(magnitude,1.0);
156
+ let slot=u32(config.w);let phase=dot(p.xz,direction)*config.z+windFrame.phase[slot>>2u][slot&3u];
157
+ if(config.x==2.0){return vec3f(0.0,select(0.0,config.y*strength*(sin(phase)+0.5*sin(1.7*phase+1.2))/1.5,surface),0.0);}
158
+ let h=clamp(p.y-floorY,0.0,1.0);let weight=select(1.0,h*h*(3.0-2.0*h),windRoot(material));let bend=config.y*strength*weight*(0.75*sin(phase)+0.25*sin(1.91*phase+0.73));return vec3f(direction.x*bend,0.0,direction.y*bend);
159
+ }
160
+ ` + n, this.cache.set(e, n), n;
161
+ }
162
+ dispose() {
163
+ this.disposed || (this.disposed = !0, this.off(), this.offDispose(), this.gpu?.buffer.destroy(), this.gpu = void 0, this.cache.clear());
164
+ }
165
+ [Symbol.dispose]() {
166
+ this.dispose();
167
+ }
168
+ };
169
+ //#endregion
170
+ export { i as VoxelWind, n as defineWindBlock, r as sampleVoxelWind };
@@ -0,0 +1,44 @@
1
+ //#region ../shared/worker.js
2
+ function e(e, t = /* @__PURE__ */ new Set(), n = /* @__PURE__ */ new Set()) {
3
+ let r = [e];
4
+ for (; r.length;) {
5
+ let e = r.pop();
6
+ if (e && typeof e == "object" && !n.has(e)) {
7
+ if (n.add(e), ArrayBuffer.isView(e)) e.buffer instanceof ArrayBuffer && t.add(e.buffer);
8
+ else if (e instanceof ArrayBuffer) t.add(e);
9
+ else if (typeof ImageBitmap < "u" && e instanceof ImageBitmap) t.add(e);
10
+ else if (e instanceof Map) for (let [t, n] of e) r.push(t, n);
11
+ else for (let t in e) r.push(e[t]);
12
+ }
13
+ }
14
+ return t;
15
+ }
16
+ var t = {
17
+ TypeError,
18
+ RangeError,
19
+ SyntaxError
20
+ }, n = ({ name: e, message: n }) => new (Object.hasOwn(t, e) ? t[e] : Error)(typeof n == "string" ? n : "Parser worker failed");
21
+ function r(t) {
22
+ self.onmessage = async ({ data: n }) => {
23
+ try {
24
+ if (!n || typeof n != "object") throw TypeError("Invalid parser worker job; post an object");
25
+ let r = await t(n);
26
+ self.postMessage({ result: r }, [...e(r)]);
27
+ } catch (e) {
28
+ let t = {
29
+ name: e?.name ?? "Error",
30
+ message: e instanceof Error ? e.message : String(e)
31
+ };
32
+ try {
33
+ self.postMessage({ error: {
34
+ ...e,
35
+ ...t
36
+ } });
37
+ } catch {
38
+ self.postMessage({ error: t });
39
+ }
40
+ }
41
+ };
42
+ }
43
+ //#endregion
44
+ export { r as n, e as r, n as t };
@@ -0,0 +1,93 @@
1
+ import { r as e, t } from "./worker-DlGRxlc4.js";
2
+ //#region ../shared/errors.js
3
+ var n = (e, t) => Object.assign(Error(t), {
4
+ name: "MargoError",
5
+ code: e
6
+ }), r = (e, t) => `${e}; see https://margoengine.com/errors#${t}`, i = (e, t, i = e) => n(e, r(t, i)), a = class {
7
+ #e;
8
+ #t;
9
+ #n = /* @__PURE__ */ new Set();
10
+ #r = [];
11
+ #i = [];
12
+ #a = !1;
13
+ constructor(e, t) {
14
+ if (!Number.isInteger(t) || t < 1) throw RangeError(`Worker pool size is ${t}; pass a whole number of 1 or more`);
15
+ this.#e = e, this.#t = t;
16
+ }
17
+ run(e, { priority: t = 0, timeout: r = Infinity, signal: i } = {}) {
18
+ return this.#a ? Promise.reject(n("disposed", "Worker pool is disposed; create a new pool")) : i?.aborted ? Promise.reject(i.reason) : new Promise((n, a) => {
19
+ let o = {
20
+ message: e,
21
+ priority: t,
22
+ timeout: r,
23
+ signal: i,
24
+ resolve: n,
25
+ reject: a,
26
+ done: !1
27
+ };
28
+ o.abort = () => {
29
+ let e = this.#i.indexOf(o);
30
+ e >= 0 && this.#i.splice(e, 1), this.#o(o, a, i.reason);
31
+ }, i?.addEventListener("abort", o.abort, { once: !0 });
32
+ let s = 0, c = this.#i.length;
33
+ for (; s < c;) {
34
+ let e = s + c >> 1;
35
+ this.#i[e].priority > t ? s = e + 1 : c = e;
36
+ }
37
+ this.#i.splice(s, 0, o), this.#s();
38
+ });
39
+ }
40
+ dispose() {
41
+ if (this.#a) return;
42
+ this.#a = !0;
43
+ let e = n("disposed", "Worker pool is disposed; create a new pool");
44
+ for (let t of this.#n) this.#l(t, e);
45
+ for (let t of this.#i.splice(0)) this.#o(t, t.reject, e);
46
+ }
47
+ #o(e, t, n) {
48
+ e.done || (e.done = !0, e.signal?.removeEventListener("abort", e.abort), t(n));
49
+ }
50
+ #s() {
51
+ for (; this.#i.length && (this.#r.length || this.#n.size < this.#t);) {
52
+ let t = this.#i.pop(), r;
53
+ try {
54
+ r = typeof t.message == "function" ? t.message() : t.message;
55
+ } catch (e) {
56
+ this.#o(t, t.reject, e);
57
+ continue;
58
+ }
59
+ if (r === void 0) {
60
+ this.#o(t, t.resolve, void 0);
61
+ continue;
62
+ }
63
+ let i = this.#r.pop() ?? this.#c();
64
+ try {
65
+ i.worker.postMessage(r, [...e(r)]);
66
+ } catch (e) {
67
+ this.#r.push(i), this.#o(t, t.reject, e);
68
+ continue;
69
+ }
70
+ i.job = t, Number.isFinite(t.timeout) && (i.timer = setTimeout(() => this.#l(i, n("failed", `Worker job timed out after ${t.timeout} ms; the worker was restarted`)), t.timeout));
71
+ }
72
+ }
73
+ #c() {
74
+ let e = {
75
+ worker: this.#e(),
76
+ job: void 0,
77
+ timer: void 0
78
+ };
79
+ return this.#n.add(e), e.worker.onmessage = ({ data: n }) => {
80
+ let r = e.job;
81
+ r && (clearTimeout(e.timer), e.job = void 0, this.#r.push(e), !n || typeof n != "object" ? this.#o(r, r.reject, /* @__PURE__ */ TypeError("Worker sent an invalid reply")) : n.error ? this.#o(r, r.reject, t(n.error)) : this.#o(r, r.resolve, n.result), this.#s());
82
+ }, e.worker.onerror = (t) => {
83
+ t.preventDefault?.(), this.#l(e, n("failed", `Worker failed: ${t.message || "unknown error"}; it was restarted`));
84
+ }, e.worker.onmessageerror = () => this.#l(e, n("failed", "Worker reply could not be deserialized; the worker was restarted")), e;
85
+ }
86
+ #l(e, t) {
87
+ clearTimeout(e.timer), e.worker.onmessage = e.worker.onerror = e.worker.onmessageerror = null, e.worker.terminate(), this.#n.delete(e);
88
+ let n = this.#r.indexOf(e);
89
+ n >= 0 && this.#r.splice(n, 1), e.job && this.#o(e.job, e.job.reject, t), e.job = void 0, this.#a || this.#s();
90
+ }
91
+ };
92
+ //#endregion
93
+ export { i as n, n as r, a as t };
@@ -0,0 +1 @@
1
+ (function(){function e(e,t=new Set,n=new Set){let r=[e];for(;r.length;){let e=r.pop();if(e&&typeof e==`object`&&!n.has(e)){if(n.add(e),ArrayBuffer.isView(e))e.buffer instanceof ArrayBuffer&&t.add(e.buffer);else if(e instanceof ArrayBuffer)t.add(e);else if(typeof ImageBitmap<`u`&&e instanceof ImageBitmap)t.add(e);else if(e instanceof Map)for(let[t,n]of e)r.push(t,n);else for(let t in e)r.push(e[t])}}return t}function t(t){self.onmessage=async({data:n})=>{try{if(!n||typeof n!=`object`)throw TypeError(`Invalid parser worker job; post an object`);let r=await t(n);self.postMessage({result:r},[...e(r)])}catch(e){let t={name:e?.name??`Error`,message:e instanceof Error?e.message:String(e)};try{self.postMessage({error:{...e,...t}})}catch{self.postMessage({error:t})}}}}var n=e=>e<=.04045?e/12.92:((e+.055)/1.055)**2.4,r=e=>e<0?-n(-e):n(e);function i(e){let[t,n,r,i,a,o,s,c,l]=e,u=a*l-o*c,d=o*s-i*l,f=i*c-a*s,p=t*u+n*d+r*f;return[u,r*c-n*l,n*o-r*a,d,t*l-r*s,r*i-t*o,f,n*s-t*c,t*a-n*i].map(e=>e/p)}var a=(e,t)=>e.map((n,r)=>{let i=r-r%3,a=r%3;return e[i]*t[a]+e[i+1]*t[3+a]+e[i+2]*t[6+a]});function o(e,t,n,r){let[a,o,s,c]=[e,t,n,r].map(e=>[e[0]/e[1],1,(1-e[0]-e[1])/e[1]]),l=[a[0],o[0],s[0],a[1],o[1],s[1],a[2],o[2],s[2]],u=i(l),d=[0,1,2].map(e=>u[e*3]*c[0]+u[e*3+1]*c[1]+u[e*3+2]*c[2]);return l.map((e,t)=>e*d[t%3])}var s=[.3127,.329],c=Object.freeze(a(i(o([.64,.33],[.3,.6],[.15,.06],s)),o([.68,.32],[.265,.69],[.15,.06],s)));function l(e,t,n,r){let i=t[0],a=t[1],o=t[2];n[r]=e[0]*i+e[1]*a+e[2]*o,n[r+1]=e[3]*i+e[4]*a+e[5]*o,n[r+2]=e[6]*i+e[7]*a+e[8]*o}function u(e,t,n,i,a,o){let s=i===`srgb`||i===`display-p3`?[r(e),r(t),r(n)]:[e,t,n];i===`display-p3`||i===`display-p3-linear`?l(c,s,a,o):(a[o]=s[0],a[o+1]=s[1],a[o+2]=s[2])}var d=[`srgb`,`srgb-linear`,`display-p3`,`display-p3-linear`];function f(e){let t=/^color\(\s*([a-z0-9-]+)\s+([^)]*)\)$/i.exec(e.trim());if(!t)return null;let n=t[1].toLowerCase(),r=t[2].split(`/`)[0].trim().split(/[\s,]+/);if(!d.includes(n))throw TypeError(`Color space '${t[1]}' is unknown; use srgb, srgb-linear, display-p3 or display-p3-linear`);let i=r.map(e=>e.endsWith(`%`)?parseFloat(e)/100:Number(e));if(i.length!==3||!i.every(Number.isFinite))throw TypeError(`Color '${e}' needs three numbers: color(${n} r g b)`);return{rgb:i,space:n}}function p(e){let t=/^#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.exec(e)?.[1];return t?(t.length<4?t.replace(/./g,`$&$&`):t).padEnd(8,`f`).match(/../g).map(e=>parseInt(e,16)/255):void 0}function m(e,t=[0,0,0],r=0){let i;if(typeof e==`number`){if(!Number.isInteger(e)||e<0||e>16777215)throw RangeError(`Numeric colors must be 0x000000..0xffffff`);i=[e>>16&255,e>>8&255,e&255].map(e=>n(e/255))}else if(typeof e==`string`&&/^\s*color\(/i.test(e)){let{rgb:t,space:n}=f(e);i=[0,0,0],u(t[0],t[1],t[2],n,i,0)}else if(typeof e==`string`){let t=p(e);if(!t)throw TypeError(`Color strings must be #rrggbb or #rgb`);i=t.slice(0,3).map(n)}else if(e&&typeof e==`object`&&e.length===3){if(i=[e[0],e[1],e[2]],i.some(e=>typeof e!=`number`||!Number.isFinite(e)||e<0))throw RangeError(`Linear RGB colors must be finite and non-negative`)}else throw TypeError(`Expected a color`);return t[r]=i[0],t[r+1]=i[1],t[r+2]=i[2],t}var h=class extends Error{constructor(e,t){super(t),this.name=`MargoError`,this.code=e}static[Symbol.hasInstance](e){return e instanceof Error&&e.name===`MargoError`}},g=Uint8Array.from({length:16},(e,t)=>t&8?255:t&4?204:51),_=Uint8Array.from({length:64},(e,t)=>{let n=t&2?2:0,r=2-n,i=t&1?0:1,a=t&4?1:0,o=t>>3&3,s=t>>5&1,c=0;for(let e=0;e<8;e++){let t=e>>n&1,l=e>>r&1;((e>>1&1)===a||(o===2?t===i&&l===s:o===1?t===i||l===s:t===i))&&(c|=1<<e)}return c}),v=e=>g[e&15],y=e=>_[e&63],ee=[`opaque`,`cutout`,`translucent`,`invisible`],te=[`cube`,`cross`,`liquid`,`slab`,`stair`,`model`,`object`],b=(e,t,n,r)=>typeof t==`number`?RangeError(`${e} ${t} is not an integer in 0..${n}; ${r}`):TypeError(`${e} must be a number; ${r}`),x=(e,t,n,r=1/0)=>{if(e===void 0)return n;let i=r<1/0?`0..${r}`:`a finite number >= 0`;if(typeof e!=`number`)throw TypeError(`Block type ${t} must be a number; pass ${i}`);if(!(e>=0&&e<=r&&Number.isFinite(e)))throw RangeError(`Block type ${t} ${e} is out of range; pass ${i}`);return e},S=(e,t,n)=>{if(e===void 0)return n;if((e&15)!==e)throw b(`Block type ${t}`,e,15,`pass a light level`);return e},C=(e,t,n)=>{if(e===void 0)return n;if(typeof e!=`boolean`)throw TypeError(`Block type ${t} must be true or false`);return e},w=e=>{if(e===void 0)return 0;if(!Number.isInteger(e)||e<0||e>2047)throw b(`Block type map layer`,e,2047,`pass a texture layer`);return e},T=(e,t,n)=>{if(e===void 0)return n[0];if(!n.includes(e))throw TypeError(`Block type ${t} must be ${n.map(e=>`'${e}'`).join(`, `)}`);return e},E=class{constructor(){this.types=[],this.names=new Map,this.define(`air`,{layer:`invisible`,solid:!1})}get table(){return this.types}define(e,t={}){if(typeof e!=`string`)throw TypeError(`Block type name must be a string such as "stone"`);if(!e)throw RangeError(`Block type name is empty; pass a name such as "stone"`);if(this.names.has(e))throw new h(`state`,`Block type "${e}" is already defined; use blocks.id("${e}") or a new name`);if(this.types.length>65535)throw RangeError(`Block types are full (65,536 ids); reuse a type with data`);if(!t||typeof t!=`object`)throw TypeError(`Block type options must be an object`);let n=t,r=T(n.layer,`layer`,ee),i=T(n.shape,`shape`,te),a=n.animation??{frames:1,fps:0};if(i===`model`!=!!n.model?.length)throw TypeError(`Model shape needs prepared boxes; use defineBlockModel from @margoengine/voxel/models`);if(!a||typeof a!=`object`)throw TypeError(`Block type animation must be {frames, fps}`);let o=a.frames;if(!Number.isInteger(o)||o<1||o>2048)throw b(`Block type animation.frames`,o,2048,`pass 1..2048 texture layers`);let s=n.textures??0,c=typeof s==`number`?[s,s,s,s,s,s]:s;if(!Array.isArray(c)||c.length!==6)throw TypeError(`Block type textures must be a layer or 6 layers (+x, -x, +y, -y, +z, -z)`);for(let e of c)if(!Number.isInteger(e)||e<0||e+o>2048)throw b(`Block type texture layer`,e,2048-o,`${o} frame(s) must fit 2048 layers`);let l=[1,1,1];n.color!==void 0&&m(n.color,l);let u=this.types.length;return this.types.push({id:u,name:e,textures:[...c],layer:r,shape:i,...n.model?{model:n.model}:{},...n.vertexMotion?{vertexMotion:{...n.vertexMotion}}:{},solid:C(n.solid,`solid`,!0),liquid:C(n.liquid,`liquid`,!1),light:S(n.light,`light`,0),opacity:S(n.opacity,`opacity`,r===`opaque`&&i===`cube`?15:0),color:l,roughness:x(n.roughness,`roughness`,1,1),metalness:x(n.metalness,`metalness`,0,1),emissive:x(n.emissive,`emissive`,0),animation:{frames:o,fps:x(a.fps,`animation.fps`,0)},maps:{normal:w(n.maps?.normal),roughness:w(n.maps?.roughness),emissive:w(n.maps?.emissive)},transmission:x(n.transmission,`transmission`,0,1),scatter:x(n.scatter,`scatter`,0,1),waterlog:this.waterlogOf(n.waterlog,i)}),this.names.set(e,u),u}waterlogOf(e,t){if(e===void 0)return 0;if(t!==`slab`&&t!==`stair`)throw TypeError(`Block type waterlog applies to slabs and stairs, not a '${t}'; drop it or change shape`);if(typeof e!=`string`&&typeof e!=`number`)throw TypeError(`Block type waterlog must be a liquid's id or name, not ${typeof e}`);let n=typeof e==`string`?this.names.get(e)??-1:e,r=Number.isInteger(n)?this.types[n]:void 0;if(!r||r.shape!==`liquid`)throw RangeError(`Block type waterlog ${e} is not a defined 'liquid' type; define the liquid first`);return n}id(e){let t=this.names.get(e);if(t===void 0)throw RangeError(`Block type "${e}" is not defined; define it with blocks.define()`);return t}get(e){let t=Number.isInteger(e)?this.types[e]:void 0;if(!t)throw RangeError(`Block id ${e} is not defined; define it with blocks.define()`);return t}},D=32768,O=e=>e<=1?0:e<=2?1:e<=4?2:e<=16?4:e<=256?8:16,k=e=>e?Math.min(1<<e,D):1,A=(e,t,n)=>[e,t,n].every(e=>typeof e==`number`)?RangeError(`Chunk cell (${e}, ${t}, ${n}) is not in 0..31; pass local coordinates (cell & 31)`):TypeError(`Chunk cell coordinates must be numbers`),j=e=>b(`Block state`,e,4294967295,`build it with blockState(id, data)`),M=e=>RangeError(`PackedChunk ${e}; pass the result of chunk.pack()`);function N(e,t,n,r){if(!n)return null;let i=new Uint32Array(1024*n),a=(1<<e)-1;if((!e||!t)&&!r?.[0])return i;for(let o=0;o<D;o++){let s=e&&t?t[o*e>>>5]>>>(o*e&31)&a:0;r&&(s=r[s]),s&&(i[o*n>>>5]|=s<<(o*n&31))}return i}var P,F,I=class e{static{P=e=>e.palette.subarray(0,e.size),F=e=>e.lightCells?e.lightCells.slice():e.lightByte}constructor(e=0){this.palette=new Uint32Array(1),this.counts=Uint16Array.of(D),this.size=1,this.live=1,this.free=[],this.lookup=null,this.bits=0,this.words=null,this.lightByte=0,this.lightRevision=0,this.lightCells=null,this.fill(e)}get(e,t,n){if((e&31)!==e||(t&31)!==t||(n&31)!==n)throw A(e,t,n);return this.palette[this.slot(e|n<<5|t<<10)]}set(e,t,n,r){if((e&31)!==e||(t&31)!==t||(n&31)!==n)throw A(e,t,n);if(r>>>0!==r)throw j(r);let i=e|n<<5|t<<10,a=this.slot(i),o=this.palette[a];if(o===r)return o;let s=this.find(r);if(s<0&&this.counts[a]===1)return this.lookup?.delete(o),this.palette[a]=r,this.lookup?.set(r,a),o;s<0&&(s=this.add(r));let c=this.bits,l=i*c,u=l>>>5,d=l&31,f=this.words;return f[u]=f[u]&~((1<<c)-1<<d)|s<<d,this.counts[s]++,--this.counts[a]===0&&this.release(a),o}fill(e){if(e>>>0!==e)throw j(e);this.palette=Uint32Array.of(e),this.counts=Uint16Array.of(D),this.size=this.live=1,this.free=[],this.lookup=null,this.bits=0,this.words=null}getLight(e,t,n){if((e&31)!==e||(t&31)!==t||(n&31)!==n)throw A(e,t,n);return this.lightCells?this.lightCells[e|n<<5|t<<10]:this.lightByte}setLight(e,t,n,r){if((e&31)!==e||(t&31)!==t||(n&31)!==n)throw A(e,t,n);if((r&255)!==r)throw b(`Light byte`,r,255,`pass sky << 4 | block`);if(this.getLight(e,t,n)!==r){if(this.lightRevision++,!this.lightCells){if(r===this.lightByte)return;this.lightCells=new Uint8Array(D).fill(this.lightByte)}this.lightCells[e|n<<5|t<<10]=r}}fillLight(e){if((e&255)!==e)throw b(`Light byte`,e,255,`pass sky << 4 | block`);(this.lightCells||this.lightByte!==e)&&this.lightRevision++,this.lightByte=e,this.lightCells=null}get byteLength(){return this.palette.byteLength+this.counts.byteLength+(this.words?.byteLength??0)+(this.lightCells?.byteLength??0)}pack(){return{palette:this.palette.slice(0,this.size),bits:this.bits,words:this.words?this.words.slice():new Uint32Array,light:this.lightCells?this.lightCells.slice():this.lightByte}}static unpack(t){if(!t||typeof t!=`object`)throw TypeError(`PackedChunk must be the object chunk.pack() returns`);let{palette:n,bits:r,words:i,light:a}=t;if(!(n instanceof Uint32Array)||!(i instanceof Uint32Array))throw TypeError(`PackedChunk palette and words must be Uint32Arrays; pass the result of chunk.pack()`);if(![0,1,2,4,8,16].includes(r))throw M(`bits ${r} is not 0, 1, 2, 4, 8 or 16`);let o=k(r);if(n.length<1||n.length>o)throw M(`palette has ${n.length} entries where ${r}-bit indices allow 1..${o}`);if(i.length!==1024*r)throw M(`words has ${i.length} entries, not ${1024*r}`);if(typeof a==`number`){if((a&255)!==a)throw M(`light byte ${a} is not in 0..255`)}else if(!(a instanceof Uint8Array))throw TypeError(`PackedChunk light must be a byte or a Uint8Array`);else if(a.length!==D)throw M(`light has ${a.length} bytes, not ${D}`);let s=new e,c=new Uint16Array(o),l=(1<<r)-1;if(!r)c[0]=D;else for(let e=0;e<D;e++){let t=i[e*r>>>5]>>>(e*r&31)&l;if(t>=n.length)throw M(`cell ${e} uses palette entry ${t} of ${n.length}`);c[t]++}s.palette=new Uint32Array(o),s.palette.set(n),s.counts=c,s.size=n.length,s.bits=r,s.words=r?i.slice():null,s.lightByte=typeof a==`number`?a:0,s.lightCells=typeof a==`number`?null:a.slice();let u=new Set,d=0;for(let e=0;e<n.length;e++)c[e]?(d++,u.add(n[e])):s.free.push(e);if(s.live=d,u.size<d||d===1||O(2*d)<r)s.compact();else if(o>16){s.lookup=new Map;for(let e=0;e<n.length;e++)c[e]&&s.lookup.set(n[e],e)}return s}slot(e){let t=this.bits,n=e*t;return t?this.words[n>>>5]>>>(n&31)&(1<<t)-1:0}find(e){if(this.lookup)return this.lookup.get(e)??-1;let{palette:t,counts:n,size:r}=this;for(let i=0;i<r;i++)if(t[i]===e&&n[i])return i;return-1}grow(e){let t=O(e),n=k(t),r=new Uint32Array(n),i=new Uint16Array(n);if(r.set(this.palette),i.set(this.counts),this.words=N(this.bits,this.words,t,null),this.palette=r,this.counts=i,this.bits=t,n>16&&!this.lookup){this.lookup=new Map;for(let e=0;e<this.size;e++)i[e]&&this.lookup.set(r[e],e)}}add(e){!this.free.length&&this.size===this.palette.length&&this.grow(this.size+1);let t=this.free.pop()??this.size++;return this.palette[t]=e,this.lookup?.set(e,t),this.live++,t}release(e){this.lookup?.delete(this.palette[e]),this.free.push(e),(--this.live===1||O(2*this.live)<this.bits)&&this.compact()}compact(){let{palette:e,counts:t,size:n}=this,r=new Uint16Array(n),i=new Map;for(let a=0;a<n;a++)if(t[a]){let t=i.get(e[a]);t===void 0&&i.set(e[a],t=i.size),r[a]=t}let a=i.size,o=a===1?0:Math.min(this.bits,O(2*a)),s=k(o);this.palette=new Uint32Array(s),this.counts=new Uint16Array(s);for(let[e,t]of i)this.palette[t]=e;for(let e=0;e<n;e++)t[e]&&(this.counts[r[e]]+=t[e]);this.words=N(this.bits,this.words,o,r),this.size=this.live=a,this.free=[],this.bits=o,this.lookup=s>16?i:null}},L=(e,t,n)=>`${e},${t},${n}`,R=(e,t,n)=>[e,t,n].every(e=>typeof e==`number`)?RangeError(`Cell (${e}, ${t}, ${n}) is not three 32-bit integers; floor positions to cells first`):TypeError(`Cell coordinates must be numbers`),z=(e,t,n)=>[e,t,n].every(e=>typeof e==`number`)?RangeError(`Chunk (${e}, ${t}, ${n}) is not three integers in -2^26..2^26-1; pass cell >> 5`):TypeError(`Chunk coordinates must be numbers`),B=e=>e<<5>>5===e;Array.from({length:27},(e,t)=>{let n=e=>e===1?[-1,0]:e===2?[0,1]:[0],r=0;for(let e of n(t/9|0))for(let i of n((t/3|0)%3))for(let a of n(t%3))r|=1<<(e+1)*9+(i+1)*3+a+1;return r});var V=class{constructor(e){if(this.dirty=new Set,this.chunks=new Map,this.columns=new Map,this.listeners=new Set,this.chunkListeners=new Set,this.cx=NaN,this.cy=0,this.cz=0,this.key=``,!(e?.blocks instanceof E))throw TypeError(`VoxelWorld needs {blocks}: pass {blocks: new BlockTypes()}`);let{minY:t=-32768,maxY:n=32767}=e;if(typeof t!=`number`||typeof n!=`number`)throw TypeError(`VoxelWorld minY and maxY must be numbers`);if(!Number.isInteger(t)||!Number.isInteger(n)||t<-32768||n>32767||t>n)throw RangeError(`VoxelWorld y range ${t}..${n} is invalid; use integers in -32768..32767, minY <= maxY`);this.blocks=e.blocks,this.minY=t,this.maxY=n}getBlock(e,t,n){if((e|0)!==e||(t|0)!==t||(n|0)!==n)throw R(e,t,n);if(t<this.minY||t>this.maxY)return 0;let r=this.at(e>>5,t>>5,n>>5);return r?r.get(e&31,t&31,n&31):0}setBlock(e,t,n,r){return this.check(e,t,n,r),this.write(e,t,n,r)}setBlocks(e){if(!e||typeof e!=`object`||!Number.isInteger(e.length)||e.length%4)throw TypeError(`setBlocks expects a flat [x, y, z, state, ...] array of quadruples`);let t=e.length,n=t=>(e[t]|0)===e[t]?e[t]>>>0:e[t];for(let r=0;r<t;r+=4)this.check(e[r],e[r+1],e[r+2],n(r+3));let r=0;for(let i=0;i<t;i+=4){let t=n(i+3);this.write(e[i],e[i+1],e[i+2],t)!==t&&r++}return r}getChunk(e,t,n){if(!B(e)||!B(t)||!B(n))throw z(e,t,n);return this.chunks.get(L(e,t,n))}addChunk(e,t,n,r=new I){if(!B(e)||!B(t)||!B(n))throw z(e,t,n);if(!(r instanceof I))throw TypeError(`addChunk expects a VoxelChunk; create one with new VoxelChunk() or VoxelChunk.unpack()`);if(t*32>this.maxY||t*32+31<this.minY)throw RangeError(`Chunk y ${t} is outside this world's cells ${this.minY}..${this.maxY}; widen minY/maxY`);let i=L(e,t,n);if(this.chunks.has(i))throw new h(`state`,`Chunk ${i} is already loaded; removeChunk(${i}) before adding another`);this.chunks.set(i,r),this.cx=NaN;let a=this.columns.get(`${e},${n}`);a?a.chunks++:this.columns.set(`${e},${n}`,{heights:new Int16Array(1024).fill(-32768),chunks:1}),this.dirty.add(i),this.touch(e,t,n,-1,1,-1,1,-1,1);for(let i of this.chunkListeners)i(e,t,n,r);return r}removeChunk(e,t,n){if(!B(e)||!B(t)||!B(n))throw z(e,t,n);let r=L(e,t,n),i=this.chunks.get(r);if(!i)return;this.chunks.delete(r),this.cx=NaN;let a=this.columns.get(`${e},${n}`);a&&!--a.chunks&&this.columns.delete(`${e},${n}`),this.dirty.add(r),this.touch(e,t,n,-1,1,-1,1,-1,1);for(let r of this.chunkListeners)r(e,t,n,void 0);return i}onChunk(e){if(typeof e!=`function`)throw TypeError(`onChunk expects a function (cx, cy, cz, chunk)`);for(let[t,n]of this.chunks){let[r,i,a]=t.split(`,`).map(Number);e(r,i,a,n)}return this.chunkListeners.add(e),()=>{this.chunkListeners.delete(e)}}heightmap(e,t){if(!B(e)||!B(t))throw z(e,0,t);return this.columns.get(`${e},${t}`)?.heights}onChange(e){if(typeof e!=`function`)throw TypeError(`onChange expects a function (x, y, z, state, previous)`);return this.listeners.add(e),()=>{this.listeners.delete(e)}}at(e,t,n){return(e!==this.cx||t!==this.cy||n!==this.cz)&&(this.cx=e,this.cy=t,this.cz=n,this.key=L(e,t,n),this.chunk=this.chunks.get(this.key)),this.chunk}check(e,t,n,r){if((e|0)!==e||(t|0)!==t||(n|0)!==n)throw R(e,t,n);if(t<this.minY||t>this.maxY)throw RangeError(`Cell y ${t} is outside this world's ${this.minY}..${this.maxY}; widen minY/maxY`);if(r>>>0!==r)throw j(r);if((r&65535)>=this.blocks.table.length)throw RangeError(`Block id ${r&65535} is not defined; define it with blocks.define() first`)}write(e,t,n,r,i=!0){let a=e>>5,o=t>>5,s=n>>5,c=e&31,l=t&31,u=n&31,d=this.at(a,o,s);if(!d){if(!r)return 0;d=this.addChunk(a,o,s),this.at(a,o,s)}let f=d.set(c,l,u,r);if(f===r)return f;if(this.dirty.add(this.key),this.touch(a,o,s,c?0:-1,+(c===31),l?0:-1,+(l===31),u?0:-1,+(u===31)),i)for(let i of this.listeners)i(e,t,n,r,f);return f}touch(e,t,n,r,i,a,o,s,c){for(let l=r;l<=i;l++)for(let r=a;r<=o;r++)for(let i=s;i<=c;i++){if(!(l|r|i))continue;let a=L(e+l,t+r,n+i);this.chunks.has(a)&&this.dirty.add(a)}}},H=-32768,U=8192,ne=(1<<27)-1,W=[0,10,5],G=1,re=2,K=4,q=256,ie=512,ae=4096,oe=Uint8Array.from({length:256},(e,t)=>{let n=0;for(let e=0;e<6;e++){let r=1<<(e>>1),i=e&1?0:r,a=!0;for(let e=0;e<8;e++)(e&r)===i&&!(t>>e&1)&&(a=!1);a&&(n|=1<<e)}return n}),J=new WeakSet,Y=e=>{if(typeof e!=`number`)throw TypeError(`VoxelLighting budget must be a number; pass cells per update`);if(!(e===1/0||Number.isInteger(e)&&e>=1))throw RangeError(`VoxelLighting budget ${e} is not an integer >= 1; pass cells per update or Infinity`);return e},se=e=>{let t=e&31,n=e>>10,r=e>>5&31;if(t&&t<31&&n&&n<31&&r&&r<31)return U;let i=e=>e?e===31?6:2:3,a=i(t),o=i(n),s=i(r),c=0;for(let e=0;e<3;e++)for(let t=0;t<3;t++)for(let n=0;n<3;n++)a>>e&o>>t&s>>n&1&&(c|=1<<e+3*t+9*n);return c},ce=class e{static attach(t,n={}){return new e(t,n)}constructor(e,t){if(this.flags=new Uint16Array(65536),this.parts=!1,this.wet=new Uint8Array(65536),this.types=0,this.disposed=!1,this.sections=new Map,this.columns=new Map,this.loads=[],this.loadHead=0,this.edits=[],this.editHead=0,this.addS=[],this.addI=[],this.addHead=0,this.cutS=[],this.cutE=[],this.cutHead=0,this.touched=[],this.j=0,!(e instanceof V))throw TypeError(`VoxelLighting.attach expects a VoxelWorld; pass new VoxelWorld({blocks})`);if(!t||typeof t!=`object`)throw TypeError(`VoxelLighting options must be an object`);if(this.limit=Y(t.budget??4096),J.has(e))throw new h(`attached`,`VoxelWorld already has a VoxelLighting; dispose() it before attaching another`);J.add(e),this.world=e,this.refresh(),this.offChunk=e.onChunk((e,t,n,r)=>{this.loads.push([e,t,n,r])}),this.offChange=e.onChange((t,n,r,i,a)=>{this.types!==e.blocks.table.length&&this.refresh();let o=this.flags[i&65535],s=this.flags[a&65535];if((o|s)&q&&i!==a)return void this.edits.push(t,n,r,7);if(o===s)return;let c=(o&15)-(s&15);this.edits.push(t,n,r,(c?G:0)|(c>0?6:0)|(o>>4<s>>4?K:0))})}get budget(){return this.limit}set budget(e){this.limit=Y(e)}skyLight(e,t,n){let r=this.chunkAt(e,t,n);if(r)return r.getLight(e&31,t&31,n&31)>>4;let i=this.world.heightmap(e>>5,n>>5);return i&&t<=i[e&31|(n&31)<<5]?0:15}blockLight(e,t,n){let r=this.chunkAt(e,t,n);return r?r.getLight(e&31,t&31,n&31)&15:0}update(){if(this.disposed)throw new h(`disposed`,`VoxelLighting is disposed; attach a new one with VoxelLighting.attach(world)`);this.types!==this.world.blocks.table.length&&this.refresh();let{limit:e,loads:t,edits:n}=this,r=0;for(;r<e&&this.loadHead<t.length;)r+=this.load(t[this.loadHead++]);if(this.loadHead===t.length){for(t.length=this.loadHead=0;r<e&&this.editHead<n.length;this.editHead+=4){let e=this.editHead;r+=this.edit(n[e],n[e+1],n[e+2],n[e+3])}if(this.editHead===n.length){for(n.length=this.editHead=0;r<e&&this.cutHead<this.cutE.length;r++)this.unspread();if(this.cutHead===this.cutE.length){for(this.cutS.length=this.cutE.length=this.cutHead=0;r<e&&this.addHead<this.addI.length;r++)this.spread();this.addHead===this.addI.length&&(this.addS.length=this.addI.length=this.addHead=0)}}}return this.flush(),r}dispose(){this.disposed||(this.disposed=!0,this.offChange(),this.offChunk(),J.delete(this.world),this.loads.length=this.edits.length=this.addI.length=this.addS.length=this.cutE.length=0,this.cutS.length=this.touched.length=0,this.sections.clear(),this.columns.clear())}[Symbol.dispose??Symbol()](){this.dispose()}chunkAt(e,t,n){if((e|0)!==e||(t|0)!==t||(n|0)!==n)throw[e,t,n].every(e=>typeof e==`number`)?RangeError(`Cell (${e}, ${t}, ${n}) is not three 32-bit integers; floor positions to cells first`):TypeError(`Cell coordinates must be numbers`);return this.world.getChunk(e>>5,t>>5,n>>5)}refresh(){let e=this.world.blocks.table;for(let t of e){let n=t.model||t.shape===`slab`?q:t.shape===`stair`?768:0;this.flags[t.id]=t.opacity|t.light<<4|n,this.wet[t.id]=t.waterlog?e[t.waterlog]?.opacity??0:0,n&&(this.parts=!0)}this.types=e.length}state(e,t){let n=e.chunk.get(t&31,t>>10,t>>5&31),r=this.flags[n&65535];if(!(r&q))return r;let i=this.world.blocks.table[n&65535]?.model;if(i)return r|i[(n>>>16)%i.length].covered<<10;let a=n>>>16,o=a&32768?Math.max(r&15,this.wet[n&65535]):r&15;return r&-16|o|oe[r&ie?y(a):v(a)]<<10}shut(e,t,n){return(e>>10+n&1)==1||(t>>10+(n^1)&1)==1}top(e,t,n){!(n&q)||n&ae||(this.put(e,t,240|this.light(e,t)&15),this.push(e,t))}cover(e,t,n){if(this.parts&&n!==H){for(let r of e)if(n>>5===r.cy){let e=t|(n&31)<<10;this.state(r,e)&q&&this.clear(r,e,0)}}}light(e,t){return e.chunk.getLight(t&31,t>>10,t>>5&31)}put(e,t,n){e.chunk.setLight(t&31,t>>10,t>>5&31,n),e.uniform=-1,e.touched||this.touched.push(e),e.touched|=se(t)}push(e,t){this.addS.push(e),this.addI.push(t)}cut(e,t,n,r){this.cutS.push(e),this.cutE.push(t|r<<15|n<<19)}near(e,t,n){let r=W[n>>1],i=t>>r&31;if(n&1){if(i)return this.j=t-(1<<r),e;this.j=t+(31<<r)}else{if(i<31)return this.j=t+(1<<r),e;this.j=t-(31<<r)}return e.links[n]}relax(e,t,n,r){let i=this.light(t,n);if((e>>4)-1<=i>>4&&(e&15)-1<=(i&15))return;let a=this.state(t,n);if(a>>10+r&1)return;let o=a&15,s=(e>>4)-1-o,c=(e&15)-1-o;(s>i>>4||c>(i&15))&&(this.put(t,n,Math.max(s,i>>4)<<4|Math.max(c,i&15)),this.push(t,n))}spread(){let e=this.addHead++,t=this.addS[e],n=this.addI[e];if(t.removed)return;let r=this.light(t,n);if(r>>4<=1&&(r&15)<=1)return;let i=this.parts?this.state(t,n)>>10:0;for(let e=0;e<6;e++){let a=i>>e&1?void 0:this.near(t,n,e);a&&this.relax(r,a,this.j,e^1)}}unspread(){let e=this.cutHead++,t=this.cutS[e],n=this.cutE[e],r=n&32767,i=n>>15&15,a=n>>19,o=this.parts&&!t.removed?this.state(t,r):0;for(let e=0;e<6;e++){let n=this.near(t,r,e);if(!n||n.removed)continue;let s=this.j,c=this.light(n,s),l=a?c&15:c>>4;if(!l)continue;let u=this.state(n,s);if(this.parts&&this.shut(o,u,e))continue;let d=a?u>>4&15:0;l<i&&d<l?(this.put(n,s,a?c&240|d:c&15),this.cut(n,s,a,l),d&&this.push(n,s)):t.removed||this.push(n,s)}}clear(e,t,n){let r=this.light(e,t),i=n?r&15:r>>4;i&&(this.put(e,t,n?r&240:r&15),this.cut(e,t,n,i))}pull(e,t){for(let n=0;n<6;n++){let r=this.near(e,t,n);r&&this.push(r,this.j)}}shade(e,t,n,r){let i=0;for(let a of e){let e=Math.max(n,a.cy<<5),o=Math.min(r,(a.cy<<5)+31);for(let n=e;n<=o;n++,i++)this.clear(a,t|(n&31)<<10,0)}return i}open(e,t,n,r){let i=0;for(let a of e){let e=a.cy<<5;if(!(e>r))for(let o=Math.min(r,e+31);o>=e;o--,i++){let e=n|(o&31)<<10,r=this.state(a,e);if(r&271)return t[n]=o,this.top(a,e,r),i;this.put(a,e,240|this.light(a,e)&15),this.push(a,e)}}return t[n]=H,i}edit(e,t,n,r){let i=e>>5,a=n>>5,o=this.sections.get(L(i,t>>5,a));if(!o)return 1;let s=e&31|(n&31)<<5|(t&31)<<10,c=this.state(o,s),l=1;if(r&G){let e=this.world.heightmap(i,a),n=this.columns.get(`${i},${a}`),u=s&1023,d=e[u];c&271&&t>d?(this.cover(n,u,d),l+=this.shade(n,u,d+1,t),e[u]=t):!(c&271)&&t===d?l+=this.open(n,e,u,t):t<=d&&r&re&&this.clear(o,s,0),t===e[u]&&this.top(o,s,c)}r&K&&this.clear(o,s,1),this.pull(o,s);let u=this.light(o,s);return(c>>4&15)>(u&15)&&(this.put(o,s,u&240|c>>4&15),this.push(o,s)),l}load([e,t,n,r]){let i=L(e,t,n);if(!r)return this.unload(i);if(this.world.getChunk(e,t,n)!==r)return 1;let{flags:a}=this,o=this.world.heightmap(e,n),s=`${e},${n}`,c=this.columns.get(s)??[],l=t<<5,u=l+31;r.fillLight(0);let d=!1,f=!1,p=1,m=1024;for(let e of P(r))d||=(a[e&65535]&271)>0,f||=(a[e&65535]&240)>0;for(let e=0;e<1024;e++){let t=H;if(d){for(let n=31;n>=0;n--,m++)if(a[r.get(e&31,n,e>>5)&65535]&271){t=l+n;break}}let n=o[e];n>u||(n>=l?t===H?p+=this.open(c,o,e,l-1):o[e]=t:t!==H&&(this.cover(c,e,n),p+=this.shade(c,e,n+1,l-1),o[e]=t))}let h={chunk:r,cx:e,cy:t,cz:n,links:[],removed:!1,touched:0,uniform:-1};this.sections.set(i,h);let g=0;for(;g<c.length&&c[g].cy>t;)g++;c.splice(g,0,h),this.columns.set(s,c);for(let r=0;r<6;r++){let i=r&1?-1:1,a=this.sections.get(L(e+(r<2?i:0),t+(r>>1==1?i:0),n+(r>3?i:0)));h.links[r]=a,a&&(a.links[r^1]=h)}let _=e=>Math.min(32,Math.max(0,o[e]+1-l)),v=!0,y=!0;for(let e=0;e<1024;e++)v&&=_(e)===0,y&&=_(e)===32;if(r.fillLight(v?240:0),h.uniform=v?240:y?0:-1,!v)for(let e=0;e<1024;e++){let t=_(e),n=e&31,i=e>>5;for(let e=t;e<32;e++,m++)r.setLight(n,e,i,240);if(this.parts&&t&&o[e]===l+t-1&&this.top(h,e|t-1<<10,this.state(h,e|t-1<<10)),!(t>31)){t&&this.push(h,e|t<<10);for(let r of[n<31?e+1:-1,n?e-1:-1,i<31?e+32:-1,i?e-32:-1])if(r>=0)for(let n=t;n<_(r);n++)this.push(h,e|n<<10)}}if(f)for(let e=0;e<32768;e++,m++){let t=this.state(h,e)>>4&15;t&&(this.put(h,e,this.light(h,e)&240|t),this.push(h,e))}for(let e=0;e<6;e++){let t=h.links[e];if(!t||h.uniform>=0&&t.uniform===h.uniform)continue;let n=W[e>>1],[r,i]=W.filter(e=>e!==n),a=e&1?0:31<<n,o=e&1?31<<n:0;for(let n=0;n<32;n++)for(let s=0;s<32;s++){let c=n<<r|s<<i,l=this.parts?this.state(t,c|o)>>10&1<<(e^1):0,u=this.parts?this.state(h,c|a)>>10&1<<e:0;l||this.relax(this.light(t,c|o),h,c|a,e),u||this.relax(this.light(h,c|a),t,c|o,e^1)}m+=2048}return h.touched||this.touched.push(h),h.touched=ne,p+(m>>3)}unload(e){let t=this.sections.get(e);if(!t)return 1;this.sections.delete(e),t.removed=!0;let n=`${t.cx},${t.cz}`,r=this.columns.get(n);r.splice(r.indexOf(t),1),r.length||this.columns.delete(n);let i=1;for(let e=0;e<6;e++){let n=t.links[e];if(!n)continue;n.links[e^1]=void 0;let r=W[e>>1],[a,o]=W.filter(e=>e!==r),s=e&1?0:31<<r;for(let e=0;e<32;e++)for(let n=0;n<32;n++){let r=e<<a|n<<o|s,i=this.light(t,r);i>>4&&this.cut(t,r,0,i>>4),i&15&&this.cut(t,r,1,i&15)}i+=128}return i}flush(){for(let e of this.touched){for(let t=0;t<27;t++)if(!e.removed&&e.touched>>t&1){let n=L(e.cx+t%3-1,e.cy+(t/3|0)%3-1,e.cz+(t/9|0)-1);this.sections.has(n)&&this.world.dirty.add(n)}e.touched=0}this.touched.length=0}},X,Z,Q,$=new Map,le=e=>{for(let t of e)t.id===0||t.id<Q.table.length||Q.define(t.name,{layer:t.layer,shape:t.shape,...t.model?{model:t.model}:{},solid:t.solid,liquid:t.liquid,light:t.light,opacity:t.opacity,...t.waterlog?{waterlog:t.waterlog}:{}})};t(e=>{if(e.init&&(Q=new E,X=new V({blocks:Q,minY:e.init.minY,maxY:e.init.maxY}),Z=ce.attach(X,{budget:1/0})),!X||!Z||!Q)throw TypeError(`VoxelLightWorker batch before init; attach a new one`);e.types&&le(e.types);for(let{cx:t,cy:n,cz:r,generation:i,chunk:a}of e.chunks){let e=L(t,n,r);X.getChunk(t,n,r)&&X.removeChunk(t,n,r),$.delete(e),a&&(X.addChunk(t,n,r,I.unpack(a)),$.set(e,i))}let t=e.edits;for(let e=0;e<t.length;e+=4)X.setBlock(t[e],t[e+1],t[e+2],t[e+3]>>>0);Z.update();let n={batch:e.batch,chunks:[],columns:[]},r=new Set;for(let e of X.dirty){let[t,i,a]=e.split(`,`).map(Number),o=X.getChunk(t,i,a);if(!o)continue;n.chunks.push({cx:t,cy:i,cz:a,generation:$.get(e)??0,light:F(o)});let s=`${t},${a}`;if(r.has(s))continue;r.add(s);let c=X.heightmap(t,a);c&&n.columns.push({cx:t,cz:a,heights:c.slice()})}return X.dirty.clear(),n})})();
@@ -0,0 +1 @@
1
+ (function(){function e(e,t=new Set,n=new Set){let r=[e];for(;r.length;){let e=r.pop();if(e&&typeof e==`object`&&!n.has(e)){if(n.add(e),ArrayBuffer.isView(e))e.buffer instanceof ArrayBuffer&&t.add(e.buffer);else if(e instanceof ArrayBuffer)t.add(e);else if(typeof ImageBitmap<`u`&&e instanceof ImageBitmap)t.add(e);else if(e instanceof Map)for(let[t,n]of e)r.push(t,n);else for(let t in e)r.push(e[t])}}return t}function t(t){self.onmessage=async({data:n})=>{try{if(!n||typeof n!=`object`)throw TypeError(`Invalid parser worker job; post an object`);let r=await t(n);self.postMessage({result:r},[...e(r)])}catch(e){let t={name:e?.name??`Error`,message:e instanceof Error?e.message:String(e)};try{self.postMessage({error:{...e,...t}})}catch{self.postMessage({error:t})}}}}var n=Uint8Array.from({length:16},(e,t)=>t&8?255:t&4?204:51),r=Uint8Array.from({length:64},(e,t)=>{let n=t&2?2:0,r=2-n,i=t&1?0:1,a=t&4?1:0,o=t>>3&3,s=t>>5&1,c=0;for(let e=0;e<8;e++){let t=e>>n&1,l=e>>r&1;((e>>1&1)===a||(o===2?t===i&&l===s:o===1?t===i||l===s:t===i))&&(c|=1<<e)}return c}),i=e=>n[e&15],a=e=>r[e&63],o=34,s=1156,c=[0,0,1,1,2,2],l=[1,-1,s,-1156,o,-34],u=[0,1,2,3,4,5].map(e=>{let t=[1,2,4][c[e]],n=0;for(let r=0;r<8;r++)(r&t)===(e&1?0:t)&&(n|=1<<r);return n}),d=u.map((e,t)=>u[t^1]),f=new WeakMap;function p(e){let t=f.get(e);if(t?.n===e.length)return t;let n=e.length;t={n,layer:new Uint8Array(n),opaque:new Uint8Array(n),shape:new Uint8Array(n)};for(let r=0;r<n;r++){let{layer:n,shape:i}=e[r],a=[`cube`,`slab`,`stair`].indexOf(i);t.shape[r]=a<0?3:a,t.layer[r]=a<0?0:[`opaque`,`cutout`,`translucent`].indexOf(n)+1,t.opaque[r]=+(n===`opaque`)}return f.set(e,t),t}var m;function h({chunk:e,shell:t,blocks:n}){let{layer:r,opaque:f,shape:h}=p(n),{palette:g,bits:_,words:v}=typeof e.pack==`function`?e.pack():e,y=(1<<_)-1,b=m??=new Uint32Array(o*s),x=e=>{let t=h[e&65535];return t===0?255:t===1?i(e>>>16):t===2?a(e>>>16):0};for(let e=0,n=0,r=0;e<34;e++)for(let i=0;i<34;i++,n+=o)if(e===0||e===33||i===0||i===33)for(let e=0;e<34;e++)b[n+e]=t[r++];else b[n]=t[r++],b[n+33]=t[r++];for(let e=0;e<32768;e++){let t=e*_;b[1+(e&31)+((e>>5&31)+1)*o+((e>>10)+1)*s]=_?g[v[t>>>5]>>>(t&31)&y]:g[0]}let S=[],C=[0,0,0];for(let e=-1;e<=32;e++)for(let t=-1;t<=32;t++)for(let n=-1;n<=32;n++){n===0&&e>=0&&e<32&&t>=0&&t<32&&(n=32);let i=n+1+(t+1)*o+(e+1)*s,a=b[i],p=a&65535;if(!r[p])continue;let m=x(a);C[0]=n,C[1]=e,C[2]=t;for(let a=0;a<6;a++){if((a&1?C[c[a]]<0:C[c[a]]>31)||(m&u[a])!==u[a])continue;let o=b[i+l[a]],s=o&65535;((f[s]?x(o):0)|(s===p?x(o):0))&d[a]||S.push(n+1|e+1<<6|t+1<<12|a<<18|r[p]-1<<21)}}return Uint32Array.from(S)}var g=32768,_=6536,v=39304,y=34,b=1156,x=1191,S=3,C=12,w=4,T=8,E=16,D=32,O=64,k=32768,A={opaque:0,cutout:1,translucent:2,invisible:3},j={cube:0,cross:w,liquid:T,slab:76,stair:76,model:D,object:D},M=[1,1,b,b,y,y],N=[y,y,1,1,1,1],P=[b,b,y,y,b,b],F=[0,0,5,5,10,10],I=[10,10,0,0,0,0],L=[5,5,10,10,5,5],R=[1,1,2,2,4,4],z=[4,4,1,1,1,1],B=[2,2,4,4,2,2],V=Array.from({length:16},(e,t)=>{let n=[],r=(e,t,r)=>{e===3?n.push(t<<2|32|r<<6):e&&n.push(e>>1|t<<2|(e>>1)+1<<4|r<<6)},i=t&3,a=t>>2;return i===a?r(i,0,2):a===3?(r(3,1,2),r(i,0,1)):(r(i,0,1),r(a,1,2)),n}),H=e=>{let t=0;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);return t},U=H(63);function W(e){let t=new Uint16Array(e.length*6).fill(65535),n=0;for(let r=0;r<e.length;r++){let{layer:i,textures:a}=e[r];if(i!==`invisible`)for(let e=0;e<6;e++){let i=a.indexOf(a[e]);if(i<e)t[r*6+e]=t[r*6+i];else if(n===4096)throw RangeError(`Block types need more than 4,096 materials; reuse texture layers or block states`);else t[r*6+e]=n++}}return t}var G=new WeakMap;function K(e){let t=G.get(e);if(t?.n===e.length)return t;let n=new Uint8Array(e.length),r=new Uint8Array(e.length),i=new Uint16Array(e.length);for(let t=0;t<e.length;t++){let a=A[e[t]?.layer],o=j[e[t]?.shape];if(a===void 0||o===void 0)throw TypeError(`MeshInput blocks[${t}] is not a block type; pass blocks.table`);n[t]=a|o|(a===0&&o===0?E:0)|(a===3||o===w?D:0),r[t]=+(e[t].shape===`stair`),i[t]=e[t].waterlog??0}let a=W(e),o=new Uint8Array(4096);for(let t of e)if(t.vertexMotion)for(let e=0;e<6;e++){let n=a[t.id*6+e];n!==65535&&(o[n]=1)}return t={n:e.length,flags:n,materials:a,stairs:r,waterlogs:i,unmerged:o},G.set(e,t),t}var q;function J(e,t,n,r,i){let a=e.out[t],o=e.count[t];if(o+3>a.length){let n=new Uint32Array(a.length*2);n.set(a),e.out[t]=a=n}a[o]=n,a[o+1]=r,a[o+2]=i,e.count[t]=o+3}var Y=Uint8Array.from({length:320},(e,t)=>t>>6?Math.floor(((t&63)*2+(t>>6))/((t>>6)*2)):0),X=(e,t,n)=>e?Y[e<<6|t>>8]|Y[e<<6|t&255]<<4:n>>4|(n&15)<<4;function Z(e,t,n,r){let{keys:i,lights:a,list:o}=e,s=n<<F[t]|t<<15,c=I[t],l=L[t];for(let t=0;t<r;t++){let n=o[t],r=i[n];if(!r)continue;let u=n&31,d=n>>5,f=a[n],p=r>>>12&3,m=r>>>14&3,h=r>>>16&3,g=r>>>18&3,_=f&255,v=f>>>8&255,y=f>>>16&255,b=f>>>24,x=1,S=1,C=r>>>26&31,w=e.unmerged[r&4095]||(r>>>24&3)==2;if(!w&&p===m&&g===h&&_===v&&b===y&&!(C&6))for(;u+x<32&&i[n+x]===r&&a[n+x]===f;)x++;if(!w&&p===g&&m===h&&_===b&&v===y&&!(C&24)){grow:for(;d+S<32;S++)for(let e=n+(S<<5),t=e+x;e<t;e++)if(i[e]!==r||a[e]!==f)break grow}for(let e=0;e<S;e++)for(let t=n+(e<<5),r=t+x;t<r;t++)i[t]=0;let T=m+g>p+h?1<<28:0;J(e,r>>>24&3,(s|u<<c|d<<l|x-1<<18|S-1<<23|T|r>>>21<<29)>>>0,r&2097151|C<<21,f)}}var Q=(e,t,n,r)=>{e[(n<<10)+t[n]++]=r};function ee(e,t,n,r){let{states:i,light:a,kind:o,occ:s,near:c,bucket:l,filled:u}=e;u.fill(0);for(let d=0;d<32;d++)for(let f=0;f<32;f++)for(let p=0,m=x+f*y+d*b;p<32;p++,m++){let h=o[m];if(h&D){if((h&C)!==w||(h&3)===S)continue;let n=p|d<<5|f<<10,r=t[(i[m]&65535)*6]|1044480,o=(a[m]>>4|(a[m]&15)<<4)*16843009>>>0;J(e,h&3,n|6<<15,r,o),J(e,h&3,n|7<<15,r,o);continue}if(n&&p&&d&&f&&p<31&&d<31&&f<31)continue;if(r){let e=h&O?k:0,t=e||(h&C)===T;(t||!s[m+1])&&Q(l,u,p,f|d<<5|(e||c[m+1]&&k)),(t||!s[m-1])&&Q(l,u,32+p,f|d<<5|(e||c[m-1]&&k)),(t||!s[m+b])&&Q(l,u,64+d,p|f<<5|(e||c[m+b]&&k)),(t||!s[m-b])&&Q(l,u,96+d,p|f<<5|(e||c[m-b]&&k)),(t||!s[m+y])&&Q(l,u,128+f,p|d<<5|(e||c[m+y]&&k)),(t||!s[m-y])&&Q(l,u,160+f,p|d<<5|(e||c[m-y]&&k));continue}let g=(h&C)===T;(g||!s[m+1])&&Q(l,u,p,f|d<<5),(g||!s[m-1])&&Q(l,u,32+p,f|d<<5),(g||!s[m+b])&&Q(l,u,64+d,p|f<<5),(g||!s[m-b])&&Q(l,u,96+d,p|f<<5),(g||!s[m+y])&&Q(l,u,128+f,p|d<<5),(g||!s[m-y])&&Q(l,u,160+f,p|d<<5)}}function te(e,t){let{states:n,light:r,kind:i,occ:a,spread:o,bucket:s,filled:c,keys:l,lights:u,list:d}=e;for(let f=0;f<6;f++){let p=M[f],m=f&1?-p:p,h=N[f],g=P[f],_=f===2,v=f<2||f>3;for(let y=0;y<32;y++){let S=f*32+y,w=0;for(let E=S<<10,D=E+c[S];E<D;E++){let c=s[E];if(c&k){w=ne(e,t,f,y,c&1023,w);continue}let S=x+y*p+(c&31)*h+(c>>5)*g,D=i[S],O=S+m,A=n[S],j=A&65535,M=(D&C)===T,N=M&&f!==3&&(n[S+b]&65535)!==j,P=N?A>>>16&7:0;if(a[O]){if(!_||!P)continue}else if((n[O]&65535)===j&&(!v||!M||P>=((n[O+b]&65535)===j?0:n[O]>>>16&7)))continue;let F=a[O-h],I=a[O+h],L=a[O-g],R=a[O+g],z=F&L?0:3-F-L-a[O-h-g],B=I&L?0:3-I-L-a[O+h-g],V=I&R?0:3-I-R-a[O+h+g],H=F&R?0:3-F-R-a[O-h+g],U=1-a[O],W=o[O],G=o[O-h],K=o[O+h],q=o[O-g],J=o[O+g],Y=r[S];l[c]=t[j*6+f]|z<<12|B<<14|V<<16|H<<18|(N?1<<20:0)|P<<21|(D&3)<<24|1<<31,u[c]=X(z+U,W+G+q+(F&L?0:o[O-h-g]),Y)|X(B+U,W+K+q+(I&L?0:o[O+h-g]),Y)<<8|X(V+U,W+K+J+(I&R?0:o[O+h+g]),Y)<<16|X(H+U,W+G+J+(F&R?0:o[O-h+g]),Y)<<24,d[w++]=c}w&&Z(e,f,y,w),e.partN&&re(e,f,y)}}}function ne(e,t,n,r,i,a){let{states:o,kind:s,flags:c,shape:l,solid:u,wet:d,wetMask:f}=e,p=M[n],m=R[n],h=z[n],g=B[n],_=!(n&1),v=x+r*p+(i&31)*N[n]+(i>>5)*P[n],y=v+(_?p:-p),S=_?m:0,w=_?0:m,E=l[v],D=d[v];if(E){let c=o[v]&65535,d=u[y]|((o[y]&65535)===c?l[y]:0),f=0,p=0;for(let e=0;e<4;e++){let t=(e&1?h:0)|(e&2?g:0);E>>(t|S)&1?d>>(t|w)&1||(f|=1<<e):E>>(t|w)&1&&(p|=1<<e)}a=$(e,n,r,i,a,v,f,p,t[c*6+n]|(s[v]&3)<<24|1<<31)}if(D){let l=f[v],p=n!==3&&d[v+b]!==D,m=p&&!E?o[v]>>>16&7:0,_=d[y]===D,x=_&&(n<2||n>3)&&(s[y]&C)===T&&m<(d[y+b]===D?0:o[y]>>>16&7),O=(n===2&&m?0:u[y])|(_&&!x?f[y]:0),k=u[v],A=0,j=0;for(let e=0;e<4;e++){let t=(e&1?h:0)|(e&2?g:0);l>>(t|S)&1?O>>(t|w)&1||(A|=1<<e):l>>(t|w)&1&&!(k>>(t|S)&1)&&(j|=1<<e)}m>=4&&(n<2||n>3)&&(A&=3),a=$(e,n,r,i,a,v,A,j,t[D*6+n]|(p?1<<20:0)|m<<21|(c[D]&3)<<24|1<<31)}return a}function $(e,t,n,r,i,a,o,s,c){let{light:l,solid:u,spread:d,keys:f,lights:p,list:m}=e,h=M[t],g=N[t],_=P[t],v=R[t],y=z[t],b=B[t],x=!(t&1);for(let t=0;t<2;t++){let n=t?s:o;if(!n)continue;let S=t?+!!x:x?2:-1,C=a+(S>>1)*h,w=S&1?v:0;for(let o of V[n]){let n=o&3,s=o>>2&3,h=o>>4&3,v=o>>6,x=c|(t|(n&1)<<1|(h&1)<<2|(s&1)<<3|(v&1)<<4)<<26,S=0;for(let e=0;e<4;e++){let t=e===1||e===2,r=t?h:n,i=e>=2?v:s,o=t?-1:0,c=e>=2?-1:0,f=r-1-o,p=i-1-c,m=r+o,T=i+c,E=C+(m>>1)*g+(T>>1)*_,D=C+(f>>1)*g+(T>>1)*_,O=C+(m>>1)*g+(p>>1)*_,k=C+(f>>1)*g+(p>>1)*_,A=m&1?y:0,j=T&1?b:0,M=f&1?y:0,N=p&1?b:0,P=u[D]>>(w|M|j)&1,F=u[O]>>(w|A|N)&1,I=P&F?1:u[k]>>(w|M|N)&1,L=P&F?0:3-P-F-I;x|=L<<12+2*e,S|=X(L+1,d[E]+(P?0:d[D])+(F?0:d[O])+(I?0:d[k]),l[a])<<8*e}if(x>>>26==32)f[r]=x,p[r]=S,m[i++]=r;else{let t=e.partN++;e.partM[t]=r,e.partK[t]=x,e.partL[t]=S>>>0}}}return i}function re(e,t,n){let{keys:r,lights:i,list:a,partM:o,partK:s,partL:c}=e,l=e.partN,u=0;for(let e=0;e<l;e++)u|=1<<(s[e]>>>26&31);for(let d=1;d<32;d++){if(!(u>>>d&1))continue;let f=0;for(let e=0;e<l;e++)if((s[e]>>>26&31)===d){let t=o[e];r[t]=s[e],i[t]=c[e],a[f++]=t}Z(e,t,n,f)}e.partN=0}function ie(e,t){let{states:n,kind:r,shape:o,solid:s,wet:c,wetMask:l,near:u}=e,{stairs:d,waterlogs:f}=t;u.fill(0);for(let e=0;e<v;e++){let t=r[e],p=n[e],m=p&65535,h=t&C?0:255,g=0,_=0;if(t&O){let e=p>>>16;h=d[m]?a(e):i(e),e&32768&&f[m]&&h!==255&&(g=f[m],_=~h&255)}else(t&C)===T&&(g=m,_=255);if(o[e]=h,s[e]=t&3?0:h,c[e]=g,l[e]=_,!(t&O))continue;let v=e%y,x=(e/y|0)%y,S=e/b|0;for(let e=Math.max(S-1,0);e<=Math.min(S+1,33);e++)for(let t=Math.max(x-1,0);t<=Math.min(x+1,33);t++)for(let n=Math.max(v-1,0);n<=Math.min(v+1,33);n++)u[n+t*y+e*b]=1}}function ae(e){let{occ:t,stack:n,seen:r}=e;for(let e=0;e<g;e++)r[e]=t[x+(e&31)+(e>>5&31)*y+(e>>10)*b];let i=0;for(let e=0;e<g&&i!==U;e++){if(r[e])continue;let t=0,a=0;for(r[e]=1,n[t++]=e;t;){let e=n[--t],i=e&31,o=e>>5&31,s=e>>10;i===31?a|=1:r[e+1]||(r[e+1]=1,n[t++]=e+1),i===0?a|=2:r[e-1]||(r[e-1]=1,n[t++]=e-1),s===31?a|=4:r[e+1024]||(r[e+1024]=1,n[t++]=e+1024),s===0?a|=8:r[e-1024]||(r[e-1024]=1,n[t++]=e-1024),o===31?a|=16:r[e+32]||(r[e+32]=1,n[t++]=e+32),o===0?a|=32:r[e-32]||(r[e-32]=1,n[t++]=e-32)}i|=H(a)}return i}function oe(e){let t=`pass a VoxelChunk or chunk.pack()`;if(e&&typeof e.pack==`function`)return e.pack();let n=e;if(!n||!(n.palette instanceof Uint32Array)||!(n.words instanceof Uint32Array))throw TypeError(`MeshInput chunk must be a VoxelChunk or a PackedChunk; ${t}`);if(typeof n.light!=`number`&&!(n.light instanceof Uint8Array))throw TypeError(`MeshInput chunk light must be a byte or a Uint8Array; ${t}`);let{palette:r,bits:i,words:a,light:o}=n,s=i?Math.min(2**i,g):1;if(![0,1,2,4,8,16].includes(i)||a.length!==1024*i||!r.length||r.length>s)throw RangeError(`MeshInput chunk has ${i}-bit indices, ${a.length} words and ${r.length} palette entries; ${t}`);if(typeof o==`number`?(o&255)!==o:o.length!==g)throw RangeError(`MeshInput chunk light must be a byte or 32,768 bytes; ${t}`);return n}function se(e){if(!e||typeof e!=`object`)throw TypeError(`meshChunk expects {chunk, shell, light, blocks}; build it with meshInput(world, key)`);let{shell:t,light:n,blocks:r}=e,i=oe(e.chunk);if(!Array.isArray(r)||!r.length)throw TypeError(`MeshInput blocks must be the block table; pass blocks.table`);if(!(t instanceof Uint32Array)||!(n instanceof Uint8Array))throw TypeError(`MeshInput shell must be a Uint32Array and light a Uint8Array; build them with meshInput()`);if(t.length!==_||n.length!==_)throw RangeError(`MeshInput shell and light have ${t.length} and ${n.length} cells, not 6,536; build them with meshInput()`);let a=K(r),{flags:o,materials:s}=a,c=q??={unmerged:new Uint8Array,states:new Uint32Array(v),light:new Uint8Array(v),kind:new Uint8Array(v),occ:new Uint8Array(v),spread:new Uint16Array(v),flags:new Uint8Array,shape:new Uint8Array(v),solid:new Uint8Array(v),wet:new Uint16Array(v),wetMask:new Uint8Array(v),near:new Uint8Array(v),partM:new Uint16Array(8192),partK:new Uint32Array(8192),partL:new Uint32Array(8192),partN:0,bucket:new Uint16Array(196608),filled:new Uint16Array(192),keys:new Uint32Array(1024),lights:new Uint32Array(1024),list:new Uint16Array(1024),stack:new Int32Array(g),seen:new Uint8Array(g),out:[new Uint32Array(12288),new Uint32Array(12288),new Uint32Array(12288)],count:[0,0,0]},{states:l,light:u,kind:d,occ:f,spread:p}=c,{palette:m,bits:h,words:w}=i,T=i.light,D=(1<<h)-1;c.unmerged=a.unmerged,c.count.fill(0),c.keys.fill(0);for(let e=0,t=0;e<32;e++)for(let n=0;n<32;n++,t+=32){let r=x+n*y+e*b;if(typeof T==`number`)u.fill(T,r,r+32);else for(let e=0;e<32;e++)u[r+e]=T[t+e];if(!h)l.fill(m[0],r,r+32);else for(let e=0;e<32;e++){let n=(t+e)*h,i=w[n>>>5]>>>(n&31)&D;if(i>=m.length)throw RangeError(`MeshInput chunk cell ${t+e} uses palette entry ${i} of ${m.length}; keep every cell index below the palette length`);l[r+e]=m[i]}}for(let e=0,r=0,i=0;e<34;e++)for(let a=0;a<34;a++,r+=y)if(e===0||e===33||a===0||a===33)for(let e=0;e<34;e++,i++)l[r+e]=t[i],u[r+e]=n[i];else l[r]=t[i],u[r]=n[i++],l[r+33]=t[i],u[r+33]=n[i++];let k=0;for(let e=0;e<v;e++){let t=o[l[e]&65535];if(t===void 0)throw RangeError(`MeshInput block id ${l[e]&65535} is not in the ${o.length}-type table; pass the table the states use`);d[e]=t,f[e]=t>>4&1,p[e]=t&E?0:u[e]>>4<<8|u[e]&15,k|=t}let A=o[m[0]&65535];if(!h&&(A&3)===S)return ce(U);c.flags=o,k&O&&ie(c,a),ee(c,s,!h&&(A&C)===0,(k&O)!==0),te(c,s);let{out:j,count:M}=c;return{opaque:j[0].slice(0,M[0]),cutout:j[1].slice(0,M[1]),translucent:j[2].slice(0,M[2]),visibility:h?ae(c):A&E?0:U}}var ce=e=>({opaque:new Uint32Array,cutout:new Uint32Array,translucent:new Uint32Array,visibility:e});function le(e){let t=new Float32Array(e.length/3*3);for(let n=0;n<t.length;n+=3){let r=e[n],i=e[n+1]>>>21,a=r>>>15&7,o=(r>>>18&31)/2+(i>>1&1)/4-(i>>2&1)/4,s=(r>>>23&31)/2+(i>>3&1)/4-(i>>4&1)/4,c=i&1?0:.5,l=(r&31)+.5,u=(r>>>5&31)+.5,d=(r>>>10&31)+.5;a<2?(l+=a?-c:c,d+=o,u+=s):a<4?(u+=a===2?c:-c,l+=o,d+=s):a<6&&(d+=a===4?c:-c,l+=o,u+=s),t[n]=l,t[n+1]=u,t[n+2]=d}return t}function ue(e,t){if(!(e instanceof Uint32Array))throw TypeError(`sortQuads expects a Uint32Array of quads, such as mesh.translucent`);if(fe(t,`sortQuads eye`),e.length%3)throw RangeError(`sortQuads got ${e.length} words, not whole quads of 3; pass a ChunkMesh layer`);let n=e.length/3,r=le(e),i=new Float64Array(n),a=new Float64Array(n),o=new Uint32Array(n),[s,c,l]=t,u=Math.floor(s),d=Math.floor(c),f=Math.floor(l);for(let t=0;t<n;t++)i[t]=de(e[t*3],e[t*3+1],u,d,f),a[t]=(r[t*3]-s)**2+(r[t*3+1]-c)**2+(r[t*3+2]-l)**2,o[t]=t;o.sort((e,t)=>i[t]-i[e]||a[t]-a[e]||e-t);let p=e.slice();for(let t=0;t<n;t++)e.set(p.subarray(o[t]*3,o[t]*3+3),t*3);return e}function de(e,t,n,r,i){let a=e&31,o=e>>>5&31,s=e>>>10&31,c=e>>>15&7,l=Math.abs(a-n)+Math.abs(o-r)+Math.abs(s-i);if(c>5||t>>>21&1||c===2&&(e>>>29||t>>>20&1))return 2*l;let u=c>>1,d=c&1?-1:1,f=Math.abs(a+(u===0?d:0)-n)+Math.abs(o+(u===1?d:0)-r)+Math.abs(s+(u===2?d:0)-i);return 2*Math.max(l,f)-1}function fe(e,t){if(!Array.isArray(e)||e.length!==3||!e.every(e=>typeof e==`number`))throw TypeError(`${t} must be [x, y, z] in blocks`);if(!e.every(Number.isFinite))throw RangeError(`${t} is [${e.join(`, `)}]; pass finite coordinates`)}t(({input:e,eye:t,border:n})=>{let r=se(e);return t&&ue(r.translucent,t),n&&(r.opaque.length||r.cutout.length||r.translucent.length)&&(r.border=h(e)),r})})();
@@ -0,0 +1 @@
1
+ (function(){function e(e,t=new Set,n=new Set){let r=[e];for(;r.length;){let e=r.pop();if(e&&typeof e==`object`&&!n.has(e)){if(n.add(e),ArrayBuffer.isView(e))e.buffer instanceof ArrayBuffer&&t.add(e.buffer);else if(e instanceof ArrayBuffer)t.add(e);else if(typeof ImageBitmap<`u`&&e instanceof ImageBitmap)t.add(e);else if(e instanceof Map)for(let[t,n]of e)r.push(t,n);else for(let t in e)r.push(e[t])}}return t}function t(t){self.onmessage=async({data:n})=>{try{if(!n||typeof n!=`object`)throw TypeError(`Invalid parser worker job; post an object`);let r=await t(n);self.postMessage({result:r},[...e(r)])}catch(e){let t={name:e?.name??`Error`,message:e instanceof Error?e.message:String(e)};try{self.postMessage({error:{...e,...t}})}catch{self.postMessage({error:t})}}}}var n=(e,t,n,r)=>typeof t==`number`?RangeError(`${e} ${t} is not an integer in 0..${n}; ${r}`):TypeError(`${e} must be a number; ${r}`),r=32768,i=e=>e<=1?0:e<=2?1:e<=4?2:e<=16?4:e<=256?8:16,a=e=>e?Math.min(1<<e,r):1,o=(e,t,n)=>[e,t,n].every(e=>typeof e==`number`)?RangeError(`Chunk cell (${e}, ${t}, ${n}) is not in 0..31; pass local coordinates (cell & 31)`):TypeError(`Chunk cell coordinates must be numbers`),s=e=>n(`Block state`,e,4294967295,`build it with blockState(id, data)`),c=e=>RangeError(`PackedChunk ${e}; pass the result of chunk.pack()`);function l(e,t,n,i){if(!n)return null;let a=new Uint32Array(1024*n),o=(1<<e)-1;if((!e||!t)&&!i?.[0])return a;for(let s=0;s<r;s++){let r=e&&t?t[s*e>>>5]>>>(s*e&31)&o:0;i&&(r=i[r]),r&&(a[s*n>>>5]|=r<<(s*n&31))}return a}var u=class e{constructor(e=0){this.palette=new Uint32Array(1),this.counts=Uint16Array.of(r),this.size=1,this.live=1,this.free=[],this.lookup=null,this.bits=0,this.words=null,this.lightByte=0,this.lightRevision=0,this.lightCells=null,this.fill(e)}get(e,t,n){if((e&31)!==e||(t&31)!==t||(n&31)!==n)throw o(e,t,n);return this.palette[this.slot(e|n<<5|t<<10)]}set(e,t,n,r){if((e&31)!==e||(t&31)!==t||(n&31)!==n)throw o(e,t,n);if(r>>>0!==r)throw s(r);let i=e|n<<5|t<<10,a=this.slot(i),c=this.palette[a];if(c===r)return c;let l=this.find(r);if(l<0&&this.counts[a]===1)return this.lookup?.delete(c),this.palette[a]=r,this.lookup?.set(r,a),c;l<0&&(l=this.add(r));let u=this.bits,d=i*u,f=d>>>5,p=d&31,m=this.words;return m[f]=m[f]&~((1<<u)-1<<p)|l<<p,this.counts[l]++,--this.counts[a]===0&&this.release(a),c}fill(e){if(e>>>0!==e)throw s(e);this.palette=Uint32Array.of(e),this.counts=Uint16Array.of(r),this.size=this.live=1,this.free=[],this.lookup=null,this.bits=0,this.words=null}getLight(e,t,n){if((e&31)!==e||(t&31)!==t||(n&31)!==n)throw o(e,t,n);return this.lightCells?this.lightCells[e|n<<5|t<<10]:this.lightByte}setLight(e,t,i,a){if((e&31)!==e||(t&31)!==t||(i&31)!==i)throw o(e,t,i);if((a&255)!==a)throw n(`Light byte`,a,255,`pass sky << 4 | block`);if(this.getLight(e,t,i)!==a){if(this.lightRevision++,!this.lightCells){if(a===this.lightByte)return;this.lightCells=new Uint8Array(r).fill(this.lightByte)}this.lightCells[e|i<<5|t<<10]=a}}fillLight(e){if((e&255)!==e)throw n(`Light byte`,e,255,`pass sky << 4 | block`);(this.lightCells||this.lightByte!==e)&&this.lightRevision++,this.lightByte=e,this.lightCells=null}get byteLength(){return this.palette.byteLength+this.counts.byteLength+(this.words?.byteLength??0)+(this.lightCells?.byteLength??0)}pack(){return{palette:this.palette.slice(0,this.size),bits:this.bits,words:this.words?this.words.slice():new Uint32Array,light:this.lightCells?this.lightCells.slice():this.lightByte}}static unpack(t){if(!t||typeof t!=`object`)throw TypeError(`PackedChunk must be the object chunk.pack() returns`);let{palette:n,bits:o,words:s,light:l}=t;if(!(n instanceof Uint32Array)||!(s instanceof Uint32Array))throw TypeError(`PackedChunk palette and words must be Uint32Arrays; pass the result of chunk.pack()`);if(![0,1,2,4,8,16].includes(o))throw c(`bits ${o} is not 0, 1, 2, 4, 8 or 16`);let u=a(o);if(n.length<1||n.length>u)throw c(`palette has ${n.length} entries where ${o}-bit indices allow 1..${u}`);if(s.length!==1024*o)throw c(`words has ${s.length} entries, not ${1024*o}`);if(typeof l==`number`){if((l&255)!==l)throw c(`light byte ${l} is not in 0..255`)}else if(!(l instanceof Uint8Array))throw TypeError(`PackedChunk light must be a byte or a Uint8Array`);else if(l.length!==r)throw c(`light has ${l.length} bytes, not ${r}`);let d=new e,f=new Uint16Array(u),p=(1<<o)-1;if(!o)f[0]=r;else for(let e=0;e<r;e++){let t=s[e*o>>>5]>>>(e*o&31)&p;if(t>=n.length)throw c(`cell ${e} uses palette entry ${t} of ${n.length}`);f[t]++}d.palette=new Uint32Array(u),d.palette.set(n),d.counts=f,d.size=n.length,d.bits=o,d.words=o?s.slice():null,d.lightByte=typeof l==`number`?l:0,d.lightCells=typeof l==`number`?null:l.slice();let m=new Set,h=0;for(let e=0;e<n.length;e++)f[e]?(h++,m.add(n[e])):d.free.push(e);if(d.live=h,m.size<h||h===1||i(2*h)<o)d.compact();else if(u>16){d.lookup=new Map;for(let e=0;e<n.length;e++)f[e]&&d.lookup.set(n[e],e)}return d}slot(e){let t=this.bits,n=e*t;return t?this.words[n>>>5]>>>(n&31)&(1<<t)-1:0}find(e){if(this.lookup)return this.lookup.get(e)??-1;let{palette:t,counts:n,size:r}=this;for(let i=0;i<r;i++)if(t[i]===e&&n[i])return i;return-1}grow(e){let t=i(e),n=a(t),r=new Uint32Array(n),o=new Uint16Array(n);if(r.set(this.palette),o.set(this.counts),this.words=l(this.bits,this.words,t,null),this.palette=r,this.counts=o,this.bits=t,n>16&&!this.lookup){this.lookup=new Map;for(let e=0;e<this.size;e++)o[e]&&this.lookup.set(r[e],e)}}add(e){!this.free.length&&this.size===this.palette.length&&this.grow(this.size+1);let t=this.free.pop()??this.size++;return this.palette[t]=e,this.lookup?.set(e,t),this.live++,t}release(e){this.lookup?.delete(this.palette[e]),this.free.push(e),(--this.live===1||i(2*this.live)<this.bits)&&this.compact()}compact(){let{palette:e,counts:t,size:n}=this,r=new Uint16Array(n),o=new Map;for(let i=0;i<n;i++)if(t[i]){let t=o.get(e[i]);t===void 0&&o.set(e[i],t=o.size),r[i]=t}let s=o.size,c=s===1?0:Math.min(this.bits,i(2*s)),u=a(c);this.palette=new Uint32Array(u),this.counts=new Uint16Array(u);for(let[e,t]of o)this.palette[t]=e;for(let e=0;e<n;e++)t[e]&&(this.counts[r[e]]+=t[e]);this.words=l(this.bits,this.words,c,r),this.size=this.live=s,this.free=[],this.bits=c,this.lookup=u>16?o:null}};function d({octaves:e=5,lacunarity:t=2,gain:n=.5,frequency:r=1}){if(!Number.isInteger(e)||e<1||e>16||![t,n,r].every(Number.isFinite))throw RangeError(`Invalid fractal options; pass integer octaves from 1 to 16 and finite lacunarity, gain and frequency`);return[e,t,n,r]}var f=(Math.sqrt(3)-1)/2,p=(3-Math.sqrt(3))/6,m=1/3,h=1/6,g=(Math.sqrt(5)-1)/4,_=(5-Math.sqrt(5))/20,v=[0,0,70,76,62],y=[1,-1,1,-1,1,-1,1,-1,0,0,0,0],b=[1,1,-1,-1,0,0,0,0,1,-1,1,-1],x=[0,0,0,0,1,1,-1,-1,1,1,-1,-1],S=e=>e*e*e*(e*(e*6-15)+10);function C(e,t,n,r){let i=Math.imul(e,374761393)^Math.imul(t,668265263)^Math.imul(n,1442695041)^r;return i=Math.imul(i^i>>>13,1274126177),(i^i>>>16)>>>0}var w=new Int8Array([1,1,0,-1,1,0,1,-1,0,-1,-1,0,1,0,1,-1,0,1,1,0,-1,-1,0,-1,0,1,1,0,-1,1,0,1,-1,0,-1,-1,1,1,0,0,-1,1,-1,1,0,0,-1,-1]);function T(e,t,n,r){let i=(e&15)*3;return w[i]*t+w[i+1]*n+w[i+2]*r}var E=(e,t,n=0,r=0)=>{if(!(Math.abs(e)<=1e7&&Math.abs(t)<=1e7&&Math.abs(n)<=1e7&&Math.abs(r)<=1e7))throw RangeError(`Noise coordinates exceed finite bounds; keep every coordinate within 10,000,000 of zero.`)};function D(e){if(!Number.isSafeInteger(e))throw RangeError(`Invalid noise seed; pass a safe integer.`);let t=e>>>0||1,n=Uint8Array.from({length:512},(e,t)=>t&255);for(let e=255;e>0;e--){t^=t<<13,t^=t>>>17,t^=t<<5;let r=Math.floor((t>>>0)/4294967296*(e+1)),i=n[e];n[e]=n[r],n[r]=i}return n.copyWithin(256,0,256),n}function O(e,t,n){let r=.5-t*t-n*n;if(r<=0)return 0;r*=r;let i=e&7,a=i<4?(i&1?-t:t)+(i&2?-n:n):i<6?i&1?-t:t:i&1?-n:n;return r*r*a}function k(e,t,n,r,i){let a=.5-n*n-r*r-i*i;return a<=0?0:(a*=a,a*a*(e[t*3]*n+e[t*3+1]*r+e[t*3+2]*i))}function A(e,t,n,r,i){let a=.5-t*t-n*n-r*r-i*i;if(a<=0)return 0;a*=a;let o=e&31,s=o>>3,c=o&4?-1:1,l=o&2?-1:1,u=o&1?-1:1;return a*a*(s===0?c*n+l*r+u*i:s===1?c*t+l*r+u*i:s===2?c*t+l*n+u*i:c*t+l*n+u*r)}var j=class{constructor(e=1){this.seed=e;let t=this.perm=D(e);this.gradients3=Float64Array.from({length:1536},(e,n)=>[y,b,x][n%3][t[Math.floor(n/3)]%12])}simplex2(e,t){E(e,t);let n=this.perm,r=(e+t)*f,i=Math.floor(e+r),a=Math.floor(t+r),o=(i+a)*p,s=e-i+o,c=t-a+o,l=+(s>c),u=1-l,d=i&255,m=a&255;return v[2]*(O(n[d+n[m]],s,c)+O(n[d+l+n[m+u]],s-l+p,c-u+p)+O(n[d+1+n[m+1]],s-1+2*p,c-1+2*p))}simplex(e,t,n){E(e,t,n);let r=this.perm,i=this.gradients3,a=(e+t+n)*m,o=Math.floor(e+a),s=Math.floor(t+a),c=Math.floor(n+a),l=(o+s+c)*h,u=e-o+l,d=t-s+l,f=n-c+l,p=o&255,g=s&255,_=c&255,y=0,b=0,x=0;u>d?y++:b++,u>f?y++:x++,d>f?b++:x++;let S=+(y>=2),C=+(b>=2),w=+(x>=2),T=+(y>=1),D=+(b>=1),O=+(x>=1);return v[3]*(k(i,p+r[g+r[_]],u,d,f)+k(i,p+S+r[g+C+r[_+w]],u-S+h,d-C+h,f-w+h)+k(i,p+T+r[g+D+r[_+O]],u-T+2*h,d-D+2*h,f-O+2*h)+k(i,p+1+r[g+1+r[_+1]],u-1+3*h,d-1+3*h,f-1+3*h))}simplex4(e,t,n,r){E(e,t,n,r);let i=this.perm,a=(e+t+n+r)*g,o=Math.floor(e+a),s=Math.floor(t+a),c=Math.floor(n+a),l=Math.floor(r+a),u=(o+s+c+l)*_,d=e-o+u,f=t-s+u,p=n-c+u,m=r-l+u,h=o&255,y=s&255,b=c&255,x=l&255,S=0,C=0,w=0,T=0;d>f?S++:C++,d>p?S++:w++,d>m?S++:T++,f>p?C++:w++,f>m?C++:T++,p>m?w++:T++;let D=0;for(let e=0;e<5;e++){let t=+(S>=4-e),n=+(C>=4-e),r=+(w>=4-e),a=+(T>=4-e);D+=A(i[h+t+i[y+n+i[b+r+i[x+a]]]],d-t+e*_,f-n+e*_,p-r+e*_,m-a+e*_)}return v[4]*D}perlin(e,t,n){E(e,t,n);let r=Math.floor(e),i=Math.floor(t),a=Math.floor(n);e-=r,t-=i,n-=a;let o=S(e),s=S(t),c=S(n),l=0;for(let u=0;u<2;u++)for(let d=0;d<2;d++)for(let f=0;f<2;f++)l+=T(C(r+u,i+d,a+f,this.seed),e-u,t-d,n-f)*(u?o:1-o)*(d?s:1-s)*(f?c:1-c);return l}worley(e,t,n){E(e,t,n);let r=Math.floor(e),i=Math.floor(t),a=Math.floor(n),o=1/0;for(let s=1;s<=2;s++)for(let c=-s;c<=s;c++)for(let l=-s;l<=s;l++)for(let u=-s;u<=s;u++){if(s===2&&Math.abs(c)<2&&Math.abs(l)<2&&Math.abs(u)<2)continue;let d=r+c,f=i+l,p=a+u,m=e<d?d-e:e>=d+1?e-(d+1):0,h=t<f?f-t:t>=f+1?t-(f+1):0,g=n<p?p-n:n>=p+1?n-(p+1):0;if(m*m+h*h+g*g>=o)continue;let _=d+C(d,f,p,this.seed)/4294967296-e,v=f+C(d,f,p,this.seed^2654435769)/4294967296-t,y=p+C(d,f,p,this.seed^2246822507)/4294967296-n;o=Math.min(o,_*_+v*v+y*y)}return Math.sqrt(o)}},M=d;function N(e,t={}){let[n,r,i,a]=M(t);return(t,o,s=0,c=0)=>{let l=0,u=0,d=1,f=a;for(let a=0;a<n;a++)l+=d*e(t*f,o*f,s*f,c*f),u+=d,d*=i,f*=r;return l/u}}var P=[`stone`,`dirt`,`grass`,`sand`,`water`,`log`,`leaves`],F=2,I=4,L=40,R=(e,t,n,r,i)=>{if(e===void 0)return n;if(typeof e!=`number`)throw TypeError(`NoiseTerrainOptions.${t} must be a number`);if(!r(e))throw RangeError(`NoiseTerrainOptions.${t} is ${e}; pass ${i}`);return e},z=e=>Number.isFinite(e),B=(e,t,n)=>{let r=Math.imul(e,374761393)^Math.imul(t,668265263)^Math.imul(n,1442695041);return r=Math.imul(r^r>>>13,1274126177),(r^r>>>16)>>>0},V=(e,t,n)=>e+(t-e)*n,H=(e,t,n,r)=>{let i=(t>>2)+9*(n>>2)+81*(r>>2),a=(t&3)/4,o=(n&3)/4;return V(V(V(e[i],e[i+1],a),V(e[i+9],e[i+10],a),o),V(V(e[i+81],e[i+82],a),V(e[i+90],e[i+91],a),o),(r&3)/4)};function U(e={}){if(!e||typeof e!=`object`)throw TypeError(`noiseTerrain options must be an object`);let t=R(e.seed,`seed`,1,e=>(e|0)===e,`an integer in -2^31..2^31-1`),n=R(e.seaLevel,`seaLevel`,0,e=>(e|0)===e,`an integer cell y`),r=R(e.height,`height`,8,z,`a finite number`),i=R(e.amplitude,`amplitude`,32,e=>e>=0&&z(e),`a finite number >= 0`),a=R(e.scale,`scale`,192,e=>e>0&&z(e),`a finite number > 0`),o=R(e.caves,`caves`,.3,e=>e>=0&&e<=1,`0..1`),s=R(e.trees,`trees`,.01,e=>e>=0&&e<=1,`0..1`),c=e.blocks??{};if(!c||typeof c!=`object`)throw TypeError(`NoiseTerrainOptions.blocks must be an object of names`);let l=P.map(e=>{let t=c[e]??e;if(typeof t!=`string`)throw TypeError(`NoiseTerrainOptions.blocks.${e} must be a block type name`);return t}),u=new j(t),d=new j(t^1540483477),f=new j(t^461845907),p=N((e,t)=>u.simplex2(e,t),{octaves:5,frequency:1/a}),m=(e,t)=>Math.floor(r+i*p(e,t)),h=.3*o,g=s*4294967296;return(e,r,i,a,o)=>{if(!Array.isArray(o?.blocks))throw TypeError(`noiseTerrain needs context {blocks}: the world BlockTypes table`);let c=new Map(o.blocks.map(e=>[e.name,e.id])),[u,p,_,v,y,b,x]=l.map((e,t)=>{let n=c.get(e);if(n===void 0&&(t<5||s>0))throw RangeError(`noiseTerrain block "${e}" is not defined; define it with blocks.define("${e}") or name another type in NoiseTerrainOptions.blocks.${P[t]}`);return n??0}),S=r*32,C=i*32,w=a*32,T=C+31,E=new Int32Array(1600),D=new Uint8Array(1600),O=(e,t)=>{let n=e+t*L;return D[n]||(D[n]=1,E[n]=m(S+e-I,w+t-I)),E[n]},k=1/0,A=-1/0,j=-1/0;for(let e=2;e<38;e++)for(let t=2;t<38;t++){let n=O(t,e);j=Math.max(j,n),t>=I&&e>=I&&t<36&&e<36&&(k=Math.min(k,n),A=Math.max(A,n))}let M=Math.max(g>0?j+7:A,n);if(T<k-3)e.fill(u);else if(C>A&&T<=n)e.fill(y);else if(C<=Math.max(A,n))for(let t=0;t<32;t++)for(let r=0;r<32;r++){let i=E[r+I+(t+I)*L],a=Math.min(T,Math.max(i,n));for(let o=C;o<=a;o++)e.set(r,o-C,t,o<i-3?u:o<i?p:o===i?i<=n+1?v:_:y)}if(h>0&&C<A){let t=new Float64Array(729),r=new Float64Array(729);for(let e=0,n=0;e<9;e++)for(let i=0;i<9;i++)for(let a=0;a<9;a++,n++){let o=(S+4*a)/64,s=(C+4*i)/40,c=(w+4*e)/64;t[n]=d.simplex(o,s,c),r[n]=f.simplex(o,s,c)}for(let i=0;i<32;i++)for(let a=0;a<32;a++){let o=a+I+(i+I)*L,s=Math.min(31,E[o]-1-C),c=Math.min(E[o-1],E[o+1],E[o-L],E[o+L]);for(let o=0;o<=s;o++)o+C>c&&o+C<=n||Math.abs(H(t,a,o,i))<h&&Math.abs(H(r,a,o,i))<h&&e.set(a,o,i,0)}}if(g>0)for(let r=2;r<38;r++)for(let i=2;i<38;i++){let a=S+i-I,o=w+r-I,s=B(a,o,t),c=E[i+r*L];if(s>=g||c<=n+1)continue;let l=c+4+s%3,u=a-S,d=o-w;if(l+1<C||c+1>T)continue;let f=!0;for(let e=-2;e<=F&&f;e++)for(let t=-2;t<=F;t++)Math.abs(t)+Math.abs(e)<4&&O(i+t,r+e)>=l-2&&(f=!1);if(!f)continue;let p=(t,n,r,i)=>{if((t&31)!==t||(n&31)!==n||(r&31)!==r)return;let a=e.get(t,n,r);(a===0||i===b&&a===x)&&e.set(t,n,r,i)};for(let e=-2;e<=1;e++)for(let t=-2;t<=F;t++)for(let n=-2;n<=F;n++)(e<0?Math.abs(n)+Math.abs(t)<4:Math.abs(n)+Math.abs(t)<2)&&p(u+n,l+e-C,d+t,x);for(let e=c+1;e<=l;e++)p(u,e-C,d,b)}return M}}var W=``,G;t(({cx:e,cy:t,cz:n,blocks:r,terrain:i})=>{let a=JSON.stringify(i);a!==W&&(G=U(i),W=a);let o=new u,s=G(o,e,t,n,{blocks:r});return{...o.pack(),ceiling:s}})})();