@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
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { t as e } from "./model-data-D_rIKJCQ.js";
|
|
2
|
+
//#region src/model-surface.ts
|
|
3
|
+
function t(e, t, n, r = -1) {
|
|
4
|
+
let i = t >> 1, a = (i + 1) % 3, o = (i + 2) % 3, s = !(t & 1), c = s ? e.max[i] : e.min[i], l = [[
|
|
5
|
+
e.min[a],
|
|
6
|
+
e.min[o],
|
|
7
|
+
e.max[a],
|
|
8
|
+
e.max[o]
|
|
9
|
+
]];
|
|
10
|
+
for (let e = 0; e < n.length; e++) {
|
|
11
|
+
if (e === r) continue;
|
|
12
|
+
let t = n[e], u = s ? t.min[i] <= c && t.max[i] > c : t.min[i] < c && t.max[i] >= c, d = e < r && (s ? t.max[i] : t.min[i]) === c;
|
|
13
|
+
(u || d) && (l = l.flatMap(([e, n, r, i]) => {
|
|
14
|
+
let s = Math.max(e, t.min[a]), c = Math.max(n, t.min[o]), l = Math.min(r, t.max[a]), u = Math.min(i, t.max[o]);
|
|
15
|
+
return s >= l || c >= u ? [[
|
|
16
|
+
e,
|
|
17
|
+
n,
|
|
18
|
+
r,
|
|
19
|
+
i
|
|
20
|
+
]] : [
|
|
21
|
+
[
|
|
22
|
+
e,
|
|
23
|
+
n,
|
|
24
|
+
s,
|
|
25
|
+
i
|
|
26
|
+
],
|
|
27
|
+
[
|
|
28
|
+
l,
|
|
29
|
+
n,
|
|
30
|
+
r,
|
|
31
|
+
i
|
|
32
|
+
],
|
|
33
|
+
[
|
|
34
|
+
s,
|
|
35
|
+
n,
|
|
36
|
+
l,
|
|
37
|
+
c
|
|
38
|
+
],
|
|
39
|
+
[
|
|
40
|
+
s,
|
|
41
|
+
u,
|
|
42
|
+
l,
|
|
43
|
+
i
|
|
44
|
+
]
|
|
45
|
+
].filter(([e, t, n, r]) => e < n && t < r);
|
|
46
|
+
}));
|
|
47
|
+
}
|
|
48
|
+
return l;
|
|
49
|
+
}
|
|
50
|
+
function n(e, t, n) {
|
|
51
|
+
let r = t >> 1, i = (r + 1) % 3, a = (r + 2) % 3, o = !(t & 1), s = n === 1 || n === 2, c = n >= 2, l = [
|
|
52
|
+
0,
|
|
53
|
+
0,
|
|
54
|
+
0
|
|
55
|
+
];
|
|
56
|
+
return l[r] = o ? e.max[r] : e.min[r], l[i] = (o ? s : c) ? e.max[i] : e.min[i], l[a] = (o ? c : s) ? e.max[a] : e.min[a], l;
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/model-mesh.ts
|
|
60
|
+
function r(r, i, a, o, s = !1, c = !1) {
|
|
61
|
+
let l = r.getChunk(i, a, o);
|
|
62
|
+
if (!l) return [];
|
|
63
|
+
let u = /* @__PURE__ */ new Map();
|
|
64
|
+
for (let d = 0; d < 32768; d++) {
|
|
65
|
+
let f = d & 31, p = d >> 10, m = d >> 5 & 31, h = l.get(f, p, m), g = r.blocks.table[h & 65535];
|
|
66
|
+
if (!g?.model || (s ? !g.solid : g.layer === "invisible")) continue;
|
|
67
|
+
let _ = e(g, h), v = [
|
|
68
|
+
i * 32 + f,
|
|
69
|
+
a * 32 + p,
|
|
70
|
+
o * 32 + m
|
|
71
|
+
], y = c ? [
|
|
72
|
+
f,
|
|
73
|
+
p,
|
|
74
|
+
m
|
|
75
|
+
] : v;
|
|
76
|
+
for (let i = 0; i < _.length; i++) for (let a = 0; a < 6; a++) {
|
|
77
|
+
let o = _[i], c = a >> 1, d = (c + 1) % 3, h = (c + 2) % 3, b = !(a & 1), x = b ? o.max[c] : o.min[c], S = [..._];
|
|
78
|
+
if (x === +!!b) {
|
|
79
|
+
let t = [...v];
|
|
80
|
+
t[c] += b ? 1 : -1;
|
|
81
|
+
let n = r.getBlock(t[0], t[1], t[2]), i = r.blocks.table[n & 65535];
|
|
82
|
+
i && (s ? i.solid : i.layer === "opaque" || i.id === g.id) && i.shape !== "cross" && !i.liquid && (S = S.concat(e(i, n).map((e) => ({
|
|
83
|
+
...e,
|
|
84
|
+
min: e.min.map((e, t) => e + (t === c ? b ? 1 : -1 : 0)),
|
|
85
|
+
max: e.max.map((e, t) => e + (t === c ? b ? 1 : -1 : 0))
|
|
86
|
+
}))));
|
|
87
|
+
}
|
|
88
|
+
for (let e of t(o, a, S, i)) {
|
|
89
|
+
let t = o.textures[a], i = `${g.id}:${t}`, s = u.get(i);
|
|
90
|
+
s || u.set(i, s = {
|
|
91
|
+
type: g,
|
|
92
|
+
texture: t,
|
|
93
|
+
positions: [],
|
|
94
|
+
normals: [],
|
|
95
|
+
indices: [],
|
|
96
|
+
uvs: [],
|
|
97
|
+
colors: []
|
|
98
|
+
});
|
|
99
|
+
let _ = s.positions.length / 3, x = {
|
|
100
|
+
...o,
|
|
101
|
+
min: [...o.min],
|
|
102
|
+
max: [...o.max]
|
|
103
|
+
};
|
|
104
|
+
x.min[d] = e[0], x.min[h] = e[1], x.max[d] = e[2], x.max[h] = e[3];
|
|
105
|
+
let S = [...v];
|
|
106
|
+
S[c] += b ? 1 : -1;
|
|
107
|
+
let C = r.getChunk(S[0] >> 5, S[1] >> 5, S[2] >> 5)?.getLight(S[0] & 31, S[1] & 31, S[2] & 31) ?? 0, w = l.getLight(f, p, m), T = Math.max(w >> 4, w & 15, C >> 4, C & 15, g.light) / 15;
|
|
108
|
+
for (let e = 0; e < 4; e++) {
|
|
109
|
+
let t = n(x, a, e), r = (t[d] - o.min[d]) / (o.max[d] - o.min[d]), i = (t[h] - o.min[h]) / (o.max[h] - o.min[h]);
|
|
110
|
+
s.positions.push(...t.map((e, t) => e + y[t])), s.normals.push(c === 0 ? b ? 1 : -1 : 0, c === 1 ? b ? 1 : -1 : 0, c === 2 ? b ? 1 : -1 : 0);
|
|
111
|
+
let l = b ? r : i, u = b ? i : r, f = o.uv[a];
|
|
112
|
+
for (let e = 0; e < 2; e++) s.uvs.push(f[e] * (1 - l) * (1 - u) + f[2 + e] * l * (1 - u) + f[4 + e] * l * u + f[6 + e] * (1 - l) * u);
|
|
113
|
+
s.colors.push(T, T, T, 1);
|
|
114
|
+
}
|
|
115
|
+
s.indices.push(_, _ + 1, _ + 2, _, _ + 2, _ + 3);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return [...u.values()];
|
|
120
|
+
}
|
|
121
|
+
//#endregion
|
|
122
|
+
export { n, t as r, r as t };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// @ts-ignore optional peer: its types apply when it is installed
|
|
2
|
+
import type { MeshGeometry } from '@margoengine/materials';
|
|
3
|
+
import type { BlockType } from './blocks.js';
|
|
4
|
+
import type { VoxelWorld } from './world.js';
|
|
5
|
+
/** One texture/type group of an extracted chunk surface, ready for materials.createMesh. */
|
|
6
|
+
export interface BlockModelSurface extends MeshGeometry {
|
|
7
|
+
/** Block type whose material supplies this group. */
|
|
8
|
+
readonly type: BlockType;
|
|
9
|
+
/** Texture layer for this group. */
|
|
10
|
+
readonly texture: number;
|
|
11
|
+
positions: number[];
|
|
12
|
+
normals: number[];
|
|
13
|
+
indices: number[];
|
|
14
|
+
uvs: number[];
|
|
15
|
+
colors: number[];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Extracts exact visible model surfaces, removing union interiors and faces hidden by solid neighbours. Positions are
|
|
19
|
+
* world blocks, or blocks from the chunk's corner with `local` (exact small numbers wherever the chunk is).
|
|
20
|
+
*/
|
|
21
|
+
export declare function meshBlockModels(world: VoxelWorld, cx: number, cy: number, cz: number, solidOnly?: boolean, local?: boolean): BlockModelSurface[];
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ModelBox } from './model-data.js';
|
|
2
|
+
/** Internal exact rectangle subtraction for the boundary of a union of boxes. */
|
|
3
|
+
export declare function faceRectangles(box: ModelBox, direction: number, blockers: readonly ModelBox[], ownIndex?: number): number[][];
|
|
4
|
+
/** Internal canonical face corner, counter-clockwise from outside. */
|
|
5
|
+
export declare function faceCorner(box: ModelBox, direction: number, corner: number): number[];
|
package/dist/models.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { Engine } from '@margoengine/core';
|
|
3
|
+
// @ts-ignore optional peer: its types apply when it is installed
|
|
4
|
+
import type { Material } from '@margoengine/materials';
|
|
5
|
+
import { type BlockTypes, type BlockTypeOptions, type BlockType } from './blocks.js';
|
|
6
|
+
export { meshBlockModels, type BlockModelSurface } from './model-mesh.js';
|
|
7
|
+
import { type VoxelWorld } from './world.js';
|
|
8
|
+
import type { VoxelOrigin } from './render.js';
|
|
9
|
+
export type { ModelBox, ModelOrientation } from './model-data.js';
|
|
10
|
+
/** A box of a custom block. Coordinates are in its unit cell. */
|
|
11
|
+
export interface BlockModelBox {
|
|
12
|
+
/** Minimum corner, each coordinate in [0,1]. */
|
|
13
|
+
min: readonly [number, number, number];
|
|
14
|
+
/** Maximum corner, strictly above min. */
|
|
15
|
+
max: readonly [number, number, number];
|
|
16
|
+
/** UV rectangle per face (+x,-x,+y,-y,+z,-z), default [0,0,1,1]. Flips are allowed. */
|
|
17
|
+
uv?: readonly (readonly [number, number, number, number])[];
|
|
18
|
+
/** Texture layer per face, default the block type's textures. */
|
|
19
|
+
textures?: readonly [number, number, number, number, number, number];
|
|
20
|
+
}
|
|
21
|
+
/** Definition passed to defineBlockModel. */
|
|
22
|
+
export interface BlockModelOptions extends Omit<BlockTypeOptions, 'shape' | 'model' | 'waterlog'> {
|
|
23
|
+
/** Union of 1 to 64 boxes. */
|
|
24
|
+
boxes: readonly BlockModelBox[];
|
|
25
|
+
}
|
|
26
|
+
/** Defines a custom box union with 24 exact cube orientations and worker-safe metadata. */
|
|
27
|
+
export declare function defineBlockModel(blocks: BlockTypes, name: string, options: BlockModelOptions): number;
|
|
28
|
+
/** State of a model in orientation 0..23. Identity is 0; all rotations are right-handed. */
|
|
29
|
+
export declare function modelState(id: number, orientation?: number): number;
|
|
30
|
+
/** Options of VoxelModelRenderer. */
|
|
31
|
+
export interface VoxelModelRendererOptions {
|
|
32
|
+
/** Override the material per type and texture layer; default block colour/PBR factors. Materials remain caller owned. */
|
|
33
|
+
material?: (type: BlockType, texture: number) => Material;
|
|
34
|
+
/**
|
|
35
|
+
* Origin rebasing for worlds far from zero, as `VoxelRendererOptions.origin` (pass the same object): an object whose
|
|
36
|
+
* `origin` is the float64 world point (blocks) drawn at engine zero, such as a `FloatingOrigin` of
|
|
37
|
+
* `@margoengine/cameras/origin`; read every update. Chunk meshes hold chunk-local positions and sit at their corner
|
|
38
|
+
* minus the origin in float64, so their float32 positions stay small wherever the world is. Default null (world and
|
|
39
|
+
* engine space are the same).
|
|
40
|
+
*/
|
|
41
|
+
origin?: VoxelOrigin | null;
|
|
42
|
+
}
|
|
43
|
+
/** Chunk-streamed textured model meshes. Call update after lighting, before engine.render. */
|
|
44
|
+
export declare class VoxelModelRenderer {
|
|
45
|
+
private readonly engine;
|
|
46
|
+
readonly world: VoxelWorld;
|
|
47
|
+
private readonly options;
|
|
48
|
+
private readonly runtime;
|
|
49
|
+
/** Loads the optional materials renderer. An aborted signal rejects with its original reason. */
|
|
50
|
+
static create(engine: Engine, world: VoxelWorld, options?: VoxelModelRendererOptions, signal?: AbortSignal): Promise<VoxelModelRenderer>;
|
|
51
|
+
private readonly pending;
|
|
52
|
+
private readonly meshes;
|
|
53
|
+
private readonly off;
|
|
54
|
+
private readonly materials;
|
|
55
|
+
private disposed;
|
|
56
|
+
private readonly versions;
|
|
57
|
+
private readonly owned;
|
|
58
|
+
private placed;
|
|
59
|
+
/** Live {@link VoxelModelRendererOptions.origin}; null for none. */
|
|
60
|
+
origin: VoxelOrigin | null;
|
|
61
|
+
private constructor();
|
|
62
|
+
private mark;
|
|
63
|
+
/**
|
|
64
|
+
* Moves every chunk mesh when the origin moved, then rebuilds changed chunks. Dirty lighting keys are observed
|
|
65
|
+
* without consuming the main renderer's queue.
|
|
66
|
+
* @throws RangeError when the origin holds a value that is not a finite number.
|
|
67
|
+
*/
|
|
68
|
+
update(): void;
|
|
69
|
+
private corner;
|
|
70
|
+
/** Releases all chunk meshes and subscriptions; caller materials and textures stay alive. */
|
|
71
|
+
dispose(): void;
|
|
72
|
+
/** Releases the renderer for explicit resource management. */
|
|
73
|
+
[Symbol.dispose](): void;
|
|
74
|
+
}
|
package/dist/models.js
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { i as e } from "./blocks-BabtpQT-.js";
|
|
2
|
+
import { r as t } from "./chunk-BCoaKSn4.js";
|
|
3
|
+
import { n } from "./world-CelTlft2.js";
|
|
4
|
+
import { n as r, r as i, t as a } from "./model-mesh-C4Gi6bMM.js";
|
|
5
|
+
import { n as o, t as s } from "./origin-DsMelUxs.js";
|
|
6
|
+
import { MargoError as c } from "@margoengine/math/errors";
|
|
7
|
+
//#region src/models.ts
|
|
8
|
+
var l = [];
|
|
9
|
+
for (let e of [
|
|
10
|
+
[
|
|
11
|
+
0,
|
|
12
|
+
1,
|
|
13
|
+
2
|
|
14
|
+
],
|
|
15
|
+
[
|
|
16
|
+
0,
|
|
17
|
+
2,
|
|
18
|
+
1
|
|
19
|
+
],
|
|
20
|
+
[
|
|
21
|
+
1,
|
|
22
|
+
0,
|
|
23
|
+
2
|
|
24
|
+
],
|
|
25
|
+
[
|
|
26
|
+
1,
|
|
27
|
+
2,
|
|
28
|
+
0
|
|
29
|
+
],
|
|
30
|
+
[
|
|
31
|
+
2,
|
|
32
|
+
0,
|
|
33
|
+
1
|
|
34
|
+
],
|
|
35
|
+
[
|
|
36
|
+
2,
|
|
37
|
+
1,
|
|
38
|
+
0
|
|
39
|
+
]
|
|
40
|
+
]) {
|
|
41
|
+
let t = (e[0] - e[1]) * (e[1] - e[2]) * (e[0] - e[2]) / -2;
|
|
42
|
+
for (let n of [
|
|
43
|
+
[
|
|
44
|
+
1,
|
|
45
|
+
1,
|
|
46
|
+
1
|
|
47
|
+
],
|
|
48
|
+
[
|
|
49
|
+
1,
|
|
50
|
+
-1,
|
|
51
|
+
-1
|
|
52
|
+
],
|
|
53
|
+
[
|
|
54
|
+
-1,
|
|
55
|
+
1,
|
|
56
|
+
-1
|
|
57
|
+
],
|
|
58
|
+
[
|
|
59
|
+
-1,
|
|
60
|
+
-1,
|
|
61
|
+
1
|
|
62
|
+
],
|
|
63
|
+
[
|
|
64
|
+
-1,
|
|
65
|
+
1,
|
|
66
|
+
1
|
|
67
|
+
],
|
|
68
|
+
[
|
|
69
|
+
1,
|
|
70
|
+
-1,
|
|
71
|
+
1
|
|
72
|
+
],
|
|
73
|
+
[
|
|
74
|
+
1,
|
|
75
|
+
1,
|
|
76
|
+
-1
|
|
77
|
+
],
|
|
78
|
+
[
|
|
79
|
+
-1,
|
|
80
|
+
-1,
|
|
81
|
+
-1
|
|
82
|
+
]
|
|
83
|
+
]) t * n[0] * n[1] * n[2] === 1 && l.push(e.map((e, t) => n[t] * (e + 1)));
|
|
84
|
+
}
|
|
85
|
+
var u = (e, t) => t.map((t) => t > 0 ? e[t - 1] : 1 - e[-t - 1]);
|
|
86
|
+
function d(e, t, n) {
|
|
87
|
+
if (!Array.isArray(n?.boxes) || !n.boxes.length || n.boxes.length > 64) throw RangeError("Block model needs 1 to 64 boxes; pass {boxes:[{min:[0,0,0],max:[1,1,1]}]}");
|
|
88
|
+
let a = n.textures ?? 0, o = typeof a == "number" ? [
|
|
89
|
+
,
|
|
90
|
+
,
|
|
91
|
+
,
|
|
92
|
+
,
|
|
93
|
+
,
|
|
94
|
+
,
|
|
95
|
+
].fill(a) : a, s = n.boxes.map((e) => {
|
|
96
|
+
if (!e || e.min?.length !== 3 || e.max?.length !== 3 || e.min.some((t, n) => !Number.isFinite(t) || t < 0 || t >= e.max[n]) || e.max.some((e) => !Number.isFinite(e) || e > 1)) throw RangeError("Model boxes need finite 0 <= min < max <= 1 on every axis; correct the box bounds");
|
|
97
|
+
let t = e.uv ?? Array.from({ length: 6 }, () => [
|
|
98
|
+
0,
|
|
99
|
+
0,
|
|
100
|
+
1,
|
|
101
|
+
1
|
|
102
|
+
]);
|
|
103
|
+
if (t.length !== 6 || t.some((e) => e.length !== 4 || e.some((e) => !Number.isFinite(e)))) throw RangeError("Model UVs need six finite [u0,v0,u1,v1] rectangles; correct the face UVs");
|
|
104
|
+
let r = e.textures ?? o;
|
|
105
|
+
if (r.length !== 6 || r.some((e) => !Number.isInteger(e) || e < 0 || e + (n.animation?.frames ?? 1) > 2048)) throw RangeError("Model textures need six layers in 0..2047 with room for animation frames; correct the layers");
|
|
106
|
+
return {
|
|
107
|
+
min: [...e.min],
|
|
108
|
+
max: [...e.max],
|
|
109
|
+
textures: [...r],
|
|
110
|
+
uv: t.map(([e, t, n, r]) => [
|
|
111
|
+
e,
|
|
112
|
+
t,
|
|
113
|
+
n,
|
|
114
|
+
t,
|
|
115
|
+
n,
|
|
116
|
+
r,
|
|
117
|
+
e,
|
|
118
|
+
r
|
|
119
|
+
])
|
|
120
|
+
};
|
|
121
|
+
}), c = l.map((e) => {
|
|
122
|
+
let t = s.map((t) => {
|
|
123
|
+
let n = u(t.min, e), i = u(t.max, e), a = {
|
|
124
|
+
min: n.map((e, t) => Math.min(e, i[t])),
|
|
125
|
+
max: n.map((e, t) => Math.max(e, i[t])),
|
|
126
|
+
uv: Array.from({ length: 6 }, () => []),
|
|
127
|
+
textures: [
|
|
128
|
+
,
|
|
129
|
+
,
|
|
130
|
+
,
|
|
131
|
+
,
|
|
132
|
+
,
|
|
133
|
+
,
|
|
134
|
+
].fill(0)
|
|
135
|
+
};
|
|
136
|
+
for (let n = 0; n < 6; n++) {
|
|
137
|
+
let i = n >> 1, o = e.findIndex((e) => Math.abs(e) === i + 1), s = o * 2 + (e[o] > 0 == !(n & 1) ? 0 : 1);
|
|
138
|
+
a.textures[s] = t.textures[n];
|
|
139
|
+
for (let i = 0; i < 4; i++) {
|
|
140
|
+
let o = u(r(t, n, i), e), c = [
|
|
141
|
+
0,
|
|
142
|
+
1,
|
|
143
|
+
2,
|
|
144
|
+
3
|
|
145
|
+
].find((e) => r(a, s, e).every((e, t) => e === o[t]));
|
|
146
|
+
a.uv[s][c * 2] = t.uv[n][i * 2], a.uv[s][c * 2 + 1] = t.uv[n][i * 2 + 1];
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return a;
|
|
150
|
+
}), n = 0, a = {
|
|
151
|
+
min: [
|
|
152
|
+
0,
|
|
153
|
+
0,
|
|
154
|
+
0
|
|
155
|
+
],
|
|
156
|
+
max: [
|
|
157
|
+
1,
|
|
158
|
+
1,
|
|
159
|
+
1
|
|
160
|
+
],
|
|
161
|
+
uv: [],
|
|
162
|
+
textures: []
|
|
163
|
+
};
|
|
164
|
+
for (let e = 0; e < 6; e++) {
|
|
165
|
+
let r = e >> 1, o = t.filter((t) => (e & 1 ? t.min[r] : t.max[r]) === (e & 1 ? 0 : 1)).map((e) => ({
|
|
166
|
+
...e,
|
|
167
|
+
min: e.min.map((e, t) => t === r ? -1 : e),
|
|
168
|
+
max: e.max.map((e, t) => t === r ? 2 : e)
|
|
169
|
+
}));
|
|
170
|
+
i(a, e, o).length || (n |= 1 << e);
|
|
171
|
+
}
|
|
172
|
+
return {
|
|
173
|
+
boxes: t,
|
|
174
|
+
covered: n
|
|
175
|
+
};
|
|
176
|
+
});
|
|
177
|
+
return e.define(t, {
|
|
178
|
+
...n,
|
|
179
|
+
shape: "model",
|
|
180
|
+
model: c,
|
|
181
|
+
opacity: n.opacity ?? 0
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
function f(t, n = 0) {
|
|
185
|
+
if (!Number.isInteger(n) || n < 0 || n >= 24) throw RangeError("Model orientation must be 0..23; use 0 for identity");
|
|
186
|
+
return e(t, n);
|
|
187
|
+
}
|
|
188
|
+
var p = class e {
|
|
189
|
+
static async create(t, n, r = {}, i) {
|
|
190
|
+
i?.throwIfAborted();
|
|
191
|
+
let a = await import("@margoengine/materials");
|
|
192
|
+
return i?.throwIfAborted(), new e(t, n, r, a);
|
|
193
|
+
}
|
|
194
|
+
constructor(e, t, n = {}, r) {
|
|
195
|
+
this.engine = e, this.world = t, this.options = n, this.runtime = r, this.pending = /* @__PURE__ */ new Set(), this.meshes = /* @__PURE__ */ new Map(), this.off = [], this.materials = /* @__PURE__ */ new Map(), this.disposed = !1, this.versions = /* @__PURE__ */ new Map(), this.owned = /* @__PURE__ */ new Set(), this.placed = [
|
|
196
|
+
0,
|
|
197
|
+
0,
|
|
198
|
+
0
|
|
199
|
+
], this.origin = s(n?.origin, "VoxelModelRenderer"), this.off.push(t.onChunk((e, t, n) => this.mark(e, t, n))), this.off.push(t.onChange((e, t, n) => this.mark(e >> 5, t >> 5, n >> 5)));
|
|
200
|
+
}
|
|
201
|
+
mark(e, t, r) {
|
|
202
|
+
for (let [i, a, o] of [
|
|
203
|
+
[
|
|
204
|
+
0,
|
|
205
|
+
0,
|
|
206
|
+
0
|
|
207
|
+
],
|
|
208
|
+
[
|
|
209
|
+
1,
|
|
210
|
+
0,
|
|
211
|
+
0
|
|
212
|
+
],
|
|
213
|
+
[
|
|
214
|
+
-1,
|
|
215
|
+
0,
|
|
216
|
+
0
|
|
217
|
+
],
|
|
218
|
+
[
|
|
219
|
+
0,
|
|
220
|
+
1,
|
|
221
|
+
0
|
|
222
|
+
],
|
|
223
|
+
[
|
|
224
|
+
0,
|
|
225
|
+
-1,
|
|
226
|
+
0
|
|
227
|
+
],
|
|
228
|
+
[
|
|
229
|
+
0,
|
|
230
|
+
0,
|
|
231
|
+
1
|
|
232
|
+
],
|
|
233
|
+
[
|
|
234
|
+
0,
|
|
235
|
+
0,
|
|
236
|
+
-1
|
|
237
|
+
]
|
|
238
|
+
]) {
|
|
239
|
+
let s = [
|
|
240
|
+
e + i,
|
|
241
|
+
t + a,
|
|
242
|
+
r + o
|
|
243
|
+
];
|
|
244
|
+
s.every((e) => e << 5 >> 5 === e) && this.pending.add(n(s[0], s[1], s[2]));
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
update() {
|
|
248
|
+
if (this.disposed) throw new c("disposed", "Model renderer is disposed; create a new VoxelModelRenderer");
|
|
249
|
+
let e = o(this.origin, "VoxelModelRenderer");
|
|
250
|
+
if (e.some((e, t) => e !== this.placed[t])) {
|
|
251
|
+
this.placed = e;
|
|
252
|
+
for (let [t, n] of this.meshes) {
|
|
253
|
+
let r = this.corner(t, e);
|
|
254
|
+
for (let e of n) e.root.setPosition(r);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
for (let e of this.world.dirty) {
|
|
258
|
+
let [n, r, i] = e.split(",").map(Number), a = this.world.getChunk(n, r, i);
|
|
259
|
+
a && this.versions.get(e) !== t(a) && this.pending.add(e);
|
|
260
|
+
}
|
|
261
|
+
for (let n of this.pending) {
|
|
262
|
+
for (let e of this.meshes.get(n) ?? []) e.destroy();
|
|
263
|
+
this.meshes.delete(n), this.versions.delete(n);
|
|
264
|
+
let [r, i, o] = n.split(",").map(Number), s = [];
|
|
265
|
+
try {
|
|
266
|
+
for (let t of a(this.world, r, i, o, !1, !0)) {
|
|
267
|
+
let r = t.type, i = `${r.id}:${t.texture}`, a = this.materials.get(i);
|
|
268
|
+
a || this.materials.set(i, a = this.options.material?.(r, t.texture) ?? new this.runtime.Material({
|
|
269
|
+
color: [...r.color],
|
|
270
|
+
roughness: r.roughness,
|
|
271
|
+
metalness: r.metalness,
|
|
272
|
+
emissive: [...r.color].map((e) => e * r.emissive),
|
|
273
|
+
transparent: r.layer === "translucent",
|
|
274
|
+
alphaTest: r.layer === "cutout" ? .5 : 0
|
|
275
|
+
})), this.options.material || this.owned.add(a);
|
|
276
|
+
let o = this.runtime.createMesh(this.engine, t, a);
|
|
277
|
+
s.push(o), o.root.setPosition(this.corner(n, e));
|
|
278
|
+
}
|
|
279
|
+
} finally {
|
|
280
|
+
s.length && this.meshes.set(n, s);
|
|
281
|
+
}
|
|
282
|
+
let c = this.world.getChunk(r, i, o);
|
|
283
|
+
c && this.versions.set(n, t(c)), this.pending.delete(n);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
corner(e, t) {
|
|
287
|
+
let [n, r, i] = e.split(",").map(Number);
|
|
288
|
+
return [
|
|
289
|
+
n * 32 - t[0],
|
|
290
|
+
r * 32 - t[1],
|
|
291
|
+
i * 32 - t[2]
|
|
292
|
+
];
|
|
293
|
+
}
|
|
294
|
+
dispose() {
|
|
295
|
+
if (!this.disposed) {
|
|
296
|
+
this.disposed = !0;
|
|
297
|
+
for (let e of this.off) e();
|
|
298
|
+
for (let e of this.meshes.values()) for (let t of e) t.destroy();
|
|
299
|
+
for (let e of this.owned) e.dispose();
|
|
300
|
+
this.meshes.clear(), this.pending.clear(), this.materials.clear(), this.owned.clear(), this.versions.clear();
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
[Symbol.dispose]() {
|
|
304
|
+
this.dispose();
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
//#endregion
|
|
308
|
+
export { p as VoxelModelRenderer, d as defineBlockModel, a as meshBlockModels, f as modelState };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel, Mesh as MeshHandle } from '@margoengine/core';
|
|
3
|
+
import { type BlockTypeOptions } from './blocks.js';
|
|
4
|
+
import { type VoxelWorld } from './world.js';
|
|
5
|
+
import type { VoxelOrigin } from './render.js';
|
|
6
|
+
/** A core mesh, or the core mesh graph returned by the glTF loader. Source meshes remain caller owned. */
|
|
7
|
+
export type ObjectBlockSource = MeshHandle | {
|
|
8
|
+
readonly root: MeshHandle;
|
|
9
|
+
readonly meshes: readonly MeshHandle[];
|
|
10
|
+
};
|
|
11
|
+
/** Object block storage, collision and light properties. Appearance comes from the source mesh materials. */
|
|
12
|
+
export type ObjectBlockOptions = Pick<BlockTypeOptions, 'solid' | 'light' | 'opacity'>;
|
|
13
|
+
/** Rotation about the cell centre: 0..3 positive quarter turns about +Y. */
|
|
14
|
+
export declare function objectState(id: number, turns?: number): number;
|
|
15
|
+
/** Options of {@link VoxelObjects.attach}. */
|
|
16
|
+
export interface VoxelObjectsOptions {
|
|
17
|
+
/**
|
|
18
|
+
* Origin rebasing for worlds far from zero, as `VoxelRendererOptions.origin` (pass the same object): an object whose
|
|
19
|
+
* `origin` is the float64 world point (blocks) drawn at engine zero, such as a `FloatingOrigin` of
|
|
20
|
+
* `@margoengine/cameras/origin`; read every update. Placements sit at their cell minus the origin in float64, so
|
|
21
|
+
* their float32 positions stay small wherever the world is. Default null (world and engine space are the same).
|
|
22
|
+
*/
|
|
23
|
+
origin?: VoxelOrigin | null;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Core mesh instances at object-block cells, owned and streamed by their chunks. Shared Geometry identities use
|
|
27
|
+
* core's existing instanced batches; its normal per-mesh frustum tests cull individual placements, including objects
|
|
28
|
+
* extending outside a chunk. Call update after block edits and before rendering. No GPU allocation on construction.
|
|
29
|
+
*/
|
|
30
|
+
export declare class VoxelObjects {
|
|
31
|
+
readonly world: VoxelWorld;
|
|
32
|
+
/**
|
|
33
|
+
* Subscribes object-block placement to this world. Call update before rendering.
|
|
34
|
+
* @throws TypeError when `origin` is not null or an object with `origin`.
|
|
35
|
+
*/
|
|
36
|
+
static attach(engine: EngineKernel, world: VoxelWorld, options?: VoxelObjectsOptions): VoxelObjects;
|
|
37
|
+
/** Live {@link VoxelObjectsOptions.origin}; null for none. */
|
|
38
|
+
origin: VoxelOrigin | null;
|
|
39
|
+
private placed;
|
|
40
|
+
private readonly engine;
|
|
41
|
+
private readonly templates;
|
|
42
|
+
private readonly pending;
|
|
43
|
+
private readonly chunks;
|
|
44
|
+
private readonly off;
|
|
45
|
+
private disposed;
|
|
46
|
+
constructor(engine: EngineKernel, world: VoxelWorld, options?: VoxelObjectsOptions);
|
|
47
|
+
private check;
|
|
48
|
+
/**
|
|
49
|
+
* Defines an object block from a snapshot of a mesh or glTF node graph. Geometry/materials are shared, transforms
|
|
50
|
+
* copied. The source may then be hidden, moved or disposed. Default solid=false and opacity=0.
|
|
51
|
+
*/
|
|
52
|
+
define(name: string, source: ObjectBlockSource, options?: ObjectBlockOptions): number;
|
|
53
|
+
/** Core handles of a placement, root first. Empty if unloaded, not an object, or update has not processed it. */
|
|
54
|
+
at(x: number, y: number, z: number): readonly MeshHandle[];
|
|
55
|
+
/** Loaded chunk, object cell and mesh-node counts, including transform-only nodes. */
|
|
56
|
+
get stats(): {
|
|
57
|
+
chunks: number;
|
|
58
|
+
cells: number;
|
|
59
|
+
meshes: number;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Moves every placement when the origin moved, then reconciles changed chunks. Unchanged cells retain their handles
|
|
63
|
+
* and GPU instance rows.
|
|
64
|
+
* @throws RangeError when the origin holds a value that is not a finite number.
|
|
65
|
+
*/
|
|
66
|
+
update(): void;
|
|
67
|
+
private cellPosition;
|
|
68
|
+
private remove;
|
|
69
|
+
/** Removes placements and subscriptions. Borrowed geometry/materials and source meshes remain caller owned. */
|
|
70
|
+
dispose(): void;
|
|
71
|
+
/** Releases placements with explicit resource management. */
|
|
72
|
+
[Symbol.dispose](): void;
|
|
73
|
+
}
|