Skip to content

Commit

Permalink
WebXR: Added XRButton (#25781)
Browse files Browse the repository at this point in the history
* Added XRButton.js

* Examples: Updated relevant examples using XRButton.

* WebXRManager: Added setSessionMode().

* WebGLRenderer: Force clear when sessionMode is 'immersive-ar'.

* Clean up.

* WebXRManager: Force alpha: true in layerInit.

* WebGLRenderer: Set state.buffers.color before calling clear() for 'immersive-ar'.

* Fix typo

* WebGLBackground: Handle WebXR sessions environmentBlendMode.

* WebXRManager: Removed setSessionMode().

* XRButton: Revert extra removal.
  • Loading branch information
mrdoob committed Apr 11, 2023
1 parent a5aa3b7 commit de2fbc6
Show file tree
Hide file tree
Showing 18 changed files with 244 additions and 436 deletions.
14 changes: 6 additions & 8 deletions examples/files.json
Expand Up @@ -357,14 +357,9 @@
],
"webxr": [
"webxr_ar_cones",
"webxr_ar_dragging",
"webxr_ar_hittest",
"webxr_ar_lighting",
"webxr_ar_paint",
"webxr_ar_plane_detection",
"webxr_vr_ballshooter",
"webxr_vr_cubes",
"webxr_vr_dragging",
"webxr_vr_handinput",
"webxr_vr_handinput_cubes",
"webxr_vr_handinput_profiles",
Expand All @@ -375,12 +370,15 @@
"webxr_vr_layers",
"webxr_vr_panorama",
"webxr_vr_panorama_depth",
"webxr_vr_paint",
"webxr_vr_rollercoaster",
"webxr_vr_sandbox",
"webxr_vr_sculpt",
"webxr_vr_teleport",
"webxr_vr_video"
"webxr_vr_video",
"webxr_xr_ballshooter",
"webxr_xr_cubes",
"webxr_xr_dragging",
"webxr_xr_paint",
"webxr_xr_sculpt"
],
"games": [
"games_fps"
Expand Down
198 changes: 198 additions & 0 deletions examples/jsm/webxr/XRButton.js
@@ -0,0 +1,198 @@
class XRButton {

static createButton( renderer ) {

const button = document.createElement( 'button' );

function showStartXR( mode ) {

let currentSession = null;

async function onSessionStarted( session ) {

session.addEventListener( 'end', onSessionEnded );

await renderer.xr.setSession( session );

button.textContent = 'STOP XR';

currentSession = session;

}

function onSessionEnded( /*event*/ ) {

currentSession.removeEventListener( 'end', onSessionEnded );

button.textContent = 'START XR';

currentSession = null;

}

//

button.style.display = '';

button.style.cursor = 'pointer';
button.style.left = 'calc(50% - 50px)';
button.style.width = '100px';

button.textContent = 'START XR';

button.onmouseenter = function () {

button.style.opacity = '1.0';

};

button.onmouseleave = function () {

button.style.opacity = '0.5';

};

button.onclick = function () {

if ( currentSession === null ) {

const sessionInit = {
optionalFeatures: [
'local-floor',
'bounded-floor',
'hand-tracking',
'layers'
]
};

navigator.xr.requestSession( mode, sessionInit )
.then( onSessionStarted );

} else {

currentSession.end();

}

};

}

function disableButton() {

button.style.display = '';

button.style.cursor = 'auto';
button.style.left = 'calc(50% - 75px)';
button.style.width = '150px';

button.onmouseenter = null;
button.onmouseleave = null;

button.onclick = null;

}

function showXRNotSupported() {

disableButton();

button.textContent = 'XR NOT SUPPORTED';

}

function showXRNotAllowed( exception ) {

disableButton();

console.warn( 'Exception when trying to call xr.isSessionSupported', exception );

button.textContent = 'XR NOT ALLOWED';

}

function stylizeElement( element ) {

element.style.position = 'absolute';
element.style.bottom = '20px';
element.style.padding = '12px 6px';
element.style.border = '1px solid #fff';
element.style.borderRadius = '4px';
element.style.background = 'rgba(0,0,0,0.1)';
element.style.color = '#fff';
element.style.font = 'normal 13px sans-serif';
element.style.textAlign = 'center';
element.style.opacity = '0.5';
element.style.outline = 'none';
element.style.zIndex = '999';

}

if ( 'xr' in navigator ) {

button.id = 'XRButton';
button.style.display = 'none';

stylizeElement( button );

navigator.xr.isSessionSupported( 'immersive-ar' )
.then( function ( supported ) {

if ( supported ) {

showStartXR( 'immersive-ar' );

} else {

navigator.xr.isSessionSupported( 'immersive-vr' )
.then( function ( supported ) {

if ( supported ) {

showStartXR( 'immersive-vr' );

} else {

showXRNotSupported();

}

} ).catch( showXRNotAllowed );

}

} ).catch( showXRNotAllowed );

return button;

} else {

const message = document.createElement( 'a' );

if ( window.isSecureContext === false ) {

message.href = document.location.href.replace( /^http:/, 'https:' );
message.innerHTML = 'WEBXR NEEDS HTTPS'; // TODO Improve message

} else {

message.href = 'https://immersiveweb.dev/';
message.innerHTML = 'WEBXR NOT AVAILABLE';

}

message.style.left = 'calc(50% - 90px)';
message.style.width = '180px';
message.style.textDecoration = 'none';

stylizeElement( message );

return message;

}

}

}

export { XRButton };
Binary file removed examples/screenshots/webxr_ar_dragging.jpg
Binary file not shown.
Binary file removed examples/screenshots/webxr_ar_paint.jpg
Binary file not shown.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes

0 comments on commit de2fbc6

Please sign in to comment.