Prompt specification
Build or customize the Three.js 3D scene "Map-scale miniature town exhibit" 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: Map-scale miniature town exhibit
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/map-scale-miniature-town-exhibit-x1731
Scene Description Prompt:
Create a standalone voxel scene in one HTML file using Three.js from cdnjs. Compose a small street model beside length bars showing a fixed map-to-ground ratio; distinguish scale factor from actual scene-unit choice and label the town invented.
Working Three.js Starter (map-scale-miniature-town-exhibit-x1731):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Morrow Blocks — Invented Town Scale Study</title>
<style>
:root {
color-scheme: light;
--ink: #17252d;
--muted: #62727a;
--paper: rgba(255, 255, 255, 0.9);
--line: rgba(23, 37, 45, 0.13);
--teal: #087f80;
--gold: #e9a72e;
--purple: #7156a5;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 45% 35%, #f8faf6 0, #e9efe9 52%, #dce6df 100%);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#stage, #labels {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#stage canvas:active { cursor: grabbing; }
#labels {
pointer-events: none;
overflow: hidden;
z-index: 2;
}
.label {
position: absolute;
left: 0;
top: 0;
transform: translate(-50%, -50%);
white-space: nowrap;
pointer-events: none;
will-change: transform;
}
.label.town {
padding: 8px 12px;
border-radius: 9px;
background: rgba(255,255,255,.92);
border: 1px solid rgba(23,37,45,.12);
box-shadow: 0 7px 24px rgba(23,37,45,.13);
text-align: center;
font-size: 11px;
font-weight: 800;
letter-spacing: .11em;
color: var(--ink);
}
.label.town small {
display: block;
margin-top: 3px;
color: var(--teal);
font-size: 9px;
letter-spacing: .15em;
}
.label.street {
padding: 4px 7px;
border-radius: 4px;
color: #f5f7f4;
background: rgba(23,37,45,.72);
font-size: 8px;
font-weight: 800;
letter-spacing: .12em;
box-shadow: 0 3px 10px rgba(0,0,0,.12);
}
.label.scale-title {
color: var(--ink);
font-size: 11px;
font-weight: 900;
letter-spacing: .13em;
text-align: center;
text-shadow: 0 1px 0 white, 0 0 8px white;
}
.label.scale-title small {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 9px;
letter-spacing: .08em;
font-weight: 700;
}
.label.ruler {
padding: 3px 5px;
border-radius: 4px;
background: rgba(255,255,255,.88);
border: 1px solid rgba(23,37,45,.09);
color: var(--ink);
font-size: 9px;
font-weight: 800;
letter-spacing: .04em;
box-shadow: 0 2px 8px rgba(23,37,45,.08);
}
.label.ruler.map {
color: #875b0d;
border-color: rgba(233,167,46,.38);
}
.label.ruler.ground {
color: #075d5e;
border-color: rgba(8,127,128,.25);
}
.label.ruler.scene {
color: #59428b;
border-color: rgba(113,86,165,.28);
}
.hud {
position: fixed;
z-index: 4;
pointer-events: none;
}
.hud > * { pointer-events: auto; }
#title {
top: 22px;
left: 24px;
max-width: 350px;
}
#title .kicker {
display: inline-flex;
align-items: center;
gap: 8px;
margin-bottom: 7px;
padding: 5px 8px;
border: 1px solid rgba(8,127,128,.2);
border-radius: 999px;
background: rgba(255,255,255,.8);
color: var(--teal);
font-size: 9px;
font-weight: 900;
letter-spacing: .13em;
text-transform: uppercase;
}
#title .kicker::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--gold);
}
#title h1 {
margin: 0;
font-size: clamp(28px, 3.4vw, 48px);
line-height: .95;
letter-spacing: -.055em;
}
#title p {
margin: 9px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
#scale-card {
right: 22px;
bottom: 22px;
width: min(365px, calc(100vw - 32px));
padding: 18px;
border: 1px solid rgba(23,37,45,.11);
border-radius: 17px;
background: var(--paper);
box-shadow: 0 18px 55px rgba(23,37,45,.16);
backdrop-filter: blur(15px);
}
.card-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 11px;
}
.card-head strong {
font-size: 10px;
letter-spacing: .14em;
text-transform: uppercase;
}
.fixed-pill {
padding: 4px 7px;
border-radius: 999px;
background: rgba(8,127,128,.1);
color: var(--teal);
font-size: 9px;
font-weight: 900;
letter-spacing: .1em;
}
.ratio {
display: flex;
align-items: baseline;
gap: 11px;
margin-bottom: 10px;
}
.ratio b {
font-size: 34px;
line-height: 1;
letter-spacing: -.04em;
}
.ratio span {
color: var(--muted);
font-size: 11px;
line-height: 1.35;
}
.equation {
display: grid;
grid-template-columns: 1fr auto 1fr auto 1fr;
align-items: center;
gap: 5px;
margin: 13px 0;
padding: 10px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(241,245,241,.72);
text-align: center;
}
.equation i {
color: #9aa6aa;
font-style: normal;
font-weight: 900;
}
.equation span {
font-size: 10px;
font-weight: 800;
line-height: 1.2;
}
.equation small {
display: block;
margin-bottom: 3px;
color: var(--muted);
font-size: 8px;
font-weight: 700;
text-transform: uppercase;
letter-spacing: .08em;
}
#unit-buttons {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
margin: 8px 0 11px;
}
#unit-buttons button {
appearance: none;
border: 1px solid rgba(23,37,45,.13);
border-radius: 8px;
padding: 8px 5px;
background: #fff;
color: var(--ink);
cursor: pointer;
font: inherit;
font-size: 10px;
font-weight: 800;
transition: .15s ease;
}
#unit-buttons button:hover {
transform: translateY(-1px);
border-color: rgba(8,127,128,.4);
}
#unit-buttons button.active {
border-color: var(--teal);
background: var(--teal);
color: white;
box-shadow: 0 5px 14px rgba(8,127,128,.2);
}
.explanation {
margin: 0;
padding-top: 11px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
line-height: 1.55;
}
.explanation strong { color: var(--ink); }
#hint {
left: 24px;
bottom: 22px;
display: flex;
align-items: center;
gap: 9px;
padding: 8px 11px;
border: 1px solid rgba(23,37,45,.1);
border-radius: 999px;
background: rgba(255,255,255,.76);
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: .03em;
backdrop-filter: blur(10px);
}
#hint b {
color: var(--ink);
font-weight: 900;
}
@media (max-width: 760px) {
#title {
top: 15px;
left: 16px;
}
#title h1 {
font-size: 31px;
}
#title p {
max-width: 240px;
font-size: 10px;
}
#scale-card {
right: 12px;
bottom: 12px;
padding: 13px;
}
#hint {
display: none;
}
.label.street {
display: none;
}
.label.ruler {
padding: 2px 3px;
font-size: 7px;
}
}
</style>
</head>
<body>
<main id="stage" aria-label="Interactive invented voxel town and scale diagram"></main>
<div id="labels" aria-hidden="true"></div>
<header id="title" class="hud">
<div class="kicker">Invented town · scale study</div>
<h1>Morrow Blocks</h1>
<p>A fictional voxel neighborhood beside a fixed 1:100 map-to-ground scale.</p>
</header>
<aside id="scale-card" class="hud" aria-label="Scale explanation">
<div class="card-head">
<strong>Scale contract</strong>
<span class="fixed-pill">RATIO FIXED</span>
</div>
<div class="ratio">
<b>1 : 100</b>
<span>map centimetres<br>to ground metres</span>
</div>
<div class="equation">
<span><small>Map</small>1 cm</span>
<i>→</i>
<span><small>Ground</small>1 m</span>
<i>→</i>
<span><small>Scene</small><span id="scene-equation">1 unit</span></span>
</div>
<div class="card-head" style="margin-bottom:6px">
<strong>Actual scene-unit choice</strong>
</div>
<div id="unit-buttons" aria-label="Choose the value of one Three.js scene unit">
<button type="button" data-unit="1" class="active">1 unit = 1 m</button>
<button type="button" data-unit="2">1 unit = 2 m</button>
<button type="button" data-unit="5">1 unit = 5 m</button>
</div>
<p class="explanation">
<strong>The 1:100 factor describes map versus ground.</strong>
The scene-unit setting only changes how many Three.js coordinates represent
one ground metre. It does <strong>not</strong> change the map ratio.
</p>
</aside>
<div id="hint" class="hud">
<b>DRAG</b><span>orbit</span>
<b>SHIFT + DRAG</b><span>pan</span>
<b>SCROLL</b><span>zoom</span>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.querySelector("#stage");
const labelsLayer = document.querySelector("#labels");
const unitButtons = [...document.querySelectorAll("#unit-buttons button")];
const sceneEquation = document.querySelector("#scene-equation");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xe9efe9, 1500, 3400);
const camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0.1, 5000);
const hemi = new THREE.HemisphereLight(0xf9fff5, 0x6b7e75, 2.25);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff2d6, 3.0);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.bias = -0.00025;
sun.shadow.normalBias = 1.4;
scene.add(sun);
scene.add(sun.target);
const worldRoot = new THREE.Group();
scene.add(worldRoot);
const clock = new THREE.Clock();
const labels = [];
const cars = [];
const fixedMapToGround = 100;
let metersPerSceneUnit = 1;
let worldScale = 1;
let baseViewHeight = 780;
let aspect = window.innerWidth / window.innerHeight;
const cameraTarget = new THREE.Vector3(50, 35, 0);
let cameraTheta = 0.74;
let cameraPhi = 0.76;
let cameraZoom = 1;
function mat(color, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.88,
metalness: 0,
...extra
});
}
function pbox(w, h, d, x, y, z, material, parent = worldRoot) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(w, h, d),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = h > 1.5;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addLabel(html, parent, position, className = "") {
const element = document.createElement("div");
element.className = `label ${className}`;
element.innerHTML = html;
labelsLayer.appendChild(element);
labels.push({
element,
parent,
position: position.clone()
});
}
function disposeWorld() {
const geometries = new Set();
const materials = new Set();
worldRoot.traverse(object => {
if (object.geometry) geometries.add(object.geometry);
if (object.material) {
const list = Array.isArray(object.material)
? object.material
: [object.material];
list.forEach(material => materials.add(material));
}
});
geometries.forEach(geometry => geometry.dispose());
materials.forEach(material => material.dispose());
worldRoot.clear();
labelsLayer.replaceChildren();
labels.length = 0;
cars.length = 0;
}
function seededRandom(seed) {
let value = seed >>> 0;
return () => {
value = (1664525 * value + 1013904223) >>> 0;
return value / 4294967296;
};
}
function formatNumber(value) {
if (Math.abs(value - Math.round(value)) < 0.0001) {
return String(Math.round(value));
}
return value.toFixed(value < 10 ? 2 : 1).replace(/\.0+$/, "");
}
function buildTown() {
const random = seededRandom(73421);
const palette = {
floor: mat(0xdfe8df),
base: mat(0x566b68),
land: mat(0x9eb985),
park: mat(0x8eaf78),
sidewalk: mat(0xd6d5c9),
asphalt: mat(0x3d4a4d),
lane: mat(0xf0dfaa),
crosswalk: mat(0xf5f1df),
roof: mat(0x596b70),
window: mat(0x294b59, {
roughness: 0.42,
emissive: 0x102a33,
emissiveIntensity: 0.32
}),
trunk: mat(0x77543d),
leaf: mat(0x4f8159),
leafLight: mat(0x6d9a63),
pole: mat(0x405154),
lamp: mat(0xffedaf, {
emissive: 0xffcc55,
emissiveIntensity: 0.55
}),
plaza: mat(0xc4c2aa),
water: mat(0x58a7ae, { roughness: 0.32 })
};
const buildingMaterials = [
mat(0xd7b58b),
mat(0xc88f72),
mat(0xdfc79c),
mat(0xa8b8aa),
mat(0xbfc7c2),
mat(0xd9a58f)
];
const town = new THREE.Group();
town.position.x = -300;
worldRoot.add(town);
// Large ground plane and the raised town plinth.
pbox(2200, 6, 1300, 50, -24, 0, palette.floor);
pbox(640, 18, 440, 0, -9, 0, palette.base, town);
pbox(628, 4, 428, 0, 1.2, 0, palette.land, town);
const verticalRoads = [-240, -80, 80, 240];
const horizontalRoads = [-145, 0, 145];
// Sidewalk underlay and road surfaces.
verticalRoads.forEach(x => {
pbox(45, 2.2, 430, x, 3.0, 0, palette.sidewalk, town);
pbox(30, 1.2, 430, x, 4.4, 0, palette.asphalt, town);
});
horizontalRoads.forEach(z => {
pbox(630, 2.2, 45, 0, 3.0, z, palette.sidewalk, town);
pbox(630, 1.2, 30, 0, 4.4, z, palette.asphalt, town);
});
// Dashed lane markings.
verticalRoads.forEach(x => {
for (let z = -205; z <= 205; z += 42) {
const nearIntersection = horizontalRoads.some(r => Math.abs(z - r) < 28);
if (!nearIntersection) {
pbox(1.4, 0.45, 15, x, 5.15, z, palette.lane, town);
}
}
});
horizontalRoads.forEach(z => {
for (let x = -302; x <= 302; x += 42) {
const nearIntersection = verticalRoads.some(r => Math.abs(x - r) < 25);
if (!nearIntersection) {
pbox(15, 0.45, 1.4, x, 5.15, z, palette.lane, town);
}
}
});
// Crosswalks at intersections.
verticalRoads.forEach(x => {
horizontalRoads.forEach(z => {
for (let i = -2; i <= 2; i++) {
pbox(4.2, 0.5, 17, x + i * 5.4, 5.2, z - 23,
palette.crosswalk, town);
pbox(4.2, 0.5, 17, x + i * 5.4, 5.2, z + 23,
palette.crosswalk, town);
}
});
});
// Building plots between the roads.
const xBands = [
[-307, -258],
[-222, -97],
[-62, 62],
[98, 222],
[258, 307]
];
const zBands = [
[-207, -164],
[-127, -18],
[18, 127],
[164, 207]
];
const frontWindows = [];
const sideWindows = [];
const treePositions = [];
const lampPositions = [];
let blockIndex = 0;
for (const [x0, x1] of xBands) {
for (const [z0, z1] of zBands) {
const blockX = (x0 + x1) / 2;
const blockZ = (z0 + z1) / 2;
const blockW = x1 - x0;
const blockD = z1 - z0;
// One small civic plaza.
if (blockIndex === 6) {
pbox(blockW - 8, 1, blockD - 8, blockX, 4, blockZ,
palette.plaza, town);
pbox(24, 4, 24, blockX, 7, blockZ, palette.base, town);
pbox(18, 2, 18, blockX, 10, blockZ, palette.water, town);
treePositions.push(
[blockX - 22, blockZ - 18],
[blockX + 22, blockZ + 18],
[blockX - 22, blockZ + 18]
);
blockIndex++;
continue;
}
const columns = blockW > 100 ? 2 : 1;
const rows = blockD > 90 ? 2 : 1;
const cellW = blockW / columns;
const cellD = blockD / rows;
for (let column = 0; column < columns; column++) {
for (let row = 0; row < rows; row++) {
if (random() < 0.08) continue;
const centerX = x0 + (column + 0.5) * cellW;
const centerZ = z0 + (row + 0.5) * cellD;
const width = cellW * (0.69 + random() * 0.16);
const depth = cellD * (0.67 + random() * 0.18);
const height = 21 + random() * 56;
const buildingMaterial =
buildingMaterials[Math.floor(random() * buildingMaterials.length)];
pbox(width, height, depth, centerX, 5 + height / 2,
centerZ, buildingMaterial, town);
pbox(width * 0.84, 4, depth * 0.84,
centerX, 7 + height, centerZ, palette.roof, town);
if (random() > 0.5) {
pbox(13, 5, 10, centerX + 5, 9.5 + height,
centerZ - 4, palette.roof, town);
}
// Window positions are collected into instanced meshes.
for (let y = 10; y < 4 + height - 3; y += 9) {
for (let x = centerX - width / 2 + 7;
x < centerX + width / 2 - 3; x += 10) {
frontWindows.push({
x,
y,
z: centerZ + depth / 2 + 0.35,
sx: 5,
sy: 4,
sz: 0.8
});
frontWindows.push({
x,
y,
z: centerZ - depth / 2 - 0.35,
sx: 5,
sy: 4,
sz: 0.8
});
}
for (let z = centerZ - depth / 2 + 7;
z < centerZ + depth / 2 - 3; z += 10) {
sideWindows.push({
x: centerX + width / 2 + 0.35,
y,
z,
sx: 0.8,
sy: 4,
sz: 5
});
sideWindows.push({
x: centerX - width / 2 - 0.35,
y,
z,
sx: 0.8,
sy: 4,
sz: 5
});
}
}
}
}
// Courtyard trees.
if (random() > 0.38) {
treePositions.push([
blockX + (random() - 0.5) * (blockW - 22),
blockZ + (random() - 0.5) * (blockD - 20)
]);
}
blockIndex++;
}
}
function makeInstances(data, material) {
const mesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
material,
data.length
);
const dummy = new THREE.Object3D();
data.forEach((item, index) => {
dummy.position.set(item.x, item.y, item.z);
dummy.scale.set(item.sx, item.sy, item.sz);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
});
mesh.instanceMatrix.needsUpdate = true;
mesh.receiveShadow = true;
town.add(mesh);
return mesh;
}
makeInstances(frontWindows, palette.window);
makeInstances(sideWindows, palette.window);
// Street trees.
verticalRoads.forEach(x => {
for (let z = -195; z <= 195; z += 44) {
const nearIntersection = horizontalRoads.some(r => Math.abs(z - r) < 34);
if (!nearIntersection) {
treePositions.push([x + 28, z]);
}
}
});
horizontalRoads.forEach(z => {
for (let x = -275; x <= 275; x += 52) {
const nearIntersection = verticalRoads.some(r => Math.abs(x - r) < 34);
if (!nearIntersection) {
treePositions.push([x, z + 28]);
}
}
});
const treeData = treePositions.map(([x, z]) => ({ x, z }));
makeInstances(
treeData.map(({ x, z }) => ({
x, y: 9, z, sx: 2.4, sy: 9, sz: 2.4
})),
palette.trunk
);
makeInstances(
treeData.map(({ x, z }) => ({
x, y: 18, z, sx: 13, sy: 13, sz: 13
})),
palette.leaf
);
makeInstances(
treeData.map(({ x, z }) => ({
x, y: 27, z, sx: 8, sy: 8, sz: 8
})),
palette.leafLight
);
// Street lights.
verticalRoads.forEach(x => {
for (let z = -175; z <= 175; z += 90) {
lampPositions.push([x + 23, z]);
}
});
horizontalRoads.forEach(z => {
for (let x = -260; x <= 260; x += 95) {
lampPositions.push([x, z + 23]);
}
});
makeInstances(
lampPositions.map(([x, z]) => ({
x, y: 14, z, sx: 1.3, sy: 18, sz: 1.3
})),
palette.pole
);
makeInstances(
lampPositions.map(([x, z]) => ({
x, y: 24, z, sx: 5, sy: 2.5, sz: 5
})),
palette.lamp
);
// Small voxel cars.
const carColors = [
mat(0xe2a443),
mat(0xb95145),
mat(0x3d7d8c),
mat(0xe4e0cf),
mat(0x526b58)
];
function addCar(x, z, axis, direction, colorIndex) {
const car = new THREE.Group();
town.add(car);
pbox(11, 4.5, 5.5, 0, 2.3, 0,
carColors[colorIndex % carColors.length], car);
pbox(6, 3.2, 4.8, -0.7, 5.5, 0,
carColors[(colorIndex + 1) % carColors.length], car);
car.position.set(x, 5, z);
cars.push({
object: car,
axis,
direction,
speed: 7 + colorIndex * 1.1,
offset: Math.abs(x + z) % 570
});
}
addCar(-170, -8, "x", 1, 0);
addCar(75, 8, "x", -1, 1);
addCar(-260, 137, "x", 1, 2);
addCar(210, -153, "x", -1, 3);
addCar(-87, 180, "z", 1, 4);
addCar(87, -130, "z", -1, 2);
// Invented town signage.
pbox(4, 20, 4, -150, 14, 215, palette.pole, town);
pbox(62, 22, 4, -150, 25, 215, mat(0xb95145), town);
addLabel(
"MORROW BLOCKS<small>INVENTED TOWN</small>",
town,
new THREE.Vector3(0, 105, -185),
"town"
);
addLabel("MAPLE AVENUE", town, new THREE.Vector3(-80, 11, -105), "street");
addLabel("CANAL STREET", town, new THREE.Vector3(145, 11, 145), "street");
addLabel("FOUNDRY WALK", town, new THREE.Vector3(-215, 11, 0), "street");
return town;
}
function buildScaleBars() {
const platform = new THREE.Group();
platform.position.x = 400;
worldRoot.add(platform);
const base = mat(0x71817c);
const platformTop = mat(0xe4e3d4);
const teal = mat(0x168789);
const navy = mat(0x244f59);
const purple = mat(0x8069aa);
const gold = mat(0xe9a72e);
pbox(600, 18, 310, 0, -9, 0, base, platform);
pbox(588, 4, 298, 0, 1.2, 0, platformTop, platform);
// The 500 m ground ruler. Map-centimetre values are its standard
// cartographic readouts at the fixed 1:100 ratio.
for (let i = 0; i < 5; i++) {
pbox(
100, 7, 27,
-250 + i * 100 + 50, 8, -35,
i % 2 === 0 ? navy : teal,
platform
);
}
// The same physical span expressed in scene coordinates.
for (let i = 0; i < 5; i++) {
pbox(
100, 5, 18,
-250 + i * 100 + 50, 7, 58,
i % 2 === 0 ? purple : mat(0x9a86bd),
platform
);
}
// Gold end markers.
pbox(2, 12, 36, -250, 8, -35, gold, platform);
pbox(2, 12, 36, 250, 8, -35, gold, platform);
// A literal one-metre marker: exactly one map centimetre at 1:100.
pbox(1, 9, 9, -250, 8, 112, gold, platform);
addLabel(
"FIXED MAP : GROUND = 1 : 100<small>500 m ground = 5 cm on map</small>",
platform,
new THREE.Vector3(0, 30, -120),
"scale-title"
);
for (let i = 0; i <= 5; i++) {
const x = -250 + i * 100;
addLabel(
`${i} cm`,
platform,
new THREE.Vector3(x, 18, -76),
"ruler map"
);
addLabel(
`${i * 100} m`,
platform,
new THREE.Vector3(x, 18, -4),
"ruler ground"
);
const sceneUnits = (i * 100) / metersPerSceneUnit;
addLabel(
`${formatNumber(sceneUnits)} u`,
platform,
new THREE.Vector3(x, 17, 92),
"ruler scene"
);
}
addLabel(
"MAP READOUT",
platform,
new THREE.Vector3(-205, 15, -103),
"ruler map"
);
addLabel(
"GROUND RULER",
platform,
new THREE.Vector3(-205, 15, -22),
"ruler ground"
);
addLabel(
"SCENE COORDINATES",
platform,
new THREE.Vector3(-190, 15, 83),
"ruler scene"
);
addLabel(
`1 cm MAP = 1 m GROUND = ${formatNumber(1 / metersPerSceneUnit)} SCENE UNIT${metersPerSceneUnit === 2 ? "S" : ""}`,
platform,
new THREE.Vector3(-120, 16, 120),
"ruler map"
);
return platform;
}
function rebuild() {
disposeWorld();
worldScale = 1 / metersPerSceneUnit;
worldRoot.scale.setScalar(worldScale);
buildTown();
buildScaleBars();
worldRoot.updateMatrixWorld(true);
sceneEquation.textContent =
`${formatNumber(1 / metersPerSceneUnit)} unit${metersPerSceneUnit === 2 ? "s" : ""}`;
unitButtons.forEach(button => {
const selected = Number(button.dataset.unit) === metersPerSceneUnit;
button.classList.toggle("active", selected);
button.setAttribute("aria-pressed", String(selected));
});
updateLighting();
updateFrustum();
resetCameraTarget();
updateCamera();
}
function updateLighting() {
sun.position.set(-360 * worldScale, 900 * worldScale, 520 * worldScale);
sun.target.position.set(50 * worldScale, 0, 0);
sun.shadow.camera.left = -900 * worldScale;
sun.shadow.camera.right = 900 * worldScale;
sun.shadow.camera.top = 750 * worldScale;
sun.shadow.camera.bottom = -750 * worldScale;
sun.shadow.camera.near = 10;
sun.shadow.camera.far = 2200 * worldScale;
sun.shadow.camera.updateProjectionMatrix();
sun.target.updateMatrixWorld();
}
function resetCameraTarget() {
cameraTarget.set(50 * worldScale, 30 * worldScale, 0);
}
function updateFrustum() {
const minimumHeight = 780 * worldScale;
const widthBasedHeight = (1500 * worldScale) / (2 * aspect) * 1.1;
baseViewHeight = Math.max(minimumHeight, widthBasedHeight);
camera.left = -baseViewHeight * aspect / 2;
camera.right = baseViewHeight * aspect / 2;
camera.top = baseViewHeight / 2;
camera.bottom = -baseViewHeight / 2;
camera.zoom = cameraZoom;
camera.updateProjectionMatrix();
}
function updateCamera() {
const distance = 1800 * worldScale;
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + distance * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + distance * Math.cos(cameraPhi),
cameraTarget.z + distance * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
function updateLabels() {
for (const label of labels) {
const world = label.parent.localToWorld(label.position.clone());
const projected = world.project(camera);
const visible =
projected.z >= -1 &&
projected.z <= 1 &&
projected.x > -1.2 &&
projected.x < 1.2 &&
projected.y > -1.2 &&
projected.y < 1.2;
label.element.style.display = visible ? "block" : "none";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
label.element.style.transform =
`translate(-50%, -50%) translate(${x.toFixed(1)}px, ${y.toFixed(1)}px)`;
}
}
function animateCars(time) {
for (const car of cars) {
const span = 600;
let travel = (time * car.speed + car.offset) % span;
if (car.direction < 0) travel = span - travel;
if (car.axis === "x") {
car.object.position.x = -300 + travel;
} else {
car.object.position.z = -300 + travel;
}
}
}
// Direct camera controls: no examples or addons are imported.
const canvas = renderer.domElement;
let dragging = false;
let panning = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("pointerdown", event => {
dragging = true;
panning = event.button === 2 || event.shiftKey;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
if (panning) {
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrixWorld, 0);
right.y = 0;
right.normalize();
const forward = new THREE.Vector3()
.setFromMatrixColumn(camera.matrixWorld, 2);
forward.y = 0;
forward.normalize();
const unitsPerPixel =
baseViewHeight / window.innerHeight / camera.zoom * 1.15;
cameraTarget.addScaledVector(right, -dx * unitsPerPixel);
cameraTarget.addScaledVector(forward, -dy * unitsPerPixel);
} else {
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi + dy * 0.005,
0.28,
1.42
);
}
updateCamera();
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("wheel", event => {
event.preventDefault();
cameraZoom = THREE.MathUtils.clamp(
cameraZoom * Math.exp(-event.deltaY * 0.001),
0.55,
3.2
);
updateFrustum();
}, { passive: false });
canvas.addEventListener("dblclick", () => {
cameraTheta = 0.74;
cameraPhi = 0.76;
cameraZoom = 1;
resetCameraTarget();
updateFrustum();
updateCamera();
});
unitButtons.forEach(button => {
button.addEventListener("click", () => {
metersPerSceneUnit = Number(button.dataset.unit);
rebuild();
});
});
window.addEventListener("resize", () => {
aspect = window.innerWidth / window.innerHeight;
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
updateFrustum();
});
function render() {
requestAnimationFrame(render);
const time = clock.getElapsedTime();
animateCars(time);
scene.updateMatrixWorld();
updateLabels();
renderer.render(scene, camera);
}
rebuild();
render();
</script>
</body>
</html>
```