WebVR.js 4.9 KB

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