Prompt specification
Build or customize the Three.js 3D scene "Village with two unequal bridges" 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: Village with two unequal bridges
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/village-with-two-unequal-bridges-x1342
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts river crossings linked by bridges and detours. Arrange homes and a market along a river with two bridges. Draw walking routes and display network distance separately from straight-line distance across water. Voxel style.
Working Three.js Starter (village-with-two-unequal-bridges-x1342):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Riverlink — Walking Network Study</title>
<style>
:root {
--ink: #17343d;
--muted: #60777b;
--paper: rgba(250, 249, 242, 0.94);
--paper-solid: #faf9f2;
--walk: #f4a62a;
--walk-dark: #9b5d0c;
--direct: #db4f91;
--direct-dark: #9d245e;
--teal: #317a76;
--border: rgba(23, 52, 61, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dbeaec;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
background: var(--paper);
border: 1px solid rgba(255, 255, 255, 0.74);
box-shadow:
0 14px 40px rgba(26, 63, 67, 0.14),
0 2px 8px rgba(26, 63, 67, 0.08);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.brand {
position: absolute;
top: 22px;
left: 22px;
padding: 15px 18px 14px;
border-radius: 16px;
min-width: 265px;
}
.brand-row {
display: flex;
align-items: center;
gap: 10px;
}
.brand-mark {
width: 28px;
height: 28px;
position: relative;
flex: 0 0 auto;
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
left: 0;
right: 0;
height: 8px;
background: var(--teal);
}
.brand-mark::before {
top: 2px;
transform: rotate(-12deg);
}
.brand-mark::after {
bottom: 2px;
transform: rotate(12deg);
}
.brand h1 {
margin: 0;
font-size: 19px;
line-height: 1;
letter-spacing: 0.11em;
font-weight: 850;
}
.brand p {
margin: 8px 0 0;
font-size: 12px;
color: var(--muted);
line-height: 1.4;
}
.legend {
position: absolute;
top: 22px;
right: 22px;
padding: 13px 15px;
border-radius: 14px;
display: grid;
gap: 10px;
min-width: 225px;
}
.legend-title {
font-size: 10px;
letter-spacing: 0.15em;
text-transform: uppercase;
font-weight: 800;
color: var(--muted);
margin-bottom: 1px;
}
.legend-item {
display: flex;
align-items: center;
gap: 10px;
font-size: 12px;
font-weight: 650;
}
.legend-line {
width: 31px;
height: 5px;
border-radius: 1px;
flex: 0 0 auto;
}
.legend-line.walk {
background: var(--walk);
box-shadow: 0 0 0 1px rgba(155, 93, 12, 0.12);
}
.legend-line.direct {
height: 3px;
background: repeating-linear-gradient(
90deg,
var(--direct) 0 8px,
transparent 8px 13px
);
}
.stops {
position: absolute;
top: 22px;
left: 50%;
transform: translateX(-50%);
display: flex;
gap: 7px;
padding: 7px;
border-radius: 15px;
pointer-events: auto;
}
.stop {
appearance: none;
border: 0;
background: transparent;
color: var(--ink);
border-radius: 10px;
padding: 10px 12px;
display: flex;
align-items: center;
gap: 8px;
cursor: pointer;
font: inherit;
white-space: nowrap;
transition: background 160ms ease, transform 160ms ease;
}
.stop:hover {
background: rgba(49, 122, 118, 0.09);
transform: translateY(-1px);
}
.stop.active {
background: var(--ink);
color: white;
}
.stop-dot {
width: 10px;
height: 10px;
border-radius: 2px;
background: var(--stop-color);
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.14);
}
.stop-name {
font-size: 12px;
font-weight: 750;
}
.metrics {
position: absolute;
left: 22px;
bottom: 22px;
width: 350px;
border-radius: 19px;
padding: 18px;
}
.eyebrow {
color: var(--teal);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.metrics h2 {
margin: 5px 0 15px;
font-size: 21px;
line-height: 1.15;
letter-spacing: -0.025em;
}
.metric-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
.metric {
border-radius: 12px;
padding: 12px;
background: rgba(255, 255, 255, 0.68);
border: 1px solid var(--border);
}
.metric.network {
border-top: 4px solid var(--walk);
}
.metric.direct {
border-top: 4px solid var(--direct);
}
.metric-label {
min-height: 29px;
color: var(--muted);
font-size: 9px;
line-height: 1.25;
font-weight: 850;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.metric-value {
margin-top: 3px;
font-size: 27px;
line-height: 1;
font-weight: 850;
letter-spacing: -0.045em;
font-variant-numeric: tabular-nums;
}
.metric-value span {
font-size: 12px;
letter-spacing: 0;
color: var(--muted);
margin-left: 3px;
}
.delta {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: 10px;
padding: 10px 12px;
border-radius: 10px;
background: rgba(244, 166, 42, 0.11);
color: var(--walk-dark);
font-size: 11px;
font-weight: 780;
}
.delta span:last-child {
color: var(--muted);
font-weight: 650;
}
.explanation {
margin: 12px 1px 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.controls {
position: absolute;
right: 22px;
bottom: 22px;
display: flex;
align-items: center;
gap: 10px;
pointer-events: auto;
}
.hint {
padding: 10px 12px;
border-radius: 11px;
font-size: 10px;
color: var(--muted);
background: rgba(250, 249, 242, 0.82);
border: 1px solid rgba(255, 255, 255, 0.75);
}
button.reset {
appearance: none;
border: 0;
border-radius: 11px;
padding: 11px 13px;
background: var(--ink);
color: white;
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
box-shadow: 0 5px 16px rgba(23, 52, 61, 0.2);
}
button.reset:hover {
background: #24505a;
}
.loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #dbeaec;
color: var(--ink);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
transition: opacity 250ms ease;
}
.loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 1100px) {
.stops {
top: 91px;
}
.legend {
min-width: 205px;
}
}
@media (max-width: 720px) {
.brand {
top: 12px;
left: 12px;
min-width: 0;
padding: 11px 13px;
}
.brand p {
display: none;
}
.brand h1 {
font-size: 15px;
}
.brand-mark {
width: 23px;
height: 23px;
}
.legend {
top: 12px;
right: 12px;
min-width: 0;
padding: 10px;
gap: 7px;
}
.legend-title,
.legend-item span:last-child {
display: none;
}
.legend-item {
gap: 5px;
}
.legend-line {
width: 25px;
}
.stops {
top: auto;
left: 12px;
right: 12px;
bottom: 12px;
transform: none;
overflow-x: auto;
justify-content: flex-start;
}
.stop {
padding: 9px 10px;
}
.metrics {
left: 12px;
right: 12px;
bottom: 83px;
width: auto;
padding: 13px;
}
.metrics h2 {
margin-bottom: 10px;
font-size: 18px;
}
.metric {
padding: 9px;
}
.metric-value {
font-size: 23px;
}
.explanation,
.hint {
display: none;
}
.controls {
right: 12px;
top: 82px;
bottom: auto;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel map of river crossings"></canvas>
<div class="hud">
<header class="brand panel">
<div class="brand-row">
<div class="brand-mark" aria-hidden="true"></div>
<h1>RIVERLINK</h1>
</div>
<p>A walking-network study of homes, markets and two river crossings.</p>
</header>
<nav class="stops panel" id="stops" aria-label="Choose an origin"></nav>
<aside class="legend panel" aria-label="Map legend">
<div class="legend-title">Distance layers</div>
<div class="legend-item">
<span class="legend-line walk"></span>
<span>Walking network</span>
</div>
<div class="legend-item">
<span class="legend-line direct"></span>
<span>Straight across water</span>
</div>
</aside>
<section class="metrics panel" id="metrics" aria-live="polite">
<div class="eyebrow" id="fromLabel">From Willow House</div>
<h2 id="routeTitle">Via North Footbridge</h2>
<div class="metric-grid">
<div class="metric network">
<div class="metric-label">Walking network distance</div>
<div class="metric-value"><b id="networkValue">0.0</b><span>m</span></div>
</div>
<div class="metric direct">
<div class="metric-label">Straight-line across water</div>
<div class="metric-value"><b id="directValue">0.0</b><span>m</span></div>
</div>
</div>
<div class="delta">
<span id="extraValue">+0.0 m detour</span>
<span id="extraPercent">0% extra</span>
</div>
<p class="explanation">
The network route follows streets and crosses a walkable bridge.
The straight-line measurement ignores streets, detours and bridge access.
</p>
</section>
<div class="controls">
<div class="hint">Drag to orbit · Shift/right-drag to pan · Wheel to zoom</div>
<button class="reset" id="resetView" type="button">Reset view</button>
</div>
</div>
<div class="loading" id="loading">Building river network…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, scene and custom camera
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdbeaec);
scene.fog = new THREE.Fog(0xdbeaec, 72, 115);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
const camera = new THREE.OrthographicCamera(-40, 40, 24, -24, 0.1, 180);
const cameraTarget = new THREE.Vector3(0, 0, 0);
let cameraTheta = 0.68;
let cameraPhi = 0.82;
const cameraRadius = 65;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
renderer.setSize(width, height, false);
const viewHeight = Math.max(43, 36 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
const hemisphere = new THREE.HemisphereLight(0xeaf8f4, 0x789080, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d0, 3.15);
sun.position.set(-24, 42, 27);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -43;
sun.shadow.camera.right = 43;
sun.shadow.camera.top = 43;
sun.shadow.camera.bottom = -43;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 100;
sun.shadow.bias = -0.00025;
scene.add(sun);
// -------------------------------------------------------------------------
// Small utility functions
// -------------------------------------------------------------------------
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0,
...options
});
}
function addBox(
width,
height,
depth,
x,
y,
z,
material,
parent = scene,
castShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addBeam(a, b, width, height, y, material, parent = scene) {
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz);
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, length),
material
);
mesh.position.set((a.x + b.x) / 2, y, (a.z + b.z) / 2);
mesh.rotation.y = Math.atan2(dx, dz);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function paintTextSprite(sprite, text, background = "#17343d", foreground = "#ffffff") {
const c = sprite.userData.canvas;
const ctx = sprite.userData.ctx;
ctx.clearRect(0, 0, c.width, c.height);
ctx.fillStyle = background;
ctx.fillRect(0, 0, c.width, c.height);
ctx.fillStyle = foreground;
ctx.fillRect(0, 0, 9, c.height);
ctx.font = "800 31px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, c.width / 2 + 3, c.height / 2 + 1);
sprite.userData.texture.needsUpdate = true;
const width = THREE.MathUtils.clamp(text.length * 0.28 + 1.4, 3.6, 8.8);
sprite.scale.set(width, 1.08, 1);
}
function makeTextSprite(
text,
x,
y,
z,
background = "#17343d",
foreground = "#ffffff"
) {
const c = document.createElement("canvas");
c.width = 512;
c.height = 96;
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.position.set(x, y, z);
sprite.renderOrder = 20;
sprite.userData.canvas = c;
sprite.userData.ctx = c.getContext("2d");
sprite.userData.texture = texture;
scene.add(sprite);
paintTextSprite(sprite, text, background, foreground);
return sprite;
}
function clearGroup(group) {
while (group.children.length) {
const child = group.children.pop();
child.traverse((object) => {
if (object.geometry) object.geometry.dispose();
if (object.material) {
if (Array.isArray(object.material)) {
object.material.forEach((material) => material.dispose());
} else {
object.material.dispose();
}
}
});
}
}
// -------------------------------------------------------------------------
// Voxel terrain and river
// -------------------------------------------------------------------------
const terrainMat = standardMaterial(0x91b98d);
const bankMat = standardMaterial(0xa9c99b);
const bankTopMat = standardMaterial(0xb6d2a7);
const roadBorderMat = standardMaterial(0x9b9b8d);
const roadMat = standardMaterial(0xd3cbb5);
const waterBaseMat = standardMaterial(0x327f9a);
const stoneMat = standardMaterial(0x8c9a91);
const woodMat = standardMaterial(0x7c4f2b);
const woodLightMat = standardMaterial(0xb17842);
const railMat = standardMaterial(0x5e3d27);
addBox(61, 1.6, 61, 0, -1.25, 0, terrainMat);
addBox(22.5, 1.2, 56, -16.75, -0.3, 0, bankMat);
addBox(22.5, 1.2, 56, 16.75, -0.3, 0, bankMat);
addBox(22.3, 0.09, 55.8, -16.75, 0.31, 0, bankTopMat, scene, false);
addBox(22.3, 0.09, 55.8, 16.75, 0.31, 0, bankTopMat, scene, false);
addBox(11, 0.36, 56, 0, -0.19, 0, waterBaseMat, scene, false);
const waterColors = [0x3e91ad, 0x459ab3, 0x3989a5, 0x4ba0b6];
const tileW = 11 / 6;
const tileD = 56 / 20;
for (let ix = 0; ix < 6; ix++) {
for (let iz = 0; iz < 20; iz++) {
const color = waterColors[(ix * 3 + iz * 5) % waterColors.length];
const material = standardMaterial(color, { roughness: 0.55 });
addBox(
tileW - 0.045,
0.055,
tileD - 0.045,
-5.5 + (ix + 0.5) * tileW,
-0.015,
-28 + (iz + 0.5) * tileD,
material,
scene,
false
);
}
}
// Stone riverbank blocks.
for (let z = -27; z <= 27; z += 2) {
if (Math.abs(Math.abs(z) - 12) < 2.2) continue;
addBox(0.48, 0.63, 1.82, -5.62, 0.22, z, stoneMat);
addBox(0.48, 0.63, 1.82, 5.62, 0.22, z, stoneMat);
}
// Small voxel water highlights.
const waveMat = standardMaterial(0xa6d9de, {
transparent: true,
opacity: 0.68,
roughness: 0.5
});
let seed = 19;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
for (let i = 0; i < 32; i++) {
const x = -4.5 + random() * 9;
const z = -26 + random() * 52;
const width = 0.45 + random() * 0.85;
addBox(width, 0.035, 0.12, x, 0.045, z, waveMat, scene, false);
}
const drifters = [];
const drifterMat = standardMaterial(0x85c3cc, {
transparent: true,
opacity: 0.72,
roughness: 0.6
});
for (let i = 0; i < 9; i++) {
const drifter = addBox(
0.18,
0.08,
0.75 + random() * 0.8,
-4 + random() * 8,
0.11,
-26 + random() * 52,
drifterMat,
scene,
false
);
drifters.push({
mesh: drifter,
speed: 0.35 + random() * 0.3
});
}
// Irregular flat grass patches.
const grassColors = [0x91b887, 0xa3c790, 0x87ad81];
const patchPositions = [
[-24, -23], [-8, -23], [-23, -9], [-7, -7], [-24, 6],
[-8, 9], [-24, 24], [-7, 25], [8, -23], [23, -23],
[8, -7], [23, -6], [23, 4], [8, 24], [23, 24]
];
patchPositions.forEach(([x, z], index) => {
const material = standardMaterial(grassColors[index % grassColors.length]);
addBox(
2.2 + (index % 3),
0.045,
1.3 + ((index + 1) % 3),
x,
0.34,
z,
material,
scene,
false
);
});
// -------------------------------------------------------------------------
// Walkable graph
// -------------------------------------------------------------------------
const nodes = {
willow: { x: -19, z: -18 },
pine: { x: -20, z: 0 },
cedar: { x: -18, z: 20 },
wN: { x: -12, z: -18 },
wN2: { x: -12, z: -12 },
detourA: { x: -18, z: -4 },
detourB: { x: -8, z: 0 },
detourC: { x: -18, z: 6 },
wS1: { x: -12, z: 12 },
wS2: { x: -12, z: 20 },
bNW: { x: -5.5, z: -12 },
bNE: { x: 5.5, z: -12 },
bSW: { x: -5.5, z: 12 },
bSE: { x: 5.5, z: 12 },
eN: { x: 12, z: -12 },
eMid: { x: 10, z: 0 },
eS: { x: 12, z: 12 },
market: { x: 15, z: 14 }
};
const edgeDefinitions = [
{ a: "willow", b: "wN" },
{ a: "wN", b: "wN2" },
{ a: "wN2", b: "bNW" },
{ a: "wN2", b: "detourA" },
{ a: "detourA", b: "pine" },
{ a: "detourA", b: "detourB" },
{ a: "detourB", b: "detourC" },
{ a: "detourC", b: "wS1" },
{ a: "wS1", b: "wS2" },
{ a: "wS2", b: "cedar" },
{ a: "wS1", b: "bSW" },
{
a: "bNW",
b: "bNE",
type: "bridge",
name: "North Footbridge"
},
{
a: "bSW",
b: "bSE",
type: "bridge",
name: "South Footbridge"
},
{ a: "bNE", b: "eN" },
{ a: "eN", b: "eMid" },
{ a: "eMid", b: "eS" },
{ a: "eS", b: "market" }
];
const adjacency = Object.fromEntries(
Object.keys(nodes).map((key) => [key, []])
);
const edges = edgeDefinitions.map((definition) => {
const a = nodes[definition.a];
const b = nodes[definition.b];
const edge = {
...definition,
length: Math.hypot(b.x - a.x, b.z - a.z)
};
adjacency[definition.a].push({ to: definition.b, edge });
adjacency[definition.b].push({ to: definition.a, edge });
return edge;
});
// Roads are drawn only for non-bridge graph edges.
edges.forEach((edge) => {
if (edge.type === "bridge") return;
const a = nodes[edge.a];
const b = nodes[edge.b];
addBeam(a, b, 2.55, 0.08, 0.39, roadBorderMat);
addBeam(a, b, 2.12, 0.10, 0.47, roadMat);
});
function shortestPath(start, end) {
const distances = {};
const previous = {};
const visited = new Set();
Object.keys(nodes).forEach((key) => {
distances[key] = Infinity;
});
distances[start] = 0;
while (visited.size < Object.keys(nodes).length) {
let current = null;
let smallest = Infinity;
for (const key of Object.keys(nodes)) {
if (!visited.has(key) && distances[key] < smallest) {
smallest = distances[key];
current = key;
}
}
if (current === null) break;
if (current === end) break;
visited.add(current);
for (const neighbor of adjacency[current]) {
const candidate = distances[current] + neighbor.edge.length;
if (candidate < distances[neighbor.to]) {
distances[neighbor.to] = candidate;
previous[neighbor.to] = {
from: current,
edge: neighbor.edge
};
}
}
}
if (!Number.isFinite(distances[end])) {
return { distance: Infinity, nodeIds: [], edges: [] };
}
const nodeIds = [end];
const pathEdges = [];
let current = end;
while (current !== start) {
const prior = previous[current];
pathEdges.unshift(prior.edge);
nodeIds.unshift(prior.from);
current = prior.from;
}
return {
distance: distances[end],
nodeIds,
edges: pathEdges
};
}
// -------------------------------------------------------------------------
// Bridges
// -------------------------------------------------------------------------
function buildBridge(z, name, labelBackground) {
const group = new THREE.Group();
scene.add(group);
for (let i = 0; i < 17; i++) {
const x = -5.6 + i * 0.7;
addBox(0.64, 0.25, 3.05, x, 0.49, z, woodLightMat, group);
}
addBox(12.2, 0.16, 0.16, 0, 1.51, z - 1.42, railMat, group);
addBox(12.2, 0.16, 0.16, 0, 1.51, z + 1.42, railMat, group);
for (let x = -5.5; x <= 5.6; x += 1.375) {
addBox(0.18, 1.38, 0.18, x, 1.05, z - 1.42, railMat, group);
addBox(0.18, 1.38, 0.18, x, 1.05, z + 1.42, railMat, group);
}
[-3.3, 0, 3.3].forEach((x) => {
addBox(0.52, 0.9, 2.5, x, -0.08, z, stoneMat, group);
});
makeTextSprite(name, 0, 4.35, z, labelBackground);
}
buildBridge(-12, "NORTH FOOTBRIDGE", "#9b5d0c");
buildBridge(12, "SOUTH FOOTBRIDGE", "#9b5d0c");
// -------------------------------------------------------------------------
// Houses, trees and market
// -------------------------------------------------------------------------
const clickableMeshes = [];
const houseDefinitions = {
willow: {
label: "WILLOW HOUSE",
x: -19,
z: -20.5,
wall: 0xe9b66f,
roof: 0xa94d3c,
color: "#e9b66f"
},
pine: {
label: "PINE HOUSE",
x: -20,
z: -2.3,
wall: 0xe8dfc4,
roof: 0x486e67,
color: "#6e978b"
},
cedar: {
label: "CEDAR HOUSE",
x: -18,
z: 22.2,
wall: 0xd9a56f,
roof: 0x8c5361,
color: "#c78b68"
}
};
function buildHouse(id, definition) {
const group = new THREE.Group();
group.position.set(definition.x, 0, definition.z);
scene.add(group);
const wall = standardMaterial(definition.wall);
const roof = standardMaterial(definition.roof);
const trim = standardMaterial(0xf8f0d9);
const door = standardMaterial(0x315c5c);
const windowMat = standardMaterial(0xffd984, {
emissive: 0xffb84d,
emissiveIntensity: 0.28
});
addBox(4.2, 2.7, 3.8, 0, 1.65, 0, wall, group);
addBox(4.55, 0.42, 4.1, 0, 3.16, 0, roof, group);
addBox(3.35, 0.42, 3.5, 0, 3.55, 0, roof, group);
addBox(2.2, 0.36, 2.6, 0, 3.88, -0.1, roof, group);
addBox(0.78, 1.55, 0.12, 0, 1.05, 1.94, door, group);
addBox(1.06, 0.16, 0.16, 0, 1.9, 2.02, trim, group);
addBox(0.86, 0.82, 0.13, -1.35, 1.65, 1.95, windowMat, group);
addBox(0.86, 0.82, 0.13, 1.35, 1.65, 1.95, windowMat, group);
addBox(0.58, 0.58, 0.13, -1.35, 1.65, -1.95, windowMat, group);
addBox(0.58, 0.58, 0.13, 1.35, 1.65, -1.95, windowMat, group);
addBox(0.65, 1.55, 0.65, 1.28, 4.16, -0.8, stoneMat, group);
addBox(1.05, 0.85, 1.05, 0, 0.76, 2.5, trim, group);
addBox(1.45, 0.25, 0.8, 0, 0.42, 2.7, stoneMat, group);
group.traverse((object) => {
if (object.isMesh) {
object.userData.stopId = id;
clickableMeshes.push(object);
}
});
makeTextSprite(
definition.label,
definition.x,
5.65,
definition.z,
"#17343d"
);
}
Object.entries(houseDefinitions).forEach(([id, definition]) => {
buildHouse(id, definition);
});
const foliageColors = [0x4f8060, 0x628f66, 0x3f7158];
const trunkMat = standardMaterial(0x72503b);
function buildTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0.3, z);
group.scale.setScalar(scale);
scene.add(group);
const foliage = standardMaterial(
foliageColors[Math.floor(random() * foliageColors.length)]
);
addBox(0.48, 1.7, 0.48, 0, 0.85, 0, trunkMat, group);
addBox(2.1, 1.5, 2.1, 0, 2.25, 0, foliage, group);
addBox(1.55, 1.1, 1.55, 0.1, 3.05, -0.08, foliage, group);
addBox(0.85, 0.8, 0.85, -0.1, 3.65, 0, foliage, group);
}
[
[-25, -13, 1], [-24, 8, 0.9], [-8, -23, 1.1],
[-8, 9, 0.85], [-25, 24, 1], [-7, 25, 0.9],
[8, -20, 1], [23, -18, 0.9], [22, -4, 1.05],
[22, 7, 0.9], [24, 14, 1.1], [8, 21, 0.85],
[23, 25, 1]
].forEach(([x, z, scale]) => buildTree(x, z, scale));
// Market plaza and striped voxel stalls.
addBox(11.5, 0.11, 8.8, 15.5, 0.4, 16.2, roadMat, scene, false);
const stallColors = [0xd85d4c, 0xe8b94f, 0x4e8c86];
const canopyLight = standardMaterial(0xf3e2bf);
[11.3, 15.4, 19.5].forEach((x, index) => {
const group = new THREE.Group();
group.position.set(x, 0, 16);
scene.add(group);
const color = standardMaterial(stallColors[index]);
const wood = standardMaterial(0xa56d3c);
addBox(3.05, 1.15, 1.65, 0, 0.93, 0, wood, group);
addBox(3.25, 0.3, 1.8, 0, 1.62, 0, color, group);
for (let stripe = 0; stripe < 4; stripe++) {
addBox(
0.76,
0.22,
2.25,
-1.16 + stripe * 0.77,
2.83,
0,
stripe % 2 === 0 ? color : canopyLight,
group
);
}
addBox(0.13, 2.35, 0.13, -1.45, 1.82, -0.88, wood, group);
addBox(0.13, 2.35, 0.13, 1.45, 1.82, -0.88, wood, group);
addBox(0.13, 2.35, 0.13, -1.45, 1.82, 0.88, wood, group);
addBox(0.13, 2.35, 0.13, 1.45, 1.82, 0.88, wood, group);
});
// Crates and a small riverside warehouse.
const crateMat = standardMaterial(0xad7846);
addBox(1.1, 1.1, 1.1, 9.7, 0.94, 18.8, crateMat);
addBox(0.85, 0.85, 0.85, 10.75, 0.8, 18.9, crateMat);
addBox(0.95, 0.95, 0.95, 10.2, 1.65, 18.8, crateMat);
addBox(5.1, 3.2, 4.2, 19.8, 1.9, 22, standardMaterial(0xd7c39c));
addBox(5.45, 0.45, 4.55, 19.8, 3.7, 22, standardMaterial(0x745343));
addBox(3.9, 0.45, 3.7, 19.8, 4.05, 22, standardMaterial(0x745343));
makeTextSprite("RIVERSIDE MARKET", 15.3, 6.25, 17, "#7c3f20");
// -------------------------------------------------------------------------
// Route drawing
// -------------------------------------------------------------------------
const faintRoutes = new THREE.Group();
const selectedRoute = new THREE.Group();
const directRoute = new THREE.Group();
scene.add(faintRoutes, selectedRoute, directRoute);
function routePoints(nodeIds) {
return nodeIds.map((id) => nodes[id]);
}
function createRouteVisual(points, color, width, opacity, parent) {
const material = standardMaterial(color, {
transparent: opacity < 1,
opacity,
roughness: 0.62,
emissive: color,
emissiveIntensity: 0.12
});
for (let i = 0; i < points.length - 1; i++) {
addBeam(
points[i],
points[i + 1],
width,
0.13,
0.68,
material,
parent
);
}
for (let i = 1; i < points.length - 1; i++) {
addBox(
width * 1.15,
width * 1.15,
width * 1.15,
points[i].x,
0.68,
points[i].z,
material,
parent
);
}
}
const routePaths = {};
["willow", "pine", "cedar"].forEach((id) => {
routePaths[id] = shortestPath(id, "market");
createRouteVisual(
routePoints(routePaths[id].nodeIds),
0x3c7774,
0.13,
0.32,
faintRoutes
);
});
let selectedMarker = null;
function selectStop(id) {
const path = routePaths[id];
const origin = nodes[id];
const destination = nodes.market;
const directDistance = Math.hypot(
destination.x - origin.x,
destination.z - origin.z
);
const extra = path.distance - directDistance;
const percent = (extra / directDistance) * 100;
const bridge = path.edges.find((edge) => edge.type === "bridge");
clearGroup(selectedRoute);
clearGroup(directRoute);
createRouteVisual(
routePoints(path.nodeIds),
0xf4a62a,
0.32,
1,
selectedRoute
);
const startMarkerMaterial = standardMaterial(0xf4a62a, {
emissive: 0xf4a62a,
emissiveIntensity: 0.5
});
selectedMarker = new THREE.Mesh(
new THREE.CylinderGeometry(0.65, 0.65, 0.13, 16),
startMarkerMaterial
);
selectedMarker.position.set(origin.x, 0.72, origin.z);
selectedRoute.add(selectedMarker);
// A faint continuous line plus voxel dashes for the direct measurement.
const directGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(origin.x, 1.26, origin.z),
new THREE.Vector3(destination.x, 1.26, destination.z)
]);
const directLine = new THREE.Line(
directGeometry,
new THREE.LineBasicMaterial({
color: 0xdb4f91,
transparent: true,
opacity: 0.28
})
);
directRoute.add(directLine);
const directMaterial = standardMaterial(0xdb4f91, {
emissive: 0xdb4f91,
emissiveIntensity: 0.26
});
const dx = destination.x - origin.x;
const dz = destination.z - origin.z;
const directLength = Math.hypot(dx, dz);
const dashLength = 0.55;
const dashGap = 0.55;
const dashCount = Math.max(1, Math.floor(directLength / (dashLength + dashGap)));
for (let i = 0; i < dashCount; i++) {
const centerT = (i + 0.5) / dashCount;
const half = dashLength / directLength / 2;
const a = {
x: origin.x + dx * (centerT - half),
z: origin.z + dz * (centerT - half)
};
const b = {
x: origin.x + dx * (centerT + half),
z: origin.z + dz * (centerT + half)
};
addBeam(a, b, 0.17, 0.13, 1.26, directMaterial, directRoute);
}
const endMarker = new THREE.Mesh(
new THREE.CylinderGeometry(0.58, 0.58, 0.13, 16),
directMaterial
);
endMarker.position.set(destination.x, 1.29, destination.z);
directRoute.add(endMarker);
document.querySelector("#fromLabel").textContent =
`From ${houseDefinitions[id].label.toLowerCase().replace(/\b\w/g, (c) => c.toUpperCase())}`;
document.querySelector("#routeTitle").textContent =
bridge ? `Via ${bridge.name}` : "Via walking network";
document.querySelector("#networkValue").textContent = path.distance.toFixed(1);
document.querySelector("#directValue").textContent = directDistance.toFixed(1);
document.querySelector("#extraValue").textContent = `+${extra.toFixed(1)} m detour`;
document.querySelector("#extraPercent").textContent = `${Math.round(percent)}% extra`;
document.querySelectorAll(".stop").forEach((button) => {
button.classList.toggle("active", button.dataset.id === id);
button.setAttribute("aria-pressed", button.dataset.id === id ? "true" : "false");
});
}
// -------------------------------------------------------------------------
// HUD stop buttons
// -------------------------------------------------------------------------
const stopContainer = document.querySelector("#stops");
Object.entries(houseDefinitions).forEach(([id, definition]) => {
const button = document.createElement("button");
button.className = "stop";
button.type = "button";
button.dataset.id = id;
button.setAttribute("aria-pressed", "false");
button.innerHTML = `
<span class="stop-dot" style="--stop-color:${definition.color}"></span>
<span class="stop-name">${definition.label.replace(" HOUSE", "")}</span>
`;
button.addEventListener("click", () => selectStop(id));
stopContainer.appendChild(button);
});
selectStop("willow");
// -------------------------------------------------------------------------
// Custom pointer controls: orbit, pan, zoom and picking
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerDown = false;
let pointerMoved = false;
let pointerButton = 0;
let lastX = 0;
let lastY = 0;
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
pointerDown = true;
pointerMoved = false;
pointerButton = event.button;
lastX = event.clientX;
lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!pointerDown) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
if (Math.abs(dx) + Math.abs(dy) > 4) pointerMoved = true;
const wantsPan = pointerButton === 2 || event.shiftKey;
if (wantsPan) {
const units =
((camera.top - camera.bottom) / camera.zoom) * 0.00135;
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrixWorld, 0);
right.y = 0;
right.normalize();
const upward = new THREE.Vector3()
.setFromMatrixColumn(camera.matrixWorld, 1);
upward.y = 0;
upward.normalize();
cameraTarget.addScaledVector(right, -dx * units);
cameraTarget.addScaledVector(upward, dy * units);
} else {
cameraTheta -= dx * 0.005;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi + dy * 0.004,
0.36,
1.28
);
}
lastX = event.clientX;
lastY = event.clientY;
updateCamera();
});
canvas.addEventListener("pointerup", (event) => {
pointerDown = false;
if (!pointerMoved && pointerButton === 0) {
const rect = canvas.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);
const hits = raycaster.intersectObjects(clickableMeshes, false);
if (hits.length) {
const stopId = hits[0].object.userData.stopId;
if (stopId) selectStop(stopId);
}
}
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", () => {
pointerDown = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
camera.zoom = THREE.MathUtils.clamp(
camera.zoom * Math.exp(-event.deltaY * 0.001),
0.65,
2.5
);
camera.updateProjectionMatrix();
},
{ passive: false }
);
document.querySelector("#resetView").addEventListener("click", () => {
cameraTheta = 0.68;
cameraPhi = 0.82;
camera.zoom = 1;
cameraTarget.set(0, 0, 0);
camera.updateProjectionMatrix();
updateCamera();
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
let previousTime = performance.now();
function animate(time) {
const delta = Math.min((time - previousTime) / 1000, 0.05);
previousTime = time;
drifters.forEach((drifter) => {
drifter.mesh.position.z += drifter.speed * delta;
if (drifter.mesh.position.z > 28) drifter.mesh.position.z = -28;
});
if (selectedMarker) {
const pulse = 1 + Math.sin(time * 0.0035) * 0.09;
selectedMarker.scale.set(pulse, 1, pulse);
}
renderer.render(scene, camera);
}
updateCamera();
renderer.setAnimationLoop(animate);
requestAnimationFrame(() => {
document.querySelector("#loading").classList.add("hidden");
});
</script>
</body>
</html>
```