Prompt specification
Build or customize the Three.js 3D scene "Pack before leaving sequence" 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: Pack before leaving sequence
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/pack-before-leaving-sequence-x3878
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows dependencies in a household morning routine. Animate a character placing food into the lunchbox, lunchbox into bag, and bag on shoulder; the departure action remains unavailable until all objects are visibly packed. Low-poly.
Working Three.js Starter (pack-before-leaving-sequence-x3878):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Morning Routine — Dependency Demo</title>
<style>
:root {
--ink: #263238;
--muted: #718087;
--paper: #fbfaf6;
--mint: #55b99d;
--mint-dark: #27836e;
--orange: #ef8b55;
--line: #dfe4df;
--locked: #aeb7b3;
--shadow: 0 18px 48px rgba(42, 55, 50, 0.16);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e4da;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button { font: inherit; }
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.brand {
position: fixed;
top: 24px;
left: 26px;
z-index: 5;
pointer-events: none;
}
.brand-kicker {
display: inline-flex;
align-items: center;
gap: 8px;
margin-bottom: 7px;
color: var(--mint-dark);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.brand-kicker::before {
content: "";
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--mint);
box-shadow: 0 0 0 5px rgba(85, 185, 157, 0.14);
}
.brand h1 {
margin: 0;
font-size: clamp(22px, 3vw, 34px);
line-height: 1.05;
letter-spacing: -0.04em;
}
.brand p {
margin: 7px 0 0;
color: var(--muted);
font-size: 13px;
}
.progress-badge {
position: fixed;
top: 24px;
left: 50%;
z-index: 5;
transform: translateX(-50%);
padding: 9px 14px;
border: 1px solid rgba(255,255,255,.7);
border-radius: 999px;
background: rgba(255,255,255,.78);
box-shadow: 0 8px 24px rgba(38,50,56,.09);
color: var(--muted);
font-size: 12px;
font-weight: 750;
pointer-events: none;
backdrop-filter: blur(10px);
}
.progress-badge strong {
margin-right: 4px;
color: var(--mint-dark);
font-size: 14px;
}
.panel {
position: fixed;
z-index: 10;
top: 22px;
right: 22px;
width: min(350px, calc(100vw - 44px));
max-height: calc(100vh - 44px);
overflow: auto;
padding: 20px;
border: 1px solid rgba(255,255,255,.82);
border-radius: 22px;
background: rgba(251,250,246,.94);
box-shadow: var(--shadow);
backdrop-filter: blur(16px);
}
.panel-header {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 14px;
}
.panel-header h2 {
margin: 0;
font-size: 18px;
letter-spacing: -0.02em;
}
.panel-header p {
margin: 5px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.4;
}
.reset-mini {
flex: 0 0 auto;
padding: 6px 9px;
border: 1px solid var(--line);
border-radius: 9px;
background: #fff;
color: var(--muted);
cursor: pointer;
font-size: 11px;
font-weight: 700;
}
.progress-track {
height: 6px;
margin: 17px 0 18px;
overflow: hidden;
border-radius: 99px;
background: #e7ebe7;
}
#progressFill {
width: 0%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--mint), #82d0b7);
transition: width .45s ease;
}
.chain {
display: grid;
gap: 9px;
}
.step {
position: relative;
display: grid;
grid-template-columns: 34px 1fr;
gap: 10px;
padding: 12px;
border: 1px solid var(--line);
border-radius: 15px;
background: rgba(255,255,255,.65);
transition: border-color .25s, opacity .25s, background .25s;
}
.step.locked {
opacity: .52;
background: rgba(241,242,239,.65);
}
.step.ready {
border-color: rgba(85,185,157,.5);
background: rgba(235,250,245,.75);
}
.step.done {
border-color: rgba(85,185,157,.7);
background: rgba(230,248,241,.9);
}
.step-number {
display: grid;
width: 30px;
height: 30px;
place-items: center;
border-radius: 10px;
background: #e8ece9;
color: var(--muted);
font-size: 12px;
font-weight: 850;
}
.step.done .step-number {
background: var(--mint);
color: #fff;
}
.step-copy {
min-width: 0;
}
.step-title-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
}
.step h3 {
margin: 0;
font-size: 13px;
letter-spacing: -.01em;
}
.step-status {
color: var(--muted);
font-size: 9px;
font-weight: 850;
letter-spacing: .1em;
}
.step.done .step-status { color: var(--mint-dark); }
.step.ready .step-status { color: #3c8f7b; }
.dependency {
display: block;
margin: 4px 0 9px;
color: var(--muted);
font-size: 11px;
}
.step button {
width: 100%;
padding: 8px 10px;
border: 0;
border-radius: 9px;
background: var(--ink);
color: #fff;
cursor: pointer;
font-size: 11px;
font-weight: 800;
transition: transform .15s, background .15s, opacity .15s;
}
.step button:hover:not(:disabled) {
transform: translateY(-1px);
background: #34444a;
}
.step button:disabled {
background: #d5dad7;
color: #8c9792;
cursor: not-allowed;
}
.step.done button {
background: #ccebdd;
color: var(--mint-dark);
}
.dependency-arrow {
height: 10px;
margin-left: 26px;
color: #a8b3ae;
font-size: 13px;
line-height: 10px;
}
.departure {
margin-top: 10px;
border-style: dashed;
}
.departure button {
background: var(--orange);
}
.departure button:hover:not(:disabled) {
background: #db7540;
}
.departure button:disabled {
background: #d5dad7;
}
.auto-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 8px;
margin-top: 14px;
}
#autoBtn {
padding: 11px 13px;
border: 0;
border-radius: 12px;
background: var(--mint-dark);
color: #fff;
cursor: pointer;
font-size: 12px;
font-weight: 800;
}
#autoBtn:hover:not(:disabled) { background: #1f705f; }
#autoBtn:disabled {
background: #cbd2ce;
color: #89938f;
cursor: not-allowed;
}
.lock-note {
align-self: center;
color: var(--muted);
font-size: 10px;
text-align: right;
}
.camera-help {
position: fixed;
z-index: 5;
bottom: 22px;
left: 24px;
display: flex;
align-items: center;
gap: 10px;
padding: 9px 11px;
border: 1px solid rgba(255,255,255,.75);
border-radius: 12px;
background: rgba(255,255,255,.78);
box-shadow: 0 8px 24px rgba(38,50,56,.09);
color: var(--muted);
font-size: 11px;
backdrop-filter: blur(10px);
}
#cameraReset {
border: 0;
border-left: 1px solid #d9dfdb;
padding: 2px 0 2px 10px;
background: transparent;
color: var(--mint-dark);
cursor: pointer;
font-size: 11px;
font-weight: 800;
}
#actionStatus {
position: fixed;
z-index: 5;
bottom: 24px;
left: 50%;
max-width: min(520px, calc(100vw - 40px));
transform: translateX(-50%);
padding: 10px 16px;
border: 1px solid rgba(255,255,255,.8);
border-radius: 999px;
background: rgba(255,255,255,.86);
box-shadow: 0 8px 26px rgba(38,50,56,.1);
color: var(--ink);
font-size: 12px;
font-weight: 700;
text-align: center;
pointer-events: none;
backdrop-filter: blur(12px);
}
.object-tag {
position: fixed;
z-index: 4;
padding: 5px 9px;
transform: translate(-50%, -50%);
border: 1px solid rgba(255,255,255,.9);
border-radius: 999px;
background: rgba(255,255,255,.84);
box-shadow: 0 5px 15px rgba(38,50,56,.12);
color: var(--ink);
font-size: 9px;
font-weight: 850;
letter-spacing: .08em;
pointer-events: none;
white-space: nowrap;
backdrop-filter: blur(8px);
}
.object-tag::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 12px;
background: rgba(38,50,56,.3);
}
@media (max-width: 760px) {
.brand {
top: 15px;
left: 16px;
}
.brand p { display: none; }
.progress-badge {
top: 16px;
right: 15px;
left: auto;
transform: none;
}
.panel {
top: auto;
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
max-height: 48vh;
padding: 15px;
border-radius: 18px;
}
.panel-header p,
.lock-note { display: none; }
.auto-row { grid-template-columns: 1fr; }
.camera-help {
top: 88px;
bottom: auto;
left: 15px;
}
#actionStatus {
bottom: calc(48vh + 20px);
width: max-content;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive low-poly morning routine"></div>
<header class="brand">
<div class="brand-kicker">Home / 07:10</div>
<h1>Morning routine</h1>
<p>Each action unlocks only after its dependency is complete.</p>
</header>
<div class="progress-badge">
<strong id="progressCount">0 / 3</strong>
objects packed
</div>
<aside class="panel" aria-label="Routine dependency controls">
<div class="panel-header">
<div>
<h2>Ready for school</h2>
<p>Pack the lunch, load the bag, then shoulder it before leaving.</p>
</div>
<button class="reset-mini" id="resetBtn" type="button">Reset</button>
</div>
<div class="progress-track" aria-hidden="true">
<div id="progressFill"></div>
</div>
<div class="chain">
<article class="step ready" id="stepFood">
<div class="step-number">1</div>
<div class="step-copy">
<div class="step-title-row">
<h3>Pack the lunchbox</h3>
<span class="step-status">READY</span>
</div>
<span class="dependency">Food → lunchbox</span>
<button id="packBtn" type="button">Pack food</button>
</div>
</article>
<div class="dependency-arrow" aria-hidden="true">↓</div>
<article class="step locked" id="stepBag">
<div class="step-number">2</div>
<div class="step-copy">
<div class="step-title-row">
<h3>Load the school bag</h3>
<span class="step-status">LOCKED</span>
</div>
<span class="dependency">Packed lunchbox → school bag</span>
<button id="bagBtn" type="button" disabled>Load bag</button>
</div>
</article>
<div class="dependency-arrow" aria-hidden="true">↓</div>
<article class="step locked" id="stepShoulder">
<div class="step-number">3</div>
<div class="step-copy">
<div class="step-title-row">
<h3>Put the bag on shoulder</h3>
<span class="step-status">LOCKED</span>
</div>
<span class="dependency">Loaded bag → character’s shoulder</span>
<button id="shoulderBtn" type="button" disabled>Shoulder bag</button>
</div>
</article>
<div class="dependency-arrow" aria-hidden="true">↓</div>
<article class="step departure locked" id="stepLeave">
<div class="step-number">4</div>
<div class="step-copy">
<div class="step-title-row">
<h3>Leave for school</h3>
<span class="step-status">LOCKED</span>
</div>
<span class="dependency">Requires all three packing actions</span>
<button id="leaveBtn" type="button" disabled>Departure locked</button>
</div>
</article>
</div>
<div class="auto-row">
<button id="autoBtn" type="button">Play full routine</button>
<span class="lock-note">Manual camera controls</span>
</div>
</aside>
<div class="camera-help">
<span>Drag to orbit · Scroll to zoom</span>
<button id="cameraReset" type="button">Reset view</button>
</div>
<div id="actionStatus" role="status" aria-live="polite">
Start by packing the food into the lunchbox.
</div>
<div class="object-tag" id="foodTag">FOOD</div>
<div class="object-tag" id="lunchTag">LUNCHBOX</div>
<div class="object-tag" id="bagTag">BAG</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e4da);
scene.fog = new THREE.Fog(0xe9e4da, 13, 24);
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.12;
sceneHost.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
60
);
const cameraTarget = new THREE.Vector3(-0.1, 1.05, -0.25);
let cameraTheta = 0.68;
let cameraPhi = 1.03;
let cameraRadius = 11.7;
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);
}
updateCamera();
// Direct camera controls; no addons are used.
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;
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(cameraPhi - dy * 0.005, 0.48, 1.37);
updateCamera();
});
const stopDragging = () => { dragging = false; };
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.009,
7,
17
);
updateCamera();
}, { passive: false });
window.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") cameraTheta -= 0.08;
if (event.key === "ArrowRight") cameraTheta += 0.08;
if (event.key === "ArrowUp") cameraPhi = Math.max(0.48, cameraPhi - 0.06);
if (event.key === "ArrowDown") cameraPhi = Math.min(1.37, cameraPhi + 0.06);
updateCamera();
});
const hemisphere = new THREE.HemisphereLight(0xfff8e9, 0x7b8c86, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffefd0, 3.25);
sun.position.set(-3, 8, 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 = 0.5;
sun.shadow.camera.far = 22;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb7d8df, 1.05);
fill.position.set(6, 4, -2);
scene.add(fill);
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
flatShading: true,
...options
});
}
const palette = {
wall: material(0xf4efe4),
wallSide: material(0xe8e3d8),
floor: material(0xd8c9b2),
rug: material(0x8eb4ad),
wood: material(0xb98458),
woodLight: material(0xd1a476),
cabinet: material(0xefe6d5),
cabinetDark: material(0xd8cbb5),
mint: material(0x55b99d),
mintDark: material(0x2e806e),
orange: material(0xe9824d),
orangeDark: material(0xb95731),
yellow: material(0xf0bb57),
blue: material(0x4f7c91),
charcoal: material(0x30383c),
skin: material(0xdca77e),
hair: material(0x4a342c),
white: material(0xfffaf0),
green: material(0x67a85a),
greenDark: material(0x3f7741)
};
function addMesh(geometry, mat, parent, position, rotation, scale) {
const mesh = new THREE.Mesh(geometry, mat);
if (position) mesh.position.set(...position);
if (rotation) mesh.rotation.set(...rotation);
if (scale) mesh.scale.set(...scale);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function box(parent, size, position, mat, rotation) {
return addMesh(new THREE.BoxGeometry(...size), mat, parent, position, rotation);
}
// Room.
const room = new THREE.Group();
scene.add(room);
const floor = addMesh(
new THREE.PlaneGeometry(18, 14),
palette.floor,
room,
[0, -0.015, 0],
[-Math.PI / 2, 0, 0]
);
floor.receiveShadow = true;
box(room, [11.2, 3.65, 0.16], [0, 1.82, -3.85], palette.wall);
box(room, [0.16, 3.65, 8], [-5.55, 1.82, 0.1], palette.wallSide);
box(room, [11.1, 0.15, 0.12], [0, 0.08, -3.73], palette.cabinetDark);
box(room, [0.12, 0.15, 8], [-5.43, 0.08, 0.1], palette.cabinetDark);
const rug = addMesh(
new THREE.CylinderGeometry(2.45, 2.45, 0.035, 32),
palette.rug,
room,
[0.6, 0.015, 0.05]
);
rug.receiveShadow = true;
// Window.
box(room, [2.35, 1.55, 0.08], [0.55, 2.18, -3.72], material(0x9ccfd1, {
emissive: 0x527a7c,
emissiveIntensity: 0.12
}));
box(room, [2.58, 0.11, 0.14], [0.55, 2.98, -3.65], palette.white);
box(room, [2.58, 0.11, 0.14], [0.55, 1.38, -3.65], palette.white);
box(room, [0.11, 1.55, 0.14], [-0.62, 2.18, -3.65], palette.white);
box(room, [0.11, 1.55, 0.14], [1.72, 2.18, -3.65], palette.white);
box(room, [0.08, 1.55, 0.14], [0.55, 2.18, -3.64], palette.white);
// Clock.
const clock = addMesh(
new THREE.CylinderGeometry(0.31, 0.31, 0.08, 12),
palette.white,
room,
[2.45, 2.75, -3.67],
[Math.PI / 2, 0, 0]
);
const handA = box(room, [0.035, 0.19, 0.035], [2.45, 2.80, -3.61], palette.charcoal);
handA.rotation.z = -0.45;
const handB = box(room, [0.035, 0.14, 0.035], [2.48, 2.75, -3.60], palette.charcoal);
handB.rotation.z = 0.7;
// Door and glowing hallway.
const hallway = box(
room,
[1.34, 2.65, 0.05],
[3.7, 1.325, -3.88],
material(0xa9d9d3, { emissive: 0x4e8e83, emissiveIntensity: 0.22 })
);
const doorPivot = new THREE.Group();
doorPivot.position.set(3.03, 0, -3.75);
room.add(doorPivot);
box(doorPivot, [1.34, 2.65, 0.11], [0.67, 1.325, 0], palette.blue);
box(doorPivot, [1.11, 0.85, 0.04], [0.67, 1.72, 0.075], material(0x638fa0));
box(doorPivot, [0.09, 0.09, 0.11], [1.17, 1.27, 0.11], palette.yellow);
box(room, [0.12, 2.8, 0.18], [2.96, 1.4, -3.67], palette.white);
box(room, [0.12, 2.8, 0.18], [4.44, 1.4, -3.67], palette.white);
box(room, [1.6, 0.12, 0.18], [3.7, 2.8, -3.67], palette.white);
// Kitchen counter.
const counter = new THREE.Group();
counter.position.set(-2.55, 0, -1.4);
room.add(counter);
box(counter, [3.75, 1.08, 1.2], [0, 0.56, 0], palette.cabinet);
box(counter, [3.92, 0.14, 1.38], [0, 1.17, 0], palette.woodLight);
box(counter, [3.75, 0.1, 1.23], [0, 0.08, 0], palette.cabinetDark);
for (const x of [-1.2, 0, 1.2]) {
box(counter, [1.1, 0.79, 0.045], [x, 0.61, 0.618], palette.cabinetDark);
box(counter, [0.28, 0.035, 0.055], [x, 0.83, 0.66], palette.charcoal);
}
// Counter details.
addMesh(
new THREE.CylinderGeometry(0.16, 0.12, 0.27, 8),
palette.mint,
counter,
[1.52, 1.38, -0.24]
);
box(counter, [0.52, 0.035, 0.34], [-1.47, 1.27, -0.19], palette.white);
box(counter, [0.44, 0.025, 0.28], [-1.47, 1.3, -0.19], palette.mint);
// Low-poly plant.
const plant = new THREE.Group();
plant.position.set(-4.55, 1.25, -1.35);
room.add(plant);
addMesh(new THREE.CylinderGeometry(0.22, 0.16, 0.35, 7), palette.orange, plant, [0, 0.18, 0]);
for (let i = 0; i < 5; i++) {
const leaf = addMesh(
new THREE.SphereGeometry(0.19, 6, 4),
i % 2 ? palette.green : palette.greenDark,
plant,
[Math.sin(i * 1.8) * 0.12, 0.5 + i * 0.055, Math.cos(i * 1.8) * 0.12],
[0, i, 0],
[0.65, 1.35, 0.65]
);
leaf.rotation.z = (i - 2) * 0.22;
}
// Lunchbox: an open tray with a hinged lid.
const lunchbox = new THREE.Group();
lunchbox.position.set(-1.25, 1.245, -1.35);
scene.add(lunchbox);
const lunchMat = material(0x4ba891);
const lunchInnerMat = material(0x2d7768);
box(lunchbox, [1.12, 0.10, 0.80], [0, 0.05, 0], lunchMat);
box(lunchbox, [1.12, 0.30, 0.08], [0, 0.23, -0.36], lunchMat);
box(lunchbox, [1.12, 0.30, 0.08], [0, 0.23, 0.36], lunchMat);
box(lunchbox, [0.08, 0.30, 0.64], [-0.52, 0.23, 0], lunchMat);
box(lunchbox, [0.08, 0.30, 0.64], [0.52, 0.23, 0], lunchMat);
box(lunchbox, [0.94, 0.025, 0.62], [0, 0.115, 0], lunchInnerMat);
const lunchLid = new THREE.Group();
lunchLid.position.set(0, 0.45, -0.38);
lunchLid.rotation.x = -1.95;
lunchbox.add(lunchLid);
box(lunchLid, [1.16, 0.09, 0.82], [0, 0, 0.41], lunchMat);
box(lunchLid, [0.34, 0.055, 0.09], [0, 0.06, 0.72], palette.mintDark);
const packedBadge = new THREE.Group();
packedBadge.visible = false;
lunchLid.add(packedBadge);
const badgeMat = material(0xd9f15a);
const badgeA = box(packedBadge, [0.25, 0.025, 0.055], [-0.08, 0.06, 0.36], badgeMat);
badgeA.rotation.y = -0.72;
const badgeB = box(packedBadge, [0.12, 0.025, 0.055], [0.06, 0.06, 0.36], badgeMat);
badgeB.rotation.y = 0.72;
// Food objects.
function makeSandwich() {
const g = new THREE.Group();
const bread = material(0xe9c985);
const crust = material(0xc78d4e);
const lettuce = material(0x65a858);
box(g, [0.54, 0.075, 0.40], [0, 0.04, 0], bread);
box(g, [0.48, 0.045, 0.35], [0, 0.10, 0], lettuce);
box(g, [0.48, 0.04, 0.35], [0, 0.14, 0], material(0xe97855));
box(g, [0.54, 0.075, 0.40], [0, 0.19, 0], bread);
box(g, [0.54, 0.025, 0.40], [0, 0.008, 0], crust);
return g;
}
function makeApple() {
const g = new THREE.Group();
addMesh(
new THREE.SphereGeometry(0.18, 8, 6),
material(0xd95745),
g,
[0, 0.17, 0],
null,
[1, 1.05, 1]
);
box(g, [0.045, 0.13, 0.04], [0, 0.37, 0], material(0x68432e));
const leaf = box(g, [0.16, 0.025, 0.08], [0.08, 0.40, 0], palette.green);
leaf.rotation.y = 0.45;
return g;
}
function makeCarrot() {
const g = new THREE.Group();
addMesh(
new THREE.ConeGeometry(0.105, 0.46, 6),
material(0xeb8d38),
g,
[0.07, 0.12, 0],
[0, 0, -Math.PI / 2]
);
box(g, [0.07, 0.18, 0.05], [-0.22, 0.12, 0], palette.green);
const leaf = box(g, [0.06, 0.20, 0.045], [-0.27, 0.20, 0.03], palette.greenDark);
leaf.rotation.z = -0.35;
return g;
}
const sandwich = makeSandwich();
const apple = makeApple();
const carrot = makeCarrot();
sandwich.position.set(-3.45, 1.245, -1.34);
apple.position.set(-2.65, 1.245, -1.36);
carrot.position.set(-2.02, 1.245, -1.34);
scene.add(sandwich, apple, carrot);
// Open school bag, built from separate low-poly panels.
const bag = new THREE.Group();
bag.position.set(1.35, 0, -0.35);
bag.rotation.y = -0.2;
scene.add(bag);
const bagMat = material(0xe58a50);
const bagDark = material(0xb95d38);
box(bag, [1.18, 0.10, 0.58], [0, 0.05, 0], bagDark);
box(bag, [1.18, 0.60, 0.09], [0, 0.35, -0.255], bagMat);
box(bag, [1.18, 0.48, 0.09], [0, 0.29, 0.255], bagMat);
box(bag, [0.09, 0.60, 0.42], [-0.545, 0.35, 0], bagMat);
box(bag, [0.09, 0.60, 0.42], [0.545, 0.35, 0], bagMat);
box(bag, [0.72, 0.34, 0.08], [0, 0.27, 0.32], bagDark);
const bagFlap = new THREE.Group();
bagFlap.position.set(0, 0.67, -0.27);
bagFlap.rotation.x = -1.72;
bag.add(bagFlap);
box(bagFlap, [1.19, 0.07, 0.60], [0, 0, 0.30], bagMat);
box(bagFlap, [0.34, 0.045, 0.08], [0, 0.055, 0.51], bagDark);
function addBagHandle(z) {
const curve = new THREE.CatmullRomCurve3([
new THREE.Vector3(-0.34, 0.58, z),
new THREE.Vector3(-0.29, 0.96, z),
new THREE.Vector3(0, 1.10, z),
new THREE.Vector3(0.29, 0.96, z),
new THREE.Vector3(0.34, 0.58, z)
]);
addMesh(
new THREE.TubeGeometry(curve, 10, 0.035, 5, false),
bagDark,
bag
);
}
addBagHandle(0.25);
addBagHandle(-0.25);
const bagRing = addMesh(
new THREE.TorusGeometry(0.79, 0.025, 5, 32),
new THREE.MeshBasicMaterial({ color: 0xf0bd5d, transparent: true, opacity: 0.8 }),
scene,
[1.35, 0.025, -0.35],
[-Math.PI / 2, 0, 0]
);
// Character.
const character = new THREE.Group();
character.position.set(-0.2, 0, 0.75);
scene.add(character);
const shirt = material(0x4f8294);
const pants = material(0x384d5a);
const shoes = material(0x29363b);
const torso = addMesh(
new THREE.CylinderGeometry(0.34, 0.43, 0.73, 6),
shirt,
character,
[0, 1.18, 0]
);
box(character, [0.63, 0.22, 0.38], [0, 0.78, 0], pants);
function makeLeg(x) {
const pivot = new THREE.Group();
pivot.position.set(x, 0.79, 0);
character.add(pivot);
addMesh(new THREE.CylinderGeometry(0.105, 0.12, 0.62, 6), pants, pivot, [0, -0.31, 0]);
box(pivot, [0.24, 0.15, 0.37], [0, -0.68, 0.075], shoes);
return pivot;
}
const leftLeg = makeLeg(-0.18);
const rightLeg = makeLeg(0.18);
function makeArm(x) {
const shoulder = new THREE.Group();
shoulder.position.set(x, 1.48, 0);
character.add(shoulder);
addMesh(new THREE.CylinderGeometry(0.09, 0.10, 0.45, 6), shirt, shoulder, [0, -0.22, 0]);
const elbow = new THREE.Group();
elbow.position.set(0, -0.44, 0);
shoulder.add(elbow);
addMesh(new THREE.CylinderGeometry(0.075, 0.085, 0.42, 6), palette.skin, elbow, [0, -0.21, 0]);
addMesh(new THREE.SphereGeometry(0.10, 7, 5), palette.skin, elbow, [0, -0.46, 0]);
shoulder.rotation.z = x > 0 ? -0.06 : 0.06;
return { shoulder, elbow };
}
const leftArm = makeArm(-0.47);
const rightArm = makeArm(0.47);
addMesh(new THREE.CylinderGeometry(0.11, 0.12, 0.18, 7), palette.skin, character, [0, 1.62, 0]);
const head = new THREE.Group();
head.position.set(0, 1.92, 0);
character.add(head);
addMesh(new THREE.SphereGeometry(0.31, 8, 6), palette.skin, head, [0, 0, 0]);
const hair = addMesh(
new THREE.SphereGeometry(0.315, 8, 6),
palette.hair,
head,
[0, 0.20, -0.025],
null,
[1.02, 0.50, 1.02]
);
hair.castShadow = true;
addMesh(new THREE.SphereGeometry(0.025, 5, 4), palette.charcoal, head, [-0.095, 0.025, 0.278]);
addMesh(new THREE.SphereGeometry(0.025, 5, 4), palette.charcoal, head, [0.095, 0.025, 0.278]);
const nose = addMesh(new THREE.ConeGeometry(0.035, 0.09, 5), palette.skin, head, [0, -0.02, 0.32]);
nose.rotation.x = Math.PI / 2;
box(head, [0.11, 0.022, 0.02], [0, -0.115, 0.283], material(0x9d594f));
// Small school poster.
box(room, [0.75, 0.95, 0.04], [-4.65, 2.1, -3.70], material(0xeab870));
box(room, [0.57, 0.08, 0.025], [-4.65, 2.38, -3.66], palette.charcoal);
box(room, [0.42, 0.08, 0.025], [-4.72, 2.18, -3.66], palette.charcoal);
box(room, [0.48, 0.08, 0.025], [-4.68, 1.98, -3.66], palette.charcoal);
// Animation helpers.
const tweens = [];
const easeInOut = (t) => t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
const easeOut = (t) => 1 - Math.pow(1 - t, 3);
const easeIn = (t) => t * t * t;
function tween(duration, update, easing = easeInOut) {
return new Promise((resolve) => {
tweens.push({
start: performance.now(),
duration,
update,
easing,
resolve
});
});
}
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const lerp = THREE.MathUtils.lerp;
function shortestAngle(from, to) {
return Math.atan2(Math.sin(to - from), Math.cos(to - from));
}
async function moveCharacter(x, z, rotation, duration = 900) {
const start = character.position.clone();
const startRotation = character.rotation.y;
const angleDelta = shortestAngle(startRotation, rotation);
await tween(duration, (eased, raw) => {
character.position.x = lerp(start.x, x, eased);
character.position.z = lerp(start.z, z, eased);
character.position.y = Math.abs(Math.sin(raw * Math.PI * 3)) * 0.045;
character.rotation.y = startRotation + angleDelta * eased;
const swing = Math.sin(raw * Math.PI * 6) * 0.38;
leftLeg.rotation.x = swing;
rightLeg.rotation.x = -swing;
});
character.position.y = 0;
leftLeg.rotation.x = 0;
rightLeg.rotation.x = 0;
}
function heldLunchboxPosition() {
character.updateMatrixWorld(true);
return character.localToWorld(new THREE.Vector3(0, 1.24, 0.72));
}
const state = {
foodPacked: false,
lunchboxInBag: false,
bagOnShoulder: false,
departed: false
};
let busy = false;
let autoRunning = false;
const packBtn = document.getElementById("packBtn");
const bagBtn = document.getElementById("bagBtn");
const shoulderBtn = document.getElementById("shoulderBtn");
const leaveBtn = document.getElementById("leaveBtn");
const autoBtn = document.getElementById("autoBtn");
const actionStatus = document.getElementById("actionStatus");
function announce(message) {
actionStatus.textContent = message;
}
function setBusy(value) {
busy = value;
updateUI();
}
function updateUI() {
const packedCount = Number(state.foodPacked) +
Number(state.lunchboxInBag) +
Number(state.bagOnShoulder);
document.getElementById("progressCount").textContent = `${packedCount} / 3`;
document.getElementById("progressFill").style.width = `${packedCount / 3 * 100}%`;
const setStep = (id, done, ready) => {
const el = document.getElementById(id);
el.classList.toggle("done", done);
el.classList.toggle("ready", !done && ready);
el.classList.toggle("locked", !done && !ready);
el.querySelector(".step-status").textContent = done ? "DONE" : ready ? "READY" : "LOCKED";
};
setStep("stepFood", state.foodPacked, true);
setStep("stepBag", state.lunchboxInBag, state.foodPacked);
setStep("stepShoulder", state.bagOnShoulder, state.lunchboxInBag);
setStep("stepLeave", state.departed, state.bagOnShoulder);
packBtn.disabled = busy || autoRunning || state.foodPacked;
bagBtn.disabled = busy || autoRunning || !state.foodPacked || state.lunchboxInBag;
shoulderBtn.disabled = busy || autoRunning || !state.lunchboxInBag || state.bagOnShoulder;
leaveBtn.disabled = busy || autoRunning || !state.bagOnShoulder || state.departed;
autoBtn.disabled = busy || autoRunning || state.departed;
packBtn.textContent = state.foodPacked ? "Packed ✓" : "Pack food";
bagBtn.textContent = state.lunchboxInBag ? "Loaded ✓" : "Load bag";
shoulderBtn.textContent = state.bagOnShoulder ? "On shoulder ✓" : "Shoulder bag";
leaveBtn.textContent = state.departed ? "Departed ✓" :
state.bagOnShoulder ? "Leave for school" : "Departure locked";
}
async function packFood() {
if (busy || state.foodPacked) return;
setBusy(true);
announce("Moving to the counter and packing the food...");
await moveCharacter(-1.85, -0.03, Math.PI, 850);
const foods = [
{ object: sandwich, target: new THREE.Vector3(-1.52, 1.39, -1.37) },
{ object: apple, target: new THREE.Vector3(-1.03, 1.39, -1.48) },
{ object: carrot, target: new THREE.Vector3(-1.23, 1.36, -1.15) }
];
for (const food of foods) {
const start = food.object.position.clone();
const startRotation = food.object.rotation.y;
await tween(800, (eased, raw) => {
food.object.position.lerpVectors(start, food.target, eased);
food.object.position.y += Math.sin(Math.PI * raw) * 0.38;
food.object.rotation.y = startRotation + raw * 0.7;
food.object.scale.setScalar(lerp(1, 0.72, eased));
rightArm.shoulder.rotation.x = -0.42 - Math.sin(Math.PI * raw) * 0.72;
rightArm.elbow.rotation.x = -0.18 - Math.sin(Math.PI * raw) * 0.3;
});
food.object.position.copy(food.target);
food.object.scale.setScalar(0.72);
}
await tween(500, (eased) => {
lunchLid.rotation.x = lerp(-1.95, 0, eased);
rightArm.shoulder.rotation.x = lerp(rightArm.shoulder.rotation.x, -0.12, eased);
});
packedBadge.visible = true;
state.foodPacked = true;
setBusy(false);
announce("Lunchbox packed. Loading it into the bag is now available.");
updateUI();
}
async function loadBag() {
if (busy || !state.foodPacked || state.lunchboxInBag) return;
setBusy(true);
announce("Picking up the packed lunchbox and loading the bag...");
const lunchStart = lunchbox.position.clone();
const heldTarget = heldLunchboxPosition();
await tween(700, (eased, raw) => {
lunchbox.position.lerpVectors(lunchStart, heldTarget, eased);
lunchbox.position.y += Math.sin(Math.PI * raw) * 0.26;
rightArm.shoulder.rotation.x = -0.85;
rightArm.elbow.rotation.x = -0.25;
leftArm.shoulder.rotation.x = -0.6;
});
await Promise.all([
moveCharacter(0.55, -0.05, Math.PI / 2, 1250),
tween(1250, () => {
lunchbox.position.copy(heldLunchboxPosition());
rightArm.shoulder.rotation.x = -0.92;
leftArm.shoulder.rotation.x = -0.84;
})
]);
bag.updateMatrixWorld(true);
const insideLocal = new THREE.Vector3(0, 0.28, 0);
const insideWorld = bag.localToWorld(insideLocal.clone());
const carryStart = lunchbox.position.clone();
await tween(850, (eased, raw) => {
lunchbox.position.lerpVectors(carryStart, insideWorld, eased);
lunchbox.position.y += Math.sin(Math.PI * raw) * 0.24;
lunchbox.scale.setScalar(lerp(0.72, 0.72, eased));
rightArm.shoulder.rotation.x = -1.05;
rightArm.elbow.rotation.x = -0.28;
});
bag.attach(lunchbox);
lunchbox.position.copy(insideLocal);
lunchbox.rotation.set(0, 0, 0);
lunchbox.scale.setScalar(0.72);
await tween(400, (eased) => {
bagFlap.rotation.x = lerp(-1.72, -0.48, eased);
});
state.lunchboxInBag = true;
setBusy(false);
announce("The lunchbox is visibly inside the bag. Shoulder the bag next.");
updateUI();
}
async function shoulderBag() {
if (busy || !state.lunchboxInBag || state.bagOnShoulder) return;
setBusy(true);
announce("Lifting the loaded bag onto the character’s shoulder...");
const bagStart = bag.position.clone();
const bagRotationStart = bag.rotation.y;
const targetLocal = new THREE.Vector3(0.64, 0.54, 0.05);
character.updateMatrixWorld(true);
const targetWorld = character.localToWorld(targetLocal.clone());
await tween(1050, (eased, raw) => {
bag.position.lerpVectors(bagStart, targetWorld, eased);
bag.position.y += Math.sin(Math.PI * raw) * 0.32;
bag.rotation.y = lerp(bagRotationStart, character.rotation.y, eased);
bag.rotation.z = lerp(0, -0.12, eased);
rightArm.shoulder.rotation.x = -1.08;
rightArm.elbow.rotation.x = -0.22;
leftArm.shoulder.rotation.x = -0.5;
});
character.attach(bag);
bag.position.copy(targetLocal);
bag.rotation.set(0, 0, -0.12);
bag.scale.setScalar(0.9);
await tween(400, (eased) => {
bagFlap.rotation.x = lerp(-0.48, -0.18, eased);
rightArm.shoulder.rotation.x = lerp(rightArm.shoulder.rotation.x, -0.18, eased);
});
bagRing.visible = false;
state.bagOnShoulder = true;
setBusy(false);
announce("Everything is packed. Departure is now unlocked.");
updateUI();
}
async function departHome() {
const allPacked = state.foodPacked && state.lunchboxInBag && state.bagOnShoulder;
if (busy || !allPacked || state.departed) return;
setBusy(true);
announce("All packed — opening the door and heading to school...");
await tween(750, (eased) => {
doorPivot.rotation.y = lerp(0, -1.2, eased);
});
await moveCharacter(2.75, -1.9, 2.42, 1250);
await moveCharacter(3.7, -4.25, Math.PI, 1050);
character.visible = false;
state.departed = true;
setBusy(false);
announce("Ready for school. Morning routine complete!");
updateUI();
}
async function playRoutine() {
if (busy || autoRunning || state.departed) return;
autoRunning = true;
updateUI();
try {
await packFood();
await delay(250);
await loadBag();
await delay(250);
await shoulderBag();
await delay(350);
await departHome();
} finally {
autoRunning = false;
updateUI();
}
}
packBtn.addEventListener("click", packFood);
bagBtn.addEventListener("click", loadBag);
shoulderBtn.addEventListener("click", shoulderBag);
leaveBtn.addEventListener("click", departHome);
autoBtn.addEventListener("click", playRoutine);
document.getElementById("resetBtn").addEventListener("click", () => location.reload());
document.getElementById("cameraReset").addEventListener("click", () => {
cameraTheta = 0.68;
cameraPhi = 1.03;
cameraRadius = 11.7;
updateCamera();
});
// Project DOM labels onto the 3D objects.
const foodTag = document.getElementById("foodTag");
const lunchTag = document.getElementById("lunchTag");
const bagTag = document.getElementById("bagTag");
const tempWorld = new THREE.Vector3();
const tempProjected = new THREE.Vector3();
function placeTag(element, worldPosition) {
tempProjected.copy(worldPosition).project(camera);
const visible = tempProjected.z > -1 && tempProjected.z < 1 &&
Math.abs(tempProjected.x) < 1.15 && Math.abs(tempProjected.y) < 1.15;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (tempProjected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-tempProjected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function updateTags() {
foodTag.textContent = state.foodPacked ? "FOOD PACKED" : "FOOD";
lunchTag.textContent = state.lunchboxInBag ? "LUNCHBOX IN BAG" : "LUNCHBOX";
bagTag.textContent = state.bagOnShoulder ? "BAG ON SHOULDER" : "BAG";
if (state.foodPacked) {
lunchbox.getWorldPosition(tempWorld);
tempWorld.y += 0.62;
} else {
tempWorld.set(-2.72, 1.91, -1.34);
}
placeTag(foodTag, tempWorld);
lunchbox.getWorldPosition(tempWorld);
tempWorld.y += 0.82;
placeTag(lunchTag, tempWorld);
bag.getWorldPosition(tempWorld);
tempWorld.y += 1.34;
placeTag(bagTag, tempWorld);
}
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);
for (let i = tweens.length - 1; i >= 0; i--) {
const tw = tweens[i];
const raw = THREE.MathUtils.clamp((now - tw.start) / tw.duration, 0, 1);
tw.update(tw.easing(raw), raw);
if (raw >= 1) {
tweens.splice(i, 1);
tw.resolve();
}
}
// Gentle idle breathing.
if (!busy) {
torso.scale.y = 1 + Math.sin(now * 0.0023) * 0.009;
}
updateTags();
renderer.render(scene, camera);
}
updateUI();
announce("Start by packing the food into the lunchbox.");
requestAnimationFrame(animate);
</script>
</body>
</html>
```