You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

demo.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * Augmented Reality template using encantar.js
  3. * @author Alexandre Martins <alemartf(at)gmail.com> (https://github.com/alemart/encantar-js)
  4. */
  5. (function() {
  6. /**
  7. * Start the AR session
  8. * @returns {Promise<Session>}
  9. */
  10. async function startARSession()
  11. {
  12. if(!AR.isSupported()) {
  13. throw new Error(
  14. 'This device is not compatible with this AR experience.\n\n' +
  15. 'User agent: ' + navigator.userAgent
  16. );
  17. }
  18. const tracker = AR.Tracker.ImageTracker();
  19. await tracker.database.add([
  20. {
  21. name: 'mage',
  22. image: document.getElementById('mage')
  23. },
  24. {
  25. name: 'cat',
  26. image: document.getElementById('cat')
  27. }
  28. ]);
  29. const viewport = AR.Viewport({
  30. container: document.getElementById('ar-viewport'),
  31. hudContainer: document.getElementById('ar-hud')
  32. });
  33. const video = document.getElementById('my-video');
  34. const useWebcam = (video === null);
  35. const source = useWebcam ? AR.Source.Camera() : AR.Source.Video(video);
  36. const session = await AR.startSession({
  37. mode: 'immersive',
  38. viewport: viewport,
  39. trackers: [ tracker ],
  40. sources: [ source ],
  41. stats: true,
  42. gizmos: true,
  43. });
  44. const scan = document.getElementById('scan');
  45. tracker.addEventListener('targetfound', event => {
  46. if(scan)
  47. scan.hidden = true;
  48. console.log('Target found: ' + event.referenceImage.name);
  49. });
  50. tracker.addEventListener('targetlost', event => {
  51. if(scan)
  52. scan.hidden = false;
  53. console.log('Target lost: ' + event.referenceImage.name);
  54. });
  55. return session;
  56. }
  57. /**
  58. * Animation loop
  59. * @param {number} time
  60. * @param {Frame} frame
  61. * @returns {void}
  62. */
  63. function animate(time, frame)
  64. {
  65. const session = frame.session;
  66. const deltaTime = session.time.delta; // given in seconds
  67. mix(frame);
  68. session.requestAnimationFrame(animate);
  69. }
  70. /**
  71. * Blend the physical and the virtual scenes
  72. * @param {Frame} frame
  73. * @returns {boolean} true if an image is being tracked
  74. */
  75. function mix(frame)
  76. {
  77. for(const result of frame.results) {
  78. if(result.tracker.type == 'image-tracker') {
  79. if(result.trackables.length > 0) {
  80. const trackable = result.trackables[0];
  81. const projectionMatrix = result.viewer.view.projectionMatrix;
  82. const viewMatrix = result.viewer.pose.viewMatrix;
  83. const modelMatrix = trackable.pose.transform.matrix;
  84. doSomethingWith(projectionMatrix, viewMatrix, modelMatrix);
  85. return true;
  86. }
  87. }
  88. }
  89. return false;
  90. }
  91. /**
  92. * Template function
  93. * @param {SpeedyMatrix} projectionMatrix
  94. * @param {SpeedyMatrix} viewMatrix
  95. * @param {SpeedyMatrix} modelMatrix
  96. * @returns {void}
  97. */
  98. function doSomethingWith(projectionMatrix, viewMatrix, modelMatrix)
  99. {
  100. /*
  101. console.log('projectionMatrix', projectionMatrix.toString());
  102. console.log('viewMatrix', viewMatrix.toString());
  103. console.log('modelMatrix', modelMatrix.toString());
  104. */
  105. }
  106. /**
  107. * Start the Demo
  108. * @returns {void}
  109. */
  110. function main()
  111. {
  112. startARSession().then(session => {
  113. const canvas = session.viewport.canvas; // render your virtual scene on this <canvas>
  114. session.requestAnimationFrame(animate); // start the animation loop
  115. }).catch(error => {
  116. alert(error.message);
  117. });
  118. }
  119. document.addEventListener('DOMContentLoaded', main);
  120. })();