Skip to Content
HooksuseViewer

useViewer

Every viewer hook in one, already connected to each other and to ModelViewer. Spread its props onto the viewer and control everything else by name.

Usage

import { ModelViewer, useViewer } from "@liveroom-tech/react-immersive"; export default function Configurator() { const viewer = useViewer(); return ( <> <ModelViewer modelUrl="/car.glb" licenseKey={licenseKey} {...viewer.props} /> <button onClick={() => viewer.bindings.setBaseColor({ group: "paint" }, "#b91c1c") } > Paint it red </button> </> ); }

Props you set on ModelViewer after the spread take precedence, so other props (showViewGizmo, sceneConfig, and so on) work as usual.

Options

All options are optional.

NameTypeDescription
bindingsRecord<string, ObjectBinding> | (() => Record<string, ObjectBinding>) | "auto"The starting bindings. Defaults to "auto": a binding for every mesh, generated when the model loads
sceneConfigSceneConfig | (() => SceneConfig)The starting scene config
cameraUseViewerCameraOptionsInitial camera position, target, and named presets
onAction(event: ObjectActionEvent) => voidCalled for every action button, after built-in behavior and declared effects
onObjectSelect(binding: ObjectBinding | null) => voidCalled when the selection changes
onObjectHover(binding: ObjectBinding | null) => voidCalled when the hovered object changes
onLoadError(error: unknown) => voidCalled if the model fails to load

With "auto" bindings, a binding the viewer already has for an object keeps its edits when the model reloads; objects with new names get fresh bindings.

Returns

NameSame asUse it for
propsSpread onto ModelViewer
bindingsuseObjectBindingsColors, textures, materials, visibility, transforms, metadata
selectionuseViewerSelectionThe selected object, and selecting from your own UI
hoveruseViewerHoverThe hovered object
camerauseViewerCameraFocusing objects, resetting, saved views
modeluseViewerModelLoading state, errors, bounds, and the model’s objects
animationsuseViewerAnimationsPlaying the model’s animation clips
actionsuseViewerActionsRunning binding actions from code
sceneuseSceneConfigLighting, environment, background, post-processing
isolationuseViewerIsolationFocusing on some objects, ghosting or hiding the rest
effectsuseViewerEffectsTransitions, pulses, flicker, runtime lights
connectionuseViewerConnectionThe live viewer, captureImage, and captureVideo

Each part is that hook’s full result, so everything the hook offers is available: viewer.camera.focusObject("door"), viewer.model.objects, viewer.animations.play("Open"), viewer.scene.lights.setIntensity("key", 2).

useViewer passes sceneConfig to ModelViewer only once you’ve supplied one or changed the scene through viewer.scene, so the default scene behaves exactly as if no scene config were passed.

When to use the individual hooks

Use the hooks one by one when state lives elsewhere: for example, when a store owns the bindings and passes them to ModelViewer, or when you only need one piece, such as the camera.