Giter Club home page Giter Club logo

react-xr's Introduction

@react-three/xr - build experiences for xr vr ar

Version Downloads Discord Shield

React components and hooks for creating VR/AR applications with react-three-fiber

npm install @react-three/xr

These demos are real, you can click them! They contain the full code, too.

Getting started

Add VRCanvas or ARCanvas component (or replace your existing react-three-fiber Canvas component)

import { VRCanvas } from '@react-three/xr'

function App() {
  return (
    <VRCanvas>
      {/* All your regular react-three-fiber elements go here */}
    </VRCanvas>

Adding controllers to the scene

To get started with default controller models add DefaultXRControllers component. It will fetch appropriate input profile models. You can learn more here.

import { VRCanvas, DefaultXRControllers } from '@react-three/xr'

<VRCanvas>
  <DefaultXRControllers />

You can access controllers' state (position, orientation, etc.) by using useXR() hook

const { controllers } = useXR()

API

VRCanvas / ARCanvas

Extended react-three-fiber Canvas that includes:

  • Button to start VR session
  • Color management
  • VR Mode
  • react-xr context

For VR apps use VRCanvas and for AR apps use ARCanvas

import { VRCanvas } from '@react-three/xr'

<VRCanvas>
  {/* All your regular react-three-fiber elements go here */}

useXR

Hook that can only beused by components inside XRCanvas component.

const { controllers } = useXR()

Controllers is an array of XRController objects

interface XRController {
  grip: Group
  controller: Group
  inputSource: XRInputSource
  // ...
  // more in XRController.ts
}

grip and controller are ThreeJS groups that have the position and orientation of xr controllers. grip has an orientation that should be used to render virtual objects such that they appear to be held in the user’s hand and controller has an orientation of the preferred pointing ray.

inputSource is the WebXR input source (MDN). Note that it will not be available before controller is connected.

useXREvent

Every controller emits following events: select, selectstart, selectend, squeeze, squeezestart, squeezeend.

To listen to those events use useXREvent hook:

const onSqueeze = useCallback(() => console.log('Squeezed'), [])
useXREvent('squeeze', onSqueeze)

it supports optional third parameter with options

const onSqueeze = useCallback(() => console.log('Left controller squeeze'), [])
useXREvent('squeeze', onSqueeze, { handedness: 'left' })

useControllers

Use this hook to get an instance of the controller

const leftController = useController('left')

<Hands>

Add hands model for hand-tracking. Currently only works on Oculus Quest with #webxr-hands experimental flag enabled

<VRCanvas>
  <Hands />

Interactions

@react-three/xr comes with built-in high level interaction components.

<Hover>

Hover component will allow you for detecting when ray shot from the controllers is pointing at the given mesh.

<Hover onChange={({isHovered}) => console.log(isHovered ? 'hovered' : 'blurred')}>
  <mesh />
</Hover>

<Select>

Select can be used when you need to select some mesh. Component will trigger onSelect function when controller is pointing at the given mesh and select event was fired.

<Select onSelect={() => console.log('mesh has been selected')}>
  <mesh />
</Select>

Getting the VR Camera (HMD) Location

To get the position of the VR camera, use three's WebXRManager instance.

const { camera } = useThree()
const cam = gl.xr.isPresenting ? gl.xr.getCamera(camera) : camera

Parent VR HMD and Controllers to another object

If you want to attach the user to an object so it can be moved around, just parent the VR camera and controllers to an object3D.

const mesh = useRef()
const { gl, camera } = useThree()

useEffect(() => {
  const cam = gl.xr.isPresenting ? gl.xr.getCamera(camera) : camera;
  const parent = mesh.current;
  if (parent) {
    parent.add(cam);

    return () => { 
      parent.remove(cam)
    };
  }
}, [gl.xr, camera, mesh]);

// bundle add the controllers to the same object as the camera so it all stays together.
const { controllers } = useXR();
useEffect(() => {
  const parent = mesh.current;
  if (parent) {
    if (controllers.length > 0) {
      controllers.forEach((c) => parent.add(c.grip));
    }

    return () => controllers.forEach((c) => parent.remove(c.grip))
  }
}, [controllers, mesh]);

return <mesh ref={mesh} position={[0, 0, 10]}>
    <boxBufferGeometry args={[1, 1, 1]} />
    <meshStandardMaterial />
</mesh>;

react-xr's People

Contributors

adamramberg avatar dependabot[bot] avatar drcmda avatar jhsu avatar micmania1 avatar mwmwmw avatar sniok avatar stephencorwin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.