WebVR.js 5.0 KB

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