Prompt specification
Build or customize the Three.js 3D scene "Direct route versus detour" 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: Direct route versus detour
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/direct-route-versus-detour-x0506
Scene Description Prompt:
Create a standalone low-poly scene in one HTML file using Three.js from cdnjs. Move a shopper between two stalls before and after adding an obstruction; show the longer alternative route and preserve the same start and destination.
Working Three.js Starter (direct-route-versus-detour-x0506):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Market Route Lab</title>
<style>
:root {
--ink: #18312d;
--muted: #667772;
--paper: rgba(255, 255, 255, 0.88);
--border: rgba(24, 49, 45, 0.12);
--aqua: #168f87;
--orange: #e98a3d;
--red: #d9534f;
--purple: #7657a8;
--shadow: 0 18px 50px rgba(31, 48, 42, 0.16);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dfe7e1;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button { font: inherit; }
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active { cursor: grabbing; }
.ui {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
position: absolute;
top: 22px;
left: 22px;
width: min(370px, calc(100vw - 44px));
padding: 20px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 22px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
pointer-events: auto;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
color: var(--aqua);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 22px;
height: 2px;
border-radius: 2px;
background: currentColor;
}
h1 {
margin: 0;
font-size: clamp(26px, 4vw, 36px);
line-height: 1;
letter-spacing: -0.045em;
}
.intro {
margin: 10px 0 17px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.mode-switch {
display: grid;
grid-template-columns: 0.8fr 1.2fr;
gap: 5px;
padding: 5px;
border: 1px solid var(--border);
border-radius: 14px;
background: rgba(31, 55, 49, 0.06);
}
.mode-switch button {
min-height: 43px;
padding: 9px 10px;
border: 0;
border-radius: 10px;
color: var(--muted);
background: transparent;
cursor: pointer;
font-size: 12px;
font-weight: 750;
transition: 180ms ease;
}
.mode-switch button span {
margin-right: 5px;
opacity: 0.55;
font-size: 10px;
}
.mode-switch button:hover {
color: var(--ink);
background: rgba(255, 255, 255, 0.55);
}
.mode-switch button.active {
color: white;
background: var(--ink);
box-shadow: 0 6px 15px rgba(24, 49, 45, 0.2);
}
.mode-switch button[data-mode="after"].active {
background: var(--orange);
}
.status {
display: flex;
align-items: center;
gap: 9px;
margin: 14px 0;
color: var(--muted);
font-size: 11px;
font-weight: 700;
}
.status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--aqua);
box-shadow: 0 0 0 5px rgba(22, 143, 135, 0.11);
transition: 250ms ease;
}
.status.blocked .status-dot {
background: var(--orange);
box-shadow: 0 0 0 5px rgba(233, 138, 61, 0.14);
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-bottom: 13px;
}
.metric {
min-width: 0;
padding: 11px 10px;
border: 1px solid var(--border);
border-radius: 12px;
background: rgba(255, 255, 255, 0.54);
}
.metric span {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.metric strong {
display: block;
font-size: 18px;
letter-spacing: -0.035em;
white-space: nowrap;
}
#currentMetric { color: var(--aqua); }
.detour #currentMetric { color: var(--orange); }
#deltaMetric { color: var(--orange); }
.actions {
display: flex;
gap: 7px;
}
.action {
min-height: 40px;
padding: 9px 12px;
border: 1px solid var(--border);
border-radius: 11px;
color: var(--ink);
background: white;
cursor: pointer;
font-size: 11px;
font-weight: 800;
transition: 160ms ease;
}
.action:hover {
transform: translateY(-1px);
border-color: rgba(24, 49, 45, 0.24);
box-shadow: 0 7px 16px rgba(31, 48, 42, 0.09);
}
#playButton {
flex: 1;
color: white;
border-color: var(--ink);
background: var(--ink);
}
.mission {
position: absolute;
top: 22px;
right: 22px;
width: 225px;
padding: 14px 16px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 17px;
background: rgba(255, 255, 255, 0.75);
box-shadow: 0 12px 30px rgba(31, 48, 42, 0.11);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.mission-title {
margin-bottom: 12px;
color: var(--muted);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.endpoint {
display: flex;
align-items: center;
gap: 10px;
margin: 8px 0;
font-size: 12px;
font-weight: 800;
}
.endpoint i {
width: 10px;
height: 10px;
border-radius: 50%;
background: var(--aqua);
box-shadow: 0 0 0 5px rgba(22, 143, 135, 0.11);
}
.endpoint.goal i {
background: var(--purple);
box-shadow: 0 0 0 5px rgba(118, 87, 168, 0.11);
}
.endpoint small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
font-weight: 600;
}
.locked {
margin-top: 11px;
padding-top: 10px;
border-top: 1px solid var(--border);
color: var(--muted);
font-size: 9px;
font-weight: 700;
}
.legend {
position: absolute;
left: 22px;
bottom: 22px;
display: flex;
align-items: center;
gap: 15px;
padding: 11px 14px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 14px;
background: rgba(255, 255, 255, 0.78);
box-shadow: 0 10px 30px rgba(31, 48, 42, 0.1);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
font-size: 10px;
font-weight: 750;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
}
.swatch {
width: 18px;
height: 4px;
border-radius: 4px;
background: var(--aqua);
}
.swatch.blocked {
height: 0;
border-top: 3px dashed var(--red);
border-radius: 0;
background: none;
}
.hint {
position: absolute;
right: 22px;
bottom: 22px;
color: rgba(24, 49, 45, 0.66);
font-size: 10px;
font-weight: 750;
text-align: right;
line-height: 1.6;
}
.world-label {
position: absolute;
z-index: 4;
padding: 6px 9px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 999px;
color: white;
box-shadow: 0 6px 15px rgba(31, 48, 42, 0.15);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
transform: translate(-50%, -50%);
transition: opacity 120ms linear;
white-space: nowrap;
}
#startLabel { background: var(--aqua); }
#goalLabel { background: var(--purple); }
#loading {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
background: #e5ebe5;
transition: opacity 400ms ease, visibility 400ms ease;
}
#loading.hidden {
opacity: 0;
visibility: hidden;
}
.loader {
display: flex;
align-items: center;
gap: 12px;
color: var(--ink);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.loader::before {
content: "";
width: 25px;
height: 25px;
border: 3px solid rgba(24, 49, 45, 0.15);
border-top-color: var(--aqua);
border-radius: 50%;
animation: spin 700ms linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (max-width: 720px) {
.panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 16px;
border-radius: 18px;
}
.intro { margin-bottom: 13px; }
.mission { display: none; }
.legend { left: 12px; bottom: 12px; gap: 10px; }
.hint { right: 12px; bottom: 14px; }
.hint .desktop-hint { display: none; }
h1 { font-size: 28px; }
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="ui">
<section class="panel" id="panel">
<div class="eyebrow">Route study / 01</div>
<h1>Market detour</h1>
<p class="intro">
One shopper, two stalls, and a shortcut that disappears when the
obstruction is added.
</p>
<div class="mode-switch" role="group" aria-label="Obstruction mode">
<button id="beforeButton" class="active" data-mode="before">
<span>01</span>Before
</button>
<button id="afterButton" data-mode="after">
<span>02</span>Add obstruction
</button>
</div>
<div class="status" id="statusRow" aria-live="polite">
<span class="status-dot"></span>
<span id="statusText">Central aisle open · direct route</span>
</div>
<div class="metrics">
<div class="metric">
<span>Direct</span>
<strong id="directMetric">15.1 m</strong>
</div>
<div class="metric">
<span>Current</span>
<strong id="currentMetric">15.1 m</strong>
</div>
<div class="metric">
<span>Extra</span>
<strong id="deltaMetric">—</strong>
</div>
</div>
<div class="actions">
<button class="action" id="playButton">Pause walk</button>
<button class="action" id="restartButton">Restart</button>
<button class="action" id="viewButton">Reset view</button>
</div>
</section>
<aside class="mission">
<div class="mission-title">Locked journey</div>
<div class="endpoint">
<i></i>
<div>Farm Fresh<small>Starting stall</small></div>
</div>
<div class="endpoint goal">
<i></i>
<div>Corner Bakery<small>Destination stall</small></div>
</div>
<div class="locked">Start and destination never move.</div>
</aside>
<div class="legend">
<div class="legend-item">
<span class="swatch" id="routeSwatch"></span>
<span id="routeLegendText">Active route</span>
</div>
<div class="legend-item" id="blockedLegend" style="display:none">
<span class="swatch blocked"></span>
<span>Blocked shortcut</span>
</div>
</div>
<div class="hint">
Drag to orbit · Scroll to zoom
<br /><span class="desktop-hint">Shift-drag to pan</span>
</div>
<div class="world-label" id="startLabel">Start</div>
<div class="world-label" id="goalLabel">Destination</div>
</div>
<div id="loading">
<div class="loader">Building the market</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdfe7e1);
scene.fog = new THREE.Fog(0xdfe7e1, 38, 68);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
120
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
document.querySelector("#scene").appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xf9fff9, 0x70806e, 2.2);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff2d7, 3.1);
sun.position.set(-9, 18, 11);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -18;
sun.shadow.camera.right = 18;
sun.shadow.camera.top = 16;
sun.shadow.camera.bottom = -16;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.bias = -0.0003;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb9d7e6, 0.7);
fill.position.set(12, 8, -10);
scene.add(fill);
// -------------------------------------------------------------------------
// Materials and geometry helpers
// -------------------------------------------------------------------------
function standard(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
flatShading: true,
...options
});
}
function addBox(parent, width, height, depth, material, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(
parent,
radiusTop,
radiusBottom,
height,
segments,
material,
x = 0,
y = 0,
z = 0
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusTop, radiusBottom, height, segments),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function roundedRect(ctx, x, y, width, height, radius) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx.lineTo(x + width, y + height - radius);
ctx.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
ctx.lineTo(x + radius, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
}
function makeSignTexture(title, subtitle, background, foreground = "#ffffff") {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 256;
const ctx = canvas.getContext("2d");
ctx.fillStyle = background;
roundedRect(ctx, 8, 8, 1008, 240, 32);
ctx.fill();
ctx.strokeStyle = "rgba(255,255,255,0.3)";
ctx.lineWidth = 6;
roundedRect(ctx, 18, 18, 988, 220, 25);
ctx.stroke();
ctx.textAlign = "center";
ctx.fillStyle = foreground;
ctx.font = "800 76px system-ui, sans-serif";
ctx.fillText(title, 512, 120);
ctx.globalAlpha = 0.78;
ctx.font = "600 30px system-ui, sans-serif";
ctx.fillText(subtitle, 512, 176);
ctx.globalAlpha = 1;
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(4, renderer.capabilities.getMaxAnisotropy());
return texture;
}
// -------------------------------------------------------------------------
// Market plaza
// -------------------------------------------------------------------------
const floorMaterial = standard(0xd9d5c9);
addBox(scene, 24, 0.18, 18, floorMaterial, 0, -0.12, 0);
const curbMaterial = standard(0xb8b3a8);
addBox(scene, 24.3, 0.22, 0.3, curbMaterial, 0, -0.03, 9.05);
addBox(scene, 24.3, 0.22, 0.3, curbMaterial, 0, -0.03, -9.05);
addBox(scene, 0.3, 0.22, 18, curbMaterial, 12.05, -0.03, 0);
addBox(scene, 0.3, 0.22, 18, curbMaterial, -12.05, -0.03, 0);
const tileVertices = [];
for (let x = -11; x <= 11; x += 1) {
tileVertices.push(x, -0.018, -8.5, x, -0.018, 8.5);
}
for (let z = -8; z <= 8; z += 1) {
tileVertices.push(-11.5, -0.017, z, 11.5, -0.017, z);
}
const tileGeometry = new THREE.BufferGeometry();
tileGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(tileVertices, 3)
);
const tileLines = new THREE.LineSegments(
tileGeometry,
new THREE.LineBasicMaterial({
color: 0x777c72,
transparent: true,
opacity: 0.16
})
);
scene.add(tileLines);
function createTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.scale.setScalar(scale);
const planter = addCylinder(
group, 0.68, 0.76, 0.38, 8, standard(0xa99d88), 0, 0.18, 0
);
planter.castShadow = true;
const trunk = addCylinder(
group, 0.15, 0.22, 1.35, 7, standard(0x76523d), 0, 0.93, 0
);
trunk.castShadow = true;
const leafMaterial = standard(0x547e61);
const crown = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.95, 0),
leafMaterial
);
crown.position.set(0, 1.9, 0);
crown.scale.set(0.9, 1.12, 0.9);
crown.rotation.set(0.2, 0.4, 0.1);
crown.castShadow = true;
group.add(crown);
const crown2 = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.65, 0),
standard(0x72966c)
);
crown2.position.set(0.45, 1.65, 0.12);
crown2.castShadow = true;
group.add(crown2);
scene.add(group);
}
createTree(-10.25, 6.65, 0.86);
createTree(10.25, 6.65, 0.92);
createTree(-10.25, -6.7, 0.78);
createTree(10.25, -6.7, 0.84);
// -------------------------------------------------------------------------
// The two stalls
// -------------------------------------------------------------------------
function createStall({
x,
z,
rotation,
title,
subtitle,
accent,
type
}) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
const wood = standard(0x9d6542);
const darkWood = standard(0x654633);
const cream = standard(0xf0e5cd);
const accentMaterial = standard(accent);
addBox(group, 3.5, 0.75, 1.45, wood, 0, 0.42, 0.35);
addBox(group, 3.75, 0.13, 1.7, darkWood, 0, 0.86, 0.35);
addBox(group, 3.45, 1.35, 0.12, cream, 0, 0.75, -0.72);
const postMaterial = standard(0x665246);
for (const px of [-1.63, 1.63]) {
for (const pz of [-0.7, 0.95]) {
addBox(group, 0.11, 2.45, 0.11, postMaterial, px, 1.23, pz);
}
}
const stripeWidth = 0.47;
for (let i = 0; i < 8; i++) {
const stripe = addBox(
group,
stripeWidth,
0.19,
2.28,
i % 2 === 0 ? accentMaterial : cream,
-1.645 + stripeWidth * 0.5 + i * stripeWidth,
2.53,
0
);
stripe.rotation.z = i % 2 === 0 ? 0.012 : -0.012;
}
addBox(group, 3.65, 0.28, 0.12, accentMaterial, 0, 2.28, 1.06);
for (let i = 0; i < 8; i++) {
addBox(
group,
0.38,
0.25,
0.1,
i % 2 === 0 ? accentMaterial : cream,
-1.59 + i * 0.455,
2.1,
1.05
);
}
const signTexture = makeSignTexture(title, subtitle, accent);
const sign = new THREE.Mesh(
new THREE.PlaneGeometry(2.8, 0.7),
new THREE.MeshBasicMaterial({
map: signTexture,
side: THREE.DoubleSide,
toneMapped: false
})
);
sign.position.set(0, 1.74, 0.94);
group.add(sign);
if (type === "produce") {
const crateMaterial = standard(0xc48a55);
for (const cx of [-1.05, 0, 1.05]) {
addBox(group, 0.68, 0.24, 0.52, crateMaterial, cx, 1.04, 0.38);
for (let j = 0; j < 3; j++) {
const fruit = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.14, 0),
standard(j % 2 ? 0xe48a3d : 0x78934f)
);
fruit.position.set(cx - 0.18 + j * 0.18, 1.25, 0.4);
fruit.castShadow = true;
group.add(fruit);
}
}
} else {
const breadMaterial = standard(0xd59a55);
for (const bx of [-1.05, -0.35, 0.35, 1.05]) {
const loaf = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.16, 0.78, 8),
breadMaterial
);
loaf.rotation.x = Math.PI / 2;
loaf.position.set(bx, 1.08, 0.42);
loaf.castShadow = true;
group.add(loaf);
}
}
scene.add(group);
return group;
}
createStall({
x: -8.35,
z: 5.45,
rotation: Math.PI,
title: "FARM FRESH",
subtitle: "FRUIT · GREENS · DAILY PICKS",
accent: 0x2f8f77,
type: "produce"
});
createStall({
x: 8.35,
z: -5.45,
rotation: 0,
title: "CORNER BAKERY",
subtitle: "BREAD · PASTRY · FRESH DAILY",
accent: 0xc85f4c,
type: "bread"
});
// -------------------------------------------------------------------------
// Fixed start and destination
// -------------------------------------------------------------------------
const START = new THREE.Vector2(-6.2, 4.3);
const DESTINATION = new THREE.Vector2(6.2, -4.3);
function createEndpoint(position, color) {
const group = new THREE.Group();
group.position.set(position.x, 0, position.y);
const material = standard(color, {
emissive: color,
emissiveIntensity: 0.14,
roughness: 0.55
});
const base = addCylinder(
group, 0.48, 0.53, 0.075, 16, material, 0, 0.015, 0
);
base.castShadow = false;
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.51, 0.045, 6, 28),
new THREE.MeshBasicMaterial({ color })
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.075;
group.add(ring);
const beam = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.11, 1.35, 8),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.18,
depthWrite: false
})
);
beam.position.y = 0.68;
group.add(beam);
scene.add(group);
return { group, ring, material };
}
const startEndpoint = createEndpoint(START, 0x168f87);
const destinationEndpoint = createEndpoint(DESTINATION, 0x7657a8);
const destinationFlag = new THREE.Group();
destinationFlag.position.set(DESTINATION.x, 0, DESTINATION.y);
const flagPole = addCylinder(
destinationFlag,
0.025,
0.025,
1.7,
6,
standard(0x5a5961),
0.28,
0.85,
0.05
);
flagPole.castShadow = false;
const flag = addBox(
destinationFlag,
0.62,
0.38,
0.045,
standard(0x7657a8),
0.57,
1.47,
0.05
);
flag.castShadow = false;
scene.add(destinationFlag);
// -------------------------------------------------------------------------
// Obstruction: a temporary work zone and closed shortcut
// -------------------------------------------------------------------------
const obstruction = new THREE.Group();
obstruction.position.set(-0.3, 0, 0);
obstruction.scale.y = 0.001;
const workZoneMaterial = standard(0x686b67);
const orangeMaterial = standard(0xe76f51);
const whiteMaterial = standard(0xf3ead7);
const blueCrateMaterial = standard(0x477b78);
const woodCrateMaterial = standard(0xb67b47);
addBox(
obstruction,
7.8,
0.08,
2.05,
workZoneMaterial,
0,
0.035,
0
);
for (const railZ of [-0.92, 0.92]) {
addBox(
obstruction,
7.65,
0.18,
0.16,
orangeMaterial,
0,
1.02,
railZ
);
for (let i = 0; i < 7; i++) {
addBox(
obstruction,
0.38,
0.18,
0.17,
whiteMaterial,
-3.25 + i * 1.08,
1.02,
railZ
);
}
for (let i = 0; i <= 4; i++) {
addBox(
obstruction,
0.1,
1.02,
0.1,
orangeMaterial,
-3.65 + i * 1.825,
0.52,
railZ
);
}
}
addBox(
obstruction,
1.42,
0.85,
1.12,
blueCrateMaterial,
-2.1,
0.5,
0.12
);
addBox(
obstruction,
1.34,
0.65,
1.05,
woodCrateMaterial,
0.15,
0.4,
-0.08
);
addBox(
obstruction,
1.45,
0.55,
1.02,
blueCrateMaterial,
2.35,
0.35,
0.06
);
const coneMaterial = standard(0xe45f43);
for (const coneX of [-3.85, 3.85]) {
addCylinder(
obstruction,
0.04,
0.25,
0.55,
8,
coneMaterial,
coneX,
0.3,
0.72
);
addCylinder(
obstruction,
0.105,
0.14,
0.12,
8,
whiteMaterial,
coneX,
0.34,
0.72
);
}
const closedTexture = makeSignTexture(
"SHORTCUT CLOSED",
"PLEASE USE THE MARKED ALTERNATIVE",
"#d9554f"
);
const closedSprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: closedTexture,
transparent: true,
depthTest: false,
toneMapped: false
})
);
closedSprite.position.set(0, 2.15, 0);
closedSprite.scale.set(2.75, 0.69, 1);
closedSprite.renderOrder = 10;
obstruction.add(closedSprite);
const crossMaterial = new THREE.MeshBasicMaterial({ color: 0xc53f3f });
const crossA = addBox(
obstruction, 0.82, 0.055, 0.13, crossMaterial, -0.3, 1.28, 0
);
const crossB = addBox(
obstruction, 0.13, 0.055, 0.82, crossMaterial, -0.3, 1.29, 0
);
crossA.rotation.y = Math.PI / 4;
crossB.rotation.y = Math.PI / 4;
scene.add(obstruction);
// -------------------------------------------------------------------------
// Route geometry
// -------------------------------------------------------------------------
const routeGroup = new THREE.Group();
scene.add(routeGroup);
const blockedRouteGroup = new THREE.Group();
scene.add(blockedRouteGroup);
function clearGroup(group) {
group.traverse((object) => {
if (object.geometry) object.geometry.dispose();
if (object.material) {
const materials = Array.isArray(object.material)
? object.material
: [object.material];
materials.forEach((material) => material.dispose());
}
});
group.clear();
}
function routeLength(points) {
let length = 0;
for (let i = 0; i < points.length - 1; i++) {
length += points[i].distanceTo(points[i + 1]);
}
return length;
}
function planRoute(withObstruction) {
const points = [START.clone()];
if (withObstruction) {
// The detour goes around the left end of the work zone, follows its
// far side, and then rejoins the original destination approach.
points.push(
new THREE.Vector2(-4.9, 2.8),
new THREE.Vector2(-4.9, -1.8),
new THREE.Vector2(4.2, -1.8)
);
}
points.push(DESTINATION.clone());
return points;
}
function pointAlongRoute(points, distance) {
for (let i = 0; i < points.length - 1; i++) {
const segmentLength = points[i].distanceTo(points[i + 1]);
if (distance <= segmentLength || i === points.length - 2) {
const t = segmentLength === 0
? 0
: THREE.MathUtils.clamp(distance / segmentLength, 0, 1);
return {
position: points[i].clone().lerp(points[i + 1], t),
direction: points[i + 1].clone().sub(points[i]).normalize()
};
}
distance -= segmentLength;
}
}
function buildRoute(points, color) {
clearGroup(routeGroup);
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.96,
depthWrite: false
});
for (let i = 0; i < points.length - 1; i++) {
const a = points[i];
const b = points[i + 1];
const dx = b.x - a.x;
const dz = b.y - a.y;
const length = Math.hypot(dx, dz);
const segment = new THREE.Mesh(
new THREE.BoxGeometry(length, 0.035, 0.13),
material
);
segment.position.set((a.x + b.x) / 2, 0.008, (a.y + b.y) / 2);
segment.rotation.y = -Math.atan2(dz, dx);
segment.renderOrder = 2;
routeGroup.add(segment);
const joint = new THREE.Mesh(
new THREE.CylinderGeometry(0.067, 0.067, 0.035, 12),
material
);
joint.position.set(a.x, 0.008, a.y);
joint.renderOrder = 2;
routeGroup.add(joint);
}
const total = routeLength(points);
let arrowDistance = 1.15;
while (arrowDistance < total - 0.65) {
const sample = pointAlongRoute(points, arrowDistance);
const arrowPivot = new THREE.Group();
arrowPivot.position.set(
sample.position.x,
0.075,
sample.position.y
);
arrowPivot.rotation.y =
-Math.atan2(sample.direction.y, sample.direction.x);
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.17, 0.42, 3),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.98,
depthWrite: false
})
);
arrow.rotation.z = -Math.PI / 2;
arrow.renderOrder = 3;
arrowPivot.add(arrow);
routeGroup.add(arrowPivot);
arrowDistance += 2.35;
}
}
function buildBlockedShortcut() {
clearGroup(blockedRouteGroup);
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(START.x, 0.018, START.y),
new THREE.Vector3(DESTINATION.x, 0.018, DESTINATION.y)
]);
const line = new THREE.Line(
geometry,
new THREE.LineDashedMaterial({
color: 0xd9534f,
dashSize: 0.28,
gapSize: 0.2,
transparent: true,
opacity: 0.72,
depthWrite: false
})
);
line.computeLineDistances();
line.renderOrder = 1;
blockedRouteGroup.add(line);
}
// -------------------------------------------------------------------------
// Low-poly shopper
// -------------------------------------------------------------------------
const shopper = new THREE.Group();
const shopperShadow = new THREE.Mesh(
new THREE.CircleGeometry(0.37, 16),
new THREE.MeshBasicMaterial({
color: 0x26342e,
transparent: true,
opacity: 0.16,
depthWrite: false
})
);
shopperShadow.rotation.x = -Math.PI / 2;
shopperShadow.position.y = 0.015;
shopper.add(shopperShadow);
const skinMaterial = standard(0xc98d65);
const jacketMaterial = standard(0xe6a642);
const trouserMaterial = standard(0x405b65);
const shoeMaterial = standard(0x263b3e);
const hairMaterial = standard(0x3e3028);
const leftLeg = addBox(
shopper, 0.14, 0.43, 0.15, trouserMaterial, -0.12, 0.25, 0
);
const rightLeg = addBox(
shopper, 0.14, 0.43, 0.15, trouserMaterial, 0.12, 0.25, 0
);
addBox(
shopper, 0.19, 0.08, 0.3, shoeMaterial, -0.12, 0.055, 0.055
);
addBox(
shopper, 0.19, 0.08, 0.3, shoeMaterial, 0.12, 0.055, 0.055
);
const torso = new THREE.Mesh(
new THREE.CylinderGeometry(0.24, 0.31, 0.58, 7),
jacketMaterial
);
torso.position.y = 0.74;
torso.castShadow = true;
shopper.add(torso);
const head = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.22, 1),
skinMaterial
);
head.position.y = 1.18;
head.castShadow = true;
shopper.add(head);
const hair = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.225, 0),
hairMaterial
);
hair.position.set(0, 1.25, -0.025);
hair.scale.set(1, 0.65, 1);
hair.castShadow = true;
shopper.add(hair);
const leftArm = addBox(
shopper, 0.12, 0.46, 0.13, jacketMaterial, -0.34, 0.72, 0
);
const rightArm = addBox(
shopper, 0.12, 0.46, 0.13, jacketMaterial, 0.34, 0.72, 0
);
const basket = new THREE.Group();
basket.position.set(0.43, 0.48, 0.08);
addBox(basket, 0.3, 0.28, 0.24, woodCrateMaterial);
addBox(basket, 0.34, 0.045, 0.28, standard(0xc79258), 0, 0.15, 0);
const handle = new THREE.Mesh(
new THREE.TorusGeometry(0.16, 0.025, 5, 12, Math.PI),
standard(0x765137)
);
handle.position.y = 0.15;
handle.rotation.z = 0;
basket.add(handle);
shopper.add(basket);
scene.add(shopper);
// -------------------------------------------------------------------------
// Custom camera controls, implemented without external addons
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 0.2, 0);
const spherical = new THREE.Spherical(29, 0.72, 0.72);
const initialSpherical = spherical.clone();
function updateCamera() {
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
}
updateCamera();
let dragging = false;
let panning = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
panning = event.button === 2 || event.button === 1 || event.shiftKey;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
if (panning) {
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
const right = new THREE.Vector3()
.crossVectors(forward, camera.up)
.normalize();
const panScale = spherical.radius * 0.0014;
cameraTarget.addScaledVector(right, -dx * panScale);
cameraTarget.addScaledVector(forward, dy * panScale);
} else {
spherical.theta -= dx * 0.006;
spherical.phi = THREE.MathUtils.clamp(
spherical.phi - dy * 0.005,
0.28,
1.38
);
}
updateCamera();
});
function endDrag(event) {
dragging = false;
panning = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("contextmenu", (event) => event.preventDefault());
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
spherical.radius = THREE.MathUtils.clamp(
spherical.radius * Math.exp(event.deltaY * 0.001),
15,
43
);
updateCamera();
},
{ passive: false }
);
// -------------------------------------------------------------------------
// UI and state
// -------------------------------------------------------------------------
const beforeButton = document.querySelector("#beforeButton");
const afterButton = document.querySelector("#afterButton");
const playButton = document.querySelector("#playButton");
const restartButton = document.querySelector("#restartButton");
const viewButton = document.querySelector("#viewButton");
const panel = document.querySelector("#panel");
const statusRow = document.querySelector("#statusRow");
const statusText = document.querySelector("#statusText");
const directMetric = document.querySelector("#directMetric");
const currentMetric = document.querySelector("#currentMetric");
const deltaMetric = document.querySelector("#deltaMetric");
const routeSwatch = document.querySelector("#routeSwatch");
const routeLegendText = document.querySelector("#routeLegendText");
const blockedLegend = document.querySelector("#blockedLegend");
const startLabel = document.querySelector("#startLabel");
const goalLabel = document.querySelector("#goalLabel");
const DIRECT_ROUTE = [START.clone(), DESTINATION.clone()];
const DETOUR_ROUTE = planRoute(true);
const directLength = routeLength(DIRECT_ROUTE);
const detourLength = routeLength(DETOUR_ROUTE);
let currentMode = "before";
let currentRoute = DIRECT_ROUTE.map((point) => point.clone());
let currentRouteLength = directLength;
let walkProgress = 0;
let playing = true;
let arrived = false;
let obstructionAmount = 0;
let obstructionTarget = 0;
function updatePlayButton() {
if (arrived) {
playButton.textContent = "Replay walk";
} else {
playButton.textContent = playing ? "Pause walk" : "Resume walk";
}
}
function resetWalk() {
walkProgress = 0;
arrived = false;
playing = true;
updatePlayButton();
}
function setMode(mode, announce = true) {
currentMode = mode;
obstructionTarget = mode === "after" ? 1 : 0;
const detour = mode === "after";
currentRoute = (detour ? DETOUR_ROUTE : DIRECT_ROUTE).map((point) =>
point.clone()
);
currentRouteLength = routeLength(currentRoute);
buildRoute(currentRoute, detour ? 0xe98a3d : 0x168f87);
buildBlockedShortcut();
blockedRouteGroup.visible = detour;
beforeButton.classList.toggle("active", !detour);
afterButton.classList.toggle("active", detour);
panel.classList.toggle("detour", detour);
statusRow.classList.toggle("blocked", detour);
statusText.textContent = detour
? "Central aisle blocked · alternative route active"
: "Central aisle open · direct route";
directMetric.textContent = `${directLength.toFixed(1)} m`;
currentMetric.textContent = `${currentRouteLength.toFixed(1)} m`;
deltaMetric.textContent = detour
? `+${(detourLength - directLength).toFixed(1)} m`
: "—";
routeSwatch.style.background = detour ? "#e98a3d" : "#168f87";
routeLegendText.textContent = detour
? "Alternative route"
: "Direct route";
blockedLegend.style.display = detour ? "flex" : "none";
resetWalk();
if (announce) {
statusRow.animate(
[
{ opacity: 0.55, transform: "translateY(-2px)" },
{ opacity: 1, transform: "translateY(0)" }
],
{ duration: 320, easing: "ease-out" }
);
}
}
beforeButton.addEventListener("click", () => setMode("before"));
afterButton.addEventListener("click", () => setMode("after"));
playButton.addEventListener("click", () => {
if (arrived) {
resetWalk();
} else {
playing = !playing;
updatePlayButton();
}
});
restartButton.addEventListener("click", resetWalk);
viewButton.addEventListener("click", () => {
spherical.copy(initialSpherical);
cameraTarget.set(0, 0.2, 0);
updateCamera();
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space" && event.target === document.body) {
event.preventDefault();
playButton.click();
}
if (event.key === "1") setMode("before");
if (event.key === "2") setMode("after");
});
setMode("before", false);
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let elapsed = 0;
function updateShopper() {
const sample = pointAlongRoute(
currentRoute,
walkProgress * currentRouteLength
);
shopper.position.set(sample.position.x, 0, sample.position.y);
shopper.rotation.y = Math.atan2(
sample.direction.x,
sample.direction.y
);
const walking = playing && !arrived;
const cycle = elapsed * 10;
const swing = walking ? Math.sin(cycle) * 0.56 : 0;
leftLeg.rotation.x = swing;
rightLeg.rotation.x = -swing;
leftArm.rotation.x = -swing * 0.7;
rightArm.rotation.x = swing * 0.7;
shopper.position.y = walking
? Math.abs(Math.sin(cycle)) * 0.035
: 0;
}
function updateLabels() {
const labels = [
{
element: startLabel,
position: new THREE.Vector3(START.x, 1.65, START.y)
},
{
element: goalLabel,
position: new THREE.Vector3(DESTINATION.x, 1.85, DESTINATION.y)
}
];
for (const label of labels) {
const projected = label.position.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
label.element.style.opacity = visible ? "1" : "0";
label.element.style.left = `${(projected.x * 0.5 + 0.5) * innerWidth}px`;
label.element.style.top = `${(-projected.y * 0.5 + 0.5) * innerHeight}px`;
}
}
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
elapsed += dt;
if (playing && !arrived) {
walkProgress += (dt * 3.15) / currentRouteLength;
if (walkProgress >= 1) {
walkProgress = 1;
arrived = true;
playing = false;
updatePlayButton();
}
}
obstructionAmount +=
(obstructionTarget - obstructionAmount) * Math.min(1, dt * 5.2);
if (obstructionTarget === 0 && obstructionAmount < 0.008) {
obstructionAmount = 0;
obstruction.visible = false;
} else {
obstruction.visible = true;
}
obstruction.scale.y = Math.max(0.001, obstructionAmount);
const pulse = 1 + Math.sin(elapsed * 2.3) * 0.055;
startEndpoint.ring.scale.setScalar(pulse);
destinationEndpoint.ring.scale.setScalar(1 + Math.sin(elapsed * 2.3 + 1.4) * 0.055);
updateShopper();
updateLabels();
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
requestAnimationFrame(() => {
document.querySelector("#loading").classList.add("hidden");
});
</script>
</body>
</html>
```