Prompt specification
Build or customize the Three.js 3D scene "Two Routes One Courier" 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: Two Routes One Courier
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-routes-one-courier-x3594
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring weighted routes and travel costs. Build a tiny map with a short expensive route and a longer cheap route. Animate both choices and total costs from the same starting state. Low-poly.
Working Three.js Starter (two-routes-one-courier-x3594):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Weighted Route Lab</title>
<style>
:root {
--ink: #f4f5e9;
--muted: #a8b8ae;
--ridge: #ffb45e;
--valley: #4de0bd;
--panel: rgba(12, 29, 25, 0.84);
--line: rgba(255, 255, 255, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #0b1c17;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene.dragging {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 55% 46%, transparent 38%, rgba(3, 12, 9, 0.32) 100%),
linear-gradient(180deg, rgba(5, 15, 11, 0.12), transparent 45%);
}
.glass {
background: var(--panel);
border: 1px solid var(--line);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
}
header {
position: fixed;
top: 24px;
left: 24px;
width: min(390px, calc(100vw - 48px));
padding: 22px 23px 20px;
border-radius: 22px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 11px;
color: #c8d6ce;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--valley);
box-shadow: 0 0 14px var(--valley);
content: "";
}
h1 {
margin: 0;
font-size: clamp(30px, 4vw, 43px);
line-height: 0.98;
letter-spacing: -0.055em;
}
h1 span {
color: var(--ridge);
}
header p {
max-width: 330px;
margin: 14px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.comparison {
position: fixed;
bottom: 24px;
left: 24px;
width: min(410px, calc(100vw - 48px));
padding: 17px;
border-radius: 22px;
}
.panel-head {
display: flex;
align-items: center;
justify-content: space-between;
margin: 0 2px 11px;
}
.panel-title {
font-size: 12px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.replay {
border: 1px solid var(--line);
border-radius: 999px;
padding: 7px 11px;
color: var(--ink);
background: rgba(255, 255, 255, 0.06);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition: background 160ms ease, transform 160ms ease;
}
.replay:hover {
background: rgba(255, 255, 255, 0.12);
transform: translateY(-1px);
}
.weight-legend {
display: flex;
align-items: center;
gap: 8px;
margin: 0 2px 10px;
color: #8fa49a;
font-size: 10px;
}
.weight-chip {
display: inline-grid;
width: 23px;
height: 19px;
place-items: center;
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 6px;
color: #213229;
background: #fff2d2;
font-size: 10px;
font-weight: 900;
box-shadow: 0 3px 8px rgba(0, 0, 0, 0.18);
}
.route-card {
position: relative;
display: grid;
grid-template-columns: 12px 1fr auto;
gap: 10px;
width: 100%;
margin: 0 0 8px;
padding: 12px 12px 15px;
overflow: hidden;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 14px;
color: var(--ink);
background: rgba(255, 255, 255, 0.035);
font: inherit;
text-align: left;
cursor: pointer;
transition: border-color 160ms ease, background 160ms ease, transform 160ms ease;
}
.route-card:hover,
.route-card.active {
border-color: rgba(255, 255, 255, 0.23);
background: rgba(255, 255, 255, 0.07);
transform: translateY(-1px);
}
.route-card.ridge {
--route-color: var(--ridge);
}
.route-card.valley {
--route-color: var(--valley);
}
.route-dot {
width: 10px;
height: 10px;
margin-top: 4px;
border-radius: 50%;
background: var(--route-color);
box-shadow: 0 0 14px color-mix(in srgb, var(--route-color), transparent 35%);
}
.route-name {
display: block;
font-size: 14px;
font-weight: 800;
letter-spacing: -0.015em;
}
.route-kind {
display: block;
margin-top: 3px;
color: #8fa49a;
font-size: 10px;
}
.route-total {
display: flex;
align-items: baseline;
gap: 4px;
white-space: nowrap;
}
.route-total strong {
color: var(--route-color);
font-size: 25px;
line-height: 1;
font-variant-numeric: tabular-nums;
}
.route-total span {
color: #82978d;
font-size: 9px;
}
.route-bar {
position: absolute;
right: 12px;
bottom: 7px;
left: 34px;
height: 3px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.08);
}
.route-bar i {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
background: var(--route-color);
box-shadow: 0 0 9px var(--route-color);
}
.route-status {
grid-column: 2 / 4;
margin-top: 8px;
color: #8fa49a;
font-size: 9px;
}
.verdict {
margin-top: 11px;
padding: 11px 12px;
border-radius: 13px;
background: rgba(0, 0, 0, 0.16);
}
.verdict span {
display: block;
margin-bottom: 4px;
color: #789087;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.verdict strong {
display: block;
font-size: 12px;
line-height: 1.4;
}
.formula {
margin: 6px 0 0;
color: #849b90;
font-size: 9px;
line-height: 1.45;
}
.view-switcher {
position: fixed;
top: 24px;
right: 24px;
display: flex;
gap: 5px;
padding: 6px;
border-radius: 999px;
}
.view-switcher button {
padding: 8px 11px;
border: 0;
border-radius: 999px;
color: #aabbb2;
background: transparent;
font: inherit;
font-size: 10px;
font-weight: 750;
cursor: pointer;
}
.view-switcher button:hover,
.view-switcher button.active {
color: #13251e;
background: #dce8df;
}
.hint {
position: fixed;
right: 24px;
bottom: 24px;
padding: 10px 13px;
border-radius: 999px;
color: #9eb0a7;
font-size: 10px;
pointer-events: none;
}
#loading {
position: fixed;
inset: 0;
display: grid;
place-items: center;
z-index: 20;
color: #dce9e1;
background: #0b1c17;
font-size: 12px;
letter-spacing: 0.12em;
text-transform: uppercase;
transition: opacity 300ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 700px) {
header {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 16px;
border-radius: 17px;
}
header p {
display: none;
}
h1 {
font-size: 29px;
}
.comparison {
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
padding: 13px;
border-radius: 17px;
}
.view-switcher {
top: 116px;
right: 12px;
}
.hint {
display: none;
}
.route-card {
padding: 9px 10px 13px;
}
.route-total strong {
font-size: 21px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly map comparing two weighted routes"></canvas>
<div class="vignette"></div>
<header class="glass">
<div class="eyebrow">Weighted route lab · simultaneous start</div>
<h1>Short doesn’t mean<br><span>cheap.</span></h1>
<p>
Two travellers leave Camp at the same moment. The map is shorter in
kilometres, but the cost of its links tells a different story.
</p>
</header>
<section class="comparison glass" aria-label="Live route comparison">
<div class="panel-head">
<span class="panel-title">Live travel cost</span>
<button class="replay" id="replay" type="button">↻ Replay</button>
</div>
<div class="weight-legend">
<span class="weight-chip">4</span>
<span>Numbers on the map are individual link costs.</span>
</div>
<button class="route-card ridge active" data-focus="ridge" type="button">
<span class="route-dot"></span>
<span>
<span class="route-name">Ridge run</span>
<span class="route-kind">Short path · 4 links</span>
</span>
<span class="route-total">
<strong id="ridge-cost">0</strong>
<span>/ 30 cost</span>
</span>
<span class="route-bar"><i id="ridge-bar"></i></span>
<span class="route-status" id="ridge-status">At Camp · ready</span>
</button>
<button class="route-card valley" data-focus="valley" type="button">
<span class="route-dot"></span>
<span>
<span class="route-name">Valley loop</span>
<span class="route-kind">Long path · 6 links</span>
</span>
<span class="route-total">
<strong id="valley-cost">0</strong>
<span>/ 22 cost</span>
</span>
<span class="route-bar"><i id="valley-bar"></i></span>
<span class="route-status" id="valley-status">At Camp · ready</span>
</button>
<div class="verdict" aria-live="polite">
<span>Route verdict</span>
<strong id="verdict-text">
Ridge is shorter on the ground, but every link is more expensive.
</strong>
<p class="formula" id="formula">
Ridge: 8 + 9 + 7 + 6 = 30 · Valley: 4 + 4 + 3 + 4 + 4 + 3 = 22
</p>
</div>
</section>
<nav class="view-switcher glass" aria-label="Camera views">
<button class="active" data-view="overview" type="button">Overview</button>
<button data-view="ridge" type="button">Ridge</button>
<button data-view="valley" type="button">Valley</button>
</nav>
<div class="hint glass">Drag to orbit · Shift-drag to pan · Scroll to zoom</div>
<div id="loading">Building the little map…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
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;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0b1c17);
scene.fog = new THREE.Fog(0x0b1c17, 19, 37);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemi = new THREE.HemisphereLight(0xd8f4e4, 0x23352a, 2.1);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffe3b3, 3.5);
sun.position.set(-7, 13, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0003;
scene.add(sun);
const world = new THREE.Group();
scene.add(world);
const V = (x, y, z) => new THREE.Vector3(x, y, z);
const UP = V(0, 1, 0);
const routes = [
{
id: "ridge",
name: "Ridge run",
color: 0xffb45e,
css: "#ffb45e",
costs: [8, 9, 7, 6],
nodes: [
V(-7.0, 0, 4.3),
V(-4.3, 0, 5.7),
V(-1.2, 0, 5.1),
V(1.8, 0, 2.7),
V(5.7, 0, 0.4)
]
},
{
id: "valley",
name: "Valley loop",
color: 0x4de0bd,
css: "#4de0bd",
costs: [4, 4, 3, 4, 4, 3],
nodes: [
V(-7.0, 0, 4.3),
V(-7.1, 0, 0.5),
V(-5.3, 0, -3.5),
V(-2.2, 0, -5.6),
V(1.0, 0, -5.2),
V(3.4, 0, -3.4),
V(5.1, 0, -1.3),
V(5.7, 0, 0.4)
]
}
];
for (const route of routes) {
route.segments = route.nodes.slice(1).map((b, i) => {
const a = route.nodes[i];
return {
a,
b,
length: a.distanceTo(b),
cost: route.costs[i] ?? 0
};
});
route.totalLength = route.segments.reduce((sum, s) => sum + s.length, 0);
route.totalCost = route.costs.reduce((sum, cost) => sum + cost, 0);
}
function makeLabel(
text,
{
background = "rgba(13, 31, 25, 0.9)",
foreground = "#edf5ef",
border = "rgba(255,255,255,0.18)",
scale = 1
} = {}
) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 256;
labelCanvas.height = 96;
const ctx = labelCanvas.getContext("2d");
const x = 8;
const y = 8;
const w = 240;
const h = 80;
const r = 19;
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + w - r, y);
ctx.quadraticCurveTo(x + w, y, x + w, y + r);
ctx.lineTo(x + w, y + h - r);
ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
ctx.lineTo(x + r, y + h);
ctx.quadraticCurveTo(x, y + h, x, y + h - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
ctx.fillStyle = background;
ctx.fill();
ctx.strokeStyle = border;
ctx.lineWidth = 3;
ctx.stroke();
ctx.fillStyle = foreground;
ctx.font = "800 36px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 128, 49);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(1.12 * scale, 0.42 * scale, 1);
sprite.renderOrder = 20;
return sprite;
}
function standard(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.88,
flatShading: true,
...options
});
}
// Faceted island.
const islandSide = standard(0x294c38);
const islandTop = standard(0x456b4c);
const islandBottom = standard(0x203c2e);
const island = new THREE.Mesh(
new THREE.CylinderGeometry(8.55, 8.95, 0.78, 10),
[islandSide, islandTop, islandBottom]
);
island.position.y = -0.4;
island.scale.z = 0.78;
island.receiveShadow = true;
world.add(island);
const perimeter = new THREE.Mesh(
new THREE.TorusGeometry(8.56, 0.065, 5, 10),
standard(0x6c8d64, { emissive: 0x17271a, emissiveIntensity: 0.2 })
);
perimeter.rotation.x = Math.PI / 2;
perimeter.position.y = 0.015;
perimeter.scale.z = 0.78;
world.add(perimeter);
function groundPatch(x, z, radius, color, stretchX = 1, stretchZ = 0.7) {
const patch = new THREE.Mesh(
new THREE.CircleGeometry(radius, 7),
standard(color)
);
patch.rotation.x = -Math.PI / 2;
patch.position.set(x, 0.012, z);
patch.scale.set(stretchX, stretchZ, 1);
patch.receiveShadow = true;
world.add(patch);
return patch;
}
groundPatch(-3.7, 2.0, 2.2, 0x55784f, 1.3, 0.6);
groundPatch(1.4, 3.8, 1.8, 0x3d6547, 1.0, 0.7);
groundPatch(3.5, -2.8, 1.6, 0x5f7b50, 1.0, 0.8);
groundPatch(-3.7, -3.9, 1.8, 0x507149, 1.2, 0.6);
// A low-poly creek winding through the valley.
function makeRibbon(points, width, color) {
const positions = [];
const indices = [];
const half = width / 2;
for (let i = 0; i < points.length; i++) {
const prev = points[Math.max(0, i - 1)];
const next = points[Math.min(points.length - 1, i + 1)];
const tangent = next.clone().sub(prev).normalize();
const perpendicular = V(-tangent.z, 0, tangent.x).multiplyScalar(half);
const left = points[i].clone().add(perpendicular);
const right = points[i].clone().sub(perpendicular);
positions.push(left.x, 0.018, left.z);
positions.push(right.x, 0.018, right.z);
if (i < points.length - 1) {
const a = i * 2;
indices.push(a, a + 1, a + 2, a + 1, a + 3, a + 2);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(
geometry,
standard(color, {
side: THREE.DoubleSide,
roughness: 0.42,
metalness: 0.04
})
);
mesh.receiveShadow = true;
world.add(mesh);
return mesh;
}
makeRibbon(
[
V(-6.8, 0, -1.1),
V(-5.7, 0, -3.8),
V(-3.0, 0, -6.0),
V(-0.1, 0, -6.2),
V(2.9, 0, -4.9),
V(4.7, 0, -2.8)
],
0.62,
0x3c91a0
);
function makeTree(x, z, scale = 1) {
const tree = new THREE.Group();
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.11, 0.16, 0.7, 5),
standard(0x72503b)
);
trunk.position.y = 0.35;
trunk.castShadow = true;
const lower = new THREE.Mesh(
new THREE.ConeGeometry(0.52, 1.15, 5),
standard(0x315d3d)
);
lower.position.y = 0.97;
lower.castShadow = true;
const upper = new THREE.Mesh(
new THREE.ConeGeometry(0.36, 0.86, 5),
standard(0x47764b)
);
upper.position.y = 1.43;
upper.castShadow = true;
tree.add(trunk, lower, upper);
tree.position.set(x, 0, z);
tree.scale.setScalar(scale);
tree.rotation.y = Math.random() * Math.PI;
world.add(tree);
}
[
[-6.2, 3.2, 0.85],
[-3.6, 3.4, 0.72],
[-0.2, 4.0, 0.9],
[2.8, 4.7, 0.78],
[6.4, 3.1, 0.85],
[-6.8, -0.4, 0.7],
[-3.2, -4.9, 0.66],
[0.4, -4.0, 0.72],
[3.1, -1.8, 0.62],
[6.7, -3.5, 0.78],
[1.7, 6.0, 0.65]
].forEach(([x, z, s]) => makeTree(x, z, s));
function makeMountain(x, z, radius, height, color) {
const mountain = new THREE.Mesh(
new THREE.ConeGeometry(radius, height, 5),
standard(color)
);
mountain.position.set(x, height / 2, z);
mountain.rotation.y = Math.random() * Math.PI;
mountain.castShadow = true;
mountain.receiveShadow = true;
world.add(mountain);
}
makeMountain(3.9, 4.4, 1.35, 2.15, 0x71836b);
makeMountain(-5.0, -2.3, 1.15, 1.8, 0x657a61);
makeMountain(6.5, 2.8, 0.95, 1.55, 0x75866e);
for (const [x, z, s] of [
[-5.8, 1.7, 0.4],
[1.0, 3.0, 0.3],
[4.5, -4.0, 0.34],
[-1.6, -3.4, 0.25]
]) {
const rock = new THREE.Mesh(
new THREE.DodecahedronGeometry(s, 0),
standard(0x849184)
);
rock.position.set(x, s * 0.72, z);
rock.rotation.set(0.3, Math.random(), 0.15);
rock.castShadow = true;
world.add(rock);
}
// Build the weighted links, their roads, and their cost chips.
for (const route of routes) {
route.lineMaterials = [];
route.segments.forEach((segment, index) => {
const midpoint = segment.a.clone().add(segment.b).multiplyScalar(0.5);
const dx = segment.b.x - segment.a.x;
const dz = segment.b.z - segment.a.z;
const direction = V(dx, 0, dz).normalize();
const road = new THREE.Mesh(
new THREE.BoxGeometry(0.72, 0.08, segment.length),
standard(route.id === "ridge" ? 0x594936 : 0x405149)
);
road.position.set(midpoint.x, 0.045, midpoint.z);
road.rotation.y = Math.atan2(dx, dz);
road.receiveShadow = true;
world.add(road);
const lineMaterial = standard(route.color, {
emissive: route.color,
emissiveIntensity: 0.38,
roughness: 0.55
});
route.lineMaterials.push(lineMaterial);
const line = new THREE.Mesh(
new THREE.CylinderGeometry(
0.075,
0.075,
segment.length,
6,
1,
false
),
lineMaterial
);
line.position.set(midpoint.x, 0.135, midpoint.z);
line.quaternion.setFromUnitVectors(UP, direction);
world.add(line);
const perpendicular = V(-direction.z, 0, direction.x);
const side = index % 2 === 0 ? 0.46 : -0.46;
const labelPosition = midpoint.clone().addScaledVector(perpendicular, side);
labelPosition.y = 0.48;
const chip = makeLabel(String(segment.cost), {
background: route.id === "ridge" ? "#ffe0ae" : "#c9fff0",
foreground: route.id === "ridge" ? "#5a3512" : "#123d32",
border: route.id === "ridge" ? "#f4b765" : "#65d9bd",
scale: 0.64
});
chip.position.copy(labelPosition);
world.add(chip);
});
// Small node caps.
route.nodes.slice(1, -1).forEach((node) => {
const cap = new THREE.Mesh(
new THREE.CylinderGeometry(0.2, 0.23, 0.16, 7),
standard(route.color, {
emissive: route.color,
emissiveIntensity: 0.18
})
);
cap.position.set(node.x, 0.11, node.z);
cap.castShadow = true;
world.add(cap);
});
}
const start = routes[0].nodes[0];
const destination = routes[0].nodes.at(-1);
const startBase = new THREE.Mesh(
new THREE.CylinderGeometry(0.43, 0.49, 0.2, 8),
standard(0xe8efe1)
);
startBase.position.set(start.x, 0.1, start.z);
startBase.castShadow = true;
world.add(startBase);
const campLabel = makeLabel("CAMP", {
background: "rgba(245,248,237,0.95)",
foreground: "#20352a",
border: "#b9c9b9",
scale: 0.85
});
campLabel.position.set(start.x - 0.1, 1.05, start.z + 0.82);
world.add(campLabel);
const tent = new THREE.Mesh(
new THREE.ConeGeometry(0.56, 0.88, 4),
standard(0xd67c4f)
);
tent.position.set(start.x - 0.58, 0.46, start.z + 0.48);
tent.rotation.y = Math.PI / 4;
tent.castShadow = true;
world.add(tent);
// A little dock at the shared destination.
for (let i = 0; i < 4; i++) {
const plank = new THREE.Mesh(
new THREE.BoxGeometry(0.46, 0.13, 1.35),
standard(i % 2 ? 0x9b7048 : 0x765138)
);
plank.position.set(destination.x + 0.48 + i * 0.44, 0.16, destination.z);
plank.castShadow = true;
plank.receiveShadow = true;
world.add(plank);
}
const goalBase = new THREE.Mesh(
new THREE.CylinderGeometry(0.42, 0.49, 0.2, 8),
standard(0xf0f5e8)
);
goalBase.position.set(destination.x, 0.22, destination.z);
goalBase.castShadow = true;
world.add(goalBase);
const goalCrystal = new THREE.Mesh(
new THREE.OctahedronGeometry(0.38),
standard(0xf3f6dc, {
emissive: 0xa8d3a7,
emissiveIntensity: 0.32
})
);
goalCrystal.position.set(destination.x, 1.0, destination.z);
goalCrystal.castShadow = true;
world.add(goalCrystal);
const dockLabel = makeLabel("DOCK", {
background: "rgba(245,248,237,0.95)",
foreground: "#20352a",
border: "#b9c9b9",
scale: 0.85
});
dockLabel.position.set(destination.x + 0.15, 1.65, destination.z + 0.76);
world.add(dockLabel);
function makeVehicle(route) {
const vehicle = new THREE.Group();
const body = new THREE.Mesh(
new THREE.BoxGeometry(0.56, 0.3, 0.82),
standard(route.color, {
roughness: 0.58,
emissive: route.color,
emissiveIntensity: 0.12
})
);
body.position.set(0, 0.29, -0.08);
body.castShadow = true;
const cabin = new THREE.Mesh(
new THREE.BoxGeometry(0.46, 0.29, 0.38),
standard(route.id === "ridge" ? 0xffd49a : 0xb5ffe8)
);
cabin.position.set(0, 0.57, 0.18);
cabin.castShadow = true;
const windscreen = new THREE.Mesh(
new THREE.BoxGeometry(0.39, 0.16, 0.025),
standard(0x183a35, { roughness: 0.25 })
);
windscreen.position.set(0, 0.59, 0.381);
const wheelGeometry = new THREE.CylinderGeometry(0.13, 0.13, 0.12, 6);
const wheelMaterial = standard(0x18231f);
for (const x of [-0.31, 0.31]) {
for (const z of [-0.26, 0.26]) {
const wheel = new THREE.Mesh(wheelGeometry, wheelMaterial);
wheel.rotation.z = Math.PI / 2;
wheel.position.set(x, 0.14, z);
wheel.castShadow = true;
vehicle.add(wheel);
}
}
const roofMarker = new THREE.Mesh(
new THREE.CylinderGeometry(0.07, 0.09, 0.12, 6),
standard(0xfff2c6, {
emissive: 0xffe49a,
emissiveIntensity: 0.5
})
);
roofMarker.position.set(0, 0.79, 0.17);
vehicle.add(body, cabin, windscreen, roofMarker);
vehicle.position.set(start.x, 0.19, start.z);
world.add(vehicle);
return vehicle;
}
for (const route of routes) {
route.vehicle = makeVehicle(route);
const ringMaterial = new THREE.MeshBasicMaterial({
color: route.color,
transparent: true,
opacity: 0,
side: THREE.DoubleSide,
depthWrite: false
});
route.arrivalRing = new THREE.Mesh(
new THREE.RingGeometry(0.48, 0.59, 24),
ringMaterial
);
route.arrivalRing.rotation.x = -Math.PI / 2;
route.arrivalRing.position.set(destination.x, 0.24, destination.z);
world.add(route.arrivalRing);
}
function sampleRoute(route, distance) {
let remaining = Math.min(distance, route.totalLength);
let cost = 0;
for (let i = 0; i < route.segments.length; i++) {
const segment = route.segments[i];
if (remaining <= segment.length || i === route.segments.length - 1) {
const local = Math.min(1, remaining / segment.length);
const position = segment.a.clone().lerp(segment.b, local);
const tangent = segment.b.clone().sub(segment.a).normalize();
return {
position,
tangent,
cost: cost + segment.cost * local
};
}
cost += segment.cost;
remaining -= segment.length;
}
return {
position: route.nodes.at(-1).clone(),
tangent: V(1, 0, 0),
cost: route.totalCost
};
}
// Custom camera controls: orbit, pan, zoom, and animated focus views.
const cameraState = {
target: V(-0.3, 0, -0.2),
yaw: 0.53,
pitch: 0.79,
distance: 21.5
};
const cameraGoal = {
target: cameraState.target.clone(),
yaw: cameraState.yaw,
pitch: cameraState.pitch,
distance: cameraState.distance
};
const views = {
overview: {
target: V(-0.3, 0, -0.2),
yaw: 0.53,
pitch: 0.79,
distance: 21.5
},
ridge: {
target: V(-0.4, 0, 2.9),
yaw: 0.25,
pitch: 0.84,
distance: 15.2
},
valley: {
target: V(-0.8, 0, -2.8),
yaw: -0.42,
pitch: 0.86,
distance: 16.3
}
};
function setCameraView(name) {
const view = views[name];
if (!view) return;
cameraGoal.target.copy(view.target);
cameraGoal.yaw = view.yaw;
cameraGoal.pitch = view.pitch;
cameraGoal.distance = view.distance;
document.querySelectorAll("[data-view]").forEach((button) => {
button.classList.toggle("active", button.dataset.view === name);
});
document.querySelectorAll("[data-focus]").forEach((button) => {
button.classList.toggle("active", button.dataset.focus === name);
});
}
document.querySelectorAll("[data-view]").forEach((button) => {
button.addEventListener("click", () => setCameraView(button.dataset.view));
});
document.querySelectorAll("[data-focus]").forEach((button) => {
button.addEventListener("click", () => setCameraView(button.dataset.focus));
});
let drag = null;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0 && event.button !== 2) return;
canvas.setPointerCapture(event.pointerId);
drag = {
id: event.pointerId,
x: event.clientX,
y: event.clientY,
button: event.button
};
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!drag || drag.id !== event.pointerId) return;
const dx = event.clientX - drag.x;
const dy = event.clientY - drag.y;
drag.x = event.clientX;
drag.y = event.clientY;
if (event.shiftKey || drag.button === 2) {
const right = V(Math.cos(cameraGoal.yaw), 0, -Math.sin(cameraGoal.yaw));
const forward = V(-Math.sin(cameraGoal.yaw), 0, -Math.cos(cameraGoal.yaw));
const panScale = cameraGoal.distance * 0.00135;
cameraGoal.target
.addScaledVector(right, -dx * panScale)
.addScaledVector(forward, -dy * panScale);
cameraGoal.target.x = THREE.MathUtils.clamp(cameraGoal.target.x, -5, 5);
cameraGoal.target.z = THREE.MathUtils.clamp(cameraGoal.target.z, -5, 5);
} else {
cameraGoal.yaw -= dx * 0.005;
cameraGoal.pitch = THREE.MathUtils.clamp(
cameraGoal.pitch - dy * 0.004,
0.34,
1.24
);
}
document.querySelectorAll("[data-view]").forEach((button) => {
button.classList.remove("active");
});
document.querySelectorAll("[data-focus]").forEach((button) => {
button.classList.remove("active");
});
});
function endDrag(event) {
if (drag && drag.id === event.pointerId) {
drag = null;
canvas.classList.remove("dragging");
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraGoal.distance = THREE.MathUtils.clamp(
cameraGoal.distance * Math.exp(event.deltaY * 0.001),
9,
30
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => setCameraView("overview"));
const ui = {
ridge: {
cost: document.querySelector("#ridge-cost"),
bar: document.querySelector("#ridge-bar"),
status: document.querySelector("#ridge-status")
},
valley: {
cost: document.querySelector("#valley-cost"),
bar: document.querySelector("#valley-bar"),
status: document.querySelector("#valley-status")
}
};
const state = {
elapsed: 0,
playing: true,
travelSpeed: 2.35
};
const verdictText = document.querySelector("#verdict-text");
const ridge = routes[0];
const valley = routes[1];
const distanceDifference = Math.round(
(1 - ridge.totalLength / valley.totalLength) * 100
);
const savings = ridge.totalCost - valley.totalCost;
verdictText.textContent =
`Ridge is ${distanceDifference}% shorter on the ground, ` +
`but its four links cost ${savings} more in total.`;
function updateRouteUI(route, sample, distance) {
const elements = ui[route.id];
const counted = Math.min(route.totalCost, Math.floor(sample.cost + 0.00001));
const progress = distance / route.totalLength;
elements.cost.textContent = counted;
elements.bar.style.width = `${(progress * 100).toFixed(2)}%`;
if (distance >= route.totalLength - 0.001) {
elements.status.textContent = `Arrived at Dock · ${route.totalCost} total cost`;
} else if (distance <= 0.001) {
elements.status.textContent = "At Camp · ready";
} else {
elements.status.textContent = `Travelling · ${counted} cost counted`;
}
}
function resetJourney() {
state.elapsed = 0;
state.playing = true;
for (const route of routes) {
route.arrivalTime = null;
route.vehicle.position.set(start.x, 0.19, start.z);
route.vehicle.rotation.y = 0;
route.arrivalRing.material.opacity = 0;
route.arrivalRing.scale.setScalar(1);
const sample = sampleRoute(route, 0);
updateRouteUI(route, sample, 0);
}
verdictText.textContent =
`Both travellers leave Camp together. Ridge is ` +
`${distanceDifference}% shorter, but Valley still saves ${savings} cost.`;
}
document.querySelector("#replay").addEventListener("click", resetJourney);
function updateCamera(dt) {
const ease = 1 - Math.exp(-dt * 7);
cameraState.target.lerp(cameraGoal.target, ease);
cameraState.yaw += (cameraGoal.yaw - cameraState.yaw) * ease;
cameraState.pitch += (cameraGoal.pitch - cameraState.pitch) * ease;
cameraState.distance +=
(cameraGoal.distance - cameraState.distance) * ease;
const horizontal =
cameraState.distance * Math.sin(cameraState.pitch);
camera.position.set(
cameraState.target.x + horizontal * Math.sin(cameraState.yaw),
cameraState.target.y + cameraState.distance * Math.cos(cameraState.pitch),
cameraState.target.z + horizontal * Math.cos(cameraState.yaw)
);
camera.lookAt(cameraState.target);
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
let firstFrame = true;
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
if (state.playing) state.elapsed += dt;
updateCamera(dt);
for (let i = 0; i < routes.length; i++) {
const route = routes[i];
const distance = Math.min(
route.totalLength,
state.elapsed * state.travelSpeed
);
const sample = sampleRoute(route, distance);
const bounce = Math.sin(state.elapsed * 9 + i * 1.7) * 0.014;
route.vehicle.position.set(
sample.position.x,
0.19 + bounce,
sample.position.z
);
route.vehicle.rotation.y = Math.atan2(
sample.tangent.x,
sample.tangent.z
);
updateRouteUI(route, sample, distance);
if (distance >= route.totalLength - 0.001) {
if (route.arrivalTime === null) {
route.arrivalTime = state.elapsed;
}
const age = state.elapsed - route.arrivalTime;
route.arrivalRing.material.opacity =
Math.max(0, 0.9 - age * 0.65);
route.arrivalRing.scale.setScalar(1 + age * 1.4);
}
}
if (
ridge.arrivalTime !== null &&
valley.arrivalTime !== null
) {
verdictText.textContent =
`Valley is ${Math.round(
(valley.totalLength / ridge.totalLength - 1) * 100
)}% farther, yet it saves ${savings} cost. Shortest is not cheapest.`;
}
goalCrystal.rotation.y += dt * 0.85;
goalCrystal.position.y = 1.0 + Math.sin(state.elapsed * 2.2) * 0.055;
renderer.render(scene, camera);
if (firstFrame) {
firstFrame = false;
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 350);
}
}
resetJourney();
animate();
</script>
</body>
</html>
```