Prompt specification
Build or customize the Three.js 3D scene "Room-sharing layout optimization" 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: Room-sharing layout optimization
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/room-sharing-layout-optimization-x0168
Scene Description Prompt:
Make a voxel bedroom puzzle with two beds and storage drawers that must fully open without blocking access. One HTML file using Three.js from cdnjs.
Working Three.js Starter (room-sharing-layout-optimization-x0168):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<title>Room to Breathe — Voxel Bedroom Puzzle</title>
<style>
:root {
--ink: #19302b;
--muted: #66756f;
--paper: #f7f4eb;
--cream: #eee9dd;
--teal: #48a995;
--coral: #ef6b55;
--yellow: #efc75e;
--blue: #78a9c7;
--line: rgba(25, 48, 43, 0.12);
--shadow: 0 18px 50px rgba(38, 51, 46, 0.14);
}
* { box-sizing: border-box; }
html, body {
margin: 0;
width: 100%;
height: 100%;
overflow: hidden;
color: var(--ink);
background: var(--cream);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button { font: inherit; }
#app {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
}
.topbar {
height: 72px;
flex: 0 0 72px;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 25px;
background: var(--paper);
border-bottom: 1px solid var(--line);
z-index: 5;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
}
.voxel-logo {
width: 35px;
height: 35px;
position: relative;
transform: rotate(-4deg);
}
.voxel-logo i {
position: absolute;
display: block;
border-radius: 2px;
box-shadow: inset -2px -2px 0 rgba(0,0,0,.08);
}
.voxel-logo i:nth-child(1) { width: 15px; height: 15px; left: 1px; top: 1px; background: var(--coral); }
.voxel-logo i:nth-child(2) { width: 16px; height: 16px; right: 1px; top: 0; background: var(--yellow); }
.voxel-logo i:nth-child(3) { width: 16px; height: 16px; left: 0; bottom: 1px; background: var(--teal); }
.voxel-logo i:nth-child(4) { width: 13px; height: 13px; right: 3px; bottom: 1px; background: var(--blue); }
.brand h1 {
margin: 0;
font-size: 17px;
line-height: 1.05;
letter-spacing: .08em;
text-transform: uppercase;
}
.brand p {
margin: 5px 0 0;
color: var(--muted);
font-size: 10px;
letter-spacing: .19em;
text-transform: uppercase;
font-weight: 700;
}
.chapter {
display: flex;
align-items: center;
gap: 10px;
color: var(--muted);
font-size: 11px;
letter-spacing: .16em;
font-weight: 800;
text-transform: uppercase;
}
.chapter b {
display: inline-grid;
place-items: center;
width: 27px;
height: 27px;
color: white;
background: var(--ink);
border-radius: 7px;
letter-spacing: 0;
}
.header-meta {
display: flex;
gap: 9px;
}
.meta-pill {
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 999px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: .09em;
text-transform: uppercase;
background: rgba(255,255,255,.45);
}
#stage {
position: relative;
flex: 1;
min-height: 0;
overflow: hidden;
background:
radial-gradient(circle at 48% 42%, rgba(255,255,255,.85), transparent 47%),
#e9e6dc;
}
#scene {
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene:active { cursor: grabbing; }
.vignette {
position: absolute;
inset: 0;
pointer-events: none;
box-shadow: inset 0 0 100px rgba(65, 70, 61, .08);
}
.card {
position: absolute;
background: rgba(250, 248, 241, .92);
border: 1px solid rgba(25,48,43,.11);
box-shadow: var(--shadow);
backdrop-filter: blur(15px);
-webkit-backdrop-filter: blur(15px);
}
.mission {
top: 22px;
left: 22px;
width: 286px;
padding: 19px;
border-radius: 18px;
}
.eyebrow {
margin-bottom: 8px;
color: var(--coral);
font-size: 10px;
font-weight: 900;
letter-spacing: .17em;
text-transform: uppercase;
}
.mission h2 {
margin: 0;
font-size: 25px;
line-height: 1.04;
letter-spacing: -.04em;
}
.mission .intro {
margin: 10px 0 16px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.checklist {
display: grid;
gap: 8px;
}
.check {
min-height: 43px;
display: flex;
align-items: center;
gap: 10px;
padding: 9px 10px;
border-radius: 10px;
background: rgba(25,48,43,.045);
transition: .2s ease;
}
.check .dot {
width: 9px;
height: 9px;
flex: 0 0 auto;
border-radius: 50%;
background: #c7cbc4;
box-shadow: 0 0 0 4px rgba(25,48,43,.04);
}
.check span {
font-size: 11px;
line-height: 1.25;
font-weight: 800;
}
.check.pass { background: rgba(72,169,149,.11); }
.check.pass .dot { background: var(--teal); box-shadow: 0 0 0 4px rgba(72,169,149,.12); }
.check.fail .dot { background: var(--coral); box-shadow: 0 0 0 4px rgba(239,107,85,.1); }
.route-toggle {
margin-top: 13px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: .05em;
text-transform: uppercase;
}
.switch {
width: 36px;
height: 21px;
padding: 3px;
border: 0;
border-radius: 999px;
background: #c9cec7;
cursor: pointer;
transition: .2s;
}
.switch::after {
content: "";
display: block;
width: 15px;
height: 15px;
border-radius: 50%;
background: white;
box-shadow: 0 2px 5px rgba(0,0,0,.18);
transition: .2s;
}
.switch.on { background: var(--teal); }
.switch.on::after { transform: translateX(15px); }
.inspector {
top: 22px;
right: 22px;
width: 246px;
padding: 17px;
border-radius: 18px;
}
.inspector-top {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 13px;
}
.inspector-top span {
color: var(--muted);
font-size: 9px;
font-weight: 900;
letter-spacing: .16em;
text-transform: uppercase;
}
#selectedName {
margin: 0 0 5px;
font-size: 19px;
letter-spacing: -.025em;
}
#selectedMeta {
min-height: 31px;
margin: 0 0 14px;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.action-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.action-button {
min-height: 39px;
border: 1px solid var(--line);
border-radius: 10px;
color: var(--ink);
background: rgba(255,255,255,.58);
cursor: pointer;
font-size: 10px;
font-weight: 850;
letter-spacing: .025em;
transition: .15s ease;
}
.action-button:hover:not(:disabled) {
transform: translateY(-1px);
border-color: rgba(25,48,43,.25);
background: white;
}
.action-button.primary {
color: white;
border-color: var(--ink);
background: var(--ink);
}
.action-button:disabled {
cursor: not-allowed;
opacity: .34;
}
.divider {
height: 1px;
margin: 15px 0 12px;
background: var(--line);
}
.camera-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 7px;
}
.camera-row button {
width: 37px;
height: 37px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255,255,255,.58);
color: var(--ink);
cursor: pointer;
font-size: 16px;
}
.camera-row button:hover { background: white; }
.camera-label {
flex: 1;
text-align: center;
color: var(--muted);
font-size: 9px;
font-weight: 850;
letter-spacing: .08em;
text-transform: uppercase;
}
.helpbar {
position: absolute;
left: 22px;
bottom: 22px;
display: flex;
align-items: center;
gap: 13px;
padding: 11px 14px;
border-radius: 12px;
color: var(--muted);
background: rgba(250,248,241,.82);
border: 1px solid var(--line);
box-shadow: 0 8px 25px rgba(38,51,46,.08);
font-size: 10px;
font-weight: 700;
pointer-events: none;
}
.helpbar b {
color: var(--ink);
background: rgba(25,48,43,.07);
padding: 4px 6px;
border-radius: 5px;
font-size: 9px;
}
.progress-wrap {
position: absolute;
right: 22px;
bottom: 22px;
display: flex;
align-items: center;
gap: 12px;
padding: 9px 13px 9px 9px;
border-radius: 999px;
color: var(--ink);
background: rgba(250,248,241,.9);
border: 1px solid var(--line);
box-shadow: 0 8px 25px rgba(38,51,46,.08);
}
#progressRing {
--p: 0;
position: relative;
width: 43px;
height: 43px;
display: grid;
place-items: center;
border-radius: 50%;
background: conic-gradient(var(--teal) calc(var(--p) * 1%), #d9ddd6 0);
transition: background .15s;
}
#progressRing::after {
content: "";
position: absolute;
inset: 5px;
border-radius: 50%;
background: var(--paper);
}
#progressRing b {
position: relative;
z-index: 1;
font-size: 11px;
}
.progress-copy strong {
display: block;
font-size: 11px;
}
.progress-copy span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
#toast {
position: absolute;
left: 50%;
bottom: 82px;
max-width: min(430px, calc(100% - 30px));
padding: 12px 17px;
border-radius: 12px;
color: white;
background: rgba(25,48,43,.94);
box-shadow: 0 12px 35px rgba(25,48,43,.2);
font-size: 11px;
font-weight: 750;
text-align: center;
transform: translate(-50%, 16px);
opacity: 0;
pointer-events: none;
transition: .22s ease;
}
#toast.show {
transform: translate(-50%, 0);
opacity: 1;
}
#winModal {
position: absolute;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
padding: 20px;
background: rgba(28,43,39,.38);
backdrop-filter: blur(9px);
-webkit-backdrop-filter: blur(9px);
opacity: 0;
visibility: hidden;
transition: .25s;
}
#winModal.show {
opacity: 1;
visibility: visible;
}
.win-card {
width: min(410px, 100%);
padding: 29px;
border-radius: 22px;
background: var(--paper);
box-shadow: 0 30px 100px rgba(0,0,0,.24);
text-align: center;
transform: translateY(14px) scale(.97);
transition: .3s cubic-bezier(.2,.8,.2,1);
}
#winModal.show .win-card {
transform: translateY(0) scale(1);
}
.win-icon {
width: 66px;
height: 66px;
display: grid;
place-items: center;
margin: 0 auto 17px;
border-radius: 18px;
color: white;
background: var(--teal);
font-size: 31px;
box-shadow: 8px 8px 0 var(--yellow);
}
.win-card h2 {
margin: 0;
font-size: 31px;
letter-spacing: -.045em;
}
.win-card p {
margin: 10px auto 21px;
max-width: 300px;
color: var(--muted);
font-size: 12px;
line-height: 1.6;
}
.win-actions {
display: flex;
justify-content: center;
gap: 9px;
}
.win-actions button {
min-height: 43px;
padding: 0 17px;
border-radius: 11px;
border: 1px solid var(--line);
background: white;
color: var(--ink);
cursor: pointer;
font-size: 11px;
font-weight: 850;
}
.win-actions button.primary {
color: white;
border-color: var(--ink);
background: var(--ink);
}
@media (max-width: 900px) {
.mission { width: 238px; padding: 16px; }
.mission h2 { font-size: 22px; }
.inspector { width: 220px; }
.header-meta .meta-pill:first-child { display: none; }
.helpbar { max-width: 440px; flex-wrap: wrap; gap: 8px; }
}
@media (max-width: 680px) {
html, body { overflow: auto; }
#app { min-height: 100dvh; }
.topbar { height: 62px; flex-basis: 62px; padding: 0 14px; }
.brand h1 { font-size: 14px; }
.brand p { font-size: 8px; }
.chapter { display: none; }
.meta-pill { font-size: 8px; }
#stage { min-height: 720px; height: calc(100dvh - 62px); }
.mission {
top: 12px;
left: 12px;
right: 12px;
width: auto;
padding: 13px;
}
.mission h2 { font-size: 20px; }
.mission .intro { display: none; }
.checklist { grid-template-columns: repeat(3, 1fr); gap: 5px; margin-top: 10px; }
.check { min-height: 51px; padding: 8px 6px; align-items: flex-start; flex-direction: column; gap: 7px; }
.check span { font-size: 9px; }
.route-toggle { margin-top: 9px; }
.inspector {
top: auto;
right: 12px;
bottom: 76px;
width: 205px;
padding: 13px;
}
.inspector-top, .divider { display: none; }
#selectedName { font-size: 16px; }
#selectedMeta { margin-bottom: 9px; }
.camera-row { display: none; }
.helpbar { display: none; }
.progress-wrap { right: auto; left: 12px; bottom: 12px; }
#toast { bottom: 22px; }
}
</style>
</head>
<body>
<div id="app">
<header class="topbar">
<div class="brand">
<div class="voxel-logo" aria-hidden="true"><i></i><i></i><i></i><i></i></div>
<div>
<h1>Room to Breathe</h1>
<p>A tiny spatial puzzle</p>
</div>
</div>
<div class="chapter"><b>03</b> Bedroom puzzle</div>
<div class="header-meta">
<span class="meta-pill">2 beds</span>
<span class="meta-pill">3 drawers</span>
</div>
</header>
<main id="stage">
<canvas id="scene" aria-label="Interactive voxel bedroom puzzle"></canvas>
<div class="vignette"></div>
<section class="card mission">
<div class="eyebrow">The objective</div>
<h2>Make room for everything.</h2>
<p class="intro">
Pull every storage drawer all the way out, then rearrange the room so
both beds remain reachable and the entry stays clear.
</p>
<div class="checklist">
<div class="check" id="bedCheck"><i class="dot"></i><span id="bedText">Checking bed access…</span></div>
<div class="check" id="drawerCheck"><i class="dot"></i><span id="drawerText">Checking drawers…</span></div>
<div class="check" id="entryCheck"><i class="dot"></i><span id="entryText">Checking entry…</span></div>
</div>
<div class="route-toggle">
<span>Show access routes</span>
<button class="switch on" id="routeToggle" aria-label="Toggle access routes"></button>
</div>
</section>
<section class="card inspector">
<div class="inspector-top"><span>Selected object</span><span id="selectionBadge">01</span></div>
<h3 id="selectedName">No object selected</h3>
<p id="selectedMeta">Drag furniture around the room.</p>
<div class="action-grid">
<button class="action-button" id="rotateButton">Rotate 90°</button>
<button class="action-button primary" id="drawerButton" disabled>Pull drawer</button>
<button class="action-button" id="hintButton">Get a hint</button>
<button class="action-button" id="resetButton">Reset room</button>
</div>
<div class="divider"></div>
<div class="camera-row">
<button id="cameraLeft" aria-label="Rotate camera left">↶</button>
<span class="camera-label">Camera view</span>
<button id="cameraRight" aria-label="Rotate camera right">↷</button>
</div>
</section>
<div class="helpbar">
<span><b>DRAG</b> furniture</span>
<span><b>CLICK</b> drawer pulls</span>
<span><b>R</b> rotate</span>
<span><b>DRAG ROOM</b> orbit</span>
</div>
<div class="progress-wrap">
<div id="progressRing"><b id="progressNumber">0/3</b></div>
<div class="progress-copy">
<strong id="progressTitle">Drawers extended</strong>
<span>One clear room, three drawers</span>
</div>
</div>
<div id="toast" role="status" aria-live="polite"></div>
<div id="winModal" role="dialog" aria-modal="true" aria-labelledby="winTitle">
<div class="win-card">
<div class="win-icon">✓</div>
<div class="eyebrow">Puzzle solved</div>
<h2 id="winTitle">Room to breathe.</h2>
<p>
All three drawers are fully open, both beds have a clear approach,
and nobody has to squeeze through the doorway.
</p>
<div class="win-actions">
<button id="keepPlaying">Keep playing</button>
<button class="primary" id="playAgain">Play again</button>
</div>
</div>
</div>
</main>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const GRID_W = 10;
const GRID_H = 8;
const CELL = 1;
const DRAWER_TRAVEL = 0.94;
const $ = id => document.getElementById(id);
const canvas = $("scene");
const stage = $("stage");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e6dc);
scene.fog = new THREE.Fog(0xe9e6dc, 20, 34);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
const camera = new THREE.OrthographicCamera(-8, 8, 6, -6, 0.1, 100);
let cameraAngle = Math.PI * 0.25;
let cameraZoom = 1;
function updateCamera() {
const radius = 16;
camera.position.set(
Math.sin(cameraAngle) * radius,
12.5,
Math.cos(cameraAngle) * radius
);
camera.lookAt(0, 0.55, 0);
camera.zoom = cameraZoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = stage.clientWidth;
const height = stage.clientHeight;
const aspect = width / height;
const viewHeight = aspect < 1.1 ? 14.6 / aspect : 10.9;
camera.left = -viewHeight * aspect / 2;
camera.right = viewHeight * aspect / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
updateCamera();
}
new ResizeObserver(resize).observe(stage);
const hemisphere = new THREE.HemisphereLight(0xfff8e9, 0x8b9c93, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d4, 3.2);
sun.position.set(-7, 13, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -11;
sun.shadow.camera.right = 11;
sun.shadow.camera.top = 11;
sun.shadow.camera.bottom = -11;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 32;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb9d9e4, 1.25);
fill.position.set(9, 7, -6);
scene.add(fill);
const materialCache = new Map();
function material(color, roughness = 0.78) {
const key = `${color}-${roughness}`;
if (!materialCache.has(key)) {
materialCache.set(key, new THREE.MeshStandardMaterial({
color,
roughness,
metalness: 0
}));
}
return materialCache.get(key);
}
function box(parent, size, position, color, roughness = 0.78) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material(color, roughness)
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeRoom() {
const room = new THREE.Group();
scene.add(room);
box(scene, [23, 0.15, 19], [0, -0.42, 0], 0xe4e2d9);
box(room, [10.45, 0.32, 8.45], [0, -0.18, 0], 0xcab58f);
const plankColors = [0xd7bd91, 0xe0c89e, 0xd4b98d, 0xe5cda3];
for (let row = 0; row < GRID_H; row++) {
const z = row - GRID_H / 2 + 0.5;
const offset = row % 2 ? 0.18 : -0.12;
box(room, [10, 0.065, 0.965], [offset, 0.035, z], plankColors[row % plankColors.length]);
box(room, [0.018, 0.012, 0.94], [-5 + offset, 0.073, z], 0xb69c76, 0.9);
}
const gridPoints = [];
for (let x = 0; x <= GRID_W; x++) {
gridPoints.push(
new THREE.Vector3(x - GRID_W / 2, 0.077, -GRID_H / 2),
new THREE.Vector3(x - GRID_W / 2, 0.077, GRID_H / 2)
);
}
for (let z = 0; z <= GRID_H; z++) {
gridPoints.push(
new THREE.Vector3(-GRID_W / 2, 0.077, z - GRID_H / 2),
new THREE.Vector3(GRID_W / 2, 0.077, z - GRID_H / 2)
);
}
const gridGeometry = new THREE.BufferGeometry().setFromPoints(gridPoints);
room.add(new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0x8f806a,
transparent: true,
opacity: 0.13
})
));
// Central woven voxel rug.
box(room, [3.35, 0.025, 2.55], [0.25, 0.087, 0.3], 0x659e97);
box(room, [3.06, 0.035, 2.25], [0.25, 0.101, 0.3], 0xe6d6b2);
box(room, [2.73, 0.041, 1.93], [0.25, 0.114, 0.3], 0x72aaa1);
for (let i = 0; i < 5; i++) {
box(room, [0.22, 0.047, 1.82], [-0.78 + i * 0.51, 0.127, 0.3], 0xd8c99f);
}
// Cutaway back and left walls.
box(room, [10.45, 2.6, 0.2], [0, 1.22, -4.1], 0xf3efe3);
box(room, [0.2, 2.6, 8.45], [-5.1, 1.22, 0], 0xece8dc);
box(room, [10.3, 0.18, 0.11], [0, 0.16, -3.97], 0xc8b992);
box(room, [0.11, 0.18, 8.25], [-4.97, 0.16, 0], 0xc8b992);
box(room, [10.45, 0.12, 0.25], [0, 2.48, -4.1], 0xd9cfb9);
// Window.
box(room, [2.25, 1.55, 0.11], [-1.45, 1.53, -3.96], 0xc8ad83);
box(room, [2.04, 1.34, 0.12], [-1.45, 1.53, -3.88], 0x83b8cd, 0.55);
box(room, [0.11, 1.35, 0.14], [-1.45, 1.53, -3.81], 0xf5edda);
box(room, [2.04, 0.11, 0.14], [-1.45, 1.53, -3.81], 0xf5edda);
box(room, [2.55, 0.12, 0.38], [-1.45, 0.74, -3.86], 0xd7c29c);
// Wall art.
box(room, [1.15, 1.22, 0.11], [2.55, 1.55, -3.95], 0xb59162);
box(room, [0.93, 1.0, 0.12], [2.55, 1.55, -3.86], 0xf7edcf);
box(room, [0.48, 0.5, 0.14], [2.48, 1.58, -3.78], 0xef6b55);
box(room, [0.34, 0.31, 0.14], [2.72, 1.37, -3.77], 0x4a9b8d);
box(room, [0.63, 0.12, 0.14], [2.56, 1.18, -3.77], 0xefc75e);
// Left-wall shelf and books.
box(room, [0.48, 0.12, 1.8], [-4.89, 1.68, 0.9], 0xb78f5f);
box(room, [0.28, 0.63, 0.24], [-4.78, 2.03, 0.42], 0xef6b55);
box(room, [0.29, 0.48, 0.25], [-4.77, 1.97, 0.75], 0xefc75e);
box(room, [0.26, 0.7, 0.23], [-4.78, 2.06, 1.04], 0x659e97);
// Entry threshold.
box(room, [2.25, 0.045, 0.45], [0, 0.088, 3.86], 0x4f9f8f);
for (let i = 0; i < 7; i++) {
box(room, [0.12, 0.052, 0.32], [-0.87 + i * 0.29, 0.116, 3.86], 0x8fc5b7);
}
// Tiny threshold lights.
for (const x of [-1.18, 1.18]) {
box(room, [0.12, 0.32, 0.12], [x, 0.23, 3.91], 0xefc75e);
box(room, [0.1, 0.1, 0.1], [x, 0.43, 3.91], 0xfff0b2);
}
}
makeRoom();
const items = [];
const itemRoots = [];
function cellToWorld(col, row, size = 1) {
return {
x: col - GRID_W / 2 + size / 2,
z: row - GRID_H / 2 + size / 2
};
}
function updateItemTransform(item) {
const p = cellToWorld(item.gx, item.gz, item.size);
item.baseX = p.x;
item.baseZ = p.z;
item.group.position.set(p.x, 0, p.z);
}
function createBed(name, col, row, blanketColor, accentColor) {
const group = new THREE.Group();
scene.add(group);
const item = {
id: `bed-${items.length + 1}`,
name,
type: "bed",
size: 2,
gx: col,
gz: row,
rot: 0,
targetRot: 0,
group,
progress: 0,
open: false,
nudge: 0
};
item.original = { gx: col, gz: row, rot: 0 };
group.userData.item = item;
items.push(item);
itemRoots.push(group);
box(group, [1.9, 0.34, 1.86], [0, 0.32, 0], 0xa9835d);
box(group, [1.72, 0.29, 1.7], [0, 0.61, 0], 0xf5efe2);
box(group, [1.73, 0.13, 0.87], [0, 0.8, 0.39], blanketColor);
box(group, [1.74, 0.05, 0.16], [0, 0.88, 0.02], accentColor);
box(group, [1.92, 1.08, 0.18], [0, 0.79, -0.88], 0xb58b5e);
box(group, [1.52, 0.2, 0.36], [0, 0.83, -0.55], 0xfff9eb);
box(group, [0.12, 0.08, 0.24], [0.54, 0.97, -0.51], accentColor);
for (const x of [-0.72, 0.72]) {
for (const z of [-0.7, 0.7]) {
box(group, [0.15, 0.25, 0.15], [x, 0.12, z], 0x745a43);
}
}
updateItemTransform(item);
return item;
}
function createChest(name, col, row, color, decorColor, decorType) {
const group = new THREE.Group();
scene.add(group);
const item = {
id: `chest-${items.length + 1}`,
name,
type: "chest",
size: 1,
gx: col,
gz: row,
rot: 0,
targetRot: 0,
group,
progress: 0,
open: false,
nudge: 0,
drawerGroup: null
};
item.original = { gx: col, gz: row, rot: 0 };
group.userData.item = item;
items.push(item);
itemRoots.push(group);
box(group, [0.84, 0.76, 0.78], [0, 0.52, 0], color);
box(group, [0.92, 0.1, 0.87], [0, 0.94, 0], decorColor);
box(group, [0.76, 0.12, 0.72], [0, 0.16, 0], 0x80654f);
for (const x of [-0.3, 0.3]) {
for (const z of [-0.27, 0.27]) {
box(group, [0.11, 0.16, 0.11], [x, 0.08, z], 0x6e5745);
}
}
const drawer = new THREE.Group();
group.add(drawer);
item.drawerGroup = drawer;
box(drawer, [0.74, 0.43, 0.68], [0, 0.5, 0.12], 0xf1e7d1);
const front = box(drawer, [0.79, 0.47, 0.12], [0, 0.51, 0.43], decorColor);
const handle = box(drawer, [0.3, 0.07, 0.12], [0, 0.53, 0.54], 0xf8e6a8, 0.5);
front.userData.action = "drawer";
handle.userData.action = "drawer";
if (decorType === "plant") {
box(group, [0.24, 0.24, 0.24], [0.16, 1.1, 0], 0xc97955);
box(group, [0.13, 0.36, 0.13], [0.16, 1.37, 0], 0x4d9570);
box(group, [0.28, 0.13, 0.13], [0.29, 1.48, 0], 0x65aa7e);
box(group, [0.12, 0.29, 0.12], [0.03, 1.55, 0], 0x78b98d);
} else if (decorType === "books") {
box(group, [0.15, 0.4, 0.23], [-0.2, 1.18, 0], 0xef6b55);
box(group, [0.16, 0.31, 0.24], [-0.02, 1.14, 0], 0xefc75e);
box(group, [0.14, 0.46, 0.22], [0.16, 1.22, 0], 0x75a7c1);
} else {
box(group, [0.36, 0.12, 0.28], [0, 1.04, 0], 0xf4d58c);
box(group, [0.25, 0.11, 0.24], [0.07, 1.16, 0], 0xef6b55);
}
updateItemTransform(item);
return item;
}
createBed("Cloud bed", 1, 1, 0xef6b55, 0xefc75e);
createBed("Sage bed", 7, 2, 0x6fa99a, 0x85b9d0);
createChest("Coral cabinet", 4, 2, 0xc56a59, 0xef8b72, "books");
createChest("Teal cabinet", 4, 6, 0x4f9189, 0x76b5a8, "plant");
createChest("Blue cabinet", 5, 6, 0x5f8fa8, 0x8eb8cd, "lamp");
const selectionRing = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-0.55, 0, -0.55),
new THREE.Vector3(0.55, 0, -0.55),
new THREE.Vector3(0.55, 0, 0.55),
new THREE.Vector3(-0.55, 0, 0.55)
]),
new THREE.LineBasicMaterial({
color: 0xefc75e,
transparent: true,
opacity: 0.95,
depthTest: false
})
);
selectionRing.position.y = 0.14;
selectionRing.renderOrder = 10;
scene.add(selectionRing);
const hintRing = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-0.64, 0, -0.64),
new THREE.Vector3(0.64, 0, -0.64),
new THREE.Vector3(0.64, 0, 0.64),
new THREE.Vector3(-0.64, 0, 0.64)
]),
new THREE.LineBasicMaterial({
color: 0xef6b55,
transparent: true,
opacity: 0.9,
depthTest: false
})
);
hintRing.position.y = 0.17;
hintRing.renderOrder = 11;
hintRing.visible = false;
scene.add(hintRing);
const accessGroup = new THREE.Group();
scene.add(accessGroup);
const accessTiles = [];
for (const bed of items.filter(item => item.type === "bed")) {
for (let i = 0; i < 8; i++) {
const tile = new THREE.Mesh(
new THREE.PlaneGeometry(0.72, 0.72),
new THREE.MeshBasicMaterial({
color: 0x48a995,
transparent: true,
opacity: 0.16,
depthWrite: false,
side: THREE.DoubleSide
})
);
tile.rotation.x = -Math.PI / 2;
tile.renderOrder = 2;
accessGroup.add(tile);
accessTiles.push({ tile, bed });
}
}
const doorTiles = [4, 5].map(col => {
const p = cellToWorld(col, 7);
const tile = new THREE.Mesh(
new THREE.PlaneGeometry(0.76, 0.76),
new THREE.MeshBasicMaterial({
color: 0x48a995,
transparent: true,
opacity: 0.24,
depthWrite: false,
side: THREE.DoubleSide
})
);
tile.rotation.x = -Math.PI / 2;
tile.position.set(p.x, 0.13, p.z);
tile.renderOrder = 2;
accessGroup.add(tile);
return tile;
});
const routeGroup = new THREE.Group();
scene.add(routeGroup);
function baseCells(item, gx = item.gx, gz = item.gz) {
const cells = [];
for (let x = 0; x < item.size; x++) {
for (let z = 0; z < item.size; z++) {
cells.push({ x: gx + x, z: gz + z });
}
}
return cells;
}
function extensionCell(item, rot = item.rot) {
switch (rot) {
case 0: return { x: item.gx, z: item.gz + 1 };
case 1: return { x: item.gx + 1, z: item.gz };
case 2: return { x: item.gx, z: item.gz - 1 };
default: return { x: item.gx - 1, z: item.gz };
}
}
function inBounds(cell) {
return cell.x >= 0 && cell.x < GRID_W && cell.z >= 0 && cell.z < GRID_H;
}
function sameCell(a, b) {
return a.x === b.x && a.z === b.z;
}
function itemHasOpenExtension(item) {
return item.type === "chest" && item.progress > 0.55;
}
function occupiedCells(other) {
const cells = baseCells(other);
if (itemHasOpenExtension(other)) cells.push(extensionCell(other));
return cells;
}
function cellBlockedByOther(cell, item) {
return items.some(other => {
if (other === item) return false;
return occupiedCells(other).some(otherCell => sameCell(otherCell, cell));
});
}
function placementValid(item, gx, gz, rot) {
if (gx < 0 || gz < 0 || gx + item.size > GRID_W || gz + item.size > GRID_H) {
return false;
}
const proposedBase = baseCells(item, gx, gz);
for (const cell of proposedBase) {
if (cellBlockedByOther(cell, item)) return false;
}
if (item.type === "chest" && item.open) {
const ext = extensionCell({ ...item, gx, gz }, rot);
if (!inBounds(ext) || cellBlockedByOther(ext, item)) return false;
}
return true;
}
function canOpenDrawer(item) {
const ext = extensionCell(item);
return inBounds(ext) && !cellBlockedByOther(ext, item);
}
const selected = { item: null };
let toastTimer;
let showRoutes = true;
let routeSignature = "";
let hasWon = false;
function showToast(message) {
clearTimeout(toastTimer);
$("toast").textContent = message;
$("toast").classList.add("show");
toastTimer = setTimeout(() => $("toast").classList.remove("show"), 2600);
}
function selectItem(item) {
selected.item = item;
updateInspector();
updateSelectionRing();
}
function updateInspector() {
const item = selected.item;
const drawerButton = $("drawerButton");
const rotateButton = $("rotateButton");
if (!item) {
$("selectedName").textContent = "No object selected";
$("selectedMeta").textContent = "Drag furniture around the room.";
$("selectionBadge").textContent = "—";
drawerButton.disabled = true;
drawerButton.textContent = "Pull drawer";
rotateButton.disabled = true;
return;
}
$("selectedName").textContent = item.name;
$("selectionBadge").textContent = item.id.split("-").pop().padStart(2, "0");
rotateButton.disabled = false;
if (item.type === "chest") {
drawerButton.disabled = false;
drawerButton.textContent = item.open ? "Push drawer" : "Pull drawer";
const amount = Math.round(item.progress * 100);
$("selectedMeta").textContent = item.open
? `Storage drawer · ${amount}% extended. Rotate or drag to find a safe position.`
: "Storage drawer · Closed. Click its handle or use Pull drawer.";
} else {
drawerButton.disabled = true;
drawerButton.textContent = "No drawer";
$("selectedMeta").textContent = "Voxel bed · Keep at least one side reachable from the entry.";
}
}
function updateSelectionRing(invalid = false) {
const item = selected.item;
if (!item) {
selectionRing.visible = false;
return;
}
selectionRing.visible = true;
selectionRing.position.set(item.baseX, 0.14, item.baseZ);
selectionRing.scale.setScalar(item.size);
selectionRing.material.color.setHex(invalid ? 0xef6b55 : 0xefc75e);
}
function toggleDrawer(item) {
if (!item || item.type !== "chest") return;
if (item.open) {
item.open = false;
showToast(`${item.name}: drawer pushed in.`);
return;
}
if (!canOpenDrawer(item)) {
item.nudge = 0.045;
showToast("That drawer is blocked. Move or rotate the nearby furniture first.");
return;
}
item.open = true;
showToast(`${item.name}: drawer pulling all the way out.`);
}
function rotateItem(item) {
if (!item) return;
const nextRot = (item.rot + 1) % 4;
if (!placementValid(item, item.gx, item.gz, nextRot)) {
item.nudge = 0.045;
showToast("There is not enough room to rotate that piece.");
return;
}
item.rot = nextRot;
item.targetRot = nextRot;
showToast(`${item.name} rotated 90°.`);
}
function adjacentCellsForBed(bed) {
const cells = [];
for (const cell of baseCells(bed)) {
const neighbors = [
{ x: cell.x - 1, z: cell.z },
{ x: cell.x + 1, z: cell.z },
{ x: cell.x, z: cell.z - 1 },
{ x: cell.x, z: cell.z + 1 }
];
for (const n of neighbors) {
if (inBounds(n) && !cells.some(c => sameCell(c, n))) cells.push(n);
}
}
return cells;
}
function computeStatus() {
const blocked = new Set();
for (const item of items) {
for (const cell of occupiedCells(item)) {
blocked.add(`${cell.x},${cell.z}`);
}
}
const doorCells = [{ x: 4, z: 7 }, { x: 5, z: 7 }];
const entryClear = doorCells.every(cell => !blocked.has(`${cell.x},${cell.z}`));
const sources = doorCells.filter(cell => !blocked.has(`${cell.x},${cell.z}`));
const reachable = new Set();
const parent = new Map();
const queue = [];
for (const source of sources) {
const key = `${source.x},${source.z}`;
reachable.add(key);
parent.set(key, null);
queue.push(source);
}
let head = 0;
while (head < queue.length) {
const cell = queue[head++];
const neighbors = [
{ x: cell.x + 1, z: cell.z },
{ x: cell.x - 1, z: cell.z },
{ x: cell.x, z: cell.z + 1 },
{ x: cell.x, z: cell.z - 1 }
];
for (const n of neighbors) {
if (!inBounds(n)) continue;
const key = `${n.x},${n.z}`;
if (blocked.has(key) || reachable.has(key)) continue;
reachable.add(key);
parent.set(key, `${cell.x},${cell.z}`);
queue.push(n);
}
}
const beds = items.filter(item => item.type === "bed").map(bed => {
const access = adjacentCellsForBed(bed);
const accessible = access.some(cell => reachable.has(`${cell.x},${cell.z}`));
return { bed, access, accessible };
});
const drawersOpen = items.filter(item =>
item.type === "chest" && item.open && item.progress > 0.995
).length;
return {
blocked,
reachable,
parent,
beds,
entryClear,
drawersOpen,
solved: drawersOpen === 3 && entryClear && beds.every(b => b.accessible)
};
}
function reconstructPath(parent, targetKey) {
if (!parent.has(targetKey)) return [];
const path = [];
let key = targetKey;
while (key) {
const [x, z] = key.split(",").map(Number);
path.push({ x, z });
key = parent.get(key);
}
return path.reverse();
}
function rebuildRoutes(status) {
while (routeGroup.children.length) {
const child = routeGroup.children[0];
routeGroup.remove(child);
if (child.geometry) child.geometry.dispose();
}
if (!showRoutes) return;
const colors = [0x48a995, 0x739fc1];
status.beds.forEach((bedStatus, index) => {
if (!bedStatus.accessible) return;
let shortest = null;
for (const cell of bedStatus.access) {
const key = `${cell.x},${cell.z}`;
if (!status.reachable.has(key)) continue;
const path = reconstructPath(status.parent, key);
if (!shortest || path.length < shortest.length) shortest = path;
}
if (!shortest || shortest.length < 2) return;
const points = shortest.map(cell => {
const p = cellToWorld(cell.x, cell.z);
return new THREE.Vector3(p.x, 0.145, p.z);
});
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const line = new THREE.Line(
geometry,
new THREE.LineDashedMaterial({
color: colors[index],
dashSize: 0.16,
gapSize: 0.1,
transparent: true,
opacity: 0.85,
depthTest: false
})
);
line.computeLineDistances();
line.renderOrder = 4;
routeGroup.add(line);
});
}
function refreshAccessTiles(status) {
for (const bedStatus of status.beds) {
bedStatus.access.forEach((cell, index) => {
const record = accessTiles.find(a => a.bed === bedStatus.bed && a.index === index);
if (!record) return;
const p = cellToWorld(cell.x, cell.z);
record.tile.position.set(p.x, 0.122, p.z);
const key = `${cell.x},${cell.z}`;
if (status.blocked.has(key)) {
record.tile.material.color.setHex(0xef6b55);
record.tile.material.opacity = 0.17;
} else if (status.reachable.has(key)) {
record.tile.material.color.setHex(0x48a995);
record.tile.material.opacity = 0.18;
} else {
record.tile.material.color.setHex(0xefc75e);
record.tile.material.opacity = 0.12;
}
});
}
doorTiles.forEach((tile, index) => {
const col = 4 + index;
const key = `${col},7`;
tile.material.color.setHex(status.blocked.has(key) ? 0xef6b55 : 0x48a995);
tile.material.opacity = status.blocked.has(key) ? 0.32 : 0.2;
});
}
// Give each access tile a stable index within its bed.
accessTiles.forEach(record => {
record.index = accessTiles.filter(a => a.bed === record.bed).indexOf(record);
});
function setCheck(id, textId, pass, text) {
const check = $(id);
$(textId).textContent = text;
check.classList.toggle("pass", pass);
check.classList.toggle("fail", !pass);
}
function updateStatusUI(status) {
const accessibleCount = status.beds.filter(b => b.accessible).length;
const bedsPass = accessibleCount === status.beds.length;
setCheck(
"bedCheck",
"bedText",
bedsPass,
bedsPass ? "Both beds are reachable" : `${accessibleCount} of 2 beds reachable`
);
setCheck(
"drawerCheck",
"drawerText",
status.drawersOpen === 3,
status.drawersOpen === 3 ? "All 3 drawers fully open" : `${status.drawersOpen} of 3 drawers fully open`
);
setCheck(
"entryCheck",
"entryText",
status.entryClear,
status.entryClear ? "Entry lane is clear" : "Entry lane is blocked"
);
$("progressRing").style.setProperty("--p", (status.drawersOpen / 3) * 100);
$("progressNumber").textContent = `${status.drawersOpen}/3`;
$("progressTitle").textContent = status.solved ? "Everything fits" : "Drawers extended";
const signature = [
...Array.from(status.blocked).sort(),
status.entryClear,
...status.beds.map(b => b.accessible)
].join("|");
if (signature !== routeSignature) {
routeSignature = signature;
rebuildRoutes(status);
}
refreshAccessTiles(status);
if (status.solved && !hasWon) {
hasWon = true;
setTimeout(() => {
$("winModal").classList.add("show");
spawnConfetti();
}, 250);
}
}
// Raycast and direct pointer controls.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const floorPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), 0);
const floorHit = new THREE.Vector3();
let dragState = null;
let orbitState = null;
function setPointer(event) {
const rect = canvas.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);
}
function findItem(object) {
let current = object;
while (current) {
if (current.userData && current.userData.item) return current.userData.item;
current = current.parent;
}
return null;
}
canvas.addEventListener("pointerdown", event => {
setPointer(event);
const hits = raycaster.intersectObjects(itemRoots, true);
const hit = hits[0];
if (hit && event.button === 0) {
const item = findItem(hit.object);
if (item) {
selectItem(item);
if (hit.object.userData.action === "drawer") {
toggleDrawer(item);
event.preventDefault();
return;
}
if (raycaster.ray.intersectPlane(floorPlane, floorHit)) {
dragState = {
item,
pointerId: event.pointerId,
startX: event.clientX,
startY: event.clientY,
oldGx: item.gx,
oldGz: item.gz,
offsetX: item.baseX - floorHit.x,
offsetZ: item.baseZ - floorHit.z
};
canvas.setPointerCapture(event.pointerId);
event.preventDefault();
}
return;
}
}
orbitState = {
pointerId: event.pointerId,
x: event.clientX,
y: event.clientY
};
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (dragState && dragState.pointerId === event.pointerId) {
setPointer(event);
if (!raycaster.ray.intersectPlane(floorPlane, floorHit)) return;
const worldX = floorHit.x + dragState.offsetX;
const worldZ = floorHit.z + dragState.offsetZ;
const size = dragState.item.size;
const gx = THREE.MathUtils.clamp(
Math.round(worldX + GRID_W / 2 - size / 2),
0,
GRID_W - size
);
const gz = THREE.MathUtils.clamp(
Math.round(worldZ + GRID_H / 2 - size / 2),
0,
GRID_H - size
);
dragState.item.gx = gx;
dragState.item.gz = gz;
updateItemTransform(dragState.item);
updateSelectionRing(!placementValid(dragState.item, gx, gz, dragState.item.rot));
return;
}
if (orbitState && orbitState.pointerId === event.pointerId) {
const dx = event.clientX - orbitState.x;
const dy = event.clientY - orbitState.y;
cameraAngle -= dx * 0.006;
cameraZoom = THREE.MathUtils.clamp(cameraZoom + dy * 0.002, 0.72, 1.35);
orbitState.x = event.clientX;
orbitState.y = event.clientY;
updateCamera();
}
});
function finishPointer(event) {
if (dragState && dragState.pointerId === event.pointerId) {
const item = dragState.item;
if (!placementValid(item, item.gx, item.gz, item.rot)) {
item.gx = dragState.oldGx;
item.gz = dragState.oldGz;
updateItemTransform(item);
showToast("That spot is occupied. The furniture returned to its previous tile.");
}
dragState = null;
updateSelectionRing();
}
if (orbitState && orbitState.pointerId === event.pointerId) {
orbitState = null;
}
}
canvas.addEventListener("pointerup", finishPointer);
canvas.addEventListener("pointercancel", finishPointer);
canvas.addEventListener("wheel", event => {
event.preventDefault();
cameraZoom = THREE.MathUtils.clamp(cameraZoom - event.deltaY * 0.0007, 0.72, 1.35);
updateCamera();
}, { passive: false });
$("rotateButton").addEventListener("click", () => rotateItem(selected.item));
$("drawerButton").addEventListener("click", () => toggleDrawer(selected.item));
$("routeToggle").addEventListener("click", () => {
showRoutes = !showRoutes;
$("routeToggle").classList.toggle("on", showRoutes);
routeSignature = "";
routeGroup.visible = showRoutes;
});
$("cameraLeft").addEventListener("click", () => {
cameraAngle -= Math.PI / 4;
updateCamera();
});
$("cameraRight").addEventListener("click", () => {
cameraAngle += Math.PI / 4;
updateCamera();
});
function highlightHint(item) {
if (!item) return;
hintRing.visible = true;
hintRing.position.set(item.baseX, 0.17, item.baseZ);
hintRing.scale.setScalar(item.size);
}
$("hintButton").addEventListener("click", () => {
const status = computeStatus();
const chests = items.filter(item => item.type === "chest");
if (status.drawersOpen < 3) {
const closed = chests.find(item => !item.open);
highlightHint(closed);
showToast("Start with the highlighted cabinet: pull its drawer all the way out.");
return;
}
if (!status.entryClear) {
const blocker = chests.find(item =>
itemHasOpenExtension(item) &&
[4, 5].some(col => {
const ext = extensionCell(item);
return ext.x === col && ext.z === 7;
})
);
highlightHint(blocker);
showToast("An open drawer is covering the entry. Rotate or move the highlighted cabinet.");
return;
}
const inaccessible = status.beds.find(bed => !bed.accessible);
if (inaccessible) {
const blocker = chests.find(item =>
occupiedCells(item).some(cell =>
inaccessible.access.some(a => sameCell(a, cell))
)
);
highlightHint(blocker || inaccessible.bed);
showToast("This bed has no clear approach. Move the highlighted obstruction away.");
return;
}
const edgeChest = chests.find(item => item.gx === 0 || item.gx === GRID_W - 1 || item.gz === 0);
highlightHint(edgeChest || chests[0]);
showToast("You are close. Keep each open drawer pointing into unused floor space.");
});
function resetRoom() {
hasWon = false;
$("winModal").classList.remove("show");
for (const item of items) {
item.gx = item.original.gx;
item.gz = item.original.gz;
item.rot = item.original.rot;
item.targetRot = item.original.rot;
item.open = false;
item.progress = 0;
item.nudge = 0;
updateItemTransform(item);
}
selectItem(items.find(item => item.name === "Teal cabinet"));
routeSignature = "";
clearConfetti();
showToast("Room reset. Find a layout that gives every drawer its own space.");
}
$("resetButton").addEventListener("click", resetRoom);
$("playAgain").addEventListener("click", resetRoom);
$("keepPlaying").addEventListener("click", () => $("winModal").classList.remove("show"));
window.addEventListener("keydown", event => {
if (event.key.toLowerCase() === "r") rotateItem(selected.item);
if (event.key.toLowerCase() === "o") toggleDrawer(selected.item);
if (event.key === "Escape") {
selected.item = null;
updateInspector();
updateSelectionRing();
}
if (event.key === "ArrowLeft") {
cameraAngle -= Math.PI / 8;
updateCamera();
}
if (event.key === "ArrowRight") {
cameraAngle += Math.PI / 8;
updateCamera();
}
});
// Voxel confetti.
const confetti = [];
const confettiGeometry = new THREE.BoxGeometry(0.1, 0.1, 0.1);
const confettiColors = [0xef6b55, 0xefc75e, 0x48a995, 0x78a9c7, 0xf7f4eb];
function spawnConfetti() {
clearConfetti();
for (let i = 0; i < 64; i++) {
const mesh = new THREE.Mesh(
confettiGeometry,
material(confettiColors[i % confettiColors.length])
);
mesh.position.set(
(Math.random() - 0.5) * 8,
3 + Math.random() * 4,
(Math.random() - 0.5) * 6
);
mesh.castShadow = true;
scene.add(mesh);
confetti.push({
mesh,
velocity: new THREE.Vector3(
(Math.random() - 0.5) * 3.5,
1 + Math.random() * 3,
(Math.random() - 0.5) * 3.5
),
spin: new THREE.Vector3(Math.random() * 6, Math.random() * 6, Math.random() * 6),
life: 4.5
});
}
}
function clearConfetti() {
for (const piece of confetti) scene.remove(piece.mesh);
confetti.length = 0;
}
const clock = new THREE.Clock();
let elapsed = 0;
let hintPulse = 0;
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
elapsed += dt;
for (const item of items) {
const target = item.open ? 1 : 0;
item.progress += (target - item.progress) * Math.min(1, dt * 7.5);
if (Math.abs(target - item.progress) < 0.001) {
item.progress = target;
}
if (item.drawerGroup) {
item.drawerGroup.position.z = item.progress * DRAWER_TRAVEL;
}
const desiredRotation = item.targetRot * Math.PI / 2;
const rotationDifference = Math.atan2(
Math.sin(desiredRotation - item.group.rotation.y),
Math.cos(desiredRotation - item.group.rotation.y)
);
item.group.rotation.y += rotationDifference * Math.min(1, dt * 10);
item.nudge *= Math.pow(0.001, dt);
item.group.position.x = item.baseX + Math.sin(elapsed * 48) * item.nudge;
item.group.position.z = item.baseZ + Math.cos(elapsed * 43) * item.nudge;
}
if (selected.item) updateSelectionRing();
if (hintRing.visible) {
hintPulse += dt;
hintRing.material.opacity = 0.45 + Math.sin(hintPulse * 7) * 0.35;
hintRing.scale.setScalar(
(selected.item?.size || 1) * (1 + Math.sin(hintPulse * 6) * 0.035)
);
}
for (let i = confetti.length - 1; i >= 0; i--) {
const piece = confetti[i];
piece.life -= dt;
piece.velocity.y -= 5.8 * dt;
piece.mesh.position.addScaledVector(piece.velocity, dt);
piece.mesh.rotation.x += piece.spin.x * dt;
piece.mesh.rotation.y += piece.spin.y * dt;
piece.mesh.rotation.z += piece.spin.z * dt;
if (piece.life <= 0 || piece.mesh.position.y < -0.7) {
scene.remove(piece.mesh);
confetti.splice(i, 1);
}
}
const status = computeStatus();
updateStatusUI(status);
renderer.render(scene, camera);
}
selectItem(items.find(item => item.name === "Teal cabinet"));
resize();
animate();
</script>
</body>
</html>
```