WebVR.js 4.1 KB

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