Prompt specification
Build or customize the Three.js 3D scene "Neighborhood at two display scales" 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: Neighborhood at two display scales
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/neighborhood-at-two-display-scales-x1323
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows map scales. Show matching neighborhood models side by side at different display scales. Preserve street relationships and compare equal real journeys using separate visible scale bars. Voxel style.
Working Three.js Starter (neighborhood-at-two-display-scales-x1323):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Street Scale — Voxel Neighborhood Comparison</title>
<style>
:root {
color-scheme: dark;
--bg: #071016;
--panel: #0d1921;
--panel-2: #101f28;
--line: rgba(154, 205, 218, 0.16);
--text: #e7f2f3;
--muted: #87a1aa;
--cyan: #54e6d2;
--cyan-dark: #167b78;
--yellow: #ffd166;
--orange: #ff8a5b;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background:
radial-gradient(circle at 10% 0%, rgba(40, 122, 132, 0.16), transparent 32rem),
radial-gradient(circle at 90% 10%, rgba(255, 138, 91, 0.09), transparent 28rem),
var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
body {
min-height: 100vh;
}
button,
input {
font: inherit;
}
.app {
width: min(1680px, 100%);
margin: 0 auto;
padding: 24px;
}
.topbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
margin-bottom: 18px;
}
.brand {
display: flex;
align-items: center;
gap: 14px;
min-width: 0;
}
.brand-mark {
position: relative;
width: 48px;
height: 48px;
flex: 0 0 auto;
border: 1px solid rgba(84, 230, 210, 0.35);
background:
linear-gradient(135deg, rgba(84, 230, 210, 0.18), transparent),
#0b181f;
border-radius: 12px;
overflow: hidden;
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
background: var(--cyan);
opacity: 0.85;
}
.brand-mark::before {
width: 34px;
height: 5px;
left: 7px;
top: 21px;
transform: rotate(-28deg);
}
.brand-mark::after {
width: 5px;
height: 34px;
left: 21px;
top: 7px;
transform: rotate(-28deg);
}
.eyebrow {
margin: 0 0 3px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(21px, 2.1vw, 31px);
line-height: 1;
letter-spacing: -0.035em;
}
.subtitle {
margin: 7px 0 0;
color: var(--muted);
font-size: 12px;
}
.toolbar {
display: flex;
align-items: center;
justify-content: flex-end;
gap: 9px;
flex-wrap: wrap;
}
.tool-button,
.toggle {
min-height: 38px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(14, 29, 37, 0.9);
color: var(--text);
cursor: pointer;
transition: border-color 160ms ease, background 160ms ease, transform 160ms ease;
}
.tool-button {
padding: 0 14px;
font-size: 12px;
font-weight: 700;
}
.tool-button:hover,
.toggle:hover {
border-color: rgba(84, 230, 210, 0.45);
background: #132630;
}
.tool-button:active {
transform: translateY(1px);
}
.toggle {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 0 11px;
color: #b9cbd0;
font-size: 11px;
font-weight: 700;
user-select: none;
}
.toggle input {
position: absolute;
opacity: 0;
pointer-events: none;
}
.toggle-track {
position: relative;
width: 27px;
height: 16px;
border-radius: 20px;
background: #26343b;
transition: background 160ms ease;
}
.toggle-track::after {
content: "";
position: absolute;
width: 12px;
height: 12px;
left: 2px;
top: 2px;
border-radius: 50%;
background: #8da0a6;
transition: transform 160ms ease, background 160ms ease;
}
.toggle input:checked + .toggle-track {
background: var(--cyan-dark);
}
.toggle input:checked + .toggle-track::after {
transform: translateX(11px);
background: var(--cyan);
}
.comparison-banner {
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
margin-bottom: 13px;
padding: 10px 14px;
border: 1px solid rgba(84, 230, 210, 0.14);
border-radius: 12px;
background:
linear-gradient(90deg, rgba(84, 230, 210, 0.08), transparent 45%),
rgba(11, 25, 33, 0.7);
}
.journey-key {
display: flex;
align-items: center;
gap: 10px;
min-width: 0;
}
.journey-line {
width: 58px;
height: 5px;
flex: 0 0 auto;
border-radius: 4px;
background: var(--cyan);
box-shadow: 0 0 18px rgba(84, 230, 210, 0.45);
}
.journey-key strong {
font-size: 12px;
letter-spacing: 0.03em;
}
.journey-key span,
.model-note {
color: var(--muted);
font-size: 11px;
}
.workspace {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 14px;
}
.map-card {
overflow: hidden;
border: 1px solid var(--line);
border-radius: 16px;
background: linear-gradient(180deg, rgba(17, 32, 41, 0.98), rgba(10, 21, 28, 0.98));
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.22);
}
.map-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
padding: 14px 16px 13px;
border-bottom: 1px solid var(--line);
}
.map-title-wrap {
min-width: 0;
}
.map-number {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.map-head h2 {
margin: 4px 0 0;
font-size: 16px;
letter-spacing: -0.015em;
}
.scale-factor {
min-width: 76px;
padding: 7px 10px;
border: 1px solid rgba(84, 230, 210, 0.2);
border-radius: 9px;
background: rgba(84, 230, 210, 0.07);
color: var(--cyan);
font-size: 12px;
font-weight: 800;
text-align: center;
font-variant-numeric: tabular-nums;
}
.viewport {
position: relative;
height: clamp(500px, 62vh, 680px);
overflow: hidden;
isolation: isolate;
background: #09151c;
}
.viewport canvas {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.viewport canvas:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: -3px;
}
.hud-chip {
position: absolute;
z-index: 3;
top: 14px;
left: 14px;
display: flex;
align-items: center;
gap: 8px;
padding: 8px 10px;
border: 1px solid rgba(151, 209, 218, 0.2);
border-radius: 9px;
background: rgba(6, 16, 22, 0.83);
backdrop-filter: blur(10px);
pointer-events: none;
}
.hud-chip .dot {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--cyan);
box-shadow: 0 0 12px rgba(84, 230, 210, 0.6);
}
.hud-chip strong {
font-size: 10px;
letter-spacing: 0.07em;
}
.compass {
position: absolute;
z-index: 3;
top: 14px;
right: 14px;
width: 44px;
height: 44px;
border: 1px solid rgba(151, 209, 218, 0.2);
border-radius: 50%;
background: rgba(6, 16, 22, 0.8);
backdrop-filter: blur(10px);
pointer-events: none;
}
.compass::before {
content: "N";
position: absolute;
top: 4px;
left: 0;
right: 0;
color: var(--muted);
font-size: 8px;
font-weight: 800;
text-align: center;
}
.compass-arrow {
position: absolute;
width: 0;
height: 0;
left: 50%;
top: 50%;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 17px solid var(--orange);
transform: translate(-50%, -38%);
transform-origin: 50% 65%;
}
.map-pin {
position: absolute;
z-index: 4;
left: 0;
top: 0;
padding: 5px 7px;
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 7px;
background: rgba(5, 13, 18, 0.9);
color: #eaf5f5;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -130%);
transition: opacity 100ms linear;
}
.map-pin.start {
border-color: rgba(84, 230, 210, 0.4);
}
.map-pin.end {
border-color: rgba(255, 138, 91, 0.45);
}
.legend {
position: absolute;
z-index: 3;
left: 14px;
bottom: 16px;
display: flex;
align-items: center;
gap: 13px;
padding: 8px 10px;
border: 1px solid rgba(151, 209, 218, 0.17);
border-radius: 9px;
background: rgba(6, 16, 22, 0.78);
backdrop-filter: blur(10px);
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
color: #b9cbd0;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.04em;
}
.swatch {
width: 13px;
height: 5px;
border-radius: 2px;
}
.swatch.route {
background: var(--cyan);
box-shadow: 0 0 8px rgba(84, 230, 210, 0.5);
}
.swatch.building {
background: #6c8392;
}
.swatch.park {
background: #5f9274;
}
.scale-box {
position: absolute;
z-index: 3;
right: 14px;
bottom: 16px;
width: 158px;
padding: 9px 10px 8px;
border: 1px solid rgba(151, 209, 218, 0.2);
border-radius: 10px;
background: rgba(6, 16, 22, 0.86);
backdrop-filter: blur(10px);
pointer-events: none;
}
.scale-top {
display: flex;
justify-content: space-between;
gap: 8px;
margin-bottom: 7px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.scale-rule {
position: relative;
width: 100px;
height: 9px;
border-left: 2px solid #e8f5f5;
border-right: 2px solid #e8f5f5;
border-bottom: 2px solid #e8f5f5;
transition: width 80ms linear;
}
.scale-labels {
display: flex;
justify-content: space-between;
margin-top: 4px;
color: #eef8f8;
font-size: 10px;
font-weight: 800;
font-variant-numeric: tabular-nums;
}
.scale-labels span:last-child {
color: var(--muted);
font-weight: 600;
}
.interaction-hint {
position: absolute;
z-index: 3;
left: 50%;
bottom: 18px;
padding: 7px 10px;
border: 1px solid rgba(151, 209, 218, 0.13);
border-radius: 20px;
background: rgba(6, 16, 22, 0.65);
color: #78939c;
font-size: 9px;
white-space: nowrap;
pointer-events: none;
transform: translateX(-50%);
}
.map-footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
padding: 11px 15px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
}
.map-footer strong {
color: #bfd0d5;
font-weight: 700;
}
.footer-tag {
flex: 0 0 auto;
color: var(--cyan);
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
@media (max-width: 1050px) {
.app {
padding: 16px;
}
.workspace {
grid-template-columns: 1fr;
}
.viewport {
height: min(68vh, 650px);
min-height: 470px;
}
.topbar {
align-items: flex-start;
}
.toolbar {
max-width: 440px;
}
}
@media (max-width: 650px) {
.app {
padding: 10px;
}
.topbar {
flex-direction: column;
gap: 14px;
}
.toolbar {
width: 100%;
max-width: none;
justify-content: flex-start;
}
.comparison-banner {
align-items: flex-start;
flex-direction: column;
gap: 7px;
}
.journey-key {
align-items: flex-start;
}
.journey-key span {
line-height: 1.45;
}
.viewport {
min-height: 440px;
}
.interaction-hint,
.legend {
display: none;
}
.scale-box {
right: 10px;
bottom: 10px;
}
.hud-chip {
top: 10px;
left: 10px;
}
.compass {
top: 10px;
right: 10px;
}
.map-footer {
align-items: flex-start;
flex-direction: column;
gap: 5px;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<p class="eyebrow">Voxel urban atlas</p>
<h1>Street Scale</h1>
<p class="subtitle">One neighborhood. One journey. Two display scales.</p>
</div>
</div>
<div class="toolbar" aria-label="Map controls">
<label class="toggle">
<input id="linkOrientation" type="checkbox" checked>
<span class="toggle-track" aria-hidden="true"></span>
Link orientation
</label>
<label class="toggle">
<input id="showLabels" type="checkbox" checked>
<span class="toggle-track" aria-hidden="true"></span>
Street labels
</label>
<button id="resetViews" class="tool-button" type="button">Reset views</button>
</div>
</header>
<section class="comparison-banner" aria-label="Journey comparison information">
<div class="journey-key">
<span class="journey-line" aria-hidden="true"></span>
<div>
<strong>Matched journey · 400 metres</strong><br>
<span>The same route is shown in both models; only the viewing scale changes.</span>
</div>
</div>
<div class="model-note">1 model unit = 1 metre · streets retain identical geometry</div>
</section>
<main class="workspace">
<article class="map-card">
<header class="map-head">
<div class="map-title-wrap">
<div class="map-number">Display 01 · Context</div>
<h2>Neighborhood overview</h2>
</div>
<div id="overviewFactor" class="scale-factor">1.00×</div>
</header>
<div id="overviewViewport" class="viewport">
<div class="hud-chip">
<span class="dot" aria-hidden="true"></span>
<strong>400 M · SAME JOURNEY</strong>
</div>
<div class="compass" aria-hidden="true">
<span class="compass-arrow"></span>
</div>
<div class="map-pin start">A · START</div>
<div class="map-pin end">B · END</div>
<div class="legend" aria-hidden="true">
<span class="legend-item"><i class="swatch route"></i>Journey</span>
<span class="legend-item"><i class="swatch building"></i>Buildings</span>
<span class="legend-item"><i class="swatch park"></i>Parks</span>
</div>
<div class="scale-box" aria-label="Scale bar for the overview map">
<div class="scale-top">
<span>On-screen scale</span>
<span class="pixel-ratio"></span>
</div>
<div class="scale-rule"></div>
<div class="scale-labels">
<span class="scale-value">100 m</span>
<span>0</span>
</div>
</div>
<div class="interaction-hint">Drag to orbit · Shift-drag to pan · Scroll to zoom</div>
</div>
<footer class="map-footer">
<span><strong>360 m</strong> vertical map window</span>
<span class="footer-tag">Full street network</span>
</footer>
</article>
<article class="map-card">
<header class="map-head">
<div class="map-title-wrap">
<div class="map-number">Display 02 · Detail</div>
<h2>Route-focused model</h2>
</div>
<div id="detailFactor" class="scale-factor">1.50×</div>
</header>
<div id="detailViewport" class="viewport">
<div class="hud-chip">
<span class="dot" aria-hidden="true"></span>
<strong>400 M · SAME JOURNEY</strong>
</div>
<div class="compass" aria-hidden="true">
<span class="compass-arrow"></span>
</div>
<div class="map-pin start">A · START</div>
<div class="map-pin end">B · END</div>
<div class="legend" aria-hidden="true">
<span class="legend-item"><i class="swatch route"></i>Journey</span>
<span class="legend-item"><i class="swatch building"></i>Buildings</span>
<span class="legend-item"><i class="swatch park"></i>Parks</span>
</div>
<div class="scale-box" aria-label="Scale bar for the detailed map">
<div class="scale-top">
<span>On-screen scale</span>
<span class="pixel-ratio"></span>
</div>
<div class="scale-rule"></div>
<div class="scale-labels">
<span class="scale-value">50 m</span>
<span>0</span>
</div>
</div>
<div class="interaction-hint">Drag to orbit · Shift-drag to pan · Scroll to zoom</div>
</div>
<footer class="map-footer">
<span><strong>240 m</strong> vertical map window</span>
<span class="footer-tag">Enlarged display scale</span>
</footer>
</article>
</main>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const X_ROADS = [-120, -60, 0, 60, 120];
const Z_ROADS = [-80, -40, 0, 40, 80];
const ROUTE_POINTS = [
new THREE.Vector3(-120, 0, 40),
new THREE.Vector3(-60, 0, 40),
new THREE.Vector3(-60, 0, 0),
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, 0, -40),
new THREE.Vector3(60, 0, -40),
new THREE.Vector3(60, 0, -80),
new THREE.Vector3(120, 0, -80),
new THREE.Vector3(120, 0, -40)
];
const INITIAL_YAW = 0.463647609;
const INITIAL_PITCH = 0.92;
function mulberry32(seed) {
return function () {
let t = seed += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
};
}
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.03,
...options
});
}
function addBox(parent, size, position, material, options = {}) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
material
);
mesh.position.copy(position);
mesh.castShadow = options.castShadow ?? true;
mesh.receiveShadow = options.receiveShadow ?? true;
if (options.rotationY) mesh.rotation.y = options.rotationY;
if (options.name) mesh.name = options.name;
parent.add(mesh);
return mesh;
}
function createStreetLabel(text, x, z, width = 86) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 96;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "rgba(5, 15, 21, 0.88)";
ctx.strokeStyle = "rgba(139, 205, 214, 0.35)";
ctx.lineWidth = 3;
ctx.beginPath();
ctx.roundRect(4, 5, 504, 86, 18);
ctx.fill();
ctx.stroke();
ctx.fillStyle = "#bcd5d9";
ctx.font = "800 32px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 49);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false,
depthTest: true
});
const sprite = new THREE.Sprite(material);
sprite.position.set(x, 8, z);
sprite.scale.set(width, width * 96 / 512, 1);
sprite.renderOrder = 4;
return sprite;
}
function createWorld(seed = 4271) {
const random = mulberry32(seed);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x09151c);
const hemisphere = new THREE.HemisphereLight(0xb8e6f4, 0x142129, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff0d8, 3.2);
sun.position.set(-120, 220, 95);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -190;
sun.shadow.camera.right = 190;
sun.shadow.camera.top = 170;
sun.shadow.camera.bottom = -170;
sun.shadow.camera.near = 10;
sun.shadow.camera.far = 520;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.35;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x77d9df, 1.15);
fill.position.set(150, 85, -140);
scene.add(fill);
const warmFill = new THREE.DirectionalLight(0xffa36f, 0.55);
warmFill.position.set(100, 45, 150);
scene.add(warmFill);
const baseMaterial = standardMaterial(0x0c1b22);
const frameMaterial = standardMaterial(0x1d3942, { metalness: 0.15 });
const asphaltMaterial = standardMaterial(0x17262d);
const sidewalkMaterial = standardMaterial(0x3a4c52);
const sidewalkEdgeMaterial = standardMaterial(0x53666a);
const laneMaterial = standardMaterial(0xa7b8b3, {
emissive: 0x60706b,
emissiveIntensity: 0.18
});
const parkMaterial = standardMaterial(0x4f7f65);
const pathMaterial = standardMaterial(0x9a9b7e);
const trunkMaterial = standardMaterial(0x664b38);
const leafMaterials = [
standardMaterial(0x4e8b69),
standardMaterial(0x6e9b71),
standardMaterial(0x3f7862)
];
const windowMaterial = standardMaterial(0x8dd9dc, {
emissive: 0x54bfc3,
emissiveIntensity: 0.75,
roughness: 0.35
});
const warmWindowMaterial = standardMaterial(0xffd17e, {
emissive: 0xffa83f,
emissiveIntensity: 0.6,
roughness: 0.4
});
const buildingPalette = [
0x536b78,
0x6e7e84,
0x7d7468,
0x55747a,
0x8c776b,
0x64748b,
0x8b8375,
0x48646c
];
const world = new THREE.Group();
scene.add(world);
addBox(
world,
new THREE.Vector3(330, 3, 250),
new THREE.Vector3(0, -1.8, 0),
baseMaterial
);
addBox(
world,
new THREE.Vector3(334, 1.1, 254),
new THREE.Vector3(0, -2.8, 0),
frameMaterial,
{ castShadow: false }
);
// Roads.
for (const x of X_ROADS) {
addBox(
world,
new THREE.Vector3(13, 0.45, 235),
new THREE.Vector3(x, 0.15, 0),
asphaltMaterial,
{ castShadow: false }
);
}
for (const z of Z_ROADS) {
addBox(
world,
new THREE.Vector3(305, 0.45, 13),
new THREE.Vector3(0, 0.16, z),
asphaltMaterial,
{ castShadow: false }
);
}
// Lane dashes.
for (const x of X_ROADS) {
for (let z = -108; z <= 108; z += 18) {
const nearCrossing = Z_ROADS.some(rz => Math.abs(rz - z) < 11);
if (nearCrossing) continue;
addBox(
world,
new THREE.Vector3(0.8, 0.12, 6),
new THREE.Vector3(x, 0.43, z),
laneMaterial,
{ castShadow: false }
);
}
}
for (const z of Z_ROADS) {
for (let x = -143; x <= 143; x += 18) {
const nearCrossing = X_ROADS.some(rx => Math.abs(rx - x) < 11);
if (nearCrossing) continue;
addBox(
world,
new THREE.Vector3(6, 0.12, 0.8),
new THREE.Vector3(x, 0.44, z),
laneMaterial,
{ castShadow: false }
);
}
}
// Block slabs and voxel buildings.
for (let bi = 0; bi < X_ROADS.length - 1; bi++) {
for (let bj = 0; bj < Z_ROADS.length - 1; bj++) {
const cx = (X_ROADS[bi] + X_ROADS[bi + 1]) / 2;
const cz = (Z_ROADS[bj] + Z_ROADS[bj + 1] - Z_ROADS[bj]) / 2;
const centerZ = (Z_ROADS[bj] + Z_ROADS[bj + 1]) / 2;
addBox(
world,
new THREE.Vector3(46, 0.9, 26),
new THREE.Vector3(cx, 0.55, centerZ),
sidewalkMaterial
);
const isPark = (bi === 1 && bj === 0) ||
(bi === 3 && bj === 2) ||
(bi === 0 && bj === 3);
if (isPark) {
addBox(
world,
new THREE.Vector3(44, 0.35, 24),
new THREE.Vector3(cx, 1.08, centerZ),
parkMaterial,
{ castShadow: false }
);
addBox(
world,
new THREE.Vector3(44, 0.18, 2.2),
new THREE.Vector3(cx, 1.3, centerZ),
pathMaterial,
{ castShadow: false }
);
addBox(
world,
new THREE.Vector3(2.2, 0.18, 24),
new THREE.Vector3(cx, 1.31, centerZ),
pathMaterial,
{ castShadow: false }
);
for (let t = 0; t < 9; t++) {
const tx = cx + (random() - 0.5) * 35;
const tz = centerZ + (random() - 0.5) * 17;
addBox(
world,
new THREE.Vector3(1.4, 4, 1.4),
new THREE.Vector3(tx, 3.1, tz),
trunkMaterial
);
addBox(
world,
new THREE.Vector3(5.3, 5.3, 5.3),
new THREE.Vector3(tx, 6.6, tz),
leafMaterials[Math.floor(random() * leafMaterials.length)]
);
addBox(
world,
new THREE.Vector3(3.4, 2.7, 3.4),
new THREE.Vector3(tx, 9.5, tz),
leafMaterials[Math.floor(random() * leafMaterials.length)]
);
}
continue;
}
for (let ix = 0; ix < 2; ix++) {
for (let iz = 0; iz < 2; iz++) {
const bx = cx + (ix === 0 ? -11.5 : 11.5);
const bz = centerZ + (iz === 0 ? -6.5 : 6.5);
const width = 15 + random() * 3.5;
const depth = 7.5 + random() * 2.3;
const height = 7 + random() * 20;
const landmark = bi === 2 && bj === 2 && ix === 1 && iz === 1;
const finalHeight = landmark ? 34 : height;
const color = buildingPalette[Math.floor(random() * buildingPalette.length)];
const buildingMaterial = standardMaterial(color);
addBox(
world,
new THREE.Vector3(width, finalHeight, depth),
new THREE.Vector3(bx, 1 + finalHeight / 2, bz),
buildingMaterial
);
addBox(
world,
new THREE.Vector3(width + 0.6, 1.1, depth + 0.6),
new THREE.Vector3(bx, 1 + finalHeight + 0.55, bz),
standardMaterial(0x30444c)
);
if (random() > 0.35) {
addBox(
world,
new THREE.Vector3(3.2, 1.8, 2.8),
new THREE.Vector3(
bx + (random() - 0.5) * width * 0.35,
1 + finalHeight + 1.9,
bz + (random() - 0.5) * depth * 0.3
),
standardMaterial(0x56676b)
);
}
const levels = Math.max(1, Math.floor(finalHeight / 5.2));
for (let level = 0; level < levels; level++) {
const y = 3.2 + level * 5.0;
if (y > finalHeight - 0.5) continue;
const mat = random() > 0.76 ? warmWindowMaterial : windowMaterial;
addBox(
world,
new THREE.Vector3(width * 0.68, 0.7, 0.3),
new THREE.Vector3(bx, y, bz + depth / 2 + 0.18),
mat,
{ castShadow: false }
);
addBox(
world,
new THREE.Vector3(width * 0.68, 0.7, 0.3),
new THREE.Vector3(bx, y, bz - depth / 2 - 0.18),
mat,
{ castShadow: false }
);
addBox(
world,
new THREE.Vector3(0.3, 0.7, depth * 0.62),
new THREE.Vector3(bx + width / 2 + 0.18, y, bz),
mat,
{ castShadow: false }
);
}
}
}
}
}
// Small vehicles.
const carColors = [
0xff725e,
0xf3c969,
0x74b8d0,
0xe7e7df,
0x5f7c86
];
for (let i = 0; i < 18; i++) {
const color = carColors[Math.floor(random() * carColors.length)];
const material = standardMaterial(color, { roughness: 0.5 });
const vertical = random() > 0.5;
const roadIndex = Math.floor(random() * X_ROADS.length);
if (vertical) {
const x = X_ROADS[roadIndex] + (random() > 0.5 ? 3.1 : -3.1);
const z = -105 + random() * 210;
addBox(
world,
new THREE.Vector3(3.1, 2, 6.2),
new THREE.Vector3(x, 1.35, z),
material
);
addBox(
world,
new THREE.Vector3(2.4, 0.7, 2.7),
new THREE.Vector3(x, 2.65, z),
standardMaterial(0x17323c),
{ castShadow: false }
);
} else {
const z = Z_ROADS[roadIndex] + (random() > 0.5 ? 3.1 : -3.1);
const x = -135 + random() * 270;
addBox(
world,
new THREE.Vector3(6.2, 2, 3.1),
new THREE.Vector3(x, 1.35, z),
material
);
addBox(
world,
new THREE.Vector3(2.7, 0.7, 2.4),
new THREE.Vector3(x, 2.65, z),
standardMaterial(0x17323c),
{ castShadow: false }
);
}
}
// Street lamps.
const lampMaterial = standardMaterial(0xffdd9a, {
emissive: 0xffb44e,
emissiveIntensity: 1.25
});
for (const x of X_ROADS) {
for (let z = -100; z <= 100; z += 40) {
addBox(
world,
new THREE.Vector3(0.7, 6, 0.7),
new THREE.Vector3(x + 7.3, 3.3, z),
standardMaterial(0x48585d),
{ castShadow: false }
);
addBox(
world,
new THREE.Vector3(1.7, 0.8, 1.7),
new THREE.Vector3(x + 7.3, 6.6, z),
lampMaterial,
{ castShadow: false }
);
}
}
// Street labels.
const labels = [
createStreetLabel("NORTH AVENUE", -32, 80, 92),
createStreetLabel("CANAL STREET", 32, 40, 84),
createStreetLabel("MARKET STREET", -32, -80, 88)
];
labels.forEach(label => world.add(label));
// Journey route: exactly 400 metres.
const routeGroup = new THREE.Group();
world.add(routeGroup);
const routeUnderlayMaterial = new THREE.MeshBasicMaterial({
color: 0x54e6d2,
transparent: true,
opacity: 0.2,
depthWrite: false
});
const routeMaterial = standardMaterial(0x64f3dc, {
emissive: 0x27cdbb,
emissiveIntensity: 1.45,
roughness: 0.38
});
const routeSegments = [];
let routeLength = 0;
for (let i = 0; i < ROUTE_POINTS.length - 1; i++) {
const a = ROUTE_POINTS[i];
const b = ROUTE_POINTS[i + 1];
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz);
const angle = -Math.atan2(dz, dx);
const center = a.clone().add(b).multiplyScalar(0.5);
routeSegments.push({ a, b, length, start: routeLength });
routeLength += length;
addBox(
routeGroup,
new THREE.Vector3(length + 1, 0.22, 9),
new THREE.Vector3(center.x, 0.72, center.z),
routeUnderlayMaterial,
{ castShadow: false, rotationY: angle }
);
addBox(
routeGroup,
new THREE.Vector3(length + 0.5, 0.38, 4.6),
new THREE.Vector3(center.x, 0.82, center.z),
routeMaterial,
{ castShadow: false, rotationY: angle }
);
}
for (let i = 1; i < ROUTE_POINTS.length - 1; i++) {
addBox(
routeGroup,
new THREE.Vector3(6.6, 0.45, 6.6),
new THREE.Vector3(ROUTE_POINTS[i].x, 0.89, ROUTE_POINTS[i].z),
routeMaterial,
{ castShadow: false }
);
}
const startMaterial = standardMaterial(0x73f4db, {
emissive: 0x25cdb5,
emissiveIntensity: 1.3
});
const endMaterial = standardMaterial(0xff9b67, {
emissive: 0xf26c3d,
emissiveIntensity: 1.15
});
const start = ROUTE_POINTS[0];
const end = ROUTE_POINTS[ROUTE_POINTS.length - 1];
addBox(
routeGroup,
new THREE.Vector3(3.2, 12, 3.2),
new THREE.Vector3(start.x, 6.4, start.z),
startMaterial
);
addBox(
routeGroup,
new THREE.Vector3(3.2, 12, 3.2),
new THREE.Vector3(end.x, 6.4, end.z),
endMaterial
);
const traveler = addBox(
routeGroup,
new THREE.Vector3(4.6, 4.6, 4.6),
new THREE.Vector3(start.x, 4.3, start.z),
standardMaterial(0xffd166, {
emissive: 0xffa62e,
emissiveIntensity: 1.25,
roughness: 0.35
}),
{ name: "traveler" }
);
function pointAtDistance(distance) {
const d = THREE.MathUtils.clamp(distance, 0, routeLength);
for (const segment of routeSegments) {
if (d <= segment.start + segment.length) {
const t = (d - segment.start) / segment.length;
return segment.a.clone().lerp(segment.b, t);
}
}
return routeSegments[routeSegments.length - 1].b.clone();
}
return {
scene,
traveler,
pointAtDistance,
routeLength,
routeMaterial,
routeUnderlayMaterial,
labels,
start,
end
};
}
function createRenderer(container) {
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.shadowMap.autoUpdate = false;
renderer.shadowMap.needsUpdate = true;
renderer.domElement.setAttribute("tabindex", "0");
renderer.domElement.setAttribute("role", "img");
container.prepend(renderer.domElement);
return renderer;
}
class MapView {
constructor(options) {
this.options = options;
this.container = options.container;
this.renderer = createRenderer(this.container);
this.world = createWorld(options.seed);
this.camera = new THREE.OrthographicCamera(-100, 100, 100, -100, 1, 900);
this.state = {
yaw: INITIAL_YAW,
pitch: INITIAL_PITCH,
zoom: 1,
target: options.target.clone()
};
this.initialTarget = options.target.clone();
this.baseHeight = options.baseHeight;
this.radius = 480;
this.dirty = true;
this.pointers = new Map();
this.pinchDistance = 0;
this.pinchZoom = 1;
this.startPin = this.container.querySelector(".map-pin.start");
this.endPin = this.container.querySelector(".map-pin.end");
this.scaleRule = this.container.querySelector(".scale-rule");
this.scaleValue = this.container.querySelector(".scale-value");
this.pixelRatio = this.container.querySelector(".pixel-ratio");
this.compassArrow = this.container.querySelector(".compass-arrow");
this.factorElement = options.factorElement;
this.renderer.domElement.setAttribute(
"aria-label",
options.ariaLabel
);
this.bindControls();
this.resizeObserver = new ResizeObserver(() => this.resize());
this.resizeObserver.observe(this.container);
this.resize();
this.updateCamera();
}
resize() {
const width = Math.max(1, this.container.clientWidth);
const height = Math.max(1, this.container.clientHeight);
const aspect = width / height;
this.camera.left = -this.baseHeight * aspect / 2;
this.camera.right = this.baseHeight * aspect / 2;
this.camera.top = this.baseHeight / 2;
this.camera.bottom = -this.baseHeight / 2;
this.camera.updateProjectionMatrix();
this.renderer.setSize(width, height, false);
this.dirty = true;
this.updateScaleBar();
}
updateCamera() {
const { yaw, pitch, zoom, target } = this.state;
const horizontal = Math.cos(pitch) * this.radius;
this.camera.position.set(
target.x + Math.sin(yaw) * horizontal,
target.y + Math.sin(pitch) * this.radius,
target.z + Math.cos(yaw) * horizontal
);
this.camera.lookAt(target);
this.camera.zoom = zoom;
this.camera.updateProjectionMatrix();
this.camera.updateMatrixWorld();
if (this.compassArrow) {
this.compassArrow.style.transform =
`translate(-50%, -38%) rotate(${-yaw}rad)`;
}
this.dirty = false;
this.updateScaleBar();
}
updateScaleBar() {
const height = Math.max(1, this.container.clientHeight);
const visibleWorldHeight = this.baseHeight / this.state.zoom;
const metresPerPixel = visibleWorldHeight / height;
const targetPixels = 150;
const rawDistance = targetPixels * metresPerPixel;
const magnitude = Math.pow(10, Math.floor(Math.log10(rawDistance)));
const candidates = [1, 2, 5, 10].map(n => n * magnitude);
let selected = candidates[0];
for (const candidate of candidates) {
if (Math.abs(candidate - rawDistance) < Math.abs(selected - rawDistance)) {
selected = candidate;
}
}
const pixelLength = selected / metresPerPixel;
const label = selected >= 1000
? `${(selected / 1000).toFixed(selected % 1000 === 0 ? 0 : 1)} km`
: `${Math.round(selected)} m`;
this.scaleRule.style.width = `${Math.min(pixelLength, this.container.clientWidth - 42)}px`;
this.scaleValue.textContent = label;
this.pixelRatio.textContent = `${metresPerPixel.toFixed(2)} m/px`;
}
setZoom(nextZoom) {
this.state.zoom = THREE.MathUtils.clamp(nextZoom, 0.55, 3.2);
this.dirty = true;
}
orbit(dx, dy) {
this.state.yaw -= dx * 0.006;
this.state.pitch = THREE.MathUtils.clamp(
this.state.pitch + dy * 0.005,
0.48,
1.36
);
this.dirty = true;
}
pan(dx, dy) {
const height = Math.max(1, this.container.clientHeight);
const visibleWorldHeight = this.baseHeight / this.state.zoom;
const worldPerPixel = visibleWorldHeight / height;
const yaw = this.state.yaw;
const right = new THREE.Vector3(Math.cos(yaw), 0, -Math.sin(yaw));
const forward = new THREE.Vector3(-Math.sin(yaw), 0, -Math.cos(yaw));
this.state.target.addScaledVector(right, -dx * worldPerPixel);
this.state.target.addScaledVector(forward, -dy * worldPerPixel);
this.state.target.x = THREE.MathUtils.clamp(this.state.target.x, -180, 180);
this.state.target.z = THREE.MathUtils.clamp(this.state.target.z, -140, 140);
this.dirty = true;
}
syncOrientationFrom(other) {
this.state.yaw = other.state.yaw;
this.state.pitch = other.state.pitch;
this.dirty = true;
}
reset() {
this.state.yaw = INITIAL_YAW;
this.state.pitch = INITIAL_PITCH;
this.state.zoom = 1;
this.state.target.copy(this.initialTarget);
this.dirty = true;
}
bindControls() {
const canvas = this.renderer.domElement;
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("pointerdown", event => {
canvas.focus({ preventScroll: true });
canvas.setPointerCapture(event.pointerId);
this.pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (this.pointers.size === 2) {
const [a, b] = [...this.pointers.values()];
this.pinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
this.pinchZoom = this.state.zoom;
}
});
canvas.addEventListener("pointermove", event => {
const previous = this.pointers.get(event.pointerId);
if (!previous) return;
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
previous.x = event.clientX;
previous.y = event.clientY;
if (this.pointers.size === 1) {
if (event.shiftKey || event.buttons === 2 || event.buttons === 4) {
this.pan(dx, dy);
} else {
this.orbit(dx, dy);
}
} else if (this.pointers.size === 2) {
const [a, b] = [...this.pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (this.pinchDistance > 0) {
this.setZoom(this.pinchZoom * distance / this.pinchDistance);
}
}
});
const releasePointer = event => {
this.pointers.delete(event.pointerId);
if (this.pointers.size < 2) this.pinchDistance = 0;
};
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener("lostpointercapture", releasePointer);
canvas.addEventListener("wheel", event => {
event.preventDefault();
const factor = Math.exp(-event.deltaY * 0.0012);
this.setZoom(this.state.zoom * factor);
}, { passive: false });
canvas.addEventListener("keydown", event => {
let handled = true;
if (event.key === "ArrowLeft") this.orbit(24, 0);
else if (event.key === "ArrowRight") this.orbit(-24, 0);
else if (event.key === "ArrowUp") this.orbit(0, -24);
else if (event.key === "ArrowDown") this.orbit(0, 24);
else if (event.key === "+" || event.key === "=") this.setZoom(this.state.zoom * 1.1);
else if (event.key === "-" || event.key === "_") this.setZoom(this.state.zoom / 1.1);
else handled = false;
if (handled) event.preventDefault();
});
}
placePin(element, worldPosition) {
const projected = worldPosition.clone();
projected.y += 13;
projected.project(this.camera);
const x = (projected.x * 0.5 + 0.5) * this.container.clientWidth;
const y = (-projected.y * 0.5 + 0.5) * this.container.clientHeight;
const visible = projected.z > -1 && projected.z < 1 &&
x > -40 && x < this.container.clientWidth + 40 &&
y > -40 && y < this.container.clientHeight + 40;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.opacity = visible ? "1" : "0";
}
update(elapsed, labelsVisible) {
if (this.dirty) this.updateCamera();
const progress = (elapsed % 28) / 28;
const distance = progress * this.world.routeLength;
const point = this.world.pointAtDistance(distance);
this.world.traveler.position.set(point.x, 4.4 + Math.sin(elapsed * 5) * 0.35, point.z);
this.world.traveler.rotation.y = elapsed * 1.4;
this.world.routeMaterial.emissiveIntensity = 1.35 + Math.sin(elapsed * 2.4) * 0.25;
this.world.routeUnderlayMaterial.opacity = 0.17 + Math.sin(elapsed * 2.4) * 0.045;
this.world.labels.forEach(label => {
label.visible = labelsVisible;
});
this.placePin(this.startPin, this.world.start);
this.placePin(this.endPin, this.world.end);
this.renderer.render(this.world.scene, this.camera);
}
}
const overview = new MapView({
container: document.getElementById("overviewViewport"),
factorElement: document.getElementById("overviewFactor"),
target: new THREE.Vector3(0, 0, 0),
baseHeight: 360,
seed: 4271,
ariaLabel: "Overview of a voxel neighborhood with a highlighted 400 metre journey"
});
const detail = new MapView({
container: document.getElementById("detailViewport"),
factorElement: document.getElementById("detailFactor"),
target: new THREE.Vector3(0, 0, -20),
baseHeight: 240,
seed: 4271,
ariaLabel: "Detailed view of the same voxel neighborhood and 400 metre journey"
});
const views = [overview, detail];
const linkOrientation = document.getElementById("linkOrientation");
const showLabels = document.getElementById("showLabels");
function synchronizeOrientation(source) {
if (!linkOrientation.checked) return;
for (const view of views) {
if (view !== source) view.syncOrientationFrom(source);
}
}
for (const view of views) {
const canvas = view.renderer.domElement;
canvas.addEventListener("pointerup", () => {
synchronizeOrientation(view);
});
canvas.addEventListener("wheel", () => {
window.requestAnimationFrame(() => synchronizeOrientation(view));
}, { passive: true });
canvas.addEventListener("keydown", () => {
window.requestAnimationFrame(() => synchronizeOrientation(view));
});
}
document.getElementById("resetViews").addEventListener("click", () => {
views.forEach(view => view.reset());
overview.factorElement.textContent = "1.00×";
detail.factorElement.textContent = "1.50×";
});
showLabels.addEventListener("change", () => {
views.forEach(view => {
view.world.labels.forEach(label => {
label.visible = showLabels.checked;
});
});
});
let previousTime = performance.now();
function animate(now) {
const elapsed = now / 1000;
const delta = Math.min((now - previousTime) / 1000, 0.1);
previousTime = now;
const relativeDetailScale =
(overview.baseHeight / detail.baseHeight) *
(detail.state.zoom / overview.state.zoom);
overview.factorElement.textContent = `${overview.state.zoom.toFixed(2)}×`;
detail.factorElement.textContent = `${relativeDetailScale.toFixed(2)}×`;
views.forEach(view => {
view.update(elapsed, showLabels.checked);
});
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```