Prompt specification
Build or customize the Three.js 3D scene "Piston rod joined to a crank disc" 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: Piston rod joined to a crank disc
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/piston-rod-joined-to-a-crank-disc-x2537
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing a piston rod connected to an offset crank pin and flywheel. Animate one revolution while keeping the rod endpoints attached throughout the motion. Voxel style.
Working Three.js Starter (piston-rod-joined-to-a-crank-disc-x2537):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Slider-Crank Mechanism</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(12, 18, 32, 0.86);
--border: rgba(148, 163, 184, 0.25);
--text: #e5edf8;
--muted: #91a2ba;
--accent: #ffb547;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #090e19;
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
canvas.grabbing {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
background: var(--panel);
border: 1px solid var(--border);
border-radius: 14px;
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
#title-panel {
top: 20px;
left: 20px;
padding: 15px 17px;
max-width: min(360px, calc(100vw - 40px));
}
h1 {
margin: 0 0 5px;
font-size: 17px;
line-height: 1.2;
letter-spacing: 0.02em;
}
#subtitle {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
#controls {
left: 20px;
bottom: 20px;
width: min(410px, calc(100vw - 40px));
padding: 14px;
}
.button-row {
display: flex;
gap: 8px;
margin-bottom: 12px;
}
button {
appearance: none;
border: 1px solid var(--border);
border-radius: 8px;
padding: 8px 12px;
color: var(--text);
background: rgba(255, 255, 255, 0.055);
font: inherit;
font-size: 12px;
font-weight: 650;
cursor: pointer;
}
button:hover {
background: rgba(255, 255, 255, 0.11);
border-color: rgba(255, 255, 255, 0.34);
}
#play {
min-width: 76px;
color: #211506;
background: var(--accent);
border-color: var(--accent);
}
.timeline-label {
display: flex;
justify-content: space-between;
gap: 12px;
margin-bottom: 7px;
color: var(--muted);
font-size: 11px;
font-variant-numeric: tabular-nums;
}
#turn {
color: var(--text);
}
input[type="range"] {
width: 100%;
margin: 0;
accent-color: var(--accent);
cursor: pointer;
}
#help {
position: fixed;
z-index: 10;
right: 20px;
bottom: 20px;
padding: 9px 12px;
color: var(--muted);
font-size: 11px;
pointer-events: none;
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
color: var(--muted);
background: #090e19;
font-size: 14px;
}
@media (max-width: 650px) {
#title-panel {
top: 12px;
left: 12px;
padding: 12px 14px;
}
#controls {
left: 12px;
bottom: 12px;
width: calc(100vw - 24px);
}
#help {
display: none;
}
h1 {
font-size: 15px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<section id="title-panel" class="panel">
<h1>Voxel Slider-Crank</h1>
<p id="subtitle">
A fixed-length connecting rod drives a piston from an offset crank pin.
The motion is constrained analytically for exact endpoint attachment.
</p>
</section>
<section id="controls" class="panel">
<div class="button-row">
<button id="play" type="button">Pause</button>
<button id="restart" type="button">Restart</button>
<button id="camera-reset" type="button">Reset camera</button>
</div>
<div class="timeline-label">
<span>One revolution</span>
<span id="turn">0° / 360°</span>
</div>
<input
id="scrub"
type="range"
min="0"
max="1000"
value="0"
step="1"
aria-label="Animation position"
/>
</section>
<div id="help" class="panel">Drag to orbit · Scroll to zoom</div>
<div id="loading">Loading Three.js…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const loading = document.getElementById("loading");
const playButton = document.getElementById("play");
const restartButton = document.getElementById("restart");
const resetCameraButton = document.getElementById("camera-reset");
const scrubber = document.getElementById("scrub");
const turnLabel = document.getElementById("turn");
const TAU = Math.PI * 2;
const DURATION = 9000;
// Mechanism dimensions, in world units.
const CRANK_CENTER_X = -2.25;
const CRANK_RADIUS = 0.92;
const CONNECTING_ROD_LENGTH = 3.45;
const ROD_Z = 0.5;
// Scene and renderer.
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x090e19);
scene.fog = new THREE.Fog(0x090e19, 15, 28);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel flywheel and piston mechanism"
);
sceneHost.appendChild(renderer.domElement);
// Orthographic camera gives the mechanism a clean, toy-like voxel look.
const camera = new THREE.OrthographicCamera(-5, 5, 3, -3, 0.1, 100);
const cameraTarget = new THREE.Vector3(-0.35, -0.08, 0);
let cameraAzimuth = 0.22;
let cameraElevation = 0.2;
let cameraZoom = 1;
// Lighting.
const hemisphere = new THREE.HemisphereLight(
0xaec9ff,
0x17120d,
1.65
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff0d2, 3.2);
keyLight.position.set(-4, 8, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x5b8cff, 1.4);
rimLight.position.set(5, 2, -6);
scene.add(rimLight);
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.16,
flatShading: true,
...options
});
}
const materials = {
wheel: material(0x26364a),
wheelRim: material(0x3e5871),
crank: material(0xe9682d),
rod: material(0xd4dde8, { metalness: 0.34 }),
rodDark: material(0x718197, { metalness: 0.38 }),
pin: material(0xffc857, {
emissive: 0x5c3100,
emissiveIntensity: 0.32,
metalness: 0.25
}),
piston: material(0x29a99b),
pistonDark: material(0x176b67),
cage: material(0x4f7182, { metalness: 0.45 }),
shell: material(0x6d9aaa, {
transparent: true,
opacity: 0.055,
depthWrite: false,
side: THREE.DoubleSide
}),
support: material(0x202a38),
base: material(0x111a28),
grid: material(0x29384b),
axis: material(0x234e5a)
};
function addBox(parent, width, height, depth, mat, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Voxel platform and grid.
addBox(
scene,
7.55,
0.14,
3.05,
materials.base,
-0.45,
-1.62,
0
);
for (let x = -4; x <= 3.01; x += 0.5) {
addBox(scene, 0.018, 0.012, 2.8, materials.grid, x, -1.542, 0);
}
for (let z = -1.4; z <= 1.41; z += 0.5) {
addBox(scene, 7.4, 0.012, 0.018, materials.grid, -0.45, -1.54, z);
}
const shadowFloor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.ShadowMaterial({ opacity: 0.25 })
);
shadowFloor.rotation.x = -Math.PI / 2;
shadowFloor.position.y = -1.705;
shadowFloor.receiveShadow = true;
scene.add(shadowFloor);
// Flywheel support.
addBox(
scene,
0.7,
1.24,
0.68,
materials.support,
CRANK_CENTER_X,
-0.92,
-0.43
);
addBox(
scene,
0.82,
0.82,
0.48,
materials.support,
CRANK_CENTER_X,
0,
-0.42
);
// Main flywheel group rotates around the Z axis.
const flywheel = new THREE.Group();
flywheel.position.set(CRANK_CENTER_X, 0, 0);
scene.add(flywheel);
const flywheelRadius = 1.28;
// Outer voxel rim.
const rimBlocks = 40;
for (let i = 0; i < rimBlocks; i++) {
const angle = (i / rimBlocks) * TAU;
const rim = addBox(
flywheel,
0.25,
0.22,
0.38,
materials.wheelRim,
Math.cos(angle) * flywheelRadius,
Math.sin(angle) * flywheelRadius,
0
);
rim.rotation.z = angle;
}
// Six voxel spokes.
for (let i = 0; i < 6; i++) {
const angle = (i / 6) * TAU;
const spoke = new THREE.Group();
spoke.rotation.z = angle;
flywheel.add(spoke);
for (
let distance = 0.38;
distance < flywheelRadius - 0.15;
distance += 0.22
) {
addBox(
spoke,
0.23,
0.18,
0.3,
materials.wheel,
distance,
0,
0
);
}
}
// Central hub and axle.
addBox(
flywheel,
0.66,
0.66,
0.5,
materials.wheel,
0,
0,
0
);
addBox(
flywheel,
0.34,
0.34,
0.72,
materials.pistonDark,
0,
0,
0.1
);
// Segmented orange crank arm.
const crankSegments = 6;
for (let i = 0; i < crankSegments; i++) {
const distance = ((i + 0.5) / crankSegments) * CRANK_RADIUS;
addBox(
flywheel,
0.18,
0.18,
0.44,
materials.crank,
distance,
0,
0.04
);
}
// Offset crank pin. Its Z depth reaches the connecting-rod plane.
addBox(
flywheel,
0.34,
0.34,
0.78,
materials.pin,
CRANK_RADIUS,
0,
0.38
);
addBox(
flywheel,
0.42,
0.42,
0.12,
materials.rodDark,
CRANK_RADIUS,
0,
0.79
);
// Slider-axis guide.
addBox(
scene,
5.8,
0.026,
0.026,
materials.axis,
-0.12,
0,
-0.24
);
// Fixed, voxel-style cylinder cage.
const cylinderCage = new THREE.Group();
scene.add(cylinderCage);
const cageStartX = 0.1;
const cageEndX = 3.0;
const cageLength = cageEndX - cageStartX;
const cageCenterX = (cageStartX + cageEndX) * 0.5;
addBox(
cylinderCage,
cageLength,
1.38,
1.18,
materials.shell,
cageCenterX,
0,
0.28
);
// Longitudinal corner rails.
for (const y of [-0.65, 0.65]) {
for (const z of [-0.52, 0.52]) {
addBox(
cylinderCage,
cageLength,
0.075,
0.075,
materials.cage,
cageCenterX,
y,
z + 0.28
);
}
}
// Square-section ribs give the housing a blocky cylindrical appearance.
const ribPositions = [0.1, 0.68, 1.26, 1.84, 2.42, 3.0];
for (const x of ribPositions) {
for (const z of [-0.52, 0.52]) {
addBox(
cylinderCage,
0.075,
1.38,
0.075,
materials.cage,
x,
0,
z + 0.28
);
}
for (const y of [-0.65, 0.65]) {
addBox(
cylinderCage,
0.075,
0.075,
1.12,
materials.cage,
x,
y,
0.28
);
}
}
// Connecting rod, built from individual voxel segments.
const connectingRod = new THREE.Group();
connectingRod.position.z = ROD_Z;
scene.add(connectingRod);
const rodSegments = 15;
for (let i = 0; i < rodSegments; i++) {
const x =
-CONNECTING_ROD_LENGTH * 0.5 +
(i / (rodSegments - 1)) * CONNECTING_ROD_LENGTH;
addBox(
connectingRod,
0.255,
i % 2 === 0 ? 0.18 : 0.14,
0.18,
i % 2 === 0 ? materials.rod : materials.rodDark,
x,
0,
0
);
}
// Exact endpoint joints.
addBox(
connectingRod,
0.35,
0.35,
0.35,
materials.pin,
CONNECTING_ROD_LENGTH * 0.5,
0,
0
);
addBox(
connectingRod,
0.35,
0.35,
0.35,
materials.pin,
-CONNECTING_ROD_LENGTH * 0.5,
0,
0
);
// Piston and crosshead. The wrist-pin center is the slider endpoint.
const piston = new THREE.Group();
piston.position.z = ROD_Z;
scene.add(piston);
addBox(
piston,
0.58,
0.72,
0.64,
materials.piston,
0.32,
0,
-0.04
);
addBox(
piston,
0.17,
0.78,
0.69,
materials.pistonDark,
0.67,
0,
-0.04
);
addBox(
piston,
0.29,
0.29,
0.42,
materials.pin,
0,
0,
0
);
/*
Exact slider-crank solution:
P = crank pin:
Px = centerX + r cos(theta)
Py = r sin(theta)
The piston is constrained to Py = 0, so the fixed rod length L
gives:
(Sx - Px)^2 + Py^2 = L^2
Sx = Px + sqrt(L^2 - Py^2)
This guarantees that both rod endpoints remain attached for
every animation frame.
*/
function applyCrankAngle(theta) {
flywheel.rotation.z = theta;
const pinX = CRANK_CENTER_X + CRANK_RADIUS * Math.cos(theta);
const pinY = CRANK_RADIUS * Math.sin(theta);
const horizontalRodDistance = Math.sqrt(
CONNECTING_ROD_LENGTH * CONNECTING_ROD_LENGTH - pinY * pinY
);
const pistonX = pinX + horizontalRodDistance;
piston.position.x = pistonX;
connectingRod.position.set(
(pinX + pistonX) * 0.5,
pinY * 0.5,
ROD_Z
);
// Local +X points from the piston wrist pin toward the crank pin.
connectingRod.rotation.z = Math.atan2(
pinY,
pinX - pistonX
);
}
applyCrankAngle(0);
// Custom camera controls implemented directly, without addons.
function updateCamera() {
const distance = 10;
const horizontalDistance = distance * Math.cos(cameraElevation);
camera.position.set(
cameraTarget.x + horizontalDistance * Math.sin(cameraAzimuth),
cameraTarget.y + distance * Math.sin(cameraElevation),
cameraTarget.z + horizontalDistance * Math.cos(cameraAzimuth)
);
camera.lookAt(cameraTarget);
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
renderer.setSize(width, height);
const baseHeight = Math.max(5.4, 7.9 / aspect);
const viewHeight = baseHeight / cameraZoom;
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
updateCamera();
}
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.classList.add("grabbing");
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraAzimuth -= deltaX * 0.006;
cameraElevation = THREE.MathUtils.clamp(
cameraElevation + deltaY * 0.005,
-0.75,
1.05
);
updateCamera();
});
function endDrag(event) {
dragging = false;
renderer.domElement.classList.remove("grabbing");
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();
cameraZoom = THREE.MathUtils.clamp(
cameraZoom * Math.exp(-event.deltaY * 0.001),
0.68,
2.2
);
resize();
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
resetCameraButton.addEventListener("click", () => {
cameraAzimuth = 0.22;
cameraElevation = 0.2;
cameraZoom = 1;
resize();
});
window.addEventListener("resize", resize);
resize();
// Animation state.
let elapsed = 0;
let playing = true;
let previousTime = performance.now();
function updatePlayButton() {
if (playing) {
playButton.textContent = "Pause";
} else if (elapsed >= DURATION) {
playButton.textContent = "Replay";
} else {
playButton.textContent = "Play";
}
}
playButton.addEventListener("click", () => {
if (playing) {
playing = false;
} else {
if (elapsed >= DURATION) {
elapsed = 0;
scrubber.value = 0;
applyCrankAngle(0);
}
playing = true;
}
updatePlayButton();
});
restartButton.addEventListener("click", () => {
elapsed = 0;
scrubber.value = 0;
applyCrankAngle(0);
playing = true;
updatePlayButton();
});
scrubber.addEventListener("input", () => {
playing = false;
elapsed = (Number(scrubber.value) / 1000) * DURATION;
applyCrankAngle((elapsed / DURATION) * TAU);
updatePlayButton();
});
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min(100, now - previousTime);
previousTime = now;
if (playing) {
elapsed = Math.min(DURATION, elapsed + delta);
const progress = elapsed / DURATION;
applyCrankAngle(progress * TAU);
scrubber.value = Math.round(progress * 1000);
if (elapsed >= DURATION) {
playing = false;
updatePlayButton();
}
}
const degrees = Math.round((elapsed / DURATION) * 360);
turnLabel.textContent = `${degrees}° / 360°`;
renderer.render(scene, camera);
}
updatePlayButton();
loading.remove();
requestAnimationFrame(animate);
</script>
</body>
</html>
```