Prompt specification
Build or customize the Three.js 3D scene "Shared snack allocation game" 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: Shared snack allocation game
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/shared-snack-allocation-game-x0328
Scene Description Prompt:
Distribute equal groups of low-poly toy snacks around a classroom table; counting beads track allocated and remaining units. Standalone HTML with Three.js from cdnjs.
Working Three.js Starter (shared-snack-allocation-game-x0328):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Snack Sharing Lab</title>
<style>
:root {
--ink: #27332f;
--muted: #6e7772;
--paper: rgba(255, 252, 244, 0.93);
--green: #48a77c;
--orange: #ed8c62;
--line: rgba(39, 51, 47, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8e1d4;
color: var(--ink);
font-family:
Inter, ui-rounded, "SF Pro Rounded", system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.overlay {
position: fixed;
inset: 0;
pointer-events: none;
}
.brand {
position: absolute;
top: 24px;
left: 26px;
max-width: 410px;
padding: 17px 20px;
border: 1px solid rgba(255, 255, 255, 0.55);
border-radius: 20px;
background: rgba(255, 252, 244, 0.82);
box-shadow: 0 14px 40px rgba(49, 43, 34, 0.09);
backdrop-filter: blur(12px);
}
.eyebrow {
margin-bottom: 6px;
color: #56836f;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.brand h1 {
margin: 0;
font-size: clamp(26px, 3vw, 38px);
line-height: 1;
letter-spacing: -0.045em;
}
.brand p {
margin: 9px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.panel {
position: absolute;
top: 24px;
right: 24px;
width: min(360px, calc(100vw - 48px));
max-height: calc(100vh - 48px);
overflow: auto;
padding: 19px;
border: 1px solid rgba(255, 255, 255, 0.62);
border-radius: 24px;
background: var(--paper);
box-shadow: 0 18px 55px rgba(49, 43, 34, 0.14);
backdrop-filter: blur(16px);
pointer-events: auto;
scrollbar-width: thin;
}
.panel-header {
display: flex;
align-items: center;
gap: 11px;
margin-bottom: 18px;
}
.panel-icon {
position: relative;
width: 38px;
height: 38px;
flex: 0 0 auto;
border-radius: 12px;
background: #e6f2e8;
}
.panel-icon::before,
.panel-icon::after {
content: "";
position: absolute;
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--green);
box-shadow: 13px 0 0 #b9d7c6;
top: 10px;
left: 8px;
}
.panel-icon::after {
top: 19px;
left: 8px;
background: var(--orange);
box-shadow: 13px 0 0 #e5bda8;
}
.panel-header small {
display: block;
margin-bottom: 2px;
color: var(--muted);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.14em;
}
.panel-header h2 {
margin: 0;
font-size: 18px;
letter-spacing: -0.025em;
}
.status-badge {
margin-left: auto;
padding: 6px 9px;
border-radius: 999px;
background: #edf3eb;
color: #47765f;
font-size: 10px;
font-weight: 800;
white-space: nowrap;
}
.counter {
margin-top: 15px;
padding: 14px;
border: 1px solid var(--line);
border-radius: 17px;
background: rgba(255, 255, 255, 0.56);
}
.counter-heading {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 11px;
font-size: 12px;
font-weight: 750;
}
.counter-heading strong {
margin-left: auto;
font-size: 23px;
line-height: 1;
letter-spacing: -0.05em;
}
.counter-heading .total {
align-self: flex-end;
margin: 0 0 2px 1px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--green);
}
.swatch.orange {
background: var(--orange);
}
.beads {
display: grid;
grid-template-columns: repeat(12, 1fr);
gap: 4px;
align-items: center;
}
.bead {
width: 100%;
aspect-ratio: 1;
max-width: 15px;
justify-self: center;
border-radius: 50%;
background: #e6dfd4;
box-shadow:
inset 0 2px 2px rgba(61, 52, 42, 0.08),
0 1px 0 rgba(255, 255, 255, 0.8);
transition:
transform 220ms cubic-bezier(0.2, 0.8, 0.2, 1),
background 220ms ease,
box-shadow 220ms ease;
}
.bead.filled.allocated {
background: var(--green);
box-shadow:
inset -2px -2px 3px rgba(20, 88, 58, 0.18),
inset 2px 2px 3px rgba(255, 255, 255, 0.28),
0 2px 3px rgba(39, 100, 74, 0.16);
}
.bead.filled.remaining {
background: var(--orange);
box-shadow:
inset -2px -2px 3px rgba(145, 57, 30, 0.16),
inset 2px 2px 3px rgba(255, 255, 255, 0.25),
0 2px 3px rgba(170, 75, 43, 0.14);
}
.bead.pop {
transform: scale(1.4);
}
.progress-label {
display: flex;
justify-content: space-between;
margin: 19px 1px 8px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
}
.progress-track {
height: 7px;
overflow: hidden;
border-radius: 999px;
background: #e7e0d6;
}
.progress-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #69bd91, #3f956d);
transition: width 350ms ease;
}
.group-heading {
margin: 18px 1px 9px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.groups {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 7px;
}
.group-chip {
padding: 8px 4px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.55);
text-align: center;
transition:
transform 180ms ease,
border-color 180ms ease,
background 180ms ease;
}
.group-chip span {
display: block;
margin-bottom: 2px;
font-size: 10px;
font-weight: 850;
}
.group-chip b {
color: var(--muted);
font-size: 11px;
}
.group-chip.complete {
transform: translateY(-2px);
border-color: rgba(72, 167, 124, 0.3);
background: #edf6ef;
}
.group-chip.complete b {
color: #3d8b67;
}
.status-text {
min-height: 34px;
margin: 15px 1px 12px;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.actions {
display: grid;
grid-template-columns: 1fr auto;
gap: 9px;
}
.action {
min-height: 43px;
border: 0;
border-radius: 13px;
cursor: pointer;
font-size: 12px;
font-weight: 800;
transition:
transform 150ms ease,
box-shadow 150ms ease,
opacity 150ms ease;
}
.action:hover:not(:disabled) {
transform: translateY(-2px);
}
.action:active:not(:disabled) {
transform: translateY(0);
}
.action:disabled {
cursor: default;
opacity: 0.58;
}
.primary {
display: flex;
align-items: center;
justify-content: center;
gap: 9px;
color: white;
background: #347d5b;
box-shadow: 0 8px 18px rgba(52, 125, 91, 0.22);
}
.primary-icon {
font-size: 17px;
line-height: 1;
}
.secondary {
width: 43px;
color: #58645e;
background: #ebe5db;
}
.controls-hint {
margin: 15px 1px 0;
padding-top: 13px;
border-top: 1px solid var(--line);
color: #7a827e;
font-size: 10px;
line-height: 1.55;
text-align: center;
}
.controls-hint strong {
color: #52605a;
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #e8e1d4;
transition: opacity 450ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
.loader-card {
padding: 20px 24px;
border-radius: 18px;
background: rgba(255, 252, 244, 0.92);
box-shadow: 0 15px 45px rgba(49, 43, 34, 0.12);
text-align: center;
}
.loader-dots {
display: flex;
justify-content: center;
gap: 7px;
margin-bottom: 13px;
}
.loader-dots i {
width: 11px;
height: 11px;
border-radius: 50%;
background: var(--green);
animation: bounce 900ms ease-in-out infinite alternate;
}
.loader-dots i:nth-child(2) {
animation-delay: 160ms;
}
.loader-dots i:nth-child(3) {
animation-delay: 320ms;
}
.loader-card span {
color: var(--muted);
font-size: 12px;
font-weight: 700;
}
@keyframes bounce {
to {
transform: translateY(-7px);
opacity: 0.55;
}
}
@media (max-width: 720px) {
.brand {
top: 12px;
left: 12px;
right: 12px;
max-width: none;
padding: 13px 15px;
border-radius: 17px;
}
.brand h1 {
font-size: 25px;
}
.brand p {
display: none;
}
.panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
max-height: 52vh;
padding: 15px;
border-radius: 20px;
}
.panel-header {
margin-bottom: 10px;
}
.counter {
margin-top: 10px;
padding: 11px;
}
.controls-hint {
display: none;
}
}
@media (prefers-reduced-motion: reduce) {
.bead,
.group-chip,
.action {
transition-duration: 1ms !important;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D classroom snack-sharing scene"></canvas>
<div class="overlay">
<header class="brand">
<div class="eyebrow">Classroom Counting Lab</div>
<h1>Snack sharing</h1>
<p>12 toy snacks. 4 equal groups. 3 snacks in every group.</p>
</header>
<aside class="panel" aria-label="Snack counting tracker">
<div class="panel-header">
<div class="panel-icon" aria-hidden="true"></div>
<div>
<small>Counting Beads</small>
<h2>Snack tracker</h2>
</div>
<span class="status-badge" id="statusBadge">0 / 4 groups</span>
</div>
<section class="counter">
<div class="counter-heading">
<span class="swatch"></span>
<span>Allocated</span>
<strong id="allocatedCount">0</strong>
<span class="total">/ 12</span>
</div>
<div class="beads" id="allocatedBeads" aria-hidden="true"></div>
</section>
<section class="counter">
<div class="counter-heading">
<span class="swatch orange"></span>
<span>Remaining</span>
<strong id="remainingCount">12</strong>
<span class="total">/ 12</span>
</div>
<div class="beads" id="remainingBeads" aria-hidden="true"></div>
</section>
<div class="progress-label">
<span>Sharing progress</span>
<span id="progressText">0%</span>
</div>
<div class="progress-track">
<div class="progress-fill" id="progressFill"></div>
</div>
<div class="group-heading">Four equal groups</div>
<div class="groups" id="groupChips">
<div class="group-chip" data-group="0"><span>A</span><b>0/3</b></div>
<div class="group-chip" data-group="1"><span>B</span><b>0/3</b></div>
<div class="group-chip" data-group="2"><span>C</span><b>0/3</b></div>
<div class="group-chip" data-group="3"><span>D</span><b>0/3</b></div>
</div>
<p class="status-text" id="statusText" aria-live="polite">
Ready to share the first group.
</p>
<div class="actions">
<button class="action primary" id="primaryButton" type="button">
<span class="primary-icon" id="primaryIcon">↻</span>
<span id="primaryLabel">Distribute all</span>
</button>
<button class="action secondary" id="resetButton" type="button" aria-label="Reset snacks">
↺
</button>
</div>
<p class="controls-hint">
<strong>Drag</strong> to orbit · <strong>Scroll</strong> to zoom ·
<strong>Tap a mat</strong> to fill the next group
</p>
</aside>
</div>
<div id="loading">
<div class="loader-card">
<div class="loader-dots"><i></i><i></i><i></i></div>
<span>Setting out the classroom snacks…</span>
</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 canvas = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const allocatedCount = document.querySelector("#allocatedCount");
const remainingCount = document.querySelector("#remainingCount");
const allocatedBeads = document.querySelector("#allocatedBeads");
const remainingBeads = document.querySelector("#remainingBeads");
const progressFill = document.querySelector("#progressFill");
const progressText = document.querySelector("#progressText");
const statusBadge = document.querySelector("#statusBadge");
const statusText = document.querySelector("#statusText");
const primaryButton = document.querySelector("#primaryButton");
const primaryIcon = document.querySelector("#primaryIcon");
const primaryLabel = document.querySelector("#primaryLabel");
const resetButton = document.querySelector("#resetButton");
const groupChipElements = [...document.querySelectorAll(".group-chip")];
const TOTAL_SNACKS = 12;
const SNACKS_PER_GROUP = 3;
const GROUP_COUNT = 4;
let allocated = 0;
let groupsCompleted = 0;
let currentGroupCounts = [0, 0, 0, 0];
let busy = false;
let groupBusy = false;
let session = 0;
const scene = new THREE.Scene();
scene.background = new THREE.Color("#e8e1d4");
scene.fog = new THREE.Fog("#e8e1d4", 20, 42);
const camera = new THREE.PerspectiveCamera(
36,
innerWidth / innerHeight,
0.1,
80
);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const world = new THREE.Group();
scene.add(world);
const clock = new THREE.Clock();
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const palette = {
wood: "#d5a269",
woodDark: "#a56f43",
wall: "#eee8dd",
floor: "#d9d2c4",
cream: "#fff8e9",
green: "#48a77c",
orange: "#ed8c62",
emptyBead: "#d9d2c5"
};
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0,
flatShading: true,
...options
});
}
function roundedGeometry(width, height, depth, radius = 0.12) {
const w = width;
const h = height;
const r = Math.min(radius, w * 0.45, h * 0.45);
const x = -w / 2;
const y = -h / 2;
const shape = new THREE.Shape();
shape.moveTo(x + r, y);
shape.lineTo(x + w - r, y);
shape.quadraticCurveTo(x + w, y, x + w, y + r);
shape.lineTo(x + w, y + h - r);
shape.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
shape.lineTo(x + r, y + h);
shape.quadraticCurveTo(x, y + h, x, y + h - r);
shape.lineTo(x, y + r);
shape.quadraticCurveTo(x, y, x + r, y);
const bevel = Math.min(r * 0.45, 0.055);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
steps: 1,
bevelEnabled: true,
bevelSegments: 1,
bevelSize: bevel,
bevelThickness: bevel,
curveSegments: 3
});
geometry.center();
return geometry;
}
function roundedBox(
width,
height,
depth,
radius,
mat,
parent = world,
x = 0,
y = 0,
z = 0
) {
const mesh = new THREE.Mesh(
roundedGeometry(width, height, depth, radius),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function box(width, height, depth, mat, parent = world, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(width, height, depth), mat);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinder(radiusTop, radiusBottom, height, segments, mat, parent = world, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusTop, radiusBottom, height, segments),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function roundedRectPath(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function canvasTexture(canvasElement) {
const texture = new THREE.CanvasTexture(canvasElement);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(8, renderer.capabilities.getMaxAnisotropy());
return texture;
}
// Lighting
scene.add(new THREE.HemisphereLight("#fff9e9", "#8d9b91", 2.0));
const keyLight = new THREE.DirectionalLight("#fff3dc", 3.2);
keyLight.position.set(-6, 12, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -15;
keyLight.shadow.camera.right = 15;
keyLight.shadow.camera.top = 15;
keyLight.shadow.camera.bottom = -15;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight("#d9e9ff", 1.1);
fillLight.position.set(8, 7, -5);
scene.add(fillLight);
// Classroom shell
const floorMat = material(palette.floor, { roughness: 0.95 });
const floor = new THREE.Mesh(new THREE.PlaneGeometry(40, 34), floorMat);
floor.rotation.x = -Math.PI / 2;
floor.receiveShadow = true;
world.add(floor);
const grid = new THREE.GridHelper(30, 30, "#b9b09f", "#c9c0b1");
grid.position.y = 0.012;
grid.material.transparent = true;
grid.material.opacity = 0.34;
world.add(grid);
const wallMat = material(palette.wall);
roundedBox(27, 7.4, 0.38, 0.16, wallMat, world, 0, 3.7, -9.2);
const rugMat = material("#bed5c6", { roughness: 1 });
const rug = new THREE.Mesh(new THREE.CircleGeometry(7.2, 36), rugMat);
rug.rotation.x = -Math.PI / 2;
rug.scale.set(1.35, 0.74, 1);
rug.position.y = 0.025;
rug.receiveShadow = true;
world.add(rug);
// Whiteboard
const boardFrame = material("#b88054");
const boardSurface = material("#fbfaf1");
roundedBox(9.2, 3.1, 0.22, 0.12, boardFrame, world, -2.2, 3.7, -8.88);
roundedBox(8.92, 2.82, 0.24, 0.08, boardSurface, world, -2.2, 3.7, -8.72);
const boardCanvas = document.createElement("canvas");
boardCanvas.width = 1024;
boardCanvas.height = 320;
const boardCtx = boardCanvas.getContext("2d");
boardCtx.fillStyle = "#fbfaf1";
boardCtx.fillRect(0, 0, boardCanvas.width, boardCanvas.height);
boardCtx.fillStyle = "#337858";
boardCtx.font = "800 72px system-ui";
boardCtx.textAlign = "center";
boardCtx.fillText("SHARE SNACKS", 512, 112);
boardCtx.fillStyle = "#69736e";
boardCtx.font = "600 35px system-ui";
boardCtx.fillText("12 snacks • 4 groups • 3 each", 512, 190);
boardCtx.strokeStyle = "#e4aa65";
boardCtx.lineWidth = 8;
boardCtx.beginPath();
boardCtx.moveTo(250, 230);
boardCtx.lineTo(774, 230);
boardCtx.stroke();
const boardTexture = canvasTexture(boardCanvas);
const boardText = new THREE.Mesh(
new THREE.PlaneGeometry(8.65, 2.7),
new THREE.MeshBasicMaterial({ map: boardTexture })
);
boardText.position.set(-2.2, 3.7, -8.58);
world.add(boardText);
roundedBox(3.2, 0.13, 0.28, 0.04, boardFrame, world, -2.2, 2.12, -8.48);
// Clock
const clockFrame = material("#9a6743");
const clockFace = material("#fff9e9");
const clockBody = cylinder(0.56, 0.56, 0.16, 24, clockFrame, world, 4.7, 3.8, -8.78);
clockBody.rotation.x = Math.PI / 2;
const clockDisk = cylinder(0.47, 0.47, 0.18, 24, clockFace, world, 4.7, 3.8, -8.66);
clockDisk.rotation.x = Math.PI / 2;
const handMat = material("#4b5b55");
const hourHand = box(0.045, 0.27, 0.035, handMat, world, 4.7, 3.88, -8.54);
hourHand.rotation.z = -0.55;
const minuteHand = box(0.035, 0.36, 0.035, handMat, world, 4.78, 3.78, -8.52);
minuteHand.rotation.z = 0.35;
// Bookcase
const shelfMat = material("#b98659");
const shelfGroup = new THREE.Group();
shelfGroup.position.set(8.4, 0, -8.2);
world.add(shelfGroup);
box(3.1, 0.18, 0.75, shelfMat, shelfGroup, 0, 0.18, 0);
box(3.1, 0.18, 0.75, shelfMat, shelfGroup, 0, 1.52, 0);
box(3.1, 0.18, 0.75, shelfMat, shelfGroup, 0, 2.86, 0);
box(0.18, 3.05, 0.75, shelfMat, shelfGroup, -1.46, 1.52, 0);
box(0.18, 3.05, 0.75, shelfMat, shelfGroup, 1.46, 1.52, 0);
const bookColors = ["#e77967", "#5ca38c", "#e4b451", "#788cc5", "#bc76a1"];
for (let row = 0; row < 3; row++) {
for (let i = 0; i < 7; i++) {
const height = 0.38 + ((i + row) % 3) * 0.07;
const book = box(
0.22,
height,
0.44,
material(bookColors[(i + row) % bookColors.length]),
shelfGroup,
-1.16 + i * 0.37,
0.27 + row * 1.34 + height / 2,
0.02
);
book.rotation.z = ((i % 3) - 1) * 0.035;
}
}
// Low-poly plant
const plantGroup = new THREE.Group();
plantGroup.position.set(-7.1, 0, -7.55);
world.add(plantGroup);
const pot = cylinder(0.52, 0.4, 0.9, 8, material("#d78962"), plantGroup, 0, 0.45, 0);
cylinder(0.43, 0.43, 0.09, 8, material("#5c4939"), plantGroup, 0, 0.91, 0);
const leafMats = [
material("#4f916b"),
material("#6ca77a"),
material("#3f7f5e")
];
for (let i = 0; i < 7; i++) {
const leaf = new THREE.Mesh(
new THREE.ConeGeometry(0.42, 1.6 + (i % 2) * 0.2, 5),
leafMats[i % leafMats.length]
);
leaf.position.set(
Math.sin((i / 7) * Math.PI * 2) * 0.25,
1.45 + (i % 2) * 0.22,
Math.cos((i / 7) * Math.PI * 2) * 0.25
);
leaf.rotation.z = Math.sin(i * 1.7) * 0.55;
leaf.rotation.x = Math.cos(i * 1.3) * 0.45;
leaf.castShadow = true;
plantGroup.add(leaf);
}
// Main classroom table
const tableTopMat = material(palette.wood);
const tableEdgeMat = material(palette.woodDark);
const legMat = material("#8f9b91");
roundedBox(11.6, 0.42, 6.6, 0.3, tableTopMat, world, 0, 1.55, 0);
roundedBox(11.35, 0.2, 6.35, 0.2, tableEdgeMat, world, 0, 1.3, 0);
for (const x of [-5.05, 5.05]) {
for (const z of [-2.65, 2.65]) {
roundedBox(0.42, 1.38, 0.42, 0.07, legMat, world, x, 0.69, z);
}
}
box(10.2, 0.2, 0.22, legMat, world, 0, 0.53, -2.65);
box(10.2, 0.2, 0.22, legMat, world, 0, 0.53, 2.65);
// Subtle plank seams
const seamMat = material("#bd8757");
for (const x of [-3.6, -1.8, 0, 1.8, 3.6]) {
box(0.025, 0.012, 6.25, seamMat, world, x, 1.768, 0);
}
// Chairs around the table
function createChair(x, z, rotation, color) {
const chair = new THREE.Group();
chair.position.set(x, 0, z);
chair.rotation.y = rotation;
world.add(chair);
const chairMat = material(color);
const chairLegMat = material("#87928b");
roundedBox(1.15, 0.18, 1.0, 0.09, chairMat, chair, 0, 0.73, 0);
roundedBox(1.15, 0.98, 0.16, 0.08, chairMat, chair, 0, 1.23, 0.45);
for (const lx of [-0.43, 0.43]) {
for (const lz of [-0.34, 0.34]) {
box(0.1, 0.72, 0.1, chairLegMat, chair, lx, 0.36, lz);
}
}
}
const chairColors = ["#8eb9ae", "#e3a47f", "#9daed0", "#d5bd76"];
const chairXs = [-4.4, -1.5, 1.5, 4.4];
chairXs.forEach((x, i) => {
createChair(x, -4.0, Math.PI, chairColors[i % chairColors.length]);
createChair(x, 4.0, 0, chairColors[(i + 1) % chairColors.length]);
});
// Four equal-group mats
const groupDefs = [
{ letter: "A", x: -3.0, z: -1.55, color: "#79c6a3" },
{ letter: "B", x: 3.0, z: -1.55, color: "#f0a778" },
{ letter: "C", x: -3.0, z: 1.55, color: "#9da9d8" },
{ letter: "D", x: 3.0, z: 1.55, color: "#e1c567" }
];
const stationHitboxes = [];
groupDefs.forEach((group, groupIndex) => {
const darker = material(new THREE.Color(group.color).multiplyScalar(0.78));
const mat = material(group.color);
roundedBox(3.75, 0.055, 1.9, 0.2, darker, world, group.x, 1.785, group.z);
roundedBox(3.55, 0.1, 1.7, 0.18, mat, world, group.x, 1.83, group.z);
// Printed group card
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 128;
const labelCtx = labelCanvas.getContext("2d");
const labelTexture = canvasTexture(labelCanvas);
const labelMesh = new THREE.Mesh(
new THREE.PlaneGeometry(1.55, 0.39),
new THREE.MeshBasicMaterial({
map: labelTexture,
transparent: true,
depthWrite: false,
side: THREE.DoubleSide
})
);
labelMesh.rotation.x = -Math.PI / 2;
labelMesh.position.set(group.x, 1.895, group.z + 0.66);
world.add(labelMesh);
group.labelTexture = labelTexture;
group.labelContext = labelCtx;
const hitbox = new THREE.Mesh(
new THREE.PlaneGeometry(3.55, 1.7),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
side: THREE.DoubleSide
})
);
hitbox.rotation.x = -Math.PI / 2;
hitbox.position.set(group.x, 1.92, group.z);
hitbox.userData.groupIndex = groupIndex;
world.add(hitbox);
stationHitboxes.push(hitbox);
});
function drawGroupLabel(index, count) {
const group = groupDefs[index];
const ctx = group.labelContext;
ctx.clearRect(0, 0, 512, 128);
roundedRectPath(ctx, 7, 7, 498, 114, 26);
ctx.fillStyle = "rgba(255, 251, 240, 0.95)";
ctx.fill();
ctx.lineWidth = 7;
ctx.strokeStyle = group.color;
ctx.stroke();
ctx.fillStyle = "#34443d";
ctx.font = "800 43px system-ui";
ctx.textBaseline = "middle";
ctx.textAlign = "left";
ctx.fillText(`GROUP ${group.letter}`, 30, 65);
ctx.fillStyle = "#63706a";
ctx.font = "750 35px system-ui";
ctx.textAlign = "right";
ctx.fillText(`${count} / 3`, 480, 65);
group.labelTexture.needsUpdate = true;
}
groupDefs.forEach((group, index) => drawGroupLabel(index, 0));
// Supply basket
const basketMat = material("#d7a15e");
const basketDark = material("#b47b40");
roundedBox(2.12, 0.16, 1.38, 0.14, basketDark, world, 0, 1.85, 0);
box(2.12, 0.32, 0.1, basketMat, world, 0, 2.02, 0.64);
box(2.12, 0.32, 0.1, basketMat, world, 0, 2.02, -0.64);
box(0.1, 0.32, 1.2, basketMat, world, -1.01, 2.02, 0);
box(0.1, 0.32, 1.2, basketMat, world, 1.01, 2.02, 0);
// Three-dimensional counting bead board
const beadBoard = new THREE.Group();
beadBoard.position.set(4.95, 0, 0);
world.add(beadBoard);
roundedBox(2.35, 0.12, 1.25, 0.13, material("#f0e7d7"), beadBoard, 0, 1.84, 0);
const allocated3D = [];
const remaining3D = [];
const beadGeometry = new THREE.SphereGeometry(0.085, 10, 6);
const beadStringMat = material("#b7aa95");
for (const z of [-0.25, 0.25]) {
const string = cylinder(0.012, 0.012, 1.96, 8, beadStringMat, beadBoard, 0.03, 1.925, z);
string.rotation.z = Math.PI / 2;
}
for (let i = 0; i < TOTAL_SNACKS; i++) {
const x = -0.88 + i * 0.17;
const allocatedBead = new THREE.Mesh(
beadGeometry,
material(palette.green)
);
allocatedBead.position.set(x, 1.95, -0.25);
allocatedBead.castShadow = true;
beadBoard.add(allocatedBead);
allocated3D.push(allocatedBead);
const remainingBead = new THREE.Mesh(
beadGeometry,
material(palette.orange)
);
remainingBead.position.set(x, 1.95, 0.25);
remainingBead.castShadow = true;
beadBoard.add(remainingBead);
remaining3D.push(remainingBead);
}
// Small classroom props
const booksOnTable = [
material("#e36f60"),
material("#77a8a0"),
material("#e8bd58")
];
booksOnTable.forEach((mat, i) => {
const book = roundedBox(0.9, 0.15, 0.65, 0.04, mat, world, -5.0, 1.88 + i * 0.15, 0.0);
book.rotation.y = i * 0.12;
});
const pencilCup = cylinder(0.28, 0.23, 0.58, 10, material("#7898b2"), world, -5.0, 2.12, -2.55);
const pencilColors = ["#f0ba45", "#e56e5c", "#65a982", "#8a77b3"];
for (let i = 0; i < 5; i++) {
const pencil = cylinder(0.025, 0.025, 0.85, 6, material(pencilColors[i]), world, -5.0 + (i - 2) * 0.075, 2.55, -2.55);
pencil.rotation.z = (i - 2) * 0.08;
}
// Toy snack packages
const snackBodyGeometry = roundedGeometry(0.62, 0.82, 0.34, 0.075);
const snackLabelGeometry = roundedGeometry(0.4, 0.34, 0.035, 0.04);
const snackSealGeometry = roundedGeometry(0.66, 0.105, 0.37, 0.035);
const snackBadgeGeometry = new THREE.CylinderGeometry(0.11, 0.11, 0.035, 12);
const snackColors = ["#ee7769", "#f0bd4f", "#65b393", "#8f8ac5"];
const snacks = [];
const initialTransforms = [];
for (let i = 0; i < TOTAL_SNACKS; i++) {
const snack = new THREE.Group();
const color = snackColors[i % snackColors.length];
const body = new THREE.Mesh(snackBodyGeometry, material(color));
body.castShadow = true;
body.receiveShadow = true;
snack.add(body);
const seal = new THREE.Mesh(snackSealGeometry, material(new THREE.Color(color).multiplyScalar(0.72)));
seal.position.y = 0.445;
seal.castShadow = true;
snack.add(seal);
const label = new THREE.Mesh(snackLabelGeometry, material("#fff8e8"));
label.position.set(0, -0.03, 0.185);
label.castShadow = true;
snack.add(label);
const badge = new THREE.Mesh(snackBadgeGeometry, material("#f3bd43"));
badge.rotation.x = Math.PI / 2;
badge.position.set(0, -0.03, 0.211);
snack.add(badge);
const column = i % 3;
const row = Math.floor(i / 3);
const initial = {
position: new THREE.Vector3(
(column - 1) * 0.62,
2.26 + (row % 2) * 0.06,
-0.51 + row * 0.34
),
rotation: new THREE.Euler(
0,
(column - 1) * 0.1,
(row % 2 ? 1 : -1) * 0.035
)
};
snack.position.copy(initial.position);
snack.rotation.copy(initial.rotation);
world.add(snack);
snacks.push(snack);
initialTransforms.push(initial);
}
function targetTransform(groupIndex, slotIndex) {
const group = groupDefs[groupIndex];
return {
position: new THREE.Vector3(
group.x + (slotIndex - 1) * 1.05,
2.285,
group.z + (slotIndex % 2 === 0 ? -0.055 : 0.055)
),
rotation: new THREE.Euler(
0,
(slotIndex - 1) * 0.13 + (groupIndex - 1.5) * 0.035,
(slotIndex - 1) * 0.025
)
};
}
// HTML bead rows
const allocatedBeadElements = [];
const remainingBeadElements = [];
for (let i = 0; i < TOTAL_SNACKS; i++) {
const a = document.createElement("span");
a.className = "bead allocated";
allocatedBeads.appendChild(a);
allocatedBeadElements.push(a);
const r = document.createElement("span");
r.className = "bead remaining";
remainingBeads.appendChild(r);
remainingBeadElements.push(r);
}
function update3DBeads(remaining) {
allocated3D.forEach((bead, i) => {
bead.material.color.set(i < allocated ? palette.green : palette.emptyBead);
});
remaining3D.forEach((bead, i) => {
bead.material.color.set(i < remaining ? palette.orange : palette.emptyBead);
});
}
function updateUI() {
const remaining = TOTAL_SNACKS - allocated;
const percent = Math.round((allocated / TOTAL_SNACKS) * 100);
allocatedCount.textContent = allocated;
remainingCount.textContent = remaining;
progressFill.style.width = `${percent}%`;
progressText.textContent = `${percent}%`;
statusBadge.textContent = `${groupsCompleted} / 4 groups`;
allocatedBeadElements.forEach((bead, i) => {
const shouldFill = i < allocated;
if (shouldFill && !bead.classList.contains("filled")) {
bead.classList.add("pop");
setTimeout(() => bead.classList.remove("pop"), 180);
}
bead.classList.toggle("filled", shouldFill);
});
remainingBeadElements.forEach((bead, i) => {
const shouldFill = i < remaining;
if (shouldFill && !bead.classList.contains("filled")) {
bead.classList.add("pop");
setTimeout(() => bead.classList.remove("pop"), 180);
}
bead.classList.toggle("filled", shouldFill);
});
groupChipElements.forEach((chip, index) => {
chip.querySelector("b").textContent = `${currentGroupCounts[index]}/3`;
chip.classList.toggle("complete", currentGroupCounts[index] === SNACKS_PER_GROUP);
});
if (allocated === TOTAL_SNACKS) {
statusText.textContent = "Great work! Every group has exactly 3 snacks.";
} else if (busy) {
const letter = String.fromCharCode(65 + groupsCompleted);
statusText.textContent = `Sharing group ${letter}. Watch the beads count each snack.`;
} else if (groupBusy) {
statusText.textContent = "Placing snacks into the next group…";
} else {
const letter = String.fromCharCode(65 + groupsCompleted);
statusText.textContent = `Group ${letter} is ready. Tap its mat or use the button.`;
}
primaryButton.disabled = busy || groupBusy;
primaryIcon.textContent = busy || groupBusy ? "•••" : "↻";
primaryLabel.textContent =
busy || groupBusy
? "Sharing snacks…"
: allocated === TOTAL_SNACKS
? "Replay sharing"
: "Distribute all";
update3DBeads(remaining);
}
// Tween system, implemented without external animation utilities
const tweens = [];
function tweenSnack(snack, target, delay = 0) {
tweens.push({
snack,
fromPosition: snack.position.clone(),
toPosition: target.position.clone(),
fromRotation: snack.rotation.clone(),
toRotation: target.rotation.clone(),
start: performance.now() + delay,
duration: 620,
arc: 1.05
});
}
function updateTweens(now) {
for (let i = tweens.length - 1; i >= 0; i--) {
const tween = tweens[i];
if (now < tween.start) continue;
const raw = THREE.MathUtils.clamp(
(now - tween.start) / tween.duration,
0,
1
);
const eased =
raw < 0.5
? 4 * raw * raw * raw
: 1 - Math.pow(-2 * raw + 2, 3) / 2;
tween.snack.position.lerpVectors(
tween.fromPosition,
tween.toPosition,
eased
);
tween.snack.position.y += Math.sin(raw * Math.PI) * tween.arc;
tween.snack.rotation.set(
THREE.MathUtils.lerp(tween.fromRotation.x, tween.toRotation.x, eased),
THREE.MathUtils.lerp(tween.fromRotation.y, tween.toRotation.y, eased),
THREE.MathUtils.lerp(tween.fromRotation.z, tween.toRotation.z, eased)
);
if (raw >= 1) {
tween.snack.position.copy(tween.toPosition);
tween.snack.rotation.copy(tween.toRotation);
tweens.splice(i, 1);
}
}
}
const sleep = (milliseconds) =>
new Promise((resolve) => setTimeout(resolve, milliseconds));
async function fillGroup(groupIndex, token = session) {
if (
groupBusy ||
groupIndex !== groupsCompleted ||
groupsCompleted >= GROUP_COUNT
) {
return false;
}
groupBusy = true;
updateUI();
for (let slot = 0; slot < SNACKS_PER_GROUP; slot++) {
if (token !== session) return false;
const snackIndex = groupIndex * SNACKS_PER_GROUP + slot;
const snack = snacks[snackIndex];
tweenSnack(snack, targetTransform(groupIndex, slot));
allocated += 1;
currentGroupCounts[groupIndex] += 1;
drawGroupLabel(groupIndex, currentGroupCounts[groupIndex]);
updateUI();
await sleep(240);
}
await sleep(500);
if (token !== session) {
groupBusy = false;
updateUI();
return false;
}
groupsCompleted = groupIndex + 1;
groupBusy = false;
updateUI();
return true;
}
async function distributeAll() {
if (busy || groupBusy) return;
const token = session;
busy = true;
updateUI();
for (let groupIndex = 0; groupIndex < GROUP_COUNT; groupIndex++) {
const completed = await fillGroup(groupIndex, token);
if (!completed || token !== session) return;
}
busy = false;
updateUI();
}
function resetScene() {
session += 1;
busy = false;
groupBusy = false;
allocated = 0;
groupsCompleted = 0;
currentGroupCounts = [0, 0, 0, 0];
tweens.length = 0;
snacks.forEach((snack, index) => {
snack.position.copy(initialTransforms[index].position);
snack.rotation.copy(initialTransforms[index].rotation);
});
groupDefs.forEach((group, index) => drawGroupLabel(index, 0));
updateUI();
}
primaryButton.addEventListener("click", () => {
if (busy || groupBusy) return;
resetScene();
distributeAll();
});
resetButton.addEventListener("click", resetScene);
// Direct camera controls: orbit, zoom, and touch drag
const orbitTarget = new THREE.Vector3(0, 1.25, 0);
const desiredOrbit = {
theta: 0.58,
phi: 1.03,
radius: 16.4
};
const currentOrbit = {
theta: desiredOrbit.theta,
phi: desiredOrbit.phi,
radius: desiredOrbit.radius
};
let dragging = false;
let pointerStart = { x: 0, y: 0 };
let previousPointer = { x: 0, y: 0 };
let pointerTravel = 0;
function setPointerFromEvent(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
pointerTravel = 0;
pointerStart = { x: event.clientX, y: event.clientY };
previousPointer = { ...pointerStart };
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousPointer.x;
const dy = event.clientY - previousPointer.y;
desiredOrbit.theta = THREE.MathUtils.clamp(
desiredOrbit.theta - dx * 0.0055,
-1.08,
1.25
);
desiredOrbit.phi = THREE.MathUtils.clamp(
desiredOrbit.phi - dy * 0.0045,
0.48,
1.34
);
pointerTravel += Math.abs(dx) + Math.abs(dy);
previousPointer = { x: event.clientX, y: event.clientY };
});
canvas.addEventListener("pointerup", (event) => {
if (!dragging) return;
dragging = false;
canvas.style.cursor = "grab";
if (
pointerTravel < 6 &&
!busy &&
!groupBusy &&
groupsCompleted < GROUP_COUNT
) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(stationHitboxes, false);
if (hits.length) {
const selectedGroup = hits[0].object.userData.groupIndex;
if (selectedGroup === groupsCompleted) {
fillGroup(selectedGroup);
}
}
}
});
canvas.addEventListener("pointercancel", () => {
dragging = false;
canvas.style.cursor = "grab";
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredOrbit.radius = THREE.MathUtils.clamp(
desiredOrbit.radius + event.deltaY * 0.012,
10.5,
23
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
desiredOrbit.theta = 0.58;
desiredOrbit.phi = 1.03;
desiredOrbit.radius = 16.4;
});
canvas.style.cursor = "grab";
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
}
addEventListener("resize", resize);
function animate() {
requestAnimationFrame(animate);
const damping = 0.085;
currentOrbit.theta += (desiredOrbit.theta - currentOrbit.theta) * damping;
currentOrbit.phi += (desiredOrbit.phi - currentOrbit.phi) * damping;
currentOrbit.radius += (desiredOrbit.radius - currentOrbit.radius) * damping;
const sinPhi = Math.sin(currentOrbit.phi);
camera.position.set(
orbitTarget.x + currentOrbit.radius * sinPhi * Math.sin(currentOrbit.theta),
orbitTarget.y + currentOrbit.radius * Math.cos(currentOrbit.phi),
orbitTarget.z + currentOrbit.radius * sinPhi * Math.cos(currentOrbit.theta)
);
camera.lookAt(orbitTarget);
updateTweens(performance.now());
renderer.render(scene, camera);
}
updateUI();
animate();
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 500);
});
// Begin with a short demonstration, then leave the scene fully distributed.
setTimeout(() => {
if (allocated === 0 && !busy && !groupBusy) {
distributeAll();
}
}, 650);
</script>
</body>
</html>
```