@adatechnology/game-destruction-three 0.25.0 → 0.26.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.
@@ -0,0 +1,826 @@
1
+ "use strict";
2
+ (() => {
3
+ // ../game-terrain/dist/index.js
4
+ var cellCoordinate = (v, origin, count, cellSize) => Math.max(0, Math.min(count - 1e-6, (v - origin) / cellSize));
5
+ var blendHeights = (near, far, t) => near * (1 - t) + far * t;
6
+ var patchColumns = new Int32Array(0);
7
+ var patchBlendX = new Float64Array(0);
8
+ var patchNear = new Float64Array(0);
9
+ var patchFar = new Float64Array(0);
10
+ function heightPatchGrid(patch, xs, zs, out) {
11
+ const width = xs.length;
12
+ const depth = zs.length;
13
+ if (out.length < width * depth) throw new RangeError(`heightPatchGrid: out holds ${out.length} heights, the grid has ${width * depth}`);
14
+ const { cellSize, originX, originZ, columns, rows, column0, row0, heights } = patch;
15
+ const stride = patch.width;
16
+ if (patchColumns.length < width) {
17
+ patchColumns = new Int32Array(width);
18
+ patchBlendX = new Float64Array(width);
19
+ patchNear = new Float64Array(width);
20
+ patchFar = new Float64Array(width);
21
+ }
22
+ for (let i = 0; i < width; i++) {
23
+ const fx = cellCoordinate(xs[i], originX, columns, cellSize);
24
+ const column = (fx | 0) - column0;
25
+ if (column < 0 || column + 1 >= stride) throw new RangeError(`heightPatchGrid: x ${xs[i]} is past the patch`);
26
+ patchColumns[i] = column;
27
+ patchBlendX[i] = fx - (fx | 0);
28
+ }
29
+ let blended = -1;
30
+ for (let j = 0; j < depth; j++) {
31
+ const fz = cellCoordinate(zs[j], originZ, rows, cellSize);
32
+ const row = (fz | 0) - row0;
33
+ if (row < 0 || row + 1 >= patch.depth) throw new RangeError(`heightPatchGrid: z ${zs[j]} is past the patch`);
34
+ const tz = fz - (fz | 0);
35
+ if (row !== blended) {
36
+ blended = row;
37
+ for (let i = 0; i < width; i++) {
38
+ const index = row * stride + patchColumns[i];
39
+ const tx = patchBlendX[i];
40
+ patchNear[i] = blendHeights(heights[index], heights[index + 1], tx);
41
+ patchFar[i] = blendHeights(heights[index + stride], heights[index + stride + 1], tx);
42
+ }
43
+ }
44
+ for (let i = 0; i < width; i++) out[j * width + i] = blendHeights(patchNear[i], patchFar[i], tz);
45
+ }
46
+ }
47
+
48
+ // src/engine-ground-collision.ts
49
+ var GROUND_COLLISION_CELL = 0.05;
50
+ var COLLISION_TOLERANCE = 5e-3;
51
+ var COLLISION_ENTER = 45e-4;
52
+ var ENGINE_BLOCK = 4;
53
+ var coarseStep = (cells, readCell) => {
54
+ const k = Math.max(1, Math.round(GROUND_COLLISION_CELL / readCell));
55
+ return cells % k === 0 ? k : 1;
56
+ };
57
+ var collisionFieldOf = (heights, cells, readCell, hasSteps, wasCoarse = false) => {
58
+ const k = coarseStep(cells, readCell);
59
+ const fine = { step: 1, field: heights };
60
+ if (k === 1 || hasSteps || heights.length === 0) return fine;
61
+ const m = cells + 1;
62
+ const get = (p, q) => heights[q * m + p];
63
+ let off2 = 0;
64
+ for (let q = 0; q <= cells; q++) {
65
+ for (let p = 0; p <= cells; p++) {
66
+ collisionBoth(get, p, q, k);
67
+ const h = heights[q * m + p];
68
+ off2 = Math.max(off2, h - both[0], both[1] - h);
69
+ }
70
+ }
71
+ const field = collisionOf(heights, cells, k);
72
+ return off2 + quantumOf(field, cells / k) <= (wasCoarse ? COLLISION_TOLERANCE : COLLISION_ENTER) ? { step: k, field } : fine;
73
+ };
74
+ var quantumOf = (field, cells) => {
75
+ const m = cells + 1;
76
+ let most = 0;
77
+ for (let bj = 0; bj < cells; bj += ENGINE_BLOCK) {
78
+ for (let bi = 0; bi < cells; bi += ENGINE_BLOCK) {
79
+ let lo = Infinity, hi = -Infinity;
80
+ for (let j = bj; j <= Math.min(cells, bj + ENGINE_BLOCK); j++) {
81
+ for (let i = bi; i <= Math.min(cells, bi + ENGINE_BLOCK); i++) {
82
+ lo = Math.min(lo, field[j * m + i]);
83
+ hi = Math.max(hi, field[j * m + i]);
84
+ }
85
+ }
86
+ most = Math.max(most, (hi - lo) / 510);
87
+ }
88
+ }
89
+ return most;
90
+ };
91
+ var collisionOf = (heights, cells, k) => {
92
+ if (k === 1) return heights;
93
+ const m = cells + 1;
94
+ const n = cells / k + 1;
95
+ const out = new Float32Array(n * n);
96
+ for (let j = 0; j < n; j++) for (let i = 0; i < n; i++) out[j * n + i] = heights[j * k * m + i * k];
97
+ return out;
98
+ };
99
+ var collisionAt = (get, p, q, k) => {
100
+ collisionBoth(get, p, q, k);
101
+ return both[0];
102
+ };
103
+ var both = new Float64Array(2);
104
+ var collisionBoth = (get, p, q, k) => {
105
+ const p0 = Math.floor(p / k) * k, q0 = Math.floor(q / k) * k;
106
+ const u = (p - p0) / k, v = (q - q0) / k;
107
+ if (u === 0 && v === 0) {
108
+ both[0] = both[1] = get(p0, q0);
109
+ return;
110
+ }
111
+ const p1 = u === 0 ? p0 : p0 + k, q1 = v === 0 ? q0 : q0 + k;
112
+ const h00 = get(p0, q0), h10 = get(p1, q0), h01 = get(p0, q1), h11 = get(p1, q1);
113
+ const main = u >= v ? h00 + u * (h10 - h00) + v * (h11 - h10) : h00 + v * (h01 - h00) + u * (h11 - h01);
114
+ const cross = u + v <= 1 ? h00 + u * (h10 - h00) + v * (h01 - h00) : h11 + (1 - u) * (h01 - h11) + (1 - v) * (h10 - h11);
115
+ both[0] = Math.min(main, cross);
116
+ both[1] = Math.max(main, cross);
117
+ };
118
+ var underBoth = (heights, cells, k) => {
119
+ if (k === 1) return heights;
120
+ const m = cells + 1;
121
+ const get = (p, q) => heights[q * m + p];
122
+ const out = new Float32Array(m * m);
123
+ for (let q = 0; q <= cells; q++) for (let p = 0; p <= cells; p++) out[q * m + p] = Math.min(heights[q * m + p], collisionAt(get, p, q, k));
124
+ return out;
125
+ };
126
+
127
+ // src/engine-ground-step.ts
128
+ var STEP_TOLERANCE = 5e-3;
129
+ var off = (a, b) => Math.abs(a - b) > STEP_TOLERANCE;
130
+ var holdsStep = (s) => {
131
+ const c00 = s[0], c10 = s[1], c01 = s[2], c11 = s[3];
132
+ return off(s[4], (c00 + c10 + c01 + c11) / 4) || off(s[5], (c00 + c10) / 2) || off(s[6], (c01 + c11) / 2) || off(s[7], (c00 + c01) / 2) || off(s[8], (c10 + c11) / 2);
133
+ };
134
+
135
+ // src/engine-ground-ring.ts
136
+ var SOLID_RING = 0.2;
137
+ var nine = new Float64Array(9);
138
+ var build = 0;
139
+ var heldSamples = new Float64Array(0);
140
+ var sampleBuild = new Uint32Array(0);
141
+ var heldCells = new Float64Array(0);
142
+ var cellBuild = new Uint32Array(0);
143
+ var createGroundRing = (heights, cells, x0, z0, cell, groundAt, fieldStep = 1) => {
144
+ const band = typeof groundAt === "function" ? null : groundAt;
145
+ const ring = Math.round(SOLID_RING / cell);
146
+ const m = cells + 1;
147
+ const width = 2 * (cells + 2 * ring) + 1;
148
+ const across = cells + 2 * ring;
149
+ if (sampleBuild.length < width * width) {
150
+ heldSamples = new Float64Array(width * width);
151
+ sampleBuild = new Uint32Array(width * width);
152
+ }
153
+ if (cellBuild.length < across * across) {
154
+ heldCells = new Float64Array(4 * across * across);
155
+ cellBuild = new Uint32Array(across * across);
156
+ }
157
+ const mark = ++build;
158
+ const read = [];
159
+ const half = (a, b) => {
160
+ if (a % 2 === 0 && b % 2 === 0 && a >= 0 && b >= 0 && a <= 2 * cells && b <= 2 * cells) return heights[b / 2 * m + a / 2];
161
+ const key = (b + 2 * ring) * width + a + 2 * ring;
162
+ if (sampleBuild[key] === mark) return heldSamples[key];
163
+ const x = x0 + a / 2 * cell;
164
+ const z = z0 + b / 2 * cell;
165
+ const h = band === null ? groundAt(x, z) : band[key];
166
+ heldSamples[key] = h;
167
+ sampleBuild[key] = mark;
168
+ read.push(x, z, h);
169
+ return h;
170
+ };
171
+ const fine = (p, q) => half(2 * p, 2 * q);
172
+ return {
173
+ cells: ring,
174
+ read,
175
+ cell(i, j, out) {
176
+ const key = (j + ring) * across + i + ring;
177
+ if (cellBuild[key] !== mark) {
178
+ const a = 2 * i, b = 2 * j;
179
+ nine[0] = half(a, b);
180
+ nine[1] = half(a + 2, b);
181
+ nine[2] = half(a, b + 2);
182
+ nine[3] = half(a + 2, b + 2);
183
+ nine[4] = half(a + 1, b + 1);
184
+ nine[5] = half(a + 1, b);
185
+ nine[6] = half(a + 1, b + 2);
186
+ nine[7] = half(a, b + 1);
187
+ nine[8] = half(a + 2, b + 1);
188
+ const step = holdsStep(nine);
189
+ let floor = Infinity;
190
+ for (let k = 0; k < 9; k++) floor = Math.min(floor, nine[k]);
191
+ const corner = (p, q, h) => fieldStep === 1 ? h : Math.min(h, collisionAt(fine, p, q, fieldStep));
192
+ const c00 = corner(i, j, nine[0]), c10 = corner(i + 1, j, nine[1]), c01 = corner(i, j + 1, nine[2]), c11 = corner(i + 1, j + 1, nine[3]);
193
+ heldCells[4 * key] = step ? floor : c00;
194
+ heldCells[4 * key + 1] = step ? floor : c10;
195
+ heldCells[4 * key + 2] = step ? floor : c01;
196
+ heldCells[4 * key + 3] = step ? floor : c11;
197
+ cellBuild[key] = mark;
198
+ }
199
+ for (let k = 0; k < 4; k++) out[k] = heldCells[4 * key + k];
200
+ }
201
+ };
202
+ };
203
+
204
+ // src/engine-ground-reach.ts
205
+ var buildLowPyramid = (heights, cells, cell) => {
206
+ const m = cells + 1;
207
+ const first = new Float64Array(cells * cells);
208
+ for (let j = 0; j < cells; j++) {
209
+ for (let i = 0; i < cells; i++) {
210
+ const h00 = heights[j * m + i], h10 = heights[j * m + i + 1], h01 = heights[(j + 1) * m + i], h11 = heights[(j + 1) * m + i + 1];
211
+ const dx = Math.max(Math.abs(h10 - h00), Math.abs(h11 - h01)), dz = Math.max(Math.abs(h01 - h00), Math.abs(h11 - h10));
212
+ first[j * cells + i] = (dx * dx + dz * dz) / (cell * cell);
213
+ }
214
+ }
215
+ const sizes = [cells];
216
+ const planes2 = [new Float64Array(0)];
217
+ const steepest = [first];
218
+ for (let s = 2; s < 2 * cells; s *= 2) {
219
+ const below = steepest[steepest.length - 1];
220
+ const n = sizes[sizes.length - 1];
221
+ const k = Math.ceil(cells / s);
222
+ const level = new Float64Array(k * k);
223
+ for (let j = 0; j < n; j++) for (let i = 0; i < n; i++) level[(j >> 1) * k + (i >> 1)] = Math.max(level[(j >> 1) * k + (i >> 1)], below[j * n + i]);
224
+ sizes.push(k);
225
+ planes2.push(new Float64Array(3 * k * k).fill(NaN));
226
+ steepest.push(level);
227
+ }
228
+ return { heights, cells, sizes, planes: planes2, steepest };
229
+ };
230
+ var steepPyramid = null;
231
+ var steepSquare = 0;
232
+ var steepIa = 0;
233
+ var steepIb = 0;
234
+ var steepJa = 0;
235
+ var steepJb = 0;
236
+ var visit = (l, bi, bj) => {
237
+ const pyramid = steepPyramid;
238
+ if (pyramid.steepest[l][bj * pyramid.sizes[l] + bi] <= steepSquare) return false;
239
+ const s = 1 << l;
240
+ const a = bi * s, c = bj * s;
241
+ const b = Math.min(pyramid.cells, a + s), d = Math.min(pyramid.cells, c + s);
242
+ if (a >= steepIb || b <= steepIa || c >= steepJb || d <= steepJa) return false;
243
+ if (l === 0) return true;
244
+ for (let dj = 0; dj < 2; dj++) for (let di = 0; di < 2; di++) if (visit(l - 1, 2 * bi + di, 2 * bj + dj)) return true;
245
+ return false;
246
+ };
247
+ var steeperOver = (pyramid, ia, ib, ja, jb, limit) => {
248
+ steepPyramid = pyramid;
249
+ steepSquare = limit * limit;
250
+ steepIa = ia;
251
+ steepIb = ib;
252
+ steepJa = ja;
253
+ steepJb = jb;
254
+ const steeper = visit(pyramid.sizes.length - 1, 0, 0);
255
+ steepPyramid = null;
256
+ return steeper;
257
+ };
258
+ var planeUnder = (pyramid, l, bi, bj) => {
259
+ const level = pyramid.planes[l];
260
+ const at = 3 * (bj * pyramid.sizes[l] + bi);
261
+ if (!Number.isNaN(level[at])) return at;
262
+ const { heights, cells } = pyramid;
263
+ const m = cells + 1;
264
+ const s = 1 << l;
265
+ const ia = bi * s, ja = bj * s, ib = Math.min(cells, ia + s), jb = Math.min(cells, ja + s);
266
+ const h00 = heights[ja * m + ia], h10 = heights[ja * m + ib], h01 = heights[jb * m + ia], h11 = heights[jb * m + ib];
267
+ const sx = (h10 - h00 + h11 - h01) / 2 / (ib - ia), sz = (h01 - h00 + h11 - h10) / 2 / (jb - ja);
268
+ let under = 0;
269
+ for (let j = ja; j <= jb; j++) for (let i = ia; i <= ib; i++) under = Math.min(under, heights[j * m + i] - (h00 + sx * (i - ia) + sz * (j - ja)));
270
+ level[at] = h00 + under;
271
+ level[at + 1] = sx;
272
+ level[at + 2] = sz;
273
+ return at;
274
+ };
275
+ var corner4 = new Float64Array(4);
276
+ var PRUNE_SLACK = 1e-6;
277
+ var planes = new Float64Array(9);
278
+ var dirs = new Float64Array(10);
279
+ var spans = new Float64Array(10);
280
+ var planeCount = 0;
281
+ var dirCount = 0;
282
+ var apart = (xa, xb, za, zb) => {
283
+ for (let d = 0; d < dirCount; d += 2) {
284
+ const dx = dirs[d], dz = dirs[d + 1];
285
+ const lo = (dx > 0 ? xa * dx : xb * dx) + (dz > 0 ? za * dz : zb * dz);
286
+ const hi = (dx > 0 ? xb * dx : xa * dx) + (dz > 0 ? zb * dz : za * dz);
287
+ if (hi < spans[d] - 1e-9 || lo > spans[d + 1] + 1e-9) return true;
288
+ }
289
+ return false;
290
+ };
291
+ var over = (xa, xb, za, zb) => {
292
+ let best = Infinity;
293
+ for (let p = 0; p < planeCount; p += 3) {
294
+ const a = planes[p], bx = planes[p + 1], bz = planes[p + 2];
295
+ const most = Math.max(a + bx * xa + bz * za - corner4[0], a + bx * xb + bz * za - corner4[1], a + bx * xa + bz * zb - corner4[2], a + bx * xb + bz * zb - corner4[3]);
296
+ if (most < best) best = most;
297
+ }
298
+ return best;
299
+ };
300
+ var searchTile = null;
301
+ var searchTop = new Int32Array(4);
302
+ var searchMost = 0;
303
+ var leaf = (ia, ja) => {
304
+ const { heights, cells, x0, z0, cell } = searchTile;
305
+ const m = cells + 1;
306
+ const xa = x0 + ia * cell, za = z0 + ja * cell;
307
+ corner4[0] = heights[ja * m + ia];
308
+ corner4[1] = heights[ja * m + ia + 1];
309
+ corner4[2] = heights[(ja + 1) * m + ia];
310
+ corner4[3] = heights[(ja + 1) * m + ia + 1];
311
+ const e = over(xa, xa + cell, za, za + cell);
312
+ if (e > searchMost) searchMost = e;
313
+ };
314
+ var search = (l, bi, bj) => {
315
+ const { cells, x0, z0, cell, pyramid } = searchTile;
316
+ const s = 1 << l;
317
+ const ia = bi * s, ja = bj * s;
318
+ if (ia >= cells || ja >= cells) return;
319
+ const ib = Math.min(cells, ia + s), jb = Math.min(cells, ja + s);
320
+ if (ia >= searchTop[0] && ib <= searchTop[1] && ja >= searchTop[2] && jb <= searchTop[3]) return;
321
+ const xa = x0 + ia * cell, xb = x0 + ib * cell, za = z0 + ja * cell, zb = z0 + jb * cell;
322
+ if (apart(xa, xb, za, zb)) return;
323
+ if (l === 0) return leaf(ia, ja);
324
+ const level = pyramid.planes[l];
325
+ const at = planeUnder(pyramid, l, bi, bj);
326
+ const h = level[at], sx = level[at + 1], sz = level[at + 2];
327
+ corner4[0] = h;
328
+ corner4[1] = h + sx * (ib - ia);
329
+ corner4[2] = h + sz * (jb - ja);
330
+ corner4[3] = h + sx * (ib - ia) + sz * (jb - ja);
331
+ if (over(xa, xb, za, zb) <= searchMost + PRUNE_SLACK) return;
332
+ for (let dj = 0; dj < 2; dj++) for (let di = 0; di < 2; di++) search(l - 1, 2 * bi + di, 2 * bj + dj);
333
+ };
334
+ var reachOf = (box, t) => {
335
+ const { heights, cells, x0, z0, cell, pyramid, ring } = t;
336
+ const m = cells + 1;
337
+ planeCount = 0;
338
+ for (let k = 0; k < 3; k++) {
339
+ const a = box.axes[k];
340
+ if (Math.abs(a[1]) < 1e-9) continue;
341
+ const bx2 = -a[0] / a[1], bz2 = -a[2] / a[1];
342
+ planes[planeCount++] = box.c[1] + box.half[k] / Math.abs(a[1]) - bx2 * box.c[0] - bz2 * box.c[2];
343
+ planes[planeCount++] = bx2;
344
+ planes[planeCount++] = bz2;
345
+ }
346
+ dirs[0] = 1;
347
+ dirs[1] = 0;
348
+ dirs[2] = 0;
349
+ dirs[3] = 1;
350
+ dirCount = 4;
351
+ for (const a of box.axes) {
352
+ const l = Math.hypot(a[0], a[2]);
353
+ if (l < 1e-9) continue;
354
+ dirs[dirCount++] = -a[2] / l;
355
+ dirs[dirCount++] = a[0] / l;
356
+ }
357
+ for (let d = 0; d < dirCount; d += 2) {
358
+ const dx = dirs[d], dz = dirs[d + 1];
359
+ let r2 = 0;
360
+ for (let k = 0; k < 3; k++) r2 += box.half[k] * Math.abs(box.axes[k][0] * dx + box.axes[k][2] * dz);
361
+ const mid = box.c[0] * dx + box.c[2] * dz;
362
+ spans[d] = mid - r2;
363
+ spans[d + 1] = mid + r2;
364
+ }
365
+ let inTile = 0;
366
+ const n = box.axes[1], ax = box.axes[0], az = box.axes[2];
367
+ const tx = box.c[0] + n[0] * box.half[1], ty = box.c[1] + n[1] * box.half[1], tz = box.c[2] + n[2] * box.half[1];
368
+ const rx = box.half[0] * Math.abs(ax[0]) + box.half[2] * Math.abs(az[0]), rz = box.half[0] * Math.abs(ax[2]) + box.half[2] * Math.abs(az[2]);
369
+ const ta = Math.max(0, Math.floor((tx - rx - x0) / cell)), tb = Math.min(cells, Math.ceil((tx + rx - x0) / cell));
370
+ const sa = Math.max(0, Math.floor((tz - rz - z0) / cell)), sb = Math.min(cells, Math.ceil((tz + rz - z0) / cell));
371
+ const bx = -n[0] / n[1], bz = -n[2] / n[1];
372
+ for (let j = sa; j <= sb; j++) {
373
+ for (let i = ta; i <= tb; i++) {
374
+ const e = ty + bx * (x0 + i * cell - tx) + bz * (z0 + j * cell - tz) - heights[j * m + i];
375
+ if (e > inTile) inTile = e;
376
+ }
377
+ }
378
+ const ia = Math.floor((spans[0] - x0) / cell), ib = Math.ceil((spans[1] - x0) / cell);
379
+ const ja = Math.floor((spans[2] - z0) / cell), jb = Math.ceil((spans[3] - z0) / cell);
380
+ const within = ia >= 0 && ja >= 0 && ib <= cells && jb <= cells;
381
+ if (within && !steeperOver(pyramid, ia, ib, ja, jb, n[1] / Math.max(1e-12, Math.hypot(n[0], n[2])))) return { tile: inTile, ring: 0 };
382
+ searchTile = t;
383
+ searchTop[0] = ta;
384
+ searchTop[1] = tb;
385
+ searchTop[2] = sa;
386
+ searchTop[3] = sb;
387
+ searchMost = inTile;
388
+ const ua = Math.max(0, ia), ub = Math.min(cells, ib), va = Math.max(0, ja), vb = Math.min(cells, jb);
389
+ if (ua < ub && va < vb) {
390
+ let l = 0;
391
+ while (1 << l < Math.max(ub - ua, vb - va) && l < pyramid.sizes.length - 1) l++;
392
+ for (let bj = va >> l; bj <= vb - 1 >> l; bj++) for (let bi = ua >> l; bi <= ub - 1 >> l; bi++) search(l, bi, bj);
393
+ }
394
+ inTile = searchMost;
395
+ searchTile = null;
396
+ let inRing = 0;
397
+ const r = ring.cells;
398
+ if (within) return { tile: inTile, ring: 0 };
399
+ for (let j = Math.max(-r, ja); j < Math.min(cells + r, jb); j++) {
400
+ const across = j >= 0 && j < cells;
401
+ for (let i = Math.max(-r, ia); i < Math.min(cells + r, ib); i++) {
402
+ if (across && i >= 0 && i < cells) i = cells;
403
+ if (i >= Math.min(cells + r, ib)) break;
404
+ const xa = x0 + i * cell, za = z0 + j * cell;
405
+ if (apart(xa, xa + cell, za, za + cell)) continue;
406
+ ring.cell(i, j, corner4);
407
+ const e = over(xa, xa + cell, za, za + cell);
408
+ if (e > inRing) inRing = e;
409
+ }
410
+ }
411
+ return { tile: inTile, ring: inRing };
412
+ };
413
+
414
+ // src/engine-ground-solid.ts
415
+ var SOLID_DEPTH = 0.5;
416
+ var SOLID_RELIEF = 5e-3;
417
+ var SOLID_MIN_CELLS = 3;
418
+ var SOLID_MARGIN = 5e-3;
419
+ var SOLID_THINNEST = 0.05;
420
+ var SEAM_SPLIT = 0.01;
421
+ var NO_SOLID = { boxes: [], ring: [] };
422
+ var unitInto = (out, x, y, z) => {
423
+ const l = Math.hypot(x, y, z);
424
+ out[0] = x / l;
425
+ out[1] = y / l;
426
+ out[2] = z / l;
427
+ return out;
428
+ };
429
+ var slopes = new Float64Array(2);
430
+ var reachAxes = [new Float64Array(3), new Float64Array(3), new Float64Array(3)];
431
+ var reachBox = { c: new Float64Array(3), axes: [reachAxes[0], reachAxes[1], reachAxes[2]], half: new Float64Array(3) };
432
+ var quatOfAxes = (a, b, c) => {
433
+ const m00 = a[0], m10 = a[1], m20 = a[2], m01 = b[0], m11 = b[1], m21 = b[2], m02 = c[0], m12 = c[1], m22 = c[2];
434
+ const trace = m00 + m11 + m22;
435
+ if (trace > 0) {
436
+ const s2 = 0.5 / Math.sqrt(trace + 1);
437
+ return [(m21 - m12) * s2, (m02 - m20) * s2, (m10 - m01) * s2, 0.25 / s2];
438
+ }
439
+ if (m00 > m11 && m00 > m22) {
440
+ const s2 = 2 * Math.sqrt(1 + m00 - m11 - m22);
441
+ return [0.25 * s2, (m01 + m10) / s2, (m02 + m20) / s2, (m21 - m12) / s2];
442
+ }
443
+ if (m11 > m22) {
444
+ const s2 = 2 * Math.sqrt(1 + m11 - m00 - m22);
445
+ return [(m01 + m10) / s2, 0.25 * s2, (m12 + m21) / s2, (m02 - m20) / s2];
446
+ }
447
+ const s = 2 * Math.sqrt(1 + m22 - m00 - m11);
448
+ return [(m02 + m20) / s, (m12 + m21) / s, 0.25 * s, (m10 - m01) / s];
449
+ };
450
+ var thickWithin = (lean, low, high, lo, hi) => {
451
+ if (low < lo || high > hi) return -Infinity;
452
+ return lean > 1e-12 ? (hi - high) / lean : lean < -1e-12 ? (low - lo) / -lean : Infinity;
453
+ };
454
+ var buildSolidVolume = (read, cells, x0, z0, cell, groundAt, step = 1) => {
455
+ const heights = underBoth(read, cells, step);
456
+ const m = cells + 1;
457
+ let low = Infinity;
458
+ for (const h of heights) low = Math.min(low, h);
459
+ const bottom = low - SOLID_DEPTH;
460
+ const boxes = [];
461
+ const ring = createGroundRing(heights, cells, x0, z0, cell, groundAt, coarseStep(cells, cell));
462
+ const reachTile = { heights, cells, x0, z0, cell, pyramid: buildLowPyramid(heights, cells, cell), ring };
463
+ const outer = ring.cells * cell;
464
+ const loX = x0 - outer, hiX = x0 + cells * cell + outer, loZ = z0 - outer, hiZ = z0 + cells * cell + outer;
465
+ const at = (i, j) => heights[j * m + i];
466
+ const slopesOf = (i0, j0, i1, j1) => {
467
+ slopes[0] = (at(i1, j0) - at(i0, j0) + at(i1, j1) - at(i0, j1)) / 2 / (i1 - i0);
468
+ slopes[1] = (at(i0, j1) - at(i0, j0) + at(i1, j1) - at(i1, j0)) / 2 / (j1 - j0);
469
+ };
470
+ const planeGap = (i0, j0, i1, j1) => {
471
+ slopesOf(i0, j0, i1, j1);
472
+ const sx = slopes[0], sz = slopes[1];
473
+ let least = Infinity;
474
+ let most = -Infinity;
475
+ for (let j = j0; j <= j1; j++) {
476
+ for (let i = i0; i <= i1; i++) {
477
+ const above = at(i, j) - (at(i0, j0) + sx * (i - i0) + sz * (j - j0));
478
+ if (above < least) least = above;
479
+ if (above > most) most = above;
480
+ }
481
+ }
482
+ return most - least;
483
+ };
484
+ const level = (i0, j0, i1, j1, least) => {
485
+ const top = least - SOLID_MARGIN;
486
+ boxes.push(x0 + (i0 + i1) / 2 * cell, (top + bottom) / 2, z0 + (j0 + j1) / 2 * cell, (i1 - i0) * cell / 2, (top - bottom) / 2, (j1 - j0) * cell / 2, 0, 0, 0, 1);
487
+ };
488
+ const sloped = (i0, j0, i1, j1, least, most) => {
489
+ const span = Math.max(i1 - i0, j1 - j0);
490
+ slopesOf(i0, j0, i1, j1);
491
+ const gx = slopes[0] / cell;
492
+ const gz = slopes[1] / cell;
493
+ const ox = x0 + i0 * cell;
494
+ const oz = z0 + j0 * cell;
495
+ const base = at(i0, j0);
496
+ const n = unitInto(reachAxes[1], -gx, 1, -gz);
497
+ const ax = unitInto(reachAxes[0], 1, gx, 0);
498
+ const az = reachAxes[2];
499
+ az[0] = ax[1] * n[2] - ax[2] * n[1];
500
+ az[1] = ax[2] * n[0] - ax[0] * n[2];
501
+ az[2] = ax[0] * n[1] - ax[1] * n[0];
502
+ const xa = ox, xb = x0 + i1 * cell, za = oz, zb = z0 + j1 * cell;
503
+ const cx = (xa + xb) / 2, cz = (za + zb) / 2, cy = base + gx * (cx - ox) + gz * (cz - oz);
504
+ let hx = 0;
505
+ let hz = 0;
506
+ for (let a = 0; a < 2; a++) {
507
+ const x = a === 0 ? xa : xb;
508
+ for (let b = 0; b < 2; b++) {
509
+ const z = b === 0 ? za : zb;
510
+ const d0 = x - cx, d1 = base + gx * (x - ox) + gz * (z - oz) - cy, d2 = z - cz;
511
+ hx = Math.max(hx, Math.abs(d0 * ax[0] + d1 * ax[1] + d2 * ax[2]));
512
+ hz = Math.max(hz, Math.abs(d0 * az[0] + d1 * az[1] + d2 * az[2]));
513
+ }
514
+ }
515
+ hx += cell;
516
+ hz += cell;
517
+ let minX = Infinity, maxX = -Infinity, minZ = Infinity, maxZ = -Infinity, highest = -Infinity;
518
+ for (let a = 0; a < 2; a++) {
519
+ const su = a === 0 ? -1 : 1;
520
+ for (let b = 0; b < 2; b++) {
521
+ const sw = b === 0 ? -1 : 1;
522
+ const qx = cx + su * hx * ax[0] + sw * hz * az[0];
523
+ const qz = cz + su * hx * ax[2] + sw * hz * az[2];
524
+ minX = Math.min(minX, qx);
525
+ maxX = Math.max(maxX, qx);
526
+ minZ = Math.min(minZ, qz);
527
+ maxZ = Math.max(maxZ, qz);
528
+ highest = Math.max(highest, cy + su * hx * ax[1] + sw * hz * az[1]);
529
+ }
530
+ }
531
+ const deep = (highest - bottom) / n[1];
532
+ const fits = Math.min(thickWithin(-n[0], minX, maxX, loX, hiX), thickWithin(-n[2], minZ, maxZ, loZ, hiZ));
533
+ const thick = Math.max(SOLID_THINNEST, Math.min(deep, fits));
534
+ if (fits < thick) {
535
+ if (span > SOLID_MIN_CELLS) return false;
536
+ level(i0, j0, i1, j1, least);
537
+ return true;
538
+ }
539
+ const hy = thick / 2;
540
+ const centre = reachBox.c;
541
+ centre[0] = cx - n[0] * hy;
542
+ centre[1] = cy - n[1] * hy;
543
+ centre[2] = cz - n[2] * hy;
544
+ reachBox.half[0] = hx;
545
+ reachBox.half[1] = hy;
546
+ reachBox.half[2] = hz;
547
+ const reach = reachOf(reachBox, reachTile);
548
+ if (reach.ring > reach.tile + SEAM_SPLIT && span > SOLID_MIN_CELLS) return false;
549
+ const drop = SOLID_MARGIN + Math.max(reach.tile, reach.ring);
550
+ if (drop - SOLID_MARGIN >= most - least) {
551
+ level(i0, j0, i1, j1, least);
552
+ return true;
553
+ }
554
+ level(i0, j0, i1, j1, least);
555
+ const q = quatOfAxes(ax, n, az);
556
+ boxes.push(centre[0], centre[1] - drop, centre[2], hx, hy, hz, q[0], q[1], q[2], q[3]);
557
+ return true;
558
+ };
559
+ const fill = (i0, j0, i1, j1) => {
560
+ let least = Infinity;
561
+ let most = -Infinity;
562
+ for (let j = j0; j <= j1; j++) {
563
+ for (let i = i0; i <= i1; i++) {
564
+ const h = at(i, j);
565
+ if (h < least) least = h;
566
+ if (h > most) most = h;
567
+ }
568
+ }
569
+ if (most - least <= SOLID_RELIEF) return level(i0, j0, i1, j1, least);
570
+ const span = Math.max(i1 - i0, j1 - j0);
571
+ if (planeGap(i0, j0, i1, j1) > SOLID_RELIEF && span > SOLID_MIN_CELLS || !sloped(i0, j0, i1, j1, least, most)) {
572
+ const im = i1 - i0 < 2 ? i1 : i0 + i1 >> 1;
573
+ const jm = j1 - j0 < 2 ? j1 : j0 + j1 >> 1;
574
+ for (let a = i0; a < i1; a = im) {
575
+ const b = a === i0 ? im : i1;
576
+ for (let d = j0; d < j1; d = jm) {
577
+ const e = d === j0 ? jm : j1;
578
+ fill(a, d, b, e);
579
+ if (e === j1) break;
580
+ }
581
+ if (b === i1) break;
582
+ }
583
+ }
584
+ };
585
+ fill(0, 0, cells, cells);
586
+ return { boxes, ring: ring.read };
587
+ };
588
+
589
+ // src/engine-ground.ts
590
+ var GROUND_CELL = 0.025;
591
+ var BOX_DEPTH = 0.05;
592
+ var UNDER_BOX = 0.01;
593
+ var NO_HEIGHTS = new Float32Array(0);
594
+ var cellSamples = new Float64Array(9);
595
+ var seriesOf = (n) => {
596
+ const m = n + 1;
597
+ return [
598
+ { offset: 0, width: m, rows: m, ox: 0, oz: 0 },
599
+ { offset: m * m, width: n, rows: n, ox: 0.5, oz: 0.5 },
600
+ { offset: m * m + n * n, width: n, rows: m, ox: 0.5, oz: 0 },
601
+ { offset: m * m + n * n + n * m, width: m, rows: n, ox: 0, oz: 0.5 }
602
+ ];
603
+ };
604
+ var sampleCount = (n) => (n + 1) * (n + 1) + n * n + 2 * n * (n + 1);
605
+ var seriesX = new Float64Array(0);
606
+ var seriesZ = new Float64Array(0);
607
+ var seriesHeights = new Float64Array(0);
608
+ var readGround = (groundAt, samples, x0, z0, size, rect, grid) => {
609
+ const n = Math.round(size / GROUND_CELL);
610
+ const c = size / n;
611
+ let changed = false;
612
+ for (const { offset, width, rows, ox, oz } of seriesOf(n)) {
613
+ const i0 = rect ? Math.max(0, Math.floor((rect.minX - x0) / c - ox)) : 0;
614
+ const i1 = rect ? Math.min(width - 1, Math.ceil((rect.maxX - x0) / c - ox)) : width - 1;
615
+ const j0 = rect ? Math.max(0, Math.floor((rect.minZ - z0) / c - oz)) : 0;
616
+ const j1 = rect ? Math.min(rows - 1, Math.ceil((rect.maxZ - z0) / c - oz)) : rows - 1;
617
+ if (i0 > i1 || j0 > j1) continue;
618
+ if (!grid) {
619
+ for (let j = j0; j <= j1; j++) {
620
+ for (let i = i0; i <= i1; i++) {
621
+ const h = groundAt(x0 + (i + ox) * c, z0 + (j + oz) * c);
622
+ if (samples[offset + j * width + i] !== h) changed = true;
623
+ samples[offset + j * width + i] = h;
624
+ }
625
+ }
626
+ continue;
627
+ }
628
+ const across = i1 - i0 + 1;
629
+ const along = j1 - j0 + 1;
630
+ if (seriesX.length < across) seriesX = new Float64Array(across);
631
+ if (seriesZ.length < along) seriesZ = new Float64Array(along);
632
+ if (seriesHeights.length < across * along) seriesHeights = new Float64Array(across * along);
633
+ for (let i = 0; i < across; i++) seriesX[i] = x0 + (i0 + i + ox) * c;
634
+ for (let j = 0; j < along; j++) seriesZ[j] = z0 + (j0 + j + oz) * c;
635
+ grid(seriesX.subarray(0, across), seriesZ.subarray(0, along), seriesHeights);
636
+ for (let j = 0; j < along; j++) {
637
+ const from = j * across;
638
+ const to = offset + (j0 + j) * width + i0;
639
+ if (!changed) {
640
+ for (let i = 0; i < across; i++) {
641
+ if (samples[to + i] !== seriesHeights[from + i]) {
642
+ changed = true;
643
+ break;
644
+ }
645
+ }
646
+ }
647
+ samples.set(seriesHeights.subarray(from, from + across), to);
648
+ }
649
+ }
650
+ return changed;
651
+ };
652
+ var shapeGround = (samples, x0, z0, size, solidGround, wasCoarse = false) => {
653
+ const n = Math.round(size / GROUND_CELL);
654
+ const c = size / n;
655
+ const m = n + 1;
656
+ const [cornerSeries, centreSeries, midXSeries, midZSeries] = seriesOf(n);
657
+ const corners = samples.subarray(cornerSeries.offset, centreSeries.offset);
658
+ const centres = samples.subarray(centreSeries.offset, midXSeries.offset);
659
+ const midX = samples.subarray(midXSeries.offset, midZSeries.offset);
660
+ const midZ = samples.subarray(midZSeries.offset);
661
+ let low = Infinity;
662
+ let high = -Infinity;
663
+ for (const h of samples) {
664
+ low = Math.min(low, h);
665
+ high = Math.max(high, h);
666
+ }
667
+ if (high - low < 1e-9) return { heights: new Float32Array(0), cells: n, collision: NO_HEIGHTS, collisionCells: 0, boxes: [], solid: NO_SOLID, level: high, samples };
668
+ const corner = (i, j) => corners[j * m + i];
669
+ const mask = new Uint8Array(n * n);
670
+ for (let j = 0; j < n; j++) {
671
+ for (let i = 0; i < n; i++) {
672
+ cellSamples[0] = corner(i, j);
673
+ cellSamples[1] = corner(i + 1, j);
674
+ cellSamples[2] = corner(i, j + 1);
675
+ cellSamples[3] = corner(i + 1, j + 1);
676
+ cellSamples[4] = centres[j * n + i];
677
+ cellSamples[5] = midX[j * n + i];
678
+ cellSamples[6] = midX[(j + 1) * n + i];
679
+ cellSamples[7] = midZ[j * m + i];
680
+ cellSamples[8] = midZ[j * m + i + 1];
681
+ if (!holdsStep(cellSamples)) continue;
682
+ for (let dj = -1; dj <= 1; dj++) for (let di = -1; di <= 1; di++) if (i + di >= 0 && j + dj >= 0 && i + di < n && j + dj < n) mask[(j + dj) * n + i + di] = 1;
683
+ }
684
+ }
685
+ const heights = new Float32Array(m * m);
686
+ for (let j = 0; j <= n; j++) {
687
+ for (let i = 0; i <= n; i++) {
688
+ let inside = true;
689
+ let least = corner(i, j);
690
+ for (let dj = -1; dj <= 0 && inside; dj++) for (let di = -1; di <= 0; di++) {
691
+ const ii = i + di, jj = j + dj;
692
+ if (ii < 0 || jj < 0 || ii >= n || jj >= n || !mask[jj * n + ii]) {
693
+ inside = false;
694
+ break;
695
+ }
696
+ least = Math.min(least, centres[jj * n + ii], corner(ii, jj), corner(ii + 1, jj), corner(ii, jj + 1), corner(ii + 1, jj + 1));
697
+ }
698
+ heights[j * m + i] = inside ? least - UNDER_BOX : corner(i, j);
699
+ }
700
+ }
701
+ const boxes = [];
702
+ for (let j = 0; j < n; j++) {
703
+ let i = 0;
704
+ while (i < n) {
705
+ if (!mask[j * n + i]) {
706
+ i++;
707
+ continue;
708
+ }
709
+ const top = centres[j * n + i];
710
+ let end = i + 1;
711
+ while (end < n && mask[j * n + end] && Math.abs(centres[j * n + end] - top) < 1e-3) end++;
712
+ let bottom = top;
713
+ for (let k = Math.max(0, i - 1); k <= Math.min(n, end + 1); k++) for (let jj = Math.max(0, j - 1); jj <= Math.min(n, j + 2); jj++) bottom = Math.min(bottom, corner(k, jj));
714
+ bottom -= BOX_DEPTH;
715
+ boxes.push(x0 + (i + end) / 2 * c, (top + bottom) / 2, z0 + (j + 0.5) * c, (end - i) * c / 2, (top - bottom) / 2, c / 2);
716
+ i = end;
717
+ }
718
+ }
719
+ const { step, field } = collisionFieldOf(heights, n, c, boxes.length > 0, wasCoarse);
720
+ return { heights, cells: n, collision: field, collisionCells: n / step, boxes, solid: solidGround ? buildSolidVolume(heights, n, x0, z0, c, solidGround, step) : NO_SOLID, level: null, samples };
721
+ };
722
+
723
+ // src/engine-ground-job.ts
724
+ var ringOf = (cell) => Math.round(SOLID_RING / cell);
725
+ var bandLength = (size) => {
726
+ const n = Math.round(size / GROUND_CELL);
727
+ const width = 2 * (n + 2 * ringOf(size / n)) + 1;
728
+ return width * width;
729
+ };
730
+ var lineX = new Float64Array(0);
731
+ var lineZ = new Float64Array(0);
732
+ var sideOut = new Float64Array(0);
733
+ var readGroundBand = (groundAt, band, x0, z0, size, grid) => {
734
+ const n = Math.round(size / GROUND_CELL);
735
+ const cell = size / n;
736
+ const r = ringOf(cell);
737
+ const lo = -2 * r;
738
+ const hi = 2 * n + 2 * r;
739
+ const width = hi - lo + 1;
740
+ const xAt = (a) => x0 + a / 2 * cell;
741
+ const zAt = (b) => z0 + b / 2 * cell;
742
+ if (!grid) {
743
+ for (let b = lo; b <= hi; b++) {
744
+ for (let a = lo; a <= hi; a++) {
745
+ if (a > 0 && a < 2 * n && b > 0 && b < 2 * n) continue;
746
+ band[(b - lo) * width + a - lo] = groundAt(xAt(a), zAt(b));
747
+ }
748
+ }
749
+ return;
750
+ }
751
+ const strip = 2 * r + 1;
752
+ const middle = 2 * n - 1;
753
+ if (lineX.length < width) lineX = new Float64Array(width);
754
+ if (lineZ.length < width) lineZ = new Float64Array(width);
755
+ if (sideOut.length < strip * middle) sideOut = new Float64Array(strip * middle);
756
+ for (let a = lo; a <= hi; a++) lineX[a - lo] = xAt(a);
757
+ const xs = lineX.subarray(0, width);
758
+ for (const from of [lo, 2 * n]) {
759
+ for (let k = 0; k < strip; k++) lineZ[k] = zAt(from + k);
760
+ grid(xs, lineZ.subarray(0, strip), band.subarray((from - lo) * width, (from - lo + strip) * width));
761
+ }
762
+ for (let k = 0; k < middle; k++) lineZ[k] = zAt(1 + k);
763
+ const zs = lineZ.subarray(0, middle);
764
+ for (const from of [lo, 2 * n]) {
765
+ grid(lineX.subarray(from - lo, from - lo + strip), zs, sideOut);
766
+ for (let k = 0; k < middle; k++) for (let i = 0; i < strip; i++) band[(1 + k - lo) * width + from - lo + i] = sideOut[k * strip + i];
767
+ }
768
+ };
769
+ var NO_GROUND = () => {
770
+ throw new Error("engine-ground-job: a patch job reads its patch only");
771
+ };
772
+ var gridPatch = null;
773
+ var patchGrid = (xs, zs, out) => heightPatchGrid(gridPatch, xs, zs, out);
774
+ var patchSamples = new Float64Array(0);
775
+ var patchBand = new Float64Array(0);
776
+ var groundOfPatch = (job, patch) => {
777
+ const count = sampleCount(Math.round(job.size / GROUND_CELL));
778
+ if (patchSamples.length !== count) patchSamples = new Float64Array(count);
779
+ if (patchBand.length !== bandLength(job.size)) patchBand = new Float64Array(bandLength(job.size));
780
+ gridPatch = patch;
781
+ readGround(NO_GROUND, patchSamples, job.x0, job.z0, job.size, void 0, patchGrid);
782
+ readGroundBand(NO_GROUND, patchBand, job.x0, job.z0, job.size, patchGrid);
783
+ gridPatch = null;
784
+ return { samples: patchSamples, band: patchBand };
785
+ };
786
+ var computeGroundTile = (job) => {
787
+ const { samples, band } = job.patch ? groundOfPatch(job, job.patch) : job;
788
+ const t = shapeGround(samples, job.x0, job.z0, job.size, null, job.wasCoarse);
789
+ const solid = t.level === null ? buildSolidVolume(t.heights, t.cells, job.x0, job.z0, job.size / t.cells, band, t.cells / t.collisionCells) : NO_SOLID;
790
+ return {
791
+ id: job.id,
792
+ cells: t.cells,
793
+ collisionCells: t.collisionCells,
794
+ level: t.level,
795
+ heights: t.heights,
796
+ collision: t.collision,
797
+ boxes: Float64Array.from(t.boxes),
798
+ solid: Float64Array.from(solid.boxes),
799
+ ring: Float64Array.from(solid.ring),
800
+ samples: job.samples,
801
+ band: job.band,
802
+ ...job.patch ? { patch: job.patch.heights } : {}
803
+ };
804
+ };
805
+ var buffersOf = (arrays) => {
806
+ const out = [];
807
+ for (const array of arrays) {
808
+ const buffer = array?.buffer;
809
+ if (buffer && buffer.byteLength > 0 && !out.includes(buffer)) out.push(buffer);
810
+ }
811
+ return out;
812
+ };
813
+ var transferOf = (r) => buffersOf([r.heights, r.collision, r.boxes, r.solid, r.ring, r.samples, r.band, r.patch]);
814
+
815
+ // src/engine-ground-port.ts
816
+ var installGroundWorker = (scope) => {
817
+ scope.onmessage = (event) => {
818
+ const result = computeGroundTile(event.data);
819
+ scope.postMessage(result, transferOf(result));
820
+ };
821
+ };
822
+
823
+ // src/ground-worker.ts
824
+ installGroundWorker(globalThis);
825
+ })();
826
+ //# sourceMappingURL=ground-worker.global.js.map