Skip to main content
A common interaction pattern: double-click a body to select it, highlight the selected body, and use the selection for further actions. This combines R3F raycasting with the library’s useSelectionHighlight hook.

The Pattern

  1. A hook listens for double-clicks and raycasts to find the clicked body
  2. The useSelectionHighlight hook applies a visual highlight to the selected body

Composing with useSelectionHighlight

Then drop it into your scene:

Using the onSelection Callback

<MujocoCanvas> also has a built-in onSelection callback that fires on double-click:

How SceneRenderer Stores Body IDs

<SceneRenderer /> sets userData.bodyID on every mesh it creates. When raycasting, walk up the object tree with obj.parent until you find a node with userData.bodyID — child meshes (e.g. geom sub-meshes) don’t have it directly, but their parent body group does.

useSelectionHighlight Options

For more advanced selection visuals (outlines, postprocessing, custom shaders), use useBodyMeshes to get direct access to the body’s Three.js meshes.