VRButton.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * @author mrdoob / http://mrdoob.com
  3. * @author Mugen87 / https://github.com/Mugen87
  4. */
  5. var VRButton = {
  6. createButton: function ( renderer, options ) {
  7. if ( options && options.referenceSpaceType ) {
  8. renderer.vr.setReferenceSpaceType( options.referenceSpaceType );
  9. }
  10. function showEnterVR( device ) {
  11. button.style.display = '';
  12. button.style.cursor = 'pointer';
  13. button.style.left = 'calc(50% - 50px)';
  14. button.style.width = '100px';
  15. button.textContent = 'ENTER_VR';
  16. button.onmouseenter = function () {
  17. button.style.opacity = '1.0';
  18. };
  19. button.onmouseleave = function () {
  20. button.style.opacity = '0.5';
  21. };
  22. button.onclick = function () {
  23. device.isPresenting ? device.exitPresent() : device.requestPresent( [ { source: renderer.domElement } ] );
  24. };
  25. renderer.vr.setDevice( device );
  26. }
  27. function showEnterXR( /*device*/ ) {
  28. var currentSession = null;
  29. function onSessionStarted( session ) {
  30. session.addEventListener( 'end', onSessionEnded );
  31. renderer.vr.setSession( session );
  32. button.textContent = 'EXIT VR';
  33. currentSession = session;
  34. }
  35. function onSessionEnded( /*event*/ ) {
  36. currentSession.removeEventListener( 'end', onSessionEnded );
  37. renderer.vr.setSession( null );
  38. button.textContent = 'ENTER VR';
  39. currentSession = null;
  40. }
  41. //
  42. button.style.display = '';
  43. button.style.cursor = 'pointer';
  44. button.style.left = 'calc(50% - 50px)';
  45. button.style.width = '100px';
  46. button.textContent = 'ENTER VR';
  47. button.onmouseenter = function () {
  48. button.style.opacity = '1.0';
  49. };
  50. button.onmouseleave = function () {
  51. button.style.opacity = '0.5';
  52. };
  53. button.onclick = function () {
  54. if ( currentSession === null ) {
  55. // WebXR's requestReferenceSpace only works if the corresponding feature
  56. // was requested at session creation time. For simplicity, just ask for
  57. // the interesting ones as optional features, but be aware that the
  58. // requestReferenceSpace call will fail if it turns out to be unavailable.
  59. // ('local' is always available for immersive sessions and doesn't need to
  60. // be requested separately.)
  61. var sessionInit = { optionalFeatures: [ 'local-floor', 'bounded-floor' ] };
  62. navigator.xr.requestSession( 'immersive-vr', sessionInit ).then( onSessionStarted );
  63. } else {
  64. currentSession.end();
  65. }
  66. };
  67. }
  68. function disableButton() {
  69. button.style.display = '';
  70. button.style.cursor = 'auto';
  71. button.style.left = 'calc(50% - 75px)';
  72. button.style.width = '150px';
  73. button.onmouseenter = null;
  74. button.onmouseleave = null;
  75. button.onclick = null;
  76. }
  77. function showVRNotFound() {
  78. disableButton();
  79. button.textContent = 'VR NOT FOUND';
  80. renderer.vr.setDevice( null );
  81. }
  82. function showXRNotFound() {
  83. disableButton();
  84. button.textContent = 'VR NOT FOUND';
  85. }
  86. function stylizeElement( element ) {
  87. element.style.position = 'absolute';
  88. element.style.bottom = '20px';
  89. element.style.padding = '12px 6px';
  90. element.style.border = '1px solid #fff';
  91. element.style.borderRadius = '4px';
  92. element.style.background = 'rgba(0,0,0,0.1)';
  93. element.style.color = '#fff';
  94. element.style.font = 'normal 13px sans-serif';
  95. element.style.textAlign = 'center';
  96. element.style.opacity = '0.5';
  97. element.style.outline = 'none';
  98. element.style.zIndex = '999';
  99. }
  100. if ( 'xr' in navigator ) {
  101. var button = document.createElement( 'button' );
  102. button.style.display = 'none';
  103. stylizeElement( button );
  104. navigator.xr.isSessionSupported( 'immersive-vr' ).then( function ( supported ) {
  105. if ( supported ) {
  106. showEnterXR();
  107. } else {
  108. showXRNotFound();
  109. }
  110. } );
  111. return button;
  112. } else if ( 'getVRDisplays' in navigator ) {
  113. var button = document.createElement( 'button' );
  114. button.style.display = 'none';
  115. stylizeElement( button );
  116. window.addEventListener( 'vrdisplayconnect', function ( event ) {
  117. showEnterVR( event.display );
  118. }, false );
  119. window.addEventListener( 'vrdisplaydisconnect', function ( /*event*/ ) {
  120. showVRNotFound();
  121. }, false );
  122. window.addEventListener( 'vrdisplaypresentchange', function ( event ) {
  123. button.textContent = event.display.isPresenting ? 'EXIT_VR' : 'ENTER_VR';
  124. }, false );
  125. window.addEventListener( 'vrdisplayactivate', function ( event ) {
  126. event.display.requestPresent( [ { source: renderer.domElement } ] );
  127. }, false );
  128. navigator.getVRDisplays()
  129. .then( function ( displays ) {
  130. if ( displays.length > 0 ) {
  131. showEnterVR( displays[ 0 ] );
  132. } else {
  133. showVRNotFound();
  134. }
  135. } ).catch( showVRNotFound );
  136. return button;
  137. } else {
  138. var message = document.createElement( 'a' );
  139. message.href = 'https://immersive-web.github.io/webxr/';
  140. message.innerHTML = 'WEBXR NOT SUPPORTED';
  141. message.style.left = 'calc(50% - 90px)';
  142. message.style.width = '180px';
  143. message.style.textDecoration = 'none';
  144. stylizeElement( message );
  145. return message;
  146. }
  147. }
  148. };
  149. export { VRButton };