2
0

WebVR.js 2.8 KB

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