Prompt specification
Build or customize the Three.js 3D scene "Interlocking toy gear pair" 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: Interlocking toy gear pair
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/interlocking-toy-gear-pair-x0337
Scene Description Prompt:
Display two thick toy gears with aligned tooth pitch and marked reference teeth; expose their contact region so opposite rotation directions are visually obvious. Build this with Three.js from cdnjs as a voxel scene in one self-contained HTML file.
Working Three.js Starter (interlocking-toy-gear-pair-x0337):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Meshed Voxel Gears</title>
<style>
:root {
color-scheme: dark;
--cyan: #58d8df;
--orange: #ff9a62;
--gold: #ffd166;
--panel: rgba(8, 18, 32, 0.78);
--border: rgba(255, 255, 255, 0.13);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #07111f;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
position: fixed;
inset: 0;
display: block;
cursor: grab;
touch-action: none;
}
canvas.dragging {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(
circle at 50% 44%,
transparent 42%,
rgba(1, 7, 15, 0.25) 78%,
rgba(1, 7, 15, 0.58) 100%
);
}
.panel {
position: fixed;
z-index: 5;
border: 1px solid var(--border);
background: var(--panel);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.title-panel {
top: 22px;
left: 22px;
width: min(370px, calc(100vw - 44px));
padding: 18px 20px;
border-radius: 18px;
}
.eyebrow {
margin-bottom: 7px;
color: #89a6bd;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(21px, 3vw, 30px);
line-height: 1.08;
letter-spacing: -0.035em;
}
.subtitle {
margin-top: 9px;
color: #a9bdce;
font-size: 13px;
line-height: 1.45;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-top: 15px;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 7px 9px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 999px;
background: rgba(255, 255, 255, 0.035);
color: #d7e4ed;
font-size: 11px;
font-weight: 700;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 2px;
box-shadow: 0 0 10px currentColor;
}
.swatch.cyan {
color: var(--cyan);
background: var(--cyan);
}
.swatch.orange {
color: var(--orange);
background: var(--orange);
}
.swatch.gold {
color: var(--gold);
background: var(--gold);
}
.controls {
right: 22px;
bottom: 22px;
display: flex;
align-items: center;
gap: 8px;
padding: 9px;
border-radius: 16px;
}
button {
min-height: 38px;
padding: 0 13px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 10px;
color: #e8f2f8;
background: rgba(255, 255, 255, 0.055);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition:
background 150ms ease,
border-color 150ms ease,
transform 150ms ease;
}
button:hover {
background: rgba(255, 255, 255, 0.11);
border-color: rgba(255, 255, 255, 0.22);
}
button:active {
transform: translateY(1px);
}
button.primary {
border-color: rgba(88, 216, 223, 0.35);
background: rgba(88, 216, 223, 0.14);
}
.speed {
display: flex;
align-items: center;
gap: 8px;
min-width: 142px;
padding: 0 8px;
color: #9db2c3;
font-size: 11px;
font-weight: 700;
}
input[type="range"] {
width: 78px;
accent-color: var(--cyan);
}
.help {
position: fixed;
z-index: 5;
left: 22px;
bottom: 22px;
padding: 10px 13px;
border-radius: 12px;
color: #8fa8bb;
font-size: 11px;
font-weight: 650;
pointer-events: none;
}
.world-label {
position: fixed;
z-index: 4;
padding: 7px 10px;
border: 1px solid currentColor;
border-radius: 999px;
background: rgba(5, 13, 24, 0.8);
box-shadow: 0 7px 24px rgba(0, 0, 0, 0.24);
font-size: 11px;
font-weight: 850;
letter-spacing: 0.08em;
pointer-events: none;
transform: translate(-50%, -50%);
white-space: nowrap;
}
#label-a {
color: var(--cyan);
}
#label-b {
color: var(--orange);
}
.contact-label {
position: fixed;
z-index: 4;
padding: 5px 8px;
border-radius: 7px;
color: #eafcff;
background: rgba(19, 49, 61, 0.82);
box-shadow: 0 0 18px rgba(88, 216, 223, 0.18);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.14em;
pointer-events: none;
transform: translate(-50%, -50%);
}
@media (max-width: 720px) {
.title-panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 16px;
}
.subtitle {
display: none;
}
.legend {
margin-top: 10px;
}
.controls {
right: 12px;
bottom: 12px;
left: 12px;
justify-content: center;
flex-wrap: wrap;
}
.help {
display: none;
}
.speed {
min-width: 120px;
}
}
</style>
</head>
<body>
<div class="vignette"></div>
<section class="panel title-panel">
<div class="eyebrow">Kinetic toy study</div>
<h1>Meshed Voxel Gears</h1>
<div class="subtitle">
Equal 12-tooth gears with a 30° tooth pitch and 15° meshing phase.
</div>
<div class="legend">
<div class="legend-item">
<span class="swatch cyan"></span>
A · counterclockwise
</div>
<div class="legend-item">
<span class="swatch orange"></span>
B · clockwise
</div>
<div class="legend-item">
<span class="swatch gold"></span>
Reference tooth
</div>
</div>
</section>
<div id="label-a" class="world-label">A · 12T · ↺</div>
<div id="label-b" class="world-label">B · 12T · ↻</div>
<div id="contact-label" class="contact-label">PITCH CONTACT</div>
<div class="panel help">Drag to orbit · Scroll to zoom</div>
<div class="panel controls">
<button id="pause" class="primary">Pause</button>
<button id="overview">Overview</button>
<button id="contact-view">Contact close-up</button>
<label class="speed">
Speed
<input
id="speed"
type="range"
min="0"
max="1.8"
step="0.05"
value="0.75"
aria-label="Animation speed"
>
</label>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer and 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.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute("aria-label", "Animated voxel gears");
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 12, 23);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
60
);
const hemi = new THREE.HemisphereLight(0xc7edff, 0x172033, 2.15);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xffffff, 3.2);
key.position.set(4.5, 7.5, 10);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -8;
key.shadow.camera.right = 8;
key.shadow.camera.top = 7;
key.shadow.camera.bottom = -7;
key.shadow.camera.near = 1;
key.shadow.camera.far = 24;
key.shadow.bias = -0.0005;
scene.add(key);
const fill = new THREE.DirectionalLight(0x69bfff, 1.45);
fill.position.set(-7, 2, 6);
scene.add(fill);
const rim = new THREE.DirectionalLight(0xffb36b, 1.2);
rim.position.set(4, -3, -6);
scene.add(rim);
const contactLight = new THREE.PointLight(0x9ef4ff, 2.5, 7, 2);
contactLight.position.set(0, 0, 3.2);
scene.add(contactLight);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const bodyMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.58,
metalness: 0.07
});
const faceMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.72,
metalness: 0.02
});
const darkMaterial = new THREE.MeshStandardMaterial({
color: 0x152334,
roughness: 0.72,
metalness: 0.18
});
const referenceMaterial = new THREE.MeshStandardMaterial({
color: 0xffd166,
emissive: 0x6c4700,
emissiveIntensity: 0.7,
roughness: 0.48,
metalness: 0.06
});
// ------------------------------------------------------------
// Shared dimensions
// ------------------------------------------------------------
const TOOTH_COUNT = 12;
const PITCH = (Math.PI * 2) / TOOTH_COUNT;
const HALF_PITCH = PITCH / 2;
const GRID = 0.18;
const BODY_RADIUS = 1.42;
const OUTER_RADIUS = 1.86;
const THICKNESS = 1.02;
const TOOTH_HALF_ANGLE = 0.13;
const GEAR_Y = -0.18;
const GEAR_X = 1.68;
function boxMesh(material, scale, position) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(...scale);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function hash(ix, iy, seed) {
let h = (ix * 73856093) ^ (iy * 19349663) ^ seed;
h = Math.imul(h ^ (h >>> 16), 2246822519);
return (h ^ (h >>> 13)) >>> 0;
}
// ------------------------------------------------------------
// Voxel gear construction
//
// The same 12 tooth sectors are used for both gears. Their local
// phase differs by half a pitch, so a tooth enters the opposing gap.
// ------------------------------------------------------------
function createGear({ x, bodyColor, seed }) {
const group = new THREE.Group();
group.position.set(x, GEAR_Y, 0);
const cells = [];
const baseColor = new THREE.Color(bodyColor);
const referenceColor = new THREE.Color(0xffd166);
const limit = Math.ceil(OUTER_RADIUS / GRID) + 1;
for (let iy = -limit; iy <= limit; iy++) {
for (let ix = -limit; ix <= limit; ix++) {
const px = ix * GRID;
const py = iy * GRID;
const radius = Math.hypot(px, py);
const angle = Math.atan2(py, px);
const inHub = radius <= 0.58;
const inRim = radius >= 1.07 && radius <= BODY_RADIUS;
const spokeIndex = Math.round(
(angle - Math.PI / 6) / (Math.PI * 2 / 6)
);
const spokeAngle = Math.PI / 6 + spokeIndex * (Math.PI * 2 / 6);
const spokeDelta = Math.abs(angle - spokeAngle);
const inSpoke =
radius >= 0.49 &&
radius <= 1.18 &&
spokeDelta <= 0.205;
const toothIndex = Math.round(angle / PITCH);
const toothAngle = toothIndex * PITCH;
const toothDelta = Math.abs(angle - toothAngle);
const isTooth =
radius >= 1.29 &&
radius <= OUTER_RADIUS &&
toothDelta <= TOOTH_HALF_ANGLE;
if (!(inHub || inRim || inSpoke || isTooth)) {
continue;
}
const normalizedToothIndex =
((toothIndex % TOOTH_COUNT) + TOOTH_COUNT) % TOOTH_COUNT;
const isReference = isTooth && normalizedToothIndex === 0;
const random = hash(ix, iy, seed);
const variation = ((random % 9) - 4) * 0.012;
const sideColor = (isReference ? referenceColor : baseColor.clone())
.offsetHSL(0, 0, variation);
const capColor = (isReference ? referenceColor : baseColor.clone())
.offsetHSL(0, 0, 0.075 + variation);
cells.push({
x: px,
y: py,
sideColor,
capColor,
isReference
});
}
}
const body = new THREE.InstancedMesh(
unitBox,
bodyMaterial,
cells.length
);
body.castShadow = true;
body.receiveShadow = true;
body.frustumCulled = false;
const faces = new THREE.InstancedMesh(
unitBox,
faceMaterial,
cells.length
);
faces.castShadow = true;
faces.receiveShadow = true;
faces.frustumCulled = false;
const matrix = new THREE.Matrix4();
const position = new THREE.Vector3();
const quaternion = new THREE.Quaternion();
const scale = new THREE.Vector3();
cells.forEach((cell, index) => {
position.set(cell.x, cell.y, 0);
scale.set(GRID * 0.96, GRID * 0.96, THICKNESS);
matrix.compose(position, quaternion, scale);
body.setMatrixAt(index, matrix);
body.setColorAt(index, cell.sideColor);
position.set(cell.x, cell.y, THICKNESS / 2 + 0.026);
scale.set(GRID * 0.88, GRID * 0.88, 0.052);
matrix.compose(position, quaternion, scale);
faces.setMatrixAt(index, matrix);
faces.setColorAt(index, cell.capColor);
});
body.instanceMatrix.needsUpdate = true;
faces.instanceMatrix.needsUpdate = true;
body.instanceColor.needsUpdate = true;
faces.instanceColor.needsUpdate = true;
group.add(body, faces);
// Square axle, hub, face plate and rotating orientation bar.
const axle = boxMesh(
darkMaterial,
[0.28, 0.28, 1.42],
[0, 0, -0.32]
);
group.add(axle);
const hub = boxMesh(
bodyMaterial,
[0.84, 0.84, THICKNESS + 0.14],
[0, 0, 0]
);
hub.material = bodyMaterial;
group.add(hub);
const hubPlate = boxMesh(
faceMaterial,
[0.67, 0.67, 0.10],
[0, 0, THICKNESS / 2 + 0.12]
);
group.add(hubPlate);
const axleCap = boxMesh(
darkMaterial,
[0.28, 0.28, 0.13],
[0, 0, THICKNESS / 2 + 0.20]
);
group.add(axleCap);
const orientationBar = boxMesh(
referenceMaterial,
[0.34, 0.09, 0.075],
[0.19, 0, THICKNESS / 2 + 0.205]
);
group.add(orientationBar);
// Raised marker on the reference tooth.
const marker = boxMesh(
referenceMaterial,
[0.22, 0.22, 0.12],
[1.63, 0, THICKNESS / 2 + 0.10]
);
group.add(marker);
scene.add(group);
return group;
}
const gearA = createGear({
x: -GEAR_X,
bodyColor: 0x35c5cf,
seed: 117
});
const gearB = createGear({
x: GEAR_X,
bodyColor: 0xff805e,
seed: 901
});
// ------------------------------------------------------------
// Voxel platform
// ------------------------------------------------------------
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.9,
metalness: 0.03
});
const floorTiles = [];
const floorStep = 0.66;
for (let iz = -3; iz <= 3; iz++) {
for (let ix = -7; ix <= 7; ix++) {
const x = ix * floorStep;
const z = iz * floorStep;
if (Math.abs(x) > 4.8 || Math.abs(z) > 2.2) {
continue;
}
floorTiles.push({ x, z, n: hash(ix, iz, 44) });
}
}
const floor = new THREE.InstancedMesh(
unitBox,
floorMaterial,
floorTiles.length
);
floor.receiveShadow = true;
floor.castShadow = true;
floor.frustumCulled = false;
const floorMatrix = new THREE.Matrix4();
const floorPosition = new THREE.Vector3();
const floorScale = new THREE.Vector3();
const floorQuaternion = new THREE.Quaternion();
floorTiles.forEach((tile, index) => {
floorPosition.set(tile.x, -2.28, tile.z);
floorScale.set(0.59, 0.22, 0.59);
floorMatrix.compose(floorPosition, floorQuaternion, floorScale);
floor.setMatrixAt(index, floorMatrix);
const shade = 0.55 + (tile.n % 5) * 0.018;
const color = new THREE.Color(0x182b3e).multiplyScalar(shade);
floor.setColorAt(index, color);
});
floor.instanceMatrix.needsUpdate = true;
floor.instanceColor.needsUpdate = true;
scene.add(floor);
// Small voxel blocks behind the mechanism for depth.
const dustMaterial = new THREE.MeshStandardMaterial({
color: 0x2a435b,
roughness: 0.85,
metalness: 0.02
});
for (let i = 0; i < 24; i++) {
const h = hash(i, 8, 310);
const h2 = hash(i, 19, 77);
const h3 = hash(i, 4, 611);
const block = boxMesh(
dustMaterial,
[0.10 + (h % 4) * 0.025, 0.10 + (h2 % 3) * 0.03, 0.10],
[
-6 + (h / 4294967295) * 12,
-1.7 + (h2 / 4294967295) * 5.4,
-2.5 - (h3 / 4294967295) * 2.5
]
);
block.castShadow = false;
scene.add(block);
}
// ------------------------------------------------------------
// Direction arrows made from voxel-like cuboids
// ------------------------------------------------------------
function createDirectionArrow(centerX, direction, color) {
const group = new THREE.Group();
group.position.set(centerX, 2.08, 0.76);
const material = new THREE.MeshBasicMaterial({ color });
for (let i = 0; i < 5; i++) {
const block = boxMesh(
material,
[0.13, 0.065, 0.065],
[-direction * (0.42 - i * 0.15), 0, 0]
);
block.castShadow = false;
group.add(block);
}
const tipX = direction * 0.46;
for (const side of [-1, 1]) {
const arm = boxMesh(
material,
[0.26, 0.06, 0.06],
[tipX - direction * 0.085, side * 0.072, 0]
);
arm.rotation.z = side * Math.PI / 4;
arm.castShadow = false;
group.add(arm);
}
scene.add(group);
return group;
}
createDirectionArrow(-GEAR_X, -1, 0x58d8df);
createDirectionArrow(GEAR_X, 1, 0xff9a62);
// ------------------------------------------------------------
// Contact bracket
// ------------------------------------------------------------
const contactMaterial = new THREE.MeshBasicMaterial({
color: 0xd8fbff,
transparent: true,
opacity: 0.68,
depthTest: false
});
const contactBracket = new THREE.Group();
contactBracket.position.set(0, GEAR_Y, 0.83);
contactBracket.renderOrder = 20;
const corner = 0.34;
for (const sx of [-1, 1]) {
for (const sy of [-1, 1]) {
const horizontal = boxMesh(
contactMaterial,
[0.16, 0.035, 0.035],
[sx * (corner - 0.08), sy * corner, 0]
);
const vertical = boxMesh(
contactMaterial,
[0.035, 0.16, 0.035],
[sx * corner, sy * (corner - 0.08), 0]
);
horizontal.castShadow = false;
vertical.castShadow = false;
horizontal.renderOrder = 20;
vertical.renderOrder = 20;
contactBracket.add(horizontal, vertical);
}
}
scene.add(contactBracket);
// ------------------------------------------------------------
// Direct camera controls: orbit and zoom without addons
// ------------------------------------------------------------
const cameraGoal = {
target: new THREE.Vector3(0, -0.08, 0),
radius: 10.6,
theta: 0.30,
phi: 1.08
};
const cameraCurrent = {
target: cameraGoal.target.clone(),
radius: cameraGoal.radius,
theta: cameraGoal.theta,
phi: cameraGoal.phi
};
const spherical = new THREE.Spherical();
const cameraOffset = new THREE.Vector3();
function updateCamera(delta) {
const smoothing = 1 - Math.exp(-delta * 8);
cameraCurrent.radius = THREE.MathUtils.lerp(
cameraCurrent.radius,
cameraGoal.radius,
smoothing
);
cameraCurrent.theta = THREE.MathUtils.lerp(
cameraCurrent.theta,
cameraGoal.theta,
smoothing
);
cameraCurrent.phi = THREE.MathUtils.lerp(
cameraCurrent.phi,
cameraGoal.phi,
smoothing
);
cameraCurrent.target.lerp(cameraGoal.target, smoothing);
spherical.set(
cameraCurrent.radius,
cameraCurrent.phi,
cameraCurrent.theta
);
cameraOffset.setFromSpherical(spherical);
camera.position.copy(cameraCurrent.target).add(cameraOffset);
camera.lookAt(cameraCurrent.target);
}
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraGoal.theta -= dx * 0.006;
cameraGoal.phi = THREE.MathUtils.clamp(
cameraGoal.phi - dy * 0.005,
0.55,
1.47
);
});
function endDrag(event) {
dragging = false;
renderer.domElement.classList.remove("dragging");
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();
cameraGoal.radius = THREE.MathUtils.clamp(
cameraGoal.radius * Math.exp(event.deltaY * 0.001),
5.0,
16.0
);
},
{ passive: false }
);
document.getElementById("overview").addEventListener("click", () => {
cameraGoal.target.set(0, -0.08, 0);
cameraGoal.radius = 10.6;
cameraGoal.theta = 0.30;
cameraGoal.phi = 1.08;
});
document.getElementById("contact-view").addEventListener("click", () => {
cameraGoal.target.set(0, GEAR_Y, 0);
cameraGoal.radius = 6.2;
cameraGoal.theta = 0.10;
cameraGoal.phi = 1.20;
});
let paused = false;
const pauseButton = document.getElementById("pause");
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
pauseButton.classList.toggle("primary", !paused);
});
let speed = Number(document.getElementById("speed").value);
document.getElementById("speed").addEventListener("input", (event) => {
speed = Number(event.target.value);
});
// ------------------------------------------------------------
// DOM labels projected from 3D positions
// ------------------------------------------------------------
const labelA = document.getElementById("label-a");
const labelB = document.getElementById("label-b");
const contactLabel = document.getElementById("contact-label");
const labelPosition = new THREE.Vector3();
function placeLabel(element, x, y, z) {
labelPosition.set(x, y, z).project(camera);
const visible =
labelPosition.z > -1 &&
labelPosition.z < 1 &&
Math.abs(labelPosition.x) < 1.25 &&
Math.abs(labelPosition.y) < 1.25;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const screenX = (labelPosition.x * 0.5 + 0.5) * window.innerWidth;
const screenY = (-labelPosition.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${screenX}px`;
element.style.top = `${screenY}px`;
}
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
let angle = 0;
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (!paused) {
angle += delta * speed;
}
// Equal external gears rotate at equal and opposite rates.
// The half-pitch phase places gear A's reference tooth into
// gear B's opposing gap at the contact line.
gearA.rotation.z = angle;
gearB.rotation.z = HALF_PITCH - angle;
contactMaterial.opacity = 0.52 + Math.sin(performance.now() * 0.004) * 0.16;
contactLight.intensity = 2.15 + Math.sin(performance.now() * 0.004) * 0.45;
updateCamera(delta);
placeLabel(labelA, -GEAR_X, GEAR_Y + 2.55, 0.3);
placeLabel(labelB, GEAR_X, GEAR_Y + 2.55, 0.3);
placeLabel(contactLabel, 0, GEAR_Y - 0.65, 1.0);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
updateCamera(1);
animate();
</script>
</body>
</html>
```