@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/bevel.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/** A cube face {@link bevelQuads} can chamfer. A decoded chunk quad satisfies this. */
|
|
2
|
+
export interface CubeFace {
|
|
3
|
+
/** Cell x of the face's origin. */
|
|
4
|
+
x: number;
|
|
5
|
+
/** Cell y of the face's origin. */
|
|
6
|
+
y: number;
|
|
7
|
+
/** Cell z of the face's origin. */
|
|
8
|
+
z: number;
|
|
9
|
+
/** 0-5: +x, −x, +y, −y, +z, −z. Faces 6 and 7 (crosses) are skipped. */
|
|
10
|
+
face: number;
|
|
11
|
+
/** Width in cells along the face's u axis: z on x faces, x on y and z faces. */
|
|
12
|
+
w: number;
|
|
13
|
+
/** Height in cells along the face's v axis: y on x and z faces, z on y faces. */
|
|
14
|
+
h: number;
|
|
15
|
+
/** Id copied onto every vertex of this face. Default 0. */
|
|
16
|
+
material?: number;
|
|
17
|
+
/** Mesher corner AO (0-3) in the corner order (0,0), (w,0), (w,h), (0,h). Default 3 (open). */
|
|
18
|
+
ao?: readonly number[];
|
|
19
|
+
/** Mesher corner sky light (0-15), same order. Default 15. */
|
|
20
|
+
sky?: readonly number[];
|
|
21
|
+
/** Mesher corner block light (0-15), same order. Default 0. */
|
|
22
|
+
block?: readonly number[];
|
|
23
|
+
}
|
|
24
|
+
/** Triangle mesh from {@link bevelQuads}. Positions are in blocks, the same space as the input cells. */
|
|
25
|
+
export interface BevelMesh {
|
|
26
|
+
/** xyz per vertex. */
|
|
27
|
+
positions: Float32Array;
|
|
28
|
+
/** Unit normal per vertex: the face's on its panel, the slope's on a chamfer strip or corner. */
|
|
29
|
+
normals: Float32Array;
|
|
30
|
+
/** Three vertex indices per triangle, counterclockwise seen from outside. */
|
|
31
|
+
indices: Uint32Array;
|
|
32
|
+
/** `CubeFace.material` per vertex. */
|
|
33
|
+
materials: Uint32Array;
|
|
34
|
+
/** Face tile coordinates per vertex, the same (u, v) as the chunk shader's `voxelTile`. */
|
|
35
|
+
uv: Float32Array;
|
|
36
|
+
/** AO, sky and block light per vertex, bilinear over the face's corners, in the mesher's units. */
|
|
37
|
+
lighting: Float32Array;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Bevels cube faces by `bevel` blocks. `0` returns each face as two flat triangles. Faces 6 and 7 are skipped. Pass
|
|
41
|
+
* only faces of full cubes: a lowered liquid top is not a cube. Each unit edge of a face's outline is chamfered when
|
|
42
|
+
* the faces around it make a convex or concave crease (a 45 degree strip `bevel` deep into both faces), and left flat
|
|
43
|
+
* where a face goes on in the same plane or where no face is found across it, whatever quads the surface was merged
|
|
44
|
+
* into. `context` lists faces that are not meshed but border these, such as a neighbouring chunk's: they decide the
|
|
45
|
+
* creases and corners along the shared seam, so two chunks meshed with each other's faces as context meet exactly.
|
|
46
|
+
* Corner AO and light are interpolated over panels, strips and corners from the face each triangle belongs to.
|
|
47
|
+
* @throws RangeError when `bevel` is not in [0, 0.5).
|
|
48
|
+
*/
|
|
49
|
+
export declare function bevelQuads(quads: readonly CubeFace[], bevel: number, context?: readonly CubeFace[]): BevelMesh;
|
|
50
|
+
/** A bevel for `VoxelRendererOptions.bevel`, from {@link voxelBevel}. */
|
|
51
|
+
export interface VoxelBevel {
|
|
52
|
+
/** Blocks the cube faces are chamfered by, in (0, 0.5). */
|
|
53
|
+
readonly amount: number;
|
|
54
|
+
/**
|
|
55
|
+
* @internal The chunk mesh: interleaved vertices (`layout`, blocks from `origin`), indices, and triangle centres in
|
|
56
|
+
* chunk blocks. The renderer passes origin [0, 0, 0] and adds the chunk's origin per draw (`origins`), and the
|
|
57
|
+
* neighbours' faces on the seams (`ChunkMesh.border`) as `context`.
|
|
58
|
+
*/
|
|
59
|
+
mesh(faces: readonly CubeFace[], origin: readonly number[], context?: readonly CubeFace[]): {
|
|
60
|
+
vertices: Float32Array;
|
|
61
|
+
indices: Uint32Array;
|
|
62
|
+
centers: Float32Array;
|
|
63
|
+
};
|
|
64
|
+
/** @internal The vertex layout: position, normal, uv, light, then material | face << 12. */
|
|
65
|
+
readonly layout: GPUVertexBufferLayout;
|
|
66
|
+
/** @internal The second vertex buffer: the chunk's origin in the renderer's integer frame, one per instance. */
|
|
67
|
+
readonly origins: GPUVertexBufferLayout;
|
|
68
|
+
/** @internal WGSL of the bevel beauty shader between the shared lit part and the shared shading end. */
|
|
69
|
+
readonly shader: string;
|
|
70
|
+
/** @internal WGSL of the bevel shadow casters (bevelVertex, bevelCutout). */
|
|
71
|
+
readonly shadow: string;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* A bevel of `amount` blocks for `VoxelRenderer.attach(engine, { bevel: voxelBevel(0.15) })`: each chunk's cube faces
|
|
75
|
+
* become a chamfered triangle mesh ({@link bevelQuads}), lit, shadowed and replayed like the quads. Its own entry, so
|
|
76
|
+
* worlds without a bevel carry none of it.
|
|
77
|
+
* @throws RangeError when `amount` is not in (0, 0.5).
|
|
78
|
+
*/
|
|
79
|
+
export declare function voxelBevel(amount: number): VoxelBevel;
|
package/dist/bevel.js
ADDED
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
import { n as e } from "./faces-DUR7LhrN.js";
|
|
2
|
+
//#region src/bevel.ts
|
|
3
|
+
var t = [
|
|
4
|
+
[
|
|
5
|
+
1,
|
|
6
|
+
0,
|
|
7
|
+
0
|
|
8
|
+
],
|
|
9
|
+
[
|
|
10
|
+
-1,
|
|
11
|
+
0,
|
|
12
|
+
0
|
|
13
|
+
],
|
|
14
|
+
[
|
|
15
|
+
0,
|
|
16
|
+
1,
|
|
17
|
+
0
|
|
18
|
+
],
|
|
19
|
+
[
|
|
20
|
+
0,
|
|
21
|
+
-1,
|
|
22
|
+
0
|
|
23
|
+
],
|
|
24
|
+
[
|
|
25
|
+
0,
|
|
26
|
+
0,
|
|
27
|
+
1
|
|
28
|
+
],
|
|
29
|
+
[
|
|
30
|
+
0,
|
|
31
|
+
0,
|
|
32
|
+
-1
|
|
33
|
+
]
|
|
34
|
+
], n = [
|
|
35
|
+
3,
|
|
36
|
+
3,
|
|
37
|
+
3,
|
|
38
|
+
3
|
|
39
|
+
], r = [
|
|
40
|
+
15,
|
|
41
|
+
15,
|
|
42
|
+
15,
|
|
43
|
+
15
|
|
44
|
+
], i = [
|
|
45
|
+
0,
|
|
46
|
+
0,
|
|
47
|
+
0,
|
|
48
|
+
0
|
|
49
|
+
], a = [
|
|
50
|
+
[2, 1],
|
|
51
|
+
[0, 2],
|
|
52
|
+
[0, 1]
|
|
53
|
+
], o = (e) => e[0] ? +(e[0] < 0) : e[1] ? 2 + +(e[1] < 0) : 4 + +(e[2] < 0), s = (e, t, n = 1) => [
|
|
54
|
+
e[0] + t[0] * n,
|
|
55
|
+
e[1] + t[1] * n,
|
|
56
|
+
e[2] + t[2] * n
|
|
57
|
+
], c = (e, t) => [
|
|
58
|
+
e[1] * t[2] - e[2] * t[1],
|
|
59
|
+
e[2] * t[0] - e[0] * t[2],
|
|
60
|
+
e[0] * t[1] - e[1] * t[0]
|
|
61
|
+
], l = (e, t) => e[0] * t[0] + e[1] * t[1] + e[2] * t[2], u = (e, t) => e[0] === t[0] && e[1] === t[1] && e[2] === t[2], d = (e, t) => [
|
|
62
|
+
(e[0] + t[0]) / 2,
|
|
63
|
+
(e[1] + t[1]) / 2,
|
|
64
|
+
(e[2] + t[2]) / 2
|
|
65
|
+
], f = (e, t) => {
|
|
66
|
+
let n = [
|
|
67
|
+
0,
|
|
68
|
+
0,
|
|
69
|
+
0
|
|
70
|
+
];
|
|
71
|
+
return n[e] = t, n;
|
|
72
|
+
}, p = (e, t) => (((e[0] + 1024) * 2048 + e[1] + 1024) * 2048 + e[2] + 1024) * 8 + t, m = (e) => {
|
|
73
|
+
let t = Math.floor(e / 8);
|
|
74
|
+
return [
|
|
75
|
+
Math.floor(t / 4194304) - 1024,
|
|
76
|
+
Math.floor(t / 2048) % 2048 - 1024,
|
|
77
|
+
t % 2048 - 1024
|
|
78
|
+
];
|
|
79
|
+
}, h = (e) => Math.round(e * 65536);
|
|
80
|
+
function g(g, _, v = []) {
|
|
81
|
+
if (!(_ >= 0 && _ < .5)) throw RangeError(`Voxel bevel ${_} must be in [0, 0.5) blocks`);
|
|
82
|
+
let y = g.filter((e) => e.face < 6), b = [...y, ...v.filter((e) => e.face < 6)], x = y.length, S = (e, t, n) => {
|
|
83
|
+
let r = [
|
|
84
|
+
e.x,
|
|
85
|
+
e.y,
|
|
86
|
+
e.z
|
|
87
|
+
], [i, o] = a[e.face >> 1];
|
|
88
|
+
return r[i] += t, r[o] += n, r;
|
|
89
|
+
}, C = (e, t, n) => {
|
|
90
|
+
let r = [
|
|
91
|
+
e.x,
|
|
92
|
+
e.y,
|
|
93
|
+
e.z
|
|
94
|
+
], [i, o] = a[e.face >> 1];
|
|
95
|
+
return r[e.face >> 1] += +!(e.face & 1), r[i] += t, r[o] += n, r;
|
|
96
|
+
}, w = /* @__PURE__ */ new Map();
|
|
97
|
+
b.forEach((e, t) => {
|
|
98
|
+
for (let n = 0; n < e.w; n++) for (let r = 0; r < e.h; r++) w.set(p(S(e, n, r), e.face), t);
|
|
99
|
+
});
|
|
100
|
+
let T = (e, n, r) => {
|
|
101
|
+
let i = t[n], a = s(e, r), c = s(a, i);
|
|
102
|
+
if (w.has(p(e, o(r)))) return [
|
|
103
|
+
e,
|
|
104
|
+
o(r),
|
|
105
|
+
i
|
|
106
|
+
];
|
|
107
|
+
if (w.has(p(a, n))) return [
|
|
108
|
+
a,
|
|
109
|
+
n,
|
|
110
|
+
[
|
|
111
|
+
-r[0],
|
|
112
|
+
-r[1],
|
|
113
|
+
-r[2]
|
|
114
|
+
]
|
|
115
|
+
];
|
|
116
|
+
if (w.has(p(c, o([
|
|
117
|
+
-r[0],
|
|
118
|
+
-r[1],
|
|
119
|
+
-r[2]
|
|
120
|
+
])))) return [
|
|
121
|
+
c,
|
|
122
|
+
o([
|
|
123
|
+
-r[0],
|
|
124
|
+
-r[1],
|
|
125
|
+
-r[2]
|
|
126
|
+
]),
|
|
127
|
+
[
|
|
128
|
+
-i[0],
|
|
129
|
+
-i[1],
|
|
130
|
+
-i[2]
|
|
131
|
+
]
|
|
132
|
+
];
|
|
133
|
+
}, E = /* @__PURE__ */ new Map(), D = (e, t, n) => {
|
|
134
|
+
let r = p(e, t) * 6 + o(n), i = E.get(r);
|
|
135
|
+
if (i === void 0) {
|
|
136
|
+
let a = T(e, t, n), o = a && T(...a);
|
|
137
|
+
i = a && o && u(o[0], e) && o[1] === t ? a : null, E.set(r, i);
|
|
138
|
+
}
|
|
139
|
+
return i ?? void 0;
|
|
140
|
+
}, O = (e, t, n) => (D(e, t, n)?.[1] ?? t) !== t, k = (e, n, r) => {
|
|
141
|
+
let i = [...r];
|
|
142
|
+
for (let a = 0; a < 3; a++) {
|
|
143
|
+
if (t[n][a]) continue;
|
|
144
|
+
let o = f(a, r[a] === e[a] + 1 ? 1 : -1);
|
|
145
|
+
O(e, n, o) && (i[a] -= _ * o[a]);
|
|
146
|
+
}
|
|
147
|
+
return i;
|
|
148
|
+
}, A = [], j = (e) => {
|
|
149
|
+
let t = [
|
|
150
|
+
0,
|
|
151
|
+
0,
|
|
152
|
+
0
|
|
153
|
+
];
|
|
154
|
+
e.forEach((n, r) => {
|
|
155
|
+
let i = e[(r + 1) % e.length];
|
|
156
|
+
t[0] += (n[1] - i[1]) * (n[2] + i[2]), t[1] += (n[2] - i[2]) * (n[0] + i[0]), t[2] += (n[0] - i[0]) * (n[1] + i[1]);
|
|
157
|
+
});
|
|
158
|
+
let n = Math.hypot(...t);
|
|
159
|
+
return n > 1e-9 ? [
|
|
160
|
+
t[0] / n,
|
|
161
|
+
t[1] / n,
|
|
162
|
+
t[2] / n
|
|
163
|
+
] : [
|
|
164
|
+
0,
|
|
165
|
+
0,
|
|
166
|
+
0
|
|
167
|
+
];
|
|
168
|
+
}, M = (e, t, n = j(e)) => {
|
|
169
|
+
let r = b[t].face;
|
|
170
|
+
(r === 0 || r === 2 || r === 5) && e.reverse(), A.push({
|
|
171
|
+
points: e,
|
|
172
|
+
owners: e.map(() => t),
|
|
173
|
+
normal: n
|
|
174
|
+
});
|
|
175
|
+
}, N = /* @__PURE__ */ new Set(), P = [];
|
|
176
|
+
y.forEach((e, n) => {
|
|
177
|
+
let r = e.face, [i, o] = a[r >> 1], l = [], p = [], m = [], h = [];
|
|
178
|
+
for (let t = 0; t < e.w; t++) l.push(_ > 0 && O(S(e, t, 0), r, f(o, -1))), p.push(_ > 0 && O(S(e, t, e.h - 1), r, f(o, 1)));
|
|
179
|
+
for (let t = 0; t < e.h; t++) m.push(_ > 0 && O(S(e, 0, t), r, f(i, -1))), h.push(_ > 0 && O(S(e, e.w - 1, t), r, f(i, 1)));
|
|
180
|
+
let g = (e, t) => [
|
|
181
|
+
0,
|
|
182
|
+
...e.flatMap((n, r) => r && (n !== e[r - 1] || t[r] !== t[r - 1]) ? [r] : []),
|
|
183
|
+
e.length
|
|
184
|
+
], v = g(l, p), y = g(m, h);
|
|
185
|
+
for (let i = 1; i < v.length; i++) for (let a = 1; a < y.length; a++) {
|
|
186
|
+
let o = v[i - 1], s = v[i], c = y[a - 1], u = y[a], d = o === 0 && m[c] ? _ : 0, f = s === e.w && h[c] ? _ : 0, g = c === 0 && l[o] ? _ : 0, b = u === e.h && p[o] ? _ : 0;
|
|
187
|
+
M([
|
|
188
|
+
C(e, o + d, c + g),
|
|
189
|
+
C(e, s - f, c + g),
|
|
190
|
+
C(e, s - f, u - b),
|
|
191
|
+
C(e, o + d, u - b)
|
|
192
|
+
], n, t[r]);
|
|
193
|
+
}
|
|
194
|
+
if (!_) return;
|
|
195
|
+
for (let t = 0; t <= e.w; t++) for (let n = 0; n <= e.h; n++) (!t || !n || t === e.w || n === e.h) && N.add(C(e, t, n).join());
|
|
196
|
+
let b = [
|
|
197
|
+
[
|
|
198
|
+
e.w,
|
|
199
|
+
0,
|
|
200
|
+
1,
|
|
201
|
+
0,
|
|
202
|
+
f(o, -1)
|
|
203
|
+
],
|
|
204
|
+
[
|
|
205
|
+
e.w,
|
|
206
|
+
e.h - 1,
|
|
207
|
+
1,
|
|
208
|
+
0,
|
|
209
|
+
f(o, 1)
|
|
210
|
+
],
|
|
211
|
+
[
|
|
212
|
+
e.h,
|
|
213
|
+
0,
|
|
214
|
+
0,
|
|
215
|
+
1,
|
|
216
|
+
f(i, -1)
|
|
217
|
+
],
|
|
218
|
+
[
|
|
219
|
+
e.h,
|
|
220
|
+
e.w - 1,
|
|
221
|
+
0,
|
|
222
|
+
1,
|
|
223
|
+
f(i, 1)
|
|
224
|
+
]
|
|
225
|
+
];
|
|
226
|
+
for (let [a, l, p, m, h] of b) {
|
|
227
|
+
let m = t[r], g = c(m, h), _ = p ? i : o, v = g[_] > 0, y, b = () => {
|
|
228
|
+
if (!y) return;
|
|
229
|
+
let [e, t, r, i] = y;
|
|
230
|
+
A.push({
|
|
231
|
+
points: v ? [
|
|
232
|
+
t,
|
|
233
|
+
e,
|
|
234
|
+
r,
|
|
235
|
+
i
|
|
236
|
+
] : [
|
|
237
|
+
e,
|
|
238
|
+
t,
|
|
239
|
+
i,
|
|
240
|
+
r
|
|
241
|
+
],
|
|
242
|
+
owners: [
|
|
243
|
+
n,
|
|
244
|
+
n,
|
|
245
|
+
n,
|
|
246
|
+
n
|
|
247
|
+
],
|
|
248
|
+
normal: [
|
|
249
|
+
0,
|
|
250
|
+
0,
|
|
251
|
+
0
|
|
252
|
+
]
|
|
253
|
+
}), A[A.length - 1].normal = j(A[A.length - 1].points), y = void 0;
|
|
254
|
+
};
|
|
255
|
+
for (let t = 0; t < a; t++) {
|
|
256
|
+
let n = p ? S(e, t, l) : S(e, l, t), a = D(n, r, h);
|
|
257
|
+
if (!a || a[1] === r) {
|
|
258
|
+
b();
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
let c = C(e, p ? t : l + +(h[i] > 0), p ? l + +(h[o] > 0) : t), m = s(c, f(_, 1)), g = k(n, r, c), v = k(n, r, m), x = d(g, k(a[0], a[1], c)), w = d(v, k(a[0], a[1], m));
|
|
262
|
+
y && u(y[1], g) && u(y[3], x) ? (y[1] = v, y[3] = w) : (b(), y = [
|
|
263
|
+
g,
|
|
264
|
+
v,
|
|
265
|
+
x,
|
|
266
|
+
w
|
|
267
|
+
]);
|
|
268
|
+
}
|
|
269
|
+
b();
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
for (let e of N) {
|
|
273
|
+
let n = e.split(",").map(Number), r = [], i = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Map();
|
|
274
|
+
for (let e = 0; e < 8; e++) {
|
|
275
|
+
let i = [
|
|
276
|
+
n[0] - (e & 1),
|
|
277
|
+
n[1] - (e >> 1 & 1),
|
|
278
|
+
n[2] - (e >> 2 & 1)
|
|
279
|
+
];
|
|
280
|
+
for (let e = 0; e < 3; e++) {
|
|
281
|
+
let a = o(f(e, n[e] === i[e] + 1 ? 1 : -1)), u = p(i, a);
|
|
282
|
+
if (!w.has(u)) continue;
|
|
283
|
+
let [d, m] = [
|
|
284
|
+
0,
|
|
285
|
+
1,
|
|
286
|
+
2
|
|
287
|
+
].filter((t) => t !== e), h = f(d, n[d] === i[d] + 1 ? 1 : -1), g = f(m, n[m] === i[m] + 1 ? 1 : -1), [_, v] = l(c(t[a], h), g) > 0 ? [h, g] : [g, h];
|
|
288
|
+
r.push(u), s.set(u, [_, v]);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
for (let e of r) {
|
|
292
|
+
let t = m(e), n = e % 8, [r, o] = s.get(e), c = D(t, n, r), l = D(t, n, o);
|
|
293
|
+
i.set(e, c && p(c[0], c[1])), a.set(e, l && p(l[0], l[1]));
|
|
294
|
+
}
|
|
295
|
+
let h = /* @__PURE__ */ new Set(), g = r.some((e) => w.get(e) >= x);
|
|
296
|
+
for (let e of r) {
|
|
297
|
+
if (h.has(e)) continue;
|
|
298
|
+
let t = e;
|
|
299
|
+
for (let n = a.get(t), i = 0; n !== void 0 && n !== e && i < r.length; n = a.get(n), i++) t = n;
|
|
300
|
+
let o = [];
|
|
301
|
+
for (let e = t; e !== void 0 && !h.has(e); e = i.get(e)) h.add(e), o.push(e);
|
|
302
|
+
let c = i.get(o[o.length - 1]) === o[0], l = [], f = [];
|
|
303
|
+
o.forEach((e, t) => {
|
|
304
|
+
let r = m(e), i = e % 8, a = k(r, i, n), u = o[(t + 1) % o.length];
|
|
305
|
+
l.push(a), f.push(w.get(e)), t === o.length - 1 && !c ? (l.push(n), f.push(w.get(o[0]))) : O(r, i, s.get(e)[0]) && (l.push(d(a, k(m(u), u % 8, n))), f.push(w.get(u)));
|
|
306
|
+
}), g && P.push(...l.map((e) => [...e]));
|
|
307
|
+
let p = (e, t) => {
|
|
308
|
+
f[(e - 1 + l.length) % l.length] = t, l.splice(e, 1), f.splice(e, 1);
|
|
309
|
+
};
|
|
310
|
+
for (let e = 0; l.length > 2 && e < l.length * 4; e++) {
|
|
311
|
+
let t = e % l.length, n = (t + 1) % l.length, r = (t + 2) % l.length;
|
|
312
|
+
if (u(l[t], l[n])) p(n, f[n]);
|
|
313
|
+
else if (u(l[t], l[r])) p(n, f[t]);
|
|
314
|
+
else continue;
|
|
315
|
+
e = -1;
|
|
316
|
+
}
|
|
317
|
+
let _ = j(l);
|
|
318
|
+
if (l.length < 3 || !_.some(Boolean)) continue;
|
|
319
|
+
let v = [
|
|
320
|
+
0,
|
|
321
|
+
1,
|
|
322
|
+
2
|
|
323
|
+
].map((e) => l.reduce((t, n) => t + n[e], 0) / l.length);
|
|
324
|
+
A.push({
|
|
325
|
+
points: l,
|
|
326
|
+
owners: f,
|
|
327
|
+
normal: _,
|
|
328
|
+
center: v
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
let F = /* @__PURE__ */ new Map(), I = (e, t) => `${t},${h(e[(t + 1) % 3])},${h(e[(t + 2) % 3])}`;
|
|
333
|
+
for (let e of [...A.map((e) => e.points), P]) for (let t of e) for (let e = 0; e < 3; e++) {
|
|
334
|
+
let n = I(t, e), r = F.get(n) ?? [];
|
|
335
|
+
r.push(t[e]), F.set(n, r);
|
|
336
|
+
}
|
|
337
|
+
for (let e of F.values()) e.sort((e, t) => e - t);
|
|
338
|
+
for (let e of A) {
|
|
339
|
+
let t = [], n = [];
|
|
340
|
+
e.points.forEach((r, i) => {
|
|
341
|
+
let a = e.points[(i + 1) % e.points.length];
|
|
342
|
+
t.push(r), n.push(e.owners[i]);
|
|
343
|
+
let o = [
|
|
344
|
+
0,
|
|
345
|
+
1,
|
|
346
|
+
2
|
|
347
|
+
].filter((e) => h(r[e]) !== h(a[e]));
|
|
348
|
+
if (o.length !== 1) return;
|
|
349
|
+
let s = o[0], c = F.get(I(r, s)), l = Math.min(r[s], a[s]), u = Math.max(r[s], a[s]), d = c.filter((e, t) => h(e) > h(l) && h(e) < h(u) && (!t || h(c[t - 1]) !== h(e)));
|
|
350
|
+
a[s] < r[s] && d.reverse();
|
|
351
|
+
for (let a of d) {
|
|
352
|
+
let o = [...r];
|
|
353
|
+
o[s] = a, t.push(o), n.push(e.owners[i]);
|
|
354
|
+
}
|
|
355
|
+
}), e.points = t, e.owners = n;
|
|
356
|
+
}
|
|
357
|
+
let L = [], R = [], z = [], B = [], V = [], H = [], U = (t, o, s) => {
|
|
358
|
+
let c = b[o], [l, u] = a[c.face >> 1], d = C(c, 0, 0), f = Math.min(Math.max((t[l] - d[l]) / c.w, 0), 1), p = Math.min(Math.max((t[u] - d[u]) / c.h, 0), 1), m = C(c, f * c.w, p * c.h), h = (e) => (e[0] * (1 - f) + e[1] * f) * (1 - p) + (e[3] * (1 - f) + e[2] * f) * p;
|
|
359
|
+
return L.push(t[0], t[1], t[2]), R.push(s[0], s[1], s[2]), z.push(c.material ?? 0), V.push(...e(c.face, m)), B.push(h(c.ao ?? n), h(c.sky ?? r), h(c.block ?? i)), L.length / 3 - 1;
|
|
360
|
+
};
|
|
361
|
+
for (let { points: e, owners: t, normal: n, center: r } of A) {
|
|
362
|
+
if (!r && e.length === 4) {
|
|
363
|
+
let [r, i, a, o] = e.map((e) => U(e, t[0], n));
|
|
364
|
+
H.push(r, i, a, r, a, o);
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
let i = r ?? [
|
|
368
|
+
0,
|
|
369
|
+
1,
|
|
370
|
+
2
|
|
371
|
+
].map((t) => e.reduce((e, n) => e + n[t], 0) / e.length), a = /* @__PURE__ */ new Map(), o = (e, t) => {
|
|
372
|
+
let r = `${e.join()}|${t}`, i = a.get(r);
|
|
373
|
+
return i === void 0 && a.set(r, i = U(e, t, n)), i;
|
|
374
|
+
};
|
|
375
|
+
e.forEach((n, r) => {
|
|
376
|
+
let a = t[r];
|
|
377
|
+
a < x && H.push(o(n, a), o(e[(r + 1) % e.length], a), o(i, a));
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
return {
|
|
381
|
+
positions: Float32Array.from(L),
|
|
382
|
+
normals: Float32Array.from(R),
|
|
383
|
+
indices: Uint32Array.from(H),
|
|
384
|
+
materials: Uint32Array.from(z),
|
|
385
|
+
uv: Float32Array.from(V),
|
|
386
|
+
lighting: Float32Array.from(B)
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
var _ = 12, v = {
|
|
390
|
+
layout: {
|
|
391
|
+
arrayStride: 48,
|
|
392
|
+
attributes: [
|
|
393
|
+
{
|
|
394
|
+
shaderLocation: 0,
|
|
395
|
+
offset: 0,
|
|
396
|
+
format: "float32x3"
|
|
397
|
+
},
|
|
398
|
+
{
|
|
399
|
+
shaderLocation: 1,
|
|
400
|
+
offset: 12,
|
|
401
|
+
format: "float32x3"
|
|
402
|
+
},
|
|
403
|
+
{
|
|
404
|
+
shaderLocation: 2,
|
|
405
|
+
offset: 24,
|
|
406
|
+
format: "float32x2"
|
|
407
|
+
},
|
|
408
|
+
{
|
|
409
|
+
shaderLocation: 3,
|
|
410
|
+
offset: 32,
|
|
411
|
+
format: "float32x3"
|
|
412
|
+
},
|
|
413
|
+
{
|
|
414
|
+
shaderLocation: 4,
|
|
415
|
+
offset: 44,
|
|
416
|
+
format: "uint32"
|
|
417
|
+
}
|
|
418
|
+
]
|
|
419
|
+
},
|
|
420
|
+
origins: {
|
|
421
|
+
arrayStride: 16,
|
|
422
|
+
stepMode: "instance",
|
|
423
|
+
attributes: [{
|
|
424
|
+
shaderLocation: 5,
|
|
425
|
+
offset: 0,
|
|
426
|
+
format: "float32x3"
|
|
427
|
+
}]
|
|
428
|
+
},
|
|
429
|
+
shader: "\nstruct In {@location(0) p:vec3f,@location(1) n:vec3f,@location(2) uv:vec2f,@location(3) lighting:vec3f,@location(4) material:u32,@location(5) chunk:vec3f}\nstruct Out {@builtin(position) clip:vec4f,@location(0) n:vec3f,@location(1) @interpolate(flat) material:u32,@location(2) uv:vec2f,@location(3) world:vec3f,@location(4) lighting:vec3f}\n@vertex fn vertex(v:In)->Out {var o:Out;let p=v.p+v.chunk;o.clip=f.camera*vec4f(p,1.0);o.n=v.n;o.material=v.material;o.uv=v.uv;o.world=p;o.lighting=v.lighting;return o;}\n@fragment fn fragment(o:Out,@builtin(front_facing) front:bool)->@location(0) vec4f {\n let id=(o.material&4095u)*2u;let m=voxelMaterials[id];let maps=voxelMaterials[id+1u];\n let uv=o.uv;let dx=dpdx(uv);let dy=dpdy(uv);let lit=o.lighting;\n var i:VoxelPixel;i.clip=o.clip;i.position=o.world;\n let outward=normalize(o.n);let normal=outward*select(-1.0,1.0,front);let tangent=voxelTangent(o.material>>12u);",
|
|
430
|
+
shadow: "\nstruct BevelOut {@builtin(position) @invariant clip:vec4f,@location(0) @interpolate(flat) material:u32,@location(1) uv:vec2f}\n@vertex fn bevelVertex(@location(0) v:vec3f,@location(2) uv:vec2f,@location(4) material:u32,@location(5) chunk:vec3f)->BevelOut {let p=v+chunk;return BevelOut(shadow.matrix*vec4f(p,1.0),material,uv);}\n@fragment fn bevelCutout(o:BevelOut) {if(!voxelOpaque(voxelMaterials[(o.material&4095u)*2u],o.uv,shadow.time)){discard;}}\n"
|
|
431
|
+
};
|
|
432
|
+
function y(e) {
|
|
433
|
+
if (!(e > 0 && e < .5)) throw RangeError(`voxelBevel ${e} must be in (0, 0.5) blocks`);
|
|
434
|
+
return {
|
|
435
|
+
amount: e,
|
|
436
|
+
...v,
|
|
437
|
+
mesh(t, n, r) {
|
|
438
|
+
let i = g(t, e, r), { positions: a, indices: o } = i, s = i.materials.length, c = new Float32Array(s * _), l = new Uint32Array(c.buffer), u = new Float32Array(o.length);
|
|
439
|
+
for (let e = 0, t = 0; e < s; e++, t += _) {
|
|
440
|
+
for (let r = 0; r < 3; r++) c[t + r] = a[e * 3 + r] + n[r], c[t + 3 + r] = i.normals[e * 3 + r], c[t + 8 + r] = i.lighting[e * 3 + r];
|
|
441
|
+
c[t + 6] = i.uv[e * 2], c[t + 7] = i.uv[e * 2 + 1], l[t + 11] = i.materials[e];
|
|
442
|
+
}
|
|
443
|
+
for (let e = 0; e < o.length; e++) for (let t = 0; t < 3; t++) u[e - e % 3 + t] += a[o[e] * 3 + t] / 3;
|
|
444
|
+
return {
|
|
445
|
+
vertices: c,
|
|
446
|
+
indices: o,
|
|
447
|
+
centers: u
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
//#endregion
|
|
453
|
+
export { g as bevelQuads, y as voxelBevel };
|