Prompt specification
Build or customize the Three.js 3D scene "Key selection on a tray" 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: Key selection on a tray
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/key-selection-on-a-tray-x0042
Scene Description Prompt:
A low-poly key organizer on a hall tray; selecting a key unfolds it and highlights its matching lock. Three.js from cdnjs, one HTML file.
Working Three.js Starter (key-selection-on-a-tray-x0042):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Hall Key Organizer</title>
<style>
:root {
--ink: #263431;
--muted: #66716d;
--paper: #f1eadf;
--accent: #d8a63f;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8dfd1;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
cursor: grab;
}
#scene.dragging {
cursor: grabbing;
}
#ui {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.intro {
position: absolute;
top: clamp(24px, 4vw, 58px);
left: clamp(22px, 4vw, 64px);
width: min(430px, calc(100vw - 44px));
text-shadow: 0 1px 20px rgba(255, 255, 255, 0.65);
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 13px;
color: #56635f;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.21em;
text-transform: uppercase;
}
.eyebrow::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 5px color-mix(in srgb, var(--accent) 18%, transparent);
content: "";
}
h1 {
max-width: 390px;
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(44px, 5.1vw, 78px);
font-weight: 400;
line-height: 0.93;
letter-spacing: -0.055em;
}
.intro p {
max-width: 350px;
margin: 19px 0 0;
color: #68716e;
font-size: 13px;
line-height: 1.65;
}
.top-actions {
position: absolute;
top: clamp(20px, 3vw, 38px);
right: clamp(20px, 3vw, 42px);
display: flex;
align-items: center;
gap: 10px;
pointer-events: auto;
}
.reset-button {
display: grid;
width: 43px;
height: 43px;
padding: 0;
place-items: center;
border: 1px solid rgba(48, 62, 58, 0.15);
border-radius: 50%;
background: rgba(250, 247, 240, 0.72);
color: var(--ink);
cursor: pointer;
box-shadow: 0 8px 30px rgba(63, 52, 39, 0.08);
backdrop-filter: blur(14px);
transition:
transform 180ms ease,
background 180ms ease;
}
.reset-button:hover {
transform: translateY(-2px) rotate(-12deg);
background: #fffaf1;
}
.reset-button svg {
width: 17px;
height: 17px;
}
.index-panel {
position: absolute;
top: 83px;
right: clamp(20px, 3vw, 42px);
width: 184px;
padding: 16px;
border: 1px solid rgba(54, 69, 65, 0.13);
border-radius: 18px;
background: rgba(247, 242, 232, 0.73);
box-shadow: 0 18px 55px rgba(71, 58, 43, 0.09);
pointer-events: auto;
backdrop-filter: blur(18px);
}
.index-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 11px;
color: #707975;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.17em;
}
.index-heading span {
display: grid;
width: 24px;
height: 19px;
place-items: center;
border-radius: 6px;
background: rgba(48, 66, 60, 0.08);
letter-spacing: 0;
}
.key-list {
display: grid;
gap: 4px;
}
.key-option {
display: grid;
grid-template-columns: 8px 27px 1fr;
align-items: center;
gap: 8px;
width: 100%;
padding: 8px 7px;
border: 0;
border-radius: 9px;
background: transparent;
color: #58635f;
text-align: left;
cursor: pointer;
transition:
color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
.key-option:hover {
color: var(--ink);
background: rgba(255, 255, 255, 0.52);
transform: translateX(-2px);
}
.key-option.active {
color: var(--ink);
background: rgba(255, 255, 255, 0.78);
font-weight: 650;
}
.key-swatch {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--swatch);
box-shadow: 0 0 0 3px color-mix(in srgb, var(--swatch) 16%, transparent);
}
.key-number {
color: #919995;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
}
.key-name {
overflow: hidden;
font-size: 11px;
text-overflow: ellipsis;
white-space: nowrap;
}
.selection-card {
position: absolute;
bottom: clamp(58px, 7vh, 92px);
left: clamp(22px, 4vw, 64px);
width: min(330px, calc(100vw - 44px));
padding: 20px 22px 18px;
overflow: hidden;
border: 1px solid rgba(45, 63, 57, 0.12);
border-left: 3px solid var(--accent);
border-radius: 4px 18px 18px 4px;
background: rgba(248, 244, 235, 0.84);
box-shadow: 0 22px 60px rgba(68, 53, 37, 0.13);
backdrop-filter: blur(18px);
transition:
border-color 220ms ease,
transform 220ms ease;
}
.selection-top {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 13px;
}
.selection-kicker {
color: #73807b;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.selection-id {
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.14em;
}
.selection-card h2 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: 28px;
font-weight: 400;
letter-spacing: -0.035em;
}
.selection-meta {
margin: 7px 0 15px;
color: #78827e;
font-size: 11px;
line-height: 1.5;
}
.selection-state {
display: flex;
align-items: center;
gap: 8px;
padding-top: 12px;
border-top: 1px solid rgba(54, 69, 65, 0.1);
color: #52645e;
font-size: 10px;
font-weight: 650;
letter-spacing: 0.02em;
}
.selection-state::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: #42c7b5;
box-shadow: 0 0 0 4px rgba(66, 199, 181, 0.13);
content: "";
}
.hint {
position: absolute;
bottom: 23px;
left: 50%;
display: flex;
align-items: center;
gap: 10px;
color: rgba(50, 64, 60, 0.7);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.13em;
text-transform: uppercase;
transform: translateX(-50%);
white-space: nowrap;
}
.mouse-icon {
position: relative;
width: 14px;
height: 20px;
border: 1.5px solid currentColor;
border-radius: 8px;
}
.mouse-icon::before {
position: absolute;
top: 3px;
left: 5px;
width: 2px;
height: 5px;
border-radius: 2px;
background: currentColor;
content: "";
}
#tooltip {
position: fixed;
z-index: 5;
padding: 7px 10px;
border: 1px solid rgba(41, 57, 52, 0.12);
border-radius: 8px;
background: rgba(37, 51, 47, 0.88);
color: #fffaf0;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.09em;
pointer-events: none;
opacity: 0;
transform: translateY(4px);
transition:
opacity 120ms ease,
transform 120ms ease;
white-space: nowrap;
}
#tooltip.visible {
opacity: 1;
transform: translateY(0);
}
#lock-callout {
position: fixed;
z-index: 4;
padding: 7px 10px;
border: 1px solid rgba(50, 184, 168, 0.3);
border-radius: 999px;
background: rgba(240, 255, 250, 0.9);
color: #227d73;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.11em;
pointer-events: none;
opacity: 0;
transform: translate(-50%, -115%);
box-shadow: 0 9px 25px rgba(41, 151, 139, 0.15);
white-space: nowrap;
transition: opacity 180ms ease;
}
#lock-callout::after {
position: absolute;
bottom: -5px;
left: 50%;
width: 8px;
height: 8px;
border-right: 1px solid rgba(50, 184, 168, 0.3);
border-bottom: 1px solid rgba(50, 184, 168, 0.3);
background: rgba(240, 255, 250, 0.9);
content: "";
transform: translateX(-50%) rotate(45deg);
}
@media (max-width: 700px) {
.intro {
top: 22px;
}
.intro h1 {
font-size: 43px;
}
.intro p {
display: none;
}
.index-panel {
top: 23px;
right: 16px;
width: auto;
padding: 7px;
border-radius: 15px;
}
.index-heading,
.key-name {
display: none;
}
.key-list {
display: flex;
}
.key-option {
display: grid;
grid-template-columns: 1fr;
width: 29px;
height: 29px;
padding: 0;
place-items: center;
}
.key-swatch {
width: 9px;
height: 9px;
}
.key-number {
display: none;
}
.selection-card {
bottom: 63px;
left: 16px;
width: calc(100vw - 32px);
padding: 15px 17px;
}
.selection-card h2 {
font-size: 23px;
}
.selection-meta {
margin-bottom: 10px;
}
.hint {
bottom: 20px;
font-size: 8px;
}
.hint .desktop-hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D hall key organizer"></canvas>
<main id="ui">
<section class="intro">
<div class="eyebrow">Hallway key index</div>
<h1>One tray.<br />Every way in.</h1>
<p>
Select a hanging key to unfold it. Its matching lock on the hall tray
will light up.
</p>
</section>
<div class="top-actions">
<button id="reset-view" class="reset-button" type="button" aria-label="Reset camera">
<svg viewBox="0 0 24 24" fill="none" aria-hidden="true">
<path
d="M4.5 8.5A8 8 0 1 1 4 15M4.5 8.5V3.8m0 4.7h4.7"
stroke="currentColor"
stroke-width="1.8"
stroke-linecap="round"
stroke-linejoin="round"
/>
</svg>
</button>
</div>
<aside class="index-panel" aria-label="Key index">
<div class="index-heading">
Key index <span>05</span>
</div>
<div id="key-list" class="key-list"></div>
</aside>
<section id="selection-card" class="selection-card" aria-live="polite">
<div class="selection-top">
<span id="selection-kicker" class="selection-kicker">Entry lock</span>
<span id="selection-id" class="selection-id">01</span>
</div>
<h2 id="selection-name">Front Door</h2>
<p id="selection-meta" class="selection-meta">
Aged brass · 32 mm · matches the front entry padlock
</p>
<div id="selection-state" class="selection-state">
Unfolded · matching lock highlighted
</div>
</section>
<div class="hint">
<span class="mouse-icon" aria-hidden="true"></span>
<span>Drag to orbit</span>
<span>·</span>
<span>Scroll to zoom</span>
<span class="desktop-hint">· Click a key</span>
</div>
</main>
<div id="tooltip"></div>
<div id="lock-callout">MATCH 01 · FRONT DOOR</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Data
// -------------------------------------------------------------------------
const keyData = [
{
id: "01",
room: "Entry",
name: "Front Door",
materialName: "Aged brass",
detail: "32 mm · front entry padlock",
accent: "#d8a63f",
accentHex: 0xd8a63f,
metal: 0xc9a25d
},
{
id: "02",
room: "Garden",
name: "Side Gate",
materialName: "Brushed steel",
detail: "28 mm · garden gate lock",
accent: "#789ba7",
accentHex: 0x789ba7,
metal: 0xaeb9b9
},
{
id: "03",
room: "Studio",
name: "Back Door",
materialName: "Warm copper",
detail: "35 mm · studio entrance lock",
accent: "#c56f4e",
accentHex: 0xc56f4e,
metal: 0xbd6e4f
},
{
id: "04",
room: "Utility",
name: "Cupboard",
materialName: "Graphite steel",
detail: "22 mm · utility cupboard lock",
accent: "#596260",
accentHex: 0x596260,
metal: 0x51585a
},
{
id: "05",
room: "Roof",
name: "Roof Hatch",
materialName: "Satin nickel",
detail: "40 mm · roof access lock",
accent: "#819b73",
accentHex: 0x819b73,
metal: 0xa8aaa2
}
];
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
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(0xe8dfd1);
scene.fog = new THREE.Fog(0xe8dfd1, 16, 31);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
60
);
const cameraTarget = new THREE.Vector3(0.1, 1.45, 0);
const cameraHome = {
theta: 0.61,
phi: 0.83
};
const cameraState = {
theta: cameraHome.theta,
phi: cameraHome.phi,
radius: window.innerWidth < 700 ? 15.8 : 12.6,
targetTheta: cameraHome.theta,
targetPhi: cameraHome.phi,
targetRadius: window.innerWidth < 700 ? 15.8 : 12.6
};
// -------------------------------------------------------------------------
// Materials and light
// -------------------------------------------------------------------------
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.65,
metalness: 0.05,
flatShading: true,
...options
});
}
const materials = {
table: standardMaterial(0xb49a7e, { roughness: 0.94 }),
wall: standardMaterial(0xded5c7, { roughness: 1 }),
trayEdge: standardMaterial(0xb9a98f, { roughness: 0.72 }),
trayInset: standardMaterial(0xe9dfcf, { roughness: 0.82 }),
organizerBack: standardMaterial(0xa9854e, {
roughness: 0.34,
metalness: 0.7
}),
organizer: standardMaterial(0x263e3a, {
roughness: 0.67,
metalness: 0.08
}),
organizerInset: standardMaterial(0x314a45, {
roughness: 0.76
}),
brass: standardMaterial(0xc7a460, {
roughness: 0.31,
metalness: 0.78
}),
silver: standardMaterial(0xb9beb9, {
roughness: 0.3,
metalness: 0.82
}),
lockMat: standardMaterial(0xcfc8ba, {
roughness: 0.76
}),
dark: standardMaterial(0x26312e, {
roughness: 0.8
})
};
const hemisphere = new THREE.HemisphereLight(0xfff8e9, 0x766858, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffefd3, 3.2);
keyLight.position.set(-5.5, 10, 7.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 28;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xc8e2df, 1.25);
fillLight.position.set(7, 5, -4);
scene.add(fillLight);
const warmLight = new THREE.PointLight(0xffcf8e, 8, 13, 2);
warmLight.position.set(-4, 5, 3);
scene.add(warmLight);
// -------------------------------------------------------------------------
// Geometry helpers
// -------------------------------------------------------------------------
function roundedShape(width, height, radius) {
const x = -width / 2;
const y = -height / 2;
const shape = new THREE.Shape();
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
return shape;
}
function roundedPanel(
width,
height,
depth,
radius,
material,
bevel = 0.035
) {
const geometry = new THREE.ExtrudeGeometry(
roundedShape(width, height, radius),
{
depth,
steps: 1,
curveSegments: 5,
bevelEnabled: bevel > 0,
bevelSize: bevel,
bevelThickness: bevel,
bevelSegments: 1
}
);
geometry.translate(0, 0, -depth / 2);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function roundedSlab(width, depth, height, radius, material) {
const mesh = roundedPanel(
width,
depth,
height,
radius,
material,
Math.min(0.035, height * 0.22)
);
mesh.geometry.rotateX(-Math.PI / 2);
return mesh;
}
function makeTextPlane(
text,
width,
height,
options = {}
) {
const {
background = "rgba(255,255,255,0)",
foreground = "#263431",
fontSize = 42,
fontWeight = 700,
circle = false
} = options;
const labelCanvas = document.createElement("canvas");
labelCanvas.width = circle ? 192 : 768;
labelCanvas.height = circle ? 192 : 160;
const context = labelCanvas.getContext("2d");
context.clearRect(0, 0, labelCanvas.width, labelCanvas.height);
if (background !== "rgba(255,255,255,0)") {
context.fillStyle = background;
context.beginPath();
context.roundRect(
4,
4,
labelCanvas.width - 8,
labelCanvas.height - 8,
circle ? 96 : 26
);
context.fill();
}
context.fillStyle = foreground;
context.font = `${fontWeight} ${fontSize}px Arial, sans-serif`;
context.textAlign = "center";
context.textBaseline = "middle";
if (circle) {
context.beginPath();
context.arc(96, 96, 70, 0, Math.PI * 2);
context.fillStyle = "rgba(250,248,241,0.95)";
context.fill();
context.fillStyle = foreground;
}
context.fillText(
text,
labelCanvas.width / 2,
labelCanvas.height / 2 + 2,
labelCanvas.width - 32
);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
texture.generateMipmaps = false;
const material = new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
depthWrite: false,
toneMapped: false,
side: THREE.DoubleSide
});
return new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
material
);
}
// -------------------------------------------------------------------------
// Hall, table, and tray
// -------------------------------------------------------------------------
const wall = new THREE.Mesh(
new THREE.PlaneGeometry(34, 20),
materials.wall
);
wall.position.set(0, 7, -8);
wall.receiveShadow = true;
scene.add(wall);
const table = new THREE.Mesh(
new THREE.BoxGeometry(26, 0.6, 18),
materials.table
);
table.position.set(0, -0.38, 0);
table.receiveShadow = true;
table.castShadow = true;
scene.add(table);
const trayEdge = roundedSlab(
11.8,
7,
0.28,
0.42,
materials.trayEdge
);
trayEdge.position.y = 0.08;
scene.add(trayEdge);
const trayInset = roundedSlab(
11.28,
6.48,
0.065,
0.3,
materials.trayInset
);
trayInset.position.y = 0.135;
scene.add(trayInset);
const lockMat = roundedSlab(
5.45,
1.65,
0.04,
0.24,
standardMaterial(0xd7cdbd, { roughness: 0.9 })
);
lockMat.position.set(2.65, 0.19, 0.34);
scene.add(lockMat);
const lockFieldLabel = makeTextPlane(
"MATCHING LOCKS",
1.55,
0.23,
{
foreground: "#7d817a",
fontSize: 34,
fontWeight: 700
}
);
lockFieldLabel.rotation.x = -Math.PI / 2;
lockFieldLabel.position.set(2.65, 0.216, 1.02);
scene.add(lockFieldLabel);
// -------------------------------------------------------------------------
// Organizer board
// -------------------------------------------------------------------------
const organizerBack = roundedPanel(
5.42,
3.55,
0.18,
0.28,
materials.organizerBack,
0.05
);
organizerBack.position.set(-2.63, 2.02, -1.07);
scene.add(organizerBack);
const organizerFace = roundedPanel(
5.2,
3.33,
0.2,
0.22,
materials.organizer,
0.04
);
organizerFace.position.set(-2.63, 2.02, -0.9);
scene.add(organizerFace);
const organizerInset = roundedPanel(
4.78,
2.92,
0.035,
0.17,
materials.organizerInset,
0.01
);
organizerInset.position.set(-2.63, 1.98, -0.782);
scene.add(organizerInset);
const organizerLabel = makeTextPlane(
"KEY / ORGANIZER",
1.55,
0.25,
{
background: "rgba(31,48,44,0.72)",
foreground: "#d9bd83",
fontSize: 31,
fontWeight: 700
}
);
organizerLabel.position.set(-2.63, 3.5, -0.75);
scene.add(organizerLabel);
const rail = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.055, 4.94, 8),
materials.brass
);
rail.rotation.z = Math.PI / 2;
rail.position.set(-2.63, 3.13, -0.57);
rail.castShadow = true;
scene.add(rail);
const screwGeometry = new THREE.CylinderGeometry(0.065, 0.065, 0.05, 8);
[
[-4.88, 3.48],
[-0.38, 3.48],
[-4.88, 0.55],
[-0.38, 0.55]
].forEach(([x, y]) => {
const screw = new THREE.Mesh(screwGeometry, materials.brass);
screw.rotation.x = Math.PI / 2;
screw.position.set(x, y, -0.755);
scene.add(screw);
});
// -------------------------------------------------------------------------
// Keys and locks
// -------------------------------------------------------------------------
const keyMeshes = [];
const lockMeshes = [];
const keyStates = [];
const lockStates = [];
const keyPositions = [-4.58, -3.48, -2.38, -1.28, -0.18];
function registerMesh(mesh, kind, index) {
mesh.userData.kind = kind;
mesh.userData.index = index;
if (kind === "key") keyMeshes.push(mesh);
else lockMeshes.push(mesh);
}
function createKey(index, x) {
const data = keyData[index];
const metalMaterial = standardMaterial(data.metal, {
roughness: 0.3,
metalness: 0.76
});
const accentMaterial = standardMaterial(data.accentHex, {
roughness: 0.36,
metalness: 0.52
});
const pivot = new THREE.Group();
pivot.position.set(x, 2.96, -0.42);
pivot.rotation.z = (index - 2) * 0.017;
scene.add(pivot);
// Peg and hanging hardware.
const peg = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 0.045, 0.4, 8),
materials.brass
);
peg.rotation.x = Math.PI / 2;
peg.position.set(x, 3.03, -0.73);
peg.castShadow = true;
scene.add(peg);
const pegStem = new THREE.Mesh(
new THREE.CylinderGeometry(0.038, 0.038, 0.15, 8),
materials.brass
);
pegStem.position.set(x, 3.08, -0.57);
scene.add(pegStem);
// Main key body.
const bow = new THREE.Mesh(
new THREE.TorusGeometry(0.25, 0.074, 5, 12),
metalMaterial
);
bow.position.y = -0.22;
pivot.add(bow);
const bowInset = new THREE.Mesh(
new THREE.CylinderGeometry(0.085, 0.085, 0.12, 8),
accentMaterial
);
bowInset.rotation.x = Math.PI / 2;
bowInset.position.set(0, -0.22, 0.015);
pivot.add(bowInset);
const neck = new THREE.Mesh(
new THREE.BoxGeometry(0.135, 0.43, 0.1),
metalMaterial
);
neck.position.y = -0.58;
pivot.add(neck);
const stem = new THREE.Mesh(
new THREE.BoxGeometry(0.13, 0.48, 0.095),
metalMaterial
);
stem.position.y = -0.96;
pivot.add(stem);
const hinge = new THREE.Mesh(
new THREE.CylinderGeometry(0.105, 0.105, 0.15, 8),
accentMaterial
);
hinge.rotation.x = Math.PI / 2;
hinge.position.y = -1.2;
pivot.add(hinge);
// Articulated lower blade.
const lower = new THREE.Group();
lower.position.y = -1.2;
lower.rotation.z = 2.25;
pivot.add(lower);
const lowerShaft = new THREE.Mesh(
new THREE.BoxGeometry(0.12, 0.88, 0.09),
metalMaterial
);
lowerShaft.position.y = -0.44;
lower.add(lowerShaft);
const toothA = new THREE.Mesh(
new THREE.BoxGeometry(0.28, 0.15, 0.095),
metalMaterial
);
toothA.position.set(0.12, -0.64, 0);
lower.add(toothA);
const toothB = new THREE.Mesh(
new THREE.BoxGeometry(0.22, 0.13, 0.095),
metalMaterial
);
toothB.position.set(0.09, -0.82, 0);
lower.add(toothB);
const tip = new THREE.Mesh(
new THREE.BoxGeometry(0.16, 0.17, 0.1),
accentMaterial
);
tip.position.set(0.035, -0.93, 0);
lower.add(tip);
pivot.traverse((child) => {
if (child.isMesh) registerMesh(child, "key", index);
});
keyStates.push({
pivot,
lower,
index,
hover: false
});
}
function createLock(index, x) {
const data = keyData[index];
const group = new THREE.Group();
group.position.set(x, 0.23, 0.2);
scene.add(group);
const bodyMaterial = standardMaterial(data.accentHex, {
roughness: 0.49,
metalness: 0.32,
emissive: 0x36d4c1,
emissiveIntensity: 0
});
const shackleMaterial = standardMaterial(0xbfc4bd, {
roughness: 0.28,
metalness: 0.86,
emissive: 0x36d4c1,
emissiveIntensity: 0
});
const body = roundedPanel(
0.7,
0.58,
0.34,
0.12,
bodyMaterial,
0.045
);
body.position.y = 0.32;
group.add(body);
const shackle = new THREE.Mesh(
new THREE.TorusGeometry(0.235, 0.069, 6, 18, Math.PI),
shackleMaterial
);
shackle.position.y = 0.59;
group.add(shackle);
const keyhole = new THREE.Mesh(
new THREE.CircleGeometry(0.062, 8),
materials.dark
);
keyhole.position.set(0, 0.32, 0.176);
group.add(keyhole);
const keyholeStem = new THREE.Mesh(
new THREE.BoxGeometry(0.035, 0.12, 0.01),
materials.dark
);
keyholeStem.position.set(0, 0.25, 0.178);
group.add(keyholeStem);
const numberPlate = makeTextPlane(
data.id,
0.22,
0.22,
{
foreground: data.accent,
fontSize: 64,
fontWeight: 800,
circle: true
}
);
numberPlate.position.set(0.2, 0.48, 0.182);
group.add(numberPlate);
group.traverse((child) => {
if (child.isMesh) registerMesh(child, "lock", index);
});
// Ground marker around the lock.
const marker = new THREE.Group();
marker.position.set(x, 0.225, 0.2);
const ringMaterial = new THREE.MeshBasicMaterial({
color: 0x37cfbd,
transparent: true,
opacity: 0,
depthWrite: false,
toneMapped: false
});
const markerRing = new THREE.Mesh(
new THREE.TorusGeometry(0.52, 0.027, 4, 28),
ringMaterial
);
markerRing.rotation.x = Math.PI / 2;
marker.add(markerRing);
const glowMaterial = new THREE.MeshBasicMaterial({
color: 0x45e0cb,
transparent: true,
opacity: 0,
depthWrite: false,
side: THREE.DoubleSide,
toneMapped: false
});
const glowDisc = new THREE.Mesh(
new THREE.CircleGeometry(0.58, 28),
glowMaterial
);
glowDisc.rotation.x = -Math.PI / 2;
glowDisc.position.y = -0.003;
marker.add(glowDisc);
scene.add(marker);
const pointLight = new THREE.PointLight(0x47e4cc, 0, 3.6, 2);
pointLight.position.set(x, 1.15, 0.55);
scene.add(pointLight);
lockStates.push({
group,
bodyMaterial,
shackleMaterial,
marker,
markerRing,
ringMaterial,
glowMaterial,
pointLight,
index,
hover: false
});
}
keyPositions.forEach((x, index) => createKey(index, x));
[0.45, 1.55, 2.65, 3.75, 4.85].forEach((x, index) =>
createLock(index, x)
);
// -------------------------------------------------------------------------
// UI and selection
// -------------------------------------------------------------------------
const selectionCard = document.querySelector("#selection-card");
const selectionKicker = document.querySelector("#selection-kicker");
const selectionId = document.querySelector("#selection-id");
const selectionName = document.querySelector("#selection-name");
const selectionMeta = document.querySelector("#selection-meta");
const selectionState = document.querySelector("#selection-state");
const keyList = document.querySelector("#key-list");
const tooltip = document.querySelector("#tooltip");
const callout = document.querySelector("#lock-callout");
const keyButtons = [];
keyData.forEach((data, index) => {
const button = document.createElement("button");
button.type = "button";
button.className = "key-option";
button.setAttribute("aria-label", `Select ${data.name} key`);
button.innerHTML = `
<span class="key-swatch" style="--swatch:${data.accent}"></span>
<span class="key-number">${data.id}</span>
<span class="key-name">${data.name}</span>
`;
button.addEventListener("click", () => selectKey(index));
keyList.appendChild(button);
keyButtons.push(button);
});
let selectedIndex = 0;
let hoveredKey = -1;
let hoveredLock = -1;
function selectKey(index) {
selectedIndex = index;
const data = keyData[index];
selectionCard.style.setProperty("--accent", data.accent);
selectionKicker.textContent = `${data.room} lock`;
selectionId.textContent = data.id;
selectionName.textContent = `${data.name} Key`;
selectionMeta.textContent = `${data.materialName} · ${data.detail}`;
selectionState.textContent = "Unfolded · matching lock highlighted";
callout.textContent = `MATCH ${data.id} · ${data.name.toUpperCase()}`;
keyButtons.forEach((button, buttonIndex) => {
button.classList.toggle("active", buttonIndex === index);
button.setAttribute("aria-pressed", String(buttonIndex === index));
});
}
// -------------------------------------------------------------------------
// Custom camera controls
// -------------------------------------------------------------------------
const pointer = {
down: false,
dragging: false,
startX: 0,
startY: 0,
lastX: 0,
lastY: 0,
id: null
};
const raycaster = new THREE.Raycaster();
const pointerNDC = new THREE.Vector2();
const pickables = [...keyMeshes, ...lockMeshes];
function updatePointerNDC(event) {
const rect = canvas.getBoundingClientRect();
pointerNDC.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointerNDC.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function pick(event) {
updatePointerNDC(event);
raycaster.setFromCamera(pointerNDC, camera);
const hits = raycaster.intersectObjects(pickables, false);
return hits.length ? hits[0].object : null;
}
function showTooltip(event, kind, index) {
const data = keyData[index];
tooltip.textContent = `${data.id} · ${kind === "key" ? "UNFOLD KEY" : data.name.toUpperCase()}`;
tooltip.style.left = `${event.clientX + 14}px`;
tooltip.style.top = `${event.clientY + 14}px`;
tooltip.classList.add("visible");
}
function clearHover() {
hoveredKey = -1;
hoveredLock = -1;
tooltip.classList.remove("visible");
}
canvas.addEventListener("pointerdown", (event) => {
pointer.down = true;
pointer.dragging = false;
pointer.id = event.pointerId;
pointer.startX = event.clientX;
pointer.startY = event.clientY;
pointer.lastX = event.clientX;
pointer.lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (pointer.down) {
const dx = event.clientX - pointer.lastX;
const dy = event.clientY - pointer.lastY;
const totalDistance = Math.hypot(
event.clientX - pointer.startX,
event.clientY - pointer.startY
);
if (totalDistance > 4) {
pointer.dragging = true;
canvas.classList.add("dragging");
}
if (pointer.dragging) {
cameraState.targetTheta -= dx * 0.006;
cameraState.targetPhi = THREE.MathUtils.clamp(
cameraState.targetPhi - dy * 0.005,
0.42,
1.23
);
}
pointer.lastX = event.clientX;
pointer.lastY = event.clientY;
tooltip.classList.remove("visible");
return;
}
const hit = pick(event);
hoveredKey = -1;
hoveredLock = -1;
if (hit) {
if (hit.userData.kind === "key") {
hoveredKey = hit.userData.index;
showTooltip(event, "key", hoveredKey);
} else {
hoveredLock = hit.userData.index;
showTooltip(event, "lock", hoveredLock);
}
canvas.style.cursor = "pointer";
} else {
canvas.style.cursor = "grab";
tooltip.classList.remove("visible");
}
});
canvas.addEventListener("pointerup", (event) => {
const wasClick = !pointer.dragging;
pointer.down = false;
canvas.classList.remove("dragging");
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
if (wasClick) {
const hit = pick(event);
if (hit) selectKey(hit.userData.index);
}
pointer.dragging = false;
});
canvas.addEventListener("pointercancel", () => {
pointer.down = false;
pointer.dragging = false;
canvas.classList.remove("dragging");
});
canvas.addEventListener("pointerleave", () => {
if (!pointer.down) clearHover();
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius + event.deltaY * 0.008,
7.8,
19
);
},
{ passive: false }
);
function resetCamera() {
cameraState.targetTheta = cameraHome.theta;
cameraState.targetPhi = cameraHome.phi;
cameraState.targetRadius = window.innerWidth < 700 ? 15.8 : 12.6;
}
document.querySelector("#reset-view").addEventListener("click", resetCamera);
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.key >= "1" && event.key <= "5") {
selectKey(Number(event.key) - 1);
}
if (event.key.toLowerCase() === "r") resetCamera();
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
function damp(current, target, speed, delta) {
return THREE.MathUtils.lerp(
current,
target,
1 - Math.exp(-speed * delta)
);
}
const clock = new THREE.Clock();
const projectedCallout = new THREE.Vector3();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
cameraState.theta = damp(
cameraState.theta,
cameraState.targetTheta,
7,
delta
);
cameraState.phi = damp(
cameraState.phi,
cameraState.targetPhi,
7,
delta
);
cameraState.radius = damp(
cameraState.radius,
cameraState.targetRadius,
7,
delta
);
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraTarget.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraTarget.y + cameraState.radius * Math.cos(cameraState.phi),
cameraTarget.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
keyStates.forEach((state) => {
const isSelected = state.index === selectedIndex;
const isHovered = state.index === hoveredKey;
const targetFold = isSelected ? 0 : 2.25;
const targetSwing = isSelected ? -0.9 : 0;
const targetScale = isSelected ? 1.045 : isHovered ? 1.025 : 1;
state.lower.rotation.z = damp(
state.lower.rotation.z,
targetFold,
7.5,
delta
);
state.pivot.rotation.x = damp(
state.pivot.rotation.x,
targetSwing,
6.5,
delta
);
const scale = damp(
state.pivot.scale.x,
targetScale,
10,
delta
);
state.pivot.scale.setScalar(scale);
});
lockStates.forEach((state) => {
const isSelected = state.index === selectedIndex;
const isHovered = state.index === hoveredLock;
const pulse = 0.5 + Math.sin(elapsed * 3.2) * 0.5;
const targetEmissive = isSelected
? 0.34 + pulse * 0.18
: isHovered
? 0.1
: 0;
state.bodyMaterial.emissiveIntensity = damp(
state.bodyMaterial.emissiveIntensity,
targetEmissive,
8,
delta
);
state.shackleMaterial.emissiveIntensity = damp(
state.shackleMaterial.emissiveIntensity,
isSelected ? 0.16 : 0,
8,
delta
);
const targetRingOpacity = isSelected ? 0.56 + pulse * 0.28 : 0;
const targetGlowOpacity = isSelected ? 0.055 + pulse * 0.05 : 0;
state.ringMaterial.opacity = damp(
state.ringMaterial.opacity,
targetRingOpacity,
9,
delta
);
state.glowMaterial.opacity = damp(
state.glowMaterial.opacity,
targetGlowOpacity,
9,
delta
);
const markerScale = isSelected ? 1 + pulse * 0.075 : 1;
state.marker.scale.setScalar(
damp(state.marker.scale.x, markerScale, 8, delta)
);
state.pointLight.intensity = damp(
state.pointLight.intensity,
isSelected ? 0.8 + pulse * 0.75 : 0,
8,
delta
);
});
scene.updateMatrixWorld();
camera.updateMatrixWorld();
const lock = lockStates[selectedIndex];
projectedCallout.set(lock.group.position.x, 1.25, lock.group.position.z);
projectedCallout.project(camera);
const screenX = (projectedCallout.x * 0.5 + 0.5) * window.innerWidth;
const screenY = (-projectedCallout.y * 0.5 + 0.5) * window.innerHeight;
const visible = projectedCallout.z > -1 && projectedCallout.z < 1;
callout.style.left = `${screenX}px`;
callout.style.top = `${screenY}px`;
callout.style.opacity = visible ? "1" : "0";
renderer.render(scene, camera);
}
// -------------------------------------------------------------------------
// Resize and initial state
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
selectKey(0);
animate();
</script>
</body>
</html>
```