Prompt specification
Build or customize the Three.js 3D scene "Pedal-powered model workshop" 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: Pedal-powered model workshop
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/pedal-powered-model-workshop-x0252
Scene Description Prompt:
A low-poly pedal-powered workshop with chain-linked generator and conveyor; interchangeable sprockets change relative speeds. Three.js from cdnjs, one HTML file.
Working Three.js Starter (pedal-powered-model-workshop-x0252):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Pedal Power Workshop</title>
<style>
:root {
--bg: #101923;
--panel: rgba(17, 29, 39, 0.88);
--line: rgba(180, 214, 218, 0.18);
--text: #e8f1ed;
--muted: #9fb8b7;
--accent: #f4a261;
--accent-2: #72c7b5;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.title {
position: absolute;
top: 24px;
left: 28px;
max-width: 420px;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.45);
}
.eyebrow {
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 8px;
font-size: clamp(24px, 4vw, 42px);
line-height: 0.98;
letter-spacing: -0.04em;
}
.subtitle {
margin: 0;
max-width: 360px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.control-card {
position: absolute;
top: 24px;
right: 24px;
width: min(330px, calc(100vw - 48px));
padding: 18px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.3);
backdrop-filter: blur(12px);
pointer-events: auto;
}
.card-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 16px;
font-size: 13px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.status {
display: inline-flex;
align-items: center;
gap: 6px;
color: var(--accent-2);
font-size: 10px;
letter-spacing: 0.08em;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--accent-2);
box-shadow: 0 0 12px var(--accent-2);
animation: pulse 1.4s ease-in-out infinite;
}
@keyframes pulse {
50% {
opacity: 0.35;
transform: scale(0.7);
}
}
.sprocket-row {
margin-top: 14px;
}
.sprocket-label {
display: flex;
justify-content: space-between;
margin-bottom: 7px;
color: var(--muted);
font-size: 11px;
}
.sprocket-label strong {
color: var(--text);
font-weight: 700;
}
.button-row {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.sprocket-button {
border: 1px solid var(--line);
border-radius: 9px;
padding: 9px 5px;
color: var(--muted);
background: rgba(255, 255, 255, 0.035);
cursor: pointer;
font: inherit;
font-size: 12px;
font-weight: 700;
transition: 160ms ease;
}
.sprocket-button:hover {
color: var(--text);
border-color: rgba(244, 162, 97, 0.55);
background: rgba(244, 162, 97, 0.09);
}
.sprocket-button.active {
color: #1b2528;
border-color: var(--accent);
background: var(--accent);
box-shadow: 0 4px 16px rgba(244, 162, 97, 0.2);
}
.readout {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 17px;
}
.metric {
padding: 11px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(0, 0, 0, 0.12);
}
.metric small {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 10px;
text-transform: uppercase;
letter-spacing: 0.08em;
}
.metric strong {
color: var(--accent-2);
font-size: 19px;
letter-spacing: -0.04em;
}
.hint {
position: absolute;
bottom: 22px;
left: 28px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 9px;
color: var(--muted);
background: rgba(17, 29, 39, 0.62);
font-size: 11px;
backdrop-filter: blur(8px);
}
.reset {
position: absolute;
right: 24px;
bottom: 22px;
padding: 9px 13px;
border: 1px solid var(--line);
border-radius: 9px;
color: var(--text);
background: rgba(17, 29, 39, 0.72);
cursor: pointer;
font: inherit;
font-size: 11px;
pointer-events: auto;
}
.reset:hover {
border-color: var(--accent);
}
@media (max-width: 680px) {
.title {
top: 16px;
left: 16px;
}
.subtitle {
display: none;
}
.control-card {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
padding: 13px;
}
.hint {
display: none;
}
.reset {
right: 12px;
bottom: 190px;
}
}
</style>
</head>
<body>
<canvas id="scene"></canvas>
<div class="hud">
<div class="title">
<div class="eyebrow">Mechanical study / 07</div>
<h1>Pedal Power<br />Workshop</h1>
<p class="subtitle">
A low-poly bicycle drives a chain generator. Swap the sprockets to
trade torque for conveyor speed.
</p>
</div>
<div class="control-card">
<div class="card-title">
<span>Sprocket exchange</span>
<span class="status"><i class="status-dot"></i>RUNNING</span>
</div>
<div class="sprocket-row">
<div class="sprocket-label">
<span>Generator / drive sprocket</span>
<strong id="generator-label">18T</strong>
</div>
<div class="button-row">
<button class="sprocket-button" data-kind="generator" data-teeth="12">12T</button>
<button class="sprocket-button active" data-kind="generator" data-teeth="18">18T</button>
<button class="sprocket-button" data-kind="generator" data-teeth="24">24T</button>
</div>
</div>
<div class="sprocket-row">
<div class="sprocket-label">
<span>Conveyor / driven sprocket</span>
<strong id="conveyor-label">12T</strong>
</div>
<div class="button-row">
<button class="sprocket-button active" data-kind="conveyor" data-teeth="12">12T</button>
<button class="sprocket-button" data-kind="conveyor" data-teeth="18">18T</button>
<button class="sprocket-button" data-kind="conveyor" data-teeth="24">24T</button>
</div>
</div>
<div class="readout">
<div class="metric">
<small>Relative ratio</small>
<strong id="ratio-readout">1.50×</strong>
</div>
<div class="metric">
<small>Conveyor speed</small>
<strong id="speed-readout">2.7 rpm</strong>
</div>
</div>
</div>
<div class="hint">Drag to orbit · Scroll to zoom · Click sprockets to exchange gears</div>
<button class="reset" id="reset-camera">Reset camera</button>
</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, camera
// ------------------------------------------------------------
const canvas = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
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;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x101923);
scene.fog = new THREE.Fog(0x101923, 18, 34);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
// Custom camera controls: no external controls module is used.
const cameraTarget = new THREE.Vector3(-0.25, 1.15, 0.1);
let cameraTheta = 0.78;
let cameraPhi = 0.92;
let cameraRadius = 14.3;
function updateCamera() {
const offset = new THREE.Vector3().setFromSphericalCoords(
cameraRadius,
cameraPhi,
cameraTheta
);
camera.position.copy(cameraTarget).add(offset);
camera.lookAt(cameraTarget);
}
updateCamera();
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(cameraPhi + dy * 0.005, 0.32, 1.25);
updateCamera();
});
canvas.addEventListener("pointerup", (event) => {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", () => {
dragging = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.012,
8.5,
23
);
updateCamera();
},
{ passive: false }
);
document.getElementById("reset-camera").addEventListener("click", () => {
cameraTheta = 0.78;
cameraPhi = 0.92;
cameraRadius = 14.3;
updateCamera();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// ------------------------------------------------------------
// Materials and reusable geometry helpers
// ------------------------------------------------------------
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.08,
flatShading: true,
...options
});
}
const MAT = {
floor: material(0x25333a),
wall: material(0x304149),
wallDark: material(0x1d2b33),
wood: material(0x9c6945),
woodLight: material(0xc28b5a),
metal: material(0x78909a, { metalness: 0.65, roughness: 0.38 }),
darkMetal: material(0x26343b, { metalness: 0.6, roughness: 0.42 }),
black: material(0x10171b),
rubber: material(0x12191d),
orange: material(0xe77d36),
yellow: material(0xf0b84d),
teal: material(0x4aa99a),
blue: material(0x527fa0),
cream: material(0xe3d6b5),
red: material(0xc94d4d),
led: material(0x73f0c2, {
emissive: 0x38d99f,
emissiveIntensity: 1.8,
roughness: 0.25
})
};
function addMesh(geometry, mat, x = 0, y = 0, z = 0, parent = scene) {
const mesh = new THREE.Mesh(geometry, mat);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function box(w, h, d, mat, x, y, z, parent = scene) {
return addMesh(new THREE.BoxGeometry(w, h, d), mat, x, y, z, parent);
}
function cylinder(
radius,
height,
mat,
x,
y,
z,
parent = scene,
segments = 12,
axis = "y"
) {
const mesh = addMesh(
new THREE.CylinderGeometry(radius, radius, height, segments),
mat,
x,
y,
z,
parent
);
if (axis === "z") mesh.rotation.x = Math.PI / 2;
if (axis === "x") mesh.rotation.z = Math.PI / 2;
return mesh;
}
function beam(a, b, radius, mat, parent = scene, segments = 6) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const length = direction.length();
const mesh = addMesh(
new THREE.CylinderGeometry(radius, radius, length, segments),
mat,
(start.x + end.x) / 2,
(start.y + end.y) / 2,
(start.z + end.z) / 2,
parent
);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
return mesh;
}
function torus(radius, tube, mat, x, y, z, parent = scene) {
return addMesh(
new THREE.TorusGeometry(radius, tube, 8, 16),
mat,
x,
y,
z,
parent
);
}
// ------------------------------------------------------------
// Workshop environment
// ------------------------------------------------------------
box(18, 0.25, 12, MAT.floor, 0, -0.15, 0);
box(18, 6.5, 0.25, MAT.wall, 0, 3.1, -4.65);
box(0.25, 6.5, 10, MAT.wallDark, -8.8, 3.1, 0);
// Floor expansion strips.
for (let x = -8; x <= 8; x += 2) {
box(0.025, 0.012, 10, MAT.darkMetal, x, -0.015, 0);
}
for (let z = -4; z <= 4; z += 2) {
box(16, 0.012, 0.025, MAT.darkMetal, 0, -0.014, z);
}
// Back wall framing.
for (let x = -8; x <= 8; x += 2.7) {
box(0.11, 6.3, 0.12, MAT.darkMetal, x, 3.1, -4.48);
}
box(17.6, 0.13, 0.14, MAT.darkMetal, 0, 5.95, -4.45);
// Pegboard.
box(3.8, 2.25, 0.1, MAT.wood, -0.4, 3.45, -4.43);
const pegMat = material(0x563c2e);
for (let row = 0; row < 5; row++) {
for (let col = 0; col < 8; col++) {
cylinder(
0.035,
0.035,
pegMat,
-2.05 + col * 0.46,
2.55 + row * 0.43,
-4.36,
scene,
8,
"z"
);
}
}
// A few simple hanging tools.
beam([-1.45, 4.25, -4.25], [-1.45, 3.48, -4.25], 0.055, MAT.metal);
torus(0.16, 0.045, MAT.metal, -1.45, 3.43, -4.24);
box(0.18, 0.56, 0.1, MAT.yellow, -0.45, 3.75, -4.22);
box(0.42, 0.12, 0.1, MAT.yellow, -0.45, 4.0, -4.22);
beam([0.35, 4.2, -4.22], [0.35, 3.55, -4.22], 0.045, MAT.red);
box(0.38, 0.13, 0.12, MAT.red, 0.35, 3.52, -4.22);
// Shelf and storage boxes.
box(3.3, 0.13, 0.72, MAT.woodLight, 3.7, 4.45, -4.1);
box(0.82, 0.65, 0.58, MAT.blue, 2.65, 4.83, -4.08);
box(0.82, 0.65, 0.58, MAT.orange, 3.65, 4.83, -4.08);
box(0.82, 0.65, 0.58, MAT.teal, 4.65, 4.83, -4.08);
// Window.
box(3.3, 1.65, 0.12, MAT.darkMetal, 5.7, 3.25, -4.43);
box(3.04, 1.4, 0.08, material(0x88bac2, {
emissive: 0x376b79,
emissiveIntensity: 0.55,
roughness: 0.4
}), 5.7, 3.25, -4.34);
box(0.07, 1.45, 0.1, MAT.darkMetal, 5.7, 3.25, -4.25);
box(3.1, 0.07, 0.1, MAT.darkMetal, 5.7, 3.25, -4.25);
// Overhead lamps.
for (const x of [-4.5, 0, 4.5]) {
beam([x, 6.0, -0.4], [x, 5.45, -0.4], 0.025, MAT.darkMetal);
cylinder(0.38, 0.22, MAT.darkMetal, x, 5.35, -0.4, scene, 10);
const bulb = addMesh(
new THREE.SphereGeometry(0.12, 8, 6),
material(0xffd27f, {
emissive: 0xffa53d,
emissiveIntensity: 2.4
}),
x,
5.19,
-0.4
);
bulb.castShadow = false;
}
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xadd8dd, 0x17212a, 1.7));
const keyLight = new THREE.DirectionalLight(0xffe0b5, 3.1);
keyLight.position.set(-5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 30;
scene.add(keyLight);
const workshopLight = new THREE.PointLight(0xffbb70, 35, 16, 2);
workshopLight.position.set(0, 5, 2);
scene.add(workshopLight);
const cyanLight = new THREE.PointLight(0x65d7d0, 18, 11, 2);
cyanLight.position.set(5, 2.6, 1);
scene.add(cyanLight);
// ------------------------------------------------------------
// Conveyor
// ------------------------------------------------------------
const conveyor = new THREE.Group();
scene.add(conveyor);
// Side rails and supports.
box(9.7, 0.18, 0.12, MAT.metal, 0, 0.88, -0.72, conveyor);
box(9.7, 0.18, 0.12, MAT.metal, 0, 0.88, 0.72, conveyor);
for (const x of [-4.25, -2.1, 0, 2.1, 4.25]) {
box(0.13, 0.85, 0.13, MAT.darkMetal, x, 0.43, -0.65, conveyor);
box(0.13, 0.85, 0.13, MAT.darkMetal, x, 0.43, 0.65, conveyor);
box(0.22, 0.08, 1.45, MAT.darkMetal, x, 0.04, 0, conveyor);
}
// Belt bed.
box(9.65, 0.16, 1.23, MAT.rubber, 0, 1.0, 0, conveyor);
// Rollers.
for (let i = 0; i < 20; i++) {
const x = -4.55 + i * 0.48;
cylinder(0.13, 1.42, MAT.metal, x, 0.98, 0, conveyor, 12, "z");
}
// Moving belt slats.
const slats = [];
for (let i = 0; i < 48; i++) {
const baseX = -4.65 + i * 0.2;
const slat = box(0.095, 0.035, 1.13, MAT.darkMetal, baseX, 1.105, 0, conveyor);
slat.userData.baseX = baseX;
slats.push(slat);
}
// Cargo crates.
const cargo = [];
const cargoColors = [MAT.wood, MAT.teal, MAT.orange, MAT.blue];
for (let i = 0; i < 4; i++) {
const crate = new THREE.Group();
const baseX = -3.55 + i * 2.15;
crate.position.set(baseX, 1.12, 0);
crate.userData.baseX = baseX;
box(0.72, 0.48, 0.7, cargoColors[i], 0, 0.25, 0, crate);
box(0.77, 0.055, 0.75, MAT.woodLight, 0, 0.48, 0, crate);
box(0.09, 0.5, 0.74, MAT.woodLight, 0, 0.25, 0, crate);
conveyor.add(crate);
cargo.push(crate);
}
// ------------------------------------------------------------
// Bicycle / pedal power
// ------------------------------------------------------------
const bike = new THREE.Group();
scene.add(bike);
const rearWheel = new THREE.Group();
rearWheel.position.set(-5.65, 0.68, 0.65);
bike.add(rearWheel);
const frontWheel = new THREE.Group();
frontWheel.position.set(-3.95, 0.68, 0.65);
bike.add(frontWheel);
function buildWheel(wheel) {
torus(0.55, 0.075, MAT.rubber, 0, 0, 0, wheel);
cylinder(0.1, 0.16, MAT.metal, 0, 0, 0, wheel, 10, "z");
for (let i = 0; i < 8; i++) {
const angle = (i / 8) * Math.PI * 2;
beam(
[0, 0, 0],
[Math.cos(angle) * 0.49, Math.sin(angle) * 0.49, 0],
0.014,
MAT.metal,
wheel,
4
);
}
}
buildWheel(rearWheel);
buildWheel(frontWheel);
const frameMat = MAT.teal;
beam([-5.65, 0.68, 0.7], [-4.78, 0.87, 0.7], 0.055, frameMat, bike);
beam([-4.78, 0.87, 0.7], [-5.08, 1.55, 0.7], 0.055, frameMat, bike);
beam([-5.08, 1.55, 0.7], [-5.65, 0.68, 0.7], 0.055, frameMat, bike);
beam([-4.78, 0.87, 0.7], [-4.25, 1.35, 0.7], 0.055, frameMat, bike);
beam([-4.25, 1.35, 0.7], [-5.08, 1.55, 0.7], 0.055, frameMat, bike);
beam([-4.25, 1.35, 0.7], [-3.95, 0.68, 0.7], 0.055, frameMat, bike);
// Handlebars and saddle.
beam([-4.22, 1.35, 0.7], [-4.1, 1.78, 0.7], 0.04, MAT.metal, bike);
beam([-4.1, 1.78, 0.38], [-4.1, 1.78, 1.02], 0.04, MAT.darkMetal, bike);
box(0.45, 0.1, 0.28, MAT.black, -5.08, 1.61, 0.7, bike);
// Crank assembly.
const crank = new THREE.Group();
crank.position.set(-4.78, 0.87, 1.15);
scene.add(crank);
cylinder(0.1, 0.25, MAT.metal, 0, 0, 0, crank, 10, "z");
beam([0, 0, 0.13], [0.25, 0.12, 0.13], 0.035, MAT.darkMetal, crank);
beam([0, 0, 0.13], [-0.25, -0.12, 0.13], 0.035, MAT.darkMetal, crank);
box(0.22, 0.07, 0.15, MAT.black, 0.25, 0.12, 0.23, crank);
box(0.22, 0.07, 0.15, MAT.black, -0.25, -0.12, 0.23, crank);
// ------------------------------------------------------------
// Generator
// ------------------------------------------------------------
box(1.65, 1.28, 1.2, MAT.orange, -2.8, 1.05, 0.42);
box(1.82, 0.16, 1.34, MAT.darkMetal, -2.8, 0.38, 0.42);
box(1.5, 0.13, 1.12, MAT.metal, -2.8, 1.73, 0.42);
// Vent slots.
for (let i = 0; i < 5; i++) {
box(0.07, 0.46, 0.025, MAT.darkMetal, -3.28 + i * 0.22, 0.93, 1.035);
}
// Gauge.
cylinder(0.19, 0.07, MAT.cream, -2.55, 1.52, 1.04, scene, 12, "z");
beam([-2.55, 1.52, 1.09], [-2.48, 1.61, 1.09], 0.016, MAT.red);
// Indicator light.
const generatorLed = addMesh(
new THREE.SphereGeometry(0.085, 8, 6),
MAT.led,
-3.25,
1.55,
1.07
);
// Generator feet.
for (const x of [-3.42, -2.18]) {
box(0.28, 0.18, 1.5, MAT.black, x, 0.13, 0.42);
}
// ------------------------------------------------------------
// Sprockets
// ------------------------------------------------------------
function sprocketRadius(teeth) {
return 0.14 + teeth * 0.035;
}
function createSprocket(teeth, mat, radius = sprocketRadius(teeth)) {
const group = new THREE.Group();
const disc = cylinder(
radius * 0.78,
0.14,
mat,
0,
0,
0,
group,
Math.max(10, teeth),
"z"
);
disc.castShadow = true;
torus(radius * 0.64, Math.max(0.025, radius * 0.055), MAT.darkMetal, 0, 0, 0.08, group);
for (let i = 0; i < teeth; i++) {
const angle = (i / teeth) * Math.PI * 2;
const tooth = box(
radius * 0.28,
radius * 0.22,
0.14,
mat,
Math.cos(angle) * radius * 0.91,
Math.sin(angle) * radius * 0.91,
0,
group
);
tooth.rotation.z = angle;
}
cylinder(radius * 0.23, 0.24, MAT.darkMetal, 0, 0, 0, group, 10, "z");
group.userData.pitchRadius = radius;
group.userData.teeth = teeth;
return group;
}
// Fixed crank sprocket and generator input sprocket.
const crankSprocket = createSprocket(12, MAT.yellow, 0.34);
crank.add(crankSprocket);
const inputSprocket = createSprocket(10, MAT.metal, 0.3);
inputSprocket.position.set(-3.1, 1.12, 1.15);
scene.add(inputSprocket);
let generatorTeeth = 18;
let conveyorTeeth = 12;
let generatorSprocket = createSprocket(generatorTeeth, MAT.orange);
generatorSprocket.position.set(-2.45, 1.12, 1.35);
scene.add(generatorSprocket);
let conveyorSprocket = createSprocket(conveyorTeeth, MAT.teal);
conveyorSprocket.position.set(3.6, 1.12, 1.35);
scene.add(conveyorSprocket);
// Axles.
cylinder(0.11, 0.55, MAT.metal, -2.45, 1.12, 1.13, scene, 10, "z");
cylinder(0.11, 1.55, MAT.metal, 3.6, 1.12, 0.65, scene, 10, "z");
// ------------------------------------------------------------
// Procedural chain paths and links
// ------------------------------------------------------------
function createChainPath(centerA, centerB, radiusA, radiusB) {
const dx = centerB.x - centerA.x;
const dy = centerB.y - centerA.y;
const distance = Math.hypot(dx, dy);
const ux = dx / distance;
const uy = dy / distance;
const px = -uy;
const py = ux;
const delta = (radiusA - radiusB) / distance;
const h = Math.sqrt(Math.max(0, 1 - delta * delta));
const topX = ux * delta + px * h;
const topY = uy * delta + py * h;
const bottomX = ux * delta - px * h;
const bottomY = uy * delta - py * h;
const z = centerA.z;
const aTop = new THREE.Vector3(
centerA.x + topX * radiusA,
centerA.y + topY * radiusA,
z
);
const bTop = new THREE.Vector3(
centerB.x + topX * radiusB,
centerB.y + topY * radiusB,
z
);
const aBottom = new THREE.Vector3(
centerA.x + bottomX * radiusA,
centerA.y + bottomY * radiusA,
z
);
const bBottom = new THREE.Vector3(
centerB.x + bottomX * radiusB,
centerB.y + bottomY * radiusB,
z
);
const points = [];
function addLine(from, to, steps) {
for (let i = 0; i < steps; i++) {
points.push(from.clone().lerp(to, i / steps));
}
}
function addArc(center, radius, startAngle, endAngle, steps) {
for (let i = 0; i < steps; i++) {
const t = i / steps;
const angle = startAngle + (endAngle - startAngle) * t;
points.push(
new THREE.Vector3(
center.x + Math.cos(angle) * radius,
center.y + Math.sin(angle) * radius,
z
)
);
}
}
const topAngle = Math.atan2(topY, topX);
const bottomAngle = Math.atan2(bottomY, bottomX);
addLine(aTop, bTop, 34);
addArc(centerB, radiusB, topAngle, bottomAngle, 26);
addLine(bBottom, aBottom, 34);
addArc(centerA, radiusA, bottomAngle, topAngle - Math.PI * 2, 26);
points.push(points[0].clone());
const lengths = [0];
let totalLength = 0;
for (let i = 1; i < points.length; i++) {
totalLength += points[i].distanceTo(points[i - 1]);
lengths.push(totalLength);
}
return { points, lengths, totalLength };
}
function createChain(centerA, centerB, radiusA, radiusB, linkCount, mat) {
const path = createChainPath(centerA, centerB, radiusA, radiusB);
const group = new THREE.Group();
const links = [];
for (let i = 0; i < linkCount; i++) {
const link = box(0.135, 0.08, 0.065, mat, 0, 0, 0, group);
link.castShadow = true;
links.push(link);
}
scene.add(group);
return {
group,
links,
path,
linkCount
};
}
function sampleChain(chain, u) {
const { points, lengths, totalLength } = chain.path;
const distance = ((u % 1) + 1) % 1 * totalLength;
let low = 0;
let high = lengths.length - 1;
while (low < high - 1) {
const mid = Math.floor((low + high) / 2);
if (lengths[mid] <= distance) low = mid;
else high = mid;
}
const segmentLength = lengths[high] - lengths[low] || 1;
const t = (distance - lengths[low]) / segmentLength;
const position = points[low].clone().lerp(points[high], t);
const tangent = points[high].clone().sub(points[low]).normalize();
return { position, tangent };
}
function updateChain(chain, travel) {
const offset = travel / chain.path.totalLength;
chain.links.forEach((link, i) => {
const u = (i / chain.linkCount + offset) % 1;
const sample = sampleChain(chain, u);
link.position.copy(sample.position);
link.rotation.z = Math.atan2(sample.tangent.y, sample.tangent.x);
});
}
const inputChain = createChain(
new THREE.Vector3(-4.78, 0.87, 1.15),
new THREE.Vector3(-3.1, 1.12, 1.15),
0.34,
0.3,
44,
MAT.darkMetal
);
let outputChain = createChain(
generatorSprocket.position.clone(),
conveyorSprocket.position.clone(),
generatorSprocket.userData.pitchRadius,
conveyorSprocket.userData.pitchRadius,
76,
MAT.darkMetal
);
function rebuildOutputChain() {
scene.remove(outputChain.group);
outputChain = createChain(
generatorSprocket.position.clone(),
conveyorSprocket.position.clone(),
generatorSprocket.userData.pitchRadius,
conveyorSprocket.userData.pitchRadius,
76,
MAT.darkMetal
);
}
// ------------------------------------------------------------
// Sprocket controls
// ------------------------------------------------------------
const ratioReadout = document.getElementById("ratio-readout");
const speedReadout = document.getElementById("speed-readout");
const generatorLabel = document.getElementById("generator-label");
const conveyorLabel = document.getElementById("conveyor-label");
const driveOmega = 1.8;
function updateReadout() {
const ratio = generatorTeeth / conveyorTeeth;
ratioReadout.textContent = `${ratio.toFixed(2)}×`;
speedReadout.textContent = `${(driveOmega * ratio * 9.55).toFixed(1)} rpm`;
generatorLabel.textContent = `${generatorTeeth}T`;
conveyorLabel.textContent = `${conveyorTeeth}T`;
}
function updateButtonStates() {
document.querySelectorAll(".sprocket-button").forEach((button) => {
const kind = button.dataset.kind;
const teeth = Number(button.dataset.teeth);
const selected = kind === "generator"
? generatorTeeth === teeth
: conveyorTeeth === teeth;
button.classList.toggle("active", selected);
button.setAttribute("aria-pressed", selected ? "true" : "false");
});
}
function exchangeSprocket(kind, teeth) {
if (kind === "generator") {
generatorTeeth = teeth;
const replacement = createSprocket(generatorTeeth, MAT.orange);
replacement.position.copy(generatorSprocket.position);
scene.remove(generatorSprocket);
generatorSprocket = replacement;
scene.add(generatorSprocket);
} else {
conveyorTeeth = teeth;
const replacement = createSprocket(conveyorTeeth, MAT.teal);
replacement.position.copy(conveyorSprocket.position);
scene.remove(conveyorSprocket);
conveyorSprocket = replacement;
scene.add(conveyorSprocket);
}
rebuildOutputChain();
updateButtonStates();
updateReadout();
}
document.querySelectorAll(".sprocket-button").forEach((button) => {
button.addEventListener("click", () => {
exchangeSprocket(
button.dataset.kind,
Number(button.dataset.teeth)
);
});
});
updateButtonStates();
updateReadout();
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
let crankAngle = 0;
let conveyorAngle = 0;
let inputChainTravel = 0;
let outputChainTravel = 0;
let beltTravel = 0;
function wrap(value, min, max) {
const range = max - min;
return ((((value - min) % range) + range) % range) + min;
}
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
// Pedal input.
crankAngle += driveOmega * dt;
crank.rotation.z = crankAngle;
// The input chain transmits pedal rotation to the generator.
inputChainTravel += driveOmega * 0.34 * dt;
inputSprocket.rotation.z = -inputChainTravel / inputSprocket.userData.pitchRadius;
// Generator output and conveyor ratio.
const currentRatio = generatorTeeth / conveyorTeeth;
const currentConveyorOmega = driveOmega * currentRatio;
generatorSprocket.rotation.z = -crankAngle;
conveyorAngle += currentConveyorOmega * dt;
conveyorSprocket.rotation.z = -conveyorAngle;
outputChainTravel +=
driveOmega * generatorSprocket.userData.pitchRadius * dt;
updateChain(inputChain, inputChainTravel);
updateChain(outputChain, outputChainTravel);
// Conveyor belt movement.
beltTravel += currentConveyorOmega * 0.39 * dt;
for (const slat of slats) {
slat.position.x = wrap(slat.userData.baseX + beltTravel, -4.68, 4.68);
}
for (const crate of cargo) {
crate.position.x = wrap(crate.userData.baseX + beltTravel, -4.1, 4.1);
}
generatorLed.material.emissiveIntensity =
1.45 + Math.sin(elapsed * 6) * 0.35;
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```