@playcanvas/supersplat-viewer 1.22.3 → 1.23.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.
- package/dist/index.js +3 -3
- package/package.json +1 -1
- package/public/index.css +0 -11
- package/public/index.html +21 -8
- package/public/index.js +1056 -1196
- package/public/index.js.map +1 -1
package/public/index.js
CHANGED
|
@@ -77723,7 +77723,7 @@ const importSettings = (settings) => {
|
|
|
77723
77723
|
return result;
|
|
77724
77724
|
};
|
|
77725
77725
|
|
|
77726
|
-
var version = "1.
|
|
77726
|
+
var version = "1.23.0";
|
|
77727
77727
|
|
|
77728
77728
|
class Tooltip {
|
|
77729
77729
|
register;
|
|
@@ -78359,8 +78359,9 @@ const initUI = (global) => {
|
|
|
78359
78359
|
if (type === 'interrupt')
|
|
78360
78360
|
dismissWalkHint();
|
|
78361
78361
|
});
|
|
78362
|
-
// show/hide the FPS button based on
|
|
78363
|
-
|
|
78362
|
+
// show/hide the FPS button based on whether walk mode is offered
|
|
78363
|
+
// (collision data exists AND scene is large enough to walk around in)
|
|
78364
|
+
events.on('walkAllowed:changed', (value) => {
|
|
78364
78365
|
dom.fpsCamera.classList.toggle('hidden', !value);
|
|
78365
78366
|
// adjust fly button shape: middle when FPS is visible, right when hidden
|
|
78366
78367
|
dom.flyCamera.classList.toggle('middle', value);
|
|
@@ -80565,6 +80566,16 @@ class WalkSource {
|
|
|
80565
80566
|
|
|
80566
80567
|
const tmpCamera = new Camera();
|
|
80567
80568
|
const tmpv = new Vec3();
|
|
80569
|
+
// Walk mode is only enabled when the scene's horizontal footprint is large
|
|
80570
|
+
// enough to walk around in. Vertical extent (Y) is irrelevant — a tall but
|
|
80571
|
+
// narrow scene isn't walkable. Both X and Z ranges must exceed this
|
|
80572
|
+
// minimum (in metres); below it walk mode is hidden and the viewer falls
|
|
80573
|
+
// back to fly as the default first-person mode.
|
|
80574
|
+
const WALK_MIN_HORIZONTAL_RANGE = 5;
|
|
80575
|
+
const isWalkAllowed = (bbox, collision) => {
|
|
80576
|
+
const { x, z } = bbox.halfExtents;
|
|
80577
|
+
return !!collision && x * 2 >= WALK_MIN_HORIZONTAL_RANGE && z * 2 >= WALK_MIN_HORIZONTAL_RANGE;
|
|
80578
|
+
};
|
|
80568
80579
|
const createCamera = (position, target, fov) => {
|
|
80569
80580
|
const result = new Camera();
|
|
80570
80581
|
result.look(position, target);
|
|
@@ -80582,6 +80593,7 @@ class CameraManager {
|
|
|
80582
80593
|
camera = new Camera();
|
|
80583
80594
|
constructor(global, bbox, collision = null) {
|
|
80584
80595
|
const { events, settings, state } = global;
|
|
80596
|
+
const walkAllowed = isWalkAllowed(bbox, collision);
|
|
80585
80597
|
const camera0 = settings.cameras[0]?.initial;
|
|
80586
80598
|
const defaultFov = camera0?.fov ?? 75;
|
|
80587
80599
|
const frameCamera = createFrameCamera(bbox, defaultFov);
|
|
@@ -80633,11 +80645,11 @@ class CameraManager {
|
|
|
80633
80645
|
state.hasAnimation = !!controllers.anim;
|
|
80634
80646
|
state.animationDuration = controllers.anim ? controllers.anim.animState.cursor.duration : 0;
|
|
80635
80647
|
// initialize camera mode and initial camera position
|
|
80636
|
-
state.cameraMode = state.hasAnimation ? 'anim' : (isObjectExperience ? 'orbit' : (
|
|
80648
|
+
state.cameraMode = state.hasAnimation ? 'anim' : (isObjectExperience ? 'orbit' : (walkAllowed ? 'walk' : 'fly'));
|
|
80637
80649
|
this.camera.copy(resetCamera);
|
|
80638
80650
|
const target = new Camera(this.camera); // the active controller updates this
|
|
80639
80651
|
const from = new Camera(this.camera); // stores the previous camera state during transition
|
|
80640
|
-
const defaultMode = isObjectExperience ? 'orbit' : (
|
|
80652
|
+
const defaultMode = isObjectExperience ? 'orbit' : (walkAllowed ? 'walk' : 'fly');
|
|
80641
80653
|
let fromMode = defaultMode;
|
|
80642
80654
|
// tracks the mode to restore when exiting walk
|
|
80643
80655
|
let preWalkMode = isObjectExperience ? 'orbit' : 'fly';
|
|
@@ -80721,7 +80733,7 @@ class CameraManager {
|
|
|
80721
80733
|
state.cameraMode = 'fly';
|
|
80722
80734
|
break;
|
|
80723
80735
|
case 'toggleWalk':
|
|
80724
|
-
if (
|
|
80736
|
+
if (walkAllowed) {
|
|
80725
80737
|
if (state.cameraMode === 'walk') {
|
|
80726
80738
|
state.cameraMode = preWalkMode;
|
|
80727
80739
|
}
|
|
@@ -81741,62 +81753,6 @@ class TouchDevice {
|
|
|
81741
81753
|
}
|
|
81742
81754
|
}
|
|
81743
81755
|
|
|
81744
|
-
/**
|
|
81745
|
-
* Distinguishes a physical mouse wheel from a trackpad two-finger scroll.
|
|
81746
|
-
*
|
|
81747
|
-
* A single wheel event is unreliable (Magic Mouse, hi-res mice, and macOS
|
|
81748
|
-
* Shift-remapping all confuse per-event heuristics), so classify on the
|
|
81749
|
-
* first event of a burst and let the rest of the burst inherit that label.
|
|
81750
|
-
* A burst is a run of wheel events separated by less than BURST_GAP_MS;
|
|
81751
|
-
* trackpads stream at ~60Hz (~16ms), wheels emit one event per notch
|
|
81752
|
-
* (typically >>50ms apart).
|
|
81753
|
-
*/
|
|
81754
|
-
const BURST_GAP_MS = 80;
|
|
81755
|
-
const isMouseWheelEvent = (event) => {
|
|
81756
|
-
// Firefox: physical wheels report line/page mode; trackpads report
|
|
81757
|
-
// pixel mode. Firefox doesn't expose wheelDelta* so this is the only
|
|
81758
|
-
// reliable signal there.
|
|
81759
|
-
if (event.deltaMode !== WheelEvent.DOM_DELTA_PIXEL) {
|
|
81760
|
-
return true;
|
|
81761
|
-
}
|
|
81762
|
-
// Chrome / Safari: the non-standard wheelDelta{X,Y} properties preserve
|
|
81763
|
-
// the raw wheel-tick value (always multiples of ±120 per notch)
|
|
81764
|
-
// regardless of macOS scroll smoothing. Trackpads and Magic Mouse emit
|
|
81765
|
-
// arbitrary values that are essentially never aligned to 120.
|
|
81766
|
-
const e = event;
|
|
81767
|
-
if (typeof e.wheelDeltaY === 'number' && e.wheelDeltaY !== 0) {
|
|
81768
|
-
return e.wheelDeltaY % 120 === 0;
|
|
81769
|
-
}
|
|
81770
|
-
if (typeof e.wheelDeltaX === 'number' && e.wheelDeltaX !== 0) {
|
|
81771
|
-
return e.wheelDeltaX % 120 === 0;
|
|
81772
|
-
}
|
|
81773
|
-
// Last-resort fallback for browsers without wheelDelta*.
|
|
81774
|
-
const { deltaX, deltaY } = event;
|
|
81775
|
-
if (deltaX !== 0 && deltaY !== 0) {
|
|
81776
|
-
return false;
|
|
81777
|
-
}
|
|
81778
|
-
return Number.isInteger(deltaX) && Number.isInteger(deltaY);
|
|
81779
|
-
};
|
|
81780
|
-
class WheelClassifier {
|
|
81781
|
-
_lastTime = -Infinity;
|
|
81782
|
-
_burstIsMouseWheel = false;
|
|
81783
|
-
/**
|
|
81784
|
-
* Classify the given event. The first event of a burst sets the label
|
|
81785
|
-
* for the burst; subsequent events within BURST_GAP_MS inherit it.
|
|
81786
|
-
*
|
|
81787
|
-
* @param event - The wheel event to classify.
|
|
81788
|
-
* @returns True if the event came from a physical mouse wheel.
|
|
81789
|
-
*/
|
|
81790
|
-
classify(event) {
|
|
81791
|
-
const now = performance.now();
|
|
81792
|
-
if (now - this._lastTime > BURST_GAP_MS) {
|
|
81793
|
-
this._burstIsMouseWheel = isMouseWheelEvent(event);
|
|
81794
|
-
}
|
|
81795
|
-
this._lastTime = now;
|
|
81796
|
-
return this._burstIsMouseWheel;
|
|
81797
|
-
}
|
|
81798
|
-
}
|
|
81799
|
-
|
|
81800
81756
|
const tmpV$1 = new Vec3();
|
|
81801
81757
|
class TrackpadDevice {
|
|
81802
81758
|
orbitSpeed = 18;
|
|
@@ -81804,22 +81760,59 @@ class TrackpadDevice {
|
|
|
81804
81760
|
trackpadOrbitSensitivity = 0.75;
|
|
81805
81761
|
trackpadPanSensitivity = 1.0;
|
|
81806
81762
|
trackpadZoomSensitivity = 2.0;
|
|
81807
|
-
_classifier = new WheelClassifier();
|
|
81808
81763
|
_global = null;
|
|
81809
81764
|
_orbit = [0, 0];
|
|
81810
81765
|
_pan = [0, 0];
|
|
81811
81766
|
_zoom = 0;
|
|
81812
|
-
|
|
81813
|
-
|
|
81814
|
-
|
|
81815
|
-
|
|
81767
|
+
// Tracks physical Ctrl key state so we can distinguish macOS-synthesized
|
|
81768
|
+
// pinch events (wheel + ctrlKey, no physical Ctrl) from a user holding
|
|
81769
|
+
// Ctrl while spinning a mouse wheel. The synthetic-Ctrl signal is the
|
|
81770
|
+
// one reliable trackpad indicator the platform gives us — every other
|
|
81771
|
+
// per-event heuristic has been tried (wheelDelta % 120, deltaMode,
|
|
81772
|
+
// fractional/diagonal deltas) and misfires on hi-res mice or in
|
|
81773
|
+
// momentum tails. Modifier-key state we can trust 100%.
|
|
81774
|
+
_ctrlDown = false;
|
|
81775
|
+
_onKeyDown = (event) => {
|
|
81776
|
+
if (event.key === 'Control') {
|
|
81777
|
+
this._ctrlDown = true;
|
|
81778
|
+
}
|
|
81779
|
+
};
|
|
81780
|
+
_onKeyUp = (event) => {
|
|
81781
|
+
if (event.key === 'Control') {
|
|
81782
|
+
this._ctrlDown = false;
|
|
81816
81783
|
}
|
|
81784
|
+
};
|
|
81785
|
+
// Window blur (alt-tab, focus another app) drops keyup events, so
|
|
81786
|
+
// clear Ctrl state defensively to avoid getting stuck.
|
|
81787
|
+
_onBlur = () => {
|
|
81788
|
+
this._ctrlDown = false;
|
|
81789
|
+
};
|
|
81790
|
+
_onWheel = (event) => {
|
|
81817
81791
|
const mode = this._global.state.cameraMode;
|
|
81818
|
-
|
|
81819
|
-
|
|
81820
|
-
|
|
81821
|
-
|
|
81822
|
-
|
|
81792
|
+
// Synthetic Ctrl (macOS pinch-to-zoom, Magic Mouse pinch): ctrlKey
|
|
81793
|
+
// is true on the event but the user isn't physically holding Ctrl.
|
|
81794
|
+
// Routes to trackpad-tuned zoom.
|
|
81795
|
+
const isPinch = event.ctrlKey && !this._ctrlDown;
|
|
81796
|
+
// Physical Ctrl + wheel/swipe: rotate. In orbit mode rotates around
|
|
81797
|
+
// the target; in fly/walk mode looks around. Applies to both mouse
|
|
81798
|
+
// wheel and trackpad swipe — we can't distinguish them, and the
|
|
81799
|
+
// modifier signals intent either way.
|
|
81800
|
+
// Corner case: user physically holds Ctrl AND pinches at the same
|
|
81801
|
+
// time — both flags are true, the gesture lands here as rotate
|
|
81802
|
+
// rather than pinch. Intentional: physical Ctrl is the explicit
|
|
81803
|
+
// signal, so it wins over the synthetic-Ctrl pinch heuristic.
|
|
81804
|
+
const isCtrlRotate = event.ctrlKey && this._ctrlDown;
|
|
81805
|
+
// Shift + wheel/swipe: pan (= strafe + vertical) in all modes. The
|
|
81806
|
+
// downstream camera controllers translate the screen-space pan
|
|
81807
|
+
// vector appropriately (orbit pans around the target plane; fly
|
|
81808
|
+
// strafes sideways and translates vertically; walk strafes).
|
|
81809
|
+
const isShiftPan = event.shiftKey;
|
|
81810
|
+
if (!isPinch && !isCtrlRotate && !isShiftPan) {
|
|
81811
|
+
// Bare wheel (mouse or trackpad swipe), unmodified fly/walk
|
|
81812
|
+
// wheels — all fall through to KeyboardMouseDevice for
|
|
81813
|
+
// standard forward/back motion (which dollys toward the orbit
|
|
81814
|
+
// target in orbit mode via flipZForOrbit, giving the universal
|
|
81815
|
+
// bare-scroll-zooms behavior).
|
|
81823
81816
|
return;
|
|
81824
81817
|
}
|
|
81825
81818
|
event.preventDefault();
|
|
@@ -81830,29 +81823,21 @@ class TrackpadDevice {
|
|
|
81830
81823
|
// so fire interrupt explicitly to keep parity with mouse-wheel.
|
|
81831
81824
|
event.stopImmediatePropagation();
|
|
81832
81825
|
this._global.events.fire('inputEvent', 'interrupt', event);
|
|
81826
|
+
if (mode === 'fly') {
|
|
81827
|
+
this._global.events.fire('navigateCancel');
|
|
81828
|
+
}
|
|
81833
81829
|
const { deltaX, deltaY } = event;
|
|
81834
|
-
if (
|
|
81835
|
-
|
|
81836
|
-
// pinch on macOS arrives as ctrl+wheel; ctrl/meta+scroll routes here too
|
|
81837
|
-
this._zoom += deltaY;
|
|
81838
|
-
}
|
|
81839
|
-
else if (event.shiftKey) {
|
|
81840
|
-
this._pan[0] += deltaX;
|
|
81841
|
-
this._pan[1] += deltaY;
|
|
81842
|
-
}
|
|
81843
|
-
else {
|
|
81844
|
-
this._orbit[0] += deltaX;
|
|
81845
|
-
this._orbit[1] += deltaY;
|
|
81846
|
-
}
|
|
81830
|
+
if (isPinch) {
|
|
81831
|
+
this._zoom += deltaY;
|
|
81847
81832
|
}
|
|
81848
|
-
else {
|
|
81849
|
-
// fly / walk: always rotate (shift is a speed modifier, not a gesture)
|
|
81850
|
-
if (mode === 'fly') {
|
|
81851
|
-
this._global.events.fire('navigateCancel');
|
|
81852
|
-
}
|
|
81833
|
+
else if (isCtrlRotate) {
|
|
81853
81834
|
this._orbit[0] += deltaX;
|
|
81854
81835
|
this._orbit[1] += deltaY;
|
|
81855
81836
|
}
|
|
81837
|
+
else {
|
|
81838
|
+
this._pan[0] += deltaX;
|
|
81839
|
+
this._pan[1] += deltaY;
|
|
81840
|
+
}
|
|
81856
81841
|
};
|
|
81857
81842
|
_canvas = null;
|
|
81858
81843
|
/**
|
|
@@ -81861,6 +81846,9 @@ class TrackpadDevice {
|
|
|
81861
81846
|
* for trackpad bursts. The coordinator enforces this by attaching
|
|
81862
81847
|
* trackpad before keyboard-mouse.
|
|
81863
81848
|
*
|
|
81849
|
+
* Keyboard listeners attach on `window` (not the canvas) so we still
|
|
81850
|
+
* see Ctrl keydown/keyup when focus is on a UI overlay.
|
|
81851
|
+
*
|
|
81864
81852
|
* @param canvas - The canvas element to listen to.
|
|
81865
81853
|
* @param global - The global app context (state, events, etc.).
|
|
81866
81854
|
*/
|
|
@@ -81868,13 +81856,20 @@ class TrackpadDevice {
|
|
|
81868
81856
|
this._canvas = canvas;
|
|
81869
81857
|
this._global = global;
|
|
81870
81858
|
canvas.addEventListener('wheel', this._onWheel, { passive: false });
|
|
81859
|
+
window.addEventListener('keydown', this._onKeyDown);
|
|
81860
|
+
window.addEventListener('keyup', this._onKeyUp);
|
|
81861
|
+
window.addEventListener('blur', this._onBlur);
|
|
81871
81862
|
}
|
|
81872
81863
|
detach() {
|
|
81873
81864
|
if (this._canvas) {
|
|
81874
81865
|
this._canvas.removeEventListener('wheel', this._onWheel);
|
|
81875
81866
|
this._canvas = null;
|
|
81876
81867
|
}
|
|
81868
|
+
window.removeEventListener('keydown', this._onKeyDown);
|
|
81869
|
+
window.removeEventListener('keyup', this._onKeyUp);
|
|
81870
|
+
window.removeEventListener('blur', this._onBlur);
|
|
81877
81871
|
this._global = null;
|
|
81872
|
+
this._ctrlDown = false;
|
|
81878
81873
|
this._orbit[0] = this._orbit[1] = 0;
|
|
81879
81874
|
this._pan[0] = this._pan[1] = 0;
|
|
81880
81875
|
this._zoom = 0;
|
|
@@ -81884,24 +81879,34 @@ class TrackpadDevice {
|
|
|
81884
81879
|
const orbitFactor = isFirstPerson ? cameraComponent.fov / 120 : 1;
|
|
81885
81880
|
const { deltas } = frame;
|
|
81886
81881
|
if (isOrbit) {
|
|
81887
|
-
// orbit rotate
|
|
81882
|
+
// Ctrl + scroll → orbit rotate
|
|
81888
81883
|
const v = tmpV$1.set(this._orbit[0], this._orbit[1], 0);
|
|
81889
81884
|
v.mulScalar(this.orbitSpeed * this.trackpadOrbitSensitivity * DISPLACEMENT_SCALE);
|
|
81890
81885
|
deltas.rotate.append([v.x, v.y, 0]);
|
|
81891
|
-
// pan in world space (matches desktop pan path); reuse tmpV after rotate append
|
|
81886
|
+
// shift + scroll → pan in world space (matches desktop pan path); reuse tmpV after rotate append
|
|
81892
81887
|
screenToWorld(cameraComponent, this._pan[0], this._pan[1], distance, tmpV$1);
|
|
81893
81888
|
tmpV$1.mulScalar(this.trackpadPanSensitivity);
|
|
81894
81889
|
deltas.move.append([tmpV$1.x, tmpV$1.y, 0]);
|
|
81895
|
-
// zoom along z; positive deltaY (scroll-down / pinch-in) → zoom out → +z for orbit
|
|
81890
|
+
// synthetic-Ctrl pinch → zoom along z; positive deltaY (scroll-down / pinch-in) → zoom out → +z for orbit
|
|
81896
81891
|
const zoomZ = this._zoom * this.wheelSpeed * this.trackpadZoomSensitivity * DISPLACEMENT_SCALE;
|
|
81897
81892
|
deltas.move.append([0, 0, zoomZ]);
|
|
81898
81893
|
}
|
|
81899
81894
|
else if (isFirstPerson) {
|
|
81900
|
-
//
|
|
81901
|
-
// pinch / Ctrl-swipe fall through to the wheel→forward path)
|
|
81895
|
+
// Ctrl + scroll → look around in fly/walk
|
|
81902
81896
|
const v = tmpV$1.set(this._orbit[0], this._orbit[1], 0);
|
|
81903
81897
|
v.mulScalar(this.orbitSpeed * orbitFactor * this.trackpadOrbitSensitivity * DISPLACEMENT_SCALE);
|
|
81904
81898
|
deltas.rotate.append([v.x, v.y, 0]);
|
|
81899
|
+
// shift + scroll → strafe (X) + vertical (Y) via the same
|
|
81900
|
+
// screen-space pan pipeline as orbit mode; the fly/walk
|
|
81901
|
+
// controllers translate this into sideways/vertical motion
|
|
81902
|
+
screenToWorld(cameraComponent, this._pan[0], this._pan[1], distance, tmpV$1);
|
|
81903
|
+
tmpV$1.mulScalar(this.trackpadPanSensitivity);
|
|
81904
|
+
deltas.move.append([tmpV$1.x, tmpV$1.y, 0]);
|
|
81905
|
+
// synthetic-Ctrl pinch → forward/back along z (same sign as the
|
|
81906
|
+
// bare-wheel path in KeyboardMouseDevice, so pinch and scroll
|
|
81907
|
+
// produce identical motion in fly/walk modes)
|
|
81908
|
+
const moveZ = -this._zoom * this.wheelSpeed * this.trackpadZoomSensitivity * DISPLACEMENT_SCALE;
|
|
81909
|
+
deltas.move.append([0, 0, moveZ]);
|
|
81905
81910
|
}
|
|
81906
81911
|
this._orbit[0] = this._orbit[1] = 0;
|
|
81907
81912
|
this._pan[0] = this._pan[1] = 0;
|
|
@@ -82263,1171 +82268,1019 @@ class MeshDebugOverlay {
|
|
|
82263
82268
|
}
|
|
82264
82269
|
}
|
|
82265
82270
|
|
|
82266
|
-
|
|
82267
|
-
|
|
82268
|
-
|
|
82269
|
-
|
|
82270
|
-
|
|
82271
|
-
|
|
82272
|
-
//
|
|
82273
|
-
|
|
82274
|
-
|
|
82275
|
-
|
|
82276
|
-
|
|
82277
|
-
|
|
82278
|
-
|
|
82279
|
-
|
|
82280
|
-
|
|
82281
|
-
|
|
82282
|
-
|
|
82283
|
-
|
|
82284
|
-
|
|
82285
|
-
|
|
82286
|
-
|
|
82287
|
-
|
|
82288
|
-
|
|
82289
|
-
|
|
82290
|
-
normalizedDepth = gl_FragCoord.z;
|
|
82291
|
-
} else {
|
|
82292
|
-
float linearDepth = 1.0 / gl_FragCoord.w;
|
|
82293
|
-
normalizedDepth = (linearDepth - camera_params.z) / (camera_params.y - camera_params.z);
|
|
82294
|
-
}
|
|
82295
|
-
|
|
82296
|
-
return vec4(gaussianColor.a * normalizedDepth, 0.0, 0.0, gaussianColor.a);
|
|
82297
|
-
}
|
|
82298
|
-
#else
|
|
82299
|
-
#ifndef PICK_CUSTOM_ID
|
|
82300
|
-
uniform uint meshInstanceId;
|
|
82301
|
-
|
|
82302
|
-
vec4 getPickOutput() {
|
|
82303
|
-
return encodePickOutput(meshInstanceId);
|
|
82271
|
+
const SVGNS = 'http://www.w3.org/2000/svg';
|
|
82272
|
+
const NUM_SAMPLES = 12;
|
|
82273
|
+
const BASE_OUTER_RADIUS = 0.2;
|
|
82274
|
+
const INNER_OUTER_RATIO = 0.17 / 0.2;
|
|
82275
|
+
// Screen-space diameter (in CSS pixels) used for both hover and target
|
|
82276
|
+
// rings when the scene has no collision data. With collision present the
|
|
82277
|
+
// rings render world-space (BASE_OUTER_RADIUS) so they orient to the
|
|
82278
|
+
// surface; without collision the picks fall back to splat depth and a
|
|
82279
|
+
// fixed pixel size is more legible. Selection happens in
|
|
82280
|
+
// screenPixelsForRing(), keyed on state.hasCollision.
|
|
82281
|
+
const SCREEN_OUTER_PIXELS = 48;
|
|
82282
|
+
const BEZIER_K = 1 / 6;
|
|
82283
|
+
const NORMAL_SMOOTH_FACTOR = 0.25;
|
|
82284
|
+
const NORMAL_SNAP_ANGLE = Math.PI / 4;
|
|
82285
|
+
const NORMAL_EPSILON$1 = 1e-6;
|
|
82286
|
+
const createNormalSnapDirections = () => {
|
|
82287
|
+
const result = [];
|
|
82288
|
+
for (let pitchStep = -2; pitchStep <= 2; pitchStep++) {
|
|
82289
|
+
const pitch = pitchStep * NORMAL_SNAP_ANGLE;
|
|
82290
|
+
const cp = Math.cos(pitch);
|
|
82291
|
+
const sy = Math.sin(pitch);
|
|
82292
|
+
if (Math.abs(cp) <= NORMAL_EPSILON$1) {
|
|
82293
|
+
result.push(new Vec3(0, sy > 0 ? 1 : -1, 0));
|
|
82294
|
+
continue;
|
|
82304
82295
|
}
|
|
82305
|
-
|
|
82306
|
-
|
|
82307
|
-
|
|
82308
|
-
#ifdef DEPTH_PICK_PASS
|
|
82309
|
-
#include "floatAsUintPS"
|
|
82310
|
-
#ifndef CAMERAPLANES
|
|
82311
|
-
#define CAMERAPLANES
|
|
82312
|
-
uniform vec4 camera_params; // x: 1/far, y: far, z: near, w: isOrtho
|
|
82313
|
-
#endif
|
|
82314
|
-
|
|
82315
|
-
vec4 getPickDepth() {
|
|
82316
|
-
float linearDepth;
|
|
82317
|
-
if (camera_params.w > 0.5) {
|
|
82318
|
-
linearDepth = gl_FragCoord.z;
|
|
82319
|
-
} else {
|
|
82320
|
-
float viewDist = 1.0 / gl_FragCoord.w;
|
|
82321
|
-
linearDepth = (viewDist - camera_params.z) / (camera_params.y - camera_params.z);
|
|
82296
|
+
for (let yawStep = 0; yawStep < 8; yawStep++) {
|
|
82297
|
+
const yaw = yawStep * NORMAL_SNAP_ANGLE;
|
|
82298
|
+
result.push(new Vec3(Math.cos(yaw) * cp, sy, Math.sin(yaw) * cp));
|
|
82322
82299
|
}
|
|
82323
|
-
return float2uint(linearDepth);
|
|
82324
82300
|
}
|
|
82325
|
-
|
|
82326
|
-
|
|
82327
|
-
const
|
|
82328
|
-
|
|
82329
|
-
|
|
82330
|
-
|
|
82331
|
-
|
|
82332
|
-
return vec4f(col) * inv;
|
|
82333
|
-
}
|
|
82334
|
-
|
|
82335
|
-
#ifdef GSPLAT_PICK_DEPTH
|
|
82336
|
-
#ifndef CAMERAPLANES
|
|
82337
|
-
#define CAMERAPLANES
|
|
82338
|
-
uniform camera_params: vec4f; // x: 1/far, y: far, z: near, w: isOrtho
|
|
82339
|
-
#endif
|
|
82340
|
-
|
|
82341
|
-
fn getPickOutput() -> vec4f {
|
|
82342
|
-
var normalizedDepth: f32;
|
|
82343
|
-
if (uniform.camera_params.w > 0.5) {
|
|
82344
|
-
normalizedDepth = pcPosition.z;
|
|
82345
|
-
} else {
|
|
82346
|
-
let linearDepth = 1.0 / pcPosition.w;
|
|
82347
|
-
normalizedDepth = (linearDepth - uniform.camera_params.z) / (uniform.camera_params.y - uniform.camera_params.z);
|
|
82348
|
-
}
|
|
82349
|
-
|
|
82350
|
-
let a = f32(gaussianColor.a);
|
|
82351
|
-
return vec4f(a * normalizedDepth, 0.0, 0.0, a);
|
|
82301
|
+
return result;
|
|
82302
|
+
};
|
|
82303
|
+
const NORMAL_SNAP_DIRECTIONS = createNormalSnapDirections();
|
|
82304
|
+
const snapNormal = (nx, ny, nz, out) => {
|
|
82305
|
+
const len = Math.sqrt(nx * nx + ny * ny + nz * nz);
|
|
82306
|
+
if (len <= NORMAL_EPSILON$1) {
|
|
82307
|
+
return out.set(0, 1, 0);
|
|
82352
82308
|
}
|
|
82353
|
-
|
|
82354
|
-
|
|
82355
|
-
|
|
82356
|
-
|
|
82357
|
-
|
|
82358
|
-
|
|
82359
|
-
|
|
82360
|
-
|
|
82361
|
-
|
|
82362
|
-
|
|
82363
|
-
|
|
82364
|
-
|
|
82365
|
-
#ifndef CAMERAPLANES
|
|
82366
|
-
#define CAMERAPLANES
|
|
82367
|
-
uniform camera_params: vec4f; // x: 1/far, y: far, z: near, w: isOrtho
|
|
82368
|
-
#endif
|
|
82369
|
-
|
|
82370
|
-
fn getPickDepth() -> vec4f {
|
|
82371
|
-
var linearDepth: f32;
|
|
82372
|
-
if (uniform.camera_params.w > 0.5) {
|
|
82373
|
-
linearDepth = pcPosition.z;
|
|
82374
|
-
} else {
|
|
82375
|
-
let viewDist = 1.0 / pcPosition.w;
|
|
82376
|
-
linearDepth = (viewDist - uniform.camera_params.z) / (uniform.camera_params.y - uniform.camera_params.z);
|
|
82309
|
+
const invLen = 1 / len;
|
|
82310
|
+
const x = nx * invLen;
|
|
82311
|
+
const y = ny * invLen;
|
|
82312
|
+
const z = nz * invLen;
|
|
82313
|
+
let best = NORMAL_SNAP_DIRECTIONS[0];
|
|
82314
|
+
let bestDot = -Infinity;
|
|
82315
|
+
for (let i = 0; i < NORMAL_SNAP_DIRECTIONS.length; i++) {
|
|
82316
|
+
const candidate = NORMAL_SNAP_DIRECTIONS[i];
|
|
82317
|
+
const dot = candidate.x * x + candidate.y * y + candidate.z * z;
|
|
82318
|
+
if (dot > bestDot) {
|
|
82319
|
+
bestDot = dot;
|
|
82320
|
+
best = candidate;
|
|
82377
82321
|
}
|
|
82378
|
-
return float2uint(linearDepth);
|
|
82379
|
-
}
|
|
82380
|
-
#endif
|
|
82381
|
-
`;
|
|
82382
|
-
const pickPassChunkInjected = [
|
|
82383
|
-
'#ifdef PICK_PASS',
|
|
82384
|
-
' #define GSPLAT_PICK_DEPTH',
|
|
82385
|
-
' #include "pickPS"',
|
|
82386
|
-
'#endif'
|
|
82387
|
-
].join('\n');
|
|
82388
|
-
const safeChunkReplace = (s, find, repl) => {
|
|
82389
|
-
const out = s.replace(find, repl);
|
|
82390
|
-
if (out === s) {
|
|
82391
|
-
throw new Error('picker: engine gsplat/pick chunk patch failed (engine version mismatch?)');
|
|
82392
82322
|
}
|
|
82393
|
-
return out;
|
|
82323
|
+
return out.copy(best);
|
|
82394
82324
|
};
|
|
82395
|
-
const
|
|
82396
|
-
|
|
82325
|
+
const tmpV = new Vec3();
|
|
82326
|
+
const tmpScreen = new Vec3();
|
|
82327
|
+
const tangent = new Vec3();
|
|
82328
|
+
const bitangent = new Vec3();
|
|
82329
|
+
const worldPt = new Vec3();
|
|
82330
|
+
const up = new Vec3(0, 1, 0);
|
|
82331
|
+
const right = new Vec3(1, 0, 0);
|
|
82332
|
+
// Compute the world-space radius such that a circle at `pos` projects to a
|
|
82333
|
+
// ring of `pixelDiameter` on screen. Used by the no-collision sizing path
|
|
82334
|
+
// (see screenPixelsForRing) to keep a constant on-screen diameter
|
|
82335
|
+
// regardless of camera distance.
|
|
82336
|
+
const worldRadiusForPixels = (camera, canvasHeight, pos, pixelDiameter) => {
|
|
82337
|
+
const cam = camera.camera;
|
|
82338
|
+
if (cam.projection === PROJECTION_ORTHOGRAPHIC) {
|
|
82339
|
+
return pixelDiameter * cam.orthoHeight / canvasHeight;
|
|
82340
|
+
}
|
|
82341
|
+
const camPos = camera.getPosition();
|
|
82342
|
+
const dx = pos.x - camPos.x;
|
|
82343
|
+
const dy = pos.y - camPos.y;
|
|
82344
|
+
const dz = pos.z - camPos.z;
|
|
82345
|
+
const distance = Math.sqrt(dx * dx + dy * dy + dz * dz);
|
|
82346
|
+
const halfFovTan = Math.tan(cam.fov * Math.PI / 360);
|
|
82347
|
+
return pixelDiameter * distance * halfFovTan / canvasHeight;
|
|
82397
82348
|
};
|
|
82398
|
-
const
|
|
82399
|
-
|
|
82349
|
+
const buildBezierRing = (sx, sy) => {
|
|
82350
|
+
const n = sx.length;
|
|
82351
|
+
let p = `M${sx[0].toFixed(1)},${sy[0].toFixed(1)}`;
|
|
82352
|
+
for (let i = 0; i < n; i++) {
|
|
82353
|
+
const i0 = (i - 1 + n) % n;
|
|
82354
|
+
const i1 = i;
|
|
82355
|
+
const i2 = (i + 1) % n;
|
|
82356
|
+
const i3 = (i + 2) % n;
|
|
82357
|
+
const cp1x = sx[i1] + (sx[i2] - sx[i0]) * BEZIER_K;
|
|
82358
|
+
const cp1y = sy[i1] + (sy[i2] - sy[i0]) * BEZIER_K;
|
|
82359
|
+
const cp2x = sx[i2] - (sx[i3] - sx[i1]) * BEZIER_K;
|
|
82360
|
+
const cp2y = sy[i2] - (sy[i3] - sy[i1]) * BEZIER_K;
|
|
82361
|
+
p += ` C${cp1x.toFixed(1)},${cp1y.toFixed(1)} ${cp2x.toFixed(1)},${cp2y.toFixed(1)} ${sx[i2].toFixed(1)},${sy[i2].toFixed(1)}`;
|
|
82362
|
+
}
|
|
82363
|
+
return `${p} Z`;
|
|
82400
82364
|
};
|
|
82401
|
-
|
|
82402
|
-
|
|
82403
|
-
|
|
82404
|
-
|
|
82405
|
-
|
|
82406
|
-
|
|
82407
|
-
|
|
82408
|
-
|
|
82409
|
-
|
|
82410
|
-
|
|
82411
|
-
|
|
82412
|
-
|
|
82413
|
-
|
|
82414
|
-
|
|
82415
|
-
|
|
82416
|
-
|
|
82417
|
-
|
|
82418
|
-
|
|
82419
|
-
|
|
82420
|
-
|
|
82421
|
-
|
|
82422
|
-
|
|
82423
|
-
|
|
82424
|
-
|
|
82425
|
-
|
|
82426
|
-
|
|
82427
|
-
|
|
82428
|
-
|
|
82429
|
-
|
|
82430
|
-
|
|
82431
|
-
|
|
82432
|
-
// Convert 16-bit half-float to 32-bit float using bit manipulation.
|
|
82433
|
-
const half2Float = (h) => {
|
|
82434
|
-
const sign = (h & 0x8000) << 16;
|
|
82435
|
-
const exponent = (h & 0x7C00) >> 10;
|
|
82436
|
-
const mantissa = h & 0x03FF;
|
|
82437
|
-
if (exponent === 0) {
|
|
82438
|
-
if (mantissa === 0) {
|
|
82439
|
-
uint32[0] = sign;
|
|
82365
|
+
class CursorRing {
|
|
82366
|
+
path;
|
|
82367
|
+
svg;
|
|
82368
|
+
canvas;
|
|
82369
|
+
camera;
|
|
82370
|
+
smoothing;
|
|
82371
|
+
smoothNx = 0;
|
|
82372
|
+
smoothNy = 1;
|
|
82373
|
+
smoothNz = 0;
|
|
82374
|
+
hasSmoothedNormal = false;
|
|
82375
|
+
outerX = new Float64Array(NUM_SAMPLES);
|
|
82376
|
+
outerY = new Float64Array(NUM_SAMPLES);
|
|
82377
|
+
innerX = new Float64Array(NUM_SAMPLES);
|
|
82378
|
+
innerY = new Float64Array(NUM_SAMPLES);
|
|
82379
|
+
constructor(svg, canvas, camera, smoothing) {
|
|
82380
|
+
this.svg = svg;
|
|
82381
|
+
this.canvas = canvas;
|
|
82382
|
+
this.camera = camera;
|
|
82383
|
+
this.smoothing = smoothing;
|
|
82384
|
+
this.path = document.createElementNS(SVGNS, 'path');
|
|
82385
|
+
this.path.setAttribute('fill', 'white');
|
|
82386
|
+
this.path.setAttribute('fill-opacity', '0.6');
|
|
82387
|
+
this.path.setAttribute('fill-rule', 'evenodd');
|
|
82388
|
+
this.path.setAttribute('stroke', 'none');
|
|
82389
|
+
this.path.style.display = 'none';
|
|
82390
|
+
svg.appendChild(this.path);
|
|
82391
|
+
}
|
|
82392
|
+
projectCircle(px, py, pz, nx, ny, nz, radius, outX, outY) {
|
|
82393
|
+
const normal = tmpV.set(nx, ny, nz);
|
|
82394
|
+
if (Math.abs(normal.y) < 0.99) {
|
|
82395
|
+
tangent.cross(normal, up).normalize();
|
|
82440
82396
|
}
|
|
82441
82397
|
else {
|
|
82442
|
-
|
|
82443
|
-
|
|
82444
|
-
|
|
82445
|
-
|
|
82446
|
-
|
|
82447
|
-
|
|
82448
|
-
|
|
82398
|
+
tangent.cross(normal, right).normalize();
|
|
82399
|
+
}
|
|
82400
|
+
bitangent.cross(normal, tangent);
|
|
82401
|
+
const cam = this.camera.camera;
|
|
82402
|
+
const angleStep = (2 * Math.PI) / NUM_SAMPLES;
|
|
82403
|
+
for (let i = 0; i < NUM_SAMPLES; i++) {
|
|
82404
|
+
const theta = i * angleStep;
|
|
82405
|
+
const ct = Math.cos(theta);
|
|
82406
|
+
const st = Math.sin(theta);
|
|
82407
|
+
const tx = ct * tangent.x + st * bitangent.x;
|
|
82408
|
+
const ty = ct * tangent.y + st * bitangent.y;
|
|
82409
|
+
const tz = ct * tangent.z + st * bitangent.z;
|
|
82410
|
+
worldPt.set(px + tx * radius, py + ty * radius, pz + tz * radius);
|
|
82411
|
+
cam.worldToScreen(worldPt, tmpScreen);
|
|
82412
|
+
outX[i] = tmpScreen.x;
|
|
82413
|
+
outY[i] = tmpScreen.y;
|
|
82449
82414
|
}
|
|
82450
82415
|
}
|
|
82451
|
-
|
|
82452
|
-
|
|
82453
|
-
|
|
82454
|
-
|
|
82455
|
-
|
|
82416
|
+
// screenPixels: null → world-space ring (fixed world radius, shrinks
|
|
82417
|
+
// with distance); number → constant on-screen diameter in CSS pixels.
|
|
82418
|
+
render(pos, normal, screenPixels) {
|
|
82419
|
+
snapNormal(normal.x, normal.y, normal.z, tmpV);
|
|
82420
|
+
let nx = tmpV.x;
|
|
82421
|
+
let ny = tmpV.y;
|
|
82422
|
+
let nz = tmpV.z;
|
|
82423
|
+
if (this.smoothing) {
|
|
82424
|
+
if (this.hasSmoothedNormal) {
|
|
82425
|
+
const t = NORMAL_SMOOTH_FACTOR;
|
|
82426
|
+
nx = this.smoothNx + (nx - this.smoothNx) * t;
|
|
82427
|
+
ny = this.smoothNy + (ny - this.smoothNy) * t;
|
|
82428
|
+
nz = this.smoothNz + (nz - this.smoothNz) * t;
|
|
82429
|
+
const len = Math.sqrt(nx * nx + ny * ny + nz * nz);
|
|
82430
|
+
if (len > 1e-6) {
|
|
82431
|
+
const invLen = 1.0 / len;
|
|
82432
|
+
nx *= invLen;
|
|
82433
|
+
ny *= invLen;
|
|
82434
|
+
nz *= invLen;
|
|
82435
|
+
}
|
|
82436
|
+
}
|
|
82437
|
+
this.smoothNx = nx;
|
|
82438
|
+
this.smoothNy = ny;
|
|
82439
|
+
this.smoothNz = nz;
|
|
82440
|
+
this.hasSmoothedNormal = true;
|
|
82441
|
+
}
|
|
82442
|
+
const outerRadius = screenPixels !== null ?
|
|
82443
|
+
worldRadiusForPixels(this.camera, this.canvas.clientHeight || 1, pos, screenPixels) :
|
|
82444
|
+
BASE_OUTER_RADIUS;
|
|
82445
|
+
const innerRadius = outerRadius * INNER_OUTER_RATIO;
|
|
82446
|
+
this.projectCircle(pos.x, pos.y, pos.z, nx, ny, nz, outerRadius, this.outerX, this.outerY);
|
|
82447
|
+
this.projectCircle(pos.x, pos.y, pos.z, nx, ny, nz, innerRadius, this.innerX, this.innerY);
|
|
82448
|
+
this.path.setAttribute('d', `${buildBezierRing(this.outerX, this.outerY)} ${buildBezierRing(this.innerX, this.innerY)}`);
|
|
82449
|
+
this.path.style.display = '';
|
|
82450
|
+
this.svg.style.display = '';
|
|
82456
82451
|
}
|
|
82457
|
-
|
|
82458
|
-
|
|
82459
|
-
|
|
82460
|
-
const device = app.graphicsDevice;
|
|
82461
|
-
const existing = pickerShaderPatchState.get(device);
|
|
82462
|
-
if (existing) {
|
|
82463
|
-
existing.refCount++;
|
|
82464
|
-
return;
|
|
82452
|
+
hide() {
|
|
82453
|
+
this.path.style.display = 'none';
|
|
82454
|
+
this.hasSmoothedNormal = false;
|
|
82465
82455
|
}
|
|
82466
|
-
|
|
82467
|
-
|
|
82468
|
-
|
|
82469
|
-
|
|
82470
|
-
|
|
82471
|
-
|
|
82472
|
-
|
|
82473
|
-
|
|
82474
|
-
|
|
82475
|
-
|
|
82476
|
-
|
|
82477
|
-
|
|
82478
|
-
|
|
82479
|
-
|
|
82480
|
-
|
|
82456
|
+
}
|
|
82457
|
+
class NavCursor {
|
|
82458
|
+
svg;
|
|
82459
|
+
hoverRing;
|
|
82460
|
+
targetRing;
|
|
82461
|
+
camera;
|
|
82462
|
+
collision;
|
|
82463
|
+
canvas;
|
|
82464
|
+
state;
|
|
82465
|
+
app;
|
|
82466
|
+
onPrerender;
|
|
82467
|
+
// True when the hover ring should track the pointer. Only walk mode
|
|
82468
|
+
// (with mouse navigation, not gaming controls) shows the hover ring;
|
|
82469
|
+
// fly/orbit only show the target ring on click.
|
|
82470
|
+
hoverActive = false;
|
|
82471
|
+
navigating = false;
|
|
82472
|
+
targetPos = null;
|
|
82473
|
+
targetNormal = null;
|
|
82474
|
+
targetMode = null;
|
|
82475
|
+
onPointerMove;
|
|
82476
|
+
onPointerLeave;
|
|
82477
|
+
collisionTarget = {
|
|
82478
|
+
position: new Vec3(),
|
|
82479
|
+
normal: new Vec3()
|
|
82481
82480
|
};
|
|
82482
|
-
|
|
82483
|
-
|
|
82484
|
-
|
|
82485
|
-
|
|
82486
|
-
|
|
82487
|
-
|
|
82488
|
-
|
|
82489
|
-
|
|
82490
|
-
|
|
82491
|
-
|
|
82492
|
-
|
|
82481
|
+
constructor(app, camera, collision, events, state) {
|
|
82482
|
+
this.camera = camera;
|
|
82483
|
+
this.collision = collision;
|
|
82484
|
+
this.canvas = app.graphicsDevice.canvas;
|
|
82485
|
+
this.state = state;
|
|
82486
|
+
this.app = app;
|
|
82487
|
+
this.svg = document.createElementNS(SVGNS, 'svg');
|
|
82488
|
+
this.svg.style.cssText = 'position:absolute;top:0;left:0;width:100%;height:100%;pointer-events:none;overflow:visible;z-index:1';
|
|
82489
|
+
this.canvas.parentElement.appendChild(this.svg);
|
|
82490
|
+
this.hoverRing = new CursorRing(this.svg, this.canvas, camera, true);
|
|
82491
|
+
this.targetRing = new CursorRing(this.svg, this.canvas, camera, false);
|
|
82492
|
+
this.svg.style.display = 'none';
|
|
82493
|
+
this.onPointerMove = (e) => {
|
|
82494
|
+
if (e.pointerType === 'touch' || e.buttons) {
|
|
82495
|
+
this.hoverRing.hide();
|
|
82496
|
+
return;
|
|
82497
|
+
}
|
|
82498
|
+
this.updateCursor(e.offsetX, e.offsetY);
|
|
82499
|
+
};
|
|
82500
|
+
this.onPointerLeave = () => {
|
|
82501
|
+
this.hoverRing.hide();
|
|
82502
|
+
};
|
|
82503
|
+
this.canvas.addEventListener('pointermove', this.onPointerMove);
|
|
82504
|
+
this.canvas.addEventListener('pointerleave', this.onPointerLeave);
|
|
82505
|
+
const updateActive = () => {
|
|
82506
|
+
// Hover ring only in walk mode with mouse navigation. Gaming
|
|
82507
|
+
// controls use pointer-lock and don't need a hover preview.
|
|
82508
|
+
this.hoverActive = state.cameraMode === 'walk' && !state.gamingControls;
|
|
82509
|
+
this.hoverRing.hide();
|
|
82510
|
+
if (this.targetMode && this.targetMode !== state.cameraMode) {
|
|
82511
|
+
this.navigating = false;
|
|
82512
|
+
this.clearTarget();
|
|
82513
|
+
}
|
|
82514
|
+
};
|
|
82515
|
+
events.on('cameraMode:changed', updateActive);
|
|
82516
|
+
events.on('inputMode:changed', updateActive);
|
|
82517
|
+
events.on('gamingControls:changed', updateActive);
|
|
82518
|
+
events.on('navigateTo', () => {
|
|
82519
|
+
this.navigating = true;
|
|
82520
|
+
this.hoverRing.hide();
|
|
82521
|
+
});
|
|
82522
|
+
events.on('navigateCancel', () => {
|
|
82523
|
+
this.navigating = false;
|
|
82524
|
+
this.clearTarget();
|
|
82525
|
+
});
|
|
82526
|
+
events.on('navigateComplete', () => {
|
|
82527
|
+
this.navigating = false;
|
|
82528
|
+
this.clearTarget();
|
|
82529
|
+
});
|
|
82530
|
+
events.on('navTarget:set', (pos, normal) => {
|
|
82531
|
+
const mode = state.cameraMode === 'walk' || state.cameraMode === 'fly' ?
|
|
82532
|
+
state.cameraMode : 'walk';
|
|
82533
|
+
this.setTarget(pos, normal, mode);
|
|
82534
|
+
});
|
|
82535
|
+
events.on('navTarget:clear', () => {
|
|
82536
|
+
this.clearTarget();
|
|
82537
|
+
});
|
|
82538
|
+
events.on('orbitTarget:set', (pos, normal) => {
|
|
82539
|
+
this.navigating = false;
|
|
82540
|
+
this.setTarget(pos, normal, 'orbit');
|
|
82541
|
+
});
|
|
82542
|
+
events.on('orbitTarget:clear', () => {
|
|
82543
|
+
if (this.targetMode === 'orbit') {
|
|
82544
|
+
this.clearTarget();
|
|
82545
|
+
}
|
|
82546
|
+
});
|
|
82547
|
+
this.onPrerender = () => {
|
|
82548
|
+
this.updateTarget();
|
|
82549
|
+
};
|
|
82550
|
+
app.on('prerender', this.onPrerender);
|
|
82551
|
+
updateActive();
|
|
82493
82552
|
}
|
|
82494
|
-
|
|
82495
|
-
|
|
82496
|
-
|
|
82553
|
+
// Ring sizing is per-scene: when the scene has collision data we get
|
|
82554
|
+
// accurate surface picks with real normals, so world-space rings read
|
|
82555
|
+
// as physical footprints on the surface with visible orientation.
|
|
82556
|
+
// Without collision the pick falls back to splat depth which can be
|
|
82557
|
+
// noisy at distance, so a fixed screen-size ring is more legible.
|
|
82558
|
+
screenPixelsForRing() {
|
|
82559
|
+
return this.state.hasCollision ? null : SCREEN_OUTER_PIXELS;
|
|
82497
82560
|
}
|
|
82498
|
-
|
|
82499
|
-
|
|
82500
|
-
|
|
82501
|
-
|
|
82502
|
-
|
|
82503
|
-
|
|
82504
|
-
pickerShaderPatchState.delete(device);
|
|
82505
|
-
};
|
|
82506
|
-
const createPickCameraSnapshot = () => ({
|
|
82507
|
-
position: new Vec3(),
|
|
82508
|
-
viewMatrix: new Mat4(),
|
|
82509
|
-
projectionMatrix: new Mat4(),
|
|
82510
|
-
nearClip: 0,
|
|
82511
|
-
farClip: 0,
|
|
82512
|
-
projection: 0
|
|
82513
|
-
});
|
|
82514
|
-
const captureCameraSnapshot = (camera, out) => {
|
|
82515
|
-
const cam = camera.camera;
|
|
82516
|
-
out.position.copy(camera.getPosition());
|
|
82517
|
-
out.viewMatrix.copy(cam.viewMatrix);
|
|
82518
|
-
out.projectionMatrix.copy(cam.projectionMatrix);
|
|
82519
|
-
out.nearClip = cam.nearClip;
|
|
82520
|
-
out.farClip = cam.farClip;
|
|
82521
|
-
out.projection = cam.projection;
|
|
82522
|
-
};
|
|
82523
|
-
const getWorldPoint = (camera, x, y, width, height, normalizedDepth, out) => {
|
|
82524
|
-
if (!Number.isFinite(normalizedDepth) || normalizedDepth < 0 || normalizedDepth > 1) {
|
|
82525
|
-
return null;
|
|
82526
|
-
}
|
|
82527
|
-
const { farClip: far, nearClip: near } = camera;
|
|
82528
|
-
const ndcDepth = camera.projection === PROJECTION_ORTHOGRAPHIC ?
|
|
82529
|
-
normalizedDepth :
|
|
82530
|
-
far * normalizedDepth / (normalizedDepth * (far - near) + near);
|
|
82531
|
-
viewProjMat.mul2(camera.projectionMatrix, camera.viewMatrix).invert();
|
|
82532
|
-
vec4.set(x / width * 2 - 1, (1 - y / height) * 2 - 1, ndcDepth * 2 - 1, 1);
|
|
82533
|
-
viewProjMat.transformVec4(vec4, vec4);
|
|
82534
|
-
if (!Number.isFinite(vec4.w) || Math.abs(vec4.w) < 1e-8) {
|
|
82535
|
-
return null;
|
|
82536
|
-
}
|
|
82537
|
-
vec4.mulScalar(1 / vec4.w);
|
|
82538
|
-
if (!Number.isFinite(vec4.x) || !Number.isFinite(vec4.y) || !Number.isFinite(vec4.z)) {
|
|
82539
|
-
return null;
|
|
82561
|
+
setTarget(pos, normal, mode) {
|
|
82562
|
+
this.targetPos = pos.clone();
|
|
82563
|
+
this.targetNormal = normal.clone();
|
|
82564
|
+
this.targetMode = mode;
|
|
82565
|
+
this.hoverRing.hide();
|
|
82566
|
+
this.targetRing.hide();
|
|
82540
82567
|
}
|
|
82541
|
-
|
|
82542
|
-
|
|
82543
|
-
|
|
82544
|
-
|
|
82545
|
-
|
|
82546
|
-
if (len > 1e-6) {
|
|
82547
|
-
normal.mulScalar(1 / len);
|
|
82568
|
+
clearTarget() {
|
|
82569
|
+
this.targetPos = null;
|
|
82570
|
+
this.targetNormal = null;
|
|
82571
|
+
this.targetMode = null;
|
|
82572
|
+
this.targetRing.hide();
|
|
82548
82573
|
}
|
|
82549
|
-
|
|
82550
|
-
|
|
82574
|
+
pickCollision(offsetX, offsetY) {
|
|
82575
|
+
if (!this.collision) {
|
|
82576
|
+
return null;
|
|
82577
|
+
}
|
|
82578
|
+
const { camera, collision } = this;
|
|
82579
|
+
const cameraPos = camera.getPosition();
|
|
82580
|
+
camera.camera.screenToWorld(offsetX, offsetY, 1.0, tmpV);
|
|
82581
|
+
tmpV.sub(cameraPos).normalize();
|
|
82582
|
+
const hit = collision.queryRay(cameraPos.x, cameraPos.y, cameraPos.z, tmpV.x, tmpV.y, tmpV.z, camera.camera.farClip);
|
|
82583
|
+
if (!hit) {
|
|
82584
|
+
return null;
|
|
82585
|
+
}
|
|
82586
|
+
const sn = collision.querySurfaceNormal(hit.x, hit.y, hit.z, tmpV.x, tmpV.y, tmpV.z);
|
|
82587
|
+
this.collisionTarget.position.set(hit.x, hit.y, hit.z);
|
|
82588
|
+
this.collisionTarget.normal.set(sn.nx, sn.ny, sn.nz);
|
|
82589
|
+
return this.collisionTarget;
|
|
82551
82590
|
}
|
|
82552
|
-
|
|
82553
|
-
|
|
82554
|
-
|
|
82555
|
-
|
|
82556
|
-
|
|
82557
|
-
|
|
82558
|
-
|
|
82559
|
-
|
|
82560
|
-
|
|
82561
|
-
|
|
82591
|
+
updateCursor(offsetX, offsetY) {
|
|
82592
|
+
if (!this.hoverActive || this.navigating) {
|
|
82593
|
+
this.hoverRing.hide();
|
|
82594
|
+
return;
|
|
82595
|
+
}
|
|
82596
|
+
const target = this.pickCollision(offsetX, offsetY);
|
|
82597
|
+
if (!target) {
|
|
82598
|
+
this.hoverRing.hide();
|
|
82599
|
+
return;
|
|
82600
|
+
}
|
|
82601
|
+
this.hoverRing.render(target.position, target.normal, this.screenPixelsForRing());
|
|
82562
82602
|
}
|
|
82563
|
-
|
|
82564
|
-
|
|
82565
|
-
|
|
82566
|
-
|
|
82567
|
-
|
|
82568
|
-
|
|
82569
|
-
|
|
82570
|
-
|
|
82571
|
-
|
|
82572
|
-
|
|
82573
|
-
|
|
82574
|
-
// symmetric, Smith 1961). Returns null on degenerate input.
|
|
82575
|
-
const fitPlaneOnce = (points) => {
|
|
82576
|
-
const n = points.length;
|
|
82577
|
-
if (n < 3)
|
|
82578
|
-
return null;
|
|
82579
|
-
let cx = 0, cy = 0, cz = 0;
|
|
82580
|
-
for (let i = 0; i < n; i++) {
|
|
82581
|
-
cx += points[i].x;
|
|
82582
|
-
cy += points[i].y;
|
|
82583
|
-
cz += points[i].z;
|
|
82603
|
+
updateTarget() {
|
|
82604
|
+
if (!this.targetPos || !this.targetNormal || !this.targetMode) {
|
|
82605
|
+
return;
|
|
82606
|
+
}
|
|
82607
|
+
const camPos = this.camera.getPosition();
|
|
82608
|
+
const dist = camPos.distance(this.targetPos);
|
|
82609
|
+
if (this.targetMode !== 'orbit' && dist < 2.0) {
|
|
82610
|
+
this.targetRing.hide();
|
|
82611
|
+
return;
|
|
82612
|
+
}
|
|
82613
|
+
this.targetRing.render(this.targetPos, this.targetNormal, this.screenPixelsForRing());
|
|
82584
82614
|
}
|
|
82585
|
-
|
|
82586
|
-
|
|
82587
|
-
|
|
82588
|
-
|
|
82589
|
-
|
|
82590
|
-
const dx = points[i].x - cx;
|
|
82591
|
-
const dy = points[i].y - cy;
|
|
82592
|
-
const dz = points[i].z - cz;
|
|
82593
|
-
cxx += dx * dx;
|
|
82594
|
-
cxy += dx * dy;
|
|
82595
|
-
cxz += dx * dz;
|
|
82596
|
-
cyy += dy * dy;
|
|
82597
|
-
cyz += dy * dz;
|
|
82598
|
-
czz += dz * dz;
|
|
82615
|
+
destroy() {
|
|
82616
|
+
this.app.off('prerender', this.onPrerender);
|
|
82617
|
+
this.canvas.removeEventListener('pointermove', this.onPointerMove);
|
|
82618
|
+
this.canvas.removeEventListener('pointerleave', this.onPointerLeave);
|
|
82619
|
+
this.svg.remove();
|
|
82599
82620
|
}
|
|
82600
|
-
|
|
82601
|
-
|
|
82602
|
-
|
|
82603
|
-
|
|
82604
|
-
|
|
82605
|
-
|
|
82606
|
-
|
|
82607
|
-
|
|
82608
|
-
|
|
82609
|
-
|
|
82610
|
-
|
|
82611
|
-
|
|
82612
|
-
const
|
|
82613
|
-
|
|
82614
|
-
|
|
82615
|
-
|
|
82616
|
-
|
|
82617
|
-
|
|
82618
|
-
|
|
82619
|
-
|
|
82620
|
-
|
|
82621
|
-
|
|
82622
|
-
|
|
82623
|
-
|
|
82624
|
-
|
|
82625
|
-
|
|
82626
|
-
|
|
82627
|
-
|
|
82628
|
-
|
|
82629
|
-
|
|
82630
|
-
|
|
82631
|
-
|
|
82632
|
-
|
|
82633
|
-
lSq = l1;
|
|
82621
|
+
}
|
|
82622
|
+
|
|
82623
|
+
/**
|
|
82624
|
+
* World picking for splat scenes.
|
|
82625
|
+
*
|
|
82626
|
+
* Uses a custom `pickPS` / `gsplatPS` patch plus `RGBA16F` alpha-weighted depth accumulation,
|
|
82627
|
+
* because the stock pick pass encodes splat IDs / last-fragment depth rather than expected depth.
|
|
82628
|
+
*/
|
|
82629
|
+
// Override global picking to pack alpha-weighted splat depth instead of meshInstance id.
|
|
82630
|
+
const pickDepthGlsl = /* glsl */ `
|
|
82631
|
+
vec4 encodePickOutput(uint id) {
|
|
82632
|
+
const vec4 inv = vec4(1.0 / 255.0);
|
|
82633
|
+
const uvec4 shifts = uvec4(16, 8, 0, 24);
|
|
82634
|
+
uvec4 col = (uvec4(id) >> shifts) & uvec4(0xff);
|
|
82635
|
+
return vec4(col) * inv;
|
|
82636
|
+
}
|
|
82637
|
+
|
|
82638
|
+
#ifdef GSPLAT_PICK_DEPTH
|
|
82639
|
+
#ifndef CAMERAPLANES
|
|
82640
|
+
#define CAMERAPLANES
|
|
82641
|
+
uniform vec4 camera_params; // x: 1/far, y: far, z: near, w: isOrtho
|
|
82642
|
+
#endif
|
|
82643
|
+
|
|
82644
|
+
vec4 getPickOutput() {
|
|
82645
|
+
float normalizedDepth;
|
|
82646
|
+
if (camera_params.w > 0.5) {
|
|
82647
|
+
normalizedDepth = gl_FragCoord.z;
|
|
82648
|
+
} else {
|
|
82649
|
+
float linearDepth = 1.0 / gl_FragCoord.w;
|
|
82650
|
+
normalizedDepth = (linearDepth - camera_params.z) / (camera_params.y - camera_params.z);
|
|
82651
|
+
}
|
|
82652
|
+
|
|
82653
|
+
return vec4(gaussianColor.a * normalizedDepth, 0.0, 0.0, gaussianColor.a);
|
|
82634
82654
|
}
|
|
82635
|
-
|
|
82636
|
-
|
|
82637
|
-
|
|
82638
|
-
|
|
82639
|
-
|
|
82655
|
+
#else
|
|
82656
|
+
#ifndef PICK_CUSTOM_ID
|
|
82657
|
+
uniform uint meshInstanceId;
|
|
82658
|
+
|
|
82659
|
+
vec4 getPickOutput() {
|
|
82660
|
+
return encodePickOutput(meshInstanceId);
|
|
82661
|
+
}
|
|
82662
|
+
#endif
|
|
82663
|
+
#endif
|
|
82664
|
+
|
|
82665
|
+
#ifdef DEPTH_PICK_PASS
|
|
82666
|
+
#include "floatAsUintPS"
|
|
82667
|
+
#ifndef CAMERAPLANES
|
|
82668
|
+
#define CAMERAPLANES
|
|
82669
|
+
uniform vec4 camera_params; // x: 1/far, y: far, z: near, w: isOrtho
|
|
82670
|
+
#endif
|
|
82671
|
+
|
|
82672
|
+
vec4 getPickDepth() {
|
|
82673
|
+
float linearDepth;
|
|
82674
|
+
if (camera_params.w > 0.5) {
|
|
82675
|
+
linearDepth = gl_FragCoord.z;
|
|
82676
|
+
} else {
|
|
82677
|
+
float viewDist = 1.0 / gl_FragCoord.w;
|
|
82678
|
+
linearDepth = (viewDist - camera_params.z) / (camera_params.y - camera_params.z);
|
|
82679
|
+
}
|
|
82680
|
+
return float2uint(linearDepth);
|
|
82640
82681
|
}
|
|
82641
|
-
|
|
82642
|
-
|
|
82643
|
-
|
|
82644
|
-
|
|
82645
|
-
|
|
82682
|
+
#endif
|
|
82683
|
+
`;
|
|
82684
|
+
const pickDepthWgsl = /* wgsl */ `
|
|
82685
|
+
fn encodePickOutput(id: u32) -> vec4f {
|
|
82686
|
+
let inv: vec4f = vec4f(1.0 / 255.0);
|
|
82687
|
+
let shifts: vec4u = vec4u(16u, 8u, 0u, 24u);
|
|
82688
|
+
let col: vec4u = (vec4u(id) >> shifts) & vec4u(0xffu);
|
|
82689
|
+
return vec4f(col) * inv;
|
|
82690
|
+
}
|
|
82691
|
+
|
|
82692
|
+
#ifdef GSPLAT_PICK_DEPTH
|
|
82693
|
+
#ifndef CAMERAPLANES
|
|
82694
|
+
#define CAMERAPLANES
|
|
82695
|
+
uniform camera_params: vec4f; // x: 1/far, y: far, z: near, w: isOrtho
|
|
82696
|
+
#endif
|
|
82697
|
+
|
|
82698
|
+
fn getPickOutput() -> vec4f {
|
|
82699
|
+
var normalizedDepth: f32;
|
|
82700
|
+
if (uniform.camera_params.w > 0.5) {
|
|
82701
|
+
normalizedDepth = pcPosition.z;
|
|
82702
|
+
} else {
|
|
82703
|
+
let linearDepth = 1.0 / pcPosition.w;
|
|
82704
|
+
normalizedDepth = (linearDepth - uniform.camera_params.z) / (uniform.camera_params.y - uniform.camera_params.z);
|
|
82705
|
+
}
|
|
82706
|
+
|
|
82707
|
+
let a = f32(gaussianColor.a);
|
|
82708
|
+
return vec4f(a * normalizedDepth, 0.0, 0.0, a);
|
|
82646
82709
|
}
|
|
82647
|
-
|
|
82648
|
-
|
|
82649
|
-
|
|
82650
|
-
|
|
82651
|
-
|
|
82652
|
-
|
|
82653
|
-
// distance from the fitted plane exceeds NORMAL_OUTLIER_THRESHOLD * the mean
|
|
82654
|
-
// residual, refit on the inliers. Sign-flips the result toward toCamera so
|
|
82655
|
-
// the cursor's tangent basis stays consistent. Returns false on degenerate
|
|
82656
|
-
// input (fewer than 3 points, collinear/coincident samples).
|
|
82657
|
-
const fitPlaneNormal = (points, toCamera, outNormal) => {
|
|
82658
|
-
const first = fitPlaneOnce(points);
|
|
82659
|
-
if (!first)
|
|
82660
|
-
return false;
|
|
82661
|
-
let residualSum = 0;
|
|
82662
|
-
for (let i = 0; i < points.length; i++) {
|
|
82663
|
-
const dx = points[i].x - first.cx;
|
|
82664
|
-
const dy = points[i].y - first.cy;
|
|
82665
|
-
const dz = points[i].z - first.cz;
|
|
82666
|
-
residualSum += Math.abs(dx * first.vx + dy * first.vy + dz * first.vz);
|
|
82667
|
-
}
|
|
82668
|
-
const threshold = (residualSum / points.length) * NORMAL_OUTLIER_THRESHOLD;
|
|
82669
|
-
const inliers = [];
|
|
82670
|
-
for (let i = 0; i < points.length; i++) {
|
|
82671
|
-
const dx = points[i].x - first.cx;
|
|
82672
|
-
const dy = points[i].y - first.cy;
|
|
82673
|
-
const dz = points[i].z - first.cz;
|
|
82674
|
-
if (Math.abs(dx * first.vx + dy * first.vy + dz * first.vz) <= threshold) {
|
|
82675
|
-
inliers.push(points[i]);
|
|
82710
|
+
#else
|
|
82711
|
+
#ifndef PICK_CUSTOM_ID
|
|
82712
|
+
uniform meshInstanceId: u32;
|
|
82713
|
+
|
|
82714
|
+
fn getPickOutput() -> vec4f {
|
|
82715
|
+
return encodePickOutput(uniform.meshInstanceId);
|
|
82676
82716
|
}
|
|
82717
|
+
#endif
|
|
82718
|
+
#endif
|
|
82719
|
+
|
|
82720
|
+
#ifdef DEPTH_PICK_PASS
|
|
82721
|
+
#include "floatAsUintPS"
|
|
82722
|
+
#ifndef CAMERAPLANES
|
|
82723
|
+
#define CAMERAPLANES
|
|
82724
|
+
uniform camera_params: vec4f; // x: 1/far, y: far, z: near, w: isOrtho
|
|
82725
|
+
#endif
|
|
82726
|
+
|
|
82727
|
+
fn getPickDepth() -> vec4f {
|
|
82728
|
+
var linearDepth: f32;
|
|
82729
|
+
if (uniform.camera_params.w > 0.5) {
|
|
82730
|
+
linearDepth = pcPosition.z;
|
|
82731
|
+
} else {
|
|
82732
|
+
let viewDist = 1.0 / pcPosition.w;
|
|
82733
|
+
linearDepth = (viewDist - uniform.camera_params.z) / (uniform.camera_params.y - uniform.camera_params.z);
|
|
82734
|
+
}
|
|
82735
|
+
return float2uint(linearDepth);
|
|
82677
82736
|
}
|
|
82678
|
-
|
|
82679
|
-
|
|
82680
|
-
|
|
82681
|
-
|
|
82682
|
-
|
|
82683
|
-
|
|
82684
|
-
|
|
82685
|
-
|
|
82686
|
-
|
|
82687
|
-
|
|
82688
|
-
|
|
82737
|
+
#endif
|
|
82738
|
+
`;
|
|
82739
|
+
const pickPassChunkInjected = [
|
|
82740
|
+
'#ifdef PICK_PASS',
|
|
82741
|
+
' #define GSPLAT_PICK_DEPTH',
|
|
82742
|
+
' #include "pickPS"',
|
|
82743
|
+
'#endif'
|
|
82744
|
+
].join('\n');
|
|
82745
|
+
const safeChunkReplace = (s, find, repl) => {
|
|
82746
|
+
const out = s.replace(find, repl);
|
|
82747
|
+
if (out === s) {
|
|
82748
|
+
throw new Error('picker: engine gsplat/pick chunk patch failed (engine version mismatch?)');
|
|
82689
82749
|
}
|
|
82690
|
-
|
|
82691
|
-
return true;
|
|
82750
|
+
return out;
|
|
82692
82751
|
};
|
|
82693
|
-
|
|
82694
|
-
|
|
82695
|
-
|
|
82696
|
-
|
|
82697
|
-
|
|
82698
|
-
|
|
82699
|
-
|
|
82700
|
-
|
|
82701
|
-
|
|
82702
|
-
|
|
82703
|
-
|
|
82704
|
-
|
|
82705
|
-
|
|
82706
|
-
|
|
82707
|
-
|
|
82708
|
-
|
|
82709
|
-
|
|
82710
|
-
|
|
82711
|
-
|
|
82712
|
-
|
|
82713
|
-
|
|
82714
|
-
|
|
82715
|
-
|
|
82716
|
-
|
|
82717
|
-
|
|
82718
|
-
|
|
82719
|
-
|
|
82720
|
-
|
|
82721
|
-
|
|
82722
|
-
|
|
82723
|
-
|
|
82724
|
-
|
|
82725
|
-
|
|
82726
|
-
|
|
82727
|
-
|
|
82728
|
-
|
|
82729
|
-
|
|
82730
|
-
|
|
82731
|
-
|
|
82732
|
-
|
|
82733
|
-
|
|
82734
|
-
|
|
82735
|
-
|
|
82736
|
-
|
|
82737
|
-
|
|
82738
|
-
cacheHeight = height;
|
|
82739
|
-
};
|
|
82740
|
-
const cameraMatches = (width, height) => {
|
|
82741
|
-
const cam = camera.camera;
|
|
82742
|
-
return cacheValid &&
|
|
82743
|
-
cacheWidth === width &&
|
|
82744
|
-
cacheHeight === height &&
|
|
82745
|
-
cacheCamera.viewMatrix.equals(cam.viewMatrix) &&
|
|
82746
|
-
cacheCamera.projectionMatrix.equals(cam.projectionMatrix) &&
|
|
82747
|
-
cacheCamera.nearClip === cam.nearClip &&
|
|
82748
|
-
cacheCamera.farClip === cam.farClip &&
|
|
82749
|
-
cacheCamera.projection === cam.projection;
|
|
82750
|
-
};
|
|
82751
|
-
const getCacheCameraSnapshot = () => {
|
|
82752
|
-
const snapshot = createPickCameraSnapshot();
|
|
82753
|
-
snapshot.position.copy(cacheCamera.position);
|
|
82754
|
-
snapshot.viewMatrix.copy(cacheCamera.viewMatrix);
|
|
82755
|
-
snapshot.projectionMatrix.copy(cacheCamera.projectionMatrix);
|
|
82756
|
-
snapshot.nearClip = cacheCamera.nearClip;
|
|
82757
|
-
snapshot.farClip = cacheCamera.farClip;
|
|
82758
|
-
snapshot.projection = cacheCamera.projection;
|
|
82759
|
-
return snapshot;
|
|
82760
|
-
};
|
|
82761
|
-
const readRasterBlock = async (blockX, blockY, blockWidth, blockHeight, viewportWidth, viewportHeight, pickCamera) => {
|
|
82762
|
-
const texY = graphicsDevice.isWebGL2 ? accumTarget.height - blockY - blockHeight : blockY;
|
|
82763
|
-
const pixels = await accumBuffer.read(blockX, texY, blockWidth, blockHeight, {
|
|
82764
|
-
renderTarget: accumTarget,
|
|
82765
|
-
immediate: true
|
|
82766
|
-
});
|
|
82767
|
-
return (x, y) => {
|
|
82768
|
-
const localX = x - blockX;
|
|
82769
|
-
const localY = y - blockY;
|
|
82770
|
-
if (localX < 0 || localX >= blockWidth || localY < 0 || localY >= blockHeight) {
|
|
82771
|
-
return null;
|
|
82772
|
-
}
|
|
82773
|
-
const row = graphicsDevice.isWebGL2 ? blockHeight - localY - 1 : localY;
|
|
82774
|
-
const index = (row * blockWidth + localX) * 4;
|
|
82775
|
-
const r = half2Float(pixels[index]);
|
|
82776
|
-
const transmittance = half2Float(pixels[index + 3]);
|
|
82777
|
-
const alpha = 1 - transmittance;
|
|
82778
|
-
if (!Number.isFinite(r) || !Number.isFinite(alpha) || alpha < 1e-6) {
|
|
82779
|
-
return null;
|
|
82780
|
-
}
|
|
82781
|
-
const normalizedDepth = r / alpha;
|
|
82782
|
-
return getWorldPoint(pickCamera, x, y, viewportWidth, viewportHeight, normalizedDepth);
|
|
82783
|
-
};
|
|
82784
|
-
};
|
|
82785
|
-
const ensureRendered = (width, height, worldLayer) => {
|
|
82786
|
-
if (cameraMatches(width, height)) {
|
|
82787
|
-
return;
|
|
82788
|
-
}
|
|
82789
|
-
// Enable gsplat IDs only while rendering the pick target so we
|
|
82790
|
-
// don't pay the memory/perf cost between pick passes.
|
|
82791
|
-
const prevEnableIds = app.scene.gsplat.enableIds;
|
|
82792
|
-
app.scene.gsplat.enableIds = true;
|
|
82793
|
-
try {
|
|
82794
|
-
if (!chunksPatched) {
|
|
82795
|
-
registerPickerShaderPatches(app);
|
|
82796
|
-
chunksPatched = true;
|
|
82797
|
-
}
|
|
82798
|
-
if (!accumPass) {
|
|
82799
|
-
initRasterAccum(width, height);
|
|
82800
|
-
}
|
|
82801
|
-
else if (cacheWidth !== width || cacheHeight !== height) {
|
|
82802
|
-
cacheValid = false;
|
|
82803
|
-
accumTarget.resize(width, height);
|
|
82804
|
-
}
|
|
82805
|
-
accumPass.init(accumTarget);
|
|
82806
|
-
accumPass.setClearColor(clearColor);
|
|
82807
|
-
accumPass.update(camera.camera, app.scene, [worldLayer], new Map(), false);
|
|
82808
|
-
accumPass.render();
|
|
82809
|
-
updateCache(width, height);
|
|
82810
|
-
cacheValid = true;
|
|
82811
|
-
}
|
|
82812
|
-
finally {
|
|
82813
|
-
app.scene.gsplat.enableIds = prevEnableIds;
|
|
82814
|
-
}
|
|
82815
|
-
};
|
|
82816
|
-
const prepareSample = (x, y) => {
|
|
82817
|
-
const width = Math.floor(graphicsDevice.width);
|
|
82818
|
-
const height = Math.floor(graphicsDevice.height);
|
|
82819
|
-
// bail out if the device hasn't been sized yet
|
|
82820
|
-
if (width <= 0 || height <= 0) {
|
|
82821
|
-
return null;
|
|
82822
|
-
}
|
|
82823
|
-
const worldLayer = app.scene.layers.getLayerByName('World');
|
|
82824
|
-
if (!worldLayer) {
|
|
82825
|
-
return null;
|
|
82826
|
-
}
|
|
82827
|
-
const screenX = Math.min(width - 1, Math.max(0, Math.floor(x * width)));
|
|
82828
|
-
const screenY = Math.min(height - 1, Math.max(0, Math.floor(y * height)));
|
|
82829
|
-
ensureRendered(width, height, worldLayer);
|
|
82830
|
-
const pickCamera = getCacheCameraSnapshot();
|
|
82831
|
-
return { width, height, screenX, screenY, pickCamera };
|
|
82832
|
-
};
|
|
82833
|
-
const pickPosition = async (x, y) => {
|
|
82834
|
-
const sample = prepareSample(x, y);
|
|
82835
|
-
if (!sample) {
|
|
82836
|
-
return null;
|
|
82837
|
-
}
|
|
82838
|
-
const { width, height, screenX, screenY, pickCamera } = sample;
|
|
82839
|
-
const pixels = await readTexture(accumBuffer, screenX, screenY, accumTarget);
|
|
82840
|
-
const r = half2Float(pixels[0]);
|
|
82841
|
-
const transmittance = half2Float(pixels[3]);
|
|
82842
|
-
const alpha = 1 - transmittance;
|
|
82843
|
-
if (!Number.isFinite(r) || !Number.isFinite(alpha) || alpha < 1e-6) {
|
|
82844
|
-
return null;
|
|
82845
|
-
}
|
|
82846
|
-
const normalizedDepth = r / alpha;
|
|
82847
|
-
const position = getWorldPoint(pickCamera, screenX, screenY, width, height, normalizedDepth);
|
|
82848
|
-
return position ? { position, camera: pickCamera, screenX, screenY, width, height } : null;
|
|
82849
|
-
};
|
|
82850
|
-
const serializePick = (operation) => {
|
|
82851
|
-
// The render targets are shared by all picks on this instance.
|
|
82852
|
-
const result = pickQueue.then(operation, operation);
|
|
82853
|
-
pickQueue = result.then(() => undefined, () => undefined);
|
|
82854
|
-
return result;
|
|
82855
|
-
};
|
|
82856
|
-
const pick = async (x, y) => {
|
|
82857
|
-
const result = await pickPosition(x, y);
|
|
82858
|
-
return result?.position ?? null;
|
|
82859
|
-
};
|
|
82860
|
-
const pickSurface = async (x, y) => {
|
|
82861
|
-
const sample = prepareSample(x, y);
|
|
82862
|
-
if (!sample) {
|
|
82863
|
-
return null;
|
|
82864
|
-
}
|
|
82865
|
-
const { width, height, screenX, screenY, pickCamera } = sample;
|
|
82866
|
-
// Single block read serves both depth (center pixel) and normal
|
|
82867
|
-
// samples. Sized to the maximum possible ring pixel-radius so the
|
|
82868
|
-
// dynamic ring offsets always lie inside the buffer we read.
|
|
82869
|
-
const blockX = Math.max(0, screenX - NORMAL_SAMPLE_MAX_PX);
|
|
82870
|
-
const blockY = Math.max(0, screenY - NORMAL_SAMPLE_MAX_PX);
|
|
82871
|
-
const blockWidth = Math.min(width - 1, screenX + NORMAL_SAMPLE_MAX_PX) - blockX + 1;
|
|
82872
|
-
const blockHeight = Math.min(height - 1, screenY + NORMAL_SAMPLE_MAX_PX) - blockY + 1;
|
|
82873
|
-
const rasterBlock = await readRasterBlock(blockX, blockY, blockWidth, blockHeight, width, height, pickCamera);
|
|
82874
|
-
const position = rasterBlock(screenX, screenY);
|
|
82875
|
-
if (!position) {
|
|
82876
|
-
return null;
|
|
82877
|
-
}
|
|
82878
|
-
const samplePixel = (px, py) => {
|
|
82879
|
-
if (px < 0 || px >= width || py < 0 || py >= height) {
|
|
82880
|
-
return null;
|
|
82881
|
-
}
|
|
82882
|
-
return rasterBlock(px, py);
|
|
82883
|
-
};
|
|
82884
|
-
// Pixel radius corresponding to a fixed world radius at the
|
|
82885
|
-
// picked-point's depth. Clamped so distant picks still sample
|
|
82886
|
-
// enough pixels and very-close picks stay inside the block read.
|
|
82887
|
-
const pixelRadius = Math.max(NORMAL_SAMPLE_MIN_PX, Math.min(NORMAL_SAMPLE_MAX_PX, worldRadiusToPixelRadius(pickCamera, position, height, NORMAL_SAMPLE_WORLD_RADIUS)));
|
|
82888
|
-
const ringPixelRadii = NORMAL_RING_FRACTIONS.map(f => Math.max(1, Math.round(f * pixelRadius)));
|
|
82889
|
-
const sampleRings = ringPixelRadii.map((radius) => {
|
|
82890
|
-
return NORMAL_SAMPLE_DIRECTIONS.map(([dx, dy]) => {
|
|
82891
|
-
return samplePixel(screenX + dx * radius, screenY + dy * radius);
|
|
82892
|
-
});
|
|
82893
|
-
});
|
|
82894
|
-
const toCamera = setCameraFacingNormal(pickCamera.position, position, new Vec3());
|
|
82895
|
-
// Collect every valid 3D sample: the picked position plus all ring
|
|
82896
|
-
// samples that didn't fall off-screen or fail the depth read.
|
|
82897
|
-
const fitPoints = [position];
|
|
82898
|
-
for (let i = 0; i < sampleRings.length; i++) {
|
|
82899
|
-
const ring = sampleRings[i];
|
|
82900
|
-
for (let j = 0; j < ring.length; j++) {
|
|
82901
|
-
const pt = ring[j];
|
|
82902
|
-
if (pt)
|
|
82903
|
-
fitPoints.push(pt);
|
|
82904
|
-
}
|
|
82905
|
-
}
|
|
82906
|
-
const normal = new Vec3();
|
|
82907
|
-
if (!fitPlaneNormal(fitPoints, toCamera, normal)) {
|
|
82908
|
-
normal.copy(toCamera);
|
|
82909
|
-
}
|
|
82910
|
-
return {
|
|
82911
|
-
position,
|
|
82912
|
-
normal
|
|
82913
|
-
};
|
|
82914
|
-
};
|
|
82915
|
-
this.pick = (x, y) => serializePick(() => pick(x, y));
|
|
82916
|
-
this.pickSurface = (x, y) => serializePick(() => pickSurface(x, y));
|
|
82917
|
-
this.release = () => {
|
|
82918
|
-
if (chunksPatched) {
|
|
82919
|
-
unregisterPickerShaderPatches(app);
|
|
82920
|
-
chunksPatched = false;
|
|
82921
|
-
}
|
|
82922
|
-
accumPass?.destroy();
|
|
82923
|
-
accumTarget?.destroy();
|
|
82924
|
-
accumBuffer?.destroy();
|
|
82925
|
-
cacheValid = false;
|
|
82926
|
-
};
|
|
82927
|
-
}
|
|
82928
|
-
}
|
|
82929
|
-
|
|
82930
|
-
const SVGNS = 'http://www.w3.org/2000/svg';
|
|
82931
|
-
const NUM_SAMPLES = 12;
|
|
82932
|
-
const BASE_OUTER_RADIUS = 0.2;
|
|
82933
|
-
const INNER_OUTER_RATIO = 0.17 / 0.2;
|
|
82934
|
-
// Scenes with halfExtents.length() below this are smaller than the walk
|
|
82935
|
-
// capsule (~1.5 m) — not navigable in walk mode and the world-space ring
|
|
82936
|
-
// engulfs the whole scene. Switch the cursor to a fixed screen size instead.
|
|
82937
|
-
const SMALL_SCENE_THRESHOLD = 2;
|
|
82938
|
-
const SCREEN_OUTER_PIXELS = 60;
|
|
82939
|
-
const BEZIER_K = 1 / 6;
|
|
82940
|
-
const NORMAL_SMOOTH_FACTOR = 0.25;
|
|
82941
|
-
const NORMAL_SNAP_ANGLE = Math.PI / 4;
|
|
82942
|
-
const NORMAL_EPSILON = 1e-6;
|
|
82943
|
-
const createNormalSnapDirections = () => {
|
|
82944
|
-
const result = [];
|
|
82945
|
-
for (let pitchStep = -2; pitchStep <= 2; pitchStep++) {
|
|
82946
|
-
const pitch = pitchStep * NORMAL_SNAP_ANGLE;
|
|
82947
|
-
const cp = Math.cos(pitch);
|
|
82948
|
-
const sy = Math.sin(pitch);
|
|
82949
|
-
if (Math.abs(cp) <= NORMAL_EPSILON) {
|
|
82950
|
-
result.push(new Vec3(0, sy > 0 ? 1 : -1, 0));
|
|
82951
|
-
continue;
|
|
82752
|
+
const patchGsplatPickGlsl = (chunk) => {
|
|
82753
|
+
return safeChunkReplace(safeChunkReplace(chunk, /#ifdef PICK_PASS\s*#include "pickPS"\s*#endif/, pickPassChunkInjected), 'pcFragColor0 = encodePickOutput(vPickId);', 'pcFragColor0 = getPickOutput();');
|
|
82754
|
+
};
|
|
82755
|
+
const patchGsplatPickWgsl = (chunk) => {
|
|
82756
|
+
return safeChunkReplace(safeChunkReplace(chunk, /#ifdef PICK_PASS\s*#include "pickPS"\s*#endif/, pickPassChunkInjected), 'output.color = encodePickOutput(vPickId);', 'output.color = getPickOutput();');
|
|
82757
|
+
};
|
|
82758
|
+
/** Per-device original chunk strings + refcount so we can restore after the last Picker releases. */
|
|
82759
|
+
const pickerShaderPatchState = new WeakMap();
|
|
82760
|
+
const vec4 = new Vec4();
|
|
82761
|
+
const viewProjMat = new Mat4();
|
|
82762
|
+
const clearColor = new Color(0, 0, 0, 1);
|
|
82763
|
+
const NORMAL_EPSILON = 1e-12;
|
|
82764
|
+
const NORMAL_DEGENERATE_EPSILON = 1e-20;
|
|
82765
|
+
// Sampling for the surface-normal estimator runs on a circular footprint of
|
|
82766
|
+
// fixed *world* radius, projected to pixels at the picked-point's depth.
|
|
82767
|
+
// Keeping the world area constant means adjacent cursor positions sample
|
|
82768
|
+
// almost the same world cluster, which stabilises the plane fit. The pixel
|
|
82769
|
+
// radius is clamped so distant picks still get enough samples and very-close
|
|
82770
|
+
// picks don't blow the block read.
|
|
82771
|
+
const NORMAL_SAMPLE_WORLD_RADIUS = 0.2;
|
|
82772
|
+
const NORMAL_SAMPLE_MIN_PX = 6;
|
|
82773
|
+
const NORMAL_SAMPLE_MAX_PX = 48;
|
|
82774
|
+
const NORMAL_RING_FRACTIONS = [0.3, 0.55, 0.8, 1.0];
|
|
82775
|
+
const NORMAL_OUTLIER_THRESHOLD = 2.5;
|
|
82776
|
+
const NORMAL_SAMPLE_DIRECTIONS = [
|
|
82777
|
+
[1, 0],
|
|
82778
|
+
[1, 1],
|
|
82779
|
+
[0, 1],
|
|
82780
|
+
[-1, 1],
|
|
82781
|
+
[-1, 0],
|
|
82782
|
+
[-1, -1],
|
|
82783
|
+
[0, -1],
|
|
82784
|
+
[1, -1]
|
|
82785
|
+
];
|
|
82786
|
+
// Shared buffer for half-to-float conversion
|
|
82787
|
+
const float32 = new Float32Array(1);
|
|
82788
|
+
const uint32 = new Uint32Array(float32.buffer);
|
|
82789
|
+
// Convert 16-bit half-float to 32-bit float using bit manipulation.
|
|
82790
|
+
const half2Float = (h) => {
|
|
82791
|
+
const sign = (h & 0x8000) << 16;
|
|
82792
|
+
const exponent = (h & 0x7C00) >> 10;
|
|
82793
|
+
const mantissa = h & 0x03FF;
|
|
82794
|
+
if (exponent === 0) {
|
|
82795
|
+
if (mantissa === 0) {
|
|
82796
|
+
uint32[0] = sign;
|
|
82952
82797
|
}
|
|
82953
|
-
|
|
82954
|
-
|
|
82955
|
-
|
|
82798
|
+
else {
|
|
82799
|
+
let e = -1;
|
|
82800
|
+
let m = mantissa;
|
|
82801
|
+
do {
|
|
82802
|
+
e++;
|
|
82803
|
+
m <<= 1;
|
|
82804
|
+
} while ((m & 0x0400) === 0);
|
|
82805
|
+
uint32[0] = sign | ((127 - 15 - e) << 23) | ((m & 0x03FF) << 13);
|
|
82956
82806
|
}
|
|
82957
82807
|
}
|
|
82958
|
-
|
|
82808
|
+
else if (exponent === 31) {
|
|
82809
|
+
uint32[0] = sign | 0x7F800000 | (mantissa << 13);
|
|
82810
|
+
}
|
|
82811
|
+
else {
|
|
82812
|
+
uint32[0] = sign | ((exponent + 127 - 15) << 23) | (mantissa << 13);
|
|
82813
|
+
}
|
|
82814
|
+
return float32[0];
|
|
82959
82815
|
};
|
|
82960
|
-
const
|
|
82961
|
-
const
|
|
82962
|
-
const
|
|
82963
|
-
if (
|
|
82964
|
-
|
|
82816
|
+
const registerPickerShaderPatches = (app) => {
|
|
82817
|
+
const device = app.graphicsDevice;
|
|
82818
|
+
const existing = pickerShaderPatchState.get(device);
|
|
82819
|
+
if (existing) {
|
|
82820
|
+
existing.refCount++;
|
|
82821
|
+
return;
|
|
82965
82822
|
}
|
|
82966
|
-
const
|
|
82967
|
-
const
|
|
82968
|
-
const
|
|
82969
|
-
const
|
|
82970
|
-
|
|
82971
|
-
|
|
82972
|
-
|
|
82973
|
-
|
|
82974
|
-
|
|
82975
|
-
|
|
82976
|
-
|
|
82977
|
-
|
|
82978
|
-
|
|
82823
|
+
const glslChunks = ShaderChunks.get(device, 'glsl');
|
|
82824
|
+
const wgslChunks = ShaderChunks.get(device, 'wgsl');
|
|
82825
|
+
const glslPickPS = glslChunks.get('pickPS');
|
|
82826
|
+
const glslGsplatPS = glslChunks.get('gsplatPS');
|
|
82827
|
+
const wgslPickPS = wgslChunks.get('pickPS');
|
|
82828
|
+
const wgslGsplatPS = wgslChunks.get('gsplatPS');
|
|
82829
|
+
// Patch strings before mutating ShaderChunks so engine mismatches leave globals untouched.
|
|
82830
|
+
const patchedGlslGsplatPS = patchGsplatPickGlsl(glslGsplatPS);
|
|
82831
|
+
const patchedWgslGsplatPS = patchGsplatPickWgsl(wgslGsplatPS);
|
|
82832
|
+
const state = {
|
|
82833
|
+
glslPickPS,
|
|
82834
|
+
glslGsplatPS,
|
|
82835
|
+
wgslPickPS,
|
|
82836
|
+
wgslGsplatPS,
|
|
82837
|
+
refCount: 1
|
|
82838
|
+
};
|
|
82839
|
+
pickerShaderPatchState.set(device, state);
|
|
82840
|
+
glslChunks.set('pickPS', pickDepthGlsl);
|
|
82841
|
+
wgslChunks.set('pickPS', pickDepthWgsl);
|
|
82842
|
+
glslChunks.set('gsplatPS', patchedGlslGsplatPS);
|
|
82843
|
+
wgslChunks.set('gsplatPS', patchedWgslGsplatPS);
|
|
82844
|
+
};
|
|
82845
|
+
const unregisterPickerShaderPatches = (app) => {
|
|
82846
|
+
const device = app.graphicsDevice;
|
|
82847
|
+
const state = pickerShaderPatchState.get(device);
|
|
82848
|
+
if (!state) {
|
|
82849
|
+
return;
|
|
82979
82850
|
}
|
|
82980
|
-
|
|
82851
|
+
state.refCount--;
|
|
82852
|
+
if (state.refCount > 0) {
|
|
82853
|
+
return;
|
|
82854
|
+
}
|
|
82855
|
+
const glslChunks = ShaderChunks.get(device, 'glsl');
|
|
82856
|
+
const wgslChunks = ShaderChunks.get(device, 'wgsl');
|
|
82857
|
+
glslChunks.set('pickPS', state.glslPickPS);
|
|
82858
|
+
glslChunks.set('gsplatPS', state.glslGsplatPS);
|
|
82859
|
+
wgslChunks.set('pickPS', state.wgslPickPS);
|
|
82860
|
+
wgslChunks.set('gsplatPS', state.wgslGsplatPS);
|
|
82861
|
+
pickerShaderPatchState.delete(device);
|
|
82981
82862
|
};
|
|
82982
|
-
const
|
|
82983
|
-
|
|
82984
|
-
|
|
82985
|
-
|
|
82986
|
-
|
|
82987
|
-
|
|
82988
|
-
|
|
82989
|
-
|
|
82990
|
-
const
|
|
82991
|
-
const tmpViewProj = new Mat4();
|
|
82992
|
-
// Compute the world-space radius such that a circle at `pos` projects to a
|
|
82993
|
-
// ring of `pixelDiameter` on screen. Used for the small-scene cursor mode
|
|
82994
|
-
// where we want a constant screen size regardless of zoom.
|
|
82995
|
-
const worldRadiusForPixels = (camera, canvasHeight, pos, pixelDiameter) => {
|
|
82863
|
+
const createPickCameraSnapshot = () => ({
|
|
82864
|
+
position: new Vec3(),
|
|
82865
|
+
viewMatrix: new Mat4(),
|
|
82866
|
+
projectionMatrix: new Mat4(),
|
|
82867
|
+
nearClip: 0,
|
|
82868
|
+
farClip: 0,
|
|
82869
|
+
projection: 0
|
|
82870
|
+
});
|
|
82871
|
+
const captureCameraSnapshot = (camera, out) => {
|
|
82996
82872
|
const cam = camera.camera;
|
|
82997
|
-
|
|
82998
|
-
|
|
82873
|
+
out.position.copy(camera.getPosition());
|
|
82874
|
+
out.viewMatrix.copy(cam.viewMatrix);
|
|
82875
|
+
out.projectionMatrix.copy(cam.projectionMatrix);
|
|
82876
|
+
out.nearClip = cam.nearClip;
|
|
82877
|
+
out.farClip = cam.farClip;
|
|
82878
|
+
out.projection = cam.projection;
|
|
82879
|
+
};
|
|
82880
|
+
const getWorldPoint = (camera, x, y, width, height, normalizedDepth, out) => {
|
|
82881
|
+
if (!Number.isFinite(normalizedDepth) || normalizedDepth < 0 || normalizedDepth > 1) {
|
|
82882
|
+
return null;
|
|
82999
82883
|
}
|
|
83000
|
-
const
|
|
83001
|
-
const
|
|
83002
|
-
|
|
83003
|
-
|
|
83004
|
-
|
|
83005
|
-
|
|
83006
|
-
|
|
82884
|
+
const { farClip: far, nearClip: near } = camera;
|
|
82885
|
+
const ndcDepth = camera.projection === PROJECTION_ORTHOGRAPHIC ?
|
|
82886
|
+
normalizedDepth :
|
|
82887
|
+
far * normalizedDepth / (normalizedDepth * (far - near) + near);
|
|
82888
|
+
viewProjMat.mul2(camera.projectionMatrix, camera.viewMatrix).invert();
|
|
82889
|
+
vec4.set(x / width * 2 - 1, (1 - y / height) * 2 - 1, ndcDepth * 2 - 1, 1);
|
|
82890
|
+
viewProjMat.transformVec4(vec4, vec4);
|
|
82891
|
+
if (!Number.isFinite(vec4.w) || Math.abs(vec4.w) < 1e-8) {
|
|
82892
|
+
return null;
|
|
82893
|
+
}
|
|
82894
|
+
vec4.mulScalar(1 / vec4.w);
|
|
82895
|
+
if (!Number.isFinite(vec4.x) || !Number.isFinite(vec4.y) || !Number.isFinite(vec4.z)) {
|
|
82896
|
+
return null;
|
|
82897
|
+
}
|
|
82898
|
+
return (out ?? new Vec3()).set(vec4.x, vec4.y, vec4.z);
|
|
83007
82899
|
};
|
|
83008
|
-
const
|
|
83009
|
-
|
|
83010
|
-
|
|
83011
|
-
|
|
83012
|
-
|
|
83013
|
-
if (Math.abs(tmpClipVec.w) < 1e-8) {
|
|
83014
|
-
return -1;
|
|
83015
|
-
}
|
|
83016
|
-
return (tmpClipVec.z / tmpClipVec.w + 1) * 0.5;
|
|
82900
|
+
const setCameraFacingNormal = (cameraPosition, position, normal) => {
|
|
82901
|
+
normal.sub2(cameraPosition, position);
|
|
82902
|
+
const len = normal.length();
|
|
82903
|
+
if (len > 1e-6) {
|
|
82904
|
+
normal.mulScalar(1 / len);
|
|
83017
82905
|
}
|
|
83018
|
-
|
|
83019
|
-
|
|
83020
|
-
const range = camera.farClip - camera.nearClip;
|
|
83021
|
-
if (range <= 0) {
|
|
83022
|
-
return -1;
|
|
82906
|
+
else {
|
|
82907
|
+
normal.set(0, 1, 0);
|
|
83023
82908
|
}
|
|
83024
|
-
return
|
|
82909
|
+
return normal;
|
|
83025
82910
|
};
|
|
83026
|
-
|
|
83027
|
-
|
|
83028
|
-
|
|
83029
|
-
|
|
83030
|
-
|
|
83031
|
-
|
|
83032
|
-
|
|
83033
|
-
|
|
83034
|
-
|
|
83035
|
-
|
|
83036
|
-
|
|
83037
|
-
|
|
83038
|
-
|
|
83039
|
-
|
|
83040
|
-
|
|
83041
|
-
|
|
83042
|
-
|
|
82911
|
+
// Project a world-space radius around `pos` to its on-screen pixel radius for
|
|
82912
|
+
// the given camera. Uses the projection matrix's [1][1] entry (= 1/tan(fov/2)
|
|
82913
|
+
// for perspective, = 1/orthoHeight for orthographic) so we don't need fov or
|
|
82914
|
+
// orthoHeight on the snapshot.
|
|
82915
|
+
const worldRadiusToPixelRadius = (cam, pos, canvasHeight, worldRadius) => {
|
|
82916
|
+
const projY = cam.projectionMatrix.data[5];
|
|
82917
|
+
if (cam.projection === PROJECTION_ORTHOGRAPHIC) {
|
|
82918
|
+
return worldRadius * projY * canvasHeight / 2;
|
|
82919
|
+
}
|
|
82920
|
+
const dx = pos.x - cam.position.x;
|
|
82921
|
+
const dy = pos.y - cam.position.y;
|
|
82922
|
+
const dz = pos.z - cam.position.z;
|
|
82923
|
+
const distance = Math.sqrt(dx * dx + dy * dy + dz * dz);
|
|
82924
|
+
if (distance < 1e-6)
|
|
82925
|
+
return Infinity;
|
|
82926
|
+
return worldRadius * projY * canvasHeight / (2 * distance);
|
|
82927
|
+
};
|
|
82928
|
+
// Single least-squares plane fit through a cluster of 3D points. The normal
|
|
82929
|
+
// is the eigenvector of the smallest eigenvalue of the points' 3x3 covariance
|
|
82930
|
+
// matrix, computed in closed form (analytic eigendecomposition for 3x3
|
|
82931
|
+
// symmetric, Smith 1961). Returns null on degenerate input.
|
|
82932
|
+
const fitPlaneOnce = (points) => {
|
|
82933
|
+
const n = points.length;
|
|
82934
|
+
if (n < 3)
|
|
82935
|
+
return null;
|
|
82936
|
+
let cx = 0, cy = 0, cz = 0;
|
|
83043
82937
|
for (let i = 0; i < n; i++) {
|
|
83044
|
-
|
|
83045
|
-
|
|
83046
|
-
|
|
83047
|
-
|
|
83048
|
-
|
|
83049
|
-
|
|
83050
|
-
|
|
83051
|
-
|
|
83052
|
-
|
|
82938
|
+
cx += points[i].x;
|
|
82939
|
+
cy += points[i].y;
|
|
82940
|
+
cz += points[i].z;
|
|
82941
|
+
}
|
|
82942
|
+
cx /= n;
|
|
82943
|
+
cy /= n;
|
|
82944
|
+
cz /= n;
|
|
82945
|
+
let cxx = 0, cxy = 0, cxz = 0, cyy = 0, cyz = 0, czz = 0;
|
|
82946
|
+
for (let i = 0; i < n; i++) {
|
|
82947
|
+
const dx = points[i].x - cx;
|
|
82948
|
+
const dy = points[i].y - cy;
|
|
82949
|
+
const dz = points[i].z - cz;
|
|
82950
|
+
cxx += dx * dx;
|
|
82951
|
+
cxy += dx * dy;
|
|
82952
|
+
cxz += dx * dz;
|
|
82953
|
+
cyy += dy * dy;
|
|
82954
|
+
cyz += dy * dz;
|
|
82955
|
+
czz += dz * dz;
|
|
82956
|
+
}
|
|
82957
|
+
const q = (cxx + cyy + czz) / 3;
|
|
82958
|
+
const a = cxx - q, b = cyy - q, c = czz - q;
|
|
82959
|
+
const p2 = a * a + b * b + c * c + 2 * (cxy * cxy + cxz * cxz + cyz * cyz);
|
|
82960
|
+
if (p2 < NORMAL_DEGENERATE_EPSILON)
|
|
82961
|
+
return null;
|
|
82962
|
+
const p = Math.sqrt(p2 / 6);
|
|
82963
|
+
const inv = 1 / p;
|
|
82964
|
+
const Bxx = a * inv, Bxy = cxy * inv, Bxz = cxz * inv;
|
|
82965
|
+
const Byy = b * inv, Byz = cyz * inv, Bzz = c * inv;
|
|
82966
|
+
const detB = Bxx * (Byy * Bzz - Byz * Byz) -
|
|
82967
|
+
Bxy * (Bxy * Bzz - Byz * Bxz) +
|
|
82968
|
+
Bxz * (Bxy * Byz - Byy * Bxz);
|
|
82969
|
+
const r = Math.max(-1, Math.min(1, detB / 2));
|
|
82970
|
+
const phi = Math.acos(r) / 3;
|
|
82971
|
+
const lambdaMin = q + 2 * p * Math.cos(phi + 2 * Math.PI / 3);
|
|
82972
|
+
const Mxx = cxx - lambdaMin, Myy = cyy - lambdaMin, Mzz = czz - lambdaMin;
|
|
82973
|
+
const v1x = cxy * cyz - cxz * Myy;
|
|
82974
|
+
const v1y = cxz * cxy - Mxx * cyz;
|
|
82975
|
+
const v1z = Mxx * Myy - cxy * cxy;
|
|
82976
|
+
const v2x = cxy * Mzz - cxz * cyz;
|
|
82977
|
+
const v2y = cxz * cxz - Mxx * Mzz;
|
|
82978
|
+
const v2z = Mxx * cyz - cxy * cxz;
|
|
82979
|
+
const v3x = Myy * Mzz - cyz * cyz;
|
|
82980
|
+
const v3y = cyz * cxz - cxy * Mzz;
|
|
82981
|
+
const v3z = cxy * cyz - Myy * cxz;
|
|
82982
|
+
const l1 = v1x * v1x + v1y * v1y + v1z * v1z;
|
|
82983
|
+
const l2 = v2x * v2x + v2y * v2y + v2z * v2z;
|
|
82984
|
+
const l3 = v3x * v3x + v3y * v3y + v3z * v3z;
|
|
82985
|
+
let vx, vy, vz, lSq;
|
|
82986
|
+
if (l1 >= l2 && l1 >= l3) {
|
|
82987
|
+
vx = v1x;
|
|
82988
|
+
vy = v1y;
|
|
82989
|
+
vz = v1z;
|
|
82990
|
+
lSq = l1;
|
|
82991
|
+
}
|
|
82992
|
+
else if (l2 >= l3) {
|
|
82993
|
+
vx = v2x;
|
|
82994
|
+
vy = v2y;
|
|
82995
|
+
vz = v2z;
|
|
82996
|
+
lSq = l2;
|
|
82997
|
+
}
|
|
82998
|
+
else {
|
|
82999
|
+
vx = v3x;
|
|
83000
|
+
vy = v3y;
|
|
83001
|
+
vz = v3z;
|
|
83002
|
+
lSq = l3;
|
|
83053
83003
|
}
|
|
83054
|
-
|
|
83004
|
+
if (lSq < NORMAL_EPSILON)
|
|
83005
|
+
return null;
|
|
83006
|
+
const invLen = 1 / Math.sqrt(lSq);
|
|
83007
|
+
return { cx, cy, cz, vx: vx * invLen, vy: vy * invLen, vz: vz * invLen };
|
|
83055
83008
|
};
|
|
83056
|
-
|
|
83057
|
-
|
|
83058
|
-
|
|
83059
|
-
|
|
83060
|
-
|
|
83061
|
-
|
|
83062
|
-
|
|
83063
|
-
|
|
83064
|
-
|
|
83065
|
-
|
|
83066
|
-
|
|
83067
|
-
|
|
83068
|
-
|
|
83069
|
-
|
|
83070
|
-
|
|
83071
|
-
innerX = new Float64Array(NUM_SAMPLES);
|
|
83072
|
-
innerY = new Float64Array(NUM_SAMPLES);
|
|
83073
|
-
constructor(svg, canvas, camera, smoothing, screenPixels) {
|
|
83074
|
-
this.svg = svg;
|
|
83075
|
-
this.canvas = canvas;
|
|
83076
|
-
this.camera = camera;
|
|
83077
|
-
this.smoothing = smoothing;
|
|
83078
|
-
this.screenPixels = screenPixels;
|
|
83079
|
-
this.path = document.createElementNS(SVGNS, 'path');
|
|
83080
|
-
this.path.setAttribute('fill', 'white');
|
|
83081
|
-
this.path.setAttribute('fill-opacity', '0.6');
|
|
83082
|
-
this.path.setAttribute('fill-rule', 'evenodd');
|
|
83083
|
-
this.path.setAttribute('stroke', 'none');
|
|
83084
|
-
this.path.style.display = 'none';
|
|
83085
|
-
svg.appendChild(this.path);
|
|
83009
|
+
// Two-pass plane fit: first pass on all points, then drop points whose
|
|
83010
|
+
// distance from the fitted plane exceeds NORMAL_OUTLIER_THRESHOLD * the mean
|
|
83011
|
+
// residual, refit on the inliers. Sign-flips the result toward toCamera so
|
|
83012
|
+
// the cursor's tangent basis stays consistent. Returns false on degenerate
|
|
83013
|
+
// input (fewer than 3 points, collinear/coincident samples).
|
|
83014
|
+
const fitPlaneNormal = (points, toCamera, outNormal) => {
|
|
83015
|
+
const first = fitPlaneOnce(points);
|
|
83016
|
+
if (!first)
|
|
83017
|
+
return false;
|
|
83018
|
+
let residualSum = 0;
|
|
83019
|
+
for (let i = 0; i < points.length; i++) {
|
|
83020
|
+
const dx = points[i].x - first.cx;
|
|
83021
|
+
const dy = points[i].y - first.cy;
|
|
83022
|
+
const dz = points[i].z - first.cz;
|
|
83023
|
+
residualSum += Math.abs(dx * first.vx + dy * first.vy + dz * first.vz);
|
|
83086
83024
|
}
|
|
83087
|
-
|
|
83088
|
-
|
|
83089
|
-
|
|
83090
|
-
|
|
83091
|
-
|
|
83092
|
-
|
|
83093
|
-
|
|
83094
|
-
|
|
83095
|
-
bitangent.cross(normal, tangent);
|
|
83096
|
-
const cam = this.camera.camera;
|
|
83097
|
-
const angleStep = (2 * Math.PI) / NUM_SAMPLES;
|
|
83098
|
-
for (let i = 0; i < NUM_SAMPLES; i++) {
|
|
83099
|
-
const theta = i * angleStep;
|
|
83100
|
-
const ct = Math.cos(theta);
|
|
83101
|
-
const st = Math.sin(theta);
|
|
83102
|
-
const tx = ct * tangent.x + st * bitangent.x;
|
|
83103
|
-
const ty = ct * tangent.y + st * bitangent.y;
|
|
83104
|
-
const tz = ct * tangent.z + st * bitangent.z;
|
|
83105
|
-
worldPt.set(px + tx * radius, py + ty * radius, pz + tz * radius);
|
|
83106
|
-
cam.worldToScreen(worldPt, tmpScreen);
|
|
83107
|
-
outX[i] = tmpScreen.x;
|
|
83108
|
-
outY[i] = tmpScreen.y;
|
|
83025
|
+
const threshold = (residualSum / points.length) * NORMAL_OUTLIER_THRESHOLD;
|
|
83026
|
+
const inliers = [];
|
|
83027
|
+
for (let i = 0; i < points.length; i++) {
|
|
83028
|
+
const dx = points[i].x - first.cx;
|
|
83029
|
+
const dy = points[i].y - first.cy;
|
|
83030
|
+
const dz = points[i].z - first.cz;
|
|
83031
|
+
if (Math.abs(dx * first.vx + dy * first.vy + dz * first.vz) <= threshold) {
|
|
83032
|
+
inliers.push(points[i]);
|
|
83109
83033
|
}
|
|
83110
83034
|
}
|
|
83111
|
-
|
|
83112
|
-
|
|
83113
|
-
|
|
83114
|
-
|
|
83115
|
-
|
|
83116
|
-
if (this.smoothing) {
|
|
83117
|
-
if (this.hasSmoothedNormal) {
|
|
83118
|
-
const t = NORMAL_SMOOTH_FACTOR;
|
|
83119
|
-
nx = this.smoothNx + (nx - this.smoothNx) * t;
|
|
83120
|
-
ny = this.smoothNy + (ny - this.smoothNy) * t;
|
|
83121
|
-
nz = this.smoothNz + (nz - this.smoothNz) * t;
|
|
83122
|
-
const len = Math.sqrt(nx * nx + ny * ny + nz * nz);
|
|
83123
|
-
if (len > 1e-6) {
|
|
83124
|
-
const invLen = 1.0 / len;
|
|
83125
|
-
nx *= invLen;
|
|
83126
|
-
ny *= invLen;
|
|
83127
|
-
nz *= invLen;
|
|
83128
|
-
}
|
|
83129
|
-
}
|
|
83130
|
-
this.smoothNx = nx;
|
|
83131
|
-
this.smoothNy = ny;
|
|
83132
|
-
this.smoothNz = nz;
|
|
83133
|
-
this.hasSmoothedNormal = true;
|
|
83134
|
-
}
|
|
83135
|
-
const outerRadius = this.screenPixels !== null ?
|
|
83136
|
-
worldRadiusForPixels(this.camera, this.canvas.clientHeight || 1, pos, this.screenPixels) :
|
|
83137
|
-
BASE_OUTER_RADIUS;
|
|
83138
|
-
const innerRadius = outerRadius * INNER_OUTER_RATIO;
|
|
83139
|
-
this.projectCircle(pos.x, pos.y, pos.z, nx, ny, nz, outerRadius, this.outerX, this.outerY);
|
|
83140
|
-
this.projectCircle(pos.x, pos.y, pos.z, nx, ny, nz, innerRadius, this.innerX, this.innerY);
|
|
83141
|
-
this.path.setAttribute('d', `${buildBezierRing(this.outerX, this.outerY)} ${buildBezierRing(this.innerX, this.innerY)}`);
|
|
83142
|
-
this.path.style.display = '';
|
|
83143
|
-
this.svg.style.display = '';
|
|
83035
|
+
let result = first;
|
|
83036
|
+
if (inliers.length >= 3 && inliers.length < points.length) {
|
|
83037
|
+
const refined = fitPlaneOnce(inliers);
|
|
83038
|
+
if (refined)
|
|
83039
|
+
result = refined;
|
|
83144
83040
|
}
|
|
83145
|
-
|
|
83146
|
-
|
|
83147
|
-
|
|
83041
|
+
let { vx, vy, vz } = result;
|
|
83042
|
+
if (vx * toCamera.x + vy * toCamera.y + vz * toCamera.z < 0) {
|
|
83043
|
+
vx = -vx;
|
|
83044
|
+
vy = -vy;
|
|
83045
|
+
vz = -vz;
|
|
83148
83046
|
}
|
|
83149
|
-
|
|
83150
|
-
|
|
83151
|
-
|
|
83152
|
-
|
|
83153
|
-
|
|
83154
|
-
|
|
83155
|
-
|
|
83156
|
-
|
|
83157
|
-
|
|
83158
|
-
|
|
83159
|
-
|
|
83160
|
-
|
|
83161
|
-
|
|
83162
|
-
|
|
83163
|
-
|
|
83164
|
-
|
|
83165
|
-
|
|
83166
|
-
|
|
83167
|
-
|
|
83168
|
-
|
|
83169
|
-
|
|
83170
|
-
|
|
83171
|
-
|
|
83172
|
-
|
|
83173
|
-
|
|
83174
|
-
|
|
83175
|
-
|
|
83176
|
-
|
|
83177
|
-
|
|
83178
|
-
|
|
83179
|
-
|
|
83180
|
-
|
|
83181
|
-
|
|
83182
|
-
|
|
83183
|
-
|
|
83184
|
-
|
|
83185
|
-
|
|
83186
|
-
|
|
83187
|
-
|
|
83188
|
-
|
|
83189
|
-
|
|
83190
|
-
|
|
83191
|
-
|
|
83192
|
-
|
|
83193
|
-
|
|
83194
|
-
|
|
83195
|
-
|
|
83196
|
-
|
|
83197
|
-
|
|
83198
|
-
|
|
83047
|
+
outNormal.set(vx, vy, vz);
|
|
83048
|
+
return true;
|
|
83049
|
+
};
|
|
83050
|
+
class Picker {
|
|
83051
|
+
pick;
|
|
83052
|
+
pickSurface;
|
|
83053
|
+
release;
|
|
83054
|
+
constructor(app, camera) {
|
|
83055
|
+
const { graphicsDevice } = app;
|
|
83056
|
+
let accumBuffer;
|
|
83057
|
+
let accumTarget;
|
|
83058
|
+
let accumPass;
|
|
83059
|
+
let chunksPatched = false;
|
|
83060
|
+
let pickQueue = Promise.resolve();
|
|
83061
|
+
let cacheValid = false;
|
|
83062
|
+
let cacheWidth = 0;
|
|
83063
|
+
let cacheHeight = 0;
|
|
83064
|
+
const cacheCamera = createPickCameraSnapshot();
|
|
83065
|
+
const initRasterAccum = (width, height) => {
|
|
83066
|
+
accumBuffer = new Texture(graphicsDevice, {
|
|
83067
|
+
format: PIXELFORMAT_RGBA16F,
|
|
83068
|
+
width,
|
|
83069
|
+
height,
|
|
83070
|
+
mipmaps: false,
|
|
83071
|
+
minFilter: FILTER_NEAREST,
|
|
83072
|
+
magFilter: FILTER_NEAREST,
|
|
83073
|
+
addressU: ADDRESS_CLAMP_TO_EDGE,
|
|
83074
|
+
addressV: ADDRESS_CLAMP_TO_EDGE,
|
|
83075
|
+
name: 'picker-accum'
|
|
83076
|
+
});
|
|
83077
|
+
accumTarget = new RenderTarget({
|
|
83078
|
+
colorBuffer: accumBuffer,
|
|
83079
|
+
depth: false // not needed — gaussians are rendered back to front
|
|
83080
|
+
});
|
|
83081
|
+
accumPass = new RenderPassPicker(graphicsDevice, app.renderer);
|
|
83082
|
+
// RGB: additive depth accumulation. Alpha: multiplicative transmittance.
|
|
83083
|
+
accumPass.blendState = new BlendState(true, BLENDEQUATION_ADD, BLENDMODE_ONE, BLENDMODE_ONE_MINUS_SRC_ALPHA, BLENDEQUATION_ADD, BLENDMODE_ZERO, BLENDMODE_ONE_MINUS_SRC_ALPHA);
|
|
83084
|
+
};
|
|
83085
|
+
const readTexture = (texture, x, y, target) => {
|
|
83086
|
+
const texY = graphicsDevice.isWebGL2 ? target.height - y - 1 : y;
|
|
83087
|
+
return texture.read(x, texY, 1, 1, {
|
|
83088
|
+
renderTarget: target,
|
|
83089
|
+
immediate: true
|
|
83090
|
+
});
|
|
83091
|
+
};
|
|
83092
|
+
const updateCache = (width, height) => {
|
|
83093
|
+
captureCameraSnapshot(camera, cacheCamera);
|
|
83094
|
+
cacheWidth = width;
|
|
83095
|
+
cacheHeight = height;
|
|
83096
|
+
};
|
|
83097
|
+
const cameraMatches = (width, height) => {
|
|
83098
|
+
const cam = camera.camera;
|
|
83099
|
+
return cacheValid &&
|
|
83100
|
+
cacheWidth === width &&
|
|
83101
|
+
cacheHeight === height &&
|
|
83102
|
+
cacheCamera.viewMatrix.equals(cam.viewMatrix) &&
|
|
83103
|
+
cacheCamera.projectionMatrix.equals(cam.projectionMatrix) &&
|
|
83104
|
+
cacheCamera.nearClip === cam.nearClip &&
|
|
83105
|
+
cacheCamera.farClip === cam.farClip &&
|
|
83106
|
+
cacheCamera.projection === cam.projection;
|
|
83107
|
+
};
|
|
83108
|
+
const getCacheCameraSnapshot = () => {
|
|
83109
|
+
const snapshot = createPickCameraSnapshot();
|
|
83110
|
+
snapshot.position.copy(cacheCamera.position);
|
|
83111
|
+
snapshot.viewMatrix.copy(cacheCamera.viewMatrix);
|
|
83112
|
+
snapshot.projectionMatrix.copy(cacheCamera.projectionMatrix);
|
|
83113
|
+
snapshot.nearClip = cacheCamera.nearClip;
|
|
83114
|
+
snapshot.farClip = cacheCamera.farClip;
|
|
83115
|
+
snapshot.projection = cacheCamera.projection;
|
|
83116
|
+
return snapshot;
|
|
83117
|
+
};
|
|
83118
|
+
const readRasterBlock = async (blockX, blockY, blockWidth, blockHeight, viewportWidth, viewportHeight, pickCamera) => {
|
|
83119
|
+
const texY = graphicsDevice.isWebGL2 ? accumTarget.height - blockY - blockHeight : blockY;
|
|
83120
|
+
const pixels = await accumBuffer.read(blockX, texY, blockWidth, blockHeight, {
|
|
83121
|
+
renderTarget: accumTarget,
|
|
83122
|
+
immediate: true
|
|
83123
|
+
});
|
|
83124
|
+
return (x, y) => {
|
|
83125
|
+
const localX = x - blockX;
|
|
83126
|
+
const localY = y - blockY;
|
|
83127
|
+
if (localX < 0 || localX >= blockWidth || localY < 0 || localY >= blockHeight) {
|
|
83128
|
+
return null;
|
|
83129
|
+
}
|
|
83130
|
+
const row = graphicsDevice.isWebGL2 ? blockHeight - localY - 1 : localY;
|
|
83131
|
+
const index = (row * blockWidth + localX) * 4;
|
|
83132
|
+
const r = half2Float(pixels[index]);
|
|
83133
|
+
const transmittance = half2Float(pixels[index + 3]);
|
|
83134
|
+
const alpha = 1 - transmittance;
|
|
83135
|
+
if (!Number.isFinite(r) || !Number.isFinite(alpha) || alpha < 1e-6) {
|
|
83136
|
+
return null;
|
|
83137
|
+
}
|
|
83138
|
+
const normalizedDepth = r / alpha;
|
|
83139
|
+
return getWorldPoint(pickCamera, x, y, viewportWidth, viewportHeight, normalizedDepth);
|
|
83140
|
+
};
|
|
83141
|
+
};
|
|
83142
|
+
const ensureRendered = (width, height, worldLayer) => {
|
|
83143
|
+
if (cameraMatches(width, height)) {
|
|
83199
83144
|
return;
|
|
83200
83145
|
}
|
|
83201
|
-
|
|
83202
|
-
|
|
83203
|
-
|
|
83204
|
-
|
|
83205
|
-
|
|
83146
|
+
// Enable gsplat IDs only while rendering the pick target so we
|
|
83147
|
+
// don't pay the memory/perf cost between pick passes.
|
|
83148
|
+
const prevEnableIds = app.scene.gsplat.enableIds;
|
|
83149
|
+
app.scene.gsplat.enableIds = true;
|
|
83150
|
+
try {
|
|
83151
|
+
if (!chunksPatched) {
|
|
83152
|
+
registerPickerShaderPatches(app);
|
|
83153
|
+
chunksPatched = true;
|
|
83154
|
+
}
|
|
83155
|
+
if (!accumPass) {
|
|
83156
|
+
initRasterAccum(width, height);
|
|
83157
|
+
}
|
|
83158
|
+
else if (cacheWidth !== width || cacheHeight !== height) {
|
|
83159
|
+
cacheValid = false;
|
|
83160
|
+
accumTarget.resize(width, height);
|
|
83161
|
+
}
|
|
83162
|
+
accumPass.init(accumTarget);
|
|
83163
|
+
accumPass.setClearColor(clearColor);
|
|
83164
|
+
accumPass.update(camera.camera, app.scene, [worldLayer], new Map(), false);
|
|
83165
|
+
accumPass.render();
|
|
83166
|
+
updateCache(width, height);
|
|
83167
|
+
cacheValid = true;
|
|
83168
|
+
}
|
|
83169
|
+
finally {
|
|
83170
|
+
app.scene.gsplat.enableIds = prevEnableIds;
|
|
83206
83171
|
}
|
|
83207
|
-
this.updateCursor(e.offsetX, e.offsetY);
|
|
83208
83172
|
};
|
|
83209
|
-
|
|
83210
|
-
|
|
83211
|
-
|
|
83212
|
-
|
|
83213
|
-
|
|
83214
|
-
|
|
83215
|
-
this.canvas.addEventListener('pointerleave', this.onPointerLeave);
|
|
83216
|
-
const updateActive = () => {
|
|
83217
|
-
const flyMouseCaptured = (state.cameraMode === 'fly' &&
|
|
83218
|
-
state.inputMode === 'desktop' &&
|
|
83219
|
-
state.gamingControls);
|
|
83220
|
-
this.active = (state.cameraMode === 'walk' && !state.gamingControls) ||
|
|
83221
|
-
(state.cameraMode === 'fly' && !flyMouseCaptured) ||
|
|
83222
|
-
state.cameraMode === 'orbit';
|
|
83223
|
-
this.surfaceCursorVersion++;
|
|
83224
|
-
this.hoverRing.hide();
|
|
83225
|
-
if (state.inputMode !== 'desktop') {
|
|
83226
|
-
this.hasSurfaceCursorPosition = false;
|
|
83227
|
-
}
|
|
83228
|
-
if (this.targetMode && this.targetMode !== state.cameraMode) {
|
|
83229
|
-
this.navigating = false;
|
|
83230
|
-
this.clearTarget();
|
|
83173
|
+
const prepareSample = (x, y) => {
|
|
83174
|
+
const width = Math.floor(graphicsDevice.width);
|
|
83175
|
+
const height = Math.floor(graphicsDevice.height);
|
|
83176
|
+
// bail out if the device hasn't been sized yet
|
|
83177
|
+
if (width <= 0 || height <= 0) {
|
|
83178
|
+
return null;
|
|
83231
83179
|
}
|
|
83232
|
-
|
|
83233
|
-
|
|
83180
|
+
const worldLayer = app.scene.layers.getLayerByName('World');
|
|
83181
|
+
if (!worldLayer) {
|
|
83182
|
+
return null;
|
|
83234
83183
|
}
|
|
83184
|
+
const screenX = Math.min(width - 1, Math.max(0, Math.floor(x * width)));
|
|
83185
|
+
const screenY = Math.min(height - 1, Math.max(0, Math.floor(y * height)));
|
|
83186
|
+
ensureRendered(width, height, worldLayer);
|
|
83187
|
+
const pickCamera = getCacheCameraSnapshot();
|
|
83188
|
+
return { width, height, screenX, screenY, pickCamera };
|
|
83235
83189
|
};
|
|
83236
|
-
|
|
83237
|
-
|
|
83238
|
-
|
|
83239
|
-
|
|
83240
|
-
this.navigating = true;
|
|
83241
|
-
this.hoverRing.hide();
|
|
83242
|
-
});
|
|
83243
|
-
events.on('navigateCancel', () => {
|
|
83244
|
-
this.navigating = false;
|
|
83245
|
-
this.clearTarget();
|
|
83246
|
-
});
|
|
83247
|
-
events.on('navigateComplete', () => {
|
|
83248
|
-
this.navigating = false;
|
|
83249
|
-
this.clearTarget();
|
|
83250
|
-
});
|
|
83251
|
-
events.on('navTarget:set', (pos, normal) => {
|
|
83252
|
-
const mode = state.cameraMode === 'walk' || state.cameraMode === 'fly' ?
|
|
83253
|
-
state.cameraMode : 'walk';
|
|
83254
|
-
this.setTarget(pos, normal, mode);
|
|
83255
|
-
});
|
|
83256
|
-
events.on('navTarget:clear', () => {
|
|
83257
|
-
this.clearTarget();
|
|
83258
|
-
});
|
|
83259
|
-
events.on('orbitTarget:set', (pos, normal) => {
|
|
83260
|
-
this.navigating = false;
|
|
83261
|
-
this.setTarget(pos, normal, 'orbit');
|
|
83262
|
-
});
|
|
83263
|
-
events.on('orbitTarget:clear', () => {
|
|
83264
|
-
if (this.targetMode === 'orbit') {
|
|
83265
|
-
this.clearTarget();
|
|
83190
|
+
const pickPosition = async (x, y) => {
|
|
83191
|
+
const sample = prepareSample(x, y);
|
|
83192
|
+
if (!sample) {
|
|
83193
|
+
return null;
|
|
83266
83194
|
}
|
|
83267
|
-
|
|
83268
|
-
|
|
83269
|
-
|
|
83195
|
+
const { width, height, screenX, screenY, pickCamera } = sample;
|
|
83196
|
+
const pixels = await readTexture(accumBuffer, screenX, screenY, accumTarget);
|
|
83197
|
+
const r = half2Float(pixels[0]);
|
|
83198
|
+
const transmittance = half2Float(pixels[3]);
|
|
83199
|
+
const alpha = 1 - transmittance;
|
|
83200
|
+
if (!Number.isFinite(r) || !Number.isFinite(alpha) || alpha < 1e-6) {
|
|
83201
|
+
return null;
|
|
83202
|
+
}
|
|
83203
|
+
const normalizedDepth = r / alpha;
|
|
83204
|
+
const position = getWorldPoint(pickCamera, screenX, screenY, width, height, normalizedDepth);
|
|
83205
|
+
return position ? { position, camera: pickCamera, screenX, screenY, width, height } : null;
|
|
83270
83206
|
};
|
|
83271
|
-
|
|
83272
|
-
|
|
83273
|
-
|
|
83274
|
-
|
|
83275
|
-
|
|
83276
|
-
|
|
83277
|
-
|
|
83278
|
-
|
|
83279
|
-
|
|
83280
|
-
|
|
83281
|
-
|
|
83282
|
-
|
|
83283
|
-
|
|
83284
|
-
|
|
83285
|
-
this.targetNormal = null;
|
|
83286
|
-
this.targetMode = null;
|
|
83287
|
-
if (mode === 'orbit') {
|
|
83288
|
-
this.hoverRing.hide();
|
|
83289
|
-
}
|
|
83290
|
-
this.targetRing.hide();
|
|
83291
|
-
this.refreshSurfaceCursor();
|
|
83292
|
-
}
|
|
83293
|
-
pickCollision(offsetX, offsetY) {
|
|
83294
|
-
if (!this.collision) {
|
|
83295
|
-
return null;
|
|
83296
|
-
}
|
|
83297
|
-
const { camera, collision } = this;
|
|
83298
|
-
const cameraPos = camera.getPosition();
|
|
83299
|
-
camera.camera.screenToWorld(offsetX, offsetY, 1.0, tmpV);
|
|
83300
|
-
tmpV.sub(cameraPos).normalize();
|
|
83301
|
-
const hit = collision.queryRay(cameraPos.x, cameraPos.y, cameraPos.z, tmpV.x, tmpV.y, tmpV.z, camera.camera.farClip);
|
|
83302
|
-
if (!hit) {
|
|
83303
|
-
return null;
|
|
83304
|
-
}
|
|
83305
|
-
const sn = collision.querySurfaceNormal(hit.x, hit.y, hit.z, tmpV.x, tmpV.y, tmpV.z);
|
|
83306
|
-
this.collisionTarget.position.set(hit.x, hit.y, hit.z);
|
|
83307
|
-
this.collisionTarget.normal.set(sn.nx, sn.ny, sn.nz);
|
|
83308
|
-
return this.collisionTarget;
|
|
83309
|
-
}
|
|
83310
|
-
async pickSceneTarget(offsetX, offsetY) {
|
|
83311
|
-
const collisionTarget = this.pickCollision(offsetX, offsetY);
|
|
83312
|
-
if (collisionTarget) {
|
|
83313
|
-
return collisionTarget;
|
|
83314
|
-
}
|
|
83315
|
-
const result = await this.picker.pickSurface(offsetX / this.canvas.clientWidth, offsetY / this.canvas.clientHeight);
|
|
83316
|
-
return result ?? null;
|
|
83317
|
-
}
|
|
83318
|
-
shouldShowSurfaceCursor() {
|
|
83319
|
-
const flyMouseCaptured = this.state.cameraMode === 'fly' &&
|
|
83320
|
-
this.state.inputMode === 'desktop' &&
|
|
83321
|
-
this.state.gamingControls;
|
|
83322
|
-
return this.active &&
|
|
83323
|
-
this.state.inputMode === 'desktop' &&
|
|
83324
|
-
this.hasSurfaceCursorPosition &&
|
|
83325
|
-
!this.navigating && ((this.state.cameraMode === 'fly' && !flyMouseCaptured) ||
|
|
83326
|
-
(this.state.cameraMode === 'orbit' && this.targetMode !== 'orbit'));
|
|
83327
|
-
}
|
|
83328
|
-
updateSurfaceCursor(offsetX, offsetY) {
|
|
83329
|
-
this.surfaceCursorX = offsetX;
|
|
83330
|
-
this.surfaceCursorY = offsetY;
|
|
83331
|
-
this.hasSurfaceCursorPosition = true;
|
|
83332
|
-
this.surfaceCursorVersion++;
|
|
83333
|
-
// Render immediately using the most recent depth sample, re-projecting
|
|
83334
|
-
// at the new pointer position. The async pick below will refresh the
|
|
83335
|
-
// sample.
|
|
83336
|
-
this.renderSurfaceCursor();
|
|
83337
|
-
if (!this.surfaceCursorPickPending) {
|
|
83338
|
-
this.processSurfaceCursor();
|
|
83339
|
-
}
|
|
83340
|
-
}
|
|
83341
|
-
renderSurfaceCursor() {
|
|
83342
|
-
if (!this.hasSurfaceSample || !this.shouldShowSurfaceCursor()) {
|
|
83343
|
-
this.hoverRing.hide();
|
|
83344
|
-
return;
|
|
83345
|
-
}
|
|
83346
|
-
const sample = this.surfaceSample;
|
|
83347
|
-
const position = getWorldPoint(sample.camera, this.surfaceCursorX, this.surfaceCursorY, sample.width, sample.height, sample.normalizedDepth, this.scratchHoverPos);
|
|
83348
|
-
if (!position) {
|
|
83349
|
-
this.hoverRing.hide();
|
|
83350
|
-
return;
|
|
83351
|
-
}
|
|
83352
|
-
this.hoverRing.render(position, sample.normal);
|
|
83353
|
-
}
|
|
83354
|
-
refreshSurfaceCursor() {
|
|
83355
|
-
if (this.hasSurfaceCursorPosition && this.shouldShowSurfaceCursor() && !this.surfaceCursorPickPending) {
|
|
83356
|
-
this.surfaceCursorVersion++;
|
|
83357
|
-
this.processSurfaceCursor();
|
|
83358
|
-
}
|
|
83359
|
-
}
|
|
83360
|
-
processSurfaceCursor() {
|
|
83361
|
-
this.surfaceCursorPickPending = true;
|
|
83362
|
-
const version = this.surfaceCursorVersion;
|
|
83363
|
-
// Capture the camera at pick fire time so the depth we extract from
|
|
83364
|
-
// the result is interpretable through the same view/projection. The
|
|
83365
|
-
// surface cursor only animates while the camera is static, so this
|
|
83366
|
-
// snapshot matches the camera the picker actually rendered with.
|
|
83367
|
-
captureCameraSnapshot(this.camera, this.surfaceSample.camera);
|
|
83368
|
-
const snapshotWidth = this.canvas.clientWidth;
|
|
83369
|
-
const snapshotHeight = this.canvas.clientHeight;
|
|
83370
|
-
this.pickSceneTarget(this.surfaceCursorX, this.surfaceCursorY).then((target) => {
|
|
83371
|
-
if (target) {
|
|
83372
|
-
this.surfaceSample.normalizedDepth = worldPointToDepth(this.surfaceSample.camera, target.position);
|
|
83373
|
-
this.surfaceSample.normal.copy(target.normal);
|
|
83374
|
-
this.surfaceSample.width = snapshotWidth;
|
|
83375
|
-
this.surfaceSample.height = snapshotHeight;
|
|
83376
|
-
this.hasSurfaceSample = this.surfaceSample.normalizedDepth >= 0;
|
|
83207
|
+
const serializePick = (operation) => {
|
|
83208
|
+
// The render targets are shared by all picks on this instance.
|
|
83209
|
+
const result = pickQueue.then(operation, operation);
|
|
83210
|
+
pickQueue = result.then(() => undefined, () => undefined);
|
|
83211
|
+
return result;
|
|
83212
|
+
};
|
|
83213
|
+
const pick = async (x, y) => {
|
|
83214
|
+
const result = await pickPosition(x, y);
|
|
83215
|
+
return result?.position ?? null;
|
|
83216
|
+
};
|
|
83217
|
+
const pickSurface = async (x, y) => {
|
|
83218
|
+
const sample = prepareSample(x, y);
|
|
83219
|
+
if (!sample) {
|
|
83220
|
+
return null;
|
|
83377
83221
|
}
|
|
83378
|
-
|
|
83379
|
-
|
|
83222
|
+
const { width, height, screenX, screenY, pickCamera } = sample;
|
|
83223
|
+
// Single block read serves both depth (center pixel) and normal
|
|
83224
|
+
// samples. Sized to the maximum possible ring pixel-radius so the
|
|
83225
|
+
// dynamic ring offsets always lie inside the buffer we read.
|
|
83226
|
+
const blockX = Math.max(0, screenX - NORMAL_SAMPLE_MAX_PX);
|
|
83227
|
+
const blockY = Math.max(0, screenY - NORMAL_SAMPLE_MAX_PX);
|
|
83228
|
+
const blockWidth = Math.min(width - 1, screenX + NORMAL_SAMPLE_MAX_PX) - blockX + 1;
|
|
83229
|
+
const blockHeight = Math.min(height - 1, screenY + NORMAL_SAMPLE_MAX_PX) - blockY + 1;
|
|
83230
|
+
const rasterBlock = await readRasterBlock(blockX, blockY, blockWidth, blockHeight, width, height, pickCamera);
|
|
83231
|
+
const position = rasterBlock(screenX, screenY);
|
|
83232
|
+
if (!position) {
|
|
83233
|
+
return null;
|
|
83380
83234
|
}
|
|
83381
|
-
|
|
83382
|
-
|
|
83383
|
-
|
|
83384
|
-
|
|
83385
|
-
|
|
83235
|
+
const samplePixel = (px, py) => {
|
|
83236
|
+
if (px < 0 || px >= width || py < 0 || py >= height) {
|
|
83237
|
+
return null;
|
|
83238
|
+
}
|
|
83239
|
+
return rasterBlock(px, py);
|
|
83240
|
+
};
|
|
83241
|
+
// Pixel radius corresponding to a fixed world radius at the
|
|
83242
|
+
// picked-point's depth. Clamped so distant picks still sample
|
|
83243
|
+
// enough pixels and very-close picks stay inside the block read.
|
|
83244
|
+
const pixelRadius = Math.max(NORMAL_SAMPLE_MIN_PX, Math.min(NORMAL_SAMPLE_MAX_PX, worldRadiusToPixelRadius(pickCamera, position, height, NORMAL_SAMPLE_WORLD_RADIUS)));
|
|
83245
|
+
const ringPixelRadii = NORMAL_RING_FRACTIONS.map(f => Math.max(1, Math.round(f * pixelRadius)));
|
|
83246
|
+
const sampleRings = ringPixelRadii.map((radius) => {
|
|
83247
|
+
return NORMAL_SAMPLE_DIRECTIONS.map(([dx, dy]) => {
|
|
83248
|
+
return samplePixel(screenX + dx * radius, screenY + dy * radius);
|
|
83249
|
+
});
|
|
83250
|
+
});
|
|
83251
|
+
const toCamera = setCameraFacingNormal(pickCamera.position, position, new Vec3());
|
|
83252
|
+
// Collect every valid 3D sample: the picked position plus all ring
|
|
83253
|
+
// samples that didn't fall off-screen or fail the depth read.
|
|
83254
|
+
const fitPoints = [position];
|
|
83255
|
+
for (let i = 0; i < sampleRings.length; i++) {
|
|
83256
|
+
const ring = sampleRings[i];
|
|
83257
|
+
for (let j = 0; j < ring.length; j++) {
|
|
83258
|
+
const pt = ring[j];
|
|
83259
|
+
if (pt)
|
|
83260
|
+
fitPoints.push(pt);
|
|
83261
|
+
}
|
|
83386
83262
|
}
|
|
83387
|
-
|
|
83388
|
-
|
|
83389
|
-
|
|
83390
|
-
this.processSurfaceCursor();
|
|
83263
|
+
const normal = new Vec3();
|
|
83264
|
+
if (!fitPlaneNormal(fitPoints, toCamera, normal)) {
|
|
83265
|
+
normal.copy(toCamera);
|
|
83391
83266
|
}
|
|
83392
|
-
|
|
83393
|
-
|
|
83394
|
-
|
|
83395
|
-
|
|
83396
|
-
|
|
83397
|
-
|
|
83398
|
-
|
|
83399
|
-
|
|
83400
|
-
|
|
83401
|
-
|
|
83402
|
-
|
|
83403
|
-
|
|
83404
|
-
|
|
83405
|
-
|
|
83406
|
-
|
|
83407
|
-
|
|
83408
|
-
|
|
83409
|
-
this.hoverRing.hide();
|
|
83410
|
-
return;
|
|
83411
|
-
}
|
|
83412
|
-
this.hoverRing.render(target.position, target.normal);
|
|
83413
|
-
}
|
|
83414
|
-
updateTarget() {
|
|
83415
|
-
if (!this.active || !this.targetPos || !this.targetNormal) {
|
|
83416
|
-
return;
|
|
83417
|
-
}
|
|
83418
|
-
const camPos = this.camera.getPosition();
|
|
83419
|
-
const dist = camPos.distance(this.targetPos);
|
|
83420
|
-
if (this.targetMode !== 'orbit' && dist < 2.0) {
|
|
83421
|
-
this.targetRing.hide();
|
|
83422
|
-
return;
|
|
83423
|
-
}
|
|
83424
|
-
this.targetRing.render(this.targetPos, this.targetNormal);
|
|
83425
|
-
}
|
|
83426
|
-
destroy() {
|
|
83427
|
-
this.app.off('prerender', this.onPrerender);
|
|
83428
|
-
this.canvas.removeEventListener('pointermove', this.onPointerMove);
|
|
83429
|
-
this.canvas.removeEventListener('pointerleave', this.onPointerLeave);
|
|
83430
|
-
this.svg.remove();
|
|
83267
|
+
return {
|
|
83268
|
+
position,
|
|
83269
|
+
normal
|
|
83270
|
+
};
|
|
83271
|
+
};
|
|
83272
|
+
this.pick = (x, y) => serializePick(() => pick(x, y));
|
|
83273
|
+
this.pickSurface = (x, y) => serializePick(() => pickSurface(x, y));
|
|
83274
|
+
this.release = () => {
|
|
83275
|
+
if (chunksPatched) {
|
|
83276
|
+
unregisterPickerShaderPatches(app);
|
|
83277
|
+
chunksPatched = false;
|
|
83278
|
+
}
|
|
83279
|
+
accumPass?.destroy();
|
|
83280
|
+
accumTarget?.destroy();
|
|
83281
|
+
accumBuffer?.destroy();
|
|
83282
|
+
cacheValid = false;
|
|
83283
|
+
};
|
|
83431
83284
|
}
|
|
83432
83285
|
}
|
|
83433
83286
|
|
|
@@ -84303,7 +84156,12 @@ class Viewer {
|
|
|
84303
84156
|
this.picker = new Picker(app, camera);
|
|
84304
84157
|
this.inputController = new InputController(global, this.picker);
|
|
84305
84158
|
this.inputController.collision = collision ?? null;
|
|
84159
|
+
// hasCollision = collision data exists (drives fly-mode collision
|
|
84160
|
+
// detection and the voxel/mesh debug overlay availability).
|
|
84161
|
+
// walkAllowed = walk mode is offered to the user; requires both
|
|
84162
|
+
// collision data and a scene large enough to walk around in.
|
|
84306
84163
|
state.hasCollision = !!collision;
|
|
84164
|
+
state.walkAllowed = isWalkAllowed(sceneBound, collision ?? null);
|
|
84307
84165
|
// Create collision debug overlay (voxel uses a compute shader, mesh
|
|
84308
84166
|
// uses standard line rendering). The voxel path requires WebGPU.
|
|
84309
84167
|
if (collision instanceof VoxelCollision && config.renderer !== 'webgl') {
|
|
@@ -84326,7 +84184,7 @@ class Viewer {
|
|
|
84326
84184
|
this.cameraManager = new CameraManager(global, sceneBound, collision);
|
|
84327
84185
|
applyCamera(this.cameraManager.camera);
|
|
84328
84186
|
if (!config.noui) {
|
|
84329
|
-
this.navCursor = new NavCursor(app, camera, collision ?? null, events, state
|
|
84187
|
+
this.navCursor = new NavCursor(app, camera, collision ?? null, events, state);
|
|
84330
84188
|
}
|
|
84331
84189
|
const { instance } = gsplat;
|
|
84332
84190
|
if (instance) {
|
|
@@ -84373,6 +84231,7 @@ class Viewer {
|
|
|
84373
84231
|
gsplat.lodRangeMin = 0;
|
|
84374
84232
|
gsplat.lodRangeMax = 1000;
|
|
84375
84233
|
gsplat.colorUpdateAngle = state.performanceMode ? 4 : 2;
|
|
84234
|
+
gsplat.minContribution = state.performanceMode ? 1 : 2;
|
|
84376
84235
|
};
|
|
84377
84236
|
if (config.fullload) {
|
|
84378
84237
|
// reveal once full quality has finished loading (used for screenshots)
|
|
@@ -85573,6 +85432,7 @@ const main = async (canvas, settingsJson, config) => {
|
|
|
85573
85432
|
hasVR: false,
|
|
85574
85433
|
hasCollision: false,
|
|
85575
85434
|
hasCollisionOverlay: false,
|
|
85435
|
+
walkAllowed: false,
|
|
85576
85436
|
collisionOverlayEnabled: false,
|
|
85577
85437
|
isFullscreen: false,
|
|
85578
85438
|
controlsHidden: false,
|