Prompt specification
Build or customize the Three.js 3D scene "Reading desk object-care routine" 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: Reading desk object-care routine
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/reading-desk-object-care-routine-x0587
Scene Description Prompt:
Use Three.js from cdnjs to compose glasses, case, cleaning cloth, book, and lamp; a character cleans the lenses, folds the frame, and places it in a matching protective case. Deliver one self-contained HTML file with visible voxel blocks.
Working Three.js Starter (reading-desk-object-care-routine-x0587):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Optical Care</title>
<style>
* { box-sizing: border-box; }
html, body {
margin: 0;
width: 100%;
height: 100%;
overflow: hidden;
background: #101a2b;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
#ui {
position: fixed;
inset: 0;
pointer-events: none;
color: #f5f7fb;
}
.panel {
position: absolute;
top: 20px;
left: 20px;
width: min(390px, calc(100vw - 40px));
padding: 18px 20px;
border: 1px solid rgba(255,255,255,.13);
border-radius: 18px;
background: rgba(13, 22, 38, .78);
box-shadow: 0 18px 45px rgba(0,0,0,.25);
backdrop-filter: blur(12px);
}
.eyebrow {
color: #73ddd2;
font-size: 11px;
font-weight: 800;
letter-spacing: .18em;
text-transform: uppercase;
}
h1 {
margin: 6px 0 8px;
font-size: clamp(24px, 4vw, 34px);
line-height: 1;
letter-spacing: -.04em;
}
#phase {
min-height: 22px;
margin: 0;
color: #cbd5e4;
font-size: 14px;
}
.track {
height: 6px;
margin: 15px 0 13px;
overflow: hidden;
border-radius: 999px;
background: rgba(255,255,255,.1);
}
#progress {
width: 0%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #54cfc4, #f4c56b);
}
.buttons {
display: flex;
flex-wrap: wrap;
gap: 8px;
pointer-events: auto;
}
button {
padding: 9px 12px;
border: 1px solid rgba(255,255,255,.14);
border-radius: 10px;
color: white;
background: rgba(255,255,255,.08);
cursor: pointer;
font: inherit;
font-size: 12px;
font-weight: 700;
transition: background .18s, transform .18s;
}
button:hover {
background: rgba(255,255,255,.15);
transform: translateY(-1px);
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-top: 14px;
}
.chip {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 5px 8px;
border-radius: 999px;
color: #d9e2ee;
background: rgba(255,255,255,.055);
font-size: 10px;
font-weight: 650;
}
.dot {
width: 7px;
height: 7px;
border-radius: 2px;
background: var(--c);
}
#hint {
position: absolute;
right: 20px;
bottom: 18px;
padding: 9px 12px;
border: 1px solid rgba(255,255,255,.1);
border-radius: 10px;
color: #b9c5d5;
background: rgba(13,22,38,.66);
font-size: 11px;
backdrop-filter: blur(10px);
}
@media (max-width: 600px) {
.panel { top: 12px; left: 12px; width: calc(100vw - 24px); }
#hint { right: 12px; bottom: 12px; }
}
</style>
</head>
<body>
<div id="ui">
<section class="panel">
<div class="eyebrow">Voxel Optical Care</div>
<h1>A Careful Pair</h1>
<p id="phase">Reaching for the cleaning cloth…</p>
<div class="track"><div id="progress"></div></div>
<div class="buttons">
<button id="replay" type="button">Replay animation</button>
<button id="resetView" type="button">Reset camera</button>
</div>
<div class="legend">
<span class="chip"><i class="dot" style="--c:#26384d"></i>Matching frame + case</span>
<span class="chip"><i class="dot" style="--c:#72d8d0"></i>Cleaning cloth</span>
<span class="chip"><i class="dot" style="--c:#c95353"></i>Open book</span>
<span class="chip"><i class="dot" style="--c:#f1c45b"></i>Voxel lamp</span>
</div>
</section>
<div id="hint">Drag to orbit · Scroll or pinch to zoom · R to replay</div>
</div>
<script type="module">
import * as THREE from
'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js';
// ---------------------------------------------------------------------
// Renderer, camera, lighting
// ---------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true });
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.05;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x101a2b);
scene.fog = new THREE.Fog(0x101a2b, 19, 35);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 2.15, 0.25);
let cameraTheta = 0.70;
let cameraPhi = 1.02;
let cameraRadius = 14.6;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraTheta = 0.70;
cameraPhi = 1.02;
cameraRadius = 14.6;
}
updateCamera();
scene.add(new THREE.HemisphereLight(0xcce8ff, 0x4b3428, 2.0));
const keyLight = new THREE.DirectionalLight(0xfff2dc, 3.2);
keyLight.position.set(5, 12, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 11;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 32;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x69c8ff, 1.5);
rimLight.position.set(-8, 6, -7);
scene.add(rimLight);
// ---------------------------------------------------------------------
// Shared voxel construction helpers
// ---------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const Y_AXIS = new THREE.Vector3(0, 1, 0);
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
...options
});
}
function voxel(w, h, d, mat, parent = scene, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(unitBox, mat);
mesh.scale.set(w, h, d);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
const mats = {
platform: material(0x172538),
floorA: material(0x2e4258),
floorB: material(0x344a61),
floorC: material(0x293d52),
wood: material(0x9d6744),
woodLight: material(0xbe8054),
frame: material(0x26384d, { roughness: 0.62, metalness: 0.12 }),
frameEdge: material(0x314b63, { roughness: 0.58, metalness: 0.10 }),
lining: material(0x59b9ad),
liningDark: material(0x337c78),
lens: material(0x9de8ed, {
transparent: true,
opacity: 0.28,
roughness: 0.18,
metalness: 0.05,
depthWrite: false
}),
cloth: material(0x72d8d0),
clothLight: material(0xa4ece6),
clothStripe: material(0xf4eee1),
skin: material(0xd69a78),
skinLight: material(0xe8b391),
hair: material(0x322626),
jacket: material(0x3d8e87),
jacketDark: material(0x28635f),
shirt: material(0xe8e0cf),
pants: material(0x344258),
shoes: material(0x1f2936),
eye: material(0x101820),
eyeWhite: material(0xf7f1e6),
bookCover: material(0xb94f50),
bookPage: material(0xeee3ca),
bookInk: material(0x7c7468),
bookmark: material(0xf0ba58),
lamp: material(0xe8b94f),
lampDark: material(0x77612d),
bulb: material(0xffefad, {
emissive: 0xffcf68,
emissiveIntensity: 2.4
})
};
// ---------------------------------------------------------------------
// Tiled voxel room and work table
// ---------------------------------------------------------------------
voxel(15.5, 0.38, 11.5, mats.platform, scene, 0, -0.27, 0.3);
for (let ix = -7; ix <= 7; ix++) {
for (let iz = -5; iz <= 5; iz++) {
const m = (ix + iz) % 3 === 0
? mats.floorC
: ((ix + iz) % 2 === 0 ? mats.floorA : mats.floorB);
voxel(
0.98, 0.075, 0.98,
m,
scene,
ix * 1.03,
0.015,
iz * 1.03 + 0.3
);
}
}
const table = new THREE.Group();
table.position.set(0, 0, 0.45);
scene.add(table);
voxel(11.5, 0.30, 5.6, mats.wood, table, 0, 2.55, 0);
voxel(11.55, 0.10, 5.65, mats.woodLight, table, 0, 2.38, 0);
voxel(10.9, 0.43, 0.18, mats.woodLight, table, 0, 2.22, 2.64);
voxel(10.9, 0.43, 0.18, mats.woodLight, table, 0, 2.22, -2.64);
for (const x of [-4.85, 4.85]) {
for (const z of [-2.08, 2.08]) {
voxel(0.48, 2.42, 0.48, mats.wood, table, x, 1.2, z);
voxel(0.68, 0.20, 0.68, mats.woodLight, table, x, 0.13, z);
}
}
// ---------------------------------------------------------------------
// Open voxel book
// ---------------------------------------------------------------------
const book = new THREE.Group();
book.position.set(-4.45, 2.71, -0.55);
book.rotation.y = -0.10;
scene.add(book);
voxel(1.22, 0.13, 1.62, mats.bookCover, book, -0.61, 0.065, 0);
voxel(1.22, 0.13, 1.62, mats.bookCover, book, 0.61, 0.065, 0);
voxel(1.06, 0.23, 1.39, mats.bookPage, book, -0.57, 0.215, 0);
voxel(1.06, 0.23, 1.39, mats.bookPage, book, 0.57, 0.215, 0);
voxel(0.10, 0.25, 1.39, mats.bookCover, book, 0, 0.22, 0);
for (const side of [-1, 1]) {
for (let line = 0; line < 6; line++) {
voxel(
0.70 - (line % 2) * 0.12,
0.025,
0.035,
mats.bookInk,
book,
side * 0.57,
0.345,
-0.52 + line * 0.19
);
}
}
voxel(0.16, 0.035, 0.85, mats.bookmark, book, 0.18, 0.35, 0.12);
// ---------------------------------------------------------------------
// Voxel desk lamp
// ---------------------------------------------------------------------
const lamp = new THREE.Group();
lamp.position.set(4.55, 2.71, -1.35);
scene.add(lamp);
voxel(1.18, 0.16, 1.02, mats.lampDark, lamp, 0, 0.08, 0);
voxel(0.88, 0.12, 0.76, mats.lamp, lamp, 0, 0.20, 0);
voxel(0.22, 2.15, 0.22, mats.lampDark, lamp, 0.36, 1.25, 0);
voxel(0.22, 0.22, 0.22, mats.lamp, lamp, 0.36, 2.28, 0);
voxel(1.08, 0.19, 0.22, mats.lampDark, lamp, -0.05, 2.39, 0);
voxel(1.08, 0.20, 0.88, mats.lamp, lamp, -0.42, 2.39, 0);
voxel(0.80, 0.20, 0.67, mats.lamp, lamp, -0.42, 2.56, 0);
voxel(0.52, 0.28, 0.48, mats.bulb, lamp, -0.42, 2.20, 0);
const lampLight = new THREE.PointLight(0xffc866, 26, 7, 2);
lampLight.position.set(-0.42, 2.03, 0);
lamp.add(lampLight);
// ---------------------------------------------------------------------
// Protective case, matching the glasses frame
// ---------------------------------------------------------------------
const caseRoot = new THREE.Group();
caseRoot.position.set(2.05, 2.71, -0.55);
scene.add(caseRoot);
voxel(2.80, 0.18, 1.55, mats.frame, caseRoot, 0, 0.09, 0);
voxel(0.18, 0.43, 1.20, mats.frame, caseRoot, -1.31, 0.35, 0);
voxel(0.18, 0.43, 1.20, mats.frame, caseRoot, 1.31, 0.35, 0);
voxel(2.45, 0.43, 0.18, mats.frame, caseRoot, 0, 0.35, -0.685);
voxel(2.45, 0.43, 0.18, mats.frame, caseRoot, 0, 0.35, 0.685);
voxel(2.42, 0.06, 1.08, mats.liningDark, caseRoot, 0, 0.21, 0);
voxel(0.07, 0.25, 1.05, mats.lining, caseRoot, -1.19, 0.33, 0);
voxel(0.07, 0.25, 1.05, mats.lining, caseRoot, 1.19, 0.33, 0);
voxel(2.38, 0.25, 0.07, mats.lining, caseRoot, 0, 0.33, -0.50);
voxel(2.38, 0.25, 0.07, mats.lining, caseRoot, 0, 0.33, 0.50);
voxel(0.40, 0.23, 0.16, mats.frameEdge, caseRoot, 0, 0.43, 0.83);
const caseLid = new THREE.Group();
caseLid.position.set(0, 0.55, -0.78);
caseLid.rotation.x = -1.78;
caseRoot.add(caseLid);
voxel(2.80, 0.18, 1.55, mats.frame, caseLid, 0, 0, 0.78);
voxel(2.43, 0.07, 1.10, mats.lining, caseLid, 0, -0.125, 0.78);
voxel(0.18, 0.12, 0.80, mats.frameEdge, caseLid, -1.30, -0.04, 0.78);
voxel(0.18, 0.12, 0.80, mats.frameEdge, caseLid, 1.30, -0.04, 0.78);
// Small pixel-style emblem inside the lid.
voxel(0.12, 0.035, 0.12, mats.lining, caseLid, -0.12, -0.175, 0.78);
voxel(0.12, 0.035, 0.12, mats.lining, caseLid, 0, -0.175, 0.78);
voxel(0.12, 0.035, 0.12, mats.lining, caseLid, 0.12, -0.175, 0.78);
// ---------------------------------------------------------------------
// Voxel glasses
// ---------------------------------------------------------------------
const glasses = new THREE.Group();
const glassesStart = new THREE.Vector3(-1.72, 2.72, -0.35);
glasses.position.copy(glassesStart);
scene.add(glasses);
function buildLensFrame(centerX) {
voxel(0.96, 0.08, 0.12, mats.frame, glasses, centerX, 0.68, 0);
voxel(0.96, 0.08, 0.12, mats.frame, glasses, centerX, 0.08, 0);
voxel(0.08, 0.60, 0.12, mats.frame, glasses, centerX - 0.46, 0.38, 0);
voxel(0.08, 0.60, 0.12, mats.frame, glasses, centerX + 0.46, 0.38, 0);
const lens = voxel(0.84, 0.50, 0.065, mats.lens, glasses, centerX, 0.38, 0);
lens.castShadow = false;
voxel(0.13, 0.035, 0.025, mats.lining, glasses, centerX - 0.18, 0.52, 0.052);
voxel(0.07, 0.025, 0.025, mats.lining, glasses, centerX + 0.16, 0.45, 0.053);
}
buildLensFrame(-0.52);
buildLensFrame(0.52);
voxel(0.29, 0.085, 0.12, mats.frame, glasses, 0, 0.56, 0);
const leftTemple = new THREE.Group();
leftTemple.position.set(-1.02, 0.62, 0);
glasses.add(leftTemple);
voxel(0.11, 0.11, 1.48, mats.frame, leftTemple, 0, 0, -0.74);
voxel(0.14, 0.14, 0.18, mats.frameEdge, leftTemple, 0, 0, -1.43);
const rightTemple = new THREE.Group();
rightTemple.position.set(1.02, 0.62, 0);
glasses.add(rightTemple);
voxel(0.11, 0.11, 1.48, mats.frame, rightTemple, 0, 0, -0.74);
voxel(0.14, 0.14, 0.18, mats.frameEdge, rightTemple, 0, 0, -1.43);
// ---------------------------------------------------------------------
// Cleaning cloth
// ---------------------------------------------------------------------
const cloth = new THREE.Group();
scene.add(cloth);
voxel(0.65, 0.08, 0.46, mats.cloth, cloth, 0, 0, 0);
voxel(0.18, 0.055, 0.39, mats.clothLight, cloth, -0.20, 0.055, 0);
voxel(0.065, 0.025, 0.46, mats.clothStripe, cloth, 0.14, 0.065, 0);
voxel(0.065, 0.025, 0.46, mats.clothStripe, cloth, 0.24, 0.065, 0);
// Tiny cleaning sparkles.
const sparkMaterial = new THREE.MeshBasicMaterial({
color: 0xfff2a8,
transparent: true,
opacity: 0,
depthTest: false
});
const sparks = [];
for (let i = 0; i < 7; i++) {
const spark = voxel(0.07, 0.07, 0.07, sparkMaterial, scene);
spark.castShadow = false;
spark.renderOrder = 5;
sparks.push(spark);
}
// ---------------------------------------------------------------------
// Voxel character
// ---------------------------------------------------------------------
const actor = new THREE.Group();
actor.position.set(-0.75, 0, -2.55);
scene.add(actor);
voxel(0.55, 0.30, 0.88, mats.shoes, actor, -0.34, 0.18, 0.12);
voxel(0.55, 0.30, 0.88, mats.shoes, actor, 0.34, 0.18, 0.12);
voxel(0.46, 1.52, 0.48, mats.pants, actor, -0.34, 1.04, 0);
voxel(0.46, 1.52, 0.48, mats.pants, actor, 0.34, 1.04, 0);
voxel(1.10, 0.34, 0.62, mats.pants, actor, 0, 1.78, 0);
voxel(1.34, 1.12, 0.76, mats.jacket, actor, 0, 2.40, 0.02);
voxel(0.76, 0.88, 0.13, mats.shirt, actor, 0, 2.42, 0.45);
voxel(0.48, 0.62, 0.14, mats.jacketDark, actor, -0.47, 2.30, 0.45);
voxel(0.48, 0.62, 0.14, mats.jacketDark, actor, 0.47, 2.30, 0.45);
voxel(0.34, 0.25, 0.34, mats.skin, actor, 0, 3.03, 0.02);
voxel(0.94, 0.88, 0.82, mats.skinLight, actor, 0, 3.56, 0.03);
voxel(0.99, 0.22, 0.86, mats.hair, actor, 0, 4.02, 0.00);
voxel(0.18, 0.58, 0.76, mats.hair, actor, -0.42, 3.72, 0.02);
voxel(0.18, 0.58, 0.76, mats.hair, actor, 0.42, 3.72, 0.02);
voxel(0.76, 0.15, 0.18, mats.hair, actor, 0, 3.91, 0.43);
voxel(0.20, 0.18, 0.10, mats.eyeWhite, actor, -0.22, 3.65, 0.46);
voxel(0.20, 0.18, 0.10, mats.eyeWhite, actor, 0.22, 3.65, 0.46);
voxel(0.075, 0.10, 0.045, mats.eye, actor, -0.20, 3.64, 0.525);
voxel(0.075, 0.10, 0.045, mats.eye, actor, 0.24, 3.64, 0.525);
voxel(0.14, 0.16, 0.14, mats.skin, actor, 0, 3.48, 0.51);
voxel(0.25, 0.055, 0.05, mats.hair, actor, 0, 3.32, 0.49);
// Arms are built from block segments and positioned with simple 2-bone IK.
const armLengthUpper = 1.70;
const armLengthLower = 1.70;
function createArm(side) {
const upper = voxel(
0.34, 1, 0.34,
mats.jacket,
actor
);
upper.scale.y = armLengthUpper;
const lower = voxel(
0.25, 1, 0.25,
mats.skin,
actor
);
lower.scale.y = armLengthLower;
const elbowBlock = voxel(0.32, 0.32, 0.32, mats.jacket, actor);
const hand = voxel(0.38, 0.34, 0.38, mats.skinLight, actor);
return {
side,
upper,
lower,
elbowBlock,
hand,
shoulder: new THREE.Vector3(side * 0.78, 2.82, 0.18)
};
}
const rightArm = createArm(1);
const leftArm = createArm(-1);
function setSegment(mesh, a, b) {
const delta = new THREE.Vector3().subVectors(b, a);
const length = Math.max(delta.length(), 0.001);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(Y_AXIS, delta.normalize());
mesh.scale.y = length;
}
function updateArm(arm, targetWorld) {
const shoulder = arm.shoulder.clone();
const desired = targetWorld.clone().sub(actor.position);
const vector = desired.sub(shoulder);
let distance = vector.length();
const maxReach = armLengthUpper + armLengthLower - 0.001;
if (distance > maxReach) {
vector.multiplyScalar(maxReach / distance);
distance = maxReach;
}
if (distance < 0.001) {
vector.set(0, -1, 0);
distance = 1;
}
const direction = vector.clone().normalize();
const along =
(
armLengthUpper * armLengthUpper -
armLengthLower * armLengthLower +
distance * distance
) / (2 * distance);
const height = Math.sqrt(
Math.max(0, armLengthUpper * armLengthUpper - along * along)
);
const bend = new THREE.Vector3(arm.side * 0.75, -1, 0.18);
bend.addScaledVector(direction, -bend.dot(direction));
if (bend.lengthSq() < 0.0001) {
bend.cross(direction);
}
bend.normalize();
const elbow = shoulder
.clone()
.addScaledVector(direction, along)
.addScaledVector(bend, height);
const end = shoulder.clone().addScaledVector(direction, distance);
setSegment(arm.upper, shoulder, elbow);
setSegment(arm.lower, elbow, end);
arm.elbowBlock.position.copy(elbow);
arm.hand.position.copy(end);
}
// ---------------------------------------------------------------------
// Animation helpers
// ---------------------------------------------------------------------
const clamp01 = x => Math.max(0, Math.min(1, x));
const smooth = x => {
x = clamp01(x);
return x * x * (3 - 2 * x);
};
const phase = (t, a, b) => smooth((t - a) / (b - a));
const lerp = THREE.MathUtils.lerp;
function mixVector(a, b, u) {
return a.clone().lerp(b, u);
}
const clothStart = new THREE.Vector3(-0.65, 2.76, -0.05);
const clothLift = new THREE.Vector3(-0.45, 3.14, -0.08);
const clothLeftStart = new THREE.Vector3(-2.38, 3.10, -0.10);
const clothLeftEnd = new THREE.Vector3(-2.08, 3.10, -0.10);
const clothRightStart = new THREE.Vector3(-1.34, 3.10, -0.10);
const clothRightEnd = new THREE.Vector3(-1.04, 3.10, -0.10);
const glassesLift = new THREE.Vector3(-1.72, 3.48, -0.35);
const caseAbove = new THREE.Vector3(2.05, 3.47, -0.55);
const caseInside = new THREE.Vector3(2.05, 2.98, -0.55);
let animationTime = 0;
let playing = true;
const duration = 15.5;
let lastTime = performance.now();
let lastPhase = '';
function idleHand(side) {
return new THREE.Vector3(
actor.position.x + side * 0.96,
1.72 + Math.sin(animationTime * 1.7) * 0.025,
actor.position.z + 0.20
);
}
function updateScene(t) {
// Character shifts along the table to make each interaction legible.
let bodyX;
if (t < 5.7) {
bodyX = -0.75;
} else if (t < 6.4) {
bodyX = lerp(-0.75, -1.0, phase(t, 5.7, 6.4));
} else if (t < 8.5) {
bodyX = -1.0;
} else if (t < 9.4) {
bodyX = lerp(-1.0, 0.10, phase(t, 8.5, 9.4));
} else if (t < 11.2) {
bodyX = 0.10;
} else if (t < 12.1) {
bodyX = lerp(0.10, 0.45, phase(t, 11.2, 12.1));
} else {
bodyX = 0.45;
}
actor.position.set(
bodyX,
0.018 * Math.sin(t * 2.1),
-2.55 + 0.025 * Math.sin(t * 1.3)
);
// Cleaning cloth position and orientation.
let clothPosition = clothStart.clone();
let clothRotationX = 0;
if (t < 0.9) {
clothPosition = clothStart.clone();
} else if (t < 1.4) {
clothPosition = mixVector(clothStart, clothLift, phase(t, 0.9, 1.4));
clothRotationX = lerp(0, Math.PI / 2, phase(t, 0.9, 1.4));
} else if (t < 1.8) {
clothPosition = mixVector(clothLift, clothLeftStart, phase(t, 1.4, 1.8));
clothRotationX = Math.PI / 2;
} else if (t < 3.1) {
const u = phase(t, 1.8, 3.1);
clothPosition = mixVector(clothLeftStart, clothLeftEnd, u);
clothPosition.x += Math.sin(u * Math.PI * 5) * 0.055;
clothPosition.y += Math.sin(u * Math.PI * 3) * 0.025;
clothRotationX = Math.PI / 2;
} else if (t < 3.5) {
clothPosition = mixVector(clothLeftEnd, clothRightStart, phase(t, 3.1, 3.5));
clothRotationX = Math.PI / 2;
} else if (t < 4.8) {
const u = phase(t, 3.5, 4.8);
clothPosition = mixVector(clothRightStart, clothRightEnd, u);
clothPosition.x += Math.sin(u * Math.PI * 6) * 0.055;
clothPosition.y += Math.sin(u * Math.PI * 4) * 0.025;
clothRotationX = Math.PI / 2;
} else if (t < 5.7) {
clothPosition = mixVector(clothRightEnd, clothStart, phase(t, 4.8, 5.7));
clothRotationX = lerp(Math.PI / 2, 0, phase(t, 4.8, 5.7));
} else {
clothPosition = clothStart.clone();
clothRotationX = 0;
}
cloth.position.copy(clothPosition);
cloth.rotation.set(
clothRotationX,
-0.15 + Math.sin(t * 2.4) * 0.035,
Math.sin(t * 3.1) * 0.025
);
// Glasses are picked up, rotated flat, and lowered into the case.
let glassesPosition = glassesStart.clone();
let glassesRotationX = 0;
if (t < 10.2) {
glassesPosition.copy(glassesStart);
} else if (t < 11.1) {
const u = phase(t, 10.2, 11.1);
glassesPosition = mixVector(glassesStart, glassesLift, u);
glassesRotationX = lerp(0, -Math.PI / 2, u);
} else if (t < 12.4) {
const u = phase(t, 11.1, 12.4);
glassesPosition = mixVector(glassesLift, caseAbove, u);
glassesPosition.y += Math.sin(u * Math.PI) * 0.16;
glassesRotationX = -Math.PI / 2;
} else if (t < 13.2) {
glassesPosition = mixVector(caseAbove, caseInside, phase(t, 12.4, 13.2));
glassesRotationX = -Math.PI / 2;
} else {
glassesPosition.copy(caseInside);
glassesRotationX = -Math.PI / 2;
}
glasses.position.copy(glassesPosition);
glasses.rotation.set(glassesRotationX, 0, 0);
// Fold one temple at a time.
leftTemple.rotation.y = -Math.PI / 2 * phase(t, 6.8, 7.5);
rightTemple.rotation.y = Math.PI / 2 * phase(t, 7.7, 8.4);
glasses.updateMatrixWorld(true);
// The right hand holds the cloth first.
let rightTarget;
if (t < 0.9) {
rightTarget = mixVector(
idleHand(1),
clothStart.clone().add(new THREE.Vector3(0.30, 0, 0.08)),
phase(t, 0, 0.9)
);
} else if (t < 5.7) {
rightTarget = cloth.position.clone().add(new THREE.Vector3(0.30, 0, 0.08));
} else if (t < 6.3) {
rightTarget = mixVector(
clothStart.clone().add(new THREE.Vector3(0.30, 0, 0.08)),
idleHand(1),
phase(t, 5.7, 6.3)
);
} else if (t < 6.8) {
const rightHinge = glassesStart.clone().add(new THREE.Vector3(1.02, 0.62, 0));
rightTarget = mixVector(idleHand(1), rightHinge, phase(t, 6.3, 6.8));
} else if (t < 10.2) {
rightTarget = glassesStart.clone().add(new THREE.Vector3(1.02, 0.62, 0));
} else if (t < 13.2) {
const graspPoint = glasses.localToWorld(new THREE.Vector3(1.02, 0.62, 0));
rightTarget = graspPoint.add(new THREE.Vector3(0, 0.02, 0.10));
} else {
rightTarget = mixVector(
glasses.localToWorld(new THREE.Vector3(1.02, 0.62, 0)),
idleHand(1),
phase(t, 13.2, 14.0)
);
}
// Both hands move to the hinges before folding.
const leftHinge = glassesStart.clone().add(new THREE.Vector3(-1.02, 0.62, 0));
let leftTarget;
if (t < 6.3) {
leftTarget = idleHand(-1);
} else if (t < 6.8) {
leftTarget = mixVector(idleHand(-1), leftHinge, phase(t, 6.3, 6.8));
} else if (t < 8.5) {
leftTarget = leftHinge.clone();
} else {
leftTarget = mixVector(leftHinge, idleHand(-1), phase(t, 8.5, 9.1));
}
updateArm(rightArm, rightTarget);
updateArm(leftArm, leftTarget);
// The lid briefly rocks while the frame is placed, then remains open.
let lidAngle = -1.78;
if (t > 12.8 && t < 13.6) {
lidAngle = lerp(-1.78, -1.52, phase(t, 12.8, 13.6));
} else if (t >= 13.6 && t < 14.2) {
lidAngle = lerp(-1.52, -1.78, phase(t, 13.6, 14.2));
}
caseLid.rotation.x = lidAngle;
// Cleaning sparkle effect.
const cleaning = t > 1.7 && t < 4.9;
sparkMaterial.opacity = cleaning
? 0.75 + 0.25 * Math.sin(t * 15)
: 0;
sparks.forEach((spark, index) => {
const angle = t * 4.5 + index * (Math.PI * 2 / sparks.length);
const radius = 0.28 + (index % 3) * 0.07;
spark.position.set(
cloth.position.x + Math.cos(angle) * radius,
cloth.position.y + 0.22 + Math.sin(angle * 1.7) * 0.20,
cloth.position.z + 0.22 + Math.sin(angle) * radius
);
const pulse = 0.7 + 0.3 * Math.sin(t * 10 + index);
spark.scale.setScalar(0.055 * pulse);
spark.visible = cleaning;
});
// Subtle lamp flicker.
lampLight.intensity = 25 + Math.sin(t * 7.3) * 0.8 + Math.sin(t * 11.1) * 0.4;
const phaseText =
t < 1.8 ? 'Reaching for the cleaning cloth…' :
t < 4.8 ? 'Cleaning both lenses with the cloth…' :
t < 6.8 ? 'Preparing to fold the frame…' :
t < 8.5 ? 'Folding the temples inward…' :
t < 10.2 ? 'Gripping the folded glasses…' :
t < 13.2 ? 'Placing the frame into its matching case…' :
t < 14.4 ? 'Releasing the frame safely…' :
'The folded glasses are safely stowed.';
if (phaseText !== lastPhase) {
document.getElementById('phase').textContent = phaseText;
lastPhase = phaseText;
}
document.getElementById('progress').style.width =
`${Math.min(100, t / duration * 100)}%`;
}
// ---------------------------------------------------------------------
// Direct camera controls: pointer drag, wheel zoom, touch pinch
// ---------------------------------------------------------------------
let dragging = false;
let previousX = 0;
let previousY = 0;
const pointers = new Map();
let pinchDistance = 0;
renderer.domElement.addEventListener('pointerdown', event => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener('pointermove', event => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 2) {
const values = [...pointers.values()];
const distance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
if (pinchDistance > 0) {
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * (pinchDistance / Math.max(distance, 1)),
8,
23
);
}
pinchDistance = distance;
return;
}
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(cameraPhi - dy * 0.005, 0.48, 1.38);
previousX = event.clientX;
previousY = event.clientY;
});
function endPointer(event) {
pointers.delete(event.pointerId);
dragging = pointers.size > 0;
if (pointers.size < 2) pinchDistance = 0;
}
renderer.domElement.addEventListener('pointerup', endPointer);
renderer.domElement.addEventListener('pointercancel', endPointer);
renderer.domElement.addEventListener('lostpointercapture', endPointer);
renderer.domElement.addEventListener(
'wheel',
event => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
8,
23
);
},
{ passive: false }
);
renderer.domElement.addEventListener('dblclick', resetCamera);
// ---------------------------------------------------------------------
// UI controls and render loop
// ---------------------------------------------------------------------
document.getElementById('replay').addEventListener('click', () => {
animationTime = 0;
playing = true;
lastTime = performance.now();
});
document.getElementById('resetView').addEventListener('click', resetCamera);
window.addEventListener('keydown', event => {
if (event.key.toLowerCase() === 'r') {
animationTime = 0;
playing = true;
lastTime = performance.now();
}
});
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min((now - lastTime) / 1000, 0.05);
lastTime = now;
if (playing) {
animationTime = Math.min(animationTime + delta, duration);
if (animationTime >= duration) playing = false;
}
updateScene(animationTime);
updateCamera();
renderer.render(scene, camera);
}
updateScene(0);
requestAnimationFrame(animate);
</script>
</body>
</html>
```