- Documentation
- Bibliothèque d'expériences
- Arbre dans un cadre
Arbre dans un cadre Suivi d'images
Un arbre 3D pousse dans un cadre en bois sur l'image.
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 tree in a frame (A-Frame)</title>
<!-- Plain HTML, no JavaScript framework: A-Frame, Tempusxr, the image tracker. 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>
<script src="page.js"></script>
<link rel="stylesheet" href="page.css" />
</head>
<body>
<!-- Open tree.png on a screen (or print it), point the camera at it: a wooden frame appears around
the picture and a 3D tree grows in it. The buttons on the right resize, turn and reset it. -->
<a-scene
tempusxr="tracker: image; targets: /examples/tree-frame/tree.txr"
renderer="alpha: true"
vr-mode-ui="enabled: false"
>
<a-camera
position="0 0 0"
look-controls="enabled: false"
wasd-controls="enabled: false"
></a-camera>
<a-light type="ambient" intensity="1.3"></a-light>
<a-light type="directional" position="-0.5 0.6 1" intensity="1.6"></a-light>
<!-- Everything is in image units: the picture is 1 wide and 1 high, +Z comes out of it. -->
<a-entity tempusxr-image-target="index: 0">
<!-- The frame: four wooden sides around the picture, a little deep. -->
<a-entity>
<a-box
position="0 0.535 0.04"
width="1.14"
height="0.07"
depth="0.08"
material="color: #8b5a2b; roughness: 0.85; flatShading: true"
></a-box>
<a-box
position="0 -0.535 0.04"
width="1.14"
height="0.07"
depth="0.08"
material="color: #8b5a2b; roughness: 0.85; flatShading: true"
></a-box>
<a-box
position="-0.535 0 0.04"
width="0.07"
height="1"
depth="0.08"
material="color: #8b5a2b; roughness: 0.85; flatShading: true"
></a-box>
<a-box
position="0.535 0 0.04"
width="0.07"
height="1"
depth="0.08"
material="color: #8b5a2b; roughness: 0.85; flatShading: true"
></a-box>
</a-entity>
<!-- The tree stands up along the height of the picture, a little in front of it. -->
<a-entity position="0 -0.4 0.14">
<a-entity tree-grow>
<a-cylinder
position="0 0.125 0"
radius-top="0.04"
radius-bottom="0.055"
height="0.25"
segments-radial="8"
material="color: #6b4a2f; roughness: 0.85; flatShading: true"
></a-cylinder>
<a-cone
position="0 0.38 0"
radius-bottom="0.27"
radius-top="0"
height="0.34"
segments-radial="9"
material="color: #2f7a3d; roughness: 0.85; flatShading: true"
></a-cone>
<a-cone
position="0 0.57 0"
radius-bottom="0.21"
radius-top="0"
height="0.3"
segments-radial="9"
material="color: #3f9a4a; roughness: 0.85; flatShading: true"
></a-cone>
<a-cone
position="0 0.75 0"
radius-bottom="0.15"
radius-top="0"
height="0.27"
segments-radial="9"
material="color: #58b85c; roughness: 0.85; flatShading: true"
></a-cone>
<a-sphere
position="0.12 0.34 0.18"
radius="0.02"
material="color: #e63946; roughness: 0.85; flatShading: true"
></a-sphere>
<a-sphere
position="-0.1 0.4 0.15"
radius="0.02"
material="color: #e63946; roughness: 0.85; flatShading: true"
></a-sphere>
<a-sphere
position="0.04 0.55 0.15"
radius="0.02"
material="color: #e63946; roughness: 0.85; flatShading: true"
></a-sphere>
<a-sphere
position="-0.05 0.7 0.1"
radius="0.02"
material="color: #e63946; roughness: 0.85; flatShading: true"
></a-sphere>
</a-entity>
</a-entity>
</a-entity>
</a-scene>
</body>
</html>
body {
margin: 0;
}
// The tree grows over a second each time the picture is found, then sways in the wind.
// Registered before the scene, so the tag below finds it.
AFRAME.registerComponent('tree-grow', {
init() {
this.growth = 0;
this.el.object3D.scale.setScalar(0.001);
const target = this.el.parentNode.parentNode;
target.addEventListener('targetFound', () => {
this.growth = 0;
this.el.sceneEl.components.tempusxr?.state.overlay?.toast('A tree grows');
});
},
tick(time) {
const target = this.el.parentNode.parentNode;
if (!target.object3D.visible) return;
this.growth = Math.min(1, this.growth + 1 / 60);
const eased = 1 - (1 - this.growth) ** 3;
this.el.object3D.scale.setScalar(Math.max(0.001, eased));
this.el.object3D.rotation.z = Math.sin(time / 700) * 0.04 * this.growth;
this.el.object3D.rotation.y = Math.cos(time / 900) * 0.15 * this.growth;
},
});
<!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 tree in a frame</title>
<!-- Three.js with the SDK, as a page without a bundler would load it. 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"
}
}
</script>
<link rel="stylesheet" href="page.css" />
</head>
<body>
<!-- Open tree.png on a screen (or print it), point the camera at it: a wooden frame appears around
the picture and a 3D tree grows in it. The buttons on the right resize, turn and reset it. -->
<div id="ar"></div>
<script type="module" src="page.js"></script>
</body>
</html>
html,
body {
margin: 0;
height: 100%;
background: #111;
}
import * as THREE from 'three';
import { create } from '@tempusxr/sdk-three';
import '@tempusxr/sdk-tracker-image';
const material = (color) =>
new THREE.MeshStandardMaterial({ color, roughness: 0.85, flatShading: true });
// Everything is in image units: the picture is 1 wide and 1 high, +Z comes out of it.
const lay = (geometry, color, x, y, z) => {
const mesh = new THREE.Mesh(geometry, material(color));
mesh.position.set(x, y, z);
return mesh;
};
// The frame: four wooden sides around the picture, a little deep.
const frame = new THREE.Group();
const T = 0.07; // width of the frame
const D = 0.08; // depth
for (const [w, h, x, y] of [
[1 + 2 * T, T, 0, 0.5 + T / 2],
[1 + 2 * T, T, 0, -0.5 - T / 2],
[T, 1, -0.5 - T / 2, 0],
[T, 1, 0.5 + T / 2, 0],
]) {
frame.add(lay(new THREE.BoxGeometry(w, h, D), '#8b5a2b', x, y, D / 2));
}
// The tree stands up along the height of the picture, a little in front of it, so it looks like a
// tree growing in the frame. Its base is at the origin of this group, which makes it grow from the ground.
const tree = new THREE.Group();
tree.add(lay(new THREE.CylinderGeometry(0.04, 0.055, 0.25, 8), '#6b4a2f', 0, 0.125, 0));
[
[0.27, 0.34, 0.38, '#2f7a3d'],
[0.21, 0.3, 0.57, '#3f9a4a'],
[0.15, 0.27, 0.75, '#58b85c'],
].forEach(([radius, height, y, color]) => {
tree.add(lay(new THREE.ConeGeometry(radius, height, 9), color, 0, y, 0));
});
for (const [x, y, z] of [
[0.12, 0.34, 0.18],
[-0.1, 0.4, 0.15],
[0.04, 0.55, 0.15],
[-0.05, 0.7, 0.1],
]) {
tree.add(lay(new THREE.SphereGeometry(0.02, 8, 6), '#e63946', x, y, z));
}
tree.position.set(0, -0.4, 0.14);
const content = new THREE.Group(); // follows the picture; the SDK moves this one
content.add(frame, tree);
content.add(new THREE.AmbientLight(0xffffff, 1.3));
const sun = new THREE.DirectionalLight(0xffffff, 1.6);
sun.position.set(-0.5, 0.6, 1);
content.add(sun);
const ar = create({ container: '#ar', cameraOptions: { idealWidth: 640, idealHeight: 480 } });
window.__ar = ar;
const tracker = await ar.use('image', { targets: ['tree.txr'] });
ar.attach(tracker.anchors[0], content);
// The tree grows over a second each time the picture is found, then sways in the wind.
let growth = 0;
tree.scale.setScalar(0.001);
ar.on('found', () => {
growth = 0;
ar.ui?.toast('A tree grows');
});
ar.session.onFrame((time) => {
if (!tracker.anchors[0]?.visible) return;
growth = Math.min(1, growth + 1 / 60);
tree.scale.setScalar(Math.max(0.001, 1 - (1 - growth) ** 3));
tree.rotation.z = Math.sin(time / 700) * 0.04 * growth;
tree.rotation.y = Math.cos(time / 900) * 0.15 * growth;
});
await ar.start();