Skip to content

Single place to convert your 2D floor plans to 3d model (no LLM)

Python

Automatic tour video: walk every room by itself and record it (G)

23f2005639338b0d278135
diff --git a/README.md b/README.md
index 876d035..02f8963 100644
--- a/README.md
+++ b/README.md
@@ -91,6 +91,7 @@ python server.py
| **Map** (M) | Room names, stairs, where you stand and look |
| **Screenshot** (P) | PNG of about 4K size |
| **Record** (R) | Video of your walk as MP4 (browsers that record WebM are converted on the server; needs ffmpeg) |
+| **Tour** (G) | Walks itself from the entrance through every room and up the stairs, looks around in each, and records it as one video. Any key or click stops it |
| **Your own furniture** | The Furniture models page shows each item and lets you replace it with your `.glb` model |
| **Sample plan, delete** | "Try the sample plan" on the home page; every plan in the list has Delete |
| **Blender file** | The house as one `.blend` with every texture packed inside, in a zip |
diff --git a/tests/test_walk.mjs b/tests/test_walk.mjs
index 9562691..092e667 100644
--- a/tests/test_walk.mjs
+++ b/tests/test_walk.mjs
@@ -1,5 +1,5 @@
// Walk test on a generated house: climb every storey's stairs (flight -> landing -> flight) and check we arrive
-// on the next floor; and check walls stop the walker. Run: node tests/test_walk.mjs jobs/<id>/meta.json
+// on the next floor; check walls stop the walker; and walk the automatic tour (walk.tourRoute) leg by leg. Run: node tests/test_walk.mjs jobs/<id>/meta.json
import fs from 'node:fs';
import * as walk from '../web/walk.js';
@@ -57,4 +57,24 @@ if (w) {
check(p.y < w[2], `wall at y=${w[2].toFixed(2)} stopped the walker at y=${p.y.toFixed(2)}`);
}
}
+
+// the automatic tour: each leg arrives by stepping (the viewer steps the same way), every stop is in its room, every
+// upper storeys are reached when there are stairs
+const route = walk.tourRoute(meta), stops = route.filter((w) => w.room);
+let stuck = null;
+for (let i = 1; i < route.length && !stuck; i++) {
+ const p = { ...route[i - 1] };
+ for (let n = 0; n < 2000 && Math.hypot(route[i].x - p.x, route[i].y - p.y) >= 0.03; n++) walk.stepTo(meta, p, route[i].x, route[i].y);
+ if (Math.hypot(route[i].x - p.x, route[i].y - p.y) >= 0.03 || Math.abs(p.z - route[i].z) > 0.05) stuck = `leg ${i} stopped at ${p.x.toFixed(2)}, ${p.y.toFixed(2)}`;
+}
+check(route.length > 1 && !stuck, `tour: ${route.length} waypoints walked without getting stuck${stuck ? ` (${stuck})` : ''}`);
+check(stops.every((w) => meta.floors[w.level].rooms.some((r) => r.label === w.room && r.rects.some((q) => walk.inRect(q, w.x, w.y)))
+ && Math.abs(w.z - meta.floors[w.level].z) < 0.01), `tour: all ${stops.length} stops stand in their room`);
+const worth = meta.floors.flatMap((f) => f.rooms.filter((r) => !['open', 'duct', 'stair'].includes(r.type)
+ && r.rects.reduce((a, q) => a + (q[1] - q[0]) * (q[3] - q[2]), 0) >= 2));
+// (not a failure: a room with no door drawn cannot be walked into)
+console.log(`${stops.length === worth.length ? 'ok ' : 'note'} tour: looked into ${stops.length} of ${worth.length} rooms`);
+meta.floors.forEach((f, k) => {
+ if (k && meta.floors[k - 1].stairs?.length) check(stops.some((w) => w.level === k), `tour: went up to ${f.name}`);
+});
process.exit(fails ? 1 : 0);
diff --git a/web/view.html b/web/view.html
index b477759..6c43013 100644
--- a/web/view.html
+++ b/web/view.html
@@ -43,12 +43,12 @@
<h1>3D walkthrough</h1>
<p id="status">Loading…</p>
<p class="desk"><kbd>W A S D</kbd> walk · <kbd>Mouse</kbd> look · <kbd>Shift</kbd> run · <kbd>T</kbd> top view · <kbd>Esc</kbd> pause</p>
- <p class="desk"><kbd>P</kbd> screenshot · <kbd>R</kbd> start / stop recording · <kbd>M</kbd> map</p>
+ <p class="desk"><kbd>P</kbd> screenshot · <kbd>R</kbd> start / stop recording · <kbd>M</kbd> map · <kbd>G</kbd> guided tour (recorded)</p>
<p class="touch">Left thumb: move · right thumb: look</p>
</div>
<div id="room"></div>
<div id="cross"></div>
-<div id="bar"><button id="topbtn">Top view</button><span id="floors"></span><button id="shot" title="Save a picture of this view (P)">Screenshot</button><button id="rec" title="Record a video of your walk (R)">Record</button><button id="blend" title="Download this house as a Blender file (.zip)">Blender file</button><a href="/" id="newplan" style="color:#ccc;font-size:13px">Home</a><span id="fps"></span></div>
+<div id="bar"><button id="topbtn">Top view</button><span id="floors"></span><button id="shot" title="Save a picture of this view (P)">Screenshot</button><button id="rec" title="Record a video of your walk (R)">Record</button><button id="tour" title="Walk through every room by itself and record it as a video where this browser can record (G); any key or click stops">Tour</button><button id="blend" title="Download this house as a Blender file (.zip)">Blender file</button><a href="/" id="newplan" style="color:#ccc;font-size:13px">Home</a><span id="fps"></span></div>
<script type="module" src="/web/view.js"></script>
</body>
</html>
diff --git a/web/view.js b/web/view.js
index cda1c98..eb4a326 100644
--- a/web/view.js
+++ b/web/view.js
@@ -93,7 +93,9 @@ const p = { ...walk.entrance(meta), z: floors[0].z, yaw: 0, pitch: 0 };
const keys = {};
let locked = false, topView = false;
addEventListener('keydown', (e) => {
+ if (tour) { if (!e.repeat) stopTour(); return; } // any key ends the tour (a held G only repeats)
keys[e.code] = true;
+ if (e.code === 'KeyG' && !e.repeat) startTour();
if (e.code === 'KeyT') toggleTop();
if (e.code === 'KeyP' && !e.repeat) screenshot();
if (e.code === 'KeyR' && !e.repeat) toggleRec();
@@ -108,12 +110,13 @@ $('overlay').addEventListener('click', () => {
});
document.addEventListener('pointerlockchange', () => {
locked = document.pointerLockElement === canvas;
- $('overlay').style.display = locked || topView ? 'none' : 'flex';
+ $('overlay').style.display = locked || topView || tour ? 'none' : 'flex';
+ if (locked && tour) document.exitPointerLock(); // (a lock asked for by leaving the top view lands late)
});
// the browser refused to capture the mouse: show the start screen, whose click captures it
document.addEventListener('pointerlockerror', () => { if (!topView) $('overlay').style.display = 'flex'; });
addEventListener('mousemove', (e) => {
- if (!locked) return;
+ if (!locked || tour) return;
p.yaw -= e.movementX * 0.0022;
p.pitch = Math.max(-1.4, Math.min(1.4, p.pitch - e.movementY * 0.0022));
});
@@ -295,6 +298,67 @@ function toggleRec() {
$('rec').classList.add('live');
label();
}
+// ---- automatic tour (G): the route of walk.tourRoute, stepped like its test, recorded as one video
+const LOOK = 5; // seconds of looking around in each room
+let tour = null, route = null;
+function startTour() {
+ if (topView) toggleTop();
+ route ||= walk.tourRoute(meta); // it depends on the house only
+ if (route.length < 2) return;
+ document.exitPointerLock?.(); // the mouse would fight the camera
+ $('overlay').style.display = 'none';
+ Object.assign(p, { x: route[0].x, y: route[0].y, z: route[0].z, pitch: 0 });
+ tour = { route, i: 0, n: 0, budget: 0, look: -1, yaw: 0, cam: null, rec: !rec };
+ if (tour.rec) toggleRec();
+ $('tour').textContent = 'Stop tour';
+ $('tour').classList.add('live');
+}
+function stopTour() {
+ if (!tour) return;
+ if (tour.rec && rec) toggleRec(); // stopping saves the video
+ tour = null;
+ $('tour').textContent = 'Tour';
+ $('tour').classList.remove('live');
+ $('overlay').style.display = locked || topView ? 'none' : 'flex';
+}
+function tourStep(dt) {
+ const w = tour.route[tour.i];
+ tour.cam = null;
+ if (tour.look >= 0) { // pan left, right and back to the heading, like tools/tour.py films it
+ const t = Math.min(1, tour.look += dt / LOOK);
+ p.yaw = tour.yaw + 1.2 * Math.sin(2 * Math.PI * t * t * (3 - 2 * t));
+ if (t === 1) { tour.look = -1; if (++tour.i === tour.route.length) stopTour(); }
+ return;
+ }
+ const head = Math.atan2(p.x - w.x, w.y - p.y), turn = Math.atan2(Math.sin(head - p.yaw), Math.cos(head - p.yaw)); // shortest way round
+ p.yaw += turn * Math.min(1, 3 * dt); // eased turn towards the next waypoint
+ p.pitch *= 1 - Math.min(1, 3 * dt);
+ tour.budget += WALK * dt * Math.max(0, Math.cos(turn)); // slow down while turning; stand still to turn around
+ for (; tour.budget >= walk.SUB; tour.budget -= walk.SUB) {
+ if (Math.hypot(w.x - p.x, w.y - p.y) < 0.03) break;
+ const bx = p.x, by = p.y;
+ walk.stepTo(meta, p, w.x, w.y);
+ // shortcut: a stuck (or endlessly sliding) walker hops to the waypoint so the recording cannot run forever;
+ // tests/test_walk.mjs fails a route where that would happen
+ if ((p.x === bx && p.y === by) || ++tour.n > 2000) Object.assign(p, { x: w.x, y: w.y, z: w.z });
+ }
+ const d = Math.hypot(w.x - p.x, w.y - p.y);
+ if (d < 0.03) { // arrived: start the next leg exactly where the planner did
+ Object.assign(p, { x: w.x, y: w.y, z: w.z });
+ tour.budget = tour.n = 0;
+ if (w.room) { tour.look = 0; tour.yaw = p.yaw; } else tour.i++;
+ return;
+ }
+ // whole steps keep the walk exact; show the part of a step not yet taken so the camera glides
+ const r = Math.min(tour.budget, d) / d;
+ tour.cam = [p.x + (w.x - p.x) * r, p.y + (w.y - p.y) * r];
+}
+$('tour').onclick = startTour;
+// a click or touch ends the tour, and does nothing else (a click on Record must not start a second video)
+let eat = false;
+addEventListener('pointerdown', () => { eat = !!tour; stopTour(); }, true);
+addEventListener('click', (e) => { if (eat) { eat = false; e.stopPropagation(); } }, true);
+
$('blend').onclick = async () => { // the server packs the textures into one .blend (a few seconds, once per build)
const b = $('blend');
b.disabled = true;
@@ -321,7 +385,8 @@ renderer.setAnimationLoop(() => {
const raw = clock.getDelta(), dt = Math.min(raw, 0.05);
frames++; fpsT += raw;
if (fpsT > 0.5) { $('fps').textContent = `${Math.round(frames / fpsT)} fps`; frames = 0; fpsT = 0; }
- if (!topView) {
+ if (!topView && tour) tourStep(dt);
+ if (!topView && !tour) {
const sp = (keys.ShiftLeft || keys.ShiftRight ? RUN : WALK) * dt;
let f = 0, s = 0;
if (keys.KeyW || keys.ArrowUp) f += 1;
@@ -339,7 +404,10 @@ renderer.setAnimationLoop(() => {
const dy = (Math.cos(p.yaw) * f + Math.sin(p.yaw) * s) * k;
walk.move(meta, p, dx, dy);
}
- camera.position.copy(W(p.x, p.y, p.z + meta.eye));
+ }
+ if (!topView) {
+ const [cx, cy] = tour?.cam || [p.x, p.y];
+ camera.position.copy(W(cx, cy, p.z + meta.eye));
camera.rotation.set(0, 0, 0);
camera.rotation.order = 'YXZ';
camera.rotation.y = p.yaw;
diff --git a/web/walk.js b/web/walk.js
index 9cd1fe2..b9ac876 100644
--- a/web/walk.js
+++ b/web/walk.js
@@ -78,3 +78,95 @@ export function move(meta, p, dx, dy) {
}
return p;
}
+
+/** one tour step of at most SUB metres towards (tx, ty); the viewer and tests/test_walk.mjs step the same way, so a
+ * leg the planner checked is the leg the viewer walks */
+export const SUB = 0.04;
+export function stepTo(meta, p, tx, ty) {
+ const dx = tx - p.x, dy = ty - p.y, d = Math.hypot(dx, dy), s = Math.min(SUB, d);
+ if (d > 0) move(meta, p, dx / d * s, dy / d * s);
+ return p;
+}
+
+// walk from a to b by SUB steps: arrives (within 3 cm, on b's height) or not
+function reaches(meta, a, b) {
+ const p = { x: a.x, y: a.y, z: a.z };
+ for (let i = 0; i < 2000; i++) {
+ if (Math.hypot(b.x - p.x, b.y - p.y) < 0.03) return Math.abs(p.z - b.z) < 0.05;
+ const bx = p.x, by = p.y;
+ stepTo(meta, p, b.x, b.y);
+ if (p.x === bx && p.y === by) return false;
+ }
+ return false;
+}
+
+/** an automatic tour: from the entrance, room by room (the nearest next, the storey you are on first), through doors
+ * and up the stairs. -> waypoints [{x, y, z, room?, level?}]; a waypoint with a room is a stop to look around.
+ * Paths come from a breadth-first search on a 0.25 m grid that moves by walk.move's rules, then are straightened
+ * wherever stepping straight still arrives. The roof is left out: it is not a room. */
+export function tourRoute(meta) {
+ const C = 0.25, b = meta.bounds, x0 = b[0] - 1, y0 = b[2] - 1;
+ const ni = Math.ceil((b[1] - b[0] + 2) / C), nj = Math.ceil((b[3] - b[2] + 2) / C);
+ const nodes = new Map(), at = (i, j, z) => { // a grid cell at one walkable height (floors, stair steps)
+ const k = `${i},${j},${Math.round(z * 20)}`;
+ if (!nodes.has(k)) nodes.set(k, { i, j, x: x0 + (i + 0.5) * C, y: y0 + (j + 0.5) * C, z, next: null });
+ return nodes.get(k);
+ };
+ const next = (n) => n.next ||= [[1, 0], [-1, 0], [0, 1], [0, -1]].flatMap(([di, dj]) => {
+ const i = n.i + di, j = n.j + dj;
+ if (i < 0 || j < 0 || i >= ni || j >= nj) return [];
+ const x = x0 + (i + 0.5) * C, y = y0 + (j + 0.5) * C, g = groundNear(meta, x, y, n.z);
+ if (g === null || g > meta.roof_z - 0.1 || blockedAt(meta, levelOf(meta, Math.max(g, n.z) - 0.05), x, y)) return [];
+ return [at(i, j, g)];
+ });
+ const bfs = (s) => {
+ const from = new Map([[s, null]]), q = [s];
+ for (let h = 0; h < q.length; h++) for (const m of next(q[h])) if (!from.has(m)) { from.set(m, q[h]); q.push(m); }
+ return { from, order: q };
+ };
+ // start: the free cell nearest the entrance
+ const e = entrance(meta), z0 = meta.floors[0].z;
+ let start = null;
+ for (let i = 0; i < ni; i++) for (let j = 0; j < nj; j++) {
+ const x = x0 + (i + 0.5) * C, y = y0 + (j + 0.5) * C;
+ if (blockedAt(meta, 0, x, y) || groundNear(meta, x, y, z0) !== z0) continue;
+ if (!start || Math.hypot(x - e.x, y - e.y) < Math.hypot(start.x - e.x, start.y - e.y)) start = at(i, j, z0);
+ }
+ if (!start) return [];
+ // a stop in each room worth a look: the reachable cell on its floor nearest the middle of its biggest part
+ const reach = bfs(start).order, area = (q) => (q[1] - q[0]) * (q[3] - q[2]);
+ const stops = [];
+ meta.floors.forEach((f, lv) => {
+ for (const r of f.rooms) {
+ if (['open', 'duct', 'stair'].includes(r.type) || r.rects.reduce((a, q) => a + area(q), 0) < 2) continue;
+ const q = r.rects.reduce((a, c) => (area(c) > area(a) ? c : a)), cx = (q[0] + q[1]) / 2, cy = (q[2] + q[3]) / 2;
+ let best = null;
+ for (const n of reach) {
+ if (Math.abs(n.z - f.z) > 0.01 || !r.rects.some((c) => inRect(c, n.x, n.y))) continue;
+ if (!best || Math.hypot(n.x - cx, n.y - cy) < Math.hypot(best.x - cx, best.y - cy)) best = n;
+ }
+ if (best) stops.push({ node: best, room: r.label, level: lv });
+ }
+ });
+ const route = [{ x: start.x, y: start.y, z: start.z }];
+ let here = start, lv = 0;
+ while (stops.length) {
+ const { from, order } = bfs(here), rank = new Map(order.map((n, k) => [n, k]));
+ // nearest by walking distance (BFS order), finishing the storey you are on before taking the stairs
+ const key = (s) => (s.level === lv ? 0 : 1e9) + (rank.get(s.node) ?? 2e9);
+ const s = stops.reduce((a, c) => (key(c) < key(a) ? c : a));
+ stops.splice(stops.indexOf(s), 1);
+ if (!from.has(s.node)) continue; // reached from the start but not from here (one-way step): skip it
+ const path = [];
+ for (let n = s.node; n; n = from.get(n)) path.unshift(n);
+ for (let a = 0; a < path.length - 1;) { // straighten: go as far ahead as a straight walk still arrives
+ let k = a + 1;
+ while (k + 1 < path.length && reaches(meta, path[a], path[k + 1])) k++;
+ if (k < path.length - 1) route.push({ x: path[k].x, y: path[k].y, z: path[k].z });
+ a = k;
+ }
+ route.push({ x: s.node.x, y: s.node.y, z: s.node.z, room: s.room, level: s.level });
+ here = s.node; lv = s.level;
+ }
+ return route;
+}