Prompt specification
Build or customize the Three.js 3D scene "Two Shafts and a Sliding Coupling Sleeve" 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 Shafts and a Sliding Coupling Sleeve
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-shafts-and-a-sliding-coupling-sleeve-x2017
Scene Description Prompt:
Model aligned shafts separated by a gap and a sliding sleeve with engagement features. Show disconnected and connected states without overlapping solid geometry. Use Three.js from cdnjs in a single HTML file, with low-poly forms.
Working Three.js Starter (two-shafts-and-a-sliding-coupling-sleeve-x2017):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Sliding Sleeve Coupling</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(9, 18, 31, 0.82);
--border: rgba(170, 220, 255, 0.17);
--text: #e8f4ff;
--muted: #91a8bc;
--cyan: #49dfd1;
--gold: #ffc857;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #07101c;
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
position: fixed;
inset: 0;
display: block;
touch-action: none;
}
button,
input {
font: inherit;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(16px);
}
#info {
top: 18px;
left: 18px;
width: min(390px, calc(100vw - 36px));
padding: 18px;
}
.eyebrow {
margin-bottom: 5px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(20px, 2.5vw, 28px);
line-height: 1.12;
letter-spacing: -0.03em;
}
.description {
margin: 9px 0 15px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.metric {
min-width: 0;
padding: 9px 8px;
border: 1px solid rgba(170, 220, 255, 0.1);
border-radius: 10px;
background: rgba(255, 255, 255, 0.035);
}
.metric strong,
.metric span {
display: block;
}
.metric strong {
color: var(--gold);
font-size: 13px;
}
.metric span {
margin-top: 3px;
color: var(--muted);
font-size: 9px;
line-height: 1.25;
text-transform: uppercase;
}
.options {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid rgba(170, 220, 255, 0.1);
color: var(--muted);
font-size: 12px;
}
.check {
display: flex;
align-items: center;
gap: 8px;
cursor: pointer;
user-select: none;
}
.check input {
accent-color: var(--cyan);
}
#stateCard {
top: 18px;
right: 18px;
display: flex;
align-items: center;
gap: 11px;
min-width: 245px;
padding: 13px 15px;
}
#stateDot {
width: 10px;
height: 10px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--gold);
box-shadow: 0 0 18px rgba(255, 200, 87, 0.8);
}
#stateCard[data-state="1"] #stateDot {
background: var(--cyan);
box-shadow: 0 0 18px rgba(73, 223, 209, 0.85);
}
#stateCard[data-state="moving"] #stateDot {
animation: pulse 0.8s ease-in-out infinite alternate;
}
@keyframes pulse {
to {
transform: scale(1.55);
opacity: 0.55;
}
}
#stateTitle {
display: block;
font-size: 11px;
letter-spacing: 0.13em;
}
#stateDescription {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
}
#controls {
bottom: 18px;
left: 50%;
display: flex;
align-items: center;
gap: 7px;
padding: 8px;
transform: translateX(-50%);
}
#controls button {
min-height: 36px;
padding: 0 14px;
border: 1px solid transparent;
border-radius: 10px;
color: var(--muted);
background: transparent;
cursor: pointer;
transition: 160ms ease;
}
#controls button:hover {
color: white;
background: rgba(255, 255, 255, 0.07);
}
#controls button.active {
border-color: rgba(73, 223, 209, 0.36);
color: #dffffb;
background: rgba(73, 223, 209, 0.13);
}
#controls .separator {
width: 1px;
height: 24px;
margin: 0 2px;
background: var(--border);
}
#legend {
bottom: 18px;
left: 18px;
padding: 10px 12px;
color: var(--muted);
font-size: 10px;
line-height: 1.7;
}
.legend-row {
display: flex;
align-items: center;
gap: 7px;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 2px;
background: #647d99;
}
.swatch.spline {
background: var(--gold);
}
.swatch.sleeve {
background: var(--cyan);
}
#hint {
position: fixed;
right: 18px;
bottom: 20px;
z-index: 5;
color: rgba(220, 237, 255, 0.5);
font-size: 10px;
pointer-events: none;
}
.world-label {
position: fixed;
z-index: 4;
padding: 5px 8px;
border: 1px solid rgba(190, 230, 255, 0.18);
border-radius: 6px;
color: #e9f7ff;
background: rgba(6, 15, 27, 0.76);
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.24);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -100%);
}
.world-label::after {
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 13px;
content: "";
background: linear-gradient(rgba(190, 230, 255, 0.55), transparent);
}
#gapLabel {
color: #ffe1a1;
border-color: rgba(255, 200, 87, 0.28);
}
#sleeveLabel {
color: #c9fff8;
border-color: rgba(73, 223, 209, 0.3);
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
color: var(--cyan);
background: #07101c;
font-size: 12px;
letter-spacing: 0.12em;
transition: opacity 300ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
#info {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 13px;
}
.description {
margin-bottom: 10px;
}
.metrics {
gap: 5px;
}
.metric {
padding: 7px 5px;
}
#stateCard {
top: auto;
right: 10px;
bottom: 78px;
min-width: 0;
padding: 10px 12px;
}
#stateDescription {
display: none;
}
#legend,
#hint {
display: none;
}
#controls {
bottom: 10px;
width: calc(100vw - 20px);
justify-content: center;
}
#controls button {
padding: 0 10px;
font-size: 11px;
}
.world-label {
font-size: 8px;
}
}
</style>
</head>
<body>
<section class="panel" id="info">
<div class="eyebrow">Low-poly clearance model</div>
<h1>Sliding Sleeve Coupling</h1>
<p class="description">
Two aligned shafts with an exposed end gap. The hollow sleeve slides
over both shaft noses and engages three longitudinal splines.
</p>
<div class="metrics">
<div class="metric">
<strong>0.30</strong>
<span>Shaft gap</span>
</div>
<div class="metric">
<strong>+0.04</strong>
<span>Bore clearance</span>
</div>
<div class="metric">
<strong>+0.03</strong>
<span>Spline clearance</span>
</div>
</div>
<div class="options">
<label class="check">
<input id="xray" type="checkbox" checked>
X-ray sleeve
</label>
<span>True hollow geometry · no solid intersection</span>
</div>
</section>
<section class="panel" id="stateCard" data-state="0">
<span id="stateDot"></span>
<span>
<strong id="stateTitle">DISCONNECTED</strong>
<small id="stateDescription">Sleeve parked clear of the shaft ends</small>
</span>
</section>
<section class="panel" id="controls">
<button class="active" data-state="0">Disconnected</button>
<button data-state="1">Connected</button>
<span class="separator"></span>
<button id="cycle" title="Animate to the other state">Cycle</button>
<button id="home" title="Reset the camera">Home view</button>
</section>
<section class="panel" id="legend">
<div class="legend-row">
<span class="swatch"></span>
Shaft body
</div>
<div class="legend-row">
<span class="swatch spline"></span>
Engagement splines
</div>
<div class="legend-row">
<span class="swatch sleeve"></span>
Hollow sliding sleeve
</div>
</section>
<div id="hint">Drag to orbit · wheel to zoom · Shift-drag to pan</div>
<div id="gapLabel" class="world-label">SHAFT GAP · 0.30</div>
<div id="sleeveLabel" class="world-label">SLIDING SLEEVE</div>
<div id="loading">LOADING MODEL</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Dimensions. Every mating pair has positive radial/angular clearance.
// -------------------------------------------------------------------------
const DEG = Math.PI / 180;
const SHAFT_RADIUS = 0.46;
const NOSE_RADIUS = 0.38;
const BORE_RADIUS = 0.50; // 0.04 clear of the main shaft
const KEYWAY_RADIUS = 0.61; // 0.03 clear of the spline crown
const SPLINE_CROWN_RADIUS = 0.58;
const SPLINE_INNER_RADIUS = 0.381;
const SPLINE_HALF_ANGLE = 8.0 * DEG;
const KEYWAY_HALF_ANGLE = 8.6 * DEG;
const KEYWAY_TAPER_ANGLE = 9.4 * DEG;
const SLEEVE_LENGTH = 3.00;
const SLEEVE_OUTER_RADIUS = 0.90;
const SLEEVE_OUTER_SIDES = 16;
const DISCONNECTED_X = -6.15;
const CONNECTED_X = 0;
const SHAFT_CENTERS = [30, 150, 270];
// -------------------------------------------------------------------------
// 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.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07101c);
scene.fog = new THREE.Fog(0x07101c, 17, 34);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(-1.0, 0, 0);
const cameraSpherical = new THREE.Spherical(12.6, 1.12, 0.74);
function updateCamera() {
camera.position.setFromSpherical(cameraSpherical);
camera.position.add(cameraTarget);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xc9efff, 0x101827, 1.65);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.4);
keyLight.position.set(2, 8, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x39dfff, 2.1);
rimLight.position.set(-7, 3, -7);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffb84d, 14, 18, 2);
warmLight.position.set(1.5, 2.5, 4);
scene.add(warmLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 28),
new THREE.MeshStandardMaterial({
color: 0x0b1624,
roughness: 0.92,
metalness: 0.05
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.05;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(30, 30, 0x31516a, 0x173044);
grid.position.y = -1.035;
grid.material.transparent = true;
grid.material.opacity = 0.38;
scene.add(grid);
// -------------------------------------------------------------------------
// Materials.
// -------------------------------------------------------------------------
const shaftMaterial = new THREE.MeshStandardMaterial({
color: 0x617d9b,
metalness: 0.78,
roughness: 0.28,
flatShading: true
});
const noseMaterial = new THREE.MeshStandardMaterial({
color: 0xa8bacb,
metalness: 0.84,
roughness: 0.23,
flatShading: true
});
const splineMaterial = new THREE.MeshStandardMaterial({
color: 0xffc857,
emissive: 0x4b2800,
emissiveIntensity: 0.16,
metalness: 0.65,
roughness: 0.27,
flatShading: true
});
const sleeveMaterial = new THREE.MeshStandardMaterial({
color: 0x28c7b7,
emissive: 0x063a37,
emissiveIntensity: 0.22,
metalness: 0.58,
roughness: 0.25,
transparent: true,
opacity: 0.48,
depthWrite: false,
side: THREE.DoubleSide,
flatShading: true
});
const sleeveEdgeMaterial = new THREE.LineBasicMaterial({
color: 0xb7fff7,
transparent: true,
opacity: 0.66,
depthTest: true
});
// -------------------------------------------------------------------------
// Geometry helpers.
// -------------------------------------------------------------------------
function createSteppedShaftGeometry(
profile,
radialSegments,
segmentMaterialIndices,
startMaterial,
endMaterial
) {
const positions = [];
const indices = [];
const groups = [];
for (const point of profile) {
for (let j = 0; j < radialSegments; j++) {
const angle = (j / radialSegments) * Math.PI * 2;
positions.push(
point.x,
point.r * Math.cos(angle),
point.r * Math.sin(angle)
);
}
}
for (let ring = 0; ring < profile.length - 1; ring++) {
const groupStart = indices.length;
const currentRing = ring * radialSegments;
const nextRing = (ring + 1) * radialSegments;
for (let j = 0; j < radialSegments; j++) {
const k = (j + 1) % radialSegments;
indices.push(
currentRing + j,
currentRing + k,
nextRing + k,
nextRing + j
);
}
groups.push({
start: groupStart,
count: indices.length - groupStart,
materialIndex: segmentMaterialIndices[ring]
});
}
const startCenter = positions.length / 3;
positions.push(profile[0].x, 0, 0);
const startGroupStart = indices.length;
for (let j = 0; j < radialSegments; j++) {
const k = (j + 1) % radialSegments;
indices.push(startCenter, k, j);
}
groups.push({
start: startGroupStart,
count: indices.length - startGroupStart,
materialIndex: startMaterial
});
const lastRing = (profile.length - 1) * radialSegments;
const endCenter = positions.length / 3;
positions.push(profile[profile.length - 1].x, 0, 0);
const endGroupStart = indices.length;
for (let j = 0; j < radialSegments; j++) {
const k = (j + 1) % radialSegments;
indices.push(endCenter, lastRing + j, lastRing + k);
}
groups.push({
start: endGroupStart,
count: indices.length - endGroupStart,
materialIndex: endMaterial
});
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
for (const group of groups) {
geometry.addGroup(
group.start,
group.count,
group.materialIndex
);
}
geometry.computeVertexNormals();
geometry.computeBoundingSphere();
return geometry;
}
function createSectorPrism(
length,
innerRadius,
outerRadius,
startAngle,
endAngle,
segments = 5
) {
const positions = [];
const indices = [];
for (let i = 0; i <= segments; i++) {
const angle = THREE.MathUtils.lerp(
startAngle,
endAngle,
i / segments
);
const c = Math.cos(angle);
const s = Math.sin(angle);
// Outer front, outer back, inner front, inner back.
positions.push(
-length / 2, outerRadius * c, outerRadius * s,
length / 2, outerRadius * c, outerRadius * s,
-length / 2, innerRadius * c, innerRadius * s,
length / 2, innerRadius * c, innerRadius * s
);
}
for (let i = 0; i < segments; i++) {
const a = i * 4;
const b = (i + 1) * 4;
// Outer curved wall.
indices.push(a, b, b + 1, a + 1);
// Inner curved wall.
indices.push(b + 2, a + 2, a + 3, b + 3);
// Front annular face.
indices.push(a, a + 2, b + 2, b);
// Back annular face.
indices.push(a + 1, b + 1, b + 3, a + 3);
}
// Radial end walls.
indices.push(0, 1, 3, 2);
const end = segments * 4;
indices.push(end, end + 2, end + 3, end + 1);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
geometry.computeBoundingSphere();
return geometry;
}
function createGrooveContour() {
const points = [{ angle: 0, radius: BORE_RADIUS }];
function appendArc(
from,
to,
radius,
maxStep,
includeEnd = true
) {
const count = Math.max(
1,
Math.ceil((to - from) / maxStep)
);
const last = includeEnd ? count : count - 1;
for (let i = 1; i <= last; i++) {
points.push({
angle: THREE.MathUtils.lerp(from, to, i / count),
radius
});
}
}
let current = 0;
for (const centerDegrees of SHAFT_CENTERS) {
const center = centerDegrees * DEG;
const outerStart = center - KEYWAY_TAPER_ANGLE;
const innerStart = center - KEYWAY_HALF_ANGLE;
const innerEnd = center + KEYWAY_HALF_ANGLE;
const outerEnd = center + KEYWAY_TAPER_ANGLE;
appendArc(
current,
outerStart,
BORE_RADIUS,
10 * DEG
);
// Tapered lead-in to the larger spline channel.
points.push({
angle: innerStart,
radius: KEYWAY_RADIUS
});
appendArc(
innerStart,
innerEnd,
KEYWAY_RADIUS,
4 * DEG
);
// Tapered lead-out back to the normal bore radius.
points.push({
angle: outerEnd,
radius: BORE_RADIUS
});
current = outerEnd;
}
appendArc(
current,
Math.PI * 2,
BORE_RADIUS,
10 * DEG,
false
);
return points;
}
function polygonRadiusAtAngle(angle) {
const sector = (Math.PI * 2) / SLEEVE_OUTER_SIDES;
const local = ((angle % sector) + sector) % sector - sector / 2;
return (
SLEEVE_OUTER_RADIUS *
Math.cos(sector / 2) /
Math.cos(local)
);
}
function createSleeveGeometry(length, contour) {
const positions = [];
const indices = [];
const count = contour.length;
for (const point of contour) {
const c = Math.cos(point.angle);
const s = Math.sin(point.angle);
const innerY = point.radius * c;
const innerZ = point.radius * s;
const outerRadius = polygonRadiusAtAngle(point.angle);
const outerY = outerRadius * c;
const outerZ = outerRadius * s;
// Outer front, outer back, inner front, inner back.
positions.push(
-length / 2, outerY, outerZ,
length / 2, outerY, outerZ,
-length / 2, innerY, innerZ,
length / 2, innerY, innerZ
);
}
for (let i = 0; i < count; i++) {
const j = (i + 1) % count;
const a = i * 4;
const b = j * 4;
// Outer wall.
indices.push(a, b, b + 1, a + 1);
// Inner wall, including the three spline channels.
indices.push(b + 2, a + 2, a + 3, b + 3);
// Front annular face.
indices.push(a, a + 2, b + 2, b);
// Back annular face.
indices.push(a + 1, b + 1, b + 3, a + 3);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
geometry.computeBoundingSphere();
return geometry;
}
// -------------------------------------------------------------------------
// Shaft assembly. Each shaft is one watertight stepped mesh.
// -------------------------------------------------------------------------
const shafts = new THREE.Group();
scene.add(shafts);
const leftShaft = new THREE.Mesh(
createSteppedShaftGeometry(
[
{ x: -4.40, r: SHAFT_RADIUS },
{ x: -1.25, r: SHAFT_RADIUS },
{ x: -1.25, r: NOSE_RADIUS },
{ x: -0.15, r: NOSE_RADIUS }
],
12,
[0, 1, 1],
0,
1
),
[shaftMaterial, noseMaterial]
);
const rightShaft = new THREE.Mesh(
createSteppedShaftGeometry(
[
{ x: 0.15, r: NOSE_RADIUS },
{ x: 1.25, r: NOSE_RADIUS },
{ x: 1.25, r: SHAFT_RADIUS },
{ x: 4.40, r: SHAFT_RADIUS }
],
12,
[0, 1, 1],
0,
1
),
[shaftMaterial, noseMaterial]
);
leftShaft.castShadow = true;
leftShaft.receiveShadow = true;
rightShaft.castShadow = true;
rightShaft.receiveShadow = true;
shafts.add(leftShaft, rightShaft);
function addSplines(centerX, length) {
for (const centerDegrees of SHAFT_CENTERS) {
const center = centerDegrees * DEG;
const spline = new THREE.Mesh(
createSectorPrism(
length,
SPLINE_INNER_RADIUS,
SPLINE_CROWN_RADIUS,
center - SPLINE_HALF_ANGLE,
center + SPLINE_HALF_ANGLE,
5
),
splineMaterial
);
spline.position.x = centerX;
spline.castShadow = true;
spline.receiveShadow = true;
shafts.add(spline);
}
}
addSplines(-0.70, 1.10);
addSplines(0.70, 1.10);
// -------------------------------------------------------------------------
// Hollow sliding sleeve with modeled internal spline channels.
// -------------------------------------------------------------------------
const sleeveGroup = new THREE.Group();
sleeveGroup.position.x = DISCONNECTED_X;
scene.add(sleeveGroup);
const sleeveGeometry = createSleeveGeometry(
SLEEVE_LENGTH,
createGrooveContour()
);
const sleeve = new THREE.Mesh(sleeveGeometry, sleeveMaterial);
sleeve.castShadow = false;
sleeve.receiveShadow = true;
sleeve.renderOrder = 2;
sleeveGroup.add(sleeve);
const sleeveEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(sleeveGeometry, 20),
sleeveEdgeMaterial
);
sleeveEdges.renderOrder = 3;
sleeveGroup.add(sleeveEdges);
// -------------------------------------------------------------------------
// Gap marker and sleeve travel guide.
// -------------------------------------------------------------------------
const gapMaterial = new THREE.LineDashedMaterial({
color: 0xffc857,
dashSize: 0.055,
gapSize: 0.035,
transparent: true,
opacity: 0.9,
depthTest: false
});
const gapLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-0.15, 1.04, 0),
new THREE.Vector3(0.15, 1.04, 0)
]),
gapMaterial
);
gapLine.computeLineDistances();
gapLine.renderOrder = 5;
scene.add(gapLine);
const gapTicks = new THREE.LineSegments(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-0.15, 0.95, 0),
new THREE.Vector3(-0.15, 1.13, 0),
new THREE.Vector3(0.15, 0.95, 0),
new THREE.Vector3(0.15, 1.13, 0)
]),
new THREE.LineBasicMaterial({
color: 0xffc857,
transparent: true,
opacity: 0.9,
depthTest: false
})
);
gapTicks.renderOrder = 5;
scene.add(gapTicks);
const travelPath = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(DISCONNECTED_X, -0.94, 0),
new THREE.Vector3(CONNECTED_X, -0.94, 0)
]),
new THREE.LineDashedMaterial({
color: 0x49dfd1,
dashSize: 0.13,
gapSize: 0.10,
transparent: true,
opacity: 0.28
})
);
travelPath.computeLineDistances();
scene.add(travelPath);
const travelArrow = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(-5.15, 1.02, 0),
1.65,
0xffc857,
0.24,
0.12
);
travelArrow.line.material.transparent = true;
travelArrow.line.material.opacity = 0.62;
travelArrow.cone.material.transparent = true;
travelArrow.cone.material.opacity = 0.72;
scene.add(travelArrow);
// -------------------------------------------------------------------------
// State animation.
// -------------------------------------------------------------------------
let stateTarget = 0;
let stateProgress = 0;
let motion = null;
const stateButtons = [
document.querySelector('[data-state="0"]'),
document.querySelector('[data-state="1"]')
];
const stateCard = document.getElementById("stateCard");
const stateTitle = document.getElementById("stateTitle");
const stateDescription = document.getElementById("stateDescription");
function setState(nextState) {
if (nextState === stateTarget && !motion) return;
motion = {
from: stateProgress,
to: nextState,
start: performance.now(),
duration: nextState === 1 ? 2100 : 1850
};
stateTarget = nextState;
for (const button of stateButtons) {
const active = Number(button.dataset.state) === nextState;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
}
}
stateButtons.forEach((button) => {
button.addEventListener("click", () => {
setState(Number(button.dataset.state));
});
});
document.getElementById("cycle").addEventListener("click", () => {
setState(stateTarget === 0 ? 1 : 0);
});
function easeInOutCubic(value) {
return value < 0.5
? 4 * value * value * value
: 1 - Math.pow(-2 * value + 2, 3) / 2;
}
let previousStatus = "";
function updateStatus() {
const isMoving = motion !== null;
const status = isMoving
? "moving"
: stateTarget === 1
? "1"
: "0";
if (status === previousStatus) return;
previousStatus = status;
stateCard.dataset.state = status;
if (isMoving) {
stateTitle.textContent = "SLIDING";
stateDescription.textContent =
"Maintaining positive radial and angular clearance";
} else if (stateTarget === 1) {
stateTitle.textContent = "CONNECTED";
stateDescription.textContent =
"Sleeve bridges the gap and captures both shaft noses";
} else {
stateTitle.textContent = "DISCONNECTED";
stateDescription.textContent =
"Sleeve parked clear of the shaft ends";
}
}
// -------------------------------------------------------------------------
// X-ray control.
// -------------------------------------------------------------------------
const xrayToggle = document.getElementById("xray");
function updateXray() {
const enabled = xrayToggle.checked;
sleeveMaterial.opacity = enabled ? 0.48 : 0.97;
sleeveMaterial.depthWrite = !enabled;
sleeveMaterial.needsUpdate = true;
sleeveEdgeMaterial.opacity = enabled ? 0.66 : 0.26;
sleeve.castShadow = !enabled;
}
xrayToggle.addEventListener("change", updateXray);
updateXray();
// -------------------------------------------------------------------------
// Custom camera controls. No OrbitControls or addons are used.
// -------------------------------------------------------------------------
const canvas = renderer.domElement;
let dragging = false;
let panning = false;
let pointerX = 0;
let pointerY = 0;
const cameraRight = new THREE.Vector3();
const cameraUp = new THREE.Vector3();
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
panning = event.shiftKey;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
if (panning) {
camera.updateMatrixWorld();
cameraRight.setFromMatrixColumn(camera.matrixWorld, 0);
cameraUp.setFromMatrixColumn(camera.matrixWorld, 1);
const panScale = cameraSpherical.radius * 0.00125;
cameraTarget.addScaledVector(cameraRight, -dx * panScale);
cameraTarget.addScaledVector(cameraUp, dy * panScale);
cameraTarget.y = THREE.MathUtils.clamp(
cameraTarget.y,
-1.5,
2.5
);
} else {
cameraSpherical.theta -= dx * 0.006;
cameraSpherical.phi = THREE.MathUtils.clamp(
cameraSpherical.phi - dy * 0.006,
0.20,
Math.PI - 0.20
);
}
});
function stopDragging(event) {
dragging = false;
panning = false;
if (event && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraSpherical.radius = THREE.MathUtils.clamp(
cameraSpherical.radius * Math.exp(event.deltaY * 0.001),
5,
40
);
},
{ passive: false }
);
function resetCamera() {
cameraTarget.set(-1.0, 0, 0);
const aspect = window.innerWidth / window.innerHeight;
const widthFitRadius =
13.2 /
(2 * Math.tan(THREE.MathUtils.degToRad(camera.fov / 2)) * aspect);
cameraSpherical.radius = Math.max(12.6, widthFitRadius);
cameraSpherical.phi = 1.12;
cameraSpherical.theta = 0.74;
}
document.getElementById("home").addEventListener("click", resetCamera);
canvas.addEventListener("dblclick", resetCamera);
// -------------------------------------------------------------------------
// Projected HTML labels.
// -------------------------------------------------------------------------
const gapLabel = document.getElementById("gapLabel");
const sleeveLabel = document.getElementById("sleeveLabel");
function positionLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) return;
element.style.left =
`${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top =
`${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
const gapLabelPosition = new THREE.Vector3(0, 1.48, 0);
const sleeveLabelPosition = new THREE.Vector3();
// -------------------------------------------------------------------------
// Resize and render loop.
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
function animate(now) {
requestAnimationFrame(animate);
if (motion) {
const raw = THREE.MathUtils.clamp(
(now - motion.start) / motion.duration,
0,
1
);
stateProgress = THREE.MathUtils.lerp(
motion.from,
motion.to,
raw
);
if (raw >= 1) {
stateProgress = motion.to;
motion = null;
}
}
const easedProgress = easeInOutCubic(stateProgress);
sleeveGroup.position.x = THREE.MathUtils.lerp(
DISCONNECTED_X,
CONNECTED_X,
easedProgress
);
travelArrow.visible = stateProgress < 0.985 || motion !== null;
updateStatus();
updateCamera();
sleeveLabelPosition.set(
sleeveGroup.position.x,
1.16,
0
);
positionLabel(gapLabel, gapLabelPosition);
positionLabel(sleeveLabel, sleeveLabelPosition);
renderer.render(scene, camera);
}
updateStatus();
renderer.render(scene, camera);
document.getElementById("loading").classList.add("hidden");
requestAnimationFrame(animate);
</script>
</body>
</html>
```