Prompt specification
Build or customize the Three.js 3D scene "Casement window latch" 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: Casement window latch
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/casement-window-latch-x0077
Scene Description Prompt:
Model a window latch with lever, spindle, keeper, and mounting screws; display its engaged position so the lever's locking tongue visibly enters the keeper. Build this with Three.js from cdnjs as a stylized scene in one self-contained HTML file.
Working Three.js Starter (casement-window-latch-x0077):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Engaged Window Latch</title>
<style>
:root {
color-scheme: light;
--ink: #152a34;
--muted: #60727a;
--panel: rgba(250, 248, 242, 0.88);
--border: rgba(28, 50, 60, 0.13);
--green: #19775c;
--orange: #dc694b;
--brass: #d6aa54;
--teal: #4d8995;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8e3d9;
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--ink);
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas.dragging {
cursor: grabbing;
}
.ui {
position: fixed;
inset: 0;
pointer-events: none;
}
.title-card {
position: absolute;
top: 24px;
left: 24px;
width: min(380px, calc(100vw - 48px));
padding: 18px 20px 17px;
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 14px 45px rgba(31, 45, 50, 0.12);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.eyebrow {
margin-bottom: 7px;
color: var(--teal);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(23px, 3vw, 31px);
font-weight: 720;
letter-spacing: -0.045em;
line-height: 1.05;
}
.subtitle {
margin: 9px 0 0;
max-width: 30ch;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.state-row {
display: flex;
align-items: center;
gap: 9px;
margin-top: 15px;
}
.state-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 5px rgba(25, 119, 92, 0.11);
transition: background 180ms, box-shadow 180ms;
}
.state-dot.unlocked {
background: #bd684d;
box-shadow: 0 0 0 5px rgba(189, 104, 77, 0.11);
}
#state-label {
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#state-detail {
color: var(--muted);
font-size: 11px;
}
.controls {
position: absolute;
right: 24px;
top: 24px;
display: flex;
flex-direction: column;
align-items: flex-end;
gap: 10px;
}
button {
pointer-events: auto;
border: 0;
border-radius: 999px;
padding: 12px 18px;
background: var(--ink);
color: white;
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
box-shadow: 0 9px 25px rgba(21, 42, 52, 0.19);
transition: transform 150ms, background 150ms, box-shadow 150ms;
}
button:hover {
transform: translateY(-1px);
background: #203b47;
box-shadow: 0 12px 30px rgba(21, 42, 52, 0.23);
}
button:active {
transform: translateY(1px);
}
.hint {
padding: 8px 12px;
border-radius: 999px;
background: rgba(250, 248, 242, 0.72);
color: var(--muted);
font-size: 10px;
letter-spacing: 0.035em;
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
}
.legend {
position: absolute;
left: 24px;
bottom: 24px;
display: flex;
flex-wrap: wrap;
gap: 7px;
max-width: calc(100vw - 48px);
padding: 10px;
border: 1px solid var(--border);
border-radius: 15px;
background: var(--panel);
box-shadow: 0 10px 35px rgba(31, 45, 50, 0.1);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
.chip {
display: flex;
align-items: center;
gap: 7px;
padding: 6px 9px;
color: #43575f;
font-size: 10px;
font-weight: 650;
white-space: nowrap;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 3px;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.09);
}
.swatch.lever { background: var(--orange); }
.swatch.spindle { background: #536872; }
.swatch.tongue { background: var(--brass); }
.swatch.keeper { background: #263a44; }
.swatch.screw { background: #aeb8b8; }
.callout {
position: absolute;
width: 8px;
height: 8px;
border: 2px solid rgba(255, 255, 255, 0.95);
border-radius: 50%;
background: var(--teal);
box-shadow: 0 2px 10px rgba(22, 48, 57, 0.3);
transform: translate(-50%, -50%);
opacity: 0;
transition: opacity 160ms;
}
.callout.visible {
opacity: 1;
}
.callout .tag {
position: absolute;
top: 0;
left: 0;
padding: 5px 8px;
border: 1px solid rgba(255, 255, 255, 0.45);
border-radius: 7px;
background: rgba(25, 44, 53, 0.86);
color: white;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.075em;
text-transform: uppercase;
white-space: nowrap;
transform: translate(var(--tx), var(--ty));
backdrop-filter: blur(8px);
-webkit-backdrop-filter: blur(8px);
}
#loading {
position: fixed;
inset: 0;
display: grid;
place-items: center;
z-index: 10;
background: #e8e3d9;
color: #526a72;
font-size: 12px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
transition: opacity 300ms;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 700px) {
.title-card {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
padding: 15px 16px;
}
.subtitle {
display: none;
}
.state-row {
margin-top: 12px;
}
.controls {
top: auto;
right: 14px;
bottom: 83px;
}
.hint,
.callout {
display: none;
}
.legend {
left: 14px;
bottom: 14px;
width: calc(100vw - 28px);
gap: 2px;
padding: 7px;
}
.chip {
padding: 5px 6px;
font-size: 9px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="ui">
<section class="title-card">
<div class="eyebrow">Casement hardware · assembly 04</div>
<h1>Window latch</h1>
<p class="subtitle">
A cutaway-style view of the operating lever, spindle, sliding locking
tongue, strike keeper, and mounting hardware.
</p>
<div class="state-row">
<span id="state-dot" class="state-dot"></span>
<span id="state-label">Engaged</span>
<span id="state-detail">tongue seated in keeper</span>
</div>
</section>
<div class="controls">
<button id="toggle" type="button" aria-pressed="true">Release latch</button>
<div class="hint">Drag to orbit · Scroll to zoom · Space to toggle</div>
</div>
<div class="legend" aria-label="Component legend">
<div class="chip"><span class="swatch lever"></span>Lever</div>
<div class="chip"><span class="swatch spindle"></span>Spindle</div>
<div class="chip"><span class="swatch tongue"></span>Locking tongue</div>
<div class="chip"><span class="swatch keeper"></span>Keeper</div>
<div class="chip"><span class="swatch screw"></span>Screws</div>
</div>
</div>
<div id="loading">Building latch assembly</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const host = document.getElementById("scene");
const loading = document.getElementById("loading");
// ---------------------------------------------------------------------
// Renderer, scene, and camera
// ---------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
host.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8e3d9);
scene.fog = new THREE.Fog(0xe8e3d9, 12, 23);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
50
);
const orbit = {
theta: -0.43,
phi: 1.18,
radius: 8.9,
target: new THREE.Vector3(-0.08, 0.18, 0.1)
};
function updateCamera() {
const portraitFactor = Math.max(
1,
0.92 / camera.aspect
);
const radius = orbit.radius * portraitFactor;
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + radius * Math.cos(orbit.phi),
orbit.target.z + radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
// ---------------------------------------------------------------------
// Lighting and environment
// ---------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xfff8eb, 0x71808a, 2.05));
const keyLight = new THREE.DirectionalLight(0xfff1d7, 3.35);
keyLight.position.set(-4.5, 8.5, 6.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 22;
keyLight.shadow.bias = -0.00035;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xb9dce7, 1.65);
fillLight.position.set(6, 4, 5);
scene.add(fillLight);
const rimLight = new THREE.DirectionalLight(0xffb57d, 1.15);
rimLight.position.set(-5, 2, -3);
scene.add(rimLight);
const wall = new THREE.Mesh(
new THREE.PlaneGeometry(24, 15),
new THREE.MeshStandardMaterial({
color: 0xe9e4db,
roughness: 0.93,
metalness: 0
})
);
wall.position.set(0, 1.5, -2.35);
wall.receiveShadow = true;
scene.add(wall);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0xd7d1c6,
roughness: 0.88,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -2.59;
floor.receiveShadow = true;
scene.add(floor);
const plinth = new THREE.Mesh(
new THREE.CylinderGeometry(4.35, 4.5, 0.16, 72),
new THREE.MeshStandardMaterial({
color: 0xcfc8bd,
roughness: 0.7,
metalness: 0.03
})
);
plinth.position.set(-0.05, -2.5, -0.3);
plinth.receiveShadow = true;
plinth.castShadow = true;
scene.add(plinth);
// ---------------------------------------------------------------------
// Materials
// ---------------------------------------------------------------------
const materials = {
sash: new THREE.MeshStandardMaterial({
color: 0x285966,
roughness: 0.47,
metalness: 0.08
}),
frame: new THREE.MeshStandardMaterial({
color: 0x173846,
roughness: 0.48,
metalness: 0.08
}),
trim: new THREE.MeshStandardMaterial({
color: 0x6f9da4,
roughness: 0.42,
metalness: 0.12
}),
gunmetal: new THREE.MeshStandardMaterial({
color: 0x25343c,
roughness: 0.29,
metalness: 0.82
}),
gunmetalLight: new THREE.MeshStandardMaterial({
color: 0x53656d,
roughness: 0.32,
metalness: 0.78
}),
dark: new THREE.MeshStandardMaterial({
color: 0x111c22,
roughness: 0.42,
metalness: 0.58
}),
brass: new THREE.MeshStandardMaterial({
color: 0xd9ad58,
roughness: 0.24,
metalness: 0.88
}),
brassDark: new THREE.MeshStandardMaterial({
color: 0x8c642c,
roughness: 0.3,
metalness: 0.84
}),
orange: new THREE.MeshStandardMaterial({
color: 0xe36e4f,
roughness: 0.31,
metalness: 0.36
}),
orangeDark: new THREE.MeshStandardMaterial({
color: 0x9f3f2e,
roughness: 0.38,
metalness: 0.32
}),
screw: new THREE.MeshStandardMaterial({
color: 0xb8c0bf,
roughness: 0.24,
metalness: 0.9
}),
glass: new THREE.MeshStandardMaterial({
color: 0xaed5dc,
roughness: 0.13,
metalness: 0.04,
transparent: true,
opacity: 0.19,
depthWrite: false,
side: THREE.DoubleSide
}),
indicator: new THREE.MeshStandardMaterial({
color: 0x2ac18a,
emissive: 0x167451,
emissiveIntensity: 1.35,
roughness: 0.25,
metalness: 0.15
})
};
// ---------------------------------------------------------------------
// Geometry helpers, implemented without addons
// ---------------------------------------------------------------------
function roundedPlateGeometry(width, height, depth, radius) {
const shape = new THREE.Shape();
const x = -width / 2;
const y = -height / 2;
const r = Math.min(radius, width / 2, height / 2);
shape.moveTo(x + r, y);
shape.lineTo(x + width - r, y);
shape.quadraticCurveTo(x + width, y, x + width, y + r);
shape.lineTo(x + width, y + height - r);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
shape.lineTo(x + r, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - r);
shape.lineTo(x, y + r);
shape.quadraticCurveTo(x, y, x + r, y);
const bevel = Math.min(0.035, depth * 0.22, r * 0.24);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
bevelEnabled: true,
bevelSegments: 3,
steps: 1,
bevelSize: bevel,
bevelThickness: bevel,
curveSegments: 12
});
geometry.translate(0, 0, -depth / 2);
geometry.computeVertexNormals();
return geometry;
}
function roundedPlate(width, height, depth, radius, material) {
const mesh = new THREE.Mesh(
roundedPlateGeometry(width, height, depth, radius),
material
);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function box(width, height, depth, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function cylinderZ(radius, length, material, segments = 32) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radius,
radius,
length,
segments
),
material
);
mesh.rotation.x = Math.PI / 2;
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function torusZ(radius, tube, material) {
const mesh = new THREE.Mesh(
new THREE.TorusGeometry(radius, tube, 10, 36),
material
);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function addScrew(x, y, z, radius = 0.07) {
const group = new THREE.Group();
const washer = torusZ(radius * 1.22, 0.014, materials.dark);
washer.position.z = z;
group.add(washer);
const head = cylinderZ(radius, 0.046, materials.screw, 28);
head.position.z = z + 0.027;
group.add(head);
const slot = box(radius * 1.35, 0.017, 0.011, materials.dark);
slot.position.z = z + 0.054;
slot.rotation.z = -0.18;
group.add(slot);
scene.add(group);
return group;
}
// ---------------------------------------------------------------------
// Stylized window frame and glass
// ---------------------------------------------------------------------
const windowGroup = new THREE.Group();
scene.add(windowGroup);
const glass = new THREE.Mesh(
new THREE.PlaneGeometry(3.45, 4.62),
materials.glass
);
glass.position.set(-0.03, 0.28, -0.63);
windowGroup.add(glass);
const sashPost = roundedPlate(
0.9, 5.14, 0.58, 0.07, materials.sash
);
sashPost.position.set(-2.28, 0.2, -0.34);
windowGroup.add(sashPost);
const fixedPost = roundedPlate(
1.08, 5.14, 0.62, 0.07, materials.frame
);
fixedPost.position.set(2.16, 0.2, -0.34);
windowGroup.add(fixedPost);
const bottomRail = roundedPlate(
4.58, 0.61, 0.66, 0.07, materials.frame
);
bottomRail.position.set(-0.02, -2.12, -0.32);
windowGroup.add(bottomRail);
const topRail = roundedPlate(
4.58, 0.46, 0.62, 0.07, materials.frame
);
topRail.position.set(-0.02, 2.76, -0.35);
windowGroup.add(topRail);
const sashBead = box(0.075, 4.72, 0.08, materials.trim);
sashBead.position.set(-1.84, 0.2, 0.015);
windowGroup.add(sashBead);
const frameBead = box(0.075, 4.72, 0.08, materials.trim);
frameBead.position.set(1.61, 0.2, 0.015);
windowGroup.add(frameBead);
// ---------------------------------------------------------------------
// Latch base plate
// ---------------------------------------------------------------------
const latch = new THREE.Group();
scene.add(latch);
const basePlate = roundedPlate(
1.72, 2.04, 0.13, 0.11, materials.gunmetal
);
basePlate.position.set(-0.94, 1.15, 0.12);
latch.add(basePlate);
const baseInset = roundedPlate(
1.43, 1.73, 0.045, 0.08, materials.gunmetalLight
);
baseInset.position.set(-0.94, 1.15, 0.207);
latch.add(baseInset);
// Subtle recessed center details.
const upperRecess = roundedPlate(
0.92, 0.35, 0.025, 0.06, materials.dark
);
upperRecess.position.set(-0.82, 1.88, 0.237);
latch.add(upperRecess);
const lowerRecess = roundedPlate(
0.58, 0.22, 0.025, 0.05, materials.dark
);
lowerRecess.position.set(-0.93, 0.38, 0.237);
latch.add(lowerRecess);
// Four mounting screws.
addScrew(-1.58, 1.94, 0.228, 0.075);
addScrew(-0.28, 1.94, 0.228, 0.075);
addScrew(-1.58, 0.43, 0.228, 0.075);
addScrew(-0.28, 0.43, 0.228, 0.075);
// ---------------------------------------------------------------------
// Spindle and rotating lever
// ---------------------------------------------------------------------
const spindleX = -0.42;
const spindleY = 1.45;
const spindleHousing = cylinderZ(
0.275, 0.39, materials.gunmetal, 40
);
spindleHousing.position.set(spindleX, spindleY, 0.365);
latch.add(spindleHousing);
const spindleBackRing = torusZ(
0.263, 0.025, materials.gunmetalLight
);
spindleBackRing.position.set(spindleX, spindleY, 0.19);
latch.add(spindleBackRing);
const spindleFrontRing = torusZ(
0.263, 0.027, materials.brassDark
);
spindleFrontRing.position.set(spindleX, spindleY, 0.545);
latch.add(spindleFrontRing);
const spindleStem = cylinderZ(
0.112, 0.3, materials.brass, 32
);
spindleStem.position.set(spindleX, spindleY, 0.61);
latch.add(spindleStem);
const driveSquare = box(
0.16, 0.16, 0.22, materials.dark
);
driveSquare.position.set(spindleX, spindleY, 0.535);
latch.add(driveSquare);
const leverPivot = new THREE.Group();
leverPivot.position.set(spindleX, spindleY, 0.69);
latch.add(leverPivot);
const leverShape = new THREE.Shape();
leverShape.moveTo(-0.12, -0.25);
leverShape.lineTo(-0.48, -0.26);
leverShape.lineTo(-1.62, -0.39);
leverShape.quadraticCurveTo(-1.88, -0.38, -1.93, -0.15);
leverShape.quadraticCurveTo(-1.96, 0.1, -1.75, 0.2);
leverShape.lineTo(-0.58, 0.25);
leverShape.quadraticCurveTo(-0.18, 0.28, 0.1, 0.08);
leverShape.lineTo(0.1, -0.08);
leverShape.quadraticCurveTo(0.02, -0.22, -0.12, -0.25);
const leverGeometry = new THREE.ExtrudeGeometry(leverShape, {
depth: 0.16,
bevelEnabled: true,
bevelSegments: 4,
steps: 1,
bevelSize: 0.04,
bevelThickness: 0.035,
curveSegments: 16
});
leverGeometry.translate(0, 0, -0.08);
leverGeometry.computeVertexNormals();
const lever = new THREE.Mesh(leverGeometry, materials.orange);
lever.castShadow = true;
lever.receiveShadow = true;
leverPivot.add(lever);
const leverHub = cylinderZ(
0.31, 0.2, materials.orangeDark, 40
);
leverHub.position.z = 0.005;
leverPivot.add(leverHub);
const gripInset = roundedPlate(
0.92, 0.17, 0.035, 0.055, materials.orangeDark
);
gripInset.position.set(-1.25, 0.005, 0.105);
leverPivot.add(gripInset);
const gripEnd = roundedPlate(
0.27, 0.28, 0.055, 0.065, materials.dark
);
gripEnd.position.set(-1.72, 0.005, 0.105);
leverPivot.add(gripEnd);
const pivotCap = cylinderZ(
0.145, 0.055, materials.brass, 32
);
pivotCap.position.set(spindleX, spindleY, 0.855);
latch.add(pivotCap);
// ---------------------------------------------------------------------
// Sliding locking tongue
// ---------------------------------------------------------------------
const tongueGuide = roundedPlate(
0.44, 0.46, 0.27, 0.065, materials.gunmetal
);
tongueGuide.position.set(-0.04, 0.62, 0.29);
latch.add(tongueGuide);
const guideMouth = roundedPlate(
0.07, 0.29, 0.29, 0.02, materials.dark
);
guideMouth.position.set(0.19, 0.62, 0.32);
latch.add(guideMouth);
const tongueGroup = new THREE.Group();
tongueGroup.position.set(0.52, 0.62, 0.315);
latch.add(tongueGroup);
const lockingTongue = roundedPlate(
1.74, 0.23, 0.18, 0.035, materials.brass
);
tongueGroup.add(lockingTongue);
const tongueTop = box(
1.42, 0.025, 0.025, materials.brassDark
);
tongueTop.position.set(-0.06, 0.085, 0.103);
tongueGroup.add(tongueTop);
const tongueGrooveA = box(
0.027, 0.155, 0.012, materials.dark
);
tongueGrooveA.position.set(0.56, 0, 0.103);
tongueGroup.add(tongueGrooveA);
const tongueGrooveB = box(
0.027, 0.155, 0.012, materials.dark
);
tongueGrooveB.position.set(0.64, 0, 0.103);
tongueGroup.add(tongueGrooveB);
// Small visual link between spindle area and the sliding bolt.
const driveLink = roundedPlate(
0.14, 0.58, 0.085, 0.04, materials.gunmetalLight
);
driveLink.position.set(-0.32, 1.01, 0.29);
driveLink.rotation.z = -0.13;
latch.add(driveLink);
const linkPivot = cylinderZ(
0.09, 0.11, materials.brassDark, 24
);
linkPivot.position.set(-0.34, 0.75, 0.35);
latch.add(linkPivot);
// ---------------------------------------------------------------------
// Keeper: an open-front U-channel with the tongue visibly entering it
// ---------------------------------------------------------------------
const keeper = new THREE.Group();
scene.add(keeper);
const keeperPlate = roundedPlate(
0.9, 1.08, 0.13, 0.08, materials.gunmetal
);
keeperPlate.position.set(1.44, 0.62, 0.12);
keeper.add(keeperPlate);
const keeperInset = roundedPlate(
0.67, 0.82, 0.035, 0.05, materials.gunmetalLight
);
keeperInset.position.set(1.46, 0.62, 0.205);
keeper.add(keeperInset);
// Dark back wall creates a visible recess behind the inserted tongue.
const keeperBack = box(
0.09, 0.58, 0.54, materials.dark
);
keeperBack.position.set(1.83, 0.62, 0.34);
keeper.add(keeperBack);
const keeperTop = roundedPlate(
0.93, 0.15, 0.57, 0.045, materials.gunmetal
);
keeperTop.position.set(1.42, 0.99, 0.35);
keeper.add(keeperTop);
const keeperBottom = roundedPlate(
0.93, 0.15, 0.57, 0.045, materials.gunmetal
);
keeperBottom.position.set(1.42, 0.25, 0.35);
keeper.add(keeperBottom);
// Brass mouth liners frame the entry without hiding the bolt.
const mouthTop = box(
0.57, 0.055, 0.48, materials.brass
);
mouthTop.position.set(1.24, 0.855, 0.39);
keeper.add(mouthTop);
const mouthBottom = box(
0.57, 0.055, 0.48, materials.brass
);
mouthBottom.position.set(1.24, 0.385, 0.39);
keeper.add(mouthBottom);
// Keeper mounting screws.
addScrew(1.17, 1.055, 0.23, 0.062);
addScrew(1.72, 1.055, 0.23, 0.062);
addScrew(1.17, 0.185, 0.23, 0.062);
addScrew(1.72, 0.185, 0.23, 0.062);
const statusLamp = new THREE.Mesh(
new THREE.SphereGeometry(0.052, 20, 14),
materials.indicator
);
statusLamp.position.set(1.32, 1.06, 0.255);
keeper.add(statusLamp);
// ---------------------------------------------------------------------
// Component callouts
// ---------------------------------------------------------------------
const calloutDefinitions = [
{
text: "Lever",
anchor: () => leverPivot.localToWorld(
new THREE.Vector3(-1.18, 0.04, 0.14)
),
tx: -66,
ty: -27
},
{
text: "Spindle",
anchor: () => new THREE.Vector3(
spindleX,
spindleY + 0.08,
0.89
),
tx: 16,
ty: -35
},
{
text: "Locking tongue",
anchor: () => tongueGroup.localToWorld(
new THREE.Vector3(0.35, 0, 0.13)
),
tx: -4,
ty: 30
},
{
text: "Keeper",
anchor: () => new THREE.Vector3(1.35, 0.94, 0.53),
tx: 34,
ty: -33
}
];
const callouts = calloutDefinitions.map((definition) => {
const element = document.createElement("div");
element.className = "callout";
element.innerHTML = `<span class="tag">${definition.text}</span>`;
element.style.setProperty("--tx", `${definition.tx}px`);
element.style.setProperty("--ty", `${definition.ty}px`);
document.querySelector(".ui").appendChild(element);
return element;
});
function updateCallouts() {
const width = window.innerWidth;
const height = window.innerHeight;
calloutDefinitions.forEach((definition, index) => {
const point = definition.anchor().project(camera);
const visible =
point.z > -1 &&
point.z < 1 &&
point.x > -1.05 &&
point.x < 1.05 &&
point.y > -1.05 &&
point.y < 1.05;
const element = callouts[index];
element.classList.toggle("visible", visible);
element.style.left = `${(point.x * 0.5 + 0.5) * width}px`;
element.style.top = `${(-point.y * 0.5 + 0.5) * height}px`;
});
}
// ---------------------------------------------------------------------
// Engaged / released animation
// ---------------------------------------------------------------------
const toggleButton = document.getElementById("toggle");
const stateDot = document.getElementById("state-dot");
const stateLabel = document.getElementById("state-label");
const stateDetail = document.getElementById("state-detail");
let engaged = true;
let targetState = 1;
let currentState = 1;
function setEngaged(nextEngaged) {
engaged = nextEngaged;
targetState = engaged ? 1 : 0;
toggleButton.textContent = engaged
? "Release latch"
: "Engage latch";
toggleButton.setAttribute("aria-pressed", String(engaged));
stateDot.classList.toggle("unlocked", !engaged);
stateLabel.textContent = engaged ? "Engaged" : "Released";
stateDetail.textContent = engaged
? "tongue seated in keeper"
: "tongue retracted from keeper";
materials.indicator.color.setHex(
engaged ? 0x2ac18a : 0xc56d50
);
materials.indicator.emissive.setHex(
engaged ? 0x167451 : 0x702f20
);
}
toggleButton.addEventListener("click", () => {
setEngaged(!engaged);
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space" && event.target === document.body) {
event.preventDefault();
setEngaged(!engaged);
}
});
// ---------------------------------------------------------------------
// Direct orbit and zoom controls
// ---------------------------------------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
orbit.theta = THREE.MathUtils.clamp(
orbit.theta + dx * 0.006,
-1.12,
0.48
);
orbit.phi = THREE.MathUtils.clamp(
orbit.phi + dy * 0.005,
0.7,
1.52
);
});
function stopDragging(event) {
dragging = false;
renderer.domElement.classList.remove("dragging");
if (
event &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
5.5,
13
);
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
// ---------------------------------------------------------------------
// Resize and render loop
// ---------------------------------------------------------------------
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
let firstFrame = true;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const smoothing = 1 - Math.exp(-8 * delta);
currentState += (targetState - currentState) * smoothing;
// The brass tongue visibly moves between the guide and keeper.
tongueGroup.position.x = THREE.MathUtils.lerp(
-0.52,
0.52,
currentState
);
// The lever turns around the spindle in sync with the tongue.
leverPivot.rotation.z = THREE.MathUtils.lerp(
-1.05,
0,
currentState
);
driveSquare.rotation.z = leverPivot.rotation.z;
materials.indicator.emissiveIntensity =
0.65 + currentState * 0.75;
updateCamera();
scene.updateMatrixWorld(true);
updateCallouts();
renderer.render(scene, camera);
if (firstFrame) {
firstFrame = false;
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 400);
}
}
resize();
updateCamera();
animate();
</script>
</body>
</html>
```