ARButton.js 3.0 KB

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