stalgiag / p5.xr

a library that helps turn p5.js sketches into immersive experiences using WebXR
GNU Lesser General Public License v2.1
117 stars 25 forks source link

Bypassing Enter AR / Enter VR button by default #224

Closed TiborUdvari closed 2 months ago

TiborUdvari commented 2 months ago

Feature enhancement details:

While developing, repeatedly pressing the Enter AR button can be tedious. Transitioning from inline to immersive sessions can help with this in the case of VR, but it's not the ideal solution.

Once a user has granted permission on a webpage, they ideally shouldn't be asked for it again. However, due to security concerns, this is currently not possible. The WebXR navigation proposal is expected to address this in the future, with the meta quest browser being the only current implementation. It would be ideal to have a solution that works while coding on a computer.

A potential initial implementation that could be more useful is for web editors like the p5 web editor or P5LIVE, leveraging the use of iframes. Theoretically, it might be possible to request an immersive session on the parent window of the sketch and recreate the rendering context with the parent window still running the immersive session.

More details here: https://github.com/immersive-web/navigation/issues/17 https://github.com/ffd8/P5LIVE/issues/95

TiborUdvari commented 2 months ago

After a lot of fumbling around, I found when using the Immersive Web Emulator, the UA doesn't actually check if the requestSession is called from the user action, so easiest way for this use case is to check if the emulator is active and directly launch the experience.

TiborUdvari commented 2 months ago

A test I made was trying to persist the XRSession on the parent frame and reuse on subsequent calls, however the WebXR Polyfill has a lot of checks to see if the instance is actually an XRSession. On a new iframe run the polyfill is run again and the prototype is not the same, so it fails on these sort of tests. I tried messing around with the prototype chains to fool it to work, but there would probably be a lot of unintended consequences.

if (!(session instanceof XRSession)) {

https://github.com/immersive-web/webxr-polyfill/blob/7a6090614e226e2d2839f251a451f559bb2358fc/src/api/XRWebGLLayer.js#L43

I believe that the parent.frame XR Session persistance should work on real devices though, but still need to validate the approach.

TiborUdvari commented 2 months ago

OK, so I've been thinking a lot about this, and IMO the default behavior should be attempting to start a session without a user action directly.

This accounts for most use cases. A flag would need to deactivate this in case somebody wants to see the normal user flow.

I found a flag on the Meta Quest Browser to deactivate the need for a user action to launch the experience, meaning that after a hard reload, a new immersive session should be started immediately. However, this might not be fluid, and maybe hosting the session on the parent iframe would also be necessary, but this would be a second step.

TiborUdvari commented 2 months ago

This was done and documented in 47695b8

Hopefully it's easier to develop now.

I left the default behaviour with createARCanvas, createVRCanvas, so that older sketches still work the same.