XRButton.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**
  2. * A utility class for creating a button that allows to initiate
  3. * immersive XR sessions based on WebXR. The button can be created
  4. * with a factory method and then appended ot the website's DOM.
  5. *
  6. * ```js
  7. * document.body.appendChild( XRButton.createButton( renderer ) );
  8. * ```
  9. *
  10. * Compared to {@link ARButton} and {@link VRButton}, this class will
  11. * try to offer an immersive AR session first. If the device does not
  12. * support this type of session, it uses an immersive VR session.
  13. *
  14. * @hideconstructor
  15. * @three_import import { XRButton } from 'three/addons/webxr/XRButton.js';
  16. */
  17. class XRButton {
  18. /**
  19. * Constructs a new XR button.
  20. *
  21. * @param {WebGLRenderer|WebGPURenderer} renderer - The renderer.
  22. * @param {XRSessionInit} [sessionInit] - The a configuration object for the AR session.
  23. * @return {HTMLElement} The button or an error message if WebXR isn't supported.
  24. */
  25. static createButton( renderer, sessionInit = {} ) {
  26. const button = document.createElement( 'button' );
  27. function showStartXR( mode ) {
  28. let currentSession = null;
  29. async function onSessionStarted( session ) {
  30. session.addEventListener( 'end', onSessionEnded );
  31. await renderer.xr.setSession( session );
  32. button.textContent = 'STOP XR';
  33. currentSession = session;
  34. }
  35. function onSessionEnded( /*event*/ ) {
  36. currentSession.removeEventListener( 'end', onSessionEnded );
  37. button.textContent = 'START XR';
  38. currentSession = null;
  39. }
  40. //
  41. button.style.display = '';
  42. button.style.cursor = 'pointer';
  43. button.style.left = 'calc(50% - 50px)';
  44. button.style.width = '100px';
  45. button.textContent = 'START XR';
  46. const sessionOptions = {
  47. ...sessionInit,
  48. optionalFeatures: [
  49. 'local-floor',
  50. 'bounded-floor',
  51. 'layers',
  52. ...( sessionInit.optionalFeatures || [] )
  53. ],
  54. };
  55. button.onmouseenter = function () {
  56. button.style.opacity = '1.0';
  57. };
  58. button.onmouseleave = function () {
  59. button.style.opacity = '0.5';
  60. };
  61. button.onclick = function () {
  62. if ( currentSession === null ) {
  63. navigator.xr.requestSession( mode, sessionOptions )
  64. .then( onSessionStarted );
  65. } else {
  66. currentSession.end();
  67. if ( navigator.xr.offerSession !== undefined ) {
  68. navigator.xr.offerSession( mode, sessionOptions )
  69. .then( onSessionStarted )
  70. .catch( ( err ) => {
  71. console.warn( err );
  72. } );
  73. }
  74. }
  75. };
  76. if ( navigator.xr.offerSession !== undefined ) {
  77. navigator.xr.offerSession( mode, sessionOptions )
  78. .then( onSessionStarted )
  79. .catch( ( err ) => {
  80. console.warn( err );
  81. } );
  82. }
  83. }
  84. function disableButton() {
  85. button.style.display = '';
  86. button.style.cursor = 'auto';
  87. button.style.left = 'calc(50% - 75px)';
  88. button.style.width = '150px';
  89. button.onmouseenter = null;
  90. button.onmouseleave = null;
  91. button.onclick = null;
  92. }
  93. function showXRNotSupported() {
  94. disableButton();
  95. button.textContent = 'XR NOT SUPPORTED';
  96. }
  97. function showXRNotAllowed( exception ) {
  98. disableButton();
  99. console.warn( 'Exception when trying to call xr.isSessionSupported', exception );
  100. button.textContent = 'XR NOT ALLOWED';
  101. }
  102. function stylizeElement( element ) {
  103. element.style.position = 'absolute';
  104. element.style.bottom = '20px';
  105. element.style.padding = '12px 6px';
  106. element.style.border = '1px solid #fff';
  107. element.style.borderRadius = '4px';
  108. element.style.background = 'rgba(0,0,0,0.1)';
  109. element.style.color = '#fff';
  110. element.style.font = 'normal 13px sans-serif';
  111. element.style.textAlign = 'center';
  112. element.style.opacity = '0.5';
  113. element.style.outline = 'none';
  114. element.style.zIndex = '999';
  115. }
  116. if ( 'xr' in navigator ) {
  117. button.id = 'XRButton';
  118. button.style.display = 'none';
  119. stylizeElement( button );
  120. navigator.xr.isSessionSupported( 'immersive-ar' )
  121. .then( function ( supported ) {
  122. if ( supported ) {
  123. showStartXR( 'immersive-ar' );
  124. } else {
  125. navigator.xr.isSessionSupported( 'immersive-vr' )
  126. .then( function ( supported ) {
  127. if ( supported ) {
  128. showStartXR( 'immersive-vr' );
  129. } else {
  130. showXRNotSupported();
  131. }
  132. } ).catch( showXRNotAllowed );
  133. }
  134. } ).catch( showXRNotAllowed );
  135. return button;
  136. } else {
  137. const message = document.createElement( 'a' );
  138. if ( window.isSecureContext === false ) {
  139. message.href = document.location.href.replace( /^http:/, 'https:' );
  140. message.innerHTML = 'WEBXR NEEDS HTTPS'; // TODO Improve message
  141. } else {
  142. message.href = 'https://immersiveweb.dev/';
  143. message.innerHTML = 'WEBXR NOT AVAILABLE';
  144. }
  145. message.style.left = 'calc(50% - 90px)';
  146. message.style.width = '180px';
  147. message.style.textDecoration = 'none';
  148. stylizeElement( message );
  149. return message;
  150. }
  151. }
  152. }
  153. export { XRButton };