Prompt specification
Build or customize the Three.js 3D scene "Two gear pitch-circle model" 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: Two gear pitch-circle model
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-gear-pitch-circle-model-x1385
Scene Description Prompt:
Animate two meshing low-poly gears with visible pitch circles and rotation speeds matching tooth-count ratios. Use Three.js from cdnjs in standalone HTML.
Working Three.js Starter (two-gear-pitch-circle-model-x1385):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Low-Poly Meshing Gears</title>
<style>
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #07111d;
color: #e8f2ff;
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
display: block;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
padding: 14px 16px;
border: 1px solid rgba(160, 200, 255, 0.18);
border-radius: 12px;
background: rgba(8, 20, 35, 0.78);
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(8px);
pointer-events: none;
}
#info {
top: 18px;
left: 18px;
max-width: min(390px, calc(100vw - 36px));
}
#info h1 {
margin: 0 0 7px;
font-size: 17px;
letter-spacing: 0.02em;
}
#info p {
margin: 4px 0;
color: #afc5dc;
font-size: 13px;
line-height: 1.45;
}
.ratio {
color: #ffe36e !important;
font-variant-numeric: tabular-nums;
}
#legend {
right: 18px;
bottom: 18px;
font-size: 12px;
color: #afc5dc;
}
.dash {
display: inline-block;
width: 28px;
margin-right: 8px;
vertical-align: middle;
border-top: 2px dashed #ffe36e;
}
.world-label {
position: fixed;
z-index: 5;
padding: 6px 9px;
transform: translate(-50%, -50%);
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 999px;
background: rgba(5, 13, 24, 0.8);
color: white;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.04em;
white-space: nowrap;
pointer-events: none;
}
@media (max-width: 600px) {
#info {
top: 10px;
left: 10px;
padding: 11px 13px;
}
#info h1 {
font-size: 15px;
}
#legend {
right: 10px;
bottom: 10px;
}
.world-label {
font-size: 10px;
}
}
</style>
</head>
<body>
<div id="info" class="panel">
<h1>Low-Poly Meshing Gears</h1>
<p>
Common module: <strong>0.24</strong> ·
Pitch-circle centers: <strong>5.76 units apart</strong>
</p>
<p class="ratio">
16 teeth @ 30 RPM ↻ → 32 teeth @ 15 RPM ↺
</p>
<p>Drag to orbit · Scroll to zoom</p>
</div>
<div id="legend" class="panel">
<span class="dash"></span>Dashed ring = pitch circle
</div>
<div id="label-a" class="world-label">16T · 30 RPM ↻</div>
<div id="label-b" class="world-label">32T · 15 RPM ↺</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Gear parameters
// ------------------------------------------------------------
const DRIVER_TEETH = 16;
const FOLLOWER_TEETH = 32;
const MODULE = 0.24;
const THICKNESS = 0.56;
const DRIVER_RPM = 30;
const FOLLOWER_RPM = DRIVER_RPM * DRIVER_TEETH / FOLLOWER_TEETH;
const driverPitchRadius = MODULE * DRIVER_TEETH / 2;
const followerPitchRadius = MODULE * FOLLOWER_TEETH / 2;
const driverOmega = THREE.MathUtils.degToRad(DRIVER_RPM) * 60 / 60;
const followerOmega = -driverOmega * DRIVER_TEETH / FOLLOWER_TEETH;
// Place the contact point at x = 0.
const driverCenter = new THREE.Vector3(-driverPitchRadius, 0, 0);
const followerCenter = new THREE.Vector3(followerPitchRadius, 0, 0);
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
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;
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111d);
scene.fog = new THREE.Fog(0x07111d, 25, 55);
const camera = new THREE.OrthographicCamera(-8, 8, 5, -5, 0.1, 100);
const target = new THREE.Vector3(
(driverCenter.x + followerCenter.x) / 2,
0,
0
);
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xb9dcff, 0x152033, 1.6));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(-4, 7, 12);
keyLight.target.position.copy(target);
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.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight, keyLight.target);
const rimLight = new THREE.DirectionalLight(0x55cfff, 1.8);
rimLight.position.set(9, -4, 8);
rimLight.target.position.copy(target);
scene.add(rimLight, rimLight.target);
// Backdrop receives the gear shadows.
const backdrop = new THREE.Mesh(
new THREE.PlaneGeometry(40, 30),
new THREE.MeshStandardMaterial({
color: 0x0b1726,
roughness: 0.95,
metalness: 0.02
})
);
backdrop.position.set(target.x, 0, -0.85);
backdrop.receiveShadow = true;
scene.add(backdrop);
// ------------------------------------------------------------
// Gear construction
// ------------------------------------------------------------
function addCircularHole(shape, x, y, radius) {
const hole = new THREE.Path();
// Clockwise winding distinguishes holes from the outer contour.
hole.absarc(x, y, radius, 0, Math.PI * 2, true);
shape.holes.push(hole);
}
function createGear({
teeth,
phase,
color,
decorativeHoles,
markerAngle
}) {
const group = new THREE.Group();
const pitchRadius = MODULE * teeth / 2;
const rootRadius = pitchRadius - MODULE * 1.25;
const outerRadius = pitchRadius + MODULE;
const angularPitch = Math.PI * 2 / teeth;
const shape = new THREE.Shape();
const contour = [];
const addPolar = (radius, angle) => {
contour.push(
new THREE.Vector2(
Math.cos(angle) * radius,
Math.sin(angle) * radius
)
);
};
// Each tooth uses a broad root, straight flanks, and a flat tip.
// The flank proportions produce the correct thickness at the
// pitch circle for both gears.
for (let i = 0; i < teeth; i++) {
const centerAngle = phase + i * angularPitch;
addPolar(rootRadius, centerAngle - 0.5000 * angularPitch);
addPolar(rootRadius, centerAngle - 0.3125 * angularPitch);
addPolar(outerRadius, centerAngle - 0.2000 * angularPitch);
addPolar(outerRadius, centerAngle + 0.2000 * angularPitch);
addPolar(rootRadius, centerAngle + 0.3125 * angularPitch);
}
shape.moveTo(contour[0].x, contour[0].y);
for (let i = 1; i < contour.length; i++) {
shape.lineTo(contour[i].x, contour[i].y);
}
shape.closePath();
const boreRadius = Math.min(0.48, pitchRadius * 0.22);
addCircularHole(shape, 0, 0, boreRadius);
// Cut lightweight openings into the gear web.
if (decorativeHoles > 0) {
const holeOrbit = pitchRadius * 0.56;
const holeRadius = pitchRadius * 0.13;
for (let i = 0; i < decorativeHoles; i++) {
const angle = phase + i * Math.PI * 2 / decorativeHoles;
addCircularHole(
shape,
Math.cos(angle) * holeOrbit,
Math.sin(angle) * holeOrbit,
holeRadius
);
}
}
const bevelSize = 0.035;
const bevelThickness = 0.035;
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: THICKNESS,
steps: 1,
curveSegments: 12,
bevelEnabled: true,
bevelSegments: 1,
bevelSize,
bevelThickness
});
// Center the extrusion around z = 0.
geometry.translate(0, 0, -THICKNESS / 2);
const bodyMaterial = new THREE.MeshStandardMaterial({
color,
roughness: 0.43,
metalness: 0.22,
flatShading: true
});
const body = new THREE.Mesh(geometry, bodyMaterial);
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const frontZ = THICKNESS / 2 + bevelThickness;
// Dark shaft visible through the central bore.
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(
boreRadius * 0.62,
boreRadius * 0.62,
THICKNESS + 0.72,
12
),
new THREE.MeshStandardMaterial({
color: 0x17212c,
roughness: 0.3,
metalness: 0.72,
flatShading: true
})
);
shaft.rotation.x = Math.PI / 2;
shaft.position.z = -0.02;
shaft.castShadow = true;
group.add(shaft);
// Low-poly hub ring.
const hubMaterial = new THREE.MeshStandardMaterial({
color: new THREE.Color(color).multiplyScalar(0.62),
roughness: 0.32,
metalness: 0.48,
flatShading: true
});
const hubRing = new THREE.Mesh(
new THREE.TorusGeometry(
boreRadius * 1.15,
0.065,
6,
Math.max(12, Math.floor(teeth / 2))
),
hubMaterial
);
hubRing.position.z = frontZ + 0.025;
hubRing.castShadow = true;
group.add(hubRing);
// Highly visible dashed pitch circle.
const circlePoints = [];
const circleSegments = 160;
for (let i = 0; i <= circleSegments; i++) {
const angle = i / circleSegments * Math.PI * 2;
circlePoints.push(
new THREE.Vector3(
Math.cos(angle) * pitchRadius,
Math.sin(angle) * pitchRadius,
0
)
);
}
const pitchCircle = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(circlePoints),
new THREE.LineDashedMaterial({
color: 0xffe36e,
dashSize: 0.14,
gapSize: 0.09,
transparent: true,
opacity: 0.95
})
);
pitchCircle.position.z = frontZ + 0.045;
pitchCircle.computeLineDistances();
pitchCircle.renderOrder = 4;
group.add(pitchCircle);
// A rotating timing mark makes the motion direction obvious.
const marker = new THREE.Mesh(
new THREE.BoxGeometry(pitchRadius * 0.25, 0.075, 0.055),
new THREE.MeshBasicMaterial({ color: 0xfff3b0 })
);
marker.position.set(
Math.cos(markerAngle) * pitchRadius * 0.76,
Math.sin(markerAngle) * pitchRadius * 0.76,
frontZ + 0.065
);
marker.rotation.z = markerAngle;
marker.renderOrder = 5;
group.add(marker);
return {
group,
pitchRadius,
outerRadius,
frontZ
};
}
// At the initial position, a driver tooth is centered on the
// contact point while a follower gap is centered there.
const driver = createGear({
teeth: DRIVER_TEETH,
phase: 0,
color: 0xff984f,
decorativeHoles: 3,
markerAngle: 0
});
const follower = createGear({
teeth: FOLLOWER_TEETH,
phase: Math.PI - Math.PI / FOLLOWER_TEETH,
color: 0x35cbb8,
decorativeHoles: 6,
markerAngle: Math.PI - Math.PI / FOLLOWER_TEETH
});
driver.group.position.copy(driverCenter);
follower.group.position.copy(followerCenter);
scene.add(driver.group, follower.group);
// ------------------------------------------------------------
// Custom camera controls: no Three.js addons are used.
// ------------------------------------------------------------
let yaw = 0;
let pitch = 0.04;
let zoom = 1;
let dragging = false;
let previousX = 0;
let previousY = 0;
function updateCamera() {
const radius = 22;
const cosPitch = Math.cos(pitch);
camera.position.set(
target.x + radius * Math.sin(yaw) * cosPitch,
target.y + radius * Math.sin(pitch),
target.z + radius * Math.cos(yaw) * cosPitch
);
camera.lookAt(target);
camera.zoom = zoom;
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
// Preserve the complete gear pair on narrow displays.
const viewHeight = Math.max(9.8, 13.2 / aspect);
camera.left = -viewHeight * aspect / 2;
camera.right = viewHeight * aspect / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
renderer.setSize(width, height);
updateCamera();
}
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
yaw -= deltaX * 0.006;
pitch = THREE.MathUtils.clamp(
pitch + deltaY * 0.005,
-1.05,
1.05
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined) {
if (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();
zoom = THREE.MathUtils.clamp(
zoom * Math.exp(-event.deltaY * 0.001),
0.65,
2.4
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", event => {
event.preventDefault();
});
window.addEventListener("resize", resize);
resize();
// ------------------------------------------------------------
// Screen-space labels
// ------------------------------------------------------------
const labelA = document.getElementById("label-a");
const labelB = document.getElementById("label-b");
const projected = new THREE.Vector3();
function positionLabel(element, x, y, z) {
projected.set(x, y, z).project(camera);
element.style.left =
`${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top =
`${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const startTime = performance.now();
function animate(now) {
const elapsed = (now - startTime) / 1000;
// Exact conjugate rotation for external gears:
// omega_follower = -omega_driver * teeth_driver / teeth_follower
driver.group.rotation.z = driverOmega * elapsed;
follower.group.rotation.z = followerOmega * elapsed;
positionLabel(
labelA,
driverCenter.x,
driver.outerRadius + 0.52,
driver.frontZ
);
positionLabel(
labelB,
followerCenter.x,
follower.outerRadius + 0.52,
follower.frontZ
);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```