VRButton.js 4.3 KB

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