XRButton.js 4.8 KB

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