Prompt specification
Build or customize the Three.js 3D scene "Harvest crate capacity markers" 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: Harvest crate capacity markers
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/harvest-crate-capacity-markers-x1073
Scene Description Prompt:
Make a stylized Three.js scene in a self-contained HTML file using cdnjs: harvest crate capacity markers. Show several crates with fixed slots and a row of fruit tokens; make crate capacity visible through geometry so full and partly filled states are countable.
Working Three.js Starter (harvest-crate-capacity-markers-x1073):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Harvest Crate Capacity</title>
<style>
:root {
--ink: #273126;
--cream: #fff8e8;
--panel: rgba(255, 250, 237, 0.9);
--line: rgba(55, 66, 47, 0.16);
--green: #617a4c;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dfe7d6;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
user-select: none;
}
canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.brand {
position: fixed;
z-index: 5;
top: 25px;
left: 28px;
max-width: 510px;
pointer-events: none;
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.45);
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 9px;
margin-bottom: 10px;
color: #526540;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 25px;
height: 2px;
border-radius: 2px;
background: #d18a3a;
}
h1 {
margin: 0;
max-width: 500px;
font-size: clamp(30px, 4vw, 54px);
line-height: 0.98;
letter-spacing: -0.055em;
font-weight: 850;
}
.subtitle {
max-width: 455px;
margin: 13px 0 0;
color: rgba(39, 49, 38, 0.72);
font-size: 14px;
line-height: 1.5;
}
.ledger {
position: fixed;
z-index: 6;
top: 24px;
right: 24px;
width: 254px;
padding: 15px;
border: 1px solid rgba(255, 255, 255, 0.62);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 18px 45px rgba(54, 59, 40, 0.13),
inset 0 1px 0 rgba(255, 255, 255, 0.7);
backdrop-filter: blur(13px);
}
.ledger-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
}
.ledger-title {
font-size: 10px;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.total {
color: #71805d;
font-size: 11px;
font-weight: 750;
}
.crate-card {
--accent: #d18a3a;
width: 100%;
display: grid;
grid-template-columns: 34px 1fr auto;
gap: 10px;
align-items: center;
margin-top: 7px;
padding: 10px;
border: 1px solid transparent;
border-radius: 13px;
color: var(--ink);
background: rgba(255, 255, 255, 0.36);
font: inherit;
text-align: left;
cursor: pointer;
transition:
transform 160ms ease,
border-color 160ms ease,
background 160ms ease;
}
.crate-card:hover {
transform: translateY(-1px);
background: rgba(255, 255, 255, 0.68);
}
.crate-card.selected {
border-color: color-mix(in srgb, var(--accent) 55%, transparent);
background: rgba(255, 255, 255, 0.86);
box-shadow: 0 5px 15px rgba(64, 68, 49, 0.08);
}
.crate-letter {
width: 32px;
height: 32px;
display: grid;
place-items: center;
border-radius: 10px;
color: #fffaf0;
background: var(--accent);
font-size: 14px;
font-weight: 850;
box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.12);
}
.crate-copy {
min-width: 0;
}
.crate-copy strong {
display: block;
font-size: 14px;
font-variant-numeric: tabular-nums;
}
.crate-copy small {
display: block;
margin-top: 1px;
color: #7a816f;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
}
.mini-grid {
display: grid;
grid-template-columns: repeat(6, 7px);
gap: 3px;
}
.mini-grid i {
width: 7px;
height: 7px;
border-radius: 50%;
background: rgba(48, 57, 42, 0.12);
box-shadow: inset 0 0 0 1px rgba(48, 57, 42, 0.08);
transition: background 180ms ease;
}
.mini-grid i.on {
background: var(--accent);
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}
.guide {
position: fixed;
z-index: 5;
left: 26px;
bottom: 23px;
max-width: 520px;
padding: 12px 15px;
border: 1px solid rgba(255, 255, 255, 0.52);
border-radius: 14px;
background: rgba(255, 250, 237, 0.76);
box-shadow: 0 10px 30px rgba(52, 62, 42, 0.1);
backdrop-filter: blur(10px);
pointer-events: none;
color: rgba(39, 49, 38, 0.76);
font-size: 11px;
line-height: 1.55;
}
.guide strong {
color: var(--ink);
}
.toolbar {
position: fixed;
z-index: 7;
right: 24px;
bottom: 23px;
display: flex;
gap: 8px;
}
.toolbar button {
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.62);
border-radius: 11px;
color: var(--ink);
background: rgba(255, 250, 237, 0.85);
box-shadow: 0 8px 24px rgba(52, 62, 42, 0.09);
font: 750 11px/1 ui-sans-serif, system-ui, sans-serif;
cursor: pointer;
backdrop-filter: blur(10px);
}
.toolbar button:hover {
background: #fffaf0;
transform: translateY(-1px);
}
.toast {
position: fixed;
z-index: 10;
left: 50%;
bottom: 87px;
max-width: calc(100vw - 30px);
padding: 10px 15px;
border-radius: 999px;
color: #fffaf0;
background: rgba(39, 49, 38, 0.9);
box-shadow: 0 12px 34px rgba(39, 49, 38, 0.2);
font-size: 11px;
font-weight: 750;
text-align: center;
opacity: 0;
pointer-events: none;
transform: translate(-50%, 12px);
transition:
opacity 180ms ease,
transform 180ms ease;
}
.toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 760px) {
.brand {
top: 14px;
left: 14px;
max-width: calc(100vw - 210px);
}
h1 {
font-size: 29px;
}
.subtitle {
display: none;
}
.eyebrow {
font-size: 9px;
}
.ledger {
top: 12px;
right: 12px;
width: 184px;
padding: 10px;
}
.crate-card {
grid-template-columns: 27px 1fr;
gap: 7px;
padding: 7px;
}
.crate-letter {
width: 27px;
height: 27px;
}
.mini-grid {
display: none;
}
.guide {
left: 12px;
bottom: 12px;
max-width: calc(100vw - 138px);
padding: 9px 11px;
font-size: 9px;
}
.toolbar {
right: 12px;
bottom: 12px;
flex-direction: column;
}
.toolbar button {
padding: 9px 10px;
font-size: 9px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive three-dimensional harvest crate scene"></canvas>
<header class="brand">
<div class="eyebrow">Packing yard / capacity study</div>
<h1>Harvest crate capacity</h1>
<p class="subtitle">
Twelve fixed slots per crate. Count the floor sockets or the raised
capacity pips—no digital gauge required.
</p>
</header>
<aside class="ledger" aria-label="Crate capacity ledger">
<div class="ledger-head">
<span class="ledger-title">Capacity ledger</span>
<span class="total" id="totalPacked">24 / 36 packed</span>
</div>
<div id="ledgerCards"></div>
</aside>
<div class="guide">
<strong>Drag</strong> to orbit · <strong>Scroll</strong> to zoom ·
<strong>1 / 2 / 3</strong> select a crate.<br />
Click a fruit token in the ready row to pack it. Click packed fruit to return it.
</div>
<div class="toolbar">
<button id="frontView" type="button">Front view</button>
<button id="resetDemo" type="button">Reset demo</button>
</div>
<div class="toast" 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";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdfe7d6);
scene.fog = new THREE.Fog(0xdfe7d6, 17, 31);
const camera = new THREE.OrthographicCamera(-7, 7, 4, -4, 0.1, 60);
const cameraTarget = new THREE.Vector3(0, 0.65, 0.18);
const view = {
theta: 0.08,
phi: 0.72,
zoom: 1
};
function updateCamera() {
const radius = 13;
const sinPhi = Math.sin(view.phi);
camera.position.set(
cameraTarget.x + radius * sinPhi * Math.sin(view.theta),
cameraTarget.y + radius * Math.cos(view.phi),
cameraTarget.z + radius * sinPhi * Math.cos(view.theta)
);
camera.lookAt(cameraTarget);
camera.zoom = view.zoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = Math.max(7.8, 12.8 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
renderer.setSize(width, height, false);
updateCamera();
}
window.addEventListener("resize", resize);
resize();
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xfff6dc, 0x71805e, 2.35));
const keyLight = new THREE.DirectionalLight(0xffe7bd, 3.1);
keyLight.position.set(-4.5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0004;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xc7dcff, 1.25);
fillLight.position.set(7, 5, -5);
scene.add(fillLight);
// -------------------------------------------------------------------------
// Shared materials and helpers
// -------------------------------------------------------------------------
const materials = {
ground: new THREE.MeshStandardMaterial({
color: 0xaaba98,
roughness: 1
}),
platform: new THREE.MeshStandardMaterial({
color: 0x76563a,
roughness: 0.9
}),
darkWood: new THREE.MeshStandardMaterial({
color: 0x754126,
roughness: 0.82
}),
wood: new THREE.MeshStandardMaterial({
color: 0xa96332,
roughness: 0.76
}),
lightWood: new THREE.MeshStandardMaterial({
color: 0xd39a5e,
roughness: 0.72
}),
paleWood: new THREE.MeshStandardMaterial({
color: 0xe4b978,
roughness: 0.68
}),
slot: new THREE.MeshStandardMaterial({
color: 0x4d3323,
roughness: 0.95
}),
hole: new THREE.MeshStandardMaterial({
color: 0x38291f,
roughness: 0.9
}),
stem: new THREE.MeshStandardMaterial({
color: 0x664328,
roughness: 0.9
}),
leaf: new THREE.MeshStandardMaterial({
color: 0x668345,
roughness: 0.8
})
};
const fruitMaterials = {
apple: new THREE.MeshStandardMaterial({
color: 0xd94d3d,
roughness: 0.48
}),
orange: new THREE.MeshStandardMaterial({
color: 0xf2a13b,
roughness: 0.56
}),
pear: new THREE.MeshStandardMaterial({
color: 0xb8bd4e,
roughness: 0.58
}),
plum: new THREE.MeshStandardMaterial({
color: 0x75508e,
roughness: 0.5
})
};
const tokenColors = {
apple: 0xd94d3d,
orange: 0xf2a13b,
pear: 0xb8bd4e,
plum: 0x75508e
};
const interactiveMeshes = [];
const crates = [];
const tokens = [];
const motions = [];
let nextTokenId = 1;
function addBox(parent, size, position, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function roundedRectShape(width, height, radius) {
const x = -width / 2;
const y = -height / 2;
const shape = new THREE.Shape();
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
return shape;
}
function makeLabelTexture(text, background = "#3d2a1d", foreground = "#fff0cd") {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 128;
const context = labelCanvas.getContext("2d");
context.fillStyle = background;
context.fillRect(0, 0, labelCanvas.width, labelCanvas.height);
context.strokeStyle = "rgba(255,240,205,0.24)";
context.lineWidth = 4;
context.strokeRect(8, 8, labelCanvas.width - 16, labelCanvas.height - 16);
context.fillStyle = foreground;
context.font = "700 48px ui-sans-serif, system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 256, 66);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
function makeLabelPlane(text, width, height, options = {}) {
const texture = makeLabelTexture(
text,
options.background,
options.foreground
);
return new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
new THREE.MeshBasicMaterial({
map: texture,
toneMapped: false
})
);
}
// -------------------------------------------------------------------------
// Ground, wooden deck, and orchard background
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(36, 36),
materials.ground
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.3;
ground.receiveShadow = true;
scene.add(ground);
const platformGeometry = new THREE.ExtrudeGeometry(
roundedRectShape(12.2, 5.6, 0.36),
{
depth: 0.28,
bevelEnabled: true,
bevelSegments: 2,
steps: 1,
bevelSize: 0.07,
bevelThickness: 0.05
}
);
platformGeometry.rotateX(Math.PI / 2);
const platform = new THREE.Mesh(platformGeometry, materials.platform);
platform.position.set(0, 0.03, 0.15);
platform.receiveShadow = true;
platform.castShadow = true;
scene.add(platform);
const plankMaterials = [
new THREE.MeshStandardMaterial({ color: 0xb9824d, roughness: 0.84 }),
new THREE.MeshStandardMaterial({ color: 0xc28d52, roughness: 0.84 }),
new THREE.MeshStandardMaterial({ color: 0xae7543, roughness: 0.84 })
];
for (let i = 0; i < 11; i++) {
const plank = addBox(
scene,
[11.7, 0.065, 0.39],
[0, 0.065, -2.15 + i * 0.46],
plankMaterials[i % plankMaterials.length]
);
plank.castShadow = false;
}
function createTree(x, z, scale, color) {
const tree = new THREE.Group();
tree.position.set(x, -0.29, z);
tree.scale.setScalar(scale);
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.11, 0.17, 1.35, 7),
materials.darkWood
);
trunk.position.y = 0.64;
trunk.castShadow = true;
tree.add(trunk);
const foliageMaterial = new THREE.MeshStandardMaterial({
color,
roughness: 0.92,
flatShading: true
});
const clusters = [
[-0.35, 1.45, 0.05, 0.72],
[0.35, 1.55, 0.08, 0.76],
[0.02, 1.92, -0.08, 0.66]
];
for (const [cx, cy, cz, radius] of clusters) {
const foliage = new THREE.Mesh(
new THREE.IcosahedronGeometry(radius, 1),
foliageMaterial
);
foliage.position.set(cx, cy, cz);
foliage.rotation.set(cx, cy, cz);
foliage.castShadow = true;
tree.add(foliage);
}
scene.add(tree);
}
createTree(-6.1, -4.1, 1.05, 0x738b58);
createTree(5.9, -4.3, 0.92, 0x80925e);
createTree(-7.0, 1.8, 0.8, 0x7b8e5d);
// -------------------------------------------------------------------------
// Crates: 4 × 3 physical floor slots and 6 × 2 raised capacity pips
// -------------------------------------------------------------------------
const crateDefinitions = [
{ id: "A", x: -3.25, count: 12, accent: "#d18a3a" },
{ id: "B", x: 0, count: 8, accent: "#8a704b" },
{ id: "C", x: 3.25, count: 4, accent: "#758c52" }
];
function createCrate(definition, index) {
const crate = {
index,
id: definition.id,
accent: definition.accent,
slots: [],
pips: [],
group: new THREE.Group()
};
crate.group.position.set(definition.x, 0.08, -0.65);
scene.add(crate.group);
// Feet and lower floor.
for (const x of [-1.08, 1.08]) {
for (const z of [-0.67, 0.67]) {
addBox(
crate.group,
[0.34, 0.22, 0.31],
[x, 0.11, z],
materials.darkWood
);
}
}
addBox(
crate.group,
[2.64, 0.17, 1.84],
[0, 0.295, 0],
materials.darkWood
);
// Five visible floor boards.
for (let i = 0; i < 5; i++) {
addBox(
crate.group,
[2.49, 0.07, 0.30],
[0, 0.397, -0.68 + i * 0.34],
i % 2 ? materials.lightWood : materials.wood
);
}
// Corner posts.
for (const x of [-1.24, 1.24]) {
for (const z of [-0.85, 0.85]) {
addBox(
crate.group,
[0.17, 1.31, 0.17],
[x, 0.92, z],
materials.darkWood
);
}
}
// Back wall slats.
for (const y of [0.63, 0.98, 1.33]) {
addBox(
crate.group,
[2.43, 0.23, 0.13],
[0, y, -0.86],
materials.wood
);
}
// Open slatted side walls.
for (const x of [-1.24, 1.24]) {
for (const z of [-0.62, -0.31, 0, 0.31, 0.62]) {
addBox(
crate.group,
[0.13, 1.02, 0.21],
[x, 0.91, z],
materials.lightWood
);
}
addBox(
crate.group,
[0.14, 0.13, 1.78],
[x, 1.43, 0],
materials.paleWood
);
}
addBox(
crate.group,
[2.56, 0.13, 0.14],
[0, 1.43, -0.86],
materials.paleWood
);
// Low front rail leaves the floor visible from above.
addBox(
crate.group,
[2.43, 0.23, 0.13],
[0, 0.57, 0.87],
materials.wood
);
// Raised capacity board.
addBox(
crate.group,
[2.39, 0.66, 0.12],
[0, 1.29, 0.94],
materials.paleWood
);
const crateLabel = makeLabelPlane(
`CRATE ${definition.id} / 12`,
1.28,
0.25
);
crateLabel.position.set(0, 1.49, 1.008);
crate.group.add(crateLabel);
// Twelve raised sockets, six across and two down.
for (let row = 0; row < 2; row++) {
for (let column = 0; column < 6; column++) {
const x = (column - 2.5) * 0.32;
const y = 1.10 + row * 0.17;
const socket = new THREE.Mesh(
new THREE.CircleGeometry(0.066, 20),
materials.hole
);
socket.position.set(x, y, 1.007);
crate.group.add(socket);
const pip = new THREE.Mesh(
new THREE.SphereGeometry(0.052, 14, 10),
new THREE.MeshStandardMaterial({
color: 0xf2a13b,
roughness: 0.48
})
);
pip.position.set(x, y, 1.037);
pip.scale.z = 0.58;
pip.visible = false;
crate.group.add(pip);
crate.pips.push(pip);
}
}
// Twelve fixed floor sockets, four columns by three rows.
for (let row = 0; row < 3; row++) {
for (let column = 0; column < 4; column++) {
const x = (column - 1.5) * 0.52;
const z = (row - 1) * 0.48;
const disk = new THREE.Mesh(
new THREE.CylinderGeometry(0.195, 0.195, 0.026, 24),
materials.slot
);
disk.position.set(x, 0.452, z);
disk.receiveShadow = true;
disk.userData.crateRef = crate;
disk.userData.slotIndex = crate.slots.length;
crate.group.add(disk);
const rim = new THREE.Mesh(
new THREE.TorusGeometry(0.191, 0.017, 8, 28),
materials.paleWood
);
rim.rotation.x = Math.PI / 2;
rim.position.set(x, 0.469, z);
rim.userData.crateRef = crate;
rim.userData.slotIndex = crate.slots.length;
crate.group.add(rim);
crate.slots.push({
index: crate.slots.length,
occupied: false,
fruit: null,
local: new THREE.Vector3(x, 0.475, z),
world: new THREE.Vector3()
});
}
}
// All crate meshes can select the crate.
crate.group.traverse((object) => {
if (object.isMesh) {
object.userData.crateRef = crate;
interactiveMeshes.push(object);
}
});
crates.push(crate);
}
crateDefinitions.forEach(createCrate);
scene.updateMatrixWorld(true);
for (const crate of crates) {
for (const slot of crate.slots) {
slot.world.copy(crate.group.localToWorld(slot.local.clone()));
}
}
// -------------------------------------------------------------------------
// Stylized fruit tokens
// -------------------------------------------------------------------------
const fruitGeometry = {
apple: new THREE.SphereGeometry(0.17, 20, 14),
orange: new THREE.SphereGeometry(0.17, 20, 14),
plum: new THREE.SphereGeometry(0.165, 18, 14),
pearLower: new THREE.SphereGeometry(0.16, 18, 14),
pearUpper: new THREE.SphereGeometry(0.125, 18, 14),
stem: new THREE.CylinderGeometry(0.018, 0.026, 0.10, 8),
leaf: new THREE.SphereGeometry(0.057, 10, 7)
};
function createToken(type) {
const token = {
id: nextTokenId++,
type,
state: "idle",
crate: null,
slotIndex: -1,
rowIndex: null,
group: new THREE.Group()
};
const group = token.group;
let stemHeight = 0.38;
if (type === "pear") {
const lower = new THREE.Mesh(
fruitGeometry.pearLower,
fruitMaterials.pear
);
lower.position.y = 0.15;
lower.scale.set(0.96, 0.91, 0.96);
group.add(lower);
const upper = new THREE.Mesh(
fruitGeometry.pearUpper,
fruitMaterials.pear
);
upper.position.y = 0.295;
upper.scale.set(0.91, 1.12, 0.91);
group.add(upper);
stemHeight = 0.45;
} else {
const body = new THREE.Mesh(
fruitGeometry[type],
fruitMaterials[type]
);
body.position.y = 0.17;
if (type === "apple") body.scale.set(1.02, 0.91, 1.02);
if (type === "plum") body.scale.set(0.94, 1.06, 0.94);
if (type === "orange") body.scale.set(1, 0.97, 1);
group.add(body);
}
const stem = new THREE.Mesh(fruitGeometry.stem, materials.stem);
stem.position.set(0, stemHeight, 0);
stem.rotation.z = -0.15;
group.add(stem);
const leaf = new THREE.Mesh(fruitGeometry.leaf, materials.leaf);
leaf.position.set(0.055, stemHeight + 0.015, 0.012);
leaf.rotation.z = 0.48;
leaf.scale.set(1.35, 0.25, 0.58);
group.add(leaf);
group.traverse((object) => {
if (object.isMesh) {
object.castShadow = true;
object.receiveShadow = true;
object.userData.tokenRef = token;
interactiveMeshes.push(object);
}
});
tokens.push(token);
return token;
}
function updatePip(crate, slotIndex) {
const slot = crate.slots[slotIndex];
const pip = crate.pips[slotIndex];
if (slot.occupied && slot.fruit) {
pip.visible = true;
pip.material.color.set(tokenColors[slot.fruit.type]);
} else {
pip.visible = false;
}
}
// Initial states: one completely full crate and two partly filled crates.
const initialPatterns = [
["apple", "orange", "plum", "pear"],
["apple", "pear", "orange"],
["orange", "apple", "plum"]
];
crates.forEach((crate, crateIndex) => {
const count = crateDefinitions[crateIndex].count;
const pattern = initialPatterns[crateIndex];
for (let i = 0; i < count; i++) {
const type = pattern[i % pattern.length];
const token = createToken(type);
const slot = crate.slots[i];
token.state = "packed";
token.crate = crate;
token.slotIndex = slot.index;
token.group.position.copy(slot.world);
slot.occupied = true;
slot.fruit = token;
scene.add(token.group);
updatePip(crate, slot.index);
}
});
// -------------------------------------------------------------------------
// Ready row with ten physical sockets; eight begin with fruit
// -------------------------------------------------------------------------
addBox(
scene,
[4.35, 0.12, 0.58],
[0, 0.12, 1.75],
materials.darkWood
);
const rowPositions = [];
for (let i = 0; i < 10; i++) {
const x = -1.8 + i * 0.4;
const socket = new THREE.Mesh(
new THREE.CylinderGeometry(0.19, 0.19, 0.022, 20),
materials.slot
);
socket.position.set(x, 0.192, 1.75);
socket.receiveShadow = true;
scene.add(socket);
rowPositions.push(new THREE.Vector3(x, 0.207, 1.75));
}
const rowTypes = [
"apple",
"orange",
"pear",
"plum",
"apple",
"orange",
"pear",
"plum"
];
rowTypes.forEach((type, index) => {
const token = createToken(type);
token.state = "row";
token.rowIndex = index;
token.group.position.copy(rowPositions[index]);
scene.add(token.group);
});
const readyLabel = makeLabelPlane(
"READY TO PACK",
1.43,
0.29,
{ background: "#536342", foreground: "#fff1cf" }
);
readyLabel.rotation.x = -Math.PI / 2;
readyLabel.position.set(2.83, 0.205, 1.75);
scene.add(readyLabel);
// Small physical arrow pointing toward the ready row.
for (let i = 0; i < 3; i++) {
const arrowPiece = addBox(
scene,
[0.22, 0.035, 0.08],
[2.01 - i * 0.17, 0.21, 1.75],
materials.lightWood
);
arrowPiece.rotation.y = -0.65;
arrowPiece.castShadow = false;
}
// -------------------------------------------------------------------------
// Selection ring and UI
// -------------------------------------------------------------------------
const selectionMaterial = new THREE.MeshStandardMaterial({
color: 0xffd36f,
emissive: 0xc37b1c,
emissiveIntensity: 0.6,
transparent: true,
opacity: 0.78,
roughness: 0.55
});
const selectionRing = new THREE.Mesh(
new THREE.TorusGeometry(1.46, 0.035, 8, 72),
selectionMaterial
);
selectionRing.rotation.x = Math.PI / 2;
selectionRing.scale.set(1.05, 0.68, 1);
selectionRing.visible = false;
scene.add(selectionRing);
let selectedCrateIndex = 1;
const ledgerCards = document.querySelector("#ledgerCards");
const totalPacked = document.querySelector("#totalPacked");
const toast = document.querySelector("#toast");
let toastTimer;
crateDefinitions.forEach((definition, index) => {
const button = document.createElement("button");
button.className = "crate-card";
button.type = "button";
button.style.setProperty("--accent", definition.accent);
button.innerHTML = `
<span class="crate-letter">${definition.id}</span>
<span class="crate-copy">
<strong class="count">0 / 12</strong>
<small class="state">EMPTY</small>
</span>
<span class="mini-grid" aria-hidden="true">
${Array.from({ length: 12 }, () => "<i></i>").join("")}
</span>
`;
button.addEventListener("click", () => selectCrate(index));
ledgerCards.appendChild(button);
});
function occupiedCount(crate) {
return crate.slots.reduce((sum, slot) => sum + Number(slot.occupied), 0);
}
function updateHUD() {
let total = 0;
crates.forEach((crate, index) => {
const count = occupiedCount(crate);
total += count;
const card = ledgerCards.children[index];
card.querySelector(".count").textContent = `${count} / 12`;
card.querySelector(".state").textContent =
count === 12 ? "FULL" : count === 0 ? "EMPTY" : "PARTIAL";
card.querySelectorAll(".mini-grid i").forEach((pip, pipIndex) => {
pip.classList.toggle("on", crate.slots[pipIndex].occupied);
});
card.classList.toggle("selected", index === selectedCrateIndex);
card.setAttribute("aria-pressed", String(index === selectedCrateIndex));
});
totalPacked.textContent = `${total} / 36 packed`;
}
function selectCrate(index) {
selectedCrateIndex = index;
const crate = crates[index];
selectionRing.position.set(
crate.group.position.x,
0.13,
crate.group.position.z
);
selectionRing.visible = true;
updateHUD();
}
function showToast(message) {
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 1900);
}
// -------------------------------------------------------------------------
// Motion and packing interactions
// -------------------------------------------------------------------------
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
function startMotion(object, destination, onComplete, arc = 0.65) {
motions.push({
object,
from: object.position.clone(),
to: destination.clone(),
startRotation: object.rotation.y,
startTime: performance.now(),
duration: reducedMotion ? 1 : 650,
arc: reducedMotion ? 0 : arc,
onComplete
});
}
function rowIsTaken(rowIndex) {
return tokens.some(
(token) =>
token.rowIndex === rowIndex &&
(token.state === "row" || token.state === "movingToRow")
);
}
function placeToken(token) {
const crate = crates[selectedCrateIndex];
const slot = crate.slots.find((candidate) => !candidate.occupied);
if (!slot) {
showToast(`Crate ${crate.id} is full — choose another crate.`);
return;
}
token.state = "movingToCrate";
token.crate = crate;
token.slotIndex = slot.index;
token.rowIndex = null;
slot.occupied = true;
slot.fruit = token;
updatePip(crate, slot.index);
updateHUD();
const typeName = token.type[0].toUpperCase() + token.type.slice(1);
showToast(
`${typeName} packed into Crate ${crate.id} · ${occupiedCount(crate)} / 12`
);
startMotion(token.group, slot.world, () => {
token.state = "packed";
});
}
function removeToken(token) {
const crate = token.crate;
const slot = crate.slots[token.slotIndex];
const freeRowIndex = rowPositions.findIndex(
(_, index) => !rowIsTaken(index)
);
if (freeRowIndex === -1) {
showToast("The ready row is full.");
return;
}
selectCrate(crate.index);
slot.occupied = false;
slot.fruit = null;
token.crate = null;
token.slotIndex = -1;
token.rowIndex = freeRowIndex;
token.state = "movingToRow";
updatePip(crate, slot.index);
updateHUD();
showToast(`Returned to the ready row · Crate ${crate.id} now ${occupiedCount(crate)} / 12`);
startMotion(token.group, rowPositions[freeRowIndex], () => {
token.state = "row";
});
}
// -------------------------------------------------------------------------
// Raycasting and custom camera controls
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function pickAt(clientX, clientY) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
return raycaster.intersectObjects(interactiveMeshes, false)[0] || null;
}
function handlePick(clientX, clientY) {
const hit = pickAt(clientX, clientY);
if (!hit) return;
const token = hit.object.userData.tokenRef;
if (token) {
if (token.state === "row") {
placeToken(token);
} else if (token.state === "packed") {
removeToken(token);
}
return;
}
const crate = hit.object.userData.crateRef;
if (crate) selectCrate(crate.index);
}
let pointerState = {
down: false,
moved: false,
startX: 0,
startY: 0,
lastX: 0,
lastY: 0
};
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointerState = {
down: true,
moved: false,
startX: event.clientX,
startY: event.clientY,
lastX: event.clientX,
lastY: event.clientY
};
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (pointerState.down) {
const dx = event.clientX - pointerState.lastX;
const dy = event.clientY - pointerState.lastY;
const distance = Math.hypot(
event.clientX - pointerState.startX,
event.clientY - pointerState.startY
);
if (distance > 4) pointerState.moved = true;
if (pointerState.moved) {
view.theta -= dx * 0.006;
view.phi = THREE.MathUtils.clamp(
view.phi + dy * 0.0045,
0.48,
1.12
);
updateCamera();
}
pointerState.lastX = event.clientX;
pointerState.lastY = event.clientY;
} else {
canvas.style.cursor = pickAt(event.clientX, event.clientY)
? "pointer"
: "grab";
}
});
canvas.addEventListener("pointerup", (event) => {
if (!pointerState.moved) handlePick(event.clientX, event.clientY);
pointerState.down = false;
canvas.style.cursor = "grab";
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", () => {
pointerState.down = false;
canvas.style.cursor = "grab";
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
view.zoom = THREE.MathUtils.clamp(
view.zoom * Math.exp(-event.deltaY * 0.001),
0.72,
1.55
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
document.querySelector("#frontView").addEventListener("click", () => {
view.theta = 0;
view.phi = 0.70;
view.zoom = 0.98;
updateCamera();
});
document.querySelector("#resetDemo").addEventListener("click", () => {
location.reload();
});
window.addEventListener("keydown", (event) => {
if (event.key === "1") selectCrate(0);
if (event.key === "2") selectCrate(1);
if (event.key === "3") selectCrate(2);
if (event.key.toLowerCase() === "r") {
view.theta = 0.08;
view.phi = 0.72;
view.zoom = 1;
updateCamera();
}
});
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
function animate(now) {
requestAnimationFrame(animate);
for (let i = motions.length - 1; i >= 0; i--) {
const motion = motions[i];
const progress = THREE.MathUtils.clamp(
(now - motion.startTime) / motion.duration,
0,
1
);
const eased =
progress < 0.5
? 4 * progress * progress * progress
: 1 - Math.pow(-2 * progress + 2, 3) / 2;
motion.object.position.lerpVectors(
motion.from,
motion.to,
eased
);
motion.object.position.y += Math.sin(Math.PI * eased) * motion.arc;
motion.object.rotation.y =
motion.startRotation + eased * Math.PI * 1.15;
if (progress >= 1) {
motion.object.position.copy(motion.to);
motion.onComplete?.();
motions.splice(i, 1);
}
}
selectionMaterial.opacity = 0.67 + Math.sin(now * 0.0035) * 0.12;
renderer.render(scene, camera);
}
selectCrate(selectedCrateIndex);
updateHUD();
requestAnimationFrame(animate);
</script>
</body>
</html>
```