Skip to content

Commit

Permalink
feat: avoiding enter xr btn Merge PR #236 from TiborUdvari/feat/user-…
Browse files Browse the repository at this point in the history
…actionless-init
  • Loading branch information
TiborUdvari committed Sep 16, 2024
2 parents abae413 + 1bd0d66 commit 47695b8
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- __QUICK START__
- [Device and Browser Support](device-support.html)
- [Immersive Web Emulator](quick-start/emulator.md)
- [Tips](quick-start/tips.md)
- [Examples](quick-start/examples.md)
***
- __REFERENCE__
Expand Down
Binary file added docs/assets/docs/enter-ar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/docs/webxr-user-activation.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions docs/quick-start/tips.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Avoiding the Enter XR Button
![Enter XR Button](../assets/docs/enter-ar.png)

Usually, you must press a button before starting an immersive AR or VR session. This is a security measure to prevent websites from overwhelming users. However, pressing this button each time you want to test a change in your code can be tedious.

By default, p5.xr `createCanvas` will attempt to launch the session without requiring the button press. If you use the [Immersive Web Emulator](quick-start/emulator.md) or have turned off the webxr-enforce-user-activation flag in your XR device browser, this will work directly. Otherwise, you must click the button to launch the interactive session.

```javascript
function setup() {
createCanvas(windowWidth, windowHeight, AR);
```

If you want to see the button while coding, you can launch the session like below.

```javascript
function setup() {
createCanvas(windowWidth, windowHeight, AR_BUTTON);
```

To allow sketches to launch directly on your XR device browser, navigate to `chrome://flags/#webxr-enforce-user-activation` and disable that flag. Note that this option may not be available in all browsers.

![Meta Quest Browser User Activation Flag](../assets/docs/webxr-user-activation.jpg)
29 changes: 29 additions & 0 deletions src/p5xr/core/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
export const LEFT = 'left';
export const RIGHT = 'right';

/**
* One of the four possible values of a an XR session (AR, VR, AR_BUTTON or VR_BUTTON)
* AR and VR attempt to launch the session without user action
* @property {String} AR
* @final
*/
export const AR = 'AR';

/**
* One of the four possible values of a an XR session (AR, VR, AR_BUTTON or VR_BUTTON)
* AR and VR attempt to launch the session without user action
* @property {String} VR
* @final
*/
export const VR = 'VR';

/**
* One of the four possible values of a an XR session (AR, VR, AR_BUTTON or VR_BUTTON)
* AR_BUTTON and VR_BUTTON do not attempt to launch the session without user action
* @property {String} AR_BUTTON
* @final
*/
export const AR_BUTTON = 'AR_BUTTON';

/**
* One of the four possible values of a an XR session (AR, VR, AR_BUTTON or VR_BUTTON)
* AR_BUTTON and VR_BUTTON do not attempt to launch the session without user action
* @property {String} VR_BUTTON
* @final
*/
export const VR_BUTTON = 'VR_BUTTON';
14 changes: 12 additions & 2 deletions src/p5xr/core/p5overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,22 @@ import compareVersions from '../utilities/versionComparator';
const originalCreateCanvas = p5.prototype.createCanvas;
p5.prototype.createCanvas = function (w, h, renderer, canvas) {
let effectiveRenderer = renderer;
if (renderer === constants.AR || renderer === constants.VR) {
const isXRNoUserAction =
renderer === constants.AR || renderer === constants.VR;
const isXRUserAction =
renderer === constants.AR_BUTTON || renderer === constants.VR_BUTTON;
if (isXRUserAction || isXRNoUserAction) {
noLoop();
p5xr.instance = renderer === constants.AR ? new p5ar() : new p5vr();
p5xr.instance =
renderer === constants.AR || renderer === constants.AR_BUTTON
? new p5ar()
: new p5vr();
effectiveRenderer = WEBGL;
w = windowWidth;
h = windowHeight;
if (isXRNoUserAction) {
p5xr.instance.startXRWithoutUserAction();
}
}
originalCreateCanvas.call(
this,
Expand Down
9 changes: 9 additions & 0 deletions src/p5xr/core/p5xr.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,15 @@ export default class p5xr {
}
}

/**
* Attempts to start an interactive session outside of the normal flow
* Useful for development with the Immersive Web Emulator or when a special flag is activated to bypass the need for a user action
*/
startXRWithoutUserAction() {
this.hasImmersive = true;
this.__onXRButtonClicked();
}

/**
* This is where the actual p5 canvas is first created, and
* the GL rendering context is accessed by p5vr.
Expand Down

0 comments on commit 47695b8

Please sign in to comment.