Prompt specification
Build or customize the Three.js 3D scene "Wind-up toy spring diagram" 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: Wind-up toy spring diagram
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/wind-up-toy-spring-diagram-x0397
Scene Description Prompt:
Show a stylized wind-up car beside its spring and gears, tracing power to the axle. Use Three.js from cdnjs in one HTML file.
Working Three.js Starter (wind-up-toy-spring-diagram-x0397):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Wind-Up Car — Power Study</title>
<style>
:root {
--bg: #09111b;
--panel: rgba(15, 27, 41, 0.78);
--line: rgba(255, 255, 255, 0.13);
--text: #f5f1e8;
--muted: #9cabbc;
--gold: #ffc857;
--coral: #ff6b5e;
--mint: #4ee6c1;
}
* {
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 {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 44%, transparent 35%, rgba(2, 7, 13, 0.32) 100%),
linear-gradient(180deg, rgba(3, 8, 14, 0.24), transparent 30%, transparent 72%, rgba(3, 8, 14, 0.4));
}
header {
position: fixed;
top: 34px;
left: 40px;
z-index: 4;
max-width: 460px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 13px;
color: var(--gold);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 28px;
height: 2px;
background: var(--gold);
box-shadow: 0 0 12px rgba(255, 200, 87, 0.6);
}
h1 {
margin: 0;
font-size: clamp(34px, 4.7vw, 67px);
line-height: 0.95;
letter-spacing: -0.055em;
font-weight: 760;
}
header p {
max-width: 390px;
margin: 18px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.65;
}
.story {
position: fixed;
top: 34px;
right: 36px;
z-index: 4;
width: 284px;
padding: 18px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
backdrop-filter: blur(16px);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.18);
}
.story-head {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 12px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.live {
display: flex;
align-items: center;
gap: 7px;
color: var(--mint);
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--mint);
box-shadow: 0 0 10px var(--mint);
animation: livePulse 1.8s ease-in-out infinite;
}
@keyframes livePulse {
50% {
opacity: 0.4;
transform: scale(0.75);
}
}
.step {
position: relative;
display: grid;
grid-template-columns: 30px 1fr;
gap: 11px;
padding: 11px 8px;
border-radius: 12px;
transition: background 0.3s ease, transform 0.3s ease;
}
.step + .step {
margin-top: 3px;
}
.step.active {
background: rgba(255, 255, 255, 0.055);
transform: translateX(3px);
}
.step-number {
display: grid;
place-items: center;
width: 28px;
height: 28px;
border: 1px solid var(--line);
border-radius: 9px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
transition: 0.3s ease;
}
.step.active .step-number {
color: #101923;
border-color: var(--gold);
background: var(--gold);
box-shadow: 0 0 18px rgba(255, 200, 87, 0.23);
}
.step strong {
display: block;
margin: 1px 0 3px;
font-size: 12px;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.step small {
display: block;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
.controls {
position: fixed;
z-index: 5;
left: 50%;
bottom: 28px;
display: flex;
gap: 8px;
padding: 7px;
transform: translateX(-50%);
border: 1px solid var(--line);
border-radius: 16px;
background: rgba(11, 20, 31, 0.82);
backdrop-filter: blur(16px);
box-shadow: 0 16px 50px rgba(0, 0, 0, 0.3);
}
button {
display: flex;
align-items: center;
gap: 8px;
min-height: 40px;
padding: 0 15px;
border: 1px solid transparent;
border-radius: 11px;
color: var(--muted);
background: transparent;
font: inherit;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.04em;
white-space: nowrap;
cursor: pointer;
transition: 0.2s ease;
}
button:hover {
color: var(--text);
background: rgba(255, 255, 255, 0.06);
}
button.active {
color: #101923;
background: var(--gold);
box-shadow: 0 5px 16px rgba(255, 200, 87, 0.18);
}
button svg {
width: 15px;
height: 15px;
fill: none;
stroke: currentColor;
stroke-width: 1.8;
stroke-linecap: round;
stroke-linejoin: round;
}
.hint {
position: fixed;
z-index: 3;
right: 38px;
bottom: 37px;
color: rgba(206, 218, 231, 0.55);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
pointer-events: none;
}
.scene-label {
position: fixed;
z-index: 3;
min-width: 130px;
padding: 9px 11px 9px 13px;
border: 1px solid rgba(255, 255, 255, 0.14);
border-left: 2px solid var(--gold);
border-radius: 9px;
background: rgba(8, 16, 26, 0.82);
backdrop-filter: blur(10px);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.18);
pointer-events: none;
transform: translate(-50%, -100%);
transition: opacity 0.15s linear;
}
.scene-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 24px;
background: linear-gradient(var(--gold), transparent);
}
.scene-label strong {
display: block;
color: var(--text);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.scene-label span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
}
.scene-label.mint {
border-left-color: var(--mint);
}
.scene-label.mint::after {
background: linear-gradient(var(--mint), transparent);
}
.scene-label.coral {
border-left-color: var(--coral);
}
.scene-label.coral::after {
background: linear-gradient(var(--coral), transparent);
}
@media (max-width: 900px) {
.story {
display: none;
}
header {
top: 22px;
left: 22px;
}
header p {
max-width: 310px;
font-size: 12px;
}
.hint {
display: none;
}
.controls {
bottom: 16px;
}
}
@media (max-width: 560px) {
h1 {
font-size: 37px;
}
header p {
display: none;
}
.scene-label {
min-width: 105px;
padding: 7px 9px;
}
.scene-label span {
display: none;
}
.controls {
width: calc(100% - 28px);
justify-content: center;
}
button {
padding: 0 11px;
font-size: 10px;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive 3D wind-up car mechanism"></main>
<div class="vignette"></div>
<header>
<div class="eyebrow">Mechanical study · 01</div>
<h1>Small spring.<br />Big motion.</h1>
<p>
Follow stored energy from a tightly wound coil, through a reduction gear
train, and into the axle that turns the wheels.
</p>
</header>
<aside class="story" aria-label="Power flow explanation">
<div class="story-head">
<span>Power flow</span>
<span class="live"><i class="live-dot"></i> Live</span>
</div>
<div class="step active" data-step="0">
<div class="step-number">01</div>
<div>
<strong>Wind</strong>
<small>The spring stores elastic energy.</small>
</div>
</div>
<div class="step" data-step="1">
<div class="step-number">02</div>
<div>
<strong>Transfer</strong>
<small>Gears trade speed for torque.</small>
</div>
</div>
<div class="step" data-step="2">
<div class="step-number">03</div>
<div>
<strong>Drive</strong>
<small>The axle turns the wheels.</small>
</div>
</div>
</aside>
<div class="controls" aria-label="Scene controls">
<button id="traceButton" class="active" aria-pressed="true">
<svg viewBox="0 0 24 24">
<path d="M3 12h4l2-7 4 14 3-7h5" />
</svg>
Trace power
</button>
<button id="rotateButton" aria-pressed="false">
<svg viewBox="0 0 24 24">
<path d="M20 7v5h-5" />
<path d="M18.2 16.5a8 8 0 1 1 .7-8.4L20 12" />
</svg>
Auto orbit
</button>
<button id="resetButton">
<svg viewBox="0 0 24 24">
<path d="M4 4v6h6" />
<path d="M5.5 15a8 8 0 1 0 .4-8.5L4 10" />
</svg>
Reset view
</button>
</div>
<div class="hint">Drag to orbit · Scroll to zoom</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, and camera
// -------------------------------------------------------------------------
const host = document.querySelector("#scene");
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.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
host.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x09111b);
scene.fog = new THREE.FogExp2(0x09111b, 0.035);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const target = new THREE.Vector3(0.15, 0.9, 0);
let theta = 0.38;
let phi = 1.12;
let radius = 10.4;
let targetTheta = theta;
let targetPhi = phi;
let targetRadius = radius;
let userZoomed = false;
let autoOrbit = false;
function updateCamera() {
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.cos(theta),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.sin(theta)
);
camera.lookAt(target);
}
updateCamera();
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xbdd8ff, 0x1a1420, 2.15));
const keyLight = new THREE.DirectionalLight(0xfff1d6, 4.2);
keyLight.position.set(-4, 8, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.bias = -0.0002;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x52d8ff, 2.8);
rimLight.position.set(6, 4, -6);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff9c54, 32, 9, 2);
warmLight.position.set(2.8, 2.7, 2.3);
scene.add(warmLight);
const mintLight = new THREE.PointLight(0x45f0cc, 22, 7, 2);
mintLight.position.set(-2, 1.8, -2);
scene.add(mintLight);
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const mat = {
body: new THREE.MeshPhysicalMaterial({
color: 0xe9dfc7,
roughness: 0.3,
metalness: 0.08,
clearcoat: 0.75,
clearcoatRoughness: 0.22
}),
bodyDark: new THREE.MeshStandardMaterial({
color: 0x17202a,
roughness: 0.62,
metalness: 0.22
}),
coral: new THREE.MeshStandardMaterial({
color: 0xff6559,
roughness: 0.32,
metalness: 0.18
}),
gold: new THREE.MeshStandardMaterial({
color: 0xffc857,
roughness: 0.25,
metalness: 0.72
}),
mint: new THREE.MeshStandardMaterial({
color: 0x4ee6c1,
roughness: 0.26,
metalness: 0.65
}),
chrome: new THREE.MeshStandardMaterial({
color: 0xd7e2e6,
roughness: 0.18,
metalness: 0.92
}),
tire: new THREE.MeshStandardMaterial({
color: 0x111820,
roughness: 0.84,
metalness: 0.05
}),
glass: new THREE.MeshPhysicalMaterial({
color: 0x143348,
roughness: 0.12,
metalness: 0.12,
transparent: true,
opacity: 0.94,
clearcoat: 1,
side: THREE.DoubleSide
}),
floor: new THREE.MeshStandardMaterial({
color: 0x0b1420,
roughness: 0.93,
metalness: 0.08
})
};
// -------------------------------------------------------------------------
// Ground and display plinth
// -------------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
mat.floor
);
floor.rotation.x = -Math.PI / 2;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(30, 30, 0x35516a, 0x1e3142);
grid.position.y = 0.006;
grid.material.transparent = true;
grid.material.opacity = 0.22;
scene.add(grid);
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(2.2, 2.3, 0.1, 72),
new THREE.MeshStandardMaterial({
color: 0x111e2b,
roughness: 0.72,
metalness: 0.2
})
);
platform.position.set(2.1, 0.06, 0.35);
platform.receiveShadow = true;
scene.add(platform);
const platformRing = new THREE.Mesh(
new THREE.TorusGeometry(2.16, 0.018, 8, 96),
new THREE.MeshBasicMaterial({
color: 0x4ee6c1,
transparent: true,
opacity: 0.38
})
);
platformRing.rotation.x = Math.PI / 2;
platformRing.position.set(2.1, 0.12, 0.35);
scene.add(platformRing);
const mechanismBase = new THREE.Mesh(
new THREE.BoxGeometry(3.2, 0.16, 1.35),
new THREE.MeshStandardMaterial({
color: 0x172431,
roughness: 0.46,
metalness: 0.5
})
);
mechanismBase.position.set(2.12, 0.2, 0.4);
mechanismBase.castShadow = true;
mechanismBase.receiveShadow = true;
scene.add(mechanismBase);
// -------------------------------------------------------------------------
// Stylized car
// -------------------------------------------------------------------------
const car = new THREE.Group();
car.position.x = -1.55;
scene.add(car);
const bodyProfile = new THREE.Shape();
bodyProfile.moveTo(-1.47, 0.58);
bodyProfile.lineTo(-1.55, 0.86);
bodyProfile.quadraticCurveTo(-1.57, 1.1, -1.3, 1.18);
bodyProfile.lineTo(-0.82, 1.21);
bodyProfile.lineTo(-0.37, 1.76);
bodyProfile.quadraticCurveTo(-0.25, 1.82, -0.07, 1.82);
bodyProfile.lineTo(0.61, 1.8);
bodyProfile.lineTo(1.23, 1.24);
bodyProfile.quadraticCurveTo(1.47, 1.15, 1.48, 0.9);
bodyProfile.lineTo(1.4, 0.58);
bodyProfile.closePath();
const bodyGeometry = new THREE.ExtrudeGeometry(bodyProfile, {
depth: 1.26,
bevelEnabled: true,
bevelSize: 0.075,
bevelThickness: 0.075,
bevelSegments: 3,
steps: 1,
curveSegments: 12
});
bodyGeometry.translate(0, 0, -0.63);
const carBody = new THREE.Mesh(bodyGeometry, mat.body);
carBody.castShadow = true;
carBody.receiveShadow = true;
car.add(carBody);
// Side windows.
function createWindow(points) {
const shape = new THREE.Shape();
shape.moveTo(points[0][0], points[0][1]);
for (let i = 1; i < points.length; i++) {
shape.lineTo(points[i][0], points[i][1]);
}
shape.closePath();
return new THREE.ShapeGeometry(shape);
}
const frontWindow = new THREE.Mesh(
createWindow([
[-0.72, 1.27],
[-0.35, 1.7],
[-0.1, 1.7],
[-0.1, 1.27]
]),
mat.glass
);
frontWindow.position.z = 0.696;
car.add(frontWindow);
const rearWindow = new THREE.Mesh(
createWindow([
[0.12, 1.7],
[0.56, 1.7],
[0.96, 1.29],
[0.12, 1.29]
]),
mat.glass
);
rearWindow.position.z = 0.696;
car.add(rearWindow);
// Rear side windows.
const frontWindowBack = frontWindow.clone();
frontWindowBack.position.z = -0.696;
frontWindowBack.rotation.y = Math.PI;
car.add(frontWindowBack);
const rearWindowBack = rearWindow.clone();
rearWindowBack.position.z = -0.696;
rearWindowBack.rotation.y = Math.PI;
car.add(rearWindowBack);
// Chassis.
const chassis = new THREE.Mesh(
new THREE.BoxGeometry(2.65, 0.17, 1.35),
mat.bodyDark
);
chassis.position.set(0, 0.54, 0);
chassis.castShadow = true;
car.add(chassis);
// Side stripe.
const stripe = new THREE.Mesh(
new THREE.BoxGeometry(2.5, 0.12, 1.37),
mat.coral
);
stripe.position.set(0, 0.88, 0);
stripe.castShadow = true;
car.add(stripe);
// Roof accent.
const roofAccent = new THREE.Mesh(
new THREE.BoxGeometry(0.85, 0.045, 1.23),
mat.coral
);
roofAccent.position.set(0.26, 1.82, 0);
car.add(roofAccent);
// Bumpers.
for (const x of [-1.48, 1.43]) {
const bumper = new THREE.Mesh(
new THREE.BoxGeometry(0.16, 0.16, 1.42),
mat.chrome
);
bumper.position.set(x, 0.72, 0);
bumper.castShadow = true;
car.add(bumper);
}
// Headlights and tail lights.
for (const z of [-0.43, 0.43]) {
const headlight = new THREE.Mesh(
new THREE.SphereGeometry(0.105, 20, 12),
new THREE.MeshStandardMaterial({
color: 0xfff2b2,
emissive: 0xffd45a,
emissiveIntensity: 1.3,
roughness: 0.2
})
);
headlight.scale.set(0.6, 0.8, 1);
headlight.position.set(-1.54, 1.01, z);
car.add(headlight);
const tailLight = new THREE.Mesh(
new THREE.BoxGeometry(0.09, 0.16, 0.19),
mat.coral
);
tailLight.position.set(1.49, 1.02, z);
car.add(tailLight);
}
// Door seams.
const doorLineMaterial = new THREE.LineBasicMaterial({
color: 0x958b75,
transparent: true,
opacity: 0.55
});
const doorGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-0.02, 0.98, 0.704),
new THREE.Vector3(-0.02, 1.69, 0.704)
]);
car.add(new THREE.Line(doorGeometry, doorLineMaterial));
// Wheel construction.
const wheelGroups = [];
function createWheel() {
const wheel = new THREE.Group();
const tire = new THREE.Mesh(
new THREE.CylinderGeometry(0.43, 0.43, 0.28, 36),
mat.tire
);
tire.rotation.x = Math.PI / 2;
tire.castShadow = true;
wheel.add(tire);
for (const z of [-0.145, 0.145]) {
const sidewall = new THREE.Mesh(
new THREE.TorusGeometry(0.335, 0.055, 10, 36),
mat.tire
);
sidewall.position.z = z;
sidewall.castShadow = true;
wheel.add(sidewall);
}
const hub = new THREE.Mesh(
new THREE.CylinderGeometry(0.18, 0.18, 0.31, 24),
mat.gold
);
hub.rotation.x = Math.PI / 2;
hub.castShadow = true;
wheel.add(hub);
const hubCap = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.075, 0.33, 20),
mat.chrome
);
hubCap.rotation.x = Math.PI / 2;
wheel.add(hubCap);
for (let i = 0; i < 4; i++) {
const spoke = new THREE.Mesh(
new THREE.BoxGeometry(0.27, 0.055, 0.075),
mat.coral
);
spoke.rotation.z = (i * Math.PI) / 4;
spoke.position.z = 0.165;
wheel.add(spoke);
}
return wheel;
}
for (const x of [-0.92, 0.9]) {
for (const z of [-0.73, 0.73]) {
const wheel = createWheel();
wheel.position.set(x, 0.46, z);
car.add(wheel);
wheelGroups.push(wheel);
}
}
// Rear axle, aligned along Z.
const axle = new THREE.Mesh(
new THREE.CylinderGeometry(0.065, 0.065, 1.82, 18),
mat.chrome
);
axle.rotation.x = Math.PI / 2;
axle.position.set(0.9, 0.46, 0);
axle.castShadow = true;
car.add(axle);
// Wind-up key on the back of the car.
const windUpKey = new THREE.Group();
windUpKey.position.set(1.58, 1.08, 0);
const keyStem = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.055, 0.32, 16),
mat.chrome
);
keyStem.rotation.z = Math.PI / 2;
windUpKey.add(keyStem);
const keyBow = new THREE.Mesh(
new THREE.TorusGeometry(0.17, 0.04, 10, 28),
mat.gold
);
keyBow.rotation.y = Math.PI / 2;
keyBow.position.x = 0.18;
windUpKey.add(keyBow);
const keyGrip = new THREE.Mesh(
new THREE.CylinderGeometry(0.025, 0.025, 0.26, 12),
mat.coral
);
keyGrip.position.x = 0.18;
keyGrip.rotation.x = Math.PI / 2;
windUpKey.add(keyGrip);
car.add(windUpKey);
// -------------------------------------------------------------------------
// Spring and gear train
// -------------------------------------------------------------------------
const springCenter = new THREE.Vector3(1.38, 1.18, 0.28);
const springPoints = [];
for (let i = 0; i <= 190; i++) {
const u = i / 190;
const angle = u * Math.PI * 2 * 5.25 + 0.35;
const r = THREE.MathUtils.lerp(0.78, 0.11, u);
springPoints.push(
new THREE.Vector3(
springCenter.x + Math.cos(angle) * r,
springCenter.y + Math.sin(angle) * r,
springCenter.z + Math.sin(u * Math.PI) * 0.025
)
);
}
const springCurve = new THREE.CatmullRomCurve3(springPoints);
const spring = new THREE.Mesh(
new THREE.TubeGeometry(springCurve, 360, 0.024, 8, false),
mat.coral
);
spring.castShadow = true;
scene.add(spring);
// Spring arbor and outer anchor.
const arbor = new THREE.Mesh(
new THREE.CylinderGeometry(0.12, 0.12, 0.35, 24),
mat.chrome
);
arbor.rotation.x = Math.PI / 2;
arbor.position.copy(springCenter);
scene.add(arbor);
const anchor = new THREE.Mesh(
new THREE.BoxGeometry(0.12, 0.26, 0.13),
mat.gold
);
anchor.position.set(springCenter.x + 0.76, springCenter.y, 0.23);
scene.add(anchor);
function createGear(radius, teeth, depth, material) {
const shape = new THREE.Shape();
const pointsPerTooth = 4;
for (let i = 0; i < teeth * pointsPerTooth; i++) {
const angle = (i / (teeth * pointsPerTooth)) * Math.PI * 2;
const phase = i % pointsPerTooth;
const r = phase === 1 || phase === 2 ? radius : radius * 0.82;
const x = Math.cos(angle) * r;
const y = Math.sin(angle) * r;
if (i === 0) shape.moveTo(x, y);
else shape.lineTo(x, y);
}
shape.closePath();
const centerHole = new THREE.Path();
centerHole.absarc(0, 0, radius * 0.13, 0, Math.PI * 2, true);
shape.holes.push(centerHole);
for (let i = 0; i < 5; i++) {
const angle = (i / 5) * Math.PI * 2 + 0.25;
const hole = new THREE.Path();
hole.absarc(
Math.cos(angle) * radius * 0.52,
Math.sin(angle) * radius * 0.52,
radius * 0.12,
0,
Math.PI * 2,
true
);
shape.holes.push(hole);
}
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
bevelEnabled: true,
bevelSize: 0.025,
bevelThickness: 0.025,
bevelSegments: 2,
curveSegments: 10
});
geometry.translate(0, 0, -depth / 2);
const gear = new THREE.Mesh(geometry, material);
gear.castShadow = true;
gear.receiveShadow = true;
const hub = new THREE.Mesh(
new THREE.CylinderGeometry(radius * 0.17, radius * 0.17, depth + 0.08, 24),
mat.chrome
);
hub.rotation.x = Math.PI / 2;
gear.add(hub);
const hubCap = new THREE.Mesh(
new THREE.CylinderGeometry(radius * 0.075, radius * 0.075, depth + 0.11, 20),
mat.bodyDark
);
hubCap.rotation.x = Math.PI / 2;
gear.add(hubCap);
return gear;
}
const gear1 = createGear(0.59, 14, 0.18, mat.gold);
gear1.position.set(1.38, 1.18, 0.77);
scene.add(gear1);
const gear2 = createGear(0.46, 10, 0.16, mat.mint);
gear2.position.set(2.25, 0.66, 0.77);
scene.add(gear2);
const gear3 = createGear(0.35, 8, 0.15, mat.coral);
gear3.position.set(2.98, 1.16, 0.77);
scene.add(gear3);
// Rear supports for the mechanism.
for (const position of [
[1.38, 1.18],
[2.25, 0.66],
[2.98, 1.16]
]) {
const support = new THREE.Mesh(
new THREE.CylinderGeometry(0.065, 0.08, 0.73, 16),
mat.chrome
);
support.position.set(position[0], 0.55, 0.39);
support.castShadow = true;
scene.add(support);
const foot = new THREE.Mesh(
new THREE.CylinderGeometry(0.15, 0.17, 0.07, 20),
mat.bodyDark
);
foot.position.set(position[0], 0.31, 0.39);
scene.add(foot);
}
// -------------------------------------------------------------------------
// Power trace: spring -> gears -> axle -> wheels
// -------------------------------------------------------------------------
const traceGroup = new THREE.Group();
scene.add(traceGroup);
const link1 = new THREE.CatmullRomCurve3([
new THREE.Vector3(1.38, 1.18, 0.94),
new THREE.Vector3(1.78, 0.94, 0.98),
new THREE.Vector3(2.25, 0.66, 0.96)
]);
const link2 = new THREE.CatmullRomCurve3([
new THREE.Vector3(2.25, 0.66, 0.96),
new THREE.Vector3(2.61, 0.9, 0.98),
new THREE.Vector3(2.98, 1.16, 0.96)
]);
const outputLink = new THREE.CatmullRomCurve3([
new THREE.Vector3(2.98, 1.16, 0.98),
new THREE.Vector3(2.72, 0.65, 1.01),
new THREE.Vector3(1.55, 0.47, 1.01),
new THREE.Vector3(0.25, 0.45, 1.01),
new THREE.Vector3(-0.65, 0.46, 1.01)
]);
const axleCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(-0.65, 0.46, 1.01),
new THREE.Vector3(-0.65, 0.46, 0.32),
new THREE.Vector3(-0.65, 0.46, -0.45),
new THREE.Vector3(-0.65, 0.46, -0.82)
]);
const traceMaterial = new THREE.MeshBasicMaterial({
color: 0x58e8c7,
transparent: true,
opacity: 0.32,
depthWrite: false
});
function addTraceCurve(curve, tubularSegments = 64) {
const mesh = new THREE.Mesh(
new THREE.TubeGeometry(curve, tubularSegments, 0.017, 6, false),
traceMaterial
);
traceGroup.add(mesh);
return mesh;
}
addTraceCurve(springCurve, 360);
addTraceCurve(link1);
addTraceCurve(link2);
addTraceCurve(outputLink, 100);
addTraceCurve(axleCurve, 70);
// Direction arrows on the output connection.
for (const t of [0.18, 0.48, 0.77]) {
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.075, 0.22, 12),
new THREE.MeshBasicMaterial({
color: 0xffdc73,
transparent: true,
opacity: 0.9
})
);
arrow.position.copy(outputLink.getPointAt(t));
arrow.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
outputLink.getTangentAt(t).normalize()
);
traceGroup.add(arrow);
}
// Soft radial sprite texture for moving energy pulses.
const glowCanvas = document.createElement("canvas");
glowCanvas.width = 64;
glowCanvas.height = 64;
const glowContext = glowCanvas.getContext("2d");
const gradient = glowContext.createRadialGradient(32, 32, 0, 32, 32, 32);
gradient.addColorStop(0, "rgba(255,255,255,1)");
gradient.addColorStop(0.18, "rgba(255,242,164,0.95)");
gradient.addColorStop(0.48, "rgba(255,200,87,0.35)");
gradient.addColorStop(1, "rgba(255,200,87,0)");
glowContext.fillStyle = gradient;
glowContext.fillRect(0, 0, 64, 64);
const glowTexture = new THREE.CanvasTexture(glowCanvas);
glowTexture.colorSpace = THREE.SRGBColorSpace;
const pulses = [];
function addPulses(curve, count, speed, offset = 0) {
for (let i = 0; i < count; i++) {
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: glowTexture,
color: 0xffe17a,
transparent: true,
opacity: 0.95,
blending: THREE.AdditiveBlending,
depthWrite: false
})
);
sprite.scale.setScalar(0.24);
traceGroup.add(sprite);
pulses.push({
sprite,
curve,
speed,
offset: offset + i / count
});
}
}
addPulses(springCurve, 5, 0.13);
addPulses(link1, 2, 0.19, 0.08);
addPulses(link2, 2, 0.22, 0.2);
addPulses(outputLink, 4, 0.17, 0.32);
addPulses(axleCurve, 3, 0.2, 0.52);
// -------------------------------------------------------------------------
// Projected labels
// -------------------------------------------------------------------------
const labels = [];
function createLabel(className, title, subtitle, anchor) {
const element = document.createElement("div");
element.className = `scene-label ${className}`;
element.innerHTML = `<strong>${title}</strong><span>${subtitle}</span>`;
document.body.appendChild(element);
labels.push({ element, anchor });
}
createLabel(
"coral",
"01 · Coil spring",
"Stores elastic energy",
new THREE.Vector3(1.38, 2.03, 0.3)
);
createLabel(
"mint",
"02 · Gear train",
"Reduces speed, increases torque",
new THREE.Vector3(2.48, 1.82, 0.82)
);
createLabel(
"",
"03 · Drive axle",
"Turns both rear wheels",
new THREE.Vector3(-0.65, 0.98, 0.92)
);
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labels) {
const projected = label.anchor.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.15 &&
projected.x < 1.15 &&
projected.y > -1.15 &&
projected.y < 1.15;
label.element.style.display = visible ? "block" : "none";
if (visible) {
label.element.style.left = `${(projected.x * 0.5 + 0.5) * width}px`;
label.element.style.top = `${(-projected.y * 0.5 + 0.5) * height}px`;
}
}
}
// -------------------------------------------------------------------------
// Custom camera controls — no external control modules
// -------------------------------------------------------------------------
let dragging = false;
let lastX = 0;
let lastY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
lastX = event.clientX;
lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
targetTheta -= dx * 0.006;
targetPhi = THREE.MathUtils.clamp(targetPhi - dy * 0.005, 0.42, 1.42);
});
function stopDragging(event) {
dragging = false;
if (event && 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();
userZoomed = true;
targetRadius = THREE.MathUtils.clamp(
targetRadius * Math.exp(event.deltaY * 0.001),
6.2,
18
);
},
{ passive: false }
);
function resetView() {
targetTheta = 0.38;
targetPhi = 1.12;
targetRadius = window.innerWidth < 700 ? 14.3 : 10.4;
userZoomed = false;
}
renderer.domElement.addEventListener("dblclick", resetView);
// -------------------------------------------------------------------------
// UI buttons
// -------------------------------------------------------------------------
const traceButton = document.querySelector("#traceButton");
const rotateButton = document.querySelector("#rotateButton");
const resetButton = document.querySelector("#resetButton");
traceButton.addEventListener("click", () => {
traceGroup.visible = !traceGroup.visible;
traceButton.classList.toggle("active", traceGroup.visible);
traceButton.setAttribute("aria-pressed", String(traceGroup.visible));
});
rotateButton.addEventListener("click", () => {
autoOrbit = !autoOrbit;
rotateButton.classList.toggle("active", autoOrbit);
rotateButton.setAttribute("aria-pressed", String(autoOrbit));
});
resetButton.addEventListener("click", resetView);
// -------------------------------------------------------------------------
// Resize
// -------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
if (!userZoomed) {
targetRadius = width < 600 ? 14.3 : width < 950 ? 11.6 : 10.4;
radius = targetRadius;
}
}
window.addEventListener("resize", resize);
resize();
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
const stepElements = [...document.querySelectorAll(".step")];
let previousStep = -1;
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
const delta = Math.min(clock.getDelta(), 0.04);
if (autoOrbit && !dragging) {
targetTheta += delta * 0.11;
}
theta += (targetTheta - theta) * 0.075;
phi += (targetPhi - phi) * 0.075;
radius += (targetRadius - radius) * 0.075;
updateCamera();
// Gear ratios and wheel motion.
gear1.rotation.z = -elapsed * 0.55;
gear2.rotation.z = elapsed * 0.92;
gear3.rotation.z = -elapsed * 1.38;
for (const wheel of wheelGroups) {
wheel.rotation.z = -elapsed * 1.48;
}
// The key slowly winds and unwinds.
windUpKey.rotation.x = Math.sin(elapsed * 0.62) * 0.48;
// Subtle spring vibration.
const springScale = 1 + Math.sin(elapsed * 2.1) * 0.008;
spring.scale.x = springScale;
spring.scale.y = springScale;
// Moving energy pulses.
for (const pulse of pulses) {
const t = (elapsed * pulse.speed + pulse.offset) % 1;
pulse.sprite.position.copy(pulse.curve.getPointAt(t));
const fade = Math.sin(t * Math.PI);
pulse.sprite.material.opacity = 0.35 + fade * 0.65;
pulse.sprite.scale.setScalar(0.17 + fade * 0.11);
}
// Highlight the corresponding explanatory step.
const activeStep = Math.floor(elapsed / 2.4) % 3;
if (activeStep !== previousStep) {
stepElements.forEach((element, index) => {
element.classList.toggle("active", index === activeStep);
});
previousStep = activeStep;
}
updateLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```