Prompt specification
Build or customize the Three.js 3D scene "Keyed Plug With One Asymmetric Notch" 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: Keyed Plug With One Asymmetric Notch
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/keyed-plug-with-one-asymmetric-notch-x2025
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring keyed connectors for unambiguous assembly. Model a plug and socket sharing one offset notch. Show why only the intended orientation fits while keeping the clearance visible. Stylized solid forms.
Working Three.js Starter (keyed-plug-with-one-asymmetric-notch-x2025):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Keyed Connector — Three.js</title>
<style>
:root {
--bg: #08111d;
--panel: rgba(13, 24, 39, 0.86);
--line: rgba(166, 194, 224, 0.18);
--text: #edf6ff;
--muted: #91a5bc;
--cyan: #55e4d2;
--amber: #ffad55;
--red: #ff6577;
}
* { 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;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 52% 43%, transparent 38%, rgba(2, 7, 14, 0.28) 100%),
linear-gradient(180deg, rgba(4, 10, 18, 0.2), transparent 24%, rgba(4, 10, 18, 0.42));
z-index: 2;
}
header {
position: fixed;
top: 28px;
left: 30px;
z-index: 4;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 9px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 28px;
height: 2px;
background: var(--cyan);
box-shadow: 0 0 14px rgba(85, 228, 210, 0.75);
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 43px);
line-height: 0.98;
font-weight: 750;
letter-spacing: -0.045em;
}
header p {
max-width: 460px;
margin: 13px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.status {
position: fixed;
top: 28px;
right: 28px;
z-index: 5;
display: flex;
align-items: center;
gap: 12px;
min-width: 260px;
padding: 13px 16px;
border: 1px solid var(--line);
border-radius: 13px;
background: var(--panel);
backdrop-filter: blur(16px);
box-shadow: 0 14px 50px rgba(0, 0, 0, 0.24);
transition: border-color 180ms ease, background 180ms ease;
}
.status.valid {
border-color: rgba(85, 228, 210, 0.36);
background: rgba(10, 37, 42, 0.88);
}
.status.blocked {
border-color: rgba(255, 101, 119, 0.42);
background: rgba(48, 19, 31, 0.9);
}
.status-dot {
width: 10px;
height: 10px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 0 5px rgba(85, 228, 210, 0.1), 0 0 18px rgba(85, 228, 210, 0.7);
}
.blocked .status-dot {
background: var(--red);
box-shadow: 0 0 0 5px rgba(255, 101, 119, 0.1), 0 0 18px rgba(255, 101, 119, 0.7);
}
.status strong {
display: block;
font-size: 11px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.status small {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 11px;
}
.panel {
position: fixed;
z-index: 5;
left: 50%;
bottom: 24px;
transform: translateX(-50%);
width: min(920px, calc(100vw - 32px));
padding: 13px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
backdrop-filter: blur(18px);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.34);
}
.panel-row {
display: flex;
align-items: center;
gap: 10px;
flex-wrap: wrap;
}
.panel-row + .panel-row {
margin-top: 10px;
padding-top: 10px;
border-top: 1px solid var(--line);
}
.control-group {
display: flex;
align-items: center;
gap: 7px;
}
.control-label {
margin-right: 3px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
}
button {
min-height: 36px;
padding: 0 13px;
border: 1px solid rgba(172, 199, 228, 0.18);
border-radius: 9px;
background: rgba(125, 154, 188, 0.08);
color: var(--text);
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
transition: background 150ms ease, border-color 150ms ease, transform 150ms ease;
}
button:hover {
background: rgba(125, 154, 188, 0.15);
border-color: rgba(172, 199, 228, 0.34);
}
button:active {
transform: translateY(1px);
}
button.active {
border-color: rgba(85, 228, 210, 0.55);
background: rgba(85, 228, 210, 0.14);
color: #c8fff7;
}
button.primary {
border-color: rgba(85, 228, 210, 0.55);
background: var(--cyan);
color: #06201f;
}
button.primary:hover {
background: #7aeee0;
}
.angle-button {
min-width: 50px;
font-variant-numeric: tabular-nums;
}
.spacer { flex: 1; }
.hint {
color: var(--muted);
font-size: 11px;
white-space: nowrap;
}
.clearance-note {
display: flex;
align-items: center;
gap: 8px;
color: var(--muted);
font-size: 11px;
}
.clearance-swatch {
width: 9px;
height: 9px;
border-radius: 2px;
background: var(--cyan);
box-shadow: 0 0 10px rgba(85, 228, 210, 0.5);
}
.callout {
position: fixed;
z-index: 3;
pointer-events: none;
padding: 7px 10px;
border: 1px solid rgba(255, 173, 85, 0.32);
border-radius: 8px;
background: rgba(16, 25, 38, 0.86);
color: #ffe1ba;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
white-space: nowrap;
transform: translate(-50%, -150%);
opacity: 0;
transition: opacity 120ms ease;
}
.callout::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 21px;
background: linear-gradient(var(--amber), transparent);
}
.callout.visible { opacity: 1; }
@media (max-width: 720px) {
header { top: 18px; left: 18px; }
header p { display: none; }
.status {
top: 18px;
right: 18px;
min-width: 0;
padding: 11px;
}
.status small { display: none; }
.panel { bottom: 12px; width: calc(100vw - 20px); padding: 10px; }
.control-label { display: none; }
.hint { display: none; }
.callout { display: none; }
button { min-height: 34px; padding: 0 10px; }
}
</style>
</head>
<body>
<div class="vignette"></div>
<header>
<div class="eyebrow">Assembly datum / 01</div>
<h1>One notch.<br />One orientation.</h1>
<p>
The plug and socket share the same offset notch. In the exploded view the
clearance envelope stays visible; rotating the plug moves solid material
into the socket shoulder.
</p>
</header>
<div id="status" class="status valid" aria-live="polite">
<span class="status-dot"></span>
<div>
<strong id="statusTitle">Datum aligned</strong>
<small id="statusDetail">Offset notches share the same axis.</small>
</div>
</div>
<div id="socketTag" class="callout">Socket offset notch</div>
<div id="plugTag" class="callout">Plug offset notch</div>
<section class="panel" aria-label="Connector controls">
<div class="panel-row">
<div class="control-group">
<span class="control-label">Rotate plug</span>
<button class="angle-button active" data-angle="0">0°</button>
<button class="angle-button" data-angle="90">90°</button>
<button class="angle-button" data-angle="180">180°</button>
<button class="angle-button" data-angle="270">270°</button>
</div>
<div class="spacer"></div>
<div class="clearance-note">
<span class="clearance-swatch"></span>
<span>Visible clearance envelope</span>
</div>
</div>
<div class="panel-row">
<button id="assembleButton" class="primary">Test assembly</button>
<button id="explodeButton">Explode</button>
<button id="clearanceButton" class="active">Clearance on</button>
<div class="spacer"></div>
<span class="hint">Drag to orbit · wheel to zoom · ← → to rotate</span>
<button id="cameraButton">Reset view</button>
</div>
</section>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x08111d);
scene.fog = new THREE.Fog(0x08111d, 13, 27);
const renderer = new THREE.WebGLRenderer({ antialias: true });
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.08;
document.body.prepend(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemisphere = new THREE.HemisphereLight(0xbfe9ff, 0x14202f, 1.55);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(5, 8, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x58d9ff, 1.8);
rimLight.position.set(-4, 3, -7);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffa85b, 18, 10, 2);
warmLight.position.set(-1.8, 2.7, 3.2);
scene.add(warmLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0x0b1623,
roughness: 0.92,
metalness: 0.03
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -2.42;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(40, 40, 0x294057, 0x1a2b3d);
grid.position.y = -2.405;
grid.material.transparent = true;
grid.material.opacity = 0.33;
scene.add(grid);
// -------------------------------------------------------------------------
// Connector profile
//
// The same asymmetric chamfered profile is used by the plug nose and the
// socket bore. One notch is cut into the top, offset from center. The bore
// is scaled by 1.075 to leave a small visible assembly clearance.
// -------------------------------------------------------------------------
const BASE = {
width: 3.4,
height: 3.0,
corner: 0.62,
notchLeft: -0.95,
notchRight: 0.35,
notchDepth: 0.72
};
function profilePoints(scale = 1) {
const w = BASE.width * scale;
const h = BASE.height * scale;
const c = BASE.corner * scale;
const nl = BASE.notchLeft * scale;
const nr = BASE.notchRight * scale;
const nd = BASE.notchDepth * scale;
return [
new THREE.Vector2(-w / 2 + c, -h / 2),
new THREE.Vector2(w / 2 - c, -h / 2),
new THREE.Vector2(w / 2, -h / 2 + c),
new THREE.Vector2(w / 2, h / 2 - c),
new THREE.Vector2(w / 2 - c, h / 2),
new THREE.Vector2(nr, h / 2),
new THREE.Vector2(nr, h / 2 - nd),
new THREE.Vector2(nl, h / 2 - nd),
new THREE.Vector2(nl, h / 2),
new THREE.Vector2(-w / 2 + c, h / 2),
new THREE.Vector2(-w / 2, h / 2 - c),
new THREE.Vector2(-w / 2, -h / 2 + c)
];
}
function shapeFrom(points) {
const shape = new THREE.Shape();
shape.moveTo(points[0].x, points[0].y);
for (let i = 1; i < points.length; i++) {
shape.lineTo(points[i].x, points[i].y);
}
shape.closePath();
return shape;
}
function pathFrom(points) {
const path = new THREE.Path();
path.moveTo(points[0].x, points[0].y);
for (let i = 1; i < points.length; i++) {
path.lineTo(points[i].x, points[i].y);
}
path.closePath();
return path;
}
function extrudedMesh(points, depth, material, bevelSize = 0.045) {
const geometry = new THREE.ExtrudeGeometry(shapeFrom(points), {
depth,
steps: 1,
bevelEnabled: true,
bevelSegments: 2,
bevelSize,
bevelThickness: bevelSize * 0.85,
curveSegments: 8
});
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function lineLoop(points, color, opacity = 1) {
const geometry = new THREE.BufferGeometry().setFromPoints(
points.map(p => new THREE.Vector3(p.x, p.y, 0))
);
const material = new THREE.LineBasicMaterial({
color,
transparent: opacity < 1,
opacity
});
const line = new THREE.LineLoop(geometry, material);
line.rotation.y = -Math.PI / 2;
return line;
}
const socketMaterial = new THREE.MeshStandardMaterial({
color: 0x34475e,
roughness: 0.57,
metalness: 0.18,
flatShading: true
});
const socketBackMaterial = new THREE.MeshStandardMaterial({
color: 0x182536,
roughness: 0.7,
metalness: 0.15,
flatShading: true
});
const plugMaterial = new THREE.MeshStandardMaterial({
color: 0x1eaa9c,
roughness: 0.5,
metalness: 0.12,
flatShading: true
});
const plugDarkMaterial = new THREE.MeshStandardMaterial({
color: 0x153d42,
roughness: 0.64,
metalness: 0.12,
flatShading: true
});
const notchMaterial = new THREE.MeshStandardMaterial({
color: 0xff9f43,
emissive: 0x4a1d00,
emissiveIntensity: 0.55,
roughness: 0.5,
metalness: 0.08
});
const metalMaterial = new THREE.MeshStandardMaterial({
color: 0x9aabba,
roughness: 0.28,
metalness: 0.72
});
const cyanMaterial = new THREE.LineBasicMaterial({
color: 0x55e4d2,
transparent: true,
opacity: 0.9
});
// -------------------------------------------------------------------------
// Socket: a hollow extruded body with the keyed bore
// -------------------------------------------------------------------------
const socketGroup = new THREE.Group();
scene.add(socketGroup);
const socketShape = shapeFrom(profilePoints(1.56));
socketShape.holes.push(pathFrom(profilePoints(1.075).reverse()));
const socketGeometry = new THREE.ExtrudeGeometry(socketShape, {
depth: 2.25,
steps: 1,
bevelEnabled: true,
bevelSegments: 2,
bevelSize: 0.065,
bevelThickness: 0.055,
curveSegments: 8
});
socketGeometry.computeVertexNormals();
const socketBody = new THREE.Mesh(socketGeometry, socketMaterial);
socketBody.rotation.y = -Math.PI / 2;
socketBody.castShadow = true;
socketBody.receiveShadow = true;
socketGroup.add(socketBody);
// Closed rear wall.
const socketBack = extrudedMesh(
profilePoints(1.56),
0.14,
socketBackMaterial,
0.04
);
socketBack.rotation.y = -Math.PI / 2;
socketBack.position.x = -2.25;
socketGroup.add(socketBack);
// Amber datum edge around the keyed opening.
const socketOpeningOutline = lineLoop(
profilePoints(1.075),
0xffad55,
0.96
);
socketOpeningOutline.position.x = 0.035;
socketGroup.add(socketOpeningOutline);
// Amber faces inside the socket's offset notch.
const socketNotchFloor = new THREE.Mesh(
new THREE.BoxGeometry(2.05, 0.035, 1.35),
notchMaterial
);
socketNotchFloor.position.set(-1.05, 0.855, -0.322);
socketGroup.add(socketNotchFloor);
for (const z of [-1.02, 0.376]) {
const side = new THREE.Mesh(
new THREE.BoxGeometry(2.05, 0.75, 0.035),
notchMaterial
);
side.position.set(-1.05, 1.225, z);
socketGroup.add(side);
}
// Four understated fastener heads on the socket face.
const screwGeometry = new THREE.CylinderGeometry(0.13, 0.13, 0.075, 20);
for (const y of [-1.55, 1.55]) {
for (const z of [-1.72, 1.72]) {
const screw = new THREE.Mesh(screwGeometry, metalMaterial);
screw.rotation.z = -Math.PI / 2;
screw.position.set(0.02, y, z);
screw.castShadow = true;
socketGroup.add(screw);
}
}
// -------------------------------------------------------------------------
// Plug
// -------------------------------------------------------------------------
const plugAssembly = new THREE.Group();
scene.add(plugAssembly);
const plugRotator = new THREE.Group();
plugAssembly.add(plugRotator);
const plugNose = extrudedMesh(
profilePoints(1),
1.65,
plugMaterial,
0.035
);
plugNose.rotation.y = -Math.PI / 2;
plugRotator.add(plugNose);
const plugBody = extrudedMesh(
profilePoints(1.32),
2.1,
plugDarkMaterial,
0.075
);
plugBody.rotation.y = Math.PI / 2;
plugRotator.add(plugBody);
const plugCollar = extrudedMesh(
profilePoints(1.13),
0.2,
plugMaterial,
0.04
);
plugCollar.rotation.y = Math.PI / 2;
plugCollar.position.x = -0.035;
plugRotator.add(plugCollar);
// Amber faces make the plug's offset notch explicit.
const plugNotchFloor = new THREE.Mesh(
new THREE.BoxGeometry(1.46, 0.03, 1.25),
notchMaterial
);
plugNotchFloor.position.set(-0.8, 0.797, -0.3);
plugRotator.add(plugNotchFloor);
for (const z of [-0.95, 0.35]) {
const side = new THREE.Mesh(
new THREE.BoxGeometry(1.46, 0.69, 0.03),
notchMaterial
);
side.position.set(-0.8, 1.135, z);
plugRotator.add(side);
}
// Cyan outline around the actual plug leading profile.
const plugFaceOutline = lineLoop(profilePoints(1), 0x72f4e5, 0.98);
plugFaceOutline.position.x = -1.67;
plugRotator.add(plugFaceOutline);
// The clearance envelope is the exact scaled bore profile.
const clearanceGuide = new THREE.Group();
clearanceGuide.position.x = -1.675;
const clearanceFill = new THREE.Mesh(
new THREE.ShapeGeometry(shapeFrom(profilePoints(1.075))),
new THREE.MeshBasicMaterial({
color: 0x55e4d2,
transparent: true,
opacity: 0.065,
side: THREE.DoubleSide,
depthWrite: false
})
);
clearanceFill.rotation.y = -Math.PI / 2;
clearanceGuide.add(clearanceFill);
const clearanceEdge = lineLoop(profilePoints(1.075), 0x55e4d2, 0.78);
clearanceGuide.add(clearanceEdge);
plugRotator.add(clearanceGuide);
// Strain relief and a soft cable.
const relief = new THREE.Mesh(
new THREE.CylinderGeometry(0.32, 0.25, 0.52, 20),
plugDarkMaterial
);
relief.rotation.z = -Math.PI / 2;
relief.position.x = 2.23;
relief.castShadow = true;
plugAssembly.add(relief);
for (let i = 0; i < 4; i++) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.29 + i * 0.008, 0.025, 8, 24),
plugDarkMaterial
);
ring.rotation.y = Math.PI / 2;
ring.position.x = 2.06 + i * 0.11;
plugAssembly.add(ring);
}
const cableCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(2.42, 0, 0),
new THREE.Vector3(3.15, 0.02, 0.08),
new THREE.Vector3(4.05, -0.12, 0.42),
new THREE.Vector3(5.1, -0.72, 1.05),
new THREE.Vector3(6.15, -1.76, 1.65),
new THREE.Vector3(7.2, -2.18, 2.1)
]);
const cable = new THREE.Mesh(
new THREE.TubeGeometry(cableCurve, 72, 0.18, 12, false),
new THREE.MeshStandardMaterial({
color: 0x101a27,
roughness: 0.78,
metalness: 0.05
})
);
cable.castShadow = true;
plugAssembly.add(cable);
// -------------------------------------------------------------------------
// Interference preview: the plug profile at its current angle, placed over
// the socket mouth. This makes the blocked geometry directly legible.
// -------------------------------------------------------------------------
const interferenceGroup = new THREE.Group();
interferenceGroup.position.x = 0.055;
scene.add(interferenceGroup);
const interferenceFill = new THREE.Mesh(
new THREE.ShapeGeometry(shapeFrom(profilePoints(1))),
new THREE.MeshBasicMaterial({
color: 0xff5268,
transparent: true,
opacity: 0.2,
side: THREE.DoubleSide,
depthWrite: false
})
);
interferenceFill.rotation.y = -Math.PI / 2;
interferenceGroup.add(interferenceFill);
const interferenceEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(
new THREE.ShapeGeometry(shapeFrom(profilePoints(1))),
15
),
new THREE.LineBasicMaterial({
color: 0xff7586,
transparent: true,
opacity: 0.95
})
);
interferenceEdges.rotation.y = -Math.PI / 2;
interferenceGroup.add(interferenceEdges);
// 3D anchor points for the HTML callouts.
const socketTagAnchor = new THREE.Object3D();
socketTagAnchor.position.set(0.04, 1.42, -0.32);
socketGroup.add(socketTagAnchor);
const plugTagAnchor = new THREE.Object3D();
plugTagAnchor.position.set(-1.67, 1.27, -0.3);
plugRotator.add(plugTagAnchor);
// -------------------------------------------------------------------------
// Direct camera controls — no examples/addons
// -------------------------------------------------------------------------
const controls = {
target: new THREE.Vector3(1.2, -0.05, 0.05),
theta: 0.62,
phi: 1.04,
radius: 13.6,
dragging: false,
lastX: 0,
lastY: 0
};
function updateCamera() {
controls.phi = THREE.MathUtils.clamp(controls.phi, 0.32, 1.48);
controls.radius = THREE.MathUtils.clamp(controls.radius, 7.3, 22);
const sinPhi = Math.sin(controls.phi);
camera.position.set(
controls.target.x + controls.radius * sinPhi * Math.sin(controls.theta),
controls.target.y + controls.radius * Math.cos(controls.phi),
controls.target.z + controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
}
function resetCamera() {
controls.target.set(1.2, -0.05, 0.05);
controls.theta = 0.62;
controls.phi = 1.04;
controls.radius = 13.6;
updateCamera();
}
renderer.domElement.addEventListener("pointerdown", event => {
controls.dragging = true;
controls.lastX = event.clientX;
controls.lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!controls.dragging) return;
const dx = event.clientX - controls.lastX;
const dy = event.clientY - controls.lastY;
controls.lastX = event.clientX;
controls.lastY = event.clientY;
controls.theta -= dx * 0.006;
controls.phi -= dy * 0.005;
updateCamera();
});
function endDrag(event) {
controls.dragging = false;
if (event.pointerId !== undefined && 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();
controls.radius *= Math.exp(event.deltaY * 0.001);
updateCamera();
}, { passive: false });
renderer.domElement.addEventListener("dblclick", resetCamera);
// -------------------------------------------------------------------------
// Interaction
// -------------------------------------------------------------------------
const status = document.getElementById("status");
const statusTitle = document.getElementById("statusTitle");
const statusDetail = document.getElementById("statusDetail");
const assembleButton = document.getElementById("assembleButton");
const explodeButton = document.getElementById("explodeButton");
const clearanceButton = document.getElementById("clearanceButton");
const angleButtons = [...document.querySelectorAll(".angle-button")];
let targetAngle = 0;
let currentAngle = 0;
let targetGap = 1.12;
let currentGap = 1.12;
let clearanceVisible = true;
let blockedUntil = 0;
function normalizedAngle(angle) {
return ((angle % 360) + 360) % 360;
}
function isAligned() {
return normalizedAngle(targetAngle) === 0;
}
function setAngle(angle) {
targetAngle = normalizedAngle(angle);
angleButtons.forEach(button => {
button.classList.toggle("active", Number(button.dataset.angle) === targetAngle);
});
updateStatus();
}
function updateStatus() {
const angle = normalizedAngle(targetAngle);
status.classList.remove("valid", "blocked");
if (angle === 0) {
status.classList.add("valid");
statusTitle.textContent = currentGap < 0.2 ? "Assembly complete" : "Datum aligned";
statusDetail.textContent = currentGap < 0.2
? "Notch and notch are registered."
: "Offset notches share the same axis.";
} else {
status.classList.add("blocked");
statusTitle.textContent = `Interference at ${angle}°`;
statusDetail.textContent = {
90: "The notch is 90° from the socket datum.",
180: "The keyed face is reversed.",
270: "The notch is 270° from the socket datum."
}[angle];
}
}
angleButtons.forEach(button => {
button.addEventListener("click", () => setAngle(Number(button.dataset.angle)));
});
assembleButton.addEventListener("click", () => {
if (isAligned()) {
targetGap = 0.08;
} else {
blockedUntil = performance.now() + 1100;
targetGap = 0.46;
setTimeout(() => {
if (performance.now() < blockedUntil) targetGap = 1.12;
}, 480);
}
updateStatus();
});
explodeButton.addEventListener("click", () => {
blockedUntil = 0;
targetGap = 1.12;
updateStatus();
});
clearanceButton.addEventListener("click", () => {
clearanceVisible = !clearanceVisible;
clearanceGuide.visible = clearanceVisible;
clearanceButton.classList.toggle("active", clearanceVisible);
clearanceButton.textContent = clearanceVisible ? "Clearance on" : "Clearance off";
});
document.getElementById("cameraButton").addEventListener("click", resetCamera);
window.addEventListener("keydown", event => {
if (event.code === "ArrowLeft") {
setAngle(normalizedAngle(targetAngle - 15));
} else if (event.code === "ArrowRight") {
setAngle(normalizedAngle(targetAngle + 15));
} else if (event.code === "Space") {
event.preventDefault();
if (isAligned()) {
targetGap = targetGap < 0.2 ? 1.12 : 0.08;
}
} else if (event.key.toLowerCase() === "r") {
resetCamera();
}
});
// -------------------------------------------------------------------------
// Projected HTML labels
// -------------------------------------------------------------------------
const socketTag = document.getElementById("socketTag");
const plugTag = document.getElementById("plugTag");
function placeTag(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
if (!visible) {
element.classList.remove("visible");
return;
}
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.classList.add("visible");
}
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
const clock = new THREE.Clock();
let lastStatusAngle = 0;
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const angleDifference =
((targetAngle - currentAngle + 540) % 360) - 180;
currentAngle += angleDifference * (1 - Math.exp(-dt * 8));
currentGap += (targetGap - currentGap) * (1 - Math.exp(-dt * 6));
plugRotator.rotation.x = THREE.MathUtils.degToRad(currentAngle);
plugAssembly.position.x = 1.67 + currentGap;
interferenceGroup.rotation.x = plugRotator.rotation.x;
interferenceGroup.visible = !isAligned();
const pulse = 0.15 + Math.sin(performance.now() * 0.006) * 0.045;
interferenceFill.material.opacity = pulse;
interferenceEdges.material.opacity = 0.78 + pulse * 0.5;
if (Math.abs(normalizedAngle(targetAngle) - lastStatusAngle) > 0.01) {
lastStatusAngle = normalizedAngle(targetAngle);
updateStatus();
}
updateCamera();
scene.updateMatrixWorld();
camera.updateMatrixWorld();
placeTag(socketTag, socketTagAnchor.getWorldPosition(new THREE.Vector3()));
placeTag(plugTag, plugTagAnchor.getWorldPosition(new THREE.Vector3()));
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
updateStatus();
resetCamera();
animate();
</script>
</body>
</html>
```