pmndrs / react-xr

🤳 VR/AR with react-three-fiber
https://npmjs.com/@react-three/xr
MIT License
1.98k stars 137 forks source link

How does the camera get updated when head is moving (HMD motion tracking) #306

Open Truemedia opened 4 months ago

Truemedia commented 4 months ago

Hi, I'm currently writing some XR abstractions for vue and trying to use this library as a basis.

I have some base code working with entering VR immersive mode when clicking a button but not sure how to update the Three camera instance I have.

Looking in this codebase and with the lack of documentation it isn't obvious how that happens, I don't see anything like a setAnimationLoop for synching anything.

Could anyone divulge a bit more information on how this achieved would be a big help, thanks.

CodyJasonBennett commented 4 months ago

setAnimationLoop is what you'll need so three.js runs its internal pose and controllers sync in WebXRManager. R3F manages this since it's a bit more complicated when rendering can be interrupted due to its frameloop API.

WebXRManager also creates an internal XRCamera where head pose is tracked; the normal camera is not mutated at all. https://threejs.org/docs/#api/en/renderers/webxr/WebXRManager.getCamera

I would familiarize yourself with the WebXR API, but also the WebXR internals of three.js. They're not super scary, just these two regions and know resizing in XR isn't supported (https://github.com/mrdoob/three.js/pull/26905):

Truemedia commented 4 months ago

Thanks i had no idea R3F had XR functionality at the core that's exactly the code I was missing.

Yeh I think I might of been messing around with cameras aswell instead of letting WebXR do some of the heavy lifting.