Prompt specification
Build or customize the Three.js 3D scene "Ford and Raised Footbridge Module" 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: Ford and Raised Footbridge Module
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/ford-and-raised-footbridge-module-x2137
Scene Description Prompt:
Make a stylized Three.js scene in a self-contained HTML file using cdnjs: ford and raised footbridge module. Model a shallow stream crossing beside a raised footbridge with visible deck height. Distinguish their geometry without suggesting either is safe under unspecified conditions.
Working Three.js Starter (ford-and-raised-footbridge-module-x2137):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Ford and Raised Footbridge</title>
<style>
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: #20302c;
background: #dce9e4;
}
#stage, #labels {
position: absolute;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#stage canvas:active { cursor: grabbing; }
#labels {
pointer-events: none;
overflow: hidden;
}
.scene-label {
position: absolute;
left: 0;
top: 0;
padding: 9px 12px 8px;
border: 1px solid rgba(255, 255, 255, .7);
border-radius: 12px;
background: rgba(248, 252, 249, .88);
box-shadow: 0 7px 25px rgba(38, 68, 57, .15);
backdrop-filter: blur(8px);
transform: translate(-50%, -100%);
white-space: nowrap;
transition: opacity .15s ease;
}
.scene-label::after {
content: "";
position: absolute;
left: 50%;
bottom: -7px;
width: 12px;
height: 12px;
transform: translateX(-50%) rotate(45deg);
border-right: 1px solid rgba(255, 255, 255, .7);
border-bottom: 1px solid rgba(255, 255, 255, .7);
background: rgba(248, 252, 249, .88);
}
.scene-label strong {
display: block;
font-size: 11px;
line-height: 1.2;
letter-spacing: .13em;
text-transform: uppercase;
}
.scene-label small {
display: block;
margin-top: 3px;
color: #5a6e66;
font-size: 12px;
}
.ford-label strong { color: #85652d; }
.bridge-label strong { color: #8a4f2c; }
.hud {
position: absolute;
z-index: 5;
pointer-events: none;
}
#heading {
top: 22px;
left: 24px;
max-width: min(420px, calc(100vw - 48px));
}
#heading h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(25px, 4vw, 42px);
font-weight: 500;
letter-spacing: -.025em;
line-height: 1;
}
#heading p {
margin: 8px 0 0;
color: #536860;
font-size: 13px;
line-height: 1.45;
}
#controls-note {
top: 22px;
right: 22px;
padding: 8px 11px;
border: 1px solid rgba(255, 255, 255, .65);
border-radius: 999px;
color: #52675f;
background: rgba(248, 252, 249, .72);
backdrop-filter: blur(8px);
font-size: 11px;
letter-spacing: .02em;
}
#notice {
left: 50%;
bottom: 20px;
width: min(720px, calc(100vw - 32px));
padding: 12px 16px;
border: 1px solid rgba(255, 255, 255, .75);
border-radius: 13px;
transform: translateX(-50%);
color: #40554d;
background: rgba(249, 252, 250, .91);
box-shadow: 0 9px 32px rgba(38, 68, 57, .13);
backdrop-filter: blur(10px);
font-size: 12px;
line-height: 1.45;
text-align: center;
}
#notice strong { color: #263c34; }
#reset {
position: absolute;
right: 22px;
bottom: 22px;
z-index: 7;
padding: 9px 13px;
border: 1px solid rgba(255, 255, 255, .7);
border-radius: 10px;
color: #334a42;
background: rgba(249, 252, 250, .85);
box-shadow: 0 5px 18px rgba(38, 68, 57, .12);
cursor: pointer;
font: inherit;
font-size: 12px;
}
#reset:hover { background: #fff; }
@media (max-width: 700px) {
#heading { top: 16px; left: 16px; }
#heading p { max-width: 250px; }
#controls-note { display: none; }
#notice {
bottom: 14px;
padding: 10px 12px;
font-size: 11px;
}
#reset { top: 15px; right: 15px; bottom: auto; }
.scene-label { padding: 7px 9px; }
.scene-label small { font-size: 10px; }
}
</style>
</head>
<body>
<div id="stage"></div>
<div id="labels">
<div id="ford-label" class="scene-label ford-label">
<strong>Low ford</strong>
<small>Gravel and stones at stream level</small>
</div>
<div id="bridge-label" class="scene-label bridge-label">
<strong>Raised footbridge</strong>
<small>Elevated timber deck and stepped approaches</small>
</div>
</div>
<header id="heading" class="hud">
<h1>Two ways across the stream</h1>
<p>A low ford sits beside a visibly elevated footbridge. Their geometry is distinct, but no access or safety status is implied.</p>
</header>
<div id="controls-note" class="hud">Drag to orbit · Scroll to zoom</div>
<div id="notice" class="hud">
<strong>Conditions are unspecified.</strong>
Water depth, flow, structural condition, and access restrictions may change.
Check current local information before considering either crossing.
</div>
<button id="reset" type="button">Reset view</button>
<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 stage = document.querySelector("#stage");
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 scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce9e4);
scene.fog = new THREE.Fog(0xdce9e4, 25, 48);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
// -------------------------------------------------------------------------
// Small helpers
// -------------------------------------------------------------------------
const material = (color, roughness = 0.9, metalness = 0) =>
new THREE.MeshStandardMaterial({ color, roughness, metalness });
const materials = {
soil: material(0x9b7959),
soilDark: material(0x765b45),
grass: material(0x7fa66d),
grassLight: material(0x94b778),
bankEdge: material(0x8b7658),
bed: material(0x718f8a),
gravel: material(0xb5a47d),
gravelDark: material(0x8f805f),
wood: material(0x9a633d),
woodLight: material(0xb97b4d),
woodDark: material(0x67442e),
stone: material(0x8d918a),
stoneLight: material(0xb4b7ad),
trunk: material(0x745039),
leaves: material(0x507a55),
leavesLight: material(0x6e9561),
reed: material(0x748c55),
sign: material(0xf1e4c5),
post: material(0x604633)
};
function box(w, h, d, x, y, z, mat, cast = true, receive = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), mat);
mesh.position.set(x, y, z);
mesh.castShadow = cast;
mesh.receiveShadow = receive;
scene.add(mesh);
return mesh;
}
function cylinder(radiusTop, radiusBottom, height, x, y, z, mat, sides = 8) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusTop, radiusBottom, height, sides),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
function beamBetween(start, end, thickness, mat) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(thickness, thickness, length),
mat
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 0, 1),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// Deterministic pseudo-random placement.
let seed = 74821;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
function range(min, max) {
return min + (max - min) * random();
}
// -------------------------------------------------------------------------
// Diorama base, banks, and shallow stream
// -------------------------------------------------------------------------
box(24, 0.7, 17, 0, -0.35, 0, materials.soilDark);
// Grass banks begin outside the 4-unit-wide channel.
box(24, 0.75, 6.5, 0, 0.375, 5.25, materials.grass);
box(24, 0.75, 6.5, 0, 0.375, -5.25, materials.grass);
// Exposed bank edges make the channel profile clear.
box(24, 0.28, 0.42, 0, 0.56, 2.02, materials.bankEdge, false, true);
box(24, 0.28, 0.42, 0, 0.56, -2.02, materials.bankEdge, false, true);
// Stream bed and translucent, shallow-looking water.
box(24, 0.25, 4.05, 0, 0.125, 0, materials.bed, false, true);
const waterMaterial = new THREE.MeshPhysicalMaterial({
color: 0x70b8bb,
roughness: 0.36,
metalness: 0.02,
transparent: true,
opacity: 0.76,
transmission: 0.08,
depthWrite: false
});
const water = new THREE.Mesh(new THREE.PlaneGeometry(24, 4.02), waterMaterial);
water.rotation.x = -Math.PI / 2;
water.position.y = 0.445;
water.receiveShadow = true;
scene.add(water);
// Small animated highlights indicate flow without claiming any flow rate.
const flowMaterial = new THREE.MeshBasicMaterial({
color: 0xe6fbf5,
transparent: true,
opacity: 0.58,
depthWrite: false
});
const flowMarks = [];
for (let i = 0; i < 24; i++) {
const mark = new THREE.Mesh(
new THREE.BoxGeometry(range(0.35, 0.9), 0.012, 0.035),
flowMaterial
);
mark.position.set(range(-11.5, 11.5), 0.465, range(-1.72, 1.72));
scene.add(mark);
flowMarks.push(mark);
}
// -------------------------------------------------------------------------
// Low ford: a broad gravel/stone causeway at approximately water level
// -------------------------------------------------------------------------
const fordX = -3.6;
// Central submerged causeway.
box(2.25, 0.23, 4.05, fordX, 0.405, 0, materials.gravel);
// Low ramps transition from the ford to the banks.
function gravelRamp(z1, y1, z2, y2) {
const dz = z2 - z1;
const dy = y2 - y1;
const length = Math.hypot(dz, dy);
const ramp = new THREE.Mesh(
new THREE.BoxGeometry(2.25, 0.17, length),
materials.gravel
);
ramp.position.set(fordX, (y1 + y2) * 0.5 - 0.075, (z1 + z2) * 0.5);
ramp.rotation.x = -Math.atan2(dy, dz);
ramp.castShadow = true;
ramp.receiveShadow = true;
scene.add(ramp);
}
gravelRamp(1.95, 0.54, 3.15, 0.83);
gravelRamp(-1.95, 0.54, -3.15, 0.83);
// Raised approach paths on the grass banks.
box(2.25, 0.16, 5.15, fordX, 0.75, 5.73, materials.gravel);
box(2.25, 0.16, 5.15, fordX, 0.75, -5.73, materials.gravel);
// Irregular low stones distinguish the ford from the timber structure.
const stoneGeometry = new THREE.IcosahedronGeometry(1, 0);
for (let i = 0; i < 30; i++) {
const stone = new THREE.Mesh(stoneGeometry, i % 3 === 0
? materials.stoneLight
: materials.stone
);
stone.position.set(
fordX + range(-0.88, 0.88),
0.54 + range(-0.02, 0.055),
range(-1.92, 1.92)
);
stone.scale.set(
range(0.17, 0.34),
range(0.07, 0.13),
range(0.18, 0.36)
);
stone.rotation.set(random(), random(), random());
stone.castShadow = true;
stone.receiveShadow = true;
scene.add(stone);
}
// -------------------------------------------------------------------------
// Raised footbridge: timber deck, piers, stairs, and railings
// -------------------------------------------------------------------------
const bridgeX = 3.55;
const deckTop = 2.58;
const deckHalfLength = 2.1;
const deckWidth = 2.15;
// Longitudinal support beams under the deck.
box(0.24, 0.32, 4.65, bridgeX - 0.72, 2.28, 0, materials.woodDark);
box(0.24, 0.32, 4.65, bridgeX + 0.72, 2.28, 0, materials.woodDark);
// Individual deck planks make the elevated structure easy to read.
const plankCount = 12;
const plankLength = (deckHalfLength * 2) / plankCount;
for (let i = 0; i < plankCount; i++) {
const z = -deckHalfLength + plankLength * (i + 0.5);
box(
deckWidth,
0.20,
plankLength - 0.035,
bridgeX,
deckTop - 0.10,
z,
i % 2 === 0 ? materials.wood : materials.woodLight
);
}
// Four visible piers emphasize the clearance above the water.
for (const xOffset of [-0.72, 0.72]) {
for (const z of [-1.35, 1.35]) {
box(
0.25,
2.03,
0.25,
bridgeX + xOffset,
1.275,
z,
materials.woodDark
);
box(
0.43,
0.16,
0.43,
bridgeX + xOffset,
0.34,
z,
materials.stone
);
}
}
// Stepped approaches on both banks.
function stairApproach(direction) {
const stepCount = 10;
const run = 0.34;
const startZ = direction * 2.1;
for (let i = 0; i < stepCount; i++) {
const t = i / (stepCount - 1);
const z = startZ + direction * run * i;
const top = THREE.MathUtils.lerp(deckTop, 0.84, t);
const height = top - 0.76;
box(
2.05,
height,
run + 0.025,
bridgeX,
0.76 + height * 0.5,
z,
i % 2 === 0 ? materials.wood : materials.woodLight
);
}
// Sloping handrails and vertical posts.
for (const xOffset of [-1.08, 1.08]) {
beamBetween(
new THREE.Vector3(bridgeX + xOffset, deckTop + 0.94, startZ),
new THREE.Vector3(bridgeX + xOffset, 1.78, startZ + direction * 3.06),
0.10,
materials.woodDark
);
for (let i = 0; i < 5; i++) {
const t = i / 4;
const z = startZ + direction * 3.06 * t;
const stepTop = THREE.MathUtils.lerp(deckTop, 0.84, t);
box(
0.10,
0.98,
0.10,
bridgeX + xOffset,
stepTop + 0.49,
z,
materials.woodDark
);
}
}
}
stairApproach(1);
stairApproach(-1);
// Deck-level railings.
for (const xOffset of [-1.08, 1.08]) {
for (const z of [-2.02, -1.02, 0, 1.02, 2.02]) {
box(
0.11,
0.98,
0.11,
bridgeX + xOffset,
deckTop + 0.49,
z,
materials.woodDark
);
}
beamBetween(
new THREE.Vector3(bridgeX + xOffset, deckTop + 0.94, -2.1),
new THREE.Vector3(bridgeX + xOffset, deckTop + 0.94, 2.1),
0.11,
materials.woodDark
);
beamBetween(
new THREE.Vector3(bridgeX + xOffset, deckTop + 0.44, -2.1),
new THREE.Vector3(bridgeX + xOffset, deckTop + 0.44, 2.1),
0.08,
materials.wood
);
}
// -------------------------------------------------------------------------
// Signs: descriptive rather than safety claims
// -------------------------------------------------------------------------
function signTexture(title, line1, line2, background, foreground) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 256;
const ctx = canvas.getContext("2d");
ctx.fillStyle = background;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = foreground;
ctx.lineWidth = 10;
ctx.strokeRect(12, 12, canvas.width - 24, canvas.height - 24);
ctx.textAlign = "center";
ctx.fillStyle = foreground;
ctx.font = "bold 51px sans-serif";
ctx.fillText(title, 256, 78);
ctx.font = "29px sans-serif";
ctx.fillText(line1, 256, 139);
ctx.fillText(line2, 256, 181);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
function makeSign(x, z, title, line1, line2, background, foreground) {
const group = new THREE.Group();
group.position.set(x, 0, z);
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.07, 0.09, 1.75, 8),
materials.post
);
post.position.y = 0.75 + 0.875;
post.castShadow = true;
group.add(post);
const board = new THREE.Mesh(
new THREE.BoxGeometry(2.1, 1.03, 0.12),
materials.sign
);
board.position.y = 2.1;
board.castShadow = true;
group.add(board);
const face = new THREE.Mesh(
new THREE.PlaneGeometry(1.98, 0.91),
new THREE.MeshBasicMaterial({
map: signTexture(title, line1, line2, background, foreground)
})
);
face.position.set(0, 2.1, 0.067);
group.add(face);
scene.add(group);
}
makeSign(
-5.45, 3.25,
"LOW FORD",
"CHECK DEPTH & FLOW",
"ACCESS MAY CHANGE",
"#e8d6a9", "#493b25"
);
makeSign(
5.45, 3.25,
"RAISED FOOTBRIDGE",
"CHECK ACCESS / CLOSURES",
"CONDITIONS VARY",
"#ead1b9", "#503325"
);
// -------------------------------------------------------------------------
// Landscape details
// -------------------------------------------------------------------------
function tree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0.75, z);
group.scale.setScalar(scale);
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.16, 0.23, 1.55, 7),
materials.trunk
);
trunk.position.y = 0.78;
trunk.castShadow = true;
group.add(trunk);
const crownGeometry = new THREE.IcosahedronGeometry(1, 1);
const crownA = new THREE.Mesh(crownGeometry, materials.leaves);
crownA.position.set(0, 1.95, 0);
crownA.scale.set(1.0, 1.2, 0.95);
crownA.castShadow = true;
group.add(crownA);
const crownB = new THREE.Mesh(crownGeometry, materials.leavesLight);
crownB.position.set(0.42, 2.22, -0.08);
crownB.scale.set(0.72, 0.82, 0.68);
crownB.castShadow = true;
group.add(crownB);
scene.add(group);
}
[
[-10.4, -5.7, 1.05], [-8.5, 6.8, 0.92], [-1.4, 7.1, 1.02],
[8.7, 6.8, 1.08], [10.4, -5.8, 0.96], [6.7, -6.7, 0.84],
[-10.5, 5.6, 0.8], [0.1, -7.1, 0.92], [10.2, 2.9, 0.75]
].forEach(args => tree(...args));
// Low-poly bank rocks.
for (let i = 0; i < 28; i++) {
const side = random() > 0.5 ? 1 : -1;
const rock = new THREE.Mesh(stoneGeometry, i % 4 === 0
? materials.stoneLight
: materials.stone
);
rock.position.set(range(-11.7, 11.7), 0.78, side * range(2.05, 2.45));
rock.scale.set(range(0.15, 0.4), range(0.12, 0.28), range(0.12, 0.32));
rock.rotation.set(random(), random(), random());
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
}
// Reeds clustered near the water but clear of both crossing alignments.
const reedGeometry = new THREE.ConeGeometry(0.055, 0.8, 5);
for (let i = 0; i < 65; i++) {
const side = random() > 0.5 ? 1 : -1;
const x = range(-11.8, 11.8);
if (Math.abs(x - fordX) < 1.5 || Math.abs(x - bridgeX) < 1.4) continue;
const reed = new THREE.Mesh(reedGeometry, materials.reed);
reed.position.set(x, 0.82, side * range(2.0, 2.65));
reed.rotation.z = range(-0.2, 0.2);
reed.castShadow = true;
scene.add(reed);
}
// Small grass tufts.
const tuftGeometry = new THREE.ConeGeometry(0.12, 0.35, 4);
for (let i = 0; i < 90; i++) {
const x = range(-11.5, 11.5);
const z = (random() > 0.5 ? 1 : -1) * range(2.8, 8.0);
if (Math.abs(x - fordX) < 1.35 || Math.abs(x - bridgeX) < 1.3) continue;
const tuft = new THREE.Mesh(tuftGeometry, materials.grassLight);
tuft.position.set(x, 0.88, z);
tuft.scale.y = range(0.7, 1.4);
tuft.castShadow = true;
scene.add(tuft);
}
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xf5fff9, 0x78654f, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d5, 3.1);
sun.position.set(-8, 15, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -15;
sun.shadow.camera.right = 15;
sun.shadow.camera.top = 13;
sun.shadow.camera.bottom = -13;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 40;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xaed9e1, 0.75);
fill.position.set(9, 7, -10);
scene.add(fill);
// -------------------------------------------------------------------------
// Custom camera controls, implemented directly without add-ons
// -------------------------------------------------------------------------
const target = new THREE.Vector3(0, 0.75, 0);
const spherical = new THREE.Spherical();
const pointer = new THREE.Vector2();
let dragging = false;
let previousX = 0;
let previousY = 0;
function baseRadius() {
return window.innerWidth / window.innerHeight < 0.8 ? 30 : 19.5;
}
function resetCamera() {
spherical.set(baseRadius(), 1.02, 0.66);
camera.fov = window.innerWidth / window.innerHeight < 0.8 ? 52 : 42;
camera.updateProjectionMatrix();
updateCamera();
}
function updateCamera() {
camera.position.setFromSpherical(spherical).add(target);
camera.lookAt(target);
}
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
spherical.theta -= dx * 0.006;
spherical.phi = THREE.MathUtils.clamp(spherical.phi - dy * 0.005, 0.38, 1.42);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined && renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
spherical.radius = THREE.MathUtils.clamp(
spherical.radius * Math.exp(event.deltaY * 0.001),
10,
38
);
updateCamera();
}, { passive: false });
document.querySelector("#reset").addEventListener("click", resetCamera);
renderer.domElement.addEventListener("dblclick", resetCamera);
// Keyboard camera controls.
window.addEventListener("keydown", event => {
const step = 0.09;
if (event.key === "ArrowLeft") spherical.theta += step;
if (event.key === "ArrowRight") spherical.theta -= step;
if (event.key === "ArrowUp") spherical.phi = Math.max(0.38, spherical.phi - step);
if (event.key === "ArrowDown") spherical.phi = Math.min(1.42, spherical.phi + step);
if (event.key === "+" || event.key === "=") spherical.radius = Math.max(10, spherical.radius - 1);
if (event.key === "-") spherical.radius = Math.min(38, spherical.radius + 1);
updateCamera();
});
// -------------------------------------------------------------------------
// Screen-space labels
// -------------------------------------------------------------------------
const fordLabel = document.querySelector("#ford-label");
const bridgeLabel = document.querySelector("#bridge-label");
const fordAnchor = new THREE.Vector3(fordX, 1.05, 0);
const bridgeAnchor = new THREE.Vector3(bridgeX, 3.95, 0);
function positionLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.opacity = visible ? "1" : "0";
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.fov = camera.aspect < 0.8 ? 52 : 42;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
resetCamera();
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
for (const mark of flowMarks) {
mark.position.x += delta * 0.52;
if (mark.position.x > 12) mark.position.x = -12;
}
positionLabel(fordLabel, fordAnchor);
positionLabel(bridgeLabel, bridgeAnchor);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```