VRButton.js 3.5 KB

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