npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-xr

v1.1.0

Published

[![npm version](https://badge.fury.io/js/react-xr.svg)](https://badge.fury.io/js/react-xr) ![npm](https://img.shields.io/npm/dt/react-xr.svg)

Downloads

131

Readme

react-xr

npm version npm

WebXR + react-three-fiber

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

Note: Extremely early in development. Contributors welcome!

Installation

yarn add react-xr

Getting started

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

import { VRCanvas } from 'react-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-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-xr'

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

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 n 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-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={(value) => console.log(value ? '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
  mesh.current.add(cam)
  return () => mesh.current.remove(cam)
}, [gl.xr.isPresenting, gl.xr, camera, mesh])

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