@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.
Files changed (90) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +1670 -2
  4. package/dist/bevel.d.ts +79 -0
  5. package/dist/bevel.js +453 -0
  6. package/dist/blocks-BabtpQT-.js +179 -0
  7. package/dist/blocks.d.ts +220 -0
  8. package/dist/border.d.ts +9 -0
  9. package/dist/chunk-BCoaKSn4.js +158 -0
  10. package/dist/chunk.d.ts +112 -0
  11. package/dist/codec-BmXhcsFt.js +412 -0
  12. package/dist/codec.d.ts +125 -0
  13. package/dist/codec.js +1 -0
  14. package/dist/debris.d.ts +114 -0
  15. package/dist/debris.js +387 -0
  16. package/dist/faces-DUR7LhrN.js +146 -0
  17. package/dist/faces.d.ts +7 -0
  18. package/dist/fluids.d.ts +74 -0
  19. package/dist/fluids.js +226 -0
  20. package/dist/generate.d.ts +78 -0
  21. package/dist/generate.js +99 -0
  22. package/dist/gi.d.ts +76 -0
  23. package/dist/gi.js +264 -0
  24. package/dist/gpu-mesher.d.ts +77 -0
  25. package/dist/gpu-mesher.js +461 -0
  26. package/dist/granular-mpm.d.ts +40 -0
  27. package/dist/granular-mpm.js +46 -0
  28. package/dist/index.d.ts +6 -0
  29. package/dist/index.js +4 -0
  30. package/dist/light-rgb.d.ts +71 -0
  31. package/dist/light-rgb.js +287 -0
  32. package/dist/light-worker.d.ts +84 -0
  33. package/dist/light-worker.js +110 -0
  34. package/dist/light.d.ts +108 -0
  35. package/dist/light.js +332 -0
  36. package/dist/light.worker.d.ts +1 -0
  37. package/dist/lightmap.d.ts +50 -0
  38. package/dist/lightmap.js +68 -0
  39. package/dist/lod.d.ts +87 -0
  40. package/dist/lod.js +182 -0
  41. package/dist/mesher.d.ts +214 -0
  42. package/dist/mesher.js +539 -0
  43. package/dist/mesher.worker.d.ts +1 -0
  44. package/dist/model-data-D_rIKJCQ.js +41 -0
  45. package/dist/model-data.d.ts +21 -0
  46. package/dist/model-mesh-C4Gi6bMM.js +122 -0
  47. package/dist/model-mesh.d.ts +21 -0
  48. package/dist/model-surface.d.ts +5 -0
  49. package/dist/models.d.ts +74 -0
  50. package/dist/models.js +308 -0
  51. package/dist/objects.d.ts +73 -0
  52. package/dist/objects.js +152 -0
  53. package/dist/octants-C_Ic7gdn.js +10 -0
  54. package/dist/octants.d.ts +16 -0
  55. package/dist/origin-DsMelUxs.js +23 -0
  56. package/dist/origin.d.ts +7 -0
  57. package/dist/physics.d.ts +147 -0
  58. package/dist/physics.js +486 -0
  59. package/dist/query.d.ts +166 -0
  60. package/dist/query.js +305 -0
  61. package/dist/render-post.d.ts +6 -0
  62. package/dist/render.d.ts +296 -0
  63. package/dist/render.js +1616 -0
  64. package/dist/shared/borrowed.d.ts +44 -0
  65. package/dist/smooth-CEwHkXli.js +968 -0
  66. package/dist/smooth-clip.d.ts +24 -0
  67. package/dist/smooth-lod.d.ts +118 -0
  68. package/dist/smooth-lod.js +244 -0
  69. package/dist/smooth.d.ts +171 -0
  70. package/dist/smooth.js +1 -0
  71. package/dist/stream.d.ts +201 -0
  72. package/dist/stream.js +402 -0
  73. package/dist/terrain.worker.d.ts +1 -0
  74. package/dist/textures.d.ts +86 -0
  75. package/dist/ticks.d.ts +126 -0
  76. package/dist/ticks.js +239 -0
  77. package/dist/unit-light-Ch7roJgB.js +7 -0
  78. package/dist/vox.d.ts +237 -0
  79. package/dist/vox.js +780 -0
  80. package/dist/wind-data.d.ts +13 -0
  81. package/dist/wind.d.ts +68 -0
  82. package/dist/wind.js +170 -0
  83. package/dist/worker-DlGRxlc4.js +44 -0
  84. package/dist/worker-pool-sPyC6jCE.js +93 -0
  85. package/dist/workers/light.js +1 -0
  86. package/dist/workers/mesher.js +1 -0
  87. package/dist/workers/terrain.js +1 -0
  88. package/dist/world-CelTlft2.js +153 -0
  89. package/dist/world.d.ts +117 -0
  90. package/package.json +173 -4
@@ -0,0 +1,114 @@
1
+ /// <reference lib="esnext.disposable" preserve="true" />
2
+ import type { Engine, Mesh, PbrMaterial } from '@margoengine/core';
3
+ import type { Vec3 } from '@margoengine/math';
4
+ // @ts-ignore optional peer: its types apply when it is installed
5
+ import type { Physics, PhysicsBody } from '@margoengine/physics';
6
+ import type { BlockType } from './blocks.js';
7
+ import { VoxelWorld } from './world.js';
8
+ import type { VoxelOrigin } from './render.js';
9
+ /** Options of {@link VoxelDebris}. */
10
+ export interface VoxelDebrisOptions {
11
+ /**
12
+ * The physics world for the pieces: each becomes a dynamic body there (with `@margoengine/voxel/physics` on the same
13
+ * world, it lands on the blocks). Default none: pieces are only taken out of the world and reported.
14
+ */
15
+ physics?: Physics;
16
+ /** Draws each piece as core meshes (one per block type) that follow its body. Default none: nothing is drawn. */
17
+ engine?: Engine;
18
+ /** Cells at this y or lower hold everything joined to them. Default `-Infinity`: only anchors and unloaded chunks. */
19
+ floor?: number;
20
+ /** Block types (ids or names) that never fall and hold everything joined to them, such as bedrock. Default none. */
21
+ anchors?: readonly (number | string)[];
22
+ /**
23
+ * A joined group of this many cells or more stays in the world as if held, which bounds the search after an edit.
24
+ * Integer 1 to 65,536, default 4,096.
25
+ */
26
+ maxCells?: number;
27
+ /** Most pieces alive at once; breaking one more removes the oldest. Integer 1 or more, default 64. */
28
+ maxPieces?: number;
29
+ /** Seconds a piece lives (counted by {@link VoxelDebris.update}'s `delta`) before it is removed. Default `Infinity`. */
30
+ lifetime?: number;
31
+ /** Mass per cell in kg (density of a 1 x 1 x 1 block). Default 1. A number > 0. */
32
+ density?: number;
33
+ /** Friction of the pieces' colliders, default 0.6. A number >= 0. */
34
+ friction?: number;
35
+ /** Core material per block type; default a `PbrMaterial` of the type's colour and roughness, one per type. */
36
+ material?: (type: BlockType) => PbrMaterial;
37
+ /**
38
+ * Origin rebasing for worlds far from zero, as `VoxelRendererOptions.origin` (pass the same object): an object whose
39
+ * `origin` is the float64 world point (blocks) drawn at engine zero, such as a `FloatingOrigin` of
40
+ * `@margoengine/cameras/origin`; read every update. Pieces' meshes sit at their centre minus the origin in float64.
41
+ * Not with `physics`, which simulates in world coordinates (docs/known-bugs.md). Default null (world and engine
42
+ * space are the same).
43
+ */
44
+ origin?: VoxelOrigin | null;
45
+ }
46
+ /** One broken-off group of blocks. */
47
+ export interface DebrisPiece {
48
+ /** Increasing id, from 1. */
49
+ readonly id: number;
50
+ /** The world cells the piece held when it broke off: x, y, z per cell. */
51
+ readonly cells: Int32Array;
52
+ /** The block state of each cell, in the order of `cells`. */
53
+ readonly states: Uint32Array;
54
+ /** Where the body started: the mean of the cell centres, in world units. Mesh vertices are relative to it. */
55
+ readonly center: Vec3;
56
+ /** Boxes of merged cells, as `[x0, y0, z0, x1, y1, z1)` world cells, six numbers per box: the collider's parts. */
57
+ readonly boxes: Int32Array;
58
+ /** Its dynamic body, or null without `physics`. Its `userData` is this piece. */
59
+ readonly body: PhysicsBody | null;
60
+ /** Its core meshes, one per block type; the first is the body's mesh and the others are its children. */
61
+ readonly meshes: readonly Mesh[];
62
+ /** Seconds since it broke off. */
63
+ readonly age: number;
64
+ }
65
+ /**
66
+ * Voxel destruction: watches a {@link VoxelWorld}'s edits and, on {@link VoxelDebris.update}, breaks off every group of
67
+ * solid blocks (face-joined, `solid` block types) that an edit cut loose. A group is held when it reaches a cell at
68
+ * `floor` or below, an `anchors` block, an unloaded chunk or `maxCells` cells; any other group is cleared from the world
69
+ * in one `setBlocks` and becomes a {@link DebrisPiece}: a dynamic body in `physics` and core meshes in `engine`.
70
+ *
71
+ * Call `update(delta)` after edits and before `physics.step()`, so the voxel colliders lose the cells before the piece
72
+ * moves. Only removals start a search, from the six cells beside each removed solid block.
73
+ */
74
+ export declare class VoxelDebris {
75
+ #private;
76
+ readonly world: VoxelWorld;
77
+ readonly physics: Physics | null;
78
+ readonly maxCells: number;
79
+ readonly maxPieces: number;
80
+ readonly lifetime: number;
81
+ readonly floor: number;
82
+ /** Live {@link VoxelDebrisOptions.origin}; null for none. */
83
+ origin: VoxelOrigin | null;
84
+ /**
85
+ * @throws TypeError when `world` is not a VoxelWorld, `physics` not a Physics, `engine` not an Engine or an option
86
+ * of the wrong type.
87
+ * @throws RangeError for an option out of range or an anchor that is not a defined block type.
88
+ * @throws MargoError `unsupported` for `origin` with `physics`.
89
+ */
90
+ constructor(world: VoxelWorld, options?: VoxelDebrisOptions);
91
+ /** The pieces alive, oldest first. */
92
+ get pieces(): readonly DebrisPiece[];
93
+ /** Cells waiting to be searched from at the next {@link VoxelDebris.update}. */
94
+ get pending(): number;
95
+ /** Calls `listener` with each piece as it breaks off, before `update` returns. Returns a function that unsubscribes. */
96
+ onBreak(listener: (piece: DebrisPiece) => void): () => void;
97
+ /**
98
+ * Breaks off the groups cut loose since the last call, ages every piece by `delta` seconds and removes those past
99
+ * `lifetime`. Moves the pieces' meshes first when the origin moved. Returns the pieces broken off by this call.
100
+ * @throws RangeError when `delta` is not a number >= 0 or the origin holds a value that is not a finite number.
101
+ * @throws MargoError `disposed` after dispose(); `unsupported` for an origin with `physics`.
102
+ */
103
+ update(delta?: number): DebrisPiece[];
104
+ /** Removes `piece`'s body and meshes now (it does not go back into the world). Unknown pieces are ignored. */
105
+ remove(piece: DebrisPiece): void;
106
+ /** Removes every piece and stops watching the world. Calling it again does nothing. */
107
+ dispose(): void;
108
+ [Symbol.dispose](): void;
109
+ }
110
+ /**
111
+ * Merges cells (x, y, z per cell) into boxes `[x0, y0, z0, x1, y1, z1)`, six numbers per box, that cover exactly those
112
+ * cells: runs along x, then runs of equal x span along z, then equal rectangles along y. Each cell is in one box.
113
+ */
114
+ export declare function mergeBoxes(cells: ArrayLike<number>): Int32Array;
package/dist/debris.js ADDED
@@ -0,0 +1,387 @@
1
+ import { n as e, t } from "./world-CelTlft2.js";
2
+ import { n, t as r } from "./origin-DsMelUxs.js";
3
+ import { MargoError as i } from "@margoengine/math/errors";
4
+ import { Geometry as a, PbrMaterial as o } from "@margoengine/core";
5
+ //#region src/debris.ts
6
+ var s = [
7
+ 1,
8
+ -1,
9
+ 0,
10
+ 0,
11
+ 0,
12
+ 0
13
+ ], c = [
14
+ 0,
15
+ 0,
16
+ 1,
17
+ -1,
18
+ 0,
19
+ 0
20
+ ], l = [
21
+ 0,
22
+ 0,
23
+ 0,
24
+ 0,
25
+ 1,
26
+ -1
27
+ ], u = [
28
+ [
29
+ 1,
30
+ 0,
31
+ 0,
32
+ 1,
33
+ 1,
34
+ 0,
35
+ 1,
36
+ 1,
37
+ 1,
38
+ 1,
39
+ 0,
40
+ 1
41
+ ],
42
+ [
43
+ 0,
44
+ 0,
45
+ 0,
46
+ 0,
47
+ 0,
48
+ 1,
49
+ 0,
50
+ 1,
51
+ 1,
52
+ 0,
53
+ 1,
54
+ 0
55
+ ],
56
+ [
57
+ 0,
58
+ 1,
59
+ 0,
60
+ 0,
61
+ 1,
62
+ 1,
63
+ 1,
64
+ 1,
65
+ 1,
66
+ 1,
67
+ 1,
68
+ 0
69
+ ],
70
+ [
71
+ 0,
72
+ 0,
73
+ 0,
74
+ 1,
75
+ 0,
76
+ 0,
77
+ 1,
78
+ 0,
79
+ 1,
80
+ 0,
81
+ 0,
82
+ 1
83
+ ],
84
+ [
85
+ 0,
86
+ 0,
87
+ 1,
88
+ 1,
89
+ 0,
90
+ 1,
91
+ 1,
92
+ 1,
93
+ 1,
94
+ 0,
95
+ 1,
96
+ 1
97
+ ],
98
+ [
99
+ 0,
100
+ 0,
101
+ 0,
102
+ 0,
103
+ 1,
104
+ 0,
105
+ 1,
106
+ 1,
107
+ 0,
108
+ 1,
109
+ 0,
110
+ 0
111
+ ]
112
+ ], d = (e, t, n) => {
113
+ if (typeof t != "number") throw TypeError(`VoxelDebris ${e} must be a number; ${n}`);
114
+ if (!(t > 0)) throw RangeError(`VoxelDebris ${e} is ${t}; ${n}`);
115
+ return t;
116
+ }, f = (e, t, n) => {
117
+ if (typeof t != "number") throw TypeError(`VoxelDebris ${e} must be a number; pass a whole number`);
118
+ if (!Number.isInteger(t) || t < 1 || t > n) throw RangeError(`VoxelDebris ${e} is ${t}; pass a whole number from 1 to ${n}`);
119
+ return t;
120
+ }, p = class {
121
+ #e;
122
+ #t;
123
+ #n;
124
+ #r;
125
+ #i = /* @__PURE__ */ new Map();
126
+ #a;
127
+ #o = /* @__PURE__ */ new Uint8Array();
128
+ #s = /* @__PURE__ */ new Uint8Array();
129
+ #c = [];
130
+ #l = /* @__PURE__ */ new Map();
131
+ #u = 0;
132
+ #d = [];
133
+ #f = 1;
134
+ #p = !1;
135
+ #m = /* @__PURE__ */ new Set();
136
+ #h;
137
+ #g = !1;
138
+ #_ = [
139
+ 0,
140
+ 0,
141
+ 0
142
+ ];
143
+ constructor(e, n = {}) {
144
+ if (!(e instanceof t)) throw TypeError("VoxelDebris needs a VoxelWorld; pass new VoxelWorld({blocks})");
145
+ if (!n || typeof n != "object") throw TypeError("VoxelDebris options must be an object; pass {}");
146
+ let { physics: i, engine: a, floor: s = -Infinity, anchors: c = [], maxCells: l = 4096, maxPieces: u = 64, lifetime: p = Infinity, density: m = 1, friction: h = .6, material: g, origin: _ } = n;
147
+ if (i !== void 0 && (typeof i?.addBody != "function" || typeof i.removeBody != "function")) throw TypeError("VoxelDebris physics must be a Physics; pass Physics.attach(engine) from @margoengine/physics");
148
+ if (a !== void 0 && typeof a?.createMesh != "function") throw TypeError("VoxelDebris engine must be an Engine; pass the engine that draws the world");
149
+ if (typeof s != "number" || Number.isNaN(s)) throw TypeError("VoxelDebris floor must be a number; pass a block y such as 0, or -Infinity");
150
+ if (!Array.isArray(c)) throw TypeError("VoxelDebris anchors must be an array; pass block ids or names such as ['bedrock']");
151
+ for (let t of c) e.blocks.get(typeof t == "string" ? e.blocks.id(t) : t);
152
+ if (typeof p != "number" || !(p > 0)) throw RangeError(`VoxelDebris lifetime is ${p}; pass seconds > 0, or Infinity`);
153
+ if (typeof h != "number" || !(h >= 0) || h === Infinity) throw RangeError(`VoxelDebris friction is ${h}; pass a number >= 0 such as 0.6`);
154
+ if (g !== void 0 && typeof g != "function") throw TypeError("VoxelDebris material must be a function; pass (type) => new PbrMaterial({ color: type.color })");
155
+ this.world = e, this.physics = i ?? null, this.origin = r(_, "VoxelDebris"), this.#v(), this.#e = a ?? null, this.floor = s, this.#a = c, this.maxCells = f("maxCells", l, 65536), this.maxPieces = f("maxPieces", u, 2 ** 31 - 1), this.lifetime = p, this.#t = d("density", m, "pass kg per cell > 0 such as 1"), this.#n = h, this.#r = g ?? ((e) => new o({
156
+ color: [...e.color],
157
+ roughness: e.roughness,
158
+ metalness: e.metalness
159
+ })), this.#h = e.onChange(this.#y);
160
+ }
161
+ get pieces() {
162
+ return this.#d;
163
+ }
164
+ get pending() {
165
+ return this.#c.length / 3;
166
+ }
167
+ onBreak(e) {
168
+ if (typeof e != "function") throw TypeError("VoxelDebris.onBreak needs a function; pass (piece) => {}");
169
+ return this.#m.add(e), () => this.#m.delete(e);
170
+ }
171
+ update(e = 0) {
172
+ if (this.#g) throw new i("disposed", "VoxelDebris is disposed; create a new one");
173
+ if (typeof e != "number" || !(e >= 0) || e === Infinity) throw RangeError(`VoxelDebris.update delta is ${e}; pass seconds >= 0 such as 1 / 60`);
174
+ let t = this.#v();
175
+ if (t.some((e, t) => e !== this.#_[t])) {
176
+ this.#_ = t;
177
+ for (let { meshes: e, center: n } of this.#d) e[0]?.position.set(n[0] - t[0], n[1] - t[1], n[2] - t[2]);
178
+ }
179
+ let n = [];
180
+ if (this.#c.length) {
181
+ this.#b();
182
+ let e = this.#c;
183
+ this.#c = [];
184
+ for (let t = 0; t < e.length; t += 3) {
185
+ let r = this.#x(e[t], e[t + 1], e[t + 2]);
186
+ r && n.push(this.#S(r.cells, r.states));
187
+ }
188
+ this.#l.clear(), this.#u = 0;
189
+ }
190
+ for (let t of this.#d) t.age += e;
191
+ for (; this.#d.length && this.#d[0].age >= this.lifetime;) this.#w(this.#d[0]);
192
+ for (let e of n) for (let t of [...this.#m]) t(e);
193
+ return n;
194
+ }
195
+ remove(e) {
196
+ let t = this.#d.find((t) => t === e);
197
+ t && this.#w(t);
198
+ }
199
+ dispose() {
200
+ if (!this.#g) {
201
+ this.#g = !0, this.#h();
202
+ for (let e of [...this.#d]) this.#w(e);
203
+ this.#c = [], this.#m.clear();
204
+ }
205
+ }
206
+ [Symbol.dispose]() {
207
+ this.dispose();
208
+ }
209
+ #v() {
210
+ if (this.origin && this.physics) throw new i("unsupported", "VoxelDebris origin with physics: physics simulates in world coordinates; use physics near zero, or no origin");
211
+ return n(this.origin, "VoxelDebris");
212
+ }
213
+ #y = (e, t, n, r, i) => {
214
+ if (!this.#p && (this.#b(), this.#o[i & 65535] && !this.#o[r & 65535])) for (let r = 0; r < 6; r++) this.#c.push(e + s[r], t + c[r], n + l[r]);
215
+ };
216
+ #b() {
217
+ let e = this.world.blocks.table;
218
+ if (this.#o.length !== e.length) {
219
+ this.#o = Uint8Array.from(e, (e) => +(e.solid && !e.liquid)), this.#s = new Uint8Array(e.length);
220
+ for (let e of this.#a) this.#s[typeof e == "string" ? this.world.blocks.id(e) : e] = 1;
221
+ }
222
+ }
223
+ #x(t, n, r) {
224
+ let i = this.world, a = this.#o, o = this.#s, s = ++this.#u, c = [
225
+ t,
226
+ n,
227
+ r
228
+ ], l = [], u = [];
229
+ for (; c.length;) {
230
+ let t = c.pop(), n = c.pop(), r = c.pop(), d = i.getChunk(r >> 5, n >> 5, t >> 5);
231
+ if (!d) return null;
232
+ let f = d.get(r & 31, n & 31, t & 31);
233
+ if (!a[f & 65535]) continue;
234
+ let p = e(r >> 5, n >> 5, t >> 5), m = this.#l.get(p);
235
+ m || this.#l.set(p, m = /* @__PURE__ */ new Int32Array(32768));
236
+ let h = r & 31 | (t & 31) << 5 | (n & 31) << 10, g = m[h];
237
+ if (g !== s) {
238
+ if (g || (m[h] = s, n <= this.floor || o[f & 65535] || l.length / 3 + 1 >= this.maxCells)) return null;
239
+ l.push(r, n, t), u.push(f), c.push(r, n + 1, t, r + 1, n, t, r - 1, n, t, r, n, t + 1, r, n, t - 1, r, n - 1, t);
240
+ }
241
+ }
242
+ return l.length ? {
243
+ cells: l,
244
+ states: u
245
+ } : null;
246
+ }
247
+ #S(e, t) {
248
+ let n = Int32Array.from(e), r = Uint32Array.from(t), i = r.length, a = Array(i * 4), o = [
249
+ 0,
250
+ 0,
251
+ 0
252
+ ];
253
+ for (let e = 0; e < i; e++) {
254
+ for (let t = 0; t < 3; t++) a[e * 4 + t] = n[e * 3 + t], o[t] += n[e * 3 + t] + .5;
255
+ a[e * 4 + 3] = 0;
256
+ }
257
+ for (let e = 0; e < 3; e++) o[e] /= i;
258
+ this.#p = !0;
259
+ try {
260
+ this.world.setBlocks(a);
261
+ } finally {
262
+ this.#p = !1;
263
+ }
264
+ let s = m(n), c = {
265
+ id: this.#f++,
266
+ cells: n,
267
+ states: r,
268
+ center: o,
269
+ boxes: s,
270
+ body: null,
271
+ meshes: [],
272
+ age: 0
273
+ };
274
+ if (this.#e && (c.meshes = this.#C(c)), this.physics) {
275
+ let e = [];
276
+ for (let t = 0; t < s.length; t += 6) e.push({
277
+ shape: "box",
278
+ halfExtents: [
279
+ (s[t + 3] - s[t]) / 2,
280
+ (s[t + 4] - s[t + 1]) / 2,
281
+ (s[t + 5] - s[t + 2]) / 2
282
+ ],
283
+ offset: [
284
+ (s[t] + s[t + 3]) / 2 - o[0],
285
+ (s[t + 1] + s[t + 4]) / 2 - o[1],
286
+ (s[t + 2] + s[t + 5]) / 2 - o[2]
287
+ ]
288
+ });
289
+ c.body = this.physics.addBody(c.meshes[0] ?? null, {
290
+ position: o,
291
+ colliders: {
292
+ shape: "compound",
293
+ parts: e,
294
+ density: this.#t,
295
+ friction: this.#n
296
+ },
297
+ userData: c
298
+ });
299
+ }
300
+ for (this.#d.push(c); this.#d.length > this.maxPieces;) this.#w(this.#d[0]);
301
+ return c;
302
+ }
303
+ #C(e) {
304
+ let { cells: t, states: n, center: r } = e, i = n.length, o = [
305
+ Infinity,
306
+ Infinity,
307
+ Infinity
308
+ ], d = [
309
+ -Infinity,
310
+ -Infinity,
311
+ -Infinity
312
+ ];
313
+ for (let e = 0; e < i * 3; e++) o[e % 3] = Math.min(o[e % 3], t[e]), d[e % 3] = Math.max(d[e % 3], t[e]);
314
+ let f = d[0] - o[0] + 3, p = d[1] - o[1] + 3, m = (e, t, n) => e - o[0] + 1 + (t - o[1] + 1) * f + (n - o[2] + 1) * f * p, h = /* @__PURE__ */ new Set();
315
+ for (let e = 0; e < i; e++) h.add(m(t[e * 3], t[e * 3 + 1], t[e * 3 + 2]));
316
+ let g = /* @__PURE__ */ new Map();
317
+ for (let e = 0; e < i; e++) {
318
+ let i = t[e * 3], a = t[e * 3 + 1], o = t[e * 3 + 2], d = n[e] & 65535;
319
+ for (let e = 0; e < 6; e++) {
320
+ if (h.has(m(i + s[e], a + c[e], o + l[e]))) continue;
321
+ let t = g.get(d);
322
+ t || g.set(d, t = {
323
+ p: [],
324
+ q: [],
325
+ t: []
326
+ });
327
+ let n = t.p.length / 3, f = u[e];
328
+ for (let n = 0; n < 4; n++) t.p.push(i + f[n * 3] - r[0], a + f[n * 3 + 1] - r[1], o + f[n * 3 + 2] - r[2]), t.q.push(s[e], c[e], l[e]);
329
+ t.t.push(n, n + 1, n + 2, n, n + 2, n + 3);
330
+ }
331
+ }
332
+ let _ = this.#e, v = [];
333
+ for (let [e, t] of g) {
334
+ let n = this.#i.get(e);
335
+ n || this.#i.set(e, n = this.#r(this.world.blocks.get(e)));
336
+ let i = _.createMesh(v[0] ?? null).setGeometry(new a(t.p, t.q, t.t)).setMaterial(n);
337
+ v.length || i.position.set(r[0] - this.#_[0], r[1] - this.#_[1], r[2] - this.#_[2]), v.push(i);
338
+ }
339
+ return v;
340
+ }
341
+ #w(e) {
342
+ let t = this.#d.indexOf(e);
343
+ if (!(t < 0)) {
344
+ this.#d.splice(t, 1), e.body?.valid && this.physics && !this.physics.isDisposed && this.physics.removeBody(e.body);
345
+ for (let t = e.meshes.length - 1; t >= 0; t--) e.meshes[t].dispose();
346
+ }
347
+ }
348
+ };
349
+ function m(e) {
350
+ let t = e.length / 3, n = Array.from({ length: t }, (e, t) => t).sort((t, n) => e[t * 3 + 1] - e[n * 3 + 1] || e[t * 3 + 2] - e[n * 3 + 2] || e[t * 3] - e[n * 3]), r = [];
351
+ for (let t of n) {
352
+ let n = e[t * 3], i = e[t * 3 + 1], a = e[t * 3 + 2], o = r[r.length - 1];
353
+ o && o[2] === i && o[3] === a && o[1] === n ? o[1] = n + 1 : r.push([
354
+ n,
355
+ n + 1,
356
+ i,
357
+ a
358
+ ]);
359
+ }
360
+ let i = [], a = /* @__PURE__ */ new Map();
361
+ for (let [e, t, n, o] of r) {
362
+ let r = `${e},${t},${n},${o}`, s = a.get(r);
363
+ a.delete(r), s ? s[4] = o + 1 : i.push(s = [
364
+ e,
365
+ t,
366
+ n,
367
+ o,
368
+ o + 1
369
+ ]), a.set(`${e},${t},${n},${o + 1}`, s);
370
+ }
371
+ let o = [], s = /* @__PURE__ */ new Map();
372
+ for (let [e, t, n, r, a] of i) {
373
+ let i = `${e},${t},${r},${a},${n}`, c = s.get(i);
374
+ s.delete(i), c ? c[4] = n + 1 : o.push(c = [
375
+ e,
376
+ n,
377
+ r,
378
+ t,
379
+ n + 1,
380
+ a
381
+ ]), s.set(`${e},${t},${r},${a},${n + 1}`, c);
382
+ }
383
+ let c = new Int32Array(o.length * 6);
384
+ return o.forEach((e, t) => c.set(e, t * 6)), c;
385
+ }
386
+ //#endregion
387
+ export { p as VoxelDebris, m as mergeBoxes };
@@ -0,0 +1,146 @@
1
+ //#region src/faces.ts
2
+ var e = [
3
+ [
4
+ 0,
5
+ 0,
6
+ -1,
7
+ 0,
8
+ 0,
9
+ 0,
10
+ -1,
11
+ 0
12
+ ],
13
+ [
14
+ 0,
15
+ 0,
16
+ 1,
17
+ 0,
18
+ 0,
19
+ 0,
20
+ -1,
21
+ 0
22
+ ],
23
+ [
24
+ 1,
25
+ 0,
26
+ 0,
27
+ 0,
28
+ 0,
29
+ 0,
30
+ 0,
31
+ 1
32
+ ],
33
+ [
34
+ 1,
35
+ 0,
36
+ 0,
37
+ 0,
38
+ 0,
39
+ 0,
40
+ 0,
41
+ -1
42
+ ],
43
+ [
44
+ 1,
45
+ 0,
46
+ 0,
47
+ 0,
48
+ 0,
49
+ 0,
50
+ -1,
51
+ 0
52
+ ],
53
+ [
54
+ -1,
55
+ 0,
56
+ 0,
57
+ 0,
58
+ 0,
59
+ 0,
60
+ -1,
61
+ 0
62
+ ],
63
+ [
64
+ 1,
65
+ 0,
66
+ 0,
67
+ -1,
68
+ 0,
69
+ 0,
70
+ -1,
71
+ 0
72
+ ],
73
+ [
74
+ -1,
75
+ 0,
76
+ 0,
77
+ 1,
78
+ 1,
79
+ 0,
80
+ -1,
81
+ 0
82
+ ]
83
+ ];
84
+ function t(t, n, r = 0) {
85
+ let i = e[t];
86
+ return [i[0] * n[0] + i[1] * n[1] + i[2] * n[2] + i[3] * r + i[4], i[5] * n[0] + i[6] * n[1] + i[7] * n[2]];
87
+ }
88
+ var n = (e) => e.filter(([e]) => e).sort((e, t) => +(e[0] < 0) - (t[0] < 0)).map(([e, t], n) => `${e < 0 ? "-" : n ? "+" : ""}${Math.abs(e) === 1 && t ? t : `${Math.abs(e)}.0${t && `*${t}`}`}`).join("") || "0.0", r = (e, t, n) => [
89
+ "x",
90
+ "y",
91
+ "z"
92
+ ].map((r, i) => [e[t + i], `${n}.${r}`]), i = `fn voxelTile(face:u32,l:vec3f,cell:vec3f)->vec2f {
93
+ switch face {
94
+ ${e.map((e, t) => ` ${t < 7 ? `case ${t}u` : "default"} {return vec2f(${n([
95
+ ...r(e, 0, "l"),
96
+ [e[3], "cell.x"],
97
+ [e[4], ""]
98
+ ])},${n(r(e, 5, "l"))});}`).join("\n")}
99
+ }
100
+ }
101
+ `, a = Math.SQRT1_2, o = `fn voxelTangent(face:u32)->vec3f {var t=array<vec3f,8>(${[
102
+ [
103
+ 0,
104
+ 0,
105
+ -1
106
+ ],
107
+ [
108
+ 0,
109
+ 0,
110
+ 1
111
+ ],
112
+ [
113
+ 1,
114
+ 0,
115
+ 0
116
+ ],
117
+ [
118
+ 1,
119
+ 0,
120
+ 0
121
+ ],
122
+ [
123
+ 1,
124
+ 0,
125
+ 0
126
+ ],
127
+ [
128
+ -1,
129
+ 0,
130
+ 0
131
+ ],
132
+ [
133
+ a,
134
+ 0,
135
+ a
136
+ ],
137
+ [
138
+ -a,
139
+ 0,
140
+ a
141
+ ]
142
+ ].map((e) => `vec3f(${e.join(",")})`).join(",")});return t[face&7u];}
143
+ fn voxelNormal(n:vec3f,t:vec3f,m:vec3f)->vec3f {let u=normalize(t-n*dot(n,t));return normalize(u*m.x+cross(n,u)*m.y+n*m.z);}
144
+ `;
145
+ //#endregion
146
+ export { t as n, i as r, o as t };
@@ -0,0 +1,7 @@
1
+ /** Tile coordinate (u, v) of `point` on face `face` (cross faces: in cell `cellX`), as the shaders' `voxelTile`. */
2
+ export declare function tile(face: number, point: readonly number[], cellX?: number): [number, number];
3
+ /** WGSL `voxelTile(face, l, cell)`: the tile coordinates of chunk-local point l on a face of cell `cell`, from TILE. */
4
+ export declare const tileShader: string;
5
+ /** dP/du per face (unit length): the tangent of the normal-map frame. */
6
+ export declare const TANGENTS: readonly (readonly [number, number, number])[];
7
+ export declare const normalShader: string;