@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,486 @@
1
+ import { n as e } from "./octants-C_Ic7gdn.js";
2
+ import { i as t } from "./chunk-BCoaKSn4.js";
3
+ import { n, t as r } from "./world-CelTlft2.js";
4
+ import { t as i } from "./model-mesh-C4Gi6bMM.js";
5
+ import { MargoError as a } from "@margoengine/math/errors";
6
+ //#region src/physics.ts
7
+ var o = [
8
+ 0,
9
+ 0,
10
+ 0,
11
+ 32,
12
+ 32,
13
+ 32
14
+ ], s = [
15
+ 1,
16
+ -1,
17
+ 0,
18
+ 0,
19
+ 0,
20
+ 0
21
+ ], c = [
22
+ 0,
23
+ 0,
24
+ 1,
25
+ -1,
26
+ 0,
27
+ 0
28
+ ], l = [
29
+ 0,
30
+ 0,
31
+ 0,
32
+ 0,
33
+ 1,
34
+ -1
35
+ ], u = [
36
+ 1,
37
+ 1024,
38
+ 32
39
+ ], d = 67108863, f = 0, p = 1, m = 2, h = 2048, g = 1, _ = Array.from({ length: 16 }, (e, t) => {
40
+ let n = [], r = (e, t, r) => {
41
+ e && n.push(e === 3 ? [
42
+ 0,
43
+ t,
44
+ 2,
45
+ r
46
+ ] : [
47
+ e >> 1,
48
+ t,
49
+ (e >> 1) + 1,
50
+ r
51
+ ]);
52
+ }, i = t & 3, a = t >> 2;
53
+ return i === a ? r(i, 0, 2) : a === 3 ? (r(3, 1, 2), r(i, 0, 1)) : (r(i, 0, 1), r(a, 1, 2)), n;
54
+ }), v = (e) => e.shape === "sphere", y = (e, t) => n(e.cx + s[t], e.cy + c[t], e.cz + l[t]), b = (e, t) => {
55
+ if (e.size !== t.size) return !1;
56
+ for (let [n, r] of e) if (t.get(n) !== r) return !1;
57
+ return !0;
58
+ }, x = (e) => {
59
+ if (typeof e != "number") throw TypeError("VoxelPhysics budget must be a number of milliseconds per step such as 2");
60
+ if (!(e >= 0)) throw RangeError(`VoxelPhysics budget ${e} is not >= 0; pass milliseconds such as 2`);
61
+ return e;
62
+ }, S = class {
63
+ constructor(e, t, n = {}) {
64
+ if (this.entries = /* @__PURE__ */ new Map(), this.work = /* @__PURE__ */ new Set(), this.owners = /* @__PURE__ */ new Map(), this.retired = [], this.centres = /* @__PURE__ */ new Map(), this.near = /* @__PURE__ */ new Map(), this.needed = [], this.cost = [
65
+ .5,
66
+ .3,
67
+ .5,
68
+ .3
69
+ ], this.spheres = [], this.cells = /* @__PURE__ */ new Int32Array(98304), this.mask = /* @__PURE__ */ new Uint8Array(32768), this.faces = /* @__PURE__ */ new Uint8Array(32768), this.plane = /* @__PURE__ */ new Uint8Array(1024), this.vertices = new Float32Array(h * 6), this.indices = new Uint32Array(h * 3), this.solid = /* @__PURE__ */ new Uint8Array(), this.part = /* @__PURE__ */ new Uint8Array(), this.disposed = !1, this.edit = (e, t, n, r, i) => {
70
+ if (this.physics.isDisposed) return this.dispose();
71
+ let a = this.solidIds(), o = this.part, u = a[r & 65535];
72
+ (o[r & 65535] || o[i & 65535]) && this.reshape(e, t, n);
73
+ for (let r = 0; r < 6; r++) this.octantsAt(e + s[r], t + c[r], n + l[r]) % 255 && this.reshape(e + s[r], t + c[r], n + l[r]);
74
+ if (u !== a[i & 65535]) {
75
+ this.set(e, t, n, !!u), this.touch(e, t, n);
76
+ for (let r = 0; r < 6; r++) {
77
+ let i = e + s[r], o = t + c[r], d = n + l[r];
78
+ (i | 0) === i && (d | 0) === d && a[this.world.getBlock(i, o, d) & 65535] && (u || this.set(i, o, d, !0), this.touch(i, o, d));
79
+ }
80
+ }
81
+ }, !e || typeof e.on != "function" || typeof e.addBody != "function") throw TypeError("VoxelPhysics needs a Physics: pass Physics.attach(engine) from @margoengine/physics");
82
+ if (!(t instanceof r)) throw TypeError("VoxelPhysics needs a VoxelWorld: pass new VoxelWorld({blocks})");
83
+ let { radius: i = 1, budget: o = 2 } = n ?? {};
84
+ if (typeof i != "number") throw TypeError("VoxelPhysics radius must be a number of chunks such as 1");
85
+ if (!Number.isInteger(i) || i < 1) throw RangeError(`VoxelPhysics radius ${i} is not an integer >= 1; pass 1 or more chunks`);
86
+ if (this.limit = x(o), e.isDisposed) throw new a("disposed", "Physics is disposed; pass a live Physics to VoxelPhysics");
87
+ if (!e.supportsShape("voxels")) throw new a("unsupported", "This physics solver has no voxel colliders; upgrade it for VoxelPhysics");
88
+ this.physics = e, this.world = t, this.radius = i, this.body = e.addBody(null, {
89
+ type: "fixed",
90
+ colliders: [],
91
+ userData: this
92
+ }), this.offStep = e.on("beforestep", () => this.update()), this.offChange = t.onChange(this.edit);
93
+ }
94
+ get budget() {
95
+ return this.limit;
96
+ }
97
+ set budget(e) {
98
+ this.limit = x(e);
99
+ }
100
+ get pending() {
101
+ return this.work.size;
102
+ }
103
+ get colliders() {
104
+ let e = /* @__PURE__ */ new Map();
105
+ for (let [t, n] of this.entries) {
106
+ let r = [];
107
+ if (n.mode !== m) n.collider && r.push(n.collider);
108
+ else for (let e of n.pieces) e.collider && r.push(e.collider);
109
+ n.parts && r.push(n.parts), r.length && e.set(t, r);
110
+ }
111
+ return e;
112
+ }
113
+ chunkOf(e) {
114
+ return this.owners.get(e?.handle)?.key;
115
+ }
116
+ dispose() {
117
+ this.disposed || (this.disposed = !0, this.offStep(), this.offChange(), this.physics.isDisposed || this.body.remove(), this.entries.clear(), this.work.clear(), this.owners.clear(), this.retired.length = 0);
118
+ }
119
+ [Symbol.dispose ?? Symbol()]() {
120
+ this.dispose();
121
+ }
122
+ update() {
123
+ if (!this.body.valid) return this.dispose();
124
+ for (let e of this.retired) this.owners.delete(e.handle), this.body.removeCollider(e);
125
+ this.retired.length = 0;
126
+ let e = this.radius, t = /* @__PURE__ */ new Map();
127
+ for (let r of this.physics.getBodies()) {
128
+ if (r.type === "fixed") continue;
129
+ let [i, a, o] = r.position, s = Math.floor(i / 32), c = Math.floor(a / 32), l = Math.floor(o / 32);
130
+ if (!(Math.abs(s) <= d - e - 1 && Math.abs(c) <= d - e - 1 && Math.abs(l) <= d - e - 1)) continue;
131
+ let u = n(s, c, l), f = t.get(u) ?? 0;
132
+ !f && r.colliders.some(v) && (f = 1), t.set(u, f);
133
+ }
134
+ b(t, this.centres) || this.survey(t);
135
+ let r = this.world;
136
+ for (let t of this.entries.values()) {
137
+ let n = this.near.get(t.key);
138
+ if (!n || r.getChunk(t.cx, t.cy, t.cz) !== t.chunk) {
139
+ this.remove(t);
140
+ continue;
141
+ }
142
+ let i = n.b <= e || t.mode !== f && n.b <= e + 1;
143
+ i !== (t.mode !== f) && this.setMode(t, i);
144
+ }
145
+ for (let t of this.needed) {
146
+ if (this.entries.has(t.key)) continue;
147
+ let { cx: n, cy: i, cz: a, key: o } = t, s = r.getChunk(n, i, a);
148
+ if (!s) continue;
149
+ let c = {
150
+ cx: n,
151
+ cy: i,
152
+ cz: a,
153
+ key: o,
154
+ chunk: s,
155
+ collider: null,
156
+ rebuild: !0,
157
+ mode: f,
158
+ pieces: [],
159
+ faces: null,
160
+ scanned: !1,
161
+ stale: 0,
162
+ parts: null,
163
+ partsDirty: !1
164
+ };
165
+ this.entries.set(o, c), this.work.add(c), t.b <= e && this.setMode(c, !0);
166
+ }
167
+ for (let t of this.entries.values()) for (let n = 0; t.stale && !t.rebuild && n < 6; n++) t.stale & 1 << n && (this.near.get(y(t, n))?.d ?? e + 1) <= e && (t.rebuild = !0, this.work.add(t));
168
+ this.work.size && this.build();
169
+ }
170
+ survey(e) {
171
+ let t = this.radius + 1, r = /* @__PURE__ */ new Map();
172
+ for (let [i, a] of e) {
173
+ let [e, o, s] = i.split(",").map(Number);
174
+ for (let i = e - t; i <= e + t; i++) for (let c = o - t; c <= o + t; c++) for (let l = s - t; l <= s + t; l++) {
175
+ let t = n(i, c, l), u = Math.max(Math.abs(i - e), Math.abs(c - o), Math.abs(l - s)), d = r.get(t);
176
+ d ? d.d = Math.min(d.d, u) : r.set(t, d = {
177
+ cx: i,
178
+ cy: c,
179
+ cz: l,
180
+ key: t,
181
+ d: u,
182
+ b: Infinity
183
+ }), a && (d.b = Math.min(d.b, u));
184
+ }
185
+ }
186
+ this.centres = e, this.near = r, this.needed = Array.from(r.values()).filter((e) => e.d <= this.radius);
187
+ }
188
+ build() {
189
+ let e = performance.now();
190
+ this.reach();
191
+ let t = !1;
192
+ for (let e of this.work) {
193
+ if (this.reaches(e, o, !1) && (e.rebuild && (this.unit(0, e), t = !0), e.partsDirty && (this.unit(3, e), t = !0), e.mode !== m && this.reaches(e, o, !0) && (e.mode === f && this.setMode(e, !0), this.activate(e)), e.mode === m)) for (let n = 0; n < e.pieces.length; n++) e.pieces[n].dirty && this.reaches(e, e.pieces[n].box, !1) && (e.scanned || this.unit(1, e), this.unit(2, e, n) && n--, t = !0);
194
+ this.settle(e);
195
+ }
196
+ if (!this.work.size || t && performance.now() - e >= this.limit) return;
197
+ let n = Array.from(this.work).sort((e, t) => this.distance(e) - this.distance(t)), r = t;
198
+ for (let t of n) {
199
+ if (t.partsDirty && !t.rebuild) {
200
+ if (r && performance.now() - e + this.cost[3] > this.limit) return;
201
+ this.unit(3, t), r = !0;
202
+ }
203
+ for (;;) {
204
+ let n = t.rebuild ? -1 : t.pieces.findIndex((e) => e.dirty);
205
+ if (!t.rebuild && n < 0) break;
206
+ let i = t.rebuild ? 0 : t.scanned ? 2 : 1;
207
+ if (r && performance.now() - e + this.cost[i] > this.limit) return;
208
+ this.unit(i, t, n), r = !0;
209
+ }
210
+ this.settle(t);
211
+ }
212
+ }
213
+ unit(e, t, n = 0) {
214
+ let r = performance.now(), i = !1;
215
+ return e === 0 ? (this.voxels(t), this.shapes(t)) : e === 3 ? this.shapes(t) : e === 1 ? (this.scan(t, t.faces ??= /* @__PURE__ */ new Uint8Array(32768)), t.scanned = !0) : i = this.piece(t, n), this.cost[e] = Math.max(performance.now() - r, this.cost[e] * .98), i;
216
+ }
217
+ settle(e) {
218
+ e.rebuild || e.partsDirty || e.pieces.some((e) => e.dirty) || (e.mode === p && this.activate(e), e.faces = null, this.work.delete(e));
219
+ }
220
+ distance(e) {
221
+ return this.near.get(e.key)?.d ?? Infinity;
222
+ }
223
+ reaches(e, t, n) {
224
+ let r = this.spheres, i = e.cx * 32 + t[0], a = e.cy * 32 + t[1], o = e.cz * 32 + t[2], s = e.cx * 32 + t[3], c = e.cy * 32 + t[4], l = e.cz * 32 + t[5];
225
+ for (let e = 0; e < r.length; e += 5) {
226
+ if (n && !r[e + 4]) continue;
227
+ let t = Math.max(i - r[e], 0, r[e] - s), u = Math.max(a - r[e + 1], 0, r[e + 1] - c), d = Math.max(o - r[e + 2], 0, r[e + 2] - l);
228
+ if (t * t + u * u + d * d <= r[e + 3] * r[e + 3]) return !0;
229
+ }
230
+ return !1;
231
+ }
232
+ reach() {
233
+ let e = this.spheres, t = 2 * this.physics.fixedTimestep;
234
+ e.length = 0;
235
+ for (let n of this.physics.getBodies()) {
236
+ if (n.type === "fixed") continue;
237
+ let [r, i, a] = n.position, [o, s, c] = n.linearVelocity, l = 0, u = 0;
238
+ for (let e of n.colliders) {
239
+ v(e) && (u = 1);
240
+ let t = e.boundingRadius ?? 32, n = e.translationWrtParent();
241
+ l = Math.max(l, t + (n ? Math.hypot(n.x, n.y, n.z) : 0));
242
+ }
243
+ e.push(r, i, a, l + t * Math.hypot(o, s, c) + g, u);
244
+ }
245
+ }
246
+ setMode(e, t) {
247
+ if (t) e.mode = p, e.pieces = [{
248
+ box: o,
249
+ collider: null,
250
+ dirty: !0
251
+ }], e.scanned = !1, this.work.add(e);
252
+ else {
253
+ for (let t of e.pieces) t.collider && this.retire(t.collider);
254
+ e.pieces = [], e.faces = null, e.collider?.setEnabled(!0), e.mode = f, e.rebuild || this.work.delete(e);
255
+ }
256
+ }
257
+ activate(e) {
258
+ e.mode = m, e.collider?.setEnabled(!1);
259
+ for (let t of e.pieces) t.collider?.setEnabled(!0);
260
+ }
261
+ attach(e, t, n) {
262
+ let r = this.body.addCollider({
263
+ ...e,
264
+ massless: !0,
265
+ enabled: n
266
+ });
267
+ return this.owners.set(r.handle, t), r;
268
+ }
269
+ retire(e) {
270
+ e.setEnabled(!1), this.retired.push(e);
271
+ }
272
+ remove(e) {
273
+ if (e.collider) {
274
+ this.retire(e.collider);
275
+ for (let t = 0; t < 6; t++) {
276
+ let n = this.entries.get(y(e, t));
277
+ n && (n.stale |= 1 << (t ^ 1));
278
+ }
279
+ }
280
+ for (let t of e.pieces) t.collider && this.retire(t.collider);
281
+ e.parts && this.retire(e.parts), this.entries.delete(e.key), this.work.delete(e);
282
+ }
283
+ solidIds() {
284
+ let e = this.world.blocks.table;
285
+ return this.solid.length !== e.length && (this.part = Uint8Array.from(e, (e) => e.solid && e.model ? 3 : e.solid && e.shape === "slab" ? 1 : e.solid && e.shape === "stair" ? 2 : 0), this.solid = Uint8Array.from(e, (e, t) => +(e.solid && !this.part[t]))), this.solid;
286
+ }
287
+ partIds() {
288
+ return this.solidIds(), this.part;
289
+ }
290
+ octantsAt(t, n, r) {
291
+ if ((t | 0) !== t || (r | 0) !== r) return 0;
292
+ let i = this.world.getBlock(t, n, r), a = this.partIds()[i & 65535];
293
+ return a === 3 ? 0 : a ? e(a === 1 ? "slab" : "stair", i >>> 16) : this.solid[i & 65535] ? 255 : 0;
294
+ }
295
+ shapes(n) {
296
+ n.partsDirty = !1;
297
+ let r = this.partIds(), { chunk: a } = n, o = [], u = [
298
+ 0,
299
+ 0,
300
+ 0
301
+ ], d = n.cx * 32, f = n.cy * 32, p = n.cz * 32, m = !1;
302
+ for (let e of t(a)) r[e & 65535] && (m = !0);
303
+ if (m) for (let t = 0; t < 32768; t++) {
304
+ let n = t & 31, i = t >> 10, m = t >> 5 & 31, h = a.get(n, i, m), g = r[h & 65535];
305
+ if (!g || g === 3) continue;
306
+ let v = e(g === 1 ? "slab" : "stair", h >>> 16), y = [
307
+ d + n,
308
+ f + i,
309
+ p + m
310
+ ];
311
+ for (let e = 0; e < 6; e++) {
312
+ let t = e >> 1, n = (t + 1) % 3, r = (t + 2) % 3, i = 1 << t, a = 1 << n, d = 1 << r, f = !(e & 1), p = f ? i : 0, m = f ? 0 : i, h = this.octantsAt(y[0] + s[e], y[1] + c[e], y[2] + l[e]), g = 0, b = 0;
313
+ for (let e = 0; e < 4; e++) {
314
+ let t = (e & 1 ? a : 0) | (e & 2 ? d : 0);
315
+ v >> (t | p) & 1 ? h >> (t | m) & 1 || (g |= 1 << e) : v >> (t | m) & 1 && (b |= 1 << e);
316
+ }
317
+ for (let e = 0; e < 2; e++) {
318
+ let i = e ? b : g;
319
+ for (let a of _[i]) {
320
+ u[t] = y[t] + (e ? .5 : +!!f);
321
+ for (let e = 0; e < 4; e++) {
322
+ let t = e === 1 || e === 2, i = e >= 2;
323
+ u[n] = y[n] + ((f ? t : i) ? a[2] : a[0]) / 2, u[r] = y[r] + ((f ? i : t) ? a[3] : a[1]) / 2, o.push(u[0], u[1], u[2]);
324
+ }
325
+ }
326
+ }
327
+ }
328
+ }
329
+ n.parts && this.retire(n.parts), n.parts = null;
330
+ let h = i(this.world, n.cx, n.cy, n.cz, !0);
331
+ if (!o.length && !h.length) return;
332
+ let g = o.length / 12, v = [], y = new Uint32Array(g * 6 + h.reduce((e, t) => e + t.indices.length, 0));
333
+ for (let e = 0; e < g; e++) y.set([
334
+ e * 4,
335
+ e * 4 + 1,
336
+ e * 4 + 2,
337
+ e * 4,
338
+ e * 4 + 2,
339
+ e * 4 + 3
340
+ ], e * 6);
341
+ for (let e of h) {
342
+ let t = o.length / 3;
343
+ for (let n of e.indices) v.push(n + t);
344
+ for (let t of e.positions) o.push(t);
345
+ }
346
+ y.set(v, g * 6), n.parts = this.attach({
347
+ shape: "trimesh",
348
+ vertices: o,
349
+ indices: y,
350
+ fixInternalEdges: !0
351
+ }, n, !0);
352
+ }
353
+ reshape(e, t, r) {
354
+ let i = this.entries.get(n(e >> 5, t >> 5, r >> 5));
355
+ i && (i.partsDirty = !0, this.work.add(i));
356
+ }
357
+ scan(e, t) {
358
+ let { palette: n, bits: r, words: i } = e.chunk.pack(), a = this.solidIds(), o = this.mask;
359
+ if (t.fill(0), r) {
360
+ let e = (1 << r) - 1;
361
+ for (let t = 0; t < 32768; t++) o[t] = a[n[i[t * r >>> 5] >>> (t * r & 31) & e] & 65535];
362
+ } else if (a[n[0] & 65535]) o.fill(1);
363
+ else return 0;
364
+ let [u, d, f, p, m, h] = s.map((t, n) => this.layer(e.cx + s[n], e.cy + c[n], e.cz + l[n], n)), g = 0;
365
+ for (let e = 0; e < 32; e++) for (let n = 0; n < 32; n++) for (let r = 0; r < 32; r++) {
366
+ let i = r | n << 5 | e << 10;
367
+ if (!o[i]) continue;
368
+ let a = +!(r < 31 ? o[i + 1] : u[n | e << 5]) | !(r ? o[i - 1] : d[n | e << 5]) << 1 | !(e < 31 ? o[i + 1024] : f[r | n << 5]) << 2 | !(e ? o[i - 1024] : p[r | n << 5]) << 3 | !(n < 31 ? o[i + 32] : m[r | e << 5]) << 4 | !(n ? o[i - 32] : h[r | e << 5]) << 5;
369
+ a && (t[i] = a, g++);
370
+ }
371
+ return g;
372
+ }
373
+ voxels(e) {
374
+ let t = e.mode !== f, n = t ? e.faces ??= /* @__PURE__ */ new Uint8Array(32768) : this.faces, r = this.cells, i = 0;
375
+ if (this.scan(e, n)) for (let t = 0; t < 32768; t++) n[t] && (r[i++] = e.cx * 32 + (t & 31), r[i++] = e.cy * 32 + (t >> 10), r[i++] = e.cz * 32 + (t >> 5 & 31));
376
+ t && (e.scanned = !0);
377
+ let a = this.attach({
378
+ shape: "voxels",
379
+ cells: r.subarray(0, i)
380
+ }, e, e.mode !== m);
381
+ e.collider && this.retire(e.collider), e.collider = a, e.rebuild = !1;
382
+ for (let t = 0; e.stale && t < 6; t++) if (e.stale & 1 << t) for (let n of e.pieces) (t & 1 ? n.box[t >> 1] === 0 : n.box[(t >> 1) + 3] === 32) && (n.dirty = !0);
383
+ e.stale = 0;
384
+ for (let t = 0; t < 6; t++) {
385
+ let n = this.entries.get(y(e, t))?.collider;
386
+ n && a.combineVoxelStates(n);
387
+ }
388
+ }
389
+ piece(e, t) {
390
+ let n = e.pieces[t], r = this.triangles(e, n.box);
391
+ if (n.collider && r < 0 && this.retire(n.collider), r < 0) {
392
+ let r = n.box, i = [
393
+ r[3] - r[0],
394
+ r[4] - r[1],
395
+ r[5] - r[2]
396
+ ], a = i[1] >= i[0] && i[1] >= i[2] ? 1 : i[0] >= i[2] ? 0 : 2, o = r[a] + (i[a] >> 1), s = r.slice(), c = r.slice();
397
+ return s[a + 3] = o, c[a] = o, e.pieces[t] = {
398
+ box: s,
399
+ collider: null,
400
+ dirty: !0
401
+ }, e.pieces.push({
402
+ box: c,
403
+ collider: null,
404
+ dirty: !0
405
+ }), !0;
406
+ }
407
+ let i = r ? this.attach({
408
+ shape: "trimesh",
409
+ vertices: this.vertices.subarray(0, r * 6),
410
+ indices: this.indices.subarray(0, r * 3),
411
+ fixInternalEdges: !0
412
+ }, e, e.mode === m) : null;
413
+ return n.collider && this.retire(n.collider), n.collider = i, n.dirty = !1, !1;
414
+ }
415
+ triangles(e, t) {
416
+ let n = e.faces, r = this.plane, i = this.vertices, a = this.indices, o = [
417
+ e.cx * 32,
418
+ e.cy * 32,
419
+ e.cz * 32
420
+ ], s = [
421
+ 0,
422
+ 0,
423
+ 0
424
+ ], c = 0;
425
+ for (let e = 0; e < 6; e++) {
426
+ let l = e >> 1, d = (l + 1) % 3, f = (l + 2) % 3, p = 1 << e, m = !(e & 1), g = t[d], _ = t[d + 3], v = t[f], y = t[f + 3];
427
+ for (let e = t[l]; e < t[l + 3]; e++) {
428
+ for (let t = g; t < _; t++) for (let i = v; i < y; i++) r[t | i << 5] = n[e * u[l] + t * u[d] + i * u[f]] & p;
429
+ for (let t = v; t < y; t++) for (let n = g; n < _; n++) {
430
+ if (!r[n | t << 5]) continue;
431
+ if (c === h / 2) return -1;
432
+ let u = n + 1, p = t + 1;
433
+ for (; u < _ && r[u | t << 5];) u++;
434
+ grow: for (; p < y;) {
435
+ for (let e = n; e < u; e++) if (!r[e | p << 5]) break grow;
436
+ p++;
437
+ }
438
+ for (let e = t; e < p; e++) for (let t = n; t < u; t++) r[t | e << 5] = 0;
439
+ let g = c * 4;
440
+ s[l] = o[l] + e + +!!m;
441
+ for (let e = 0; e < 4; e++) {
442
+ let r = e === 1 || e === 2, a = e >= 2;
443
+ s[d] = o[d] + ((m ? r : a) ? u : n), s[f] = o[f] + ((m ? a : r) ? p : t), i[(g + e) * 3] = s[0], i[(g + e) * 3 + 1] = s[1], i[(g + e) * 3 + 2] = s[2];
444
+ }
445
+ let v = c * 6;
446
+ a[v] = a[v + 3] = g, a[v + 1] = g + 1, a[v + 2] = a[v + 4] = g + 2, a[v + 5] = g + 3, c++;
447
+ }
448
+ }
449
+ }
450
+ return c * 2;
451
+ }
452
+ layer(e, t, n, r) {
453
+ let i = /* @__PURE__ */ new Uint8Array(1024), a = Math.abs(e) <= d && Math.abs(t) <= d && Math.abs(n) <= d ? this.world.getChunk(e, t, n) : void 0;
454
+ if (!a) return i;
455
+ let o = this.solidIds(), s = r & 1 ? 31 : 0;
456
+ for (let e = 0; e < 32; e++) for (let t = 0; t < 32; t++) i[e | t << 5] = o[(r < 2 ? a.get(s, t, e) : r < 4 ? a.get(e, s, t) : a.get(e, t, s)) & 65535];
457
+ return i;
458
+ }
459
+ set(e, t, r, i) {
460
+ let a = e >> 5, o = t >> 5, s = r >> 5, c = this.entries.get(n(a, o, s));
461
+ if (!c?.collider) return;
462
+ c.collider.setVoxel(e, t, r, i);
463
+ let l = e & 31, u = t & 31, d = r & 31;
464
+ l === 31 && this.propagate(c, a + 1, o, s, e, t, r), l === 0 && this.propagate(c, a - 1, o, s, e, t, r), u === 31 && this.propagate(c, a, o + 1, s, e, t, r), u === 0 && this.propagate(c, a, o - 1, s, e, t, r), d === 31 && this.propagate(c, a, o, s + 1, e, t, r), d === 0 && this.propagate(c, a, o, s - 1, e, t, r);
465
+ }
466
+ propagate(e, t, r, i, a, o, s) {
467
+ let c = this.entries.get(n(t, r, i))?.collider;
468
+ c && e.collider.propagateVoxelChange(c, a, o, s);
469
+ }
470
+ touch(e, t, r) {
471
+ let i = this.entries.get(n(e >> 5, t >> 5, r >> 5));
472
+ if (!i?.pieces.length) return;
473
+ let a = e & 31, o = t & 31, s = r & 31;
474
+ i.scanned = !1;
475
+ for (let e of i.pieces) {
476
+ let t = e.box;
477
+ if (a >= t[0] && o >= t[1] && s >= t[2] && a < t[3] && o < t[4] && s < t[5]) {
478
+ e.dirty = !0;
479
+ break;
480
+ }
481
+ }
482
+ this.work.add(i);
483
+ }
484
+ };
485
+ //#endregion
486
+ export { S as VoxelPhysics };
@@ -0,0 +1,166 @@
1
+ import type { BoxLike, MutableVec3, Vec3 } from '@margoengine/math';
2
+ import type { BlockType } from './blocks.js';
3
+ import type { VoxelWorld } from './world.js';
4
+ /** A hit from {@link raycastVoxels}. */
5
+ export interface VoxelHit {
6
+ /** The cell hit. */
7
+ cell: MutableVec3;
8
+ /**
9
+ * Outward normal of the face the ray entered through: one unit axis (at an exact edge or corner, the first crossed
10
+ * axis in x, y, z order), or [0, 0, 0] when the ray starts inside the cell. For a slab or stair it is the face of its
11
+ * half-block boxes the ray meets (a slab's top, a stair's step or riser).
12
+ */
13
+ normal: MutableVec3;
14
+ /** `cell + normal`: the cell in front of that face, where a placed block goes (`cell` itself for a zero normal). */
15
+ adjacent: MutableVec3;
16
+ /**
17
+ * Where the ray enters the cell, exactly on its face (that coordinate is an integer; a multiple of 0.5 on a slab or
18
+ * stair, where it enters the block's boxes), or the origin.
19
+ */
20
+ point: MutableVec3;
21
+ /** From the origin to `point`, in blocks. */
22
+ distance: number;
23
+ /** Block state of the cell. */
24
+ state: number;
25
+ }
26
+ /** Result of {@link sweepBox}. */
27
+ export interface VoxelSweep {
28
+ /** Where the box's min corner ends up: `box.min` plus the motion the solid blocks allowed. */
29
+ position: MutableVec3;
30
+ /** Per axis (x, y, z): a solid block stopped the move along it, or touched the box at the end of it. */
31
+ hit: [x: boolean, y: boolean, z: boolean];
32
+ /** The box stands on a solid block: its downward move was stopped, or with motion y 0, it rests on one. */
33
+ onGround: boolean;
34
+ }
35
+ /** Options of {@link VoxelCharacter}. Distances in blocks, times in seconds. */
36
+ export interface VoxelCharacterOptions {
37
+ /** Body box `[width (x), height (y), depth (z)]`. Default [0.6, 1.8, 0.6]. */
38
+ size?: Vec3;
39
+ /** Tallest ledge walked onto without jumping. Default 0.6. */
40
+ stepHeight?: number;
41
+ /** Downward acceleration, blocks/s². Default 32 (the default 9 blocks/s jump rises 1.27 blocks). */
42
+ gravity?: number;
43
+ /** Speeds in blocks/s. Defaults: walk 4.317, sprint 5.612, swim 2, fly 11, jump 9 (the take-off speed). */
44
+ speeds?: {
45
+ walk?: number;
46
+ sprint?: number;
47
+ swim?: number;
48
+ fly?: number;
49
+ jump?: number;
50
+ };
51
+ }
52
+ /** What the player asks a {@link VoxelCharacter} to do during one {@link VoxelCharacter.update}. Every field is optional. */
53
+ export interface VoxelMoveIntent {
54
+ /**
55
+ * World-space wish direction; its length (clamped to 1) scales the speed, so a half-tilted stick walks at half speed.
56
+ * Walking uses x and z only (flatten the camera's forward first); swimming and flying use all three.
57
+ */
58
+ direction?: Vec3;
59
+ /** Jump off the ground; in liquid swim up (against a wall: climb out); flying, rise. */
60
+ jump?: boolean;
61
+ /** Swim or fly down. Ignored when walking. */
62
+ descend?: boolean;
63
+ /** Walk at `speeds.sprint` instead of `speeds.walk`. */
64
+ sprint?: boolean;
65
+ }
66
+ /**
67
+ * Casts a ray from `origin` along `dir` (any non-zero length) up to `max` blocks and returns the first cell it passes
68
+ * through whose block `filter` accepts, or null. Traverses cell by cell (J. Amanatides and A. Woo, "A Fast Voxel
69
+ * Traversal Algorithm for Ray Tracing", Eurographics 1987), stepping every tied axis at once, so a ray through an
70
+ * exact edge or corner skips the cells it only touches there, and a ray in a face plane runs in the cells on its
71
+ * positive side (a cell is the half-open box [x, x + 1) x [y, y + 1) x [z, z + 1)). A ray that starts inside an
72
+ * accepted cell hits it at distance 0; one that starts on its face and points in enters through that face.
73
+ * By default `filter` accepts every block that is drawn and is not a liquid (`layer !== 'invisible' && !liquid`):
74
+ * solid or not, so cross-shaped blocks are hit as their whole cell box; pass `(state, type) => type.liquid` to pick
75
+ * water. Air (state 0) and unknown ids are never hit. Cost: one {@link VoxelWorld.getBlock} per cell crossed.
76
+ * @throws TypeError when `world` is not a VoxelWorld, `origin` or `dir` is not three numbers, `max` is not a
77
+ * number or `filter` is not a function.
78
+ * @throws RangeError for a non-finite value, a zero `dir`, a negative `max`, or an origin outside the 32-bit cells.
79
+ */
80
+ export declare function raycastVoxels(world: VoxelWorld, origin: Vec3, dir: Vec3, max: number, filter?: (state: number, type: BlockType) => boolean): VoxelHit | null;
81
+ /**
82
+ * Moves `box` by `motion` through `world`'s solid blocks (`BlockType.solid`: each its whole cell, a slab or stair its
83
+ * half-block boxes, as `shapeOctants` gives them) and reports where it
84
+ * stops. Axis-separated: y first, then x, then z, each clipped at the first solid cell ahead after scanning every
85
+ * cell layer the move crosses, so nothing tunnels at any speed (cost grows with the distance swept). Touching, or
86
+ * overlapping by up to 1e-6 blocks, is not a collision; blocks the box already overlaps are ignored, so it can move
87
+ * out of a block placed inside it.
88
+ * @throws TypeError when `world` is not a VoxelWorld, `box` is not `{min: {x, y, z}, max: {x, y, z}}` (a Box3 works)
89
+ * or `motion` is not three numbers.
90
+ * @throws RangeError for a non-finite value or an empty box (a min above its max).
91
+ */
92
+ export declare function sweepBox(world: VoxelWorld, box: BoxLike, motion: Vec3): VoxelSweep;
93
+ /**
94
+ * A player body in a voxel world: an axis-aligned box that walks with acceleration and friction, falls under
95
+ * gravity, jumps, steps onto ledges up to `stepHeight` (only when the space above is clear, and only from the
96
+ * ground, on landing or in liquid), swims in liquid blocks (buoyant, slower, jump against a wall to climb out) and
97
+ * flies. {@link VoxelCharacter.update} runs fixed 1/120 s steps (G. Fiedler, "Fix Your Timestep!", 2004), so the
98
+ * path does not depend on the frame rate; {@link VoxelCharacter.renderPosition} interpolates between steps for
99
+ * smooth drawing. Collision is {@link sweepBox}, with step-up tried when a horizontal move is blocked.
100
+ *
101
+ * ```ts
102
+ * const player = new VoxelCharacter(world);
103
+ * player.position = [0, 80, 0];
104
+ * engine.onFrame((_, dt) => {
105
+ * player.update(dt, { direction: [forwardX, 0, forwardZ], jump: keys.has('Space') });
106
+ * const [x, y, z] = player.renderPosition; // the eye is the game's choice: 1.62 above the feet here
107
+ * engine.camera.position = [x, y + 1.62, z];
108
+ * });
109
+ * ```
110
+ */
111
+ export declare class VoxelCharacter {
112
+ /** The world it moves in. */
113
+ readonly world: VoxelWorld;
114
+ /** Feet: the bottom centre of the body box, after the last fixed step. Write it (or assign an array) to teleport. */
115
+ position: MutableVec3;
116
+ /** Velocity, blocks/s. */
117
+ readonly velocity: MutableVec3;
118
+ /**
119
+ * `position` interpolated between the last two fixed steps by the time left over: draw the body or the camera here
120
+ * for smooth motion at any frame rate. It trails `position` by less than one step.
121
+ */
122
+ readonly renderPosition: MutableVec3;
123
+ /** Body box `[width, height, depth]`; may change between updates (crouching). */
124
+ readonly size: MutableVec3;
125
+ /** Tallest ledge walked onto without jumping, blocks. */
126
+ stepHeight: number;
127
+ /** Downward acceleration, blocks/s². */
128
+ gravity: number;
129
+ /** Speeds, blocks/s; `jump` is the take-off speed. */
130
+ readonly speeds: {
131
+ walk: number;
132
+ sprint: number;
133
+ swim: number;
134
+ fly: number;
135
+ jump: number;
136
+ };
137
+ /** Fly mode: no gravity; `jump` and `descend` move up and down. */
138
+ flying: boolean;
139
+ private ground;
140
+ private liquid;
141
+ private pushing;
142
+ private time;
143
+ private readonly previous;
144
+ private readonly last;
145
+ private readonly box;
146
+ private readonly start;
147
+ private readonly stepped;
148
+ private readonly hits;
149
+ /**
150
+ * @throws TypeError when `world` is not a VoxelWorld or an option has the wrong type.
151
+ * @throws RangeError for a non-finite or negative option, or a size that is not positive.
152
+ */
153
+ constructor(world: VoxelWorld, options?: VoxelCharacterOptions);
154
+ /** Standing on a solid block after the last step. */
155
+ get onGround(): boolean;
156
+ /** The body overlapped a liquid block in the last step (it swam). */
157
+ get inLiquid(): boolean;
158
+ /**
159
+ * Advances the body by `dt` seconds of `intent`: whole fixed steps of 1/120 s (the remainder carries to the next
160
+ * call; at most 0.25 s per call, the rest of a longer stall is dropped), then updates `renderPosition`.
161
+ * @throws TypeError when `dt` is not a number or `intent` is not an object of the {@link VoxelMoveIntent} fields.
162
+ * @throws RangeError for a negative or non-finite `dt`, or a non-finite direction or teleported position.
163
+ */
164
+ update(dt: number, intent?: VoxelMoveIntent): void;
165
+ private step;
166
+ }