WebVR.js 5.0 KB

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