Prompt specification
Build or customize the Three.js 3D scene "Key Token and Persistent Lock State" 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 Token and Persistent Lock State
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/key-token-and-persistent-lock-state-x3505
Scene Description Prompt:
Please make a stylized Three.js scene in one standalone HTML file using cdnjs. Model a key, lock, and door with a visible locked-unlocked indicator. Separate possessing the key from having already changed the lock's state.
Working Three.js Starter (key-token-and-persistent-lock-state-x3505):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Key, Lock & Door</title>
<style>
:root {
color-scheme: dark;
--bg: #0b0f19;
--panel: rgba(16, 22, 37, 0.9);
--border: rgba(255, 255, 255, 0.12);
--text: #eef3ff;
--muted: #9ba8c5;
--red: #ff5268;
--green: #55e39b;
--blue: #8bb8ff;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
width: 100%;
height: 100%;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--text);
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
top: 22px;
left: 22px;
width: min(360px, calc(100vw - 44px));
padding: 22px;
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
backdrop-filter: blur(16px);
user-select: none;
}
.eyebrow {
margin: 0 0 7px;
color: var(--blue);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0 0 8px;
font-size: 29px;
line-height: 1.05;
letter-spacing: -0.04em;
}
.intro {
margin: 0 0 18px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.stats {
display: grid;
gap: 8px;
margin-bottom: 17px;
}
.stat {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 11px 12px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 11px;
background: rgba(255, 255, 255, 0.035);
}
.stat span {
color: var(--muted);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.1em;
}
.stat strong {
font-size: 12px;
text-align: right;
}
.key-status {
color: #d7def0;
}
.locked {
color: var(--red);
}
.unlocked {
color: var(--green);
}
.open {
color: var(--blue);
}
.actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
button {
min-height: 39px;
padding: 9px 11px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 10px;
background: rgba(255, 255, 255, 0.075);
color: var(--text);
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
transition: background 150ms ease, transform 150ms ease,
border-color 150ms ease;
}
button:hover {
background: rgba(139, 184, 255, 0.16);
border-color: rgba(139, 184, 255, 0.55);
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
#message {
min-height: 38px;
margin: 15px 0 0;
color: #dce5fb;
font-size: 13px;
line-height: 1.45;
}
.note {
margin: 13px 0 0;
padding-top: 12px;
border-top: 1px solid rgba(255, 255, 255, 0.09);
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.hint {
position: fixed;
right: 22px;
bottom: 20px;
padding: 10px 14px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(11, 15, 25, 0.72);
color: var(--muted);
font-size: 11px;
letter-spacing: 0.02em;
backdrop-filter: blur(12px);
pointer-events: none;
}
@media (max-width: 650px) {
.panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 16px;
}
h1 {
font-size: 24px;
}
.intro {
margin-bottom: 12px;
}
.hint {
right: 12px;
bottom: 12px;
left: 12px;
text-align: center;
border-radius: 10px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<aside class="panel">
<p class="eyebrow">Interactive object study</p>
<h1>Keyhouse</h1>
<p class="intro">
A key can be carried without changing the lock. The lock remembers
its state even if the key is dropped.
</p>
<div class="stats">
<div class="stat">
<span>KEY POSSESSION</span>
<strong id="keyStatus" class="key-status">NOT POSSESSED</strong>
</div>
<div class="stat">
<span>LOCK STATE</span>
<strong id="lockStatus" class="locked">LOCKED</strong>
</div>
<div class="stat">
<span>DOOR</span>
<strong id="doorStatus">CLOSED</strong>
</div>
</div>
<div class="actions">
<button id="pickupButton">Pick up key</button>
<button id="useKeyButton">Use key on lock</button>
<button id="doorButton">Open door</button>
<button id="dropButton">Drop key</button>
</div>
<p id="message" aria-live="polite">
Pick up the golden key. Carrying it does not unlock anything yet.
</p>
<p class="note">
Click the key, the lock, or the door directly. You can also use the
buttons. Drag to orbit the camera and scroll to zoom.
</p>
</aside>
<div class="hint">Drag to orbit · Scroll to zoom · Click objects to interact</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Scene setup
// ------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0b0f19);
scene.fog = new THREE.Fog(0x0b0f19, 8, 18);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
50
);
scene.add(camera);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
document.getElementById("scene").appendChild(renderer.domElement);
const clock = new THREE.Clock();
// ------------------------------------------------------------
// Lights
// ------------------------------------------------------------
scene.add(
new THREE.HemisphereLight(0x9ebcff, 0x10131d, 1.45)
);
const keyLight = new THREE.DirectionalLight(0xffffff, 2.4);
keyLight.position.set(4.5, 7, 5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -3;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x527dff, 22, 12, 2);
rimLight.position.set(-4.2, 3.6, 2.4);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffa64d, 13, 8, 2);
warmLight.position.set(2.8, 3.8, 2.8);
scene.add(warmLight);
// ------------------------------------------------------------
// Materials and helpers
// ------------------------------------------------------------
const materials = {
wall: new THREE.MeshStandardMaterial({
color: 0x20283a,
roughness: 0.88,
metalness: 0.04
}),
wallInset: new THREE.MeshStandardMaterial({
color: 0x29334a,
roughness: 0.9
}),
floor: new THREE.MeshStandardMaterial({
color: 0x111827,
roughness: 0.72,
metalness: 0.12
}),
frame: new THREE.MeshStandardMaterial({
color: 0x35435d,
roughness: 0.48,
metalness: 0.42
}),
door: new THREE.MeshStandardMaterial({
color: 0x243b52,
roughness: 0.55,
metalness: 0.18
}),
doorInset: new THREE.MeshStandardMaterial({
color: 0x1b2d40,
roughness: 0.68,
metalness: 0.1
}),
metal: new THREE.MeshStandardMaterial({
color: 0x9caec4,
roughness: 0.28,
metalness: 0.78
}),
darkMetal: new THREE.MeshStandardMaterial({
color: 0x263247,
roughness: 0.3,
metalness: 0.72
}),
gold: new THREE.MeshStandardMaterial({
color: 0xffc857,
roughness: 0.2,
metalness: 0.88,
emissive: 0x4b2600,
emissiveIntensity: 0.22
}),
black: new THREE.MeshStandardMaterial({
color: 0x05070b,
roughness: 0.4,
metalness: 0.35
})
};
function box(width, height, depth, material, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinder(
radiusTop,
radiusBottom,
height,
material,
x,
y,
z,
parent = scene,
segments = 32
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
segments
),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// ------------------------------------------------------------
// Environment
// ------------------------------------------------------------
box(11, 6.5, 0.35, materials.wall, 0, 3.05, -0.92);
// Decorative wall panels
box(1.05, 5.3, 0.06, materials.wallInset, -4.65, 3.1, -0.71);
box(1.05, 5.3, 0.06, materials.wallInset, 4.65, 3.1, -0.71);
box(8.3, 0.05, 0.06, materials.wallInset, 0, 0.42, -0.70);
const floor = box(
13,
0.24,
10,
materials.floor,
0,
-0.13,
1.3
);
floor.receiveShadow = true;
// Rug
const rug = box(
5.3,
0.035,
3.5,
new THREE.MeshStandardMaterial({
color: 0x27334b,
roughness: 0.96
}),
0,
0.015,
2.2
);
rug.receiveShadow = true;
// ------------------------------------------------------------
// Door and frame
// ------------------------------------------------------------
box(0.3, 4.3, 0.42, materials.frame, -1.68, 2.15, -0.48);
box(0.3, 4.3, 0.42, materials.frame, 1.68, 2.15, -0.48);
box(3.66, 0.3, 0.42, materials.frame, 0, 4.3, -0.48);
box(3.66, 0.18, 0.5, materials.darkMetal, 0, 0.09, -0.45);
const doorPivot = new THREE.Group();
doorPivot.position.set(-1.5, 0, -0.56);
scene.add(doorPivot);
const doorSlab = box(
3,
4,
0.22,
materials.door,
1.5,
2,
0,
doorPivot
);
doorSlab.userData.action = "door";
box(
2.48,
3.28,
0.055,
materials.doorInset,
1.5,
2.02,
0.14,
doorPivot
);
// Hinges
for (const y of [0.72, 2, 3.28]) {
cylinder(
0.1,
0.1,
0.5,
materials.metal,
0.04,
y,
0.02,
doorPivot,
20
);
}
// Lock assembly, parented to the door
const lockGroup = new THREE.Group();
lockGroup.position.set(2.2, 1.58, 0.2);
lockGroup.userData.action = "lock";
doorPivot.add(lockGroup);
box(0.48, 0.68, 0.13, materials.metal, 0, 0, 0, lockGroup);
box(0.35, 0.52, 0.03, materials.darkMetal, 0, 0, 0.08, lockGroup);
const keyhole = cylinder(
0.075,
0.075,
0.025,
materials.black,
0,
-0.07,
0.115,
lockGroup,
24
);
keyhole.rotation.x = Math.PI / 2;
box(0.045, 0.12, 0.022, materials.black, 0, -0.13, 0.118, lockGroup);
const indicatorMaterial = new THREE.MeshStandardMaterial({
color: 0xff4058,
emissive: 0xff4058,
emissiveIntensity: 2.4,
roughness: 0.2,
metalness: 0.15
});
const indicatorLED = new THREE.Mesh(
new THREE.SphereGeometry(0.075, 24, 16),
indicatorMaterial
);
indicatorLED.position.set(0, 0.225, 0.11);
lockGroup.add(indicatorLED);
const indicatorLight = new THREE.PointLight(0xff4058, 1.8, 2.2, 2);
indicatorLight.position.set(0, 0.25, 0.25);
lockGroup.add(indicatorLight);
// Door handle
const handleBase = cylinder(
0.13,
0.13,
0.12,
materials.metal,
2.2,
1.28,
0.27,
doorPivot,
24
);
handleBase.rotation.x = Math.PI / 2;
const handle = new THREE.Mesh(
new THREE.TorusGeometry(0.16, 0.035, 12, 24, Math.PI * 1.45),
materials.metal
);
handle.position.set(2.27, 1.28, 0.38);
handle.rotation.set(0, 0, -0.45);
handle.castShadow = true;
doorPivot.add(handle);
// ------------------------------------------------------------
// Large visible lock-state sign
// ------------------------------------------------------------
const signCanvas = document.createElement("canvas");
signCanvas.width = 640;
signCanvas.height = 200;
const signContext = signCanvas.getContext("2d");
const signTexture = new THREE.CanvasTexture(signCanvas);
signTexture.colorSpace = THREE.SRGBColorSpace;
const sign = new THREE.Mesh(
new THREE.PlaneGeometry(1.9, 0.6),
new THREE.MeshBasicMaterial({
map: signTexture,
transparent: true
})
);
sign.position.set(0, 4.58, -0.63);
scene.add(sign);
function drawLockSign(unlocked) {
const ctx = signContext;
const accent = unlocked ? "#55e39b" : "#ff5268";
ctx.clearRect(0, 0, signCanvas.width, signCanvas.height);
ctx.fillStyle = "rgba(8, 12, 22, 0.94)";
ctx.beginPath();
ctx.roundRect(4, 4, 632, 192, 28);
ctx.fill();
ctx.strokeStyle = accent;
ctx.lineWidth = 7;
ctx.stroke();
ctx.fillStyle = accent;
ctx.beginPath();
ctx.arc(72, 100, 25, 0, Math.PI * 2);
ctx.fill();
ctx.font = "800 58px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#f5f8ff";
ctx.fillText(unlocked ? "UNLOCKED" : "LOCKED", 366, 99);
ctx.font = "600 20px system-ui, sans-serif";
ctx.fillStyle = "#aebbd3";
ctx.fillText("LOCK STATE", 365, 158);
signTexture.needsUpdate = true;
}
// ------------------------------------------------------------
// Pedestal and key
// ------------------------------------------------------------
cylinder(
0.8,
0.95,
0.72,
materials.darkMetal,
-3.05,
0.36,
0.72
);
cylinder(
0.88,
0.88,
0.1,
materials.metal,
-3.05,
0.77,
0.72
);
const keyHalo = new THREE.Mesh(
new THREE.TorusGeometry(0.64, 0.025, 10, 48),
new THREE.MeshStandardMaterial({
color: 0xffc857,
emissive: 0xff7a12,
emissiveIntensity: 1.3,
metalness: 0.7,
roughness: 0.25
})
);
keyHalo.position.set(-3.05, 0.835, 0.72);
keyHalo.rotation.x = Math.PI / 2;
scene.add(keyHalo);
function createKey() {
const group = new THREE.Group();
const bow = new THREE.Mesh(
new THREE.TorusGeometry(0.23, 0.065, 14, 32),
materials.gold
);
bow.position.x = -0.38;
bow.castShadow = true;
group.add(bow);
const shaft = box(
0.78,
0.105,
0.105,
materials.gold,
0.19,
0,
0,
group
);
shaft.castShadow = true;
box(0.1, 0.2, 0.105, materials.gold, 0.43, -0.12, 0, group);
box(0.1, 0.27, 0.105, materials.gold, 0.58, -0.15, 0, group);
box(0.1, 0.18, 0.105, materials.gold, 0.7, -0.1, 0, group);
return group;
}
const worldKey = createKey();
worldKey.position.set(-3.05, 1.14, 0.72);
worldKey.rotation.set(0, 0.1, -0.3);
worldKey.scale.setScalar(0.82);
worldKey.traverse((object) => {
object.userData.action = "pickupKey";
});
scene.add(worldKey);
// A small label above the pedestal
const keyLabelCanvas = document.createElement("canvas");
keyLabelCanvas.width = 256;
keyLabelCanvas.height = 96;
const keyLabelContext = keyLabelCanvas.getContext("2d");
keyLabelContext.fillStyle = "rgba(10, 15, 26, 0.85)";
keyLabelContext.beginPath();
keyLabelContext.roundRect(4, 4, 248, 88, 18);
keyLabelContext.fill();
keyLabelContext.fillStyle = "#ffc857";
keyLabelContext.font = "800 34px system-ui, sans-serif";
keyLabelContext.textAlign = "center";
keyLabelContext.textBaseline = "middle";
keyLabelContext.fillText("KEY", 128, 48);
const keyLabelTexture = new THREE.CanvasTexture(keyLabelCanvas);
keyLabelTexture.colorSpace = THREE.SRGBColorSpace;
const keyLabel = new THREE.Mesh(
new THREE.PlaneGeometry(0.72, 0.27),
new THREE.MeshBasicMaterial({
map: keyLabelTexture,
transparent: true
})
);
keyLabel.position.set(-3.05, 1.87, 0.55);
scene.add(keyLabel);
// The carried key is attached to the camera.
const handKey = createKey();
handKey.position.set(0.62, -0.42, -1.25);
handKey.rotation.set(0.08, -0.12, -0.25);
handKey.scale.setScalar(0.53);
handKey.visible = false;
camera.add(handKey);
// ------------------------------------------------------------
// Independent interaction state
// ------------------------------------------------------------
let hasKey = false;
let lockState = "LOCKED";
let doorOpen = false;
let doorTargetAngle = 0;
const keyStatus = document.getElementById("keyStatus");
const lockStatus = document.getElementById("lockStatus");
const doorStatus = document.getElementById("doorStatus");
const message = document.getElementById("message");
const doorButton = document.getElementById("doorButton");
function setMessage(text) {
message.textContent = text;
}
function updateLockVisuals() {
const unlocked = lockState === "UNLOCKED";
const color = unlocked ? 0x55e39b : 0xff5268;
indicatorMaterial.color.setHex(color);
indicatorMaterial.emissive.setHex(color);
indicatorLight.color.setHex(color);
drawLockSign(unlocked);
}
function updateUI() {
keyStatus.textContent = hasKey ? "POSSESSED" : "NOT POSSESSED";
keyStatus.className = hasKey ? "unlocked" : "key-status";
lockStatus.textContent = lockState;
lockStatus.className = lockState === "UNLOCKED" ? "unlocked" : "locked";
doorStatus.textContent = doorOpen ? "OPEN" : "CLOSED";
doorStatus.className = doorOpen ? "open" : "";
doorButton.textContent = doorOpen ? "Close door" : "Open door";
}
function pickupKey() {
if (hasKey) {
setMessage("You already possess the key.");
return;
}
hasKey = true;
worldKey.visible = false;
keyHalo.visible = false;
handKey.visible = true;
setMessage(
"The key is in your possession, but the lock has not changed yet. Use it on the lock."
);
updateUI();
}
function useKeyOnLock() {
if (!hasKey) {
setMessage(
"You do not have the key. Possessing the key is required to change the lock."
);
return;
}
if (lockState === "LOCKED") {
lockState = "UNLOCKED";
setMessage(
"The key turns in the lock. The lock state is now UNLOCKED, independently of key possession."
);
} else {
lockState = "LOCKED";
if (doorOpen) {
doorOpen = false;
doorTargetAngle = 0;
}
setMessage(
"The key turns again and relocks the door. The lock state is now LOCKED."
);
}
updateLockVisuals();
updateUI();
}
function toggleDoor() {
if (lockState !== "UNLOCKED") {
setMessage(
"The door is locked. Change the lock state by using the key on the lock."
);
return;
}
doorOpen = !doorOpen;
doorTargetAngle = doorOpen ? -1.12 : 0;
setMessage(
doorOpen
? "The unlocked door swings open."
: "The door closes again. The lock remains unlocked."
);
updateUI();
}
function dropKey() {
if (!hasKey) {
setMessage("You are not carrying the key.");
return;
}
hasKey = false;
handKey.visible = false;
worldKey.visible = true;
keyHalo.visible = true;
setMessage(
lockState === "UNLOCKED"
? "You dropped the key. Notice that the lock is still UNLOCKED: the two states are independent."
: "You dropped the key. The lock is still LOCKED."
);
updateUI();
}
document
.getElementById("pickupButton")
.addEventListener("click", pickupKey);
document
.getElementById("useKeyButton")
.addEventListener("click", useKeyOnLock);
document
.getElementById("doorButton")
.addEventListener("click", toggleDoor);
document
.getElementById("dropButton")
.addEventListener("click", dropKey);
updateLockVisuals();
updateUI();
// ------------------------------------------------------------
// Direct object interaction with raycasting
// ------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const interactables = [worldKey, lockGroup, doorSlab];
function getAction(object) {
let current = object;
while (current) {
if (current.userData.action) {
return current.userData.action;
}
current = current.parent;
}
return null;
}
function handleObjectClick(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(interactables, true);
if (!hits.length) {
return;
}
const action = getAction(hits[0].object);
if (action === "pickupKey") {
pickupKey();
} else if (action === "lock") {
useKeyOnLock();
} else if (action === "door") {
toggleDoor();
}
}
// ------------------------------------------------------------
// Custom orbit-style camera controls
// ------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 1.75, 0);
let cameraYaw = 0.58;
let cameraPitch = 0.3;
let cameraRadius = 8.4;
function updateCamera() {
const horizontalRadius =
Math.cos(cameraPitch) * cameraRadius;
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontalRadius,
cameraTarget.y + Math.sin(cameraPitch) * cameraRadius,
cameraTarget.z + Math.cos(cameraYaw) * horizontalRadius
);
camera.lookAt(cameraTarget);
}
updateCamera();
let pointerDown = false;
let pointerMoved = false;
let previousX = 0;
let previousY = 0;
let downX = 0;
let downY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerDown = true;
pointerMoved = false;
previousX = event.clientX;
previousY = event.clientY;
downX = event.clientX;
downY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerDown) {
return;
}
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
if (
Math.abs(event.clientX - downX) +
Math.abs(event.clientY - downY) >
5
) {
pointerMoved = true;
}
cameraYaw -= dx * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + dy * 0.0045,
0.08,
0.85
);
previousX = event.clientX;
previousY = event.clientY;
updateCamera();
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (pointerDown && !pointerMoved) {
handleObjectClick(event);
}
pointerDown = false;
pointerMoved = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = false;
pointerMoved = false;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.007,
5.2,
12
);
updateCamera();
},
{ passive: false }
);
// ------------------------------------------------------------
// Resize and animation
// ------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
// Smooth door movement
doorPivot.rotation.y +=
(doorTargetAngle - doorPivot.rotation.y) *
Math.min(1, delta * 6);
// Floating key on the pedestal
if (!hasKey) {
worldKey.position.y = 1.14 + Math.sin(elapsed * 2.2) * 0.055;
worldKey.rotation.y = 0.1 + Math.sin(elapsed * 1.2) * 0.12;
keyHalo.material.emissiveIntensity =
1.15 + Math.sin(elapsed * 2.4) * 0.25;
} else {
handKey.position.y = -0.42 + Math.sin(elapsed * 2.1) * 0.018;
handKey.rotation.z = -0.25 + Math.sin(elapsed * 1.6) * 0.025;
}
// Gentle pulsing indicator
indicatorMaterial.emissiveIntensity =
lockState === "UNLOCKED"
? 2.0 + Math.sin(elapsed * 4) * 0.45
: 1.8 + Math.sin(elapsed * 4) * 0.35;
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```