- Documentation
- Exemples de suivi d'images
Interactif
Touchez le contenu 3D. Touchez le cube pour changer sa couleur, touchez la balle pour la faire sauter. Le toucher traverse le canvas transparent jusqu'aux objets posés sur l'image.
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.
A-Frame
Ajoutez cursor="rayOrigin: mouse" et raycaster="objects: .clickable" à la scène, et la classe clickable à ce qui peut être touché. Un toucher envoie un événement click à l'entité, comme pour tout élément.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
<title>Interactive (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>
</head>
<body>
<!-- Show poster.png, then tap the yellow cube (new color) or the blue ball (it jumps). -->
<a-scene
tempusxr="tracker: image; targets: /examples/poster-video/poster.txr"
cursor="rayOrigin: mouse"
raycaster="objects: .clickable"
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
id="cube"
class="clickable"
position="-0.2 0 0.15"
scale="0.25 0.25 0.25"
color="#fecb02"
></a-box>
<a-sphere
id="ball"
class="clickable"
position="0.2 0 0.15"
radius="0.13"
color="#6ec1ff"
></a-sphere>
</a-entity>
</a-scene>
<script>
const colors = ['#fecb02', '#ff5e62', '#8b4bb5', '#3d7a68'];
let next = 1;
document.getElementById('cube').addEventListener('click', (event) => {
event.target.setAttribute('color', colors[next++ % colors.length]);
});
document.getElementById('ball').addEventListener('click', (event) => {
event.target.setAttribute(
'animation__jump',
'property: position; from: 0.2 0 0.15; to: 0.2 0 0.45; dir: alternate; dur: 250; loop: 2; easing: easeOutQuad',
);
});
</script>
</body>
</html>
Three.js
Un toucher est un rayon de la caméra à travers ce point de l'écran : raycaster.setFromCamera(pointeur, ar.stage.camera), puis intersectObjects() dit quel objet est touché.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
<title>Interactive (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;
}
</style>
</head>
<body>
<!-- Show poster.png, then tap the yellow cube (new color) or the blue ball (it jumps). -->
<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' });
window.__ar = ar;
const tracker = await ar.use('image', { targets: ['/examples/poster-video/poster.txr'] });
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 cube = new THREE.Mesh(
new THREE.BoxGeometry(0.25, 0.25, 0.25),
new THREE.MeshStandardMaterial({ color: '#fecb02' }),
);
cube.position.set(-0.2, 0, 0.15);
const ball = new THREE.Mesh(
new THREE.SphereGeometry(0.13, 32, 16),
new THREE.MeshStandardMaterial({ color: '#6ec1ff' }),
);
ball.position.set(0.2, 0, 0.15);
content.add(cube, ball);
ar.attach(tracker.anchors[0], content);
// A tap is a ray from the camera through that point of the screen.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const canvas = ar.stage.renderer.domElement;
const colors = ['#fecb02', '#ff5e62', '#8b4bb5', '#3d7a68'];
let next = 1;
let jumpStart = -1;
canvas.addEventListener('pointerdown', (event) => {
const box = canvas.getBoundingClientRect();
pointer.set(
((event.clientX - box.left) / box.width) * 2 - 1,
-((event.clientY - box.top) / box.height) * 2 + 1,
);
raycaster.setFromCamera(pointer, ar.stage.camera);
const hit = raycaster.intersectObjects([cube, ball])[0]?.object;
if (hit === cube) cube.material.color.set(colors[next++ % colors.length]);
if (hit === ball) jumpStart = performance.now();
});
ar.session.onFrame(() => {
const t = (performance.now() - jumpStart) / 500;
ball.position.z = jumpStart > 0 && t < 1 ? 0.15 + Math.sin(t * Math.PI) * 0.3 : 0.15;
});
await ar.start();
</script>
</body>
</html>
À retenir
- L'overlay laisse passer les touchers partout sauf sur ses propres boutons : la scène 3D peut donc être touchée.
- Sur ordinateur, passez la souris sur l'objet avant de cliquer.
