Prompt specification
Build or customize the Three.js 3D scene "Cyclist yielding near a small canal bridge" 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: Cyclist yielding near a small canal bridge
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/cyclist-yielding-near-a-small-canal-bridge-x2802
Scene Description Prompt:
Create a standalone voxel scene in one HTML file using Three.js from cdnjs. Create a compact Jordaan vignette with bridge approach, pedestrian, and cyclist. Use simple movement rules and preserve space for each route near the narrow crossing.
Working Three.js Starter (cyclist-yielding-near-a-small-canal-bridge-x2802):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Jordaan — Bridge Approach</title>
<style>
:root {
--ink: #18302f;
--muted: #617370;
--paper: rgba(250, 248, 239, 0.92);
--line: rgba(24, 48, 47, 0.13);
--ped: #e77d61;
--cycle: #278f88;
--gold: #e9b85f;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce6e1;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button, input { font: inherit; }
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--line);
background: var(--paper);
box-shadow: 0 14px 40px rgba(24, 48, 47, 0.13);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#intro {
top: 22px;
left: 22px;
width: min(360px, calc(100vw - 44px));
padding: 18px 19px 17px;
border-radius: 18px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 22px;
height: 2px;
content: "";
background: var(--ped);
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 38px);
line-height: 0.98;
letter-spacing: -0.045em;
}
#intro p {
margin: 11px 0 15px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px 14px;
padding-top: 12px;
border-top: 1px solid var(--line);
font-size: 11px;
font-weight: 700;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 7px;
}
.swatch {
width: 16px;
height: 7px;
border-radius: 2px;
background: var(--ped);
}
.swatch.cycle { background: var(--cycle); }
.swatch.gold { background: var(--gold); }
#status {
top: 22px;
right: 22px;
min-width: 228px;
padding: 13px 15px;
border-radius: 14px;
}
.status-top {
display: flex;
align-items: center;
gap: 8px;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#statusDot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--cycle);
box-shadow: 0 0 0 4px rgba(39, 143, 136, 0.13);
}
#status.yield #statusDot {
background: var(--ped);
box-shadow: 0 0 0 4px rgba(231, 125, 97, 0.15);
}
#statusText {
margin-top: 5px;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
#dock {
left: 50%;
bottom: 22px;
display: flex;
align-items: center;
gap: 8px;
padding: 9px;
border-radius: 16px;
transform: translateX(-50%);
}
.control {
min-height: 38px;
padding: 0 13px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.72);
color: var(--ink);
cursor: pointer;
font-size: 11px;
font-weight: 800;
white-space: nowrap;
transition:
transform 120ms ease,
background 120ms ease,
color 120ms ease;
}
.control:hover {
background: white;
transform: translateY(-1px);
}
.control.primary {
min-width: 92px;
border-color: var(--ink);
background: var(--ink);
color: #fffdf5;
}
.control.active {
border-color: rgba(231, 125, 97, 0.42);
background: rgba(231, 125, 97, 0.14);
color: #9d412e;
}
.speed {
display: flex;
align-items: center;
gap: 8px;
padding: 0 8px 0 10px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.05em;
text-transform: uppercase;
}
input[type="range"] {
width: 82px;
accent-color: var(--cycle);
}
#help {
right: 22px;
bottom: 24px;
padding: 9px 12px;
border-radius: 10px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
line-height: 1.4;
}
kbd {
padding: 2px 5px;
border: 1px solid var(--line);
border-radius: 4px;
background: white;
color: var(--ink);
font: inherit;
}
@media (max-width: 760px) {
#intro {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px;
}
#intro p { display: none; }
#intro .legend { margin-top: 11px; }
#status {
top: 12px;
right: 12px;
display: none;
}
#dock {
right: 12px;
bottom: 12px;
left: 12px;
justify-content: center;
overflow-x: auto;
transform: none;
}
#help { display: none; }
.speed { display: none; }
}
</style>
</head>
<body>
<main id="stage" aria-label="Interactive voxel scene of a Jordaan bridge approach"></main>
<section id="intro" class="panel">
<div class="eyebrow">Amsterdam · Jordaan</div>
<h1>Bridge<br>Approach</h1>
<p>
A compact traffic vignette with a protected pedestrian edge and a separate
cycle lane. Near the narrow bridge, the cyclist yields to the pedestrian.
</p>
<div class="legend" aria-label="Scene legend">
<span><i class="swatch"></i> Pedestrian route</span>
<span><i class="swatch cycle"></i> Cycle route</span>
<span><i class="swatch gold"></i> Narrow zone</span>
</div>
</section>
<section id="status" class="panel" aria-live="polite">
<div class="status-top">
<i id="statusDot"></i>
<span>Traffic state</span>
</div>
<div id="statusText">Routes separated · traffic moving</div>
</section>
<nav id="dock" class="panel" aria-label="Scene controls">
<button id="playButton" class="control primary" type="button">Pause</button>
<button id="priorityButton" class="control active" type="button"
aria-pressed="true">Pedestrian priority</button>
<button id="resetButton" class="control" type="button">Reset</button>
<label class="speed">
Speed
<input id="speedInput" type="range" min="0.5" max="1.6" step="0.1"
value="1" aria-label="Traffic speed" />
</label>
</nav>
<div id="help" class="panel">
Drag to orbit · scroll to zoom<br />
<kbd>←</kbd> <kbd>→</kbd> rotate · <kbd>+</kbd> <kbd>−</kbd> zoom
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.querySelector("#stage");
const statusPanel = document.querySelector("#status");
const statusText = document.querySelector("#statusText");
const playButton = document.querySelector("#playButton");
const priorityButton = document.querySelector("#priorityButton");
const resetButton = document.querySelector("#resetButton");
const speedInput = document.querySelector("#speedInput");
// ---------------------------------------------------------------------
// Renderer, camera, and lighting
// ---------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce6e1);
scene.fog = new THREE.Fog(0xdce6e1, 21, 40);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
stage.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-10, 10, 7, -7, 0.1, 80);
const cameraTarget = new THREE.Vector3(0, 0.8, 0.5);
const orbit = {
azimuth: 2.35,
polar: 0.78,
radius: 22,
zoom: 1
};
const hemisphere = new THREE.HemisphereLight(0xf4fbf7, 0x766b58, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffefd1, 3.1);
sun.position.set(-8, 14, -7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -18;
sun.shadow.camera.right = 18;
sun.shadow.camera.top = 18;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 40;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0xbfd8dd, 0.8);
rimLight.position.set(10, 7, 12);
scene.add(rimLight);
// ---------------------------------------------------------------------
// Voxel construction helpers
// ---------------------------------------------------------------------
const materialCache = new Map();
function getMaterial(color) {
if (color instanceof THREE.Material) return color;
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0
})
);
}
return materialCache.get(color);
}
function box(parent, width, height, depth, color, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
getMaterial(color)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function beam(parent, a, b, thickness, color) {
const direction = b.clone().sub(a);
const mesh = box(
parent,
thickness,
thickness,
direction.length(),
color,
(a.x + b.x) / 2,
(a.y + b.y) / 2,
(a.z + b.z) / 2
);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 0, 1),
direction.normalize()
);
return mesh;
}
function emissiveMaterial(color, intensity = 0.2) {
return new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: intensity,
roughness: 0.7
});
}
// ---------------------------------------------------------------------
// Terrain, canal, and bridge
// ---------------------------------------------------------------------
const PED_X = -1.05;
const CYCLE_X = 0.45;
const C = {
northBank: 0xb9ad92,
southBank: 0xaea68d,
paving: 0xc9c0a9,
canalWall: 0x847f70,
water: 0x4f9eaa,
deck: 0x9c947f,
pedLane: 0xd98268,
cycleLane: 0x4b9991,
rail: 0x334a47,
gold: 0xe2ad55,
brickA: 0x9f503b,
brickB: 0xb36a48,
brickC: 0xc18a61,
brickD: 0x745044,
cream: 0xd5c4a2,
roof: 0x394b49,
trunk: 0x70513b,
leaf: 0x61785b,
leafLight: 0x78906a
};
// Diorama base.
box(scene, 28, 0.45, 24, 0x8f8978, 0, -0.72, 0);
// Canal and two banks.
box(scene, 28, 0.18, 4.6, C.water, 0, -0.12, 0);
box(scene, 28, 0.8, 9.4, C.northBank, 0, -0.12, 7);
box(scene, 28, 0.8, 9.4, C.southBank, 0, -0.12, -7);
// Canal coping.
box(scene, 28, 0.52, 0.28, C.canalWall, 0, 0.03, 2.45);
box(scene, 28, 0.52, 0.28, C.canalWall, 0, 0.03, -2.45);
// Bridge deck.
box(scene, 3.65, 0.44, 5.2, C.deck, 0, 0.48, 0);
// Approaches.
box(scene, 3.8, 0.05, 7.2, C.paving, 0, 0.305, -6.9);
box(scene, 3.8, 0.05, 7.2, C.paving, 0, 0.305, 6.9);
// Voxel steps at both bridge entries.
for (const side of [-1, 1]) {
box(scene, 3.65, 0.31, 0.4, C.deck, 0, 0.445, side * 2.8);
box(scene, 3.8, 0.16, 0.4, C.deck, 0, 0.36, side * 3.2);
}
// Route surfaces.
box(scene, 1.12, 0.035, 7.15, C.pedLane, PED_X, 0.322, -6.85);
box(scene, 1.12, 0.035, 7.15, C.pedLane, PED_X, 0.322, 6.85);
box(scene, 1.18, 0.035, 5.12, C.pedLane, PED_X, 0.717, 0);
box(scene, 1.18, 0.035, 7.15, C.cycleLane, CYCLE_X, 0.322, -6.85);
box(scene, 1.18, 0.035, 7.15, C.cycleLane, CYCLE_X, 0.322, 6.85);
box(scene, 1.18, 0.035, 5.12, C.cycleLane, CYCLE_X, 0.717, 0);
// Colored strips continue over the steps.
for (const side of [-1, 1]) {
box(scene, 1.12, 0.025, 0.39, C.pedLane, PED_X, 0.615, side * 2.8);
box(scene, 1.12, 0.025, 0.39, C.pedLane, PED_X, 0.455, side * 3.2);
box(scene, 1.18, 0.025, 0.39, C.cycleLane, CYCLE_X, 0.615, side * 2.8);
box(scene, 1.18, 0.025, 0.39, C.cycleLane, CYCLE_X, 0.455, side * 3.2);
}
function routeSurfaceY(z) {
const a = Math.abs(z);
if (a <= 2.6) return 0.75;
if (a <= 3.0) return 0.63;
if (a <= 3.4) return 0.47;
return 0.34;
}
// Dashed route markers made entirely from voxels.
for (let z = -10.1; z <= 10.1; z += 0.86) {
const dashLength = 0.4;
box(
scene, 0.1, 0.025, dashLength,
0xf4b096, PED_X, routeSurfaceY(z) + 0.025, z
);
box(
scene, 0.1, 0.025, dashLength,
0x8fd0c8, CYCLE_X, routeSurfaceY(z) + 0.025, z
);
}
// Bridge rails.
for (const x of [-1.73, 1.73]) {
box(scene, 0.1, 0.12, 5.2, C.rail, x, 1.25, 0);
box(scene, 0.1, 0.09, 5.2, C.rail, x, 0.94, 0);
for (let z = -2.45; z <= 2.46; z += 0.65) {
box(scene, 0.13, 0.68, 0.13, C.rail, x, 1.0, z);
}
}
// The separator bollards keep both routes physically distinct.
for (let z = -2.35; z <= 2.36; z += 0.58) {
box(scene, 0.13, 0.47, 0.13, 0xe9e0c8, -0.34, 0.98, z);
box(scene, 0.15, 0.10, 0.15, C.gold, -0.34, 1.22, z);
}
// Narrow-zone edge markers.
for (const z of [-1.08, 1.08]) {
for (const x of [-1.62, 0.98]) {
box(scene, 0.12, 0.045, 0.32, C.gold, x, 0.755, z);
}
}
// ---------------------------------------------------------------------
// Bridge signals
// ---------------------------------------------------------------------
function createSignal(z) {
const group = new THREE.Group();
group.position.set(1.43, 0.74, z);
scene.add(group);
box(group, 0.09, 0.82, 0.09, C.rail, 0, 0.4, 0);
box(group, 0.3, 0.56, 0.24, 0x253936, 0, 0.98, 0);
const red = emissiveMaterial(0xff5a48, 0.08);
const green = emissiveMaterial(0x62c98f, 0.08);
box(group, 0.17, 0.17, 0.08, red, 0, 1.1, -0.14);
box(group, 0.17, 0.17, 0.08, green, 0, 0.86, -0.14);
return { red, green };
}
const signals = {
south: createSignal(-1.28),
north: createSignal(1.28)
};
function setSignals(waiting) {
for (const signal of Object.values(signals)) {
signal.red.emissiveIntensity = waiting ? 2.3 : 0.05;
signal.green.emissiveIntensity = waiting ? 0.05 : 1.6;
}
}
// ---------------------------------------------------------------------
// Buildings, trees, café, boats, and street details
// ---------------------------------------------------------------------
function addHouse(x, z, width, depth, height, brick, frontDirection) {
const bottom = 0.28;
const bodyHeight = height;
box(
scene, width, bodyHeight, depth, brick,
x, bottom + bodyHeight / 2, z
);
// Stepped Dutch gable.
for (let i = 0; i < 3; i++) {
const tierWidth = width * (0.82 - i * 0.22);
box(
scene, tierWidth, 0.26, depth, brick,
x, bottom + bodyHeight + 0.13 + i * 0.26, z
);
}
box(
scene, width * 0.32, 0.18, depth + 0.06, C.roof,
x, bottom + bodyHeight + 0.98, z
);
const facadeZ = z + frontDirection * (depth / 2 + 0.035);
const outside = frontDirection * 0.07;
// Windows.
const columns = width > 2.4 ? 3 : 2;
for (let level = 0; level < Math.min(3, Math.floor(height / 1.25)); level++) {
for (let column = 0; column < columns; column++) {
const wx = x + (column - (columns - 1) / 2) * (width / (columns + 0.7));
const wy = bottom + 0.78 + level * 1.08;
box(scene, 0.64, 0.74, 0.08, 0xeee1c3, wx, wy, facadeZ);
box(
scene, 0.48, 0.56, 0.06,
emissiveMaterial(level === 0 ? 0xf1c879 : 0x91b8bd, 0.22),
wx, wy, facadeZ + outside
);
box(scene, 0.055, 0.58, 0.07, 0xeee1c3, wx, wy, facadeZ + outside * 1.2);
}
}
// Door.
const doorX = x + width * 0.22;
box(scene, 0.57, 1.25, 0.09, 0x40534c, doorX, bottom + 0.625, facadeZ);
box(scene, 0.06, 0.06, 0.06, C.gold, doorX + 0.17, bottom + 0.65, facadeZ + outside * 1.5);
}
// North-bank canal facades.
addHouse(-7.2, 5.25, 2.35, 3.35, 3.65, C.brickA, -1);
addHouse(-4.65, 5.2, 2.15, 3.3, 4.15, C.cream, -1);
addHouse(4.55, 5.2, 2.25, 3.4, 3.85, C.brickB, -1);
addHouse(7.05, 5.3, 2.35, 3.45, 4.4, C.brickC, -1);
// South-bank buildings.
addHouse(-6.7, -5.35, 2.45, 3.5, 3.55, C.brickC, 1);
addHouse(-3.95, -5.6, 2.15, 3.35, 4.0, C.brickD, 1);
addHouse(6.65, -5.35, 2.6, 3.45, 4.15, C.brickA, 1);
function addTree(x, z, scale = 1) {
box(scene, 0.36 * scale, 1.9 * scale, 0.36 * scale, C.trunk,
x, 0.28 + 0.95 * scale, z);
box(scene, 1.55 * scale, 1.2 * scale, 1.4 * scale, C.leaf,
x, 2.18 * scale, z);
box(scene, 1.05 * scale, 0.85 * scale, 1.0 * scale, C.leafLight,
x + 0.18 * scale, 3.05 * scale, z - 0.08 * scale);
box(scene, 0.8 * scale, 0.65 * scale, 0.85 * scale, C.leaf,
x - 0.25 * scale, 2.65 * scale, z + 0.15 * scale);
}
addTree(-3.25, 3.8, 0.82);
addTree(3.05, 3.8, 0.85);
addTree(-9.0, 7.7, 1.05);
addTree(9.25, 7.8, 1.0);
addTree(3.35, -3.65, 0.74);
addTree(-9.0, -7.8, 1.05);
// Small canal-side café terrace.
box(scene, 3.4, 1.5, 2.5, 0xc7b88f, 4.3, 1.03, -4.15);
box(scene, 3.65, 0.18, 2.7, 0x405850, 4.3, 1.86, -4.15);
for (let i = 0; i < 7; i++) {
const stripe = box(
scene, 0.5, 0.1, 2.9, i % 2 ? 0xe9dfc5 : 0x3e7c73,
2.85 + i * 0.5, 1.72, -4.15
);
stripe.rotation.x = -0.08;
}
// Café tables and stools.
for (const x of [3.3, 4.7]) {
box(scene, 0.12, 0.7, 0.12, 0x384b47, x, 0.63, -2.85);
box(scene, 0.85, 0.12, 0.85, 0x6e4e3d, x, 1.02, -2.85);
box(scene, 0.46, 0.48, 0.46, 0x7b5a43, x + 0.8, 0.52, -2.75);
}
// Square voxel boat.
const boat = new THREE.Group();
boat.position.set(-5.6, -0.02, 0.35);
boat.rotation.y = -0.12;
scene.add(boat);
box(boat, 2.6, 0.3, 1.05, 0x3b514d, 0, 0.12, 0);
box(boat, 2.15, 0.09, 0.75, 0xc89a5e, 0, 0.31, 0);
box(boat, 1.0, 0.62, 0.72, 0xe6dfc8, -0.25, 0.62, 0);
box(boat, 0.62, 0.28, 0.04, 0x7fafb0, -0.25, 0.76, -0.39);
box(boat, 0.22, 0.65, 0.22, 0x43514d, 0.62, 0.65, 0);
box(boat, 0.08, 0.08, 0.75, 0x43514d, 0.62, 0.95, 0);
// A second, smaller cargo boat.
const cargo = new THREE.Group();
cargo.position.set(5.9, -0.03, -0.55);
cargo.rotation.y = 0.16;
scene.add(cargo);
box(cargo, 2.2, 0.25, 0.9, 0x754f3c, 0, 0.1, 0);
box(cargo, 0.7, 0.55, 0.68, 0xd7c49c, -0.45, 0.5, 0);
box(cargo, 0.62, 0.5, 0.62, 0x527c70, 0.55, 0.48, 0);
// Street lamps.
function addLamp(x, z) {
box(scene, 0.09, 2.6, 0.09, 0x304541, x, 1.58, z);
box(scene, 0.32, 0.13, 0.32, 0x304541, x, 2.91, z);
box(scene, 0.24, 0.26, 0.24, emissiveMaterial(0xffd38b, 1.1), x, 2.73, z);
}
addLamp(-2.35, 3.25);
addLamp(2.35, 3.25);
addLamp(-2.35, -3.25);
addLamp(2.35, -3.25);
// Deterministic cobble accents.
let seed = 9137;
function random() {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
}
for (let i = 0; i < 95; i++) {
const side = random() > 0.5 ? 1 : -1;
const z = side * (3.2 + random() * 7.1);
const x = (random() - 0.5) * 26;
if (Math.abs(x) < 2.1) continue;
box(
scene,
0.17 + random() * 0.22,
0.025,
0.17 + random() * 0.22,
random() > 0.5 ? 0xc4baa3 : 0xb5aa92,
x, 0.295, z
);
}
// Animated water highlights.
const waterHighlights = [];
for (let i = 0; i < 18; i++) {
const x = -12 + random() * 24;
const z = -1.65 + random() * 3.3;
if (Math.abs(x) < 2.2) continue;
const material = new THREE.MeshStandardMaterial({
color: 0xb7e1dc,
emissive: 0x8bc8c5,
emissiveIntensity: 0.12,
transparent: true,
opacity: 0.55,
roughness: 0.65
});
const glint = box(scene, 0.6 + random() * 1.0, 0.025, 0.09, material, x, 0.005, z);
waterHighlights.push({ mesh: glint, phase: random() * Math.PI * 2, baseX: x });
}
// ---------------------------------------------------------------------
// Pedestrian voxel model
// ---------------------------------------------------------------------
function createPedestrian() {
const group = new THREE.Group();
const legs = [];
const arms = [];
for (const side of [-1, 1]) {
const legPivot = new THREE.Group();
legPivot.position.set(side * 0.13, 0.58, 0);
group.add(legPivot);
box(legPivot, 0.19, 0.5, 0.21, 0x354b4d, 0, -0.25, 0);
box(legPivot, 0.21, 0.11, 0.31, 0x263638, 0, -0.53, 0.035);
legs.push(legPivot);
const armPivot = new THREE.Group();
armPivot.position.set(side * 0.34, 1.04, 0);
group.add(armPivot);
box(armPivot, 0.14, 0.42, 0.17, 0xe77d61, 0, -0.2, 0);
box(armPivot, 0.13, 0.12, 0.15, 0xe5b08d, 0, -0.45, 0);
arms.push(armPivot);
}
box(group, 0.5, 0.55, 0.3, 0xe77d61, 0, 0.85, 0);
box(group, 0.28, 0.3, 0.27, 0xdca47f, 0, 1.28, 0.015);
box(group, 0.31, 0.13, 0.3, 0x47362e, 0, 1.45, -0.015);
box(group, 0.12, 0.24, 0.05, 0x47362e, 0.16, 1.31, 0.01);
scene.add(group);
return { group, legs, arms };
}
const pedestrian = createPedestrian();
// ---------------------------------------------------------------------
// Cyclist and voxel bicycle model
// ---------------------------------------------------------------------
function createVoxelWheel(zPosition) {
const wheel = new THREE.Group();
wheel.position.set(0, 0.31, zPosition);
box(wheel, 0.12, 0.11, 0.48, 0x263638, 0, 0.27, 0);
box(wheel, 0.12, 0.11, 0.48, 0x263638, 0, -0.27, 0);
box(wheel, 0.12, 0.48, 0.11, 0x263638, 0, 0, 0.27);
box(wheel, 0.12, 0.48, 0.11, 0x263638, 0, 0, -0.27);
box(wheel, 0.14, 0.08, 0.08, 0xb9c0ba, 0, 0, 0);
box(wheel, 0.14, 0.08, 0.08, 0xb9c0ba, 0, 0, 0);
return wheel;
}
function createCyclist() {
const group = new THREE.Group();
const rearWheel = createVoxelWheel(-0.66);
const frontWheel = createVoxelWheel(0.66);
group.add(rearWheel, frontWheel);
const rearAxle = new THREE.Vector3(0, 0.31, -0.66);
const frontAxle = new THREE.Vector3(0, 0.31, 0.66);
const crank = new THREE.Vector3(0, 0.35, -0.04);
const seatPost = new THREE.Vector3(0, 0.83, -0.29);
const handlePost = new THREE.Vector3(0, 0.86, 0.48);
beam(group, rearAxle, crank, 0.09, 0xe4ae4c);
beam(group, crank, seatPost, 0.09, 0xe4ae4c);
beam(group, seatPost, rearAxle, 0.09, 0xe4ae4c);
beam(group, seatPost, handlePost, 0.09, 0xe4ae4c);
beam(group, crank, handlePost, 0.09, 0xe4ae4c);
beam(group, handlePost, frontAxle, 0.09, 0xe4ae4c);
box(group, 0.39, 0.1, 0.28, 0x354b4d, 0, 0.91, -0.31);
box(group, 0.55, 0.08, 0.08, 0x354b4d, 0, 1.03, 0.49);
box(group, 0.1, 0.1, 0.1, 0x354b4d, -0.25, 1.03, 0.49);
box(group, 0.1, 0.1, 0.1, 0x354b4d, 0.25, 1.03, 0.49);
// Rider.
box(group, 0.44, 0.52, 0.29, 0x2c8e88, 0, 1.08, 0.07);
box(group, 0.32, 0.3, 0.28, 0xe1ae87, 0, 1.48, 0.1);
box(group, 0.35, 0.12, 0.3, 0x42352f, 0, 1.64, 0.08);
box(group, 0.15, 0.45, 0.16, 0x2c8e88, -0.27, 1.09, 0.17);
box(group, 0.15, 0.45, 0.16, 0x2c8e88, 0.27, 1.09, 0.17);
box(group, 0.15, 0.13, 0.17, 0xe1ae87, -0.27, 0.82, 0.17);
box(group, 0.15, 0.13, 0.17, 0xe1ae87, 0.27, 0.82, 0.17);
// Small delivery parcel.
box(group, 0.48, 0.37, 0.42, 0xd6b97e, 0, 1.18, -0.42);
box(group, 0.51, 0.055, 0.45, 0x806845, 0, 1.18, -0.42);
scene.add(group);
return { group, wheels: [rearWheel, frontWheel] };
}
const cyclist = createCyclist();
// ---------------------------------------------------------------------
// Movement rules
//
// 1. The pedestrian is hard-clamped to the pedestrian lane.
// 2. The cyclist is hard-clamped to the cycle lane.
// 3. Neither actor changes lanes.
// 4. The cyclist stops at the narrow-zone hold line when the pedestrian
// occupies or is approaching that zone.
// 5. Inside the narrow zone, the cyclist also reduces speed.
// ---------------------------------------------------------------------
const initialState = {
pedestrianZ: 2.3,
pedestrianDirection: -1,
cyclistZ: -2.7,
cyclistDirection: 1
};
let pedestrianZ = initialState.pedestrianZ;
let pedestrianDirection = initialState.pedestrianDirection;
let cyclistZ = initialState.cyclistZ;
let cyclistDirection = initialState.cyclistDirection;
let paused = false;
let priorityEnabled = true;
let speedMultiplier = 1;
let cyclistWaiting = false;
let walkPhase = 0;
let cycleTravel = 0;
function resetTraffic() {
pedestrianZ = initialState.pedestrianZ;
pedestrianDirection = initialState.pedestrianDirection;
cyclistZ = initialState.cyclistZ;
cyclistDirection = initialState.cyclistDirection;
paused = false;
cyclistWaiting = false;
walkPhase = 0;
cycleTravel = 0;
playButton.textContent = "Pause";
}
function updateTraffic(dt) {
const narrowCenter = 0;
const narrowHalfLength = 1.08;
const holdLine = cyclistDirection > 0 ? -1.08 : 1.08;
// Pedestrian: constant route, reversible movement.
const pedestrianSpeed = 0.88 * speedMultiplier;
pedestrianZ += pedestrianDirection * pedestrianSpeed * dt;
walkPhase += dt * pedestrianSpeed * 7;
if (pedestrianZ < -9.65) {
pedestrianZ = -9.65;
pedestrianDirection = 1;
} else if (pedestrianZ > 9.65) {
pedestrianZ = 9.65;
pedestrianDirection = -1;
}
// The pedestrian occupies or is about to occupy the pinch.
const pedAtPinch = Math.abs(pedestrianZ - narrowCenter) < 1.45;
const pedApproachingFromNorth =
pedestrianDirection < 0 &&
pedestrianZ >= 1.45 &&
pedestrianZ < 3.2;
const pedApproachingFromSouth =
pedestrianDirection > 0 &&
pedestrianZ <= -1.45 &&
pedestrianZ > -3.2;
const pedestrianConflict =
pedAtPinch || pedApproachingFromNorth || pedApproachingFromSouth;
// Cyclist approaches the hold line from either direction.
const cyclistBeforeHold =
cyclistDirection > 0
? cyclistZ <= holdLine && cyclistZ > holdLine - 1.15
: cyclistZ >= holdLine && cyclistZ < holdLine + 1.15;
cyclistWaiting =
priorityEnabled &&
cyclistBeforeHold &&
pedestrianConflict;
let cyclistSpeed = 1.85 * speedMultiplier;
// Slow down through the narrow shared bridge envelope.
if (Math.abs(cyclistZ - narrowCenter) < narrowHalfLength + 0.55) {
cyclistSpeed = Math.min(cyclistSpeed, 0.95 * speedMultiplier);
}
if (cyclistWaiting) cyclistSpeed = 0;
const previousCyclistZ = cyclistZ;
cyclistZ += cyclistDirection * cyclistSpeed * dt;
cycleTravel += Math.abs(cyclistZ - previousCyclistZ);
if (cyclistZ > 9.65) {
cyclistZ = 9.65;
cyclistDirection = -1;
} else if (cyclistZ < -9.65) {
cyclistZ = -9.65;
cyclistDirection = 1;
}
// Hard lane constraints preserve each route.
pedestrian.group.position.set(PED_X, routeSurfaceY(pedestrianZ), pedestrianZ);
cyclist.group.position.set(CYCLE_X, routeSurfaceY(cyclistZ), cyclistZ);
pedestrian.group.rotation.y =
pedestrianDirection > 0 ? 0 : Math.PI;
cyclist.group.rotation.y =
cyclistDirection > 0 ? 0 : Math.PI;
// Walking animation.
const swing = Math.sin(walkPhase) * 0.55;
pedestrian.legs[0].rotation.x = swing;
pedestrian.legs[1].rotation.x = -swing;
pedestrian.arms[0].rotation.x = -swing * 0.8;
pedestrian.arms[1].rotation.x = swing * 0.8;
// Pedaling animation.
cyclist.wheels.forEach((wheel) => {
wheel.rotation.x = -cycleTravel / 0.29;
});
const pedalSwing = cyclistSpeed > 0.02 ? Math.sin(cycleTravel * 9) * 0.45 : 0;
cyclist.group.children[8].rotation.x = pedalSwing;
cyclist.group.children[9].rotation.x = -pedalSwing;
setSignals(cyclistWaiting);
updateStatus();
}
function updateStatus() {
statusPanel.classList.toggle("yield", cyclistWaiting);
if (cyclistWaiting) {
statusText.textContent = "Cyclist yielding · pedestrian has priority";
} else if (Math.abs(pedestrianZ) < 1.35) {
statusText.textContent = "Pedestrian in pinch · cycle route remains clear";
} else if (priorityEnabled) {
statusText.textContent = "Routes separated · ready at narrow zone";
} else {
statusText.textContent = "Priority off · both routes remain separated";
}
}
// ---------------------------------------------------------------------
// UI controls
// ---------------------------------------------------------------------
playButton.addEventListener("click", () => {
paused = !paused;
playButton.textContent = paused ? "Play" : "Pause";
});
priorityButton.addEventListener("click", () => {
priorityEnabled = !priorityEnabled;
priorityButton.classList.toggle("active", priorityEnabled);
priorityButton.setAttribute("aria-pressed", String(priorityEnabled));
priorityButton.textContent = priorityEnabled
? "Pedestrian priority"
: "Priority off";
updateStatus();
});
resetButton.addEventListener("click", resetTraffic);
speedInput.addEventListener("input", () => {
speedMultiplier = Number(speedInput.value);
});
// ---------------------------------------------------------------------
// Direct camera controls: no addons or example modules
// ---------------------------------------------------------------------
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
orbit.azimuth -= deltaX * 0.006;
orbit.polar = THREE.MathUtils.clamp(
orbit.polar + deltaY * 0.005,
0.38,
1.25
);
updateCamera();
});
function endDrag(event) {
dragging = false;
renderer.domElement.style.cursor = "grab";
if (event?.pointerId !== undefined) {
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.style.cursor = "grab";
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.zoom = THREE.MathUtils.clamp(
orbit.zoom * Math.exp(-event.deltaY * 0.001),
0.65,
2.4
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
let handled = true;
switch (event.key) {
case "ArrowLeft":
orbit.azimuth += 0.09;
break;
case "ArrowRight":
orbit.azimuth -= 0.09;
break;
case "ArrowUp":
orbit.polar = Math.max(0.38, orbit.polar - 0.07);
break;
case "ArrowDown":
orbit.polar = Math.min(1.25, orbit.polar + 0.07);
break;
case "+":
case "=":
orbit.zoom = Math.min(2.4, orbit.zoom * 1.1);
break;
case "-":
case "_":
orbit.zoom = Math.max(0.65, orbit.zoom / 1.1);
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
updateCamera();
}
});
function updateCamera() {
const x =
cameraTarget.x +
orbit.radius * Math.sin(orbit.polar) * Math.sin(orbit.azimuth);
const y =
cameraTarget.y +
orbit.radius * Math.cos(orbit.polar);
const z =
cameraTarget.z +
orbit.radius * Math.sin(orbit.polar) * Math.cos(orbit.azimuth);
camera.position.set(x, y, z);
camera.lookAt(cameraTarget);
camera.zoom = orbit.zoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = aspect < 0.8 ? 19 : 14;
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
updateCamera();
}
window.addEventListener("resize", resize);
resize();
// ---------------------------------------------------------------------
// Animation loop
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
if (!paused) {
updateTraffic(dt);
}
waterHighlights.forEach((highlight, index) => {
highlight.phase += dt * (0.35 + index * 0.008);
highlight.mesh.position.x =
highlight.baseX + Math.sin(highlight.phase) * 0.35;
highlight.mesh.material.opacity =
0.38 + Math.sin(highlight.phase * 1.7) * 0.14;
});
renderer.render(scene, camera);
}
resetTraffic();
animate();
</script>
</body>
</html>
```