Tempusxr English
  1. Documentation
  2. Bibliothèque d'expériences
  3. Du plan à la pièce

Du plan à la pièce Suivi d'images

Les pièces s'élèvent d'un plan. Redimensionnez et tournez-les avec les boutons.

Ouvrir en A-FrameOuvrir en Three.jsRetour à la bibliothèque d'expériences

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 plan to room (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 plan.png on a screen (or print it), point the camera at it: the rooms rise from the drawing.
         The buttons on the right resize, turn and reset the room. The rooms come from layout.json. -->
    <a-scene
      tempusxr="tracker: image; targets: /examples/plan-to-room/plan.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.2"></a-light>
      <a-light type="directional" position="-0.4 -0.6 1" intensity="1.6"></a-light>
      <a-entity tempusxr-image-target="index: 0">
        <a-entity id="room" room-rise></a-entity>
      </a-entity>
    </a-scene>

  </body>
</html>
body {
  margin: 0;
}
// The walls rise over a second each time the plan is found. Registered before the scene.
AFRAME.registerComponent('room-rise', {
  init() {
    this.rise = 0;
    this.el.object3D.scale.z = 0.001;
    const target = this.el.parentNode;
    target.addEventListener('targetFound', () => (this.rise = 0));
  },
  tick() {
    if (!this.el.parentNode.object3D.visible) return;
    this.rise = Math.min(1, this.rise + 1 / 60);
    this.el.object3D.scale.z = Math.max(0.001, 1 - (1 - this.rise) ** 3);
  },
});

const sceneEl = document.querySelector('a-scene');
const build = async () => {
  const layout = await (await fetch('/examples/plan-to-room/layout.json')).json();
  const { width: W, height: H } = layout.size;
  // The anchor is the image, 1 unit wide, centered. A point of the drawing (x, y in pixels)
  // is at ((x - W / 2) / W, (H / 2 - y) / W), and height goes toward the viewer (+Z).
  const room = document.getElementById('room');
  const box = (rect, height, color, lift = 0) => {
    const el = document.createElement('a-box');
    const cx = (rect.x + rect.w / 2 - W / 2) / W;
    const cy = (H / 2 - (rect.y + rect.h / 2)) / W;
    el.setAttribute('position', `${cx} ${cy} ${lift / W + height / (2 * W)}`);
    el.setAttribute('width', rect.w / W);
    el.setAttribute('height', rect.h / W);
    el.setAttribute('depth', height / W);
    el.setAttribute('material', `color: ${color}; roughness: 0.8`);
    room.appendChild(el);
  };
  for (const r of layout.rooms) box(r, 1, r.floor);
  for (const wall of layout.walls) box(wall, layout.wallHeight, '#f4f1ea');
  for (const item of layout.furniture) box(item, item.height, item.color, 1);
};
if (sceneEl.hasLoaded) build();
else sceneEl.addEventListener('loaded', build);
<!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 plan to room</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 plan.png on a screen (or print it), point the camera at it: the rooms rise from the drawing.
         The buttons on the right resize, turn and reset the room. -->
    <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 layout = await (await fetch('layout.json')).json();
const { width: W } = layout.size;

// The anchor is the image, 1 unit wide, centered. A point of the drawing (x, y in pixels)
// is at ((x - W / 2) / W, (H / 2 - y) / W), and height goes toward the viewer (+Z).
const point = (x, y) => [(x - layout.size.width / 2) / W, (layout.size.height / 2 - y) / W];
const box = (rect, height, color, lift = 0) => {
  const [cx, cy] = point(rect.x + rect.w / 2, rect.y + rect.h / 2);
  const mesh = new THREE.Mesh(
    new THREE.BoxGeometry(rect.w / W, rect.h / W, height / W),
    new THREE.MeshStandardMaterial({ color, roughness: 0.8 }),
  );
  mesh.position.set(cx, cy, lift / W + height / (2 * W));
  return mesh;
};

const room = new THREE.Group(); // grows from the drawing
for (const r of layout.rooms) room.add(box(r, 1, r.floor));
for (const wall of layout.walls) room.add(box(wall, layout.wallHeight, '#f4f1ea'));
for (const item of layout.furniture) room.add(box(item, item.height, item.color, 1));

const content = new THREE.Group(); // follows the image; the SDK moves this one
content.add(room);
content.add(new THREE.AmbientLight(0xffffff, 1.2));
const sun = new THREE.DirectionalLight(0xffffff, 1.6);
sun.position.set(-0.4, -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: ['plan.txr'] });
ar.attach(tracker.anchors[0], content);

// The walls rise over a second each time the plan is found.
let rise = 0;
room.scale.z = 0.001;
ar.on('found', () => {
  rise = 0;
  ar.ui?.toast('Plan found');
});
ar.session.onFrame(() => {
  if (!tracker.anchors[0]?.visible) return;
  rise = Math.min(1, rise + 1 / 60);
  const eased = 1 - (1 - rise) ** 3;
  room.scale.z = Math.max(0.001, eased);
});

await ar.start();

Ouvrir sur votre téléphone

Scannez ce code avec l'appareil photo du téléphone.

Ouvrir sur cet appareil