@adatechnology/game-destruction-three 1.3.1 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -956,7 +956,8 @@ function restingBox(f) {
956
956
  z: p.z,
957
957
  reachX: Math.abs(e[0]) * x + Math.abs(e[4]) * y + Math.abs(e[8]) * z,
958
958
  reachZ: Math.abs(e[2]) * x + Math.abs(e[6]) * y + Math.abs(e[10]) * z,
959
- top: p.y + Math.abs(e[1]) * x + Math.abs(e[5]) * y + Math.abs(e[9]) * z
959
+ top: p.y + Math.abs(e[1]) * x + Math.abs(e[5]) * y + Math.abs(e[9]) * z,
960
+ bottom: p.y - Math.abs(e[1]) * x - Math.abs(e[5]) * y - Math.abs(e[9]) * z
960
961
  };
961
962
  }
962
963
  var BUILDERS = {
@@ -979,10 +980,13 @@ var SUPPORT_GAP = 0.02;
979
980
  var PILE_STEP = 0.01;
980
981
  var TIP_SPEED = 0.6;
981
982
  var TIP_TURN = 3;
983
+ var RIDGE_TURN = Math.PI * (3 - Math.sqrt(5));
984
+ var SIDE_BY_SIDE = 1e-4;
982
985
  var POINT = [0, 0, 0, 0, 0, 0];
983
986
  var SHARD_SHAPES = 6;
984
987
  var SWEEP_MIN_SPEED = 1;
985
988
  var WALL_RESTITUTION = 0.25;
989
+ var ACROSS_MIN_STEP = 1e-4;
986
990
  var WALL_GLIDE = 0.7;
987
991
  var TUMBLE_REACH = 0.1;
988
992
  var GROUND_RESTITUTION = 0.25;
@@ -1029,6 +1033,8 @@ var FragmentSystem = class {
1029
1033
  __publicField(this, "rng", createRng(FRAGMENT_SEED));
1030
1034
  /** The corners of the fragment being stepped (`writeCorners`), what of them it rests on, its halves at its fade, and its centre over that support: kept between calls. */
1031
1035
  __publicField(this, "corners", new Float64Array(24));
1036
+ /** The corners of `corners` from the lowest (`ridgeUnder`). */
1037
+ __publicField(this, "lowCorners", [0, 0, 0, 0, 0, 0, 0, 0]);
1032
1038
  __publicField(this, "support", createSupport());
1033
1039
  __publicField(this, "halves", { x: 0, y: 0, z: 0 });
1034
1040
  __publicField(this, "lever", { x: 0, y: 0, z: 0 });
@@ -1043,8 +1049,20 @@ var FragmentSystem = class {
1043
1049
  __publicField(this, "supportTurn", 0);
1044
1050
  /** The pose before the last update and after it, of the fragments that were awake (`interpolate`); null when the option is off. */
1045
1051
  __publicField(this, "trail");
1052
+ /** Fragments crushed so far under a moving piece that came down on them with no room left (`meetMover`): they fade where they were. */
1053
+ __publicField(this, "crushed", 0);
1054
+ /** The pieces moving this update (`FragmentSystemOptions.movers`). */
1055
+ __publicField(this, "moving", []);
1056
+ /** Where a fragment was when its step began, and that point carried by a moving piece. */
1057
+ __publicField(this, "before");
1058
+ __publicField(this, "carried");
1059
+ /** The move of a piece since the last update, and its inverse. */
1060
+ __publicField(this, "shift");
1061
+ __publicField(this, "unshift");
1046
1062
  __publicField(this, "markGeometry", null);
1047
1063
  __publicField(this, "markMaterial", null);
1064
+ /** The floor under the fragment `pileUnder` is asked about (`floorUnder`, or the ground when that is all that was measured): what two pieces at one height must both stand on. */
1065
+ __publicField(this, "pileFloor", 0);
1048
1066
  /** Set by `pileUnder`: the way off the edge when the fragment's centre is past what it lies on, else null. */
1049
1067
  __publicField(this, "offEdge", null);
1050
1068
  this.box = new THREE.BoxGeometry(1, 1, 1);
@@ -1053,6 +1071,10 @@ var FragmentSystem = class {
1053
1071
  this.m = new THREE.Matrix4();
1054
1072
  this.v = new THREE.Vector3();
1055
1073
  this.w = new THREE.Vector3();
1074
+ this.before = new THREE.Vector3();
1075
+ this.carried = new THREE.Vector3();
1076
+ this.shift = new THREE.Matrix4();
1077
+ this.unshift = new THREE.Matrix4();
1056
1078
  this.instances = new FragmentInstances(THREE, o.scene);
1057
1079
  this.trail = o.interpolate ? new PoseTrail() : null;
1058
1080
  }
@@ -1221,6 +1243,8 @@ var FragmentSystem = class {
1221
1243
  const live = {
1222
1244
  mesh,
1223
1245
  floor: null,
1246
+ ridgeTries: 0,
1247
+ ridgeWay: null,
1224
1248
  home,
1225
1249
  vel: new this.THREE.Vector3(f.velocity[0], f.velocity[1], f.velocity[2]),
1226
1250
  ang: new this.THREE.Vector3(f.angularVelocity[0], f.angularVelocity[1], f.angularVelocity[2]),
@@ -1382,7 +1406,9 @@ var FragmentSystem = class {
1382
1406
  update(dt) {
1383
1407
  this.tick++;
1384
1408
  this.clock += dt;
1409
+ this.moving = this.o.movers?.() ?? [];
1385
1410
  this.wakeUnsupported();
1411
+ this.wakeMoved();
1386
1412
  this.wakeRigid();
1387
1413
  this.beginTick();
1388
1414
  const g = this.o.gravity;
@@ -1416,6 +1442,23 @@ var FragmentSystem = class {
1416
1442
  if (f.rigid) this.o.rigid?.remove(f.rigid);
1417
1443
  f.rigid = null;
1418
1444
  }
1445
+ /** A fragment asleep in the box of a piece that moves wakes in the same update: what it lay on, or what lies on it, is going. */
1446
+ wakeMoved() {
1447
+ if (this.moving.length === 0) return;
1448
+ for (const f of this.list) if (f.asleep && !f.dying && !f.rigid && this.nearAnyMover(f)) this.rouse(f);
1449
+ }
1450
+ /** Whether the fragment is in the box of the moving piece (or within its reach of it). */
1451
+ nearMover(f, mover) {
1452
+ const p = f.mesh.position;
1453
+ const reach = Math.max(f.half.x, f.half.y, f.half.z);
1454
+ const b = mover.box;
1455
+ return p.x >= b[0] - reach && p.x <= b[3] + reach && p.y >= b[1] - reach && p.y <= b[4] + reach && p.z >= b[2] - reach && p.z <= b[5] + reach;
1456
+ }
1457
+ /** Whether the fragment is in the box of any piece moving this update. */
1458
+ nearAnyMover(f) {
1459
+ for (const mover of this.moving) if (this.nearMover(f, mover)) return true;
1460
+ return false;
1461
+ }
1419
1462
  /** The debris moved by a rigid body that the body's world woke (something fell on it, a blast, its ground dug): it is awake here too. */
1420
1463
  wakeRigid() {
1421
1464
  for (const f of this.list) if (f.rigid && f.asleep && !f.dying && !f.rigid.sleeping) this.rouse(f);
@@ -1497,29 +1540,77 @@ var FragmentSystem = class {
1497
1540
  const ext = Math.abs(e[1]) * f.half.x + Math.abs(e[5]) * f.half.y + Math.abs(e[9]) * f.half.z;
1498
1541
  f.floor = null;
1499
1542
  const bottom = f.mesh.position.y - ext;
1500
- const pile = this.pileUnder(f, bottom);
1501
- const offEdge = this.offEdge;
1502
1543
  const ground = this.o.groundAt(f.mesh.position.x, f.mesh.position.z);
1544
+ this.pileFloor = ground;
1545
+ let pile = this.pileUnder(f, bottom);
1503
1546
  if (bottom <= ground + SUPPORT_GAP && pile <= ground) continue;
1504
1547
  const floor = this.floorUnder(f);
1548
+ if (floor > ground) {
1549
+ this.pileFloor = floor;
1550
+ pile = this.pileUnder(f, bottom);
1551
+ }
1552
+ const offEdge = this.offEdge;
1505
1553
  if (bottom > Math.max(floor, pile) + SUPPORT_GAP || pile > floor && offEdge) this.rouse(f);
1506
1554
  }
1507
1555
  }
1508
- /** What a fragment lands on: measured where it is, and again only once it moved `FLOOR_REMEASURE` m off that spot (a ray down is not free). */
1556
+ /** What a fragment lands on: measured where it is, and again only once it moved half its size (at most `FLOOR_REMEASURE` m) off that spot (a ray down is not free). */
1509
1557
  floorUnder(f) {
1510
1558
  const p = f.mesh.position;
1511
1559
  if (!this.o.surfaceAt) return this.o.groundAt(p.x, p.z);
1512
1560
  const at = f.floor;
1513
- if (!at || Math.abs(at.x - p.x) > FLOOR_REMEASURE || Math.abs(at.z - p.z) > FLOOR_REMEASURE || p.y < at.y - FLOOR_REMEASURE) {
1561
+ const away = Math.min(FLOOR_REMEASURE, Math.max(f.half.x, f.half.y, f.half.z));
1562
+ if (!at || Math.abs(at.x - p.x) > away || Math.abs(at.z - p.z) > away || p.y < at.y - FLOOR_REMEASURE) {
1514
1563
  f.floor = { x: p.x, z: p.z, y: this.o.surfaceAt(p.x, p.y, p.z) };
1515
1564
  }
1516
1565
  return f.floor.y;
1517
1566
  }
1567
+ /**
1568
+ * Whether the fragment, come down on `floor` (what is built under its centre), lies only on its centre: none of its four lowest corners is over anything that high, so it
1569
+ * balances on an edge narrower than itself. Then the way it slides off (towards its lowest corner, or one more `RIDGE_TURN` round each time it lands so again, kept while it
1570
+ * slides), else null.
1571
+ * Asked once per measure of the floor (`floorUnder`), at the first contact.
1572
+ */
1573
+ ridgeUnder(f, e, floor) {
1574
+ const at = f.floor;
1575
+ const surfaceAt = this.o.surfaceAt;
1576
+ if (!at || !surfaceAt) return null;
1577
+ if (at.ridge !== void 0) return at.ridge;
1578
+ at.ridge = null;
1579
+ const p = f.mesh.position;
1580
+ if (floor <= this.o.groundAt(p.x, p.z) + SUPPORT_GAP) return f.ridgeWay = null;
1581
+ const c = this.corners;
1582
+ writeCorners({ e, half: f.half, corners: c });
1583
+ const low = this.lowCorners;
1584
+ for (let i = 0; i < 8; i++) {
1585
+ let k = i;
1586
+ while (k > 0 && c[3 * low[k - 1] + 1] > c[3 * i + 1]) {
1587
+ low[k] = low[k - 1];
1588
+ k--;
1589
+ }
1590
+ low[k] = i;
1591
+ }
1592
+ for (let k = 0; k < 4; k++) {
1593
+ const i = low[k];
1594
+ if (surfaceAt(p.x + c[3 * i], p.y, p.z + c[3 * i + 2]) >= floor - SUPPORT_GAP) return f.ridgeWay = null;
1595
+ }
1596
+ if (f.ridgeWay) return at.ridge = f.ridgeWay;
1597
+ const lowest = low[0];
1598
+ const lx = c[3 * lowest], lz = c[3 * lowest + 2];
1599
+ const tilt = c[3 * low[3] + 1] - c[3 * lowest + 1];
1600
+ const d = Math.hypot(lx, lz);
1601
+ f.ridgeTries++;
1602
+ const angle = f.seq * RIDGE_TURN + f.ridgeTries * RIDGE_TURN;
1603
+ f.ridgeWay = tilt > SUPPORT_GAP && d > 1e-6 && f.ridgeTries === 1 ? { x: lx / d, z: lz / d } : { x: Math.cos(angle), z: Math.sin(angle) };
1604
+ return at.ridge = f.ridgeWay;
1605
+ }
1518
1606
  step(f, dt, g) {
1519
1607
  const mesh = f.mesh;
1520
1608
  f.box = void 0;
1521
1609
  f.vel.y -= g * dt;
1610
+ this.before.copy(mesh.position);
1522
1611
  if (!this.hitWall(f, dt)) mesh.position.addScaledVector(f.vel, dt);
1612
+ const stirred = this.nearAnyMover(f);
1613
+ if (stirred) f.floor = null;
1523
1614
  const w = f.ang.length();
1524
1615
  if (w > 1e-5) {
1525
1616
  this.dq.setFromAxisAngle(this.v.copy(f.ang).multiplyScalar(1 / w), w * dt);
@@ -1531,6 +1622,7 @@ var FragmentSystem = class {
1531
1622
  const sc = f.dying ? f.fade : 1;
1532
1623
  const ext = (Math.abs(e[1]) * h.x + Math.abs(e[5]) * h.y + Math.abs(e[9]) * h.z) * sc;
1533
1624
  const floor = this.floorUnder(f);
1625
+ this.pileFloor = floor;
1534
1626
  const pile = this.pileUnder(f, mesh.position.y - ext + Math.max(0, -f.vel.y) * dt);
1535
1627
  const gy = Math.max(floor, pile);
1536
1628
  const tip = pile > floor ? this.offEdge : null;
@@ -1539,8 +1631,15 @@ var FragmentSystem = class {
1539
1631
  if (mesh.position.y - ext < gy) {
1540
1632
  contact = true;
1541
1633
  mesh.position.y = gy + ext;
1634
+ const ridge = pile > floor ? null : this.ridgeUnder(f, e, floor);
1542
1635
  if (pile > floor) {
1543
1636
  turning = this.restOnDebris(f, { g, dt, gy, sc, tip });
1637
+ } else if (ridge) {
1638
+ f.vel.y = 0;
1639
+ f.vel.x += ridge.x * TIP_SPEED * dt * 10;
1640
+ f.vel.z += ridge.z * TIP_SPEED * dt * 10;
1641
+ f.floor = null;
1642
+ turning = true;
1544
1643
  } else {
1545
1644
  writeCorners({ e, half: this.scaledHalf(f, sc), corners: this.corners });
1546
1645
  findSupport({ corners: this.corners, reach: Math.max(h.x, h.y, h.z) * sc, support: this.support });
@@ -1555,6 +1654,9 @@ var FragmentSystem = class {
1555
1654
  f.ang.multiplyScalar(Math.exp(-0.15 * dt));
1556
1655
  if (f.air > 0) this.flutter(f, dt);
1557
1656
  }
1657
+ if (stirred) {
1658
+ for (const mover of this.moving) if (this.nearMover(f, mover)) this.meetMover(f, mover, dt);
1659
+ }
1558
1660
  f.contact = contact;
1559
1661
  if (contact && !turning && isSlow(f)) {
1560
1662
  f.still += dt;
@@ -1570,11 +1672,12 @@ var FragmentSystem = class {
1570
1672
  hitWall(f, dt) {
1571
1673
  const sweep = this.o.sweep;
1572
1674
  if (!sweep) return false;
1573
- const speed = f.vel.length();
1574
- if (speed < SWEEP_MIN_SPEED) return false;
1575
- if (f.contact && f.vel.y <= 0 && Math.hypot(f.vel.x, f.vel.z) < SWEEP_MIN_SPEED) return false;
1675
+ const across = f.vel.length() < SWEEP_MIN_SPEED || f.contact && f.vel.y <= 0 && Math.hypot(f.vel.x, f.vel.z) < SWEEP_MIN_SPEED;
1676
+ const vy = across ? 0 : f.vel.y;
1677
+ const speed = Math.hypot(f.vel.x, vy, f.vel.z);
1678
+ if (speed * dt < ACROSS_MIN_STEP) return false;
1576
1679
  const p = f.mesh.position;
1577
- const dir = [f.vel.x / speed, f.vel.y / speed, f.vel.z / speed];
1680
+ const dir = [f.vel.x / speed, vy / speed, f.vel.z / speed];
1578
1681
  const e = this.m.makeRotationFromQuaternion(f.mesh.quaternion).elements;
1579
1682
  const reach = (x, y, z) => (Math.abs(e[0] * x + e[1] * y + e[2] * z) * f.half.x + Math.abs(e[4] * x + e[5] * y + e[6] * z) * f.half.y + Math.abs(e[8] * x + e[9] * y + e[10] * z) * f.half.z) * (f.dying ? f.fade : 1);
1580
1683
  const ahead = reach(dir[0], dir[1], dir[2]);
@@ -1591,6 +1694,68 @@ var FragmentSystem = class {
1591
1694
  f.floor = null;
1592
1695
  return true;
1593
1696
  }
1697
+ /**
1698
+ * The fragment's way this step as the moving piece sees it: from where the piece carried the fragment's start to where the fragment got. A face of the piece on that way
1699
+ * stops it on the face, moving as the piece moves there, with what it had across the face kept as off a wall: a panel coming down on a sheet takes it along.
1700
+ */
1701
+ meetMover(f, mover, dt) {
1702
+ const p = f.mesh.position;
1703
+ const shift = this.shift.fromArray(mover.delta);
1704
+ const from = this.carried.copy(this.before).applyMatrix4(shift);
1705
+ const rx = p.x - from.x, ry = p.y - from.y, rz = p.z - from.z;
1706
+ const span = Math.hypot(rx, ry, rz);
1707
+ if (span < 1e-6) return;
1708
+ const dir = [rx / span, ry / span, rz / span];
1709
+ const e = this.m.makeRotationFromQuaternion(f.mesh.quaternion).elements;
1710
+ const reach = (x, y, z) => (Math.abs(e[0] * x + e[1] * y + e[2] * z) * f.half.x + Math.abs(e[4] * x + e[5] * y + e[6] * z) * f.half.y + Math.abs(e[8] * x + e[9] * y + e[10] * z) * f.half.z) * (f.dying ? f.fade : 1);
1711
+ const hit = mover.sweep([from.x, from.y, from.z], dir, span + reach(dir[0], dir[1], dir[2]));
1712
+ if (!hit) return;
1713
+ const nx = hit.normal[0] ?? 0, ny = hit.normal[1] ?? 0, nz = hit.normal[2] ?? 0;
1714
+ if (rx * nx + ry * ny + rz * nz >= 0) return;
1715
+ const px = hit.point[0] ?? p.x, py = hit.point[1] ?? p.y, pz = hit.point[2] ?? p.z;
1716
+ const was = this.v.set(px, py, pz).applyMatrix4(this.unshift.copy(shift).invert());
1717
+ const cx = (px - was.x) / dt, cy = (py - was.y) / dt, cz = (pz - was.z) / dt;
1718
+ this.alongFace(f, nx, ny, nz);
1719
+ const off2 = Math.min(f.half.x, f.half.y, f.half.z) * (f.dying ? f.fade : 1) + 2e-3;
1720
+ const qx = px + nx * off2, qy = py + ny * off2, qz = pz + nz * off2;
1721
+ if (this.pinched(from, qx, qy, qz, off2)) {
1722
+ p.copy(from);
1723
+ f.vel.set(cx, cy, cz);
1724
+ this.crushed++;
1725
+ f.dying = true;
1726
+ this.activate(f);
1727
+ return;
1728
+ }
1729
+ p.set(qx, qy, qz);
1730
+ const ux = f.vel.x - cx, uy = f.vel.y - cy, uz = f.vel.z - cz;
1731
+ const inward = Math.min(0, ux * nx + uy * ny + uz * nz);
1732
+ const tx = ux - inward * nx, ty = uy - inward * ny, tz = uz - inward * nz;
1733
+ f.vel.set(cx + tx * WALL_GLIDE - inward * WALL_RESTITUTION * nx, cy + ty * WALL_GLIDE - inward * WALL_RESTITUTION * ny, cz + tz * WALL_GLIDE - inward * WALL_RESTITUTION * nz);
1734
+ f.ang.multiplyScalar(0.55);
1735
+ }
1736
+ /**
1737
+ * Whether a fragment the piece carried to `p` and pushed on to (x, y, z), `off` its reach from the face, gets there through something else or ends below what is under it there.
1738
+ */
1739
+ pinched(p, x, y, z, off2) {
1740
+ const floor = this.o.surfaceAt ? this.o.surfaceAt(x, y, z) : this.o.groundAt(x, z);
1741
+ if (y - off2 < floor) return true;
1742
+ const sweep = this.o.sweep;
1743
+ const dx = x - p.x, dy = y - p.y, dz = z - p.z;
1744
+ const length = Math.hypot(dx, dy, dz);
1745
+ if (!sweep || length < 1e-6) return false;
1746
+ return sweep([p.x, p.y, p.z], [dx / length, dy / length, dz / length], length) !== null;
1747
+ }
1748
+ /** Turns the fragment the least way that lays its thinnest side on the face of normal (nx, ny, nz). */
1749
+ alongFace(f, nx, ny, nz) {
1750
+ const e = this.m.makeRotationFromQuaternion(f.mesh.quaternion).elements;
1751
+ const half = [f.half.x, f.half.y, f.half.z];
1752
+ let thin = 0;
1753
+ for (let i = 1; i < 3; i++) if (half[i] < half[thin]) thin = i;
1754
+ const side = e[thin * 4] * nx + e[thin * 4 + 1] * ny + e[thin * 4 + 2] * nz >= 0 ? 1 : -1;
1755
+ this.v.set(e[thin * 4] * side, e[thin * 4 + 1] * side, e[thin * 4 + 2] * side);
1756
+ this.dq.setFromUnitVectors(this.v, this.w.set(nx, ny, nz));
1757
+ f.mesh.quaternion.premultiply(this.dq).normalize();
1758
+ }
1594
1759
  /**
1595
1760
  * The top of the debris lying under a falling fragment: a piece lands on the pile, not through it, so two flat pieces never end up inside each other. Only pieces
1596
1761
  * below it count (a resting one keeps its box), those whose top is at or below where the faller's bottom was a step ago plus a small step.
@@ -1601,6 +1766,7 @@ var FragmentSystem = class {
1601
1766
  const reachX = Math.abs(e[0]) * f.half.x + Math.abs(e[4]) * f.half.y + Math.abs(e[8]) * f.half.z;
1602
1767
  const reachZ = Math.abs(e[2]) * f.half.x + Math.abs(e[6]) * f.half.y + Math.abs(e[10]) * f.half.z;
1603
1768
  const mine = shadowOf(e, f.half);
1769
+ const lowest = p.y - (Math.abs(e[1]) * f.half.x + Math.abs(e[5]) * f.half.y + Math.abs(e[9]) * f.half.z);
1604
1770
  let top = Number.NEGATIVE_INFINITY;
1605
1771
  const under = this.under;
1606
1772
  under.length = 0;
@@ -1609,6 +1775,7 @@ var FragmentSystem = class {
1609
1775
  const box = other.box ?? (other.box = restingBox(other));
1610
1776
  const beneath = !other.asleep && other.mesh.position.y < bottom;
1611
1777
  if (box.top > bottom + PILE_STEP && !beneath || box.top < top - PILE_STEP) continue;
1778
+ if (box.bottom >= lowest - SIDE_BY_SIDE && (other.seq > f.seq || box.bottom > this.pileFloor + SUPPORT_GAP)) continue;
1612
1779
  if (Math.abs(box.x - p.x) >= box.reachX + reachX || Math.abs(box.z - p.z) >= box.reachZ + reachZ) continue;
1613
1780
  if (!shadowsOverlap(mine, box.shadow, box.x - p.x, box.z - p.z)) continue;
1614
1781
  if (box.top > top + PILE_STEP) under.length = 0;
@@ -9180,8 +9347,10 @@ var MIN_FRAGMENTS_PER_SLICE = 8;
9180
9347
  var STATIC_REFRESH = 0.5;
9181
9348
  var STATIC_REFRESH_PER_UPDATE = 8;
9182
9349
  var SURFACE_RAYS = 4;
9183
- var SURFACE_INSIDE = 5e-3;
9350
+ var SURFACE_INSIDE = 1e-3;
9184
9351
  var SURFACE_STEP = 0.15;
9352
+ var MOVER_MIN_MOVE = 1e-3;
9353
+ var MOVER_RAYS = 4;
9185
9354
  var STATIC_SPAN = 3;
9186
9355
  var VERTICES_PER_UPDATE = 3e3;
9187
9356
  var FEET_BAND = 0.03;
@@ -9592,6 +9761,15 @@ var Renderer = class {
9592
9761
  __publicField(this, "stopContactHits", null);
9593
9762
  __publicField(this, "box");
9594
9763
  __publicField(this, "tmpQ");
9764
+ /** The pieces that moved this update, as the fragments see them (`collectMovers`), and the record kept per piece. */
9765
+ __publicField(this, "moving", []);
9766
+ __publicField(this, "movers", /* @__PURE__ */ new WeakMap());
9767
+ __publicField(this, "moverFrom");
9768
+ __publicField(this, "moverTo");
9769
+ __publicField(this, "moverBox");
9770
+ __publicField(this, "moverBoxMoved");
9771
+ /** The box of a geometry's positions, made once (`drawnBoxOf`). */
9772
+ __publicField(this, "geometryBoxes", /* @__PURE__ */ new WeakMap());
9595
9773
  __publicField(this, "v1");
9596
9774
  __publicField(this, "v2");
9597
9775
  __publicField(this, "v3");
@@ -9620,6 +9798,10 @@ var Renderer = class {
9620
9798
  this.workCosts = resolveWorkUnitCosts(o.workBudget?.costs);
9621
9799
  this.box = new THREE.Box3();
9622
9800
  this.tmpQ = new THREE.Quaternion();
9801
+ this.moverFrom = new THREE.Matrix4();
9802
+ this.moverTo = new THREE.Matrix4();
9803
+ this.moverBox = new THREE.Box3();
9804
+ this.moverBoxMoved = new THREE.Box3();
9623
9805
  this.v1 = new THREE.Vector3();
9624
9806
  this.v2 = new THREE.Vector3();
9625
9807
  this.v3 = new THREE.Vector3();
@@ -9649,6 +9831,8 @@ var Renderer = class {
9649
9831
  sweep: (from, direction, distance) => typeof this.world.raycast === "function" ? this.world.raycast([from[0], from[1], from[2]], [direction[0], direction[1], direction[2]], distance) : null,
9650
9832
  maxAwake: o.maxAwakeFragments ?? Infinity,
9651
9833
  ...o.interpolate ? { interpolate: true } : {},
9834
+ // the loose pieces that moved: the scraps on and in them go with them, and a part coming at a scrap meets it
9835
+ movers: () => this.moving,
9652
9836
  rigid: {
9653
9837
  add: (mesh, half, mass, velocity, angular) => {
9654
9838
  const body = this.pieceSys.create(`${DEBRIS_ID}${++this.debrisMade}`, mesh, [half[0], half[1], half[2]], new THREE.Vector3(), mass, [velocity[0], velocity[1], velocity[2]], [angular[0], angular[1], angular[2]]);
@@ -10252,6 +10436,8 @@ var Renderer = class {
10252
10436
  this.pieceSys.update(dt);
10253
10437
  this.measureCost();
10254
10438
  this.updateFading(dt);
10439
+ this.collectMovers();
10440
+ this.syncTransforms();
10255
10441
  this.fragments.update(dt);
10256
10442
  this.updateActive(dt);
10257
10443
  this.updateLeaks(dt);
@@ -11753,6 +11939,7 @@ var Renderer = class {
11753
11939
  }
11754
11940
  }
11755
11941
  this.bounds.set(newId, nb);
11942
+ nb.lastWorld = Float64Array.from(group.matrixWorld.elements);
11756
11943
  for (const pv of nb.parts.values()) this.tagPart(pv, newId);
11757
11944
  this.followSplit(from, nb);
11758
11945
  const meshes = this.meshesOf(nb);
@@ -12054,6 +12241,103 @@ var Renderer = class {
12054
12241
  }
12055
12242
  return "";
12056
12243
  }
12244
+ /**
12245
+ * The loose pieces that moved since the world was last told where they are (`FragmentSystemOptions.movers`), read before it is told: each with the box it took then and
12246
+ * takes now, its move, and a ray through its own parts.
12247
+ */
12248
+ collectMovers() {
12249
+ const out = this.moving;
12250
+ out.length = 0;
12251
+ for (const b of this.bounds.values()) {
12252
+ const piece = b.piece;
12253
+ const was = b.lastWorld;
12254
+ if (!piece || piece.sleeping || !b.sync || !was || !this.world.has(b.id)) continue;
12255
+ b.root.updateWorldMatrix(true, false);
12256
+ const now = b.root.matrixWorld.elements;
12257
+ let same2 = true;
12258
+ for (let i = 0; i < 16 && same2; i++) same2 = now[i] === was[i];
12259
+ if (same2) continue;
12260
+ let solid = this.movers.get(b);
12261
+ if (!solid) {
12262
+ solid = this.moverOf(b.id);
12263
+ this.movers.set(b, solid);
12264
+ }
12265
+ const before = this.moverFrom.fromArray(was);
12266
+ this.moverTo.copy(b.root.matrixWorld).multiply(before.invert()).toArray(solid.delta);
12267
+ const box = solid.box;
12268
+ box.fill(Infinity, 0, 3);
12269
+ box.fill(-Infinity, 3, 6);
12270
+ this.drawnBoxOf(b.root, this.moverBox);
12271
+ const back = this.moverFrom.fromArray(solid.delta).invert();
12272
+ if (this.farthestCorner(back) < MOVER_MIN_MOVE) continue;
12273
+ this.growBox(box);
12274
+ this.growBox(box, back);
12275
+ out.push(solid);
12276
+ }
12277
+ }
12278
+ /**
12279
+ * The world box of what is drawn under `root`, from the box of each mesh's positions, kept per geometry here: never by `Box3.setFromObject`, which writes a bounding box
12280
+ * into each geometry it reads and so changes what the piece is made of for anyone who reads it whole.
12281
+ */
12282
+ drawnBoxOf(root, out) {
12283
+ out.makeEmpty();
12284
+ root.updateWorldMatrix(true, true);
12285
+ root.traverseVisible((node) => {
12286
+ const mesh = node;
12287
+ const position = mesh.isMesh ? mesh.geometry?.getAttribute("position") : void 0;
12288
+ if (!position) return;
12289
+ let local = this.geometryBoxes.get(mesh.geometry);
12290
+ if (!local) {
12291
+ local = new this.THREE.Box3().setFromBufferAttribute(position);
12292
+ this.geometryBoxes.set(mesh.geometry, local);
12293
+ }
12294
+ out.union(this.moverBoxMoved.copy(local).applyMatrix4(mesh.matrixWorld));
12295
+ });
12296
+ }
12297
+ /** How far (m) the corner of `moverBox` that moves most goes under `matrix`. */
12298
+ farthestCorner(matrix) {
12299
+ if (this.moverBox.isEmpty()) return 0;
12300
+ const { min, max } = this.moverBox;
12301
+ let most = 0;
12302
+ for (let i = 0; i < 8; i++) {
12303
+ const corner = this.v1.set(i & 1 ? max.x : min.x, i & 2 ? max.y : min.y, i & 4 ? max.z : min.z);
12304
+ const x = corner.x, y = corner.y, z = corner.z;
12305
+ corner.applyMatrix4(matrix);
12306
+ most = Math.max(most, Math.hypot(corner.x - x, corner.y - y, corner.z - z));
12307
+ }
12308
+ return most;
12309
+ }
12310
+ /** `box` grown by `moverBox`, under `matrix` when one is given. */
12311
+ growBox(box, matrix) {
12312
+ if (this.moverBox.isEmpty()) return;
12313
+ const at = matrix ? this.moverBoxMoved.copy(this.moverBox).applyMatrix4(matrix) : this.moverBox;
12314
+ box[0] = Math.min(box[0], at.min.x);
12315
+ box[1] = Math.min(box[1], at.min.y);
12316
+ box[2] = Math.min(box[2], at.min.z);
12317
+ box[3] = Math.max(box[3], at.max.x);
12318
+ box[4] = Math.max(box[4], at.max.y);
12319
+ box[5] = Math.max(box[5], at.max.z);
12320
+ }
12321
+ /** A moving piece as the fragments see it, made once per bound object: its sweep meets only its own parts, the first of them past whatever else is on the way. */
12322
+ moverOf(objectId) {
12323
+ return {
12324
+ box: [0, 0, 0, 0, 0, 0],
12325
+ delta: new Array(16).fill(0),
12326
+ sweep: (from, direction, distance) => {
12327
+ let start = [from[0], from[1], from[2]];
12328
+ let left = distance;
12329
+ for (let k = 0; k < MOVER_RAYS && left > 0; k++) {
12330
+ const hit = this.world.raycast(start, [direction[0], direction[1], direction[2]], left);
12331
+ if (!hit) return null;
12332
+ if (hit.object === objectId) return { point: [start[0] + direction[0] * hit.distance, start[1] + direction[1] * hit.distance, start[2] + direction[2] * hit.distance], normal: hit.normal };
12333
+ const past = hit.distance + SURFACE_INSIDE;
12334
+ start = [start[0] + direction[0] * past, start[1] + direction[1] * past, start[2] + direction[2] * past];
12335
+ left -= past;
12336
+ }
12337
+ return null;
12338
+ }
12339
+ };
12340
+ }
12057
12341
  /** The top of the ground or of a standing part of the world under (x, z), below height y: a ray straight down through the destruction world. */
12058
12342
  surfaceAt(x, y, z) {
12059
12343
  const ground = this.groundAt(x, z);