WebVR.js 2.9 KB

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