WebVR.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. createButton: function ( renderer ) {
  9. function showEnterVR( display ) {
  10. button.style.display = '';
  11. button.style.cursor = 'pointer';
  12. button.style.left = 'calc(50% - 50px)';
  13. button.style.width = '100px';
  14. button.textContent = 'ENTER VR';
  15. button.onmouseenter = function () { button.style.opacity = '1.0'; };
  16. button.onmouseleave = function () { button.style.opacity = '0.5'; };
  17. button.onclick = function () {
  18. display.isPresenting ? display.exitPresent() : display.requestPresent( [ { source: renderer.domElement } ] );
  19. };
  20. renderer.vr.setDevice( display );
  21. }
  22. function showVRNotFound() {
  23. button.style.display = '';
  24. button.style.cursor = 'auto';
  25. button.style.left = 'calc(50% - 75px)';
  26. button.style.width = '150px';
  27. button.textContent = 'VR NOT FOUND';
  28. button.onmouseenter = null;
  29. button.onmouseleave = null;
  30. button.onclick = null;
  31. renderer.vr.setDevice( null );
  32. }
  33. function stylizeElement( element ) {
  34. element.style.position = 'absolute';
  35. element.style.bottom = '20px';
  36. element.style.padding = '12px 6px';
  37. element.style.border = '1px solid #fff';
  38. element.style.borderRadius = '4px';
  39. element.style.background = 'transparent';
  40. element.style.color = '#fff';
  41. element.style.font = 'normal 13px sans-serif';
  42. element.style.textAlign = 'center';
  43. element.style.opacity = '0.5';
  44. element.style.outline = 'none';
  45. element.style.zIndex = '999';
  46. }
  47. if ( 'getVRDisplays' in navigator ) {
  48. var button = document.createElement( 'button' );
  49. button.style.display = 'none';
  50. stylizeElement( button );
  51. window.addEventListener( 'vrdisplayconnect', function ( event ) {
  52. showEnterVR( event.display );
  53. }, false );
  54. window.addEventListener( 'vrdisplaydisconnect', function ( event ) {
  55. showVRNotFound();
  56. }, false );
  57. window.addEventListener( 'vrdisplaypresentchange', function ( event ) {
  58. button.textContent = event.display.isPresenting ? 'EXIT VR' : 'ENTER VR';
  59. }, false );
  60. navigator.getVRDisplays()
  61. .then( function ( displays ) {
  62. if ( displays.length > 0 ) {
  63. showEnterVR( displays[ 0 ] );
  64. } else {
  65. showVRNotFound();
  66. }
  67. } );
  68. return button;
  69. } else {
  70. var message = document.createElement( 'a' );
  71. message.href = 'https://webvr.info';
  72. message.innerHTML = 'WEBVR NOT SUPPORTED';
  73. message.style.left = 'calc(50% - 90px)';
  74. message.style.width = '180px';
  75. message.style.textDecoration = 'none';
  76. stylizeElement( message );
  77. return message;
  78. }
  79. },
  80. // DEPRECATED
  81. checkAvailability: function () {
  82. console.warn( 'WEBVR.checkAvailability has been deprecated.' );
  83. return new Promise( function () {} );
  84. },
  85. getMessageContainer: function () {
  86. console.warn( 'WEBVR.getMessageContainer has been deprecated.' );
  87. return document.createElement( 'div' );
  88. },
  89. getButton: function () {
  90. console.warn( 'WEBVR.getButton has been deprecated.' );
  91. return document.createElement( 'div' );
  92. },
  93. getVRDisplay: function () {
  94. console.warn( 'WEBVR.getVRDisplay has been deprecated.' );
  95. }
  96. };