WebVR.js 3.0 KB

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