Prompt specification
Build or customize the Three.js 3D scene "Entryway shoe-change bench" 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: Entryway shoe-change bench
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/entryway-shoe-change-bench-x0171
Scene Description Prompt:
Please use Three.js from cdnjs for a stylized scene based on this idea: entryway shoe-change bench. Compose an entryway with bench, shoe cubbies, bags, and door mat; animate a visitor sitting, storing shoes, and leaving the central passage clear. Return one self-contained HTML file.
Technical checks: In the seated pose, place the hips on the bench and keep the head attached to the torso. Verify contact from a side view and leave the exit route clear.
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 (entryway-shoe-change-bench-x0171):
```html
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
<title>Vestíbulo — Banco de Troca</title>
<style>
:root {
--ink: #20313a;
--muted: #64747a;
--paper: rgba(255, 252, 246, 0.92);
--accent: #d75f4f;
--green: #347767;
--line: rgba(32, 49, 58, 0.13);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d8e2df;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active { cursor: grabbing; }
.panel {
position: fixed;
z-index: 5;
background: var(--paper);
border: 1px solid rgba(255, 255, 255, 0.65);
box-shadow: 0 16px 45px rgba(29, 47, 52, 0.15);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
#title-card {
top: 22px;
left: 22px;
width: min(360px, calc(100vw - 44px));
padding: 18px 20px 17px;
border-radius: 18px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 5px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(21px, 3vw, 29px);
line-height: 1.05;
letter-spacing: -0.035em;
}
#title-card p {
margin: 9px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
#passage-card {
top: 22px;
right: 22px;
max-width: min(310px, calc(100vw - 44px));
padding: 13px 16px;
border-radius: 16px;
display: flex;
align-items: center;
gap: 11px;
pointer-events: none;
}
.status-dot {
width: 11px;
height: 11px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 5px rgba(52, 119, 103, 0.13);
}
#passage-card strong {
display: block;
font-size: 13px;
}
#passage-card small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 11px;
}
#controls {
left: 50%;
bottom: 20px;
width: min(960px, calc(100vw - 32px));
padding: 12px;
border-radius: 19px;
transform: translateX(-50%);
}
#status-row {
min-height: 25px;
padding: 0 7px 8px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
color: var(--muted);
font-size: 12px;
}
#status {
color: var(--ink);
font-weight: 700;
}
#hint {
white-space: nowrap;
font-size: 11px;
}
.button-row {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
button {
appearance: none;
border: 1px solid var(--line);
border-radius: 11px;
padding: 10px 13px;
background: #fffdf8;
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition: transform 120ms ease, background 120ms ease, opacity 120ms ease;
}
button:hover:not(:disabled) {
transform: translateY(-1px);
background: #f6eee3;
}
button:active:not(:disabled) {
transform: translateY(0);
}
button.primary {
border-color: transparent;
background: var(--accent);
color: white;
}
button.primary:hover:not(:disabled) {
background: #c84f40;
}
button:disabled {
opacity: 0.42;
cursor: not-allowed;
}
.separator {
width: 1px;
align-self: stretch;
margin: 2px 2px;
background: var(--line);
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #d8e2df;
color: var(--ink);
font-weight: 700;
letter-spacing: 0.04em;
}
@media (max-width: 700px) {
#title-card {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px 15px;
}
#title-card p { display: none; }
#passage-card {
top: 108px;
right: 12px;
max-width: calc(100vw - 24px);
}
#controls {
bottom: 10px;
width: calc(100vw - 20px);
padding: 9px;
}
#hint { display: none; }
button {
flex: 1 1 auto;
padding: 9px 10px;
}
.separator { display: none; }
}
</style>
</head>
<body>
<div id="loading">Preparando o vestíbulo…</div>
<section id="title-card" class="panel">
<div class="eyebrow">Estudo de cena · vestíbulo</div>
<h1>Banco de troca</h1>
<p>
Banco com cubbies, bolsas e tapete. Observe o visitante sentando,
guardando os calçados e liberando a passagem central.
</p>
</section>
<section id="passage-card" class="panel" aria-live="polite">
<span class="status-dot"></span>
<div>
<strong>Passagem central preservada</strong>
<small id="passage-detail">Nenhum objeto no trajeto da porta</small>
</div>
</section>
<section id="controls" class="panel" aria-label="Controles da cena">
<div id="status-row">
<span id="status" aria-live="polite">Visitante sentado no banco</span>
<span id="hint">Arraste para orbitar · roda para zoom</span>
</div>
<div class="button-row">
<button id="btn-cycle" class="primary" type="button">Ciclo completo</button>
<button id="btn-sit" type="button">Repetir sentar</button>
<button id="btn-store" type="button">Guardar calçados</button>
<button id="btn-exit" type="button">Sair do corredor</button>
<span class="separator"></span>
<button id="btn-pause" type="button">Pausar</button>
<button id="btn-side" type="button">Vista lateral</button>
<button id="btn-default" type="button">Câmera geral</button>
<button id="btn-reset" type="button">Reiniciar</button>
</div>
</section>
<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 statusEl = $("status");
const passageDetail = $("passage-detail");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd8e2df);
scene.fog = new THREE.Fog(0xd8e2df, 15, 28);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 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.12;
renderer.domElement.id = "scene";
document.body.prepend(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
60
);
const hemi = new THREE.HemisphereLight(0xf8f4e8, 0x53666b, 2.25);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xfff0d7, 3.15);
key.position.set(4.5, 8.5, 5.2);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -8;
key.shadow.camera.right = 8;
key.shadow.camera.top = 8;
key.shadow.camera.bottom = -8;
key.shadow.camera.near = 0.5;
key.shadow.camera.far = 25;
key.shadow.normalBias = 0.025;
scene.add(key);
const fill = new THREE.DirectionalLight(0xa8c9d6, 1.05);
fill.position.set(-4, 4, -2);
scene.add(fill);
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.76,
metalness: 0.0,
...options
});
const M = {
floor: material(0xcbb79b),
floorLine: material(0xb89f82),
wall: material(0xeee8dc),
wallSide: material(0xe2ddd1),
baseboard: material(0xf8f3e9),
wood: material(0xb7774e),
woodLight: material(0xd39a67),
woodDark: material(0x865138),
cubby: material(0x68483b),
teal: material(0x4f817b),
tealDark: material(0x345d59),
mat: material(0x526c70, { roughness: 0.96 }),
matStripe: material(0xe7d6b7),
brass: material(0xc49a54, { metalness: 0.72, roughness: 0.28 }),
frame: material(0x34464c),
art: material(0xe7a45e),
artBlue: material(0x6e9ca4),
plant: material(0x537b58),
plantLight: material(0x729669),
skin: material(0xd79772),
hair: material(0x3d2b28),
shirt: material(0xd65f50),
pants: material(0x2f4055),
shoe: material(0xf0e6d5),
shoeDark: material(0x344451),
white: material(0xfffaf1),
black: material(0x20282c)
};
function addBox(w, h, d, mat, x, y, z, cast = true, receive = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), mat);
mesh.position.set(x, y, z);
mesh.castShadow = cast;
mesh.receiveShadow = receive;
scene.add(mesh);
return mesh;
}
function addCylinder(radiusTop, radiusBottom, height, mat, x, y, z, segments = 20) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusTop, radiusBottom, height, segments),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// ---------------------------------------------------------
// Room shell
// ---------------------------------------------------------
addBox(10, 0.16, 7.5, M.floor, 0, -0.08, 0, false, true);
for (let x = -4.4; x <= 4.5; x += 0.8) {
addBox(0.012, 0.008, 7.35, M.floorLine, x, 0.006, 0, false, true);
}
addBox(10, 4.45, 0.18, M.wall, 0, 2.17, -3.52, false, true);
addBox(0.18, 4.45, 7.5, M.wallSide, -5.0, 2.17, 0, false, true);
addBox(9.85, 0.16, 0.08, M.baseboard, 0, 0.09, -3.40, false, true);
addBox(0.08, 0.16, 7.35, M.baseboard, -4.88, 0.09, 0, false, true);
// Door and frame
addBox(1.72, 2.82, 0.10, M.tealDark, 1.35, 1.45, -3.36);
addBox(1.56, 2.68, 0.08, M.teal, 1.35, 1.41, -3.29);
addBox(0.13, 2.95, 0.16, M.baseboard, 0.43, 1.48, -3.27);
addBox(0.13, 2.95, 0.16, M.baseboard, 2.27, 1.48, -3.27);
addBox(1.97, 0.13, 0.16, M.baseboard, 1.35, 2.91, -3.27);
addBox(0.98, 0.66, 0.025, M.frame, 1.35, 2.13, -3.235, false, true);
addBox(0.88, 0.56, 0.03, material(0x91b8c0, {
roughness: 0.3,
emissive: 0x24454b,
emissiveIntensity: 0.22
}), 1.35, 2.13, -3.215, false, true);
addBox(0.035, 0.56, 0.035, M.baseboard, 1.35, 2.13, -3.185);
addBox(0.88, 0.035, 0.035, M.baseboard, 1.35, 2.13, -3.185);
const handle = new THREE.Mesh(
new THREE.TorusGeometry(0.105, 0.025, 10, 24, Math.PI),
M.brass
);
handle.position.set(1.96, 1.26, -3.16);
handle.rotation.z = -Math.PI / 2;
handle.castShadow = true;
scene.add(handle);
// Door mat, deliberately centered but not blocking the path.
addBox(1.88, 0.035, 0.78, M.mat, 1.35, 0.022, -2.82, false, true);
for (let i = 0; i < 5; i++) {
addBox(0.025, 0.007, 0.62, M.matStripe, 0.69 + i * 0.33, 0.044, -2.82, false, true);
}
// Wall art and hooks
addBox(1.06, 0.83, 0.07, M.woodDark, -3.48, 2.76, -3.38);
addBox(0.91, 0.68, 0.025, M.artBlue, -3.48, 2.76, -3.325, false, true);
addBox(0.30, 0.46, 0.028, M.art, -3.58, 2.70, -3.30, false, true);
addBox(0.12, 0.34, 0.03, M.woodLight, -3.30, 2.79, -3.295, false, true);
addBox(0.92, 0.10, 0.08, M.woodDark, -0.55, 2.42, -3.35);
for (const x of [-0.83, -0.55, -0.27]) {
addCylinder(0.025, 0.025, 0.15, M.brass, x, 2.32, -3.25, 10);
}
// ---------------------------------------------------------
// Bench and open shoe cubbies
// ---------------------------------------------------------
const benchX = -2.65;
const benchZ = -2.45;
// Dark cubby cavity and shelves
addBox(2.48, 0.82, 0.08, M.cubby, benchX, 0.49, -2.91, false, true);
addBox(0.10, 0.88, 0.88, M.woodDark, -3.87, 0.45, -2.48);
addBox(0.10, 0.88, 0.88, M.woodDark, -1.43, 0.45, -2.48);
addBox(0.09, 0.78, 0.84, M.woodDark, -2.65, 0.48, -2.49);
addBox(2.42, 0.08, 0.86, M.wood, benchX, 0.50, -2.48);
addBox(2.42, 0.08, 0.86, M.wood, benchX, 0.10, -2.48);
// Seat, back and legs
addBox(2.58, 0.17, 1.10, M.woodLight, benchX, 0.945, benchZ);
addBox(2.58, 0.58, 0.12, M.wood, benchX, 1.37, -2.97);
for (const x of [-3.72, -1.58]) {
for (const z of [-2.80, -2.10]) {
addBox(0.13, 0.87, 0.13, M.woodDark, x, 0.435, z);
}
}
// A pair of slippers on the floor, later moved into a cubby.
const looseShoes = new THREE.Group();
scene.add(looseShoes);
function createLooseShoe() {
const g = new THREE.Group();
const sole = new THREE.Mesh(
new THREE.BoxGeometry(0.28, 0.075, 0.52),
M.shoe
);
sole.position.y = 0.0375;
sole.castShadow = true;
sole.receiveShadow = true;
g.add(sole);
const upper = new THREE.Mesh(
new THREE.SphereGeometry(1, 18, 12),
M.shoeDark
);
upper.scale.set(0.145, 0.135, 0.245);
upper.position.set(0, 0.135, 0.035);
upper.castShadow = true;
g.add(upper);
const heel = new THREE.Mesh(
new THREE.SphereGeometry(1, 14, 10),
M.shoeDark
);
heel.scale.set(0.135, 0.11, 0.11);
heel.position.set(0, 0.115, -0.13);
heel.castShadow = true;
g.add(heel);
for (let i = 0; i < 3; i++) {
const lace = new THREE.Mesh(
new THREE.BoxGeometry(0.17, 0.015, 0.018),
M.white
);
lace.position.set(0, 0.235 - i * 0.006, 0.015 + i * 0.045);
lace.castShadow = true;
g.add(lace);
}
return g;
}
const looseShoeA = createLooseShoe();
const looseShoeB = createLooseShoe();
looseShoeA.position.x = -0.145;
looseShoeB.position.x = 0.145;
looseShoes.add(looseShoeA, looseShoeB);
// Tote bag on the bench
const tote = new THREE.Group();
tote.position.set(-1.77, 1.32, -2.43);
tote.rotation.y = -0.12;
scene.add(tote);
const toteBody = new THREE.Mesh(
new THREE.BoxGeometry(0.55, 0.49, 0.27),
material(0xc48b52, { roughness: 0.9 })
);
toteBody.position.y = 0.25;
toteBody.castShadow = true;
tote.add(toteBody);
for (const z of [-0.145, 0.145]) {
const handle = new THREE.Mesh(
new THREE.TorusGeometry(0.16, 0.025, 9, 24, Math.PI),
M.woodDark
);
handle.position.set(0, 0.46, z);
handle.castShadow = true;
tote.add(handle);
}
// Duffel near the left wall
const duffel = new THREE.Group();
duffel.position.set(-4.30, 0.31, -1.18);
duffel.rotation.y = 0.18;
scene.add(duffel);
const duffelBody = new THREE.Mesh(
new THREE.SphereGeometry(1, 20, 14),
material(0x687d68)
);
duffelBody.scale.set(0.47, 0.31, 0.67);
duffelBody.castShadow = true;
duffel.add(duffelBody);
const duffelHandle = new THREE.Mesh(
new THREE.TorusGeometry(0.28, 0.032, 10, 28, Math.PI),
M.woodDark
);
duffelHandle.position.set(0, 0.25, 0);
duffelHandle.rotation.z = Math.PI;
duffelHandle.castShadow = true;
duffel.add(duffelHandle);
// Small plant at the far right, outside the central passage.
const plantPot = addCylinder(0.31, 0.24, 0.52, M.art, 3.72, 0.26, -3.00);
plantPot.castShadow = true;
for (let i = 0; i < 7; i++) {
const leaf = new THREE.Mesh(
new THREE.SphereGeometry(1, 14, 10),
i % 2 ? M.plant : M.plantLight
);
const a = (i / 7) * Math.PI * 2;
leaf.scale.set(0.10, 0.48, 0.15);
leaf.position.set(
3.72 + Math.cos(a) * 0.18,
0.80 + (i % 3) * 0.09,
-3.00 + Math.sin(a) * 0.18
);
leaf.rotation.z = Math.cos(a) * 0.52;
leaf.rotation.x = Math.sin(a) * 0.52;
leaf.castShadow = true;
scene.add(leaf);
}
// ---------------------------------------------------------
// Visitor rig
//
// All body segments are continuously connected by endpoints.
// In the seated pose, the pelvis bottom is exactly at the
// bench top (1.03), and the neck ends inside the head mesh.
// ---------------------------------------------------------
const rig = new THREE.Group();
scene.add(rig);
const sphere = new THREE.SphereGeometry(1, 24, 16);
const unitCylinder = new THREE.CylinderGeometry(1, 1, 1, 16);
function makeSphereMesh(mat, sx, sy, sz) {
const mesh = new THREE.Mesh(sphere, mat);
mesh.scale.set(sx, sy, sz);
mesh.castShadow = true;
mesh.receiveShadow = true;
rig.add(mesh);
return mesh;
}
function makeSegmentMesh(mat) {
const mesh = new THREE.Mesh(unitCylinder, mat);
mesh.castShadow = true;
mesh.receiveShadow = true;
rig.add(mesh);
return mesh;
}
const pelvisMesh = makeSphereMesh(M.pants, 0.29, 0.19, 0.23);
const torsoMesh = makeSegmentMesh(M.shirt);
const shoulderPadMesh = makeSphereMesh(M.shirt, 0.31, 0.16, 0.22);
const neckMesh = makeSegmentMesh(M.skin);
const upperArmL = makeSegmentMesh(M.shirt);
const upperArmR = makeSegmentMesh(M.shirt);
const forearmL = makeSegmentMesh(M.skin);
const forearmR = makeSegmentMesh(M.skin);
const handLMesh = makeSphereMesh(M.skin, 0.105, 0.105, 0.105);
const handRMesh = makeSphereMesh(M.skin, 0.105, 0.105, 0.105);
const thighL = makeSegmentMesh(M.pants);
const thighR = makeSegmentMesh(M.pants);
const shinL = makeSegmentMesh(M.pants);
const shinR = makeSegmentMesh(M.pants);
function makeFoot() {
const g = new THREE.Group();
const sole = new THREE.Mesh(
new THREE.BoxGeometry(0.27, 0.09, 0.48),
M.shoe
);
sole.position.set(0, 0.045, 0.05);
sole.castShadow = true;
sole.receiveShadow = true;
g.add(sole);
const upper = new THREE.Mesh(
new THREE.SphereGeometry(1, 16, 10),
M.shoeDark
);
upper.scale.set(0.135, 0.115, 0.225);
upper.position.set(0, 0.125, 0.075);
upper.castShadow = true;
g.add(upper);
rig.add(g);
return g;
}
const footL = makeFoot();
const footR = makeFoot();
const headGroup = new THREE.Group();
rig.add(headGroup);
const headMesh = new THREE.Mesh(sphere, M.skin);
headMesh.scale.setScalar(0.27);
headMesh.castShadow = true;
headGroup.add(headMesh);
const hairMesh = new THREE.Mesh(sphere, M.hair);
hairMesh.scale.set(0.285, 0.17, 0.275);
hairMesh.position.y = 0.145;
hairMesh.castShadow = true;
headGroup.add(hairMesh);
for (const x of [-0.265, 0.265]) {
const ear = new THREE.Mesh(sphere, M.skin);
ear.scale.set(0.055, 0.085, 0.05);
ear.position.x = x;
ear.castShadow = true;
headGroup.add(ear);
}
for (const x of [-0.085, 0.085]) {
const eye = new THREE.Mesh(new THREE.SphereGeometry(0.031, 12, 8), M.black);
eye.position.set(x, 0.02, 0.248);
headGroup.add(eye);
}
const nose = new THREE.Mesh(new THREE.SphereGeometry(0.042, 12, 8), M.skin);
nose.position.set(0, -0.045, 0.275);
nose.castShadow = true;
headGroup.add(nose);
const POSE_STAND = {
pelvis: [0, 1.03, 0],
shoulder: [0, 1.83, 0],
head: [0, 2.25, 0.04],
kneeL: [-0.14, 0.56, 0],
kneeR: [0.14, 0.56, 0],
ankleL: [-0.15, 0.10, 0],
ankleR: [0.15, 0.10, 0],
elbowL: [-0.37, 1.30, 0.04],
elbowR: [0.37, 1.30, 0.04],
handL: [-0.42, 0.83, 0.08],
handR: [0.42, 0.83, 0.08]
};
const POSE_SEATED = {
pelvis: [0, 1.22, 0],
shoulder: [0, 1.88, 0.08],
head: [0, 2.31, 0.11],
kneeL: [-0.14, 0.74, 0.76],
kneeR: [0.14, 0.74, 0.76],
ankleL: [-0.15, 0.10, 0.98],
ankleR: [0.15, 0.10, 0.98],
elbowL: [-0.38, 1.28, 0.40],
elbowR: [0.38, 1.28, 0.40],
handL: [-0.25, 0.84, 0.70],
handR: [0.25, 0.84, 0.70]
};
const POSE_BEND = {
pelvis: [0, 0.65, 0],
shoulder: [0, 1.25, 0.18],
head: [0, 1.67, 0.23],
kneeL: [-0.14, 0.42, 0.04],
kneeR: [0.14, 0.42, 0.04],
ankleL: [-0.15, 0.10, 0.02],
ankleR: [0.15, 0.10, 0.02],
elbowL: [-0.36, 0.86, 0.10],
elbowR: [0.36, 0.86, 0.38],
handL: [-0.40, 0.42, 0.16],
handR: [0.18, 0.42, 0.34]
};
function clonePose(pose) {
const result = {};
for (const key of Object.keys(pose)) {
result[key] = pose[key].slice();
}
return result;
}
function lerpPose(a, b, t) {
const result = {};
for (const key of Object.keys(a)) {
result[key] = a[key].map((v, i) => THREE.MathUtils.lerp(v, b[key][i], t));
}
return result;
}
const up = new THREE.Vector3(0, 1, 0);
const tempA = new THREE.Vector3();
const tempB = new THREE.Vector3();
const tempDir = new THREE.Vector3();
const tempMid = new THREE.Vector3();
function setSegment(mesh, a, b, radiusX, radiusZ = radiusX) {
tempA.fromArray(a);
tempB.fromArray(b);
tempDir.subVectors(tempB, tempA);
const length = Math.max(tempDir.length(), 0.0001);
tempMid.addVectors(tempA, tempB).multiplyScalar(0.5);
mesh.position.copy(tempMid);
mesh.quaternion.setFromUnitVectors(up, tempDir.normalize());
mesh.scale.set(radiusX, length, radiusZ);
}
let root = { x: -2.75, z: -2.45, rot: 0 };
let currentPose = clonePose(POSE_SEATED);
let shoesStored = false;
let paused = false;
let busy = false;
let userInteracted = false;
function applyPose(pose, rootState = root) {
rig.position.set(rootState.x, 0, rootState.z);
rig.rotation.y = rootState.rot;
pelvisMesh.position.fromArray(pose.pelvis);
setSegment(torsoMesh, pose.pelvis, pose.shoulder, 0.31, 0.235);
shoulderPadMesh.position.fromArray(pose.shoulder);
// The neck terminates inside the head volume, preventing a visible seam.
setSegment(neckMesh, pose.shoulder, pose.head, 0.095);
headGroup.position.fromArray(pose.head);
const shoulderL = [pose.shoulder[0] - 0.275, pose.shoulder[1] - 0.025, pose.shoulder[2]];
const shoulderR = [pose.shoulder[0] + 0.275, pose.shoulder[1] - 0.025, pose.shoulder[2]];
const hipL = [pose.pelvis[0] - 0.145, pose.pelvis[1], pose.pelvis[2]];
const hipR = [pose.pelvis[0] + 0.145, pose.pelvis[1], pose.pelvis[2]];
setSegment(upperArmL, shoulderL, pose.elbowL, 0.105);
setSegment(upperArmR, shoulderR, pose.elbowR, 0.105);
setSegment(forearmL, pose.elbowL, pose.handL, 0.082);
setSegment(forearmR, pose.elbowR, pose.handR, 0.082);
handLMesh.position.fromArray(pose.handL);
handRMesh.position.fromArray(pose.handR);
setSegment(thighL, hipL, pose.kneeL, 0.13);
setSegment(thighR, hipR, pose.kneeR, 0.13);
setSegment(shinL, pose.kneeL, pose.ankleL, 0.105);
setSegment(shinR, pose.kneeR, pose.ankleR, 0.105);
footL.position.set(pose.ankleL[0], pose.ankleL[1] - 0.08, pose.ankleL[2] + 0.08);
footR.position.set(pose.ankleR[0], pose.ankleR[1] - 0.08, pose.ankleR[2] + 0.08);
}
applyPose(currentPose);
// ---------------------------------------------------------
// Camera controls implemented without addons
// ---------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(-0.35, 1.10, -0.45),
theta: 0.67,
phi: 1.13,
radius: 11.6
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
updateCamera();
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.55,
1.43
);
updateCamera();
});
renderer.domElement.addEventListener("pointerup", (event) => {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
});
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.008,
3.3,
16
);
updateCamera();
}, { passive: false });
function generalCamera() {
cameraState.target.set(-0.35, 1.10, -0.45);
cameraState.theta = 0.67;
cameraState.phi = 1.13;
cameraState.radius = 11.6;
updateCamera();
}
function sideCamera() {
cameraState.target.set(-2.65, 1.28, -2.02);
cameraState.theta = Math.PI / 2;
cameraState.phi = 1.32;
cameraState.radius = 4.9;
updateCamera();
statusEl.textContent = "Vista lateral ativa — verificar apoio do quadril e conexão cabeça–tronco";
}
// ---------------------------------------------------------
// Small pause-aware tween engine
// ---------------------------------------------------------
const activeTweens = [];
const easeInOut = (t) =>
t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
function tween(duration, update, easing = easeInOut) {
return new Promise((resolve) => {
activeTweens.push({ elapsed: 0, duration, update, easing, resolve });
});
}
function updateTweens(dt) {
if (paused) return;
for (let i = activeTweens.length - 1; i >= 0; i--) {
const tw = activeTweens[i];
tw.elapsed += dt;
const raw = Math.min(tw.elapsed / tw.duration, 1);
tw.update(tw.easing(raw), raw);
if (raw >= 1) {
activeTweens.splice(i, 1);
tw.resolve();
}
}
}
function lerpAngle(a, b, t) {
const delta = Math.atan2(Math.sin(b - a), Math.cos(b - a));
return a + delta * t;
}
function updateStatus(text) {
statusEl.textContent = text;
}
function setBusy(value) {
busy = value;
$("btn-cycle").disabled = value;
$("btn-sit").disabled = value;
$("btn-store").disabled = value || shoesStored;
$("btn-exit").disabled = value;
$("btn-reset").disabled = value;
}
function resetData() {
root = { x: -2.75, z: -2.45, rot: 0 };
currentPose = clonePose(POSE_SEATED);
shoesStored = false;
looseShoes.position.set(-2.16, 0, -1.89);
looseShoes.rotation.y = 0.06;
applyPose(currentPose);
passageDetail.textContent = "Nenhum objeto no trajeto da porta";
}
// Sit down in two connected phases. The final pelvis bottom is
// exactly on the bench top; head and torso remain one connected rig.
async function sitSequence() {
updateStatus("Levantando em frente ao banco…");
const fromPose = clonePose(currentPose);
const fromRoot = { ...root };
await tween(0.82, (p) => {
currentPose = lerpPose(fromPose, POSE_STAND, p);
root.x = THREE.MathUtils.lerp(fromRoot.x, -2.75, p);
root.z = THREE.MathUtils.lerp(fromRoot.z, -1.55, p);
root.rot = lerpAngle(fromRoot.rot, 0, p);
applyPose(currentPose);
});
updateStatus("Sentando: quadril no assento, pés no piso…");
await tween(1.18, (p) => {
currentPose = lerpPose(POSE_STAND, POSE_SEATED, p);
root.x = THREE.MathUtils.lerp(-2.75, -2.75, p);
root.z = THREE.MathUtils.lerp(-1.55, -2.45, p);
applyPose(currentPose);
});
currentPose = clonePose(POSE_SEATED);
root = { x: -2.75, z: -2.45, rot: 0 };
applyPose(currentPose);
updateStatus("Sentado — quadril apoiado, tronco e cabeça conectados");
}
async function storeSequence() {
if (shoesStored) {
updateStatus("Os calçados já estão guardados no cubby");
return;
}
updateStatus("Levantando e aproximando-se dos cubbies…");
const seatedPose = clonePose(currentPose);
const seatedRoot = { ...root };
await tween(0.88, (p) => {
currentPose = lerpPose(seatedPose, POSE_STAND, p);
root.x = THREE.MathUtils.lerp(seatedRoot.x, -2.0, p);
root.z = THREE.MathUtils.lerp(seatedRoot.z, -1.55, p);
root.rot = lerpAngle(seatedRoot.rot, Math.PI, p);
applyPose(currentPose);
});
updateStatus("Agachando para recolher os calçados…");
const standPose = clonePose(currentPose);
await tween(0.72, (p) => {
currentPose = lerpPose(standPose, POSE_BEND, p);
applyPose(currentPose);
});
updateStatus("Recolhendo o par de calçados…");
const shoeStart = looseShoes.position.clone();
await tween(0.62, (p) => {
applyPose(currentPose);
rig.updateMatrixWorld(true);
const handWorld = rig.localToWorld(new THREE.Vector3().fromArray(currentPose.handR));
handWorld.y -= 0.15;
looseShoes.position.lerpVectors(shoeStart, handWorld, p);
looseShoes.rotation.y = THREE.MathUtils.lerp(0.06, 0.0, p);
});
updateStatus("Guardando os calçados no cubby…");
const carryStart = looseShoes.position.clone();
const reachPose = clonePose(currentPose);
await tween(0.88, (p) => {
currentPose = clonePose(reachPose);
currentPose.handR[2] = THREE.MathUtils.lerp(0.34, 0.86, p);
currentPose.elbowR[2] = THREE.MathUtils.lerp(0.38, 0.58, p);
applyPose(currentPose);
rig.updateMatrixWorld(true);
const handWorld = rig.localToWorld(new THREE.Vector3().fromArray(currentPose.handR));
handWorld.y -= 0.15;
looseShoes.position.copy(handWorld);
});
const carried = looseShoes.position.clone();
const shoeTarget = new THREE.Vector3(-2.12, 0.12, -2.43);
await tween(0.68, (p) => {
looseShoes.position.lerpVectors(carried, shoeTarget, p);
});
looseShoes.position.copy(shoeTarget);
shoesStored = true;
$("btn-store").disabled = busy || true;
passageDetail.textContent = "Calçados guardados; entrada da porta livre";
updateStatus("Soltando os calçados e endireitando o corpo…");
const bentPose = clonePose(currentPose);
await tween(0.55, (p) => {
currentPose = clonePose(bentPose);
currentPose.handR[2] = THREE.MathUtils.lerp(0.86, 0.34, p);
currentPose.elbowR[2] = THREE.MathUtils.lerp(0.58, 0.38, p);
applyPose(currentPose);
});
const bentPose2 = clonePose(currentPose);
await tween(0.65, (p) => {
currentPose = lerpPose(bentPose2, POSE_STAND, p);
applyPose(currentPose);
});
currentPose = clonePose(POSE_STAND);
applyPose(currentPose);
updateStatus("Calçados guardados — visitante em pé junto ao banco");
}
async function walkTo(duration, targetX, targetZ) {
const from = { ...root };
const targetYaw = Math.atan2(targetX - from.x, targetZ - from.z);
await tween(duration, (p, raw) => {
root.x = THREE.MathUtils.lerp(from.x, targetX, p);
root.z = THREE.MathUtils.lerp(from.z, targetZ, p);
root.rot = lerpAngle(from.rot, targetYaw, p);
currentPose = clonePose(POSE_STAND);
const stride = Math.sin(raw * Math.PI * 6);
const bob = Math.abs(Math.sin(raw * Math.PI * 6)) * 0.025;
currentPose.pelvis[1] += bob;
currentPose.shoulder[1] += bob;
currentPose.head[1] += bob;
currentPose.head[2] += 0.012;
currentPose.kneeL[2] = stride * 0.24;
currentPose.ankleL[2] = stride * 0.38;
currentPose.ankleL[1] = 0.10 + Math.max(0, stride) * 0.10;
currentPose.kneeR[2] = -stride * 0.24;
currentPose.ankleR[2] = -stride * 0.38;
currentPose.ankleR[1] = 0.10 + Math.max(0, -stride) * 0.10;
currentPose.elbowL[2] = -stride * 0.16;
currentPose.elbowR[2] = stride * 0.16;
currentPose.handL[2] = -stride * 0.23;
currentPose.handR[2] = stride * 0.23;
applyPose(currentPose);
});
}
async function exitSequence() {
if (currentPose.pelvis[1] < 1.15) {
updateStatus("Levantando antes de sair…");
const pose = clonePose(currentPose);
const oldRoot = { ...root };
await tween(0.80, (p) => {
currentPose = lerpPose(pose, POSE_STAND, p);
root.x = THREE.MathUtils.lerp(oldRoot.x, -2.0, p);
root.z = THREE.MathUtils.lerp(oldRoot.z, -1.55, p);
root.rot = lerpAngle(oldRoot.rot, Math.PI, p);
applyPose(currentPose);
});
}
updateStatus("Caminhando pela lateral, sem ocupar a passagem central…");
await walkTo(1.42, -3.48, -0.30);
await walkTo(1.36, -3.92, 2.55);
currentPose = clonePose(POSE_STAND);
applyPose(currentPose);
passageDetail.textContent = "Visitante recuado à lateral; trajeto da porta totalmente livre";
updateStatus("Visitante saiu do corredor central — passagem preservada");
}
async function performSit() {
if (busy) return;
userInteracted = true;
setBusy(true);
try {
await sitSequence();
} finally {
setBusy(false);
}
}
async function performStore() {
if (busy || shoesStored) return;
userInteracted = true;
setBusy(true);
try {
await storeSequence();
} finally {
setBusy(false);
}
}
async function performExit() {
if (busy) return;
userInteracted = true;
setBusy(true);
try {
await exitSequence();
} finally {
setBusy(false);
}
}
async function performCycle() {
if (busy) return;
userInteracted = true;
clearTimeout(autoTimer);
setBusy(true);
try {
resetData();
await sitSequence();
await storeSequence();
await exitSequence();
} finally {
setBusy(false);
}
}
function resetScene() {
if (busy) return;
userInteracted = true;
clearTimeout(autoTimer);
resetData();
updateStatus("Cena reiniciada — visitante sentado, calçados no piso");
}
$("btn-sit").addEventListener("click", performSit);
$("btn-store").addEventListener("click", performStore);
$("btn-exit").addEventListener("click", performExit);
$("btn-cycle").addEventListener("click", performCycle);
$("btn-reset").addEventListener("click", resetScene);
$("btn-pause").addEventListener("click", () => {
paused = !paused;
$("btn-pause").textContent = paused ? "Continuar" : "Pausar";
updateStatus(paused ? "Animação pausada — câmera ainda pode ser orbitada" : "Animação em execução");
});
$("btn-side").addEventListener("click", sideCamera);
$("btn-default").addEventListener("click", generalCamera);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
});
// A short automatic demonstration lets the corrected sitting pose
// be inspected immediately. All phases remain available as buttons.
const autoTimer = setTimeout(() => {
if (!userInteracted && !busy) performSit();
}, 1300);
// ---------------------------------------------------------
// Render loop
// ---------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
updateTweens(dt);
// A subtle breathing motion keeps the resting visitor alive.
if (!busy && activeTweens.length === 0 && !paused) {
const breath = Math.sin(clock.elapsedTime * 1.65) * 0.007;
const renderPose = clonePose(currentPose);
renderPose.shoulder[1] += breath;
renderPose.head[1] += breath;
renderPose.head[2] += breath * 0.25;
applyPose(renderPose);
}
updateCamera();
renderer.render(scene, camera);
}
$("loading").remove();
animate();
</script>
</body>
</html>
```