Prompt specification
Build or customize the Three.js 3D scene "Adapter connection chain" 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: Adapter connection chain
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/adapter-connection-chain-x0310
Scene Description Prompt:
I'd like to see this as a voxel Three.js scene: adapter connection chain. Join a handle, adapter, extension, and socket through aligned interfaces; rotate the completed assembly and verify that each connected piece shares the same angular motion. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (adapter-connection-chain-x0310):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Adapter Connection Chain</title>
<style>
:root {
--bg: #070b14;
--panel: rgba(10, 17, 31, 0.82);
--border: rgba(148, 177, 214, 0.18);
--text: #eaf2ff;
--muted: #8292ad;
--cyan: #52dcff;
--green: #55efad;
--amber: #ffb84d;
--violet: #c084fc;
}
* { 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;
outline: none;
}
.hud {
position: fixed;
inset: 0;
z-index: 5;
pointer-events: none;
}
.glass {
background: var(--panel);
border: 1px solid var(--border);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
}
.brand {
position: absolute;
top: 24px;
left: 24px;
display: flex;
align-items: center;
gap: 13px;
}
.brand-icon {
width: 40px;
height: 40px;
position: relative;
display: grid;
place-items: center;
border: 1px solid rgba(82, 220, 255, 0.35);
background: rgba(82, 220, 255, 0.08);
box-shadow: inset 0 0 22px rgba(82, 220, 255, 0.08);
}
.brand-icon::before,
.brand-icon::after {
content: "";
position: absolute;
background: var(--cyan);
}
.brand-icon::before {
width: 22px;
height: 5px;
}
.brand-icon::after {
width: 5px;
height: 22px;
}
.eyebrow {
margin: 0 0 4px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(18px, 2vw, 27px);
line-height: 1;
letter-spacing: -0.04em;
}
.telemetry {
position: absolute;
top: 24px;
right: 24px;
min-width: 205px;
padding: 13px 15px;
border-radius: 12px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 22px;
}
.telemetry-label {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.telemetry strong {
display: block;
margin-top: 4px;
font-size: 14px;
letter-spacing: 0.04em;
}
.live-dot {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 5px rgba(85, 239, 173, 0.09), 0 0 18px var(--green);
animation: pulse 1.8s ease-in-out infinite;
}
@keyframes pulse {
50% { opacity: 0.55; transform: scale(0.85); }
}
.panel {
position: absolute;
top: 102px;
right: 24px;
width: 318px;
padding: 17px;
border-radius: 15px;
pointer-events: auto;
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 13px;
}
.panel-title strong {
font-size: 12px;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.lock-pill {
color: var(--green);
border: 1px solid rgba(85, 239, 173, 0.25);
background: rgba(85, 239, 173, 0.07);
border-radius: 999px;
padding: 5px 8px;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.08em;
}
.table-head,
.part-row {
display: grid;
grid-template-columns: 1fr 58px 58px;
align-items: center;
gap: 6px;
}
.table-head {
padding: 0 7px 7px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.11em;
}
.table-head span:not(:first-child) { text-align: right; }
.part-row {
min-height: 35px;
padding: 7px;
border-top: 1px solid rgba(148, 177, 214, 0.09);
font-size: 11px;
}
.part-name {
display: flex;
align-items: center;
gap: 8px;
color: #dbe7f8;
}
.part-name i {
width: 7px;
height: 7px;
background: var(--part-color);
box-shadow: 0 0 10px color-mix(in srgb, var(--part-color) 65%, transparent);
}
.part-row b {
text-align: right;
font-size: 11px;
font-variant-numeric: tabular-nums;
}
.part-row .delta { color: var(--green); }
.drift {
margin-top: 10px;
padding: 10px 7px;
border-top: 1px solid rgba(148, 177, 214, 0.12);
display: flex;
align-items: center;
justify-content: space-between;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
}
.drift b {
color: var(--green);
font-size: 12px;
letter-spacing: 0;
}
.test-button,
.control-button {
color: var(--text);
border: 1px solid var(--border);
background: rgba(255, 255, 255, 0.045);
cursor: pointer;
transition: border-color 160ms ease, background 160ms ease, transform 160ms ease;
}
.test-button:hover,
.control-button:hover {
border-color: rgba(82, 220, 255, 0.48);
background: rgba(82, 220, 255, 0.09);
}
.test-button:active,
.control-button:active { transform: translateY(1px); }
.test-button {
width: 100%;
margin-top: 9px;
padding: 11px 12px;
border-radius: 9px;
color: var(--cyan);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.1em;
}
.result {
min-height: 32px;
margin-top: 10px;
color: var(--muted);
font-size: 10px;
line-height: 1.55;
}
.result.pass { color: var(--green); }
.result.fail { color: #ff718b; }
.dock {
position: absolute;
left: 50%;
bottom: 23px;
transform: translateX(-50%);
padding: 9px;
border-radius: 14px;
display: flex;
align-items: center;
gap: 8px;
pointer-events: auto;
white-space: nowrap;
}
.control-button {
height: 37px;
padding: 0 13px;
border-radius: 8px;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.08em;
}
.control-button.active {
color: #061018;
border-color: var(--cyan);
background: var(--cyan);
}
.speed-control {
height: 37px;
padding: 0 12px;
border-left: 1px solid var(--border);
border-right: 1px solid var(--border);
display: flex;
align-items: center;
gap: 9px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.1em;
}
input[type="range"] {
width: 82px;
accent-color: var(--cyan);
cursor: pointer;
}
#speedValue {
width: 32px;
color: var(--text);
text-align: right;
font-variant-numeric: tabular-nums;
}
.hint {
position: absolute;
left: 25px;
bottom: 31px;
color: rgba(170, 187, 211, 0.6);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#labels {
position: fixed;
inset: 0;
z-index: 3;
pointer-events: none;
}
.part-tag {
position: absolute;
transform: translate(-50%, -100%);
padding: 6px 8px;
border: 1px solid color-mix(in srgb, var(--tag-color) 42%, transparent);
border-radius: 6px;
background: rgba(7, 12, 23, 0.78);
color: #dce8fa;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.1em;
white-space: nowrap;
box-shadow: 0 8px 22px rgba(0, 0, 0, 0.25);
}
.part-tag span {
margin-right: 6px;
color: var(--tag-color);
}
.part-tag::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 17px;
background: linear-gradient(var(--tag-color), transparent);
opacity: 0.65;
}
@media (max-width: 760px) {
.brand { top: 16px; left: 16px; }
.brand-icon { width: 34px; height: 34px; }
.telemetry { display: none; }
.panel {
top: auto;
right: 12px;
bottom: 84px;
width: calc(100% - 24px);
padding: 12px;
}
.part-row { min-height: 29px; padding: 5px 7px; }
.dock {
bottom: 12px;
width: calc(100% - 24px);
justify-content: center;
}
.control-button { padding: 0 9px; }
.speed-control { display: none; }
.hint { display: none; }
.part-tag { display: none; }
.result { min-height: 0; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel adapter connection chain"></canvas>
<div id="labels"></div>
<div class="hud">
<header class="brand">
<div class="brand-icon" aria-hidden="true"></div>
<div>
<p class="eyebrow">Voxel interface study / 04</p>
<h1>Adapter Connection Chain</h1>
</div>
</header>
<div class="telemetry glass">
<div>
<div class="telemetry-label">Chain state</div>
<strong id="chainState">LOCKED · 4/4</strong>
</div>
<span class="live-dot"></span>
</div>
<aside class="panel glass">
<div class="panel-title">
<strong>Angular telemetry</strong>
<span class="lock-pill" id="lockPill">SYNCED</span>
</div>
<div class="table-head">
<span>COMPONENT</span>
<span>WORLD θ</span>
<span>Δθ</span>
</div>
<div class="part-row" style="--part-color:#4d8dff">
<span class="part-name"><i></i>Handle</span>
<b class="angle">0.0°</b>
<b class="delta">0.00°</b>
</div>
<div class="part-row" style="--part-color:#ffad42">
<span class="part-name"><i></i>Adapter</span>
<b class="angle">0.0°</b>
<b class="delta">0.00°</b>
</div>
<div class="part-row" style="--part-color:#42ddb1">
<span class="part-name"><i></i>Extension</span>
<b class="angle">0.0°</b>
<b class="delta">0.00°</b>
</div>
<div class="part-row" style="--part-color:#bb78ff">
<span class="part-name"><i></i>Socket</span>
<b class="angle">0.0°</b>
<b class="delta">0.00°</b>
</div>
<div class="drift">
<span>MAX WORLD DRIFT</span>
<b id="driftValue">0.00°</b>
</div>
<button class="test-button" id="testButton">RUN 90° SYNC TEST</button>
<div class="result" id="resultText">
Live Δθ is the angular change per frame. All values are measured in world space.
</div>
</aside>
<div class="dock glass">
<button class="control-button active" id="spinButton" aria-pressed="true">PAUSE SPIN</button>
<div class="speed-control">
<span>SPEED</span>
<input id="speedSlider" type="range" min="8" max="50" value="24" />
<span id="speedValue">24°</span>
</div>
<button class="control-button" id="resetButton">RESET VIEW</button>
</div>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
</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 renderer = new THREE.WebGLRenderer({
canvas,
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;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x070b14);
scene.fog = new THREE.Fog(0x070b14, 19, 39);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
theta: 0.72,
phi: 1.12,
radius: window.innerWidth < 760 ? 18.2 : 15.4,
target: new THREE.Vector3(-0.45, -0.05, 0)
};
function updateCamera() {
const { theta, phi, radius, target } = cameraState;
camera.position.set(
target.x + radius * Math.sin(phi) * Math.cos(theta),
target.y + radius * Math.cos(phi),
target.z + radius * Math.sin(phi) * Math.sin(theta)
);
camera.lookAt(target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xb9ddff, 0x111521, 1.75);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.0);
keyLight.position.set(4, 10, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -13;
keyLight.shadow.camera.right = 13;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const cyanLight = new THREE.DirectionalLight(0x46d9ff, 1.45);
cyanLight.position.set(-8, 3, 5);
scene.add(cyanLight);
const violetLight = new THREE.PointLight(0xb66cff, 24, 15, 2);
violetLight.position.set(5, 2.5, 3);
scene.add(violetLight);
function standard(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.08,
...options
});
}
const materials = {
handle: standard(0x347cf4, { roughness: 0.6 }),
handleDark: standard(0x1854bd, { roughness: 0.7 }),
handleLight: standard(0x69a2ff, { roughness: 0.56 }),
adapter: standard(0xf59b32, { roughness: 0.64 }),
adapterLight: standard(0xffc05a, { roughness: 0.56 }),
adapterDark: standard(0xb85d1e, { roughness: 0.72 }),
extension: standard(0x31c99d, { roughness: 0.6 }),
extensionLight: standard(0x62e6bd, { roughness: 0.54 }),
extensionDark: standard(0x167a65, { roughness: 0.72 }),
socket: standard(0x9a5de0, { roughness: 0.62 }),
socketLight: standard(0xc18cff, { roughness: 0.55 }),
socketDark: standard(0x5c2f91, { roughness: 0.75 }),
metal: standard(0xc1ccdc, { roughness: 0.33, metalness: 0.7 }),
metalDark: standard(0x65738a, { roughness: 0.42, metalness: 0.62 }),
black: standard(0x090d16, { roughness: 0.9 }),
gold: standard(0xffc857, {
roughness: 0.35,
metalness: 0.45,
emissive: 0x7a3d00,
emissiveIntensity: 0.18
})
};
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x06101d,
transparent: true,
opacity: 0.42
});
function addBox(parent, size, position, material, edge = false) {
const geometry = new THREE.BoxGeometry(...size);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
if (edge) {
mesh.add(new THREE.LineSegments(
new THREE.EdgesGeometry(geometry),
edgeMaterial
));
}
return mesh;
}
// A tiled voxel floor.
const tileGeometry = new THREE.BoxGeometry(0.78, 0.16, 0.78);
const tileMaterial = standard(0xffffff, { roughness: 0.96, metalness: 0.02 });
const columns = 31;
const rows = 23;
const tiles = new THREE.InstancedMesh(
tileGeometry,
tileMaterial,
columns * rows
);
tiles.receiveShadow = true;
tiles.instanceMatrix.setUsage(THREE.StaticDrawUsage);
const tileMatrix = new THREE.Matrix4();
const tileColor = new THREE.Color();
let tileIndex = 0;
for (let x = 0; x < columns; x++) {
for (let z = 0; z < rows; z++) {
const px = (x - (columns - 1) / 2) * 0.82;
const pz = (z - (rows - 1) / 2) * 0.82;
const noise = Math.abs(Math.sin(x * 17.17 + z * 31.73));
tileMatrix.makeTranslation(px, -2.18, pz);
tiles.setMatrixAt(tileIndex, tileMatrix);
const centerGlow = Math.max(0, 1 - Math.abs(pz) / 4.5);
tileColor.setHSL(
0.60,
0.22,
0.075 + noise * 0.018 + centerGlow * 0.018
);
tiles.setColorAt(tileIndex, tileColor);
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
if (tiles.instanceColor) tiles.instanceColor.needsUpdate = true;
scene.add(tiles);
// The entire connected chain rotates around this common X axis.
const assembly = new THREE.Group();
scene.add(assembly);
const handle = new THREE.Group();
const adapter = new THREE.Group();
const extension = new THREE.Group();
const socket = new THREE.Group();
handle.position.x = -4.65;
adapter.position.x = -2.50;
extension.position.x = 0.30;
socket.position.x = 3.40;
assembly.add(handle, adapter, extension, socket);
const pieces = [handle, adapter, extension, socket];
// HANDLE: square core and segmented voxel grip.
addBox(handle, [2.7, 0.62, 0.62], [0, 0, 0], materials.handleDark, true);
for (let i = 0; i < 9; i++) {
const x = -0.98 + i * 0.245;
const mat = i % 2 === 0 ? materials.handle : materials.handleLight;
addBox(handle, [0.19, 1.02, 1.02], [x, 0, 0], mat, true);
}
addBox(handle, [0.14, 1.15, 1.15], [-1.40, 0, 0], materials.handle, true);
addBox(handle, [0.30, 0.54, 0.54], [1.35, 0, 0], materials.metal, true);
addBox(
handle,
[0.86, 0.09, 0.18],
[0.05, 0.57, 0.37],
standard(0x8fc3ff, {
emissive: 0x174b9c,
emissiveIntensity: 0.7
})
);
// ADAPTER: stepped, block-built coupling body.
addBox(adapter, [1.18, 1.05, 1.05], [0, 0, 0], materials.adapter, true);
addBox(adapter, [0.38, 1.40, 1.40], [-0.58, 0, 0], materials.adapterDark, true);
addBox(adapter, [0.38, 1.40, 1.40], [0.58, 0, 0], materials.adapterLight, true);
addBox(adapter, [0.56, 1.54, 1.54], [0, 0, 0], materials.adapter, true);
addBox(
adapter,
[0.68, 0.08, 0.34],
[0, 0.81, 0.24],
standard(0xffdf82, {
emissive: 0x8a4b00,
emissiveIntensity: 0.45
})
);
addBox(adapter, [0.10, 0.54, 0.54], [-0.78, 0, 0], materials.metal, true);
addBox(adapter, [0.10, 0.54, 0.54], [0.78, 0, 0], materials.metal, true);
// EXTENSION: square shaft, raised rails and end necks.
addBox(extension, [3.52, 0.54, 0.54], [0, 0, 0], materials.extensionDark, true);
addBox(extension, [3.78, 0.18, 0.84], [0, 0.34, 0], materials.extension, true);
addBox(extension, [3.78, 0.18, 0.84], [0, -0.34, 0], materials.extension, true);
addBox(extension, [3.42, 0.16, 0.16], [0, 0, 0.49], materials.extensionLight, true);
addBox(extension, [3.42, 0.16, 0.16], [0, 0, -0.49], materials.extensionLight, true);
addBox(extension, [0.28, 0.56, 0.56], [-1.78, 0, 0], materials.metal, true);
addBox(extension, [0.28, 0.56, 0.56], [1.78, 0, 0], materials.metal, true);
// SOCKET: larger voxel housing with a dark front aperture.
addBox(socket, [1.46, 1.34, 1.34], [0, 0, 0], materials.socket, true);
addBox(socket, [1.68, 1.52, 1.52], [-0.14, 0, 0], materials.socketLight, true);
addBox(socket, [1.28, 1.16, 1.16], [-0.92, 0, 0], materials.socketDark, true);
addBox(socket, [0.28, 0.58, 0.58], [-1.08, 0, 0], materials.metal, true);
addBox(socket, [0.06, 0.78, 0.78], [0.86, 0, 0], materials.black);
addBox(socket, [0.12, 0.18, 1.05], [0.90, 0.48, 0], materials.socketLight, true);
addBox(socket, [0.12, 0.18, 1.05], [0.90, -0.48, 0], materials.socketLight, true);
addBox(socket, [0.12, 0.78, 0.18], [0.90, 0, 0.48], materials.socketLight, true);
addBox(socket, [0.12, 0.78, 0.18], [0.90, 0, -0.48], materials.socketLight, true);
// Identical octagonal voxel interfaces at each connection.
function createInterface(x) {
const group = new THREE.Group();
group.position.x = x;
addBox(group, [0.46, 0.44, 0.44], [0, 0, 0], materials.metalDark, true);
const ringOffsets = [
[0.64, 0], [-0.64, 0],
[0, 0.64], [0, -0.64],
[0.45, 0.45], [-0.45, 0.45],
[0.45, -0.45], [-0.45, -0.45]
];
for (const [y, z] of ringOffsets) {
addBox(
group,
[0.22, 0.22, 0.22],
[0, y, z],
materials.metal,
true
);
}
addBox(group, [0.25, 0.25, 0.25], [0, 0.73, 0], materials.gold, true);
assembly.add(group);
}
createInterface(-3.30);
createInterface(-1.70);
createInterface(2.30);
// A dashed axis makes the shared rotation axis explicit.
const axisGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-6.25, 0, 0),
new THREE.Vector3(5.10, 0, 0)
]);
const axis = new THREE.Line(
axisGeometry,
new THREE.LineDashedMaterial({
color: 0x65dfff,
dashSize: 0.18,
gapSize: 0.13,
transparent: true,
opacity: 0.32
})
);
axis.computeLineDistances();
assembly.add(axis);
// Offset witness blocks make the rotation easy to see.
const witnessMaterials = [
standard(0xb9d8ff, { emissive: 0x245ca8, emissiveIntensity: 0.8 }),
standard(0xffe1a0, { emissive: 0x9b5000, emissiveIntensity: 0.8 }),
standard(0xa3ffe1, { emissive: 0x087a5e, emissiveIntensity: 0.8 }),
standard(0xe2c0ff, { emissive: 0x6320a6, emissiveIntensity: 0.8 })
];
addBox(handle, [0.18, 0.19, 0.48], [-0.12, 0.65, 0.40], witnessMaterials[0]);
addBox(adapter, [0.18, 0.19, 0.48], [0.00, 0.87, 0.40], witnessMaterials[1]);
addBox(extension, [0.18, 0.19, 0.48], [-0.30, 0.52, 0.43], witnessMaterials[2]);
addBox(socket, [0.18, 0.19, 0.48], [-0.25, 0.86, 0.40], witnessMaterials[3]);
// DOM labels projected from fixed world-space anchors.
const labelHost = document.querySelector("#labels");
const labelData = [
{ name: "01", text: "HANDLE", x: -4.65, color: "#69a2ff" },
{ name: "02", text: "ADAPTER", x: -2.50, color: "#ffc05a" },
{ name: "03", text: "EXTENSION", x: 0.30, color: "#62e6bd" },
{ name: "04", text: "SOCKET", x: 3.40, color: "#c18cff" }
];
const projected = new THREE.Vector3();
for (const label of labelData) {
label.element = document.createElement("div");
label.element.className = "part-tag";
label.element.style.setProperty("--tag-color", label.color);
label.element.innerHTML = `<span>${label.name}</span>${label.text}`;
labelHost.appendChild(label.element);
}
function updateLabels() {
for (const label of labelData) {
projected.set(label.x, 1.48, 0).project(camera);
const visible = projected.z > -1 && projected.z < 1;
label.element.style.display = visible ? "block" : "none";
label.element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
label.element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
}
// Custom camera controls; no external controls addon is used.
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;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.30,
1.45
);
});
function endDrag(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
9.5,
27
);
}, { passive: false });
function resetView() {
cameraState.theta = 0.72;
cameraState.phi = 1.12;
cameraState.radius = window.innerWidth < 760 ? 18.2 : 15.4;
cameraState.target.set(-0.45, -0.05, 0);
}
canvas.addEventListener("dblclick", resetView);
document.querySelector("#resetButton").addEventListener("click", resetView);
const spinButton = document.querySelector("#spinButton");
const speedSlider = document.querySelector("#speedSlider");
const speedValue = document.querySelector("#speedValue");
const testButton = document.querySelector("#testButton");
const resultText = document.querySelector("#resultText");
const driftValue = document.querySelector("#driftValue");
const lockPill = document.querySelector("#lockPill");
const chainState = document.querySelector("#chainState");
const angleElements = [...document.querySelectorAll(".angle")];
const deltaElements = [...document.querySelectorAll(".delta")];
const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let autoSpin = !reducedMotion;
let spinSpeed = Number(speedSlider.value);
let test = null;
let lastTestDeltas = null;
let previousQuaternions = null;
let liveDeltas = [0, 0, 0, 0];
let lastTelemetryUpdate = 0;
function updateSpinButton() {
spinButton.textContent = autoSpin ? "PAUSE SPIN" : "RESUME SPIN";
spinButton.classList.toggle("active", autoSpin);
spinButton.setAttribute("aria-pressed", String(autoSpin));
}
updateSpinButton();
spinButton.addEventListener("click", () => {
autoSpin = !autoSpin;
lastTestDeltas = null;
resultText.className = "result";
resultText.textContent = "Live Δθ is the angular change per frame. All values are measured in world space.";
updateSpinButton();
});
speedSlider.addEventListener("input", () => {
spinSpeed = Number(speedSlider.value);
speedValue.textContent = `${spinSpeed}°`;
});
function getWorldQuaternions() {
scene.updateMatrixWorld(true);
return pieces.map((piece) => {
const quaternion = new THREE.Quaternion();
piece.getWorldQuaternion(quaternion);
return quaternion;
});
}
function quaternionAngle(quaternion) {
const euler = new THREE.Euler().setFromQuaternion(quaternion, "XYZ");
return THREE.MathUtils.euclideanModulo(euler.x * THREE.MathUtils.RAD2DEG, 360);
}
function beginSyncTest() {
if (test) return;
autoSpin = false;
updateSpinButton();
lastTestDeltas = null;
const startQuaternions = getWorldQuaternions();
test = {
startTime: performance.now(),
duration: 1450,
fromAngle: assembly.rotation.x,
toAngle: assembly.rotation.x + Math.PI / 2,
startQuaternions
};
testButton.textContent = "TESTING 90°…";
testButton.disabled = true;
resultText.className = "result";
resultText.textContent = "Rotating the completed chain 90° around its shared X axis…";
chainState.textContent = "SYNC TEST";
lockPill.textContent = "TESTING";
}
testButton.addEventListener("click", beginSyncTest);
function finishSyncTest() {
const endQuaternions = getWorldQuaternions();
const deltas = endQuaternions.map((endQuaternion, index) => {
const delta = endQuaternion.clone().multiply(
test.startQuaternions[index].clone().invert()
);
return 2 * Math.acos(
THREE.MathUtils.clamp(Math.abs(delta.w), -1, 1)
) * THREE.MathUtils.RAD2DEG;
});
const maxError = Math.max(...deltas.map((value) => Math.abs(value - 90)));
const spread = Math.max(...deltas) - Math.min(...deltas);
const passed = maxError < 0.05 && spread < 0.02;
lastTestDeltas = deltas;
test = null;
testButton.disabled = false;
testButton.textContent = "RUN AGAIN · 90° TEST";
resultText.className = `result ${passed ? "pass" : "fail"}`;
resultText.textContent = passed
? `PASS — all 4 components moved 90.0° about the same X axis. Maximum mismatch: ${maxError.toFixed(3)}°.`
: `FAIL — maximum mismatch was ${maxError.toFixed(3)}°.`;
chainState.textContent = passed ? "LOCKED · 4/4" : "CHECK FAILED";
lockPill.textContent = passed ? "SYNCED" : "FAILED";
}
function updateTelemetry(now) {
if (now - lastTelemetryUpdate < 70) return;
lastTelemetryUpdate = now;
const quaternions = getWorldQuaternions();
if (previousQuaternions) {
liveDeltas = quaternions.map((quaternion, index) => {
return quaternion.angleTo(previousQuaternions[index]) *
THREE.MathUtils.RAD2DEG;
});
}
previousQuaternions = quaternions.map((quaternion) => quaternion.clone());
let maxDrift = 0;
for (let i = 1; i < quaternions.length; i++) {
maxDrift = Math.max(
maxDrift,
quaternions[0].angleTo(quaternions[i]) * THREE.MathUtils.RAD2DEG
);
}
const displayedDeltas = lastTestDeltas || liveDeltas;
angleElements.forEach((element, index) => {
element.textContent = `${quaternionAngle(quaternions[index]).toFixed(1)}°`;
deltaElements[index].textContent = `${displayedDeltas[index].toFixed(2)}°`;
});
driftValue.textContent = `${maxDrift.toFixed(2)}°`;
if (!test) {
const locked = maxDrift < 0.01;
lockPill.textContent = locked ? "SYNCED" : "CHECK";
chainState.textContent = locked ? "LOCKED · 4/4" : "VERIFYING";
lockPill.style.color = locked ? "#55efad" : "#ffb84d";
}
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
let previousTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (test) {
const raw = THREE.MathUtils.clamp(
(now - test.startTime) / test.duration,
0,
1
);
const eased = raw < 0.5
? 4 * raw * raw * raw
: 1 - Math.pow(-2 * raw + 2, 3) / 2;
assembly.rotation.x = THREE.MathUtils.lerp(
test.fromAngle,
test.toAngle,
eased
);
if (raw >= 1) finishSyncTest();
} else if (autoSpin) {
assembly.rotation.x += THREE.MathUtils.degToRad(spinSpeed) * delta;
}
updateCamera();
renderer.render(scene, camera);
updateTelemetry(now);
updateLabels();
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```