VRButton.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. class VRButton {
  2. static createButton( renderer ) {
  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 sessionInit = { optionalFeatures: [ 'local-floor', 'bounded-floor', 'hand-tracking', 'layers' ] };
  30. button.onmouseenter = function () {
  31. button.style.opacity = '1.0';
  32. };
  33. button.onmouseleave = function () {
  34. button.style.opacity = '0.5';
  35. };
  36. button.onclick = function () {
  37. if ( currentSession === null ) {
  38. navigator.xr.requestSession( 'immersive-vr', sessionInit ).then( onSessionStarted );
  39. } else {
  40. currentSession.end();
  41. if ( navigator.xr.offerSession !== undefined ) {
  42. navigator.xr.offerSession( 'immersive-vr', sessionInit )
  43. .then( onSessionStarted )
  44. .catch( ( err ) => {
  45. console.warn( err );
  46. } );
  47. }
  48. }
  49. };
  50. if ( navigator.xr.offerSession !== undefined ) {
  51. navigator.xr.offerSession( 'immersive-vr', sessionInit )
  52. .then( onSessionStarted )
  53. .catch( ( err ) => {
  54. console.warn( err );
  55. } );
  56. }
  57. }
  58. function disableButton() {
  59. button.style.display = '';
  60. button.style.cursor = 'auto';
  61. button.style.left = 'calc(50% - 75px)';
  62. button.style.width = '150px';
  63. button.onmouseenter = null;
  64. button.onmouseleave = null;
  65. button.onclick = null;
  66. }
  67. function showWebXRNotFound() {
  68. disableButton();
  69. button.textContent = 'VR NOT SUPPORTED';
  70. }
  71. function showVRNotAllowed( exception ) {
  72. disableButton();
  73. console.warn( 'Exception when trying to call xr.isSessionSupported', exception );
  74. button.textContent = 'VR NOT ALLOWED';
  75. }
  76. function stylizeElement( element ) {
  77. element.style.position = 'absolute';
  78. element.style.bottom = '20px';
  79. element.style.padding = '12px 6px';
  80. element.style.border = '1px solid #fff';
  81. element.style.borderRadius = '4px';
  82. element.style.background = 'rgba(0,0,0,0.1)';
  83. element.style.color = '#fff';
  84. element.style.font = 'normal 13px sans-serif';
  85. element.style.textAlign = 'center';
  86. element.style.opacity = '0.5';
  87. element.style.outline = 'none';
  88. element.style.zIndex = '999';
  89. }
  90. if ( 'xr' in navigator ) {
  91. button.id = 'VRButton';
  92. button.style.display = 'none';
  93. stylizeElement( button );
  94. navigator.xr.isSessionSupported( 'immersive-vr' ).then( function ( supported ) {
  95. supported ? showEnterVR() : showWebXRNotFound();
  96. if ( supported && VRButton.xrSessionIsGranted ) {
  97. button.click();
  98. }
  99. } ).catch( showVRNotAllowed );
  100. return button;
  101. } else {
  102. const message = document.createElement( 'a' );
  103. if ( window.isSecureContext === false ) {
  104. message.href = document.location.href.replace( /^http:/, 'https:' );
  105. message.innerHTML = 'WEBXR NEEDS HTTPS'; // TODO Improve message
  106. } else {
  107. message.href = 'https://immersiveweb.dev/';
  108. message.innerHTML = 'WEBXR NOT AVAILABLE';
  109. }
  110. message.style.left = 'calc(50% - 90px)';
  111. message.style.width = '180px';
  112. message.style.textDecoration = 'none';
  113. stylizeElement( message );
  114. return message;
  115. }
  116. }
  117. static registerSessionGrantedListener() {
  118. if ( typeof navigator !== 'undefined' && 'xr' in navigator ) {
  119. // WebXRViewer (based on Firefox) has a bug where addEventListener
  120. // throws a silent exception and aborts execution entirely.
  121. if ( /WebXRViewer\//i.test( navigator.userAgent ) ) return;
  122. navigator.xr.addEventListener( 'sessiongranted', () => {
  123. VRButton.xrSessionIsGranted = true;
  124. } );
  125. }
  126. }
  127. }
  128. VRButton.xrSessionIsGranted = false;
  129. VRButton.registerSessionGrantedListener();
  130. export { VRButton };