ARButton.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * @author mrdoob / http://mrdoob.com
  3. * @author Mugen87 / https://github.com/Mugen87
  4. */
  5. var ARButton = {
  6. createButton: function ( renderer ) {
  7. function showStartAR( /*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.xr.setReferenceSpaceType( 'local' );
  17. renderer.xr.setSession( session );
  18. button.textContent = 'STOP AR';
  19. currentSession = session;
  20. }
  21. function onSessionEnded( /*event*/ ) {
  22. currentSession.removeEventListener( 'end', onSessionEnded );
  23. button.textContent = 'START AR';
  24. currentSession = null;
  25. }
  26. //
  27. button.style.display = '';
  28. button.style.cursor = 'pointer';
  29. button.style.left = 'calc(50% - 50px)';
  30. button.style.width = '100px';
  31. button.textContent = 'START AR';
  32. button.onmouseenter = function () {
  33. button.style.opacity = '1.0';
  34. };
  35. button.onmouseleave = function () {
  36. button.style.opacity = '0.5';
  37. };
  38. button.onclick = function () {
  39. if ( currentSession === null ) {
  40. navigator.xr.requestSession( 'immersive-ar' ).then( onSessionStarted );
  41. } else {
  42. currentSession.end();
  43. }
  44. };
  45. }
  46. function disableButton() {
  47. button.style.display = '';
  48. button.style.cursor = 'auto';
  49. button.style.left = 'calc(50% - 75px)';
  50. button.style.width = '150px';
  51. button.onmouseenter = null;
  52. button.onmouseleave = null;
  53. button.onclick = null;
  54. }
  55. function showARNotSupported() {
  56. disableButton();
  57. button.textContent = 'AR NOT SUPPORTED';
  58. }
  59. function stylizeElement( element ) {
  60. element.style.position = 'absolute';
  61. element.style.bottom = '20px';
  62. element.style.padding = '12px 6px';
  63. element.style.border = '1px solid #fff';
  64. element.style.borderRadius = '4px';
  65. element.style.background = 'rgba(0,0,0,0.1)';
  66. element.style.color = '#fff';
  67. element.style.font = 'normal 13px sans-serif';
  68. element.style.textAlign = 'center';
  69. element.style.opacity = '0.5';
  70. element.style.outline = 'none';
  71. element.style.zIndex = '999';
  72. }
  73. if ( 'xr' in navigator ) {
  74. var button = document.createElement( 'button' );
  75. button.style.display = 'none';
  76. stylizeElement( button );
  77. navigator.xr.isSessionSupported( 'immersive-ar' ).then( function ( supported ) {
  78. supported ? showStartAR() : showARNotSupported();
  79. } ).catch( showARNotSupported );
  80. return button;
  81. } else {
  82. var message = document.createElement( 'a' );
  83. message.href = 'https://immersiveweb.dev/';
  84. if ( window.isSecureContext === false ) {
  85. message.innerHTML = 'WEBXR NEEDS HTTPS'; // TODO Improve message
  86. } else {
  87. message.innerHTML = 'WEBXR NOT AVAILABLE';
  88. }
  89. message.style.left = 'calc(50% - 90px)';
  90. message.style.width = '180px';
  91. message.style.textDecoration = 'none';
  92. stylizeElement( message );
  93. return message;
  94. }
  95. }
  96. };
  97. export { ARButton };