WebVR.js 5.1 KB

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