Prompt specification
Build or customize the Three.js 3D scene "Bicycle chain link articulation" 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: Bicycle chain link articulation
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/bicycle-chain-link-articulation-x0249
Scene Description Prompt:
I'd like to see this as a stylized Three.js scene: bicycle chain link articulation. Show inner and outer chain plates, rollers, and pins in an exploded short section; reassembled links must bend at pins while plate lengths stay constant. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (bicycle-chain-link-articulation-x0249):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Chain Link Articulation — Three.js Study</title>
<style>
:root {
color-scheme: dark;
--bg: #071018;
--panel: rgba(10, 23, 32, 0.78);
--line: rgba(178, 218, 226, 0.16);
--text: #eaf6f7;
--muted: #8ca8b0;
--inner: #ff7052;
--outer: #32c9b5;
--roller: #8a9ca7;
--pin: #e5f0f3;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
outline: none;
}
#scene:active {
cursor: grabbing;
}
.header {
position: fixed;
z-index: 5;
top: 30px;
left: 34px;
max-width: 430px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 12px;
color: var(--outer);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.eyebrow::before {
width: 25px;
height: 1px;
content: "";
background: currentColor;
}
h1 {
margin: 0;
font-size: clamp(31px, 4.2vw, 58px);
font-weight: 540;
line-height: 0.98;
letter-spacing: -0.045em;
}
.intro {
max-width: 390px;
margin: 17px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.65;
}
.status {
position: fixed;
z-index: 5;
top: 30px;
right: 32px;
display: flex;
align-items: center;
gap: 9px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(7, 16, 24, 0.58);
color: #b8cdd2;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.16em;
text-transform: uppercase;
backdrop-filter: blur(12px);
pointer-events: none;
}
.status-dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--outer);
box-shadow: 0 0 12px var(--outer);
}
.panel {
position: fixed;
z-index: 6;
bottom: 28px;
left: 30px;
width: min(360px, calc(100vw - 60px));
padding: 18px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 22px 70px rgba(0, 0, 0, 0.3);
backdrop-filter: blur(18px);
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 17px;
}
.panel-heading strong {
font-size: 10px;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.rigidity {
display: flex;
align-items: center;
gap: 7px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.rigidity::before {
width: 5px;
height: 5px;
border-radius: 50%;
content: "";
background: var(--outer);
box-shadow: 0 0 8px rgba(50, 201, 181, 0.7);
}
.control {
margin-top: 15px;
}
.control-label {
display: flex;
align-items: baseline;
justify-content: space-between;
margin-bottom: 8px;
color: #c8dadd;
font-size: 11px;
}
.control-label output {
color: var(--outer);
font-variant-numeric: tabular-nums;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
appearance: none;
border-radius: 999px;
background: rgba(151, 193, 202, 0.2);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
appearance: none;
border: 3px solid #0a171f;
border-radius: 50%;
background: var(--outer);
box-shadow: 0 0 0 1px rgba(50, 201, 181, 0.45);
}
input[type="range"]::-moz-range-thumb {
width: 10px;
height: 10px;
border: 3px solid #0a171f;
border-radius: 50%;
background: var(--outer);
box-shadow: 0 0 0 1px rgba(50, 201, 181, 0.45);
}
.buttons {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-top: 18px;
}
button {
min-height: 36px;
padding: 8px 10px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.025);
color: #a9c0c6;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.09em;
text-transform: uppercase;
cursor: pointer;
transition:
border-color 160ms ease,
background 160ms ease,
color 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(50, 201, 181, 0.42);
color: #e7ffff;
transform: translateY(-1px);
}
button.active {
border-color: rgba(50, 201, 181, 0.52);
background: rgba(50, 201, 181, 0.1);
color: #bffff5;
}
.legend {
position: fixed;
z-index: 5;
right: 30px;
bottom: 30px;
width: 184px;
padding: 15px 16px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(8, 19, 27, 0.64);
backdrop-filter: blur(14px);
pointer-events: none;
}
.legend-title {
margin-bottom: 12px;
color: #728d96;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
margin: 8px 0;
color: #b7cbd0;
font-size: 10px;
}
.swatch {
width: 15px;
height: 5px;
border-radius: 99px;
background: var(--c);
box-shadow: 0 0 10px color-mix(in srgb, var(--c) 40%, transparent);
}
.hint {
position: fixed;
z-index: 5;
bottom: 31px;
left: 50%;
color: rgba(169, 194, 201, 0.55);
font-size: 9px;
font-weight: 650;
letter-spacing: 0.12em;
text-transform: uppercase;
transform: translateX(-50%);
pointer-events: none;
white-space: nowrap;
}
.callout {
--c: var(--outer);
position: fixed;
z-index: 4;
display: flex;
align-items: center;
opacity: 0;
pointer-events: none;
transition: opacity 220ms ease;
will-change: left, top;
}
.callout i {
display: block;
width: 7px;
height: 7px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 50%;
background: var(--c);
box-shadow: 0 0 12px var(--c);
transform: translate(-50%, -50%);
}
.callout span {
margin-left: 8px;
padding: 6px 8px;
border: 1px solid rgba(255, 255, 255, 0.11);
border-radius: 6px;
background: rgba(7, 16, 24, 0.78);
color: #dcebed;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
white-space: nowrap;
backdrop-filter: blur(8px);
}
@media (max-width: 780px) {
.header {
top: 22px;
left: 22px;
max-width: calc(100vw - 44px);
}
.intro {
max-width: 320px;
font-size: 11px;
}
.status {
display: none;
}
.panel {
bottom: 18px;
left: 18px;
width: calc(100vw - 36px);
}
.legend,
.hint {
display: none;
}
}
</style>
</head>
<body>
<canvas
id="scene"
aria-label="Interactive exploded view of an articulated bicycle chain"
></canvas>
<header class="header">
<div class="eyebrow">Kinematic study / 01</div>
<h1>Chain link<br>articulation.</h1>
<p class="intro">
Alternating inner and outer plates, rollers, and axle pins.
Reassemble the section to inspect revolute motion while every plate
retains a fixed endpoint-to-endpoint span.
</p>
</header>
<div class="status">
<span class="status-dot"></span>
<span id="modeLabel">Exploded view</span>
</div>
<section class="panel" aria-label="Scene controls">
<div class="panel-heading">
<strong>Assembly controls</strong>
<span class="rigidity">Fixed plate span</span>
</div>
<div class="control">
<label class="control-label" for="assembly">
<span>Exploded separation</span>
<output id="assemblyValue">84%</output>
</label>
<input
id="assembly"
type="range"
min="0"
max="100"
value="84"
step="1"
>
</div>
<div class="control">
<label class="control-label" for="bend">
<span>Articulation per pin</span>
<output id="bendValue">8.0°</output>
</label>
<input
id="bend"
type="range"
min="0"
max="16"
value="8"
step="0.1"
>
</div>
<div class="buttons">
<button id="explodeButton" type="button">Explode</button>
<button id="assembleButton" class="active" type="button">Reassemble</button>
<button id="flexButton" type="button">Flex motion</button>
</div>
</section>
<aside class="legend" aria-label="Component legend">
<div class="legend-title">Component key</div>
<div class="legend-row">
<span class="swatch" style="--c: var(--outer)"></span>
Outer plates
</div>
<div class="legend-row">
<span class="swatch" style="--c: var(--inner)"></span>
Inner plates
</div>
<div class="legend-row">
<span class="swatch" style="--c: var(--roller)"></span>
Rollers
</div>
<div class="legend-row">
<span class="swatch" style="--c: var(--pin)"></span>
Axle pins
</div>
</aside>
<div class="hint">Drag to orbit · Scroll to zoom · E / R</div>
<div id="outerCallout" class="callout" style="--c: var(--outer)">
<i></i><span>Outer plate</span>
</div>
<div id="innerCallout" class="callout" style="--c: var(--inner)">
<i></i><span>Inner plate</span>
</div>
<div id="rollerCallout" class="callout" style="--c: var(--roller)">
<i></i><span>Roller</span>
</div>
<div id="pinCallout" class="callout" style="--c: var(--pin)">
<i></i><span>Axle pin</span>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const assemblyInput = document.querySelector("#assembly");
const bendInput = document.querySelector("#bend");
const assemblyValue = document.querySelector("#assemblyValue");
const bendValue = document.querySelector("#bendValue");
const modeLabel = document.querySelector("#modeLabel");
const explodeButton = document.querySelector("#explodeButton");
const assembleButton = document.querySelector("#assembleButton");
const flexButton = document.querySelector("#flexButton");
const outerCallout = document.querySelector("#outerCallout");
const innerCallout = document.querySelector("#innerCallout");
const rollerCallout = document.querySelector("#rollerCallout");
const pinCallout = document.querySelector("#pinCallout");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071018);
scene.fog = new THREE.FogExp2(0x071018, 0.025);
const renderer = new THREE.WebGLRenderer({
canvas,
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.18;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
// Direct camera controls: no examples/addons are used.
const orbit = {
yaw: 0.34,
pitch: 0.39,
radius: 11.8,
targetYaw: 0.34,
targetPitch: 0.39,
targetRadius: 11.8,
target: new THREE.Vector3(0, 0.05, 0)
};
function updateCamera(dt) {
const smoothing = 1 - Math.exp(-dt * 10);
orbit.yaw += (orbit.targetYaw - orbit.yaw) * smoothing;
orbit.pitch += (orbit.targetPitch - orbit.pitch) * smoothing;
orbit.radius += (orbit.targetRadius - orbit.radius) * smoothing;
const cp = Math.cos(orbit.pitch);
camera.position.set(
orbit.target.x +
orbit.radius * Math.sin(orbit.yaw) * cp,
orbit.target.y +
orbit.radius * Math.sin(orbit.pitch),
orbit.target.z +
orbit.radius * Math.cos(orbit.yaw) * cp
);
camera.lookAt(orbit.target);
}
// Lighting.
scene.add(new THREE.HemisphereLight(0xbceeff, 0x101820, 1.55));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.4);
keyLight.position.set(4.5, 7.5, 7.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const warmRim = new THREE.PointLight(0xff6748, 22, 16, 2);
warmRim.position.set(-5, 1.5, 2.5);
scene.add(warmRim);
const coolRim = new THREE.PointLight(0x28d6c1, 24, 18, 2);
coolRim.position.set(5, -0.5, -2.5);
scene.add(coolRim);
// Ground and technical grid.
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(42, 42),
new THREE.MeshStandardMaterial({
color: 0x0a151d,
metalness: 0.08,
roughness: 0.92
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -2.25;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(32, 32, 0x24434a, 0x142b34);
grid.position.y = -2.235;
grid.material.transparent = true;
grid.material.opacity = 0.28;
grid.material.depthWrite = false;
scene.add(grid);
const PITCH = 1.22;
const LINK_COUNT = 8;
const innerMaterial = new THREE.MeshPhysicalMaterial({
color: 0xff7052,
metalness: 0.58,
roughness: 0.27,
clearcoat: 0.42,
clearcoatRoughness: 0.28
});
const outerMaterial = new THREE.MeshPhysicalMaterial({
color: 0x32c9b5,
metalness: 0.68,
roughness: 0.24,
clearcoat: 0.5,
clearcoatRoughness: 0.25
});
const rollerMaterial = new THREE.MeshStandardMaterial({
color: 0x738793,
metalness: 0.9,
roughness: 0.23
});
const rollerEdgeMaterial = new THREE.MeshStandardMaterial({
color: 0xa8bac3,
metalness: 0.95,
roughness: 0.2
});
const pinMaterial = new THREE.MeshStandardMaterial({
color: 0xe5f0f3,
metalness: 0.95,
roughness: 0.17
});
const pinCapMaterial = new THREE.MeshStandardMaterial({
color: 0xaebfc6,
metalness: 0.94,
roughness: 0.22
});
// A stylized waisted, dog-bone plate with real pivot holes.
function createPlateGeometry(type) {
const isOuter = type === "outer";
const r = isOuter ? 0.34 : 0.275;
const waist = isOuter ? 0.165 : 0.125;
const depth = isOuter ? 0.085 : 0.075;
const holeRadius = isOuter ? 0.072 : 0.066;
const shape = new THREE.Shape();
shape.moveTo(0, r);
shape.lineTo(0.20, r);
shape.bezierCurveTo(
0.38, r,
0.48, waist,
PITCH * 0.5, waist
);
shape.bezierCurveTo(
PITCH - 0.48, waist,
PITCH - 0.38, r,
PITCH - 0.17, r
);
shape.lineTo(PITCH, r);
shape.bezierCurveTo(
PITCH + 0.15, r,
PITCH + 0.15, -r,
PITCH, -r
);
shape.lineTo(PITCH - 0.17, -r);
shape.bezierCurveTo(
PITCH - 0.38, -r,
PITCH - 0.48, -waist,
PITCH * 0.5, -waist
);
shape.bezierCurveTo(
0.48, -waist,
0.38, -r,
0.20, -r
);
shape.lineTo(0, -r);
shape.bezierCurveTo(
-0.15, -r,
-0.15, r,
0, r
);
shape.closePath();
for (const x of [0, PITCH]) {
const hole = new THREE.Path();
hole.absarc(x, 0, holeRadius, 0, Math.PI * 2, true);
shape.holes.push(hole);
}
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
steps: 1,
curveSegments: 18,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: isOuter ? 0.022 : 0.018,
bevelThickness: isOuter ? 0.020 : 0.016
});
geometry.translate(0, 0, -depth * 0.5);
geometry.computeVertexNormals();
return geometry;
}
const outerGeometry = createPlateGeometry("outer");
const innerGeometry = createPlateGeometry("inner");
const outerEdges = new THREE.EdgesGeometry(outerGeometry, 34);
const innerEdges = new THREE.EdgesGeometry(innerGeometry, 34);
const outerEdgeMaterial = new THREE.LineBasicMaterial({
color: 0xd8fff8,
transparent: true,
opacity: 0.23
});
const innerEdgeMaterial = new THREE.LineBasicMaterial({
color: 0xffe8df,
transparent: true,
opacity: 0.22
});
function createPlate(type, side) {
const isOuter = type === "outer";
const mesh = new THREE.Mesh(
isOuter ? outerGeometry : innerGeometry,
isOuter ? outerMaterial : innerMaterial
);
mesh.castShadow = true;
mesh.receiveShadow = true;
const edges = new THREE.LineSegments(
isOuter ? outerEdges : innerEdges,
isOuter ? outerEdgeMaterial : innerEdgeMaterial
);
edges.scale.setScalar(1.002);
mesh.add(edges);
mesh.userData.side = side;
return mesh;
}
const links = [];
for (let i = 0; i < LINK_COUNT; i++) {
const type = i % 2 === 0 ? "outer" : "inner";
const group = new THREE.Group();
const plates = [];
for (const side of [-1, 1]) {
const plate = createPlate(type, side);
group.add(plate);
plates.push(plate);
}
scene.add(group);
links.push({
group,
plates,
type,
index: i
});
}
// Shared joint hardware.
const rollerGeometry = new THREE.CylinderGeometry(
0.145, 0.145, 0.25, 32
);
const rollerRingGeometry = new THREE.TorusGeometry(
0.118, 0.022, 10, 32
);
const pinGeometry = new THREE.CylinderGeometry(
0.052, 0.052, 0.94, 24
);
const pinCapGeometry = new THREE.CylinderGeometry(
0.097, 0.097, 0.055, 24
);
const joints = [];
for (let i = 0; i <= LINK_COUNT; i++) {
const rollerGroup = new THREE.Group();
const pinGroup = new THREE.Group();
const roller = new THREE.Mesh(
rollerGeometry,
rollerMaterial
);
roller.rotation.x = Math.PI / 2;
roller.castShadow = true;
roller.receiveShadow = true;
rollerGroup.add(roller);
for (const z of [-0.132, 0.132]) {
const ring = new THREE.Mesh(
rollerRingGeometry,
rollerEdgeMaterial
);
ring.position.z = z;
ring.castShadow = true;
rollerGroup.add(ring);
}
const pin = new THREE.Mesh(
pinGeometry,
pinMaterial
);
pin.rotation.x = Math.PI / 2;
pin.castShadow = true;
pinGroup.add(pin);
for (const z of [-0.495, 0.495]) {
const cap = new THREE.Mesh(
pinCapGeometry,
pinCapMaterial
);
cap.rotation.x = Math.PI / 2;
cap.position.z = z;
cap.castShadow = true;
pinGroup.add(cap);
}
scene.add(rollerGroup, pinGroup);
joints.push({
rollerGroup,
pinGroup
});
}
let assemblyTarget = assemblyInput.value / 100;
let assemblyCurrent = assemblyTarget;
let bendTarget = Number(bendInput.value);
let bendCurrent = bendTarget;
let flexing = false;
function setAssembly(value) {
assemblyTarget = THREE.MathUtils.clamp(value, 0, 1);
assemblyInput.value = Math.round(assemblyTarget * 100);
updateButtonStates();
}
function setBend(value) {
bendTarget = THREE.MathUtils.clamp(value, 0, 16);
bendInput.value = bendTarget.toFixed(1);
}
function updateButtonStates() {
explodeButton.classList.toggle("active", assemblyTarget > 0.5);
assembleButton.classList.toggle("active", assemblyTarget <= 0.5);
flexButton.classList.toggle("active", flexing);
}
assemblyInput.addEventListener("input", () => {
setAssembly(Number(assemblyInput.value) / 100);
});
bendInput.addEventListener("input", () => {
setBend(Number(bendInput.value));
});
explodeButton.addEventListener("click", () => {
setAssembly(1);
});
assembleButton.addEventListener("click", () => {
setAssembly(0);
});
flexButton.addEventListener("click", () => {
flexing = !flexing;
updateButtonStates();
});
function resetView() {
orbit.targetYaw = 0.34;
orbit.targetPitch = 0.39;
orbit.targetRadius = 11.8;
}
canvas.addEventListener("dblclick", resetView);
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
if (event.key.toLowerCase() === "e") {
setAssembly(1);
} else if (event.key.toLowerCase() === "r") {
setAssembly(0);
} else if (event.code === "Space") {
event.preventDefault();
flexing = !flexing;
updateButtonStates();
}
});
// Pointer orbit controls implemented locally.
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
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;
orbit.targetYaw -= dx * 0.006;
orbit.targetPitch = THREE.MathUtils.clamp(
orbit.targetPitch + dy * 0.005,
-0.12,
1.15
);
});
function stopDragging(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.targetRadius = THREE.MathUtils.clamp(
orbit.targetRadius * Math.exp(event.deltaY * 0.001),
6.2,
20
);
},
{ passive: false }
);
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
const points = Array.from(
{ length: LINK_COUNT + 1 },
() => new THREE.Vector3()
);
const projected = new THREE.Vector3();
const labelAnchor = new THREE.Vector3();
function projectLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.1 &&
Math.abs(projected.y) < 1.1;
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
element.style.opacity = visible && assemblyCurrent > 0.27 ? "1" : "0";
}
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
const response = 1 - Math.exp(-dt * 4.5);
assemblyCurrent += (assemblyTarget - assemblyCurrent) * response;
bendCurrent += (bendTarget - bendCurrent) * response;
const oscillation = flexing ? Math.sin(elapsed * 0.82) * 2.6 : 0;
const turnDegrees = bendCurrent + oscillation;
const turn = THREE.MathUtils.degToRad(turnDegrees);
/*
* Each rigid link is rooted at its incoming pin.
* Link i+1 rotates about the exact endpoint of link i.
* The endpoint span is always PITCH and no link is scaled.
*/
points[0].set(0, 0, 0);
for (let i = 0; i < LINK_COUNT; i++) {
const theta = turn * (i - (LINK_COUNT - 1) * 0.5);
points[i + 1].set(
points[i].x + Math.cos(theta) * PITCH,
points[i].y + Math.sin(theta) * PITCH,
0
);
}
// Recenter the articulated chain without changing its geometry.
let minX = Infinity;
let maxX = -Infinity;
let minY = Infinity;
let maxY = -Infinity;
for (const point of points) {
minX = Math.min(minX, point.x);
maxX = Math.max(maxX, point.x);
minY = Math.min(minY, point.y);
maxY = Math.max(maxY, point.y);
}
const centerX = (minX + maxX) * 0.5;
const centerY = (minY + maxY) * 0.5;
for (let i = 0; i < LINK_COUNT; i++) {
const theta = turn * (i - (LINK_COUNT - 1) * 0.5);
const link = links[i];
link.group.position.set(
points[i].x - centerX,
points[i].y - centerY,
0
);
link.group.rotation.z = theta;
const isOuter = link.type === "outer";
const baseSpacing = isOuter ? 0.36 : 0.19;
const explosionSpacing = isOuter ? 0.92 : 0.58;
for (const plate of link.plates) {
plate.position.z =
plate.userData.side *
(baseSpacing + assemblyCurrent * explosionSpacing);
}
}
for (let i = 0; i <= LINK_COUNT; i++) {
const joint = joints[i];
joint.rollerGroup.position.set(
points[i].x - centerX,
points[i].y - centerY,
-assemblyCurrent * 0.28
);
joint.pinGroup.position.set(
points[i].x - centerX,
points[i].y - centerY,
assemblyCurrent * 1.20
);
}
assemblyValue.textContent =
`${Math.round(assemblyCurrent * 100)}%`;
bendValue.textContent =
`${(bendCurrent + oscillation).toFixed(1)}°`;
modeLabel.textContent =
assemblyCurrent > 0.42 ? "Exploded view" : "Reassembled";
updateCamera(dt);
scene.updateMatrixWorld(true);
// Lightweight projected labels for the exploded components.
labelAnchor.set(0.43, 0.33, 0);
links[0].plates[1].localToWorld(labelAnchor);
projectLabel(outerCallout, labelAnchor);
labelAnchor.set(0.74, -0.22, 0);
links[1].plates[1].localToWorld(labelAnchor);
projectLabel(innerCallout, labelAnchor);
joints[1].rollerGroup.getWorldPosition(labelAnchor);
labelAnchor.y += 0.22;
projectLabel(rollerCallout, labelAnchor);
joints[3].pinGroup.getWorldPosition(labelAnchor);
labelAnchor.y += 0.24;
projectLabel(pinCallout, labelAnchor);
renderer.render(scene, camera);
}
updateButtonStates();
animate();
</script>
</body>
</html>
```