VRButton.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. class VRButton {
  2. static createButton( renderer, sessionInit = {} ) {
  3. const button = document.createElement( 'button' );
  4. function showEnterVR( /*device*/ ) {
  5. let currentSession = null;
  6. async function onSessionStarted( session ) {
  7. session.addEventListener( 'end', onSessionEnded );
  8. await renderer.xr.setSession( session );
  9. button.textContent = 'EXIT VR';
  10. currentSession = session;
  11. }
  12. function onSessionEnded( /*event*/ ) {
  13. currentSession.removeEventListener( 'end', onSessionEnded );
  14. button.textContent = 'ENTER VR';
  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 = 'ENTER VR';
  23. // WebXR's requestReferenceSpace only works if the corresponding feature
  24. // was requested at session creation time. For simplicity, just ask for
  25. // the interesting ones as optional features, but be aware that the
  26. // requestReferenceSpace call will fail if it turns out to be unavailable.
  27. // ('local' is always available for immersive sessions and doesn't need to
  28. // be requested separately.)
  29. const sessionOptions = {
  30. ...sessionInit,
  31. optionalFeatures: [
  32. 'local-floor',
  33. 'bounded-floor',
  34. 'layers',
  35. ...( sessionInit.optionalFeatures || [] )
  36. ],
  37. };
  38. button.onmouseenter = function () {
  39. button.style.opacity = '1.0';
  40. };
  41. button.onmouseleave = function () {
  42. button.style.opacity = '0.5';
  43. };
  44. button.onclick = function () {
  45. if ( currentSession === null ) {
  46. navigator.xr.requestSession( 'immersive-vr', sessionOptions ).then( onSessionStarted );
  47. } else {
  48. currentSession.end();
  49. if ( navigator.xr.offerSession !== undefined ) {
  50. navigator.xr.offerSession( 'immersive-vr', sessionOptions )
  51. .then( onSessionStarted )
  52. .catch( ( err ) => {
  53. console.warn( err );
  54. } );
  55. }
  56. }
  57. };
  58. if ( navigator.xr.offerSession !== undefined ) {
  59. navigator.xr.offerSession( 'immersive-vr', sessionOptions )
  60. .then( onSessionStarted )
  61. .catch( ( err ) => {
  62. console.warn( err );
  63. } );
  64. }
  65. }
  66. function disableButton() {
  67. button.style.display = '';
  68. button.style.cursor = 'auto';
  69. button.style.left = 'calc(50% - 75px)';
  70. button.style.width = '150px';
  71. button.onmouseenter = null;
  72. button.onmouseleave = null;
  73. button.onclick = null;
  74. }
  75. function showWebXRNotFound() {
  76. disableButton();
  77. button.textContent = 'VR NOT SUPPORTED';
  78. }
  79. function showVRNotAllowed( exception ) {
  80. disableButton();
  81. console.warn( 'Exception when trying to call xr.isSessionSupported', exception );
  82. button.textContent = 'VR NOT ALLOWED';
  83. }
  84. function stylizeElement( element ) {
  85. element.style.position = 'absolute';
  86. element.style.bottom = '20px';
  87. element.style.padding = '12px 6px';
  88. element.style.border = '1px solid #fff';
  89. element.style.borderRadius = '4px';
  90. element.style.background = 'rgba(0,0,0,0.1)';
  91. element.style.color = '#fff';
  92. element.style.font = 'normal 13px sans-serif';
  93. element.style.textAlign = 'center';
  94. element.style.opacity = '0.5';
  95. element.style.outline = 'none';
  96. element.style.zIndex = '999';
  97. }
  98. if ( 'xr' in navigator ) {
  99. button.id = 'VRButton';
  100. button.style.display = 'none';
  101. stylizeElement( button );
  102. navigator.xr.isSessionSupported( 'immersive-vr' ).then( function ( supported ) {
  103. supported ? showEnterVR() : showWebXRNotFound();
  104. if ( supported && VRButton.xrSessionIsGranted ) {
  105. button.click();
  106. }
  107. } ).catch( showVRNotAllowed );
  108. return button;
  109. } else {
  110. const message = document.createElement( 'a' );
  111. if ( window.isSecureContext === false ) {
  112. message.href = document.location.href.replace( /^http:/, 'https:' );
  113. message.innerHTML = 'WEBXR NEEDS HTTPS'; // TODO Improve message
  114. } else {
  115. message.href = 'https://immersiveweb.dev/';
  116. message.innerHTML = 'WEBXR NOT AVAILABLE';
  117. }
  118. message.style.left = 'calc(50% - 90px)';
  119. message.style.width = '180px';
  120. message.style.textDecoration = 'none';
  121. stylizeElement( message );
  122. return message;
  123. }
  124. }
  125. static registerSessionGrantedListener() {
  126. if ( typeof navigator !== 'undefined' && 'xr' in navigator ) {
  127. // WebXRViewer (based on Firefox) has a bug where addEventListener
  128. // throws a silent exception and aborts execution entirely.
  129. if ( /WebXRViewer\//i.test( navigator.userAgent ) ) return;
  130. navigator.xr.addEventListener( 'sessiongranted', () => {
  131. VRButton.xrSessionIsGranted = true;
  132. } );
  133. }
  134. }
  135. }
  136. VRButton.xrSessionIsGranted = false;
  137. VRButton.registerSessionGrantedListener();
  138. export { VRButton };