Tempusxr English
Menu
  1. Documentation
  2. Exemples de suivi d'images

Multi-pistes

Les mêmes trois images, suivies en même temps. Montrez deux images ensemble et chacune reçoit son contenu. Un compteur dans l'overlay dit combien sont visibles.

Essayer

Ouvrez les démos sur un téléphone : la caméra a besoin d'une page sécurisée. Puis visez l'image ci-dessous, affichée sur un second écran ou imprimée.

Image à viser avec la caméra : card.png Image à viser avec la caméra : poster.png

A-Frame

maxTrack: 3 sur le composant tempusxr laisse le traqueur suivre trois images à la fois. targetFound et targetLost partent sur chaque entité, et la valeur de l'overlay change avec overlay.set().

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
    <title>Multi-tracks (A-Frame)</title>
    <!-- A-Frame, Tempusxr and the image tracker, as plain script tags. -->
    <script src="/node_modules/aframe/dist/aframe-v1.8.0.min.js"></script>
    <script src="/packages/sdk-aframe/dist/index.umd.cjs"></script>
    <script src="/packages/sdk-tracker-image/dist/index.umd.cjs"></script>
    <style>
      .pill {
        padding: 6px 16px;
        border-radius: 999px;
        background: rgba(0, 0, 0, 0.55);
        color: #fff;
        font-weight: 700;
      }
    </style>
  </head>
  <body>
    <!--
      The same three images, followed all at once (maxTrack: 3). Put two or three of
      card.png, poster.png and art.png in front of the camera together. The counter on top
      is a data-tx-bind value of the overlay.
    -->
    <div id="ui" class="tx-overlay">
      <div data-tx-slot="top-left" class="pill">In view: <b data-tx-bind="count">0</b></div>
      <div data-tx-slot="center" data-tx-show="scanning" class="pill">Show the images</div>
    </div>

    <a-scene
      tempusxr="tracker: image; targets: /examples/tracking/multi.txr; maxTrack: 3; overlay: #ui"
      renderer="alpha: true"
      vr-mode-ui="enabled: false"
    >
      <a-camera></a-camera>
      <a-light type="ambient" intensity="0.9"></a-light>
      <a-light type="directional" position="0.5 1 1" intensity="0.8"></a-light>

      <a-entity tempusxr-image-target="index: 0">
        <a-box position="0 0 0.12" scale="0.25 0.25 0.25" color="#fecb02"></a-box>
      </a-entity>
      <a-entity tempusxr-image-target="index: 1">
        <a-sphere position="0 0 0.2" radius="0.18" color="#ff5e62"></a-sphere>
      </a-entity>
      <a-entity tempusxr-image-target="index: 2">
        <a-cone
          position="0 0 0.2"
          rotation="90 0 0"
          radius-bottom="0.15"
          height="0.35"
          color="#3d7a68"
        ></a-cone>
      </a-entity>
    </a-scene>

    <script>
      const scene = document.querySelector('a-scene');
      let overlay = null;
      let inView = 0;
      scene.addEventListener('tempusxr-ready', (event) => {
        overlay = event.detail.overlay;
        overlay.set('count', 0);
      });
      for (const target of document.querySelectorAll('[tempusxr-image-target]')) {
        target.addEventListener('targetFound', () => overlay?.set('count', ++inView));
        target.addEventListener('targetLost', () => overlay?.set('count', --inView));
      }
    </script>
  </body>
</html>

Three.js

maxTrack: 3 est une option du traqueur d'images. Les événements found et lost arrivent avec l'ancre qui a changé.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
    <title>Multi-tracks (Three.js)</title>
    <!-- Three.js with the SDK, as a page without a bundler loads it. -->
    <script type="importmap">
      {
        "imports": {
          "three": "/node_modules/three/build/three.module.js",
          "@tempusxr/sdk-core": "/packages/sdk-core/dist/index.js",
          "@tempusxr/sdk-three": "/packages/sdk-three/dist/index.js",
          "@tempusxr/sdk-tracker-image": "/packages/sdk-tracker-image/dist/index.js"
        }
      }
    </script>
    <style>
      html,
      body {
        margin: 0;
        height: 100%;
        background: #111;
      }
      .pill {
        padding: 6px 16px;
        border-radius: 999px;
        background: rgba(0, 0, 0, 0.55);
        color: #fff;
        font-weight: 700;
      }
    </style>
  </head>
  <body>
    <!--
      The same three images, followed all at once (maxTrack: 3). Put two or three of
      card.png, poster.png and art.png in front of the camera together. The counter on top
      is a data-tx-bind value of the overlay.
    -->
    <div id="ui" class="tx-overlay">
      <div data-tx-slot="top-left" class="pill">In view: <b data-tx-bind="count">0</b></div>
      <div data-tx-slot="center" data-tx-show="scanning" class="pill">Show the images</div>
    </div>
    <div id="ar"></div>
    <script type="module">
      import * as THREE from 'three';
      import { create } from '@tempusxr/sdk-three';
      import '@tempusxr/sdk-tracker-image';

      const ar = create({ container: '#ar', overlay: document.getElementById('ui') });
      window.__ar = ar;
      const tracker = await ar.use('image', {
        targets: ['/examples/tracking/multi.txr'],
        maxTrack: 3,
      });

      const shapes = [
        [new THREE.BoxGeometry(0.25, 0.25, 0.25), '#fecb02', 0.12],
        [new THREE.SphereGeometry(0.18, 32, 16), '#ff5e62', 0.2],
        [new THREE.ConeGeometry(0.15, 0.35, 32).rotateX(Math.PI / 2), '#3d7a68', 0.2],
      ];
      shapes.forEach(([geometry, color, lift], index) => {
        const content = new THREE.Group();
        content.add(new THREE.AmbientLight(0xffffff, 1.2));
        const sun = new THREE.DirectionalLight(0xffffff, 1.5);
        sun.position.set(0.5, 1, 1);
        content.add(sun);
        const mesh = new THREE.Mesh(geometry, new THREE.MeshStandardMaterial({ color }));
        mesh.position.z = lift;
        content.add(mesh);
        ar.attach(tracker.anchors[index], content);
      });

      let inView = 0;
      ar.ui.set('count', 0);
      ar.on('found', () => ar.ui.set('count', ++inView));
      ar.on('lost', () => ar.ui.set('count', --inView));

      await ar.start();
    </script>
  </body>
</html>

À retenir