Prompt specification
Build or customize the Three.js 3D scene "Kitchen waste sorting nook" 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: Kitchen waste sorting nook
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/kitchen-waste-sorting-nook-x0055
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing a pedal-operated waste bin. Arrange three bins beneath a countertop with food scraps, paper, and cans; moving an item to its correct bin triggers the corresponding lid and category symbol. Voxel style.
Working Three.js Starter (kitchen-waste-sorting-nook-x0055):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"
/>
<title>Voxel Waste Sorting Station</title>
<style>
:root {
--ink: #20332f;
--paper: #f5efe2;
--food: #58a96f;
--paper-bin: #4f7fc5;
--cans: #d99a35;
--shadow: rgba(38, 48, 44, 0.18);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8e0d1;
color: var(--ink);
font-family: "Courier New", Courier, monospace;
}
#app,
canvas {
width: 100%;
height: 100%;
display: block;
}
canvas {
touch-action: none;
outline: none;
}
.panel {
position: fixed;
z-index: 10;
border: 3px solid var(--ink);
background: rgba(245, 239, 226, 0.94);
box-shadow: 7px 7px 0 var(--shadow);
backdrop-filter: blur(8px);
}
#header {
top: 22px;
left: 22px;
max-width: 390px;
padding: 16px 18px;
}
#header h1 {
margin: 0 0 7px;
font-size: clamp(22px, 3vw, 34px);
line-height: 1;
letter-spacing: 2px;
}
#header p {
margin: 0;
font-size: 13px;
line-height: 1.55;
}
#progress {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-top: 14px;
}
.status-chip {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 6px 8px;
border: 2px solid var(--ink);
background: #fffaf0;
font-size: 11px;
font-weight: bold;
letter-spacing: 0.5px;
}
.status-chip::before {
content: "";
width: 12px;
height: 12px;
border: 2px solid var(--ink);
background: var(--chip-color);
}
.status-chip.done {
color: #fff;
background: var(--ink);
}
.status-chip.done::before {
background: var(--chip-color);
}
#toolbar {
top: 22px;
right: 22px;
display: flex;
gap: 10px;
}
button {
border: 3px solid var(--ink);
padding: 11px 14px;
background: var(--ink);
color: #fffaf0;
font: 700 13px "Courier New", Courier, monospace;
letter-spacing: 0.5px;
cursor: pointer;
box-shadow: 4px 4px 0 var(--shadow);
}
button:hover {
transform: translate(-1px, -1px);
box-shadow: 6px 6px 0 var(--shadow);
}
button:active {
transform: translate(2px, 2px);
box-shadow: 1px 1px 0 var(--shadow);
}
#instructions {
bottom: 20px;
left: 50%;
transform: translateX(-50%);
width: max-content;
max-width: calc(100vw - 32px);
padding: 11px 15px;
text-align: center;
font-size: 12px;
font-weight: bold;
letter-spacing: 0.3px;
}
#toast {
position: fixed;
z-index: 20;
left: 50%;
bottom: 86px;
transform: translate(-50%, 20px);
max-width: calc(100vw - 32px);
padding: 12px 17px;
border: 3px solid var(--ink);
background: #fffaf0;
box-shadow: 6px 6px 0 var(--shadow);
font-size: 13px;
font-weight: bold;
text-align: center;
opacity: 0;
pointer-events: none;
transition:
opacity 160ms ease,
transform 160ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#toast.good {
background: #dff0df;
}
#toast.bad {
background: #f5d7ca;
}
#complete {
position: fixed;
z-index: 30;
inset: 0;
display: grid;
place-items: center;
background: rgba(32, 51, 47, 0.3);
opacity: 0;
pointer-events: none;
transition: opacity 250ms ease;
}
#complete.show {
opacity: 1;
pointer-events: auto;
}
#complete-card {
width: min(420px, calc(100vw - 40px));
padding: 30px;
border: 4px solid var(--ink);
background: var(--paper);
text-align: center;
box-shadow: 10px 10px 0 var(--shadow);
}
#complete-card h2 {
margin: 0 0 10px;
font-size: 30px;
letter-spacing: 2px;
}
#complete-card p {
margin: 0 0 22px;
line-height: 1.6;
}
@media (max-width: 680px) {
#header {
top: 12px;
left: 12px;
right: 12px;
max-width: none;
padding: 12px;
}
#header p {
font-size: 11px;
}
#toolbar {
top: auto;
right: 12px;
bottom: 67px;
}
#instructions {
bottom: 12px;
font-size: 10px;
}
#toast {
bottom: 125px;
}
}
</style>
</head>
<body>
<div id="app"></div>
<section id="header" class="panel">
<h1>SORT STATION</h1>
<p>
Drag each voxel item into its matching bin.<br />
The correct lid opens and its category symbol appears.
</p>
<div id="progress">
<span class="status-chip" id="status-food" style="--chip-color: var(--food)">
FOOD
</span>
<span class="status-chip" id="status-paper" style="--chip-color: var(--paper-bin)">
PAPER
</span>
<span class="status-chip" id="status-cans" style="--chip-color: var(--cans)">
CANS
</span>
</div>
</section>
<div id="toolbar">
<button id="reset" type="button">RESET ITEMS</button>
</div>
<div id="instructions" class="panel">
DRAG ITEMS TO SORT · DRAG BACKGROUND TO ORBIT · SCROLL TO ZOOM
</div>
<div id="toast" role="status" aria-live="polite"></div>
<div id="complete">
<div id="complete-card">
<h2>ALL SORTED!</h2>
<p>
The food scraps, paper, and cans are all in the correct bins.
Nicely done, sorting champion!
</p>
<button id="play-again" type="button">PLAY AGAIN</button>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const app = document.querySelector("#app");
const toast = document.querySelector("#toast");
const completePanel = document.querySelector("#complete");
const CATEGORY = {
food: {
name: "ORGANICS",
short: "FOOD",
color: 0x58a96f,
light: 0xdcebd9,
ui: "#58a96f"
},
paper: {
name: "PAPER",
short: "PAPER",
color: 0x4f7fc5,
light: 0xdce7f7,
ui: "#4f7fc5"
},
cans: {
name: "CANS",
short: "CANS",
color: 0xd99a35,
light: 0xf3e2bd,
ui: "#d99a35"
}
};
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false
});
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",
"Interactive voxel waste sorting station"
);
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8e0d1);
scene.fog = new THREE.Fog(0xe8e0d1, 21, 38);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 2.7, 0);
const orbit = {
theta: 0.59,
phi: 1.07,
radius: 17.4,
targetTheta: 0.59,
targetPhi: 1.07,
targetRadius: 17.4
};
function updateCamera(dt = 1) {
const smoothing = 1 - Math.exp(-dt * 10);
orbit.theta += (orbit.targetTheta - orbit.theta) * smoothing;
orbit.phi += (orbit.targetPhi - orbit.phi) * smoothing;
orbit.radius += (orbit.targetRadius - orbit.radius) * smoothing;
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
cameraTarget.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
cameraTarget.y + orbit.radius * Math.cos(orbit.phi),
cameraTarget.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xfff6df, 0x718178, 2.1);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff2d4, 3.2);
sun.position.set(7, 13, 8);
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 = 35;
sun.shadow.bias = -0.0003;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xc7ddff, 0.8);
fill.position.set(-8, 7, 3);
scene.add(fill);
function cube(
parent,
width,
height,
depth,
x,
y,
z,
color,
castShadow = true,
receiveShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.02
})
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
parent.add(mesh);
return mesh;
}
function createTextSprite(text, background, foreground, width = 512, height = 128) {
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const context = canvas.getContext("2d");
context.fillStyle = background;
context.fillRect(0, 0, width, height);
context.strokeStyle = foreground;
context.lineWidth = 10;
context.strokeRect(5, 5, width - 10, height - 10);
context.fillStyle = foreground;
context.textAlign = "center";
context.textBaseline = "middle";
context.font = `700 ${Math.floor(height * 0.47)}px "Courier New", monospace`;
context.fillText(text, width / 2, height / 2 + 3);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.renderOrder = 8;
return sprite;
}
// Environment: tiled floor, wall, and countertop.
cube(scene, 14.5, 0.25, 10.5, 0, -0.2, 0.4, 0xd3c9b7);
cube(scene, 14.6, 7.2, 0.28, 0, 3.35, -2.85, 0xded5c5);
for (let x = -7; x <= 7; x += 1) {
cube(scene, 0.025, 0.012, 10.1, x, -0.065, 0.4, 0xbeb4a3, false, false);
}
for (let z = -4; z <= 5; z += 1) {
cube(scene, 14.1, 0.012, 0.025, 0, -0.064, z, 0xbeb4a3, false, false);
}
// Decorative square wall tiles.
for (let row = 0; row < 4; row++) {
for (let col = 0; col < 9; col++) {
const color = (row + col) % 3 === 0 ? 0xe7dfd0 : 0xdcd3c3;
cube(
scene,
1.32,
0.95,
0.055,
-6 + col * 1.5,
1.25 + row * 1.12,
-2.67,
color,
false,
false
);
}
}
// Countertop.
cube(scene, 13.3, 0.48, 2.7, 0, 4.96, -1.18, 0xb87c4d);
cube(scene, 13.45, 0.16, 2.82, 0, 5.27, -1.18, 0xe6b778);
cube(scene, 13.3, 0.16, 0.14, 0, 4.73, 0.23, 0x8d5c39);
for (let x = -6; x <= 6; x += 1) {
cube(scene, 0.035, 0.22, 0.04, x, 4.75, 0.31, 0x744a31, false, false);
}
const wallSign = createTextSprite(
"SORT STATION",
"#20332f",
"#f5efe2",
640,
150
);
wallSign.position.set(0, 6.65, -2.55);
wallSign.scale.set(4.8, 1.125, 1);
scene.add(wallSign);
const bins = [];
const dropZones = [];
const symbols = [];
function createSymbol(type) {
const category = CATEGORY[type];
const group = new THREE.Group();
// Pixel-style bordered category plaque.
cube(group, 1.48, 1.48, 0.15, 0, 0, 0, 0x20332f);
cube(group, 1.30, 1.30, 0.12, 0, 0, 0.09, category.color);
cube(group, 1.12, 1.12, 0.06, 0, 0, 0.17, category.light);
if (type === "food") {
const red = 0xd84d45;
const applePixels = [
[-0.30, 0.05],
[-0.15, 0.20],
[0.00, 0.20],
[0.15, 0.20],
[0.30, 0.05],
[-0.30, -0.10],
[-0.15, -0.10],
[0.00, -0.10],
[0.15, -0.10],
[0.30, -0.10],
[-0.15, -0.25],
[0.00, -0.25],
[0.15, -0.25]
];
applePixels.forEach(([x, y]) => {
cube(group, 0.16, 0.16, 0.12, x, y, 0.27, red);
});
cube(group, 0.08, 0.20, 0.10, 0, 0.36, 0.27, 0x70462f);
cube(group, 0.27, 0.08, 0.10, 0.15, 0.43, 0.27, 0x3f8b4c);
} else if (type === "paper") {
cube(group, 0.75, 0.86, 0.11, 0, 0, 0.27, 0xfffdf4);
cube(group, 0.16, 0.16, 0.04, 0.29, 0.31, 0.35, 0xcbd8e8);
cube(group, 0.50, 0.055, 0.035, -0.03, 0.18, 0.35, 0x4f7fc5);
cube(group, 0.50, 0.055, 0.035, -0.03, 0.02, 0.35, 0x4f7fc5);
cube(group, 0.34, 0.055, 0.035, -0.11, -0.14, 0.35, 0x4f7fc5);
} else {
cube(group, 0.58, 0.82, 0.12, 0, -0.03, 0.27, 0xcbd1d2);
cube(group, 0.62, 0.24, 0.04, 0, -0.02, 0.35, 0x4f7fc5);
cube(group, 0.64, 0.08, 0.13, 0, 0.40, 0.27, 0xe4e7e6);
cube(group, 0.22, 0.07, 0.06, 0, 0.47, 0.34, 0x677174);
}
group.visible = false;
scene.add(group);
return group;
}
function createBin(type, x, z, index) {
const category = CATEGORY[type];
const group = new THREE.Group();
group.position.set(x, 0, z);
scene.add(group);
// Bin shell and dark interior.
cube(group, 2.65, 0.34, 2.30, 0, 0.25, 0, 0x596763);
cube(group, 0.18, 2.15, 2.30, -1.23, 1.38, 0, 0x66736f);
cube(group, 0.18, 2.15, 2.30, 1.23, 1.38, 0, 0x66736f);
cube(group, 2.65, 2.15, 0.18, 0, 1.38, -1.05, 0x66736f);
cube(group, 2.65, 2.15, 0.18, 0, 1.38, 1.05, 0x66736f);
cube(group, 2.20, 0.09, 1.86, 0, 0.49, 0, 0x283633);
// Colored front face and ribs.
cube(group, 2.18, 1.48, 0.07, 0, 1.23, 1.16, category.color);
cube(group, 0.08, 1.25, 0.035, -0.72, 1.20, 1.21, category.light);
cube(group, 0.08, 1.25, 0.035, 0.72, 1.20, 1.21, category.light);
// Small top rim blocks emphasize the voxel construction.
cube(group, 0.26, 0.18, 2.38, -1.25, 2.48, 0, 0x46534f);
cube(group, 0.26, 0.18, 2.38, 1.25, 2.48, 0, 0x46534f);
cube(group, 2.65, 0.18, 0.26, 0, 2.48, -1.10, 0x46534f);
// Hinged lid. Negative X rotation lifts its front edge.
const lid = new THREE.Group();
lid.position.set(0, 2.55, -1.10);
group.add(lid);
cube(lid, 2.76, 0.18, 2.36, 0, 0.07, 1.14, category.color);
cube(lid, 2.82, 0.12, 0.22, 0, 0.04, 2.25, category.light);
cube(lid, 0.88, 0.18, 0.18, 0, 0.20, 2.00, 0x34413d);
cube(lid, 0.25, 0.20, 2.42, -1.25, 0.02, 1.14, 0x3d4b47);
cube(lid, 0.25, 0.20, 2.42, 1.25, 0.02, 1.14, 0x3d4b47);
const label = createTextSprite(
category.name,
`#${category.color.toString(16).padStart(6, "0")}`,
"#20332f"
);
label.position.set(0, 1.22, 1.27);
label.scale.set(2.03, 0.51, 1);
group.add(label);
// Invisible raycast target representing the bin opening.
const dropMaterial = new THREE.MeshBasicMaterial({
color: category.color,
transparent: true,
opacity: 0,
depthWrite: false,
side: THREE.DoubleSide
});
const dropZone = new THREE.Mesh(
new THREE.BoxGeometry(2.40, 2.30, 2.10),
dropMaterial
);
dropZone.position.set(0, 1.35, 0);
dropZone.userData.binType = type;
group.add(dropZone);
dropZones.push(dropZone);
const symbol = createSymbol(type);
symbol.position.set(x, 3.30, 2.65);
const bin = {
type,
x,
z,
index,
group,
lid,
symbol,
dropZone,
openTimer: 0,
shake: 0,
symbolActive: false,
symbolAge: 0
};
bins.push(bin);
symbols.push(symbol);
return bin;
}
createBin("food", -4.12, 1.02, 0);
createBin("paper", 0, 1.02, 1);
createBin("cans", 4.12, 1.02, 2);
const items = [];
const itemMeshes = [];
function registerItem(type, group) {
group.userData.type = type;
group.traverse((child) => {
if (child.isMesh) {
child.userData.itemRoot = group;
itemMeshes.push(child);
}
});
const item = {
type,
group,
originalPosition: group.position.clone(),
originalRotation: group.rotation.clone(),
sorted: false
};
items.push(item);
return item;
}
// Food scraps: a blocky apple core.
const foodGroup = new THREE.Group();
foodGroup.position.set(-4.12, 5.28, -1.15);
scene.add(foodGroup);
cube(foodGroup, 0.30, 0.50, 0.30, 0, 0.27, 0, 0xe8c982);
cube(foodGroup, 0.13, 0.43, 0.34, -0.21, 0.28, 0, 0xd84d45);
cube(foodGroup, 0.13, 0.43, 0.34, 0.21, 0.28, 0, 0xd84d45);
cube(foodGroup, 0.37, 0.10, 0.36, 0, 0.54, 0, 0xd84d45);
cube(foodGroup, 0.37, 0.10, 0.36, 0, 0.04, 0, 0xd84d45);
cube(foodGroup, 0.09, 0.20, 0.09, 0, 0.68, 0, 0x70462f);
cube(foodGroup, 0.28, 0.07, 0.13, 0.15, 0.76, 0, 0x3f8b4c);
registerItem("food", foodGroup);
// Paper: layered sheet with pixel writing.
const paperGroup = new THREE.Group();
paperGroup.position.set(0, 5.29, -1.15);
scene.add(paperGroup);
cube(paperGroup, 1.03, 0.06, 0.82, 0, 0.03, 0, 0xfffdf4);
cube(paperGroup, 0.17, 0.035, 0.16, 0.43, 0.075, 0.33, 0xcbd8e8);
cube(paperGroup, 0.55, 0.025, 0.055, -0.08, 0.075, 0.17, 0x4f7fc5);
cube(paperGroup, 0.55, 0.025, 0.055, -0.08, 0.075, 0.01, 0x4f7fc5);
cube(paperGroup, 0.37, 0.025, 0.055, -0.17, 0.075, -0.15, 0x4f7fc5);
registerItem("paper", paperGroup);
// Can: a squared-off aluminum can with a colored label band.
const canGroup = new THREE.Group();
canGroup.position.set(4.12, 5.29, -1.15);
scene.add(canGroup);
cube(canGroup, 0.60, 0.78, 0.60, 0, 0.39, 0, 0xcbd1d2);
cube(canGroup, 0.63, 0.25, 0.63, 0, 0.40, 0, 0xd99a35);
cube(canGroup, 0.64, 0.08, 0.64, 0, 0.79, 0, 0xe4e7e6);
cube(canGroup, 0.23, 0.055, 0.10, 0, 0.85, 0, 0x677174);
registerItem("cans", canGroup);
// Pointer and interaction state.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane();
const planeNormal = new THREE.Vector3();
const planePoint = new THREE.Vector3();
let draggingItem = null;
let dragOffset = new THREE.Vector3();
let orbiting = false;
let orbitPointerX = 0;
let orbitPointerY = 0;
let hoveredBin = null;
let completionTimer = null;
function setPointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function itemAtPointer(event) {
setPointer(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(itemMeshes, false);
return hits.length ? items.find((item) => item.group === hits[0].object.userData.itemRoot) : null;
}
function binAtPointer(event) {
setPointer(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(dropZones, false);
return hits.length ? hits[0].object.userData.binType : null;
}
function setHoveredBin(type) {
if (hoveredBin === type) return;
hoveredBin = type;
dropZones.forEach((zone) => {
zone.material.opacity =
zone.userData.binType === type ? 0.18 : 0;
});
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
const item = itemAtPointer(event);
if (item && !item.sorted) {
draggingItem = item;
camera.getWorldDirection(planeNormal);
dragPlane.setFromNormalAndCoplanarPoint(
planeNormal,
item.group.position
);
raycaster.setFromCamera(pointer, camera);
if (raycaster.ray.intersectPlane(dragPlane, planePoint)) {
dragOffset.copy(item.group.position).sub(planePoint);
} else {
dragOffset.set(0, 0, 0);
}
item.group.scale.setScalar(1.08);
renderer.domElement.style.cursor = "grabbing";
return;
}
orbiting = true;
orbitPointerX = event.clientX;
orbitPointerY = event.clientY;
renderer.domElement.style.cursor = "move";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (draggingItem) {
setPointer(event);
raycaster.setFromCamera(pointer, camera);
if (raycaster.ray.intersectPlane(dragPlane, planePoint)) {
draggingItem.group.position
.copy(planePoint)
.add(dragOffset);
draggingItem.group.position.x = THREE.MathUtils.clamp(
draggingItem.group.position.x,
-6.3,
6.3
);
draggingItem.group.position.y = THREE.MathUtils.clamp(
draggingItem.group.position.y,
0.45,
6.6
);
draggingItem.group.position.z = THREE.MathUtils.clamp(
draggingItem.group.position.z,
-3.3,
4.3
);
draggingItem.group.rotation.y += 0.018;
}
setHoveredBin(binAtPointer(event));
return;
}
if (orbiting) {
const dx = event.clientX - orbitPointerX;
const dy = event.clientY - orbitPointerY;
orbitPointerX = event.clientX;
orbitPointerY = event.clientY;
orbit.targetTheta = THREE.MathUtils.clamp(
orbit.targetTheta - dx * 0.006,
-0.6,
1.2
);
orbit.targetPhi = THREE.MathUtils.clamp(
orbit.targetPhi - dy * 0.005,
0.78,
1.34
);
return;
}
const item = itemAtPointer(event);
renderer.domElement.style.cursor = item ? "grab" : "default";
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (draggingItem) {
const item = draggingItem;
const targetType = binAtPointer(event);
draggingItem = null;
setHoveredBin(null);
renderer.domElement.style.cursor = "default";
if (targetType === item.type) {
sortCorrectly(item, targetType);
} else {
rejectItem(item, targetType);
}
return;
}
orbiting = false;
renderer.domElement.style.cursor = "default";
});
renderer.domElement.addEventListener("pointercancel", () => {
if (draggingItem) {
returnItem(draggingItem);
draggingItem = null;
}
orbiting = false;
setHoveredBin(null);
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.targetRadius = THREE.MathUtils.clamp(
orbit.targetRadius + event.deltaY * 0.008,
12.5,
23
);
},
{ passive: false }
);
// Tween system.
const tweens = [];
function addTween(duration, update, complete = null) {
const tween = {
elapsed: 0,
duration,
update,
complete
};
tweens.push(tween);
return tween;
}
function updateTweens(dt) {
for (let i = tweens.length - 1; i >= 0; i--) {
const tween = tweens[i];
tween.elapsed += dt;
const t = Math.min(tween.elapsed / tween.duration, 1);
tween.update(t);
if (t >= 1) {
tweens.splice(i, 1);
if (tween.complete) tween.complete();
}
}
}
const confetti = [];
function spawnConfetti(bin) {
const colors = [
bin.type === "food" ? 0x58a96f : 0xffffff,
bin.type === "paper" ? 0x4f7fc5 : 0xf5efe2,
bin.type === "cans" ? 0xd99a35 : 0x20332f,
0xe56b5d
];
for (let i = 0; i < 18; i++) {
const mesh = cube(
scene,
0.10 + Math.random() * 0.08,
0.10 + Math.random() * 0.08,
0.10 + Math.random() * 0.08,
bin.x + (Math.random() - 0.5) * 0.7,
3.25 + Math.random() * 0.25,
2.55 + (Math.random() - 0.5) * 0.4,
colors[i % colors.length],
false,
false
);
confetti.push({
mesh,
velocity: new THREE.Vector3(
(Math.random() - 0.5) * 2.2,
1.8 + Math.random() * 2.0,
(Math.random() - 0.5) * 1.6
),
spin: new THREE.Vector3(
Math.random() * 5,
Math.random() * 5,
Math.random() * 5
),
life: 1.7
});
}
}
function updateConfetti(dt) {
for (let i = confetti.length - 1; i >= 0; i--) {
const particle = confetti[i];
particle.life -= dt;
particle.velocity.y -= 4.8 * dt;
particle.mesh.position.addScaledVector(particle.velocity, dt);
particle.mesh.rotation.x += particle.spin.x * dt;
particle.mesh.rotation.y += particle.spin.y * dt;
particle.mesh.rotation.z += particle.spin.z * dt;
if (particle.life <= 0) {
scene.remove(particle.mesh);
particle.mesh.geometry.dispose();
particle.mesh.material.dispose();
confetti.splice(i, 1);
}
}
}
let toastTimer = null;
function showToast(message, kind = "") {
clearTimeout(toastTimer);
toast.textContent = message;
toast.className = kind ? `show ${kind}` : "show";
toastTimer = setTimeout(() => {
toast.className = "";
}, 2200);
}
function returnItem(item) {
const startPosition = item.group.position.clone();
const startRotation = item.group.rotation.clone();
const endPosition = item.originalPosition.clone();
const endRotation = item.originalRotation.clone();
addTween(
0.55,
(t) => {
const eased = 1 - Math.pow(1 - t, 3);
item.group.position.lerpVectors(startPosition, endPosition, eased);
item.group.rotation.set(
THREE.MathUtils.lerp(
startRotation.x,
endRotation.x,
eased
),
THREE.MathUtils.lerp(
startRotation.y,
endRotation.y,
eased
),
THREE.MathUtils.lerp(
startRotation.z,
endRotation.z,
eased
)
);
item.group.scale.setScalar(
THREE.MathUtils.lerp(1.08, 1, eased)
);
},
() => {
item.group.position.copy(endPosition);
item.group.rotation.copy(endRotation);
item.group.scale.setScalar(1);
}
);
}
function rejectItem(item, targetType) {
const bin = bins.find((candidate) => candidate.type === targetType);
if (bin) {
bin.shake = 0.65;
}
const destination =
targetType
? `the ${CATEGORY[targetType].name} bin`
: "a bin";
showToast(
`Not quite! That item belongs in the ${CATEGORY[item.type].name} bin, not ${destination}.`,
"bad"
);
returnItem(item);
}
function sortCorrectly(item, targetType) {
const bin = bins.find((candidate) => candidate.type === targetType);
item.sorted = true;
bin.openTimer = 1.8;
bin.symbolActive = true;
bin.symbolAge = 0;
const startPosition = item.group.position.clone();
const endPosition = new THREE.Vector3(bin.x, 0.72, bin.z);
const startScale = item.group.scale.x;
addTween(
0.62,
(t) => {
const eased = t * t * (3 - 2 * t);
item.group.position.lerpVectors(startPosition, endPosition, eased);
item.group.rotation.y += 0.16;
item.group.scale.setScalar(
THREE.MathUtils.lerp(startScale, 0.18, eased)
);
},
() => {
item.group.visible = false;
}
);
spawnConfetti(bin);
showToast(
`Correct! ${CATEGORY[item.type].short} → ${CATEGORY[item.type].name}.`,
"good"
);
document.querySelector(`#status-${item.type}`).classList.add("done");
const allSorted = items.every((candidate) => candidate.sorted);
if (allSorted) {
clearTimeout(completionTimer);
completionTimer = setTimeout(() => {
completePanel.classList.add("show");
}, 850);
}
}
function resetItems() {
clearTimeout(completionTimer);
completePanel.classList.remove("show");
items.forEach((item) => {
item.sorted = false;
item.group.visible = true;
item.group.position.copy(item.originalPosition);
item.group.rotation.copy(item.originalRotation);
item.group.scale.setScalar(1);
document
.querySelector(`#status-${item.type}`)
.classList.remove("done");
});
bins.forEach((bin) => {
bin.openTimer = 0;
bin.shake = 0;
bin.symbolActive = false;
bin.symbolAge = 0;
bin.symbol.visible = false;
bin.lid.rotation.x = 0;
bin.group.position.x = bin.x;
});
confetti.forEach((particle) => {
scene.remove(particle.mesh);
particle.mesh.geometry.dispose();
particle.mesh.material.dispose();
});
confetti.length = 0;
draggingItem = null;
orbiting = false;
setHoveredBin(null);
toast.className = "";
}
document
.querySelector("#reset")
.addEventListener("click", resetItems);
document
.querySelector("#play-again")
.addEventListener("click", resetItems);
// Animation loop.
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
updateCamera(dt);
updateTweens(dt);
updateConfetti(dt);
bins.forEach((bin) => {
bin.openTimer = Math.max(0, bin.openTimer - dt);
const targetAngle = bin.openTimer > 0 ? -1.10 : 0;
bin.lid.rotation.x +=
(targetAngle - bin.lid.rotation.x) *
Math.min(1, dt * 8.5);
if (bin.shake > 0) {
bin.shake = Math.max(0, bin.shake - dt);
bin.group.position.x =
bin.x + Math.sin(bin.shake * 65) * bin.shake * 0.17;
} else {
bin.group.position.x = bin.x;
}
if (bin.symbolActive) {
bin.symbolAge += dt;
bin.symbol.visible = true;
const pop = Math.min(1, bin.symbolAge * 4.5);
const pulse = 1 + Math.sin(elapsed * 3 + bin.index) * 0.035;
bin.symbol.scale.setScalar(pop * pulse);
bin.symbol.quaternion.copy(camera.quaternion);
bin.symbol.position.y =
3.30 + Math.sin(elapsed * 2.1 + bin.index * 1.7) * 0.07;
}
});
renderer.render(scene, camera);
}
animate();
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
</script>
</body>
</html>
```