@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@playcanvas/supersplat-viewer",
3
- "version": "1.38.0",
3
+ "version": "1.38.1",
4
4
  "author": "PlayCanvas <support@playcanvas.com>",
5
5
  "homepage": "https://github.com/playcanvas/supersplat-viewer#readme",
6
6
  "description": "Viewer for https://superspl.at",
package/public/index.js CHANGED
@@ -84577,12 +84577,6 @@ class MeshCollision {
84577
84577
  queryCapsule(cx, cy, cz, halfHeight, radius, out) {
84578
84578
  return resolveIterative(cx, cy, cz, (rx, ry, rz, push) => this._deepestCapsulePenetration(rx, ry, rz, halfHeight, radius, push), this._constraintNormals, this._push, out);
84579
84579
  }
84580
- isFreeAt(x, y, z) {
84581
- // Approximation: a point is in free space iff no triangle is within
84582
- // half a voxel of it. Matches the voxel-grid notion for data derived
84583
- // from the same carve.
84584
- return !this._deepestSpherePenetration(x, y, z, this.voxelResolution * 0.5, this._push);
84585
- }
84586
84580
  querySurfaceNormal(x, y, z, rdx, rdy, rdz) {
84587
84581
  const len = Math.sqrt(rdx * rdx + rdy * rdy + rdz * rdz);
84588
84582
  if (len < 1e-10) {
@@ -85119,24 +85113,6 @@ class VoxelCollision {
85119
85113
  get flipXY() {
85120
85114
  return false;
85121
85115
  }
85122
- isFreeAt(x, y, z) {
85123
- // Without voxel data, there's no carve to consult — report not-free
85124
- // rather than have the lattice search treat the world as wide open.
85125
- if (this._nodes.length === 0) {
85126
- return false;
85127
- }
85128
- const res = this._voxelResolution;
85129
- const ix = Math.floor((x - this._gridMinX) / res);
85130
- const iy = Math.floor((y - this._gridMinY) / res);
85131
- const iz = Math.floor((z - this._gridMinZ) / res);
85132
- // Outside the carved grid is "no data" — also not-free. Without this
85133
- // check the lattice search would spend candidates on out-of-bounds
85134
- // cells (and `isVoxelSolid` would mislead callers by returning false).
85135
- if (ix < 0 || iy < 0 || iz < 0 || ix >= this._numVoxelsX || iy >= this._numVoxelsY || iz >= this._numVoxelsZ) {
85136
- return false;
85137
- }
85138
- return !this.isVoxelSolid(ix, iy, iz);
85139
- }
85140
85116
  querySurfaceNormal(x, y, z, rdx, rdy, rdz) {
85141
85117
  const nudge = this._voxelResolution * 0.25;
85142
85118
  const ix = Math.floor((x + Math.sign(rdx) * nudge - this._gridMinX) / this._voxelResolution);
@@ -85173,7 +85149,7 @@ class VoxelCollision {
85173
85149
  result.nz = bestNz;
85174
85150
  return result;
85175
85151
  }
85176
- queryRay(ox, oy, oz, dx, dy, dz, maxDist) {
85152
+ queryRay(ox, oy, oz, dx, dy, dz, maxDist, exitSolid = false) {
85177
85153
  if (this._nodes.length === 0) {
85178
85154
  return null;
85179
85155
  }
@@ -85265,12 +85241,15 @@ class VoxelCollision {
85265
85241
  const tDeltaZ = Math.abs(dz) > EPS ? res * Math.abs(invDz) : Infinity;
85266
85242
  let currentT = tNear;
85267
85243
  const maxSteps = this._numVoxelsX + this._numVoxelsY + this._numVoxelsZ;
85244
+ // a ray from inside solid looks for the first free voxel instead, where it leaves
85245
+ const inside = exitSolid && tNear === 0 && this.isVoxelSolid(ix, iy, iz);
85268
85246
  for (let step = 0; step < maxSteps; step++) {
85269
- if (this.isVoxelSolid(ix, iy, iz)) {
85247
+ if (this.isVoxelSolid(ix, iy, iz) !== inside) {
85270
85248
  return {
85271
85249
  x: ox + dx * currentT,
85272
85250
  y: oy + dy * currentT,
85273
- z: oz + dz * currentT
85251
+ z: oz + dz * currentT,
85252
+ exit: inside
85274
85253
  };
85275
85254
  }
85276
85255
  // Advance along the axis with the smallest tMax
@@ -85296,13 +85275,22 @@ class VoxelCollision {
85296
85275
  iz += stepZ;
85297
85276
  tMaxZ += tDeltaZ;
85298
85277
  }
85299
- if (ix < 0 ||
85278
+ const outside = ix < 0 ||
85300
85279
  iy < 0 ||
85301
85280
  iz < 0 ||
85302
85281
  ix >= this._numVoxelsX ||
85303
85282
  iy >= this._numVoxelsY ||
85304
- iz >= this._numVoxelsZ ||
85305
- currentT > maxDist) {
85283
+ iz >= this._numVoxelsZ;
85284
+ // past the grid is free, so a ray inside solid that reaches its edge leaves there
85285
+ if (outside && inside && currentT <= maxDist) {
85286
+ return {
85287
+ x: ox + dx * currentT,
85288
+ y: oy + dy * currentT,
85289
+ z: oz + dz * currentT,
85290
+ exit: true
85291
+ };
85292
+ }
85293
+ if (outside || currentT > maxDist) {
85306
85294
  return null;
85307
85295
  }
85308
85296
  }
@@ -85708,8 +85696,8 @@ class FlippedVoxelCollision extends VoxelCollision {
85708
85696
  result.ny = -result.ny;
85709
85697
  return result;
85710
85698
  }
85711
- queryRay(ox, oy, oz, dx, dy, dz, maxDist) {
85712
- const hit = super.queryRay(-ox, -oy, oz, -dx, -dy, dz, maxDist);
85699
+ queryRay(ox, oy, oz, dx, dy, dz, maxDist, exitSolid = false) {
85700
+ const hit = super.queryRay(-ox, -oy, oz, -dx, -dy, dz, maxDist, exitSolid);
85713
85701
  if (hit) {
85714
85702
  hit.x = -hit.x;
85715
85703
  hit.y = -hit.y;
@@ -85732,9 +85720,6 @@ class FlippedVoxelCollision extends VoxelCollision {
85732
85720
  }
85733
85721
  return result;
85734
85722
  }
85735
- isFreeAt(x, y, z) {
85736
- return super.isFreeAt(-x, -y, z);
85737
- }
85738
85723
  }
85739
85724
  /**
85740
85725
  * Load a VoxelCollision from a .voxel.json URL.
@@ -93530,7 +93515,7 @@ const importSettings = (settings) => {
93530
93515
  return result;
93531
93516
  };
93532
93517
 
93533
- var version = "1.38.0";
93518
+ var version = "1.38.1";
93534
93519
 
93535
93520
  class Tooltip {
93536
93521
  register;
@@ -101086,14 +101071,6 @@ class SpawnState {
101086
101071
  this._distance = distance;
101087
101072
  this._has = true;
101088
101073
  }
101089
- /**
101090
- * Forget any previously-stored spawn pose so a subsequent `has` check
101091
- * reports false. Used by controllers that scope spawn to a single mode
101092
- * entry (e.g. walk).
101093
- */
101094
- clear() {
101095
- this._has = false;
101096
- }
101097
101074
  /**
101098
101075
  * Copy the captured pose into the supplied targets. Caller must check
101099
101076
  * `has` first; calling `restore` before `store` returns the field
@@ -101297,13 +101274,6 @@ class SphereMover {
101297
101274
  }
101298
101275
  }
101299
101276
 
101300
- /**
101301
- * Maximum Euclidean distance (metres) from the spawn origin to search for a valid placement.
101302
- * Used by the cylinder search, which has to reach far enough to find a standable floor. The
101303
- * sphere search sizes itself from its own radius instead — see `SPHERE_SEARCH_REACH_RADII`.
101304
- */
101305
- const SEARCH_RADIUS = 5;
101306
- const SEARCH_RADIUS_SQ = SEARCH_RADIUS * SEARCH_RADIUS;
101307
101277
  /**
101308
101278
  * Reach of the sphere search, in multiples of the sphere's radius.
101309
101279
  *
@@ -101311,7 +101281,7 @@ const SEARCH_RADIUS_SQ = SEARCH_RADIUS * SEARCH_RADIUS;
101311
101281
  * inside geometry — `FlyController` holds collision until the camera is clear rather than relying
101312
101282
  * on a rescue — so reaching a few radii is enough. Bounding the reach is what makes the search
101313
101283
  * cheap: at radius 0.2 on a 0.05 m grid the worst case, finding nothing, is 7,153 probes, where
101314
- * reaching `SEARCH_RADIUS` cost 8,120,601. A sphere deep inside geometry never finds a free cell
101284
+ * reaching 5 m cost 8,120,601. A sphere deep inside geometry never finds a free cell
101315
101285
  * to tighten `bestDistSq`, so nothing breaks the search early and it pays for every shell.
101316
101286
  *
101317
101287
  * The lattice still steps by `collision.voxelResolution`, deliberately. A coarser step is not
@@ -101321,8 +101291,6 @@ const SEARCH_RADIUS_SQ = SEARCH_RADIUS * SEARCH_RADIUS;
101321
101291
  * placement at all.
101322
101292
  */
101323
101293
  const SPHERE_SEARCH_REACH_RADII = 3;
101324
- /** Ray budget when probing for ground/ceiling under or above a candidate column. */
101325
- const RAY_MAX_DIST = 1000;
101326
101294
  // Scratch values reused across calls — these helpers run only at spawn time
101327
101295
  // so the lack of re-entrancy is fine.
101328
101296
  const scratchPush = { x: 0, y: 0, z: 0 };
@@ -101386,115 +101354,6 @@ const findSphereSpawn = (collision, ox, oy, oz, radius, out) => {
101386
101354
  }
101387
101355
  return found;
101388
101356
  };
101389
- /**
101390
- * Find the closest standable cylinder placement to (ox, oy, oz). Output is the
101391
- * floor world position the cylinder rests on (highest geometry hit across the
101392
- * footprint). Used for walk-camera spawn.
101393
- *
101394
- * The algorithm:
101395
- *
101396
- * 1. Lattice search for the closest free voxel `C` (per `isFreeAt`) that has
101397
- * geometry somewhere below — i.e. a downward ray from `C` hits.
101398
- * 2. At `(C.x, C.z)`, fan rays up and down from `C.y` through every cell
101399
- * (step = `collision.voxelResolution`) within the xz footprint disc.
101400
- * Compute, across columns:
101401
- * - `floor = max(yDown_i)` (highest ground in the footprint)
101402
- * - `ceiling = min(yUp_i)` (lowest obstruction overhead, or ∞)
101403
- * Every column must have a `yDown` hit; otherwise the cylinder is partly
101404
- * unsupported and the candidate is discarded.
101405
- * 3. The cylinder fits iff `floor + halfHeight ≤ ceiling - halfHeight`,
101406
- * i.e. enough vertical clearance for the full cylinder above the floor.
101407
- * The "resting on ground" placement has centre at `floor + halfHeight`.
101408
- *
101409
- * Cylinder math (flat ends, no hemispheres) intentionally matches the carve,
101410
- * which used a separable XYZ dilation.
101411
- *
101412
- * @param collision - Active collision implementation.
101413
- * @param ox - Origin X (world space).
101414
- * @param oy - Origin Y (world space).
101415
- * @param oz - Origin Z (world space).
101416
- * @param halfHeight - Cylinder half-height (full height = 2 × halfHeight).
101417
- * @param radius - Cylinder radius.
101418
- * @param out - Receives the floor position the cylinder rests on.
101419
- * @returns True if a valid placement was found.
101420
- */
101421
- const findCylinderSpawn = (collision, ox, oy, oz, halfHeight, radius, out) => {
101422
- const step = collision.voxelResolution;
101423
- const maxCells = Math.ceil(SEARCH_RADIUS / step);
101424
- // Round up so float division (e.g. 0.2 / 0.05) can't accidentally drop
101425
- // the outer ring of footprint cells; the Euclidean check below trims any
101426
- // overshoot back to the true radius.
101427
- const footCells = Math.ceil(radius / step);
101428
- const radiusSq = radius * radius;
101429
- let bestDistSq = Infinity;
101430
- let found = false;
101431
- for (let r = 0; r <= maxCells; r++) {
101432
- const shellMinDistSq = r * step * (r * step);
101433
- if (shellMinDistSq >= bestDistSq)
101434
- break;
101435
- for (let dy = -r; dy <= r; dy++) {
101436
- const absDy = dy < 0 ? -dy : dy;
101437
- for (let dz = -r; dz <= r; dz++) {
101438
- const absDz = dz < 0 ? -dz : dz;
101439
- // Enumerate the Chebyshev shell of radius r directly rather than scanning the
101440
- // whole cube and skipping its interior — see `findSphereSpawn`.
101441
- const full = absDy === r || absDz === r;
101442
- const dxStep = full ? 1 : 2 * r;
101443
- for (let dx = -r; dx <= r; dx += dxStep) {
101444
- const distSq = (dx * dx + dy * dy + dz * dz) * step * step;
101445
- if (distSq >= bestDistSq || distSq > SEARCH_RADIUS_SQ)
101446
- continue;
101447
- const cx = ox + dx * step;
101448
- const cy = oy + dy * step;
101449
- const cz = oz + dz * step;
101450
- // Stage 1: cheap filter — only consider free voxels.
101451
- // The footprint loop below will reject candidates whose
101452
- // center column has no ground support, so no separate
101453
- // down-ray probe is needed here.
101454
- if (!collision.isFreeAt(cx, cy, cz))
101455
- continue;
101456
- // Stage 2/3: fan rays through the xz footprint.
101457
- let floor = -Infinity;
101458
- let ceiling = Infinity;
101459
- let supported = true;
101460
- for (let i = -footCells; i <= footCells && supported; i++) {
101461
- const fxOff = i * step;
101462
- const fxOffSq = fxOff * fxOff;
101463
- for (let j = -footCells; j <= footCells; j++) {
101464
- const fzOff = j * step;
101465
- if (fxOffSq + fzOff * fzOff > radiusSq)
101466
- continue;
101467
- const fx = cx + fxOff;
101468
- const fz = cz + fzOff;
101469
- const down = collision.queryRay(fx, cy, fz, 0, -1, 0, RAY_MAX_DIST);
101470
- if (!down) {
101471
- supported = false;
101472
- break;
101473
- }
101474
- if (down.y > floor)
101475
- floor = down.y;
101476
- const up = collision.queryRay(fx, cy, fz, 0, 1, 0, RAY_MAX_DIST);
101477
- if (up && up.y < ceiling)
101478
- ceiling = up.y;
101479
- }
101480
- }
101481
- if (!supported)
101482
- continue;
101483
- // Cylinder vertical extent = 2 × halfHeight. Need that
101484
- // much room between floor and ceiling.
101485
- if (floor + 2 * halfHeight > ceiling)
101486
- continue;
101487
- bestDistSq = distSq;
101488
- out.x = cx;
101489
- out.y = floor;
101490
- out.z = cz;
101491
- found = true;
101492
- }
101493
- }
101494
- }
101495
- }
101496
- return found;
101497
- };
101498
101357
 
101499
101358
  /** Radius of the camera collision sphere (meters) */
101500
101359
  const CAMERA_RADIUS = 0.2;
@@ -101502,7 +101361,7 @@ const forward$2 = new Vec3();
101502
101361
  const right$2 = new Vec3();
101503
101362
  const up$1 = new Vec3();
101504
101363
  const offset$1 = new Vec3();
101505
- const spawnProbe$1 = new Vec3();
101364
+ const spawnProbe = new Vec3();
101506
101365
  const clearProbe = { x: 0, y: 0, z: 0 };
101507
101366
  // Whether the camera sphere fits at `position` without intersecting collision geometry.
101508
101367
  const isClear = (collision, position) => !collision.querySphere(position.x, position.y, position.z, CAMERA_RADIUS, clearProbe);
@@ -101565,8 +101424,8 @@ class FlyController {
101565
101424
  const collision = this._mover.collision ?? this._pendingCollision;
101566
101425
  this._pendingCollision = null;
101567
101426
  if (collision) {
101568
- if (findSphereSpawn(collision, this._position.x, this._position.y, this._position.z, CAMERA_RADIUS, spawnProbe$1)) {
101569
- this._position.copy(spawnProbe$1);
101427
+ if (findSphereSpawn(collision, this._position.x, this._position.y, this._position.z, CAMERA_RADIUS, spawnProbe)) {
101428
+ this._position.copy(spawnProbe);
101570
101429
  }
101571
101430
  // Engage at the placed position. The search starts at the camera's own cell, so
101572
101431
  // failing it means the camera is inside geometry with no free space within reach to
@@ -101891,15 +101750,41 @@ class OrbitController {
101891
101750
 
101892
101751
  const FIXED_DT = 1 / 60;
101893
101752
  const MAX_SUBSTEPS = 10;
101753
+ /**
101754
+ * How far below the capsule a floor must be for collision to engage. With none in reach the
101755
+ * capsule is over a hole in the collision, or far above anything it could land on, and engaged
101756
+ * collision would drop it out of the scene, so collision is held instead.
101757
+ */
101758
+ const FLOOR_SEARCH_DIST = 10;
101759
+ /**
101760
+ * How far below the camera entry looks for a floor to stand on. Unbounded in practice, unlike
101761
+ * `FLOOR_SEARCH_DIST`: entering from high up, from an orbit view, should land on the floor below.
101762
+ */
101763
+ const ENTRY_FLOOR_DIST = 1000;
101764
+ /**
101765
+ * How far above the camera entry looks for a floor, when there is none to stand on below it: a
101766
+ * camera sunk into the ground or just under it stands on the surface above instead.
101767
+ */
101768
+ const ENTRY_RISE_REACH = 2;
101769
+ /**
101770
+ * Spacing of the rays that look for a floor above the camera. Each is cast down from its own
101771
+ * height, so a surface between two floors cannot hide the lower one from all of them.
101772
+ */
101773
+ const ENTRY_RISE_STEP = 0.25;
101774
+ /**
101775
+ * Reach of the entry un-clip, in capsule radii, as `FlyController`'s: enough to free a capsule
101776
+ * that clips a surface, not to relocate one deep inside geometry, which holds collision instead.
101777
+ */
101778
+ const UNCLIP_REACH_RADII = 3;
101894
101779
  /** Pre-allocated push-out vector for capsule collision */
101895
101780
  const out = { x: 0, y: 0, z: 0 };
101896
101781
  const v = new Vec3();
101897
101782
  const d = new Vec3();
101783
+ const nudged = new Vec3();
101898
101784
  const forward = new Vec3();
101899
101785
  const right$1 = new Vec3();
101900
101786
  const moveStep = [0, 0, 0];
101901
101787
  const offset = new Vec3();
101902
- const spawnProbe = new Vec3();
101903
101788
  /**
101904
101789
  * First-person camera controller with spring-damper suspension over collision terrain.
101905
101790
  *
@@ -101977,7 +101862,6 @@ class WalkController {
101977
101862
  _targetAngles = new Vec3();
101978
101863
  _distance = 1;
101979
101864
  _spawn = new SpawnState();
101980
- _spawnGrounded = false;
101981
101865
  _velocity = new Vec3();
101982
101866
  _pendingMove = [0, 0, 0];
101983
101867
  _accumulator = 0;
@@ -101986,10 +101870,12 @@ class WalkController {
101986
101870
  _jumpHeld = false;
101987
101871
  _collision = null;
101988
101872
  /**
101989
- * Collision held because the capsule is inside geometry, mirroring `FlyController`. While
101990
- * held, `_heldFloorY` stands in for the ground probe so the camera walks on the level it
101991
- * entered at instead of falling, and the capsule check is skipped. `_step` engages it for
101992
- * real as soon as the capsule fits.
101873
+ * Collision held because the capsule is inside geometry, mirroring `FlyController`, or
101874
+ * because there is no floor within `FLOOR_SEARCH_DIST` below it, where engaged collision
101875
+ * would fall out of the scene.
101876
+ * While held, `_heldFloorY` stands in for the ground probe so the camera walks on the level
101877
+ * it was held at instead of falling, and the capsule check is skipped. `_step` engages it for
101878
+ * real as soon as the capsule fits over a floor.
101993
101879
  */
101994
101880
  _pendingCollision = null;
101995
101881
  /**
@@ -102004,15 +101890,14 @@ class WalkController {
102004
101890
  _hasPosition = false;
102005
101891
  /**
102006
101892
  * Optional collision for capsule collision with sliding. Assigning while the capsule is
102007
- * inside geometry holds the attachment instead: engaged collision cannot escape a solid
102008
- * region, so the capsule keeps the synthetic floor and `_step` engages it once it fits.
102009
- * Collision normally lands this way, after walk mode may already be active.
101893
+ * inside geometry, or over no floor in reach, holds the attachment instead: engaged collision
101894
+ * cannot escape a solid region and has nothing to land on over a void, so the capsule keeps
101895
+ * the synthetic floor and `_step` engages it once it fits over a floor. Collision normally
101896
+ * lands this way, after walk mode may already be active.
102010
101897
  */
102011
101898
  set collision(value) {
102012
- if (value && this._hasPosition && !this._capsuleClear(value, this._position)) {
102013
- this._pendingCollision = value;
102014
- this._collision = null;
102015
- this._heldFloorY = this._position.y - this.eyeHeight - this.hoverHeight;
101899
+ if (value && this._hasPosition && !this._canEngage(value, this._position)) {
101900
+ this._hold(value);
102016
101901
  return;
102017
101902
  }
102018
101903
  this._pendingCollision = null;
@@ -102024,47 +101909,26 @@ class WalkController {
102024
101909
  }
102025
101910
  onEnter(camera) {
102026
101911
  this.goto(camera);
102027
- // Spawn is scoped to this walk-mode entry; reset so a stale spawn from a previous entry
102028
- // can't be restored if this entry fails or has no collision to place against.
102029
- this._spawn.clear();
102030
101912
  // Entry may follow a previous one that ended with collision held, so start from the
102031
101913
  // collision we have either way. Whether to hold again is decided fresh, from this
102032
101914
  // entry's position.
102033
101915
  const collision = this._collision ?? this._pendingCollision;
102034
- // Stand on the entry level until collision can place the capsule. This covers collision
102035
- // that has not arrived yet as well as a spawn search that finds nowhere to go: engaged
102036
- // collision would strand the camera either way, with no floor under the ground probe and
102037
- // nothing for push-out to push against. Held, the user can walk out the way they came,
102038
- // and the setter or `_step` engages collision once the capsule fits.
101916
+ // Stand on the entry level until collision can engage. This covers collision that has not
101917
+ // arrived yet as well as an entry inside geometry or over no floor, where engaged
101918
+ // collision would strand the camera or drop it. Held, the user can walk out the way they
101919
+ // came, and the setter or `_step` engages collision once the capsule fits over a floor.
102039
101920
  this._pendingCollision = null;
102040
101921
  this._collision = null;
102041
101922
  this._heldFloorY = this._position.y - this.eyeHeight - this.hoverHeight;
102042
101923
  this._grounded = true;
102043
101924
  if (collision) {
102044
- // Treat the walk capsule as a tight-superset cylinder for spawn.
102045
- // The carve was produced with a separable XYZ dilation (flat ends,
102046
- // no hemispheres), so cylinder math matches the data exactly.
102047
- // Include `hoverHeight` in the height so the ceiling-clearance
102048
- // check accounts for the placed capsule's full vertical envelope:
102049
- // foot sits at `floor + hoverHeight`, head at `floor + hoverHeight
102050
- // + capsuleHeight`.
102051
- if (findCylinderSpawn(collision, camera.position.x, camera.position.y, camera.position.z, (this.capsuleHeight + this.hoverHeight) * 0.5, this.capsuleRadius, spawnProbe)) {
102052
- // spawnProbe is the floor world position the cylinder rests
102053
- // on. Eye sits hoverHeight + eyeHeight above the floor.
102054
- this._position.set(spawnProbe.x, spawnProbe.y + this.hoverHeight + this.eyeHeight, spawnProbe.z);
102055
- this._storeSpawn();
102056
- // The placement is known to fit, so engage directly rather than re-testing it
102057
- // through the setter's capsule query, which is a different test from the
102058
- // cylinder search and could hold on a hairline disagreement.
102059
- this._heldFloorY = null;
102060
- this._collision = collision;
102061
- }
102062
- else {
102063
- // No placement within reach. The setter holds if the capsule is inside geometry
102064
- // and engages otherwise, which is what `_step` would do on its first step anyway.
102065
- this.collision = collision;
101925
+ if (!this._standOnFloor(collision)) {
101926
+ this._fit(collision, this._position);
102066
101927
  }
101928
+ this.collision = collision;
102067
101929
  }
101930
+ // reset returns to where walk was entered
101931
+ this._spawn.store(this._position, this._angles, this._distance);
102068
101932
  this._prevPosition.copy(this._position);
102069
101933
  }
102070
101934
  update(deltaTime, inputFrame, camera) {
@@ -102099,14 +101963,20 @@ class WalkController {
102099
101963
  camera.fov = this.fov;
102100
101964
  }
102101
101965
  _step(dt, move) {
102102
- // engage a held attachment as soon as the capsule reaches space it fits in
102103
- if (this._pendingCollision && this._capsuleClear(this._pendingCollision, this._position)) {
101966
+ // engage a held attachment as soon as the capsule reaches space it fits in, over a floor
101967
+ if (this._pendingCollision && this._canEngage(this._pendingCollision, this._position)) {
102104
101968
  this._collision = this._pendingCollision;
102105
101969
  this._pendingCollision = null;
102106
101970
  this._heldFloorY = null;
102107
101971
  }
102108
101972
  // ground probe: cast a ray downward to find the terrain surface
102109
- const groundY = this._probeGround(this._position);
101973
+ let groundY = this._probeGround(this._position);
101974
+ // airborne with no floor in reach, over a hole in the collision, would fall out of the
101975
+ // scene: hold collision and walk on at this level until there is floor below again
101976
+ if (groundY === null && this._collision && !this._floorBelow(this._collision, this._position)) {
101977
+ this._hold(this._collision);
101978
+ groundY = this._heldFloorY;
101979
+ }
102110
101980
  const hasGround = groundY !== null;
102111
101981
  // jump (require release before re-triggering)
102112
101982
  if (this._velocity.y < 0) {
@@ -102189,17 +102059,18 @@ class WalkController {
102189
102059
  this._prevPosition.copy(this._position);
102190
102060
  this._targetAngles.copy(this._angles);
102191
102061
  this._resetMotion();
102192
- this._grounded = this._spawnGrounded;
102062
+ // decide afresh at the spawn whether to engage or hold, since a held floor belongs to
102063
+ // wherever it was held, mirroring `FlyController`
102064
+ const collision = this._collision ?? this._pendingCollision;
102065
+ if (collision) {
102066
+ this.collision = collision;
102067
+ }
102193
102068
  camera.position.copy(this._position);
102194
102069
  camera.angles.copy(this._angles);
102195
102070
  camera.distance = this._distance;
102196
102071
  camera.fov = this.fov;
102197
102072
  return true;
102198
102073
  }
102199
- _storeSpawn() {
102200
- this._spawn.store(this._position, this._angles, this._distance);
102201
- this._spawnGrounded = this._grounded;
102202
- }
102203
102074
  _resetMotion() {
102204
102075
  this._velocity.set(0, 0, 0);
102205
102076
  this._grounded = false;
@@ -102222,6 +102093,96 @@ class WalkController {
102222
102093
  const half = this.capsuleHeight * 0.5 - this.capsuleRadius;
102223
102094
  return !collision.queryCapsule(pos.x, center, pos.z, half, this.capsuleRadius, out);
102224
102095
  }
102096
+ /**
102097
+ * Whether there is a floor within `FLOOR_SEARCH_DIST` below the capsule.
102098
+ *
102099
+ * @param collision - Collision to test against.
102100
+ * @param pos - Eye position in PlayCanvas world space.
102101
+ * @returns True if a downward ray from the capsule's foot hits geometry in reach.
102102
+ */
102103
+ _floorBelow(collision, pos) {
102104
+ return !!collision.queryRay(pos.x, pos.y - this.eyeHeight, pos.z, 0, -1, 0, FLOOR_SEARCH_DIST);
102105
+ }
102106
+ _canEngage(collision, pos) {
102107
+ return this._capsuleClear(collision, pos) && this._floorBelow(collision, pos);
102108
+ }
102109
+ /**
102110
+ * Put the eye at standing height on a floor, so the capsule neither sinks into the ground nor
102111
+ * hangs over it: the floor below the camera however far down, for a camera flown onto the
102112
+ * ground or entering from above, and failing that the nearest floor up to `ENTRY_RISE_REACH`
102113
+ * above it, for a camera sunk into the ground or just under it. Below comes first, or an
102114
+ * entry from high up could stand on a roof. A floor counts only if the capsule fits standing
102115
+ * on it, after `_fit`; with none, the camera stays where it is.
102116
+ *
102117
+ * @param collision - Collision to test against.
102118
+ * @returns True if the camera was moved onto a floor.
102119
+ */
102120
+ _standOnFloor(collision) {
102121
+ const pos = this._position;
102122
+ if (this._tryStandFrom(collision, pos.y, ENTRY_FLOOR_DIST))
102123
+ return true;
102124
+ for (let rise = ENTRY_RISE_STEP; rise <= ENTRY_RISE_REACH + 1e-6; rise += ENTRY_RISE_STEP) {
102125
+ if (this._tryStandFrom(collision, pos.y + rise, rise))
102126
+ return true;
102127
+ }
102128
+ return false;
102129
+ }
102130
+ /**
102131
+ * Stand on the floor a downward ray from `fromY` finds within `maxDist`, if the capsule fits
102132
+ * there once `_fit` has freed it from a wall it clips. A ray that hits at its origin started
102133
+ * inside geometry and finds no floor.
102134
+ *
102135
+ * @param collision - Collision to test against.
102136
+ * @param fromY - Height to cast from, above the camera's column.
102137
+ * @param maxDist - Reach of the ray.
102138
+ * @returns True if the camera was moved onto a floor.
102139
+ */
102140
+ _tryStandFrom(collision, fromY, maxDist) {
102141
+ const pos = this._position;
102142
+ const hit = collision.queryRay(pos.x, fromY, pos.z, 0, -1, 0, maxDist);
102143
+ if (!hit || hit.y >= fromY)
102144
+ return false;
102145
+ v.set(pos.x, hit.y + this.hoverHeight + this.eyeHeight, pos.z);
102146
+ if (!this._fit(collision, v))
102147
+ return false;
102148
+ pos.copy(v);
102149
+ return true;
102150
+ }
102151
+ /**
102152
+ * Fit the capsule at `p`, freeing it from a surface it clips: move it by the push-out of one
102153
+ * capsule query, if that is within `UNCLIP_REACH_RADII` and leaves it clear. Otherwise `p` is
102154
+ * left as it was.
102155
+ *
102156
+ * @param collision - Collision to test against.
102157
+ * @param p - Eye position, moved if the capsule had to be freed.
102158
+ * @returns True if the capsule fits at `p`.
102159
+ */
102160
+ _fit(collision, p) {
102161
+ const center = p.y - this.eyeHeight + this.capsuleHeight * 0.5;
102162
+ const half = this.capsuleHeight * 0.5 - this.capsuleRadius;
102163
+ if (!collision.queryCapsule(p.x, center, p.z, half, this.capsuleRadius, out))
102164
+ return true;
102165
+ const reach = this.capsuleRadius * UNCLIP_REACH_RADII;
102166
+ if (out.x * out.x + out.y * out.y + out.z * out.z > reach * reach)
102167
+ return false;
102168
+ nudged.set(p.x + out.x, p.y + out.y, p.z + out.z);
102169
+ if (!this._capsuleClear(collision, nudged))
102170
+ return false;
102171
+ p.copy(nudged);
102172
+ return true;
102173
+ }
102174
+ /**
102175
+ * Hold `collision` rather than engage it, standing on the current level.
102176
+ *
102177
+ * @param collision - Collision to engage once the capsule can.
102178
+ */
102179
+ _hold(collision) {
102180
+ this._pendingCollision = collision;
102181
+ this._collision = null;
102182
+ this._heldFloorY = this._position.y - this.eyeHeight - this.hoverHeight;
102183
+ this._velocity.y = 0;
102184
+ this._jumping = false;
102185
+ }
102225
102186
  /**
102226
102187
  * Cast multiple rays downward to find the average ground surface height.
102227
102188
  * Uses 5 rays (center + 4 cardinal at capsule radius) to spatially filter
@@ -104315,9 +104276,13 @@ class NavInteraction {
104315
104276
  const cameraPos = camera.getPosition();
104316
104277
  camera.camera.screenToWorld(offsetX, offsetY, 1.0, tmpV$4);
104317
104278
  tmpV$4.sub(cameraPos).normalize();
104318
- const hit = this.collision.queryRay(cameraPos.x, cameraPos.y, cameraPos.z, tmpV$4.x, tmpV$4.y, tmpV$4.z, camera.camera.farClip);
104279
+ // a camera inside solid geometry picks where the ray leaves it, not its own position
104280
+ const hit = this.collision.queryRay(cameraPos.x, cameraPos.y, cameraPos.z, tmpV$4.x, tmpV$4.y, tmpV$4.z, camera.camera.farClip, true);
104319
104281
  if (!hit)
104320
104282
  return null;
104283
+ // an exit's surface faces along the ray, so its normal is found looking back along it
104284
+ if (hit.exit)
104285
+ tmpV$4.mulScalar(-1);
104321
104286
  const sn = this.collision.querySurfaceNormal(hit.x, hit.y, hit.z, tmpV$4.x, tmpV$4.y, tmpV$4.z);
104322
104287
  return {
104323
104288
  position: new Vec3(hit.x, hit.y, hit.z),
@@ -105890,10 +105855,14 @@ class NavCursor {
105890
105855
  const cameraPos = camera.getPosition();
105891
105856
  camera.camera.screenToWorld(offsetX, offsetY, 1.0, tmpV);
105892
105857
  tmpV.sub(cameraPos).normalize();
105893
- const hit = collision.queryRay(cameraPos.x, cameraPos.y, cameraPos.z, tmpV.x, tmpV.y, tmpV.z, camera.camera.farClip);
105858
+ // a camera inside solid geometry picks where the ray leaves it, not its own position
105859
+ const hit = collision.queryRay(cameraPos.x, cameraPos.y, cameraPos.z, tmpV.x, tmpV.y, tmpV.z, camera.camera.farClip, true);
105894
105860
  if (!hit) {
105895
105861
  return null;
105896
105862
  }
105863
+ // an exit's surface faces along the ray, so its normal is found looking back along it
105864
+ if (hit.exit)
105865
+ tmpV.mulScalar(-1);
105897
105866
  const sn = collision.querySurfaceNormal(hit.x, hit.y, hit.z, tmpV.x, tmpV.y, tmpV.z);
105898
105867
  this.collisionTarget.position.set(hit.x, hit.y, hit.z);
105899
105868
  this.collisionTarget.normal.set(sn.nx, sn.ny, sn.nz);