Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

demo.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.Image();
  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. if(scan)
  46. scan.style.pointerEvents = 'none';
  47. tracker.addEventListener('targetfound', event => {
  48. if(scan)
  49. scan.hidden = true;
  50. console.log('Target found: ' + event.referenceImage.name);
  51. });
  52. tracker.addEventListener('targetlost', event => {
  53. if(scan)
  54. scan.hidden = false;
  55. console.log('Target lost: ' + event.referenceImage.name);
  56. });
  57. return session;
  58. }
  59. /**
  60. * Animation loop
  61. * @param {number} time
  62. * @param {Frame} frame
  63. * @returns {void}
  64. */
  65. function animate(time, frame)
  66. {
  67. const session = frame.session;
  68. const deltaTime = session.time.delta; // given in seconds
  69. mix(frame);
  70. session.requestAnimationFrame(animate);
  71. }
  72. /**
  73. * Blend the physical and the virtual scenes
  74. * @param {Frame} frame
  75. * @returns {boolean} true if an image is being tracked
  76. */
  77. function mix(frame)
  78. {
  79. for(const result of frame.results) {
  80. if(result.of('image-tracker')) {
  81. if(result.trackables.length > 0) {
  82. const trackable = result.trackables[0];
  83. const projectionMatrix = result.viewer.view.projectionMatrix;
  84. const viewMatrix = result.viewer.pose.viewMatrix;
  85. const modelMatrix = trackable.pose.transform.matrix;
  86. doSomethingWith(projectionMatrix, viewMatrix, modelMatrix);
  87. return true;
  88. }
  89. }
  90. }
  91. return false;
  92. }
  93. /**
  94. * Template function
  95. * @param {SpeedyMatrix} projectionMatrix
  96. * @param {SpeedyMatrix} viewMatrix
  97. * @param {SpeedyMatrix} modelMatrix
  98. * @returns {void}
  99. */
  100. function doSomethingWith(projectionMatrix, viewMatrix, modelMatrix)
  101. {
  102. /*
  103. console.log('projectionMatrix', projectionMatrix.toString());
  104. console.log('viewMatrix', viewMatrix.toString());
  105. console.log('modelMatrix', modelMatrix.toString());
  106. */
  107. }
  108. /**
  109. * Start the Demo
  110. * @returns {void}
  111. */
  112. function main()
  113. {
  114. startARSession().then(session => {
  115. const canvas = session.viewport.canvas; // render your virtual scene on this <canvas>
  116. session.requestAnimationFrame(animate); // start the animation loop
  117. }).catch(error => {
  118. alert(error.message);
  119. });
  120. }
  121. document.addEventListener('DOMContentLoaded', main);
  122. })();