Prompt specification
Build or customize the Three.js 3D scene "Compare along-canal and bridge-crossing routes" 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: Compare along-canal and bridge-crossing routes
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/compare-along-canal-and-bridge-crossing-routes-x3004
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts Gion's Shirakawa canal in Kyoto. Let users select two doorways on opposite sides of the watercourse. Show how bridge positions determine walking distance even when the destination is directly visible. Low-poly.
Working Three.js Starter (compare-along-canal-and-bridge-crossing-routes-x3004):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Gion — Shirakawa Canal</title>
<style>
:root {
--ink: #263331;
--muted: #69746f;
--paper: rgba(249, 247, 240, 0.94);
--east: #28a9bd;
--west: #ec6c70;
--walk: #f2ad32;
--goal: #65a96b;
--line: rgba(38, 51, 49, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce5df;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button,
select {
font: inherit;
}
#scene,
#labels {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
#labels {
z-index: 4;
pointer-events: none;
overflow: hidden;
}
.brand {
position: fixed;
z-index: 6;
top: 24px;
left: 26px;
max-width: min(440px, calc(100vw - 400px));
pointer-events: none;
text-shadow: 0 1px 14px rgba(255, 255, 255, 0.7);
}
.brand .eyebrow {
margin-bottom: 7px;
color: #68746e;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.24em;
text-transform: uppercase;
}
.brand h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(31px, 4vw, 52px);
font-weight: 500;
line-height: 0.96;
letter-spacing: -0.035em;
}
.brand p {
max-width: 390px;
margin: 12px 0 0;
color: #52605a;
font-size: 13px;
line-height: 1.55;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 13px;
margin-top: 12px;
color: #53615b;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.legend i {
width: 9px;
height: 9px;
border-radius: 50%;
background: currentColor;
}
.legend .east {
color: var(--east);
}
.legend .west {
color: var(--west);
}
.legend .walk {
color: var(--walk);
}
.panel {
position: fixed;
z-index: 10;
top: 18px;
right: 18px;
width: 342px;
max-height: calc(100vh - 36px);
overflow: auto;
padding: 19px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 18px;
background: var(--paper);
box-shadow:
0 18px 50px rgba(39, 51, 47, 0.16),
0 2px 8px rgba(39, 51, 47, 0.08);
backdrop-filter: blur(15px);
}
.panel-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
margin-bottom: 15px;
}
.panel h2 {
margin: 3px 0 0;
font-family: Georgia, "Times New Roman", serif;
font-size: 22px;
font-weight: 500;
}
.micro {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.selection {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-bottom: 13px;
}
.field label {
display: block;
margin: 0 0 6px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
select {
width: 100%;
min-height: 38px;
padding: 8px 28px 8px 10px;
border: 1px solid var(--line);
border-radius: 9px;
outline: none;
background: rgba(255, 255, 255, 0.8);
color: var(--ink);
font-size: 12px;
font-weight: 650;
}
select:focus {
border-color: rgba(38, 51, 49, 0.4);
box-shadow: 0 0 0 3px rgba(38, 51, 49, 0.06);
}
.route-summary {
position: relative;
overflow: hidden;
padding: 15px;
border: 1px solid rgba(242, 173, 50, 0.28);
border-radius: 13px;
background:
linear-gradient(135deg, rgba(255, 249, 230, 0.95),
rgba(255, 255, 255, 0.76));
}
.route-summary::after {
content: "";
position: absolute;
right: -23px;
bottom: -32px;
width: 100px;
height: 100px;
border: 15px solid rgba(242, 173, 50, 0.08);
border-radius: 50%;
pointer-events: none;
}
.distance-row {
display: flex;
align-items: baseline;
gap: 8px;
}
#distance {
font-family: Georgia, "Times New Roman", serif;
font-size: 43px;
line-height: 1;
letter-spacing: -0.04em;
}
.distance-unit {
color: var(--muted);
font-size: 13px;
font-weight: 700;
}
#viaText {
margin-top: 8px;
font-size: 12px;
font-weight: 800;
}
#comparisonText {
margin-top: 6px;
color: #69736e;
font-size: 11px;
line-height: 1.5;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin: 17px 0 8px;
color: var(--muted);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.bridge-options {
display: grid;
gap: 6px;
}
.bridge-option {
display: grid;
grid-template-columns: 31px 1fr auto;
align-items: center;
gap: 9px;
width: 100%;
padding: 9px 10px;
border: 1px solid transparent;
border-radius: 10px;
background: rgba(255, 255, 255, 0.53);
color: var(--ink);
text-align: left;
cursor: pointer;
transition: 140ms ease;
}
.bridge-option:hover {
transform: translateX(2px);
border-color: var(--line);
background: white;
}
.bridge-option.active {
border-color: rgba(242, 173, 50, 0.58);
background: rgba(255, 246, 221, 0.95);
}
.bridge-number {
display: grid;
place-items: center;
width: 28px;
height: 28px;
border-radius: 50%;
background: #e8ece8;
color: #56615c;
font-size: 9px;
font-weight: 850;
}
.bridge-option.active .bridge-number {
background: var(--walk);
color: #fff;
}
.bridge-name {
display: grid;
gap: 2px;
font-size: 11px;
}
.bridge-name small {
color: var(--muted);
font-size: 10px;
}
.bridge-length {
text-align: right;
font-size: 11px;
font-weight: 800;
}
.bridge-length small {
display: block;
margin-top: 2px;
color: #8a7770;
font-size: 9px;
font-weight: 650;
}
.shortest-badge {
display: inline-block;
margin-left: 5px;
padding: 2px 5px;
border-radius: 4px;
background: rgba(101, 169, 107, 0.16);
color: #4e8a54;
font-size: 7px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.panel-actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 11px;
}
.panel-actions button,
.reset-view {
min-height: 36px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.8);
color: var(--ink);
font-size: 10px;
font-weight: 800;
cursor: pointer;
transition: 140ms ease;
}
.panel-actions button:hover,
.reset-view:hover {
background: white;
border-color: rgba(38, 51, 49, 0.3);
}
.panel-actions .primary {
border-color: #303d38;
background: #303d38;
color: white;
}
.empty-state {
padding: 18px 12px;
border: 1px dashed var(--line);
border-radius: 11px;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
text-align: center;
}
.world-label {
position: absolute;
transform: translate(-50%, -50%);
white-space: nowrap;
user-select: none;
}
.door-label {
display: inline-flex;
align-items: center;
gap: 6px;
min-height: 25px;
padding: 5px 8px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 7px;
background: rgba(250, 248, 241, 0.92);
box-shadow: 0 3px 12px rgba(31, 45, 41, 0.14);
color: var(--ink);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.05em;
pointer-events: auto;
cursor: pointer;
}
.door-label::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--door-color);
}
.door-label.origin {
border-color: var(--walk);
background: #fff7df;
}
.door-label.destination {
border-color: var(--goal);
background: #f2faf0;
}
.bridge-label {
padding: 4px 7px;
border: 1px solid rgba(255, 255, 255, 0.78);
border-radius: 12px;
background: rgba(36, 48, 45, 0.78);
box-shadow: 0 2px 9px rgba(31, 45, 41, 0.12);
color: white;
font-size: 8px;
font-weight: 850;
letter-spacing: 0.04em;
pointer-events: auto;
cursor: pointer;
}
.bridge-label.active {
background: var(--walk);
color: #28322e;
}
.direct-label {
padding: 5px 8px;
border-radius: 8px;
background: rgba(111, 54, 67, 0.86);
color: white;
font-size: 8px;
font-weight: 850;
letter-spacing: 0.04em;
box-shadow: 0 3px 12px rgba(72, 30, 40, 0.14);
}
.hint {
position: fixed;
z-index: 6;
bottom: 22px;
left: 26px;
max-width: 330px;
color: rgba(41, 56, 51, 0.76);
font-size: 10px;
font-weight: 700;
line-height: 1.55;
pointer-events: none;
}
.hint kbd {
display: inline-block;
padding: 1px 5px;
border: 1px solid rgba(38, 51, 49, 0.2);
border-radius: 4px;
background: rgba(255, 255, 255, 0.55);
color: var(--ink);
font: inherit;
}
.reset-view {
position: fixed;
z-index: 11;
left: 24px;
bottom: 58px;
padding: 0 13px;
background: rgba(249, 247, 240, 0.84);
backdrop-filter: blur(8px);
}
.toast {
position: fixed;
z-index: 20;
left: 50%;
bottom: 26px;
transform: translate(-50%, 12px);
max-width: min(420px, calc(100vw - 30px));
padding: 10px 14px;
border-radius: 10px;
background: rgba(37, 49, 45, 0.92);
color: white;
font-size: 11px;
font-weight: 700;
opacity: 0;
transition: 180ms ease;
pointer-events: none;
}
.toast.visible {
transform: translate(-50%, 0);
opacity: 1;
}
@media (max-width: 760px) {
.brand {
top: 15px;
left: 17px;
max-width: calc(100vw - 34px);
}
.brand h1 {
font-size: 33px;
}
.brand p {
max-width: 300px;
font-size: 11px;
}
.legend {
display: none;
}
.panel {
top: auto;
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
max-height: 46vh;
padding: 14px;
border-radius: 15px;
}
.panel-head {
margin-bottom: 10px;
}
.panel h2 {
font-size: 19px;
}
.route-summary {
padding: 12px;
}
#distance {
font-size: 35px;
}
.hint,
.reset-view {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="labels"></div>
<header class="brand">
<div class="eyebrow">Kyoto · Gion · Walking study</div>
<h1>Shirakawa Canal</h1>
<p>
Choose two doorways on opposite banks. The destination may be directly
visible, but every walking route must find a bridge.
</p>
<div class="legend">
<span class="east"><i></i>East doorway</span>
<span class="west"><i></i>West doorway</span>
<span class="walk"><i></i>Walking route</span>
</div>
</header>
<aside class="panel" aria-label="Route planner">
<div class="panel-head">
<div>
<div class="micro">Walking network</div>
<h2>Door to door</h2>
</div>
<div class="micro">Low-poly study</div>
</div>
<div class="selection">
<div class="field">
<label for="originSelect">Start doorway</label>
<select id="originSelect"></select>
</div>
<div class="field">
<label for="destinationSelect">Destination</label>
<select id="destinationSelect"></select>
</div>
</div>
<div id="routeSummary" aria-live="polite">
<div class="distance-row">
<span id="distance">—</span>
<span class="distance-unit">m walk</span>
</div>
<div id="viaText">Choose two opposite-bank doorways</div>
<div id="comparisonText"></div>
</div>
<div id="emptyState" class="empty-state" hidden>
Select a destination on the opposite bank.
</div>
<div class="section-title" id="bridgeHeading">
Compare bridge positions
<span id="shortestNote"></span>
</div>
<div id="bridgeOptions" class="bridge-options"></div>
<div class="panel-actions">
<button id="shortestButton" class="primary" type="button">
Show shortest route
</button>
<button id="resetButton" type="button">
Reset example
</button>
</div>
</aside>
<button id="resetView" class="reset-view" type="button">
Reset camera
</button>
<div class="hint">
<kbd>Drag</kbd> orbit · <kbd>Right-drag / Shift-drag</kbd> pan ·
<kbd>Scroll</kbd> zoom · Click a doorway or bridge
</div>
<div id="toast" class="toast"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Constants and DOM
// ------------------------------------------------------------
const COLORS = {
east: 0x28a9bd,
west: 0xec6c70,
walk: 0xf2ad32,
goal: 0x65a96b,
direct: 0x9f5263,
ink: 0x263331
};
const DOOR_X = 5.45;
const BANK_X = 2.65;
const EDGE_X = 1.78;
const sceneHost = document.getElementById("scene");
const labelsHost = document.getElementById("labels");
const originSelect = document.getElementById("originSelect");
const destinationSelect = document.getElementById("destinationSelect");
const distanceElement = document.getElementById("distance");
const viaText = document.getElementById("viaText");
const comparisonText = document.getElementById("comparisonText");
const bridgeOptions = document.getElementById("bridgeOptions");
const bridgeHeading = document.getElementById("bridgeHeading");
const shortestNote = document.getElementById("shortestNote");
const emptyState = document.getElementById("emptyState");
const toast = document.getElementById("toast");
const doors = [];
const doorHitTargets = [];
const bridgeHitTargets = [];
const bridges = [];
const rippleMeshes = [];
let originId = "EAST2";
let destinationId = "WEST3";
let activeBridge = 0;
let candidates = [];
let activeRouteMaterials = [];
let toastTimer;
// ------------------------------------------------------------
// Renderer, scene, camera
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce5df);
scene.fog = new THREE.Fog(0xdce5df, 42, 100);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
180
);
const controls = {
yaw: 0.055,
pitch: 0.66,
radius: 40,
target: new THREE.Vector3(0, 0.7, -1),
dragging: false,
moved: false,
pointerId: null,
lastX: 0,
lastY: 0
};
function updateCamera() {
const { yaw, pitch, radius, target } = controls;
const horizontal = Math.cos(pitch) * radius;
camera.position.set(
target.x + Math.sin(yaw) * horizontal,
target.y + Math.sin(pitch) * radius,
target.z + Math.cos(yaw) * horizontal
);
camera.lookAt(target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(
0xf8f5e9,
0x718078,
2.25
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff0cf, 3.1);
sun.position.set(-17, 32, 18);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -38;
sun.shadow.camera.right = 38;
sun.shadow.camera.top = 42;
sun.shadow.camera.bottom = -42;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 100;
sun.shadow.bias = -0.0003;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xbad7db, 0.7);
fill.position.set(18, 14, -25);
scene.add(fill);
// ------------------------------------------------------------
// Small procedural helpers
// ------------------------------------------------------------
let randomSeed = 73421;
function random() {
randomSeed =
(randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
function material(color, roughness = 0.85, flatShading = false) {
return new THREE.MeshStandardMaterial({
color,
roughness,
metalness: 0,
flatShading
});
}
function box(w, h, d, mat, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(w, h, d),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinderBetween(start, end, radius, mat, parent = scene, sides = 7) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, sides),
mat
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
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((m) => m.dispose());
} else {
object.material.dispose();
}
}
});
}
}
function makeWorldLabel(className, text, onClick) {
const element = document.createElement("button");
element.type = "button";
element.className = `world-label ${className}`;
element.textContent = text;
element.addEventListener("click", onClick);
labelsHost.appendChild(element);
return element;
}
function showToast(message) {
toast.textContent = message;
toast.classList.add("visible");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
toast.classList.remove("visible");
}, 2200);
}
// ------------------------------------------------------------
// Ground, canal, embankments and promenades
// ------------------------------------------------------------
const groundMat = material(0xd8d4c3);
const pavementMat = material(0xc9c5b8);
const stoneMat = material(0x9da39d);
const stoneDarkMat = material(0x7c8580);
const waterMat = new THREE.MeshStandardMaterial({
color: 0x4d9ba1,
roughness: 0.3,
metalness: 0.08
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(150, 150),
groundMat
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.16;
ground.receiveShadow = true;
scene.add(ground);
box(3.45, 0.18, 68, waterMat, 0, 0.02, 0);
box(0.28, 0.55, 68, stoneDarkMat, -1.82, 0.16, 0);
box(0.28, 0.55, 68, stoneDarkMat, 1.82, 0.16, 0);
for (const side of [-1, 1]) {
box(2.05, 0.28, 68, pavementMat, side * 2.84, 0.13, 0);
box(1.35, 0.22, 68, groundMat, side * 4.53, 0.08, 0);
for (let z = -32; z <= 32; z += 2) {
box(
0.36,
0.12,
1.82,
side < 0 ? stoneMat : stoneDarkMat,
side * 1.84,
0.46,
z
);
}
}
// Low-poly water glints.
const rippleMaterial = new THREE.MeshBasicMaterial({
color: 0xc0e5df,
transparent: true,
opacity: 0.28,
depthWrite: false
});
for (let i = 0; i < 32; i++) {
const ripple = new THREE.Mesh(
new THREE.PlaneGeometry(0.55 + random() * 1.15, 0.025 + random() * 0.035),
rippleMaterial.clone()
);
ripple.rotation.x = -Math.PI / 2;
ripple.rotation.z = random() * Math.PI;
ripple.position.set(
(random() - 0.5) * 2.65,
0.125,
-32 + random() * 64
);
ripple.userData.baseZ = ripple.position.z;
ripple.userData.phase = random() * Math.PI * 2;
rippleMeshes.push(ripple);
scene.add(ripple);
}
// ------------------------------------------------------------
// Traditional low-poly buildings and selectable doorways
// ------------------------------------------------------------
const plasterMaterials = [
material(0xe4d6bd),
material(0xd7c5aa),
material(0xc9b79d),
material(0xeee3d0),
material(0xb9aa95)
];
const roofMaterials = [
material(0x343c3b),
material(0x424846),
material(0x4a4b44)
];
const woodMat = material(0x5d4433);
const darkWoodMat = material(0x362c25);
const doorMat = material(0x4a382d);
const windowMat = material(0x273330);
function makeBuilding(side, z, selectedDoorId = null) {
const depth = 4.4 + random() * 0.7;
const width = 5.2 + random() * 0.55;
const height = 3.65 + random() * 1.15;
const centerX = side * (5.55 + depth / 2);
const frontX = side * 5.51;
const plaster = plasterMaterials[
Math.floor(random() * plasterMaterials.length)
];
const roof = roofMaterials[
Math.floor(random() * roofMaterials.length)
];
box(depth, height, width, plaster, centerX, height / 2, z);
box(depth + 0.08, 0.35, width + 0.08,
stoneDarkMat, centerX, 0.18, z);
// Gabled roof with two simple low-poly slabs.
const half = depth / 2;
const rise = 0.9;
const slopeLength = Math.hypot(half, rise);
const slopeAngle = Math.atan2(rise, half);
const roofY = height + 0.68;
const leftRoof = box(
slopeLength + 0.15,
0.18,
width + 0.55,
roof,
centerX - half / 2,
roofY,
z
);
leftRoof.rotation.z = slopeAngle;
const rightRoof = box(
slopeLength + 0.15,
0.18,
width + 0.55,
roof,
centerX + half / 2,
roofY,
z
);
rightRoof.rotation.z = -slopeAngle;
// Horizontal timber facade bands.
box(
0.10,
0.10,
width - 0.45,
woodMat,
frontX,
height * 0.62,
z
);
if (height > 4.25) {
box(
0.10,
0.10,
width - 0.45,
woodMat,
frontX,
height * 0.86,
z
);
}
// Windows.
const windowY = height > 4.25 ? 3.55 : 2.55;
for (const offset of [-1.55, 0, 1.55]) {
box(
0.11,
0.68,
0.82,
windowMat,
frontX,
windowY,
z + offset
);
box(
0.14,
0.07,
0.95,
woodMat,
frontX,
windowY + 0.38,
z + offset
);
box(
0.14,
0.07,
0.95,
woodMat,
frontX,
windowY - 0.38,
z + offset
);
}
if (selectedDoorId) {
// Door panel and frame.
box(0.10, 1.72, 1.03, doorMat, frontX, 1.02, z);
box(0.17, 0.12, 1.28, darkWoodMat, frontX, 1.94, z);
box(0.17, 2.02, 0.12, darkWoodMat, frontX, 1.05, z - 0.61);
box(0.17, 2.02, 0.12, darkWoodMat, frontX, 1.05, z + 0.61);
box(0.15, 0.08, 0.94, woodMat, frontX, 1.18, z);
const sideName = side > 0 ? "east" : "west";
const sideColor = side > 0 ? COLORS.east : COLORS.west;
const markerMaterial = new THREE.MeshStandardMaterial({
color: sideColor,
emissive: sideColor,
emissiveIntensity: 0.15,
roughness: 0.5,
metalness: 0.05
});
const marker = new THREE.Mesh(
new THREE.TorusGeometry(0.39, 0.058, 6, 18),
markerMaterial
);
marker.position.set(side * 5.25, 1.24, z);
marker.rotation.y = Math.PI / 2;
marker.castShadow = false;
scene.add(marker);
// Larger invisible hit area for easy selection.
const hit = new THREE.Mesh(
new THREE.SphereGeometry(0.65, 8, 6),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
})
);
hit.position.copy(marker.position);
scene.add(hit);
const label = makeWorldLabel(
"door-label",
selectedDoorId,
() => selectDoor(selectedDoorId)
);
label.style.setProperty(
"--door-color",
side > 0 ? "#28a9bd" : "#ec6c70"
);
label.setAttribute(
"aria-label",
`Select ${sideName} bank doorway ${selectedDoorId}`
);
const door = {
id: selectedDoorId,
side: sideName,
sign: side,
z,
marker,
hit,
label,
anchor: new THREE.Vector3(side * 5.12, 2.38, z)
};
marker.userData.doorId = selectedDoorId;
hit.userData.doorId = selectedDoorId;
doors.push(door);
doorHitTargets.push(hit);
} else {
// Decorative, non-selectable doorway.
const decorativeZ = z + (random() - 0.5) * 1.4;
box(0.09, 1.52, 0.82, doorMat, frontX, 0.91, decorativeZ);
box(0.13, 0.09, 1.02, darkWoodMat, frontX, 1.72, decorativeZ);
}
}
const doorZPositions = [-17, -1, 15];
for (const side of [1, -1]) {
const sideName = side > 0 ? "east" : "west";
doorZPositions.forEach((z, index) => {
makeBuilding(
side,
z,
`${sideName.toUpperCase()}${index + 1}`
);
});
[-27, -9, 7, 23].forEach((z) => {
makeBuilding(side, z);
});
}
// ------------------------------------------------------------
// Trees, lanterns and small street details
// ------------------------------------------------------------
const trunkMat = material(0x695044);
const leafMaterials = [
material(0x789264, 0.95, true),
material(0x91a36c, 0.95, true),
material(0x657f59, 0.95, true),
material(0xc3a5a0, 0.95, true)
];
function makeTree(x, z, blossom = false) {
const group = new THREE.Group();
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.19, 2.0, 6),
trunkMat
);
trunk.position.y = 1.0;
trunk.castShadow = true;
group.add(trunk);
const leafMat = blossom
? leafMaterials[3]
: leafMaterials[Math.floor(random() * 3)];
for (let i = 0; i < 4; i++) {
const crown = new THREE.Mesh(
new THREE.IcosahedronGeometry(
0.72 + random() * 0.42,
0
),
leafMat
);
crown.position.set(
(random() - 0.5) * 0.9,
2.15 + random() * 0.72,
(random() - 0.5) * 0.9
);
crown.scale.y = 0.8 + random() * 0.35;
crown.castShadow = true;
crown.receiveShadow = true;
group.add(crown);
}
group.position.set(x, 0, z);
scene.add(group);
}
for (const side of [-1, 1]) {
[-30, -12, 3, 20, 29].forEach((z, index) => {
makeTree(side * 4.25, z + (index % 2 ? 1.2 : -0.8), index === 2);
});
}
const lanternGlow = new THREE.MeshStandardMaterial({
color: 0xffc36d,
emissive: 0xff9c35,
emissiveIntensity: 0.7,
roughness: 0.65
});
function makeLantern(x, z) {
const group = new THREE.Group();
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.095, 1.3, 6),
stoneDarkMat
);
post.position.y = 0.65;
post.castShadow = true;
group.add(post);
const lamp = box(
0.28,
0.34,
0.28,
lanternGlow,
0,
1.38,
0,
group
);
lamp.castShadow = false;
const cap = new THREE.Mesh(
new THREE.ConeGeometry(0.25, 0.17, 4),
darkWoodMat
);
cap.position.y = 1.63;
cap.rotation.y = Math.PI / 4;
group.add(cap);
group.position.set(x, 0, z);
scene.add(group);
}
// ------------------------------------------------------------
// Bridges
// ------------------------------------------------------------
const bridgeData = [
{ z: -23, name: "Willow Bridge" },
{ z: -9, name: "Machiya Bridge" },
{ z: 6, name: "Gion Bridge" },
{ z: 20, name: "Lantern Bridge" }
];
function createBridge(bridge, index) {
const group = new THREE.Group();
const deckMats = [
material(0x9c9b8e),
material(0xaaa798),
material(0x8d918b)
];
// Arched slab made from eight simple planks.
for (let i = 0; i < 8; i++) {
const x = -1.75 + i * 0.5;
const arch = 0.26 * (1 - Math.abs(x) / 1.9);
const plank = box(
0.51,
0.15,
1.43,
deckMats[i % deckMats.length],
x,
0.47 + arch,
bridge.z,
group
);
plank.castShadow = true;
}
// Dark wooden handrails.
for (const railZ of [bridge.z - 0.71, bridge.z + 0.71]) {
for (const postX of [-1.65, -0.82, 0, 0.82, 1.65]) {
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 0.055, 0.7, 6),
darkWoodMat
);
post.position.set(postX, 1.04, railZ);
post.castShadow = true;
group.add(post);
}
cylinderBetween(
new THREE.Vector3(-1.85, 1.36, railZ),
new THREE.Vector3(1.85, 1.36, railZ),
0.045,
darkWoodMat,
group
);
}
const hit = new THREE.Mesh(
new THREE.BoxGeometry(4.2, 1.5, 1.8),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
})
);
hit.position.set(0, 0.85, bridge.z);
hit.userData.bridgeIndex = index;
group.add(hit);
bridgeHitTargets.push(hit);
scene.add(group);
const label = makeWorldLabel(
"bridge-label",
`B${index + 1}`,
() => {
if (candidates.length) {
activeBridge = index;
renderRouteState();
}
}
);
Object.assign(bridge, {
group,
hit,
label,
anchor: new THREE.Vector3(0, 1.85, bridge.z)
});
bridges.push(bridge);
}
bridgeData.forEach(createBridge);
bridgeData.forEach((bridge) => {
makeLantern(-3.12, bridge.z + 1.0);
makeLantern(3.12, bridge.z + 1.0);
});
// ------------------------------------------------------------
// Route geometry
// ------------------------------------------------------------
const routeGroup = new THREE.Group();
const directGroup = new THREE.Group();
scene.add(routeGroup, directGroup);
const directLabel = makeWorldLabel(
"direct-label",
"DIRECT SIGHTLINE",
() => {}
);
directLabel.style.pointerEvents = "none";
directLabel.userData = {};
function horizontalLength(points) {
let length = 0;
for (let i = 1; i < points.length; i++) {
const dx = points[i].x - points[i - 1].x;
const dz = points[i].z - points[i - 1].z;
length += Math.hypot(dx, dz);
}
return length;
}
function makeRoutePoints(start, destination, bridgeZ) {
const s = start.sign;
return [
new THREE.Vector3(s * DOOR_X, 0.39, start.z),
new THREE.Vector3(s * BANK_X, 0.39, start.z),
new THREE.Vector3(s * BANK_X, 0.39, bridgeZ),
new THREE.Vector3(s * EDGE_X, 0.69, bridgeZ),
new THREE.Vector3(0, 0.98, bridgeZ),
new THREE.Vector3(-s * EDGE_X, 0.69, bridgeZ),
new THREE.Vector3(-s * BANK_X, 0.39, bridgeZ),
new THREE.Vector3(-s * BANK_X, 0.39, destination.z),
new THREE.Vector3(-s * DOOR_X, 0.39, destination.z)
];
}
function drawRoute(points, color, opacity, radius) {
const routeMaterial = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity,
depthWrite: false
});
for (let i = 1; i < points.length; i++) {
cylinderBetween(
points[i - 1],
points[i],
radius,
routeMaterial,
routeGroup,
6
);
}
return routeMaterial;
}
function computeCandidates() {
const origin = doors.find((door) => door.id === originId);
const destination = doors.find(
(door) => door.id === destinationId
);
if (!origin || !destination) {
candidates = [];
return;
}
candidates = bridges.map((bridge, index) => {
const points = makeRoutePoints(
origin,
destination,
bridge.z
);
return {
index,
bridge,
points,
length: horizontalLength(points)
};
});
}
function drawDirectLine(origin, destination) {
clearGroup(directGroup);
const start = new THREE.Vector3(
origin.sign * DOOR_X,
1.35,
origin.z
);
const end = new THREE.Vector3(
-destination.sign * DOOR_X,
1.35,
destination.z
);
const geometry = new THREE.BufferGeometry().setFromPoints([
start,
end
]);
const line = new THREE.Line(
geometry,
new THREE.LineDashedMaterial({
color: COLORS.direct,
dashSize: 0.38,
gapSize: 0.23,
transparent: true,
opacity: 0.9,
depthWrite: false
})
);
line.computeLineDistances();
directGroup.add(line);
const midpoint = start.clone().lerp(end, 0.5);
midpoint.y += 0.62;
directLabel.userData.anchor = midpoint;
directLabel.style.display = "block";
}
// ------------------------------------------------------------
// Small walking pedestrian
// ------------------------------------------------------------
const pedestrian = new THREE.Group();
const coatMat = material(0x325f66);
const skinMat = material(0xd8b38f);
const trousersMat = material(0x34404a);
const torso = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.17, 0.46, 6),
coatMat
);
torso.position.y = 0.55;
torso.castShadow = true;
pedestrian.add(torso);
const head = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.15, 1),
skinMat
);
head.position.y = 0.93;
head.castShadow = true;
pedestrian.add(head);
const leftLeg = box(
0.09,
0.31,
0.1,
trousersMat,
-0.08,
0.19,
0,
pedestrian
);
const rightLeg = box(
0.09,
0.31,
0.1,
trousersMat,
0.08,
0.19,
0,
pedestrian
);
scene.add(pedestrian);
function samplePolyline(points, distance) {
const lengths = [];
let total = 0;
for (let i = 1; i < points.length; i++) {
const length = points[i].distanceTo(points[i - 1]);
lengths.push(length);
total += length;
}
let remaining = ((distance % total) + total) % total;
for (let i = 0; i < lengths.length; i++) {
if (remaining <= lengths[i]) {
const t = lengths[i] === 0
? 0
: remaining / lengths[i];
const point = points[i].clone().lerp(
points[i + 1],
t
);
const direction = points[i + 1]
.clone()
.sub(points[i]);
return { point, direction };
}
remaining -= lengths[i];
}
return {
point: points[points.length - 1].clone(),
direction: new THREE.Vector3(0, 0, 1)
};
}
// ------------------------------------------------------------
// UI and route calculations
// ------------------------------------------------------------
function populateSelects() {
originSelect.innerHTML = doors
.map((door) => {
const bank = door.side === "east" ? "East" : "West";
return `<option value="${door.id}">${bank} · ${door.id}</option>`;
})
.join("");
const origin = doors.find((door) => door.id === originId);
destinationSelect.innerHTML = doors
.filter((door) => !origin || door.side !== origin.side)
.map((door) => {
const bank = door.side === "east" ? "East" : "West";
return `<option value="${door.id}">${bank} · ${door.id}</option>`;
})
.join("");
originSelect.value = originId || "";
if (origin && destinationId) {
const destination = doors.find(
(door) => door.id === destinationId
);
if (destination && destination.side !== origin.side) {
destinationSelect.value = destinationId;
}
}
}
function selectDoor(id) {
const door = doors.find((item) => item.id === id);
if (!door) return;
if (!originId) {
originId = id;
destinationId = null;
} else if (!destinationId) {
if (door.side === originDoor()?.side) {
originId = id;
} else {
destinationId = id;
}
} else if (door.side === originDoor()?.side) {
originId = id;
} else {
destinationId = id;
}
refreshRoute();
}
function originDoor() {
return doors.find((door) => door.id === originId);
}
function destinationDoor() {
return doors.find((door) => door.id === destinationId);
}
function refreshRoute() {
const origin = originDoor();
const destination = destinationDoor();
if (origin && destination && origin.side === destination.side) {
destinationId = null;
}
computeCandidates();
if (candidates.length) {
const shortest = Math.min(
...candidates.map((candidate) => candidate.length)
);
activeBridge = candidates.find(
(candidate) => candidate.length === shortest
).index;
}
populateSelects();
renderRouteState();
updateDoorMarkers();
}
function updateDoorMarkers() {
for (const door of doors) {
let role = "";
let markerColor = door.side === "east" ? COLORS.east : COLORS.west;
let emissiveIntensity = 0.15;
if (door.id === originId) {
role = "origin";
markerColor = COLORS.walk;
emissiveIntensity = 0.72;
} else if (door.id === destinationId) {
role = "destination";
markerColor = COLORS.goal;
emissiveIntensity = 0.62;
}
door.marker.material.color.setHex(markerColor);
door.marker.material.emissive.setHex(markerColor);
door.marker.material.emissiveIntensity = emissiveIntensity;
door.marker.scale.setScalar(role ? 1.17 : 1);
door.label.className = `world-label door-label ${role}`;
door.label.textContent = role
? `${role === "origin" ? "START" : "GOAL"} · ${door.id}`
: door.id;
}
}
function renderRouteState() {
clearGroup(routeGroup);
clearGroup(directGroup);
activeRouteMaterials = [];
const origin = originDoor();
const destination = destinationDoor();
if (!origin || !destination) {
distanceElement.textContent = "—";
viaText.textContent = "Choose two opposite-bank doorways";
comparisonText.textContent = "";
bridgeOptions.innerHTML = "";
bridgeHeading.hidden = true;
emptyState.hidden = false;
directLabel.style.display = "none";
delete directLabel.userData.anchor;
pedestrian.visible = false;
for (const bridge of bridges) {
bridge.label.classList.remove("active");
}
return;
}
emptyState.hidden = true;
bridgeHeading.hidden = false;
const directDistance = Math.hypot(
Math.abs(origin.sign * DOOR_X -
destination.sign * DOOR_X),
Math.abs(destination.z - origin.z)
);
const sorted = [...candidates].sort(
(a, b) => a.length - b.length
);
const shortestIndex = sorted[0].index;
const active = candidates[activeBridge];
// Faint alternatives.
for (const candidate of candidates) {
if (candidate.index === activeBridge) continue;
drawRoute(
candidate.points,
0x78958e,
0.18,
0.026
);
}
// Selected route.
const activeMaterial = drawRoute(
active.points,
COLORS.walk,
0.96,
0.075
);
activeRouteMaterials.push(activeMaterial);
drawDirectLine(origin, destination);
distanceElement.textContent = active.length.toFixed(1);
viaText.textContent =
`Via Bridge ${active.index + 1} · ${active.bridge.name}`;
const detour = active.length - directDistance;
comparisonText.textContent =
`Straight sightline: ${directDistance.toFixed(1)} m. ` +
`The walk is ${detour.toFixed(1)} m longer because the canal ` +
`must be crossed at the bridge.`;
shortestNote.textContent =
`Shortest: ${sorted[0].length.toFixed(1)} m`;
bridgeOptions.innerHTML = "";
for (const candidate of sorted) {
const button = document.createElement("button");
button.type = "button";
button.className = "bridge-option";
if (candidate.index === activeBridge) {
button.classList.add("active");
}
const extra = candidate.length - directDistance;
const badge = candidate.index === shortestIndex
? `<span class="shortest-badge">Best</span>`
: "";
button.innerHTML = `
<span class="bridge-number">B${candidate.index + 1}</span>
<span class="bridge-name">
<strong>${candidate.bridge.name}${badge}</strong>
<small>${candidate.length.toFixed(1)} m total walk</small>
</span>
<span class="bridge-length">
${candidate.length.toFixed(1)} m
<small>+${extra.toFixed(1)} m vs sightline</small>
</span>
`;
button.addEventListener("click", () => {
activeBridge = candidate.index;
renderRouteState();
});
bridgeOptions.appendChild(button);
}
for (const bridge of bridges) {
const candidate = candidates.find(
(item) => item.index === bridges.indexOf(bridge)
);
bridge.label.textContent =
`B${bridges.indexOf(bridge) + 1} · ${candidate.length.toFixed(1)} m`;
bridge.label.classList.toggle(
"active",
bridges.indexOf(bridge) === activeBridge
);
}
pedestrian.visible = true;
}
originSelect.addEventListener("change", () => {
originId = originSelect.value;
const origin = originDoor();
const destination = destinationDoor();
if (origin && destination && origin.side === destination.side) {
destinationId = null;
}
refreshRoute();
});
destinationSelect.addEventListener("change", () => {
destinationId = destinationSelect.value;
refreshRoute();
});
document.getElementById("shortestButton").addEventListener("click", () => {
if (!candidates.length) return;
activeBridge = [...candidates].sort(
(a, b) => a.length - b.length
)[0].index;
renderRouteState();
});
document.getElementById("resetButton").addEventListener("click", () => {
originId = "EAST2";
destinationId = "WEST3";
refreshRoute();
showToast("Example restored: East EAST2 to West WEST3.");
});
// ------------------------------------------------------------
// Direct camera controls — no addons
// ------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function resetCamera() {
controls.yaw = 0.055;
controls.pitch = 0.66;
controls.radius = 40;
controls.target.set(0, 0.7, -1);
updateCamera();
}
document.getElementById("resetView").addEventListener("click", resetCamera);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("pointerdown", (event) => {
controls.dragging = true;
controls.moved = false;
controls.pointerId = event.pointerId;
controls.lastX = event.clientX;
controls.lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!controls.dragging || controls.pointerId !== event.pointerId) {
return;
}
const dx = event.clientX - controls.lastX;
const dy = event.clientY - controls.lastY;
controls.lastX = event.clientX;
controls.lastY = event.clientY;
if (Math.abs(dx) + Math.abs(dy) > 1) {
controls.moved = true;
}
if (event.button === 2 || event.shiftKey) {
const panScale = controls.radius * 0.0015;
const right = new THREE.Vector3(
Math.cos(controls.yaw),
0,
-Math.sin(controls.yaw)
);
const forward = new THREE.Vector3(
-Math.sin(controls.yaw),
0,
-Math.cos(controls.yaw)
);
controls.target.addScaledVector(right, -dx * panScale);
controls.target.addScaledVector(forward, dy * panScale);
} else {
controls.yaw -= dx * 0.005;
controls.pitch = THREE.MathUtils.clamp(
controls.pitch + dy * 0.004,
0.32,
1.2
);
}
updateCamera();
});
renderer.domElement.addEventListener("pointerup", (event) => {
const wasClick =
controls.dragging &&
!controls.moved &&
event.button === 0;
controls.dragging = false;
controls.pointerId = null;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
if (!wasClick) return;
const rect = renderer.domElement.getBoundingClientRect();
pointer.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const doorHits = raycaster.intersectObjects(
doorHitTargets,
false
);
if (doorHits.length) {
selectDoor(doorHits[0].object.userData.doorId);
return;
}
const bridgeHits = raycaster.intersectObjects(
bridgeHitTargets,
false
);
if (bridgeHits.length && candidates.length) {
activeBridge = bridgeHits[0].object.userData.bridgeIndex;
renderRouteState();
}
});
renderer.domElement.addEventListener("pointercancel", () => {
controls.dragging = false;
controls.pointerId = null;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.radius = THREE.MathUtils.clamp(
controls.radius + event.deltaY * 0.025,
16,
72
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (
event.target instanceof HTMLInputElement ||
event.target instanceof HTMLSelectElement
) {
return;
}
const step = 0.08;
if (event.key === "ArrowLeft") {
controls.yaw += step;
} else if (event.key === "ArrowRight") {
controls.yaw -= step;
} else if (event.key === "ArrowUp") {
controls.pitch = Math.min(
1.2,
controls.pitch + step * 0.5
);
} else if (event.key === "ArrowDown") {
controls.pitch = Math.max(
0.32,
controls.pitch - step * 0.5
);
} else if (event.key === "+" || event.key === "=") {
controls.radius = Math.max(16, controls.radius - 2);
} else if (event.key === "-" || event.key === "_") {
controls.radius = Math.min(72, controls.radius + 2);
} else if (event.key.toLowerCase() === "r") {
resetCamera();
} else {
return;
}
event.preventDefault();
updateCamera();
});
// ------------------------------------------------------------
// Project world labels into screen space
// ------------------------------------------------------------
const projected = new THREE.Vector3();
function positionWorldLabel(element, anchor) {
if (!anchor) {
element.style.display = "none";
return;
}
projected.copy(anchor).project(camera);
if (projected.z < -1 || projected.z > 1) {
element.style.display = "none";
return;
}
element.style.display = "";
element.style.left =
`${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top =
`${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
function updateLabels() {
for (const door of doors) {
positionWorldLabel(door.label, door.anchor);
}
for (const bridge of bridges) {
positionWorldLabel(bridge.label, bridge.anchor);
}
positionWorldLabel(
directLabel,
directLabel.userData.anchor
);
}
// ------------------------------------------------------------
// Resize and animation
// ------------------------------------------------------------
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
const time = elapsed % 1000;
for (const ripple of rippleMeshes) {
ripple.position.z =
ripple.userData.baseZ +
Math.sin(time * 0.45 + ripple.userData.phase) * 0.13;
ripple.material.opacity =
0.17 + Math.sin(time * 0.7 + ripple.userData.phase) * 0.08;
}
if (candidates.length && pedestrian.visible) {
const active = candidates[activeBridge];
const sample = samplePolyline(
active.points,
elapsed * 2.25
);
pedestrian.position.copy(sample.point);
pedestrian.position.y += 0.03;
if (sample.direction.lengthSq() > 0.001) {
pedestrian.rotation.y = Math.atan2(
sample.direction.x,
sample.direction.z
);
}
const stride = Math.sin(elapsed * 10) * 0.035;
leftLeg.position.z = stride;
rightLeg.position.z = -stride;
leftLeg.rotation.x = stride * 5;
rightLeg.rotation.x = -stride * 5;
}
if (activeRouteMaterials.length) {
activeRouteMaterials[0].opacity =
0.88 + Math.sin(elapsed * 3.2) * 0.1;
}
updateLabels();
renderer.render(scene, camera);
}
refreshRoute();
updateCamera();
animate();
</script>
</body>
</html>
```