ARButton.js 5.4 KB

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