Prompt specification
Build or customize the Three.js 3D scene "Same mass different item counts" 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: Same mass different item counts
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/same-mass-different-item-counts-x0558
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows an animation of small and large produce models filling separate baskets until a symbolic mass target is reached; display differing counts while the target indicator remains equal. Voxel style.
Working Three.js Starter (same-mass-different-item-counts-x0558):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Voxel Harvest — Equal Mass, Different Counts</title>
<style>
:root {
--ink: #18302a;
--muted: #69756f;
--cream: #f3eee3;
--paper: rgba(255, 252, 244, 0.88);
--coral: #ef654c;
--green: #4f8b57;
--gold: #d39a43;
--line: rgba(24, 48, 42, 0.16);
--shadow: 8px 8px 0 rgba(24, 48, 42, 0.09);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--cream);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#world {
position: fixed;
inset: 0;
}
#world canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.grain {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
opacity: 0.22;
background-image:
radial-gradient(rgba(24, 48, 42, 0.12) 0.65px, transparent 0.65px),
radial-gradient(rgba(255, 255, 255, 0.7) 0.65px, transparent 0.65px);
background-position: 0 0, 4px 4px;
background-size: 8px 8px;
mix-blend-mode: multiply;
}
.hero {
position: fixed;
z-index: 5;
top: 30px;
left: 32px;
width: min(430px, calc(100vw - 64px));
pointer-events: none;
animation: reveal 700ms ease-out both;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 13px;
color: var(--muted);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.pixel-mark {
display: grid;
grid-template-columns: repeat(3, 5px);
grid-template-rows: repeat(3, 5px);
gap: 2px;
transform: rotate(45deg);
}
.pixel-mark i {
display: block;
background: var(--ink);
}
.pixel-mark i:nth-child(1),
.pixel-mark i:nth-child(5),
.pixel-mark i:nth-child(9) {
opacity: 0.25;
}
h1 {
margin: 0;
font-size: clamp(34px, 4.1vw, 62px);
line-height: 0.98;
letter-spacing: -0.055em;
}
h1 em {
color: var(--coral);
font-style: normal;
}
.hero p {
max-width: 365px;
margin: 17px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.target-lock {
position: fixed;
z-index: 6;
top: 30px;
left: 50%;
display: flex;
align-items: center;
gap: 16px;
min-width: 215px;
padding: 13px 18px;
border: 2px solid var(--ink);
background: var(--ink);
color: #fffaf0;
box-shadow: 5px 5px 0 rgba(239, 101, 76, 0.45);
transform: translateX(-50%);
transition: box-shadow 250ms ease, transform 250ms ease;
animation: reveal 700ms 100ms ease-out both;
}
.target-lock.reached {
box-shadow: 5px 5px 0 var(--green);
}
.scale-icon {
position: relative;
width: 31px;
height: 31px;
flex: 0 0 auto;
}
.scale-icon::before {
content: "";
position: absolute;
top: 6px;
left: 2px;
width: 27px;
height: 3px;
background: currentColor;
}
.scale-icon::after {
content: "";
position: absolute;
top: 8px;
left: 13px;
width: 4px;
height: 19px;
background: currentColor;
box-shadow: -11px 13px 0 -1px currentColor, 11px 13px 0 -1px currentColor;
}
.target-copy span {
display: block;
margin-bottom: 2px;
color: #bbc9c3;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.target-copy strong {
font-size: 22px;
line-height: 1;
letter-spacing: -0.04em;
}
.target-copy small {
font-size: 11px;
letter-spacing: 0.06em;
}
.controls {
position: fixed;
z-index: 6;
top: 30px;
right: 32px;
display: flex;
flex-direction: column;
align-items: flex-end;
gap: 10px;
animation: reveal 700ms 180ms ease-out both;
}
#replay {
display: inline-flex;
align-items: center;
gap: 10px;
border: 2px solid var(--ink);
padding: 12px 16px;
background: #fffaf0;
color: var(--ink);
box-shadow: 4px 4px 0 var(--ink);
cursor: pointer;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
transition: transform 120ms ease, box-shadow 120ms ease, background 120ms ease;
}
#replay:hover {
background: #fffdf7;
transform: translate(-2px, -2px);
box-shadow: 6px 6px 0 var(--ink);
}
#replay:active {
transform: translate(2px, 2px);
box-shadow: 2px 2px 0 var(--ink);
}
.replay-icon {
font-size: 17px;
line-height: 1;
}
.gesture-tip {
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.basket-card {
position: fixed;
z-index: 5;
bottom: 28px;
width: min(340px, calc(50vw - 42px));
padding: 18px;
border: 2px solid var(--ink);
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
animation: reveal 700ms 260ms ease-out both;
}
#smallCard {
left: 28px;
}
#largeCard {
right: 28px;
animation-delay: 340ms;
}
.card-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.card-index {
color: var(--muted);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.target-pill {
border: 1px solid var(--line);
padding: 5px 7px;
color: var(--ink);
background: rgba(255, 255, 255, 0.55);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.12em;
white-space: nowrap;
}
.produce-name {
display: flex;
align-items: center;
gap: 10px;
margin: 9px 0 13px;
}
.produce-swatch {
width: 16px;
height: 16px;
border: 2px solid var(--ink);
box-shadow: 3px 3px 0 var(--ink);
}
#smallCard .produce-swatch {
background: var(--coral);
}
#largeCard .produce-swatch {
background: var(--green);
}
.produce-name h2 {
margin: 0;
font-size: 19px;
letter-spacing: -0.035em;
}
.stats {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 12px;
margin-bottom: 14px;
}
.count {
display: flex;
align-items: baseline;
gap: 7px;
}
.count strong {
font-size: 38px;
line-height: 0.9;
letter-spacing: -0.065em;
}
.count span {
color: var(--muted);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.unit-mass {
padding: 6px 8px;
background: rgba(24, 48, 42, 0.06);
font-size: 11px;
font-weight: 800;
white-space: nowrap;
}
.mass-label {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 7px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.mass-label strong {
color: var(--ink);
font-size: 11px;
}
.track {
position: relative;
height: 12px;
border: 2px solid var(--ink);
background: rgba(24, 48, 42, 0.08);
overflow: hidden;
}
.track-fill {
display: block;
width: 0;
height: 100%;
transition: width 700ms cubic-bezier(0.22, 0.8, 0.28, 1);
}
#smallBar {
background: var(--coral);
}
#largeBar {
background: var(--green);
}
.target-tick {
position: absolute;
z-index: 2;
top: -2px;
right: -2px;
width: 3px;
height: 16px;
background: var(--ink);
}
.status {
position: fixed;
z-index: 5;
bottom: 38px;
left: 50%;
display: flex;
align-items: center;
gap: 9px;
padding: 9px 12px;
border: 1px solid rgba(24, 48, 42, 0.18);
background: rgba(255, 252, 244, 0.76);
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
transform: translateX(-50%);
backdrop-filter: blur(8px);
animation: reveal 700ms 400ms ease-out both;
}
.status-dot {
width: 7px;
height: 7px;
background: var(--gold);
box-shadow: 0 0 0 0 rgba(211, 154, 67, 0.5);
animation: pulse 1.4s infinite;
}
.status.reached .status-dot {
background: var(--green);
animation: none;
}
.count.pop {
animation: count-pop 360ms cubic-bezier(0.2, 0.8, 0.2, 1);
}
@keyframes reveal {
from {
opacity: 0;
transform: translateY(14px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes count-pop {
0% { transform: scale(1); }
45% { transform: scale(1.22); color: var(--coral); }
100% { transform: scale(1); }
}
@keyframes pulse {
0% { box-shadow: 0 0 0 0 rgba(211, 154, 67, 0.5); }
70% { box-shadow: 0 0 0 7px rgba(211, 154, 67, 0); }
100% { box-shadow: 0 0 0 0 rgba(211, 154, 67, 0); }
}
@media (max-width: 980px) {
.hero {
top: 22px;
left: 22px;
}
.hero p {
display: none;
}
.target-lock {
top: 22px;
}
.controls {
top: 22px;
right: 22px;
}
.gesture-tip {
display: none;
}
.basket-card {
bottom: 18px;
width: min(310px, calc(50vw - 32px));
padding: 14px;
}
#smallCard {
left: 16px;
}
#largeCard {
right: 16px;
}
.status {
bottom: 25px;
}
}
@media (max-width: 680px) {
.hero {
top: 18px;
left: 18px;
width: calc(100vw - 36px);
}
.eyebrow {
margin-bottom: 8px;
font-size: 9px;
}
h1 {
max-width: 300px;
font-size: 34px;
}
.target-lock {
top: 120px;
left: 18px;
min-width: 0;
padding: 9px 12px;
transform: none;
}
.target-copy span {
font-size: 8px;
}
.target-copy strong {
font-size: 17px;
}
.scale-icon {
width: 25px;
height: 25px;
transform: scale(0.78);
}
.controls {
top: 18px;
right: 18px;
}
#replay {
padding: 9px 11px;
font-size: 0;
}
#replay .replay-icon {
font-size: 18px;
}
.basket-card {
bottom: 12px;
width: calc(50vw - 20px);
padding: 11px;
}
#smallCard {
left: 10px;
}
#largeCard {
right: 10px;
}
.card-top {
align-items: flex-start;
flex-direction: column;
gap: 5px;
}
.produce-name {
margin: 8px 0 9px;
}
.produce-name h2 {
font-size: 15px;
}
.produce-swatch {
width: 13px;
height: 13px;
}
.count strong {
font-size: 30px;
}
.count span {
font-size: 9px;
}
.unit-mass {
padding: 5px;
font-size: 9px;
}
.mass-label {
font-size: 8px;
}
.mass-label > span {
display: none;
}
.status {
display: none;
}
}
</style>
</head>
<body>
<div id="world" aria-label="Interactive 3D voxel animation of produce filling two baskets"></div>
<div class="grain"></div>
<header class="hero">
<div class="eyebrow">
<span class="pixel-mark" aria-hidden="true">
<i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i>
</span>
Voxel harvest / mass study 01
</div>
<h1>Same target.<br><em>Different counts.</em></h1>
<p>
One basket takes many light pieces. The other takes fewer heavy pieces.
Both finish at exactly 12 symbolic mass units.
</p>
</header>
<div class="target-lock" id="targetLock">
<div class="scale-icon" aria-hidden="true"></div>
<div class="target-copy">
<span>Shared mass target</span>
<strong>12 <small>MU</small></strong>
</div>
</div>
<div class="controls">
<button id="replay" type="button" aria-label="Replay the basket filling animation">
<span class="replay-icon" aria-hidden="true">↻</span>
Replay fill
</button>
<div class="gesture-tip">Drag to orbit · Scroll to zoom · Double-click to reset</div>
</div>
<section class="basket-card" id="smallCard" aria-label="Small produce basket statistics">
<div class="card-top">
<span class="card-index">01 / Small produce</span>
<span class="target-pill">TARGET 12 MU</span>
</div>
<div class="produce-name">
<span class="produce-swatch" aria-hidden="true"></span>
<h2>Voxel apples</h2>
</div>
<div class="stats">
<div class="count">
<strong id="smallCount" aria-live="polite">0</strong>
<span>items</span>
</div>
<div class="unit-mass">× 1 MU each</div>
</div>
<div class="mass-label">
<span>Accumulated mass</span>
<strong><span id="smallMass">0</span> / 12 MU</strong>
</div>
<div class="track" aria-hidden="true">
<i class="track-fill" id="smallBar"></i>
<b class="target-tick"></b>
</div>
</section>
<section class="basket-card" id="largeCard" aria-label="Large produce basket statistics">
<div class="card-top">
<span class="card-index">02 / Large produce</span>
<span class="target-pill">TARGET 12 MU</span>
</div>
<div class="produce-name">
<span class="produce-swatch" aria-hidden="true"></span>
<h2>Voxel melons</h2>
</div>
<div class="stats">
<div class="count">
<strong id="largeCount" aria-live="polite">0</strong>
<span>items</span>
</div>
<div class="unit-mass">× 4 MU each</div>
</div>
<div class="mass-label">
<span>Accumulated mass</span>
<strong><span id="largeMass">0</span> / 12 MU</strong>
</div>
<div class="track" aria-hidden="true">
<i class="track-fill" id="largeBar"></i>
<b class="target-tick"></b>
</div>
</section>
<div class="status" id="status" aria-live="polite">
<span class="status-dot"></span>
<span id="statusText">Preparing fill…</span>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------------
// Renderer and scene
// ---------------------------------------------------------------------------
const world = document.querySelector("#world");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.domElement.setAttribute("aria-label", "Voxel baskets filling to an equal symbolic mass target");
world.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf3eee3);
scene.fog = new THREE.Fog(0xf3eee3, 24, 48);
const camera = new THREE.OrthographicCamera(-10, 10, 6, -6, 0.1, 100);
const hemisphere = new THREE.HemisphereLight(0xfff8e8, 0x718278, 2.35);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff2d6, 3.15);
sun.position.set(-7, 13, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -13;
sun.shadow.camera.right = 13;
sun.shadow.camera.top = 13;
sun.shadow.camera.bottom = -13;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 36;
sun.shadow.bias = -0.0008;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0xb9d9d0, 1.15);
rimLight.position.set(9, 7, -8);
scene.add(rimLight);
// ---------------------------------------------------------------------------
// Materials and voxel helpers
// ---------------------------------------------------------------------------
const material = (color, extra = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0,
flatShading: true,
...extra
});
const mats = {
ground: material(0xe8e0d1),
grid: material(0xd8cebd),
platform: material(0xd4c5aa),
platformTop: material(0xfff9ec),
wood: material(0xc78b4e),
woodLight: material(0xdba66a),
woodDark: material(0x9b6337),
woodInside: material(0x845532),
apple: material(0xe85b43),
appleLight: material(0xf37b5d),
appleDark: material(0xb73e32),
appleHighlight: material(0xffc19b),
leaf: material(0x4f8b57),
stem: material(0x6b4630),
melon: material(0x4f9257),
melonLight: material(0x68a866),
melonDark: material(0x275f3f),
melonStripe: material(0x8fbc70),
ink: material(0x24423a)
};
function box(parent, size, position, mat, castShadow = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(...size), mat);
mesh.position.set(...position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addGround() {
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(70, 70),
mats.ground
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.02;
floor.receiveShadow = true;
scene.add(floor);
// Pixel-grid floor markings.
for (let x = -14; x <= 14; x += 2) {
box(scene, [0.018, 0.012, 22], [x, 0.002, -1], mats.grid, false);
}
for (let z = -12; z <= 10; z += 2) {
box(scene, [28, 0.012, 0.018], [0, 0.004, z], mats.grid, false);
}
}
addGround();
// ---------------------------------------------------------------------------
// Baskets
// ---------------------------------------------------------------------------
function createBasket(x, accentMat, outsideSign) {
const root = new THREE.Group();
root.position.x = x;
scene.add(root);
// Layered voxel platform.
box(root, [6.15, 0.34, 4.65], [0, 0.17, 0], mats.platform);
box(root, [5.72, 0.15, 4.18], [0, 0.415, 0], mats.platformTop);
// Colored corner blocks make the two stations distinct.
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
box(
root,
[0.28, 0.18, 0.28],
[sx * 2.63, 0.56, sz * 1.84],
accentMat
);
}
}
const basket = new THREE.Group();
root.add(basket);
// Basket base and inner floor.
box(basket, [5.0, 0.18, 3.5], [0, 0.65, 0], mats.woodDark);
box(basket, [4.55, 0.075, 3.08], [0, 0.77, 0], mats.woodInside);
// Woven horizontal rails.
const railYs = [0.98, 1.37, 1.76];
railYs.forEach((y, railIndex) => {
const railMat = railIndex % 2 === 0 ? mats.woodLight : mats.wood;
box(basket, [4.92, 0.19, 0.16], [0, y, -1.71], railMat);
box(basket, [4.92, 0.19, 0.16], [0, y, 1.71], railMat);
box(basket, [0.16, 0.19, 3.35], [-2.43, y, 0], railMat);
box(basket, [0.16, 0.19, 3.35], [2.43, y, 0], railMat);
});
// Vertical slats.
[-2.1, -1.4, -0.7, 0, 0.7, 1.4, 2.1].forEach((sx, index) => {
const slatMat = index % 2 === 0 ? mats.wood : mats.woodLight;
box(basket, [0.14, 1.3, 0.14], [sx, 1.45, -1.72], slatMat);
box(basket, [0.14, 1.3, 0.14], [sx, 1.45, 1.72], slatMat);
});
[-1.15, -0.38, 0.38, 1.15].forEach((sz, index) => {
const slatMat = index % 2 === 0 ? mats.woodLight : mats.wood;
box(basket, [0.14, 1.3, 0.14], [-2.43, 1.45, sz], slatMat);
box(basket, [0.14, 1.3, 0.14], [2.43, 1.45, sz], slatMat);
});
// Thick top rim.
box(basket, [5.08, 0.18, 0.2], [0, 2.12, -1.73], mats.woodDark);
box(basket, [5.08, 0.18, 0.2], [0, 2.12, 1.73], mats.woodDark);
box(basket, [0.2, 0.18, 3.52], [-2.47, 2.12, 0], mats.woodDark);
box(basket, [0.2, 0.18, 3.52], [2.47, 2.12, 0], mats.woodDark);
// Blocky handles at both short ends.
for (const side of [-1, 1]) {
box(basket, [0.19, 0.85, 0.18], [side * 2.56, 2.43, -1.03], mats.woodDark);
box(basket, [0.19, 0.85, 0.18], [side * 2.56, 2.43, 1.03], mats.woodDark);
box(basket, [0.19, 0.18, 2.25], [side * 2.56, 2.84, 0], mats.woodDark);
}
// Small colored identification blocks on the basket's front lip.
[-0.9, 0, 0.9].forEach((px) => {
box(basket, [0.34, 0.16, 0.08], [px, 0.67, 1.8], accentMat);
});
// A little unit sample beside each basket.
const sampleX = outsideSign * 2.92;
const sampleZ = 1.05;
box(root, [0.72, 0.26, 0.72], [sampleX, 0.13, sampleZ], mats.platform);
if (outsideSign < 0) {
box(root, [0.28, 0.28, 0.28], [sampleX, 0.4, sampleZ], mats.apple);
} else {
box(root, [0.68, 0.68, 0.68], [sampleX, 0.6, sampleZ], mats.melon);
}
return basket;
}
const coralMat = material(0xef654c);
const greenMat = material(0x4f8b57);
const smallBasket = createBasket(-4.15, coralMat, -1);
const largeBasket = createBasket(4.15, greenMat, 1);
// ---------------------------------------------------------------------------
// Produce models
// ---------------------------------------------------------------------------
function createApple() {
const group = new THREE.Group();
box(group, [0.46, 0.44, 0.46], [0, 0, 0], mats.apple);
box(group, [0.25, 0.39, 0.39], [-0.25, 0, 0], mats.appleLight);
box(group, [0.25, 0.39, 0.39], [0.25, 0, 0], mats.apple);
box(group, [0.38, 0.18, 0.38], [0, -0.24, 0], mats.appleDark);
box(group, [0.33, 0.17, 0.33], [0, 0.25, 0], mats.appleLight);
box(group, [0.11, 0.19, 0.11], [0.02, 0.42, 0], mats.stem);
const leaf = box(
group,
[0.23, 0.065, 0.12],
[0.14, 0.47, 0.01],
mats.leaf
);
leaf.rotation.z = -0.45;
box(group, [0.09, 0.09, 0.045], [-0.15, 0.15, 0.235], mats.appleHighlight);
return group;
}
function createMelon() {
const group = new THREE.Group();
box(group, [1.16, 0.34, 1.16], [0, -0.48, 0], mats.melonDark);
box(group, [1.48, 0.82, 1.48], [0, 0, 0], mats.melon);
box(group, [1.18, 0.3, 1.18], [0, 0.55, 0], mats.melonLight);
box(group, [0.72, 0.19, 0.72], [0, 0.79, 0], mats.melon);
// Front and back voxel stripes.
[-0.49, 0, 0.49].forEach((x) => {
box(group, [0.11, 0.68, 0.025], [x, 0.02, 0.751], mats.melonStripe);
box(group, [0.11, 0.68, 0.025], [x, 0.02, -0.751], mats.melonStripe);
});
// Side voxel stripes.
[-0.48, 0, 0.48].forEach((z) => {
box(group, [0.025, 0.68, 0.11], [0.751, 0.02, z], mats.melonStripe);
box(group, [0.025, 0.68, 0.11], [-0.751, 0.02, z], mats.melonStripe);
});
box(group, [0.17, 0.27, 0.17], [0, 1, 0], mats.stem);
box(group, [0.13, 0.09, 0.35], [0.11, 0.98, 0], mats.leaf);
return group;
}
const items = [];
function registerItem(parent, model, finalPosition, stage, offset = 0) {
const group = model;
group.visible = false;
group.position.copy(finalPosition);
parent.add(group);
const seed = Math.random();
items.push({
group,
finalPosition: finalPosition.clone(),
startPosition: finalPosition.clone().add(
new THREE.Vector3(
(seed - 0.5) * 0.8,
3.1 + seed * 0.5,
(seed - 0.5) * 0.6
)
),
startRotation: new THREE.Euler(
(seed - 0.5) * 0.7,
(seed - 0.5) * Math.PI,
(seed - 0.5) * 0.5
),
targetRotation: new THREE.Euler(
0,
(seed - 0.5) * 0.65,
0
),
stage,
offset,
startTime: 0,
duration: 0.88
});
}
// Four light apples per stage.
const smallPositions = [
[
[-1.18, 1.07, -0.64],
[1.18, 1.07, -0.64],
[-1.18, 1.07, 0.64],
[1.18, 1.07, 0.64]
],
[
[-0.66, 1.55, -0.43],
[0.66, 1.55, -0.43],
[-0.66, 1.55, 0.43],
[0.66, 1.55, 0.43]
],
[
[-1.48, 2.03, 0],
[-0.49, 2.03, 0],
[0.49, 2.03, 0],
[1.48, 2.03, 0]
]
];
smallPositions.forEach((positions, stage) => {
positions.forEach((position, index) => {
registerItem(
smallBasket,
createApple(),
new THREE.Vector3(...position),
stage,
index * 0.075
);
});
});
// One heavy melon per stage.
const largePositions = [
[-1.05, 1.58, 0],
[1.05, 1.58, 0],
[0, 3.05, 0]
];
largePositions.forEach((position, stage) => {
registerItem(
largeBasket,
createMelon(),
new THREE.Vector3(...position),
stage,
stage * 0.02
);
});
// ---------------------------------------------------------------------------
// Interface state and animation timeline
// ---------------------------------------------------------------------------
const smallCount = document.querySelector("#smallCount");
const largeCount = document.querySelector("#largeCount");
const smallMass = document.querySelector("#smallMass");
const largeMass = document.querySelector("#largeMass");
const smallBar = document.querySelector("#smallBar");
const largeBar = document.querySelector("#largeBar");
const status = document.querySelector("#status");
const statusText = document.querySelector("#statusText");
const targetLock = document.querySelector("#targetLock");
const replayButton = document.querySelector("#replay");
const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
const timeScale = reducedMotion ? 0.35 : 1;
const stageStarts = [0.45, 1.5, 2.55].map((time) => time * timeScale);
const itemDuration = 0.88 * timeScale;
let animationStart = performance.now();
let currentStage = -1;
let completed = false;
function popNumber(element) {
element.classList.remove("pop");
void element.offsetWidth;
element.classList.add("pop");
}
function setProgress(element, percentage) {
element.style.width = `${percentage}%`;
}
function activateStage(stage) {
const mass = (stage + 1) * 4;
smallCount.textContent = (stage + 1) * 4;
largeCount.textContent = stage + 1;
smallMass.textContent = mass;
largeMass.textContent = mass;
setProgress(smallBar, mass / 12 * 100);
setProgress(largeBar, mass / 12 * 100);
popNumber(smallCount);
popNumber(largeCount);
status.classList.remove("reached");
statusText.textContent = `Filling baskets · ${mass} / 12 MU each`;
}
function resetAnimation() {
animationStart = performance.now();
currentStage = -1;
completed = false;
items.forEach((item) => {
item.group.visible = false;
item.group.position.copy(item.finalPosition);
item.group.scale.setScalar(0.001);
});
smallCount.textContent = "0";
largeCount.textContent = "0";
smallMass.textContent = "0";
largeMass.textContent = "0";
smallBar.style.transition = "none";
largeBar.style.transition = "none";
setProgress(smallBar, 0);
setProgress(largeBar, 0);
void smallBar.offsetWidth;
smallBar.style.transition = "";
largeBar.style.transition = "";
status.classList.remove("reached");
targetLock.classList.remove("reached");
statusText.textContent = "Preparing fill…";
}
replayButton.addEventListener("click", resetAnimation);
// ---------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, keyboard, and double-click reset
// ---------------------------------------------------------------------------
const controls = {
target: new THREE.Vector3(0, 1.2, 0),
yaw: 0.23,
polar: 1.04,
radius: 21,
dragging: false,
pointerX: 0,
pointerY: 0
};
function resetCamera() {
controls.yaw = 0.23;
controls.polar = 1.04;
controls.radius = 21;
camera.zoom = 1;
camera.updateProjectionMatrix();
}
function updateCamera() {
const sinPolar = Math.sin(controls.polar);
camera.position.set(
controls.target.x + controls.radius * sinPolar * Math.sin(controls.yaw),
controls.target.y + controls.radius * Math.cos(controls.polar),
controls.target.z + controls.radius * sinPolar * Math.cos(controls.yaw)
);
camera.lookAt(controls.target);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
controls.dragging = true;
controls.pointerX = event.clientX;
controls.pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!controls.dragging) return;
const dx = event.clientX - controls.pointerX;
const dy = event.clientY - controls.pointerY;
controls.pointerX = event.clientX;
controls.pointerY = event.clientY;
controls.yaw -= dx * 0.006;
controls.polar = THREE.MathUtils.clamp(
controls.polar - dy * 0.005,
0.62,
1.36
);
});
function endDrag(event) {
controls.dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
const factor = event.deltaY > 0 ? 0.9 : 1.1;
camera.zoom = THREE.MathUtils.clamp(camera.zoom * factor, 0.72, 1.7);
camera.updateProjectionMatrix();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") controls.yaw += 0.08;
if (event.key === "ArrowRight") controls.yaw -= 0.08;
if (event.key === "ArrowUp") controls.polar = Math.max(0.62, controls.polar - 0.06);
if (event.key === "ArrowDown") controls.polar = Math.min(1.36, controls.polar + 0.06);
if (event.key === "+" || event.key === "=") camera.zoom = Math.min(1.7, camera.zoom * 1.08);
if (event.key === "-") camera.zoom = Math.max(0.72, camera.zoom / 1.08);
if (event.key.toLowerCase() === "r") resetCamera();
camera.updateProjectionMatrix();
});
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
let viewHeight = 10.6;
const minimumWidth = 16.2;
if (viewHeight * aspect < minimumWidth) {
viewHeight = minimumWidth / aspect;
}
camera.left = -viewHeight * aspect / 2;
camera.right = viewHeight * aspect / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
updateCamera();
// ---------------------------------------------------------------------------
// Main animation loop
// ---------------------------------------------------------------------------
const easeOutCubic = (t) => 1 - Math.pow(1 - t, 3);
const easeOutBack = (t) => {
const c1 = 1.70158;
const c3 = c1 + 1;
return 1 + c3 * Math.pow(t - 1, 3) + c1 * Math.pow(t - 1, 2);
};
function animate(now) {
requestAnimationFrame(animate);
const elapsed = (now - animationStart) / 1000;
while (
currentStage < stageStarts.length - 1 &&
elapsed >= stageStarts[currentStage + 1]
) {
currentStage++;
activateStage(currentStage);
}
items.forEach((item) => {
const itemStart = stageStarts[item.stage] + item.offset;
item.startTime = itemStart;
item.duration = itemDuration;
if (elapsed < itemStart) return;
const progress = THREE.MathUtils.clamp(
(elapsed - itemStart) / item.duration,
0,
1
);
const eased = easeOutCubic(progress);
const scale = progress === 1 ? 1 : Math.max(0.001, easeOutBack(progress));
item.group.visible = true;
item.group.position.set(
THREE.MathUtils.lerp(item.startPosition.x, item.finalPosition.x, eased),
THREE.MathUtils.lerp(item.startPosition.y, item.finalPosition.y, eased)
+ Math.sin(progress * Math.PI) * 1.25,
THREE.MathUtils.lerp(item.startPosition.z, item.finalPosition.z, eased)
);
item.group.scale.setScalar(scale);
item.group.rotation.set(
THREE.MathUtils.lerp(item.startRotation.x, item.targetRotation.x, eased),
THREE.MathUtils.lerp(item.startRotation.y, item.targetRotation.y, eased),
THREE.MathUtils.lerp(item.startRotation.z, item.targetRotation.z, eased)
);
if (progress === 1) {
item.group.position.copy(item.finalPosition);
item.group.scale.setScalar(1);
}
});
const finalLanding = stageStarts[2] + 0.3 + itemDuration;
if (!completed && elapsed >= finalLanding) {
completed = true;
status.classList.add("reached");
targetLock.classList.add("reached");
statusText.textContent = "Target reached · 12 MU in both baskets";
}
updateCamera();
renderer.render(scene, camera);
}
resetAnimation();
requestAnimationFrame(animate);
</script>
</body>
</html>
```