@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.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +1670 -2
- 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-BmXhcsFt.js +412 -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 +40 -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 +1616 -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 +173 -4
package/dist/fluids.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { VoxelWorld } from './world.js';
|
|
2
|
+
/** One flowing liquid of a {@link VoxelFluids}. */
|
|
3
|
+
export interface VoxelFluid {
|
|
4
|
+
/** The liquid's block type, by id or name (a `'liquid'` shape). */
|
|
5
|
+
block: number | string;
|
|
6
|
+
/** Cells it runs sideways from a source or a fall, 1 to 7. Default 7 (water); 3 is lava-like. */
|
|
7
|
+
reach?: number;
|
|
8
|
+
/** Ticks between its steps, an integer >= 1. Default 5 (water at 20 ticks a second); 30 is lava-like. */
|
|
9
|
+
delay?: number;
|
|
10
|
+
/** A flowing cell between two sources, on solid ground or a source, becomes a source. Default false. */
|
|
11
|
+
infinite?: boolean;
|
|
12
|
+
}
|
|
13
|
+
/** Two liquids touching: the cell of `fluid` turns into a block. */
|
|
14
|
+
export interface VoxelFluidMix {
|
|
15
|
+
/** The liquid whose cell turns, by id or name. */
|
|
16
|
+
fluid: number | string;
|
|
17
|
+
/** The liquid it touches (beside or above it), by id or name. */
|
|
18
|
+
touches: number | string;
|
|
19
|
+
/** What a source of `fluid` becomes (lava source and water: obsidian). */
|
|
20
|
+
source: number | string;
|
|
21
|
+
/** What a flowing cell of `fluid` becomes (flowing lava and water: cobblestone). */
|
|
22
|
+
flowing: number | string;
|
|
23
|
+
}
|
|
24
|
+
/** Options of {@link VoxelFluids.attach}. */
|
|
25
|
+
export interface VoxelFluidsOptions {
|
|
26
|
+
/** The liquids that flow. */
|
|
27
|
+
fluids: readonly VoxelFluid[];
|
|
28
|
+
/** Liquids turning to blocks where they meet. Default none. */
|
|
29
|
+
mixes?: readonly VoxelFluidMix[];
|
|
30
|
+
/** Ticks a second {@link VoxelFluids.update} advances by. Default 20. */
|
|
31
|
+
ticksPerSecond?: number;
|
|
32
|
+
/** Most cells one tick updates; the rest wait for the next tick. Default 65,536. */
|
|
33
|
+
maxUpdates?: number;
|
|
34
|
+
}
|
|
35
|
+
/** Flags of a liquid's state data. */
|
|
36
|
+
export declare const FLUID_FALLING = 8;
|
|
37
|
+
/**
|
|
38
|
+
* Water and lava that spread and fall: attach it to a world, call {@link VoxelFluids.update} every frame, and any
|
|
39
|
+
* liquid block set in the world (a source) flows. Unloaded chunks and the world's y range stop it like rock.
|
|
40
|
+
*/
|
|
41
|
+
export declare class VoxelFluids {
|
|
42
|
+
#private;
|
|
43
|
+
readonly world: VoxelWorld;
|
|
44
|
+
/**
|
|
45
|
+
* Follows `world`'s edits: every change of a liquid cell or its neighbours schedules them.
|
|
46
|
+
* @throws TypeError for a wrong world or option type; RangeError for an option out of range or a block that is not
|
|
47
|
+
* defined (or a fluid that is not a `'liquid'` shape).
|
|
48
|
+
*/
|
|
49
|
+
static attach(world: VoxelWorld, options: VoxelFluidsOptions): VoxelFluids;
|
|
50
|
+
private constructor();
|
|
51
|
+
/** Cells waiting for their tick. */
|
|
52
|
+
get pending(): number;
|
|
53
|
+
/** Ticks run so far. */
|
|
54
|
+
get ticks(): number;
|
|
55
|
+
/**
|
|
56
|
+
* Advances by `seconds` (whole ticks at `ticksPerSecond`; the rest carries over), at most 10 ticks a call.
|
|
57
|
+
* Returns the cells updated.
|
|
58
|
+
* @throws RangeError for negative or non-finite seconds; MargoError `disposed` after `dispose()`.
|
|
59
|
+
*/
|
|
60
|
+
update(seconds: number): number;
|
|
61
|
+
/**
|
|
62
|
+
* Runs one tick: every cell due now settles and flows. Returns the cells updated.
|
|
63
|
+
* @throws MargoError `disposed` after `dispose()`.
|
|
64
|
+
*/
|
|
65
|
+
tick(): number;
|
|
66
|
+
/**
|
|
67
|
+
* Runs ticks until nothing is pending or `maxTicks` ran (a loading screen, a test). Returns the ticks run.
|
|
68
|
+
* @throws RangeError for `maxTicks` that is not an integer >= 1.
|
|
69
|
+
*/
|
|
70
|
+
settle(maxTicks?: number): number;
|
|
71
|
+
/** Stops following the world and drops what is scheduled. The liquid stays as it is. */
|
|
72
|
+
dispose(): void;
|
|
73
|
+
[Symbol.dispose](): void;
|
|
74
|
+
}
|
package/dist/fluids.js
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { i as e } from "./blocks-BabtpQT-.js";
|
|
5
|
+
import { MargoError as t } from "@margoengine/math/errors";
|
|
6
|
+
//#region src/fluids.ts
|
|
7
|
+
var n = 8, r = [
|
|
8
|
+
[
|
|
9
|
+
1,
|
|
10
|
+
0,
|
|
11
|
+
0
|
|
12
|
+
],
|
|
13
|
+
[
|
|
14
|
+
-1,
|
|
15
|
+
0,
|
|
16
|
+
0
|
|
17
|
+
],
|
|
18
|
+
[
|
|
19
|
+
0,
|
|
20
|
+
0,
|
|
21
|
+
1
|
|
22
|
+
],
|
|
23
|
+
[
|
|
24
|
+
0,
|
|
25
|
+
0,
|
|
26
|
+
-1
|
|
27
|
+
]
|
|
28
|
+
], i = (e, t, n, r) => {
|
|
29
|
+
if (typeof t != "number") throw TypeError(`VoxelFluids ${e} must be a number`);
|
|
30
|
+
if (!Number.isInteger(t) || t < n || t > r) throw RangeError(`VoxelFluids ${e} is ${t}; pass an integer in ${n}..${r}`);
|
|
31
|
+
return t;
|
|
32
|
+
}, a = class n {
|
|
33
|
+
#e = /* @__PURE__ */ new Map();
|
|
34
|
+
#t = /* @__PURE__ */ new Map();
|
|
35
|
+
#n = /* @__PURE__ */ new Map();
|
|
36
|
+
#r = 0;
|
|
37
|
+
#i = 0;
|
|
38
|
+
#a;
|
|
39
|
+
#o;
|
|
40
|
+
#s;
|
|
41
|
+
#c = !1;
|
|
42
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
43
|
+
return new n(e, t);
|
|
44
|
+
}
|
|
45
|
+
constructor(e, t) {
|
|
46
|
+
if (typeof e?.onChange != "function" || typeof e.getBlock != "function") throw TypeError("VoxelFluids.attach expects a VoxelWorld; pass new VoxelWorld({blocks})");
|
|
47
|
+
if (!t || !Array.isArray(t.fluids)) throw TypeError("VoxelFluids options need fluids: [{block: \"water\"}, ...]");
|
|
48
|
+
let { ticksPerSecond: n = 20, maxUpdates: a = 65536, mixes: o = [] } = t, s = e.blocks.table, c = (e, t) => {
|
|
49
|
+
let n = typeof e == "string" ? s.find((t) => t.name === e) : s[e];
|
|
50
|
+
if (typeof e != "string" && typeof e != "number") throw TypeError(`VoxelFluids ${t} must be a block id or name`);
|
|
51
|
+
if (!n) throw RangeError(`VoxelFluids ${t} "${e}" is not a defined block; define it with blocks.define()`);
|
|
52
|
+
return n.id;
|
|
53
|
+
};
|
|
54
|
+
if (typeof n != "number") throw TypeError("VoxelFluids ticksPerSecond must be a number");
|
|
55
|
+
if (!(n > 0 && n <= 1e3)) throw RangeError(`VoxelFluids ticksPerSecond is ${n}; pass a number in (0, 1000]`);
|
|
56
|
+
this.#a = n, this.#o = i("maxUpdates", a, 1, 2 ** 30);
|
|
57
|
+
for (let e of t.fluids) {
|
|
58
|
+
if (!e || typeof e != "object") throw TypeError("VoxelFluids fluids must be objects {block}");
|
|
59
|
+
let t = c(e.block, "fluid block");
|
|
60
|
+
if (s[t].shape !== "liquid") throw RangeError(`VoxelFluids fluid "${s[t].name}" is not a liquid; define it with shape: 'liquid'`);
|
|
61
|
+
let n = i("reach", e.reach ?? 7, 1, 7);
|
|
62
|
+
this.#e.set(t, {
|
|
63
|
+
id: t,
|
|
64
|
+
step: Math.floor(7 / n),
|
|
65
|
+
last: n * Math.floor(7 / n),
|
|
66
|
+
delay: i("delay", e.delay ?? 5, 1, 1 << 20),
|
|
67
|
+
infinite: !!e.infinite,
|
|
68
|
+
mixes: []
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
if (!Array.isArray(o)) throw TypeError("VoxelFluids mixes must be an array");
|
|
72
|
+
for (let e of o) {
|
|
73
|
+
let t = this.#e.get(c(e?.fluid, "mix fluid"));
|
|
74
|
+
if (!t) throw RangeError("VoxelFluids mix fluid is not one of fluids; list it in fluids");
|
|
75
|
+
t.mixes.push({
|
|
76
|
+
touches: c(e.touches, "mix touches"),
|
|
77
|
+
source: c(e.source, "mix source"),
|
|
78
|
+
flowing: c(e.flowing, "mix flowing")
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
this.world = e, this.#s = e.onChange((e, t, n) => {
|
|
82
|
+
this.#l(e, t, n), this.#l(e, t + 1, n), this.#l(e, t - 1, n);
|
|
83
|
+
for (let [i, , a] of r) this.#l(e + i, t, n + a);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
get pending() {
|
|
87
|
+
return this.#t.size;
|
|
88
|
+
}
|
|
89
|
+
get ticks() {
|
|
90
|
+
return this.#r;
|
|
91
|
+
}
|
|
92
|
+
#l(e, t, n) {
|
|
93
|
+
let r = this.#e.get(this.world.getBlock(e, t, n) & 65535);
|
|
94
|
+
if (!r) return;
|
|
95
|
+
let i = `${e},${t},${n}`, a = this.#r + r.delay, o = this.#t.get(i);
|
|
96
|
+
if (o !== void 0 && o <= a) return;
|
|
97
|
+
this.#t.set(i, a);
|
|
98
|
+
let s = this.#n.get(a);
|
|
99
|
+
s || this.#n.set(a, s = []), s.push(i);
|
|
100
|
+
}
|
|
101
|
+
update(e) {
|
|
102
|
+
if (this.#c) throw new t("disposed", "VoxelFluids is disposed; attach a new one");
|
|
103
|
+
if (typeof e != "number" || !(e >= 0) || !Number.isFinite(e)) throw RangeError(`VoxelFluids.update seconds is ${e}; pass the frame's seconds, 0 or more`);
|
|
104
|
+
this.#i = Math.min(this.#i + e * this.#a, 10);
|
|
105
|
+
let n = 0;
|
|
106
|
+
for (; this.#i >= 1;) this.#i--, n += this.tick();
|
|
107
|
+
return n;
|
|
108
|
+
}
|
|
109
|
+
tick() {
|
|
110
|
+
if (this.#c) throw new t("disposed", "VoxelFluids is disposed; attach a new one");
|
|
111
|
+
let e = ++this.#r, n = this.#n.get(e) ?? [];
|
|
112
|
+
this.#n.delete(e);
|
|
113
|
+
let r = 0;
|
|
114
|
+
for (let t = 0; t < n.length; t++) {
|
|
115
|
+
let i = n[t];
|
|
116
|
+
if (this.#t.get(i) !== e) continue;
|
|
117
|
+
if (r >= this.#o) {
|
|
118
|
+
let r = n.slice(t).filter((t) => this.#t.get(t) === e);
|
|
119
|
+
for (let t of r) this.#t.set(t, e + 1);
|
|
120
|
+
this.#n.set(e + 1, [...r, ...this.#n.get(e + 1) ?? []]);
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
this.#t.delete(i);
|
|
124
|
+
let [a, o, s] = i.split(",").map(Number);
|
|
125
|
+
this.#p(a, o, s), r++;
|
|
126
|
+
}
|
|
127
|
+
return r;
|
|
128
|
+
}
|
|
129
|
+
settle(e = 1e5) {
|
|
130
|
+
i("maxTicks", e, 1, 2 ** 31);
|
|
131
|
+
let t = 0;
|
|
132
|
+
for (; this.#t.size && t < e;) this.tick(), t++;
|
|
133
|
+
return t;
|
|
134
|
+
}
|
|
135
|
+
#u(e, t, n) {
|
|
136
|
+
let r = this.world;
|
|
137
|
+
if (t < r.minY || t > r.maxY || !r.getChunk(e >> 5, t >> 5, n >> 5)) return !1;
|
|
138
|
+
let i = r.blocks.table[r.getBlock(e, t, n) & 65535];
|
|
139
|
+
return !i || i.id === 0 || !i.solid && !i.liquid;
|
|
140
|
+
}
|
|
141
|
+
#d(e, t, n, r) {
|
|
142
|
+
let i = this.world.getBlock(e, t, n);
|
|
143
|
+
if ((i & 65535) !== r.id) return Infinity;
|
|
144
|
+
let a = i >>> 16;
|
|
145
|
+
return a & 8 ? 0 : a & 7;
|
|
146
|
+
}
|
|
147
|
+
#f(e, t, n, r) {
|
|
148
|
+
this.world.getBlock(e, t, n) !== r && this.world.setBlock(e, t, n, r);
|
|
149
|
+
}
|
|
150
|
+
#p(t, n, i) {
|
|
151
|
+
let a = this.world, o = a.getBlock(t, n, i), s = this.#e.get(o & 65535);
|
|
152
|
+
if (!s) return;
|
|
153
|
+
let c = o >>> 16, l = c === 0;
|
|
154
|
+
for (let e of s.mixes) for (let [o, s, c] of [...r, [
|
|
155
|
+
0,
|
|
156
|
+
1,
|
|
157
|
+
0
|
|
158
|
+
]]) if ((a.getBlock(t + o, n + s, i + c) & 65535) === e.touches) return void a.setBlock(t, n, i, l ? e.source : e.flowing);
|
|
159
|
+
let u = 0;
|
|
160
|
+
if (!l) {
|
|
161
|
+
let l = 0, d = Infinity;
|
|
162
|
+
for (let [e, , o] of r) {
|
|
163
|
+
let r = this.#d(t + e, n, i + o, s);
|
|
164
|
+
r === 0 && !(a.getBlock(t + e, n, i + o) >>> 16 & 8) && l++, d = Math.min(d, r);
|
|
165
|
+
}
|
|
166
|
+
let f = a.getBlock(t, n - 1, i), p = (f & 65535) === s.id ? !(f >>> 16) : !this.#u(t, n - 1, i), m;
|
|
167
|
+
if (m = s.infinite && l >= 2 && p ? e(s.id, 0) : (a.getBlock(t, n + 1, i) & 65535) === s.id ? e(s.id, 8) : d + s.step <= s.last ? e(s.id, d + s.step) : 0, m !== o) return void a.setBlock(t, n, i, m);
|
|
168
|
+
u = c & 8 ? 0 : c & 7;
|
|
169
|
+
}
|
|
170
|
+
if (this.#u(t, n - 1, i)) return void this.#f(t, n - 1, i, e(s.id, 8));
|
|
171
|
+
let d = this.world.getBlock(t, n - 1, i);
|
|
172
|
+
if ((d & 65535) === s.id && d >>> 16) return;
|
|
173
|
+
let f = u + s.step;
|
|
174
|
+
if (f > s.last) return;
|
|
175
|
+
let p = Math.min(4, s.last / s.step), m = r.map(([e, , r]) => this.#h(t + e, n, i + r, s) ? this.#g(t + e, n, i + r, e, r, p, s) : Infinity), h = Math.min(...m);
|
|
176
|
+
r.forEach(([r, , a], o) => {
|
|
177
|
+
m[o] === Infinity || h < p + 1 && m[o] !== h || this.#m(t + r, n, i + a, s, f) && this.#f(t + r, n, i + a, e(s.id, f));
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
#m(e, t, n, r, i) {
|
|
181
|
+
let a = this.world.getBlock(e, t, n);
|
|
182
|
+
if ((a & 65535) === r.id) {
|
|
183
|
+
let e = a >>> 16;
|
|
184
|
+
return e !== 0 && !(e & 8) && (e & 7) > i;
|
|
185
|
+
}
|
|
186
|
+
return this.#u(e, t, n);
|
|
187
|
+
}
|
|
188
|
+
#h(e, t, n, r) {
|
|
189
|
+
let i = this.world.getBlock(e, t, n);
|
|
190
|
+
return (i & 65535) === r.id ? !!(i >>> 16) : this.#u(e, t, n);
|
|
191
|
+
}
|
|
192
|
+
#g(e, t, n, i, a, o, s) {
|
|
193
|
+
let c = [[
|
|
194
|
+
e,
|
|
195
|
+
n,
|
|
196
|
+
i,
|
|
197
|
+
a
|
|
198
|
+
]], l = /* @__PURE__ */ new Set([`${e},${n}`]);
|
|
199
|
+
for (let e = 0; e <= o && c.length; e++) {
|
|
200
|
+
let n = [];
|
|
201
|
+
for (let [i, a, o, u] of c) {
|
|
202
|
+
if (this.#u(i, t - 1, a) || (this.world.getBlock(i, t - 1, a) & 65535) === s.id) return e;
|
|
203
|
+
for (let [e, , c] of r) {
|
|
204
|
+
if (e === -o && c === -u) continue;
|
|
205
|
+
let r = i + e, d = a + c, f = `${r},${d}`;
|
|
206
|
+
!l.has(f) && this.#h(r, t, d, s) && (l.add(f), n.push([
|
|
207
|
+
r,
|
|
208
|
+
d,
|
|
209
|
+
e,
|
|
210
|
+
c
|
|
211
|
+
]));
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
c = n;
|
|
215
|
+
}
|
|
216
|
+
return o + 1;
|
|
217
|
+
}
|
|
218
|
+
dispose() {
|
|
219
|
+
this.#c || (this.#c = !0, this.#s(), this.#t.clear(), this.#n.clear());
|
|
220
|
+
}
|
|
221
|
+
[Symbol.dispose]() {
|
|
222
|
+
this.dispose();
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
//#endregion
|
|
226
|
+
export { n as FLUID_FALLING, a as VoxelFluids };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { BlockType } from './blocks.js';
|
|
2
|
+
import { VoxelChunk } from './chunk.js';
|
|
3
|
+
/** What a {@link VoxelGenerate} function gets besides the chunk. */
|
|
4
|
+
export interface VoxelGenerateContext {
|
|
5
|
+
/** The world's block types by id (its `BlockTypes.table`): find a type by `name` for its id. */
|
|
6
|
+
readonly blocks: readonly BlockType[];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Fills `chunk`, a new air chunk, with the blocks of chunk (cx, cy, cz): local cell (x, y, z) is world cell
|
|
10
|
+
* (32 cx + x, 32 cy + y, 32 cz + z). It must give the same blocks for the same chunk every time and in any order, so
|
|
11
|
+
* anything that crosses chunk borders (a tree) has to follow from the coordinates alone. Light can stay 0:
|
|
12
|
+
* `@margoengine/voxel/light` recomputes it.
|
|
13
|
+
*
|
|
14
|
+
* It may return the column's ceiling: an integer cell y that no chunk of column (cx, cz) has a block above, whatever
|
|
15
|
+
* its cy. A streamer then loads that column's chunks wholly above it as air without a job. A ceiling too low loses
|
|
16
|
+
* blocks; return nothing when unsure.
|
|
17
|
+
*/
|
|
18
|
+
export type VoxelGenerate = (chunk: VoxelChunk, cx: number, cy: number, cz: number, context: VoxelGenerateContext) => void | number | Promise<void | number>;
|
|
19
|
+
/**
|
|
20
|
+
* Serves `generate` from the module worker that calls it. A {@link VoxelStreamer} whose `generator` is this worker's
|
|
21
|
+
* URL posts one job per chunk that is not in its store: `generate` fills a new air chunk, which goes back packed (its
|
|
22
|
+
* arrays move without copies). A throw or rejection fails that chunk: the streamer reports it through `onError`.
|
|
23
|
+
*
|
|
24
|
+
* ```ts
|
|
25
|
+
* // islands.worker.js; with Vite: import islands from './islands.worker.js?worker&url'
|
|
26
|
+
* import { serveVoxelGenerator } from '@margoengine/voxel/generate';
|
|
27
|
+
* serveVoxelGenerator((chunk, cx, cy, cz, { blocks }) => {
|
|
28
|
+
* if (cy < 0) chunk.fill(blocks.find((type) => type.name === 'stone').id);
|
|
29
|
+
* });
|
|
30
|
+
* ```
|
|
31
|
+
* @throws TypeError when `generate` is not a function.
|
|
32
|
+
*/
|
|
33
|
+
export declare function serveVoxelGenerator(generate: VoxelGenerate): void;
|
|
34
|
+
/** Options of {@link noiseTerrain}: plain data (VoxelStreamer posts them to its terrain workers). */
|
|
35
|
+
export interface NoiseTerrainOptions {
|
|
36
|
+
/** Seed of the ground, caves and trees, an integer in -2^31..2^31-1: same seed and options, same world. Default 1. */
|
|
37
|
+
seed?: number;
|
|
38
|
+
/** Cell y the sea fills up to: open cells at or below it over low ground are water. Default 0. */
|
|
39
|
+
seaLevel?: number;
|
|
40
|
+
/** Mean ground height, in cells. Default 8. */
|
|
41
|
+
height?: number;
|
|
42
|
+
/** How far the ground rises above and sinks below `height`, in blocks (>= 0). Default 32. */
|
|
43
|
+
amplitude?: number;
|
|
44
|
+
/** Width of the largest hills, in blocks (> 0). Default 192. */
|
|
45
|
+
scale?: number;
|
|
46
|
+
/** How much rock is tunnelled into caves, 0 (none) to 1; rock beside water is never tunnelled. Default 0.3. */
|
|
47
|
+
caves?: number;
|
|
48
|
+
/** Chance that a grass column grows a tree where its crown has open air, 0 to 1. Default 0.01. */
|
|
49
|
+
trees?: number;
|
|
50
|
+
/**
|
|
51
|
+
* Names of the block types to build with, looked up in the world's types; each defaults to its key. `log` and
|
|
52
|
+
* `leaves` are needed only when `trees` > 0.
|
|
53
|
+
*/
|
|
54
|
+
blocks?: {
|
|
55
|
+
stone?: string;
|
|
56
|
+
dirt?: string;
|
|
57
|
+
grass?: string;
|
|
58
|
+
sand?: string;
|
|
59
|
+
water?: string;
|
|
60
|
+
log?: string;
|
|
61
|
+
leaves?: string;
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The built-in world generator: a fractal (fbm) simplex heightmap of grass over dirt over stone, sand beaches and sea
|
|
66
|
+
* floors, water up to `seaLevel`, cave tunnels where two 3D simplex fields are both near zero (sampled every 4 blocks
|
|
67
|
+
* and blended, never through the top block, and never beside water: rock next to the sea stays), and trees on grass:
|
|
68
|
+
* a column grows one when its hash says so and its whole crown is in the open air above the ground (no leaves in
|
|
69
|
+
* hillsides or caves), so a crown that crosses a chunk border is the same in every chunk. Block types come by name
|
|
70
|
+
* ({@link NoiseTerrainOptions.blocks}), so it works with any `BlockTypes`. Runs on any thread; VoxelStreamer runs it in
|
|
71
|
+
* workers. The function it returns gives the column's ceiling (see {@link VoxelGenerate}): the highest ground, sea or
|
|
72
|
+
* tree crown that reaches the column.
|
|
73
|
+
* @throws TypeError for an option of the wrong type; the returned function throws TypeError for a context without
|
|
74
|
+
* `blocks`.
|
|
75
|
+
* @throws RangeError for an option out of range; the returned function throws RangeError for a block name the context
|
|
76
|
+
* does not define.
|
|
77
|
+
*/
|
|
78
|
+
export declare function noiseTerrain(options?: NoiseTerrainOptions): (chunk: VoxelChunk, cx: number, cy: number, cz: number, context: VoxelGenerateContext) => number;
|
package/dist/generate.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { n as e } from "./chunk-BCoaKSn4.js";
|
|
2
|
+
import { n as t } from "./worker-DlGRxlc4.js";
|
|
3
|
+
import { Noise as n, fbm as r } from "@margoengine/procedural/noise";
|
|
4
|
+
//#region src/generate.ts
|
|
5
|
+
function i(n) {
|
|
6
|
+
if (typeof n != "function") throw TypeError("serveVoxelGenerator expects a function (chunk, cx, cy, cz, context)");
|
|
7
|
+
t(async ({ cx: t, cy: r, cz: i, blocks: a }) => {
|
|
8
|
+
let o = new e(), s = await n(o, t, r, i, { blocks: a });
|
|
9
|
+
return Number.isInteger(s) ? {
|
|
10
|
+
...o.pack(),
|
|
11
|
+
ceiling: s
|
|
12
|
+
} : o.pack();
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
var a = [
|
|
16
|
+
"stone",
|
|
17
|
+
"dirt",
|
|
18
|
+
"grass",
|
|
19
|
+
"sand",
|
|
20
|
+
"water",
|
|
21
|
+
"log",
|
|
22
|
+
"leaves"
|
|
23
|
+
], o = 2, s = 4, c = 40, l = (e, t, n, r, i) => {
|
|
24
|
+
if (e === void 0) return n;
|
|
25
|
+
if (typeof e != "number") throw TypeError(`NoiseTerrainOptions.${t} must be a number`);
|
|
26
|
+
if (!r(e)) throw RangeError(`NoiseTerrainOptions.${t} is ${e}; pass ${i}`);
|
|
27
|
+
return e;
|
|
28
|
+
}, u = (e) => Number.isFinite(e), d = (e, t, n) => {
|
|
29
|
+
let r = Math.imul(e, 374761393) ^ Math.imul(t, 668265263) ^ Math.imul(n, 1442695041);
|
|
30
|
+
return r = Math.imul(r ^ r >>> 13, 1274126177), (r ^ r >>> 16) >>> 0;
|
|
31
|
+
}, f = (e, t, n) => e + (t - e) * n, p = (e, t, n, r) => {
|
|
32
|
+
let i = (t >> 2) + 9 * (n >> 2) + 81 * (r >> 2), a = (t & 3) / 4, o = (n & 3) / 4;
|
|
33
|
+
return f(f(f(e[i], e[i + 1], a), f(e[i + 9], e[i + 10], a), o), f(f(e[i + 81], e[i + 82], a), f(e[i + 90], e[i + 91], a), o), (r & 3) / 4);
|
|
34
|
+
};
|
|
35
|
+
function m(e = {}) {
|
|
36
|
+
if (!e || typeof e != "object") throw TypeError("noiseTerrain options must be an object");
|
|
37
|
+
let t = l(e.seed, "seed", 1, (e) => (e | 0) === e, "an integer in -2^31..2^31-1"), i = l(e.seaLevel, "seaLevel", 0, (e) => (e | 0) === e, "an integer cell y"), f = l(e.height, "height", 8, u, "a finite number"), m = l(e.amplitude, "amplitude", 32, (e) => e >= 0 && u(e), "a finite number >= 0"), h = l(e.scale, "scale", 192, (e) => e > 0 && u(e), "a finite number > 0"), g = l(e.caves, "caves", .3, (e) => e >= 0 && e <= 1, "0..1"), _ = l(e.trees, "trees", .01, (e) => e >= 0 && e <= 1, "0..1"), v = e.blocks ?? {};
|
|
38
|
+
if (!v || typeof v != "object") throw TypeError("NoiseTerrainOptions.blocks must be an object of names");
|
|
39
|
+
let y = a.map((e) => {
|
|
40
|
+
let t = v[e] ?? e;
|
|
41
|
+
if (typeof t != "string") throw TypeError(`NoiseTerrainOptions.blocks.${e} must be a block type name`);
|
|
42
|
+
return t;
|
|
43
|
+
}), b = new n(t), x = new n(t ^ 1540483477), S = new n(t ^ 461845907), C = r((e, t) => b.simplex2(e, t), {
|
|
44
|
+
octaves: 5,
|
|
45
|
+
frequency: 1 / h
|
|
46
|
+
}), w = (e, t) => Math.floor(f + m * C(e, t)), T = .3 * g, E = _ * 4294967296;
|
|
47
|
+
return (e, n, r, l, u) => {
|
|
48
|
+
if (!Array.isArray(u?.blocks)) throw TypeError("noiseTerrain needs context {blocks}: the world BlockTypes table");
|
|
49
|
+
let f = new Map(u.blocks.map((e) => [e.name, e.id])), [m, h, g, v, b, C, D] = y.map((e, t) => {
|
|
50
|
+
let n = f.get(e);
|
|
51
|
+
if (n === void 0 && (t < 5 || _ > 0)) throw RangeError(`noiseTerrain block "${e}" is not defined; define it with blocks.define("${e}") or name another type in NoiseTerrainOptions.blocks.${a[t]}`);
|
|
52
|
+
return n ?? 0;
|
|
53
|
+
}), O = n * 32, k = r * 32, A = l * 32, j = k + 31, M = /* @__PURE__ */ new Int32Array(1600), N = /* @__PURE__ */ new Uint8Array(1600), P = (e, t) => {
|
|
54
|
+
let n = e + t * c;
|
|
55
|
+
return N[n] || (N[n] = 1, M[n] = w(O + e - s, A + t - s)), M[n];
|
|
56
|
+
}, F = Infinity, I = -Infinity, L = -Infinity;
|
|
57
|
+
for (let e = 2; e < 38; e++) for (let t = 2; t < 38; t++) {
|
|
58
|
+
let n = P(t, e);
|
|
59
|
+
L = Math.max(L, n), t >= s && e >= s && t < 36 && e < 36 && (F = Math.min(F, n), I = Math.max(I, n));
|
|
60
|
+
}
|
|
61
|
+
let R = Math.max(E > 0 ? L + 7 : I, i);
|
|
62
|
+
if (j < F - 3) e.fill(m);
|
|
63
|
+
else if (k > I && j <= i) e.fill(b);
|
|
64
|
+
else if (k <= Math.max(I, i)) for (let t = 0; t < 32; t++) for (let n = 0; n < 32; n++) {
|
|
65
|
+
let r = M[n + s + (t + s) * c], a = Math.min(j, Math.max(r, i));
|
|
66
|
+
for (let o = k; o <= a; o++) e.set(n, o - k, t, o < r - 3 ? m : o < r ? h : o === r ? r <= i + 1 ? v : g : b);
|
|
67
|
+
}
|
|
68
|
+
if (T > 0 && k < I) {
|
|
69
|
+
let t = /* @__PURE__ */ new Float64Array(729), n = /* @__PURE__ */ new Float64Array(729);
|
|
70
|
+
for (let e = 0, r = 0; e < 9; e++) for (let i = 0; i < 9; i++) for (let a = 0; a < 9; a++, r++) {
|
|
71
|
+
let o = (O + 4 * a) / 64, s = (k + 4 * i) / 40, c = (A + 4 * e) / 64;
|
|
72
|
+
t[r] = x.simplex(o, s, c), n[r] = S.simplex(o, s, c);
|
|
73
|
+
}
|
|
74
|
+
for (let r = 0; r < 32; r++) for (let a = 0; a < 32; a++) {
|
|
75
|
+
let o = a + s + (r + s) * c, l = Math.min(31, M[o] - 1 - k), u = Math.min(M[o - 1], M[o + 1], M[o - c], M[o + c]);
|
|
76
|
+
for (let o = 0; o <= l; o++) o + k > u && o + k <= i || Math.abs(p(t, a, o, r)) < T && Math.abs(p(n, a, o, r)) < T && e.set(a, o, r, 0);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (E > 0) for (let n = 2; n < 38; n++) for (let r = 2; r < 38; r++) {
|
|
80
|
+
let a = O + r - s, l = A + n - s, u = d(a, l, t), f = M[r + n * c];
|
|
81
|
+
if (u >= E || f <= i + 1) continue;
|
|
82
|
+
let p = f + 4 + u % 3, m = a - O, h = l - A;
|
|
83
|
+
if (p + 1 < k || f + 1 > j) continue;
|
|
84
|
+
let g = !0;
|
|
85
|
+
for (let e = -2; e <= o && g; e++) for (let t = -2; t <= o; t++) Math.abs(t) + Math.abs(e) < 4 && P(r + t, n + e) >= p - 2 && (g = !1);
|
|
86
|
+
if (!g) continue;
|
|
87
|
+
let _ = (t, n, r, i) => {
|
|
88
|
+
if ((t & 31) !== t || (n & 31) !== n || (r & 31) !== r) return;
|
|
89
|
+
let a = e.get(t, n, r);
|
|
90
|
+
(a === 0 || i === C && a === D) && e.set(t, n, r, i);
|
|
91
|
+
};
|
|
92
|
+
for (let e = -2; e <= 1; e++) for (let t = -2; t <= o; t++) for (let n = -2; n <= o; n++) (e < 0 ? Math.abs(n) + Math.abs(t) < 4 : Math.abs(n) + Math.abs(t) < 2) && _(m + n, p + e - k, h + t, D);
|
|
93
|
+
for (let e = f + 1; e <= p; e++) _(m, e - k, h, C);
|
|
94
|
+
}
|
|
95
|
+
return R;
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
//#endregion
|
|
99
|
+
export { m as noiseTerrain, i as serveVoxelGenerator };
|
package/dist/gi.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { Engine } from '@margoengine/core';
|
|
3
|
+
import type { Vec3 } from '@margoengine/math';
|
|
4
|
+
import type { VoxelWorld } from './world.js';
|
|
5
|
+
/** Options of {@link VoxelGI.attach}. */
|
|
6
|
+
export interface VoxelGIOptions {
|
|
7
|
+
/** The world whose blocks cast and bounce the light. */
|
|
8
|
+
world: VoxelWorld;
|
|
9
|
+
/** Linear albedo of block types by name, each channel 0 to 1, for the light they bounce. Default 0.5 grey. */
|
|
10
|
+
albedo?: Record<string, Vec3>;
|
|
11
|
+
/** Strength of the bounce light, 0 to 8. Live. Default 1. */
|
|
12
|
+
strength?: number;
|
|
13
|
+
/** Half-angle of the sun's cone in radians, 0 to 0.5: wider gives softer shadow edges. Default 0.05. */
|
|
14
|
+
softness?: number;
|
|
15
|
+
/** Open cells traced per update, 4,096 to 262,144: all 262,144 cells of the window are refreshed every `262144 / cells` updates. Default 32,768. */
|
|
16
|
+
cells?: number;
|
|
17
|
+
/** Bounce rays per traced cell per update, 1 to 16. Default 4. */
|
|
18
|
+
rays?: number;
|
|
19
|
+
/** Longest ray in cells, 4 to 64. Default 32. */
|
|
20
|
+
range?: number;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Ray-traced bounce light and soft sun shadows for a `VoxelRenderer`, from the world's blocks themselves: add it to the
|
|
24
|
+
* renderer and call {@link VoxelGI.update} each frame. The light follows the engine's directional light
|
|
25
|
+
* (`engine.lighting`) and works inside a 64 x 64 x 64 window of cells around the camera. Added with
|
|
26
|
+
* {@link VoxelGI.add}, it follows the renderer's `origin` (worlds far from zero).
|
|
27
|
+
*/
|
|
28
|
+
export declare class VoxelGI {
|
|
29
|
+
#private;
|
|
30
|
+
readonly world: VoxelWorld;
|
|
31
|
+
/** Bounce light strength, 0 to 8. Live; update() throws RangeError for any other value. */
|
|
32
|
+
strength: number;
|
|
33
|
+
/** Engine this effect belongs to (the renderer checks it). */
|
|
34
|
+
readonly owner: Engine;
|
|
35
|
+
private constructor();
|
|
36
|
+
/**
|
|
37
|
+
* @throws TypeError when `engine` is not an Engine or `world` is not a VoxelWorld.
|
|
38
|
+
* @throws RangeError for an option out of range or an albedo that is not [r, g, b] in 0..1.
|
|
39
|
+
* @throws MargoError `unsupported` without the WebGPU backend.
|
|
40
|
+
*/
|
|
41
|
+
static attach(engine: Engine, options: VoxelGIOptions): VoxelGI;
|
|
42
|
+
/**
|
|
43
|
+
* Adds the effect to a `VoxelRenderer` (its `addLighting`) and follows that renderer's `origin` from then on: the
|
|
44
|
+
* window is placed around the camera's world cell and the shading reads it in engine space. Returns the remover.
|
|
45
|
+
*/
|
|
46
|
+
add(voxels: {
|
|
47
|
+
addLighting(effect: VoxelGI): () => void;
|
|
48
|
+
}): () => void;
|
|
49
|
+
/** Lowest corner of the window in world cells, once the first update placed it. */
|
|
50
|
+
get origin(): Vec3 | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* Follows the camera (the window moves in steps of 32 cells), uploads the window's blocks when they changed, and
|
|
53
|
+
* traces the next `cells` cells of the window. Call it once a frame, before rendering. Returns the cells traced.
|
|
54
|
+
* @throws MargoError `disposed` after `dispose()`.
|
|
55
|
+
* @throws RangeError when the renderer's origin holds a value that is not a finite number, or `strength` is not
|
|
56
|
+
* from 0 to 8.
|
|
57
|
+
*/
|
|
58
|
+
update(): number;
|
|
59
|
+
/**
|
|
60
|
+
* The renderer's lighting hooks: bounce light and the soft sun term. Undefined until the first `update()`, and again
|
|
61
|
+
* after a device restore until the next `update()` makes the volumes on the new device.
|
|
62
|
+
*/
|
|
63
|
+
pbr(group: number): {
|
|
64
|
+
declarations: string;
|
|
65
|
+
light: string;
|
|
66
|
+
direct(d: string): string;
|
|
67
|
+
} | undefined;
|
|
68
|
+
/**
|
|
69
|
+
* Bind group entries of the hooks: the four volumes, their sampler and the window. Empty while `pbr` is undefined,
|
|
70
|
+
* so a frame drawn after a device restore and before the next `update()` binds nothing of the lost device.
|
|
71
|
+
*/
|
|
72
|
+
bindings(): GPUBindGroupEntry[];
|
|
73
|
+
/** Stops following the world and frees the GPU resources. */
|
|
74
|
+
dispose(): void;
|
|
75
|
+
[Symbol.dispose](): void;
|
|
76
|
+
}
|