Prompt specification
Build or customize the Three.js 3D scene "Cyclist Encounters a Broken Link" 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 Encounters a Broken Link
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/cyclist-encounters-a-broken-link-x2090
Scene Description Prompt:
A voxel cyclist reaches a visible gap in the cycle route and stops instead of teleporting. Three.js from cdnjs, one HTML file.
Working Three.js Starter (cyclist-encounters-a-broken-link-x2090):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Gap-Aware Voxel Cyclist</title>
<style>
:root {
--ink: #10232c;
--panel: rgba(10, 27, 35, 0.88);
--line: rgba(255, 255, 255, 0.14);
--mint: #63f2c5;
--orange: #ff7849;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #91c9d7;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#app,
canvas {
width: 100%;
height: 100%;
display: block;
}
canvas {
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
top: 22px;
left: 22px;
width: min(360px, calc(100vw - 44px));
padding: 20px;
color: white;
background: var(--panel);
border: 1px solid var(--line);
border-radius: 18px;
box-shadow: 0 18px 50px rgba(5, 18, 25, 0.25);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
user-select: none;
z-index: 5;
}
.eyebrow {
color: var(--mint);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 8px 0 7px;
font-size: clamp(25px, 4vw, 34px);
line-height: 1;
letter-spacing: -0.04em;
}
.description {
margin: 0;
color: rgba(255, 255, 255, 0.68);
font-size: 13px;
line-height: 1.55;
}
.status {
display: flex;
align-items: center;
gap: 10px;
margin-top: 18px;
padding: 11px 12px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.055);
}
.status-dot {
width: 9px;
height: 9px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--mint);
box-shadow: 0 0 0 5px rgba(99, 242, 197, 0.12);
}
.status-dot.braking {
background: #ffd166;
box-shadow: 0 0 0 5px rgba(255, 209, 102, 0.12);
}
.status-dot.stopped {
background: var(--orange);
box-shadow: 0 0 0 5px rgba(255, 120, 73, 0.14);
}
#statusText {
font-size: 11px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-top: 10px;
}
.metric {
padding: 10px 12px;
border-radius: 10px;
background: rgba(255, 255, 255, 0.055);
}
.metric-label {
color: rgba(255, 255, 255, 0.48);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.metric-value {
margin-top: 3px;
font-size: 19px;
font-weight: 800;
font-variant-numeric: tabular-nums;
}
.progress {
height: 4px;
margin-top: 13px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.1);
}
#progressFill {
width: 0%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--mint), #ffe36e);
transition: width 80ms linear;
}
.controls {
position: fixed;
top: 22px;
right: 22px;
display: flex;
gap: 7px;
z-index: 6;
}
.controls button {
width: 40px;
height: 40px;
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 11px;
color: white;
background: rgba(10, 27, 35, 0.84);
box-shadow: 0 8px 24px rgba(5, 18, 25, 0.18);
backdrop-filter: blur(12px);
font: 800 18px system-ui, sans-serif;
cursor: pointer;
}
.controls button:hover {
background: rgba(23, 52, 63, 0.95);
border-color: rgba(255, 255, 255, 0.38);
}
#replay {
position: fixed;
right: 22px;
bottom: 22px;
z-index: 6;
padding: 12px 17px;
border: 0;
border-radius: 12px;
color: #10232c;
background: var(--mint);
box-shadow: 0 10px 30px rgba(5, 18, 25, 0.22);
font: 850 12px system-ui, sans-serif;
letter-spacing: 0.04em;
text-transform: uppercase;
cursor: pointer;
}
#replay:hover {
filter: brightness(1.07);
}
.hint {
position: fixed;
left: 50%;
bottom: 22px;
padding: 9px 13px;
transform: translateX(-50%);
color: rgba(255, 255, 255, 0.85);
background: rgba(10, 27, 35, 0.62);
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 99px;
font-size: 11px;
white-space: nowrap;
pointer-events: none;
z-index: 4;
}
@media (max-width: 680px) {
.panel {
top: 12px;
left: 12px;
width: calc(100vw - 78px);
padding: 15px;
}
.description,
.metrics {
display: none;
}
.status {
margin-top: 12px;
}
.controls {
top: 12px;
right: 12px;
flex-direction: column;
}
.controls button {
width: 38px;
height: 38px;
}
#replay {
right: 12px;
bottom: 12px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div id="app"></div>
<section class="panel" aria-label="Ride information">
<div class="eyebrow">Infrastructure Test / 03</div>
<h1>Gap-aware ride</h1>
<p class="description">
A voxel cyclist detects the missing cycleway, brakes, and stops safely
instead of crossing by teleportation.
</p>
<div class="status">
<span id="statusDot" class="status-dot"></span>
<strong id="statusText">Riding toward gap</strong>
</div>
<div class="metrics">
<div class="metric">
<div class="metric-label">Speed</div>
<div id="speedValue" class="metric-value">0.0 km/h</div>
</div>
<div class="metric">
<div class="metric-label">Gap width</div>
<div class="metric-value">3.0 m</div>
</div>
</div>
<div class="progress"><div id="progressFill"></div></div>
</section>
<div class="controls" aria-label="Camera controls">
<button id="zoomIn" title="Zoom in" aria-label="Zoom in">+</button>
<button id="zoomOut" title="Zoom out" aria-label="Zoom out">−</button>
<button id="resetView" title="Reset camera" aria-label="Reset camera">↺</button>
</div>
<button id="replay">Replay ride</button>
<div class="hint">Drag to orbit · Scroll to zoom · Shift-drag to pan</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const app = document.querySelector("#app");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x91c9d7);
scene.fog = new THREE.Fog(0x91c9d7, 25, 52);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
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.05;
app.appendChild(renderer.domElement);
scene.add(new THREE.HemisphereLight(0xd9f6ff, 0x6f7950, 2.25));
const sun = new THREE.DirectionalLight(0xfff1ce, 3.3);
sun.position.set(-8, 15, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -22;
sun.shadow.camera.right = 22;
sun.shadow.camera.top = 18;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const X_AXIS = new THREE.Vector3(1, 0, 0);
const materialCache = new Map();
function material(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0.02,
flatShading: true
})
);
}
return materialCache.get(color);
}
function addBox(
parent,
size,
position,
color,
rotation = [0, 0, 0],
customMaterial = null
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
customMaterial || material(color)
);
mesh.position.set(position[0], position[1], position[2]);
mesh.rotation.set(rotation[0], rotation[1], rotation[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function setBeam(mesh, a, b, width, depth) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const length = direction.length();
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.scale.set(length, width, depth);
mesh.quaternion.setFromUnitVectors(
X_AXIS,
direction.normalize()
);
}
function addBeam(parent, a, b, width, depth, color) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
material(color)
);
mesh.castShadow = true;
mesh.receiveShadow = true;
setBeam(mesh, a, b, width, depth);
parent.add(mesh);
return mesh;
}
// ------------------------------------------------------------------
// Environment
// ------------------------------------------------------------------
const groundTop = -1.2;
addBox(
scene,
[42, 0.55, 25],
[1, groundTop - 0.275, 0],
0x769f61
);
// Dark recessed area makes the missing elevated route unambiguous.
addBox(
scene,
[3.0, 0.18, 4.1],
[6.0, groundTop + 0.04, 0],
0x25383a
);
const leftStart = -14;
const leftEnd = 4.5;
const rightStart = 7.5;
const rightEnd = 16;
const routeTop = 0.45;
const routeWidth = 3.2;
function addRouteSection(start, end) {
const length = end - start;
const center = (start + end) * 0.5;
addBox(
scene,
[length, 0.5, routeWidth],
[center, routeTop - 0.25, 0],
0x344d55
);
// Green edge strips.
for (const z of [-1.48, 1.48]) {
addBox(
scene,
[length, 0.13, 0.2],
[center, routeTop + 0.035, z],
0x3ebf9a
);
}
// White lane boundaries.
for (const z of [-1.17, 1.17]) {
addBox(
scene,
[length - 0.12, 0.025, 0.075],
[center, routeTop + 0.025, z],
0xf2f0d7
);
}
// Supports.
for (let x = start + 1; x < end - 0.5; x += 3.4) {
addBox(
scene,
[0.45, 1.3, 2.4],
[x, -0.55, 0],
0x5b6d69
);
}
}
addRouteSection(leftStart, leftEnd);
addRouteSection(rightStart, rightEnd);
// Stop line immediately before the missing segment.
addBox(
scene,
[0.12, 0.035, 2.5],
[4.30, routeTop + 0.035, 0],
0xffd166
);
// Checkerboard hazard markings at both exposed edges.
function addHazardEdge(x, direction) {
for (let i = 0; i < 8; i++) {
const z = -1.05 + i * 0.3;
addBox(
scene,
[0.28, 0.06, 0.3],
[x + direction * 0.13, routeTop + 0.05, z],
i % 2 === 0 ? 0xff7849 : 0xffe7bd
);
}
}
addHazardEdge(leftEnd, -1);
addHazardEdge(rightStart, 1);
// Side bollards.
function addBollard(x, z) {
addBox(
scene,
[0.16, 1.25, 0.16],
[x, routeTop + 0.62, z],
0x40545a
);
for (let i = 0; i < 4; i++) {
addBox(
scene,
[0.19, 0.18, 0.19],
[x, routeTop + 0.16 + i * 0.25, z],
i % 2 === 0 ? 0xff7849 : 0xffe7bd
);
}
}
for (const x of [4.24, 7.76]) {
addBollard(x, -1.88);
addBollard(x, 1.88);
}
// Pixel-style warning arrow on the approach.
for (let i = 0; i < 4; i++) {
addBox(
scene,
[0.22, 0.035, 0.8 - i * 0.12],
[2.0 + i * 0.22, routeTop + 0.04, 0],
0xffb84d
);
}
// Warning sign generated locally with a canvas texture.
function makeGapSprite() {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 176;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#ff7849";
ctx.fillRect(0, 0, 512, 176);
ctx.fillStyle = "#172a31";
for (let i = -2; i < 14; i++) {
ctx.save();
ctx.translate(i * 52, 0);
ctx.transform(1, 0, -0.5, 1, 0, 0);
ctx.fillRect(0, 0, 24, 176);
ctx.restore();
}
ctx.fillStyle = "#fff4d8";
ctx.fillRect(24, 25, 464, 126);
ctx.fillStyle = "#172a31";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "900 68px Arial, sans-serif";
ctx.fillText("GAP // 3.0 m", 256, 88);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: true
})
);
sprite.scale.set(3.25, 1.12, 1);
sprite.position.set(6.0, 2.55, -2.25);
scene.add(sprite);
addBox(
scene,
[0.12, 1.7, 0.12],
[6.0, 1.05, -2.25],
0x40545a
);
}
makeGapSprite();
// Voxel trees.
function addTree(x, z, scale = 1) {
const tree = new THREE.Group();
tree.position.set(x, groundTop, z);
tree.scale.setScalar(scale);
addBox(tree, [0.42, 2.3, 0.42], [0, 1.15, 0], 0x79533c);
addBox(tree, [2.3, 1.35, 2.15], [0, 2.75, 0], 0x3f8a59);
addBox(tree, [1.55, 1.0, 1.45], [0.35, 3.65, -0.1], 0x57a963);
addBox(tree, [1.0, 0.75, 1.05], [-0.42, 4.25, 0.08], 0x6cbc69);
scene.add(tree);
}
[
[-12, 4.4, 1.15],
[-7, -4.8, 0.9],
[-1, 5.2, 1.1],
[3, -5.1, 0.82],
[10, 4.6, 1.05],
[14, -4.7, 1.2],
[-16, -3.8, 0.78]
].forEach((args) => addTree(...args));
// Distant block buildings.
function addBuilding(x, z, w, h, d, color) {
addBox(scene, [w, h, d], [x, groundTop + h / 2, z], color);
const rows = Math.max(1, Math.floor(h / 1.1));
const columns = Math.max(1, Math.floor(w / 1.1));
for (let row = 0; row < rows; row++) {
for (let col = 0; col < columns; col++) {
addBox(
scene,
[0.32, 0.38, 0.03],
[
x - w / 2 + 0.65 + col * 1.1,
groundTop + 0.65 + row * 1.05,
z + d / 2 + 0.025
],
0xb8e4df
);
}
}
}
addBuilding(-9, -9.4, 4.4, 4.8, 3.0, 0x7f9ca2);
addBuilding(-2.5, -10.5, 3.4, 6.6, 3.4, 0x6e879e);
addBuilding(8.8, -9.1, 5.3, 4.2, 3.2, 0x849b9a);
addBuilding(14.2, -10.2, 3.8, 6.0, 3.5, 0x718a9e);
// Blocky clouds.
function addCloud(x, y, z) {
addBox(scene, [2.2, 0.65, 0.85], [x, y, z], 0xf4fbf1);
addBox(scene, [1.25, 1.05, 0.85], [x - 0.4, y + 0.4, z], 0xf4fbf1);
addBox(scene, [1.25, 0.85, 0.85], [x + 0.65, y + 0.25, z], 0xf4fbf1);
}
addCloud(-10, 8.5, -6);
addCloud(2, 10, -8);
addCloud(12, 8.2, -4);
// ------------------------------------------------------------------
// Voxel cyclist
// ------------------------------------------------------------------
const bike = new THREE.Group();
bike.position.y = routeTop;
scene.add(bike);
const rider = new THREE.Group();
bike.add(rider);
const wheels = [];
function createWheel(x, z) {
const wheel = new THREE.Group();
wheel.position.set(x, 0.52, z);
// Chunky, voxel-like tire ring.
for (let i = 0; i < 12; i++) {
const angle = (i / 12) * Math.PI * 2;
addBox(
wheel,
[0.18, 0.18, 0.16],
[Math.cos(angle) * 0.48, Math.sin(angle) * 0.48, 0],
0x17252a
);
}
// Spokes.
for (let i = 0; i < 4; i++) {
const angle = (i / 4) * Math.PI * 2;
addBeam(
wheel,
[0, 0, 0],
[Math.cos(angle) * 0.38, Math.sin(angle) * 0.38, 0],
0.055,
0.055,
0x93a5a7
);
}
addBox(wheel, [0.15, 0.15, 0.23], [0, 0, 0], 0xd6dddd);
bike.add(wheel);
wheels.push(wheel);
return wheel;
}
createWheel(-0.85, 0.48);
createWheel(-0.85, -0.48);
createWheel(0.95, 0.48);
createWheel(0.95, -0.48);
// Frame.
const frameColor = 0xff7849;
addBeam(bike, [-0.85, 0.52, 0], [-0.3, 1.35, 0], 0.09, 0.09, frameColor);
addBeam(bike, [-0.3, 1.35, 0], [0, 0.56, 0], 0.09, 0.09, frameColor);
addBeam(bike, [0, 0.56, 0], [-0.85, 0.52, 0], 0.09, 0.09, frameColor);
addBeam(bike, [0, 0.56, 0], [0.95, 0.52, 0], 0.09, 0.09, frameColor);
addBeam(bike, [0.95, 0.52, 0], [0.56, 1.38, 0], 0.085, 0.085, frameColor);
addBeam(bike, [-0.3, 1.35, 0], [0.56, 1.38, 0], 0.08, 0.08, frameColor);
// Fork and handlebars.
addBeam(bike, [0.95, 0.52, 0.17], [0.56, 1.38, 0.17], 0.055, 0.055, 0xdbe3df);
addBeam(bike, [0.95, 0.52, -0.17], [0.56, 1.38, -0.17], 0.055, 0.055, 0xdbe3df);
addBeam(bike, [0.56, 1.38, -0.48], [0.56, 1.38, 0.48], 0.07, 0.07, 0x24383d);
addBox(bike, [0.48, 0.10, 0.30], [-0.31, 1.42, 0], 0x25383d);
// Crank and pedals.
const crank = new THREE.Group();
crank.position.set(0, 0.56, 0);
bike.add(crank);
addBox(crank, [0.62, 0.08, 0.08], [0, 0, 0.56], 0xe9eee8);
addBox(crank, [0.62, 0.08, 0.08], [0, 0, -0.56], 0xe9eee8);
addBox(crank, [0.23, 0.07, 0.23], [0.28, 0, 0.56], 0x25383d);
addBox(crank, [0.23, 0.07, 0.23], [-0.28, 0, -0.56], 0x25383d);
// Rider torso, head, helmet and face.
addBox(rider, [0.54, 0.28, 0.46], [-0.27, 1.49, 0], 0x263d46);
addBox(
rider,
[0.62, 0.80, 0.50],
[-0.04, 1.88, 0],
0xffcf55,
[0, 0, -0.38]
);
addBox(rider, [0.25, 0.22, 0.25], [0.09, 2.27, 0], 0xe8a778);
addBox(rider, [0.38, 0.38, 0.40], [0.27, 2.48, 0], 0xe8a778);
addBox(rider, [0.52, 0.18, 0.53], [0.25, 2.73, 0], 0x2fc5a5);
addBox(rider, [0.50, 0.07, 0.12], [0.27, 2.84, 0], 0x87f2d5);
addBox(rider, [0.13, 0.13, 0.16], [0.51, 2.46, 0.12], 0xe8a778);
addBox(rider, [0.07, 0.07, 0.035], [0.38, 2.54, 0.205], 0x18292e);
// Arms.
for (const side of [-1, 1]) {
addBeam(
rider,
[0.06, 2.08, side * 0.27],
[0.48, 1.55, side * 0.40],
0.16,
0.16,
0xffcf55
);
addBox(
rider,
[0.18, 0.16, 0.16],
[0.52, 1.49, side * 0.40],
0x25383d
);
}
// Animated legs.
const legs = [];
for (const side of [-1, 1]) {
const upper = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
material(0x354f59)
);
const lower = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
material(0x354f59)
);
upper.castShadow = lower.castShadow = true;
rider.add(upper, lower);
legs.push({ side, upper, lower });
}
// Brake light.
const brakeMaterial = new THREE.MeshStandardMaterial({
color: 0x6d1717,
emissive: 0xff2b12,
emissiveIntensity: 0,
roughness: 0.6
});
const brakeLight = addBox(
bike,
[0.10, 0.16, 0.18],
[-1.19, 0.91, 0],
0xff3d20,
[0, 0, 0],
brakeMaterial
);
// ------------------------------------------------------------------
// Camera controls implemented directly, without addons
// ------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 0.9, 0);
const spherical = new THREE.Spherical(19.5, 1.16, 0.72);
const initialSpherical = spherical.clone();
const initialTarget = cameraTarget.clone();
function applyCamera() {
spherical.phi = THREE.MathUtils.clamp(spherical.phi, 0.32, 1.42);
spherical.radius = THREE.MathUtils.clamp(spherical.radius, 8, 31);
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
applyCamera();
const canvas = renderer.domElement;
let dragging = false;
let panning = false;
let lastX = 0;
let lastY = 0;
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
panning = event.button === 2 || event.shiftKey;
lastX = event.clientX;
lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
if (panning) {
const right = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 0);
const up = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 1);
const scale = spherical.radius * 0.0015;
cameraTarget.addScaledVector(right, -dx * scale);
cameraTarget.addScaledVector(up, dy * scale);
} else {
spherical.theta -= dx * 0.006;
spherical.phi -= dy * 0.005;
}
applyCamera();
});
function endDrag() {
dragging = false;
panning = false;
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("lostpointercapture", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
spherical.radius *= Math.exp(event.deltaY * 0.001);
applyCamera();
},
{ passive: false }
);
document.querySelector("#zoomIn").addEventListener("click", () => {
spherical.radius *= 0.86;
applyCamera();
});
document.querySelector("#zoomOut").addEventListener("click", () => {
spherical.radius *= 1.16;
applyCamera();
});
document.querySelector("#resetView").addEventListener("click", () => {
spherical.copy(initialSpherical);
cameraTarget.copy(initialTarget);
applyCamera();
});
// ------------------------------------------------------------------
// Ride animation
// ------------------------------------------------------------------
const statusText = document.querySelector("#statusText");
const statusDot = document.querySelector("#statusDot");
const speedValue = document.querySelector("#speedValue");
const progressFill = document.querySelector("#progressFill");
const startX = -9.2;
const stopX = 2.72;
const cruiseSpeed = 2.0;
const brakeStartX = 0.22;
const brakeDuration = 2.5;
const brakeStartTime = (brakeStartX - startX) / cruiseSpeed;
const brakeEndTime = brakeStartTime + brakeDuration;
let elapsed = 0;
let lastStatus = "";
function setStatus(text, state) {
if (text === lastStatus) return;
lastStatus = text;
statusText.textContent = text;
statusDot.className = `status-dot ${state}`;
}
function resetRide() {
elapsed = 0;
lastStatus = "";
setStatus("Riding toward gap", "");
}
document.querySelector("#replay").addEventListener("click", resetRide);
function updateRide(time) {
let x;
let speed;
if (time < brakeStartTime) {
x = startX + cruiseSpeed * time;
speed = cruiseSpeed;
setStatus("Riding toward gap", "");
} else if (time < brakeEndTime) {
const u = (time - brakeStartTime) / brakeDuration;
x =
brakeStartX +
cruiseSpeed *
brakeDuration *
(u - 0.5 * u * u);
speed = cruiseSpeed * (1 - u);
setStatus("Gap detected — braking", "braking");
} else {
x = stopX;
speed = 0;
setStatus("Stopped safely at gap", "stopped");
}
const distance = Math.max(0, x - startX);
const wheelAngle = -distance / 0.52;
const pedalAngle = -wheelAngle * 2.15;
bike.position.x = x;
rider.position.y = speed > 0 ? Math.sin(distance * 7.2) * 0.035 : 0;
for (const wheel of wheels) {
wheel.rotation.z = wheelAngle;
}
crank.rotation.z = pedalAngle;
// Update voxel leg segments from hip to knee to rotating pedal.
for (const leg of legs) {
const side = leg.side;
const phase = side > 0 ? 0 : Math.PI;
const angle = pedalAngle + phase;
const pedal = [
Math.cos(angle) * 0.28,
0.56 + Math.sin(angle) * 0.28,
side * 0.56
];
const hip = [-0.27, 1.45, side * 0.19];
const knee = [
-0.06 + Math.sin(angle) * 0.10,
1.00 + Math.cos(angle) * 0.04,
side * 0.31
];
setBeam(leg.upper, hip, knee, 0.19, 0.19);
setBeam(leg.lower, knee, pedal, 0.16, 0.16);
}
brakeMaterial.emissiveIntensity =
time >= brakeStartTime && time < brakeEndTime ? 2.4 : 0;
speedValue.textContent = `${(speed * 3.6).toFixed(1)} km/h`;
progressFill.style.width = `${Math.min(1, distance / (stopX - startX)) * 100}%`;
}
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
elapsed += dt;
updateRide(elapsed);
renderer.render(scene, camera);
}
resetRide();
animate();
</script>
</body>
</html>
```