WebVR.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. 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. if ( options === undefined ) options = {};
  26. if ( options.frameOfReferenceType === undefined ) options.frameOfReferenceType = 'stage';
  27. session.addEventListener( 'end', onSessionEnded );
  28. renderer.vr.setSession( session, options );
  29. button.textContent = 'EXIT XR';
  30. currentSession = session;
  31. }
  32. function onSessionEnded( event ) {
  33. currentSession.removeEventListener( 'end', onSessionEnded );
  34. renderer.vr.setSession( null );
  35. button.textContent = 'ENTER XR';
  36. currentSession = null;
  37. }
  38. //
  39. button.style.display = '';
  40. button.style.cursor = 'pointer';
  41. button.style.left = 'calc(50% - 50px)';
  42. button.style.width = '100px';
  43. button.textContent = 'ENTER XR';
  44. button.onmouseenter = function () { button.style.opacity = '1.0'; };
  45. button.onmouseleave = function () { button.style.opacity = '0.5'; };
  46. button.onclick = function () {
  47. if ( currentSession === null ) {
  48. device.requestSession( { immersive: true } ).then( onSessionStarted );
  49. } else {
  50. currentSession.end();
  51. }
  52. };
  53. renderer.vr.setDevice( device );
  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 = 'transparent';
  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 ) {
  81. var button = document.createElement( 'button' );
  82. button.style.display = 'none';
  83. stylizeElement( button );
  84. navigator.xr.requestDevice().then( function ( device ) {
  85. device.supportsSession( { immersive: true } ).then( function () {
  86. showEnterXR( device );
  87. } ).catch( showVRNotFound );
  88. } ).catch( showVRNotFound );
  89. return button;
  90. } else if ( 'getVRDisplays' in navigator ) {
  91. var button = document.createElement( 'button' );
  92. button.style.display = 'none';
  93. stylizeElement( button );
  94. window.addEventListener( 'vrdisplayconnect', function ( event ) {
  95. showEnterVR( event.display );
  96. }, false );
  97. window.addEventListener( 'vrdisplaydisconnect', function ( event ) {
  98. showVRNotFound();
  99. }, false );
  100. window.addEventListener( 'vrdisplaypresentchange', function ( event ) {
  101. button.textContent = event.display.isPresenting ? 'EXIT VR' : 'ENTER VR';
  102. }, false );
  103. window.addEventListener( 'vrdisplayactivate', function ( event ) {
  104. event.display.requestPresent( [ { source: renderer.domElement } ] );
  105. }, false );
  106. navigator.getVRDisplays()
  107. .then( function ( displays ) {
  108. if ( displays.length > 0 ) {
  109. showEnterVR( displays[ 0 ] );
  110. } else {
  111. showVRNotFound();
  112. }
  113. } );
  114. return button;
  115. } else {
  116. var message = document.createElement( 'a' );
  117. message.href = 'https://webvr.info';
  118. message.innerHTML = 'WEBVR NOT SUPPORTED';
  119. message.style.left = 'calc(50% - 90px)';
  120. message.style.width = '180px';
  121. message.style.textDecoration = 'none';
  122. stylizeElement( message );
  123. return message;
  124. }
  125. },
  126. // DEPRECATED
  127. checkAvailability: function () {
  128. console.warn( 'WEBVR.checkAvailability has been deprecated.' );
  129. return new Promise( function () {} );
  130. },
  131. getMessageContainer: function () {
  132. console.warn( 'WEBVR.getMessageContainer has been deprecated.' );
  133. return document.createElement( 'div' );
  134. },
  135. getButton: function () {
  136. console.warn( 'WEBVR.getButton has been deprecated.' );
  137. return document.createElement( 'div' );
  138. },
  139. getVRDisplay: function () {
  140. console.warn( 'WEBVR.getVRDisplay has been deprecated.' );
  141. }
  142. };