Closed itsdouges closed 1 year ago
By @verekia:
Relevant lines: https://github.com/pmndrs/react-xr/blob/master/src/XR.tsx#L29 https://github.com/pmndrs/react-xr/blob/master/src/XR.tsx#L431
const XRContext = React.createContext<UseBoundStore<XRState>>(null!)
// ...
export function XR(props: XRProps) {
// ...
return (
<XRContext.Provider value={store}>
<XRManager {...props} />
</XRContext.Provider>
)
}
// ...
export function useXR() {
// ...
const store = React.useContext(XRContext)
if (!store) throw new Error('useXR must be used within an <XR /> component!')
Would it work to export
that const XRContext
?
By @verekia:
My projects are mixed experiences. You can play with mobile, desktop, and VR. Metaverses (like https://hyperfy.io or https://thirdroom.io/) are also mixed experiences. I think it's a great strength of 3D on the web to be able to enable WebXR experiences relatively easily. With R3F in particular, turning a regular Three.js scene into an XR scene is as simple as adding <XR>
so I suspect there will be other cases of mixed experiences in the R3F ecosystem.
Here's how I see the next steps for XR support:
No-op to not break the regular desktop editor for mixed experiences scenes.
Might be easy to get it to work by simply adding R3F <XR>
. This will put the user's body at (0,0,0) and they won't be able to move their body, but moving the head in VR or headset AR, and moving the phone in mobile AR should work by default to move the camera.
This is very limited, but can already be a bit useful in some situations where the scene is built at the right scale and near (0,0,0). For instance, if you are making a real-size chessboard that's positioned at table level, you can see the whole scene only by moving your "head". That's a good start.
Note that WebXR requires HTTPS even on localhost.
For VR and headset AR:
For mobile AR, the same controls with virtual joysticks.
This is already a lot more useful to look around a bigger scene.
Being able to edit the scene end-to-end within XR.
For VR and headset AR:
<Interactive>
<Interactive>
on their gizmosFor mobile AR:
Probably not worth implementing, but I guess it would be a mix of the XR selection / transformations above + a responsive layout to show the files and context panel outside of the canvas.
By @verekia:
Userland no-op workaround:
contexts.ts
import { createContext, useContext } from 'react'
const isInReactXRContext = createContext(false)
export const IsInReactXRProvider = isInReactXRContext.Provider
export const useIsInReactXR = () => useContext(isInReactXRContext)
canvas.tsx
<Canvas>
<XR>
<IsInReactXRProvider value={true}>
<YourApp />
my-react-xr.ts
import { InteractiveProps, Interactive as XRInteractive } from '@react-three/xr'
import { useIsInReactXR } from './contexts'
export const Interactive = (props: InteractiveProps) => {
const isInReactXR = useIsInReactXR()
if (isInReactXR) {
return <XRInteractive {...props} />
}
return <>{props.children}</>
}
// Implement other react-xr components that you use
In your app, replace:
import { Interactive } from '@react-three/xr'
by:
import { Interactive } from './my-react-xr'
Can <XR />
not be mounted in user-land? I'd be happy to export the context, but I'm confused as to the need.
There's a larger story of how best to handle missing context providers. As Triplex can open leaf components (similar to how Storybook works) you can open components that need context that aren't currently available.
Storybook handles this by adding decorators.
In @verekia'a case he also has components that are used in XR and non-XR cases.
Currently I'm positioning Triplex to not need unique "story" files so this needs a bit of thinking how best to handle.
A provider config will be available in the next release.
Originally raised by @verekia.
The following code causes a
useXR must be used within an <XR /> component!
error:Versions used:
See: https://github.com/pmndrs/react-xr#xr