Prompt specification
Build or customize the Three.js 3D scene "Equal and Unequal Wheel-Speed Modes" 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: Equal and Unequal Wheel-Speed Modes
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/equal-and-unequal-wheel-speed-modes-x2022
Scene Description Prompt:
Show a voxel ideal differential switching between equal and unequal wheel speeds while their average follows the carrier. Three.js from cdnjs, one HTML file.
Working Three.js Starter (equal-and-unequal-wheel-speed-modes-x2022):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Ideal Differential</title>
<style>
:root {
color-scheme: dark;
--bg: #071018;
--panel: rgba(11, 23, 34, 0.88);
--line: rgba(148, 190, 211, 0.2);
--text: #edf8ff;
--muted: #8fa9b8;
--cyan: #50e3ff;
--orange: #ff9d55;
--yellow: #ffe06b;
--green: #72f2a6;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--text);
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
}
.panel {
pointer-events: auto;
border: 1px solid var(--line);
background: var(--panel);
backdrop-filter: blur(16px);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
}
.intro {
position: absolute;
top: 24px;
left: 24px;
width: min(420px, calc(100vw - 48px));
padding: 20px 22px;
border-radius: 18px;
}
.eyebrow {
margin-bottom: 8px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(24px, 3vw, 36px);
line-height: 1;
letter-spacing: -0.04em;
}
.subtitle {
margin: 11px 0 16px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.equation {
display: inline-flex;
align-items: center;
gap: 9px;
padding: 9px 12px;
border: 1px solid rgba(114, 242, 166, 0.26);
border-radius: 10px;
background: rgba(114, 242, 166, 0.07);
color: var(--green);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 13px;
font-weight: 700;
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 16px;
}
#status {
display: flex;
align-items: center;
gap: 8px;
color: var(--yellow);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#status::before {
width: 8px;
height: 8px;
border-radius: 2px;
background: currentColor;
box-shadow: 0 0 14px currentColor;
content: "";
}
.controls {
position: absolute;
top: 24px;
right: 24px;
width: 280px;
padding: 18px;
border-radius: 18px;
}
.control-title {
margin-bottom: 13px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
button {
width: 100%;
padding: 12px 14px;
border: 1px solid rgba(80, 227, 255, 0.35);
border-radius: 11px;
background: rgba(80, 227, 255, 0.12);
color: var(--text);
font: inherit;
font-size: 13px;
font-weight: 800;
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease,
border-color 120ms ease;
}
button:hover {
transform: translateY(-1px);
border-color: var(--cyan);
background: rgba(80, 227, 255, 0.19);
}
button:active {
transform: translateY(1px);
}
.toggle-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 13px;
color: var(--muted);
font-size: 12px;
}
.toggle-row input {
width: 17px;
height: 17px;
accent-color: var(--cyan);
}
.slider-block {
margin-top: 19px;
}
.slider-label {
display: flex;
justify-content: space-between;
margin-bottom: 9px;
color: var(--muted);
font-size: 12px;
}
#carrierValue {
color: var(--yellow);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-weight: 800;
}
input[type="range"] {
width: 100%;
accent-color: var(--cyan);
cursor: pointer;
}
.legend {
display: grid;
gap: 8px;
margin-top: 17px;
padding-top: 15px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 11px;
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 2px;
box-shadow: 0 0 10px currentColor;
}
.metrics {
position: absolute;
right: 24px;
bottom: 24px;
left: 24px;
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 10px;
}
.metric {
min-width: 0;
padding: 14px 16px;
border-radius: 14px;
}
.metric-label {
display: flex;
align-items: center;
gap: 7px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.metric-dot {
width: 7px;
height: 7px;
border-radius: 2px;
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
.metric-value {
margin-top: 7px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: clamp(18px, 2.4vw, 28px);
font-weight: 800;
letter-spacing: -0.04em;
white-space: nowrap;
}
.metric-value small {
color: var(--muted);
font-size: 10px;
font-weight: 600;
letter-spacing: 0;
}
.bar {
height: 4px;
margin-top: 10px;
overflow: hidden;
border-radius: 4px;
background: rgba(255, 255, 255, 0.08);
}
.bar > span {
display: block;
width: 50%;
height: 100%;
border-radius: inherit;
background: currentColor;
box-shadow: 0 0 12px currentColor;
transition: width 80ms linear;
}
.hint {
position: absolute;
bottom: 154px;
left: 24px;
color: rgba(205, 228, 239, 0.55);
font-size: 11px;
letter-spacing: 0.03em;
}
.tag {
position: absolute;
z-index: 3;
padding: 7px 9px;
transform: translate(-50%, -100%);
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(7, 16, 24, 0.86);
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.25);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
font-weight: 800;
white-space: nowrap;
pointer-events: none;
}
.tag::after {
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 18px;
background: currentColor;
opacity: 0.55;
content: "";
}
#carrierTag {
color: var(--yellow);
}
#leftTag {
color: var(--cyan);
}
#rightTag {
color: var(--orange);
}
@media (max-width: 850px) {
.intro {
top: 14px;
left: 14px;
width: min(360px, calc(100vw - 28px));
padding: 16px;
}
.subtitle {
display: none;
}
.controls {
top: auto;
right: 14px;
bottom: 148px;
width: 230px;
padding: 14px;
}
.legend,
.hint {
display: none;
}
.metrics {
right: 14px;
bottom: 14px;
left: 14px;
gap: 6px;
}
.metric {
padding: 11px 9px;
}
.metric-label {
font-size: 8px;
}
.metric-value {
font-size: 16px;
}
.metric-value small {
display: block;
margin-top: 2px;
}
.tag {
display: none;
}
}
@media (max-width: 540px) {
.controls {
right: 14px;
bottom: 144px;
width: 205px;
}
.control-title {
margin-bottom: 8px;
}
.slider-block {
margin-top: 13px;
}
.metrics {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.metric {
padding: 9px;
}
.bar {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="hud">
<section class="panel intro">
<div class="eyebrow">Mechanism Lab / 01</div>
<h1>Voxel Differential</h1>
<p class="subtitle">
Switch between equal and unequal output speeds. In an ideal
differential, the average wheel speed always follows the carrier.
</p>
<div class="equation">
ω<sub>c</sub> = (ω<sub>L</sub> + ω<sub>R</sub>) / 2
</div>
<div class="status-row">
<div id="status">Equal output</div>
</div>
</section>
<section class="panel controls">
<div class="control-title">Controls</div>
<button id="modeButton" type="button">
Switch to unequal
</button>
<label class="toggle-row">
<span>Automatic switching</span>
<input id="autoSwitch" type="checkbox" checked>
</label>
<div class="slider-block">
<div class="slider-label">
<span>Carrier speed</span>
<span id="carrierValue">60 rpm</span>
</div>
<input
id="carrierSlider"
type="range"
min="20"
max="120"
step="1"
value="60"
aria-label="Carrier speed"
>
</div>
<div class="legend">
<div class="legend-item">
<span class="swatch" style="color:#ffe06b;background:#ffe06b"></span>
Carrier input
</div>
<div class="legend-item">
<span class="swatch" style="color:#50e3ff;background:#50e3ff"></span>
Left output
</div>
<div class="legend-item">
<span class="swatch" style="color:#ff9d55;background:#ff9d55"></span>
Right output
</div>
</div>
</section>
<div class="hint">
Drag to orbit · Scroll to zoom · Space to switch mode
</div>
<section class="metrics">
<div class="metric" style="color:#ffe06b">
<div class="metric-label">
<span class="metric-dot"></span>
Carrier
</div>
<div class="metric-value">
<span id="carrierMetric">60.0</span>
<small>rpm</small>
</div>
<div class="bar"><span id="carrierBar"></span></div>
</div>
<div class="metric" style="color:#50e3ff">
<div class="metric-label">
<span class="metric-dot"></span>
Left wheel
</div>
<div class="metric-value">
<span id="leftMetric">60.0</span>
<small>rpm</small>
</div>
<div class="bar"><span id="leftBar"></span></div>
</div>
<div class="metric" style="color:#ff9d55">
<div class="metric-label">
<span class="metric-dot"></span>
Right wheel
</div>
<div class="metric-value">
<span id="rightMetric">60.0</span>
<small>rpm</small>
</div>
<div class="bar"><span id="rightBar"></span></div>
</div>
<div class="metric" style="color:#72f2a6">
<div class="metric-label">
<span class="metric-dot"></span>
Wheel average
</div>
<div class="metric-value">
<span id="averageMetric">60.0</span>
<small>rpm</small>
</div>
<div class="bar"><span id="averageBar"></span></div>
</div>
</section>
<div id="carrierTag" class="tag">CARRIER</div>
<div id="leftTag" class="tag">LEFT</div>
<div id="rightTag" class="tag">RIGHT</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Scene
// ------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071018);
scene.fog = new THREE.FogExp2(0x071018, 0.055);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
document.querySelector("#scene").appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xbdeeff, 0x10202b, 1.8);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xffffff, 3.2);
key.position.set(4.5, 8, 5.5);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -7;
key.shadow.camera.right = 7;
key.shadow.camera.top = 7;
key.shadow.camera.bottom = -7;
key.shadow.normalBias = 0.025;
scene.add(key);
const rim = new THREE.DirectionalLight(0x50e3ff, 2.1);
rim.position.set(-5, 3, -4);
scene.add(rim);
const warm = new THREE.PointLight(0xff9d55, 18, 8, 2);
warm.position.set(3.5, 2.5, -2.5);
scene.add(warm);
// ------------------------------------------------------------
// Shared materials and voxel helpers
// ------------------------------------------------------------
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const materials = {
ground: new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.95,
metalness: 0.02
}),
dark: new THREE.MeshStandardMaterial({
color: 0x162833,
roughness: 0.65,
metalness: 0.38
}),
darker: new THREE.MeshStandardMaterial({
color: 0x0a151d,
roughness: 0.72,
metalness: 0.32
}),
tire: new THREE.MeshStandardMaterial({
color: 0x101b23,
roughness: 0.92,
metalness: 0.04
}),
metal: new THREE.MeshStandardMaterial({
color: 0x8ca7b4,
roughness: 0.35,
metalness: 0.72
}),
carrier: new THREE.MeshStandardMaterial({
color: 0xffe06b,
emissive: 0x8a5e00,
emissiveIntensity: 0.55,
roughness: 0.4,
metalness: 0.48
}),
carrierDark: new THREE.MeshStandardMaterial({
color: 0xb98e22,
roughness: 0.5,
metalness: 0.58
}),
left: new THREE.MeshStandardMaterial({
color: 0x50e3ff,
emissive: 0x006779,
emissiveIntensity: 0.42,
roughness: 0.42,
metalness: 0.42
}),
right: new THREE.MeshStandardMaterial({
color: 0xff9d55,
emissive: 0x7d3000,
emissiveIntensity: 0.42,
roughness: 0.42,
metalness: 0.42
}),
green: new THREE.MeshStandardMaterial({
color: 0x72f2a6,
emissive: 0x0e6d35,
emissiveIntensity: 0.45,
roughness: 0.42,
metalness: 0.4
})
};
function voxel(
width,
height,
depth,
material,
x = 0,
y = 0,
z = 0,
parent = scene
) {
const mesh = new THREE.Mesh(cubeGeometry, material);
mesh.scale.set(width, height, depth);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// ------------------------------------------------------------
// Voxel ground
// ------------------------------------------------------------
const tileCount = 23;
const tileGeometry = new THREE.BoxGeometry(0.62, 1, 0.62);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.92,
metalness: 0.04
});
const tiles = new THREE.InstancedMesh(
tileGeometry,
tileMaterial,
tileCount * tileCount
);
const dummy = new THREE.Object3D();
const tileColor = new THREE.Color();
let tileIndex = 0;
for (let x = 0; x < tileCount; x++) {
for (let z = 0; z < tileCount; z++) {
const px = (x - (tileCount - 1) / 2) * 0.66;
const pz = (z - (tileCount - 1) / 2) * 0.66;
const height = 0.045 + ((x * 7 + z * 11) % 4) * 0.018;
dummy.position.set(px, -height / 2, pz);
dummy.scale.set(1, height, 1);
dummy.updateMatrix();
tiles.setMatrixAt(tileIndex, dummy.matrix);
const checker = (x + z) % 2;
tileColor.set(checker ? 0x102530 : 0x0b1c26);
tiles.setColorAt(tileIndex, tileColor);
tileIndex++;
}
}
tiles.receiveShadow = true;
scene.add(tiles);
// A brighter central service lane.
for (let i = -8; i <= 8; i++) {
voxel(
0.56,
0.018,
0.56,
i % 2 === 0 ? materials.darker : materials.dark,
i * 0.66,
0.015,
0
);
}
// ------------------------------------------------------------
// Bench / supports
// ------------------------------------------------------------
voxel(5.8, 0.16, 0.34, materials.dark, 0, 0.18, 0);
voxel(0.34, 0.16, 2.4, materials.dark, 0, 0.18, 0);
function support(x) {
voxel(0.34, 0.9, 0.42, materials.darker, x, 0.64, 0);
voxel(0.58, 0.14, 0.64, materials.metal, x, 1.12, 0);
}
support(-2.35);
support(0);
support(2.35);
// ------------------------------------------------------------
// Central rotating carrier
// ------------------------------------------------------------
const carrierGroup = new THREE.Group();
carrierGroup.position.set(0, 1.3, 0);
scene.add(carrierGroup);
voxel(0.82, 0.82, 0.82, materials.carrier, 0, 0, 0, carrierGroup);
// Colored voxel bands make the carrier rotation readable.
voxel(0.92, 0.18, 0.92, materials.carrierDark, 0, 0.29, 0, carrierGroup);
voxel(0.92, 0.18, 0.92, materials.carrierDark, 0, -0.29, 0, carrierGroup);
voxel(0.18, 0.92, 0.92, materials.green, 0.35, 0, 0, carrierGroup);
voxel(0.18, 0.92, 0.92, materials.green, -0.35, 0, 0, carrierGroup);
// Voxel ring gear around the carrier.
const carrierRing = new THREE.Group();
carrierGroup.add(carrierRing);
for (let i = 0; i < 20; i++) {
const angle = (i / 20) * Math.PI * 2;
const tooth = voxel(
0.46,
0.2,
0.24,
materials.carrierDark,
0,
Math.sin(angle) * 0.98,
Math.cos(angle) * 0.98,
carrierRing
);
tooth.rotation.x = -angle;
}
// ------------------------------------------------------------
// Output gears and voxel wheels
// ------------------------------------------------------------
function createOutput(side, accentMaterial) {
const sideName = side < 0 ? "left" : "right";
// Side bevel-style gear.
const gear = new THREE.Group();
gear.position.set(side * 0.91, 1.3, 0);
scene.add(gear);
for (let i = 0; i < 12; i++) {
const angle = (i / 12) * Math.PI * 2;
const tooth = voxel(
0.3,
0.18,
0.2,
accentMaterial,
0,
Math.sin(angle) * 0.46,
Math.cos(angle) * 0.46,
gear
);
tooth.rotation.x = -angle;
}
voxel(0.35, 0.22, 0.22, materials.metal, 0, 0, 0, gear);
// Main wheel pivot. Its axis is the X axis.
const wheel = new THREE.Group();
wheel.position.set(side * 2.38, 1.3, 0);
scene.add(wheel);
// Chunky voxel tire.
for (let i = 0; i < 18; i++) {
const angle = (i / 18) * Math.PI * 2;
const segment = voxel(
0.48,
0.28,
0.2,
materials.tire,
0,
Math.sin(angle) * 0.72,
Math.cos(angle) * 0.72,
wheel
);
segment.rotation.x = -angle;
}
// Inner metallic ring.
for (let i = 0; i < 14; i++) {
const angle = (i / 14) * Math.PI * 2;
const segment = voxel(
0.52,
0.13,
0.15,
materials.metal,
0,
Math.sin(angle) * 0.53,
Math.cos(angle) * 0.53,
wheel
);
segment.rotation.x = -angle;
}
// Four blocky spokes.
for (let i = 0; i < 4; i++) {
const angle = (i / 4) * Math.PI * 2;
const spoke = voxel(
0.2,
0.14,
0.7,
i === 0 ? accentMaterial : materials.dark,
0,
Math.sin(angle) * 0.28,
Math.cos(angle) * 0.28,
wheel
);
spoke.rotation.x = -angle;
}
voxel(0.58, 0.3, 0.58, accentMaterial, 0, 0, 0, wheel);
voxel(0.68, 0.12, 0.68, materials.metal, 0, 0, 0, wheel);
// Square output shaft, built from rotating voxel segments.
for (let i = 0; i < 6; i++) {
voxel(
0.18,
0.16,
0.16,
i % 2 ? materials.metal : accentMaterial,
-side * (0.36 + i * 0.28),
0,
0,
wheel
);
}
// Small direction marker on the shaft.
voxel(
0.1,
0.24,
0.24,
materials.green,
-side * 0.57,
0,
0,
wheel
);
return { gear, wheel, sideName };
}
const leftOutput = createOutput(-1, materials.left);
const rightOutput = createOutput(1, materials.right);
// Central carrier shaft running along X.
const carrierShaft = new THREE.Group();
carrierShaft.position.set(0, 1.3, 0);
scene.add(carrierShaft);
for (let i = -4; i <= 4; i++) {
voxel(
0.18,
0.15,
0.15,
i % 2 ? materials.carrierDark : materials.metal,
i * 0.2,
0,
0,
carrierShaft
);
}
// ------------------------------------------------------------
// Camera controls implemented directly
// ------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 1.12, 0);
let cameraTheta = 0.62;
let cameraPhi = 0.98;
let cameraRadius = 8.5;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.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.48,
1.43
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (
event.pointerId !== undefined &&
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();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
5.7,
14
);
updateCamera();
}, { passive: false });
renderer.domElement.addEventListener("dblclick", () => {
cameraTheta = 0.62;
cameraPhi = 0.98;
cameraRadius = 8.5;
updateCamera();
});
updateCamera();
// ------------------------------------------------------------
// Differential state and UI
// ------------------------------------------------------------
const carrierSlider = document.querySelector("#carrierSlider");
const carrierValue = document.querySelector("#carrierValue");
const autoSwitch = document.querySelector("#autoSwitch");
const modeButton = document.querySelector("#modeButton");
const statusElement = document.querySelector("#status");
const carrierMetric = document.querySelector("#carrierMetric");
const leftMetric = document.querySelector("#leftMetric");
const rightMetric = document.querySelector("#rightMetric");
const averageMetric = document.querySelector("#averageMetric");
const carrierBar = document.querySelector("#carrierBar");
const leftBar = document.querySelector("#leftBar");
const rightBar = document.querySelector("#rightBar");
const averageBar = document.querySelector("#averageBar");
const carrierTag = document.querySelector("#carrierTag");
const leftTag = document.querySelector("#leftTag");
const rightTag = document.querySelector("#rightTag");
let carrierRPM = Number(carrierSlider.value);
let targetBlend = 0; // 0 = equal, 1 = unequal
let blend = 0;
let autoTimer = 0;
const speedBias = 0.58;
function setTarget(value) {
targetBlend = value;
autoTimer = 0;
modeButton.textContent =
targetBlend > 0.5 ? "Switch to equal" : "Switch to unequal";
}
function toggleMode() {
autoSwitch.checked = false;
setTarget(targetBlend > 0.5 ? 0 : 1);
}
modeButton.addEventListener("click", toggleMode);
autoSwitch.addEventListener("change", () => {
autoTimer = 0;
});
carrierSlider.addEventListener("input", () => {
carrierRPM = Number(carrierSlider.value);
carrierValue.textContent = `${carrierRPM} rpm`;
});
window.addEventListener("keydown", event => {
if (event.code === "Space") {
event.preventDefault();
toggleMode();
}
});
// ------------------------------------------------------------
// DOM labels projected into the 3D scene
// ------------------------------------------------------------
const projected = new THREE.Vector3();
function positionTag(element, x, y, z) {
projected.set(x, y, z).project(camera);
element.style.left =
`${(projected.x * 0.5 + 0.5) * innerWidth}px`;
element.style.top =
`${(-projected.y * 0.5 + 0.5) * innerHeight}px`;
}
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
const rpmToRad = Math.PI * 2 / 60;
let carrierAngle = 0;
let leftAngle = 0;
let rightAngle = 0;
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
// Automatic switching every 5.5 seconds.
if (autoSwitch.checked) {
autoTimer += dt;
if (autoTimer >= 5.5) {
setTarget(targetBlend > 0.5 ? 0 : 1);
}
}
// Smoothly transition without changing the average speed.
const smoothing = 1 - Math.exp(-dt * 4.2);
blend += (targetBlend - blend) * smoothing;
if (Math.abs(targetBlend - blend) < 0.0001) {
blend = targetBlend;
}
// Ideal differential relationships:
// left + right = 2 * carrier
// left = carrier * (1 + bias * blend)
// right = carrier * (1 - bias * blend)
const leftRPM = carrierRPM * (1 + speedBias * blend);
const rightRPM = carrierRPM * (1 - speedBias * blend);
const averageRPM = (leftRPM + rightRPM) / 2;
carrierAngle += carrierRPM * rpmToRad * dt;
leftAngle += leftRPM * rpmToRad * dt;
rightAngle += rightRPM * rpmToRad * dt;
carrierGroup.rotation.x = carrierAngle;
carrierShaft.rotation.x = carrierAngle;
leftOutput.gear.rotation.x = leftAngle;
leftOutput.wheel.rotation.x = leftAngle;
rightOutput.gear.rotation.x = rightAngle;
rightOutput.wheel.rotation.x = rightAngle;
// Gentle emissive pulse on the carrier.
materials.carrier.emissiveIntensity =
0.48 + Math.sin(performance.now() * 0.003) * 0.12;
// UI readouts.
carrierMetric.textContent = carrierRPM.toFixed(1);
leftMetric.textContent = leftRPM.toFixed(1);
rightMetric.textContent = rightRPM.toFixed(1);
averageMetric.textContent = averageRPM.toFixed(1);
const scaleMax = carrierRPM * 1.7;
carrierBar.style.width = `${carrierRPM / scaleMax * 100}%`;
leftBar.style.width = `${leftRPM / scaleMax * 100}%`;
rightBar.style.width = `${rightRPM / scaleMax * 100}%`;
averageBar.style.width = `${averageRPM / scaleMax * 100}%`;
const changing = Math.abs(targetBlend - blend) > 0.025;
if (changing) {
statusElement.textContent = "Switching outputs";
statusElement.style.color = "#50e3ff";
} else if (targetBlend > 0.5) {
statusElement.textContent = "Unequal output";
statusElement.style.color = "#ff9d55";
} else {
statusElement.textContent = "Equal output";
statusElement.style.color = "#ffe06b";
}
carrierTag.textContent = `CARRIER ${carrierRPM.toFixed(1)} rpm`;
leftTag.textContent = `LEFT ${leftRPM.toFixed(1)} rpm`;
rightTag.textContent = `RIGHT ${rightRPM.toFixed(1)} rpm`;
positionTag(carrierTag, 0, 2.65, 0);
positionTag(leftTag, -2.38, 2.5, 0);
positionTag(rightTag, 2.38, 2.5, 0);
renderer.render(scene, camera);
}
// ------------------------------------------------------------
// Resize
// ------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.fov = camera.aspect < 1 ? 54 : 42;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
});
animate();
</script>
</body>
</html>
```