Prompt specification
Build or customize the Three.js 3D scene "Bicycle drivetrain inspection corner" 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: Bicycle drivetrain inspection corner
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/bicycle-drivetrain-inspection-corner-x0251
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts bicycle chains and sprockets. Build a bicycle rear triangle on a stand with crank, chain, and rear sprockets; highlight the full motion path when the pedals turn slowly. Voxel style.
Working Three.js Starter (bicycle-drivetrain-inspection-corner-x0251):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Chain Drive</title>
<style>
:root {
--ink: #203436;
--panel: rgba(245, 249, 247, 0.9);
--line: rgba(32, 52, 54, 0.16);
--teal: #08bdb8;
--orange: #e88a37;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d8e4e2;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
body {
user-select: none;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
canvas:active {
cursor: grabbing;
}
.header {
position: fixed;
z-index: 10;
top: 26px;
left: 30px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 8px;
color: #4d6668;
font: 700 10px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 4vw, 48px);
font-weight: 850;
line-height: 0.94;
letter-spacing: -0.055em;
text-transform: uppercase;
}
.subtitle {
max-width: 380px;
margin-top: 12px;
color: #526a6c;
font-size: 12px;
line-height: 1.55;
}
.status {
position: fixed;
z-index: 10;
top: 28px;
right: 28px;
min-width: 178px;
padding: 13px 15px;
border: 1px solid var(--line);
border-radius: 4px;
background: rgba(245, 249, 247, 0.72);
backdrop-filter: blur(12px);
pointer-events: none;
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
font: 700 10px/1.3 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.status-row + .status-row {
margin-top: 7px;
color: #607779;
}
.live-dot {
display: inline-block;
width: 7px;
height: 7px;
margin-right: 7px;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 0 0 4px rgba(8, 189, 184, 0.13);
vertical-align: 1px;
}
.controls {
position: fixed;
z-index: 20;
right: 26px;
bottom: 26px;
width: min(310px, calc(100vw - 52px));
padding: 18px;
border: 1px solid var(--line);
border-radius: 5px;
background: var(--panel);
box-shadow: 0 18px 50px rgba(38, 63, 64, 0.12);
backdrop-filter: blur(14px);
}
.controls-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 15px;
font: 800 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.controls-title span:last-child {
color: var(--teal);
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-bottom: 17px;
}
button {
min-height: 38px;
padding: 0 12px;
border: 1px solid rgba(32, 52, 54, 0.18);
border-radius: 3px;
background: rgba(255, 255, 255, 0.65);
color: var(--ink);
font: 750 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
transition:
background 150ms ease,
color 150ms ease,
transform 150ms ease;
}
button:hover {
transform: translateY(-1px);
background: white;
}
button.active {
border-color: var(--teal);
background: var(--teal);
color: #102b2d;
}
.speed-label {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 9px;
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.speed-label output {
color: var(--orange);
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0 0 18px;
border-radius: 0;
outline: none;
appearance: none;
background: linear-gradient(
90deg,
var(--orange) 0%,
var(--orange) 30%,
rgba(32, 52, 54, 0.16) 30%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
border: 3px solid #f4f7f5;
border-radius: 50%;
appearance: none;
background: var(--orange);
box-shadow: 0 1px 5px rgba(32, 52, 54, 0.25);
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 3px solid #f4f7f5;
border-radius: 50%;
background: var(--orange);
box-shadow: 0 1px 5px rgba(32, 52, 54, 0.25);
}
.legend {
display: flex;
gap: 17px;
padding-top: 14px;
border-top: 1px solid var(--line);
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
color: #5d7476;
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.swatch {
width: 14px;
height: 4px;
border-radius: 1px;
background: var(--teal);
}
.swatch.orange {
background: var(--orange);
}
.hint {
position: fixed;
z-index: 10;
bottom: 31px;
left: 30px;
color: rgba(32, 52, 54, 0.62);
font: 700 9px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
pointer-events: none;
}
@media (max-width: 700px) {
.header {
top: 18px;
left: 18px;
}
.subtitle,
.hint {
display: none;
}
.status {
top: 18px;
right: 18px;
min-width: 145px;
padding: 10px;
}
.status-row {
font-size: 8px;
}
.controls {
right: 14px;
bottom: 14px;
width: calc(100vw - 28px);
padding: 14px;
}
}
</style>
</head>
<body>
<header class="header">
<div class="eyebrow">Kinetic Study / 01</div>
<h1>Chain<br />Drive</h1>
<div class="subtitle">
A voxel bicycle rear triangle, cassette, crank, and moving chain on a
workshop stand.
</div>
</header>
<div class="status">
<div class="status-row">
<span id="motionState"><i class="live-dot"></i>Slow motion</span>
<span id="statusRpm">10 RPM</span>
</div>
<div class="status-row">
<span>Motion path</span>
<span id="pathState">Full / On</span>
</div>
</div>
<section class="controls" aria-label="Drivetrain controls">
<div class="controls-title">
<span>Drivetrain control</span>
<span id="driveLed">●</span>
</div>
<div class="button-row">
<button id="playButton" type="button">Pause</button>
<button id="traceButton" class="active" type="button">Path: On</button>
</div>
<label class="speed-label" for="speed">
<span>Pedal speed</span>
<output id="speedOutput">10 RPM</output>
</label>
<input
id="speed"
type="range"
min="4"
max="24"
step="1"
value="10"
aria-label="Pedal speed in revolutions per minute"
/>
<div class="legend">
<div class="legend-item">
<span class="swatch"></span>
Chain route
</div>
<div class="legend-item">
<span class="swatch orange"></span>
Pedal orbit
</div>
</div>
</section>
<div class="hint">Drag to orbit · Scroll to zoom · Space to pause</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Renderer, scene, lighting
// ---------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd8e4e2);
scene.fog = new THREE.Fog(0xd8e4e2, 13, 27);
const camera = new THREE.PerspectiveCamera(
36,
innerWidth / innerHeight,
0.1,
60
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
document.body.prepend(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xf5fbff, 0x71817b, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff4df, 3.2);
sun.position.set(3.5, 8.5, 6);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -8;
sun.shadow.camera.right = 8;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -8;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 25;
sun.shadow.bias = -0.0005;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0xbce9ef, 1.4);
rimLight.position.set(-6, 4, -5);
scene.add(rimLight);
// ---------------------------------------------------------------------
// Materials and voxel construction helpers
// ---------------------------------------------------------------------
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.05,
...options
});
const MAT = {
frame: material(0xe85e4b, { roughness: 0.6 }),
frameDark: material(0xb94035, { roughness: 0.66 }),
tire: material(0x263437, { roughness: 0.93 }),
rim: material(0xaebcb8, { roughness: 0.42, metalness: 0.5 }),
spoke: material(0x82918f, { roughness: 0.45, metalness: 0.55 }),
metal: material(0xdce5e1, { roughness: 0.34, metalness: 0.7 }),
darkMetal: material(0x3b4d50, { roughness: 0.38, metalness: 0.65 }),
gold: material(0xe9b44d, { roughness: 0.42, metalness: 0.6 }),
goldDark: material(0xb9802d, { roughness: 0.5, metalness: 0.55 }),
crank: material(0xe98237, { roughness: 0.45, metalness: 0.45 }),
stand: material(0x31585a, { roughness: 0.7 }),
standDark: material(0x203f42, { roughness: 0.75 }),
saddle: material(0x293638, { roughness: 0.95 }),
bolt: material(0x8b9996, { roughness: 0.35, metalness: 0.75 })
};
function box(w, h, d, mat, x = 0, y = 0, z = 0, parent = scene) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), mat);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function beamBetween(
a,
b,
width,
depth,
mat,
parent = scene
) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, length, depth),
mat
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// ---------------------------------------------------------------------
// Tiled voxel ground
// ---------------------------------------------------------------------
const tileGeometry = new THREE.BoxGeometry(0.94, 0.08, 0.94);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1
});
const tileCountX = 13;
const tileCountZ = 9;
const tiles = new THREE.InstancedMesh(
tileGeometry,
tileMaterial,
tileCountX * tileCountZ
);
const tileDummy = new THREE.Object3D();
const tileColor = new THREE.Color();
let tileIndex = 0;
for (let ix = 0; ix < tileCountX; ix++) {
for (let iz = 0; iz < tileCountZ; iz++) {
const x = (ix - (tileCountX - 1) / 2) * 1.01;
const z = (iz - (tileCountZ - 1) / 2) * 1.01;
tileDummy.position.set(x, -0.07, z);
tileDummy.rotation.set(0, 0, 0);
tileDummy.scale.set(1, 1, 1);
tileDummy.updateMatrix();
tiles.setMatrixAt(tileIndex, tileDummy.matrix);
const variation = ((ix * 17 + iz * 31) % 5) * 0.012;
tileColor.setHSL(0.43, 0.08, 0.73 + variation);
tiles.setColorAt(tileIndex, tileColor);
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
tiles.instanceColor.needsUpdate = true;
tiles.receiveShadow = true;
scene.add(tiles);
box(13.2, 0.12, 9.2, material(0xb7c9c5), 0, -0.17, 0);
// ---------------------------------------------------------------------
// Main bicycle geometry
// ---------------------------------------------------------------------
const rear = new THREE.Vector3(-2.65, 1.68, 0);
const bottomBracket = new THREE.Vector3(0.25, 1.05, 0);
const seatTop = new THREE.Vector3(-0.25, 2.95, 0);
// Rear triangle.
beamBetween(
[bottomBracket.x, bottomBracket.y, 0],
[seatTop.x, seatTop.y, 0],
0.23,
0.23,
MAT.frame
);
for (const side of [-1, 1]) {
beamBetween(
[bottomBracket.x, bottomBracket.y, side * 0.16],
[rear.x, rear.y, side * 0.22],
0.16,
0.14,
MAT.frameDark
);
beamBetween(
[seatTop.x, seatTop.y, side * 0.15],
[rear.x, rear.y, side * 0.22],
0.15,
0.14,
MAT.frame
);
box(
0.31,
0.28,
0.12,
MAT.frameDark,
rear.x,
rear.y,
side * 0.27
);
box(
0.1,
0.1,
0.08,
MAT.bolt,
rear.x,
rear.y,
side * 0.35
);
}
box(0.4, 0.4, 0.56, MAT.darkMetal, bottomBracket.x, bottomBracket.y, 0);
box(0.46, 0.46, 0.08, MAT.metal, bottomBracket.x, bottomBracket.y, 0.32);
box(0.46, 0.46, 0.08, MAT.metal, bottomBracket.x, bottomBracket.y, -0.32);
// Seatpost and compact saddle.
beamBetween(
[seatTop.x, seatTop.y, 0],
[-0.34, 3.32, 0],
0.12,
0.14,
MAT.metal
);
box(0.72, 0.14, 0.34, MAT.saddle, -0.39, 3.38, 0);
box(0.32, 0.07, 0.22, MAT.saddle, -0.65, 3.46, 0);
// ---------------------------------------------------------------------
// Voxel rear wheel
// ---------------------------------------------------------------------
const rearSpin = new THREE.Group();
rearSpin.position.copy(rear);
scene.add(rearSpin);
const wheelRadius = 1.53;
const tireSegments = 40;
const tireLength = (Math.PI * 2 * wheelRadius) / tireSegments;
for (let i = 0; i < tireSegments; i++) {
const angle = (i / tireSegments) * Math.PI * 2;
const tire = box(
tireLength * 0.82,
0.17,
0.24,
MAT.tire,
Math.cos(angle) * wheelRadius,
Math.sin(angle) * wheelRadius,
0,
rearSpin
);
tire.rotation.z = angle + Math.PI / 2;
}
const rimRadius = 1.34;
for (let i = 0; i < 32; i++) {
const angle = (i / 32) * Math.PI * 2;
const rim = box(
0.21,
0.1,
0.15,
MAT.rim,
Math.cos(angle) * rimRadius,
Math.sin(angle) * rimRadius,
0,
rearSpin
);
rim.rotation.z = angle + Math.PI / 2;
}
for (let i = 0; i < 10; i++) {
const angle = (i / 10) * Math.PI * 2;
const z = i % 2 === 0 ? 0.04 : -0.04;
beamBetween(
[0, 0, z],
[
Math.cos(angle) * (rimRadius - 0.07),
Math.sin(angle) * (rimRadius - 0.07),
z
],
0.045,
0.045,
MAT.spoke,
rearSpin
);
}
box(0.34, 0.34, 0.52, MAT.darkMetal, 0, 0, 0, rearSpin);
box(0.13, 0.13, 0.64, MAT.metal, 0, 0, 0, rearSpin);
// ---------------------------------------------------------------------
// Voxel gears
// ---------------------------------------------------------------------
function createVoxelGear(
radius,
toothCount,
depth,
outerMaterial,
innerMaterial,
z,
parent,
withSpokes = true
) {
const group = new THREE.Group();
group.position.z = z;
parent.add(group);
const circumference = Math.PI * 2 * radius;
const toothWidth = circumference / toothCount;
for (let i = 0; i < toothCount; i++) {
const angle = (i / toothCount) * Math.PI * 2;
const tooth = box(
toothWidth * 0.72,
0.13,
depth,
outerMaterial,
Math.cos(angle) * radius,
Math.sin(angle) * radius,
0,
group
);
tooth.rotation.z = angle + Math.PI / 2;
const inner = box(
toothWidth * 0.82,
0.11,
depth * 0.92,
innerMaterial,
Math.cos(angle) * (radius - 0.11),
Math.sin(angle) * (radius - 0.11),
0,
group
);
inner.rotation.z = angle + Math.PI / 2;
}
if (withSpokes && radius > 0.42) {
for (let i = 0; i < 5; i++) {
const angle = (i / 5) * Math.PI * 2 + 0.18;
beamBetween(
[0, 0, 0],
[
Math.cos(angle) * (radius - 0.18),
Math.sin(angle) * (radius - 0.18),
0
],
0.075,
0.07,
innerMaterial,
group
);
}
}
box(0.25, 0.25, depth + 0.07, MAT.metal, 0, 0, 0, group);
return group;
}
// Rear cassette: three visible stepped sprockets.
createVoxelGear(
0.36,
24,
0.09,
MAT.gold,
MAT.goldDark,
0.39,
rearSpin,
false
);
createVoxelGear(
0.28,
21,
0.08,
MAT.metal,
MAT.darkMetal,
0.30,
rearSpin,
false
);
createVoxelGear(
0.21,
18,
0.07,
MAT.goldDark,
MAT.darkMetal,
0.21,
rearSpin,
false
);
// ---------------------------------------------------------------------
// Crank and chainring
// ---------------------------------------------------------------------
const crankRotor = new THREE.Group();
crankRotor.position.copy(bottomBracket);
scene.add(crankRotor);
createVoxelGear(
0.74,
34,
0.10,
MAT.gold,
MAT.goldDark,
0.39,
crankRotor,
true
);
beamBetween(
[0, 0, 0.58],
[0.88, 0, 0.58],
0.14,
0.13,
MAT.crank,
crankRotor
);
beamBetween(
[0, 0, -0.34],
[-0.88, 0, -0.34],
0.14,
0.13,
MAT.darkMetal,
crankRotor
);
const drivePedal = box(
0.36,
0.13,
0.24,
MAT.darkMetal,
0.88,
0,
0.70,
crankRotor
);
const farPedal = box(
0.36,
0.13,
0.24,
MAT.darkMetal,
-0.88,
0,
-0.48,
crankRotor
);
box(
0.18,
0.18,
0.74,
MAT.metal,
bottomBracket.x,
bottomBracket.y,
0.14
);
// ---------------------------------------------------------------------
// Workshop stand
// ---------------------------------------------------------------------
box(0.22, 3.3, 0.22, MAT.stand, 0.62, 1.72, -0.70);
box(0.55, 0.18, 0.55, MAT.standDark, 0.62, 0.15, -0.70);
box(2.1, 0.16, 0.22, MAT.stand, 0.60, 0.13, -0.70);
box(0.22, 0.16, 1.55, MAT.stand, 0.60, 0.13, -0.70);
box(
0.42,
0.42,
0.42,
MAT.standDark,
0.62,
0.32,
-0.70
);
beamBetween(
[0.62, 3.04, -0.70],
[-0.25, 3.04, -0.20],
0.16,
0.16,
MAT.stand
);
box(
0.38,
0.38,
0.42,
MAT.standDark,
-0.25,
2.98,
-0.04
);
box(
0.34,
0.18,
0.18,
MAT.crank,
-0.25,
2.98,
0.21
);
// Small voxel bolts on the stand.
box(0.1, 0.1, 0.08, MAT.bolt, 0.62, 3.21, -0.55);
box(0.1, 0.1, 0.08, MAT.bolt, -0.17, 3.21, -0.04);
// ---------------------------------------------------------------------
// Exact external-tangent chain path
// ---------------------------------------------------------------------
const CHAIN_RING_RADIUS = 0.74;
const CHAIN_REAR_RADIUS = 0.36;
function makeChainPath(centerA, radiusA, centerB, radiusB) {
const dx = centerB.x - centerA.x;
const dy = centerB.y - centerA.y;
const length = Math.hypot(dx, dy);
const ux = dx / length;
const uy = dy / length;
const px = -uy;
const py = ux;
const a = (radiusA - radiusB) / length;
const b = Math.sqrt(Math.max(0, 1 - a * a));
const candidate1 = {
x: a * ux + b * px,
y: a * uy + b * py
};
const candidate2 = {
x: a * ux - b * px,
y: a * uy - b * py
};
const topNormal =
candidate1.y > candidate2.y ? candidate1 : candidate2;
const bottomNormal =
candidate1.y > candidate2.y ? candidate2 : candidate1;
const topAngle = Math.atan2(topNormal.y, topNormal.x);
const bottomAngle = Math.atan2(bottomNormal.y, bottomNormal.x);
const bigTop = {
x: centerA.x + topNormal.x * radiusA,
y: centerA.y + topNormal.y * radiusA
};
const smallTop = {
x: centerB.x + topNormal.x * radiusB,
y: centerB.y + topNormal.y * radiusB
};
const bigBottom = {
x: centerA.x + bottomNormal.x * radiusA,
y: centerA.y + bottomNormal.y * radiusA
};
const smallBottom = {
x: centerB.x + bottomNormal.x * radiusB,
y: centerB.y + bottomNormal.y * radiusB
};
const segments = [
{
type: "line",
start: smallTop,
end: bigTop,
length: Math.hypot(
bigTop.x - smallTop.x,
bigTop.y - smallTop.y
)
},
{
type: "arc",
center: centerA,
radius: radiusA,
startAngle: topAngle,
endAngle: bottomAngle,
length:
radiusA *
Math.abs(
bottomAngle -
topAngle +
(bottomAngle < topAngle ? 0 : -Math.PI * 2)
)
},
{
type: "line",
start: bigBottom,
end: smallBottom,
length: Math.hypot(
smallBottom.x - bigBottom.x,
smallBottom.y - bigBottom.y
)
},
{
type: "arc",
center: centerB,
radius: radiusB,
startAngle: bottomAngle,
endAngle: topAngle - Math.PI * 2,
length:
radiusB *
Math.abs(topAngle - bottomAngle - Math.PI * 2)
}
];
let totalLength = 0;
for (const segment of segments) {
segment.startDistance = totalLength;
totalLength += segment.length;
}
function pointAt(distance) {
let d = ((distance % totalLength) + totalLength) % totalLength;
for (const segment of segments) {
const endDistance = segment.startDistance + segment.length;
if (d <= endDistance || segment === segments[segments.length - 1]) {
const t = THREE.MathUtils.clamp(
(d - segment.startDistance) / segment.length,
0,
1
);
if (segment.type === "line") {
const x = THREE.MathUtils.lerp(
segment.start.x,
segment.end.x,
t
);
const y = THREE.MathUtils.lerp(
segment.start.y,
segment.end.y,
t
);
const tx = segment.end.x - segment.start.x;
const ty = segment.end.y - segment.start.y;
const tangentLength = Math.hypot(tx, ty) || 1;
return {
x,
y,
tx: tx / tangentLength,
ty: ty / tangentLength
};
}
const angle = THREE.MathUtils.lerp(
segment.startAngle,
segment.endAngle,
t
);
const direction = Math.sign(
segment.endAngle - segment.startAngle
);
return {
x: segment.center.x + Math.cos(angle) * segment.radius,
y: segment.center.y + Math.sin(angle) * segment.radius,
tx: direction * -Math.sin(angle),
ty: direction * Math.cos(angle)
};
}
d -= segment.length;
}
return { x: 0, y: 0, tx: 1, ty: 0 };
}
return { segments, totalLength, pointAt };
}
const chainPath = makeChainPath(
bottomBracket,
CHAIN_RING_RADIUS,
rear,
CHAIN_REAR_RADIUS
);
// ---------------------------------------------------------------------
// Animated chain links
// ---------------------------------------------------------------------
const chainLinkCount = 112;
const chainSpacing = chainPath.totalLength / chainLinkCount;
const chainMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.38,
metalness: 0.7
});
const chainLinks = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.075, 0.045, 0.065),
chainMaterial,
chainLinkCount
);
chainLinks.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
chainLinks.castShadow = true;
chainLinks.receiveShadow = true;
scene.add(chainLinks);
const linkColor = new THREE.Color();
for (let i = 0; i < chainLinkCount; i++) {
linkColor.set(i % 2 === 0 ? 0x263437 : 0xc3cfca);
chainLinks.setColorAt(i, linkColor);
}
chainLinks.instanceColor.needsUpdate = true;
const rollerCount = Math.floor(chainLinkCount / 2);
const chainRollers = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.05, 0.05, 0.075),
MAT.metal,
rollerCount
);
chainRollers.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
chainRollers.castShadow = true;
scene.add(chainRollers);
// ---------------------------------------------------------------------
// Full-path motion highlights
// ---------------------------------------------------------------------
const traceMaterial = new THREE.MeshBasicMaterial({
color: 0x00d9d0,
transparent: true,
opacity: 0.9,
depthWrite: false
});
const traceCount = Math.floor(chainPath.totalLength / 0.075);
const chainTrace = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.043, 0.043, 0.043),
traceMaterial,
traceCount
);
const traceDummy = new THREE.Object3D();
for (let i = 0; i < traceCount; i++) {
const point = chainPath.pointAt(
(i / traceCount) * chainPath.totalLength
);
traceDummy.position.set(point.x, point.y, 0.555);
traceDummy.rotation.set(0, 0, Math.atan2(point.ty, point.tx));
traceDummy.scale.set(1, 1, 1);
traceDummy.updateMatrix();
chainTrace.setMatrixAt(i, traceDummy.matrix);
}
chainTrace.instanceMatrix.needsUpdate = true;
chainTrace.renderOrder = 3;
scene.add(chainTrace);
const orbitMaterial = new THREE.MeshBasicMaterial({
color: 0xff9a42,
transparent: true,
opacity: 0.86,
depthWrite: false
});
const orbitCount = 54;
const pedalOrbit = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.052, 0.052, 0.052),
orbitMaterial,
orbitCount
);
for (let i = 0; i < orbitCount; i++) {
const angle = (i / orbitCount) * Math.PI * 2;
traceDummy.position.set(
bottomBracket.x + Math.cos(angle) * 0.94,
bottomBracket.y + Math.sin(angle) * 0.94,
0.665
);
traceDummy.rotation.set(0, 0, angle);
traceDummy.scale.set(1, 1, 1);
traceDummy.updateMatrix();
pedalOrbit.setMatrixAt(i, traceDummy.matrix);
}
pedalOrbit.instanceMatrix.needsUpdate = true;
pedalOrbit.renderOrder = 4;
scene.add(pedalOrbit);
// ---------------------------------------------------------------------
// Camera controls implemented without addons
// ---------------------------------------------------------------------
const cameraControl = {
target: new THREE.Vector3(-1.0, 1.55, 0),
radius: 11.1,
theta: 0.66,
phi: 1.12,
desiredRadius: 11.1,
desiredTheta: 0.66,
desiredPhi: 1.12,
dragging: false,
pointerX: 0,
pointerY: 0
};
function updateCamera() {
cameraControl.radius +=
(cameraControl.desiredRadius - cameraControl.radius) * 0.1;
cameraControl.theta +=
(cameraControl.desiredTheta - cameraControl.theta) * 0.1;
cameraControl.phi +=
(cameraControl.desiredPhi - cameraControl.phi) * 0.1;
const sinPhi = Math.sin(cameraControl.phi);
camera.position.set(
cameraControl.target.x +
cameraControl.radius * sinPhi * Math.sin(cameraControl.theta),
cameraControl.target.y +
cameraControl.radius * Math.cos(cameraControl.phi),
cameraControl.target.z +
cameraControl.radius * sinPhi * Math.cos(cameraControl.theta)
);
camera.lookAt(cameraControl.target);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
cameraControl.dragging = true;
cameraControl.pointerX = event.clientX;
cameraControl.pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!cameraControl.dragging) return;
const dx = event.clientX - cameraControl.pointerX;
const dy = event.clientY - cameraControl.pointerY;
cameraControl.pointerX = event.clientX;
cameraControl.pointerY = event.clientY;
cameraControl.desiredTheta -= dx * 0.006;
cameraControl.desiredPhi = THREE.MathUtils.clamp(
cameraControl.desiredPhi - dy * 0.005,
0.52,
1.46
);
});
function stopDragging(event) {
cameraControl.dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraControl.desiredRadius = THREE.MathUtils.clamp(
cameraControl.desiredRadius + event.deltaY * 0.009,
6.2,
17
);
},
{ passive: false }
);
function resetCamera() {
cameraControl.desiredTheta = 0.66;
cameraControl.desiredPhi = 1.12;
cameraControl.desiredRadius = 11.1;
}
// ---------------------------------------------------------------------
// UI state
// ---------------------------------------------------------------------
const playButton = document.getElementById("playButton");
const traceButton = document.getElementById("traceButton");
const speedInput = document.getElementById("speed");
const speedOutput = document.getElementById("speedOutput");
const statusRpm = document.getElementById("statusRpm");
const pathState = document.getElementById("pathState");
const motionState = document.getElementById("motionState");
const driveLed = document.getElementById("driveLed");
let playing = true;
let traceVisible = true;
let rpm = Number(speedInput.value);
function updateSpeedUI() {
const percentage =
((rpm - Number(speedInput.min)) /
(Number(speedInput.max) - Number(speedInput.min))) *
100;
speedInput.style.background = `linear-gradient(
90deg,
var(--orange) 0%,
var(--orange) ${percentage}%,
rgba(32, 52, 54, 0.16) ${percentage}%
)`;
speedOutput.textContent = `${rpm} RPM`;
statusRpm.textContent = `${rpm} RPM`;
}
speedInput.addEventListener("input", () => {
rpm = Number(speedInput.value);
updateSpeedUI();
});
playButton.addEventListener("click", () => {
playing = !playing;
playButton.textContent = playing ? "Pause" : "Play";
motionState.innerHTML = playing
? '<i class="live-dot"></i>Slow motion'
: "Paused";
driveLed.style.color = playing ? "var(--teal)" : "#9aa7a5";
});
traceButton.addEventListener("click", () => {
traceVisible = !traceVisible;
traceButton.classList.toggle("active", traceVisible);
traceButton.textContent = traceVisible ? "Path: On" : "Path: Off";
pathState.textContent = traceVisible ? "Full / On" : "Hidden";
});
addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
if (event.code === "Space") {
event.preventDefault();
playButton.click();
}
if (event.key.toLowerCase() === "r") {
resetCamera();
}
if (event.key.toLowerCase() === "t") {
traceButton.click();
}
});
updateSpeedUI();
// ---------------------------------------------------------------------
// Resize and animation loop
// ---------------------------------------------------------------------
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.fov = innerWidth < 700 ? 44 : 36;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
}
addEventListener("resize", resize);
resize();
updateCamera();
const clock = new THREE.Clock();
let crankAngle = 0;
const chainDummy = new THREE.Object3D();
const rollerDummy = new THREE.Object3D();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
if (playing) {
crankAngle += delta * (rpm / 60) * Math.PI * 2;
}
// Clockwise rotation as viewed from the drive side.
crankRotor.rotation.z = -crankAngle;
drivePedal.rotation.z = crankAngle;
farPedal.rotation.z = crankAngle;
// The rear sprocket and wheel turn faster than the crank.
rearSpin.rotation.z =
-crankAngle * (CHAIN_RING_RADIUS / CHAIN_REAR_RADIUS);
const chainTravel = crankAngle * CHAIN_RING_RADIUS;
for (let i = 0; i < chainLinkCount; i++) {
const distance =
(i * chainSpacing + chainTravel) % chainPath.totalLength;
const point = chainPath.pointAt(distance);
const rotation = Math.atan2(point.ty, point.tx);
chainDummy.position.set(point.x, point.y, 0.475);
chainDummy.rotation.set(0, 0, rotation);
chainDummy.scale.set(1, 1, 1);
chainDummy.updateMatrix();
chainLinks.setMatrixAt(i, chainDummy.matrix);
if (i % 2 === 0) {
const rollerIndex = i / 2;
rollerDummy.position.set(point.x, point.y, 0.515);
rollerDummy.rotation.set(0, 0, rotation);
rollerDummy.scale.set(1, 1, 1);
rollerDummy.updateMatrix();
chainRollers.setMatrixAt(rollerIndex, rollerDummy.matrix);
}
}
chainLinks.instanceMatrix.needsUpdate = true;
chainRollers.instanceMatrix.needsUpdate = true;
traceMaterial.opacity = traceVisible
? 0.63 + Math.sin(elapsed * 3.1) * 0.2
: 0;
orbitMaterial.opacity = traceVisible
? 0.65 + Math.sin(elapsed * 3.1 + 1.2) * 0.18
: 0;
chainTrace.visible = traceVisible;
pedalOrbit.visible = traceVisible;
updateCamera();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```