@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,179 @@
1
+ import { n as e, t } from "./octants-C_Ic7gdn.js";
2
+ import { parseColor as n } from "@margoengine/math/color";
3
+ import { MargoError as r } from "@margoengine/math/errors";
4
+ //#region src/blocks.ts
5
+ var i = [
6
+ "opaque",
7
+ "cutout",
8
+ "translucent",
9
+ "invisible"
10
+ ], a = [
11
+ "cube",
12
+ "cross",
13
+ "liquid",
14
+ "slab",
15
+ "stair",
16
+ "model",
17
+ "object"
18
+ ], o = [
19
+ "+x",
20
+ "-x",
21
+ "+z",
22
+ "-z"
23
+ ], s = (e, t, n, r) => typeof t == "number" ? /* @__PURE__ */ RangeError(`${e} ${t} is not an integer in 0..${n}; ${r}`) : /* @__PURE__ */ TypeError(`${e} must be a number; ${r}`);
24
+ function c(e, t = 0) {
25
+ if ((e & 65535) !== e) throw s("Block id", e, 65535, "pass an id from blocks.define()");
26
+ if ((t & 65535) !== t) throw s("Block state data", t, 65535, "pass 16 bits of data");
27
+ return (e | t << 16) >>> 0;
28
+ }
29
+ var l = (e) => e & 65535, u = (e) => e >>> 16, d = (e, t = !1) => c(e, t ? 4 : 0), f = (e) => c(e, 8);
30
+ function p(e, t, n = !1, r) {
31
+ let i = o.indexOf(t);
32
+ if (i < 0) throw TypeError(`Stair facing ${String(t)} must be '+x', '-x', '+z' or '-z'`);
33
+ let a = 0;
34
+ if (r !== void 0) {
35
+ let e = r === "straight" ? 0 : r.startsWith("inner") ? 1 : r.startsWith("outer") ? 2 : -1, n = o.indexOf(r.slice(5));
36
+ if (e < 0 || e && (n < 0 || n >> 1 == i >> 1)) throw TypeError(`Stair corner ${String(r)} must be 'straight' or 'inner'/'outer' with a side across ${t}, such as '${i < 2 ? "inner+z" : "inner+x"}'`);
37
+ a = 64 | e << 3 | (e && n % 2 == 0 ? 32 : 0);
38
+ }
39
+ return c(e, i | (n ? 4 : 0) | a);
40
+ }
41
+ function m(e, n = !0) {
42
+ if (typeof e != "number") throw TypeError("waterlogState expects a block state number");
43
+ if (e >>> 0 !== e) throw RangeError(`Block state ${e} is not a u32; pass a state from blockState()`);
44
+ return (n ? e | t << 16 : e & ~(t << 16)) >>> 0;
45
+ }
46
+ var h = [
47
+ [1, 0],
48
+ [-1, 0],
49
+ [0, 1],
50
+ [0, -1]
51
+ ];
52
+ function g(e, t, n, r) {
53
+ if (typeof e?.getBlock != "function") throw TypeError("connectStairs expects a VoxelWorld and a cell");
54
+ let i = e.blocks.table, a = [], o = (t, r, a) => {
55
+ let o = e.getBlock(t, n, r);
56
+ return i[o & 65535]?.shape === "stair" && (o >>> 16 & 4) === a ? o >>> 16 & 3 : -1;
57
+ };
58
+ for (let [s, l] of [[0, 0], ...h]) {
59
+ let u = t + s, d = r + l, f = e.getBlock(u, n, d), p = f >>> 16;
60
+ if (i[f & 65535]?.shape !== "stair" || p & 64) continue;
61
+ let m = p & 3, [g, _] = h[m], v = p & 4, y = o(u + g, d + _, v), b = o(u - g, d - _, v), x = (e, t) => o(u + t * h[e][0], d + t * h[e][1], v) !== m, S = y >= 0 && y >> 1 != m >> 1 && x(y, -1) ? 16 | (y % 2 == 0 ? 32 : 0) : b >= 0 && b >> 1 != m >> 1 && x(b, 1) ? 8 | (b % 2 == 0 ? 32 : 0) : 0, C = (p & -57 | S) >>> 0;
62
+ C !== p && a.push(u, n, d, c(f & 65535, C));
63
+ }
64
+ a.length && e.setBlocks(a);
65
+ }
66
+ var _ = (t, n) => e(t.shape, n >>> 16), v = (e, t, n, r = Infinity) => {
67
+ if (e === void 0) return n;
68
+ let i = r < Infinity ? `0..${r}` : "a finite number >= 0";
69
+ if (typeof e != "number") throw TypeError(`Block type ${t} must be a number; pass ${i}`);
70
+ if (!(e >= 0 && e <= r && Number.isFinite(e))) throw RangeError(`Block type ${t} ${e} is out of range; pass ${i}`);
71
+ return e;
72
+ }, y = (e, t, n) => {
73
+ if (e === void 0) return n;
74
+ if ((e & 15) !== e) throw s(`Block type ${t}`, e, 15, "pass a light level");
75
+ return e;
76
+ }, b = (e, t, n) => {
77
+ if (e === void 0) return n;
78
+ if (typeof e != "boolean") throw TypeError(`Block type ${t} must be true or false`);
79
+ return e;
80
+ }, x = (e) => {
81
+ if (e === void 0) return 0;
82
+ if (!Number.isInteger(e) || e < 0 || e > 2047) throw s("Block type map layer", e, 2047, "pass a texture layer");
83
+ return e;
84
+ }, S = (e, t, n) => {
85
+ if (e === void 0) return n[0];
86
+ if (!n.includes(e)) throw TypeError(`Block type ${t} must be ${n.map((e) => `'${e}'`).join(", ")}`);
87
+ return e;
88
+ }, C = class {
89
+ constructor() {
90
+ this.types = [], this.names = /* @__PURE__ */ new Map(), this.define("air", {
91
+ layer: "invisible",
92
+ solid: !1
93
+ });
94
+ }
95
+ get table() {
96
+ return this.types;
97
+ }
98
+ define(e, t = {}) {
99
+ if (typeof e != "string") throw TypeError("Block type name must be a string such as \"stone\"");
100
+ if (!e) throw RangeError("Block type name is empty; pass a name such as \"stone\"");
101
+ if (this.names.has(e)) throw new r("state", `Block type "${e}" is already defined; use blocks.id("${e}") or a new name`);
102
+ if (this.types.length > 65535) throw RangeError("Block types are full (65,536 ids); reuse a type with data");
103
+ if (!t || typeof t != "object") throw TypeError("Block type options must be an object");
104
+ let o = t, c = S(o.layer, "layer", i), l = S(o.shape, "shape", a), u = o.animation ?? {
105
+ frames: 1,
106
+ fps: 0
107
+ };
108
+ if (l === "model" != !!o.model?.length) throw TypeError("Model shape needs prepared boxes; use defineBlockModel from @margoengine/voxel/models");
109
+ if (!u || typeof u != "object") throw TypeError("Block type animation must be {frames, fps}");
110
+ let d = u.frames;
111
+ if (!Number.isInteger(d) || d < 1 || d > 2048) throw s("Block type animation.frames", d, 2048, "pass 1..2048 texture layers");
112
+ let f = o.textures ?? 0, p = typeof f == "number" ? [
113
+ f,
114
+ f,
115
+ f,
116
+ f,
117
+ f,
118
+ f
119
+ ] : f;
120
+ if (!Array.isArray(p) || p.length !== 6) throw TypeError("Block type textures must be a layer or 6 layers (+x, -x, +y, -y, +z, -z)");
121
+ for (let e of p) if (!Number.isInteger(e) || e < 0 || e + d > 2048) throw s("Block type texture layer", e, 2048 - d, `${d} frame(s) must fit 2048 layers`);
122
+ let m = [
123
+ 1,
124
+ 1,
125
+ 1
126
+ ];
127
+ o.color !== void 0 && n(o.color, m);
128
+ let h = this.types.length;
129
+ return this.types.push({
130
+ id: h,
131
+ name: e,
132
+ textures: [...p],
133
+ layer: c,
134
+ shape: l,
135
+ ...o.model ? { model: o.model } : {},
136
+ ...o.vertexMotion ? { vertexMotion: { ...o.vertexMotion } } : {},
137
+ solid: b(o.solid, "solid", !0),
138
+ liquid: b(o.liquid, "liquid", !1),
139
+ light: y(o.light, "light", 0),
140
+ opacity: y(o.opacity, "opacity", c === "opaque" && l === "cube" ? 15 : 0),
141
+ color: m,
142
+ roughness: v(o.roughness, "roughness", 1, 1),
143
+ metalness: v(o.metalness, "metalness", 0, 1),
144
+ emissive: v(o.emissive, "emissive", 0),
145
+ animation: {
146
+ frames: d,
147
+ fps: v(u.fps, "animation.fps", 0)
148
+ },
149
+ maps: {
150
+ normal: x(o.maps?.normal),
151
+ roughness: x(o.maps?.roughness),
152
+ emissive: x(o.maps?.emissive)
153
+ },
154
+ transmission: v(o.transmission, "transmission", 0, 1),
155
+ scatter: v(o.scatter, "scatter", 0, 1),
156
+ waterlog: this.waterlogOf(o.waterlog, l)
157
+ }), this.names.set(e, h), h;
158
+ }
159
+ waterlogOf(e, t) {
160
+ if (e === void 0) return 0;
161
+ if (t !== "slab" && t !== "stair") throw TypeError(`Block type waterlog applies to slabs and stairs, not a '${t}'; drop it or change shape`);
162
+ if (typeof e != "string" && typeof e != "number") throw TypeError(`Block type waterlog must be a liquid's id or name, not ${typeof e}`);
163
+ let n = typeof e == "string" ? this.names.get(e) ?? -1 : e, r = Number.isInteger(n) ? this.types[n] : void 0;
164
+ if (!r || r.shape !== "liquid") throw RangeError(`Block type waterlog ${e} is not a defined 'liquid' type; define the liquid first`);
165
+ return n;
166
+ }
167
+ id(e) {
168
+ let t = this.names.get(e);
169
+ if (t === void 0) throw RangeError(`Block type "${e}" is not defined; define it with blocks.define()`);
170
+ return t;
171
+ }
172
+ get(e) {
173
+ let t = Number.isInteger(e) ? this.types[e] : void 0;
174
+ if (!t) throw RangeError(`Block id ${e} is not defined; define it with blocks.define()`);
175
+ return t;
176
+ }
177
+ };
178
+ //#endregion
179
+ export { g as a, _ as c, m as d, c as i, d as l, u as n, f as o, l as r, s, C as t, p as u };
@@ -0,0 +1,220 @@
1
+ import type { ColorInput, Vec3 } from '@margoengine/math';
2
+ import type { VoxelWorld } from './world.js';
3
+ import type { ModelOrientation } from './model-data.js';
4
+ import type { VoxelBlockMotion } from './wind-data.js';
5
+ /**
6
+ * Render pass of a block type: `'opaque'` (back-face culled, hides its neighbours' faces), `'cutout'` (alpha
7
+ * tested, e.g. leaves), `'translucent'` (sorted and blended, e.g. glass and water) or `'invisible'` (never drawn, e.g. air).
8
+ */
9
+ export type BlockLayer = 'opaque' | 'cutout' | 'translucent' | 'invisible';
10
+ /**
11
+ * Geometry of a block type: a full `'cube'`, two diagonal quads (`'cross'`, e.g. grass and flowers), a `'liquid'`, a
12
+ * half-height `'slab'` or a `'stair'`. A slab fills the lower half of its cell, the upper half when bit 2 of its
13
+ * state data is set, or both halves when bit 3 is ({@link slabState}, {@link doubleSlabState}). A stair is a lower
14
+ * slab plus the upper quarter on its back side: state data bits 0-1 name that side (0 +x, 1 -x, 2 +z, 3 -z), bit 2
15
+ * turns it upside down, bits 3-4 make it an inner (1) or outer (2) corner toward the side bit 5 names on the other
16
+ * horizontal axis (0 its -, 1 its +), and bit 6 marks a corner set by hand ({@link stairState},
17
+ * {@link connectStairs}). Bit 15 of either waterlogs it ({@link waterlogState}). Slabs and stairs are meshed, lit,
18
+ * shadowed, collided and picked as their half-block boxes ({@link shapeOctants}).
19
+ */
20
+ export type BlockShape = 'cube' | 'cross' | 'liquid' | 'slab' | 'stair' | 'model' | 'object';
21
+ /** Side a stair's full-height back faces: `'+x'`, `'-x'`, `'+z'` or `'-z'`. */
22
+ export type StairFacing = '+x' | '-x' | '+z' | '-z';
23
+ /**
24
+ * A stair's corner: `'straight'`, or an inner or outer corner toward a side on the horizontal axis across its back
25
+ * (`'inner+z'` for a stair facing `'+x'` keeps its whole back and adds the front quarter on the +z side; `'outer+z'`
26
+ * keeps only the back quarter on the +z side).
27
+ */
28
+ export type StairCorner = 'straight' | 'inner+x' | 'inner-x' | 'inner+z' | 'inner-z' | 'outer+x' | 'outer-x' | 'outer+z' | 'outer-z';
29
+ /** Options of {@link BlockTypes.define}. Every field is optional. */
30
+ export interface BlockTypeOptions {
31
+ /** Prepared optional vertex motion; use defineWindBlock from the wind entry. Default none. */
32
+ vertexMotion?: VoxelBlockMotion;
33
+ /** Prepared box model; use defineBlockModel from the optional models entry. Default none. */
34
+ model?: readonly ModelOrientation[];
35
+ /**
36
+ * Texture layer (0-2047) of every face, or one per face in the order +x, -x, +y, -y, +z, -z (`[side, side, top,
37
+ * bottom, side, side]`). Default 0.
38
+ */
39
+ textures?: number | readonly [number, number, number, number, number, number];
40
+ /** Render pass. Default `'opaque'`. */
41
+ layer?: BlockLayer;
42
+ /** Geometry. Default `'cube'`. */
43
+ shape?: BlockShape;
44
+ /** Collides with bodies and characters. Default true. */
45
+ solid?: boolean;
46
+ /** Characters swim in it and its faces against the same liquid are hidden. Default false. */
47
+ liquid?: boolean;
48
+ /** Block light it emits, 0-15. Default 0. */
49
+ light?: number;
50
+ /** Light it absorbs, 0-15 (15 blocks light). Default 15 for an opaque cube, 0 otherwise. */
51
+ opacity?: number;
52
+ /** Tint multiplied with the texture: `'#rrggbb'`, `0xrrggbb` or linear `[r, g, b]`. Default white. */
53
+ color?: ColorInput;
54
+ /** Perceptual roughness, 0-1. Default 1. */
55
+ roughness?: number;
56
+ /** Metalness, 0-1. Default 0. */
57
+ metalness?: number;
58
+ /** Emitted light as a multiple of the textured colour, >= 0. Default 0. */
59
+ emissive?: number;
60
+ /** Animated texture: `frames` consecutive layers from each face's layer (1-2048), played at `fps` (>= 0). */
61
+ animation?: {
62
+ frames: number;
63
+ fps: number;
64
+ };
65
+ /**
66
+ * Texture layers (1-2047) of a tangent-space normal map (glTF convention: +y up the image), a roughness map (red)
67
+ * and an emissive map (red, times `emissive`). 0 or absent: none, the scalar values apply. Default none.
68
+ */
69
+ maps?: {
70
+ normal?: number;
71
+ roughness?: number;
72
+ emissive?: number;
73
+ };
74
+ /**
75
+ * Translucent blocks only: how much of the block's own colour shows through, 0-1. It lowers alpha by up to 85% and
76
+ * mixes in the base colour: a cheap look, not refraction. Default 0.
77
+ */
78
+ transmission?: number;
79
+ /** Translucent blocks only: interior scatter, 0-1; 1 halves the see-through part of `transmission`. Default 0. */
80
+ scatter?: number;
81
+ /**
82
+ * Slabs and stairs only: the liquid (a defined `'liquid'` type's id or name) that fills the rest of the cell when a
83
+ * state is waterlogged ({@link waterlogState}). Default none: the waterlogged bit is ignored.
84
+ */
85
+ waterlog?: number | string;
86
+ }
87
+ /** A defined block type with every option resolved; `color` is linear RGB. Plain, structured-cloneable data. */
88
+ export interface BlockType {
89
+ /** Optional vertex motion, copied as worker-safe data. */
90
+ readonly vertexMotion?: VoxelBlockMotion;
91
+ /** Prepared orientations for a custom box model, absent on built-in shapes. */
92
+ readonly model?: readonly ModelOrientation[];
93
+ /** Its id: the low 16 bits of a block state. */
94
+ readonly id: number;
95
+ /** Its unique name. */
96
+ readonly name: string;
97
+ /** Texture layer per face, +x, -x, +y, -y, +z, -z. */
98
+ readonly textures: readonly [number, number, number, number, number, number];
99
+ /** Render pass. */
100
+ readonly layer: BlockLayer;
101
+ /** Geometry. */
102
+ readonly shape: BlockShape;
103
+ /** Collides with bodies and characters. */
104
+ readonly solid: boolean;
105
+ /** Characters swim in it. */
106
+ readonly liquid: boolean;
107
+ /** Block light it emits, 0-15. */
108
+ readonly light: number;
109
+ /** Light it absorbs, 0-15. */
110
+ readonly opacity: number;
111
+ /** Linear RGB tint. */
112
+ readonly color: Vec3;
113
+ /** Perceptual roughness, 0-1. */
114
+ readonly roughness: number;
115
+ /** Metalness, 0-1. */
116
+ readonly metalness: number;
117
+ /** Emitted light as a multiple of the textured colour. */
118
+ readonly emissive: number;
119
+ /** Texture animation; `frames` 1 means none. */
120
+ readonly animation: {
121
+ readonly frames: number;
122
+ readonly fps: number;
123
+ };
124
+ /** Normal, roughness and emissive map layers; 0 is none. */
125
+ readonly maps: {
126
+ readonly normal: number;
127
+ readonly roughness: number;
128
+ readonly emissive: number;
129
+ };
130
+ /** See-through part of a translucent block, 0-1. */
131
+ readonly transmission: number;
132
+ /** Interior scatter of a translucent block, 0-1. */
133
+ readonly scatter: number;
134
+ /** Id of the liquid a waterlogged state of this slab or stair holds; 0 for none. */
135
+ readonly waterlog: number;
136
+ }
137
+ export declare const integerError: (what: string, value: unknown, max: number, fix: string) => Error;
138
+ /**
139
+ * A block state: `id` in the low 16 bits and `state` (orientation, growth, water level: the block type's own meaning)
140
+ * in the high 16, as an unsigned 32-bit integer. A state of id `n` and data 0 is `n` itself, so block ids are states.
141
+ * @throws TypeError for a non-number. @throws RangeError when `id` or `state` is not an integer in 0..65535.
142
+ */
143
+ export declare function blockState(id: number, state?: number): number;
144
+ /** The block type id of block state `s` (its low 16 bits). */
145
+ export declare const blockId: (s: number) => number;
146
+ /** The data of block state `s` (its high 16 bits). */
147
+ export declare const blockData: (s: number) => number;
148
+ /**
149
+ * The block state of slab block `id`: its lower half, or its upper half when `top`.
150
+ * @throws TypeError for a non-number id. @throws RangeError when `id` is not an integer in 0..65535.
151
+ */
152
+ export declare const slabState: (id: number, top?: boolean) => number;
153
+ /**
154
+ * The block state of slab block `id` filling both halves of its cell: one block of the slab's material, shaped and
155
+ * lit like a cube.
156
+ * @throws TypeError for a non-number id. @throws RangeError when `id` is not an integer in 0..65535.
157
+ */
158
+ export declare const doubleSlabState: (id: number) => number;
159
+ /**
160
+ * The block state of stair block `id` with its full-height back toward `facing`, upside down (the slab on top) when
161
+ * `upsideDown`. Without `corner` it is straight and {@link connectStairs} may turn it into a corner; with one (even
162
+ * `'straight'`) that corner is kept.
163
+ * @throws TypeError for a non-number id, a facing other than `'+x'`, `'-x'`, `'+z'`, `'-z'`, or a corner that is not a
164
+ * {@link StairCorner} or whose side is not across `facing` (`'inner+x'` for a stair facing `'+x'`).
165
+ * @throws RangeError when `id` is not an integer in 0..65535.
166
+ */
167
+ export declare function stairState(id: number, facing: StairFacing, upsideDown?: boolean, corner?: StairCorner): number;
168
+ /**
169
+ * Block state `state` waterlogged (the slab's or stair's `waterlog` liquid fills the rest of its cell), or not when
170
+ * `wet` is false: bit 15 of its data.
171
+ * @throws TypeError for a non-number. @throws RangeError when `state` is not an unsigned 32-bit integer.
172
+ */
173
+ export declare function waterlogState(state: number, wet?: boolean): number;
174
+ /**
175
+ * Turns the stairs at cell (x, y, z) of `world` and beside it on x and z into the corners their neighbours call for:
176
+ * a stair whose back meets the side of another stair of the same half becomes an outer corner toward that stair's
177
+ * back, else one whose front meets such a stair becomes an inner corner toward it, else straight. A turn is refused
178
+ * when the cell on the far side of the turn (away from the back neighbour's back, toward the front neighbour's back)
179
+ * holds a stair of the same facing and half, so a straight run stays straight. Stairs placed with
180
+ * an explicit corner ({@link stairState}) keep it. Call it after placing or removing a block; it edits only cells that
181
+ * change, through `world.setBlocks`.
182
+ * @throws TypeError when `world` is not a VoxelWorld. @throws RangeError when a coordinate is not a 32-bit integer.
183
+ */
184
+ export declare function connectStairs(world: VoxelWorld, x: number, y: number, z: number): void;
185
+ /**
186
+ * The half-block cells block state `state` of `type` fills, as an 8-bit mask: bit `x + 2y + 4z` for the octant at
187
+ * (x, y, z), each 0 for the cell's low half on that axis and 1 for its high half. 255 for a cube (of any layer), the
188
+ * filled octants of a slab or stair, 0 for a cross or a liquid. Collision, picking, light and the path tracer's shapes
189
+ * use these boxes.
190
+ */
191
+ export declare const shapeOctants: (type: BlockType, state: number) => number;
192
+ /**
193
+ * The block types of a world. Id 0 is `'air'` (invisible, not solid, passes light); {@link BlockTypes.define} assigns
194
+ * the next ids in order, so define types in the same order wherever saved states are read back.
195
+ */
196
+ export declare class BlockTypes {
197
+ private readonly types;
198
+ private readonly names;
199
+ constructor();
200
+ /** Every defined type, indexed by id: plain data to post to workers. */
201
+ get table(): readonly BlockType[];
202
+ /**
203
+ * Defines block type `name` and returns its id (also its plain block state).
204
+ * @throws TypeError for a non-string name or an option of the wrong type.
205
+ * @throws RangeError for an empty name, an option out of range, or more than 65,536 types.
206
+ * @throws MargoError `'state'` when `name` is already defined.
207
+ */
208
+ define(name: string, options?: BlockTypeOptions): number;
209
+ private waterlogOf;
210
+ /**
211
+ * The id of block type `name`.
212
+ * @throws RangeError when no type has that name.
213
+ */
214
+ id(name: string): number;
215
+ /**
216
+ * The block type with id `id` (the {@link blockId} of a state).
217
+ * @throws RangeError when no type has that id.
218
+ */
219
+ get(id: number): BlockType;
220
+ }
@@ -0,0 +1,9 @@
1
+ import type { MeshInput } from './mesher.js';
2
+ /**
3
+ * The shell's whole box faces that touch the chunk, one word each: local cell x + 1 at bits 0-5, y + 1 at 6-11,
4
+ * z + 1 at 12-17 (each 0-33), face 18-20 (0-5: +x, -x, +y, -y, +z, -z), layer 21-22 (opaque, cutout, translucent).
5
+ * A face is listed when the mesher of the shell cell's own chunk meshes it whole (the faces a bevel chamfers; a face
6
+ * partly covered is trimmed, and a slab's or stair's mid-cell face inset): the cell fills all four octants on the
7
+ * face's side, and no opaque octant of the front cell, nor an octant of the same block there, touches the face.
8
+ */
9
+ export declare function meshBorder({ chunk, shell, blocks }: MeshInput): Uint32Array;
@@ -0,0 +1,158 @@
1
+ import { s as e } from "./blocks-BabtpQT-.js";
2
+ //#region src/chunk.ts
3
+ var t = 32, n = 32768, r = (e) => e <= 1 ? 0 : e <= 2 ? 1 : e <= 4 ? 2 : e <= 16 ? 4 : e <= 256 ? 8 : 16, i = (e) => e ? Math.min(1 << e, n) : 1, a = (e, t, n) => [
4
+ e,
5
+ t,
6
+ n
7
+ ].every((e) => typeof e == "number") ? /* @__PURE__ */ RangeError(`Chunk cell (${e}, ${t}, ${n}) is not in 0..31; pass local coordinates (cell & 31)`) : /* @__PURE__ */ TypeError("Chunk cell coordinates must be numbers"), o = (t) => e("Block state", t, 4294967295, "build it with blockState(id, data)"), s = (e) => /* @__PURE__ */ RangeError(`PackedChunk ${e}; pass the result of chunk.pack()`);
8
+ function c(e, t, r, i) {
9
+ if (!r) return null;
10
+ let a = new Uint32Array(1024 * r), o = (1 << e) - 1;
11
+ if ((!e || !t) && !i?.[0]) return a;
12
+ for (let s = 0; s < n; s++) {
13
+ let n = e && t ? t[s * e >>> 5] >>> (s * e & 31) & o : 0;
14
+ i && (n = i[n]), n && (a[s * r >>> 5] |= n << (s * r & 31));
15
+ }
16
+ return a;
17
+ }
18
+ var l, u, d, f, p, m, h = class t {
19
+ static {
20
+ p = (e) => e.lightRevision, l = (e, t, r) => {
21
+ let i = /* @__PURE__ */ new Set();
22
+ for (let n = 0; n < r; n++) e.find(t[n]) < 0 && i.add(t[n]);
23
+ let a = e.size + i.size - e.free.length;
24
+ a > e.palette.length && e.palette.length < n && e.grow(a);
25
+ }, u = (e) => {
26
+ e.bits && (e.live === 1 || r(2 * e.live) < e.bits) && e.compact();
27
+ }, d = (e) => e.bits ? void 0 : e.palette[0], f = (e) => e.palette.subarray(0, e.size), m = (e, t) => {
28
+ e.lightRevision++, typeof t == "number" ? (e.lightByte = t, e.lightCells = null) : e.lightCells = t;
29
+ };
30
+ }
31
+ constructor(e = 0) {
32
+ this.palette = /* @__PURE__ */ new Uint32Array(1), this.counts = Uint16Array.of(n), 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);
33
+ }
34
+ get(e, t, n) {
35
+ if ((e & 31) !== e || (t & 31) !== t || (n & 31) !== n) throw a(e, t, n);
36
+ return this.palette[this.slot(e | n << 5 | t << 10)];
37
+ }
38
+ set(e, t, n, r) {
39
+ if ((e & 31) !== e || (t & 31) !== t || (n & 31) !== n) throw a(e, t, n);
40
+ if (r >>> 0 !== r) throw o(r);
41
+ let i = e | n << 5 | t << 10, s = this.slot(i), c = this.palette[s];
42
+ if (c === r) return c;
43
+ let l = this.find(r);
44
+ if (l < 0 && this.counts[s] === 1) return this.lookup?.delete(c), this.palette[s] = r, this.lookup?.set(r, s), c;
45
+ l < 0 && (l = this.add(r));
46
+ let u = this.bits, d = i * u, f = d >>> 5, p = d & 31, m = this.words;
47
+ return m[f] = m[f] & ~((1 << u) - 1 << p) | l << p, this.counts[l]++, --this.counts[s] === 0 && this.release(s), c;
48
+ }
49
+ fill(e) {
50
+ if (e >>> 0 !== e) throw o(e);
51
+ this.palette = Uint32Array.of(e), this.counts = Uint16Array.of(n), this.size = this.live = 1, this.free = [], this.lookup = null, this.bits = 0, this.words = null;
52
+ }
53
+ getLight(e, t, n) {
54
+ if ((e & 31) !== e || (t & 31) !== t || (n & 31) !== n) throw a(e, t, n);
55
+ return this.lightCells ? this.lightCells[e | n << 5 | t << 10] : this.lightByte;
56
+ }
57
+ setLight(t, r, i, o) {
58
+ if ((t & 31) !== t || (r & 31) !== r || (i & 31) !== i) throw a(t, r, i);
59
+ if ((o & 255) !== o) throw e("Light byte", o, 255, "pass sky << 4 | block");
60
+ if (this.getLight(t, r, i) !== o) {
61
+ if (this.lightRevision++, !this.lightCells) {
62
+ if (o === this.lightByte) return;
63
+ this.lightCells = new Uint8Array(n).fill(this.lightByte);
64
+ }
65
+ this.lightCells[t | i << 5 | r << 10] = o;
66
+ }
67
+ }
68
+ fillLight(t) {
69
+ if ((t & 255) !== t) throw e("Light byte", t, 255, "pass sky << 4 | block");
70
+ (this.lightCells || this.lightByte !== t) && this.lightRevision++, this.lightByte = t, this.lightCells = null;
71
+ }
72
+ get byteLength() {
73
+ return this.palette.byteLength + this.counts.byteLength + (this.words?.byteLength ?? 0) + (this.lightCells?.byteLength ?? 0);
74
+ }
75
+ pack() {
76
+ return {
77
+ palette: this.palette.slice(0, this.size),
78
+ bits: this.bits,
79
+ words: this.words ? this.words.slice() : /* @__PURE__ */ new Uint32Array(),
80
+ light: this.lightCells ? this.lightCells.slice() : this.lightByte
81
+ };
82
+ }
83
+ static unpack(e) {
84
+ if (!e || typeof e != "object") throw TypeError("PackedChunk must be the object chunk.pack() returns");
85
+ let { palette: a, bits: o, words: c, light: l } = e;
86
+ if (!(a instanceof Uint32Array) || !(c instanceof Uint32Array)) throw TypeError("PackedChunk palette and words must be Uint32Arrays; pass the result of chunk.pack()");
87
+ if (![
88
+ 0,
89
+ 1,
90
+ 2,
91
+ 4,
92
+ 8,
93
+ 16
94
+ ].includes(o)) throw s(`bits ${o} is not 0, 1, 2, 4, 8 or 16`);
95
+ let u = i(o);
96
+ if (a.length < 1 || a.length > u) throw s(`palette has ${a.length} entries where ${o}-bit indices allow 1..${u}`);
97
+ if (c.length !== 1024 * o) throw s(`words has ${c.length} entries, not ${1024 * o}`);
98
+ if (typeof l == "number") {
99
+ if ((l & 255) !== l) throw s(`light byte ${l} is not in 0..255`);
100
+ } else if (!(l instanceof Uint8Array)) throw TypeError("PackedChunk light must be a byte or a Uint8Array");
101
+ else if (l.length !== n) throw s(`light has ${l.length} bytes, not ${n}`);
102
+ let d = new t(), f = new Uint16Array(u), p = (1 << o) - 1;
103
+ if (!o) f[0] = n;
104
+ else for (let e = 0; e < n; e++) {
105
+ let t = c[e * o >>> 5] >>> (e * o & 31) & p;
106
+ if (t >= a.length) throw s(`cell ${e} uses palette entry ${t} of ${a.length}`);
107
+ f[t]++;
108
+ }
109
+ d.palette = new Uint32Array(u), d.palette.set(a), d.counts = f, d.size = a.length, d.bits = o, d.words = o ? c.slice() : null, d.lightByte = typeof l == "number" ? l : 0, d.lightCells = typeof l == "number" ? null : l.slice();
110
+ let m = /* @__PURE__ */ new Set(), h = 0;
111
+ for (let e = 0; e < a.length; e++) f[e] ? (h++, m.add(a[e])) : d.free.push(e);
112
+ if (d.live = h, m.size < h || h === 1 || r(2 * h) < o) d.compact();
113
+ else if (u > 16) {
114
+ d.lookup = /* @__PURE__ */ new Map();
115
+ for (let e = 0; e < a.length; e++) f[e] && d.lookup.set(a[e], e);
116
+ }
117
+ return d;
118
+ }
119
+ slot(e) {
120
+ let t = this.bits, n = e * t;
121
+ return t ? this.words[n >>> 5] >>> (n & 31) & (1 << t) - 1 : 0;
122
+ }
123
+ find(e) {
124
+ if (this.lookup) return this.lookup.get(e) ?? -1;
125
+ let { palette: t, counts: n, size: r } = this;
126
+ for (let i = 0; i < r; i++) if (t[i] === e && n[i]) return i;
127
+ return -1;
128
+ }
129
+ grow(e) {
130
+ let t = r(e), n = i(t), a = new Uint32Array(n), o = new Uint16Array(n);
131
+ if (a.set(this.palette), o.set(this.counts), this.words = c(this.bits, this.words, t, null), this.palette = a, this.counts = o, this.bits = t, n > 16 && !this.lookup) {
132
+ this.lookup = /* @__PURE__ */ new Map();
133
+ for (let e = 0; e < this.size; e++) o[e] && this.lookup.set(a[e], e);
134
+ }
135
+ }
136
+ add(e) {
137
+ !this.free.length && this.size === this.palette.length && this.grow(this.size + 1);
138
+ let t = this.free.pop() ?? this.size++;
139
+ return this.palette[t] = e, this.lookup?.set(e, t), this.live++, t;
140
+ }
141
+ release(e) {
142
+ this.lookup?.delete(this.palette[e]), this.free.push(e), (--this.live === 1 || r(2 * this.live) < this.bits) && this.compact();
143
+ }
144
+ compact() {
145
+ let { palette: e, counts: t, size: n } = this, a = new Uint16Array(n), o = /* @__PURE__ */ new Map();
146
+ for (let r = 0; r < n; r++) if (t[r]) {
147
+ let t = o.get(e[r]);
148
+ t === void 0 && o.set(e[r], t = o.size), a[r] = t;
149
+ }
150
+ let s = o.size, l = s === 1 ? 0 : Math.min(this.bits, r(2 * s)), u = i(l);
151
+ this.palette = new Uint32Array(u), this.counts = new Uint16Array(u);
152
+ for (let [e, t] of o) this.palette[t] = e;
153
+ for (let e = 0; e < n; e++) t[e] && (this.counts[a[e]] += t[e]);
154
+ this.words = c(this.bits, this.words, l, a), this.size = this.live = s, this.free = [], this.bits = l, this.lookup = u > 16 ? o : null;
155
+ }
156
+ };
157
+ //#endregion
158
+ export { l as a, d as c, f as i, m as l, h as n, u as o, p as r, o as s, t };