Skip to Content
HooksuseViewerCamera

useViewerCamera

Provides declarative initial framing, live camera state, named presets, and camera movement helpers without direct CameraControls calls.

Usage

import { ModelViewer, useViewerCamera } from "@liveroom-tech/react-immersive"; const camera = useViewerCamera({ initialPosition: [0, 2, 8], initialTarget: [0, 1.2, 0], presets: { detail: { position: [2, 1.2, 2.5], target: [0, 1.1, 0], fov: 35 }, }, }); const { cameraState, resetView, focusObject, fitScene, setCameraTarget, lookAt, orbitTo, dollyTo, savePreset, goToPreset, handleCameraChange, handleViewerReady, } = camera; <ModelViewer onCameraChange={handleCameraChange} onViewerReady={handleViewerReady} ... />

Returns

NameTypeDescription
isRenderPausedbooleanThe viewer is offscreen and not rendering; commands jump instead of animating
setCameraPresets(presets: Record<string, ViewerCameraPreset>) => voidReplace named views without moving the camera
cameraStateViewerCameraState | nullLatest camera position, target, fov, zoom
cameraPresetsRecord<string, ViewerCameraPreset>Current named views
resetView() => Promise<boolean>Resets the controls to their initial saved state
focusObject(id: string, options?: FocusObjectOptions) => Promise<boolean>Moves to a binding (by key, binding.id, or binding.modelObjectId): to its cameraState view when it has a position and target, otherwise a fit around the object with optional padding
fitScene() => Promise<boolean>Fits the camera to the whole loaded scene
setCameraTarget(target, transition?) => Promise<boolean>Sets the camera target to a given [x, y, z]
setCameraState(state, transition?) => Promise<boolean>Restores a complete camera state
lookAt(position, target, transition?) => Promise<boolean>Moves the camera position and target together
orbitTo(azimuth, polar, transition?) => Promise<boolean>Orbits to angles in degrees (azimuth 0 = front, 90 = right; polar 0 = straight down, 90 = level)
dollyTo(distance, transition?) => Promise<boolean>Moves to a distance from the current target
savePreset(name: string) => ViewerCameraState | nullSaves the current view under a name
goToPreset(name, transition?) => Promise<boolean>Moves to a named view
handleCameraChange(state: ViewerCameraState) => voidCallback to pass to onCameraChange
handleViewerReady(viewer: ViewerReadyState) => voidPass to onViewerReady, or compose with useViewerConnection

Example

<button onClick={() => resetView()}>Reset Camera</button> <button onClick={() => focusObject("some-object-id")}>Focus Object</button> <button onClick={() => fitScene()}>Fit Scene</button> <button onClick={() => setCameraTarget([0, 0, 0])}>Target Origin</button> <button onClick={() => lookAt([4, 2, 6], [0, 1, 0])}>Three-quarter view</button> <button onClick={() => orbitTo(90, 60)}>View from the right</button> <button onClick={() => dollyTo(4)}>Move closer</button> <button onClick={() => savePreset("custom")}>Save View</button> <button onClick={() => goToPreset("custom")}>Restore View</button> <pre>{JSON.stringify(cameraState, null, 2)}</pre>

Each initial position or target is optional and is applied once for every loaded model. Set initialTransition: true to animate into it. The default is false. cameraPresets contains the current named preset record.

Object focus padding

Pass FocusObjectOptions as the optional second argument to focusObject:

await camera.focusObject("door", { padding: 0 }); // Tight fit. await camera.focusObject("door", { padding: 0.2, paddingRight: 1, // Override the right side to leave extra room. paddingTop: 0, }); // With useViewer: viewer.camera.focusObject("door", options).

Padding is measured in scene units along the fitted view’s axes. padding sets all four sides; paddingTop, paddingBottom, paddingLeft, and paddingRight override individual sides. Omitted values default to 0.5, preserving existing calls. Zero is supported. Negative or non-finite effective padding returns false without moving the camera.

A binding’s saved cameraState still takes precedence. Padding affects only automatic object fitting, so it does not alter an authored camera view. It does not change fitScene, reset framing, or selection behavior.

Viewer-ready capture methods

ViewerReadyState includes captureImage, captureVideo, and invalidate. Compose camera with useViewerConnection to capture PNGs or record cinematic/turntable video while keeping camera controls connected. After WebGL recovery , handleViewerReady reconnects to the rebuilt viewer.

Offscreen rendering and interrupted moves

The viewer stops rendering while it is offscreen, and isRenderPaused on useViewerCamera() reports this. Camera commands still complete while it is paused: a move jumps straight to its end instead of animating, and a move that is animating when the viewer scrolls away finishes the same way. The camera is where you asked when the viewer comes back, so an external guided tour, an initial position or a restored shared view never waits for the user to scroll.

Camera commands return false when the viewer disconnects, for example after unmounting or switching to a first-person controller. Animation hooks expose isRenderPaused separately from playback intent: a clip can remain isPlaying while its clock is suspended, then continue on return.

Loading edited presets

presets initializes the hook once, like a React state initializer. Rerendering with new options preserves views saved during the session. To replace them after fetching saved edits, call camera.setCameraPresets(savedPresets). This replaces all named views, without moving the camera; pass {} to clear them. Call goToPreset(name) explicitly to open one of the loaded views.