XRButton.js 4.0 KB

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