Prompt specification
Build or customize the Three.js 3D scene "Differential Output Axes and Carrier" 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: Differential Output Axes and Carrier
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/differential-output-axes-and-carrier-x2021
Scene Description Prompt:
Create a standalone cutaway scene in one HTML file using Three.js from cdnjs. Model a symmetric ideal differential with two output shafts and a carrier. Display three rotation coordinates but only two independent degrees of freedom under the gear constraint.
Working Three.js Starter (differential-output-axes-and-carrier-x2021):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Symmetric Ideal Differential — Cutaway</title>
<style>
:root {
color-scheme: dark;
--bg: #071019;
--panel: rgba(10, 21, 31, 0.86);
--border: rgba(154, 196, 218, 0.18);
--text: #eaf5fb;
--muted: #91a9b7;
--carrier: #4e9cff;
--left: #ffad45;
--right: #36d7c4;
--good: #6ee7a8;
}
* {
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);
}
#app {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
#hud {
position: fixed;
z-index: 10;
top: 18px;
left: 18px;
width: min(390px, calc(100vw - 36px));
max-height: calc(100vh - 36px);
overflow: auto;
padding: 18px;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
box-shadow:
0 24px 70px rgba(0, 0, 0, 0.38),
inset 0 1px rgba(255, 255, 255, 0.035);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.eyebrow {
margin-bottom: 7px;
color: #7ea0b3;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 24px;
font-weight: 680;
line-height: 1.15;
letter-spacing: -0.035em;
}
.badge {
display: flex;
align-items: center;
gap: 8px;
width: fit-content;
margin: 12px 0 17px;
padding: 7px 10px;
border: 1px solid rgba(110, 231, 168, 0.23);
border-radius: 999px;
background: rgba(110, 231, 168, 0.07);
color: var(--good);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.035em;
}
.badge::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 12px currentColor;
content: "";
}
.section-title {
display: flex;
justify-content: space-between;
align-items: center;
margin: 16px 0 8px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.coordinates {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 8px;
}
.coordinate {
min-width: 0;
padding: 10px;
border: 1px solid var(--border);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
}
.coordinate .name {
display: flex;
align-items: center;
gap: 6px;
overflow: hidden;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-overflow: ellipsis;
text-transform: uppercase;
white-space: nowrap;
}
.coordinate .swatch {
flex: 0 0 auto;
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--color);
box-shadow: 0 0 9px var(--color);
}
.coordinate .value {
margin-top: 7px;
font-variant-numeric: tabular-nums;
font-size: 18px;
font-weight: 690;
letter-spacing: -0.035em;
white-space: nowrap;
}
.constraint {
margin-top: 12px;
padding: 12px;
border: 1px solid rgba(110, 231, 168, 0.22);
border-radius: 11px;
background: rgba(110, 231, 168, 0.055);
}
.constraint-title {
display: flex;
justify-content: space-between;
align-items: center;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#lock {
color: var(--good);
}
.equation {
margin-top: 8px;
font-family: Georgia, "Times New Roman", serif;
font-size: 19px;
letter-spacing: 0.02em;
}
.equation sub {
font-size: 0.62em;
}
.residual {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: var(--muted);
font-size: 10px;
}
.residual b {
color: var(--good);
font-variant-numeric: tabular-nums;
}
.mapping {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-top: 8px;
}
.mapping div {
padding: 8px 9px;
border: 1px solid var(--border);
border-radius: 8px;
color: var(--muted);
font-size: 10px;
}
.mapping strong {
display: block;
margin-bottom: 2px;
color: var(--text);
font-size: 12px;
}
.control {
margin-top: 12px;
}
.control label {
display: flex;
justify-content: space-between;
margin-bottom: 7px;
color: #bed0db;
font-size: 11px;
}
.control output {
color: var(--text);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
appearance: none;
border-radius: 999px;
outline: none;
background: linear-gradient(
90deg,
rgba(78, 156, 255, 0.85),
rgba(54, 215, 196, 0.75)
);
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
appearance: none;
border: 2px solid #dff7ff;
border-radius: 50%;
background: #163044;
box-shadow: 0 0 0 4px rgba(78, 156, 255, 0.12);
cursor: ew-resize;
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 2px solid #dff7ff;
border-radius: 50%;
background: #163044;
cursor: ew-resize;
}
.buttons {
display: flex;
gap: 7px;
margin-top: 15px;
}
button {
flex: 1;
min-height: 34px;
padding: 7px 10px;
border: 1px solid var(--border);
border-radius: 8px;
background: rgba(255, 255, 255, 0.045);
color: var(--text);
font: inherit;
font-size: 11px;
cursor: pointer;
transition:
background 140ms ease,
border-color 140ms ease,
transform 140ms ease;
}
button:hover {
border-color: rgba(174, 218, 238, 0.4);
background: rgba(255, 255, 255, 0.085);
}
button:active {
transform: translateY(1px);
}
#run {
border-color: rgba(110, 231, 168, 0.3);
color: var(--good);
}
.note {
margin: 12px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
.part-label {
position: fixed;
z-index: 5;
pointer-events: none;
padding: 5px 8px;
transform: translate(-50%, -50%);
border: 1px solid color-mix(in srgb, var(--color) 40%, transparent);
border-radius: 6px;
background: rgba(7, 16, 25, 0.72);
color: var(--color);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
white-space: nowrap;
backdrop-filter: blur(6px);
}
#legend {
position: fixed;
z-index: 6;
right: 18px;
bottom: 18px;
display: flex;
flex-wrap: wrap;
gap: 8px 14px;
max-width: calc(100vw - 36px);
padding: 10px 12px;
border: 1px solid var(--border);
border-radius: 10px;
background: rgba(7, 16, 25, 0.72);
color: var(--muted);
font-size: 10px;
backdrop-filter: blur(10px);
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.legend-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--color);
box-shadow: 0 0 8px var(--color);
}
#hint {
position: fixed;
z-index: 5;
left: 50%;
bottom: 18px;
transform: translateX(-50%);
color: rgba(194, 215, 226, 0.62);
font-size: 10px;
letter-spacing: 0.035em;
white-space: nowrap;
pointer-events: none;
}
@media (max-width: 760px) {
#hud {
top: 10px;
left: 10px;
width: min(355px, calc(100vw - 20px));
max-height: calc(100vh - 20px);
padding: 14px;
}
h1 {
font-size: 20px;
}
.part-label {
display: none;
}
#hint {
display: none;
}
#legend {
right: 10px;
bottom: 10px;
}
}
</style>
</head>
<body>
<div id="app"></div>
<section id="hud">
<div class="eyebrow">Mechanism study · interactive cutaway</div>
<h1>Symmetric Ideal Differential</h1>
<div class="badge">3 rotation coordinates · 2 independent DOF</div>
<div class="section-title">
<span>Rotation coordinates</span>
<span>about the output axis X</span>
</div>
<div class="coordinates">
<div class="coordinate" style="--color: var(--carrier)">
<div class="name">
<span class="swatch"></span>
Carrier φ<sub>C</sub>
</div>
<div class="value" id="valC">+0.0°</div>
</div>
<div class="coordinate" style="--color: var(--left)">
<div class="name">
<span class="swatch"></span>
Left φ<sub>L</sub>
</div>
<div class="value" id="valL">+0.0°</div>
</div>
<div class="coordinate" style="--color: var(--right)">
<div class="name">
<span class="swatch"></span>
Right φ<sub>R</sub>
</div>
<div class="value" id="valR">+0.0°</div>
</div>
</div>
<div class="constraint">
<div class="constraint-title">
<span>Gear constraint</span>
<span id="lock">● Locked</span>
</div>
<div class="equation">
φ<sub>L</sub> + φ<sub>R</sub> − 2φ<sub>C</sub> = 0
</div>
<div class="residual">
<span>Constraint residual</span>
<b id="residual">0.000°</b>
</div>
</div>
<div class="section-title">
<span>Independent coordinates</span>
<span>only two sliders</span>
</div>
<div class="mapping">
<div>
<strong>q₁ = φ<sub>C</sub></strong>
carrier / mean output
</div>
<div>
<strong>q₂ = ½(φ<sub>L</sub> − φ<sub>R</sub>)</strong>
differential split
</div>
</div>
<div class="control">
<label for="q1">
<span>q₁ · Carrier angle</span>
<output id="q1Value">+0.0°</output>
</label>
<input id="q1" type="range" min="-180" max="180" step="0.1" value="20">
</div>
<div class="control">
<label for="q2">
<span>q₂ · Output split</span>
<output id="q2Value">+0.0°</output>
</label>
<input id="q2" type="range" min="-180" max="180" step="0.1" value="0">
</div>
<div class="buttons">
<button id="run" type="button">Pause</button>
<button id="reset" type="button">Reset</button>
<button id="view" type="button">Reset view</button>
</div>
<p class="note">
The carrier and both outputs rotate about X. The right output is
reconstructed from the two independent coordinates, so the ideal
differential has one kinematic constraint and two remaining DOF.
</p>
</section>
<div
id="labelCarrier"
class="part-label"
style="--color: var(--carrier)"
>
Carrier / cross pin
</div>
<div
id="labelLeft"
class="part-label"
style="--color: var(--left)"
>
Left output
</div>
<div
id="labelRight"
class="part-label"
style="--color: var(--right)"
>
Right output
</div>
<div id="legend">
<span class="legend-item" style="--color: var(--carrier)">
<span class="legend-dot"></span> Carrier
</span>
<span class="legend-item" style="--color: var(--left)">
<span class="legend-dot"></span> Left output
</span>
<span class="legend-item" style="--color: var(--right)">
<span class="legend-dot"></span> Right output
</span>
<span class="legend-item" style="--color: #d9a441">
<span class="legend-dot"></span> Spider gears
</span>
</div>
<div id="hint">Drag to orbit · Wheel to zoom · Double-click to reset view</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const TAU = Math.PI * 2;
const DEG = Math.PI / 180;
const app = document.getElementById("app");
// ------------------------------------------------------------------
// Renderer, scene, camera
// ------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
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;
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071019);
scene.fog = new THREE.FogExp2(0x071019, 0.026);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const orbit = {
theta: 0.72,
phi: 1.17,
radius: 8.2
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.radius * Math.cos(orbit.phi),
orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(0, -0.04, 0);
}
updateCamera();
// ------------------------------------------------------------------
// Lighting
// ------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xb6dcff, 0x111820, 1.75));
const keyLight = new THREE.DirectionalLight(0xfff1d7, 3.2);
keyLight.position.set(4.5, 7.0, 5.0);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 5;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 20;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4aa8ff, 2.0);
rimLight.position.set(-5, 2.5, -4);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffa64d, 18, 12, 2);
warmLight.position.set(2.5, 1.2, 2.7);
scene.add(warmLight);
// ------------------------------------------------------------------
// Materials and geometry helpers
// ------------------------------------------------------------------
const materials = {
steel: new THREE.MeshStandardMaterial({
color: 0x718391,
metalness: 0.84,
roughness: 0.26
}),
darkSteel: new THREE.MeshStandardMaterial({
color: 0x253541,
metalness: 0.76,
roughness: 0.34
}),
carrier: new THREE.MeshStandardMaterial({
color: 0x287dc9,
emissive: 0x071d33,
metalness: 0.55,
roughness: 0.25
}),
left: new THREE.MeshStandardMaterial({
color: 0xe48728,
emissive: 0x2b1103,
metalness: 0.68,
roughness: 0.24
}),
right: new THREE.MeshStandardMaterial({
color: 0x18aa9d,
emissive: 0x032a28,
metalness: 0.68,
roughness: 0.24
}),
spider: new THREE.MeshStandardMaterial({
color: 0xd8a23e,
emissive: 0x2a1902,
metalness: 0.72,
roughness: 0.22
}),
cut: new THREE.MeshStandardMaterial({
color: 0xffb54c,
emissive: 0x5d2700,
metalness: 0.5,
roughness: 0.3
}),
housing: new THREE.MeshPhysicalMaterial({
color: 0x7194a4,
metalness: 0.15,
roughness: 0.28,
transparent: true,
opacity: 0.14,
depthWrite: false,
side: THREE.DoubleSide
}),
housingInner: new THREE.MeshPhysicalMaterial({
color: 0x8eb0c2,
metalness: 0.1,
roughness: 0.36,
transparent: true,
opacity: 0.08,
depthWrite: false,
side: THREE.BackSide
})
};
function addMesh(geometry, material, parent = scene) {
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function rod(startArray, endArray, radius, material, parent = scene, segments = 24) {
const start = new THREE.Vector3(...startArray);
const end = new THREE.Vector3(...endArray);
const direction = end.clone().sub(start);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
radius,
radius,
length,
segments
);
const mesh = addMesh(geometry, material, parent);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
return mesh;
}
function tubeThrough(points, radius, material, parent = scene) {
const curve = new THREE.CatmullRomCurve3(
points.map((point) => new THREE.Vector3(...point))
);
const geometry = new THREE.TubeGeometry(
curve,
Math.max(16, points.length * 2),
radius,
8,
false
);
return addMesh(geometry, material, parent);
}
// Procedural bevel-gear teeth. This is visual geometry, not a contact solver.
function bevelTeethGeometry(
smallRadius,
largeRadius,
depth,
toothCount,
toothWidth
) {
const positions = [];
const indices = [];
for (let i = 0; i < toothCount; i++) {
const angle = (i / toothCount) * TAU;
const radial = new THREE.Vector2(Math.cos(angle), Math.sin(angle));
const tangent = new THREE.Vector2(-Math.sin(angle), Math.cos(angle));
const base = positions.length / 3;
const sections = [
{
z: -depth / 2,
inner: smallRadius * 0.91,
outer: smallRadius * 1.04,
width: toothWidth * 0.55
},
{
z: depth / 2,
inner: largeRadius * 0.91,
outer: largeRadius * 1.04,
width: toothWidth
}
];
for (const section of sections) {
const inner = radial.clone().multiplyScalar(section.inner);
const outer = radial.clone().multiplyScalar(section.outer);
const halfWidth = tangent.clone().multiplyScalar(section.width / 2);
const points = [
inner.clone().sub(halfWidth),
inner.clone().add(halfWidth),
outer.clone().sub(halfWidth),
outer.clone().add(halfWidth)
];
for (const point of points) {
positions.push(point.x, point.y, section.z);
}
}
const faces = [
[0, 3, 2, 0, 1, 3],
[4, 6, 7, 4, 7, 5],
[0, 4, 5, 0, 5, 1],
[2, 3, 7, 2, 7, 6],
[0, 2, 6, 0, 6, 4],
[1, 5, 7, 1, 7, 3]
];
for (const face of faces) {
for (const index of face) {
indices.push(base + index);
}
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
geometry.computeBoundingSphere();
return geometry;
}
function createBevelGear({
smallRadius,
largeRadius,
depth,
teeth,
material,
toothMaterial = material
}) {
const group = new THREE.Group();
const bodyGeometry = new THREE.CylinderGeometry(
largeRadius * 0.96,
smallRadius * 0.96,
depth,
64
);
bodyGeometry.rotateX(Math.PI / 2);
addMesh(bodyGeometry, material, group);
const toothGeometry = bevelTeethGeometry(
smallRadius,
largeRadius,
depth,
teeth,
largeRadius * 0.17
);
addMesh(toothGeometry, toothMaterial, group);
const hubGeometry = new THREE.CylinderGeometry(
smallRadius * 0.50,
smallRadius * 0.50,
depth * 1.18,
32
);
hubGeometry.rotateX(Math.PI / 2);
addMesh(hubGeometry, materials.darkSteel, group);
const smallRim = addMesh(
new THREE.TorusGeometry(smallRadius, 0.028, 8, 48),
materials.steel,
group
);
smallRim.position.z = -depth * 0.46;
const largeRim = addMesh(
new THREE.TorusGeometry(largeRadius * 0.96, 0.032, 8, 56),
materials.steel,
group
);
largeRim.position.z = depth * 0.46;
return { group };
}
// ------------------------------------------------------------------
// Floor and mounting details
// ------------------------------------------------------------------
const floor = addMesh(
new THREE.PlaneGeometry(22, 22),
new THREE.MeshStandardMaterial({
color: 0x0b151d,
metalness: 0.08,
roughness: 0.82
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -2.13;
floor.castShadow = false;
const grid = new THREE.GridHelper(18, 36, 0x204251, 0x102530);
grid.position.y = -2.115;
grid.material.transparent = true;
grid.material.opacity = 0.3;
scene.add(grid);
// ------------------------------------------------------------------
// Cutaway housing: rear half of the cylindrical casing
// ------------------------------------------------------------------
const housingGroup = new THREE.Group();
scene.add(housingGroup);
const shellGeometry = new THREE.CylinderGeometry(
1.72,
1.72,
4.25,
80,
1,
true,
Math.PI / 2,
Math.PI
);
shellGeometry.rotateZ(-Math.PI / 2);
const shell = addMesh(shellGeometry, materials.housing, housingGroup);
shell.castShadow = false;
const innerShellGeometry = new THREE.CylinderGeometry(
1.63,
1.63,
4.22,
80,
1,
true,
Math.PI / 2,
Math.PI
);
innerShellGeometry.rotateZ(-Math.PI / 2);
const innerShell = addMesh(
innerShellGeometry,
materials.housingInner,
housingGroup
);
innerShell.castShadow = false;
// Flanges at both ends.
for (const x of [-2.13, 2.13]) {
const flange = addMesh(
new THREE.TorusGeometry(1.72, 0.075, 12, 96),
materials.steel,
housingGroup
);
flange.rotation.y = Math.PI / 2;
flange.position.x = x;
for (let i = 0; i < 12; i++) {
const angle = (i / 12) * TAU;
const bolt = rod(
[x - 0.065, Math.cos(angle) * 1.72, Math.sin(angle) * 1.72],
[x + 0.065, Math.cos(angle) * 1.72, Math.sin(angle) * 1.72],
0.065,
materials.darkSteel,
housingGroup,
12
);
bolt.castShadow = true;
}
}
// Bright cut edges at the two longitudinal boundaries.
for (const y of [-1.72, 1.72]) {
tubeThrough(
[
[-2.13, y, 0],
[-1.0, y, 0],
[0, y, 0],
[1.0, y, 0],
[2.13, y, 0]
],
0.027,
materials.cut,
housingGroup
);
}
// Semicircular cut rims.
for (const x of [-2.13, 2.13]) {
const arc = [];
for (let i = 0; i <= 30; i++) {
const angle = Math.PI / 2 + (i / 30) * Math.PI;
arc.push([
x,
-1.72 * Math.sin(angle),
1.72 * Math.cos(angle)
]);
}
tubeThrough(arc, 0.028, materials.cut, housingGroup);
}
// Housing feet.
for (const x of [-1.35, 1.35]) {
const foot = addMesh(
new THREE.BoxGeometry(0.5, 0.19, 0.92),
materials.darkSteel,
housingGroup
);
foot.position.set(x, -1.92, 0);
}
// ------------------------------------------------------------------
// Carrier and spider gears
// ------------------------------------------------------------------
const carrier = new THREE.Group();
scene.add(carrier);
// Central carrier hub.
rod(
[-1.18, 0, 0],
[1.18, 0, 0],
0.13,
materials.carrier,
carrier
);
// Carrier cage rails.
for (const z of [-0.93, 0.93]) {
rod(
[-1.16, 0, z],
[0, 0, z],
0.075,
materials.carrier,
carrier
);
rod(
[0, 0, z],
[1.16, 0, z],
0.075,
materials.carrier,
carrier
);
rod(
[0, 0, z],
[0, 0, 0],
0.085,
materials.carrier,
carrier
);
}
// Carrier support rings.
for (const x of [-1.10, 1.10]) {
const ring = addMesh(
new THREE.TorusGeometry(0.58, 0.072, 10, 56),
materials.carrier,
carrier
);
ring.rotation.y = Math.PI / 2;
ring.position.x = x;
}
// Cross pin carrying the two spider gears.
rod(
[0, -0.86, 0],
[0, 0.86, 0],
0.095,
materials.spider,
carrier
);
const spiderRotors = [];
function addSpiderGear(y, direction) {
const mount = new THREE.Group();
mount.position.set(0, y, 0);
mount.rotation.x = direction > 0 ? -Math.PI / 2 : Math.PI / 2;
carrier.add(mount);
const rotor = new THREE.Group();
mount.add(rotor);
const gear = createBevelGear({
smallRadius: 0.15,
largeRadius: 0.42,
depth: 0.40,
teeth: 14,
material: materials.spider
});
rotor.add(gear.group);
spiderRotors.push({ rotor, direction });
}
addSpiderGear(0.49, 1);
addSpiderGear(-0.49, -1);
// Carrier index stripe.
const carrierStripe = addMesh(
new THREE.BoxGeometry(0.18, 0.055, 0.55),
materials.cut,
carrier
);
carrierStripe.position.set(0, 0.17, 0.78);
// ------------------------------------------------------------------
// Left and right output shafts and side gears
// ------------------------------------------------------------------
const leftOutput = new THREE.Group();
const rightOutput = new THREE.Group();
scene.add(leftOutput, rightOutput);
rod(
[-3.08, 0, 0],
[-0.62, 0, 0],
0.16,
materials.steel,
leftOutput
);
rod(
[0.62, 0, 0],
[3.08, 0, 0],
0.16,
materials.steel,
rightOutput
);
// Output couplings.
rod(
[-3.30, 0, 0],
[-3.03, 0, 0],
0.28,
materials.darkSteel,
leftOutput
);
rod(
[3.03, 0, 0],
[3.30, 0, 0],
0.28,
materials.darkSteel,
rightOutput
);
// Splined output ends.
function addSplines(root, x, colorMaterial) {
for (let i = 0; i < 12; i++) {
const angle = (i / 12) * TAU;
const spline = addMesh(
new THREE.BoxGeometry(0.55, 0.052, 0.078),
colorMaterial,
root
);
spline.position.set(
x,
Math.cos(angle) * 0.235,
Math.sin(angle) * 0.235
);
spline.rotation.x = angle;
}
}
addSplines(leftOutput, -2.70, materials.steel);
addSplines(rightOutput, 2.70, materials.steel);
// Shaft flanges.
rod(
[-1.18, 0, 0],
[-1.08, 0, 0],
0.37,
materials.darkSteel,
leftOutput
);
rod(
[1.08, 0, 0],
[1.18, 0, 0],
0.37,
materials.darkSteel,
rightOutput
);
// Side bevel gears. Their large ends face outward.
const leftGear = createBevelGear({
smallRadius: 0.27,
largeRadius: 0.78,
depth: 0.96,
teeth: 24,
material: materials.left
});
leftGear.group.position.x = -0.60;
leftGear.group.rotation.y = -Math.PI / 2;
leftOutput.add(leftGear.group);
const rightGear = createBevelGear({
smallRadius: 0.27,
largeRadius: 0.78,
depth: 0.96,
teeth: 24,
material: materials.right
});
rightGear.group.position.x = 0.60;
rightGear.group.rotation.y = Math.PI / 2;
rightOutput.add(rightGear.group);
// Colored output index marks make shaft rotation readable.
const leftMarker = addMesh(
new THREE.BoxGeometry(0.22, 0.075, 0.09),
materials.left,
leftOutput
);
leftMarker.position.set(-3.10, 0.28, 0);
const rightMarker = addMesh(
new THREE.BoxGeometry(0.22, 0.075, 0.09),
materials.right,
rightOutput
);
rightMarker.position.set(3.10, 0.28, 0);
// ------------------------------------------------------------------
// Kinematic model:
//
// q1 = carrier angle
// q2 = half the left/right angular difference
//
// phiC = q1
// phiL = q1 + q2
// phiR = q1 - q2
//
// Therefore phiL + phiR - 2 phiC = 0 exactly.
// ------------------------------------------------------------------
let q1 = 20;
let q2 = 0;
let running = true;
let elapsed = 0;
const ui = {
valC: document.getElementById("valC"),
valL: document.getElementById("valL"),
valR: document.getElementById("valR"),
residual: document.getElementById("residual"),
q1: document.getElementById("q1"),
q2: document.getElementById("q2"),
q1Value: document.getElementById("q1Value"),
q2Value: document.getElementById("q2Value"),
run: document.getElementById("run")
};
function signedAngle(value) {
const rounded = Math.abs(value) < 0.05 ? 0 : value;
return `${rounded >= 0 ? "+" : ""}${rounded.toFixed(1)}°`;
}
function applyKinematics() {
const carrierAngle = q1;
const leftAngle = q1 + q2;
const rightAngle = q1 - q2;
carrier.rotation.x = carrierAngle * DEG;
leftOutput.rotation.x = leftAngle * DEG;
rightOutput.rotation.x = rightAngle * DEG;
// Illustrative relative spin of the free spider gears.
for (const spider of spiderRotors) {
spider.rotor.rotation.z =
spider.direction * q2 * 1.55 * DEG;
}
const residual = leftAngle + rightAngle - 2 * carrierAngle;
ui.valC.textContent = signedAngle(carrierAngle);
ui.valL.textContent = signedAngle(leftAngle);
ui.valR.textContent = signedAngle(rightAngle);
ui.residual.textContent = `${Math.abs(residual).toFixed(3)}°`;
ui.q1Value.textContent = signedAngle(q1);
ui.q2Value.textContent = signedAngle(q2);
}
ui.q1.addEventListener("input", () => {
running = false;
q1 = Number(ui.q1.value);
updateRunButton();
applyKinematics();
});
ui.q2.addEventListener("input", () => {
running = false;
q2 = Number(ui.q2.value);
updateRunButton();
applyKinematics();
});
function updateRunButton() {
ui.run.textContent = running ? "Pause" : "Run";
}
ui.run.addEventListener("click", () => {
running = !running;
updateRunButton();
});
document.getElementById("reset").addEventListener("click", () => {
running = false;
q1 = 20;
q2 = 0;
ui.q1.value = q1;
ui.q2.value = q2;
updateRunButton();
applyKinematics();
});
function resetView() {
orbit.theta = 0.72;
orbit.phi = 1.17;
orbit.radius = 8.2;
updateCamera();
}
document.getElementById("view").addEventListener("click", resetView);
// ------------------------------------------------------------------
// Direct camera controls: drag to orbit, wheel to zoom
// ------------------------------------------------------------------
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;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi + dy * 0.005,
0.32,
1.49
);
updateCamera();
});
function endDrag(event) {
dragging = false;
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();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
4.2,
15
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetView);
// ------------------------------------------------------------------
// Screen-space labels
// ------------------------------------------------------------------
const labels = [
{
element: document.getElementById("labelCarrier"),
position: new THREE.Vector3(0, 1.15, 0.35)
},
{
element: document.getElementById("labelLeft"),
position: new THREE.Vector3(-2.75, 0.48, 0)
},
{
element: document.getElementById("labelRight"),
position: new THREE.Vector3(2.75, 0.48, 0)
}
];
function updateLabels() {
const width = renderer.domElement.clientWidth;
const height = renderer.domElement.clientHeight;
for (const label of labels) {
const projected = label.position.clone().project(camera);
label.element.style.left = `${(projected.x * 0.5 + 0.5) * width}px`;
label.element.style.top = `${(-projected.y * 0.5 + 0.5) * height}px`;
label.element.style.opacity =
projected.z > -1 && projected.z < 1 ? "1" : "0";
}
}
// ------------------------------------------------------------------
// Resize and animation loop
// ------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
elapsed += dt;
if (running) {
q1 += dt * 26;
if (q1 > 180) q1 -= 360;
q2 = 68 * Math.sin(elapsed * 0.70);
ui.q1.value = q1;
ui.q2.value = q2;
}
applyKinematics();
renderer.render(scene, camera);
updateLabels();
}
updateRunButton();
applyKinematics();
animate();
</script>
</body>
</html>
```