Prompt specification
Build or customize the Three.js 3D scene "One Consumable Key Two Doors" 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: One Consumable Key Two Doors
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/one-consumable-key-two-doors-x3506
Scene Description Prompt:
Make a voxel Three.js scene in a self-contained HTML file using cdnjs: one consumable key two doors. Create a tabletop puzzle with one explicitly consumable key and two doors. Show the consequence of choosing one door and allow reset to inspect the alternative.
Working Three.js Starter (one-consumable-key-two-doors-x3506):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Threshold — One Key, Two Doors</title>
<style>
:root {
--ink: #eef5e8;
--muted: #a9b5aa;
--panel: rgba(17, 24, 26, 0.82);
--line: rgba(255, 255, 255, 0.12);
--gold: #ffc857;
--green: #83e6a4;
--red: #ff7b72;
}
* {
box-sizing: border-box;
}
[hidden] {
display: none !important;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #11191e;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#viewport {
position: fixed;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 44%, transparent 42%, rgba(4, 8, 10, 0.38) 100%),
linear-gradient(to bottom, rgba(4, 8, 10, 0.18), transparent 22%);
}
.glass {
background: var(--panel);
border: 1px solid var(--line);
box-shadow:
0 20px 60px rgba(0, 0, 0, 0.28),
inset 0 1px 0 rgba(255, 255, 255, 0.04);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#topbar {
position: fixed;
z-index: 10;
top: 22px;
left: 22px;
right: 22px;
min-height: 58px;
padding: 10px 12px 10px 18px;
border-radius: 16px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
}
.brand {
display: flex;
align-items: center;
gap: 11px;
font-size: 12px;
font-weight: 900;
letter-spacing: 0.16em;
}
.brand-mark {
width: 20px;
height: 20px;
background:
linear-gradient(90deg, var(--gold) 0 50%, transparent 50%),
linear-gradient(0deg, var(--gold) 0 50%, transparent 50%);
box-shadow:
7px 7px 0 rgba(255, 200, 87, 0.28),
-7px -7px 0 rgba(255, 200, 87, 0.18);
transform: rotate(45deg);
}
.top-actions,
.inventory {
display: flex;
align-items: center;
gap: 12px;
}
.inventory {
min-height: 36px;
padding: 7px 12px;
border: 1px solid rgba(255, 200, 87, 0.28);
border-radius: 10px;
color: var(--gold);
background: rgba(255, 200, 87, 0.07);
font-size: 11px;
font-weight: 900;
letter-spacing: 0.11em;
transition: 0.25s ease;
}
.inventory.spent {
color: var(--red);
border-color: rgba(255, 123, 114, 0.3);
background: rgba(255, 123, 114, 0.07);
}
.key-glyph {
position: relative;
width: 24px;
height: 10px;
background: currentColor;
box-shadow: -7px -4px 0 -2px currentColor, -7px 4px 0 -2px currentColor;
}
.key-glyph::after {
content: "";
position: absolute;
right: 2px;
bottom: -5px;
width: 4px;
height: 5px;
background: currentColor;
box-shadow: 6px 0 0 currentColor;
}
.quiet-button {
border: 1px solid var(--line);
border-radius: 9px;
padding: 9px 13px;
color: var(--ink);
background: rgba(255, 255, 255, 0.05);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.12em;
cursor: pointer;
transition: 0.2s ease;
}
.quiet-button:hover:not(:disabled) {
background: rgba(255, 255, 255, 0.1);
border-color: rgba(255, 255, 255, 0.24);
}
button:disabled {
cursor: not-allowed;
opacity: 0.38;
}
#mission {
position: fixed;
z-index: 8;
top: 102px;
left: 22px;
width: min(330px, calc(100vw - 44px));
padding: 24px;
border-radius: 20px;
}
.eyebrow {
color: var(--gold);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.17em;
}
h1 {
margin: 12px 0 12px;
font-size: clamp(28px, 3vw, 42px);
line-height: 0.98;
letter-spacing: -0.055em;
}
.description {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.65;
}
.rule {
height: 1px;
margin: 20px 0;
background: var(--line);
}
.state-row {
display: flex;
justify-content: space-between;
gap: 16px;
padding: 7px 0;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
}
.state-row span:first-child {
color: var(--muted);
}
.state-row strong {
color: var(--ink);
text-align: right;
}
.warning {
display: flex;
gap: 10px;
margin-top: 18px;
padding: 12px;
border: 1px solid rgba(255, 200, 87, 0.2);
border-radius: 12px;
color: #e8d9b9;
background: rgba(255, 200, 87, 0.055);
font-size: 11px;
line-height: 1.5;
}
.warning-dot {
flex: 0 0 auto;
width: 8px;
height: 8px;
margin-top: 4px;
border-radius: 50%;
background: var(--gold);
box-shadow: 0 0 14px var(--gold);
}
#outcome {
position: fixed;
z-index: 12;
top: 102px;
right: 22px;
width: min(330px, calc(100vw - 44px));
padding: 22px;
border-radius: 20px;
border-top: 3px solid var(--gold);
animation: arrive 0.45s cubic-bezier(0.2, 0.8, 0.2, 1);
}
#outcome.success {
border-top-color: var(--green);
}
#outcome.danger {
border-top-color: var(--red);
}
@keyframes arrive {
from {
opacity: 0;
transform: translateY(-12px) scale(0.97);
}
to {
opacity: 1;
transform: translateY(0) scale(1);
}
}
#outcomeTitle {
margin: 8px 0 10px;
font-size: 28px;
line-height: 1;
letter-spacing: -0.04em;
}
#outcomeText {
color: var(--muted);
font-size: 13px;
line-height: 1.6;
}
#resetOutcome {
width: 100%;
margin-top: 18px;
border: 0;
border-radius: 11px;
padding: 13px;
color: #171614;
background: var(--gold);
font-size: 11px;
font-weight: 900;
letter-spacing: 0.08em;
cursor: pointer;
transition: 0.2s ease;
}
#resetOutcome:hover {
filter: brightness(1.08);
transform: translateY(-1px);
}
#controls {
position: fixed;
z-index: 11;
left: 50%;
bottom: 22px;
width: min(560px, calc(100vw - 44px));
padding: 14px;
border-radius: 18px;
transform: translateX(-50%);
text-align: center;
}
#instruction {
margin-bottom: 11px;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
#doorActions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
}
.door-button {
position: relative;
min-height: 58px;
overflow: hidden;
border: 1px solid rgba(255, 200, 87, 0.3);
border-radius: 12px;
color: var(--ink);
background:
linear-gradient(135deg, rgba(255, 200, 87, 0.15), rgba(255, 200, 87, 0.045));
cursor: pointer;
transition:
transform 0.18s ease,
border-color 0.18s ease,
background 0.18s ease;
}
.door-button:hover:not(:disabled) {
transform: translateY(-2px);
border-color: var(--gold);
background:
linear-gradient(135deg, rgba(255, 200, 87, 0.25), rgba(255, 200, 87, 0.08));
}
.door-button span {
display: block;
font-size: 13px;
font-weight: 950;
letter-spacing: 0.12em;
}
.door-button small {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
}
.camera-hint {
margin-top: 10px;
color: #78847d;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.07em;
}
#toast {
position: fixed;
z-index: 20;
left: 50%;
top: 24%;
padding: 11px 16px;
border: 1px solid rgba(255, 200, 87, 0.35);
border-radius: 999px;
color: var(--gold);
background: rgba(17, 24, 26, 0.9);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.12em;
opacity: 0;
pointer-events: none;
transform: translate(-50%, 8px);
transition: 0.25s ease;
}
#toast.visible {
opacity: 1;
transform: translate(-50%, 0);
}
#loading {
position: fixed;
z-index: 100;
inset: 0;
display: grid;
place-items: center;
color: var(--gold);
background: #11191e;
font-size: 11px;
font-weight: 900;
letter-spacing: 0.18em;
transition: opacity 0.5s ease;
}
#loading.done {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
#mission {
top: 92px;
width: 230px;
padding: 17px;
}
#mission h1 {
font-size: 26px;
}
#mission .description,
#mission .rule,
#mission .warning {
display: none;
}
#outcome {
top: 92px;
width: 250px;
padding: 17px;
}
#controls {
bottom: 12px;
}
.brand {
font-size: 10px;
}
.inventory {
padding: 7px 9px;
}
}
@media (max-width: 520px) {
#mission {
display: none;
}
#outcome {
top: 90px;
right: 12px;
width: calc(100vw - 24px);
}
#topbar {
top: 12px;
left: 12px;
right: 12px;
}
#controls {
width: calc(100vw - 24px);
bottom: 12px;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<div class="vignette"></div>
<header id="topbar" class="glass">
<div class="brand">
<span class="brand-mark"></span>
VOXEL THRESHOLD
</div>
<div class="top-actions">
<div id="inventoryBadge" class="inventory">
<span class="key-glyph"></span>
<span>KEY <b id="keyCount">1</b>/1</span>
</div>
<button id="resetTop" class="quiet-button" type="button" disabled>
RESET
</button>
</div>
</header>
<section id="mission" class="glass">
<div class="eyebrow">TABLETOP PUZZLE · 01</div>
<h1>One key.<br />Two possible futures.</h1>
<p class="description">
Choose either door. The gold key is consumed the moment it is used,
whether the choice succeeds or fails.
</p>
<div class="rule"></div>
<div class="state-row">
<span>KEY STATUS</span>
<strong id="keyState">READY · SINGLE USE</strong>
</div>
<div class="state-row">
<span>DOOR A</span>
<strong id="doorAState">UNOPENED</strong>
</div>
<div class="state-row">
<span>DOOR B</span>
<strong id="doorBState">UNOPENED</strong>
</div>
<div class="warning">
<span class="warning-dot"></span>
<span>Reset restores the key and lets you inspect the alternative door.</span>
</div>
</section>
<section id="outcome" class="glass" role="status" aria-live="polite" hidden>
<div id="outcomeTag" class="eyebrow">RESULT</div>
<h2 id="outcomeTitle">Treasure chamber</h2>
<p id="outcomeText"></p>
<button id="resetOutcome" type="button">RESET & INSPECT OTHER DOOR</button>
</section>
<div id="controls" class="glass">
<div id="instruction">Use your one key to open a door.</div>
<div id="doorActions">
<button id="doorA" class="door-button" type="button">
<span>DOOR A</span>
<small>USE THE KEY</small>
</button>
<button id="doorB" class="door-button" type="button">
<span>DOOR B</span>
<small>USE THE KEY</small>
</button>
</div>
<div class="camera-hint">
DRAG TO ORBIT · SCROLL TO ZOOM · CLICK A DOOR · 1 / 2 TO CHOOSE · R TO RESET
</div>
</div>
<div id="toast">KEY RESTORED</div>
<div id="loading">BUILDING VOXEL TABLE…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
const viewport = $("viewport");
// -------------------------------------------------------------------------
// Renderer, scene, and camera
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x11191e);
scene.fog = new THREE.Fog(0x11191e, 18, 32);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: false,
powerPreference: "high-performance"
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.18;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
viewport.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xb9d7e8, 0x40342b, 1.65);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffedcf, 3.1);
sun.position.set(7, 12, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.00035;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x73a8ff, 1.1);
rimLight.position.set(-8, 6, -7);
scene.add(rimLight);
const cameraTarget = new THREE.Vector3(0, 1.65, -0.7);
let cameraTheta = 0;
let cameraPhi = 1.02;
let cameraRadius = 15.4;
function updateCamera() {
cameraPhi = THREE.MathUtils.clamp(cameraPhi, 0.56, 1.34);
cameraRadius = THREE.MathUtils.clamp(cameraRadius, 10.5, 22);
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
// -------------------------------------------------------------------------
// Voxel construction helpers
// -------------------------------------------------------------------------
const materialCache = new Map();
function material(color, options = {}) {
if (color && color.isMaterial) return color;
const key = `${color}-${JSON.stringify(options)}`;
if (!materialCache.has(key)) {
materialCache.set(
key,
new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0,
...options
})
);
}
return materialCache.get(key);
}
function box(
width,
height,
depth,
color,
x = 0,
y = 0,
z = 0,
parent = scene,
options = {}
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material(color, options)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function group(x = 0, y = 0, z = 0, parent = scene) {
const result = new THREE.Group();
result.position.set(x, y, z);
parent.add(result);
return result;
}
const COLORS = {
table: 0x4a3428,
tableEdge: 0x211c19,
floorA: 0x61706b,
floorB: 0x56645f,
floorC: 0x6b7771,
wall: 0x65706b,
wallDark: 0x48534f,
frame: 0x303a38,
frameLight: 0x7c877f,
wood: 0x6b4933,
woodLight: 0x80573c,
woodDark: 0x503525,
gold: 0xffc857,
goldDark: 0xc68a27,
red: 0xff554d,
redDark: 0x8f2829,
cream: 0xefe6c7
};
// -------------------------------------------------------------------------
// Tabletop and tiled floor
// -------------------------------------------------------------------------
box(
13.6,
1.05,
10.5,
COLORS.table,
0,
-0.63,
0
);
box(
14.0,
0.22,
10.9,
COLORS.tableEdge,
0,
-1.15,
0
);
box(
13.1,
0.18,
10.0,
COLORS.wallDark,
0,
-0.03,
0
);
const floorColors = [COLORS.floorA, COLORS.floorB, COLORS.floorC];
for (let row = 0; row < 8; row++) {
for (let col = 0; col < 12; col++) {
const variation = (col * 7 + row * 13) % floorColors.length;
box(
1.02,
0.17,
1.02,
floorColors[variation],
-5.5 + col,
0.08,
-3.5 + row
);
}
}
// Table corner blocks.
for (const x of [-6.45, 6.45]) {
for (const z of [-4.8, 4.8]) {
box(0.45, 0.5, 0.45, COLORS.tableEdge, x, 0.16, z);
box(0.18, 0.18, 0.18, COLORS.goldDark, x, 0.49, z);
}
}
// -------------------------------------------------------------------------
// Rear wall with two real openings
// -------------------------------------------------------------------------
const wallZ = -4.25;
const wallBottom = 0.12;
const wallTop = 5.4;
function wallBlock(width, x, bottom, height) {
box(
width,
height,
0.46,
COLORS.wall,
x,
bottom + height / 2,
wallZ
);
}
// Left edge, center divider, right edge.
wallBlock(1.95, -5.425, wallBottom, wallTop - wallBottom);
wallBlock(3.5, 0, wallBottom, wallTop - wallBottom);
wallBlock(1.95, 5.425, wallBottom, wallTop - wallBottom);
// Lintels above both door openings.
wallBlock(2.7, -3.1, 3.9, 1.5);
wallBlock(2.7, 3.1, 3.9, 1.5);
// Block seams and cap stones.
for (let y = 1.1; y < 5.2; y += 1.05) {
for (const x of [-5.4, -1.5, 0, 1.5, 5.4]) {
box(
0.08,
0.75,
0.04,
COLORS.wallDark,
x,
y,
wallZ + 0.245
);
}
}
box(13.0, 0.25, 0.58, COLORS.wallDark, 0, 5.48, wallZ);
box(13.2, 0.12, 0.66, COLORS.frameLight, 0, 5.65, wallZ);
// -------------------------------------------------------------------------
// Doors, frames, pixel labels, and raycast targets
// -------------------------------------------------------------------------
const doorAnimations = [];
const doorHitTargets = [];
const LETTER_PATTERNS = {
A: [
"01110",
"10001",
"10001",
"11111",
"10001",
"10001",
"10001"
],
B: [
"11110",
"10001",
"10001",
"11110",
"10001",
"10001",
"11110"
]
};
function addPixelLetter(letter, x, y, z) {
box(1.08, 1.18, 0.24, COLORS.frame, x, y, z);
const pattern = LETTER_PATTERNS[letter];
const cell = 0.115;
for (let row = 0; row < pattern.length; row++) {
for (let col = 0; col < pattern[row].length; col++) {
if (pattern[row][col] !== "1") continue;
box(
cell * 0.86,
cell * 0.86,
0.08,
COLORS.gold,
x + (col - 2) * cell,
y + (3 - row) * cell,
z + 0.16,
scene,
{
emissive: 0x7b4b08,
emissiveIntensity: 0.45
}
);
}
}
}
function createDoor(index, x, letter) {
const doorWidth = 2.45;
const doorHeight = 3.65;
const pivot = group(x - doorWidth / 2, 1.95, -3.67);
box(
doorWidth,
doorHeight,
0.26,
COLORS.wood,
doorWidth / 2,
0,
0,
pivot
);
for (let plank = 0; plank < 5; plank++) {
box(
0.43,
3.38,
0.075,
plank % 2 === 0 ? COLORS.woodLight : COLORS.wood,
0.245 + plank * 0.49,
0,
0.17,
pivot
);
}
box(
0.14,
2.9,
0.1,
COLORS.woodDark,
doorWidth / 2,
0,
0.225,
pivot
);
box(
0.18,
0.48,
0.18,
COLORS.gold,
doorWidth - 0.34,
0,
0.31,
pivot,
{
metalness: 0.65,
roughness: 0.25,
emissive: 0x5c3907,
emissiveIntensity: 0.3
}
);
// Stone frame.
box(0.32, 4.05, 0.5, COLORS.frame, x - 1.35, 2.03, -3.82);
box(0.32, 4.05, 0.5, COLORS.frame, x + 1.35, 2.03, -3.82);
box(3.02, 0.32, 0.5, COLORS.frame, x, 3.9, -3.82);
box(0.1, 3.7, 0.08, COLORS.frameLight, x - 1.17, 1.95, -3.53);
box(0.1, 3.7, 0.08, COLORS.frameLight, x + 1.17, 1.95, -3.53);
addPixelLetter(letter, x, 4.58, -3.88);
// Invisible, stable target so either the visible door or the UI can be used.
const hitMaterial = new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
});
const hitTarget = new THREE.Mesh(
new THREE.BoxGeometry(2.55, 3.8, 0.6),
hitMaterial
);
hitTarget.position.set(x, 1.95, -3.45);
hitTarget.userData.doorIndex = index;
scene.add(hitTarget);
doorHitTargets.push(hitTarget);
doorAnimations.push({
pivot,
amount: 0,
target: 0,
baseY: pivot.position.y
});
}
createDoor(0, -3.1, "A");
createDoor(1, 3.1, "B");
// -------------------------------------------------------------------------
// One and only one key
// -------------------------------------------------------------------------
const pedestal = group(0, 0, 0.75);
box(1.9, 0.27, 1.3, COLORS.tableEdge, 0, 0.28, 0, pedestal);
box(1.55, 0.16, 1.0, COLORS.frame, 0, 0.49, 0, pedestal);
box(1.22, 0.07, 0.72, COLORS.goldDark, 0, 0.61, 0, pedestal);
const keyGroup = group(0, 0.82, 0.75);
const keyVisual = group(0, 0, 0, keyGroup);
const keyMaterial = new THREE.MeshStandardMaterial({
color: COLORS.gold,
metalness: 0.72,
roughness: 0.22,
emissive: 0x6f4307,
emissiveIntensity: 0.38
});
// Shaft.
box(1.45, 0.18, 0.23, keyMaterial, 0.28, 0, 0, keyVisual);
// Square bow.
box(0.17, 0.18, 0.29, keyMaterial, -0.72, 0.18, 0, keyVisual);
box(0.17, 0.18, 0.29, keyMaterial, -0.72, -0.18, 0, keyVisual);
box(0.17, 0.52, 0.29, keyMaterial, -0.88, 0, 0, keyVisual);
box(0.17, 0.52, 0.29, keyMaterial, -0.55, 0, 0, keyVisual);
// Teeth.
box(0.18, 0.28, 0.25, keyMaterial, 0.67, -0.15, 0, keyVisual);
box(0.18, 0.28, 0.25, keyMaterial, 0.98, -0.15, 0, keyVisual);
box(0.18, 0.18, 0.25, keyMaterial, 0.98, 0.13, 0, keyVisual);
const keyLight = new THREE.PointLight(0xffbf4d, 1.1, 4.5, 2);
keyLight.position.set(0, 0.8, 0.2);
keyGroup.add(keyLight);
// Small particle cubes used to make the consumption unmistakable.
const keyParticles = [];
const particleMaterial = new THREE.MeshStandardMaterial({
color: COLORS.gold,
emissive: 0x9a5b08,
emissiveIntensity: 0.8,
metalness: 0.4,
roughness: 0.3
});
for (let i = 0; i < 18; i++) {
const size = 0.035 + Math.random() * 0.07;
const particle = box(
size,
size,
size,
particleMaterial,
0,
0,
0,
scene,
{ emissive: 0x9a5b08, emissiveIntensity: 0.8 }
);
particle.visible = false;
keyParticles.push({
mesh: particle,
velocity: new THREE.Vector3(),
start: new THREE.Vector3()
});
}
// -------------------------------------------------------------------------
// Outcomes behind the doors
// -------------------------------------------------------------------------
function createTreasure(x) {
const root = group(x, 0.18, -4.72);
root.visible = false;
box(1.65, 0.16, 1.25, COLORS.frame, 0, 0.08, 0, root);
box(1.48, 0.72, 1.0, COLORS.wood, 0, 0.48, 0, root);
box(1.5, 0.09, 1.02, COLORS.goldDark, 0, 0.48, 0.515, root);
for (const bandX of [-0.48, 0.48]) {
box(0.14, 0.77, 1.04, COLORS.gold, bandX, 0.48, 0, root);
}
const lid = group(0, 0.85, 0, root);
box(1.53, 0.27, 1.04, COLORS.woodLight, 0, 0, 0, lid);
box(1.55, 0.08, 1.06, COLORS.goldDark, 0, 0.02, 0, lid);
for (const bandX of [-0.48, 0.48]) {
box(0.14, 0.29, 1.07, COLORS.gold, bandX, 0, 0, lid);
}
box(0.25, 0.27, 0.16, COLORS.gold, 0, -0.13, 0.58, lid, {
metalness: 0.7,
roughness: 0.2
});
const bits = [];
for (let i = 0; i < 7; i++) {
const bit = box(
0.16,
0.16,
0.16,
COLORS.gold,
(i - 3) * 0.34,
1.65 + Math.random() * 0.7,
Math.sin(i * 1.7) * 0.25,
root,
{
emissive: 0x9b5a08,
emissiveIntensity: 0.8,
metalness: 0.5,
roughness: 0.25
}
);
bits.push({ mesh: bit, baseY: bit.position.y });
}
const light = new THREE.PointLight(0xffc44d, 0, 5.5, 2);
light.position.set(0, 2, 0.3);
root.add(light);
return { kind: "safe", root, lid, bits, light };
}
function createTrap(x) {
const root = group(x, 0.18, -4.72);
root.visible = false;
box(1.85, 0.14, 1.35, COLORS.frame, 0, 0.07, 0, root);
const spikes = [];
const redMaterial = new THREE.MeshStandardMaterial({
color: COLORS.red,
emissive: 0x7b1618,
emissiveIntensity: 0.75,
roughness: 0.48,
metalness: 0.18
});
for (let i = 0; i < 5; i++) {
const spike = group((i - 2) * 0.36, 0.12, 0, root);
box(0.36, 0.25, 0.4, COLORS.redDark, 0, 0.12, 0, spike);
box(0.27, 0.42, 0.31, redMaterial, 0, 0.43, 0, spike);
box(0.17, 0.35, 0.22, redMaterial, 0, 0.79, 0, spike);
spikes.push({ group: spike, baseY: 0.12, phase: i * 0.85 });
}
// Warning cage around the hazard.
for (const cageX of [-0.95, 0.95]) {
box(0.12, 2.3, 0.12, COLORS.frame, cageX, 1.15, -0.42, root);
box(0.12, 2.3, 0.12, COLORS.frame, cageX, 1.15, 0.42, root);
}
box(2.05, 0.12, 0.12, COLORS.frame, 0, 2.3, -0.42, root);
box(2.05, 0.12, 0.12, COLORS.frame, 0, 2.3, 0.42, root);
const light = new THREE.PointLight(0xff3029, 0, 6, 2);
light.position.set(0, 1.4, 0.2);
root.add(light);
return { kind: "danger", root, spikes, light };
}
// One chamber is safe and the other is trapped. The mapping stays fixed
// through resets, so the other door can genuinely be inspected afterward.
const secretDoor = Math.random() < 0.5 ? 0 : 1;
const outcomeVisuals = [
secretDoor === 0 ? createTreasure(-3.1) : createTrap(-3.1),
secretDoor === 1 ? createTreasure(3.1) : createTrap(3.1)
];
// -------------------------------------------------------------------------
// Puzzle state and UI
// -------------------------------------------------------------------------
let puzzleState = "ready"; // ready -> resolving -> resolved
let chosenDoor = -1;
let keyConsumed = false;
let consumeElapsed = 0;
let resolveTimer = 0;
let outcomeShown = false;
let toastTimer = 0;
const doorAButton = $("doorA");
const doorBButton = $("doorB");
const resetTopButton = $("resetTop");
const outcomePanel = $("outcome");
function setDoorButtonsDisabled(disabled) {
doorAButton.disabled = disabled;
doorBButton.disabled = disabled;
}
function showToast(text) {
$("toast").textContent = text;
$("toast").classList.add("visible");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
$("toast").classList.remove("visible");
}, 1300);
}
function consumeKey() {
keyConsumed = true;
consumeElapsed = 0;
const start = keyGroup.position.clone();
start.y += 0.1;
for (const particle of keyParticles) {
particle.mesh.visible = true;
particle.mesh.scale.setScalar(1);
particle.start.copy(start);
particle.start.x += (Math.random() - 0.5) * 0.75;
particle.start.y += (Math.random() - 0.5) * 0.25;
particle.start.z += (Math.random() - 0.5) * 0.45;
particle.velocity.set(
(Math.random() - 0.5) * 2.7,
0.7 + Math.random() * 1.8,
(Math.random() - 0.5) * 2.7
);
}
}
function chooseDoor(index) {
if (puzzleState !== "ready" || keyConsumed) return;
chosenDoor = index;
puzzleState = "resolving";
outcomeShown = false;
resolveTimer = 1.1;
consumeKey();
setDoorButtonsDisabled(true);
doorAnimations[index].target = 1;
const visual = outcomeVisuals[index];
visual.root.visible = true;
visual.root.scale.setScalar(0.01);
$("keyCount").textContent = "0";
$("keyState").textContent = "CONSUMED · 0/1";
$("inventoryBadge").classList.add("spent");
$("instruction").textContent =
"The key has dissolved. The door is opening…";
$("doorAState").textContent = index === 0 ? "OPENING…" : "LOCKED";
$("doorBState").textContent = index === 1 ? "OPENING…" : "LOCKED";
}
function revealOutcome() {
if (outcomeShown) return;
outcomeShown = true;
puzzleState = "resolved";
resetTopButton.disabled = false;
const safe = chosenDoor === secretDoor;
const selectedName = chosenDoor === 0 ? "A" : "B";
const otherName = chosenDoor === 0 ? "B" : "A";
outcomePanel.hidden = false;
outcomePanel.classList.toggle("success", safe);
outcomePanel.classList.toggle("danger", !safe);
if (safe) {
$("outcomeTag").textContent = "SAFE CHOICE · DOOR " + selectedName;
$("outcomeTitle").textContent = "Treasure chamber";
$("outcomeText").textContent =
`Door ${selectedName} revealed the voxel treasure. The key is spent, ` +
`but you can reset the table and inspect Door ${otherName}.`;
$("doorAState").textContent = chosenDoor === 0 ? "OPEN · TREASURE" : "LOCKED";
$("doorBState").textContent = chosenDoor === 1 ? "OPEN · TREASURE" : "LOCKED";
$("instruction").textContent = "The key is gone. Reset to try the other door.";
} else {
$("outcomeTag").textContent = "DANGER · DOOR " + selectedName;
$("outcomeTitle").textContent = "Trap chamber";
$("outcomeText").textContent =
`Door ${selectedName} triggered the voxel trap. The key was still ` +
`consumed. Reset restores it so you can inspect Door ${otherName}.`;
$("doorAState").textContent = chosenDoor === 0 ? "OPEN · TRAPPED" : "LOCKED";
$("doorBState").textContent = chosenDoor === 1 ? "OPEN · TRAPPED" : "LOCKED";
$("instruction").textContent = "The trap is armed and the key is gone.";
}
}
function resetPuzzle() {
puzzleState = "ready";
chosenDoor = -1;
keyConsumed = false;
consumeElapsed = 0;
resolveTimer = 0;
outcomeShown = false;
setDoorButtonsDisabled(false);
resetTopButton.disabled = true;
outcomePanel.hidden = true;
keyGroup.visible = true;
keyVisual.visible = true;
keyVisual.scale.setScalar(1);
keyVisual.position.set(0, 0, 0);
keyVisual.rotation.set(0, 0, 0);
for (const particle of keyParticles) {
particle.mesh.visible = false;
}
for (const door of doorAnimations) {
door.amount = 0;
door.target = 0;
door.pivot.rotation.y = 0;
}
for (const visual of outcomeVisuals) {
visual.root.visible = false;
visual.root.scale.setScalar(1);
visual.light.intensity = 0;
if (visual.kind === "safe") {
visual.lid.rotation.z = 0;
visual.lid.position.y = 0.85;
}
}
$("keyCount").textContent = "1";
$("keyState").textContent = "READY · SINGLE USE";
$("inventoryBadge").classList.remove("spent");
$("doorAState").textContent = "UNOPENED";
$("doorBState").textContent = "UNOPENED";
$("instruction").textContent = "Use your one key to open a door.";
showToast("KEY RESTORED");
}
doorAButton.addEventListener("click", () => chooseDoor(0));
doorBButton.addEventListener("click", () => chooseDoor(1));
$("resetOutcome").addEventListener("click", resetPuzzle);
resetTopButton.addEventListener("click", resetPuzzle);
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, and click-to-choose
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerDown = false;
let pointerMoved = false;
let pointerStartX = 0;
let pointerStartY = 0;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointerDown = true;
pointerMoved = false;
pointerStartX = previousX = event.clientX;
pointerStartY = previousY = 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 - pointerStartX) +
Math.abs(event.clientY - pointerStartY) >
5
) {
pointerMoved = true;
}
cameraTheta -= dx * 0.006;
cameraPhi -= dy * 0.005;
previousX = event.clientX;
previousY = event.clientY;
updateCamera();
});
renderer.domElement.addEventListener("pointerup", (event) => {
pointerDown = false;
if (pointerMoved) return;
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(doorHitTargets, false);
if (hits.length) {
chooseDoor(hits[0].object.userData.doorIndex);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = false;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius += event.deltaY * 0.009;
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.key === "1") chooseDoor(0);
if (event.key === "2") chooseDoor(1);
if (event.key.toLowerCase() === "r") resetPuzzle();
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
elapsed += dt;
// Key idle motion.
if (!keyConsumed) {
keyVisual.position.y = Math.sin(elapsed * 2.25) * 0.065;
keyVisual.rotation.y = -0.16 + Math.sin(elapsed * 0.85) * 0.07;
keyVisual.rotation.z = Math.sin(elapsed * 1.4) * 0.025;
keyLight.intensity = 1 + Math.sin(elapsed * 3) * 0.16;
} else {
consumeElapsed += dt;
const disappear = Math.min(1, consumeElapsed / 0.46);
const scale = Math.max(0.001, 1 - disappear);
keyVisual.scale.setScalar(scale);
keyVisual.position.y = disappear * 0.65;
keyVisual.rotation.y += dt * 7;
keyVisual.rotation.z += dt * 5;
if (consumeElapsed >= 0.48) {
keyVisual.visible = false;
}
for (const particle of keyParticles) {
const t = consumeElapsed;
particle.mesh.position.copy(particle.start).addScaledVector(particle.velocity, t);
particle.mesh.position.y -= 2.3 * t * t;
particle.mesh.rotation.x += dt * 7;
particle.mesh.rotation.y += dt * 8;
particle.mesh.scale.setScalar(Math.max(0.001, 1 - t / 0.72));
if (t >= 0.72) {
particle.mesh.visible = false;
}
}
}
// Doors ease open around their hinges.
for (const door of doorAnimations) {
const speed = 1 - Math.exp(-dt * 4.2);
door.amount += (door.target - door.amount) * speed;
const eased = 1 - Math.pow(1 - door.amount, 3);
door.pivot.rotation.y = -eased * 1.48;
}
// Reveal and animate the selected chamber.
if (chosenDoor >= 0) {
const visual = outcomeVisuals[chosenDoor];
const amount = doorAnimations[chosenDoor].amount;
visual.root.scale.setScalar(Math.max(0.01, 0.18 + amount * 0.82));
if (visual.kind === "safe") {
visual.light.intensity = amount * 5.5;
visual.lid.rotation.z = -amount * 0.5;
visual.lid.position.y = 0.85 + amount * 0.32;
visual.bits.forEach((bit, i) => {
bit.mesh.position.y =
bit.baseY + Math.sin(elapsed * 2.2 + i * 0.8) * 0.13;
bit.mesh.rotation.x += dt * 0.8;
bit.mesh.rotation.y += dt * 1.1;
});
} else {
visual.light.intensity =
amount * (4.8 + Math.sin(elapsed * 7) * 1.4);
visual.spikes.forEach((spike) => {
spike.group.position.y =
spike.baseY + Math.max(0, Math.sin(elapsed * 5 + spike.phase)) * 0.13;
});
}
}
if (puzzleState === "resolving") {
resolveTimer -= dt;
if (resolveTimer <= 0) revealOutcome();
}
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
animate();
requestAnimationFrame(() => {
$("loading").classList.add("done");
setTimeout(() => $("loading").remove(), 600);
});
</script>
</body>
</html>
```