WebVR.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. isAvailable: function () {
  9. console.warn( 'WEBVR: isAvailable() is being deprecated. Use .checkAvailability() instead.' );
  10. return navigator.getVRDisplays !== undefined;
  11. },
  12. checkAvailability: function () {
  13. return new Promise( function( resolve, reject ) {
  14. if ( navigator.getVRDisplays !== undefined ) {
  15. navigator.getVRDisplays().then( function ( displays ) {
  16. if ( displays.length === 0 ) {
  17. reject( 'WebVR supported, but no VRDisplays found.' );
  18. } else {
  19. resolve();
  20. }
  21. }, function () {
  22. reject( 'Your browser does not support WebVR. See <a href="https://webvr.info">webvr.info</a> for assistance.' );
  23. } );
  24. } else {
  25. reject( 'Your browser does not support WebVR. See <a href="https://webvr.info">webvr.info</a> for assistance.' );
  26. }
  27. } );
  28. },
  29. getVRDisplay: function ( onDisplay ) {
  30. if ( 'getVRDisplays' in navigator ) {
  31. navigator.getVRDisplays()
  32. .then( function ( displays ) {
  33. onDisplay( displays[ 0 ] );
  34. } );
  35. }
  36. },
  37. getMessage: function () {
  38. console.warn( 'WEBVR: getMessage() is being deprecated. Use .getMessageContainer( message ) instead.' );
  39. var message;
  40. if ( navigator.getVRDisplays ) {
  41. navigator.getVRDisplays().then( function ( displays ) {
  42. if ( displays.length === 0 ) message = 'WebVR supported, but no VRDisplays found.';
  43. } );
  44. } else {
  45. message = 'Your browser does not support WebVR. See <a href="http://webvr.info">webvr.info</a> for assistance.';
  46. }
  47. if ( message !== undefined ) {
  48. var container = document.createElement( 'div' );
  49. container.style.position = 'absolute';
  50. container.style.left = '0';
  51. container.style.top = '0';
  52. container.style.right = '0';
  53. container.style.zIndex = '999';
  54. container.align = 'center';
  55. var error = document.createElement( 'div' );
  56. error.style.fontFamily = 'sans-serif';
  57. error.style.fontSize = '16px';
  58. error.style.fontStyle = 'normal';
  59. error.style.lineHeight = '26px';
  60. error.style.backgroundColor = '#fff';
  61. error.style.color = '#000';
  62. error.style.padding = '10px 20px';
  63. error.style.margin = '50px';
  64. error.style.display = 'inline-block';
  65. error.innerHTML = message;
  66. container.appendChild( error );
  67. return container;
  68. }
  69. },
  70. getMessageContainer: function ( message ) {
  71. var container = document.createElement( 'div' );
  72. container.style.position = 'absolute';
  73. container.style.left = '0';
  74. container.style.top = '0';
  75. container.style.right = '0';
  76. container.style.zIndex = '999';
  77. container.align = 'center';
  78. var error = document.createElement( 'div' );
  79. error.style.fontFamily = 'sans-serif';
  80. error.style.fontSize = '16px';
  81. error.style.fontStyle = 'normal';
  82. error.style.lineHeight = '26px';
  83. error.style.backgroundColor = '#fff';
  84. error.style.color = '#000';
  85. error.style.padding = '10px 20px';
  86. error.style.margin = '50px';
  87. error.style.display = 'inline-block';
  88. error.innerHTML = message;
  89. container.appendChild( error );
  90. return container;
  91. },
  92. getButton: function ( display, canvas ) {
  93. if ( 'VREffect' in THREE && display instanceof THREE.VREffect ) {
  94. console.error( 'WebVR.getButton() now expects a VRDisplay.' );
  95. return document.createElement( 'button' );
  96. }
  97. var button = document.createElement( 'button' );
  98. button.style.position = 'absolute';
  99. button.style.left = 'calc(50% - 50px)';
  100. button.style.bottom = '20px';
  101. button.style.width = '100px';
  102. button.style.border = '0';
  103. button.style.padding = '8px';
  104. button.style.cursor = 'pointer';
  105. button.style.backgroundColor = '#000';
  106. button.style.color = '#fff';
  107. button.style.fontFamily = 'sans-serif';
  108. button.style.fontSize = '13px';
  109. button.style.fontStyle = 'normal';
  110. button.style.textAlign = 'center';
  111. button.style.zIndex = '999';
  112. if ( display ) {
  113. button.textContent = 'ENTER VR';
  114. button.onclick = function () {
  115. display.isPresenting ? display.exitPresent() : display.requestPresent( [ { source: canvas } ] );
  116. };
  117. window.addEventListener( 'vrdisplaypresentchange', function () {
  118. button.textContent = display.isPresenting ? 'EXIT VR' : 'ENTER VR';
  119. }, false );
  120. } else {
  121. button.textContent = 'NO VR DISPLAY';
  122. }
  123. return button;
  124. }
  125. };