Prompt specification
Build or customize the Three.js 3D scene "Balance outdoor seating with neighborhood passage" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Balance outdoor seating with neighborhood passage
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/balance-outdoor-seating-with-neighborhood-passage-x2868
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows Trastevere's squares and lanes in Rome. Let users rearrange temporary cafe tables in a schematic square. Highlight blocked residential entrances and compare available walking routes around the fixed public-space features. Voxel style.
Working Three.js Starter (balance-outdoor-seating-with-neighborhood-passage-x2868):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Trastevere Pedestrian Atlas</title>
<style>
:root {
--ink: #182321;
--panel: rgba(22, 30, 29, 0.94);
--panel-soft: rgba(255, 255, 255, 0.065);
--line: rgba(255, 255, 255, 0.13);
--text: #f5f1e8;
--muted: #aebbb6;
--terracotta: #e47c4c;
--gold: #efc367;
--green: #72d19b;
--blue: #70bde8;
--red: #ff655f;
--shadow: 0 18px 50px rgba(12, 20, 18, 0.24);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #bdc8c0;
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
#scene,
#labels,
#tooltip {
position: fixed;
inset: 0;
}
#scene {
z-index: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
#labels {
z-index: 3;
pointer-events: none;
overflow: hidden;
}
.map-label {
position: absolute;
left: 0;
top: 0;
white-space: nowrap;
transform: translate(-50%, -50%);
padding: 5px 8px;
border-radius: 5px;
color: #24302e;
background: rgba(249, 244, 228, 0.88);
border: 1px solid rgba(37, 49, 46, 0.2);
box-shadow: 0 4px 12px rgba(22, 30, 29, 0.12);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.055em;
text-transform: uppercase;
text-align: center;
text-shadow: 0 1px rgba(255, 255, 255, 0.45);
}
.map-label small {
display: block;
margin-top: 2px;
color: #66716d;
font-size: 8px;
font-weight: 750;
letter-spacing: 0.09em;
}
.map-label.square-label {
padding: 7px 10px;
color: #fffaf0;
background: rgba(28, 36, 34, 0.84);
border-color: rgba(255, 255, 255, 0.17);
text-shadow: none;
}
.map-label.entrance-label {
min-width: 80px;
color: #24503b;
background: rgba(231, 247, 236, 0.94);
border-color: rgba(44, 118, 80, 0.35);
font-size: 8px;
}
.map-label.entrance-label.blocked {
color: #fff;
background: rgba(210, 66, 60, 0.94);
border-color: rgba(255, 255, 255, 0.3);
}
.map-label.endpoint {
color: #fff;
background: rgba(27, 37, 34, 0.88);
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#header {
top: 18px;
left: 18px;
width: 340px;
padding: 16px 17px;
}
.eyebrow {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 7px;
color: var(--gold);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.location-pill {
padding: 4px 7px;
color: #d7e0dc;
border: 1px solid var(--line);
border-radius: 999px;
color: var(--muted);
font-size: 8px;
letter-spacing: 0.09em;
}
h1 {
margin: 0;
font-size: 23px;
line-height: 1.05;
letter-spacing: -0.035em;
}
.subtitle {
margin: 8px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.status-strip {
display: flex;
align-items: center;
gap: 8px;
margin-top: 13px;
padding-top: 11px;
border-top: 1px solid var(--line);
color: #d4ddd9;
font-size: 10px;
}
.status-dot {
width: 7px;
height: 7px;
flex: 0 0 auto;
border-radius: 2px;
background: var(--green);
box-shadow: 0 0 12px rgba(114, 209, 155, 0.6);
}
#control-panel {
top: 171px;
left: 18px;
width: 340px;
max-height: calc(100vh - 230px);
overflow: auto;
padding: 6px 16px 16px;
scrollbar-width: thin;
scrollbar-color: rgba(255, 255, 255, 0.2) transparent;
}
.section {
padding: 14px 0;
border-bottom: 1px solid var(--line);
}
.section:last-child {
border-bottom: 0;
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 10px;
}
.section-heading h2 {
margin: 0;
color: #eef2ee;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.105em;
text-transform: uppercase;
}
.count-badge {
min-width: 22px;
padding: 3px 7px;
border-radius: 999px;
color: var(--gold);
background: rgba(239, 195, 103, 0.12);
font-size: 9px;
font-weight: 800;
text-align: center;
}
.hint {
margin: -2px 0 10px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.legend-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
min-width: 0;
color: #d6dfdb;
font-size: 10px;
}
.swatch {
width: 12px;
height: 12px;
flex: 0 0 auto;
border-radius: 3px;
box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.14);
}
.swatch.table {
background: var(--terracotta);
}
.swatch.fixed {
background: #63a5a3;
}
.swatch.open {
background: var(--green);
}
.swatch.blocked {
background: var(--red);
}
.entrance-list {
display: grid;
gap: 7px;
}
.entrance-row {
display: grid;
grid-template-columns: 30px 1fr auto;
align-items: center;
gap: 8px;
padding: 8px 9px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 9px;
background: rgba(255, 255, 255, 0.035);
}
.entrance-row.is-blocked {
border-color: rgba(255, 101, 95, 0.38);
background: rgba(255, 101, 95, 0.08);
}
.entrance-icon {
display: grid;
width: 27px;
height: 27px;
place-items: center;
border-radius: 7px;
color: var(--green);
background: rgba(114, 209, 155, 0.1);
font-size: 13px;
font-weight: 900;
}
.is-blocked .entrance-icon {
color: var(--red);
background: rgba(255, 101, 95, 0.12);
}
.entrance-name {
color: #eef2ee;
font-size: 10px;
font-weight: 750;
}
.entrance-state {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 8px;
font-weight: 650;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.is-blocked .entrance-state {
color: #ff9d98;
}
.toggle {
position: relative;
width: 31px;
height: 18px;
flex: 0 0 auto;
}
.toggle input {
position: absolute;
opacity: 0;
pointer-events: none;
}
.toggle-track {
position: absolute;
inset: 0;
border-radius: 999px;
background: #52605c;
transition: 160ms ease;
}
.toggle-track::after {
content: "";
position: absolute;
top: 3px;
left: 3px;
width: 12px;
height: 12px;
border-radius: 50%;
background: white;
transition: 160ms ease;
}
.toggle input:checked + .toggle-track {
background: var(--red);
}
.toggle input:checked + .toggle-track::after {
transform: translateX(13px);
}
.button-row {
display: flex;
gap: 8px;
margin-top: 11px;
}
.button {
flex: 1;
min-height: 34px;
padding: 8px 10px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.055);
color: #e8eee9;
cursor: pointer;
font-size: 10px;
font-weight: 750;
transition:
background 140ms ease,
transform 140ms ease,
border-color 140ms ease;
}
.button:hover {
background: rgba(255, 255, 255, 0.1);
border-color: rgba(255, 255, 255, 0.23);
}
.button:active {
transform: translateY(1px);
}
.button.primary {
color: #211b16;
border-color: transparent;
background: var(--gold);
}
.button.primary:hover {
background: #f6d17e;
}
#route-panel {
top: 18px;
right: 18px;
width: 306px;
padding: 16px;
}
.route-intro {
margin: -2px 0 12px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.route-toggles {
display: flex;
gap: 14px;
margin-bottom: 10px;
}
.check-label {
display: inline-flex;
align-items: center;
gap: 6px;
color: #dce4e0;
font-size: 10px;
font-weight: 700;
cursor: pointer;
}
.check-label input {
accent-color: var(--gold);
}
.route-card {
width: 100%;
margin-top: 9px;
padding: 12px;
border: 1px solid rgba(255, 255, 255, 0.11);
border-radius: 11px;
background: rgba(255, 255, 255, 0.04);
color: inherit;
text-align: left;
cursor: pointer;
transition:
border-color 150ms ease,
background 150ms ease,
transform 150ms ease;
}
.route-card:hover {
transform: translateY(-1px);
background: rgba(255, 255, 255, 0.07);
}
.route-card.recommended {
border-color: rgba(239, 195, 103, 0.58);
background: rgba(239, 195, 103, 0.075);
}
.route-card-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
}
.route-name {
display: flex;
align-items: center;
gap: 8px;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.02em;
}
.route-letter {
display: grid;
width: 23px;
height: 23px;
place-items: center;
border-radius: 6px;
color: #14201c;
font-size: 10px;
font-weight: 900;
}
.route-letter.a {
background: var(--green);
}
.route-letter.b {
background: var(--blue);
}
.recommendation {
padding: 3px 6px;
border-radius: 999px;
color: var(--gold);
background: rgba(239, 195, 103, 0.12);
font-size: 7px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.route-description {
margin: 8px 0 10px 31px;
color: var(--muted);
font-size: 9px;
line-height: 1.35;
}
.route-metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-left: 31px;
}
.metric {
padding: 7px 8px;
border-radius: 7px;
background: rgba(0, 0, 0, 0.16);
}
.metric-value {
color: #f7f8f3;
font-size: 13px;
font-weight: 800;
}
.metric-label {
margin-top: 2px;
color: var(--muted);
font-size: 7px;
font-weight: 750;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.comparison {
margin-top: 11px;
padding: 9px 10px;
border-left: 2px solid var(--gold);
border-radius: 0 7px 7px 0;
color: #d6dfdb;
background: rgba(255, 255, 255, 0.035);
font-size: 9px;
line-height: 1.45;
}
.map-options {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: 11px;
padding-top: 11px;
border-top: 1px solid var(--line);
}
#bottom-hint {
position: fixed;
z-index: 8;
left: 50%;
bottom: 16px;
display: flex;
align-items: center;
gap: 10px;
padding: 9px 13px;
border: 1px solid rgba(255, 255, 255, 0.16);
border-radius: 999px;
color: #e8eee9;
background: rgba(24, 32, 30, 0.84);
box-shadow: 0 10px 30px rgba(20, 27, 25, 0.16);
backdrop-filter: blur(10px);
font-size: 9px;
white-space: nowrap;
transform: translateX(-50%);
}
.hint-key {
color: var(--gold);
font-weight: 800;
}
#toast {
position: fixed;
z-index: 30;
left: 50%;
top: 22px;
padding: 10px 14px;
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 9px;
color: #f6f8f4;
background: rgba(24, 32, 30, 0.94);
box-shadow: var(--shadow);
font-size: 10px;
font-weight: 700;
opacity: 0;
pointer-events: none;
transform: translate(-50%, -12px);
transition:
opacity 180ms ease,
transform 180ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#tooltip {
z-index: 25;
inset: auto;
width: max-content;
max-width: 220px;
padding: 7px 9px;
border-radius: 7px;
color: #fff;
background: rgba(20, 28, 26, 0.92);
box-shadow: 0 8px 24px rgba(20, 28, 26, 0.22);
font-size: 9px;
font-weight: 700;
pointer-events: none;
opacity: 0;
transform: translate(12px, 12px);
transition: opacity 100ms ease;
}
#tooltip.visible {
opacity: 1;
}
.footnote {
margin-top: 10px;
color: #7f8e89;
font-size: 8px;
line-height: 1.45;
}
@media (max-width: 1050px) {
#header,
#control-panel {
width: 300px;
}
#route-panel {
width: 278px;
}
}
@media (max-width: 760px) {
#header {
top: 10px;
left: 10px;
width: calc(100% - 20px);
padding: 12px 13px;
}
h1 {
font-size: 19px;
}
.subtitle {
display: none;
}
.status-strip {
margin-top: 9px;
padding-top: 8px;
}
#control-panel {
top: 119px;
left: 10px;
width: calc(100% - 20px);
max-height: 31vh;
padding: 4px 13px 10px;
}
#route-panel {
top: auto;
right: 10px;
bottom: 57px;
width: calc(100% - 20px);
max-height: 37vh;
overflow: auto;
padding: 13px;
}
#route-panel .route-intro,
#route-panel .map-options,
#route-panel .footnote {
display: none;
}
#route-panel .section-heading {
margin-bottom: 6px;
}
.route-card {
padding: 9px;
}
.route-description {
display: none;
}
.route-metrics {
margin-left: 31px;
}
#bottom-hint {
bottom: 10px;
max-width: calc(100% - 20px);
overflow: hidden;
text-overflow: ellipsis;
}
.hint-secondary {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="labels"></div>
<header id="header" class="panel">
<div class="eyebrow">
<span>Roma · Trastevere</span>
<span class="location-pill">41.889° N</span>
</div>
<h1>Pedestrian Atlas</h1>
<p class="subtitle">
Rearrange temporary café furniture, mark residential access closures,
and compare walking routes around the square’s fixed public-space features.
</p>
<div class="status-strip">
<span class="status-dot"></span>
<span id="live-status">4 movable tables · routes updating live</span>
</div>
</header>
<aside id="control-panel" class="panel">
<section class="section">
<div class="section-heading">
<h2>Map Key</h2>
<span class="count-badge">Voxel plan</span>
</div>
<div class="legend-grid">
<div class="legend-item">
<span class="swatch table"></span>
<span>Movable café table</span>
</div>
<div class="legend-item">
<span class="swatch fixed"></span>
<span>Fixed public feature</span>
</div>
<div class="legend-item">
<span class="swatch open"></span>
<span>Entrance open</span>
</div>
<div class="legend-item">
<span class="swatch blocked"></span>
<span>Entrance blocked</span>
</div>
</div>
<p class="hint" style="margin-top: 11px">
Drag orange tables directly on the map. Their footprints affect both
route calculations.
</p>
<div class="button-row">
<button id="reset-tables" class="button" type="button">Reset tables</button>
<button id="reset-view" class="button" type="button">Reset view</button>
</div>
</section>
<section class="section">
<div class="section-heading">
<h2>Residential Entrances</h2>
<span id="blocked-count" class="count-badge">1 blocked</span>
</div>
<p class="hint">
Toggle a closure to highlight the affected residential entrance.
</p>
<div class="entrance-list">
<div class="entrance-row" data-entrance-row="scala">
<span class="entrance-icon">⌂</span>
<span>
<span class="entrance-name">Via della Scala</span>
<span class="entrance-state">Access open</span>
</span>
<label class="toggle" title="Mark entrance blocked">
<input
type="checkbox"
data-entrance="scala"
aria-label="Mark Via della Scala entrance blocked"
/>
<span class="toggle-track"></span>
</label>
</div>
<div class="entrance-row is-blocked" data-entrance-row="salumi">
<span class="entrance-icon">!</span>
<span>
<span class="entrance-name">Via dei Salumi</span>
<span class="entrance-state">Access blocked</span>
</span>
<label class="toggle" title="Mark entrance blocked">
<input
type="checkbox"
data-entrance="salumi"
checked
aria-label="Mark Via dei Salumi entrance blocked"
/>
<span class="toggle-track"></span>
</label>
</div>
<div class="entrance-row" data-entrance-row="cinque">
<span class="entrance-icon">⌂</span>
<span>
<span class="entrance-name">Vicolo del Cinque</span>
<span class="entrance-state">Access open</span>
</span>
<label class="toggle" title="Mark entrance blocked">
<input
type="checkbox"
data-entrance="cinque"
aria-label="Mark Vicolo del Cinque entrance blocked"
/>
<span class="toggle-track"></span>
</label>
</div>
</div>
</section>
<section class="section">
<div class="section-heading">
<h2>Fixed Public Space</h2>
<span class="count-badge">4 features</span>
</div>
<p class="hint">
The fountain, benches, planter, and cycle stand remain fixed. Routes
avoid their complete footprints.
</p>
<div class="legend-grid">
<div class="legend-item">
<span class="swatch" style="background:#63a5a3"></span>
<span>Fountain</span>
</div>
<div class="legend-item">
<span class="swatch" style="background:#b78654"></span>
<span>Benches</span>
</div>
<div class="legend-item">
<span class="swatch" style="background:#739662"></span>
<span>Planter</span>
</div>
<div class="legend-item">
<span class="swatch" style="background:#7c8581"></span>
<span>Cycle stand</span>
</div>
</div>
</section>
</aside>
<aside id="route-panel" class="panel">
<div class="section-heading">
<h2>Route Comparison</h2>
<span class="count-badge">Live</span>
</div>
<p class="route-intro">
From the west-lane approach to the Piazza Trilussa café stop. Both routes
avoid fixed features and movable table footprints.
</p>
<div class="route-toggles">
<label class="check-label">
<input id="show-route-a" type="checkbox" checked />
Show route A
</label>
<label class="check-label">
<input id="show-route-b" type="checkbox" checked />
Show route B
</label>
</div>
<button id="route-card-a" class="route-card" type="button">
<div class="route-card-top">
<span class="route-name">
<span class="route-letter a">A</span>
North fountain edge
</span>
<span class="recommendation" style="visibility: hidden">Shorter</span>
</div>
<div class="route-description">
Stays on the northern side of the fountain before turning toward
Piazza Trilussa.
</div>
<div class="route-metrics">
<div class="metric">
<div id="route-a-distance" class="metric-value">— m</div>
<div class="metric-label">Distance</div>
</div>
<div class="metric">
<div id="route-a-time" class="metric-value">— s</div>
<div class="metric-label">Walking time</div>
</div>
</div>
</button>
<button id="route-card-b" class="route-card" type="button">
<div class="route-card-top">
<span class="route-name">
<span class="route-letter b">B</span>
South lane
</span>
<span class="recommendation" style="visibility: hidden">Shorter</span>
</div>
<div class="route-description">
Uses the southern lane, passing the benches and planters before
entering Piazza Trilussa.
</div>
<div class="route-metrics">
<div class="metric">
<div id="route-b-distance" class="metric-value">— m</div>
<div class="metric-label">Distance</div>
</div>
<div class="metric">
<div id="route-b-time" class="metric-value">— s</div>
<div class="metric-label">Walking time</div>
</div>
</div>
</button>
<div id="route-comparison" class="comparison">
Comparing two valid pedestrian routes…
</div>
<div class="map-options">
<label class="check-label">
<input id="show-labels" type="checkbox" checked />
Map labels
</label>
<button id="plan-view" class="button" style="flex: 0 0 112px" type="button">
Plan view
</button>
</div>
<p class="footnote">
Schematic spatial model, not a survey or navigation map. Distances are
illustrative and update when furniture is moved.
</p>
</aside>
<div id="bottom-hint">
<span><span class="hint-key">DRAG</span> orange tables</span>
<span class="hint-secondary">· <span class="hint-key">DRAG</span> map to orbit</span>
<span class="hint-secondary">· <span class="hint-key">SHIFT + DRAG</span> pan</span>
<span class="hint-secondary">· <span class="hint-key">SCROLL</span> zoom</span>
</div>
<div id="toast" role="status" aria-live="polite"></div>
<div id="tooltip"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// State and DOM
// -------------------------------------------------------------------------
const sceneContainer = document.getElementById("scene");
const labelsLayer = document.getElementById("labels");
const tooltip = document.getElementById("tooltip");
const toast = document.getElementById("tooltip");
const toastElement = document.getElementById("toast");
const liveStatus = document.getElementById("live-status");
const blockedCount = document.getElementById("blocked-count");
const state = {
blocked: {
scala: false,
salumi: true,
cinque: false
},
showLabels: true,
routes: {
a: { visible: true, path: null, distance: 0, seconds: 0 },
b: { visible: true, path: null, distance: 0, seconds: 0 }
}
};
let toastTimer = 0;
function showToast(message) {
toastElement.textContent = message;
toastElement.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toastElement.classList.remove("show"), 1800);
}
// -------------------------------------------------------------------------
// Renderer, scene, lights, and direct orthographic camera controls
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb9c7bf);
scene.fog = new THREE.Fog(0xb9c7bf, 72, 125);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel map of public spaces in Trastevere"
);
renderer.domElement.tabIndex = 0;
sceneContainer.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-30, 30, 22, -22, 0.1, 180);
const cameraState = {
target: new THREE.Vector3(0, 0, 1),
radius: 62,
theta: 0.54,
phi: 0.79,
zoom: 1
};
const defaultCamera = {
target: new THREE.Vector3(0, 0, 1),
radius: 62,
theta: 0.54,
phi: 0.79,
zoom: 1
};
function updateCamera() {
const { target, radius, theta, phi, zoom } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(target);
camera.zoom = zoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const halfHeight = 23;
camera.left = -halfHeight * aspect;
camera.right = halfHeight * aspect;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
resize();
const hemiLight = new THREE.HemisphereLight(0xf6f2dc, 0x6e7d78, 2.25);
scene.add(hemiLight);
const sun = new THREE.DirectionalLight(0xfff0ce, 3.1);
sun.position.set(-24, 42, 18);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -38;
sun.shadow.camera.right = 38;
sun.shadow.camera.top = 32;
sun.shadow.camera.bottom = -32;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 100;
sun.shadow.bias = -0.0005;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fillLight = new THREE.DirectionalLight(0xb9d9e1, 0.85);
fillLight.position.set(24, 18, -24);
scene.add(fillLight);
// -------------------------------------------------------------------------
// Materials and voxel construction helpers
// -------------------------------------------------------------------------
const materials = {
base: new THREE.MeshStandardMaterial({ color: 0x81918a, roughness: 1 }),
plaza: new THREE.MeshStandardMaterial({ color: 0xd8c7a7, roughness: 0.96 }),
lane: new THREE.MeshStandardMaterial({ color: 0xa9a79b, roughness: 1 }),
trilussa: new THREE.MeshStandardMaterial({ color: 0xc8b99e, roughness: 0.96 }),
curb: new THREE.MeshStandardMaterial({ color: 0xeee0c4, roughness: 0.9 }),
pavingLine: new THREE.MeshBasicMaterial({
color: 0xb7a789,
transparent: true,
opacity: 0.42
}),
cream: new THREE.MeshStandardMaterial({ color: 0xe3d4b7, roughness: 0.92 }),
paleCream: new THREE.MeshStandardMaterial({ color: 0xeee1c8, roughness: 0.95 }),
terracotta: new THREE.MeshStandardMaterial({ color: 0xc97451, roughness: 0.92 }),
ochre: new THREE.MeshStandardMaterial({ color: 0xd49c61, roughness: 0.94 }),
rose: new THREE.MeshStandardMaterial({ color: 0xc58e78, roughness: 0.95 }),
roof: new THREE.MeshStandardMaterial({ color: 0x795b4a, roughness: 0.96 }),
roofLight: new THREE.MeshStandardMaterial({ color: 0xa27a5e, roughness: 0.96 }),
window: new THREE.MeshStandardMaterial({
color: 0x3e5559,
roughness: 0.42,
metalness: 0.08
}),
windowWarm: new THREE.MeshStandardMaterial({
color: 0xe8b66e,
roughness: 0.55,
emissive: 0x6a3f16,
emissiveIntensity: 0.13
}),
fountain: new THREE.MeshStandardMaterial({ color: 0x69a5a2, roughness: 0.78 }),
fountainStone: new THREE.MeshStandardMaterial({ color: 0xc5c1ac, roughness: 0.9 }),
water: new THREE.MeshStandardMaterial({
color: 0x4e9ba3,
roughness: 0.22,
metalness: 0.12
}),
wood: new THREE.MeshStandardMaterial({ color: 0xa96f3d, roughness: 0.9 }),
woodDark: new THREE.MeshStandardMaterial({ color: 0x765036, roughness: 0.96 }),
metal: new THREE.MeshStandardMaterial({
color: 0x697571,
roughness: 0.55,
metalness: 0.35
}),
leaf: new THREE.MeshStandardMaterial({ color: 0x617e50, roughness: 0.98 }),
leafLight: new THREE.MeshStandardMaterial({ color: 0x7c975b, roughness: 0.98 }),
trunk: new THREE.MeshStandardMaterial({ color: 0x77543c, roughness: 1 }),
table: new THREE.MeshStandardMaterial({ color: 0xe47c4c, roughness: 0.78 }),
tableDark: new THREE.MeshStandardMaterial({ color: 0xb95c38, roughness: 0.86 }),
routeA: new THREE.MeshBasicMaterial({ color: 0x72d19b }),
routeB: new THREE.MeshBasicMaterial({ color: 0x70bde8 }),
routeArrowA: new THREE.MeshBasicMaterial({ color: 0xd8ffe9 }),
routeArrowB: new THREE.MeshBasicMaterial({ color: 0xd9f2ff })
};
function box(width, height, depth, x, y, z, material, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinder(radius, height, x, y, z, material, parent = scene, segments = 12) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, height, segments),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
let randomSeed = 71423;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
const interactiveRoots = [];
const labels = [];
function addLabel(html, x, y, z, className = "") {
const element = document.createElement("div");
element.className = `map-label ${className}`;
element.innerHTML = html;
labelsLayer.appendChild(element);
const label = {
element,
position: new THREE.Vector3(x, y, z),
visible: true
};
labels.push(label);
return label;
}
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labels) {
if (!label.visible || !state.showLabels) {
label.element.style.display = "none";
continue;
}
const projected = label.position.clone().project(camera);
if (projected.z < -1 || projected.z > 1) {
label.element.style.display = "none";
continue;
}
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
label.element.style.display = "";
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
// -------------------------------------------------------------------------
// Ground, paving, streets, and voxel buildings
// -------------------------------------------------------------------------
box(59, 0.65, 47, 0, -0.55, 1, materials.base);
// Piazza di Santa Maria in Trastevere.
box(24, 0.16, 18, -3, -0.04, 1, materials.plaza);
box(24.5, 0.2, 0.22, -3, 0.06, 10.1, materials.curb);
box(0.22, 0.2, 18.5, -15.1, 0.06, 1, materials.curb);
// Surrounding lanes.
box(43, 0.13, 5, 0, -0.02, -9.5, materials.lane);
box(43, 0.13, 5, 0, -0.02, 11.5, materials.lane);
box(6, 0.13, 27, -18, -0.015, 1, materials.lane);
box(3.2, 0.13, 27, 20.5, -0.015, 1, materials.lane);
box(4, 0.13, 13, 11, -0.01, 5.5, materials.lane);
// Piazza Trilussa.
box(9.5, 0.16, 12, 16.5, 0.015, 6, materials.trilussa);
box(9.7, 0.2, 0.2, 16.5, 0.11, 12.1, materials.curb);
box(0.2, 0.2, 12.2, 21.2, 0.11, 6, materials.curb);
// Fine paving grid.
for (let x = -14; x <= 8; x += 2) {
box(0.025, 0.012, 17.5, x, 0.055, 1, materials.pavingLine);
}
for (let z = -7; z <= 9; z += 2) {
box(23.5, 0.012, 0.025, -3, 0.055, z, materials.pavingLine);
}
// Lane edge blocks.
for (let x = -20; x <= 20; x += 2) {
box(1.25, 0.08, 0.25, x, 0.08, -7, materials.curb);
box(1.25, 0.08, 0.25, x, 0.08, 10, materials.curb);
}
function addBuilding(x, z, width, depth, height, bodyMaterial) {
const body = box(width, height, depth, x, height / 2 + 0.15, z, bodyMaterial);
const roofMaterial = random() > 0.5 ? materials.roof : materials.roofLight;
box(width + 0.3, 0.35, depth + 0.3, x, height + 0.3, z, roofMaterial);
// A few stepped roof volumes.
if (random() > 0.48) {
box(
Math.max(1.1, width * 0.25),
0.6,
Math.max(1.1, depth * 0.24),
x + width * 0.12,
height + 0.75,
z - depth * 0.08,
bodyMaterial
);
}
const columns = Math.max(2, Math.floor(width / 2.1));
const rows = Math.max(2, Math.floor(height / 2.2));
if (z < -13) {
for (let col = 0; col < columns; col++) {
for (let row = 0; row < rows; row++) {
const wx = x - width / 2 + 0.85 + col * ((width - 1.7) / Math.max(1, columns - 1));
const wy = 1.15 + row * 1.75;
box(
0.5,
0.72,
0.08,
wx,
wy,
z + depth / 2 + 0.055,
random() > 0.76 ? materials.windowWarm : materials.window
);
}
}
} else if (z > 14) {
for (let col = 0; col < columns; col++) {
for (let row = 0; row < rows; row++) {
const wx = x - width / 2 + 0.85 + col * ((width - 1.7) / Math.max(1, columns - 1));
const wy = 1.15 + row * 1.75;
box(
0.5,
0.72,
0.08,
wx,
wy,
z - depth / 2 - 0.055,
random() > 0.76 ? materials.windowWarm : materials.window
);
}
}
} else if (x < -21) {
for (let col = 0; col < columns; col++) {
for (let row = 0; row < rows; row++) {
const wz = z - depth / 2 + 0.85 + col * ((depth - 1.7) / Math.max(1, columns - 1));
const wy = 1.15 + row * 1.75;
box(
0.08,
0.72,
0.5,
x + width / 2 + 0.055,
wy,
wz,
random() > 0.76 ? materials.windowWarm : materials.window
);
}
}
} else if (x > 21) {
for (let col = 0; col < columns; col++) {
for (let row = 0; row < rows; row++) {
const wz = z - depth / 2 + 0.85 + col * ((depth - 1.7) / Math.max(1, columns - 1));
const wy = 1.15 + row * 1.75;
box(
0.08,
0.72,
0.5,
x - width / 2 - 0.055,
wy,
wz,
random() > 0.76 ? materials.windowWarm : materials.window
);
}
}
}
return body;
}
const buildingColors = [
materials.cream,
materials.paleCream,
materials.terracotta,
materials.ochre,
materials.rose
];
[-18.5, -10.5, -2.5, 5.5, 13.5, 21.5].forEach((x, index) => {
addBuilding(
x,
-17,
6.4 + (index % 2) * 0.8,
6.5,
5.2 + random() * 5.2,
buildingColors[index % buildingColors.length]
);
});
[-18.5, -10, -1.5, 7, 15.5, 23].forEach((x, index) => {
addBuilding(
x,
18,
6.4 + (index % 2) * 0.8,
6.4,
5 + random() * 5.5,
buildingColors[(index + 2) % buildingColors.length]
);
});
[-8.5, 1.5, 11.5].forEach((z, index) => {
addBuilding(
-25,
z,
6.2,
6.8 + (index % 2),
5.5 + random() * 4.5,
buildingColors[(index + 1) % buildingColors.length]
);
});
[-8.5, 1.5, 11.5].forEach((z, index) => {
addBuilding(
25,
z,
6.2,
6.8 + (index % 2),
5 + random() * 4.5,
buildingColors[(index + 3) % buildingColors.length]
);
});
// Small awnings and roof chimneys.
box(2.4, 0.25, 1.1, -8, 3.2, -13.65, materials.terracotta);
box(2.4, 0.25, 1.1, 9, 3.2, 14.65, materials.ochre);
// -------------------------------------------------------------------------
// Fixed public-space features
// -------------------------------------------------------------------------
const fixedFeatures = [
{ id: "fountain", x: -4, z: 0, hx: 2.2, hz: 1.7 },
{ id: "bench-north", x: 4, z: -5, hx: 1.9, hz: 0.68 },
{ id: "bench-south", x: 1, z: 6.5, hx: 1.9, hz: 0.68 },
{ id: "planter", x: -9, z: 5, hx: 1.15, hz: 1.15 },
{ id: "bike-rack", x: 8, z: 2, hx: 0.85, hz: 1.9 }
];
function makeFountain() {
const group = new THREE.Group();
group.position.set(-4, 0, 0);
group.userData.interactiveType = "fixed";
group.userData.info = "Fontana · fixed public-space feature";
scene.add(group);
box(4.4, 0.45, 3.4, 0, 0.28, 0, materials.fountainStone, group);
box(3.7, 0.18, 2.7, 0, 0.57, 0, materials.water, group);
box(0.8, 1.5, 0.8, 0, 1.25, 0, materials.fountainStone, group);
box(1.5, 0.25, 1.5, 0, 2.08, 0, materials.fountain, group);
box(0.35, 0.65, 0.35, 0, 2.48, 0, materials.fountain, group);
const spout = cylinder(0.1, 0.6, 0, 2.8, 0, materials.water, group, 8);
spout.rotation.z = Math.PI / 2;
interactiveRoots.push(group);
addLabel("Fontana<small>Fixed feature</small>", -4, 3.15, 0);
}
function makeBench(x, z, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
group.userData.interactiveType = "fixed";
group.userData.info = "Public bench · fixed footprint";
scene.add(group);
box(3.5, 0.22, 0.75, 0, 0.95, 0, materials.wood, group);
box(3.5, 0.65, 0.18, 0, 1.35, -0.29, materials.woodDark, group);
box(0.22, 0.9, 0.65, -1.3, 0.45, 0, materials.metal, group);
box(0.22, 0.9, 0.65, 1.3, 0.45, 0, materials.metal, group);
interactiveRoots.push(group);
}
function makePlanter(x, z) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.userData.interactiveType = "fixed";
group.userData.info = "Planter and tree · fixed footprint";
scene.add(group);
box(2.2, 0.65, 2.2, 0, 0.33, 0, materials.fountainStone, group);
box(1.9, 0.12, 1.9, 0, 0.69, 0, materials.trunk, group);
box(0.38, 2.6, 0.38, 0, 1.85, 0, materials.trunk, group);
box(2.3, 1.5, 2.1, 0, 3.3, 0, materials.leaf, group);
box(1.4, 1.2, 1.3, -0.55, 4.05, 0.25, materials.leafLight, group);
box(1.1, 0.9, 1.2, 0.7, 3.9, -0.25, materials.leafLight, group);
interactiveRoots.push(group);
addLabel("Planter<small>Fixed feature</small>", -9, 5.0, 5);
}
function makeBikeRack(x, z) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.userData.interactiveType = "fixed";
group.userData.info = "Cycle stand · fixed footprint";
scene.add(group);
for (let i = -1; i <= 1; i++) {
box(0.16, 1.15, 0.16, i * 0.65, 0.58, 0, materials.metal, group);
box(0.16, 0.16, 1.7, i * 0.65, 1.1, 0, materials.metal, group);
}
box(2.4, 0.12, 0.12, 0, 0.15, 0, materials.metal, group);
interactiveRoots.push(group);
}
makeFountain();
makeBench(4, -5);
makeBench(1, 6.5, Math.PI);
makePlanter(-9, 5);
makeBikeRack(8, 2);
// -------------------------------------------------------------------------
// Movable café tables
// -------------------------------------------------------------------------
const tables = [];
function makeTable(x, z) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.userData.interactiveType = "table";
group.userData.info = "Movable café table · drag to rearrange";
scene.add(group);
// Pedestal and tabletop.
box(0.18, 0.95, 0.18, 0, 0.55, 0, materials.tableDark, group);
box(0.75, 0.12, 0.75, 0, 0.09, 0, materials.tableDark, group);
box(1.05, 0.18, 1.05, 0, 1.12, 0, materials.table, group);
// Four small voxel chairs.
const chairPositions = [
[0, 1.18, Math.PI],
[0, -1.18, 0],
[1.18, 0, -Math.PI / 2],
[-1.18, 0, Math.PI / 2]
];
for (const [cx, cz, rotation] of chairPositions) {
const chair = new THREE.Group();
chair.position.set(cx, 0, cz);
chair.rotation.y = rotation;
group.add(chair);
box(0.56, 0.12, 0.56, 0, 0.58, 0, materials.table, chair);
box(0.56, 0.58, 0.12, 0, 0.86, 0.25, materials.tableDark, chair);
box(0.09, 0.5, 0.09, -0.2, 0.3, -0.18, materials.tableDark, chair);
box(0.09, 0.5, 0.09, 0.2, 0.3, -0.18, materials.tableDark, chair);
}
// A small umbrella pole makes each table immediately recognizable.
cylinder(0.055, 1.7, 0, 1.95, 0, materials.woodDark, group, 8);
const canopy = box(2.25, 0.14, 2.25, 0, 2.82, 0, materials.table, group);
canopy.rotation.y = Math.PI / 4;
const table = {
group,
initial: new THREE.Vector2(x, z)
};
group.userData.table = table;
tables.push(table);
interactiveRoots.push(group);
return table;
}
[
[-1, 4],
[-8, 0],
[3.5, 3],
[6, -0.5],
[-2, -5.5]
].forEach(([x, z]) => makeTable(x, z));
// -------------------------------------------------------------------------
// Residential entrance markers
// -------------------------------------------------------------------------
const entranceDefinitions = {
scala: {
name: "Via della Scala",
x: -15,
z: -2,
rotation: Math.PI / 2
},
salumi: {
name: "Via dei Salumi",
x: 0,
z: -8,
rotation: 0
},
cinque: {
name: "Vicolo del Cinque",
x: 9,
z: 5,
rotation: -Math.PI / 2
}
};
const entranceVisuals = {};
function makeEntrance(id, definition) {
const group = new THREE.Group();
group.position.set(definition.x, 0, definition.z);
group.rotation.y = definition.rotation;
group.userData.interactiveType = "entrance";
group.userData.entranceId = id;
group.userData.info = `${definition.name} · click to toggle access`;
scene.add(group);
// Small facade panel behind the doorway.
box(3.1, 2.65, 0.25, 0, 1.33, -0.25, materials.cream, group);
box(0.22, 2.15, 0.28, -0.72, 1.08, 0.05, materials.woodDark, group);
box(0.22, 2.15, 0.28, 0.72, 1.08, 0.05, materials.woodDark, group);
box(1.65, 0.22, 0.28, 0, 2.12, 0.05, materials.woodDark, group);
const doorMaterial = new THREE.MeshStandardMaterial({
color: 0x4d8060,
roughness: 0.72,
emissive: 0x102a1c,
emissiveIntensity: 0.15
});
const door = box(1.2, 1.8, 0.16, 0, 0.95, 0.1, doorMaterial, group);
door.castShadow = false;
const barrier = new THREE.Group();
group.add(barrier);
box(2.7, 0.22, 0.16, 0, 0.85, 0.55, materials.tableDark, barrier);
box(0.16, 1.0, 0.16, -1.1, 0.5, 0.55, materials.metal, barrier);
box(0.16, 1.0, 0.16, 1.1, 0.5, 0.55, materials.metal, barrier);
const beaconMaterial = new THREE.MeshStandardMaterial({
color: 0xff655f,
emissive: 0xff332d,
emissiveIntensity: 0.8
});
const beacon = cylinder(
0.12,
0.8,
0,
2.65,
0,
beaconMaterial,
group,
8
);
const label = addLabel(
`${definition.name}<small>Access open</small>`,
definition.x,
3.3,
definition.z,
"entrance-label"
);
entranceVisuals[id] = {
group,
door,
doorMaterial,
barrier,
beacon,
beaconMaterial,
label
};
interactiveRoots.push(group);
}
Object.entries(entranceDefinitions).forEach(([id, definition]) => {
makeEntrance(id, definition);
});
function updateEntranceVisual(id) {
const visual = entranceVisuals[id];
const blocked = state.blocked[id];
const definition = entranceDefinitions[id];
visual.barrier.visible = blocked;
visual.beacon.visible = blocked;
visual.doorMaterial.color.setHex(blocked ? 0xd6534e : 0x4d8060);
visual.doorMaterial.emissive.setHex(blocked ? 0x5b100c : 0x102a1c);
visual.doorMaterial.emissiveIntensity = blocked ? 0.36 : 0.15;
visual.beaconMaterial.emissiveIntensity = blocked ? 0.9 : 0;
visual.label.element.classList.toggle("blocked", blocked);
visual.label.element.innerHTML = blocked
? `${definition.name}<small>Access blocked</small>`
: `${definition.name}<small>Access open</small>`;
const row = document.querySelector(`[data-entrance-row="${id}"]`);
row.classList.toggle("is-blocked", blocked);
row.querySelector(".entrance-icon").textContent = blocked ? "!" : "⌂";
row.querySelector(".entrance-state").textContent = blocked
? "Access blocked"
: "Access open";
}
Object.keys(state.blocked).forEach(updateEntranceVisual);
// -------------------------------------------------------------------------
// Street and endpoint labels
// -------------------------------------------------------------------------
addLabel(
"Piazza di Santa Maria<small>In Trastevere</small>",
-4,
2.7,
1,
"square-label"
);
addLabel(
"Piazza Trilussa<small>Route destination</small>",
16.5,
2.6,
6,
"square-label"
);
addLabel("Via della Lungaretta", -3, 1.2, -10.8);
addLabel("Via della Scala", -19.2, 1.2, 7.8);
addLabel("Vicolo del Cinque", 11.1, 1.4, 2.2);
addLabel("Via dei Salumi", 0, 1.2, -14.8);
addLabel("Start · west lane", -19, 1.7, -4, "endpoint");
addLabel("Café stop", 16, 1.8, 6, "endpoint");
// -------------------------------------------------------------------------
// Grid-based pedestrian routing with obstacle avoidance
// -------------------------------------------------------------------------
const routeGroups = {
a: new THREE.Group(),
b: new THREE.Group()
};
scene.add(routeGroups.a, routeGroups.b);
const GRID = {
minX: -22,
maxX: 22,
minZ: -13,
maxZ: 14
};
const START = { x: -19, z: -4 };
const END = { x: 16, z: 6 };
function isWalkable(x, z) {
const mainPlaza = x >= -15 && x <= 9 && z >= -8 && z <= 10;
const northLane = x >= -21 && x <= 21 && z >= -12 && z <= -7;
const southLane = x >= -21 && x <= 21 && z >= 9 && z <= 14;
const westLane = x >= -21 && x <= -15 && z >= -12 && z <= 14;
const eastLane = x >= 19 && x <= 22 && z >= -12 && z <= 14;
const connector = x >= 9 && x <= 13 && z >= -1 && z <= 12;
const trilussa = x >= 12 && x <= 21 && z >= 0 && z <= 12;
return (
mainPlaza ||
northLane ||
southLane ||
westLane ||
eastLane ||
connector ||
trilussa
);
}
function obstacleAt(x, z) {
for (const feature of fixedFeatures) {
if (
Math.abs(x - feature.x) <= feature.hx + 0.28 &&
Math.abs(z - feature.z) <= feature.hz + 0.28
) {
return true;
}
}
for (const table of tables) {
if (
Math.abs(x - table.group.position.x) <= 1.02 &&
Math.abs(z - table.group.position.z) <= 1.02
) {
return true;
}
}
return false;
}
function pointKey(x, z) {
return `${x},${z}`;
}
function heuristic(a, b) {
return Math.abs(a.x - b.x) + Math.abs(a.z - b.z);
}
function routePenalty(x, z, bias) {
if (bias === "north") {
if (z > 1) return 2.45;
if (z < -7) return 0;
return 0.32;
}
if (z < 0) return 2.45;
if (z > 8) return 0;
return 0.32;
}
function findRoute(bias) {
const open = [];
const gScore = new Map();
const fScore = new Map();
const cameFrom = new Map();
const closed = new Set();
const startKey = pointKey(START.x, START.z);
const goalKey = pointKey(END.x, END.z);
gScore.set(startKey, 0);
fScore.set(startKey, heuristic(START, END));
open.push({ x: START.x, z: START.z, f: heuristic(START, END) });
const directions = [
{ x: 1, z: 0 },
{ x: -1, z: 0 },
{ x: 0, z: 1 },
{ x: 0, z: -1 }
];
while (open.length) {
open.sort((a, b) => a.f - b.f);
const current = open.shift();
const currentKey = pointKey(current.x, current.z);
if (currentKey === goalKey) {
const path = [];
let cursor = goalKey;
const [gx, gz] = cursor.split(",").map(Number);
path.push(new THREE.Vector3(gx, 0, gz));
while (cameFrom.has(cursor)) {
cursor = cameFrom.get(cursor);
const [px, pz] = cursor.split(",").map(Number);
path.push(new THREE.Vector3(px, 0, pz));
}
path.reverse();
return path;
}
if (closed.has(currentKey)) continue;
closed.add(currentKey);
for (const direction of directions) {
const nx = current.x + direction.x;
const nz = current.z + direction.z;
if (
nx < GRID.minX ||
nx > GRID.maxX ||
nz < GRID.minZ ||
nz > GRID.maxZ
) {
continue;
}
if (!isWalkable(nx, nz) || obstacleAt(nx, nz)) continue;
const neighborKey = pointKey(nx, nz);
if (closed.has(neighborKey)) continue;
const tentative =
(gScore.get(currentKey) ?? Infinity) +
1 +
routePenalty(nx, nz, bias);
if (tentative < (gScore.get(neighborKey) ?? Infinity)) {
cameFrom.set(neighborKey, currentKey);
gScore.set(neighborKey, tentative);
fScore.set(neighborKey, tentative + heuristic({ x: nx, z: nz }, END));
const existing = open.findIndex(
(node) => node.x === nx && node.z === nz
);
if (existing >= 0) {
open[existing] = { x: nx, z: nz, f: fScore.get(neighborKey) };
} else {
open.push({ x: nx, z: nz, f: fScore.get(neighborKey) });
}
}
}
}
return null;
}
function simplifyPath(path) {
if (!path || path.length < 3) return path || [];
const simplified = [path[0]];
for (let i = 1; i < path.length - 1; i++) {
const previous = simplified[simplified.length - 1];
const current = path[i];
const next = path[i + 1];
const sameX = previous.x === current.x && current.x === next.x;
const sameZ = previous.z === current.z && current.z === next.z;
if (!sameX && !sameZ) {
simplified.push(current);
}
}
simplified.push(path[path.length - 1]);
return simplified;
}
function pathDistance(path) {
if (!path) return 0;
let distance = 0;
for (let i = 1; i < path.length; i++) {
distance += path[i].distanceTo(path[i - 1]);
}
return distance;
}
function clearRouteGroup(group) {
for (const child of [...group.children]) {
child.traverse((object) => {
if (object.geometry) object.geometry.dispose();
});
group.remove(child);
}
}
function drawRoute(routeKey, path) {
const group = routeGroups[routeKey];
const material =
routeKey === "a" ? materials.routeA : materials.routeB;
const arrowMaterial =
routeKey === "a" ? materials.routeArrowA : materials.routeArrowB;
clearRouteGroup(group);
if (!path) return;
const simplified = simplifyPath(path);
for (let i = 1; i < simplified.length; i++) {
const start = simplified[i - 1];
const end = simplified[i];
const dx = end.x - start.x;
const dz = end.z - start.z;
const length = Math.hypot(dx, dz);
if (length < 0.001) continue;
const segment = box(
0.28,
0.09,
length,
(start.x + end.x) / 2,
0.16,
(start.z + end.z) / 2,
material,
group
);
segment.rotation.y = Math.atan2(dx, dz);
segment.castShadow = false;
segment.receiveShadow = false;
// Pale direction markers along longer segments.
if (length > 5) {
for (let markerDistance = 3; markerDistance < length - 1; markerDistance += 4) {
const t = markerDistance / length;
const marker = box(
0.18,
0.025,
0.72,
start.x + dx * t,
0.215,
start.z + dz * t,
arrowMaterial,
group
);
marker.rotation.y = Math.atan2(dx, dz);
marker.castShadow = false;
marker.receiveShadow = false;
}
}
}
for (const point of simplified) {
const node = box(
0.34,
0.1,
0.34,
point.x,
0.165,
point.z,
material,
group
);
node.castShadow = false;
node.receiveShadow = false;
}
group.visible = state.routes[routeKey].visible;
}
function updateRouteUI() {
const routeA = state.routes.a;
const routeB = state.routes.b;
const routeADistanceElement = document.getElementById("route-a-distance");
const routeATimeElement = document.getElementById("route-a-time");
const routeBDistanceElement = document.getElementById("route-b-distance");
const routeBTimeElement = document.getElementById("route-b-time");
const comparison = document.getElementById("route-comparison");
const cardA = document.getElementById("route-card-a");
const cardB = document.getElementById("route-card-b");
routeADistanceElement.textContent = routeA.path
? `${Math.round(routeA.distance)} m`
: "Unavailable";
routeATimeElement.textContent = routeA.path
? `${Math.round(routeA.seconds)} s`
: "—";
routeBDistanceElement.textContent = routeB.path
? `${Math.round(routeB.distance)} m`
: "Unavailable";
routeBTimeElement.textContent = routeB.path
? `${Math.round(routeB.seconds)} s`
: "—";
const aRecommendation = cardA.querySelector(".recommendation");
const bRecommendation = cardB.querySelector(".recommendation");
cardA.classList.remove("recommended");
cardB.classList.remove("recommended");
aRecommendation.style.visibility = "hidden";
bRecommendation.style.visibility = "hidden";
if (routeA.path && routeB.path) {
const difference = Math.abs(routeA.distance - routeB.distance);
const aIsShorter = routeA.distance <= routeB.distance;
cardA.classList.toggle("recommended", aIsShorter);
cardB.classList.toggle("recommended", !aIsShorter);
if (aIsShorter) {
aRecommendation.style.visibility = "visible";
} else {
bRecommendation.style.visibility = "visible";
}
comparison.textContent =
difference < 1
? "Both alternatives are effectively equal in length. Furniture position is not creating a meaningful detour."
: `Route ${aIsShorter ? "A" : "B"} is approximately ${Math.round(difference)} m shorter. Both remain clear of fixed features and table footprints.`;
} else if (!routeA.path && !routeB.path) {
comparison.textContent =
"No valid route is currently available. Move a table away from a constrained passage.";
} else {
comparison.textContent = `Route ${routeA.path ? "A" : "B"} is currently unavailable.`;
}
}
function recomputeRoutes() {
const routeA = findRoute("north");
const routeB = findRoute("south");
state.routes.a.path = routeA;
state.routes.b.path = routeB;
state.routes.a.distance = pathDistance(routeA) * 1.25;
state.routes.b.distance = pathDistance(routeB) * 1.25;
state.routes.a.seconds = state.routes.a.distance / 1.18;
state.routes.b.seconds = state.routes.b.distance / 1.18;
drawRoute("a", routeA);
drawRoute("b", routeB);
updateRouteUI();
const moved = tables.filter(
(table) =>
table.group.position.distanceTo(
new THREE.Vector3(table.initial.x, 0, table.initial.y)
) > 0.08
).length;
const closedCount = Object.values(state.blocked).filter(Boolean).length;
liveStatus.textContent =
`${moved} table${moved === 1 ? "" : "s"} moved · ` +
`${closedCount} entrance${closedCount === 1 ? "" : "s"} blocked · routes live`;
}
let routeUpdateQueued = false;
function queueRouteUpdate() {
if (routeUpdateQueued) return;
routeUpdateQueued = true;
requestAnimationFrame(() => {
routeUpdateQueued = false;
recomputeRoutes();
});
}
// -------------------------------------------------------------------------
// Pointer interaction: table dragging, entrance toggling, orbit, and pan
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), -0.85);
const dragPoint = new THREE.Vector3();
let pointerMode = null;
let draggedTable = null;
let dragOffset = new THREE.Vector2();
let previousPointer = { x: 0, y: 0 };
let hoveredObject = null;
function setPointerFromEvent(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
}
function getInteractiveHit(event) {
setPointerFromEvent(event);
const hits = raycaster.intersectObjects(interactiveRoots, true);
for (const hit of hits) {
let object = hit.object;
while (object && !object.userData.interactiveType) {
object = object.parent;
}
if (object) return object;
}
return null;
}
function validTablePosition(table, x, z) {
if (x < -14 || x > 8 || z < -7 || z > 9) return false;
for (const feature of fixedFeatures) {
const minimumX = feature.hx + 1.1;
const minimumZ = feature.hz + 1.1;
if (
Math.abs(x - feature.x) < minimumX &&
Math.abs(z - feature.z) < minimumZ
) {
return false;
}
}
for (const other of tables) {
if (other === table) continue;
const dx = x - other.group.position.x;
const dz = z - other.group.position.z;
if (Math.hypot(dx, dz) < 2.05) return false;
}
return true;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
previousPointer = { x: event.clientX, y: event.clientY };
const hit = getInteractiveHit(event);
if (hit?.userData.interactiveType === "table" && event.button === 0) {
pointerMode = "drag-table";
draggedTable = hit.userData.table;
if (raycaster.ray.intersectPlane(dragPlane, dragPoint)) {
dragOffset.set(
draggedTable.group.position.x - dragPoint.x,
draggedTable.group.position.z - dragPoint.z
);
} else {
dragOffset.set(0, 0);
}
draggedTable.group.scale.setScalar(1.035);
tooltip.classList.remove("visible");
return;
}
if (hit?.userData.interactiveType === "entrance" && event.button === 0) {
const id = hit.userData.entranceId;
state.blocked[id] = !state.blocked[id];
const checkbox = document.querySelector(
`[data-entrance="${id}"]`
);
checkbox.checked = state.blocked[id];
updateEntranceVisual(id);
updateBlockedCount();
showToast(
`${entranceDefinitions[id].name}: ${
state.blocked[id] ? "access blocked" : "access open"
}`
);
return;
}
if (event.button === 2 || event.shiftKey) {
pointerMode = "pan";
} else {
pointerMode = "orbit";
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (pointerMode === "drag-table" && draggedTable) {
setPointerFromEvent(event);
if (raycaster.ray.intersectPlane(dragPlane, dragPoint)) {
const candidateX = dragPoint.x + dragOffset.x;
const candidateZ = dragPoint.z + dragOffset.y;
if (validTablePosition(draggedTable, candidateX, candidateZ)) {
draggedTable.group.position.x = candidateX;
draggedTable.group.position.z = candidateZ;
queueRouteUpdate();
}
}
return;
}
if (pointerMode === "orbit") {
const dx = event.clientX - previousPointer.x;
const dy = event.clientY - previousPointer.y;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.005,
0.32,
1.28
);
previousPointer = { x: event.clientX, y: event.clientY };
updateCamera();
return;
}
if (pointerMode === "pan") {
const dx = event.clientX - previousPointer.x;
const dy = event.clientY - previousPointer.y;
const worldPerPixel = (46 / cameraState.zoom) / window.innerHeight;
const right = new THREE.Vector3(
Math.cos(cameraState.theta),
0,
-Math.sin(cameraState.theta)
);
const forward = new THREE.Vector3(
Math.sin(cameraState.theta),
0,
Math.cos(cameraState.theta)
);
cameraState.target
.addScaledVector(right, -dx * worldPerPixel)
.addScaledVector(forward, -dy * worldPerPixel);
cameraState.target.x = THREE.MathUtils.clamp(
cameraState.target.x,
-12,
12
);
cameraState.target.z = THREE.MathUtils.clamp(
cameraState.target.z,
-9,
10
);
previousPointer = { x: event.clientX, y: event.clientY };
updateCamera();
return;
}
// Hover feedback.
const hit = getInteractiveHit(event);
const interactive = hit?.userData;
if (interactive) {
hoveredObject = hit;
tooltip.textContent = interactive.info;
tooltip.classList.add("visible");
const tooltipX = Math.min(event.clientX, window.innerWidth - 235);
const tooltipY = Math.min(event.clientY, window.innerHeight - 55);
tooltip.style.left = `${tooltipX}px`;
tooltip.style.top = `${tooltipY}px`;
renderer.domElement.style.cursor =
interactive.interactiveType === "table" ? "grab" : "pointer";
} else {
hoveredObject = null;
tooltip.classList.remove("visible");
renderer.domElement.style.cursor = "grab";
}
});
function endPointerInteraction(event) {
if (draggedTable) {
draggedTable.group.scale.setScalar(1);
draggedTable = null;
recomputeRoutes();
}
pointerMode = null;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endPointerInteraction);
renderer.domElement.addEventListener("pointercancel", endPointerInteraction);
renderer.domElement.addEventListener("pointerleave", () => {
if (!pointerMode) {
tooltip.classList.remove("visible");
renderer.domElement.style.cursor = "grab";
}
});
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.001),
0.72,
2.5
);
updateCamera();
},
{ passive: false }
);
// -------------------------------------------------------------------------
// UI controls
// -------------------------------------------------------------------------
function updateBlockedCount() {
const count = Object.values(state.blocked).filter(Boolean).length;
blockedCount.textContent = `${count} blocked`;
blockedCount.style.color = count ? "var(--red)" : "var(--green)";
}
document.querySelectorAll("[data-entrance]").forEach((checkbox) => {
checkbox.addEventListener("change", () => {
const id = checkbox.dataset.entrance;
state.blocked[id] = checkbox.checked;
updateEntranceVisual(id);
updateBlockedCount();
});
});
document.getElementById("show-route-a").addEventListener("change", (event) => {
state.routes.a.visible = event.target.checked;
routeGroups.a.visible = event.target.checked;
});
document.getElementById("show-route-b").addEventListener("change", (event) => {
state.routes.b.visible = event.target.checked;
routeGroups.b.visible = event.target.checked;
});
document.getElementById("show-labels").addEventListener("change", (event) => {
state.showLabels = event.target.checked;
});
document.getElementById("reset-tables").addEventListener("click", () => {
for (const table of tables) {
table.group.position.x = table.initial.x;
table.group.position.z = table.initial.y;
}
recomputeRoutes();
showToast("Café furniture returned to its starting layout");
});
document.getElementById("reset-view").addEventListener("click", () => {
cameraState.target.copy(defaultCamera.target);
cameraState.radius = defaultCamera.radius;
cameraState.theta = defaultCamera.theta;
cameraState.phi = defaultCamera.phi;
cameraState.zoom = defaultCamera.zoom;
updateCamera();
});
document.getElementById("plan-view").addEventListener("click", () => {
cameraState.target.set(-1, 0, 1);
cameraState.theta = 0;
cameraState.phi = 0.14;
cameraState.zoom = 1.18;
updateCamera();
showToast("Plan view");
});
function focusRoute(routeKey) {
const path = state.routes[routeKey].path;
if (!path) {
showToast(`Route ${routeKey.toUpperCase()} is unavailable`);
return;
}
const center = new THREE.Vector3();
path.forEach((point) => center.add(point));
center.multiplyScalar(1 / path.length);
cameraState.target.set(center.x, 0, center.z);
cameraState.zoom = 1.38;
updateCamera();
showToast(`Focused route ${routeKey.toUpperCase()}`);
}
document.getElementById("route-card-a").addEventListener("click", () => {
focusRoute("a");
});
document.getElementById("route-card-b").addEventListener("click", () => {
focusRoute("b");
});
// -------------------------------------------------------------------------
// Initial render loop
// -------------------------------------------------------------------------
updateBlockedCount();
updateCamera();
recomputeRoutes();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
for (const id of Object.keys(entranceVisuals)) {
if (state.blocked[id]) {
const pulse = 1 + Math.sin(elapsed * 5.5) * 0.14;
entranceVisuals[id].beacon.scale.set(pulse, 1, pulse);
}
}
updateLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```