Prompt specification
Build or customize the Three.js 3D scene "Window latch opening sequence" 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: Window latch opening sequence
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/window-latch-opening-sequence-x0078
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows a casement window latch. Animate the lever rotating clear before the casement swings outward; prevent window movement while the locking tongue remains inside its fixed frame-mounted keeper. Voxel style.
Working Three.js Starter (window-latch-opening-sequence-x0078):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Casement Latch</title>
<style>
:root {
color-scheme: dark;
--ink: #eaf4ef;
--muted: #91a8a4;
--panel: rgba(17, 28, 34, 0.9);
--line: rgba(174, 217, 205, 0.2);
--orange: #ff7849;
--teal: #66d5c7;
--red: #ff5c68;
--green: #8be28b;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #172229;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
color: var(--ink);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.panel {
position: fixed;
z-index: 10;
background: var(--panel);
border: 1px solid var(--line);
box-shadow: 0 10px 35px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(10px);
}
#info {
top: 18px;
left: 18px;
width: min(350px, calc(100vw - 36px));
padding: 16px;
}
h1 {
margin: 0 0 5px;
font-size: 18px;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.subtitle {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
#readout {
margin-top: 14px;
padding: 11px;
border: 1px solid var(--line);
background: rgba(0, 0, 0, 0.18);
}
#state-title {
font-size: 12px;
font-weight: 800;
letter-spacing: 0.08em;
color: var(--orange);
}
#state-detail {
margin-top: 5px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
min-height: 32px;
}
#interlock {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 10px;
font-size: 10px;
letter-spacing: 0.08em;
}
#interlock-status {
color: var(--red);
font-weight: 800;
white-space: nowrap;
}
#interlock-status::before {
content: "";
display: inline-block;
width: 7px;
height: 7px;
margin-right: 7px;
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
#controls {
bottom: 18px;
left: 50%;
transform: translateX(-50%);
display: flex;
gap: 9px;
padding: 10px;
}
button {
appearance: none;
border: 1px solid rgba(255, 255, 255, 0.16);
border-radius: 0;
padding: 12px 16px;
background: #263840;
color: var(--ink);
font: 800 11px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.08em;
cursor: pointer;
transition: transform 120ms ease, background 120ms ease, opacity 120ms ease;
}
button:hover:not(:disabled) {
transform: translateY(-2px);
background: #324952;
}
button:focus-visible {
outline: 2px solid var(--teal);
outline-offset: 3px;
}
button:disabled {
opacity: 0.48;
cursor: default;
}
#open-button {
background: var(--orange);
color: #171b1d;
border-color: transparent;
}
#open-button:hover:not(:disabled) {
background: #ff916b;
}
#steps {
bottom: 18px;
right: 18px;
display: grid;
grid-template-columns: repeat(3, 1fr);
width: min(370px, calc(100vw - 36px));
overflow: hidden;
}
.step {
position: relative;
padding: 13px 10px 12px;
border-left: 1px solid var(--line);
color: #6f8885;
text-align: center;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.06em;
}
.step:first-child { border-left: 0; }
.step b {
display: block;
margin-bottom: 5px;
font-size: 17px;
}
.step.active {
color: var(--teal);
background: rgba(102, 213, 199, 0.08);
}
.step.done {
color: var(--green);
}
#legend {
position: fixed;
z-index: 9;
right: 20px;
top: 20px;
display: flex;
gap: 14px;
padding: 9px 11px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.04em;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 9px;
height: 9px;
background: currentColor;
}
.lever-color { color: var(--orange); }
.keeper-color { color: #ffd166; }
.sash-color { color: #d9eee5; }
#camera-hint {
position: fixed;
z-index: 8;
top: 62px;
right: 20px;
color: #718985;
font-size: 9px;
letter-spacing: 0.04em;
}
@media (max-width: 760px) {
#info {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 12px;
}
#legend, #camera-hint {
display: none;
}
#steps {
right: 10px;
bottom: 76px;
width: calc(100vw - 20px);
}
#controls {
bottom: 10px;
width: calc(100vw - 20px);
}
button {
flex: 1;
padding: 12px 8px;
}
}
</style>
</head>
<body>
<section id="info" class="panel">
<h1>Voxel Casement Latch</h1>
<p class="subtitle">
A mechanically sequenced casement window. The lever must clear, the tongue
must leave the frame-mounted keeper, and only then can the sash swing.
</p>
<div id="readout">
<div id="state-title">LOCKED</div>
<div id="state-detail">
The locking tongue is seated in the fixed keeper. Window movement is interlocked.
</div>
<div id="interlock">
<span>WINDOW INTERLOCK</span>
<span id="interlock-status">ENGAGED</span>
</div>
</div>
</section>
<div id="legend" class="panel">
<span class="legend-item"><i class="swatch lever-color"></i>LEVER</span>
<span class="legend-item"><i class="swatch keeper-color"></i>FIXED KEEPER</span>
<span class="legend-item"><i class="swatch sash-color"></i>SASH</span>
</div>
<div id="camera-hint">DRAG TO ORBIT · WHEEL TO ZOOM · SPACE TO RELEASE</div>
<div id="steps" class="panel">
<div class="step" data-step="lever"><b>01</b>ROTATE LEVER</div>
<div class="step" data-step="tongue"><b>02</b>RETRACT TONGUE</div>
<div class="step" data-step="sash"><b>03</b>SWING SASH</div>
</div>
<div id="controls" class="panel">
<button id="open-button">RELEASE + OPEN</button>
<button id="reset-button">RESET</button>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, scene, camera, and lighting
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x172229);
scene.fog = new THREE.Fog(0x172229, 10, 20);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
50
);
const hemi = new THREE.HemisphereLight(0xc9fff4, 0x26313a, 2.1);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xfff1d4, 3.5);
keyLight.position.set(4.5, 7.5, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -5;
keyLight.shadow.camera.right = 5;
keyLight.shadow.camera.top = 5;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 20;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x72d9d0, 1.8);
rimLight.position.set(-5, 2.5, -2);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Voxel construction helpers
// -------------------------------------------------------------------------
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.82,
metalness: options.metalness ?? 0.02,
flatShading: true,
transparent: options.transparent ?? false,
opacity: options.opacity ?? 1,
depthWrite: options.depthWrite ?? true,
emissive: options.emissive ?? 0x000000,
emissiveIntensity: options.emissiveIntensity ?? 0
});
}
function box(name, size, position, mat, parent = scene, shadows = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(...size), mat);
mesh.name = name;
mesh.position.set(...position);
mesh.castShadow = shadows;
mesh.receiveShadow = shadows;
parent.add(mesh);
return mesh;
}
const mat = {
wall: material(0x25343a),
wallTileA: material(0x2b3c41),
wallTileB: material(0x30464a),
floor: material(0x1a272d),
floorTileA: material(0x203137),
floorTileB: material(0x24383d),
frame: material(0xdac89f),
frameLight: material(0xf1e2bd),
frameShade: material(0xbda77e),
sash: material(0x8bcfbf),
sashDark: material(0x4f958c),
glass: material(0x80d9d2, {
transparent: true,
opacity: 0.23,
depthWrite: false,
roughness: 0.25
}),
reflection: material(0xc8fff4, {
transparent: true,
opacity: 0.25,
depthWrite: false
}),
dark: material(0x26343a),
darker: material(0x182329),
orange: material(0xff7849),
orangeDark: material(0xb7442c),
brass: material(0xe9b84c, { metalness: 0.15 }),
keeper: material(0xe9b84c, {
emissive: 0x7a3512,
emissiveIntensity: 0.12
}),
indicator: material(0xff5c68, {
emissive: 0xff2635,
emissiveIntensity: 0.6
})
};
// -------------------------------------------------------------------------
// Block-built wall and floor
// -------------------------------------------------------------------------
box("Wall backing", [6.7, 5.4, 0.38], [0, 0, -0.58], mat.wall);
for (let row = -3; row <= 3; row++) {
for (let col = -4; col <= 4; col++) {
const offset = row % 2 === 0 ? 0.21 : 0;
box(
"Wall voxel",
[0.72, 0.73, 0.07],
[col * 0.76 + offset, row * 0.74, -0.34],
(row + col) % 2 === 0 ? mat.wallTileA : mat.wallTileB
);
}
}
box("Floor slab", [8.5, 0.28, 7.5], [0, -2.12, 1.7], mat.floor);
for (let row = 0; row < 6; row++) {
for (let col = 0; col < 9; col++) {
box(
"Floor voxel",
[0.86, 0.055, 0.86],
[(col - 4) * 0.92, -1.955, (row - 1.2) * 0.92],
(row + col) % 2 === 0 ? mat.floorTileA : mat.floorTileB,
scene,
false
);
}
}
// -------------------------------------------------------------------------
// Fixed frame
// -------------------------------------------------------------------------
const fixedFrame = new THREE.Group();
fixedFrame.name = "Fixed frame";
scene.add(fixedFrame);
box("Left fixed post", [0.34, 3.28, 0.42], [-1.93, 0, -0.02], mat.frame, fixedFrame);
box("Right fixed post", [0.34, 3.28, 0.42], [1.93, 0, -0.02], mat.frame, fixedFrame);
box("Fixed top rail", [4.18, 0.34, 0.42], [0, 1.55, -0.02], mat.frameLight, fixedFrame);
box("Fixed bottom rail", [4.18, 0.34, 0.42], [0, -1.55, -0.02], mat.frameShade, fixedFrame);
box("Projecting sill", [4.72, 0.18, 0.86], [0, -1.72, 0.15], mat.frameLight, fixedFrame);
// Pixel corner accents.
for (const x of [-1.93, 1.93]) {
for (const y of [-1.55, 1.55]) {
box("Fixed corner cap", [0.43, 0.43, 0.49], [x, y, 0.01], mat.sashDark, fixedFrame);
}
}
// Fixed hinge blocks.
for (const y of [-0.94, 0.94]) {
box("Fixed hinge block", [0.31, 0.46, 0.34], [-1.73, y, 0.08], mat.dark, fixedFrame);
box("Fixed hinge pin", [0.13, 0.54, 0.42], [-1.66, y, 0.13], mat.brass, fixedFrame);
}
// -------------------------------------------------------------------------
// Frame-mounted keeper. This object never belongs to the sash group.
// -------------------------------------------------------------------------
const keeper = new THREE.Group();
keeper.name = "Fixed frame-mounted keeper";
keeper.position.set(1.93, 0.08, 0);
fixedFrame.add(keeper);
box("Keeper mounting plate", [0.16, 0.64, 0.38], [0.1, 0, 0.12], mat.dark, keeper);
box("Keeper upper jaw", [0.38, 0.11, 0.29], [-0.02, 0.15, 0.23], mat.keeper, keeper);
box("Keeper lower jaw", [0.38, 0.11, 0.29], [-0.02, -0.15, 0.23], mat.keeper, keeper);
box("Keeper end stop", [0.11, 0.34, 0.29], [0.16, 0, 0.23], mat.keeper, keeper);
box("Keeper slot shadow", [0.18, 0.18, 0.08], [-0.01, 0, 0.08], mat.darker, keeper);
const keeperIndicator = box(
"Keeper indicator",
[0.12, 0.12, 0.12],
[0.07, 0.39, 0.31],
mat.indicator,
keeper
);
// -------------------------------------------------------------------------
// Hinged sash
// -------------------------------------------------------------------------
const sash = new THREE.Group();
sash.name = "Casement sash";
sash.position.set(-1.64, 0, 0.08);
scene.add(sash);
const sashWidth = 3.25;
const sashHeight = 2.48;
box("Sash left stile", [0.23, sashHeight, 0.24], [0.12, 0, 0], mat.sash, sash);
box("Sash right stile", [0.23, sashHeight, 0.24], [sashWidth - 0.12, 0, 0], mat.sash, sash);
box("Sash top rail", [sashWidth, 0.23, 0.24], [sashWidth / 2, sashHeight / 2 - 0.12, 0], mat.sash, sash);
box("Sash bottom rail", [sashWidth, 0.23, 0.24], [sashWidth / 2, -sashHeight / 2 + 0.12, 0], mat.sash, sash);
// Four-by-three voxel glass panes.
const paneLeft = 0.29;
const paneRight = sashWidth - 0.29;
const paneBottom = -1.01;
const paneTop = 1.01;
const paneCols = 4;
const paneRows = 3;
const paneW = (paneRight - paneLeft) / paneCols;
const paneH = (paneTop - paneBottom) / paneRows;
for (let row = 0; row < paneRows; row++) {
for (let col = 0; col < paneCols; col++) {
const x = paneLeft + paneW * (col + 0.5);
const y = paneBottom + paneH * (row + 0.5);
box("Glass voxel pane", [paneW - 0.035, paneH - 0.035, 0.055], [x, y, 0], mat.glass, sash, false);
// Sparse square reflections preserve the voxel aesthetic.
if ((row + col) % 3 === 0) {
box(
"Pixel reflection",
[paneW * 0.22, paneH * 0.12, 0.018],
[x - paneW * 0.19, y + paneH * 0.18, 0.042],
mat.reflection,
sash,
false
);
}
}
}
// Thin internal mullions.
for (let col = 1; col < paneCols; col++) {
const x = paneLeft + paneW * col;
box("Vertical mullion", [0.045, 2.04, 0.09], [x, 0, 0.025], mat.sashDark, sash);
}
for (let row = 1; row < paneRows; row++) {
const y = paneBottom + paneH * row;
box("Horizontal mullion", [2.69, 0.045, 0.09], [sashWidth / 2, y, 0.025], mat.sashDark, sash);
}
// Moving hinge leaves.
for (const y of [-0.94, 0.94]) {
box("Moving hinge leaf", [0.34, 0.28, 0.29], [0.12, y, 0.09], mat.sashDark, sash);
}
// -------------------------------------------------------------------------
// Latch assembly, mounted on the moving sash
// -------------------------------------------------------------------------
const latch = new THREE.Group();
latch.name = "Sash-mounted latch";
sash.add(latch);
box("Latch housing", [0.42, 0.70, 0.25], [2.79, 0.08, 0.24], mat.dark, latch);
box("Latch face plate", [0.28, 0.50, 0.08], [2.78, 0.08, 0.40], mat.orangeDark, latch);
box("Latch guide", [0.25, 0.13, 0.17], [3.02, 0.08, 0.22], mat.dark, latch);
const leverPivot = new THREE.Group();
leverPivot.name = "Rotating lever pivot";
leverPivot.position.set(2.70, 0.10, 0.45);
latch.add(leverPivot);
box("Lever handle", [0.18, 0.76, 0.17], [0, -0.36, 0], mat.orange, leverPivot);
box("Lever grip", [0.27, 0.18, 0.24], [0, -0.71, 0.01], mat.orange, leverPivot);
box("Lever pivot cap", [0.25, 0.25, 0.18], [0, 0, 0.04], mat.brass, leverPivot);
const tongue = box(
"Sliding locking tongue",
[0.78, 0.11, 0.13],
[3.30, 0.08, 0.22],
mat.brass,
latch
);
// -------------------------------------------------------------------------
// Mechanical animation state
// -------------------------------------------------------------------------
const stateTitle = document.getElementById("state-title");
const stateDetail = document.getElementById("state-detail");
const interlockStatus = document.getElementById("interlock-status");
const openButton = document.getElementById("open-button");
const resetButton = document.getElementById("reset-button");
const stepElements = [...document.querySelectorAll(".step")];
const HALF_PI = Math.PI / 2;
const OPEN_ANGLE = THREE.MathUtils.degToRad(100);
const LEVER_TIME = 0.85;
const TONGUE_TIME = 0.58;
const OPEN_TIME = 2.35;
const CLOSE_TIME = 2.10;
const EXTEND_TIME = 0.58;
const LEVER_RESET_TIME = 0.68;
const FULL_RETRACTION = 0.78;
let phase = "locked";
let phaseTime = 0;
let leverAngle = 0;
let tongueOffset = 0;
let sashAngle = 0;
let closingStartAngle = 0;
let lastTime = performance.now();
function clamp01(value) {
return Math.max(0, Math.min(1, value));
}
function smooth(value) {
const t = clamp01(value);
return t * t * (3 - 2 * t);
}
function lerp(a, b, t) {
return a + (b - a) * t;
}
function tongueIsEngaged() {
// The sash is only permitted to move after the tongue has moved fully
// out of the fixed keeper. This is the mechanical interlock condition.
return tongueOffset > -FULL_RETRACTION + 0.001;
}
function setStep(activeStep, completedSteps = []) {
for (const element of stepElements) {
element.classList.toggle("active", element.dataset.step === activeStep);
element.classList.toggle("done", completedSteps.includes(element.dataset.step));
}
}
function setPhase(nextPhase) {
phase = nextPhase;
phaseTime = 0;
if (nextPhase === "lever") {
stateTitle.textContent = "LEVER CLEARING";
stateTitle.style.color = "var(--orange)";
stateDetail.textContent =
"The lever rotates away from the keeper. The tongue is still seated, so the sash remains interlocked.";
setStep("lever");
} else if (nextPhase === "retract") {
stateTitle.textContent = "TONGUE RETRACTING";
stateTitle.style.color = "#ffd166";
stateDetail.textContent =
"The lever is clear. The tongue slides out of the fixed frame-mounted keeper; the sash stays stationary.";
setStep("tongue");
} else if (nextPhase === "opening") {
stateTitle.textContent = "SASH OPENING";
stateTitle.style.color = "var(--teal)";
stateDetail.textContent =
"The tongue is fully clear of the keeper. The interlock releases and the casement swings outward.";
setStep("sash");
} else if (nextPhase === "open") {
stateTitle.textContent = "OPEN";
stateTitle.style.color = "var(--green)";
stateDetail.textContent =
"The casement is open and the tongue remains safely retracted from the fixed keeper.";
setStep(null, ["lever", "tongue", "sash"]);
openButton.disabled = true;
openButton.textContent = "WINDOW OPEN";
} else if (nextPhase === "closing") {
stateTitle.textContent = "SASH CLOSING";
stateTitle.style.color = "var(--teal)";
stateDetail.textContent =
"The tongue remains retracted while the sash returns to the fixed frame.";
setStep("sash");
} else if (nextPhase === "extend") {
stateTitle.textContent = "TONGUE SEATING";
stateTitle.style.color = "#ffd166";
stateDetail.textContent =
"The sash is stationary. The locking tongue enters the frame-mounted keeper.";
setStep("tongue");
} else if (nextPhase === "leverReset") {
stateTitle.textContent = "LEVER RESETTING";
stateTitle.style.color = "var(--orange)";
stateDetail.textContent =
"The tongue is fully engaged in the fixed keeper. The sash is interlocked while the lever returns.";
setStep("lever");
} else if (nextPhase === "locked") {
stateTitle.textContent = "LOCKED";
stateTitle.style.color = "var(--orange)";
stateDetail.textContent =
"The locking tongue is seated in the fixed keeper. Window movement is interlocked.";
setStep(null);
}
}
function resetImmediately() {
leverAngle = 0;
tongueOffset = 0;
sashAngle = 0;
openButton.disabled = false;
openButton.textContent = "RELEASE + OPEN";
setPhase("locked");
}
function beginRelease() {
if (phase !== "locked") return;
openButton.disabled = true;
openButton.textContent = "RELEASING…";
setPhase("lever");
}
function beginReset() {
if (phase === "open" || sashAngle < -0.01) {
closingStartAngle = sashAngle;
openButton.disabled = true;
openButton.textContent = "CLOSING…";
setPhase("closing");
} else {
resetImmediately();
}
}
openButton.addEventListener("click", beginRelease);
resetButton.addEventListener("click", beginReset);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
beginRelease();
}
if (event.key.toLowerCase() === "r") {
beginReset();
}
});
// -------------------------------------------------------------------------
// Animation update
// -------------------------------------------------------------------------
function animateMechanism(dt) {
phaseTime += dt;
if (phase === "lever") {
leverAngle = lerp(0, -HALF_PI, smooth(phaseTime / LEVER_TIME));
tongueOffset = 0;
sashAngle = 0;
if (phaseTime >= LEVER_TIME) {
leverAngle = -HALF_PI;
setPhase("retract");
}
} else if (phase === "retract") {
leverAngle = -HALF_PI;
tongueOffset = lerp(
0,
-FULL_RETRACTION,
smooth(phaseTime / TONGUE_TIME)
);
sashAngle = 0;
if (phaseTime >= TONGUE_TIME) {
tongueOffset = -FULL_RETRACTION;
setPhase("opening");
}
} else if (phase === "opening") {
leverAngle = -HALF_PI;
tongueOffset = -FULL_RETRACTION;
// Explicit hard interlock: no sash movement is possible while the
// tongue is still in the fixed keeper.
if (tongueIsEngaged()) {
sashAngle = 0;
} else {
sashAngle = lerp(
0,
-OPEN_ANGLE,
smooth(phaseTime / OPEN_TIME)
);
}
if (phaseTime >= OPEN_TIME) {
sashAngle = -OPEN_ANGLE;
setPhase("open");
}
} else if (phase === "closing") {
leverAngle = -HALF_PI;
tongueOffset = -FULL_RETRACTION;
// Closing is also forbidden if the tongue has entered the keeper.
if (tongueIsEngaged()) {
sashAngle = closingStartAngle;
} else {
sashAngle = lerp(
closingStartAngle,
0,
smooth(phaseTime / CLOSE_TIME)
);
}
if (phaseTime >= CLOSE_TIME) {
sashAngle = 0;
setPhase("extend");
}
} else if (phase === "extend") {
sashAngle = 0;
leverAngle = -HALF_PI;
tongueOffset = lerp(
-FULL_RETRACTION,
0,
smooth(phaseTime / EXTEND_TIME)
);
if (phaseTime >= EXTEND_TIME) {
tongueOffset = 0;
setPhase("leverReset");
}
} else if (phase === "leverReset") {
sashAngle = 0;
tongueOffset = 0;
leverAngle = lerp(
-HALF_PI,
0,
smooth(phaseTime / LEVER_RESET_TIME)
);
if (phaseTime >= LEVER_RESET_TIME) {
leverAngle = 0;
openButton.disabled = false;
openButton.textContent = "RELEASE + OPEN";
setPhase("locked");
}
} else if (phase === "locked") {
leverAngle = 0;
tongueOffset = 0;
sashAngle = 0;
} else if (phase === "open") {
leverAngle = -HALF_PI;
tongueOffset = -FULL_RETRACTION;
sashAngle = -OPEN_ANGLE;
}
leverPivot.rotation.z = leverAngle;
tongue.position.x = 3.30 + tongueOffset;
sash.rotation.y = sashAngle;
const engaged = tongueIsEngaged();
if (engaged) {
interlockStatus.textContent = "ENGAGED";
interlockStatus.style.color = "var(--red)";
mat.keeper.emissive.setHex(0x7a3512);
mat.keeper.emissiveIntensity = 0.12;
mat.indicator.color.setHex(0xff5c68);
mat.indicator.emissive.setHex(0xff2635);
} else {
interlockStatus.textContent = "CLEAR";
interlockStatus.style.color = "var(--green)";
mat.keeper.emissive.setHex(0x2b6b49);
mat.keeper.emissiveIntensity = 0.08;
mat.indicator.color.setHex(0x8be28b);
mat.indicator.emissive.setHex(0x4cbf62);
}
keeperIndicator.scale.y = engaged ? 1 : 0.75;
}
// -------------------------------------------------------------------------
// Direct camera controls, implemented without Three.js addons
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 0.05, 0.1);
let cameraTheta = 0.43;
let cameraPhi = 0.16;
let cameraRadius = 8.2;
let dragging = false;
let previousX = 0;
let previousY = 0;
function updateCamera() {
const horizontal = cameraRadius * Math.cos(cameraPhi);
camera.position.set(
cameraTarget.x + horizontal * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.sin(cameraPhi),
cameraTarget.z + horizontal * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraTheta -= deltaX * 0.006;
cameraPhi = THREE.MathUtils.clamp(cameraPhi + deltaY * 0.005, -0.18, 0.88);
updateCamera();
});
renderer.domElement.addEventListener("pointerup", (event) => {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
});
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.006,
5.2,
11.5
);
updateCamera();
}, { passive: false });
updateCamera();
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
function frame(now) {
const dt = Math.min((now - lastTime) / 1000, 0.05);
lastTime = now;
animateMechanism(dt);
renderer.render(scene, camera);
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
</script>
</body>
</html>
```