react-xr
VR/AR for react-three-fiber
README
@react-three/xr
React components and hooks for creating VR/AR applications with @react-three/fiber
- ```bash
- npm install @react-three/xr
- ```
Examples
These demos are real, you can click them! They contain the full code, too.
Getting started
The following adds a button to start your session and controllers inside an XR manager to prepare your scene for WebXR rendering and interaction.
- ```jsx
- import { VRButton, ARButton, XR, Controllers, Hands } from '@react-three/xr'
- import { Canvas } from '@react-three/fiber'
- function App() {
- return (
- <>
- <VRButton />
- <Canvas>
- <XR>
- <Controllers />
- <Hands />
- <mesh>
- <boxGeometry />
- <meshBasicMaterial color="blue" />
- </mesh>
- </XR>
- </Canvas>
- </>
- )
- }
- ```
XRButton
XRButton is an HTML button that can be used to init and display info about your WebXR session. This is aliased by ARButton and VRButton with sensible session defaults.
- ```jsx
- <XRButton
- /* The type of `XRSession` to create */
- mode={'AR' | 'VR' | 'inline'}
- /**
- * `XRSession` configuration options
- * @see https://immersive-web.github.io/webxr/#feature-dependencies
- */
- sessionInit={{ optionalFeatures: ['local-floor', 'bounded-floor', 'hand-tracking', 'layers'] }}
- /** Whether this button should only enter an `XRSession`. Default is `false` */
- enterOnly={false}
- /** Whether this button should only exit an `XRSession`. Default is `false` */
- exitOnly={false}
- /** This callback gets fired if XR initialization fails. */
- onError={(error) => ...}
- >
- {/* Can accept regular DOM children and has an optional callback with the XR button status (unsupported, exited, entered) */}
- {(status) => `WebXR ${status}`}
- </XRButton>
- ```
XR
XR is a WebXR manager that configures your scene for XR rendering and interaction. This lives within a R3F Canvas.
- ```jsx
- <Canvas>
- <XR
- /**
- * Enables foveated rendering. Default is `0`
- * 0 = no foveation, full resolution
- * 1 = maximum foveation, the edges render at lower resolution
- */
- foveation={0}
- /**
- * The target framerate for the XRSystem. Smaller rates give more CPU headroom at the cost of responsiveness.
- * Recommended range is `72`-`120`. Default is unset and left to the device.
- * @note If your experience cannot effectively reach the target framerate, it will be subject to frame reprojection
- * which will halve the effective framerate. Choose a conservative estimate that balances responsiveness and
- * headroom based on your experience.
- * @see https://developer.mozilla.org/en-US/docs/Web/API/WebXR_Device_API/Rendering#refresh_rate_and_frame_rate
- */
- frameRate={72 | 90 | 120}
- /** Type of WebXR reference space to use. Default is `local-floor` */
- referenceSpace="local-floor"
- /** Called as an XRSession is requested */
- onSessionStart={(event: XREvent<XRManagerEvent>) => ...}
- /** Called after an XRSession is terminated */
- onSessionEnd={(event: XREvent<XRManagerEvent>) => ...}
- /** Called when an XRSession is hidden or unfocused. */
- onVisibilityChange={(event: XREvent<XRSessionEvent>) => ...}
- /** Called when available inputsources change */
- onInputSourcesChange={(event: XREvent<XRSessionEvent>) => ...}
- >
- {/* All your regular react-three-fiber elements go here */}
- </XR>
- </Canvas>
- ```