WebVR.js 4.8 KB

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