WebVR.js 4.2 KB

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