WebVR.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 showVRNotFound() {
  56. button.style.display = '';
  57. button.style.cursor = 'auto';
  58. button.style.left = 'calc(50% - 75px)';
  59. button.style.width = '150px';
  60. button.textContent = 'VR NOT FOUND';
  61. button.onmouseenter = null;
  62. button.onmouseleave = null;
  63. button.onclick = null;
  64. renderer.vr.setDevice( null );
  65. }
  66. function stylizeElement( element ) {
  67. element.style.position = 'absolute';
  68. element.style.bottom = '20px';
  69. element.style.padding = '12px 6px';
  70. element.style.border = '1px solid #fff';
  71. element.style.borderRadius = '4px';
  72. element.style.background = 'rgba(0,0,0,0.1)';
  73. element.style.color = '#fff';
  74. element.style.font = 'normal 13px sans-serif';
  75. element.style.textAlign = 'center';
  76. element.style.opacity = '0.5';
  77. element.style.outline = 'none';
  78. element.style.zIndex = '999';
  79. }
  80. if ( 'xr' in navigator && 'supportsSession' in navigator.xr ) {
  81. var button = document.createElement( 'button' );
  82. button.style.display = 'none';
  83. stylizeElement( button );
  84. navigator.xr.supportsSession( 'immersive-vr' ).then( showEnterXR );
  85. return button;
  86. } else if ( 'getVRDisplays' in navigator ) {
  87. var button = document.createElement( 'button' );
  88. button.style.display = 'none';
  89. stylizeElement( button );
  90. window.addEventListener( 'vrdisplayconnect', function ( event ) {
  91. showEnterVR( event.display );
  92. }, false );
  93. window.addEventListener( 'vrdisplaydisconnect', function ( event ) {
  94. showVRNotFound();
  95. }, false );
  96. window.addEventListener( 'vrdisplaypresentchange', function ( event ) {
  97. button.textContent = event.display.isPresenting ? 'EXIT VR' : 'ENTER VR';
  98. }, false );
  99. window.addEventListener( 'vrdisplayactivate', function ( event ) {
  100. event.display.requestPresent( [ { source: renderer.domElement } ] );
  101. }, false );
  102. navigator.getVRDisplays()
  103. .then( function ( displays ) {
  104. if ( displays.length > 0 ) {
  105. showEnterVR( displays[ 0 ] );
  106. } else {
  107. showVRNotFound();
  108. }
  109. } ).catch( showVRNotFound );
  110. return button;
  111. } else {
  112. var message = document.createElement( 'a' );
  113. message.href = 'https://webvr.info';
  114. message.innerHTML = 'WEBVR NOT SUPPORTED';
  115. message.style.left = 'calc(50% - 90px)';
  116. message.style.width = '180px';
  117. message.style.textDecoration = 'none';
  118. stylizeElement( message );
  119. return message;
  120. }
  121. },
  122. // DEPRECATED
  123. checkAvailability: function () {
  124. console.warn( 'WEBVR.checkAvailability has been deprecated.' );
  125. return new Promise( function () {} );
  126. },
  127. getMessageContainer: function () {
  128. console.warn( 'WEBVR.getMessageContainer has been deprecated.' );
  129. return document.createElement( 'div' );
  130. },
  131. getButton: function () {
  132. console.warn( 'WEBVR.getButton has been deprecated.' );
  133. return document.createElement( 'div' );
  134. },
  135. getVRDisplay: function () {
  136. console.warn( 'WEBVR.getVRDisplay has been deprecated.' );
  137. }
  138. };