Prompt specification
Build or customize the Three.js 3D scene "Delivery route planning miniature city" 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: Delivery route planning miniature city
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/delivery-route-planning-miniature-city-x1919
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts shortest paths in a weighted graph. Compose a bounded street network with synthetic travel costs and blocked links; find the least-cost route and label the costs invented rather than real traffic data. Voxel style.
Working Three.js Starter (delivery-route-planning-miniature-city-x1919):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Route Lab — Weighted Street Graph</title>
<style>
:root {
color-scheme: dark;
--ink: #eef5f7;
--muted: #9fb1bd;
--panel: rgba(14, 23, 34, 0.88);
--border: rgba(164, 202, 214, 0.18);
--cyan: #36e6c5;
--red: #ff6677;
--amber: #f4c56b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #101722;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.panel {
position: absolute;
top: 22px;
left: 22px;
width: min(350px, calc(100vw - 44px));
padding: 20px;
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.34);
backdrop-filter: blur(14px);
pointer-events: auto;
}
.eyebrow {
margin-bottom: 7px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 27px;
line-height: 1.05;
letter-spacing: -0.035em;
}
.intro {
margin: 10px 0 17px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-bottom: 16px;
}
.stat {
padding: 11px 12px;
border: 1px solid var(--border);
border-radius: 11px;
background: rgba(255, 255, 255, 0.035);
}
.stat span {
display: block;
margin-bottom: 3px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.stat strong {
font-size: 23px;
font-variant-numeric: tabular-nums;
}
.stat strong em {
margin-left: 3px;
color: var(--cyan);
font-size: 12px;
font-style: normal;
}
.route-label {
margin-bottom: 6px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#path {
min-height: 42px;
color: #d8f8f1;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 12px;
line-height: 1.65;
overflow-wrap: anywhere;
}
.disclaimer {
margin-top: 15px;
padding: 11px 12px;
border-left: 3px solid var(--amber);
border-radius: 4px 10px 10px 4px;
background: rgba(244, 197, 107, 0.08);
color: #d9c9a8;
font-size: 11px;
line-height: 1.55;
}
.disclaimer strong {
color: #ffe3a7;
}
.legend {
display: grid;
gap: 8px;
margin-top: 16px;
color: var(--muted);
font-size: 11px;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
}
.swatch {
width: 22px;
height: 8px;
border-radius: 2px;
background: #293847;
}
.swatch.route {
background: var(--cyan);
box-shadow: 0 0 12px rgba(54, 230, 197, 0.4);
}
.swatch.blocked {
background: var(--red);
}
.actions {
position: absolute;
top: 22px;
right: 22px;
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
padding: 10px 13px;
border: 1px solid var(--border);
border-radius: 10px;
background: rgba(14, 23, 34, 0.84);
color: var(--ink);
font: 700 12px ui-sans-serif, system-ui, sans-serif;
cursor: pointer;
backdrop-filter: blur(12px);
}
button:hover {
border-color: rgba(54, 230, 197, 0.55);
color: var(--cyan);
}
.controls {
position: absolute;
right: 22px;
bottom: 20px;
padding: 10px 13px;
border: 1px solid var(--border);
border-radius: 10px;
background: rgba(14, 23, 34, 0.76);
color: var(--muted);
font-size: 11px;
backdrop-filter: blur(10px);
}
kbd {
padding: 2px 5px;
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 4px;
background: rgba(255, 255, 255, 0.06);
color: #dce9ef;
font: 700 10px ui-monospace, monospace;
}
@media (max-width: 700px) {
.panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 15px;
}
h1 {
font-size: 22px;
}
.intro,
.legend,
.disclaimer {
display: none;
}
.stats {
margin-bottom: 10px;
}
.actions {
top: auto;
right: 12px;
bottom: 54px;
}
.controls {
right: 12px;
bottom: 12px;
}
}
</style>
</head>
<body>
<div id="hud">
<section class="panel">
<div class="eyebrow">Synthetic weighted graph</div>
<h1>Voxel Route Lab</h1>
<p class="intro">
A bounded street network solved with Dijkstra’s least-cost algorithm.
Blocked links are excluded from the search.
</p>
<div class="stats">
<div class="stat">
<span>Least cost</span>
<strong><b id="totalCost">—</b><em>units</em></strong>
</div>
<div class="stat">
<span>Route links</span>
<strong id="edgeCount">—</strong>
</div>
</div>
<div class="route-label">Shortest node sequence</div>
<div id="path" aria-live="polite">Calculating…</div>
<div class="disclaimer">
<strong>Invented costs, not traffic data.</strong>
Edge labels are synthetic travel units chosen for this demonstration.
They do not represent real traffic, distance, time, road conditions, or travel advice.
</div>
<div class="legend">
<div class="legend-row">
<span class="swatch route"></span>
<span>Least-cost route</span>
</div>
<div class="legend-row">
<span class="swatch"></span>
<span>Open street link · label is invented cost</span>
</div>
<div class="legend-row">
<span class="swatch blocked"></span>
<span>Blocked link · excluded by the router</span>
</div>
</div>
</section>
<div class="actions">
<button id="labelsBtn" type="button">Costs: ON</button>
<button id="resetBtn" type="button">Reset view</button>
</div>
<div class="controls">
Drag to orbit · <kbd>Shift</kbd> + drag to pan · Wheel to zoom · <kbd>R</kbd> reset
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene setup
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x101722);
const renderer = new THREE.WebGLRenderer({ antialias: 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.08;
document.body.prepend(renderer.domElement);
const camera = new THREE.OrthographicCamera(-30, 30, 22, -22, 0.1, 180);
const initialCameraPosition = new THREE.Vector3(39, 43, 46);
const cameraTarget = new THREE.Vector3(0, 0, 0);
camera.position.copy(initialCameraPosition);
camera.lookAt(cameraTarget);
scene.add(new THREE.HemisphereLight(0xc7e7ff, 0x46394a, 2.0));
const sun = new THREE.DirectionalLight(0xfff1cf, 3.2);
sun.position.set(24, 42, 18);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -32;
sun.shadow.camera.right = 32;
sun.shadow.camera.top = 28;
sun.shadow.camera.bottom = -28;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 90;
sun.shadow.bias = -0.0005;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x68bfe3, 1.15);
fill.position.set(-30, 18, -24);
scene.add(fill);
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.02,
...options
});
}
const materials = {
base: standardMaterial(0x172331),
ground: standardMaterial(0x29464a),
sidewalk: standardMaterial(0x718083),
road: standardMaterial(0x273541),
blockedRoad: standardMaterial(0x56323d),
lane: standardMaterial(0xe1c978, { emissive: 0x4b421c, emissiveIntensity: 0.15 }),
node: standardMaterial(0x91a1a5),
curb: standardMaterial(0x53696c),
plot: standardMaterial(0x355357),
window: standardMaterial(0xffdf9a, {
emissive: 0xb27a26,
emissiveIntensity: 0.38,
roughness: 0.45
}),
route: standardMaterial(0x32e3c2, {
emissive: 0x087e69,
emissiveIntensity: 0.5,
roughness: 0.42
}),
routePixel: standardMaterial(0xc8fff3, {
emissive: 0x5ce9cc,
emissiveIntensity: 0.8
}),
barrier: standardMaterial(0xff596c, {
emissive: 0x7b1022,
emissiveIntensity: 0.3
}),
barrierWhite: standardMaterial(0xffe8df),
start: standardMaterial(0x62e58c, {
emissive: 0x176c3c,
emissiveIntensity: 0.55
}),
goal: standardMaterial(0xff6478, {
emissive: 0x7f1729,
emissiveIntensity: 0.55
})
};
function box(width, height, depth, material, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = height > 0.22;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Bounded synthetic graph
// -------------------------------------------------------------------------
const ROWS = 5;
const COLS = 6;
const X_SPACING = 7.4;
const Z_SPACING = 6.6;
const nodeId = (row, col) =>
`${String.fromCharCode(65 + row)}${col + 1}`;
const nodes = new Map();
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
const id = nodeId(row, col);
const position = new THREE.Vector3(
(col - (COLS - 1) / 2) * X_SPACING,
0,
(row - (ROWS - 1) / 2) * Z_SPACING
);
nodes.set(id, {
id,
row,
col,
position,
x: position.x,
z: position.z
});
}
}
const horizontalCosts = [
[5, 7, 6, 8, 4],
[4, 6, 9, 7, 5],
[8, 5, 7, 4, 9],
[6, 10, 8, 6, 7],
[7, 6, 5, 8, 6]
];
const verticalCosts = [
[6, 5, 7, 4],
[4, 8, 6, 5],
[7, 6, 9, 5],
[5, 7, 8, 6],
[6, 4, 5, 7],
[8, 5, 6, 4]
];
const keyFor = (a, b) => [a, b].sort().join("|");
const blockedKeys = new Set([
keyFor("B4", "C4"),
keyFor("C4", "D4"),
keyFor("C2", "C3"),
keyFor("D3", "D4")
]);
const edges = [];
const edgeByKey = new Map();
function addEdge(a, b, cost) {
const key = keyFor(a, b);
const edge = {
a,
b,
cost,
blocked: blockedKeys.has(key),
key
};
edges.push(edge);
edgeByKey.set(key, edge);
}
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS - 1; col++) {
addEdge(
nodeId(row, col),
nodeId(row, col + 1),
horizontalCosts[row][col]
);
}
}
for (let col = 0; col < COLS; col++) {
for (let row = 0; row < ROWS - 1; row++) {
addEdge(
nodeId(row, col),
nodeId(row + 1, col),
verticalCosts[col][row]
);
}
}
// Dijkstra's shortest-path calculation. Blocked links are never inserted
// into the adjacency list.
function dijkstra(startId, goalId) {
const adjacency = new Map();
for (const id of nodes.keys()) {
adjacency.set(id, []);
}
for (const edge of edges) {
if (edge.blocked) continue;
adjacency.get(edge.a).push({ to: edge.b, cost: edge.cost, edge });
adjacency.get(edge.b).push({ to: edge.a, cost: edge.cost, edge });
}
const distances = new Map();
const previous = new Map();
const visited = new Set();
for (const id of nodes.keys()) {
distances.set(id, Infinity);
}
distances.set(startId, 0);
const queue = [...nodes.keys()];
while (queue.length) {
queue.sort(
(a, b) => distances.get(a) - distances.get(b)
);
const current = queue.shift();
if (visited.has(current)) continue;
visited.add(current);
if (current === goalId) break;
for (const neighbor of adjacency.get(current)) {
const nextDistance = distances.get(current) + neighbor.cost;
if (nextDistance < distances.get(neighbor.to)) {
distances.set(neighbor.to, nextDistance);
previous.set(neighbor.to, {
node: current,
edge: neighbor.edge
});
}
}
}
const pathNodes = [];
const pathEdges = [];
let cursor = goalId;
while (cursor !== startId) {
pathNodes.push(cursor);
const step = previous.get(cursor);
if (!step) break;
pathEdges.push(step.edge);
cursor = step.node;
}
pathNodes.push(startId);
pathNodes.reverse();
pathEdges.reverse();
return {
nodes: pathNodes.map((id) => nodes.get(id)),
edges: pathEdges,
cost: distances.get(goalId)
};
}
const startId = "A1";
const goalId = nodeId(ROWS - 1, COLS - 1);
const route = dijkstra(startId, goalId);
const routeEdgeKeys = new Set(route.edges.map((edge) => edge.key));
document.getElementById("totalCost").textContent = route.cost;
document.getElementById("edgeCount").textContent = route.edges.length;
document.getElementById("path").textContent =
route.nodes.map((node) => node.id).join(" → ");
// -------------------------------------------------------------------------
// Diorama base and streets
// -------------------------------------------------------------------------
box(46.5, 1.2, 35.5, materials.base, 0, -0.92, 0);
box(45.5, 0.34, 34.5, materials.ground, 0, -0.17, 0);
// Raised perimeter curb blocks.
for (let x = -21; x <= 21; x += 3) {
box(1.8, 0.34, 0.35, materials.curb, x, 0.13, -17);
box(1.8, 0.34, 0.35, materials.curb, x, 0.13, 17);
}
for (let z = -14; z <= 14; z += 3.5) {
box(0.35, 0.34, 1.8, materials.curb, -22.5, 0.13, z);
box(0.35, 0.34, 1.8, materials.curb, 22.5, 0.13, z);
}
function buildStreetSegment(edge) {
const a = nodes.get(edge.a);
const b = nodes.get(edge.b);
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 midpointX = (a.x + b.x) / 2;
const midpointZ = (a.z + b.z) / 2;
const sidewalk = box(
length,
0.16,
2.4,
materials.sidewalk,
midpointX,
0.08,
midpointZ
);
sidewalk.rotation.y = angle;
sidewalk.castShadow = false;
const road = box(
length,
0.12,
1.55,
edge.blocked ? materials.blockedRoad : materials.road,
midpointX,
0.20,
midpointZ
);
road.rotation.y = angle;
road.castShadow = false;
if (!edge.blocked) {
const dashCount = Math.max(1, Math.floor(length / 1.35));
for (let i = 0; i < dashCount; i++) {
const t = (i + 0.5) / dashCount;
const dash = box(
0.48,
0.035,
0.075,
materials.lane,
a.x + dx * t,
0.285,
a.z + dz * t
);
dash.rotation.y = angle;
dash.castShadow = false;
}
}
}
for (const edge of edges) {
buildStreetSegment(edge);
}
// Intersection tiles and graph-node hubs.
for (const node of nodes.values()) {
const intersection = box(
2.4,
0.16,
2.4,
materials.sidewalk,
node.x,
0.08,
node.z
);
intersection.castShadow = false;
const asphalt = box(
1.55,
0.12,
1.55,
materials.road,
node.x,
0.20,
node.z
);
asphalt.castShadow = false;
const hub = box(
0.28,
0.10,
0.28,
materials.node,
node.x,
0.31,
node.z
);
hub.castShadow = false;
}
// Closed-link barricades.
for (const edge of edges.filter((candidate) => candidate.blocked)) {
const a = nodes.get(edge.a);
const b = nodes.get(edge.b);
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 barricade = new THREE.Group();
barricade.position.set((a.x + b.x) / 2, 0, (a.z + b.z) / 2);
barricade.rotation.y = angle;
scene.add(barricade);
box(0.14, 0.34, 1.8, materials.barrier, 0, 0.73, 0, barricade);
box(0.18, 0.64, 0.18, materials.barrierWhite, 0, 0.36, -0.58, barricade);
box(0.18, 0.64, 0.18, materials.barrierWhite, 0, 0.36, 0.58, barricade);
for (const z of [-0.45, 0, 0.45]) {
box(0.155, 0.19, 0.22, materials.barrierWhite, 0, 0.74, z, barricade);
}
}
// -------------------------------------------------------------------------
// Least-cost route overlay
// -------------------------------------------------------------------------
for (const edge of route.edges) {
const a = nodes.get(edge.a);
const b = nodes.get(edge.b);
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 pixelCount = Math.max(3, Math.floor(length / 0.8));
const routeStrip = box(
length,
0.10,
0.68,
materials.route,
(a.x + b.x) / 2,
0.38,
(a.z + b.z) / 2
);
routeStrip.rotation.y = angle;
routeStrip.castShadow = false;
for (let i = 0; i < pixelCount; i++) {
const t = (i + 0.5) / pixelCount;
const pixel = box(
0.20,
0.035,
0.46,
materials.routePixel,
a.x + dx * t,
0.45,
a.z + dz * t
);
pixel.rotation.y = angle;
pixel.castShadow = false;
}
}
for (const node of route.nodes) {
const routeNode = box(
0.76,
0.14,
0.76,
materials.routePixel,
node.x,
0.50,
node.z
);
routeNode.castShadow = false;
}
// -------------------------------------------------------------------------
// Billboard labels generated with the core Three.js Sprite API
// -------------------------------------------------------------------------
const labelGroup = new THREE.Group();
scene.add(labelGroup);
function roundedRect(context, x, y, width, height, radius) {
context.beginPath();
context.moveTo(x + radius, y);
context.arcTo(x + width, y, x + width, y + height, radius);
context.arcTo(x + width, y + height, x, y + height, radius);
context.arcTo(x, y + height, x, y, radius);
context.arcTo(x, y, x + width, y, radius);
context.closePath();
}
function makeLabel(text, options = {}) {
const canvas = document.createElement("canvas");
canvas.width = 320;
canvas.height = 96;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(
context,
5,
5,
canvas.width - 10,
canvas.height - 10,
18
);
context.fillStyle = options.background || "rgba(13,22,31,0.92)";
context.fill();
context.lineWidth = options.lineWidth || 3;
context.strokeStyle = options.border || "rgba(188,211,220,0.28)";
context.stroke();
context.fillStyle = options.color || "#edf6f8";
context.font = "800 43px ui-monospace, SFMono-Regular, Menlo, monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, canvas.width / 2, canvas.height / 2 + 1);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.generateMipmaps = false;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
const width = options.width || 1.55;
const height = options.height || 0.58;
sprite.scale.set(width, height, 1);
sprite.renderOrder = 20;
return sprite;
}
for (const edge of edges) {
const a = nodes.get(edge.a);
const b = nodes.get(edge.b);
const midpoint = new THREE.Vector3(
(a.x + b.x) / 2,
1.02,
(a.z + b.z) / 2
);
let label;
if (edge.blocked) {
label = makeLabel("CLOSED", {
background: "rgba(91,25,39,0.95)",
border: "#ff6677",
color: "#ffe7eb",
width: 2.25
});
} else if (routeEdgeKeys.has(edge.key)) {
label = makeLabel(`${edge.cost}u`, {
background: "rgba(5,71,65,0.96)",
border: "#36e6c5",
color: "#dffff8",
width: 1.48
});
} else {
label = makeLabel(`${edge.cost}u`, {
background: "rgba(13,22,31,0.9)",
border: "rgba(188,211,220,0.28)",
color: "#edf6f8",
width: 1.48
});
}
label.position.copy(midpoint);
labelGroup.add(label);
}
// Start and goal beacons.
function addBeacon(node, colorMaterial, labelText, labelColor) {
const group = new THREE.Group();
group.position.set(node.x, 0, node.z);
scene.add(group);
box(0.74, 0.18, 0.74, colorMaterial, 0, 0.40, 0, group);
box(0.16, 1.45, 0.16, colorMaterial, 0, 1.18, 0, group);
box(0.72, 0.48, 0.16, colorMaterial, 0.30, 1.70, 0, group);
const label = makeLabel(labelText, {
background: "rgba(13,22,31,0.94)",
border: labelColor,
color: labelColor,
width: 2.15
});
label.position.set(node.x, 2.48, node.z);
labelGroup.add(label);
return group;
}
const startBeacon = addBeacon(
nodes.get(startId),
materials.start,
"START · S",
"#62e58c"
);
const goalBeacon = addBeacon(
nodes.get(goalId),
materials.goal,
"GOAL · T",
"#ff6478"
);
// -------------------------------------------------------------------------
// Voxel city blocks
// -------------------------------------------------------------------------
function mulberry32(seed) {
return function random() {
let value = seed += 0x6D2B79F5;
value = Math.imul(value ^ (value >>> 15), value | 1);
value ^= value + Math.imul(value ^ (value >>> 7), value | 61);
return ((value ^ (value >>> 14)) >>> 0) / 4294967296;
};
}
const random = mulberry32(240519);
const buildingPalette = [
0xe26d5a,
0xf0b957,
0x4aa7a0,
0x5f83c8,
0x9b72b4,
0xd88964,
0x72a66d,
0xc66f89
];
for (let row = 0; row < ROWS - 1; row++) {
for (let col = 0; col < COLS - 1; col++) {
const left = nodes.get(nodeId(row, col));
const right = nodes.get(nodeId(row, col + 1));
const back = nodes.get(nodeId(row + 1, col));
const front = nodes.get(nodeId(row + 1, col + 1));
const centerX = (left.x + right.x) / 2;
const centerZ = (back.z + front.z) / 2;
box(
4.15,
0.07,
3.55,
materials.plot,
centerX,
0.035,
centerZ
);
const width = 2.35 + random() * 1.05;
const depth = 2.0 + random() * 0.85;
const height = 1.35 + random() * 3.25;
const offsetX = (random() - 0.5) * 0.38;
const offsetZ = (random() - 0.5) * 0.25;
const color = buildingPalette[
Math.floor(random() * buildingPalette.length)
];
const buildingMaterial = standardMaterial(color);
const roofMaterial = standardMaterial(
new THREE.Color(color).multiplyScalar(0.76)
);
const buildingX = centerX + offsetX;
const buildingZ = centerZ + offsetZ;
box(
width,
height,
depth,
buildingMaterial,
buildingX,
0.07 + height / 2,
buildingZ
);
box(
width + 0.14,
0.16,
depth + 0.14,
roofMaterial,
buildingX,
0.07 + height + 0.08,
buildingZ
);
// Simple luminous window bands on all four voxel faces.
for (const level of [0.45, 0.72]) {
const y = 0.07 + height * level;
for (const side of [-1, 1]) {
box(
width * 0.60,
0.17,
0.035,
materials.window,
buildingX,
y,
buildingZ + side * (depth / 2 + 0.025)
);
box(
0.035,
0.17,
depth * 0.56,
materials.window,
buildingX + side * (width / 2 + 0.025),
y,
buildingZ
);
}
}
// Rooftop equipment.
if (random() > 0.25) {
box(
0.55 + random() * 0.35,
0.35 + random() * 0.4,
0.48 + random() * 0.3,
roofMaterial,
buildingX + (random() - 0.5) * 0.7,
0.07 + height + 0.35,
buildingZ + (random() - 0.5) * 0.5
);
}
// A smaller annex adds irregular block silhouettes.
if (random() > 0.34) {
const annexWidth = 0.75 + random() * 0.55;
const annexDepth = 0.75 + random() * 0.55;
const annexHeight = 0.7 + random() * 1.2;
const annexSide = random() > 0.5 ? 1 : -1;
box(
annexWidth,
annexHeight,
annexDepth,
buildingMaterial,
buildingX + annexSide * (width / 2 + annexWidth / 2 - 0.15),
0.07 + annexHeight / 2,
buildingZ + (random() - 0.5) * 0.65
);
}
}
}
// -------------------------------------------------------------------------
// Camera controls implemented directly, without Three.js addons
// -------------------------------------------------------------------------
let dragging = false;
let dragMode = "rotate";
let lastPointerX = 0;
let lastPointerY = 0;
function resetCamera() {
cameraTarget.set(0, 0, 0);
camera.position.copy(initialCameraPosition);
camera.zoom = 1;
camera.updateProjectionMatrix();
camera.lookAt(cameraTarget);
}
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
dragMode = event.button === 2 || event.shiftKey ? "pan" : "rotate";
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
if (dragMode === "rotate") {
const offset = camera.position.clone().sub(cameraTarget);
const spherical = new THREE.Spherical().setFromVector3(offset);
spherical.theta -= dx * 0.006;
spherical.phi = THREE.MathUtils.clamp(
spherical.phi - dy * 0.006,
0.35,
1.35
);
camera.position
.copy(cameraTarget)
.add(new THREE.Vector3().setFromSpherical(spherical));
camera.lookAt(cameraTarget);
} else {
camera.updateMatrixWorld();
const worldPerPixel =
(camera.top - camera.bottom) / (window.innerHeight * camera.zoom);
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrixWorld, 0);
right.y = 0;
right.normalize();
const up = new THREE.Vector3()
.setFromMatrixColumn(camera.matrixWorld, 1);
up.y = 0;
if (up.lengthSq() < 0.001) {
up.set(0, 0, -1);
} else {
up.normalize();
}
const movement = new THREE.Vector3()
.addScaledVector(right, -dx * worldPerPixel)
.addScaledVector(up, dy * worldPerPixel);
cameraTarget.add(movement);
camera.position.add(movement);
camera.lookAt(cameraTarget);
}
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
camera.zoom = THREE.MathUtils.clamp(
camera.zoom * Math.exp(-event.deltaY * 0.001),
0.58,
3.1
);
camera.updateProjectionMatrix();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") {
resetCamera();
}
});
document.getElementById("resetBtn").addEventListener("click", resetCamera);
const labelsButton = document.getElementById("labelsBtn");
labelsButton.addEventListener("click", () => {
labelGroup.visible = !labelGroup.visible;
labelsButton.textContent = `Costs: ${labelGroup.visible ? "ON" : "OFF"}`;
});
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
// Fit the complete miniature on both desktop and narrow screens.
const frustumHeight = Math.max(43, 54 / aspect);
camera.left = (-frustumHeight * aspect) / 2;
camera.right = (frustumHeight * aspect) / 2;
camera.top = frustumHeight / 2;
camera.bottom = -frustumHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
resize();
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const time = clock.getElapsedTime();
materials.route.emissiveIntensity = 0.46 + Math.sin(time * 2.4) * 0.10;
materials.routePixel.emissiveIntensity = 0.72 + Math.sin(time * 2.4 + 1) * 0.12;
startBeacon.rotation.y = Math.sin(time * 0.7) * 0.025;
goalBeacon.rotation.y = Math.sin(time * 0.7 + 1.5) * 0.025;
renderer.render(scene, camera);
});
</script>
</body>
</html>
```