@playcanvas/supersplat-viewer 1.38.0 → 1.38.1

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/viewer.js CHANGED
@@ -475,12 +475,6 @@ class MeshCollision {
475
475
  queryCapsule(cx, cy, cz, halfHeight, radius, out) {
476
476
  return resolveIterative(cx, cy, cz, (rx, ry, rz, push) => this._deepestCapsulePenetration(rx, ry, rz, halfHeight, radius, push), this._constraintNormals, this._push, out);
477
477
  }
478
- isFreeAt(x, y, z) {
479
- // Approximation: a point is in free space iff no triangle is within
480
- // half a voxel of it. Matches the voxel-grid notion for data derived
481
- // from the same carve.
482
- return !this._deepestSpherePenetration(x, y, z, this.voxelResolution * 0.5, this._push);
483
- }
484
478
  querySurfaceNormal(x, y, z, rdx, rdy, rdz) {
485
479
  const len = Math.sqrt(rdx * rdx + rdy * rdy + rdz * rdz);
486
480
  if (len < 1e-10) {
@@ -1017,24 +1011,6 @@ class VoxelCollision {
1017
1011
  get flipXY() {
1018
1012
  return false;
1019
1013
  }
1020
- isFreeAt(x, y, z) {
1021
- // Without voxel data, there's no carve to consult — report not-free
1022
- // rather than have the lattice search treat the world as wide open.
1023
- if (this._nodes.length === 0) {
1024
- return false;
1025
- }
1026
- const res = this._voxelResolution;
1027
- const ix = Math.floor((x - this._gridMinX) / res);
1028
- const iy = Math.floor((y - this._gridMinY) / res);
1029
- const iz = Math.floor((z - this._gridMinZ) / res);
1030
- // Outside the carved grid is "no data" — also not-free. Without this
1031
- // check the lattice search would spend candidates on out-of-bounds
1032
- // cells (and `isVoxelSolid` would mislead callers by returning false).
1033
- if (ix < 0 || iy < 0 || iz < 0 || ix >= this._numVoxelsX || iy >= this._numVoxelsY || iz >= this._numVoxelsZ) {
1034
- return false;
1035
- }
1036
- return !this.isVoxelSolid(ix, iy, iz);
1037
- }
1038
1014
  querySurfaceNormal(x, y, z, rdx, rdy, rdz) {
1039
1015
  const nudge = this._voxelResolution * 0.25;
1040
1016
  const ix = Math.floor((x + Math.sign(rdx) * nudge - this._gridMinX) / this._voxelResolution);
@@ -1071,7 +1047,7 @@ class VoxelCollision {
1071
1047
  result.nz = bestNz;
1072
1048
  return result;
1073
1049
  }
1074
- queryRay(ox, oy, oz, dx, dy, dz, maxDist) {
1050
+ queryRay(ox, oy, oz, dx, dy, dz, maxDist, exitSolid = false) {
1075
1051
  if (this._nodes.length === 0) {
1076
1052
  return null;
1077
1053
  }
@@ -1163,12 +1139,15 @@ class VoxelCollision {
1163
1139
  const tDeltaZ = Math.abs(dz) > EPS ? res * Math.abs(invDz) : Infinity;
1164
1140
  let currentT = tNear;
1165
1141
  const maxSteps = this._numVoxelsX + this._numVoxelsY + this._numVoxelsZ;
1142
+ // a ray from inside solid looks for the first free voxel instead, where it leaves
1143
+ const inside = exitSolid && tNear === 0 && this.isVoxelSolid(ix, iy, iz);
1166
1144
  for (let step = 0; step < maxSteps; step++) {
1167
- if (this.isVoxelSolid(ix, iy, iz)) {
1145
+ if (this.isVoxelSolid(ix, iy, iz) !== inside) {
1168
1146
  return {
1169
1147
  x: ox + dx * currentT,
1170
1148
  y: oy + dy * currentT,
1171
- z: oz + dz * currentT
1149
+ z: oz + dz * currentT,
1150
+ exit: inside
1172
1151
  };
1173
1152
  }
1174
1153
  // Advance along the axis with the smallest tMax
@@ -1194,13 +1173,22 @@ class VoxelCollision {
1194
1173
  iz += stepZ;
1195
1174
  tMaxZ += tDeltaZ;
1196
1175
  }
1197
- if (ix < 0 ||
1176
+ const outside = ix < 0 ||
1198
1177
  iy < 0 ||
1199
1178
  iz < 0 ||
1200
1179
  ix >= this._numVoxelsX ||
1201
1180
  iy >= this._numVoxelsY ||
1202
- iz >= this._numVoxelsZ ||
1203
- currentT > maxDist) {
1181
+ iz >= this._numVoxelsZ;
1182
+ // past the grid is free, so a ray inside solid that reaches its edge leaves there
1183
+ if (outside && inside && currentT <= maxDist) {
1184
+ return {
1185
+ x: ox + dx * currentT,
1186
+ y: oy + dy * currentT,
1187
+ z: oz + dz * currentT,
1188
+ exit: true
1189
+ };
1190
+ }
1191
+ if (outside || currentT > maxDist) {
1204
1192
  return null;
1205
1193
  }
1206
1194
  }
@@ -1606,8 +1594,8 @@ class FlippedVoxelCollision extends VoxelCollision {
1606
1594
  result.ny = -result.ny;
1607
1595
  return result;
1608
1596
  }
1609
- queryRay(ox, oy, oz, dx, dy, dz, maxDist) {
1610
- const hit = super.queryRay(-ox, -oy, oz, -dx, -dy, dz, maxDist);
1597
+ queryRay(ox, oy, oz, dx, dy, dz, maxDist, exitSolid = false) {
1598
+ const hit = super.queryRay(-ox, -oy, oz, -dx, -dy, dz, maxDist, exitSolid);
1611
1599
  if (hit) {
1612
1600
  hit.x = -hit.x;
1613
1601
  hit.y = -hit.y;
@@ -1630,9 +1618,6 @@ class FlippedVoxelCollision extends VoxelCollision {
1630
1618
  }
1631
1619
  return result;
1632
1620
  }
1633
- isFreeAt(x, y, z) {
1634
- return super.isFreeAt(-x, -y, z);
1635
- }
1636
1621
  }
1637
1622
  /**
1638
1623
  * Load a VoxelCollision from a .voxel.json URL.
@@ -9428,7 +9413,7 @@ const importSettings = (settings) => {
9428
9413
  return result;
9429
9414
  };
9430
9415
 
9431
- var version = "1.38.0";
9416
+ var version = "1.38.1";
9432
9417
 
9433
9418
  class Tooltip {
9434
9419
  register;
@@ -14713,14 +14698,6 @@ class SpawnState {
14713
14698
  this._distance = distance;
14714
14699
  this._has = true;
14715
14700
  }
14716
- /**
14717
- * Forget any previously-stored spawn pose so a subsequent `has` check
14718
- * reports false. Used by controllers that scope spawn to a single mode
14719
- * entry (e.g. walk).
14720
- */
14721
- clear() {
14722
- this._has = false;
14723
- }
14724
14701
  /**
14725
14702
  * Copy the captured pose into the supplied targets. Caller must check
14726
14703
  * `has` first; calling `restore` before `store` returns the field
@@ -14924,13 +14901,6 @@ class SphereMover {
14924
14901
  }
14925
14902
  }
14926
14903
 
14927
- /**
14928
- * Maximum Euclidean distance (metres) from the spawn origin to search for a valid placement.
14929
- * Used by the cylinder search, which has to reach far enough to find a standable floor. The
14930
- * sphere search sizes itself from its own radius instead — see `SPHERE_SEARCH_REACH_RADII`.
14931
- */
14932
- const SEARCH_RADIUS = 5;
14933
- const SEARCH_RADIUS_SQ = SEARCH_RADIUS * SEARCH_RADIUS;
14934
14904
  /**
14935
14905
  * Reach of the sphere search, in multiples of the sphere's radius.
14936
14906
  *
@@ -14938,7 +14908,7 @@ const SEARCH_RADIUS_SQ = SEARCH_RADIUS * SEARCH_RADIUS;
14938
14908
  * inside geometry — `FlyController` holds collision until the camera is clear rather than relying
14939
14909
  * on a rescue — so reaching a few radii is enough. Bounding the reach is what makes the search
14940
14910
  * cheap: at radius 0.2 on a 0.05 m grid the worst case, finding nothing, is 7,153 probes, where
14941
- * reaching `SEARCH_RADIUS` cost 8,120,601. A sphere deep inside geometry never finds a free cell
14911
+ * reaching 5 m cost 8,120,601. A sphere deep inside geometry never finds a free cell
14942
14912
  * to tighten `bestDistSq`, so nothing breaks the search early and it pays for every shell.
14943
14913
  *
14944
14914
  * The lattice still steps by `collision.voxelResolution`, deliberately. A coarser step is not
@@ -14948,8 +14918,6 @@ const SEARCH_RADIUS_SQ = SEARCH_RADIUS * SEARCH_RADIUS;
14948
14918
  * placement at all.
14949
14919
  */
14950
14920
  const SPHERE_SEARCH_REACH_RADII = 3;
14951
- /** Ray budget when probing for ground/ceiling under or above a candidate column. */
14952
- const RAY_MAX_DIST = 1000;
14953
14921
  // Scratch values reused across calls — these helpers run only at spawn time
14954
14922
  // so the lack of re-entrancy is fine.
14955
14923
  const scratchPush = { x: 0, y: 0, z: 0 };
@@ -15013,115 +14981,6 @@ const findSphereSpawn = (collision, ox, oy, oz, radius, out) => {
15013
14981
  }
15014
14982
  return found;
15015
14983
  };
15016
- /**
15017
- * Find the closest standable cylinder placement to (ox, oy, oz). Output is the
15018
- * floor world position the cylinder rests on (highest geometry hit across the
15019
- * footprint). Used for walk-camera spawn.
15020
- *
15021
- * The algorithm:
15022
- *
15023
- * 1. Lattice search for the closest free voxel `C` (per `isFreeAt`) that has
15024
- * geometry somewhere below — i.e. a downward ray from `C` hits.
15025
- * 2. At `(C.x, C.z)`, fan rays up and down from `C.y` through every cell
15026
- * (step = `collision.voxelResolution`) within the xz footprint disc.
15027
- * Compute, across columns:
15028
- * - `floor = max(yDown_i)` (highest ground in the footprint)
15029
- * - `ceiling = min(yUp_i)` (lowest obstruction overhead, or ∞)
15030
- * Every column must have a `yDown` hit; otherwise the cylinder is partly
15031
- * unsupported and the candidate is discarded.
15032
- * 3. The cylinder fits iff `floor + halfHeight ≤ ceiling - halfHeight`,
15033
- * i.e. enough vertical clearance for the full cylinder above the floor.
15034
- * The "resting on ground" placement has centre at `floor + halfHeight`.
15035
- *
15036
- * Cylinder math (flat ends, no hemispheres) intentionally matches the carve,
15037
- * which used a separable XYZ dilation.
15038
- *
15039
- * @param collision - Active collision implementation.
15040
- * @param ox - Origin X (world space).
15041
- * @param oy - Origin Y (world space).
15042
- * @param oz - Origin Z (world space).
15043
- * @param halfHeight - Cylinder half-height (full height = 2 × halfHeight).
15044
- * @param radius - Cylinder radius.
15045
- * @param out - Receives the floor position the cylinder rests on.
15046
- * @returns True if a valid placement was found.
15047
- */
15048
- const findCylinderSpawn = (collision, ox, oy, oz, halfHeight, radius, out) => {
15049
- const step = collision.voxelResolution;
15050
- const maxCells = Math.ceil(SEARCH_RADIUS / step);
15051
- // Round up so float division (e.g. 0.2 / 0.05) can't accidentally drop
15052
- // the outer ring of footprint cells; the Euclidean check below trims any
15053
- // overshoot back to the true radius.
15054
- const footCells = Math.ceil(radius / step);
15055
- const radiusSq = radius * radius;
15056
- let bestDistSq = Infinity;
15057
- let found = false;
15058
- for (let r = 0; r <= maxCells; r++) {
15059
- const shellMinDistSq = r * step * (r * step);
15060
- if (shellMinDistSq >= bestDistSq)
15061
- break;
15062
- for (let dy = -r; dy <= r; dy++) {
15063
- const absDy = dy < 0 ? -dy : dy;
15064
- for (let dz = -r; dz <= r; dz++) {
15065
- const absDz = dz < 0 ? -dz : dz;
15066
- // Enumerate the Chebyshev shell of radius r directly rather than scanning the
15067
- // whole cube and skipping its interior — see `findSphereSpawn`.
15068
- const full = absDy === r || absDz === r;
15069
- const dxStep = full ? 1 : 2 * r;
15070
- for (let dx = -r; dx <= r; dx += dxStep) {
15071
- const distSq = (dx * dx + dy * dy + dz * dz) * step * step;
15072
- if (distSq >= bestDistSq || distSq > SEARCH_RADIUS_SQ)
15073
- continue;
15074
- const cx = ox + dx * step;
15075
- const cy = oy + dy * step;
15076
- const cz = oz + dz * step;
15077
- // Stage 1: cheap filter — only consider free voxels.
15078
- // The footprint loop below will reject candidates whose
15079
- // center column has no ground support, so no separate
15080
- // down-ray probe is needed here.
15081
- if (!collision.isFreeAt(cx, cy, cz))
15082
- continue;
15083
- // Stage 2/3: fan rays through the xz footprint.
15084
- let floor = -Infinity;
15085
- let ceiling = Infinity;
15086
- let supported = true;
15087
- for (let i = -footCells; i <= footCells && supported; i++) {
15088
- const fxOff = i * step;
15089
- const fxOffSq = fxOff * fxOff;
15090
- for (let j = -footCells; j <= footCells; j++) {
15091
- const fzOff = j * step;
15092
- if (fxOffSq + fzOff * fzOff > radiusSq)
15093
- continue;
15094
- const fx = cx + fxOff;
15095
- const fz = cz + fzOff;
15096
- const down = collision.queryRay(fx, cy, fz, 0, -1, 0, RAY_MAX_DIST);
15097
- if (!down) {
15098
- supported = false;
15099
- break;
15100
- }
15101
- if (down.y > floor)
15102
- floor = down.y;
15103
- const up = collision.queryRay(fx, cy, fz, 0, 1, 0, RAY_MAX_DIST);
15104
- if (up && up.y < ceiling)
15105
- ceiling = up.y;
15106
- }
15107
- }
15108
- if (!supported)
15109
- continue;
15110
- // Cylinder vertical extent = 2 × halfHeight. Need that
15111
- // much room between floor and ceiling.
15112
- if (floor + 2 * halfHeight > ceiling)
15113
- continue;
15114
- bestDistSq = distSq;
15115
- out.x = cx;
15116
- out.y = floor;
15117
- out.z = cz;
15118
- found = true;
15119
- }
15120
- }
15121
- }
15122
- }
15123
- return found;
15124
- };
15125
14984
 
15126
14985
  /** Radius of the camera collision sphere (meters) */
15127
14986
  const CAMERA_RADIUS = 0.2;
@@ -15129,7 +14988,7 @@ const forward$2 = new Vec3();
15129
14988
  const right$2 = new Vec3();
15130
14989
  const up$1 = new Vec3();
15131
14990
  const offset$1 = new Vec3();
15132
- const spawnProbe$1 = new Vec3();
14991
+ const spawnProbe = new Vec3();
15133
14992
  const clearProbe = { x: 0, y: 0, z: 0 };
15134
14993
  // Whether the camera sphere fits at `position` without intersecting collision geometry.
15135
14994
  const isClear = (collision, position) => !collision.querySphere(position.x, position.y, position.z, CAMERA_RADIUS, clearProbe);
@@ -15192,8 +15051,8 @@ class FlyController {
15192
15051
  const collision = this._mover.collision ?? this._pendingCollision;
15193
15052
  this._pendingCollision = null;
15194
15053
  if (collision) {
15195
- if (findSphereSpawn(collision, this._position.x, this._position.y, this._position.z, CAMERA_RADIUS, spawnProbe$1)) {
15196
- this._position.copy(spawnProbe$1);
15054
+ if (findSphereSpawn(collision, this._position.x, this._position.y, this._position.z, CAMERA_RADIUS, spawnProbe)) {
15055
+ this._position.copy(spawnProbe);
15197
15056
  }
15198
15057
  // Engage at the placed position. The search starts at the camera's own cell, so
15199
15058
  // failing it means the camera is inside geometry with no free space within reach to
@@ -15518,15 +15377,41 @@ class OrbitController {
15518
15377
 
15519
15378
  const FIXED_DT = 1 / 60;
15520
15379
  const MAX_SUBSTEPS = 10;
15380
+ /**
15381
+ * How far below the capsule a floor must be for collision to engage. With none in reach the
15382
+ * capsule is over a hole in the collision, or far above anything it could land on, and engaged
15383
+ * collision would drop it out of the scene, so collision is held instead.
15384
+ */
15385
+ const FLOOR_SEARCH_DIST = 10;
15386
+ /**
15387
+ * How far below the camera entry looks for a floor to stand on. Unbounded in practice, unlike
15388
+ * `FLOOR_SEARCH_DIST`: entering from high up, from an orbit view, should land on the floor below.
15389
+ */
15390
+ const ENTRY_FLOOR_DIST = 1000;
15391
+ /**
15392
+ * How far above the camera entry looks for a floor, when there is none to stand on below it: a
15393
+ * camera sunk into the ground or just under it stands on the surface above instead.
15394
+ */
15395
+ const ENTRY_RISE_REACH = 2;
15396
+ /**
15397
+ * Spacing of the rays that look for a floor above the camera. Each is cast down from its own
15398
+ * height, so a surface between two floors cannot hide the lower one from all of them.
15399
+ */
15400
+ const ENTRY_RISE_STEP = 0.25;
15401
+ /**
15402
+ * Reach of the entry un-clip, in capsule radii, as `FlyController`'s: enough to free a capsule
15403
+ * that clips a surface, not to relocate one deep inside geometry, which holds collision instead.
15404
+ */
15405
+ const UNCLIP_REACH_RADII = 3;
15521
15406
  /** Pre-allocated push-out vector for capsule collision */
15522
15407
  const out = { x: 0, y: 0, z: 0 };
15523
15408
  const v = new Vec3();
15524
15409
  const d = new Vec3();
15410
+ const nudged = new Vec3();
15525
15411
  const forward = new Vec3();
15526
15412
  const right$1 = new Vec3();
15527
15413
  const moveStep = [0, 0, 0];
15528
15414
  const offset = new Vec3();
15529
- const spawnProbe = new Vec3();
15530
15415
  /**
15531
15416
  * First-person camera controller with spring-damper suspension over collision terrain.
15532
15417
  *
@@ -15604,7 +15489,6 @@ class WalkController {
15604
15489
  _targetAngles = new Vec3();
15605
15490
  _distance = 1;
15606
15491
  _spawn = new SpawnState();
15607
- _spawnGrounded = false;
15608
15492
  _velocity = new Vec3();
15609
15493
  _pendingMove = [0, 0, 0];
15610
15494
  _accumulator = 0;
@@ -15613,10 +15497,12 @@ class WalkController {
15613
15497
  _jumpHeld = false;
15614
15498
  _collision = null;
15615
15499
  /**
15616
- * Collision held because the capsule is inside geometry, mirroring `FlyController`. While
15617
- * held, `_heldFloorY` stands in for the ground probe so the camera walks on the level it
15618
- * entered at instead of falling, and the capsule check is skipped. `_step` engages it for
15619
- * real as soon as the capsule fits.
15500
+ * Collision held because the capsule is inside geometry, mirroring `FlyController`, or
15501
+ * because there is no floor within `FLOOR_SEARCH_DIST` below it, where engaged collision
15502
+ * would fall out of the scene.
15503
+ * While held, `_heldFloorY` stands in for the ground probe so the camera walks on the level
15504
+ * it was held at instead of falling, and the capsule check is skipped. `_step` engages it for
15505
+ * real as soon as the capsule fits over a floor.
15620
15506
  */
15621
15507
  _pendingCollision = null;
15622
15508
  /**
@@ -15631,15 +15517,14 @@ class WalkController {
15631
15517
  _hasPosition = false;
15632
15518
  /**
15633
15519
  * Optional collision for capsule collision with sliding. Assigning while the capsule is
15634
- * inside geometry holds the attachment instead: engaged collision cannot escape a solid
15635
- * region, so the capsule keeps the synthetic floor and `_step` engages it once it fits.
15636
- * Collision normally lands this way, after walk mode may already be active.
15520
+ * inside geometry, or over no floor in reach, holds the attachment instead: engaged collision
15521
+ * cannot escape a solid region and has nothing to land on over a void, so the capsule keeps
15522
+ * the synthetic floor and `_step` engages it once it fits over a floor. Collision normally
15523
+ * lands this way, after walk mode may already be active.
15637
15524
  */
15638
15525
  set collision(value) {
15639
- if (value && this._hasPosition && !this._capsuleClear(value, this._position)) {
15640
- this._pendingCollision = value;
15641
- this._collision = null;
15642
- this._heldFloorY = this._position.y - this.eyeHeight - this.hoverHeight;
15526
+ if (value && this._hasPosition && !this._canEngage(value, this._position)) {
15527
+ this._hold(value);
15643
15528
  return;
15644
15529
  }
15645
15530
  this._pendingCollision = null;
@@ -15651,47 +15536,26 @@ class WalkController {
15651
15536
  }
15652
15537
  onEnter(camera) {
15653
15538
  this.goto(camera);
15654
- // Spawn is scoped to this walk-mode entry; reset so a stale spawn from a previous entry
15655
- // can't be restored if this entry fails or has no collision to place against.
15656
- this._spawn.clear();
15657
15539
  // Entry may follow a previous one that ended with collision held, so start from the
15658
15540
  // collision we have either way. Whether to hold again is decided fresh, from this
15659
15541
  // entry's position.
15660
15542
  const collision = this._collision ?? this._pendingCollision;
15661
- // Stand on the entry level until collision can place the capsule. This covers collision
15662
- // that has not arrived yet as well as a spawn search that finds nowhere to go: engaged
15663
- // collision would strand the camera either way, with no floor under the ground probe and
15664
- // nothing for push-out to push against. Held, the user can walk out the way they came,
15665
- // and the setter or `_step` engages collision once the capsule fits.
15543
+ // Stand on the entry level until collision can engage. This covers collision that has not
15544
+ // arrived yet as well as an entry inside geometry or over no floor, where engaged
15545
+ // collision would strand the camera or drop it. Held, the user can walk out the way they
15546
+ // came, and the setter or `_step` engages collision once the capsule fits over a floor.
15666
15547
  this._pendingCollision = null;
15667
15548
  this._collision = null;
15668
15549
  this._heldFloorY = this._position.y - this.eyeHeight - this.hoverHeight;
15669
15550
  this._grounded = true;
15670
15551
  if (collision) {
15671
- // Treat the walk capsule as a tight-superset cylinder for spawn.
15672
- // The carve was produced with a separable XYZ dilation (flat ends,
15673
- // no hemispheres), so cylinder math matches the data exactly.
15674
- // Include `hoverHeight` in the height so the ceiling-clearance
15675
- // check accounts for the placed capsule's full vertical envelope:
15676
- // foot sits at `floor + hoverHeight`, head at `floor + hoverHeight
15677
- // + capsuleHeight`.
15678
- if (findCylinderSpawn(collision, camera.position.x, camera.position.y, camera.position.z, (this.capsuleHeight + this.hoverHeight) * 0.5, this.capsuleRadius, spawnProbe)) {
15679
- // spawnProbe is the floor world position the cylinder rests
15680
- // on. Eye sits hoverHeight + eyeHeight above the floor.
15681
- this._position.set(spawnProbe.x, spawnProbe.y + this.hoverHeight + this.eyeHeight, spawnProbe.z);
15682
- this._storeSpawn();
15683
- // The placement is known to fit, so engage directly rather than re-testing it
15684
- // through the setter's capsule query, which is a different test from the
15685
- // cylinder search and could hold on a hairline disagreement.
15686
- this._heldFloorY = null;
15687
- this._collision = collision;
15688
- }
15689
- else {
15690
- // No placement within reach. The setter holds if the capsule is inside geometry
15691
- // and engages otherwise, which is what `_step` would do on its first step anyway.
15692
- this.collision = collision;
15552
+ if (!this._standOnFloor(collision)) {
15553
+ this._fit(collision, this._position);
15693
15554
  }
15555
+ this.collision = collision;
15694
15556
  }
15557
+ // reset returns to where walk was entered
15558
+ this._spawn.store(this._position, this._angles, this._distance);
15695
15559
  this._prevPosition.copy(this._position);
15696
15560
  }
15697
15561
  update(deltaTime, inputFrame, camera) {
@@ -15726,14 +15590,20 @@ class WalkController {
15726
15590
  camera.fov = this.fov;
15727
15591
  }
15728
15592
  _step(dt, move) {
15729
- // engage a held attachment as soon as the capsule reaches space it fits in
15730
- if (this._pendingCollision && this._capsuleClear(this._pendingCollision, this._position)) {
15593
+ // engage a held attachment as soon as the capsule reaches space it fits in, over a floor
15594
+ if (this._pendingCollision && this._canEngage(this._pendingCollision, this._position)) {
15731
15595
  this._collision = this._pendingCollision;
15732
15596
  this._pendingCollision = null;
15733
15597
  this._heldFloorY = null;
15734
15598
  }
15735
15599
  // ground probe: cast a ray downward to find the terrain surface
15736
- const groundY = this._probeGround(this._position);
15600
+ let groundY = this._probeGround(this._position);
15601
+ // airborne with no floor in reach, over a hole in the collision, would fall out of the
15602
+ // scene: hold collision and walk on at this level until there is floor below again
15603
+ if (groundY === null && this._collision && !this._floorBelow(this._collision, this._position)) {
15604
+ this._hold(this._collision);
15605
+ groundY = this._heldFloorY;
15606
+ }
15737
15607
  const hasGround = groundY !== null;
15738
15608
  // jump (require release before re-triggering)
15739
15609
  if (this._velocity.y < 0) {
@@ -15816,17 +15686,18 @@ class WalkController {
15816
15686
  this._prevPosition.copy(this._position);
15817
15687
  this._targetAngles.copy(this._angles);
15818
15688
  this._resetMotion();
15819
- this._grounded = this._spawnGrounded;
15689
+ // decide afresh at the spawn whether to engage or hold, since a held floor belongs to
15690
+ // wherever it was held, mirroring `FlyController`
15691
+ const collision = this._collision ?? this._pendingCollision;
15692
+ if (collision) {
15693
+ this.collision = collision;
15694
+ }
15820
15695
  camera.position.copy(this._position);
15821
15696
  camera.angles.copy(this._angles);
15822
15697
  camera.distance = this._distance;
15823
15698
  camera.fov = this.fov;
15824
15699
  return true;
15825
15700
  }
15826
- _storeSpawn() {
15827
- this._spawn.store(this._position, this._angles, this._distance);
15828
- this._spawnGrounded = this._grounded;
15829
- }
15830
15701
  _resetMotion() {
15831
15702
  this._velocity.set(0, 0, 0);
15832
15703
  this._grounded = false;
@@ -15849,6 +15720,96 @@ class WalkController {
15849
15720
  const half = this.capsuleHeight * 0.5 - this.capsuleRadius;
15850
15721
  return !collision.queryCapsule(pos.x, center, pos.z, half, this.capsuleRadius, out);
15851
15722
  }
15723
+ /**
15724
+ * Whether there is a floor within `FLOOR_SEARCH_DIST` below the capsule.
15725
+ *
15726
+ * @param collision - Collision to test against.
15727
+ * @param pos - Eye position in PlayCanvas world space.
15728
+ * @returns True if a downward ray from the capsule's foot hits geometry in reach.
15729
+ */
15730
+ _floorBelow(collision, pos) {
15731
+ return !!collision.queryRay(pos.x, pos.y - this.eyeHeight, pos.z, 0, -1, 0, FLOOR_SEARCH_DIST);
15732
+ }
15733
+ _canEngage(collision, pos) {
15734
+ return this._capsuleClear(collision, pos) && this._floorBelow(collision, pos);
15735
+ }
15736
+ /**
15737
+ * Put the eye at standing height on a floor, so the capsule neither sinks into the ground nor
15738
+ * hangs over it: the floor below the camera however far down, for a camera flown onto the
15739
+ * ground or entering from above, and failing that the nearest floor up to `ENTRY_RISE_REACH`
15740
+ * above it, for a camera sunk into the ground or just under it. Below comes first, or an
15741
+ * entry from high up could stand on a roof. A floor counts only if the capsule fits standing
15742
+ * on it, after `_fit`; with none, the camera stays where it is.
15743
+ *
15744
+ * @param collision - Collision to test against.
15745
+ * @returns True if the camera was moved onto a floor.
15746
+ */
15747
+ _standOnFloor(collision) {
15748
+ const pos = this._position;
15749
+ if (this._tryStandFrom(collision, pos.y, ENTRY_FLOOR_DIST))
15750
+ return true;
15751
+ for (let rise = ENTRY_RISE_STEP; rise <= ENTRY_RISE_REACH + 1e-6; rise += ENTRY_RISE_STEP) {
15752
+ if (this._tryStandFrom(collision, pos.y + rise, rise))
15753
+ return true;
15754
+ }
15755
+ return false;
15756
+ }
15757
+ /**
15758
+ * Stand on the floor a downward ray from `fromY` finds within `maxDist`, if the capsule fits
15759
+ * there once `_fit` has freed it from a wall it clips. A ray that hits at its origin started
15760
+ * inside geometry and finds no floor.
15761
+ *
15762
+ * @param collision - Collision to test against.
15763
+ * @param fromY - Height to cast from, above the camera's column.
15764
+ * @param maxDist - Reach of the ray.
15765
+ * @returns True if the camera was moved onto a floor.
15766
+ */
15767
+ _tryStandFrom(collision, fromY, maxDist) {
15768
+ const pos = this._position;
15769
+ const hit = collision.queryRay(pos.x, fromY, pos.z, 0, -1, 0, maxDist);
15770
+ if (!hit || hit.y >= fromY)
15771
+ return false;
15772
+ v.set(pos.x, hit.y + this.hoverHeight + this.eyeHeight, pos.z);
15773
+ if (!this._fit(collision, v))
15774
+ return false;
15775
+ pos.copy(v);
15776
+ return true;
15777
+ }
15778
+ /**
15779
+ * Fit the capsule at `p`, freeing it from a surface it clips: move it by the push-out of one
15780
+ * capsule query, if that is within `UNCLIP_REACH_RADII` and leaves it clear. Otherwise `p` is
15781
+ * left as it was.
15782
+ *
15783
+ * @param collision - Collision to test against.
15784
+ * @param p - Eye position, moved if the capsule had to be freed.
15785
+ * @returns True if the capsule fits at `p`.
15786
+ */
15787
+ _fit(collision, p) {
15788
+ const center = p.y - this.eyeHeight + this.capsuleHeight * 0.5;
15789
+ const half = this.capsuleHeight * 0.5 - this.capsuleRadius;
15790
+ if (!collision.queryCapsule(p.x, center, p.z, half, this.capsuleRadius, out))
15791
+ return true;
15792
+ const reach = this.capsuleRadius * UNCLIP_REACH_RADII;
15793
+ if (out.x * out.x + out.y * out.y + out.z * out.z > reach * reach)
15794
+ return false;
15795
+ nudged.set(p.x + out.x, p.y + out.y, p.z + out.z);
15796
+ if (!this._capsuleClear(collision, nudged))
15797
+ return false;
15798
+ p.copy(nudged);
15799
+ return true;
15800
+ }
15801
+ /**
15802
+ * Hold `collision` rather than engage it, standing on the current level.
15803
+ *
15804
+ * @param collision - Collision to engage once the capsule can.
15805
+ */
15806
+ _hold(collision) {
15807
+ this._pendingCollision = collision;
15808
+ this._collision = null;
15809
+ this._heldFloorY = this._position.y - this.eyeHeight - this.hoverHeight;
15810
+ this._velocity.y = 0;
15811
+ this._jumping = false;
15812
+ }
15852
15813
  /**
15853
15814
  * Cast multiple rays downward to find the average ground surface height.
15854
15815
  * Uses 5 rays (center + 4 cardinal at capsule radius) to spatially filter
@@ -17942,9 +17903,13 @@ class NavInteraction {
17942
17903
  const cameraPos = camera.getPosition();
17943
17904
  camera.camera.screenToWorld(offsetX, offsetY, 1.0, tmpV$4);
17944
17905
  tmpV$4.sub(cameraPos).normalize();
17945
- const hit = this.collision.queryRay(cameraPos.x, cameraPos.y, cameraPos.z, tmpV$4.x, tmpV$4.y, tmpV$4.z, camera.camera.farClip);
17906
+ // a camera inside solid geometry picks where the ray leaves it, not its own position
17907
+ const hit = this.collision.queryRay(cameraPos.x, cameraPos.y, cameraPos.z, tmpV$4.x, tmpV$4.y, tmpV$4.z, camera.camera.farClip, true);
17946
17908
  if (!hit)
17947
17909
  return null;
17910
+ // an exit's surface faces along the ray, so its normal is found looking back along it
17911
+ if (hit.exit)
17912
+ tmpV$4.mulScalar(-1);
17948
17913
  const sn = this.collision.querySurfaceNormal(hit.x, hit.y, hit.z, tmpV$4.x, tmpV$4.y, tmpV$4.z);
17949
17914
  return {
17950
17915
  position: new Vec3(hit.x, hit.y, hit.z),
@@ -19517,10 +19482,14 @@ class NavCursor {
19517
19482
  const cameraPos = camera.getPosition();
19518
19483
  camera.camera.screenToWorld(offsetX, offsetY, 1.0, tmpV);
19519
19484
  tmpV.sub(cameraPos).normalize();
19520
- const hit = collision.queryRay(cameraPos.x, cameraPos.y, cameraPos.z, tmpV.x, tmpV.y, tmpV.z, camera.camera.farClip);
19485
+ // a camera inside solid geometry picks where the ray leaves it, not its own position
19486
+ const hit = collision.queryRay(cameraPos.x, cameraPos.y, cameraPos.z, tmpV.x, tmpV.y, tmpV.z, camera.camera.farClip, true);
19521
19487
  if (!hit) {
19522
19488
  return null;
19523
19489
  }
19490
+ // an exit's surface faces along the ray, so its normal is found looking back along it
19491
+ if (hit.exit)
19492
+ tmpV.mulScalar(-1);
19524
19493
  const sn = collision.querySurfaceNormal(hit.x, hit.y, hit.z, tmpV.x, tmpV.y, tmpV.z);
19525
19494
  this.collisionTarget.position.set(hit.x, hit.y, hit.z);
19526
19495
  this.collisionTarget.normal.set(sn.nx, sn.ny, sn.nz);