Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 | import React, { ReactNode, useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useTheme } from '@mui/material';
import { Entity } from '@playcanvas/react';
import { Camera, Script } from '@playcanvas/react/components';
import { useApp } from '@playcanvas/react/hooks';
import { Color, Entity as PcEntity, Vec3, XrInputSource } from 'playcanvas';
import { XrControllers } from 'playcanvas/scripts/esm/xr/xr-controllers.mjs';
import { CameraEntityContext } from '../../hooks/cameraEntityContext';
import { useCameraPosition } from '../../hooks/useCameraPosition';
import { useDevicePerformance } from '../../hooks/useDevicePerformance';
import { useXr } from '../../hooks/useXr';
import Annotation, { AnnotationProps } from './Annotation';
import AnnotationPanel from './AnnotationPanel';
import { AutoRotator } from './AutoRotator';
import BoundaryRing from './BoundaryRing';
import BoundaryWall from './BoundaryWall';
import GradientSky from './GradientSky';
import SplatControls from './SplatControls';
import SplatModel, { SplatModelProps } from './SplatModel';
import { TfAnnotationManager } from './TfAnnotationManager';
import { TfXrNavigation } from './TfXrNavigation';
import VrAnnotationPanel from './VrAnnotationPanel';
import XrAnnotationInteraction from './XrAnnotationInteraction';
import XrExitButton from './XrExitButton';
import XrGazeDwell from './XrGazeDwell';
import XrPointerRay from './XrPointerRay';
import { XrStartPose } from './XrStartPose';
import { SplatFormat } from './splatFormat';
import { useAdoptedCamera } from './useAdoptedCamera';
import { useXrRenderTuning } from './useXrRenderTuning';
import { WalkthroughCamera } from './walkthrough-camera';
import { rayHitsInteractiveUi } from './xr-interactive-ui';
const DEFAULT_FOCUS_POINT: [number, number, number] = [0, 0.1, 0];
const DEFAULT_POSITION: [number, number, number] = [1, 0.1, 0];
// How far outside the clamp radius the boundary wall stands. Without it the wall would be coplanar
// with the surface the head is clamped to, so a pinned user would have it in their face and through
// their near clip plane.
const WALL_INSET = 0.25;
/**
* Largest world size (metres) a hotspot is allowed to reach as the camera approaches it, so it
* stops growing rather than filling the view from close up.
*/
const MAX_HOTSPOT_WORLD_SIZE = 0.75;
/**
* Every coordinate, distance and size in this component is world-space, in the metres an XR
* headset imposes on the scene: `scaleFactor` converts the splat's own arbitrary units to them,
* and is applied to nothing but the splat itself.
*/
export interface VirtualWalkthroughViewerProps {
splatSrc: string;
splatFormat?: SplatFormat;
/**
* Camera component mounted on the camera entity. Defaults to a 60 degree camera clearing to the
* horizon color.
*
* Pass null to mount no camera of the walkthrough's own, which is what a caller sharing a scene
* it does not own wants: an XR session is welded to the camera it was started on, so a
* walkthrough that brought its own could only take over by ending the session and dropping the
* user out of the headset. The walkthrough adopts the scene's existing camera into its rig
* instead, for as long as it is mounted.
*
* A scene shared this way is expected to hold exactly one camera. The adoption takes the first
* camera in graph traversal order, which is only certain to be the one the headset renders
* through while it is the only one there: a scene that also carries a spectator or overlay camera
* can hand the rig the wrong entity, and locomotion then goes inert with nothing reported. Prefer
* the session's own camera, `app.xr.camera`, over the traversal if that ever needs to hold.
*/
camera?: ReactNode;
/** World-space point the camera looks at. */
origin?: [number, number, number];
/** World-space point the camera starts at. */
cameraPosition?: [number, number, number];
/** World-space centre (x, y, z) and radius (m) of the circle the camera is held inside. */
sceneBounds?: { x: number; y: number; z: number; m: number };
/** Three world-space points defining the plane the camera walks on. */
groundPlane?: [number, number, number][];
skyColor?: string;
groundColor?: string;
/** Eye height (m) above the ground plane. */
averageCameraHeight?: number;
/**
* Scale that converts the splat model's units to the world's metres. Applied to the splat
* entity alone — every other coordinate this component takes is already world-space.
*/
scaleFactor?: number;
/**
* Overrides passed through to the splat model, for tuning things like `splatBudget` and
* `maxPixelRatio`. The props this component drives itself — `splatSrc`, `splatFormat`,
* `rotation`, `scaleFactor` and `revealRain` — take precedence over anything set here.
*/
splatModelProps?: Partial<SplatModelProps>;
/**
* Requests a VR session when this goes from false to true, and when the viewer mounts with it
* already true. Never re-requests on its own, so exiting VR leaves the user out. Browsers only
* grant an immersive session inside a user gesture, so the caller must raise this from a click.
*/
autoStartVr?: boolean;
/**
* Called whenever the viewer ends up out of XR: when a session ends, and when one was requested
* but couldn't be started, in which case the error says why.
*/
onXrExit?: (error?: Error) => void;
/**
* Runs instead of ending the XR session when the user activates the in-headset exit button.
* Without it the button ends the session, which in turn reports `onXrExit`; with it the session
* stays running and neither happens, leaving the handler to decide what closing means.
*/
onXrClose?: () => void;
annotations: AnnotationProps[];
onSaveAnnotations: (annotations: AnnotationProps[]) => void | Promise<void>;
editable?: boolean;
showFreeFly?: boolean;
isFullScreen?: boolean;
onToggleFullScreen?: () => void;
maxImagesPerAnnotation?: number;
}
const VirtualWalkthroughViewer = ({
splatSrc,
splatFormat,
camera = <Camera clearColor='#EAF8FF' fov={60} />,
origin = DEFAULT_FOCUS_POINT,
cameraPosition = DEFAULT_POSITION,
sceneBounds,
groundPlane: groundPlaneProp,
skyColor,
groundColor,
averageCameraHeight = 0,
scaleFactor = 1,
splatModelProps,
autoStartVr = false,
onXrExit,
onXrClose,
annotations,
onSaveAnnotations,
editable = false,
showFreeFly = false,
isFullScreen = false,
onToggleFullScreen,
maxImagesPerAnnotation,
}: VirtualWalkthroughViewerProps) => {
const theme = useTheme();
// Held as state so the effects and hooks that address these entities re-run once they
// exist. Addressed directly: a viewer mounted into a host's scene shares the graph
// with the host's entities, so the camera can be found by component type.
const [cameraRoot, setCameraRoot] = useState<PcEntity | null>(null);
const [cameraEntity, setCameraEntity] = useState<PcEntity | null>(null);
const adoptedCamera = useAdoptedCamera(cameraRoot, cameraEntity);
// The entity carrying the live camera component: the viewer's own, or the host's while adopted.
// Everything that poses the camera or projects through it addresses this rather than the entity
// the viewer mounts a camera of its own on, which holds nothing while a camera is adopted.
const activeCamera = adoptedCamera ?? cameraEntity;
const { setCamera } = useCameraPosition(activeCamera);
const { isHighPerformance } = useDevicePerformance();
const app = useApp();
const [showAnnotations, setShowAnnotations] = useState(true);
const [autoRotate, setAutoRotate] = useState(true);
const [isEdit, setIsEdit] = useState(false);
const [isFreeFly, setIsFreeFly] = useState(false);
const [selectedAnnotationIndex, setSelectedAnnotationIndex] = useState(-1);
const [localAnnotations, setLocalAnnotations] = useState<AnnotationProps[]>(annotations);
const [isTextFieldFocused, setIsTextFieldFocused] = useState(false);
const [viewingAnnotation, setViewingAnnotation] = useState<AnnotationProps | null>(null);
const [viewingAnnotationIndex, setViewingAnnotationIndex] = useState(-1);
const [viewedScreenPos, setViewedScreenPos] = useState<{ x: number; y: number; size?: number } | null>(null);
const reportXrError = useCallback(
(error: Error) => {
console.warn(error.message, error);
onXrExit?.(error);
},
[onXrExit]
);
const { isCurrentlyInXr, isCurrentlyInVr, isCurrentlyInAr, isXrAvailable, startXr } = useXr({
onError: reportXrError,
});
useXrRenderTuning();
const autoStartRequested = useRef(false);
// Edge-triggered so that ending a session doesn't pull the user straight back into one. The ref
// is written before the early returns, so lowering the prop re-arms the next rising edge.
useEffect(() => {
const isRisingEdge = autoStartVr && !autoStartRequested.current;
autoStartRequested.current = autoStartVr;
if (!isRisingEdge || isCurrentlyInXr) {
return;
}
if (!isXrAvailable('VR')) {
reportXrError(new Error('A VR session was requested, but this device reports no immersive VR support'));
return;
}
startXr('VR');
}, [autoStartVr, isCurrentlyInXr, isXrAvailable, startXr, reportXrError]);
const wasInXr = useRef(false);
// Edge-detected rather than reported whenever the viewer is out of XR, which would fire on mount
// and tell a caller a session had ended before one ever began.
useEffect(() => {
if (wasInXr.current && !isCurrentlyInXr) {
onXrExit?.();
}
wasInXr.current = isCurrentlyInXr;
}, [isCurrentlyInXr, onXrExit]);
const sceneBoundsRadius = useMemo(() => {
if (sceneBounds?.m !== undefined) {
return sceneBounds.m;
}
const dx = cameraPosition[0] - origin[0];
const dy = cameraPosition[1] - origin[1];
const dz = cameraPosition[2] - origin[2];
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}, [cameraPosition, sceneBounds, origin]);
const sceneBoundsCenter = useMemo(
() =>
sceneBounds
? new Vec3(sceneBounds.x, sceneBounds.y, sceneBounds.z)
: new Vec3(origin[0], cameraPosition[1], origin[2]),
[sceneBounds, origin, cameraPosition]
);
const groundPlane = useMemo<Vec3[]>(
() => (groundPlaneProp?.length === 3 ? groundPlaneProp.map((p) => new Vec3(p[0], p[1], p[2])) : []),
[groundPlaneProp]
);
// Also runs when a session ends. WalkthroughCamera is unmounted for the duration of a session, so
// the camera entity is left holding the last head pose — an eye height above wherever in the rig
// the user was standing — which is neither where nor how high the walkthrough left off.
useEffect(() => {
if (isCurrentlyInXr) {
return;
}
setCamera(origin, cameraPosition);
}, [origin, cameraPosition, setCamera, isCurrentlyInXr]);
// Re-applied when a session ends for the same reason: the script that comes back is a new
// instance, and without the ground plane it walks the camera at the bounds centre's height
// instead of an eye height above the ground.
useEffect(() => {
if (!groundPlane.length || isCurrentlyInXr) {
return;
}
// @ts-expect-error - scripts are added dynamically to the entity
const walkthroughCam = cameraRoot?.script?.walkthroughCamera;
if (walkthroughCam) {
// Should be changed to a react prop if shallowEquals in playcanvas/react is fixed (see https://github.com/playcanvas/react/pull/298)
walkthroughCam.groundPlane = groundPlane;
}
}, [groundPlane, cameraRoot, isCurrentlyInXr]);
useEffect(() => {
// Set imperatively for the same reason as BoundaryRing: boundsCenter is a Vec3, and the
// @playcanvas/react memo() comparator stops at the first prop with an .equals() method.
// @ts-expect-error - scripts are added dynamically to the entity
const navigation = cameraRoot?.script?.tfXrNavigation;
if (navigation) {
navigation.boundsCenter = sceneBoundsCenter;
navigation.boundsRadius = sceneBoundsRadius;
}
}, [cameraRoot, sceneBoundsCenter, sceneBoundsRadius]);
const handleToggleFreeFly = useCallback(() => {
const newFreeFly = !isFreeFly;
// @ts-expect-error - scripts are added dynamically to the entity
const walkthroughCam = cameraRoot?.script?.walkthroughCamera;
if (walkthroughCam) {
walkthroughCam.freeFly = newFreeFly;
}
if (!newFreeFly) {
setCamera(origin, cameraPosition);
}
setIsFreeFly(newFreeFly);
}, [isFreeFly, cameraRoot, setCamera, origin, cameraPosition]);
useEffect(() => {
setLocalAnnotations(annotations);
}, [annotations]);
useEffect(() => {
if (!isEdit) {
setSelectedAnnotationIndex(-1);
}
}, [isEdit]);
const handleAnnotationPositionChange = useCallback(
(position: [number, number, number]) => {
setLocalAnnotations((prev) => {
if (selectedAnnotationIndex === -1) {
return prev;
}
const updated = [...prev];
updated[selectedAnnotationIndex] = { ...updated[selectedAnnotationIndex], position };
return updated;
});
},
[selectedAnnotationIndex]
);
const handleSave = useCallback(() => {
const saveAndClose = async () => {
await onSaveAnnotations(localAnnotations);
setIsEdit(false);
setSelectedAnnotationIndex(-1);
};
void saveAndClose();
}, [onSaveAnnotations, localAnnotations]);
const handleCancel = useCallback(() => {
setLocalAnnotations(annotations);
setIsEdit(false);
setSelectedAnnotationIndex(-1);
}, [annotations]);
const handleAddAnnotation = useCallback(() => {
const newAnnotation: AnnotationProps = { position: origin, title: '' };
setLocalAnnotations((prev) => [...prev, newAnnotation]);
setSelectedAnnotationIndex(localAnnotations.length);
}, [origin, localAnnotations]);
const handleDeleteAnnotation = useCallback(() => {
if (selectedAnnotationIndex === -1) {
return;
}
setLocalAnnotations((prev) => prev.filter((_, index) => index !== selectedAnnotationIndex));
setSelectedAnnotationIndex(-1);
}, [selectedAnnotationIndex]);
const handleDeselectAnnotation = useCallback(() => {
setSelectedAnnotationIndex(-1);
}, []);
const handleAnnotationView = useCallback(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
(annotation: AnnotationProps, annotationIndex: number, screenX: number, screenY: number) => {
setViewingAnnotation(annotation);
setViewingAnnotationIndex(annotationIndex);
// Cleared so the connector line waits for the hotspot's post-aim position
// rather than briefly pointing at its old spot.
setViewedScreenPos(null);
},
[]
);
const handleAnnotationScreenPositionUpdate = useCallback(
(_index: number, screenX: number, screenY: number, size?: number) => {
if (isCurrentlyInXr) {
return;
}
// The scene is frozen while the panel is open, so guard against redundant
// updates from the per-frame callback to avoid needless re-renders.
setViewedScreenPos((prev) =>
prev && prev.x === screenX && prev.y === screenY && prev.size === size ? prev : { x: screenX, y: screenY, size }
);
},
[isCurrentlyInXr]
);
const handleCloseAnnotation = useCallback(() => {
setViewingAnnotation(null);
setViewingAnnotationIndex(-1);
setViewedScreenPos(null);
}, []);
const isTeleportBlocked = useCallback(
(inputSource: XrInputSource) => rayHitsInteractiveUi(app, inputSource.getOrigin(), inputSource.getDirection()),
[app]
);
useEffect(() => {
if (!isCurrentlyInXr) {
handleCloseAnnotation();
}
}, [isCurrentlyInXr, handleCloseAnnotation]);
const handleAnnotationUpdate = useCallback(
(updates: Partial<AnnotationProps>) => {
if (selectedAnnotationIndex === -1) {
return;
}
setLocalAnnotations((prev) => {
const updated = [...prev];
updated[selectedAnnotationIndex] = { ...updated[selectedAnnotationIndex], ...updates };
return updated;
});
},
[selectedAnnotationIndex]
);
const handleImagesChange = useCallback(
(files: File[]) => handleAnnotationUpdate({ images: files }),
[handleAnnotationUpdate]
);
const canSave = useMemo(
() => localAnnotations.every((annotation) => annotation.title && annotation.title.trim() !== ''),
[localAnnotations]
);
const splatModel = useMemo(
() => (
<SplatModel
key='splat'
{...splatModelProps}
splatSrc={splatSrc}
splatFormat={splatFormat}
rotation={[-180, 0, 0]}
scaleFactor={scaleFactor}
revealRain={isHighPerformance}
/>
),
[isHighPerformance, splatSrc, splatFormat, scaleFactor, splatModelProps]
);
return (
<CameraEntityContext.Provider value={activeCamera}>
<GradientSky
topColor={skyColor || '#FFFFFF'}
horizonColor={skyColor || '#EAF8FF'}
groundColor={groundColor || '#C3BDB7'}
/>
<Entity name='camera-root' ref={setCameraRoot}>
<Entity name='camera' ref={setCameraEntity}>
{camera}
</Entity>
{/* Mounted on the rig rather than on the camera entity, so it poses whichever camera the rig
is holding — the viewer's own, or one adopted from the host scene, which hangs off the rig
beside the entity the viewer mounts its own on. */}
{!isCurrentlyInXr && (
<Script
script={WalkthroughCamera}
boundsCenter={sceneBoundsCenter}
boundsRadius={sceneBoundsRadius}
enableFly={!isTextFieldFocused}
averageCameraHeight={averageCameraHeight}
/>
)}
{/* Sibling of the camera (not a child): WalkthroughCamera rewrites the camera entity's
transform every frame, so the button drives its own world pose from the XR head pose. */}
<XrExitButton onClose={onXrClose} />
<Script script={XrControllers} enabled={!isEdit} />
<XrAnnotationInteraction onDismiss={handleCloseAnnotation} />
<XrPointerRay />
{/* VR only: the panel is driven by controller rays, which AR sessions don't have. */}
{isCurrentlyInVr && viewingAnnotation && (
<VrAnnotationPanel
key={viewingAnnotationIndex}
annotation={viewingAnnotation}
annotationIndex={viewingAnnotationIndex}
/>
)}
<XrGazeDwell activeIndex={viewingAnnotationIndex} />
{/* Number props rather than a Vec3: @playcanvas/react's memo() comparator stops at the
first prop with an .equals() method, so a Vec3 would stop the ones after it updating. */}
<Script
script={XrStartPose}
targetX={cameraPosition[0]}
targetZ={cameraPosition[2]}
focusX={origin[0]}
focusZ={origin[2]}
/>
<Script
script={TfXrNavigation}
enabled={!isEdit}
enableTeleport={!isCurrentlyInAr && !viewingAnnotation}
enableSnapVertical={false}
isTeleportBlocked={isTeleportBlocked}
/>
{!isCurrentlyInXr && (
<Script
script={AutoRotator}
enabled={!isEdit && autoRotate && !viewingAnnotation}
startDelay={0.5}
restartDelay={3}
startFadeInTime={0.5}
/>
)}
</Entity>
{/* Deliberately unscaled: the splat carries scaleFactor itself, so everything mounted here
shares the one world space the camera rig and the XR head pose already live in. */}
<Entity name='content-root'>
{splatModel}
{sceneBounds?.m !== undefined && groundPlane.length === 3 && (
<BoundaryRing center={sceneBoundsCenter} radius={sceneBoundsRadius} groundPlane={groundPlane} />
)}
{localAnnotations.length > 0 && (
<Entity name='annotations-root'>
<Script
script={TfAnnotationManager}
enabled={true}
hotspotSize={30}
maxWorldSize={MAX_HOTSPOT_WORLD_SIZE}
opacity={1}
hotspotColor={new Color().fromString(theme.palette.TwClrIcnBrand as string)}
hoverColor={new Color().fromString('#ffffff')}
hotspotBackgroundColor={theme.palette.TwClrBaseWhite as string}
/>
{localAnnotations.map((annotation, index) => (
<Annotation
key={`annotation-${index}`}
{...annotation}
index={index}
visible={showAnnotations}
isEdit={isEdit}
isSelected={selectedAnnotationIndex === index}
isViewed={viewingAnnotationIndex === index}
onSelect={() => setSelectedAnnotationIndex(index)}
onPositionChange={handleAnnotationPositionChange}
onView={(anno, screenX, screenY) => handleAnnotationView(anno, index, screenX, screenY)}
onScreenPositionUpdate={handleAnnotationScreenPositionUpdate}
/>
))}
</Entity>
)}
</Entity>
{isCurrentlyInXr && sceneBoundsRadius > 0 && (
<BoundaryWall
center={sceneBoundsCenter}
radius={sceneBoundsRadius + WALL_INSET}
groundPlane={groundPlane}
baseY={0}
/>
)}
<SplatControls
defaultCameraFocus={origin}
defaultCameraPosition={cameraPosition}
showAnnotations={showAnnotations}
onToggleAnnotations={setShowAnnotations}
autoRotate={autoRotate}
onToggleAutoRotate={setAutoRotate}
isEdit={isEdit}
onToggleEdit={setIsEdit}
onSave={handleSave}
onCancel={handleCancel}
onAddAnnotation={handleAddAnnotation}
onDeleteAnnotation={handleDeleteAnnotation}
onDeselectAnnotation={handleDeselectAnnotation}
hasSelectedAnnotation={selectedAnnotationIndex >= 0}
selectedAnnotation={selectedAnnotationIndex >= 0 ? localAnnotations[selectedAnnotationIndex] : null}
selectedAnnotationIndex={selectedAnnotationIndex}
onAnnotationUpdate={handleAnnotationUpdate}
onImagesChange={handleImagesChange}
maxImagesPerAnnotation={maxImagesPerAnnotation}
onTextFieldFocus={setIsTextFieldFocused}
canSave={canSave}
editable={editable}
isFullScreen={isFullScreen}
onToggleFullScreen={onToggleFullScreen}
isFreeFly={isFreeFly}
onToggleFreeFly={handleToggleFreeFly}
showFreeFly={showFreeFly}
onError={reportXrError}
/>
<AnnotationPanel
annotation={viewingAnnotation}
hotspotPosition={viewedScreenPos}
onClose={handleCloseAnnotation}
/>
</CameraEntityContext.Provider>
);
};
export default VirtualWalkthroughViewer;
|