- Documentation
- Bibliothèque d'expériences
- Essayez vos propres images
Essayez vos propres images Suivi d'images
Choisissez des images sur votre téléphone, puis visez-les avec la caméra. Plusieurs cibles à la fois.
Code
Le code de cette expérience, séparé en fichiers comme vous les garderiez : HTML, CSS et JavaScript. La page en ligne les garde dans un seul fichier ; copiez chaque partie dans son fichier et la page marche pareil.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
<title>Tempusxr image tracking with A-Frame</title>
<!-- 1. A-Frame. 2. Tempusxr for A-Frame. 3. The image tracker. All plain script tags. Needs pnpm build. -->
<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>
<!-- Only this test page needs the compiler, to turn the images you pick into a target file. -->
<script src="/packages/target-compiler/dist/index.umd.cjs"></script>
<link rel="stylesheet" href="page.css" />
</head>
<body>
<div id="picker">
<strong>Image tracking with A-Frame tags</strong>
<label>
Target images, or a compiled .txr file
<input id="files" type="file" accept="image/*,.txr" multiple />
</label>
<button id="start">Start</button>
<div id="status">Pick one or more images (or a .txr), then Start.</div>
</div>
<!--
Your own overlay: copy this block, change it, remove what you do not need. Open the page
with ?ui=custom to try it. Slots (top-left, top-right, center, bottom) are direct children.
data-tx-show: visible only in those states. data-tx-action: info, close, share, reload,
fullscreen, exit, or your own name. data-tx-bind: text filled by overlay.set('score', 12).
-->
<template id="custom-ui">
<div id="my-ui" class="tx-overlay">
<div data-tx-slot="top-left" class="my-pill">Score <b data-tx-bind="score">0</b></div>
<div data-tx-slot="top-right" class="tx-rail">
<button class="tx-btn" type="button" data-tx-action="info" aria-label="Help">?</button>
<button class="tx-btn" type="button" data-tx-action="boost" aria-label="Boost">
+10
</button>
</div>
<div data-tx-slot="center" data-tx-show="scanning" class="my-hint">Find the poster</div>
<div data-tx-slot="bottom">
<button class="tx-cta" type="button" data-tx-show="found" data-tx-action="play">
Play
</button>
<p class="tx-message" data-tx-show="error" data-tx-bind="message"></p>
</div>
<div class="tx-modal" data-tx-modal="info" hidden>
<div class="tx-panel">
<button class="tx-close" type="button" data-tx-action="close" aria-label="Close">
x
</button>
<h2>Help</h2>
<p>Aim at the poster, then tap Play.</p>
</div>
</div>
</div>
</template>
<!--
The whole experience is this scene. targets: the .txr file (made by the target compiler).
This page lets you pick images instead and fills in `targets` for you. Open it with
?txr=URL to use a .txr file straight away.
-->
<a-scene
tempusxr="tracker: image; targets: targets.txr; maxTrack: 2; autoStart: false; debug: true"
renderer="alpha: true"
vr-mode-ui="enabled: false"
embedded="false"
>
<a-camera
position="0 0 0"
look-controls="enabled: false"
wasd-controls="enabled: false"
></a-camera>
<!-- Content follows the first image. The image is 1 unit wide. tempusxr-image-plane gives the plane the real proportions of the image. -->
<a-entity tempusxr-image-target="index: 0">
<a-plane
tempusxr-image-plane
width="1"
height="0.75"
color="#40c4ff"
opacity="0.3"
></a-plane>
<a-box position="0 0 0.15" width="0.3" height="0.3" depth="0.3" color="#ff5252"></a-box>
</a-entity>
<!-- Second image, when more than one was picked. -->
<a-entity tempusxr-image-target="index: 1">
<a-box position="0 0 0.15" width="0.3" height="0.3" depth="0.3" color="#69f0ae"></a-box>
</a-entity>
</a-scene>
<script src="page.js"></script>
</body>
</html>
body {
margin: 0;
font:
16px/1.5 system-ui,
sans-serif;
}
#picker {
position: fixed;
z-index: 20;
inset: 0 0 auto 0;
padding: 12px 16px;
background: rgba(0, 0, 0, 0.75);
color: #fff;
}
#picker.hidden {
display: none;
}
#picker input,
#picker button {
font: inherit;
}
#picker label {
display: block;
margin: 6px 0;
}
.my-pill {
padding: 6px 14px;
border-radius: 999px;
background: rgba(0, 0, 0, 0.55);
color: #fff;
font-weight: 600;
}
.my-hint {
padding: 8px 18px;
border-radius: 999px;
background: rgba(0, 0, 0, 0.55);
color: #fff;
font-weight: 600;
}
const scene = document.querySelector('a-scene');
const status = document.getElementById('status');
function begin(url) {
scene.setAttribute('tempusxr', 'targets', url);
scene.components.tempusxr.start();
document.getElementById('picker').classList.add('hidden');
}
async function toTargetUrl(files) {
const txr = files.find((file) => file.name.endsWith('.txr'));
if (txr) return URL.createObjectURL(txr);
const sources = [];
for (const file of files) {
sources.push({
name: file.name.replace(/\.[^.]+$/, ''),
image: await createImageBitmap(file),
});
}
const result = await TempusxrTargetCompiler.compileTargets(sources, {
onProgress: (p) => (status.textContent = 'Compiling targets: ' + Math.round(p) + '%'),
});
return URL.createObjectURL(new Blob([result.txr]));
}
document.getElementById('start').addEventListener('click', async () => {
const files = [...document.getElementById('files').files];
if (files.length === 0) {
status.textContent = 'Pick at least one image or a .txr file first.';
return;
}
try {
begin(await toTargetUrl(files));
} catch (error) {
status.textContent = 'Error: ' + (error.message || error);
}
});
// ?ui=custom uses the overlay above instead of the built-in one, and shows how to react to it.
if (new URLSearchParams(location.search).get('ui') === 'custom') {
document.body.append(document.getElementById('custom-ui').content);
scene.setAttribute('tempusxr', 'overlay', '#my-ui');
scene.addEventListener('tempusxr-ready', (event) => {
const overlay = event.detail.overlay;
let score = 0;
document.getElementById('my-ui').addEventListener('click', (click) => {
const action = click.target.closest('[data-tx-action]')?.dataset.txAction;
if (action === 'boost' || action === 'play') {
score += 10;
overlay.set('score', score);
overlay.toast('+10', { color: '#fecb02' });
}
});
});
}
const txrParam = new URLSearchParams(location.search).get('txr');
if (txrParam) {
const go = () => begin(txrParam);
if (scene.hasLoaded) go();
else scene.addEventListener('loaded', go, { once: true });
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
<title>Tempusxr image tracking</title>
<link rel="stylesheet" href="page.css" />
<!-- Same files a page without a bundler would load. Needs pnpm build. -->
<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",
"@tempusxr/target-compiler": "/packages/target-compiler/dist/index.js"
}
}
</script>
</head>
<body>
<div id="ar"></div>
<div id="panel">
<div class="setup">
<strong>Image tracking</strong>
<label>
Target images, or a compiled .txr file
<input id="files" type="file" accept="image/*,.txr" multiple />
</label>
<label>
Targets followed at once
<input id="max" type="number" min="1" max="8" value="1" style="width: 4em" />
</label>
<button id="start">Start</button>
</div>
<div id="status">Pick one or more images (or a .txr), then Start.</div>
<div id="rate"></div>
<ul id="targets"></ul>
</div>
<script type="module" src="page.js"></script>
</body>
</html>
html,
body {
margin: 0;
height: 100%;
background: #111;
color: #fff;
font:
16px/1.5 system-ui,
sans-serif;
}
#ar {
position: fixed;
inset: 0;
}
#panel {
position: fixed;
z-index: 2;
inset: 0 0 auto 0;
padding: 12px 16px;
background: rgba(0, 0, 0, 0.75);
}
#panel.running {
display: none;
}
/* Status text moves into the overlay's top-left slot once tracking runs. */
[data-tx-slot='top-left'] {
max-width: 100%;
padding: 6px 10px;
border-radius: 10px;
background: rgba(0, 0, 0, 0.5);
font-size: 13px;
line-height: 1.4;
overflow-wrap: anywhere;
}
#panel.running .setup {
display: none;
}
button,
input {
font: inherit;
}
label {
display: block;
margin: 6px 0;
}
ul {
margin: 6px 0 0;
padding-left: 18px;
}
.found {
color: #5ef28a;
}
.lost {
color: #aaa;
}
.bad {
color: #ff8a80;
}
import * as THREE from 'three';
import { create } from '@tempusxr/sdk-three';
import '@tempusxr/sdk-tracker-image';
import { compileTargets, loadImage } from '@tempusxr/target-compiler';
const $ = (id) => document.getElementById(id);
// Try smoothing values from the address, for example ?minCutOff=1&beta=4 or ?smoothing=off
function smoothingOptions() {
const params = new URLSearchParams(location.search);
if (params.get('smoothing') === 'off') return false;
const options = {};
if (params.has('minCutOff')) options.minCutOff = Number(params.get('minCutOff'));
if (params.has('beta')) options.beta = Number(params.get('beta'));
return options;
}
// Camera image size the tracker works on. Smaller is faster: try ?res=1280x720 to compare.
function cameraSize() {
const match = /^(\d+)x(\d+)$/.exec(
new URLSearchParams(location.search).get('res') ?? '640x480',
);
const [width, height] = match ? [Number(match[1]), Number(match[2])] : [640, 480];
// ?facing=user tries the front camera (shown mirrored, like a mirror).
const facing =
new URLSearchParams(location.search).get('facing') === 'user' ? 'user' : 'environment';
return { idealWidth: width, idealHeight: height, facing };
}
const colors = [
0xff5252, 0x40c4ff, 0x69f0ae, 0xffd740, 0xe040fb, 0xff6e40, 0x18ffff, 0xb2ff59,
];
async function readTxr(files) {
const txrFile = files.find((file) => file.name.endsWith('.txr'));
if (txrFile) return new Uint8Array(await txrFile.arrayBuffer());
const sources = [];
for (const file of files) {
sources.push({ name: file.name.replace(/\.[^.]+$/, ''), image: await loadImage(file) });
}
const result = await compileTargets(sources, {
onProgress: (p) =>
($('status').textContent = 'Compiling targets: ' + Math.round(p) + '%'),
});
window.__quality = result.quality;
return result.txr;
}
$('start').addEventListener('click', async () => {
const files = [...$('files').files];
if (files.length === 0) {
$('status').textContent = 'Pick at least one image or a .txr file first.';
return;
}
$('start').disabled = true;
try {
const txr = await readTxr(files);
$('status').textContent = 'Loading the tracker...';
const ar = create({ container: '#ar', cameraOptions: cameraSize() });
window.__ar = ar;
// The overlay has a slot for your own content: the status text goes there.
ar.ui?.root
.querySelector('[data-tx-slot="top-left"]')
?.append($('status'), $('rate'), $('targets'));
const tracker = await ar.use('image', {
targets: txr,
maxTrack: Number($('max').value) || 1,
smoothing: smoothingOptions(),
});
const list = $('targets');
const items = new Map();
tracker.anchors.forEach((anchor, index) => {
const { width, height } = anchor.size;
const group = new THREE.Group();
const plane = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
new THREE.MeshBasicMaterial({
color: colors[index % colors.length],
transparent: true,
opacity: 0.25,
side: THREE.DoubleSide,
}),
);
const outline = new THREE.LineSegments(
new THREE.EdgesGeometry(plane.geometry),
new THREE.LineBasicMaterial({ color: 0xffffff }),
);
const cube = new THREE.Mesh(
new THREE.BoxGeometry(width / 4, width / 4, width / 4),
new THREE.MeshNormalMaterial(),
);
cube.position.z = width / 8;
group.add(plane, outline, cube);
ar.attach(anchor, group);
const item = document.createElement('li');
item.textContent = anchor.id + ' - looking';
item.className = 'lost';
list.append(item);
items.set(anchor, item);
});
ar.on('found', (anchor) => {
items.get(anchor).textContent = anchor.id + ' - found';
items.get(anchor).className = 'found';
});
ar.on('lost', (anchor) => {
items.get(anchor).textContent = anchor.id + ' - lost';
items.get(anchor).className = 'lost';
});
ar.on('error', (error) => {
$('status').textContent = 'Error: ' + (error.code || error.message);
$('status').className = 'bad';
});
// Pose updates per second: how often the tracker really gives a new pose.
let updates = 0;
ar.on('update', () => updates++);
setInterval(() => {
$('rate').textContent = 'pose updates per second: ' + updates;
updates = 0;
}, 1000);
await ar.start();
$('panel').classList.add('running');
$('status').textContent = 'Point the camera at a target image.';
} catch (error) {
$('status').textContent = 'Error: ' + (error.code || error.message || error);
$('status').className = 'bad';
$('start').disabled = false;
}
});