WebVR.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. var 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 () { button.style.opacity = '1.0'; };
  19. button.onmouseleave = function () { button.style.opacity = '0.5'; };
  20. button.onclick = function () {
  21. device.isPresenting ? device.exitPresent() : device.requestPresent( [ { source: renderer.domElement } ] );
  22. };
  23. renderer.vr.setDevice( device );
  24. }
  25. function showEnterXR( device ) {
  26. var currentSession = null;
  27. function onSessionStarted( session ) {
  28. session.addEventListener( 'end', onSessionEnded );
  29. renderer.vr.setSession( session );
  30. button.textContent = 'EXIT XR';
  31. currentSession = session;
  32. }
  33. function onSessionEnded( event ) {
  34. currentSession.removeEventListener( 'end', onSessionEnded );
  35. renderer.vr.setSession( null );
  36. button.textContent = 'ENTER XR';
  37. currentSession = null;
  38. }
  39. //
  40. button.style.display = '';
  41. button.style.cursor = 'pointer';
  42. button.style.left = 'calc(50% - 50px)';
  43. button.style.width = '100px';
  44. button.textContent = 'ENTER XR';
  45. button.onmouseenter = function () { button.style.opacity = '1.0'; };
  46. button.onmouseleave = function () { button.style.opacity = '0.5'; };
  47. button.onclick = function () {
  48. if ( currentSession === null ) {
  49. navigator.xr.requestSession( 'immersive-vr' ).then( onSessionStarted );
  50. } else {
  51. currentSession.end();
  52. }
  53. };
  54. }
  55. function disableButton() {
  56. button.style.display = '';
  57. button.style.cursor = 'auto';
  58. button.style.left = 'calc(50% - 75px)';
  59. button.style.width = '150px';
  60. button.onmouseenter = null;
  61. button.onmouseleave = null;
  62. button.onclick = null;
  63. }
  64. function showVRNotFound() {
  65. disableButton();
  66. button.textContent = 'VR NOT FOUND';
  67. renderer.vr.setDevice( null );
  68. }
  69. function showXRNotFound() {
  70. disableButton();
  71. button.textContent = 'XR NOT FOUND';
  72. }
  73. function stylizeElement( element ) {
  74. element.style.position = 'absolute';
  75. element.style.bottom = '20px';
  76. element.style.padding = '12px 6px';
  77. element.style.border = '1px solid #fff';
  78. element.style.borderRadius = '4px';
  79. element.style.background = 'rgba(0,0,0,0.1)';
  80. element.style.color = '#fff';
  81. element.style.font = 'normal 13px sans-serif';
  82. element.style.textAlign = 'center';
  83. element.style.opacity = '0.5';
  84. element.style.outline = 'none';
  85. element.style.zIndex = '999';
  86. }
  87. if ( 'xr' in navigator && 'supportsSession' in navigator.xr ) {
  88. var button = document.createElement( 'button' );
  89. button.style.display = 'none';
  90. stylizeElement( button );
  91. navigator.xr.supportsSession( 'immersive-vr' ).then( showEnterXR ).catch( showXRNotFound );
  92. return button;
  93. } else if ( 'getVRDisplays' in navigator ) {
  94. var button = document.createElement( 'button' );
  95. button.style.display = 'none';
  96. stylizeElement( button );
  97. window.addEventListener( 'vrdisplayconnect', function ( event ) {
  98. showEnterVR( event.display );
  99. }, false );
  100. window.addEventListener( 'vrdisplaydisconnect', function ( event ) {
  101. showVRNotFound();
  102. }, false );
  103. window.addEventListener( 'vrdisplaypresentchange', function ( event ) {
  104. button.textContent = event.display.isPresenting ? 'EXIT VR' : 'ENTER VR';
  105. }, false );
  106. window.addEventListener( 'vrdisplayactivate', function ( event ) {
  107. event.display.requestPresent( [ { source: renderer.domElement } ] );
  108. }, false );
  109. navigator.getVRDisplays()
  110. .then( function ( displays ) {
  111. if ( displays.length > 0 ) {
  112. showEnterVR( displays[ 0 ] );
  113. } else {
  114. showVRNotFound();
  115. }
  116. } ).catch( showVRNotFound );
  117. return button;
  118. } else {
  119. var message = document.createElement( 'a' );
  120. message.href = 'https://webvr.info';
  121. message.innerHTML = 'WEBVR NOT SUPPORTED';
  122. message.style.left = 'calc(50% - 90px)';
  123. message.style.width = '180px';
  124. message.style.textDecoration = 'none';
  125. stylizeElement( message );
  126. return message;
  127. }
  128. },
  129. // DEPRECATED
  130. checkAvailability: function () {
  131. console.warn( 'WEBVR.checkAvailability has been deprecated.' );
  132. return new Promise( function () {} );
  133. },
  134. getMessageContainer: function () {
  135. console.warn( 'WEBVR.getMessageContainer has been deprecated.' );
  136. return document.createElement( 'div' );
  137. },
  138. getButton: function () {
  139. console.warn( 'WEBVR.getButton has been deprecated.' );
  140. return document.createElement( 'div' );
  141. },
  142. getVRDisplay: function () {
  143. console.warn( 'WEBVR.getVRDisplay has been deprecated.' );
  144. }
  145. };