Prompt specification
Build or customize the Three.js 3D scene "Classroom parcel weighing play" 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: Classroom parcel weighing play
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/classroom-parcel-weighing-play-x0323
Scene Description Prompt:
Classroom parcel weighing play. Compose a pretend post office with scale, parcels, and standard weight blocks; users balance each parcel and record its mass as a count of reference blocks. Please build this with Three.js from cdnjs in one self-contained HTML file, using stylized solid shapes.
Working Three.js Starter (classroom-parcel-weighing-play-x0323):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Parcel Balance Post Office</title>
<style>
:root {
--ink: #183b43;
--muted: #658087;
--cream: #fffaf0;
--panel: rgba(255, 252, 243, 0.92);
--teal: #238b82;
--teal-dark: #176b65;
--gold: #f2b84b;
--coral: #e98268;
--green: #4f9d69;
--line: rgba(24, 59, 67, 0.13);
--shadow: 0 18px 45px rgba(36, 65, 67, 0.17);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--ink);
background: #dce9e5;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.topbar {
position: fixed;
z-index: 5;
top: 18px;
left: 20px;
right: 20px;
display: flex;
align-items: center;
justify-content: space-between;
pointer-events: none;
}
.brand,
.top-actions {
pointer-events: auto;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
padding: 10px 16px 10px 11px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 18px;
background: rgba(255, 252, 243, 0.92);
box-shadow: 0 10px 30px rgba(30, 63, 66, 0.13);
backdrop-filter: blur(12px);
}
.brand-icon {
display: grid;
place-items: center;
width: 42px;
height: 42px;
border-radius: 13px;
color: white;
font-size: 23px;
background: var(--teal);
box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.1);
}
.brand h1 {
margin: 0;
font-size: 17px;
line-height: 1.1;
letter-spacing: -0.02em;
}
.brand p {
margin: 4px 0 0;
color: var(--muted);
font-size: 11px;
font-weight: 650;
letter-spacing: 0.03em;
}
.top-actions {
display: flex;
gap: 8px;
}
.small-button {
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 12px;
padding: 10px 13px;
color: var(--ink);
background: rgba(255, 252, 243, 0.9);
box-shadow: 0 8px 24px rgba(30, 63, 66, 0.1);
cursor: pointer;
font-size: 12px;
font-weight: 750;
transition: transform 0.15s ease, background 0.15s ease;
}
.small-button:hover {
transform: translateY(-2px);
background: white;
}
.panel {
position: fixed;
z-index: 4;
border: 1px solid rgba(255, 255, 255, 0.82);
border-radius: 22px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(15px);
}
.tasks-panel {
top: 94px;
left: 20px;
width: 282px;
max-height: calc(100vh - 118px);
padding: 19px;
overflow: auto;
}
.eyebrow {
margin: 0 0 6px;
color: var(--teal);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.tasks-panel h2,
.console h2 {
margin: 0;
font-size: 21px;
line-height: 1.15;
letter-spacing: -0.035em;
}
.intro {
margin: 9px 0 16px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
#task-list {
display: grid;
gap: 9px;
}
.task-card {
display: grid;
grid-template-columns: 38px 1fr auto;
align-items: center;
gap: 10px;
width: 100%;
padding: 11px;
text-align: left;
color: var(--ink);
border: 1px solid var(--line);
border-radius: 15px;
background: rgba(255, 255, 255, 0.68);
cursor: pointer;
transition: transform 0.15s ease, border-color 0.15s ease,
box-shadow 0.15s ease;
}
.task-card:hover {
transform: translateY(-2px);
border-color: rgba(35, 139, 130, 0.42);
box-shadow: 0 8px 20px rgba(32, 71, 73, 0.08);
}
.task-card.active {
border-color: var(--teal);
background: rgba(224, 246, 238, 0.78);
box-shadow: 0 0 0 3px rgba(35, 139, 130, 0.08);
}
.task-card.complete {
background: rgba(231, 246, 232, 0.8);
}
.parcel-chip {
display: grid;
place-items: center;
width: 38px;
height: 38px;
border-radius: 12px;
color: white;
font-size: 16px;
font-weight: 850;
box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.12);
}
.task-name {
font-size: 12px;
font-weight: 800;
}
.task-detail {
margin-top: 3px;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
.badge {
padding: 5px 7px;
border-radius: 999px;
color: var(--muted);
background: rgba(24, 59, 67, 0.07);
font-size: 9px;
font-weight: 800;
white-space: nowrap;
}
.complete .badge {
color: #337b4e;
background: rgba(79, 157, 105, 0.13);
}
.how {
margin-top: 17px;
padding-top: 15px;
border-top: 1px solid var(--line);
}
.how h3 {
margin: 0 0 9px;
font-size: 11px;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.step {
display: flex;
gap: 9px;
align-items: flex-start;
margin: 8px 0;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
.step-number {
display: grid;
flex: 0 0 auto;
place-items: center;
width: 21px;
height: 21px;
border-radius: 7px;
color: var(--teal-dark);
background: rgba(35, 139, 130, 0.12);
font-size: 10px;
font-weight: 850;
}
.console {
top: 94px;
right: 20px;
width: 318px;
max-height: calc(100vh - 118px);
padding: 19px;
overflow: auto;
}
.selected-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin: 17px 0 12px;
}
.selected-name {
display: flex;
align-items: center;
gap: 9px;
font-size: 13px;
font-weight: 850;
}
#selected-swatch {
width: 13px;
height: 13px;
border-radius: 5px;
background: var(--gold);
box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.12);
}
#clear-scale {
padding: 6px 9px;
border: 0;
border-radius: 9px;
color: var(--muted);
background: rgba(24, 59, 67, 0.06);
cursor: pointer;
font-size: 10px;
font-weight: 750;
}
.counter-card {
padding: 15px;
border: 1px solid var(--line);
border-radius: 17px;
background: rgba(255, 255, 255, 0.68);
}
.counter-label {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.counter-value {
display: flex;
align-items: baseline;
gap: 7px;
margin: 5px 0 10px;
}
#block-count {
font-size: 38px;
line-height: 1;
font-weight: 900;
letter-spacing: -0.06em;
}
.counter-value span {
color: var(--muted);
font-size: 12px;
font-weight: 750;
}
#block-dots {
display: grid;
grid-template-columns: repeat(5, 1fr);
gap: 6px;
}
.block-dot {
height: 18px;
border-radius: 6px;
background: rgba(24, 59, 67, 0.08);
transition: background 0.15s ease, transform 0.15s ease;
}
.block-dot.filled {
transform: translateY(-1px);
background: var(--teal);
box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.12);
}
.block-controls {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 10px;
}
.control-button {
border: 0;
border-radius: 12px;
padding: 11px 8px;
cursor: pointer;
color: white;
background: var(--teal);
box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.12);
font-size: 12px;
font-weight: 850;
transition: transform 0.12s ease, filter 0.12s ease;
}
.control-button:hover:not(:disabled) {
transform: translateY(-2px);
filter: brightness(1.05);
}
.control-button:active:not(:disabled) {
transform: translateY(1px);
}
.control-button.secondary {
color: var(--ink);
background: rgba(24, 59, 67, 0.09);
box-shadow: none;
}
.control-button:disabled {
opacity: 0.38;
cursor: not-allowed;
}
.balance-card {
margin-top: 12px;
padding: 13px;
border-radius: 16px;
background: rgba(35, 139, 130, 0.08);
transition: background 0.2s ease;
}
.balance-card.balanced {
background: rgba(79, 157, 105, 0.13);
}
.balance-top {
display: flex;
align-items: center;
gap: 7px;
margin-bottom: 10px;
font-size: 11px;
font-weight: 850;
}
.status-dot {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--gold);
box-shadow: 0 0 0 4px rgba(242, 184, 75, 0.15);
}
.balanced .status-dot {
background: var(--green);
box-shadow: 0 0 0 4px rgba(79, 157, 105, 0.14);
}
.balance-track {
position: relative;
height: 9px;
border-radius: 999px;
background: rgba(24, 59, 67, 0.1);
}
.balance-track::after {
position: absolute;
top: -3px;
bottom: -3px;
left: 50%;
width: 2px;
content: "";
background: rgba(24, 59, 67, 0.35);
}
#balance-needle {
position: absolute;
top: 50%;
left: 50%;
width: 17px;
height: 17px;
border: 3px solid white;
border-radius: 50%;
background: var(--gold);
box-shadow: 0 3px 10px rgba(24, 59, 67, 0.22);
transform: translate(-50%, -50%);
transition: left 0.25s ease, background 0.2s ease;
}
.balanced #balance-needle {
background: var(--green);
}
#balance-hint {
margin: 9px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.4;
}
.record-area {
margin-top: 12px;
}
#record-weight {
width: 100%;
border: 0;
border-radius: 13px;
padding: 13px;
color: white;
background: var(--coral);
box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.12);
cursor: pointer;
font-size: 12px;
font-weight: 900;
letter-spacing: 0.02em;
}
#record-weight:disabled {
opacity: 0.38;
cursor: not-allowed;
}
#next-parcel {
width: 100%;
margin-top: 8px;
border: 1px solid var(--line);
border-radius: 12px;
padding: 11px;
color: var(--teal-dark);
background: rgba(35, 139, 130, 0.08);
cursor: pointer;
font-size: 11px;
font-weight: 850;
}
.mass-note {
margin-top: 10px;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
text-align: center;
}
.orbit-hint {
position: fixed;
z-index: 3;
bottom: 16px;
left: 50%;
padding: 8px 12px;
border-radius: 999px;
color: rgba(24, 59, 67, 0.76);
background: rgba(255, 252, 243, 0.72);
font-size: 10px;
font-weight: 700;
transform: translateX(-50%);
pointer-events: none;
backdrop-filter: blur(8px);
}
#toast {
position: fixed;
z-index: 10;
top: 88px;
left: 50%;
max-width: min(440px, calc(100vw - 30px));
padding: 12px 17px;
border-radius: 13px;
color: white;
background: var(--ink);
box-shadow: 0 14px 35px rgba(24, 59, 67, 0.25);
font-size: 12px;
font-weight: 750;
text-align: center;
opacity: 0;
transform: translate(-50%, -12px);
transition: opacity 0.2s ease, transform 0.2s ease;
pointer-events: none;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 760px) {
.topbar {
top: 10px;
left: 10px;
right: 10px;
}
.brand {
padding: 8px 11px 8px 8px;
}
.brand-icon {
width: 34px;
height: 34px;
border-radius: 10px;
font-size: 18px;
}
.brand h1 {
font-size: 14px;
}
.brand p,
.small-button {
display: none;
}
.tasks-panel {
top: 66px;
left: 10px;
right: 10px;
width: auto;
padding: 12px;
max-height: none;
}
.tasks-panel > .eyebrow,
.tasks-panel h2,
.intro,
.how {
display: none;
}
#task-list {
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 6px;
}
.task-card {
grid-template-columns: 28px 1fr;
gap: 7px;
padding: 8px;
}
.parcel-chip {
width: 28px;
height: 28px;
border-radius: 9px;
font-size: 12px;
}
.task-name {
overflow: hidden;
font-size: 10px;
text-overflow: ellipsis;
white-space: nowrap;
}
.task-detail,
.badge {
display: none;
}
.console {
top: auto;
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
max-height: 44vh;
padding: 13px;
}
.console > .eyebrow,
.console h2 {
display: none;
}
.selected-row {
margin-top: 0;
}
.counter-card {
padding: 10px;
}
.counter-value {
margin: 3px 0 7px;
}
#block-count {
font-size: 29px;
}
.block-dot {
height: 13px;
}
.balance-card {
margin-top: 8px;
padding: 10px;
}
.record-area {
margin-top: 8px;
}
.mass-note,
.orbit-hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive post office parcel weighing scene"></canvas>
<header class="topbar">
<div class="brand">
<div class="brand-icon">✉</div>
<div>
<h1>Parcel Balance Post</h1>
<p>CLASSROOM WEIGHING PLAY</p>
</div>
</div>
<div class="top-actions">
<button class="small-button" id="reset-view">Reset view</button>
<button class="small-button" id="reset-activity">Reset activity</button>
</div>
</header>
<aside class="panel tasks-panel">
<p class="eyebrow">Today's deliveries</p>
<h2>Weigh every parcel</h2>
<p class="intro">
Each standard block weighs exactly <strong>1 kg</strong>.
Balance the parcel, then record the number of blocks.
</p>
<div id="task-list"></div>
<section class="how">
<h3>How to play</h3>
<div class="step">
<span class="step-number">1</span>
<span>Click a parcel on the table or choose a delivery card.</span>
</div>
<div class="step">
<span class="step-number">2</span>
<span>Add or remove 1 kg blocks until the beam balances.</span>
</div>
<div class="step">
<span class="step-number">3</span>
<span>Record the block count as the parcel's mass.</span>
</div>
</section>
</aside>
<aside class="panel console">
<p class="eyebrow">Balance desk</p>
<h2>Match the blocks</h2>
<div class="selected-row">
<div class="selected-name">
<span id="selected-swatch"></span>
<span id="selected-name">Choose a parcel</span>
</div>
<button id="clear-scale">Return parcel</button>
</div>
<div class="counter-card">
<div class="counter-label">Reference blocks on the pan</div>
<div class="counter-value">
<strong id="block-count">0</strong>
<span>× 1 kg blocks</span>
</div>
<div id="block-dots" aria-label="Reference block count"></div>
<div class="block-controls">
<button class="control-button secondary" id="remove-block">− Remove block</button>
<button class="control-button" id="add-block">+ Add block</button>
</div>
</div>
<div class="balance-card" id="balance-card">
<div class="balance-top">
<span class="status-dot"></span>
<span id="balance-status">Waiting for a parcel</span>
</div>
<div class="balance-track">
<span id="balance-needle"></span>
</div>
<p id="balance-hint">Choose a parcel to begin weighing.</p>
</div>
<div class="record-area">
<button id="record-weight" disabled>Record the mass</button>
<button id="next-parcel" hidden>Choose the next parcel →</button>
</div>
<p class="mass-note">
The recorded block count is the parcel's mass in kilograms.
</p>
</aside>
<div class="orbit-hint">Drag the room to orbit · Scroll to zoom</div>
<div id="toast" role="status" aria-live="polite"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce9e5);
scene.fog = new THREE.Fog(0xdce9e5, 16, 28);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
80
);
const cameraTarget = new THREE.Vector3(0, 1.15, 0);
let cameraTheta = 0.38;
let cameraPhi = 0.99;
let cameraRadius = 13.2;
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();
const hemisphere = new THREE.HemisphereLight(0xfff8e8, 0x66837e, 2.3);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff3d8, 3.1);
sun.position.set(-5, 10, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.0004;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb9e5df, 1.1);
fill.position.set(7, 5, -4);
scene.add(fill);
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
...options
});
}
function addBox(
parent,
width,
height,
depth,
color,
x = 0,
y = 0,
z = 0,
options = {}
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
color instanceof THREE.Material ? color : material(color, options)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(
parent,
radiusTop,
radiusBottom,
height,
color,
x = 0,
y = 0,
z = 0,
segments = 24
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusTop, radiusBottom, height, segments),
color instanceof THREE.Material ? color : material(color)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function roundedRectPath(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + width - r, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + r);
ctx.lineTo(x + width, y + height - r);
ctx.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
ctx.lineTo(x + r, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function makeTextTexture(text, background = "#fffaf0", foreground = "#183b43") {
const textCanvas = document.createElement("canvas");
textCanvas.width = 512;
textCanvas.height = 128;
const ctx = textCanvas.getContext("2d");
ctx.clearRect(0, 0, 512, 128);
roundedRectPath(ctx, 6, 6, 500, 116, 28);
ctx.fillStyle = background;
ctx.fill();
ctx.fillStyle = foreground;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "800 49px system-ui, sans-serif";
ctx.fillText(text, 256, 65, 460);
const texture = new THREE.CanvasTexture(textCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
function makeTextPlane(
text,
width,
height,
background,
foreground,
parent = scene
) {
const texture = makeTextTexture(text, background, foreground);
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
toneMapped: false
})
);
parent.add(mesh);
return mesh;
}
function makeSprite(text, width = 1.5, background = "#fffaf0") {
const texture = makeTextTexture(text, background, "#183b43");
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
toneMapped: false,
depthTest: true
})
);
sprite.scale.set(width, width * 0.25, 1);
return sprite;
}
// ------------------------------------------------------------
// Stylized post office room
// ------------------------------------------------------------
addBox(scene, 14, 0.22, 11, 0xd8c6a5, 0, -0.12, 0);
addBox(scene, 14, 5.6, 0.25, 0xf3e9d5, 0, 2.8, -5.72);
addBox(scene, 0.25, 5.6, 11, 0xe8dcc6, -7, 2.8, 0);
addBox(scene, 14, 0.18, 0.12, 0x8eb6ad, 0, 0.1, -5.54);
addBox(scene, 0.12, 0.18, 11, 0x8eb6ad, -6.84, 0.1, 0);
const rug = addCylinder(scene, 3.5, 3.5, 0.025, 0xe9d9b9, 0.45, 0.012, 0.35, 56);
rug.scale.set(1.45, 1, 0.72);
rug.receiveShadow = true;
// Wall sign.
const officeSign = makeTextPlane(
"PARCEL BALANCE POST",
4.2,
0.88,
"#238b82",
"#fffaf0"
);
officeSign.position.set(0.2, 3.72, -5.56);
const motto = makeTextPlane(
"WEIGH • COUNT • RECORD",
3.1,
0.48,
"#fffaf0",
"#176b65"
);
motto.position.set(0.2, 3.03, -5.55);
// Service counter.
addBox(scene, 5.5, 1.08, 1.15, 0xc46f59, 2.7, 0.54, -4.65);
addBox(scene, 5.8, 0.16, 1.32, 0xf0d7a8, 2.7, 1.16, -4.65);
for (let i = 0; i < 3; i++) {
addBox(
scene,
1.25,
0.68,
0.05,
0xe5a58f,
1.2 + i * 1.5,
0.61,
-4.055
);
addBox(
scene,
0.56,
0.06,
0.07,
0x7e5146,
1.2 + i * 1.5,
0.7,
-4.015
);
}
// Sorting cubbies.
addBox(scene, 4.0, 1.05, 0.2, 0x8bbab0, 2.7, 2.45, -5.47);
for (let row = 0; row < 2; row++) {
for (let col = 0; col < 4; col++) {
addBox(
scene,
0.77,
0.33,
0.1,
0x335d5e,
1.17 + col * 1.02,
2.17 + row * 0.55,
-5.33
);
}
}
// Shelf and small parcels.
addBox(scene, 3.0, 0.13, 0.66, 0xb78358, -3.9, 1.15, -5.19);
addBox(scene, 3.0, 0.13, 0.66, 0xb78358, -3.9, 2.15, -5.19);
addBox(scene, 3.0, 0.13, 0.66, 0xb78358, -3.9, 3.15, -5.19);
for (let i = 0; i < 3; i++) {
addBox(scene, 0.58, 0.55, 0.48, 0xe6b453, -4.85 + i * 0.85, 1.49, -5.13);
addBox(scene, 0.12, 0.56, 0.49, 0xf8e6b4, -4.85 + i * 0.85, 1.49, -5.12);
}
for (let i = 0; i < 2; i++) {
addBox(scene, 0.66, 0.48, 0.52, 0x76aaa0, -4.45 + i * 1.0, 2.46, -5.12);
addBox(scene, 0.11, 0.49, 0.53, 0xf7e6b9, -4.45 + i * 1.0, 2.46, -5.11);
}
// Wall clock.
const clock = addCylinder(scene, 0.47, 0.47, 0.13, 0xfff7df, 5.05, 3.76, -5.43, 40);
clock.rotation.x = Math.PI / 2;
const clockRim = new THREE.Mesh(
new THREE.TorusGeometry(0.46, 0.045, 10, 40),
material(0x315f61)
);
clockRim.position.set(5.05, 3.76, -5.34);
scene.add(clockRim);
addBox(scene, 0.05, 0.3, 0.035, 0x315f61, 5.05, 3.84, -5.31);
addBox(scene, 0.23, 0.05, 0.035, 0x315f61, 5.14, 3.76, -5.31);
// Parcel staging table.
addBox(scene, 3.65, 0.16, 1.45, 0xe0b46d, -3.55, 0.72, 0.2);
addBox(scene, 0.18, 0.7, 1.15, 0x9b704b, -5.05, 0.35, 0.2);
addBox(scene, 0.18, 0.7, 1.15, 0x9b704b, -2.05, 0.35, 0.2);
const tableSign = makeSprite("CLICK A PARCEL", 1.75, "#fff3cc");
tableSign.position.set(-3.55, 1.77, 0.55);
scene.add(tableSign);
// Reference block supply table.
addBox(scene, 1.6, 0.13, 1.15, 0x9fbaa6, 4.3, 0.7, 0.38);
addBox(scene, 0.14, 0.65, 0.9, 0x6e9586, 3.65, 0.35, 0.38);
addBox(scene, 0.14, 0.65, 0.9, 0x6e9586, 4.95, 0.35, 0.38);
const blockSign = makeSprite("ADD 1 kg", 1.18, "#d9f1e9");
blockSign.position.set(4.3, 1.47, 0.63);
scene.add(blockSign);
// Decorative post box.
addBox(scene, 0.92, 1.35, 0.72, 0xc85d4e, 5.7, 0.68, -3.5);
addBox(scene, 1.05, 0.16, 0.84, 0x315f61, 5.7, 1.4, -3.5);
addBox(scene, 0.58, 0.09, 0.08, 0x315f61, 5.7, 1.05, -3.1);
addBox(scene, 0.42, 0.08, 0.08, 0xf5dfaf, 5.7, 0.62, -3.1);
// ------------------------------------------------------------
// Parcels
// ------------------------------------------------------------
const parcelData = [
{
id: "A",
name: "Sunrise Mail",
actual: 3,
color: 0xe7ad45,
home: new THREE.Vector3(-4.65, 1.08, 0.14),
rotation: -0.08,
recorded: false,
recordedMass: null
},
{
id: "B",
name: "Harbor Box",
actual: 5,
color: 0x67a79d,
home: new THREE.Vector3(-3.55, 1.08, 0.18),
rotation: 0.04,
recorded: false,
recordedMass: null
},
{
id: "C",
name: "Garden Parcel",
actual: 2,
color: 0xe58069,
home: new THREE.Vector3(-2.45, 1.08, 0.13),
rotation: -0.03,
recorded: false,
recordedMass: null
}
];
const parcelGroups = [];
function createParcel(parcel) {
const group = new THREE.Group();
group.position.copy(parcel.home);
group.rotation.y = parcel.rotation;
group.userData.parcelId = parcel.id;
addBox(group, 0.92, 0.61, 0.78, parcel.color, 0, -0.045, 0);
addBox(group, 0.96, 0.14, 0.82, parcel.color, 0, 0.295, 0);
// Packing tape.
addBox(group, 0.18, 0.025, 0.84, 0xf8e6b7, 0, 0.377, 0);
addBox(group, 0.18, 0.69, 0.025, 0xf8e6b7, 0, 0, 0.402);
const label = makeTextPlane(
parcel.id,
0.34,
0.2,
"#fffaf0",
"#183b43",
group
);
label.position.set(0.22, 0.04, 0.42);
group.traverse((object) => {
if (object.isMesh) object.userData.parcelId = parcel.id;
});
parcel.group = group;
parcel.label = label;
parcelGroups.push(group);
scene.add(group);
}
parcelData.forEach(createParcel);
function updateParcelLabel(parcel, text) {
const oldMap = parcel.label.material.map;
parcel.label.material.map = makeTextTexture(text, "#fffaf0", "#183b43");
parcel.label.material.needsUpdate = true;
if (oldMap) oldMap.dispose();
}
// ------------------------------------------------------------
// Two-pan balance scale
// ------------------------------------------------------------
const scaleRoot = new THREE.Group();
scaleRoot.position.set(1.05, 0, 0.25);
scene.add(scaleRoot);
addBox(scaleRoot, 2.45, 0.18, 1.55, 0x315f61, 0, 0.09, 0);
addBox(scaleRoot, 1.65, 0.12, 1.05, 0x77a79b, 0, 0.21, 0);
const column = addCylinder(scaleRoot, 0.14, 0.21, 1.86, 0x4b7771, 0, 1.19, 0, 20);
column.castShadow = true;
const pivot = new THREE.Mesh(
new THREE.SphereGeometry(0.23, 24, 16),
material(0xf0b94f)
);
pivot.position.set(0, 2.16, 0);
pivot.castShadow = true;
scaleRoot.add(pivot);
const beamGroup = new THREE.Group();
beamGroup.position.set(0, 2.16, 0);
scaleRoot.add(beamGroup);
addBox(beamGroup, 3.8, 0.13, 0.19, 0xd29d45, 0, 0, 0);
addBox(beamGroup, 0.28, 0.22, 0.28, 0xe9b655, -1.75, 0, 0);
addBox(beamGroup, 0.28, 0.22, 0.28, 0xe9b655, 1.75, 0, 0);
const panLeft = new THREE.Group();
const panRight = new THREE.Group();
scaleRoot.add(panLeft, panRight);
function createPan(parent, radius, color) {
const plate = addCylinder(parent, radius, radius * 0.94, 0.12, color, 0, 0, 0, 48);
const rim = new THREE.Mesh(
new THREE.TorusGeometry(radius, 0.055, 10, 48),
material(0x386865)
);
rim.rotation.x = Math.PI / 2;
rim.position.y = 0.075;
rim.castShadow = true;
parent.add(rim);
return plate;
}
createPan(panLeft, 0.93, 0xf0c56e);
createPan(panRight, 0.86, 0x80b9a9);
const leftPanLabel = makeSprite("PARCEL", 1.1, "#fff0c9");
leftPanLabel.position.set(-0.05, -0.35, 0.76);
panLeft.add(leftPanLabel);
const rightPanLabel = makeSprite("1 kg BLOCKS", 1.45, "#d9f1e9");
rightPanLabel.position.set(0, -0.35, 0.7);
panRight.add(rightPanLabel);
const stringMaterial = material(0x4a6967, { metalness: 0.15 });
const strings = [];
for (const side of ["left", "right"]) {
for (const offset of [-1, 1]) {
const string = new THREE.Mesh(
new THREE.CylinderGeometry(0.015, 0.015, 1, 8),
stringMaterial
);
string.castShadow = true;
scaleRoot.add(string);
strings.push({ side, offset, mesh: string });
}
}
const yAxis = new THREE.Vector3(0, 1, 0);
function positionCylinderBetween(mesh, a, b) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.scale.set(1, length, 1);
mesh.quaternion.setFromUnitVectors(yAxis, direction.normalize());
}
// Five reference blocks live on the right pan.
const scaleBlocks = [];
function createScaleBlock(index) {
const group = new THREE.Group();
group.visible = false;
group.userData.action = "removeBlock";
addBox(group, 0.4, 0.31, 0.4, 0x3e958a, 0, 0, 0);
const label = makeTextPlane(
"1 kg",
0.31,
0.14,
"#f7f0d7",
"#245f5b",
group
);
label.position.set(0, 0, 0.205);
group.traverse((object) => {
if (object.isMesh) object.userData.action = "removeBlock";
});
group.userData.index = index;
scaleBlocks.push(group);
scene.add(group);
}
for (let i = 0; i < 5; i++) createScaleBlock(i);
// A physical supply block can be clicked to add another block.
const supplyBlock = new THREE.Group();
supplyBlock.position.set(4.3, 0.91, 0.38);
supplyBlock.userData.action = "addBlock";
addBox(supplyBlock, 0.52, 0.38, 0.52, 0x3e958a, 0, 0, 0);
const supplyLabel = makeTextPlane(
"1 kg",
0.39,
0.18,
"#f7f0d7",
"#245f5b",
supplyBlock
);
supplyLabel.position.set(0, 0, 0.265);
supplyBlock.traverse((object) => {
if (object.isMesh) object.userData.action = "addBlock";
});
scene.add(supplyBlock);
// ------------------------------------------------------------
// Application state and UI
// ------------------------------------------------------------
let selectedId = null;
let blockCount = 0;
let currentAngle = 0;
let toastTimer = null;
const taskList = document.querySelector("#task-list");
const selectedName = document.querySelector("#selected-name");
const selectedSwatch = document.querySelector("#selected-swatch");
const blockCountElement = document.querySelector("#block-count");
const blockDots = document.querySelector("#block-dots");
const balanceCard = document.querySelector("#balance-card");
const balanceStatus = document.querySelector("#balance-status");
const balanceHint = document.querySelector("#balance-hint");
const balanceNeedle = document.querySelector("#balance-needle");
const addBlockButton = document.querySelector("#add-block");
const removeBlockButton = document.querySelector("#remove-block");
const recordButton = document.querySelector("#record-weight");
const nextButton = document.querySelector("#next-parcel");
const clearButton = document.querySelector("#clear-scale");
const toast = document.querySelector("#toast");
for (let i = 0; i < 5; i++) {
const dot = document.createElement("span");
dot.className = "block-dot";
blockDots.appendChild(dot);
}
function selectedParcel() {
return parcelData.find((parcel) => parcel.id === selectedId) || null;
}
function showToast(message) {
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 2600);
}
function renderTasks() {
taskList.innerHTML = "";
for (const parcel of parcelData) {
const card = document.createElement("button");
card.className = "task-card";
card.type = "button";
card.setAttribute("aria-pressed", String(parcel.id === selectedId));
if (parcel.id === selectedId && !parcel.recorded) {
card.classList.add("active");
}
if (parcel.recorded) {
card.classList.add("complete");
}
const detail = parcel.recorded
? `${parcel.recordedMass} kg · ${parcel.recordedMass} block${
parcel.recordedMass === 1 ? "" : "s"
}`
: parcel.id === selectedId
? "On the balance"
: "Ready to weigh";
const badge = parcel.recorded
? "Done"
: parcel.id === selectedId
? "Weighing"
: "To do";
card.innerHTML = `
<span class="parcel-chip" style="background:#${parcel.color
.toString(16)
.padStart(6, "0")}">${parcel.id}</span>
<span>
<span class="task-name">${parcel.name}</span>
<span class="task-detail" style="display:block">${detail}</span>
</span>
<span class="badge">${badge}</span>
`;
card.addEventListener("click", () => selectParcel(parcel.id));
taskList.appendChild(card);
}
}
function updateUI() {
const parcel = selectedParcel();
const difference = parcel ? parcel.actual - blockCount : 0;
const balanced = Boolean(parcel && difference === 0);
selectedName.textContent = parcel
? `Parcel ${parcel.id} · ${parcel.name}`
: "Choose a parcel";
selectedSwatch.style.background = parcel
? `#${parcel.color.toString(16).padStart(6, "0")}`
: "#f2b84b";
blockCountElement.textContent = blockCount;
[...blockDots.children].forEach((dot, index) => {
dot.classList.toggle("filled", index < blockCount);
});
addBlockButton.disabled = !parcel || parcel.recorded || blockCount >= 5;
removeBlockButton.disabled = !parcel || parcel.recorded || blockCount === 0;
clearButton.disabled = !parcel;
balanceCard.classList.toggle("balanced", balanced);
if (!parcel) {
balanceStatus.textContent = "Waiting for a parcel";
balanceHint.textContent = "Choose a parcel to begin weighing.";
balanceNeedle.style.left = "50%";
} else if (parcel.recorded) {
balanceStatus.textContent = `Recorded: ${parcel.recordedMass} kg`;
balanceHint.textContent = `${parcel.recordedMass} reference block${
parcel.recordedMass === 1 ? "" : "s"
} exactly balanced Parcel ${parcel.id}.`;
balanceNeedle.style.left = "50%";
} else if (difference > 0) {
balanceStatus.textContent = "The parcel side is heavier";
balanceHint.textContent =
"Add one more 1 kg block to the other pan, then watch the beam.";
balanceNeedle.style.left = `${50 - Math.min(difference, 3) * 14}%`;
} else if (difference < 0) {
balanceStatus.textContent = "The block side is heavier";
balanceHint.textContent =
"Remove one 1 kg block, then watch the beam move toward level.";
balanceNeedle.style.left = `${50 + Math.min(-difference, 3) * 14}%`;
} else {
balanceStatus.textContent = "Balanced!";
balanceHint.textContent =
"The beam is level. Record this number of 1 kg blocks as the mass.";
balanceNeedle.style.left = "50%";
}
recordButton.disabled = !balanced || parcel.recorded;
recordButton.textContent = parcel?.recorded
? "Mass recorded ✓"
: "Record the mass";
const hasPending = parcelData.some((item) => !item.recorded);
nextButton.hidden = !(parcel?.recorded && hasPending);
renderTasks();
}
function selectParcel(id) {
const parcel = parcelData.find((item) => item.id === id);
if (!parcel) return;
if (selectedId !== id) {
blockCount = parcel.recorded ? parcel.recordedMass : 0;
}
selectedId = id;
updateUI();
showToast(
parcel.recorded
? `Parcel ${id} is already recorded as ${parcel.recordedMass} kg.`
: `Parcel ${id} is on the left pan. Add 1 kg blocks to balance it.`
);
}
function clearScale() {
selectedId = null;
blockCount = 0;
updateUI();
}
function addBlock() {
const parcel = selectedParcel();
if (!parcel || parcel.recorded || blockCount >= 5) return;
blockCount += 1;
updateUI();
}
function removeBlock() {
const parcel = selectedParcel();
if (!parcel || parcel.recorded || blockCount === 0) return;
blockCount -= 1;
updateUI();
}
function recordWeight() {
const parcel = selectedParcel();
if (!parcel || parcel.recorded) return;
if (parcel.actual !== blockCount) return;
parcel.recorded = true;
parcel.recordedMass = blockCount;
updateParcelLabel(parcel, `${parcel.id} · ${blockCount} kg`);
showToast(
`Parcel ${parcel.id} weighs ${blockCount} kg, measured with ${blockCount} reference block${
blockCount === 1 ? "" : "s"
}.`
);
if (parcelData.every((item) => item.recorded)) {
spawnConfetti();
showToast("All parcels weighed! The post office is ready for delivery.");
}
updateUI();
}
function nextParcel() {
const next = parcelData.find((item) => !item.recorded);
if (next) selectParcel(next.id);
else showToast("Every parcel has been weighed. Great work!");
}
addBlockButton.addEventListener("click", addBlock);
removeBlockButton.addEventListener("click", removeBlock);
recordButton.addEventListener("click", recordWeight);
nextButton.addEventListener("click", nextParcel);
clearButton.addEventListener("click", clearScale);
document.querySelector("#reset-view").addEventListener("click", () => {
cameraTheta = 0.38;
cameraPhi = 0.99;
cameraRadius = 13.2;
updateCamera();
});
document.querySelector("#reset-activity").addEventListener("click", () => {
selectedId = null;
blockCount = 0;
for (const parcel of parcelData) {
parcel.recorded = false;
parcel.recordedMass = null;
updateParcelLabel(parcel, parcel.id);
}
clearConfetti();
updateUI();
showToast("Activity reset. Choose a parcel to begin again.");
});
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLButtonElement) return;
if (event.key === "+" || event.key === "=") {
addBlock();
} else if (event.key === "-" || event.key === "_") {
removeBlock();
} else if (event.key === "Enter") {
recordWeight();
}
});
// ------------------------------------------------------------
// Direct camera controls and object picking
// ------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerStart = null;
let pointerMoved = false;
function setPointerFromEvent(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function findUserData(object, key) {
while (object) {
if (object.userData[key]) return object.userData[key];
object = object.parent;
}
return null;
}
function isReallyVisible(object) {
while (object) {
if (!object.visible) return false;
object = object.parent;
}
return true;
}
function pick(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const pickables = [
...parcelGroups,
...scaleBlocks,
supplyBlock
];
const hits = raycaster.intersectObjects(pickables, true);
for (const hit of hits) {
if (!isReallyVisible(hit.object)) continue;
const parcelId = findUserData(hit.object, "parcelId");
if (parcelId) {
selectParcel(parcelId);
return;
}
const action = findUserData(hit.object, "action");
if (action === "addBlock") {
addBlock();
return;
}
if (action === "removeBlock") {
removeBlock();
return;
}
}
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointerStart = {
x: event.clientX,
y: event.clientY,
lastX: event.clientX,
lastY: event.clientY
};
pointerMoved = false;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!pointerStart) return;
const dx = event.clientX - pointerStart.lastX;
const dy = event.clientY - pointerStart.lastY;
if (
Math.abs(event.clientX - pointerStart.x) +
Math.abs(event.clientY - pointerStart.y) >
7
) {
pointerMoved = true;
}
if (pointerMoved) {
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi + dy * 0.005,
0.62,
1.34
);
updateCamera();
}
pointerStart.lastX = event.clientX;
pointerStart.lastY = event.clientY;
});
canvas.addEventListener("pointerup", (event) => {
if (!pointerMoved) pick(event);
pointerStart = null;
pointerMoved = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", () => {
pointerStart = null;
pointerMoved = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.009,
8.5,
18
);
updateCamera();
},
{ passive: false }
);
// ------------------------------------------------------------
// Confetti
// ------------------------------------------------------------
const confetti = [];
const confettiGeometry = new THREE.BoxGeometry(0.09, 0.18, 0.035);
const confettiMaterials = [
material(0xf2b84b),
material(0xe98268),
material(0x238b82),
material(0x8cc8ad)
];
function spawnConfetti() {
for (let i = 0; i < 64; i++) {
const mesh = new THREE.Mesh(
confettiGeometry,
confettiMaterials[i % confettiMaterials.length]
);
mesh.position.set(
(Math.random() - 0.5) * 4,
3.2 + Math.random() * 1.2,
0.8 + (Math.random() - 0.5) * 2
);
mesh.rotation.set(
Math.random() * Math.PI,
Math.random() * Math.PI,
Math.random() * Math.PI
);
scene.add(mesh);
confetti.push({
mesh,
velocity: new THREE.Vector3(
(Math.random() - 0.5) * 3.5,
2 + Math.random() * 3,
(Math.random() - 0.5) * 2.5
),
spin: new THREE.Vector3(
Math.random() * 5,
Math.random() * 5,
Math.random() * 5
),
life: 0
});
}
}
function clearConfetti() {
for (const piece of confetti) scene.remove(piece.mesh);
confetti.length = 0;
}
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
function damp(current, target, lambda, dt) {
return THREE.MathUtils.lerp(
current,
target,
1 - Math.exp(-lambda * dt)
);
}
function updateScale(dt) {
const parcel = selectedParcel();
const difference = parcel ? parcel.actual - blockCount : 0;
const targetAngle = THREE.MathUtils.clamp(
difference * 0.075,
-0.34,
0.34
);
currentAngle = damp(currentAngle, targetAngle, 6, dt);
beamGroup.rotation.z = currentAngle;
const arm = 1.75;
const cos = Math.cos(currentAngle);
const sin = Math.sin(currentAngle);
const leftEnd = new THREE.Vector2(
-arm * cos,
2.16 - arm * sin
);
const rightEnd = new THREE.Vector2(
arm * cos,
2.16 + arm * sin
);
const stringLength = 0.72;
panLeft.position.set(
leftEnd.x,
leftEnd.y - stringLength,
0
);
panRight.position.set(
rightEnd.x,
rightEnd.y - stringLength,
0
);
for (const string of strings) {
const end = string.side === "left" ? leftEnd : rightEnd;
const pan = string.side === "left" ? panLeft : panRight;
const top = new THREE.Vector3(end.x, end.y, 0);
const bottom = new THREE.Vector3(
pan.position.x + string.offset * 0.5,
pan.position.y + 0.07,
0
);
positionCylinderBetween(string.mesh, top, bottom);
}
const statusColor = difference === 0 ? 0x4f9d69 : 0xf0b94f;
pivot.material.color.setHex(statusColor);
// Parcels move smoothly between the table and the balance pan.
for (const parcel of parcelData) {
const group = parcel.group;
if (parcel.id === selectedId) {
const target = scaleRoot.localToWorld(
new THREE.Vector3(
panLeft.position.x,
panLeft.position.y + 0.08 + 0.36,
panLeft.position.z
)
);
group.position.lerp(target, 1 - Math.exp(-6 * dt));
group.rotation.x = damp(group.rotation.x, 0, 7, dt);
group.rotation.y = damp(group.rotation.y, 0, 7, dt);
group.rotation.z = damp(group.rotation.z, 0, 7, dt);
} else {
group.position.lerp(parcel.home, 1 - Math.exp(-6 * dt));
group.rotation.y = damp(
group.rotation.y,
parcel.rotation,
6,
dt
);
}
}
// Reference blocks stack on the right pan.
const supplyWorld = supplyBlock.position.clone();
for (let i = 0; i < scaleBlocks.length; i++) {
const block = scaleBlocks[i];
const wasVisible = block.visible;
block.visible = i < blockCount;
if (!block.visible) continue;
const layer = Math.floor(i / 2);
const slot = i % 2;
let offsetX = slot === 0 ? -0.23 : 0.23;
if (i === 4) offsetX = 0;
const target = scaleRoot.localToWorld(
new THREE.Vector3(
panRight.position.x + offsetX,
panRight.position.y + 0.08 + 0.155 + layer * 0.315,
panRight.position.z
)
);
if (!wasVisible) {
block.position.copy(supplyWorld);
block.scale.setScalar(0.5);
}
block.position.lerp(target, 1 - Math.exp(-8 * dt));
block.scale.lerp(new THREE.Vector3(1, 1, 1), 1 - Math.exp(-8 * dt));
block.rotation.y = damp(
block.rotation.y,
i % 2 === 0 ? -0.05 : 0.05,
6,
dt
);
}
}
function updateConfetti(dt) {
for (let i = confetti.length - 1; i >= 0; i--) {
const piece = confetti[i];
piece.life += dt;
piece.velocity.y -= 4.8 * dt;
piece.mesh.position.addScaledVector(piece.velocity, dt);
piece.mesh.rotation.x += piece.spin.x * dt;
piece.mesh.rotation.y += piece.spin.y * dt;
piece.mesh.rotation.z += piece.spin.z * dt;
if (piece.mesh.position.y < -0.15 || piece.life > 4.5) {
scene.remove(piece.mesh);
confetti.splice(i, 1);
}
}
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
updateUI();
const animationClock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const dt = Math.min(animationClock.getDelta(), 0.05);
const elapsed = animationClock.elapsedTime;
supplyBlock.position.y = 0.91 + Math.sin(elapsed * 2.2) * 0.025;
supplyBlock.rotation.y = Math.sin(elapsed * 1.4) * 0.06;
updateScale(dt);
updateConfetti(dt);
renderer.render(scene, camera);
});
</script>
</body>
</html>
```