XRButton.js 3.6 KB

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