@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
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Samples per axis: local -1..33. Index (x + 1) + 35 (z + 1) + 1225 (y + 1). */
|
|
2
|
+
export declare const G = 35;
|
|
3
|
+
/** One material's triangles. */
|
|
4
|
+
export interface ClipPart {
|
|
5
|
+
positions: Float32Array;
|
|
6
|
+
normals: Float32Array;
|
|
7
|
+
indices: Uint32Array;
|
|
8
|
+
}
|
|
9
|
+
/** A box in local cells (inclusive faces) and the faces that get a cap: bit 2a is the low face of axis a, 2a + 1 the high. */
|
|
10
|
+
export interface ClipBox {
|
|
11
|
+
lo: readonly [number, number, number];
|
|
12
|
+
hi: readonly [number, number, number];
|
|
13
|
+
skirts: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Meshes the samples with Surface Nets, keeps the parts of the triangles inside each box (a triangle cut by a face is
|
|
17
|
+
* clipped there, Sutherland and Hodgman 1974, so the cut points lie exactly on the face), and closes every face whose
|
|
18
|
+
* bit is set with a cap reaching up to `skirt` cells from the border (see closeFaces): with `skirt` at least the face's
|
|
19
|
+
* size the mesh of each box bounds a closed solid. Positions are local cells times `scale`.
|
|
20
|
+
*/
|
|
21
|
+
export declare function meshClipped(density: Float32Array, material: Uint8Array, boxes: readonly ClipBox[], skirt: number, scale: number): {
|
|
22
|
+
parts: Map<number, ClipPart>;
|
|
23
|
+
triangles: number;
|
|
24
|
+
};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { Engine, PbrMaterial } from '@margoengine/core';
|
|
3
|
+
import type { Vec3 } from '@margoengine/math';
|
|
4
|
+
import type { VoxelOrigin } from './render.js';
|
|
5
|
+
import { type DensityField, type SmoothChunkMesh, type SmoothMeshPart, SmoothTerrain } from './smooth.js';
|
|
6
|
+
/** Options of {@link SmoothLod.attach}. */
|
|
7
|
+
export interface SmoothLodOptions {
|
|
8
|
+
/** The real terrain (level 0): level 1 leaves out the 32-block boxes of its loaded chunks. */
|
|
9
|
+
terrain: SmoothTerrain;
|
|
10
|
+
/** The density in world blocks, positive inside: the field the real chunks were filled from. */
|
|
11
|
+
density: DensityField;
|
|
12
|
+
/** Material per world block, 0 to 255. Default 0 everywhere. */
|
|
13
|
+
material?: DensityField;
|
|
14
|
+
/** Core material per material index, as `SmoothTerrainView`. Needed unless `createMesh` is given. */
|
|
15
|
+
materials?: readonly PbrMaterial[];
|
|
16
|
+
/**
|
|
17
|
+
* Makes the mesh of one node part in place of a core mesh, as `SmoothTerrainView` `createMesh`; `part` is in the
|
|
18
|
+
* node's own blocks, `origin` its corner in engine space and `level` its level. Default: a core mesh with
|
|
19
|
+
* `materials[material]` (or `materials[0]`).
|
|
20
|
+
*/
|
|
21
|
+
createMesh?: (part: SmoothMeshPart, material: number, origin: Vec3, level: number) => {
|
|
22
|
+
dispose(): void;
|
|
23
|
+
};
|
|
24
|
+
/** Far levels, an integer in 1..12. Default 4. */
|
|
25
|
+
levels?: number;
|
|
26
|
+
/**
|
|
27
|
+
* Nodes of a level from the eye before the next coarser level takes over, an integer in 2..32, as `VoxelLod`: the
|
|
28
|
+
* view reaches `radius * 32 * 2^levels` blocks. Keep `2 * radius` above the streamed chunk distance + 1. Default 8.
|
|
29
|
+
*/
|
|
30
|
+
radius?: number;
|
|
31
|
+
/** Lowest block y the far levels hold. Default -64. */
|
|
32
|
+
minY?: number;
|
|
33
|
+
/** Highest block y the far levels hold. Default 255. */
|
|
34
|
+
maxY?: number;
|
|
35
|
+
/** Most nodes meshed per {@link SmoothLod.update}, an integer >= 1 or Infinity. Default 8. */
|
|
36
|
+
budget?: number;
|
|
37
|
+
/**
|
|
38
|
+
* How far each face's cap reaches from the node's border into the solid, in cells of the node's level, 0 to 32.
|
|
39
|
+
* Default 32: the whole face, so each node mesh bounds a closed solid and every seam is closed whatever the ground does
|
|
40
|
+
* there. A smaller depth draws fewer hidden triangles but can leave a gap where the ground runs nearly parallel to a
|
|
41
|
+
* node face; 0 leaves the faces open.
|
|
42
|
+
*/
|
|
43
|
+
skirt?: number;
|
|
44
|
+
/** Origin rebasing, as `SmoothTerrainViewOptions.origin`. Default null. */
|
|
45
|
+
origin?: VoxelOrigin | null;
|
|
46
|
+
}
|
|
47
|
+
/** What a {@link SmoothLod} holds. */
|
|
48
|
+
export interface SmoothLodStats {
|
|
49
|
+
/** Nodes on show per level (index 0 is level 1). */
|
|
50
|
+
readonly nodes: readonly number[];
|
|
51
|
+
/** Wanted nodes not meshed yet, and level-1 nodes waiting to be meshed again for the real terrain. */
|
|
52
|
+
readonly pending: number;
|
|
53
|
+
/** Meshes drawn. */
|
|
54
|
+
readonly meshes: number;
|
|
55
|
+
}
|
|
56
|
+
/** Options of {@link meshSmoothLodNode}. */
|
|
57
|
+
export interface SmoothLodNodeOptions {
|
|
58
|
+
/** Material per world block. Default 0. */
|
|
59
|
+
material?: DensityField;
|
|
60
|
+
/** Cap depth in cells of the level, 0 to 32, as {@link SmoothLodOptions.skirt}. Default 32 (closed). */
|
|
61
|
+
skirt?: number;
|
|
62
|
+
/**
|
|
63
|
+
* Bit k (0..7) set leaves out octant k (x = bit 0, y = bit 1, z = bit 2), 16 cells per axis: the level-1 boxes the real
|
|
64
|
+
* terrain draws. Default 0.
|
|
65
|
+
*/
|
|
66
|
+
mask?: number;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Meshes node (x, y, z) of far level `level` (1..12): `density` sampled every 2^level blocks from the node's corner
|
|
70
|
+
* (32 x 2^level, ...), one sample past each face, divided by 2^level and clamped to -1..1 so it stays about the distance
|
|
71
|
+
* in cells; Surface Nets, the triangles clipped to the node's box (or to its octants left in by `mask`), caps on every
|
|
72
|
+
* box face. Positions are in the node's own blocks.
|
|
73
|
+
* @throws TypeError when `density` or `material` is not a function; RangeError for a level outside 1..12, node
|
|
74
|
+
* coordinates that are not whole numbers, a skirt outside 0 to 32, a mask outside 0..255, a NaN density or a material
|
|
75
|
+
* outside 0 to 255.
|
|
76
|
+
*/
|
|
77
|
+
export declare function meshSmoothLodNode(density: DensityField, level: number, x: number, y: number, z: number, options?: SmoothLodNodeOptions): SmoothChunkMesh;
|
|
78
|
+
/**
|
|
79
|
+
* Far levels of detail for a {@link SmoothTerrain}: coarse Surface Nets meshes of the same density field around the
|
|
80
|
+
* eye, each level's cells twice the size of the one before, drawn as core meshes (or by `createMesh`). Call
|
|
81
|
+
* {@link SmoothLod.update} every frame. Draw the real terrain with `SmoothTerrainView` option `skirt` so its border
|
|
82
|
+
* with level 1 is closed too.
|
|
83
|
+
*/
|
|
84
|
+
export declare class SmoothLod {
|
|
85
|
+
#private;
|
|
86
|
+
/** Far levels, coarsest last: `scale` is blocks per cell, 2^level. */
|
|
87
|
+
readonly levels: readonly {
|
|
88
|
+
readonly level: number;
|
|
89
|
+
readonly scale: number;
|
|
90
|
+
}[];
|
|
91
|
+
readonly terrain: SmoothTerrain;
|
|
92
|
+
/** Live {@link SmoothLodOptions.origin}; null for none. */
|
|
93
|
+
origin: VoxelOrigin | null;
|
|
94
|
+
/** Most nodes meshed per update. Live. */
|
|
95
|
+
budget: number;
|
|
96
|
+
private constructor();
|
|
97
|
+
/**
|
|
98
|
+
* @throws TypeError when `engine` is not an Engine, `terrain` is not a SmoothTerrain, `density`, `material` or
|
|
99
|
+
* `createMesh` is not a function, `materials` is empty without `createMesh`, or `origin` is not null or an object
|
|
100
|
+
* with `origin`.
|
|
101
|
+
* @throws RangeError for an option out of range, or `minY` above `maxY`.
|
|
102
|
+
*/
|
|
103
|
+
static attach(engine: Engine, options: SmoothLodOptions): SmoothLod;
|
|
104
|
+
/** Nodes on show per level, wanted nodes still to mesh, and meshes drawn. */
|
|
105
|
+
get stats(): SmoothLodStats;
|
|
106
|
+
/**
|
|
107
|
+
* Chooses the nodes for `eye` (world blocks; default the engine camera plus the origin), meshes up to `budget` of the
|
|
108
|
+
* missing ones nearest first (level-1 nodes whose real chunks loaded or unloaded count too), and drops a node no
|
|
109
|
+
* longer wanted once no missing node overlaps it, so a level change never opens a hole. Moves the meshes when the
|
|
110
|
+
* origin moved. Returns the nodes meshed: 0 once everything is current.
|
|
111
|
+
* @throws MargoError `disposed` after dispose(); TypeError when `eye` is not three finite numbers; errors of the
|
|
112
|
+
* fields propagate.
|
|
113
|
+
*/
|
|
114
|
+
update(eye?: Vec3): number;
|
|
115
|
+
/** Removes every node mesh. */
|
|
116
|
+
dispose(): void;
|
|
117
|
+
[Symbol.dispose](): void;
|
|
118
|
+
}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { n as e, t } from "./origin-DsMelUxs.js";
|
|
5
|
+
import { i as n, t as r } from "./smooth-CEwHkXli.js";
|
|
6
|
+
import { MargoError as i } from "@margoengine/math/errors";
|
|
7
|
+
import { Geometry as a } from "@margoengine/core";
|
|
8
|
+
//#region src/smooth-lod.ts
|
|
9
|
+
var o = (e, t, n, r) => {
|
|
10
|
+
if (typeof t != "number") throw TypeError(`SmoothLod ${e} must be a number`);
|
|
11
|
+
if (!(t === r || Number.isInteger(t) && t >= n && t <= r)) throw RangeError(`SmoothLod ${e} is ${t}; pass an integer in ${n}..${r}`);
|
|
12
|
+
return t;
|
|
13
|
+
}, s = (e) => {
|
|
14
|
+
if (typeof e != "number" || !(e >= 0 && e <= 32)) throw RangeError(`SmoothLod skirt is ${e}; pass a depth from 0 to 32 cells`);
|
|
15
|
+
return e;
|
|
16
|
+
}, c = (e, t, n) => Math.max(t - e, e - (t + n), 0);
|
|
17
|
+
function l(e, t, r, i, a, o = {}) {
|
|
18
|
+
if (typeof e != "function") throw TypeError("meshSmoothLodNode density must be a function (x, y, z)");
|
|
19
|
+
if (!Number.isInteger(t) || t < 1 || t > 12) throw RangeError(`meshSmoothLodNode level is ${t}; pass an integer in 1..12`);
|
|
20
|
+
if (![
|
|
21
|
+
r,
|
|
22
|
+
i,
|
|
23
|
+
a
|
|
24
|
+
].every(Number.isSafeInteger)) throw RangeError(`meshSmoothLodNode node ${r},${i},${a}; pass whole numbers`);
|
|
25
|
+
let { material: c, skirt: l = 32, mask: u = 0 } = o ?? {};
|
|
26
|
+
if (c !== void 0 && typeof c != "function") throw TypeError("meshSmoothLodNode material must be a function (x, y, z)");
|
|
27
|
+
if (s(l), !Number.isInteger(u) || u < 0 || u > 255) throw RangeError(`meshSmoothLodNode mask is ${u}; pass an integer in 0..255`);
|
|
28
|
+
let d = 2 ** t, f = /* @__PURE__ */ new Float32Array(42875), p = /* @__PURE__ */ new Uint8Array(42875), m = r * 32 * d, h = i * 32 * d, g = a * 32 * d;
|
|
29
|
+
for (let t = 0, n = 0; t < 35; t++) for (let r = 0; r < 35; r++) for (let i = 0; i < 35; i++, n++) {
|
|
30
|
+
let a = m + (i - 1) * d, o = h + (t - 1) * d, s = g + (r - 1) * d, l = e(a, o, s);
|
|
31
|
+
if (typeof l != "number" || Number.isNaN(l)) throw RangeError(`SmoothLod density is ${l}; return a number (positive inside)`);
|
|
32
|
+
if (f[n] = Math.max(-1, Math.min(1, l / d)), c) {
|
|
33
|
+
let e = c(a, o, s);
|
|
34
|
+
if (!Number.isInteger(e) || e < 0 || e > 255) throw RangeError(`SmoothLod material is ${e}; return a whole number from 0 to 255`);
|
|
35
|
+
p[n] = e;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
let _ = [];
|
|
39
|
+
if (!u) _.push({
|
|
40
|
+
lo: [
|
|
41
|
+
0,
|
|
42
|
+
0,
|
|
43
|
+
0
|
|
44
|
+
],
|
|
45
|
+
hi: [
|
|
46
|
+
32,
|
|
47
|
+
32,
|
|
48
|
+
32
|
|
49
|
+
],
|
|
50
|
+
skirts: 63
|
|
51
|
+
});
|
|
52
|
+
else for (let e = 0; e < 8; e++) {
|
|
53
|
+
if (u & 1 << e) continue;
|
|
54
|
+
let t = [
|
|
55
|
+
(e & 1) * 16,
|
|
56
|
+
(e >> 1 & 1) * 16,
|
|
57
|
+
(e >> 2) * 16
|
|
58
|
+
], n = 0;
|
|
59
|
+
for (let t = 0; t < 6; t++) {
|
|
60
|
+
let r = t >> 1, i = r === 0 ? 1 : r === 1 ? 2 : 4;
|
|
61
|
+
(!(t & 1 ? !(e & i) : e & i) || u & 1 << (e ^ i)) && (n |= 1 << t);
|
|
62
|
+
}
|
|
63
|
+
_.push({
|
|
64
|
+
lo: t,
|
|
65
|
+
hi: [
|
|
66
|
+
t[0] + 16,
|
|
67
|
+
t[1] + 16,
|
|
68
|
+
t[2] + 16
|
|
69
|
+
],
|
|
70
|
+
skirts: n
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return n(f, p, _, l, d);
|
|
74
|
+
}
|
|
75
|
+
var u = class n {
|
|
76
|
+
#e;
|
|
77
|
+
#t;
|
|
78
|
+
#n;
|
|
79
|
+
#r;
|
|
80
|
+
#i;
|
|
81
|
+
#a;
|
|
82
|
+
#o;
|
|
83
|
+
#s;
|
|
84
|
+
#c;
|
|
85
|
+
#l;
|
|
86
|
+
#u = 0;
|
|
87
|
+
#d = [
|
|
88
|
+
0,
|
|
89
|
+
0,
|
|
90
|
+
0
|
|
91
|
+
];
|
|
92
|
+
#f = !1;
|
|
93
|
+
constructor(e, n) {
|
|
94
|
+
let { levels: r = 4, radius: i = 8, minY: a = -64, maxY: c = 255, budget: l = 8, skirt: u = 32 } = n;
|
|
95
|
+
if (o("levels", r, 1, 12), this.#a = o("radius", i, 2, 32), this.#o = o("minY", a, -32768, 32767), this.#s = o("maxY", c, -32768, 32767), a > c) throw RangeError(`SmoothLod minY ${a} is above maxY ${c}; pass minY <= maxY`);
|
|
96
|
+
this.budget = o("budget", l, 1, Infinity), this.#c = s(u), this.#e = e, this.terrain = n.terrain, this.#t = n.density, this.#n = n.material, this.#r = n.materials ?? [], this.#i = n.createMesh, this.origin = t(n.origin, "SmoothLod"), this.levels = Array.from({ length: r }, (e, t) => ({
|
|
97
|
+
level: t + 1,
|
|
98
|
+
scale: 2 ** (t + 1)
|
|
99
|
+
})), this.#l = this.levels.map(() => /* @__PURE__ */ new Map());
|
|
100
|
+
}
|
|
101
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
102
|
+
if (typeof e?.createMesh != "function") throw TypeError("SmoothLod.attach expects an Engine");
|
|
103
|
+
if (!t || typeof t != "object") throw TypeError("SmoothLod expects {terrain, density, materials}");
|
|
104
|
+
if (!(t.terrain instanceof r)) throw TypeError("SmoothLod terrain must be a SmoothTerrain");
|
|
105
|
+
if (typeof t.density != "function") throw TypeError("SmoothLod density must be a function (x, y, z) => number, the field of the real terrain");
|
|
106
|
+
if (t.material !== void 0 && typeof t.material != "function") throw TypeError("SmoothLod material must be a function (x, y, z) => material");
|
|
107
|
+
if (t.createMesh !== void 0 && typeof t.createMesh != "function") throw TypeError("SmoothLod createMesh must be a function (part, material, origin, level) => mesh");
|
|
108
|
+
if (!t.createMesh && (!Array.isArray(t.materials) || !t.materials.length)) throw TypeError("SmoothLod materials must be a non-empty array of PbrMaterial, or pass createMesh");
|
|
109
|
+
return new n(e, t);
|
|
110
|
+
}
|
|
111
|
+
get stats() {
|
|
112
|
+
let e = 0;
|
|
113
|
+
return {
|
|
114
|
+
nodes: this.#l.map((t) => {
|
|
115
|
+
for (let n of t.values()) e += n.meshes.length;
|
|
116
|
+
return t.size;
|
|
117
|
+
}),
|
|
118
|
+
pending: this.#u,
|
|
119
|
+
meshes: e
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
update(t) {
|
|
123
|
+
if (this.#f) throw new i("disposed", "SmoothLod is disposed; attach a new one");
|
|
124
|
+
let n = e(this.origin, "SmoothLod");
|
|
125
|
+
if (n.some((e, t) => e !== this.#d[t])) {
|
|
126
|
+
this.#d = n;
|
|
127
|
+
for (let e = 0; e < this.#l.length; e++) for (let t of this.#l[e].values()) {
|
|
128
|
+
let r = 32 << e + 1;
|
|
129
|
+
t.meshes.every((e) => {
|
|
130
|
+
let i = e.position;
|
|
131
|
+
return typeof i?.set == "function" && (i.set(t.x * r - n[0], t.y * r - n[1], t.z * r - n[2]), !0);
|
|
132
|
+
}) || (t.mask = -1);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
let r = this.#e.camera.position, a = t ?? [
|
|
136
|
+
r[0] + n[0],
|
|
137
|
+
r[1] + n[1],
|
|
138
|
+
r[2] + n[2]
|
|
139
|
+
], o = Number(a[0]), s = Number(a[1]), l = Number(a[2]);
|
|
140
|
+
if (![
|
|
141
|
+
o,
|
|
142
|
+
s,
|
|
143
|
+
l
|
|
144
|
+
].every(Number.isFinite)) throw TypeError("SmoothLod.update expects the eye as [x, y, z]");
|
|
145
|
+
let u = this.levels.map(() => /* @__PURE__ */ new Map()), d = this.#a, f = this.levels.length, p = (e, t, n, r) => {
|
|
146
|
+
let i = 32 << e, a = Math.max(c(o, t * i, i), c(s, n * i, i), c(l, r * i, i));
|
|
147
|
+
if (e >= 2 && a < d * i / 2) {
|
|
148
|
+
for (let i = 0; i < 8; i++) p(e - 1, 2 * t + (i & 1), 2 * n + (i >> 1 & 1), 2 * r + (i >> 2));
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
if ((n + 1) * i <= this.#o || n * i > this.#s) return;
|
|
152
|
+
let f = `${t},${n},${r}`;
|
|
153
|
+
u[e - 1].set(f, {
|
|
154
|
+
level: e,
|
|
155
|
+
x: t,
|
|
156
|
+
y: n,
|
|
157
|
+
z: r,
|
|
158
|
+
key: f,
|
|
159
|
+
d: a
|
|
160
|
+
});
|
|
161
|
+
}, m = 32 << f, h = d * m, g = (e) => Math.floor((e - h) / m), _ = (e) => Math.floor((e + h) / m), v = Math.max(g(s), Math.floor(this.#o / m)), y = Math.min(_(s), Math.floor(this.#s / m));
|
|
162
|
+
for (let e = g(o); e <= _(o); e++) for (let t = g(l); t <= _(l); t++) for (let n = v; n <= y; n++) Math.max(c(o, e * m, m), c(s, n * m, m), c(l, t * m, m)) < h && p(f, e, n, t);
|
|
163
|
+
let b = [];
|
|
164
|
+
for (let e = 0; e < f; e++) for (let t of u[e].values()) {
|
|
165
|
+
let n = this.#l[e].get(t.key);
|
|
166
|
+
(!n || n.mask !== (e === 0 ? this.#p(t.x, t.y, t.z) : 0)) && b.push(t);
|
|
167
|
+
}
|
|
168
|
+
b.sort((e, t) => e.d - t.d);
|
|
169
|
+
let x = 0;
|
|
170
|
+
for (let e of b) {
|
|
171
|
+
if (x >= this.budget) break;
|
|
172
|
+
this.#m(e, n), x++;
|
|
173
|
+
}
|
|
174
|
+
let S = b.filter((e) => !this.#l[e.level - 1].get(e.key));
|
|
175
|
+
this.#u = b.length - x;
|
|
176
|
+
for (let e = 0; e < f; e++) {
|
|
177
|
+
let t = 32 << e + 1;
|
|
178
|
+
for (let [n, r] of this.#l[e]) {
|
|
179
|
+
if (u[e].has(n)) continue;
|
|
180
|
+
let i = r.x * t, a = r.y * t, o = r.z * t;
|
|
181
|
+
S.some((e) => {
|
|
182
|
+
let n = 32 << e.level;
|
|
183
|
+
return e.x * n < i + t && i < (e.x + 1) * n && e.y * n < a + t && a < (e.y + 1) * n && e.z * n < o + t && o < (e.z + 1) * n;
|
|
184
|
+
}) || this.#h(e, n);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return x;
|
|
188
|
+
}
|
|
189
|
+
#p(e, t, n) {
|
|
190
|
+
let r = 0;
|
|
191
|
+
for (let i = 0; i < 8; i++) this.terrain.getChunk(2 * e + (i & 1), 2 * t + (i >> 1 & 1), 2 * n + (i >> 2)) && (r |= 1 << i);
|
|
192
|
+
return r;
|
|
193
|
+
}
|
|
194
|
+
#m({ level: e, x: t, y: n, z: r, key: i }, o) {
|
|
195
|
+
let s = e === 1 ? this.#p(t, n, r) : 0;
|
|
196
|
+
this.#h(e - 1, i);
|
|
197
|
+
let c = {
|
|
198
|
+
x: t,
|
|
199
|
+
y: n,
|
|
200
|
+
z: r,
|
|
201
|
+
mask: s,
|
|
202
|
+
meshes: []
|
|
203
|
+
};
|
|
204
|
+
if (this.#l[e - 1].set(i, c), s === 255) return;
|
|
205
|
+
let u = l(this.#t, e, t, n, r, {
|
|
206
|
+
material: this.#n,
|
|
207
|
+
skirt: this.#c,
|
|
208
|
+
mask: s
|
|
209
|
+
}), d = 32 << e, f = [
|
|
210
|
+
t * d - o[0],
|
|
211
|
+
n * d - o[1],
|
|
212
|
+
r * d - o[2]
|
|
213
|
+
];
|
|
214
|
+
for (let [t, n] of u.parts) {
|
|
215
|
+
if (n.positions.length < 9) continue;
|
|
216
|
+
if (this.#i) {
|
|
217
|
+
let r = this.#i(n, t, f, e);
|
|
218
|
+
if (typeof r?.dispose != "function") throw TypeError("SmoothLod createMesh must return an object with dispose()");
|
|
219
|
+
c.meshes.push(r);
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
let r = this.#e.createMesh().setGeometry(new a(n.positions, n.normals, n.indices)).setMaterial(this.#r[t] ?? this.#r[0]);
|
|
223
|
+
r.position.set(f[0], f[1], f[2]), c.meshes.push(r);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
#h(e, t) {
|
|
227
|
+
let n = this.#l[e].get(t);
|
|
228
|
+
if (n) {
|
|
229
|
+
for (let e of n.meshes) e.dispose();
|
|
230
|
+
this.#l[e].delete(t);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
dispose() {
|
|
234
|
+
if (!this.#f) {
|
|
235
|
+
this.#f = !0;
|
|
236
|
+
for (let e = 0; e < this.#l.length; e++) for (let t of [...this.#l[e].keys()]) this.#h(e, t);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
[Symbol.dispose]() {
|
|
240
|
+
this.dispose();
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
//#endregion
|
|
244
|
+
export { u as SmoothLod, l as meshSmoothLodNode };
|
package/dist/smooth.d.ts
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { Engine, PbrMaterial } from '@margoengine/core';
|
|
3
|
+
import type { Vec3 } from '@margoengine/math';
|
|
4
|
+
import type { VoxelOrigin } from './render.js';
|
|
5
|
+
/** One chunk of a {@link SmoothTerrain}: 32 x 32 x 32 densities and materials, index `x + 32 z + 1024 y`. */
|
|
6
|
+
export interface SmoothChunk {
|
|
7
|
+
/** Density in 1/127ths: below 0 air, above 0 solid, the surface where it crosses 0. */
|
|
8
|
+
readonly density: Int8Array;
|
|
9
|
+
/** Material index per cell, 0 to 255 (the view's `materials`). */
|
|
10
|
+
readonly material: Uint8Array;
|
|
11
|
+
}
|
|
12
|
+
/** Options of {@link SmoothTerrain}. */
|
|
13
|
+
export interface SmoothTerrainOptions {
|
|
14
|
+
/** Lowest and highest chunk y, inclusive. Default -4 and 7 (y -128 to 255). */
|
|
15
|
+
minChunkY?: number;
|
|
16
|
+
maxChunkY?: number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A density field `(x, y, z) => density` for {@link SmoothTerrain.addChunk}: positive inside, negative outside, about
|
|
20
|
+
* the signed distance in blocks near the surface (clamped to -1..1 when stored). A material function picks each cell's
|
|
21
|
+
* material.
|
|
22
|
+
*/
|
|
23
|
+
export type DensityField = (x: number, y: number, z: number) => number;
|
|
24
|
+
/**
|
|
25
|
+
* Smooth voxel terrain: chunks of signed densities and materials, loaded and unloaded by chunk, edited by sculpting.
|
|
26
|
+
* Every change marks the chunks whose mesh it touches in {@link SmoothTerrain.dirty} (a chunk and the neighbours that
|
|
27
|
+
* read its border cells), which {@link SmoothTerrainView} meshes.
|
|
28
|
+
*/
|
|
29
|
+
export declare class SmoothTerrain {
|
|
30
|
+
#private;
|
|
31
|
+
/** Keys (`'cx,cy,cz'`) of chunks to mesh again. A removed chunk's key stays until its mesh is dropped. */
|
|
32
|
+
readonly dirty: Set<string>;
|
|
33
|
+
readonly minChunkY: number;
|
|
34
|
+
readonly maxChunkY: number;
|
|
35
|
+
/** @throws RangeError when the chunk y range is not whole numbers with min <= max. */
|
|
36
|
+
constructor(options?: SmoothTerrainOptions);
|
|
37
|
+
/** Keys of the loaded chunks. */
|
|
38
|
+
keys(): IterableIterator<string>;
|
|
39
|
+
/** The chunk at chunk coordinates, or undefined when it is not loaded. */
|
|
40
|
+
getChunk(cx: number, cy: number, cz: number): SmoothChunk | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* Loads chunk (cx, cy, cz), filled from `density` (and `material`, default 0) sampled at each cell's integer world
|
|
43
|
+
* coordinates, or all air. Replaces a loaded chunk.
|
|
44
|
+
* @throws TypeError when `density` or `material` is not a function.
|
|
45
|
+
* @throws RangeError for coordinates outside the chunk range or a material outside 0 to 255.
|
|
46
|
+
*/
|
|
47
|
+
addChunk(cx: number, cy: number, cz: number, density?: DensityField, material?: DensityField): SmoothChunk;
|
|
48
|
+
/** Unloads chunk (cx, cy, cz); its key and its neighbours' go to `dirty`. Returns the chunk, if it was loaded. */
|
|
49
|
+
removeChunk(cx: number, cy: number, cz: number): SmoothChunk | undefined;
|
|
50
|
+
/** The density at a world cell, -1 to 1, or undefined in an unloaded chunk. */
|
|
51
|
+
getDensity(x: number, y: number, z: number): number | undefined;
|
|
52
|
+
/** The material at a world cell, or undefined in an unloaded chunk. */
|
|
53
|
+
getMaterial(x: number, y: number, z: number): number | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Adds (`strength` > 0) or carves (`strength` < 0) a smooth ball: each cell within `radius` of `center` moves toward
|
|
56
|
+
* solid or air by `strength` times its closeness (1 at the centre, 0 at the radius), clamped to -1..1. Added solid
|
|
57
|
+
* takes `material` where a cell turns solid. Cells in unloaded chunks are skipped. Returns the cells changed.
|
|
58
|
+
* @throws RangeError for a non-finite centre, a radius outside 0 to 64, a strength outside -2 to 2 or a bad material.
|
|
59
|
+
*/
|
|
60
|
+
sculpt(center: Vec3, radius: number, strength: number, material?: number): number;
|
|
61
|
+
}
|
|
62
|
+
/** One material's triangles of a meshed smooth chunk, in the chunk's own blocks. */
|
|
63
|
+
export interface SmoothMeshPart {
|
|
64
|
+
positions: Float32Array;
|
|
65
|
+
normals: Float32Array;
|
|
66
|
+
indices: Uint32Array;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Output of {@link meshSmoothChunk}: triangles in chunk-local blocks (the chunk's corner at the origin), grouped by
|
|
70
|
+
* material. Surface points sit at the mean of their net cell's edge crossings; normals follow the density gradient.
|
|
71
|
+
*/
|
|
72
|
+
export interface SmoothChunkMesh {
|
|
73
|
+
/** Per material index present: xyz positions, xyz normals and triangle indices. */
|
|
74
|
+
parts: Map<number, SmoothMeshPart>;
|
|
75
|
+
/** Triangles in all parts. */
|
|
76
|
+
triangles: number;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Meshes chunk `key` of `terrain` with Surface Nets: one point per net cell the surface crosses (the cell between eight
|
|
80
|
+
* samples), placed at the mean of its edge crossings, and one quad per pair of neighbouring samples across the surface
|
|
81
|
+
* whose lower sample lies in the chunk, joining the four net cells around that edge, split into two triangles. Samples
|
|
82
|
+
* one cell past the chunk come from the loaded neighbours, so neighbouring chunks share their border points exactly and
|
|
83
|
+
* the surface has no seams; past an unloaded neighbour the chunk's own border samples repeat (no wall there). Each
|
|
84
|
+
* quad takes the material of its solid sample. Undefined when the chunk is not loaded.
|
|
85
|
+
* @throws RangeError when `key` is not three chunk coordinates.
|
|
86
|
+
*/
|
|
87
|
+
export declare function meshSmoothChunk(terrain: SmoothTerrain, key: string, options?: MeshSmoothChunkOptions): SmoothChunkMesh | undefined;
|
|
88
|
+
/** Options of {@link meshSmoothChunk}. */
|
|
89
|
+
export interface MeshSmoothChunkOptions {
|
|
90
|
+
/**
|
|
91
|
+
* Cap depth in blocks, 0 to 32: when given, the mesh is clipped to the chunk's box, so it ends exactly on the faces,
|
|
92
|
+
* and every face with no loaded neighbour is closed by the solid part of its plane, up to this depth from the border.
|
|
93
|
+
* 32 closes those faces whole, so the chunk joins a coarser far level of `@margoengine/voxel/smooth-lod` with no
|
|
94
|
+
* crack; a smaller depth can leave a gap where the ground runs nearly parallel to a face. Default: no clipping and no
|
|
95
|
+
* caps.
|
|
96
|
+
*/
|
|
97
|
+
skirt?: number;
|
|
98
|
+
}
|
|
99
|
+
/** Options of {@link SmoothTerrainView.attach}. */
|
|
100
|
+
export interface SmoothTerrainViewOptions {
|
|
101
|
+
/** The terrain to draw. The view meshes the chunks in `terrain.dirty` and removes their keys. */
|
|
102
|
+
terrain: SmoothTerrain;
|
|
103
|
+
/**
|
|
104
|
+
* Core material per material index; an index without one draws with `materials[0]`. Needed unless `createMesh`
|
|
105
|
+
* is given.
|
|
106
|
+
*/
|
|
107
|
+
materials?: readonly PbrMaterial[];
|
|
108
|
+
/**
|
|
109
|
+
* Makes the mesh of one chunk part in place of a core mesh with `materials[material]`: for textured materials, such
|
|
110
|
+
* as a triplanar `PbrMaterial` of `@margoengine/materials` (`createMesh(engine, part, rock)`), which need no UVs.
|
|
111
|
+
* `part` holds the part's positions, normals and indices in the chunk's own blocks; place the mesh at `origin`, the
|
|
112
|
+
* chunk's corner in engine space (world blocks minus the view's `origin` option). The view disposes what it returns
|
|
113
|
+
* when the chunk is meshed again, unloaded or the view is disposed. When the view's origin moves, a returned object
|
|
114
|
+
* with `position.set(x, y, z)` (a core mesh) is moved to the chunk's new corner; any other is made again. Default: a
|
|
115
|
+
* core mesh with `materials[material]` (or `materials[0]`).
|
|
116
|
+
*/
|
|
117
|
+
createMesh?: (part: SmoothMeshPart, material: number, origin: Vec3) => {
|
|
118
|
+
dispose(): void;
|
|
119
|
+
};
|
|
120
|
+
/** Most chunks meshed per {@link SmoothTerrainView.update}, nearest the camera first. Default 4. */
|
|
121
|
+
budget?: number;
|
|
122
|
+
/**
|
|
123
|
+
* Origin rebasing for worlds far from zero, as `VoxelRendererOptions.origin` (pass the same object): an object whose
|
|
124
|
+
* `origin` is the float64 world point (blocks) drawn at engine zero, such as a `FloatingOrigin` of
|
|
125
|
+
* `@margoengine/cameras/origin`; read every update. Chunk meshes are placed at their corner minus the origin in
|
|
126
|
+
* float64, so their float32 positions stay small wherever the terrain is. Default null (world and engine space are
|
|
127
|
+
* the same).
|
|
128
|
+
*/
|
|
129
|
+
origin?: VoxelOrigin | null;
|
|
130
|
+
/**
|
|
131
|
+
* Cap depth in blocks (0 to 32) for terrain drawn beside `SmoothLod` far levels: each chunk mesh is clipped to its
|
|
132
|
+
* box and its faces with no loaded neighbour are closed (see {@link MeshSmoothChunkOptions.skirt}); pass 32 for
|
|
133
|
+
* closed seams. Default undefined: unclipped meshes, the surface open past an unloaded neighbour.
|
|
134
|
+
*/
|
|
135
|
+
skirt?: number;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Draws a {@link SmoothTerrain} as core meshes: one mesh per chunk and material, placed at the chunk's corner. Call
|
|
139
|
+
* {@link SmoothTerrainView.update} each frame (it meshes up to `budget` dirty chunks, nearest the camera first, on this
|
|
140
|
+
* thread); a block world's `VoxelRenderer` draws beside it in the same scene.
|
|
141
|
+
*/
|
|
142
|
+
export declare class SmoothTerrainView {
|
|
143
|
+
#private;
|
|
144
|
+
readonly terrain: SmoothTerrain;
|
|
145
|
+
/** Live {@link SmoothTerrainViewOptions.origin}; null for none. */
|
|
146
|
+
origin: VoxelOrigin | null;
|
|
147
|
+
private constructor();
|
|
148
|
+
/**
|
|
149
|
+
* @throws TypeError when `engine` is not an Engine, `terrain` is not a SmoothTerrain, `materials` is empty or
|
|
150
|
+
* `origin` is not null or an object with `origin`.
|
|
151
|
+
* @throws RangeError when `budget` is not a whole number of 1 or more or `skirt` is outside 0 to 32.
|
|
152
|
+
*/
|
|
153
|
+
static attach(engine: Engine, options: SmoothTerrainViewOptions): SmoothTerrainView;
|
|
154
|
+
/** Chunks drawn, and dirty chunks waiting. */
|
|
155
|
+
get stats(): {
|
|
156
|
+
chunks: number;
|
|
157
|
+
meshes: number;
|
|
158
|
+
pending: number;
|
|
159
|
+
};
|
|
160
|
+
/**
|
|
161
|
+
* Moves the chunk meshes when the origin moved, then meshes up to `budget` dirty chunks, nearest the camera first,
|
|
162
|
+
* and swaps their core meshes. Returns the chunks done. When meshing or `createMesh` throws, the error goes on, the
|
|
163
|
+
* chunk stays pending with its previous meshes drawn, and the new meshes made before the throw are disposed.
|
|
164
|
+
* @throws MargoError `disposed` after dispose().
|
|
165
|
+
* @throws RangeError when the origin holds a value that is not a finite number.
|
|
166
|
+
*/
|
|
167
|
+
update(): number;
|
|
168
|
+
/** Removes every chunk mesh from the engine. */
|
|
169
|
+
dispose(): void;
|
|
170
|
+
[Symbol.dispose](): void;
|
|
171
|
+
}
|
package/dist/smooth.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { t as SmoothTerrain, n as SmoothTerrainView, r as meshSmoothChunk } from "./smooth-CEwHkXli.js";
|