Prompt specification
Build or customize the Three.js 3D scene "Folding table sorting station" 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: Folding table sorting station
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/folding-table-sorting-station-x0046
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing laundry baskets and sorting. Place socks, shirts, and towels beside three labeled baskets; clicking an item moves it into the matching group without intersecting neighboring objects during transit. Low-poly.
Working Three.js Starter (folding-table-sorting-station-x0046):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Low-Poly Laundry Sorter</title>
<style>
:root {
color-scheme: light;
--ink: #263238;
--muted: #66716f;
--panel: rgba(255, 255, 255, 0.82);
--border: rgba(38, 50, 56, 0.12);
--accent: #e4775f;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #f3eee5;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.ui {
position: fixed;
z-index: 10;
pointer-events: none;
}
.top-left {
top: 20px;
left: 20px;
max-width: min(390px, calc(100vw - 40px));
}
.eyebrow {
margin: 0 0 5px;
color: var(--accent);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.55rem, 3vw, 2.35rem);
line-height: 1;
letter-spacing: -0.045em;
}
.subtitle {
margin: 9px 0 0;
max-width: 350px;
color: var(--muted);
font-size: 0.9rem;
line-height: 1.45;
}
.top-right {
top: 20px;
right: 20px;
display: flex;
align-items: center;
gap: 9px;
pointer-events: auto;
}
.counter,
button {
border: 1px solid var(--border);
background: var(--panel);
box-shadow: 0 8px 25px rgba(44, 52, 49, 0.09);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
.counter {
padding: 11px 15px;
border-radius: 14px;
font-size: 0.84rem;
font-weight: 750;
white-space: nowrap;
}
button {
min-height: 42px;
padding: 0 15px;
border-radius: 14px;
color: var(--ink);
font: inherit;
font-size: 0.82rem;
font-weight: 750;
cursor: pointer;
transition:
transform 150ms ease,
background 150ms ease,
box-shadow 150ms ease;
}
button:hover {
transform: translateY(-2px);
background: rgba(255, 255, 255, 0.96);
box-shadow: 0 11px 28px rgba(44, 52, 49, 0.13);
}
button:active {
transform: translateY(0);
}
button.primary {
border-color: transparent;
background: var(--ink);
color: white;
}
button.primary:hover {
background: #354449;
}
.bottom {
left: 50%;
bottom: 18px;
width: min(680px, calc(100vw - 28px));
transform: translateX(-50%);
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
padding: 12px 15px;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 10px 30px rgba(44, 52, 49, 0.10);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
#status {
min-width: 0;
font-size: 0.84rem;
font-weight: 650;
line-height: 1.35;
}
.controls {
flex: 0 0 auto;
color: var(--muted);
font-size: 0.72rem;
line-height: 1.45;
text-align: right;
}
#loading {
position: fixed;
z-index: 30;
inset: 0;
display: grid;
place-items: center;
background: #f3eee5;
color: var(--muted);
font-size: 0.9rem;
font-weight: 700;
transition: opacity 300ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 720px) {
.top-left {
top: 14px;
left: 14px;
}
.subtitle {
max-width: 270px;
font-size: 0.8rem;
}
.top-right {
top: auto;
right: 14px;
bottom: 78px;
}
.counter {
display: none;
}
button {
min-height: 39px;
padding: 0 12px;
}
.bottom {
bottom: 12px;
align-items: flex-start;
}
.controls {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly laundry sorting scene"></canvas>
<header class="ui top-left">
<p class="eyebrow">Low-poly laundry lab</p>
<h1>Laundry Sorter</h1>
<p class="subtitle">
Click socks, shirts, or towels to send them into the matching basket.
Each item travels through a clear overhead route before being lowered.
</p>
</header>
<div class="ui top-right">
<div class="counter" id="counter">0 / 6 sorted</div>
<button id="sort-all" class="primary" type="button">Sort all</button>
<button id="reset" type="button">Reset</button>
</div>
<div class="ui bottom">
<div id="status">Choose a piece of laundry to begin sorting.</div>
<div class="controls">
Drag to orbit · Wheel to zoom<br>
Right-drag to pan
</div>
</div>
<div id="loading">Building the laundry room…</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 statusElement = document.querySelector("#status");
const counterElement = document.querySelector("#counter");
const sortAllButton = document.querySelector("#sort-all");
const resetButton = document.querySelector("#reset");
const loadingElement = document.querySelector("#loading");
// -------------------------------------------------------------------------
// Renderer, camera, and lighting
// -------------------------------------------------------------------------
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.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf3eee5);
scene.fog = new THREE.Fog(0xf3eee5, 19, 34);
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0.9, 0.35);
let cameraRadius = 14.5;
let cameraTheta = 0.34;
let cameraPhi = 1.03;
let userChangedZoom = false;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.fov = camera.aspect < 0.85 ? 52 : 38;
camera.updateProjectionMatrix();
if (!userChangedZoom) {
cameraRadius = camera.aspect < 0.85 ? 18 : 14.5;
}
updateCamera();
}
window.addEventListener("resize", resize);
resize();
const hemisphere = new THREE.HemisphereLight(
0xfffaf0,
0x89968d,
2.15
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff4df, 3.1);
sun.position.set(-5.5, 10, 7.5);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 9;
sun.shadow.camera.bottom = -7;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 28;
sun.shadow.bias = -0.0003;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb9d9e8, 0.8);
fill.position.set(7, 5, -6);
scene.add(fill);
// -------------------------------------------------------------------------
// Floor and visual helpers
// -------------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 22),
new THREE.MeshStandardMaterial({
color: 0xf3eee5,
roughness: 1,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.025;
floor.receiveShadow = true;
scene.add(floor);
const floorShape = new THREE.Mesh(
new THREE.CircleGeometry(7.2, 48),
new THREE.MeshBasicMaterial({
color: 0xe8e1d6,
transparent: true,
opacity: 0.62,
depthWrite: false
})
);
floorShape.rotation.x = -Math.PI / 2;
floorShape.position.set(0, -0.018, 0.45);
floorShape.scale.set(1, 0.57, 1);
scene.add(floorShape);
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
flatShading: true,
...options
});
}
function addBox(parent, width, height, depth, x, y, z, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function roundedRect(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();
}
// -------------------------------------------------------------------------
// Categories and baskets
// -------------------------------------------------------------------------
const categories = {
socks: {
id: "socks",
label: "SOCKS",
color: 0xe4775f,
css: "#e4775f",
x: -3.45,
count: 0,
items: [],
slots: [
{ x: -0.47, z: 0.055 },
{ x: 0.47, z: -0.055 }
]
},
shirts: {
id: "shirts",
label: "SHIRTS",
color: 0x4f89a8,
css: "#4f89a8",
x: 0,
count: 0,
items: [],
slots: [
{ x: -0.52, z: 0 },
{ x: 0.52, z: 0 }
]
},
towels: {
id: "towels",
label: "TOWELS",
color: 0x6e9c75,
css: "#6e9c75",
x: 3.45,
count: 0,
items: [],
slots: [
{ x: -0.49, z: 0.025 },
{ x: 0.49, z: -0.025 }
]
}
};
function drawCategoryLabel(category) {
const ctx = category.labelContext;
const canvas = category.labelCanvas;
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(ctx, 12, 12, 488, 144, 30);
ctx.fillStyle = "rgba(45, 54, 50, 0.13)";
ctx.fill();
roundedRect(ctx, 8, 6, 488, 144, 30);
ctx.fillStyle = category.css;
ctx.fill();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#ffffff";
ctx.font = "800 48px system-ui, sans-serif";
ctx.fillText(category.label, 252, 58);
ctx.fillStyle = "rgba(255,255,255,0.82)";
ctx.font = "650 25px system-ui, sans-serif";
ctx.fillText(
`${category.count} ${category.count === 1 ? "piece" : "pieces"} sorted`,
252,
111
);
category.labelTexture.needsUpdate = true;
}
function createBasket(category) {
const group = new THREE.Group();
group.position.x = category.x;
scene.add(group);
const bodyMaterial = standardMaterial(category.color, {
roughness: 0.76
});
const rimMaterial = standardMaterial(
new THREE.Color(category.color).lerp(
new THREE.Color(0xffffff),
0.24
),
{ roughness: 0.7 }
);
const interiorMaterial = standardMaterial(
new THREE.Color(category.color).lerp(
new THREE.Color(0x273a34),
0.44
),
{ roughness: 1 }
);
// Basket floor and visible interior.
addBox(group, 2.18, 0.18, 1.58, 0, 0.11, 0, bodyMaterial);
addBox(group, 1.94, 0.04, 1.35, 0, 0.205, 0, interiorMaterial);
// Four corner posts.
for (const x of [-1.04, 1.04]) {
for (const z of [-0.75, 0.75]) {
addBox(group, 0.14, 1.26, 0.14, x, 0.67, z, bodyMaterial);
}
}
// Front and back horizontal slats.
for (const y of [0.38, 0.72, 1.06]) {
for (const z of [-0.75, 0.75]) {
addBox(group, 2.08, 0.12, 0.10, 0, y, z, bodyMaterial);
}
}
// Side horizontal slats.
for (const y of [0.38, 0.72, 1.06]) {
for (const x of [-1.04, 1.04]) {
addBox(group, 0.10, 0.12, 1.40, x, y, 0, bodyMaterial);
}
}
// Top rim.
for (const z of [-0.78, 0.78]) {
addBox(group, 2.20, 0.14, 0.14, 0, 1.34, z, rimMaterial);
}
for (const x of [-1.08, 1.08]) {
addBox(group, 0.14, 0.14, 1.62, x, 1.34, 0, rimMaterial);
}
// Sign support.
addBox(group, 0.075, 1.35, 0.075, 0, 1.57, -0.94, rimMaterial);
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 168;
const labelContext = labelCanvas.getContext("2d");
const labelTexture = new THREE.CanvasTexture(labelCanvas);
labelTexture.colorSpace = THREE.SRGBColorSpace;
labelTexture.anisotropy = renderer.capabilities.getMaxAnisotropy();
category.labelCanvas = labelCanvas;
category.labelContext = labelContext;
category.labelTexture = labelTexture;
drawCategoryLabel(category);
const label = new THREE.Sprite(
new THREE.SpriteMaterial({
map: labelTexture,
transparent: true,
depthWrite: false,
toneMapped: false
})
);
label.position.set(category.x, 2.18, -0.94);
label.scale.set(2.34, 0.77, 1);
label.renderOrder = 3;
scene.add(label);
category.labelSprite = label;
// A subtle color patch on the floor beneath each basket.
const patch = new THREE.Mesh(
new THREE.CircleGeometry(1.38, 32),
new THREE.MeshBasicMaterial({
color: category.color,
transparent: true,
opacity: 0.10,
depthWrite: false
})
);
patch.rotation.x = -Math.PI / 2;
patch.position.set(category.x, -0.012, 0);
patch.scale.set(1, 0.72, 1);
scene.add(patch);
}
Object.values(categories).forEach(createBasket);
// -------------------------------------------------------------------------
// Low-poly laundry items
// -------------------------------------------------------------------------
const allItems = [];
const itemMeshes = [];
function registerItem(group, data) {
scene.add(group);
const item = {
group,
category: data.category,
name: data.name,
slotIndex: data.slotIndex,
state: "waiting",
materials: [],
motion: null
};
item.homePosition = group.position.clone();
item.homeRotation = group.rotation.y;
group.traverse((object) => {
if (!object.isMesh) return;
object.userData.item = item;
itemMeshes.push(object);
if (object.material && !item.materials.includes(object.material)) {
item.materials.push(object.material);
}
});
data.category.items.push(item);
allItems.push(item);
return item;
}
function createSocks(category, slotIndex, color, accentColor, position, rotation) {
const group = new THREE.Group();
group.position.copy(position);
group.rotation.y = rotation;
const sockShape = new THREE.Shape();
sockShape.moveTo(-0.17, -0.39);
sockShape.lineTo(0.12, -0.39);
sockShape.lineTo(0.12, 0.02);
sockShape.lineTo(0.20, 0.09);
sockShape.lineTo(0.20, 0.28);
sockShape.lineTo(0.10, 0.35);
sockShape.lineTo(-0.17, 0.35);
sockShape.closePath();
const geometry = new THREE.ExtrudeGeometry(sockShape, {
depth: 0.105,
bevelEnabled: false,
steps: 1,
curveSegments: 1
});
geometry.rotateX(-Math.PI / 2);
geometry.translate(0, 0, -0.025);
const sockMaterial = standardMaterial(color, {
roughness: 0.96,
side: THREE.DoubleSide
});
const cuffMaterial = standardMaterial(accentColor, {
roughness: 0.92
});
for (const side of [-1, 1]) {
const sock = new THREE.Mesh(geometry, sockMaterial);
sock.position.x = side * 0.21;
sock.rotation.y = side * 0.07;
sock.castShadow = true;
sock.receiveShadow = true;
group.add(sock);
const cuff = new THREE.Mesh(
new THREE.BoxGeometry(0.265, 0.018, 0.09),
cuffMaterial
);
cuff.position.set(0, 0.112, 0.315);
cuff.castShadow = true;
sock.add(cuff);
}
return registerItem(group, {
category,
slotIndex,
name: `Sock pair ${slotIndex + 1}`
});
}
function createShirt(category, slotIndex, color, accentColor, position, rotation) {
const group = new THREE.Group();
group.position.copy(position);
group.rotation.y = rotation;
const shirtShape = new THREE.Shape();
shirtShape.moveTo(-0.26, -0.46);
shirtShape.lineTo(0.26, -0.46);
shirtShape.lineTo(0.26, 0.20);
shirtShape.lineTo(0.38, 0.34);
shirtShape.lineTo(0.22, 0.50);
shirtShape.lineTo(0.09, 0.43);
shirtShape.lineTo(0, 0.37);
shirtShape.lineTo(-0.09, 0.43);
shirtShape.lineTo(-0.22, 0.50);
shirtShape.lineTo(-0.38, 0.34);
shirtShape.lineTo(-0.26, 0.20);
shirtShape.closePath();
const geometry = new THREE.ExtrudeGeometry(shirtShape, {
depth: 0.14,
bevelEnabled: false,
steps: 1,
curveSegments: 1
});
geometry.rotateX(-Math.PI / 2);
geometry.translate(0, 0, 0.025);
const shirtMaterial = standardMaterial(color, {
roughness: 0.94,
side: THREE.DoubleSide
});
const accentMaterial = standardMaterial(accentColor, {
roughness: 0.9
});
const shirt = new THREE.Mesh(geometry, shirtMaterial);
shirt.castShadow = true;
shirt.receiveShadow = true;
group.add(shirt);
const collar = new THREE.Mesh(
new THREE.BoxGeometry(0.23, 0.018, 0.065),
accentMaterial
);
collar.position.set(0, 0.15, -0.37);
collar.castShadow = true;
shirt.add(collar);
const pocket = new THREE.Mesh(
new THREE.BoxGeometry(0.15, 0.015, 0.13),
accentMaterial
);
pocket.position.set(0.12, 0.151, 0.06);
pocket.castShadow = true;
shirt.add(pocket);
return registerItem(group, {
category,
slotIndex,
name: `Shirt ${slotIndex + 1}`
});
}
function createTowel(category, slotIndex, color, stripeColor, position, rotation) {
const group = new THREE.Group();
group.position.copy(position);
group.rotation.y = rotation;
const towelMaterial = standardMaterial(color, {
roughness: 1
});
const stripeMaterial = standardMaterial(stripeColor, {
roughness: 0.95
});
const lower = addBox(
group,
0.90,
0.15,
0.62,
0,
0.075,
0,
towelMaterial
);
const upper = addBox(
group,
0.83,
0.07,
0.57,
0.025,
0.185,
-0.01,
towelMaterial
);
const stripe = addBox(
group,
0.56,
0.015,
0.085,
0.01,
0.227,
0.055,
stripeMaterial
);
lower.castShadow = upper.castShadow = stripe.castShadow = true;
lower.receiveShadow = upper.receiveShadow = stripe.receiveShadow = true;
return registerItem(group, {
category,
slotIndex,
name: `Towel ${slotIndex + 1}`
});
}
createSocks(
categories.socks,
0,
0xf1d8c8,
0xe4775f,
new THREE.Vector3(-3.92, 0, 1.55),
0.10
);
createSocks(
categories.socks,
1,
0x9ab2c2,
0xe4775f,
new THREE.Vector3(-2.88, 0, 1.98),
-0.13
);
createShirt(
categories.shirts,
0,
0x7ba8bd,
0xf4efe5,
new THREE.Vector3(-0.53, 0, 1.55),
0.09
);
createShirt(
categories.shirts,
1,
0xe3b65f,
0xf8f0da,
new THREE.Vector3(0.56, 0, 1.96),
-0.12
);
createTowel(
categories.towels,
0,
0x9e91af,
0xf0e7d8,
new THREE.Vector3(2.88, 0, 1.53),
0.07
);
createTowel(
categories.towels,
1,
0xd58b76,
0xf7e8da,
new THREE.Vector3(3.98, 0, 1.96),
-0.11
);
// Flat floor rings provide subtle click targets without blocking the view.
allItems.forEach((item) => {
const ring = new THREE.Mesh(
new THREE.RingGeometry(0.43, 0.465, 32),
new THREE.MeshBasicMaterial({
color: item.category.color,
transparent: true,
opacity: 0.28,
depthWrite: false,
side: THREE.DoubleSide
})
);
ring.rotation.x = -Math.PI / 2;
ring.position.set(
item.homePosition.x,
-0.009,
item.homePosition.z
);
scene.add(ring);
});
// -------------------------------------------------------------------------
// Sorting animation
//
// Only one item moves at a time. Every item:
// 1. lifts vertically,
// 2. enters a clear service lane in front of the baskets,
// 3. crosses above all neighboring objects,
// 4. approaches above its assigned basket slot,
// 5. lowers vertically into that non-overlapping slot.
// -------------------------------------------------------------------------
const CLEARANCE_Y = 4.2;
const SERVICE_LANE_Z = 2.75;
const FINAL_Y = 0.235;
const motionQueue = [];
let activeItem = null;
let sortedCount = 0;
function smoothStep(t) {
return t * t * (3 - 2 * t);
}
function easeInQuadratic(t) {
return t * t;
}
function updateCounter() {
counterElement.textContent = `${sortedCount} / ${allItems.length} sorted`;
}
function processQueue() {
if (activeItem || motionQueue.length === 0) return;
activeItem = motionQueue.shift();
activeItem.state = "moving";
const item = activeItem;
const category = item.category;
const slot = category.slots[item.slotIndex];
const start = item.group.position.clone();
const destination = new THREE.Vector3(
category.x + slot.x,
FINAL_Y,
slot.z
);
item.motion = {
startTime: performance.now(),
segments: [
{
duration: 520,
from: start.clone(),
to: new THREE.Vector3(start.x, CLEARANCE_Y, start.z),
easing: "smooth"
},
{
duration: 360,
from: new THREE.Vector3(start.x, CLEARANCE_Y, start.z),
to: new THREE.Vector3(start.x, CLEARANCE_Y, SERVICE_LANE_Z),
easing: "smooth"
},
{
duration: 760,
from: new THREE.Vector3(start.x, CLEARANCE_Y, SERVICE_LANE_Z),
to: new THREE.Vector3(destination.x, CLEARANCE_Y, SERVICE_LANE_Z),
easing: "smooth"
},
{
duration: 480,
from: new THREE.Vector3(destination.x, CLEARANCE_Y, SERVICE_LANE_Z),
to: new THREE.Vector3(destination.x, CLEARANCE_Y, destination.z),
easing: "smooth"
},
{
duration: 620,
from: new THREE.Vector3(destination.x, CLEARANCE_Y, destination.z),
to: destination.clone(),
easing: "drop"
}
]
};
statusElement.textContent =
`${item.name} is routing above the baskets…`;
}
function finishItem(item) {
const category = item.category;
item.state = "sorted";
item.motion = null;
category.count += 1;
sortedCount += 1;
drawCategoryLabel(category);
updateCounter();
statusElement.textContent =
`${item.name} sorted into the ${category.label} basket.`;
activeItem = null;
window.setTimeout(processQueue, 120);
}
function updateMotion(now) {
const item = activeItem;
if (!item || !item.motion) return;
let remaining = now - item.motion.startTime;
let segmentIndex = 0;
while (
segmentIndex < item.motion.segments.length - 1 &&
remaining > item.motion.segments[segmentIndex].duration
) {
remaining -= item.motion.segments[segmentIndex].duration;
segmentIndex += 1;
}
const segment = item.motion.segments[segmentIndex];
const progress = THREE.MathUtils.clamp(
remaining / segment.duration,
0,
1
);
const eased =
segment.easing === "drop"
? easeInQuadratic(progress)
: smoothStep(progress);
item.group.position.lerpVectors(segment.from, segment.to, eased);
if (
segmentIndex === item.motion.segments.length - 1 &&
progress >= 1
) {
item.group.position.copy(segment.to);
finishItem(item);
}
}
function queueItem(item) {
if (!item || item.state !== "waiting") return;
item.state = "queued";
motionQueue.push(item);
statusElement.textContent =
activeItem
? `${item.name} queued behind the current item.`
: `${item.name} queued for sorting.`;
processQueue();
}
function sortEverything() {
let added = 0;
for (const item of allItems) {
if (item.state === "waiting") {
item.state = "queued";
motionQueue.push(item);
added += 1;
}
}
if (added === 0) {
statusElement.textContent = "Everything is already sorted.";
} else {
statusElement.textContent = `${added} pieces queued for sorting.`;
processQueue();
}
}
function resetScene() {
motionQueue.length = 0;
activeItem = null;
sortedCount = 0;
for (const item of allItems) {
item.state = "waiting";
item.motion = null;
item.group.position.copy(item.homePosition);
item.group.rotation.y = item.homeRotation;
item.group.scale.setScalar(1);
}
for (const category of Object.values(categories)) {
category.count = 0;
drawCategoryLabel(category);
}
setHoveredItem(null);
updateCounter();
statusElement.textContent = "Choose a piece of laundry to begin sorting.";
}
sortAllButton.addEventListener("click", sortEverything);
resetButton.addEventListener("click", resetScene);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") {
resetScene();
}
if (event.code === "Space") {
event.preventDefault();
sortEverything();
}
});
// -------------------------------------------------------------------------
// Picking, hover feedback, and direct camera controls
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let hoveredItem = null;
function setHoveredItem(item) {
if (hoveredItem === item) return;
if (hoveredItem) {
for (const material of hoveredItem.materials) {
material.emissive.setHex(0x000000);
}
}
hoveredItem = item;
if (hoveredItem) {
for (const material of hoveredItem.materials) {
material.emissive.setHex(0x3d2a12);
material.emissiveIntensity = 0.32;
}
}
}
function updatePointer(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 pickItem(event) {
updatePointer(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(itemMeshes, false);
if (hits.length === 0) return null;
return hits[0].object.userData.item || null;
}
const pointerState = {
down: false,
mode: "orbit",
startX: 0,
startY: 0,
lastX: 0,
lastY: 0
};
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0 && event.button !== 2) return;
pointerState.down = true;
pointerState.mode =
event.button === 2 || event.shiftKey ? "pan" : "orbit";
pointerState.startX = event.clientX;
pointerState.startY = event.clientY;
pointerState.lastX = event.clientX;
pointerState.lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
setHoveredItem(null);
});
canvas.addEventListener("pointermove", (event) => {
if (!pointerState.down) {
const item = pickItem(event);
setHoveredItem(item && item.state === "waiting" ? item : null);
canvas.style.cursor = hoveredItem ? "pointer" : "grab";
return;
}
const dx = event.clientX - pointerState.lastX;
const dy = event.clientY - pointerState.lastY;
pointerState.lastX = event.clientX;
pointerState.lastY = event.clientY;
if (pointerState.mode === "orbit") {
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi + dy * 0.005,
0.48,
1.34
);
} else {
const panScale = cameraRadius * 0.00135;
const right = new THREE.Vector3(
Math.cos(cameraTheta),
0,
-Math.sin(cameraTheta)
);
const forward = new THREE.Vector3(
-Math.sin(cameraTheta),
0,
-Math.cos(cameraTheta)
);
cameraTarget.addScaledVector(right, -dx * panScale);
cameraTarget.addScaledVector(forward, dy * panScale);
cameraTarget.x = THREE.MathUtils.clamp(cameraTarget.x, -4, 4);
cameraTarget.z = THREE.MathUtils.clamp(cameraTarget.z, -2, 3);
}
updateCamera();
});
canvas.addEventListener("pointerup", (event) => {
if (!pointerState.down) return;
const travel = Math.hypot(
event.clientX - pointerState.startX,
event.clientY - pointerState.startY
);
const wasClick =
event.button === 0 &&
pointerState.mode === "orbit" &&
travel < 6;
pointerState.down = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
if (wasClick) {
const item = pickItem(event);
if (item && item.state === "waiting") {
queueItem(item);
} else if (item && item.state === "sorted") {
statusElement.textContent =
`${item.name} is already in the ${item.category.label} basket.`;
}
}
canvas.style.cursor = "grab";
});
canvas.addEventListener("pointercancel", () => {
pointerState.down = false;
setHoveredItem(null);
canvas.style.cursor = "grab";
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
userChangedZoom = true;
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
8,
25
);
updateCamera();
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
function animate(now) {
requestAnimationFrame(animate);
updateMotion(now);
renderer.render(scene, camera);
}
updateCounter();
requestAnimationFrame(animate);
requestAnimationFrame(() => {
loadingElement.classList.add("hidden");
window.setTimeout(() => loadingElement.remove(), 350);
});
</script>
</body>
</html>
```