@margoengine/voxel 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +1671 -3
- package/dist/bevel.d.ts +79 -0
- package/dist/bevel.js +453 -0
- package/dist/blocks-BabtpQT-.js +179 -0
- package/dist/blocks.d.ts +220 -0
- package/dist/border.d.ts +9 -0
- package/dist/chunk-BCoaKSn4.js +158 -0
- package/dist/chunk.d.ts +112 -0
- package/dist/codec-ZZFj1xvt.js +410 -0
- package/dist/codec.d.ts +125 -0
- package/dist/codec.js +1 -0
- package/dist/debris.d.ts +114 -0
- package/dist/debris.js +387 -0
- package/dist/faces-DUR7LhrN.js +146 -0
- package/dist/faces.d.ts +7 -0
- package/dist/fluids.d.ts +74 -0
- package/dist/fluids.js +226 -0
- package/dist/generate.d.ts +78 -0
- package/dist/generate.js +99 -0
- package/dist/gi.d.ts +76 -0
- package/dist/gi.js +264 -0
- package/dist/gpu-mesher.d.ts +77 -0
- package/dist/gpu-mesher.js +461 -0
- package/dist/granular-mpm.d.ts +32 -0
- package/dist/granular-mpm.js +46 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +4 -0
- package/dist/light-rgb.d.ts +71 -0
- package/dist/light-rgb.js +287 -0
- package/dist/light-worker.d.ts +84 -0
- package/dist/light-worker.js +110 -0
- package/dist/light.d.ts +108 -0
- package/dist/light.js +332 -0
- package/dist/light.worker.d.ts +1 -0
- package/dist/lightmap.d.ts +50 -0
- package/dist/lightmap.js +68 -0
- package/dist/lod.d.ts +87 -0
- package/dist/lod.js +182 -0
- package/dist/mesher.d.ts +214 -0
- package/dist/mesher.js +539 -0
- package/dist/mesher.worker.d.ts +1 -0
- package/dist/model-data-D_rIKJCQ.js +41 -0
- package/dist/model-data.d.ts +21 -0
- package/dist/model-mesh-C4Gi6bMM.js +122 -0
- package/dist/model-mesh.d.ts +21 -0
- package/dist/model-surface.d.ts +5 -0
- package/dist/models.d.ts +74 -0
- package/dist/models.js +308 -0
- package/dist/objects.d.ts +73 -0
- package/dist/objects.js +152 -0
- package/dist/octants-C_Ic7gdn.js +10 -0
- package/dist/octants.d.ts +16 -0
- package/dist/origin-DsMelUxs.js +23 -0
- package/dist/origin.d.ts +7 -0
- package/dist/physics.d.ts +147 -0
- package/dist/physics.js +486 -0
- package/dist/query.d.ts +166 -0
- package/dist/query.js +305 -0
- package/dist/render-post.d.ts +6 -0
- package/dist/render.d.ts +296 -0
- package/dist/render.js +1613 -0
- package/dist/shared/borrowed.d.ts +44 -0
- package/dist/smooth-CEwHkXli.js +968 -0
- package/dist/smooth-clip.d.ts +24 -0
- package/dist/smooth-lod.d.ts +118 -0
- package/dist/smooth-lod.js +244 -0
- package/dist/smooth.d.ts +171 -0
- package/dist/smooth.js +1 -0
- package/dist/stream.d.ts +201 -0
- package/dist/stream.js +402 -0
- package/dist/terrain.worker.d.ts +1 -0
- package/dist/textures.d.ts +86 -0
- package/dist/ticks.d.ts +126 -0
- package/dist/ticks.js +239 -0
- package/dist/unit-light-Ch7roJgB.js +7 -0
- package/dist/vox.d.ts +237 -0
- package/dist/vox.js +780 -0
- package/dist/wind-data.d.ts +13 -0
- package/dist/wind.d.ts +68 -0
- package/dist/wind.js +170 -0
- package/dist/worker-DlGRxlc4.js +44 -0
- package/dist/worker-pool-sPyC6jCE.js +93 -0
- package/dist/workers/light.js +1 -0
- package/dist/workers/mesher.js +1 -0
- package/dist/workers/terrain.js +1 -0
- package/dist/world-CelTlft2.js +153 -0
- package/dist/world.d.ts +117 -0
- package/package.json +175 -6
package/dist/lod.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { c as e, n as t } from "./chunk-BCoaKSn4.js";
|
|
2
|
+
import { n, t as r } from "./world-CelTlft2.js";
|
|
3
|
+
import { Noise as i, fbm as a } from "@margoengine/procedural/noise";
|
|
4
|
+
//#region src/lod.ts
|
|
5
|
+
var o = (e, t, n, r) => {
|
|
6
|
+
if (typeof t != "number") throw TypeError(`VoxelLod ${e} must be a number`);
|
|
7
|
+
if (!(t === r || Number.isInteger(t) && t >= n && t <= r)) throw RangeError(`VoxelLod ${e} is ${t}; pass an integer in ${n}..${r}`);
|
|
8
|
+
return t;
|
|
9
|
+
}, s = (e, t, n) => Math.max(t - e, e - (t + n), 0), c = class {
|
|
10
|
+
#e;
|
|
11
|
+
#t;
|
|
12
|
+
#n;
|
|
13
|
+
#r;
|
|
14
|
+
#i;
|
|
15
|
+
#a;
|
|
16
|
+
#o = /* @__PURE__ */ new Set();
|
|
17
|
+
#s;
|
|
18
|
+
#c = 0;
|
|
19
|
+
#l = !1;
|
|
20
|
+
constructor(e) {
|
|
21
|
+
if (!e || typeof e != "object") throw TypeError("VoxelLod expects {world, generate}");
|
|
22
|
+
let { world: t, generate: i, levels: a = 4, radius: s = 8, minY: c = -64, maxY: l = 255, budget: u = 8 } = e;
|
|
23
|
+
if (!(t instanceof r)) throw TypeError("VoxelLod world must be the real VoxelWorld");
|
|
24
|
+
if (typeof i != "function") throw TypeError("VoxelLod generate must be a function, such as noiseTerrainLod(terrainOptions)");
|
|
25
|
+
if (o("levels", a, 1, 12), this.#n = o("radius", s, 2, 32), this.#r = o("minY", c, -32768, 32767), this.#i = o("maxY", l, -32768, 32767), c > l) throw RangeError(`VoxelLod minY ${c} is above maxY ${l}; pass minY <= maxY`);
|
|
26
|
+
this.budget = o("budget", u, 1, Infinity), this.#e = t, this.#t = i, this.levels = Array.from({ length: a }, (e, n) => ({
|
|
27
|
+
level: n + 1,
|
|
28
|
+
scale: 2 ** (n + 1),
|
|
29
|
+
world: new r({ blocks: t.blocks })
|
|
30
|
+
})), this.#a = this.levels.map(() => /* @__PURE__ */ new Map()), this.#s = t.onChunk((e, t, r) => {
|
|
31
|
+
let i = n(e >> 1, t >> 1, r >> 1);
|
|
32
|
+
this.#a[0].has(i) && this.#o.add(i);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
get stats() {
|
|
36
|
+
return {
|
|
37
|
+
nodes: this.#a.map((e) => {
|
|
38
|
+
let t = 0;
|
|
39
|
+
for (let n of e.values()) t += +n.shown;
|
|
40
|
+
return t;
|
|
41
|
+
}),
|
|
42
|
+
pending: this.#c + this.#o.size
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
update(e) {
|
|
46
|
+
if (this.#l) throw TypeError("VoxelLod is disposed; create a new VoxelLod");
|
|
47
|
+
let t = Number(e?.[0]), r = Number(e?.[1]), i = Number(e?.[2]);
|
|
48
|
+
if (![
|
|
49
|
+
t,
|
|
50
|
+
r,
|
|
51
|
+
i
|
|
52
|
+
].every(Number.isFinite)) throw TypeError("VoxelLod.update expects the eye as [x, y, z]");
|
|
53
|
+
let a = this.levels.map(() => /* @__PURE__ */ new Map()), o = this.#n, c = this.levels.length, l = (e, c, u, d) => {
|
|
54
|
+
let f = 32 << e, p = Math.max(s(t, c * f, f), s(r, u * f, f), s(i, d * f, f));
|
|
55
|
+
if (e >= 2 && p < o * f / 2) {
|
|
56
|
+
for (let t = 0; t < 8; t++) l(e - 1, 2 * c + (t & 1), 2 * u + (t >> 1 & 1), 2 * d + (t >> 2));
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if ((u + 1) * f <= this.#r || u * f > this.#i) return;
|
|
60
|
+
let m = n(c, u, d);
|
|
61
|
+
a[e - 1].set(m, {
|
|
62
|
+
level: e,
|
|
63
|
+
x: c,
|
|
64
|
+
y: u,
|
|
65
|
+
z: d,
|
|
66
|
+
key: m,
|
|
67
|
+
d: p
|
|
68
|
+
});
|
|
69
|
+
}, u = 32 << c, d = o * u, f = (e) => Math.floor((e - d) / u), p = (e) => Math.floor((e + d) / u), m = Math.max(f(r), Math.floor(this.#r / u)), h = Math.min(p(r), Math.floor(this.#i / u));
|
|
70
|
+
for (let e = f(t); e <= p(t); e++) for (let n = f(i); n <= p(i); n++) for (let a = m; a <= h; a++) Math.max(s(t, e * u, u), s(r, a * u, u), s(i, n * u, u)) < d && l(c, e, a, n);
|
|
71
|
+
let g = [];
|
|
72
|
+
for (let e = 0; e < c; e++) for (let t of a[e].values()) this.#a[e].has(t.key) || g.push(t);
|
|
73
|
+
g.sort((e, t) => e.d - t.d);
|
|
74
|
+
let _ = 0;
|
|
75
|
+
for (let e of g) {
|
|
76
|
+
if (_ >= this.budget) break;
|
|
77
|
+
this.#u(e), _++;
|
|
78
|
+
}
|
|
79
|
+
for (let e of this.#o) {
|
|
80
|
+
if (_ >= this.budget) break;
|
|
81
|
+
this.#o.delete(e);
|
|
82
|
+
let t = this.#a[0].get(e);
|
|
83
|
+
t && this.#d(1, t), _++;
|
|
84
|
+
}
|
|
85
|
+
let v = g.filter((e) => !this.#a[e.level - 1].has(e.key));
|
|
86
|
+
this.#c = v.length;
|
|
87
|
+
for (let e = 0; e < c; e++) {
|
|
88
|
+
let t = 32 << e + 1;
|
|
89
|
+
for (let [n, r] of this.#a[e]) {
|
|
90
|
+
if (a[e].has(n)) continue;
|
|
91
|
+
let i = r.x * t, o = r.y * t, s = r.z * t;
|
|
92
|
+
v.some((e) => {
|
|
93
|
+
let n = 32 << e.level;
|
|
94
|
+
return e.x * n < i + t && i < (e.x + 1) * n && e.y * n < o + t && o < (e.y + 1) * n && e.z * n < s + t && s < (e.z + 1) * n;
|
|
95
|
+
}) || (this.#a[e].delete(n), this.#o.delete(n), r.shown && this.levels[e].world.removeChunk(r.x, r.y, r.z));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return _;
|
|
99
|
+
}
|
|
100
|
+
#u({ level: n, x: r, y: i, z: a, key: o }) {
|
|
101
|
+
let s = new t(), c = 2 ** n;
|
|
102
|
+
this.#t(s, r, i, a, c, { blocks: this.#e.blocks.table });
|
|
103
|
+
let l = {
|
|
104
|
+
x: r,
|
|
105
|
+
y: i,
|
|
106
|
+
z: a,
|
|
107
|
+
cells: e(s) === 0 ? void 0 : s,
|
|
108
|
+
shown: !1
|
|
109
|
+
};
|
|
110
|
+
this.#a[n - 1].set(o, l), this.#d(n, l);
|
|
111
|
+
}
|
|
112
|
+
#d(n, r) {
|
|
113
|
+
let i = this.levels[n - 1].world, { x: a, y: o, z: s } = r, c = r.cells;
|
|
114
|
+
if (c && n === 1) {
|
|
115
|
+
let n;
|
|
116
|
+
for (let e = 0; e < 8; e++) {
|
|
117
|
+
let r = e & 1, i = e >> 1 & 1, l = e >> 2;
|
|
118
|
+
if (this.#e.getChunk(2 * a + r, 2 * o + i, 2 * s + l)) {
|
|
119
|
+
n ??= t.unpack(c.pack());
|
|
120
|
+
for (let e = 0; e < 16; e++) for (let t = 0; t < 16; t++) for (let a = 0; a < 16; a++) n.set(16 * r + a, 16 * i + e, 16 * l + t, 0);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
n && (c = e(n) === 0 ? void 0 : n);
|
|
124
|
+
}
|
|
125
|
+
r.shown && i.removeChunk(a, o, s), r.shown = !!c, c && i.addChunk(a, o, s, c === r.cells ? t.unpack(c.pack()) : c);
|
|
126
|
+
}
|
|
127
|
+
dispose() {
|
|
128
|
+
if (!this.#l) {
|
|
129
|
+
this.#l = !0, this.#s();
|
|
130
|
+
for (let e = 0; e < this.levels.length; e++) {
|
|
131
|
+
for (let t of this.#a[e].values()) t.shown && this.levels[e].world.removeChunk(t.x, t.y, t.z);
|
|
132
|
+
this.#a[e].clear();
|
|
133
|
+
}
|
|
134
|
+
this.#o.clear();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
[Symbol.dispose]() {
|
|
138
|
+
this.dispose();
|
|
139
|
+
}
|
|
140
|
+
}, l = [
|
|
141
|
+
"stone",
|
|
142
|
+
"dirt",
|
|
143
|
+
"grass",
|
|
144
|
+
"sand",
|
|
145
|
+
"water"
|
|
146
|
+
];
|
|
147
|
+
function u(e = {}) {
|
|
148
|
+
if (!e || typeof e != "object") throw TypeError("noiseTerrainLod options must be an object");
|
|
149
|
+
let t = (t, n, r, i) => {
|
|
150
|
+
let a = e[t] ?? n;
|
|
151
|
+
if (typeof a != "number") throw TypeError(`NoiseTerrainOptions.${t} must be a number`);
|
|
152
|
+
if (!r(a)) throw RangeError(`NoiseTerrainOptions.${t} is ${a}; pass ${i}`);
|
|
153
|
+
return a;
|
|
154
|
+
}, n = Number.isFinite, r = t("seed", 1, (e) => (e | 0) === e, "an integer in -2^31..2^31-1"), o = t("seaLevel", 0, (e) => (e | 0) === e, "an integer cell y"), s = t("height", 8, n, "a finite number"), c = t("amplitude", 32, (e) => e >= 0 && n(e), "a finite number >= 0"), u = t("scale", 192, (e) => e > 0 && n(e), "a finite number > 0"), d = e.blocks ?? {}, f = l.map((e) => {
|
|
155
|
+
let t = d[e] ?? e;
|
|
156
|
+
if (typeof t != "string") throw TypeError(`NoiseTerrainOptions.blocks.${e} must be a block type name`);
|
|
157
|
+
return t;
|
|
158
|
+
}), p = new i(r), m = a((e, t) => p.simplex2(e, t), {
|
|
159
|
+
octaves: 5,
|
|
160
|
+
frequency: 1 / u
|
|
161
|
+
}), h = (e, t) => Math.floor(s + c * m(e, t)), g = /* @__PURE__ */ new Int32Array(1024), _ = /* @__PURE__ */ new Uint8Array(1024);
|
|
162
|
+
return (e, t, n, r, i, a) => {
|
|
163
|
+
let s = new Map(a.blocks.map((e) => [e.name, e.id])), [c, l, u, d, p] = f.map((e) => {
|
|
164
|
+
let t = s.get(e);
|
|
165
|
+
if (t === void 0) throw RangeError(`noiseTerrainLod block "${e}" is not defined; define it with blocks.define("${e}")`);
|
|
166
|
+
return t;
|
|
167
|
+
}), m = t * 32 * i, v = n * 32 * i, y = r * 32 * i, b = i >> 1, x = Math.floor((o + 1 - v + b) / i), S = Math.ceil(3 / i), C = Infinity, w = x;
|
|
168
|
+
for (let e = 0; e < 32; e++) for (let t = 0; t < 32; t++) {
|
|
169
|
+
let n = h(m + t * i + b, y + e * i + b), r = Math.floor((n + 1 - v + b) / i);
|
|
170
|
+
g[t + 32 * e] = r, _[t + 32 * e] = +(n <= o + 1), C = Math.min(C, r), w = Math.max(w, r);
|
|
171
|
+
}
|
|
172
|
+
if (!(w <= 0)) {
|
|
173
|
+
if (C > 33 + S) return e.fill(c);
|
|
174
|
+
for (let t = 0; t < 32; t++) for (let n = 0; n < 32; n++) {
|
|
175
|
+
let r = g[n + 32 * t], i = _[n + 32 * t];
|
|
176
|
+
for (let a = 0; a < Math.min(32, Math.max(r, x)); a++) e.set(n, a, t, a >= r ? p : a === r - 1 ? i ? d : u : a >= r - 1 - S ? l : c);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
//#endregion
|
|
182
|
+
export { c as VoxelLod, u as noiseTerrainLod };
|
package/dist/mesher.d.ts
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { Vec3 } from '@margoengine/math';
|
|
3
|
+
import type { BlockType } from './blocks.js';
|
|
4
|
+
import type { PackedChunk, VoxelChunk } from './chunk.js';
|
|
5
|
+
import type { VoxelWorld } from './world.js';
|
|
6
|
+
/** u32 words per quad in a {@link ChunkMesh} layer array: 3 (12 bytes). Quad `i` is words `3i` to `3i + 2`. */
|
|
7
|
+
export declare const QUAD_WORDS = 3;
|
|
8
|
+
/**
|
|
9
|
+
* Input of {@link meshChunk}: one chunk, the cells around it and the block table. {@link meshInput} builds it from a
|
|
10
|
+
* world. Every field is plain data that a worker can receive.
|
|
11
|
+
*/
|
|
12
|
+
export interface MeshInput {
|
|
13
|
+
/** The chunk to mesh: a {@link VoxelChunk} or its {@link VoxelChunk.pack} output. */
|
|
14
|
+
chunk: PackedChunk | VoxelChunk;
|
|
15
|
+
/**
|
|
16
|
+
* Block states of the 6,536 shell cells: the cells of the 34 x 34 x 34 cube at local coordinates -1..32 outside the
|
|
17
|
+
* chunk's 0..31, in order of `(x + 1) + 34 (z + 1) + 1156 (y + 1)` (x fastest, then z, then y). They decide the
|
|
18
|
+
* faces, ambient occlusion and smooth light at the chunk's borders.
|
|
19
|
+
*/
|
|
20
|
+
shell: Uint32Array;
|
|
21
|
+
/** Light bytes (`sky << 4 | block`) of the shell cells, in the same order. */
|
|
22
|
+
light: Uint8Array;
|
|
23
|
+
/** The block types the states refer to: `BlockTypes.table`, or a worker's copy of it. */
|
|
24
|
+
blocks: readonly BlockType[];
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Output of {@link meshChunk}: the quads of each render layer, {@link QUAD_WORDS} u32 each, and which chunk faces see
|
|
28
|
+
* each other. Every array owns its buffer, so it can be transferred. {@link decodeQuad} reads a quad back.
|
|
29
|
+
*
|
|
30
|
+
* Quad bits, low bits first. Word 0: local cell x 0-4, y 5-9, z 10-14; face 15-17 (0-5: +x, -x, +y, -y, +z, -z;
|
|
31
|
+
* 6, 7: the diagonals of a cross block); w - 1 at 18-22 and h - 1 at 23-27 (size in cells along u and v); flip 28;
|
|
32
|
+
* liquidDrop 29-31. Word 1: material 0-11; AO of corners 0-3 at 12-13, 14-15, 16-17, 18-19 (0 darkest, 3 open);
|
|
33
|
+
* wave 20; inset 21; trim 22-25; 26-31 zero. Word 2: corner c's sky light at bits 8c to 8c + 3 and block light at
|
|
34
|
+
* 8c + 4 to 8c + 7.
|
|
35
|
+
*
|
|
36
|
+
* Geometry: (u, v) are (z, y) on x faces, (x, z) on y faces and (x, y) on z faces; the face lies on the cell's side
|
|
37
|
+
* toward its normal (cell + 1 for +x, +y, +z). Face 6 runs from the cell's (x, z) corner to (x + 1, z + 1), face 7 from
|
|
38
|
+
* (x + 1, z) to (x, z + 1), both with v = y. Corner c sits at (u, v) = (0, 0), (w, 0), (w, h), (0, h) from the cell.
|
|
39
|
+
* Triangles are (0, 1, 2) and (0, 2, 3), or (0, 1, 3) and (1, 2, 3) when flip is set. A liquid's top corners (the whole
|
|
40
|
+
* +y face, corners 2 and 3 of a side face) sit liquidDrop eighths of a block below the cell top; wave marks a quad of a
|
|
41
|
+
* liquid's surface (its +y face and the sides of a cell without the same liquid above). Slabs, stairs and the faces
|
|
42
|
+
* they partly cover use the half-block bits: inset moves the face half a block back toward its cell's centre, and trim
|
|
43
|
+
* bits 1, 2, 4, 8 move the quad's start along u, its end along u, its start along v and its end along v half a block
|
|
44
|
+
* inward, so corner 0 sits at (0.5, 0) when bit 1 is set and corner 2 at (w - 0.5, h) when bit 2 is.
|
|
45
|
+
*/
|
|
46
|
+
export interface ChunkMesh {
|
|
47
|
+
/** Quads of `'opaque'` blocks. */
|
|
48
|
+
opaque: Uint32Array;
|
|
49
|
+
/** Quads of `'cutout'` blocks. */
|
|
50
|
+
cutout: Uint32Array;
|
|
51
|
+
/** Quads of `'translucent'` blocks. */
|
|
52
|
+
translucent: Uint32Array;
|
|
53
|
+
/**
|
|
54
|
+
* Chunk faces (0-5: +x, -x, +y, -y, +z, -z) joined through cells that are not opaque cubes: bit `a * 6 + b` is set
|
|
55
|
+
* when faces `a < b` are connected, so a renderer can skip chunks that no line of sight crosses (cave culling).
|
|
56
|
+
*/
|
|
57
|
+
visibility: number;
|
|
58
|
+
/**
|
|
59
|
+
* With `border` asked of {@link VoxelMesher.mesh}: the whole cube faces of the shell cells that touch the chunk, one
|
|
60
|
+
* u32 each, the faces the neighbouring chunks mesh along the seams (a bevel chamfers the chunk's seam edges and
|
|
61
|
+
* corners with them). Bits: local x + 1 at 0-5, y + 1 at 6-11, z + 1 at 12-17 (cells -1 to 32), face 18-20 (0-5),
|
|
62
|
+
* render layer 21-22 (0 opaque, 1 cutout, 2 translucent). Undefined when not asked or the mesh has no quads.
|
|
63
|
+
*/
|
|
64
|
+
border?: Uint32Array;
|
|
65
|
+
}
|
|
66
|
+
/** One quad's fields, from {@link decodeQuad} (see {@link ChunkMesh} for their meaning). */
|
|
67
|
+
export interface DecodedQuad {
|
|
68
|
+
/** Local cell x of the quad's corner 0, 0-31. */
|
|
69
|
+
x: number;
|
|
70
|
+
/** Local cell y, 0-31. */
|
|
71
|
+
y: number;
|
|
72
|
+
/** Local cell z, 0-31. */
|
|
73
|
+
z: number;
|
|
74
|
+
/** 0-5: +x, -x, +y, -y, +z, -z; 6, 7: cross diagonals. */
|
|
75
|
+
face: number;
|
|
76
|
+
/** Width in cells along u, 1-32. */
|
|
77
|
+
w: number;
|
|
78
|
+
/** Height in cells along v, 1-32. */
|
|
79
|
+
h: number;
|
|
80
|
+
/** Triangles split along corners 1-3 instead of 0-2. */
|
|
81
|
+
flip: boolean;
|
|
82
|
+
/** Eighths of a block the liquid surface sits below the cell top, 0-7. */
|
|
83
|
+
liquidDrop: number;
|
|
84
|
+
/** Material id from {@link blockMaterials}, 0-4095. */
|
|
85
|
+
material: number;
|
|
86
|
+
/** Ambient occlusion per corner, 0 (darkest) to 3 (open). */
|
|
87
|
+
ao: [number, number, number, number];
|
|
88
|
+
/** Part of a liquid surface. */
|
|
89
|
+
wave: boolean;
|
|
90
|
+
/** The face lies half a block inside its cell, toward the cell's centre (a slab's top, a stair's step). */
|
|
91
|
+
inset: boolean;
|
|
92
|
+
/**
|
|
93
|
+
* Half-block trims: bit 1 starts the quad half a block later along u, bit 2 ends it half a block earlier along u,
|
|
94
|
+
* bits 4 and 8 the same along v. 0 for whole-cell quads.
|
|
95
|
+
*/
|
|
96
|
+
trim: number;
|
|
97
|
+
/** Sky light per corner, 0-15. */
|
|
98
|
+
sky: [number, number, number, number];
|
|
99
|
+
/** Block light per corner, 0-15. */
|
|
100
|
+
block: [number, number, number, number];
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Material ids of a block table: entry `id * 6 + face` (faces +x, -x, +y, -y, +z, -z) is the id that
|
|
104
|
+
* {@link meshChunk} writes for that face; a cross block's two quads use face 0's. Ids count up from 0 in block id
|
|
105
|
+
* order, one per distinct texture layer of each block, so a renderer builds its material table (texture layer plus the
|
|
106
|
+
* block's tint, roughness, metalness, emissive and animation) from the same walk. Invisible blocks get 0xffff.
|
|
107
|
+
* @throws RangeError when the blocks need more than 4,096 materials (share texture layers within blocks).
|
|
108
|
+
*/
|
|
109
|
+
export declare function blockMaterials(blocks: readonly BlockType[]): Uint16Array;
|
|
110
|
+
/**
|
|
111
|
+
* Meshes one chunk into greedy quads per render layer (see {@link ChunkMesh} for the quad format). Faces: an opaque
|
|
112
|
+
* cube hides every face against it; faces between two blocks of the same id are hidden; every other face is kept (so
|
|
113
|
+
* an opaque face behind glass stays and the glass face against it goes). Each face vertex gets ambient occlusion from
|
|
114
|
+
* the opaque cubes around it and smooth light averaged from the open cells around it, and the AO picks the triangle
|
|
115
|
+
* diagonal. Faces merge only when all their words are equal and their corners equal along the merge axis, so the
|
|
116
|
+
* merged mesh shades exactly like one quad per block face. Cross blocks emit two diagonal quads; liquid blocks lower
|
|
117
|
+
* their surface by their state data's low 3 bits (eighths of a block) and mark it to wave. Invisible blocks emit
|
|
118
|
+
* nothing. Synchronous; it reuses about 1 MB of scratch between calls, so a call allocates little beyond its result.
|
|
119
|
+
* @throws TypeError when `input` or one of its fields has the wrong type.
|
|
120
|
+
* @throws RangeError for wrong array lengths, a cell index past the palette, a block id outside `blocks`, or more than
|
|
121
|
+
* 4,096 materials.
|
|
122
|
+
*/
|
|
123
|
+
export declare function meshChunk(input: MeshInput): ChunkMesh;
|
|
124
|
+
/**
|
|
125
|
+
* Reads quad `i` of a {@link ChunkMesh} layer array.
|
|
126
|
+
* @throws TypeError when `words` is not a Uint32Array.
|
|
127
|
+
* @throws RangeError when `i` is not the index of a whole quad in `words`.
|
|
128
|
+
*/
|
|
129
|
+
export declare function decodeQuad(words: Uint32Array, i: number): DecodedQuad;
|
|
130
|
+
/**
|
|
131
|
+
* The {@link MeshInput} of chunk `key` (`'cx,cy,cz'`, from `chunkKey`) of `world`: the chunk packed, the states and
|
|
132
|
+
* light bytes of its shell from the loaded neighbours (unloaded cells are air with light 0) and `world.blocks.table`.
|
|
133
|
+
* Undefined when that chunk is not loaded (a removed key in `world.dirty`: drop its mesh).
|
|
134
|
+
* @throws TypeError when `world` is not a VoxelWorld or `key` is not a string.
|
|
135
|
+
* @throws RangeError when `key` is not three chunk coordinates.
|
|
136
|
+
*/
|
|
137
|
+
export declare function meshInput(world: VoxelWorld, key: string): MeshInput | undefined;
|
|
138
|
+
/**
|
|
139
|
+
* Centre of every quad of a layer array, three numbers per quad in the chunk's local blocks: the point
|
|
140
|
+
* {@link sortQuads} measures from. Cross quads (faces 6 and 7) use their cell's centre.
|
|
141
|
+
*/
|
|
142
|
+
export declare function quadCenters(words: Uint32Array): Float32Array;
|
|
143
|
+
/**
|
|
144
|
+
* Sorts the quads of a translucent layer array back to front from `eye`, in place. The order is exact for every quad
|
|
145
|
+
* on a cell face: a ray from the eye crosses cells whose distance from the eye's cell, counted per axis, never falls
|
|
146
|
+
* on any axis, so their grid (L1) distance rises by one at every face it crosses (the back-to-front grid order of
|
|
147
|
+
* Frieder, Gordon and Reynolds 1985, here for a perspective eye anywhere in the grid). A quad on the face between two
|
|
148
|
+
* cells is keyed by the farther cell, a quad inside a cell (a lowered or waving liquid top, an inset slab face, a
|
|
149
|
+
* cross) by its cell, and larger keys draw first; quads with one key are inside one cell and draw by centre distance,
|
|
150
|
+
* then in their current order. The order changes only when the eye's cell does, so a renderer re-sorts then.
|
|
151
|
+
* The translucent layer is never merged ({@link meshChunk}), so every quad has one cell.
|
|
152
|
+
* @param eye The camera position in the chunk's local block units: world position minus 32 times the chunk coordinates.
|
|
153
|
+
* @returns `words`.
|
|
154
|
+
* @throws TypeError when `words` is not a Uint32Array or `eye` is not three numbers.
|
|
155
|
+
* @throws RangeError when `words` is not whole quads or `eye` is not finite.
|
|
156
|
+
*/
|
|
157
|
+
export declare function sortQuads(words: Uint32Array, eye: Vec3): Uint32Array;
|
|
158
|
+
/** Options of {@link VoxelMesher}. */
|
|
159
|
+
export interface VoxelMesherOptions {
|
|
160
|
+
/**
|
|
161
|
+
* Number of workers, started as jobs arrive. Default: half the logical cores (`navigator.hardwareConcurrency`, 4
|
|
162
|
+
* when unknown) minus one, from 1 to 4: 3 on an 8-thread laptop, leaving the main thread and the world generation
|
|
163
|
+
* workers their share.
|
|
164
|
+
*/
|
|
165
|
+
workers?: number;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Meshes chunks off the main thread: each job runs {@link meshChunk} in one of a pool of workers, lowest priority
|
|
169
|
+
* number first (equal priorities in call order). The {@link MeshInput} is built with {@link meshInput} on this thread
|
|
170
|
+
* when a worker takes the job, so it holds the world's latest blocks and a queued job holds no copy; its buffers and
|
|
171
|
+
* the mesh's move between the threads without copies. A newer `mesh` call for a key supersedes the older pending one,
|
|
172
|
+
* so only the newest edit's mesh is delivered.
|
|
173
|
+
*
|
|
174
|
+
* ```ts
|
|
175
|
+
* const mesher = new VoxelMesher();
|
|
176
|
+
* for (const key of world.dirty) {
|
|
177
|
+
* mesher.mesh(world, key, distanceToCamera(key)).then((mesh) => mesh && upload(key, mesh));
|
|
178
|
+
* world.dirty.delete(key);
|
|
179
|
+
* }
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
export declare class VoxelMesher {
|
|
183
|
+
#private;
|
|
184
|
+
/** Number of workers in the pool. */
|
|
185
|
+
readonly workers: number;
|
|
186
|
+
/**
|
|
187
|
+
* @throws RangeError when `workers` is not a whole number of 1 or more.
|
|
188
|
+
* @throws MargoError `unsupported` when this context has no Web Workers.
|
|
189
|
+
*/
|
|
190
|
+
constructor(options?: VoxelMesherOptions);
|
|
191
|
+
/**
|
|
192
|
+
* Starts the workers now rather than with the first jobs, so their code downloads before the chunks arrive to compete
|
|
193
|
+
* with it and the first mesh does not wait for it. Meshing is otherwise the same. A worker that fails to start is
|
|
194
|
+
* dropped here: the first job starts another and reports its failure.
|
|
195
|
+
*/
|
|
196
|
+
warm(): void;
|
|
197
|
+
/**
|
|
198
|
+
* Meshes chunk `key` of `world` in a worker. Resolves null when the job was cancelled ({@link VoxelMesher.cancel},
|
|
199
|
+
* dispose) or superseded by a newer `mesh` call for the key (keep the current mesh until that one arrives), and when
|
|
200
|
+
* the chunk is not loaded once a worker takes the job (drop its mesh). Rejects when the worker fails.
|
|
201
|
+
* @param priority Lower is meshed sooner, such as the chunk's distance to the camera. Default 0.
|
|
202
|
+
* @param eye The camera's world position: the translucent quads come back sorted from it ({@link sortQuads}).
|
|
203
|
+
* @param border Also list the shell's cube faces in {@link ChunkMesh.border}. Default false.
|
|
204
|
+
* @throws TypeError when `world` is not a VoxelWorld, `key` is not a string or `eye` is not three numbers.
|
|
205
|
+
* @throws RangeError when `key` is not three chunk coordinates, or `priority` or `eye` is not finite.
|
|
206
|
+
* @throws MargoError `disposed` after {@link VoxelMesher.dispose}.
|
|
207
|
+
*/
|
|
208
|
+
mesh(world: VoxelWorld, key: string, priority?: number, eye?: Vec3, border?: boolean): Promise<ChunkMesh | null>;
|
|
209
|
+
/** Drops the pending job of chunk `key`, if any: its `mesh` promise resolves null. */
|
|
210
|
+
cancel(key: string): void;
|
|
211
|
+
/** Cancels every pending job and terminates the workers. */
|
|
212
|
+
dispose(): void;
|
|
213
|
+
[Symbol.dispose](): void;
|
|
214
|
+
}
|