Prompt specification
Build or customize the Three.js 3D scene "Compact guest room packing" 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: Compact guest room packing
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/compact-guest-room-packing-x0167
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts under-bed drawers. Compose a guest room with suitcase, bed drawers, desk, and chair; a visitor stores belongings while the animation preserves a clear route to the door. Stylized solid forms.
Technical checks: Keep the packed bag fully inside the drawer volume after closure without intersecting the drawer front. Leave the path to the door unobstructed.
Return one complete HTML document that works with the cdnjs Three.js r160 module inside a sandbox with scripts allowed. Make the requested subject visible at the default camera. Use procedural assets and local button handlers. Do not use native form submission, pointer lock, external assets or runtime requests other than Three.js.
Working Three.js Starter (compact-guest-room-packing-x0167):
```html
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Quarto de hóspedes — gavetas sob a cama</title>
<style>
:root {
color-scheme: light;
--ink: #25333a;
--muted: #64747b;
--paper: rgba(255, 255, 255, 0.92);
--line: rgba(37, 51, 58, 0.12);
--green: #2f8b6c;
--green-soft: #e4f3ed;
--orange: #ef7f32;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e5dc;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#app, #stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active { cursor: grabbing; }
.panel {
position: fixed;
top: 20px;
left: 20px;
width: min(340px, calc(100vw - 40px));
padding: 18px 18px 16px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--paper);
box-shadow: 0 16px 46px rgba(43, 54, 57, 0.14);
backdrop-filter: blur(14px);
user-select: none;
z-index: 5;
}
.eyebrow {
margin-bottom: 5px;
color: var(--green);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 22px;
line-height: 1.12;
letter-spacing: -0.03em;
}
.subtitle {
margin: 7px 0 13px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.status {
display: flex;
align-items: center;
gap: 9px;
min-height: 38px;
padding: 9px 11px;
border-radius: 11px;
background: #f0f3f1;
color: #42565c;
font-size: 13px;
font-weight: 700;
}
.status-dot {
width: 9px;
height: 9px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--orange);
box-shadow: 0 0 0 4px rgba(239, 127, 50, 0.13);
}
.status.done {
background: var(--green-soft);
color: #226b53;
}
.status.done .status-dot {
background: var(--green);
box-shadow: 0 0 0 4px rgba(47, 139, 108, 0.14);
}
.detail {
min-height: 38px;
margin: 10px 1px 11px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.progress {
height: 5px;
overflow: hidden;
border-radius: 99px;
background: #e5e9e7;
}
#progressFill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--green), #71b99b);
transition: width 80ms linear;
}
.facts {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 12px;
}
.fact {
min-height: 63px;
padding: 10px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.72);
}
.fact.wide { grid-column: 1 / -1; }
.fact span {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.fact strong {
display: block;
font-size: 12px;
line-height: 1.3;
}
.good { color: var(--green); }
.orange { color: #c85e1e; }
.controls {
position: fixed;
right: 20px;
bottom: 20px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
z-index: 5;
}
button {
min-height: 40px;
padding: 0 14px;
border: 1px solid rgba(37, 51, 58, 0.14);
border-radius: 12px;
background: rgba(255, 255, 255, 0.92);
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 750;
box-shadow: 0 8px 24px rgba(43, 54, 57, 0.10);
cursor: pointer;
transition: transform 120ms ease, background 120ms ease, opacity 120ms ease;
}
button:hover:not(:disabled) {
transform: translateY(-1px);
background: white;
}
button:disabled {
opacity: 0.46;
cursor: default;
}
button.primary {
border-color: transparent;
background: var(--green);
color: white;
}
.hint {
position: fixed;
left: 20px;
bottom: 18px;
padding: 8px 11px;
border-radius: 10px;
background: rgba(255, 255, 255, 0.72);
color: rgba(37, 51, 58, 0.66);
font-size: 11px;
pointer-events: none;
z-index: 4;
}
@media (max-width: 720px) {
.panel {
top: 10px;
left: 10px;
width: min(310px, calc(100vw - 20px));
padding: 14px;
}
.subtitle, .detail { display: none; }
.facts { grid-template-columns: 1fr; }
.fact.wide { grid-column: auto; }
.controls { right: 10px; bottom: 10px; }
.hint { display: none; }
}
</style>
</head>
<body>
<div id="app">
<div id="stage" aria-label="Cena interativa de um quarto de hóspedes com gavetas sob a cama"></div>
<section class="panel" aria-live="polite">
<div class="eyebrow">Hotel · cuarto de hóspedes</div>
<h1>Guardar sem bloquear a passagem</h1>
<p class="subtitle">A bolsa laranja é levada até uma gaveta sob a cama. O teste geométrico confirma que ela fica totalmente contida quando a gaveta fecha.</p>
<div id="statusBox" class="status">
<span class="status-dot"></span>
<span id="statusText">Preparando a animação</span>
</div>
<p id="detailText" class="detail">A rota central até a porta permanece livre.</p>
<div class="progress"><div id="progressFill"></div></div>
<div class="facts">
<div class="fact">
<span>Bolsa laranja</span>
<strong id="bagState">Sobre a mala</strong>
</div>
<div class="fact">
<span>Gaveta</span>
<strong id="drawerState">Fechada</strong>
</div>
<div class="fact wide">
<span>Rota até a porta</span>
<strong id="routeState" class="good">Livre · corridor verificado</strong>
</div>
<div class="fact wide">
<span>Teste de contenção</span>
<strong id="containmentState">Aguardando depósito</strong>
</div>
</div>
</section>
<div class="controls">
<button id="replayButton" class="primary" type="button">Reexecutar</button>
<button id="inspectButton" type="button" disabled>Inspecionar gaveta</button>
<button id="cameraButton" type="button">Câmera inicial</button>
</div>
<div class="hint">Arraste para orbitar · use a roda para aproximar</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
const statusBox = document.getElementById("statusBox");
const statusText = document.getElementById("statusText");
const detailText = document.getElementById("detailText");
const progressFill = document.getElementById("progressFill");
const bagState = document.getElementById("bagState");
const drawerState = document.getElementById("drawerState");
const routeState = document.getElementById("routeState");
const containmentState = document.getElementById("containmentState");
const replayButton = document.getElementById("replayButton");
const inspectButton = document.getElementById("inspectButton");
const cameraButton = document.getElementById("cameraButton");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e5dc);
scene.fog = new THREE.Fog(0xe9e5dc, 18, 31);
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
stage.appendChild(renderer.domElement);
renderer.domElement.setAttribute("aria-label", "Quarto 3D de hóspedes");
const camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 0.1, 70);
const cameraTarget = new THREE.Vector3(-0.05, 0.75, -0.25);
const cameraHome = { radius: 15.6, theta: 0.69, phi: 1.02 };
const orbit = { ...cameraHome };
function updateCamera() {
const aspect = camera.aspect;
const responsiveRadius = aspect < 1 ? orbit.radius * (1.28 / aspect) ** 0.45 : orbit.radius;
const spherical = new THREE.Spherical(responsiveRadius, orbit.phi, orbit.theta);
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xfff9eb, 0x71827d, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff1d6, 3.15);
sun.position.set(5.5, 10.5, 6.5);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -8;
sun.shadow.camera.right = 8;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -8;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 26;
sun.shadow.bias = -0.00018;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xc8e5ef, 1.0);
fill.position.set(-5, 5, -3);
scene.add(fill);
const material = (color, roughness = 0.78, metalness = 0) =>
new THREE.MeshStandardMaterial({ color, roughness, metalness });
const mats = {
floor: material(0xcaa979, 0.92),
floorAlt: material(0xd4b585, 0.92),
floorLine: material(0xa98c63, 0.95),
wall: material(0xf0eee6, 0.95),
wallSide: material(0xe2e5df, 0.95),
trim: material(0xfbfaf5, 0.85),
wood: material(0x9d7049, 0.78),
woodLight: material(0xc79b68, 0.76),
woodDark: material(0x60483b, 0.82),
cream: material(0xf4efe2, 0.93),
blanket: material(0x729c91, 0.95),
blanketDark: material(0x557c73, 0.95),
pillow: material(0xfffaf0, 0.98),
metal: material(0x30383b, 0.48, 0.42),
brass: material(0xc89b58, 0.36, 0.58),
teal: material(0x3d7f82, 0.7),
tealDark: material(0x245c61, 0.78),
orange: material(0xef7f32, 0.67),
orangeDark: material(0xb95320, 0.75),
shirt: material(0x416f9e, 0.88),
pants: material(0x344452, 0.9),
skin: material(0xd9a47c, 0.9),
hair: material(0x49372f, 0.96),
rug: material(0xc7b7a1, 0.98),
rugStripe: material(0x738f85, 0.96),
plant: material(0x4e8066, 0.92),
pot: material(0xc77d55, 0.86),
paper: material(0xf7f3e9, 0.96),
black: material(0x171d20, 0.78)
};
function box(parent, size, position, mat, cast = true, receive = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(size[0], size[1], size[2]), mat);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = cast;
mesh.receiveShadow = receive;
parent.add(mesh);
return mesh;
}
function cylinder(parent, radiusTop, radiusBottom, height, position, mat, segments = 16) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusTop, radiusBottom, height, segments),
mat
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function sphere(parent, radius, position, mat, scale = [1, 1, 1]) {
const mesh = new THREE.Mesh(new THREE.SphereGeometry(radius, 18, 12), mat);
mesh.position.set(position[0], position[1], position[2]);
mesh.scale.set(scale[0], scale[1], scale[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Quarto em corte, com a porta na parede do fundo.
const room = new THREE.Group();
scene.add(room);
box(room, [9.2, 0.18, 8.7], [0, -0.09, 0], mats.floor, false, true);
for (let i = 0; i < 12; i++) {
const x = -4.125 + i * 0.75;
box(room, [0.735, 0.018, 8.56], [x, 0.006, 0], i % 2 ? mats.floorAlt : mats.floor, false, true);
box(room, [0.012, 0.021, 8.56], [x + 0.365, 0.011, 0], mats.floorLine, false, true);
}
box(room, [0.16, 3.45, 8.7], [-4.57, 1.725, 0], mats.wallSide);
box(room, [4.825, 3.45, 0.18], [-2.0875, 1.725, -4.28], mats.wall);
box(room, [2.725, 3.45, 0.18], [3.1375, 1.725, -4.28], mats.wall);
box(room, [1.45, 0.79, 0.18], [1.05, 3.055, -4.28], mats.wall);
box(room, [9.15, 0.12, 0.08], [0, 0.06, -4.16], mats.trim);
box(room, [0.13, 3.35, 0.08], [-4.48, 1.675, -4.16], mats.trim);
box(room, [0.13, 0.08, 8.5], [-4.48, 3.35, 0], mats.trim);
// Moldura e porta aberta para fora, sem invadir o corredor interno.
box(room, [0.11, 2.72, 0.18], [0.31, 1.36, -4.15], mats.trim);
box(room, [0.11, 2.72, 0.18], [1.79, 1.36, -4.15], mats.trim);
box(room, [1.59, 0.11, 0.18], [1.05, 2.67, -4.15], mats.trim);
const door = new THREE.Group();
door.position.set(0.36, 0, -4.21);
door.rotation.y = 0.34;
room.add(door);
box(door, [1.39, 2.58, 0.11], [0.695, 1.29, 0], material(0xaab9b2, 0.8));
box(door, [1.12, 0.83, 0.035], [0.695, 1.86, 0.072], material(0x879e97, 0.8));
box(door, [1.12, 0.72, 0.035], [0.695, 0.83, 0.072], material(0x879e97, 0.8));
sphere(door, 0.055, [1.18, 1.2, 0.12], mats.brass);
// Quadro procedural.
box(room, [1.28, 0.92, 0.07], [-1.25, 2.25, -4.15], mats.wood);
box(room, [1.12, 0.76, 0.035], [-1.25, 2.25, -4.102], mats.paper);
box(room, [0.48, 0.52, 0.025], [-1.42, 2.24, -4.075], mats.blanket);
sphere(room, 0.17, [-1.05, 2.33, -4.065], mats.orange, [1, 1, 0.12]);
// Rota marcada no piso.
const routeGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(1.05, 0.035, -3.95),
new THREE.Vector3(1.05, 0.035, 3.05)
]);
const routeMaterial = new THREE.LineDashedMaterial({
color: 0x4b9c7f,
dashSize: 0.22,
gapSize: 0.16,
transparent: true,
opacity: 0.65
});
const routeLine = new THREE.Line(routeGeometry, routeMaterial);
routeLine.computeLineDistances();
scene.add(routeLine);
const arrowMaterial = new THREE.MeshStandardMaterial({
color: 0x4b9c7f,
roughness: 0.7,
emissive: 0x173b2d,
emissiveIntensity: 0.18
});
[-2.55, -0.15, 2.2].forEach((z) => {
const arrow = new THREE.Mesh(new THREE.ConeGeometry(0.105, 0.25, 3), arrowMaterial);
arrow.rotation.x = Math.PI / 2;
arrow.position.set(1.05, 0.045, z);
scene.add(arrow);
});
box(scene, [2.12, 0.025, 3.5], [1.05, 0.028, 0.7], mats.rug, false, true);
box(scene, [1.92, 0.009, 0.09], [1.05, 0.046, -0.72], mats.rugStripe, false, false);
box(scene, [1.92, 0.009, 0.09], [1.05, 0.046, 2.0], mats.rugStripe, false, false);
// Cama com três gavetas low-level.
const bed = new THREE.Group();
scene.add(bed);
box(bed, [2.5, 0.18, 4.9], [-2.75, 0.99, -0.4], mats.woodDark);
box(bed, [2.43, 0.42, 4.76], [-2.75, 1.29, -0.4], mats.cream);
box(bed, [2.48, 0.10, 2.12], [-2.75, 1.54, 0.83], mats.blanket);
box(bed, [2.48, 0.035, 0.15], [-2.75, 1.61, -0.1], mats.blanketDark);
box(bed, [2.55, 1.27, 0.15], [-2.75, 1.51, -2.86], mats.wood);
sphere(bed, 0.43, [-3.16, 1.58, -2.12], mats.pillow, [1.18, 0.30, 0.72]);
sphere(bed, 0.43, [-2.35, 1.58, -2.12], mats.pillow, [1.18, 0.30, 0.72]);
[[-3.78, -2.58], [-1.72, -2.58], [-3.78, 1.77], [-1.72, 1.77]].forEach(([x, z]) => {
box(bed, [0.15, 0.83, 0.15], [x, 0.415, z], mats.woodDark);
});
const CLOSED_DRAWER_X = -1.44;
const DRAWER_OPEN_DISTANCE = 1.05;
const drawerSpecs = [-1.55, -0.30, 0.95];
const drawerGroups = [];
function createDrawer(z, index) {
const group = new THREE.Group();
group.name = index === 1 ? "middleStorageDrawer" : `drawer${index}`;
group.position.set(CLOSED_DRAWER_X, 0, z);
scene.add(group);
// Tabuleiro real: fundo, laterais, fundo traseiro e frente.
box(group, [1.78, 0.08, 1.05], [-0.96, 0.27, 0], mats.woodLight);
box(group, [1.78, 0.54, 0.07], [-0.96, 0.57, -0.52], mats.woodLight);
box(group, [1.78, 0.54, 0.07], [-0.96, 0.57, 0.52], mats.woodLight);
box(group, [0.07, 0.54, 1.05], [-1.86, 0.57, 0], mats.woodLight);
box(group, [0.14, 0.64, 1.14], [0.04, 0.57, 0], index === 1 ? mats.wood : mats.woodDark);
const pull = cylinder(group, 0.034, 0.034, 0.47, [0.17, 0.61, 0], mats.brass, 12);
pull.rotation.x = Math.PI / 2;
box(group, [0.08, 0.06, 0.08], [0.13, 0.61, -0.23], mats.brass);
box(group, [0.08, 0.06, 0.08], [0.13, 0.61, 0.23], mats.brass);
drawerGroups.push(group);
return group;
}
drawerSpecs.forEach((z, i) => createDrawer(z, i));
const storageDrawer = drawerGroups[1];
// Mesa de trabalho e cadeira.
const desk = new THREE.Group();
desk.position.set(3.55, 0, -1.55);
scene.add(desk);
box(desk, [1.45, 0.14, 2.5], [0, 1.48, 0], mats.woodLight);
box(desk, [0.09, 1.3, 2.25], [-0.56, 0.72, 0], mats.wood);
[[-0.55, -1.03], [0.55, -1.03], [-0.55, 1.03], [0.55, 1.03]].forEach(([x, z]) => {
box(desk, [0.11, 1.4, 0.11], [x, 0.7, z], mats.woodDark);
});
box(desk, [0.46, 0.055, 0.62], [-0.14, 1.59, 0.28], mats.paper);
box(desk, [0.31, 0.04, 0.5], [-0.14, 1.64, 0.24], mats.blanket);
cylinder(desk, 0.09, 0.075, 0.18, [0.31, 1.64, -0.68], mats.teal);
cylinder(desk, 0.012, 0.012, 0.38, [0.31, 1.88, -0.68], mats.metal, 10);
cylinder(desk, 0.24, 0.12, 0.24, [0.31, 2.05, -0.68], mats.blanket);
cylinder(desk, 0.19, 0.19, 0.025, [0.31, 1.93, -0.68], mats.paper);
const chair = new THREE.Group();
chair.position.set(2.28, 0, -1.55);
chair.rotation.y = Math.PI / 2;
scene.add(chair);
box(chair, [0.72, 0.15, 0.68], [0, 0.82, 0], mats.tealDark);
box(chair, [0.72, 0.68, 0.12], [0, 1.2, -0.29], mats.teal);
cylinder(chair, 0.045, 0.045, 0.64, [0, 0.43, 0], mats.metal, 12);
for (let i = 0; i < 5; i++) {
const angle = i / 5 * Math.PI * 2;
const leg = box(chair, [0.08, 0.07, 0.52], [Math.sin(angle) * 0.21, 0.14, Math.cos(angle) * 0.21], mats.metal);
leg.rotation.y = angle;
sphere(chair, 0.075, [Math.sin(angle) * 0.43, 0.085, Math.cos(angle) * 0.43], mats.black, [0.65, 1, 0.65]);
}
// Mala com detalhes procedurais.
const suitcase = new THREE.Group();
suitcase.position.set(3.18, 0, 0.72);
suitcase.rotation.y = -0.08;
scene.add(suitcase);
box(suitcase, [0.91, 1.18, 0.52], [0, 0.72, 0], mats.teal);
box(suitcase, [0.84, 0.07, 0.55], [0, 0.22, 0], mats.tealDark);
[-0.23, 0.23].forEach((x) => {
box(suitcase, [0.045, 0.87, 0.025], [x, 0.74, 0.272], mats.tealDark);
});
[-0.32, 0.32].forEach((x) => {
const wheel = cylinder(suitcase, 0.085, 0.085, 0.09, [x, 0.09, 0.06], mats.black, 12);
wheel.rotation.z = Math.PI / 2;
});
box(suitcase, [0.25, 0.055, 0.08], [0.28, 1.34, 0], mats.metal);
// Bolsa laranja. As alças fazem parte da geometra e entram no teste de volume.
const bag = new THREE.Group();
bag.name = "orangeDuffel";
scene.add(bag);
box(bag, [0.90, 0.44, 0.50], [0, 0, 0], mats.orange);
[-0.25, 0.25].forEach((x) => {
box(bag, [0.055, 0.026, 0.505], [x, 0.232, 0], mats.orangeDark);
});
box(bag, [0.82, 0.018, 0.025], [0, 0.02, 0.257], mats.orangeDark);
const handleCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(-0.24, 0.19, 0),
new THREE.Vector3(-0.14, 0.29, 0),
new THREE.Vector3(0.14, 0.29, 0),
new THREE.Vector3(0.24, 0.19, 0)
]);
const bagHandle = new THREE.Mesh(
new THREE.TubeGeometry(handleCurve, 24, 0.022, 8, false),
mats.orangeDark
);
bagHandle.castShadow = true;
bag.add(bagHandle);
const bagStart = new THREE.Vector3(3.02, 1.52, 0.72);
bag.position.copy(bagStart);
bag.updateMatrixWorld(true);
// Volume interior parametrizado a partir da geometria real da gaveta.
const cavityBounds = new THREE.Box3(
new THREE.Vector3(-1.82, 0.31, -0.475),
new THREE.Vector3(-0.15, 0.88, 0.475)
);
const frontPanelBounds = new THREE.Box3(
new THREE.Vector3(-0.03, 0.25, -0.57),
new THREE.Vector3(0.11, 0.89, 0.57)
);
// Bounds locais da bolsa com a raiz temporariamente na origem.
bag.position.set(0, 0, 0);
bag.updateMatrixWorld(true);
const bagBaseBounds = new THREE.Box3().setFromObject(bag);
const targetLocal = new THREE.Vector3(
-0.72,
cavityBounds.min.y + 0.015 - bagBaseBounds.min.y,
0
);
const prospectiveBounds = bagBaseBounds.clone().translate(targetLocal);
const prospectiveFit = cavityBounds.containsBox(prospectiveBounds);
if (!prospectiveFit) {
console.error("A posição de depósito não contém a bolsa na cavidade.", prospectiveBounds, cavityBounds);
}
bag.position.copy(bagStart);
// Hóspede estilizado.
const visitor = new THREE.Group();
visitor.name = "guest";
scene.add(visitor);
box(visitor, [0.54, 0.68, 0.31], [0, 1.09, 0], mats.shirt);
cylinder(visitor, 0.12, 0.14, 0.16, [0, 0.73, 0], mats.skin);
sphere(visitor, 0.215, [0, 1.62, 0], mats.skin, [0.92, 1.05, 0.92]);
sphere(visitor, 0.205, [0, 1.72, -0.025], mats.hair, [0.98, 0.48, 0.94]);
sphere(visitor, 0.021, [-0.068, 1.65, 0.188], mats.black, [0.72, 1, 0.45]);
sphere(visitor, 0.021, [0.068, 1.65, 0.188], mats.black, [0.72, 1, 0.45]);
sphere(visitor, 0.028, [0, 1.56, 0.192], mats.skin, [1.2, 0.62, 0.35]);
const arms = [];
const legs = [];
[-1, 1].forEach((side) => {
const arm = new THREE.Group();
arm.position.set(side * 0.34, 1.35, 0);
visitor.add(arm);
cylinder(arm, 0.082, 0.068, 0.47, [0, -0.235, 0], mats.shirt, 12);
sphere(arm, 0.075, [0, -0.49, 0], mats.skin, [0.85, 1, 0.85]);
arms.push(arm);
const leg = new THREE.Group();
leg.position.set(side * 0.145, 0.79, 0);
visitor.add(leg);
cylinder(leg, 0.09, 0.078, 0.63, [0, -0.315, 0], mats.pants, 12);
box(leg, [0.19, 0.13, 0.31], [0, -0.69, 0.07], mats.black);
legs.push(leg);
});
const pathToBag = [
new THREE.Vector3(1.05, 0, -3.68),
new THREE.Vector3(1.16, 0, -1.85),
new THREE.Vector3(1.78, 0, -0.15),
new THREE.Vector3(2.25, 0, 0.62)
];
const pathToDrawer = [
new THREE.Vector3(2.25, 0, 0.62),
new THREE.Vector3(1.55, 0, 0.28),
new THREE.Vector3(0.72, 0, -0.18),
new THREE.Vector3(0.28, 0, -0.30)
];
const pathToDoor = [
new THREE.Vector3(0.28, 0, -0.30),
new THREE.Vector3(0.86, 0, -1.38),
new THREE.Vector3(1.05, 0, -2.8),
new THREE.Vector3(1.05, 0, -3.68)
];
function smooth01(value) {
const x = THREE.MathUtils.clamp(value, 0, 1);
return x * x * (3 - 2 * x);
}
function samplePath(points, u) {
const lengths = [];
let total = 0;
for (let i = 0; i < points.length - 1; i++) {
const length = points[i].distanceTo(points[i + 1]);
lengths.push(length);
total += length;
}
let distance = THREE.MathUtils.clamp(u, 0, 1) * total;
for (let i = 0; i < lengths.length; i++) {
if (distance <= lengths[i] || i === lengths.length - 1) {
const local = lengths[i] ? THREE.MathUtils.clamp(distance / lengths[i], 0, 1) : 0;
const position = points[i].clone().lerp(points[i + 1], local);
const direction = points[i + 1].clone().sub(points[i]).normalize();
return { position, direction };
}
distance -= lengths[i];
}
return { position: points[0].clone(), direction: new THREE.Vector3(0, 0, 1) };
}
function setFacing(yaw) {
visitor.rotation.y = yaw;
}
function poseVisitor(walking, time, carry, reach) {
const swing = walking ? Math.sin(time * 10.5) * 0.55 : 0;
legs[0].rotation.x = swing;
legs[1].rotation.x = -swing;
arms[0].rotation.x = carry ? -1.02 : walking ? -swing * 0.5 : -0.08;
arms[1].rotation.x = carry ? -1.02 : walking ? swing * 0.5 : -0.08;
arms[0].rotation.z = reach ? -0.12 : 0;
arms[1].rotation.z = reach ? 0.12 : 0;
}
const timeline = {
toBagStart: 0.7,
toBagEnd: 3.8,
pickStart: 3.8,
pickEnd: 4.5,
toDrawerStart: 4.5,
toDrawerEnd: 7.0,
openStart: 7.0,
openEnd: 8.0,
placeStart: 8.0,
placeEnd: 9.0,
closeStart: 9.0,
closeEnd: 10.4,
returnStart: 10.4,
returnEnd: 13.2
};
const totalDuration = timeline.returnEnd;
let animationTime = 0;
let playing = true;
let placed = false;
let inspectionOpen = false;
let routeClear = true;
let storedValidation = null;
function localBagBounds() {
const result = new THREE.Box3();
bag.updateWorldMatrix(true, true);
storageDrawer.updateWorldMatrix(true, false);
const inverseDrawer = storageDrawer.matrixWorld.clone().invert();
const point = new THREE.Vector3();
bag.traverse((object) => {
if (!object.isMesh || !object.geometry) return;
if (!object.geometry.boundingBox) object.geometry.computeBoundingBox();
const bounds = object.geometry.boundingBox;
for (let x = 0; x < 2; x++) {
for (let y = 0; y < 2; y++) {
for (let z = 0; z < 2; z++) {
point.set(
x ? bounds.max.x : bounds.min.x,
y ? bounds.max.y : bounds.min.y,
z ? bounds.max.z : bounds.min.z
);
point.applyMatrix4(object.matrixWorld);
point.applyMatrix4(inverseDrawer);
result.expandByPoint(point);
}
}
}
});
return result;
}
function validateStoredBag() {
if (!placed || bag.parent !== storageDrawer) {
return { contained: false, checked: false, bounds: null, frontGap: null };
}
const bounds = localBagBounds();
const contained = cavityBounds.containsBox(bounds);
const frontGap = frontPanelBounds.min.x - bounds.max.x;
const clearOfFront = !bounds.intersectsBox(frontPanelBounds) && frontGap > 0;
return {
contained: contained && clearOfFront,
checked: true,
bounds,
frontGap,
clearOfFront
};
}
const routeBounds = new THREE.Box3(
new THREE.Vector3(0.58, 0, -3.95),
new THREE.Vector3(1.52, 2.15, 3.05)
);
const routeObstacles = [bed, desk, chair, suitcase, ...drawerGroups];
function checkRouteGeometry() {
scene.updateMatrixWorld(true);
for (const obstacle of routeObstacles) {
const obstacleBounds = new THREE.Box3().setFromObject(obstacle);
if (obstacleBounds.intersectsBox(routeBounds)) return false;
}
return true;
}
function updateVisitor(time) {
let walking = false;
let carry = false;
let reach = false;
if (time < timeline.toBagStart) {
visitor.position.set(1.05, 0, -3.68);
setFacing(0);
} else if (time < timeline.toBagEnd) {
const u = smooth01((time - timeline.toBagStart) / (timeline.toBagEnd - timeline.toBagStart));
const sample = samplePath(pathToBag, u);
visitor.position.copy(sample.position);
setFacing(Math.atan2(sample.direction.x, sample.direction.z));
walking = true;
} else if (time < timeline.pickEnd) {
visitor.position.set(2.25, 0, 0.62);
setFacing(Math.PI / 2);
reach = true;
} else if (time < timeline.toDrawerEnd) {
const u = smooth01((time - timeline.toDrawerStart) / (timeline.toDrawerEnd - timeline.toDrawerStart));
const sample = samplePath(pathToDrawer, u);
visitor.position.copy(sample.position);
setFacing(Math.atan2(sample.direction.x, sample.direction.z));
walking = true;
carry = true;
} else if (time < timeline.returnStart) {
visitor.position.set(0.28, 0, -0.30);
setFacing(-Math.PI / 2);
carry = time < timeline.placeEnd;
reach = time >= timeline.openStart;
} else {
const u = smooth01((time - timeline.returnStart) / (timeline.returnEnd - timeline.returnStart));
const sample = samplePath(pathToDoor, u);
visitor.position.copy(sample.position);
setFacing(Math.atan2(sample.direction.x, sample.direction.z));
walking = true;
}
poseVisitor(walking, time, carry, reach);
}
function updateDrawer(time) {
let extension = 0;
if (time >= timeline.openStart && time < timeline.openEnd) {
extension = DRAWER_OPEN_DISTANCE *
smooth01((time - timeline.openStart) / (timeline.openEnd - timeline.openStart));
} else if (time >= timeline.openEnd && time < timeline.closeStart) {
extension = DRAWER_OPEN_DISTANCE;
} else if (time >= timeline.closeStart && time < timeline.closeEnd) {
extension = DRAWER_OPEN_DISTANCE *
(1 - smooth01((time - timeline.closeStart) / (timeline.closeEnd - timeline.closeStart)));
} else if (time >= totalDuration && inspectionOpen) {
extension = DRAWER_OPEN_DISTANCE;
}
storageDrawer.position.x = CLOSED_DRAWER_X + extension;
}
function updateBag(time) {
visitor.updateMatrixWorld(true);
if (!placed) {
const carryPosition = visitor.localToWorld(new THREE.Vector3(0, 1.08, 0.54));
const carryQuaternion = visitor.quaternion.clone();
if (time < timeline.pickStart) {
bag.position.copy(bagStart);
bag.quaternion.identity();
} else if (time < timeline.pickEnd) {
const u = smooth01((time - timeline.pickStart) / (timeline.pickEnd - timeline.pickStart));
bag.position.lerpVectors(bagStart, carryPosition, u);
bag.position.y += Math.sin(u * Math.PI) * 0.10;
bag.quaternion.slerpQuaternions(new THREE.Quaternion(), carryQuaternion, u);
} else if (time < timeline.placeStart) {
bag.position.copy(carryPosition);
bag.quaternion.copy(carryQuaternion);
} else {
const u = smooth01((time - timeline.placeStart) / (timeline.placeEnd - timeline.placeStart));
const targetWorld = storageDrawer.localToWorld(targetLocal.clone());
bag.position.lerpVectors(carryPosition, targetWorld, u);
bag.position.y += Math.sin(u * Math.PI) * 0.10;
bag.quaternion.slerpQuaternions(carryQuaternion, new THREE.Quaternion(), u);
}
if (time >= timeline.placeEnd) {
storageDrawer.attach(bag);
bag.position.copy(targetLocal);
bag.quaternion.identity();
bag.updateMatrixWorld(true);
placed = true;
}
}
}
function updateHUD(time) {
progressFill.style.width = `${THREE.MathUtils.clamp(time / totalDuration, 0, 1) * 100}%`;
let title = "";
let detail = "";
let done = false;
if (time < timeline.toBagStart) {
title = "Entrada pela porta";
detail = "A mala e a bolsa permanecem fora do corredor central.";
} else if (time < timeline.toBagEnd) {
title = "Aproximando-se da mala";
detail = "O hóspede percorre o quarto sem mover os móveis do corredor.";
} else if (time < timeline.pickEnd) {
title = "Retirando a bolsa laranja";
detail = "A bolsa deixa a mala e passa a ser transportada manualmente.";
} else if (time < timeline.toDrawerEnd) {
title = "Levando a bolsa à gaveta";
detail = "A gaveta central será aberta somente quando a bolsa chegar.";
} else if (time < timeline.placeEnd) {
title = "Abrindo e posicionando a gaveta";
detail = "A bolsa é conduzida para o interior do tabuleiro da gaveta.";
} else if (time < timeline.closeEnd) {
title = "Fechando a gaveta";
detail = "A bolsa acompanha oдвиж da gaveta e permanece totalmente contida.";
} else if (time < totalDuration) {
title = "Bolsa guardada — retorno à porta";
detail = "A frente da gaveta está fechada e a rota central permanece livre.";
} else {
title = "Bolsa guardada com segurança";
detail = "A animação terminou. A bolsa não ultrapassa a frente nem o volume interno da gaveta.";
done = true;
}
if (inspectionOpen && done) {
title = "Inspeção da gaveta";
detail = "A gaveta pode ser aberta para conferir a bolsa; ela continua presa ao volume interno e não atravessa a frente.";
}
statusText.textContent = title;
detailText.textContent = detail;
statusBox.classList.toggle("done", done);
const drawerExtension = storageDrawer.position.x - CLOSED_DRAWER_X;
drawerState.textContent = drawerExtension > 0.015
? `Aberta · ${Math.round(drawerExtension / DRAWER_OPEN_DISTANCE * 100)}%`
: "Fechada";
if (!placed) {
bagState.textContent = time >= timeline.pickEnd ? "Em transporte" : "Sobre a mala";
bagState.className = time >= timeline.pickEnd ? "orange" : "";
} else {
bagState.textContent = "100% dentro da gaveta";
bagState.className = "good";
}
routeState.textContent = routeClear ? "Livre · corredor verificado" : "Obstáculo detectado";
routeState.className = routeClear ? "good" : "orange";
storedValidation = validateStoredBag();
if (!storedValidation.checked) {
containmentState.textContent = "Aguardando depósito";
containmentState.className = "";
} else if (storedValidation.contained) {
containmentState.textContent = `Aprovado · folga frontal ${Math.round(storedValidation.frontGap * 100)} cm`;
containmentState.className = "good";
} else {
containmentState.textContent = "Falha · volume fora da gaveta";
containmentState.className = "orange";
}
}
function resetAnimation() {
animationTime = 0;
playing = true;
placed = false;
inspectionOpen = false;
inspectButton.disabled = true;
inspectButton.textContent = "Inspecionar gaveta";
if (bag.parent !== scene) scene.attach(bag);
scene.add(bag);
bag.position.copy(bagStart);
bag.quaternion.identity();
bag.updateMatrixWorld(true);
storageDrawer.position.x = CLOSED_DRAWER_X;
visitor.position.set(1.05, 0, -3.68);
visitor.rotation.set(0, 0, 0);
updateHUD(0);
}
replayButton.addEventListener("click", resetAnimation);
inspectButton.addEventListener("click", () => {
if (!placed) return;
inspectionOpen = !inspectionOpen;
inspectButton.textContent = inspectionOpen ? "Fechar gaveta" : "Inspecionar gaveta";
});
cameraButton.addEventListener("click", () => {
orbit.radius = cameraHome.radius;
orbit.theta = cameraHome.theta;
orbit.phi = cameraHome.phi;
updateCamera();
});
// Controles de câmera implementados diretamente, sem addons.
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(orbit.phi + dy * 0.005, 0.34, 1.36);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(orbit.radius + event.deltaY * 0.009, 8.5, 23);
updateCamera();
}, { passive: false });
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
updateCamera();
});
// Diagnóstico acessível para inspeção dos estados reais da cena.
window.__underbedDiagnostics = {
getState() {
const validation = validateStoredBag();
return {
time: animationTime,
playing,
bagParent: bag.parent?.name || null,
placed,
drawerExtension: storageDrawer.position.x - CLOSED_DRAWER_X,
routeClear,
cavityBounds: {
min: cavityBounds.min.toArray(),
max: cavityBounds.max.toArray()
},
bagLocalBounds: validation.bounds ? {
min: validation.bounds.min.toArray(),
max: validation.bounds.max.toArray()
} : null,
frontPanelBounds: {
min: frontPanelBounds.min.toArray(),
max: frontPanelBounds.max.toArray()
},
frontGap: validation.frontGap,
fullyContained: validation.contained
};
}
};
const clock = new THREE.Clock();
function frame() {
const delta = Math.min(clock.getDelta(), 0.05);
if (playing) {
animationTime = Math.min(totalDuration, animationTime + delta);
if (animationTime >= totalDuration) {
playing = false;
inspectButton.disabled = !placed;
}
}
updateDrawer(animationTime);
updateVisitor(animationTime);
updateBag(animationTime);
scene.updateMatrixWorld(true);
routeClear = checkRouteGeometry();
updateHUD(animationTime);
renderer.render(scene, camera);
requestAnimationFrame(frame);
}
resetAnimation();
frame();
</script>
</body>
</html>
```