WebVR.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.zIndex = '999';
  45. }
  46. if ( 'getVRDisplays' in navigator ) {
  47. var button = document.createElement( 'button' );
  48. button.style.display = 'none';
  49. stylizeElement( button );
  50. window.addEventListener( 'vrdisplayconnect', function ( event ) {
  51. showEnterVR( event.display );
  52. }, false );
  53. window.addEventListener( 'vrdisplaydisconnect', function ( event ) {
  54. showVRNotFound();
  55. }, false );
  56. window.addEventListener( 'vrdisplaypresentchange', function ( event ) {
  57. button.textContent = event.display.isPresenting ? 'EXIT VR' : 'ENTER VR';
  58. }, false );
  59. navigator.getVRDisplays()
  60. .then( function ( displays ) {
  61. if ( displays.length > 0 ) {
  62. showEnterVR( displays[ 0 ] );
  63. } else {
  64. showVRNotFound();
  65. }
  66. } );
  67. return button;
  68. } else {
  69. var message = document.createElement( 'a' );
  70. message.href = 'https://webvr.info';
  71. message.innerHTML = 'WEBVR NOT SUPPORTED';
  72. message.style.left = 'calc(50% - 90px)';
  73. message.style.width = '180px';
  74. message.style.textDecoration = 'none';
  75. stylizeElement( message );
  76. return message;
  77. }
  78. },
  79. // DEPRECATED
  80. checkAvailability: function () {
  81. console.warn( 'WEBVR.checkAvailability has been deprecated.' );
  82. return new Promise( function () {} );
  83. },
  84. getMessageContainer: function () {
  85. console.warn( 'WEBVR.getMessageContainer has been deprecated.' );
  86. return document.createElement( 'div' );
  87. },
  88. getButton: function () {
  89. console.warn( 'WEBVR.getButton has been deprecated.' );
  90. return document.createElement( 'div' );
  91. },
  92. getVRDisplay: function () {
  93. console.warn( 'WEBVR.getVRDisplay has been deprecated.' );
  94. }
  95. };