WebVR.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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.frameOfReferenceType ) {
  10. renderer.vr.setFrameOfReferenceType( options.frameOfReferenceType );
  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 VR';
  31. currentSession = session;
  32. }
  33. function onSessionEnded( event ) {
  34. currentSession.removeEventListener( 'end', onSessionEnded );
  35. renderer.vr.setSession( null );
  36. button.textContent = 'ENTER VR';
  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 VR';
  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. if (device) {
  50. device.requestSession( { immersive: true, exclusive: true /* DEPRECATED */ } ).then( onSessionStarted );
  51. } else {
  52. navigator.xr.requestSession( { mode: 'immersive-vr' } ).then( onSessionStarted );
  53. }
  54. } else {
  55. currentSession.end();
  56. }
  57. };
  58. if ( device ) {
  59. renderer.vr.setDevice( device );
  60. }
  61. }
  62. function showVRNotFound() {
  63. button.style.display = '';
  64. button.style.cursor = 'auto';
  65. button.style.left = 'calc(50% - 75px)';
  66. button.style.width = '150px';
  67. button.textContent = 'VR NOT FOUND';
  68. button.onmouseenter = null;
  69. button.onmouseleave = null;
  70. button.onclick = null;
  71. renderer.vr.setDevice( null );
  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 ) {
  88. var button = document.createElement( 'button' );
  89. button.style.display = 'none';
  90. stylizeElement( button );
  91. if (navigator.xr.supportsSessionMode) {
  92. navigator.xr.supportsSessionMode( 'immersive-vr' ).then( function () {
  93. showEnterXR( );
  94. });
  95. } else {
  96. navigator.xr.requestDevice().then( function ( device ) {
  97. device.supportsSession( { immersive: true, exclusive: true /* DEPRECATED */ } )
  98. .then( function () { showEnterXR( device ); } )
  99. .catch( showVRNotFound );
  100. } ).catch( showVRNotFound );
  101. }
  102. return button;
  103. } else if ( 'getVRDisplays' in navigator ) {
  104. var button = document.createElement( 'button' );
  105. button.style.display = 'none';
  106. stylizeElement( button );
  107. window.addEventListener( 'vrdisplayconnect', function ( event ) {
  108. showEnterVR( event.display );
  109. }, false );
  110. window.addEventListener( 'vrdisplaydisconnect', function ( event ) {
  111. showVRNotFound();
  112. }, false );
  113. window.addEventListener( 'vrdisplaypresentchange', function ( event ) {
  114. button.textContent = event.display.isPresenting ? 'EXIT VR' : 'ENTER VR';
  115. }, false );
  116. window.addEventListener( 'vrdisplayactivate', function ( event ) {
  117. event.display.requestPresent( [ { source: renderer.domElement } ] );
  118. }, false );
  119. navigator.getVRDisplays()
  120. .then( function ( displays ) {
  121. if ( displays.length > 0 ) {
  122. showEnterVR( displays[ 0 ] );
  123. } else {
  124. showVRNotFound();
  125. }
  126. } ).catch( showVRNotFound );
  127. return button;
  128. } else {
  129. var message = document.createElement( 'a' );
  130. message.href = 'https://webvr.info';
  131. message.innerHTML = 'WEBVR NOT SUPPORTED';
  132. message.style.left = 'calc(50% - 90px)';
  133. message.style.width = '180px';
  134. message.style.textDecoration = 'none';
  135. stylizeElement( message );
  136. return message;
  137. }
  138. },
  139. // DEPRECATED
  140. checkAvailability: function () {
  141. console.warn( 'WEBVR.checkAvailability has been deprecated.' );
  142. return new Promise( function () {} );
  143. },
  144. getMessageContainer: function () {
  145. console.warn( 'WEBVR.getMessageContainer has been deprecated.' );
  146. return document.createElement( 'div' );
  147. },
  148. getButton: function () {
  149. console.warn( 'WEBVR.getButton has been deprecated.' );
  150. return document.createElement( 'div' );
  151. },
  152. getVRDisplay: function () {
  153. console.warn( 'WEBVR.getVRDisplay has been deprecated.' );
  154. }
  155. };