ARButton.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * @author mrdoob / http://mrdoob.com
  3. * @author Mugen87 / https://github.com/Mugen87
  4. */
  5. var ARButton = {
  6. createButton: function ( renderer ) {
  7. function showEnterXR( /*device*/ ) {
  8. var currentSession = null;
  9. function onSessionStarted( session ) {
  10. session.addEventListener( 'end', onSessionEnded );
  11. /*
  12. session.updateWorldTrackingState( {
  13. 'planeDetectionState': { 'enabled': true }
  14. } );
  15. */
  16. renderer.vr.setReferenceSpaceType( 'local' );
  17. renderer.vr.setSession( session );
  18. button.textContent = 'STOP AR';
  19. currentSession = session;
  20. }
  21. function onSessionEnded( /*event*/ ) {
  22. currentSession.removeEventListener( 'end', onSessionEnded );
  23. renderer.vr.setSession( null );
  24. button.textContent = 'START AR';
  25. currentSession = null;
  26. }
  27. //
  28. button.style.display = '';
  29. button.style.cursor = 'pointer';
  30. button.style.left = 'calc(50% - 50px)';
  31. button.style.width = '100px';
  32. button.textContent = 'START AR';
  33. button.onmouseenter = function () {
  34. button.style.opacity = '1.0';
  35. };
  36. button.onmouseleave = function () {
  37. button.style.opacity = '0.5';
  38. };
  39. button.onclick = function () {
  40. if ( currentSession === null ) {
  41. navigator.xr.requestSession( 'immersive-ar' ).then( onSessionStarted );
  42. } else {
  43. currentSession.end();
  44. }
  45. };
  46. }
  47. function disableButton() {
  48. button.style.display = '';
  49. button.style.cursor = 'auto';
  50. button.style.left = 'calc(50% - 75px)';
  51. button.style.width = '150px';
  52. button.onmouseenter = null;
  53. button.onmouseleave = null;
  54. button.onclick = null;
  55. }
  56. function showXRNotFound() {
  57. disableButton();
  58. button.textContent = 'XR NOT FOUND';
  59. }
  60. function stylizeElement( element ) {
  61. element.style.position = 'absolute';
  62. element.style.bottom = '20px';
  63. element.style.padding = '12px 6px';
  64. element.style.border = '1px solid #fff';
  65. element.style.borderRadius = '4px';
  66. element.style.background = 'rgba(0,0,0,0.1)';
  67. element.style.color = '#fff';
  68. element.style.font = 'normal 13px sans-serif';
  69. element.style.textAlign = 'center';
  70. element.style.opacity = '0.5';
  71. element.style.outline = 'none';
  72. element.style.zIndex = '999';
  73. }
  74. if ( 'xr' in navigator ) {
  75. var button = document.createElement( 'button' );
  76. button.style.display = 'none';
  77. stylizeElement( button );
  78. navigator.xr.isSessionSupported( 'immersive-ar' ).then( function ( supported ) {
  79. if ( supported ) {
  80. showEnterXR();
  81. } else {
  82. showXRNotFound();
  83. }
  84. } );
  85. return button;
  86. } else {
  87. var message = document.createElement( 'a' );
  88. message.href = 'https://immersive-web.github.io/webxr/';
  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 { ARButton };