WebVR.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. getMessageContainer: function ( message ) {
  36. var container = document.createElement( 'div' );
  37. container.style.position = 'absolute';
  38. container.style.left = '0';
  39. container.style.top = '0';
  40. container.style.right = '0';
  41. container.style.zIndex = '999';
  42. container.align = 'center';
  43. var error = document.createElement( 'div' );
  44. error.style.fontFamily = 'sans-serif';
  45. error.style.fontSize = '16px';
  46. error.style.fontStyle = 'normal';
  47. error.style.lineHeight = '26px';
  48. error.style.backgroundColor = '#fff';
  49. error.style.color = '#000';
  50. error.style.padding = '10px 20px';
  51. error.style.margin = '50px';
  52. error.style.display = 'inline-block';
  53. error.innerHTML = message;
  54. container.appendChild( error );
  55. return container;
  56. },
  57. getButton: function ( display, canvas ) {
  58. if ( 'VREffect' in THREE && display instanceof THREE.VREffect ) {
  59. console.error( 'WebVR.getButton() now expects a VRDisplay.' );
  60. return document.createElement( 'button' );
  61. }
  62. var button = document.createElement( 'button' );
  63. button.style.position = 'absolute';
  64. button.style.left = 'calc(50% - 50px)';
  65. button.style.bottom = '20px';
  66. button.style.width = '100px';
  67. button.style.border = '0';
  68. button.style.padding = '8px';
  69. button.style.cursor = 'pointer';
  70. button.style.backgroundColor = '#000';
  71. button.style.color = '#fff';
  72. button.style.fontFamily = 'sans-serif';
  73. button.style.fontSize = '13px';
  74. button.style.fontStyle = 'normal';
  75. button.style.textAlign = 'center';
  76. button.style.zIndex = '999';
  77. if ( display ) {
  78. button.textContent = 'ENTER VR';
  79. button.onclick = function () {
  80. display.isPresenting ? display.exitPresent() : display.requestPresent( [ { source: canvas } ] );
  81. };
  82. window.addEventListener( 'vrdisplaypresentchange', function () {
  83. button.textContent = display.isPresenting ? 'EXIT VR' : 'ENTER VR';
  84. }, false );
  85. } else {
  86. button.textContent = 'NO VR DISPLAY';
  87. }
  88. return button;
  89. }
  90. };