VRButton.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @author mrdoob / http://mrdoob.com
  3. * @author Mugen87 / https://github.com/Mugen87
  4. */
  5. var VRButton = {
  6. createButton: function ( renderer, options ) {
  7. if ( options && options.referenceSpaceType ) {
  8. renderer.xr.setReferenceSpaceType( options.referenceSpaceType );
  9. }
  10. function showEnterVR( /*device*/ ) {
  11. var currentSession = null;
  12. function onSessionStarted( session ) {
  13. session.addEventListener( 'end', onSessionEnded );
  14. renderer.xr.setSession( session );
  15. button.textContent = 'EXIT VR';
  16. currentSession = session;
  17. }
  18. function onSessionEnded( /*event*/ ) {
  19. currentSession.removeEventListener( 'end', onSessionEnded );
  20. button.textContent = 'ENTER VR';
  21. currentSession = null;
  22. }
  23. //
  24. button.style.display = '';
  25. button.style.cursor = 'pointer';
  26. button.style.left = 'calc(50% - 50px)';
  27. button.style.width = '100px';
  28. button.textContent = 'ENTER VR';
  29. button.onmouseenter = function () {
  30. button.style.opacity = '1.0';
  31. };
  32. button.onmouseleave = function () {
  33. button.style.opacity = '0.5';
  34. };
  35. button.onclick = function () {
  36. if ( currentSession === null ) {
  37. // WebXR's requestReferenceSpace only works if the corresponding feature
  38. // was requested at session creation time. For simplicity, just ask for
  39. // the interesting ones as optional features, but be aware that the
  40. // requestReferenceSpace call will fail if it turns out to be unavailable.
  41. // ('local' is always available for immersive sessions and doesn't need to
  42. // be requested separately.)
  43. var sessionInit = { optionalFeatures: [ 'local-floor', 'bounded-floor' ] };
  44. navigator.xr.requestSession( 'immersive-vr', sessionInit ).then( onSessionStarted );
  45. } else {
  46. currentSession.end();
  47. }
  48. };
  49. }
  50. function disableButton() {
  51. button.style.display = '';
  52. button.style.cursor = 'auto';
  53. button.style.left = 'calc(50% - 75px)';
  54. button.style.width = '150px';
  55. button.onmouseenter = null;
  56. button.onmouseleave = null;
  57. button.onclick = null;
  58. }
  59. function showWebXRNotFound() {
  60. disableButton();
  61. button.textContent = 'VR NOT SUPPORTED';
  62. }
  63. function stylizeElement( element ) {
  64. element.style.position = 'absolute';
  65. element.style.bottom = '20px';
  66. element.style.padding = '12px 6px';
  67. element.style.border = '1px solid #fff';
  68. element.style.borderRadius = '4px';
  69. element.style.background = 'rgba(0,0,0,0.1)';
  70. element.style.color = '#fff';
  71. element.style.font = 'normal 13px sans-serif';
  72. element.style.textAlign = 'center';
  73. element.style.opacity = '0.5';
  74. element.style.outline = 'none';
  75. element.style.zIndex = '999';
  76. }
  77. if ( 'xr' in navigator ) {
  78. var button = document.createElement( 'button' );
  79. button.style.display = 'none';
  80. stylizeElement( button );
  81. navigator.xr.isSessionSupported( 'immersive-vr' ).then( function ( supported ) {
  82. supported ? showEnterVR() : showWebXRNotFound();
  83. } );
  84. return button;
  85. } else {
  86. var message = document.createElement( 'a' );
  87. message.href = 'https://immersiveweb.dev/';
  88. if ( window.isSecureContext === false ) {
  89. message.innerHTML = 'WEBXR NEEDS HTTPS'; // TODO Improve message
  90. } else {
  91. message.innerHTML = 'WEBXR NOT AVAILABLE';
  92. }
  93. message.style.left = 'calc(50% - 90px)';
  94. message.style.width = '180px';
  95. message.style.textDecoration = 'none';
  96. stylizeElement( message );
  97. return message;
  98. }
  99. }
  100. };
  101. export { VRButton };