diff --git a/examples/files.js b/examples/files.js index a9e974d04eed1e5e8ff4807b3d1566d4440f93ab..bc01e85ec2831b1b8631658b065698d6cbf7a898 100644 --- a/examples/files.js +++ b/examples/files.js @@ -323,6 +323,7 @@ var files = { "webaudio_visualizer" ], "webxr": [ + "webxr_ar_paint", "webxr_vr_ballshooter", "webxr_vr_cubes", "webxr_vr_dragging", diff --git a/examples/jsm/webxr/ARButton.d.ts b/examples/jsm/webxr/ARButton.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..a826344b04d0e6f8ce3914900c63902b0a1e53af --- /dev/null +++ b/examples/jsm/webxr/ARButton.d.ts @@ -0,0 +1,7 @@ +import { + WebGLRenderer +} from '../../../src/Three'; + +export namespace ARButton { + export function createButton( renderer: WebGLRenderer ): HTMLElement; +} diff --git a/examples/jsm/webxr/ARButton.js b/examples/jsm/webxr/ARButton.js new file mode 100644 index 0000000000000000000000000000000000000000..6fe226aabb4ed96d63cae4b92af4fd2ac8f3739b --- /dev/null +++ b/examples/jsm/webxr/ARButton.js @@ -0,0 +1,173 @@ +/** + * @author mrdoob / http://mrdoob.com + * @author Mugen87 / https://github.com/Mugen87 + */ + +var ARButton = { + + createButton: function ( renderer ) { + + function showEnterXR( /*device*/ ) { + + var currentSession = null; + + function onSessionStarted( session ) { + + session.addEventListener( 'end', onSessionEnded ); + + /* + session.updateWorldTrackingState( { + 'planeDetectionState': { 'enabled': true } + } ); + */ + + renderer.vr.setReferenceSpaceType( 'local' ); + renderer.vr.setSession( session ); + button.textContent = 'STOP AR'; + + currentSession = session; + + } + + function onSessionEnded( /*event*/ ) { + + currentSession.removeEventListener( 'end', onSessionEnded ); + + renderer.vr.setSession( null ); + button.textContent = 'START AR'; + + currentSession = null; + + } + + // + + button.style.display = ''; + + button.style.cursor = 'pointer'; + button.style.left = 'calc(50% - 50px)'; + button.style.width = '100px'; + + button.textContent = 'START AR'; + + button.onmouseenter = function () { + + button.style.opacity = '1.0'; + + }; + + button.onmouseleave = function () { + + button.style.opacity = '0.5'; + + }; + + button.onclick = function () { + + if ( currentSession === null ) { + + navigator.xr.requestSession( 'immersive-ar' ).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 showXRNotFound() { + + disableButton(); + + button.textContent = 'XR NOT FOUND'; + + } + + 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 ) { + + var button = document.createElement( 'button' ); + button.style.display = 'none'; + + stylizeElement( button ); + + navigator.xr.isSessionSupported( 'immersive-ar' ).then( function ( supported ) { + + if ( supported ) { + + showEnterXR(); + + } else { + + showXRNotFound(); + + } + + } ); + + return button; + + } else { + + var message = document.createElement( 'a' ); + message.href = 'https://immersive-web.github.io/webxr/'; + + if ( window.isSecureContext === false ) { + + message.innerHTML = 'WEBXR NEEDS HTTPS'; // TODO Improve message + + } else { + + message.innerHTML = 'WEBXR NOT AVAILABLE'; + + } + + message.style.left = 'calc(50% - 90px)'; + message.style.width = '180px'; + message.style.textDecoration = 'none'; + + stylizeElement( message ); + + return message; + + } + + } + +}; + +export { ARButton }; diff --git a/examples/webxr_ar_paint.html b/examples/webxr_ar_paint.html new file mode 100644 index 0000000000000000000000000000000000000000..dfedf156ea068f7cd33b042333a8bc7fc688c465 --- /dev/null +++ b/examples/webxr_ar_paint.html @@ -0,0 +1,168 @@ + + + + three.js ar - paint + + + + + + + + +
+ three.js ar - paint +
+ + + +