Prompt specification
Build or customize the Three.js 3D scene "Layered ingredients classification 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: Layered ingredients classification game
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/layered-ingredients-classification-game-x0128
Scene Description Prompt:
Please make a low-poly Three.js scene in one standalone HTML file using cdnjs. Create a cooking classroom with onion, cabbage, and citrus sections; users match whole objects to their cut surfaces, with aligned exploded views revealing each structural relationship.
Working Three.js Starter (layered-ingredients-classification-game-x0128):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>The Structure Kitchen</title>
<style>
:root {
--ink: #24332d;
--muted: #65736c;
--cream: #f4efe4;
--paper: rgba(255, 252, 243, 0.91);
--line: rgba(36, 51, 45, 0.14);
--green: #2d7057;
--orange: #d87935;
--yellow: #d7a642;
--shadow: 0 18px 50px rgba(38, 46, 40, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
color: var(--ink);
background: var(--cream);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 58% 42%, transparent 35%, rgba(73, 65, 49, 0.08) 100%),
linear-gradient(180deg, rgba(255, 255, 255, 0.12), transparent 24%);
}
.topbar {
position: fixed;
z-index: 20;
top: 0;
left: 0;
right: 0;
display: flex;
align-items: center;
justify-content: space-between;
padding: 22px 26px;
pointer-events: none;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
}
.brand-mark {
display: grid;
place-items: center;
width: 42px;
height: 42px;
border-radius: 13px;
color: #fffaf0;
background: var(--ink);
box-shadow: 0 8px 22px rgba(36, 51, 45, 0.22);
font-size: 13px;
font-weight: 800;
letter-spacing: -0.04em;
}
.brand-name {
font-size: 13px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.brand-sub {
margin-top: 3px;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.04em;
}
.progress {
min-width: 164px;
padding: 11px 14px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 16px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(15px);
pointer-events: auto;
}
.progress-head {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.progress-track {
height: 5px;
overflow: hidden;
border-radius: 99px;
background: rgba(36, 51, 45, 0.1);
}
#progressFill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #2d7057, #79a568);
transition: width 400ms ease;
}
.lesson {
position: fixed;
z-index: 15;
top: 98px;
left: 26px;
width: 310px;
padding: 23px;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 24px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
}
.lesson-index {
color: var(--green);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.lesson h1 {
margin: 8px 0 2px;
font-family: Georgia, "Times New Roman", serif;
font-size: 35px;
font-weight: 500;
letter-spacing: -0.035em;
}
.latin {
margin: 0 0 18px;
color: var(--muted);
font-family: Georgia, "Times New Roman", serif;
font-size: 14px;
font-style: italic;
}
.task-card {
padding: 15px;
border: 1px solid var(--line);
border-radius: 15px;
background: rgba(255, 255, 255, 0.48);
}
.task-title {
display: flex;
align-items: center;
gap: 7px;
margin-bottom: 7px;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.task-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--yellow);
box-shadow: 0 0 0 4px rgba(215, 166, 66, 0.13);
}
.task-card p {
margin: 0;
color: #53615a;
font-size: 13px;
line-height: 1.55;
}
.alignment-status {
display: inline-flex;
align-items: center;
gap: 7px;
margin: 14px 0 18px;
padding: 7px 10px;
border-radius: 99px;
color: #6e5b2b;
background: rgba(215, 166, 66, 0.14);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.alignment-status::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--yellow);
}
.alignment-status.complete {
color: #245d49;
background: rgba(45, 112, 87, 0.12);
}
.alignment-status.complete::before {
background: var(--green);
}
.facts-title {
margin: 0 0 9px;
color: var(--muted);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.fact {
display: grid;
grid-template-columns: 10px 1fr;
gap: 10px;
padding: 10px 0;
border-top: 1px solid var(--line);
}
.fact:first-child {
border-top: 0;
}
.fact-dot {
width: 7px;
height: 7px;
margin-top: 4px;
border-radius: 2px;
background: #c7c9c2;
transform: rotate(45deg);
}
.fact strong {
display: block;
margin-bottom: 3px;
font-size: 12px;
}
.fact span {
display: block;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.fact.locked strong,
.fact.locked span {
opacity: 0.48;
}
.tip {
display: flex;
gap: 10px;
margin-top: 15px;
padding-top: 15px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.tip-icon {
flex: 0 0 auto;
display: grid;
place-items: center;
width: 25px;
height: 25px;
border-radius: 8px;
color: var(--green);
background: rgba(45, 112, 87, 0.1);
font-weight: 900;
}
#labels {
position: fixed;
z-index: 8;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
top: 0;
left: 0;
padding: 7px 10px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 9px;
color: var(--ink);
background: rgba(255, 252, 243, 0.88);
box-shadow: 0 7px 22px rgba(36, 51, 45, 0.12);
backdrop-filter: blur(8px);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
white-space: nowrap;
opacity: 0;
transition: opacity 180ms ease, background 180ms ease;
}
.world-label.station {
padding: 9px 12px;
border-radius: 12px;
background: rgba(36, 51, 45, 0.88);
color: #fffaf0;
}
.world-label.part {
border-radius: 99px;
color: #244f40;
background: rgba(239, 249, 235, 0.94);
}
.bottom-nav {
position: fixed;
z-index: 20;
bottom: 26px;
left: 50%;
display: flex;
gap: 7px;
padding: 7px;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 19px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
transform: translateX(-50%);
}
.section-button {
display: flex;
align-items: center;
gap: 9px;
min-width: 132px;
padding: 11px 14px;
border: 0;
border-radius: 13px;
color: var(--muted);
background: transparent;
cursor: pointer;
transition: color 180ms ease, background 180ms ease, transform 180ms ease;
}
.section-button:hover {
color: var(--ink);
background: rgba(36, 51, 45, 0.055);
transform: translateY(-1px);
}
.section-button.active {
color: #fffaf0;
background: var(--ink);
}
.section-swatch {
width: 12px;
height: 12px;
border-radius: 4px;
background: var(--swatch);
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}
.section-copy {
text-align: left;
}
.section-copy strong {
display: block;
font-size: 12px;
}
.section-copy small {
display: block;
margin-top: 2px;
font-size: 9px;
opacity: 0.68;
}
.toolbar {
position: fixed;
z-index: 20;
right: 26px;
bottom: 27px;
display: flex;
gap: 8px;
}
.tool-button {
padding: 11px 14px;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 13px;
color: var(--ink);
background: var(--paper);
box-shadow: 0 8px 25px rgba(36, 51, 45, 0.1);
backdrop-filter: blur(12px);
cursor: pointer;
font-size: 11px;
font-weight: 800;
}
.tool-button:hover,
.tool-button.active {
color: #fff;
background: var(--green);
}
.gesture-hint {
position: fixed;
z-index: 12;
right: 28px;
top: 102px;
display: flex;
align-items: center;
gap: 10px;
max-width: 245px;
padding: 11px 13px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 15px;
color: var(--muted);
background: rgba(255, 252, 243, 0.8);
box-shadow: 0 10px 30px rgba(36, 51, 45, 0.09);
backdrop-filter: blur(12px);
font-size: 10px;
line-height: 1.4;
}
.gesture-icon {
flex: 0 0 auto;
font-size: 18px;
}
#toast {
position: fixed;
z-index: 30;
left: 50%;
top: 91px;
padding: 12px 18px;
border-radius: 14px;
color: #fffaf0;
background: rgba(36, 51, 45, 0.94);
box-shadow: var(--shadow);
font-size: 12px;
font-weight: 700;
opacity: 0;
pointer-events: none;
transform: translate(-50%, -10px);
transition: opacity 180ms ease, transform 180ms ease;
}
#toast.visible {
opacity: 1;
transform: translate(-50%, 0);
}
#toast.error {
background: #9b553d;
}
@media (max-width: 980px) {
.lesson {
top: 88px;
left: 16px;
width: 270px;
padding: 18px;
}
.lesson h1 {
font-size: 29px;
}
.gesture-hint {
display: none;
}
.section-button {
min-width: 105px;
}
.section-copy small {
display: none;
}
.toolbar {
right: 16px;
bottom: 100px;
}
}
@media (max-width: 700px) {
.topbar {
padding: 14px;
}
.brand-sub {
display: none;
}
.progress {
min-width: 130px;
}
.lesson {
top: 76px;
width: calc(100% - 28px);
padding: 15px;
}
.lesson h1 {
margin-top: 4px;
font-size: 25px;
}
.latin,
.facts-title,
#factList,
.tip {
display: none;
}
.task-card {
margin-top: 12px;
padding: 11px;
}
.alignment-status {
margin-bottom: 0;
}
.bottom-nav {
right: 10px;
bottom: 10px;
left: 10px;
justify-content: stretch;
transform: none;
}
.section-button {
flex: 1;
justify-content: center;
min-width: 0;
padding: 10px 8px;
}
.section-copy strong {
font-size: 10px;
}
.toolbar {
right: 12px;
bottom: 87px;
}
.tool-button {
padding: 9px 10px;
font-size: 9px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="vignette"></div>
<header class="topbar">
<div class="brand">
<div class="brand-mark">CL</div>
<div>
<div class="brand-name">The Structure Kitchen</div>
<div class="brand-sub">A low-poly culinary anatomy lab</div>
</div>
</div>
<div class="progress">
<div class="progress-head">
<span>Aligned structures</span>
<span id="progressText">0 / 3</span>
</div>
<div class="progress-track">
<div id="progressFill"></div>
</div>
</div>
</header>
<aside class="lesson">
<div class="lesson-index" id="lessonIndex">Section 01 · Onion</div>
<h1 id="lessonTitle">Onion</h1>
<p class="latin" id="lessonLatin">Allium cepa</p>
<div class="task-card">
<div class="task-title">
<span class="task-dot"></span>
<span id="taskTitle">Your task</span>
</div>
<p id="taskText">
Drag the whole onion onto its concentric cut face.
</p>
</div>
<div class="alignment-status" id="alignmentStatus">Not aligned</div>
<div class="facts-title">Structural relationship</div>
<div id="factList"></div>
<div class="tip">
<div class="tip-icon">↔</div>
<div>
Drag the ingredient itself. Use a section button to inspect its station,
or choose Classroom View to see all three.
</div>
</div>
</aside>
<div id="labels"></div>
<div class="gesture-hint">
<span class="gesture-icon">↗</span>
<span>Drag a whole ingredient onto a cut face. Drag the background to orbit; scroll to zoom.</span>
</div>
<div id="toast" role="status" aria-live="polite"></div>
<nav class="bottom-nav" aria-label="Kitchen sections">
<button class="section-button active" data-section="onion" style="--swatch:#c9a34c">
<span class="section-swatch"></span>
<span class="section-copy">
<strong>Onion</strong>
<small>Concentric layers</small>
</span>
</button>
<button class="section-button" data-section="cabbage" style="--swatch:#79a568">
<span class="section-swatch"></span>
<span class="section-copy">
<strong>Cabbage</strong>
<small>Nested leaves</small>
</span>
</button>
<button class="section-button" data-section="citrus" style="--swatch:#df873c">
<span class="section-swatch"></span>
<span class="section-copy">
<strong>Citrus</strong>
<small>Radial segments</small>
</span>
</button>
</nav>
<div class="toolbar">
<button class="tool-button" id="classroomView">Classroom view</button>
<button class="tool-button" id="resetButton">Reset</button>
</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 container = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.75));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8dfcf);
scene.fog = new THREE.Fog(0xe8dfcf, 19, 34);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
70
);
const state = {
active: "onion",
overview: false,
focus: new THREE.Vector3(-5.2, 2.25, 0.15),
focusGoal: new THREE.Vector3(-5.2, 2.25, 0.15),
radius: 9.2,
radiusGoal: 9.2,
phi: 0.96,
phiGoal: 0.96,
theta: 0,
thetaGoal: 0
};
const clock = new THREE.Clock();
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const tweens = [];
const labels = [];
const wholePick = [];
const sections = [];
const materials = {
floor: new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.94
}),
counter: new THREE.MeshStandardMaterial({
color: 0xe2d2b7,
roughness: 0.83
}),
cabinet: new THREE.MeshStandardMaterial({
color: 0x527568,
roughness: 0.8
}),
cabinetDark: new THREE.MeshStandardMaterial({
color: 0x355b50,
roughness: 0.8
}),
wall: new THREE.MeshStandardMaterial({
color: 0xf0e8d9,
roughness: 0.92
}),
metal: new THREE.MeshStandardMaterial({
color: 0x89958d,
roughness: 0.42,
metalness: 0.55,
flatShading: true
}),
darkMetal: new THREE.MeshStandardMaterial({
color: 0x3d4843,
roughness: 0.45,
metalness: 0.45,
flatShading: true
}),
wood: new THREE.MeshStandardMaterial({
color: 0xc99757,
roughness: 0.86
}),
paper: new THREE.MeshStandardMaterial({
color: 0xfaf2df,
roughness: 0.9
}),
pot: new THREE.MeshStandardMaterial({
color: 0xe4dfce,
roughness: 0.73
})
};
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0,
flatShading: true,
...options
});
}
function mesh(geometry, mat, x = 0, y = 0, z = 0, parent = scene) {
const object = new THREE.Mesh(geometry, mat);
object.position.set(x, y, z);
object.castShadow = true;
object.receiveShadow = true;
parent.add(object);
return object;
}
function box(w, h, d, mat, x, y, z, parent = scene) {
return mesh(new THREE.BoxGeometry(w, h, d), mat, x, y, z, parent);
}
function cylinder(rt, rb, h, segments, mat, x, y, z, parent = scene) {
return mesh(
new THREE.CylinderGeometry(rt, rb, h, segments),
mat,
x,
y,
z,
parent
);
}
function setShadows(object) {
object.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
}
function makeTileTexture() {
const canvas = document.createElement("canvas");
canvas.width = canvas.height = 512;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#d8d1bf";
ctx.fillRect(0, 0, 512, 512);
for (let y = 0; y < 4; y++) {
for (let x = 0; x < 4; x++) {
const offset = y % 2 ? 32 : 0;
const px = x * 128 + offset;
const py = y * 128;
ctx.fillStyle = (x + y) % 3 === 0 ? "#e9e2d1" : "#eee8d9";
ctx.fillRect(px + 3, py + 3, 122, 122);
ctx.strokeStyle = "rgba(96, 91, 77, .16)";
ctx.strokeRect(px + 4, py + 4, 120, 120);
}
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(5, 3);
return texture;
}
function makeWoodTexture() {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 256;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#c9985b";
ctx.fillRect(0, 0, 512, 256);
for (let i = 0; i < 90; i++) {
ctx.strokeStyle = `rgba(92, 57, 27, ${0.035 + Math.random() * 0.07})`;
ctx.lineWidth = 1 + Math.random() * 2;
const y = Math.random() * 256;
ctx.beginPath();
ctx.moveTo(0, y);
ctx.bezierCurveTo(
140,
y + Math.random() * 18 - 9,
350,
y + Math.random() * 18 - 9,
512,
y + Math.random() * 9 - 4
);
ctx.stroke();
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
function makeChalkTexture() {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 512;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#29463d";
ctx.fillRect(0, 0, 1024, 512);
ctx.strokeStyle = "rgba(235, 226, 198, .55)";
ctx.lineWidth = 7;
ctx.strokeRect(22, 22, 980, 468);
ctx.textAlign = "center";
ctx.fillStyle = "#f4ead2";
ctx.font = "700 66px Georgia";
ctx.fillText("THE STRUCTURE KITCHEN", 512, 165);
ctx.font = "italic 35px Georgia";
ctx.fillText("Whole → Cut → Relationship", 512, 235);
ctx.strokeStyle = "rgba(235, 226, 198, .35)";
ctx.lineWidth = 3;
ctx.beginPath();
ctx.moveTo(210, 285);
ctx.lineTo(814, 285);
ctx.stroke();
ctx.font = "700 27px sans-serif";
ctx.fillText("OBSERVE • MATCH • REVEAL", 512, 350);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
const tileTexture = makeTileTexture();
const woodTexture = makeWoodTexture();
materials.floor.map = tileTexture;
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xfff5dd, 0x8a8170, 2.1));
const sunlight = new THREE.DirectionalLight(0xffedc6, 3.1);
sunlight.position.set(5, 11, 8);
sunlight.castShadow = true;
sunlight.shadow.mapSize.set(2048, 2048);
sunlight.shadow.camera.left = -12;
sunlight.shadow.camera.right = 12;
sunlight.shadow.camera.top = 10;
sunlight.shadow.camera.bottom = -7;
sunlight.shadow.camera.near = 1;
sunlight.shadow.camera.far = 30;
sunlight.shadow.bias = -0.00025;
scene.add(sunlight);
const fill = new THREE.DirectionalLight(0xbcd9d4, 1.25);
fill.position.set(-8, 5, 4);
scene.add(fill);
// -------------------------------------------------------------------------
// Classroom architecture
// -------------------------------------------------------------------------
const room = new THREE.Group();
scene.add(room);
const floor = box(29, 0.2, 19, materials.floor, 0, -0.1, 1.5, room);
box(29, 10, 0.25, materials.wall, 0, 4.9, -3.8, room);
box(0.25, 10, 19, materials.wall, -14.4, 4.9, 1.5, room);
// Backsplash tile band.
const tileBand = box(29, 2.15, 0.08, material(0xdad7c7), 0, 2.73, -3.64, room);
tileBand.material.map = tileTexture;
tileBand.material.map.repeat.set(8, 1);
// Main workbench.
box(16.7, 0.28, 4.0, materials.counter, 0, 1.5, -0.35, room);
box(16.25, 1.22, 3.65, materials.cabinet, 0, 0.78, -0.38, room);
for (let x = -7.25; x <= 7.25; x += 2.45) {
box(2.25, 1.03, 0.12, materials.cabinetDark, x, 0.79, 1.48, room);
box(0.42, 0.055, 0.08, materials.metal, x, 1.05, 1.57, room);
}
// Toe kick.
box(16.1, 0.18, 0.14, materials.darkMetal, 0, 0.16, 1.54, room);
// Counter edge.
box(16.76, 0.07, 4.05, materials.wood, 0, 1.35, -0.35, room);
// Wall shelf.
box(12.8, 0.18, 0.8, materials.wood, 0, 4.23, -3.31, room);
const bowlMat = material(0xd8d0bd);
const bowlGreen = material(0x759987);
const jarMat = material(0xc7bda7);
for (let i = 0; i < 7; i++) {
const x = -5.8 + i * 1.85;
const bowl = cylinder(0.4, 0.25, 0.26, 12, i % 2 ? bowlMat : bowlGreen, x, 4.45, -3.22, room);
bowl.rotation.x = Math.PI;
}
for (let i = 0; i < 4; i++) {
cylinder(0.22, 0.25, 0.58, 10, jarMat, -2.8 + i * 1.8, 4.61, -3.26, room);
cylinder(0.24, 0.24, 0.08, 10, materials.wood, -2.8 + i * 1.8, 4.94, -3.26, room);
}
// Chalkboard.
box(5.9, 2.55, 0.16, materials.wood, 0, 6.18, -3.61, room);
const chalkboard = mesh(
new THREE.PlaneGeometry(5.65, 2.3),
new THREE.MeshStandardMaterial({
map: makeChalkTexture(),
roughness: 0.95
}),
0,
6.18,
-3.51,
room
);
chalkboard.castShadow = false;
// Utensil rail and hanging tools.
const rail = cylinder(0.055, 0.055, 10.5, 10, materials.metal, 0, 5.68, -3.37, room);
rail.rotation.z = Math.PI / 2;
for (let i = 0; i < 7; i++) {
const x = -4.5 + i * 1.45;
const handle = cylinder(0.035, 0.035, 0.72, 8, materials.metal, x, 5.33, -3.25, room);
if (i % 3 === 0) {
const pan = cylinder(0.32, 0.26, 0.08, 12, materials.darkMetal, x, 4.87, -3.23, room);
pan.rotation.x = Math.PI / 2;
} else {
const spoon = mesh(
new THREE.SphereGeometry(0.13, 8, 6),
materials.metal,
x,
4.9,
-3.23,
room
);
spoon.scale.set(0.7, 1.35, 0.25);
}
}
// Pendant lights.
for (const x of [-5.2, 0, 5.2]) {
cylinder(0.025, 0.025, 1.4, 8, materials.darkMetal, x, 8.05, -0.1, room);
const shade = mesh(
new THREE.ConeGeometry(0.58, 0.48, 12, 1, true),
material(0xe4c776, { side: THREE.DoubleSide }),
x,
7.23,
-0.1,
room
);
shade.rotation.x = Math.PI;
const bulb = mesh(
new THREE.SphereGeometry(0.16, 10, 8),
material(0xffe9a5, {
emissive: 0xffb94e,
emissiveIntensity: 1.8
}),
x,
7.02,
-0.1,
room
);
const light = new THREE.PointLight(0xffd58d, 10, 8, 2);
light.position.set(x, 6.95, -0.1);
room.add(light);
}
// Small decorative ingredient bowls on the counter.
for (const [x, z, color] of [
[-7.4, 0.65, 0xc29b49],
[7.35, 0.8, 0xdf873c],
[-7.25, -1.25, 0x719460]
]) {
const bowl = cylinder(0.46, 0.31, 0.28, 12, material(0xe7dfcd), x, 1.8, z, room);
bowl.rotation.x = Math.PI;
const produce = mesh(
new THREE.IcosahedronGeometry(0.27, 1),
material(color),
x,
1.93,
z,
room
);
produce.scale.y = 0.8;
}
// -------------------------------------------------------------------------
// Whole ingredients
// -------------------------------------------------------------------------
function createOnion() {
const group = new THREE.Group();
const skin = material(0xb68c42);
const skinLight = material(0xd0aa61);
const root = material(0x79613b);
const body = mesh(
new THREE.SphereGeometry(0.78, 10, 7),
skin,
0,
0,
0,
group
);
body.scale.set(0.9, 1.04, 0.9);
const shoulder = mesh(
new THREE.SphereGeometry(0.6, 10, 6),
skinLight,
0,
0.1,
0,
group
);
shoulder.scale.set(0.9, 0.72, 0.9);
const neck = mesh(
new THREE.ConeGeometry(0.16, 0.4, 7),
skin,
0,
0.91,
0,
group
);
for (let i = 0; i < 5; i++) {
const a = (i / 5) * Math.PI * 2;
const r = mesh(
new THREE.ConeGeometry(0.035, 0.22, 5),
root,
Math.cos(a) * 0.22,
-0.84,
Math.sin(a) * 0.22,
group
);
r.rotation.z = Math.PI;
r.rotation.y = -a;
}
return group;
}
function createCabbage() {
const group = new THREE.Group();
const body = mesh(
new THREE.SphereGeometry(0.83, 12, 8),
material(0x8ead70),
0,
0,
0,
group
);
body.scale.set(1, 0.9, 1);
const leafColors = [0x6f9659, 0x9cbd7b, 0x789f62, 0xaac487];
const leafGeometry = new THREE.SphereGeometry(0.37, 7, 5);
for (let i = 0; i < 7; i++) {
const a = (i / 7) * Math.PI * 2;
const leaf = mesh(
leafGeometry,
material(leafColors[i % leafColors.length]),
Math.cos(a) * 0.52,
0.16 + Math.sin(i * 1.7) * 0.16,
Math.sin(a) * 0.52,
group
);
leaf.scale.set(0.62, 1.08, 0.2);
leaf.rotation.y = -a + Math.PI / 2;
leaf.rotation.z = Math.sin(i) * 0.18;
}
const vein = mesh(
new THREE.BoxGeometry(0.055, 0.045, 0.82),
material(0xd7d9a9),
0,
0.63,
0.08,
group
);
vein.rotation.x = 0.08;
return group;
}
function createCitrus() {
const group = new THREE.Group();
const body = mesh(
new THREE.SphereGeometry(0.78, 14, 10),
material(0xe28a35),
0,
0,
0,
group
);
body.scale.set(1, 0.97, 1);
const dimpleMat = material(0xd97b2d);
for (let i = 0; i < 10; i++) {
const a = i * 2.399;
const y = 1 - (i / 9) * 2;
const r = Math.sqrt(Math.max(0, 1 - y * y));
const dimple = mesh(
new THREE.IcosahedronGeometry(0.045, 0),
dimpleMat,
Math.cos(a) * r * 0.77,
y * 0.74,
Math.sin(a) * r * 0.77,
group
);
dimple.scale.set(1, 0.6, 1);
}
const stem = cylinder(
0.065,
0.09,
0.2,
7,
material(0x6f7541),
0,
0.84,
0,
group
);
const leaf = mesh(
new THREE.SphereGeometry(0.27, 7, 5),
material(0x4f814e),
0.25,
0.77,
0.02,
group
);
leaf.scale.set(1.25, 0.13, 0.55);
leaf.rotation.z = -0.35;
return group;
}
// -------------------------------------------------------------------------
// Cut surfaces
// -------------------------------------------------------------------------
function registerLabel(section, anchor, text, kind, offset = new THREE.Vector3()) {
anchor.position.copy(offset);
const element = document.createElement("div");
element.className = `world-label ${kind}`;
element.textContent = text;
document.querySelector("#labels").appendChild(element);
labels.push({
element,
anchor,
section,
kind,
text
});
}
function registerPart(section, object, base, exploded, label, labelOffset) {
object.position.copy(base);
const part = {
object,
base: base.clone(),
exploded: exploded.clone()
};
section.parts.push(part);
if (label) {
registerLabel(section, object, label, "part", labelOffset);
}
return part;
}
function createOnionTarget(section) {
const target = new THREE.Group();
const colors = [0x9d7738, 0xd7b861, 0xf0d98d, 0xf5e6b6, 0xfff6d7];
const radii = [0.84, 0.7, 0.57, 0.43, 0.27];
for (let i = 0; i < radii.length; i++) {
const layer = mesh(
new THREE.CylinderGeometry(radii[i], radii[i], 0.055, 14),
material(colors[i]),
0,
0,
0,
target
);
const base = new THREE.Vector3(0, 0.028 + i * 0.009, 0);
const exploded = new THREE.Vector3(0, 0.16 + i * 0.25, 0);
registerPart(
section,
layer,
base,
exploded,
i === 0 ? "Papery skin" : i === 2 ? "Flesh rings" : i === 4 ? "Central core" : null,
new THREE.Vector3(0, 0.09, 0)
);
}
for (let i = 0; i < 5; i++) {
const a = (i / 5) * Math.PI * 2;
const root = box(
0.13,
0.012,
0.025,
material(0x9b8356),
Math.cos(a) * 0.1,
0.085,
Math.sin(a) * 0.1,
target
);
root.rotation.y = -a;
}
return target;
}
function createCabbageTarget(section) {
const target = new THREE.Group();
const colors = [0x507c45, 0x6f9758, 0x91b474, 0xb7ce91, 0xdbe3b4];
const radii = [0.88, 0.73, 0.58, 0.43, 0.27];
for (let i = 0; i < radii.length; i++) {
const layer = mesh(
new THREE.CylinderGeometry(
radii[i],
radii[i] * 0.9,
0.1,
10,
1,
false,
0,
Math.PI / 2
),
material(colors[i]),
0,
0,
0,
target
);
const base = new THREE.Vector3(0, 0.05 + i * 0.012, 0);
const exploded = new THREE.Vector3(
(i - 2) * 0.14,
0.14 + i * 0.2,
0
);
registerPart(
section,
layer,
base,
exploded,
i === 0 ? "Outer leaves" : i === 2 ? "Inner leaves" : i === 4 ? "Heart" : null,
new THREE.Vector3(0, 0.11, 0)
);
const vein = box(
radii[i] * 0.62,
0.012,
0.022,
material(i > 2 ? 0xe9edca : 0xaecb85),
radii[i] * 0.33,
0.065,
0,
layer
);
vein.rotation.y = -0.43;
}
return target;
}
function citrusSegmentGeometry(innerRadius, outerRadius, angleStart, angleEnd) {
const shape = new THREE.Shape();
const steps = 6;
shape.moveTo(
Math.cos(angleStart) * innerRadius,
Math.sin(angleStart) * innerRadius
);
for (let i = 0; i <= steps; i++) {
const a = THREE.MathUtils.lerp(angleStart, angleEnd, i / steps);
shape.lineTo(Math.cos(a) * outerRadius, Math.sin(a) * outerRadius);
}
shape.lineTo(
Math.cos(angleEnd) * innerRadius,
Math.sin(angleEnd) * innerRadius
);
shape.closePath();
return new THREE.ExtrudeGeometry(shape, {
depth: 0.045,
bevelEnabled: false,
curveSegments: 1
});
}
function createCitrusTarget(section) {
const target = new THREE.Group();
const peel = mesh(
new THREE.RingGeometry(0.69, 0.86, 18),
material(0xdc792d, { side: THREE.DoubleSide }),
0,
0,
0,
target
);
peel.rotation.x = -Math.PI / 2;
registerPart(
section,
peel,
new THREE.Vector3(0, 0.035, 0),
new THREE.Vector3(0, 1.03, 0),
"Peel",
new THREE.Vector3(0, 0.05, 0)
);
const pith = mesh(
new THREE.CircleGeometry(0.69, 24),
material(0xf4e4b9, { side: THREE.DoubleSide }),
0,
0,
0,
target
);
pith.rotation.x = -Math.PI / 2;
registerPart(
section,
pith,
new THREE.Vector3(0, 0.048, 0),
new THREE.Vector3(0, 0.58, 0),
"Pith",
new THREE.Vector3(0, 0.04, 0)
);
const segmentCount = 10;
for (let i = 0; i < segmentCount; i++) {
const a0 = (i / segmentCount) * Math.PI * 2 + 0.035;
const a1 = ((i + 1) / segmentCount) * Math.PI * 2 - 0.035;
const segment = mesh(
citrusSegmentGeometry(0.08, 0.64, a0, a1),
material(i % 2 ? 0xf2a13e : 0xeb9131),
0,
0,
0,
target
);
segment.rotation.x = -Math.PI / 2;
const mid = (a0 + a1) / 2;
const outward = new THREE.Vector3(Math.cos(mid), 0, -Math.sin(mid)).multiplyScalar(0.17);
registerPart(
section,
segment,
new THREE.Vector3(0, 0.068, 0),
new THREE.Vector3(outward.x, 0.17, outward.z),
i === 0 ? "Juice sacs" : null,
new THREE.Vector3(0.2, 0.08, -0.2)
);
}
return target;
}
// -------------------------------------------------------------------------
// Stations
// -------------------------------------------------------------------------
const sectionDefinitions = [
{
id: "onion",
name: "Onion",
latin: "Allium cepa",
x: -5.2,
task: "Drag the whole onion onto its concentric cut face.",
completedTask: "Aligned. The concentric layers now reveal how the bulb grows around its core.",
facts: [
["Papery skin", "A dry outer covering protects the moisture-rich bulb."],
["Flesh rings", "Nested leaf sheaths form the onion’s visible concentric rings."],
["Central core", "The youngest leaf bases meet near the center of the bulb."]
]
},
{
id: "cabbage",
name: "Cabbage",
latin: "Brassica oleracea",
x: 0,
task: "Drag the whole cabbage onto its quartered, nested-leaf cut face.",
completedTask: "Aligned. The separated leaves reveal the head from coarse outer leaves to its dense heart.",
facts: [
["Outer leaves", "Broad, older leaves wrap around and shield the inner head."],
["Inner leaves", "Younger leaves overlap tightly, creating a compact sphere."],
["Heart", "The youngest leaves remain tightly curled at the center."]
]
},
{
id: "citrus",
name: "Citrus",
latin: "Citrus × sinensis",
x: 5.2,
task: "Drag the whole citrus fruit onto its radial segment cut face.",
completedTask: "Aligned. Peel, pith, and juice sacs separate to show the fruit’s radial construction.",
facts: [
["Peel", "The colored rind contains aromatic oils in oil-filled glands."],
["Pith", "A pale, spongy layer separates the rind from the juice-bearing interior."],
["Juice sacs", "Specialized vesicles radiate from the center in wedge-shaped segments."]
]
}
];
for (let index = 0; index < sectionDefinitions.length; index++) {
const definition = sectionDefinitions[index];
const station = new THREE.Group();
station.position.x = definition.x;
scene.add(station);
const section = {
...definition,
index,
station,
whole: null,
wholeHome: new THREE.Vector3(),
target: null,
parts: [],
matched: false
};
// Ingredient pedestal.
cylinder(0.68, 0.78, 0.76, 12, material(0xded5c1), -1.55, 1.98, 0.3, station);
cylinder(0.71, 0.71, 0.055, 12, materials.wood, -1.55, 2.38, 0.3, station);
// Colored station tile.
const stationTile = box(
3.35,
0.018,
2.3,
material(
definition.id === "onion" ? 0xd8bf76 :
definition.id === "cabbage" ? 0x91ad7e : 0xdca066,
{ roughness: 0.92 }
),
0,
0.012,
1.6,
station
);
stationTile.receiveShadow = true;
// Cutting board.
const board = box(
2.15,
0.1,
2.05,
material(0xffffff),
1.15,
1.69,
0.3,
station
);
board.material.map = woodTexture;
board.material.color.set(0xffffff);
// Small knife beside the board.
const knife = new THREE.Group();
knife.position.set(2.0, 1.78, 0.75);
knife.rotation.y = -0.45;
station.add(knife);
box(0.58, 0.035, 0.15, materials.darkMetal, 0, 0, 0, knife);
box(0.36, 0.065, 0.13, materials.wood, -0.48, 0.01, 0, knife);
if (definition.id === "onion") {
section.whole = createOnion();
} else if (definition.id === "cabbage") {
section.whole = createCabbage();
} else {
section.whole = createCitrus();
}
section.whole.userData.kind = "whole";
section.whole.userData.section = section;
section.whole.position.set(-1.55, 3.16, 0.3);
section.wholeHome.copy(section.whole.position);
station.add(section.whole);
wholePick.push(section.whole);
section.target =
definition.id === "onion" ? createOnionTarget(section) :
definition.id === "cabbage" ? createCabbageTarget(section) :
createCitrusTarget(section);
section.target.position.set(1.15, 1.745, 0.3);
section.target.userData.kind = "target";
section.target.userData.section = section;
station.add(section.target);
// Labels.
const stationAnchor = new THREE.Object3D();
stationAnchor.position.set(0, 4.65, -0.3);
station.add(stationAnchor);
registerLabel(section, stationAnchor, `0${index + 1} · ${definition.name}`, "station");
const wholeAnchor = new THREE.Object3D();
section.whole.add(wholeAnchor);
registerLabel(section, wholeAnchor, "Drag whole", "whole", new THREE.Vector3(0, 1.14, 0));
const targetAnchor = new THREE.Object3D();
section.target.add(targetAnchor);
registerLabel(section, targetAnchor, "Cut surface", "target", new THREE.Vector3(0, 1.12, 0));
sections.push(section);
}
// -------------------------------------------------------------------------
// Tween utilities
// -------------------------------------------------------------------------
function tween(duration, update, complete = null, delay = 0) {
tweens.push({
start: performance.now() + delay,
duration,
update,
complete
});
}
function updateTweens(now) {
for (let i = tweens.length - 1; i >= 0; i--) {
const item = tweens[i];
if (now < item.start) continue;
const t = Math.min(1, (now - item.start) / item.duration);
const eased = 1 - Math.pow(1 - t, 3);
item.update(eased, t);
if (t >= 1) {
tweens.splice(i, 1);
if (item.complete) item.complete();
}
}
}
// -------------------------------------------------------------------------
// Lesson and progress UI
// -------------------------------------------------------------------------
const lessonIndex = document.querySelector("#lessonIndex");
const lessonTitle = document.querySelector("#lessonTitle");
const lessonLatin = document.querySelector("#lessonLatin");
const taskText = document.querySelector("#taskText");
const alignmentStatus = document.querySelector("#alignmentStatus");
const factList = document.querySelector("#factList");
const progressText = document.querySelector("#progressText");
const progressFill = document.querySelector("#progressFill");
const toast = document.querySelector("#toast");
let toastTimer = null;
function showToast(message, isError = false) {
toast.textContent = message;
toast.classList.toggle("error", isError);
toast.classList.add("visible");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
toast.classList.remove("visible");
}, 2400);
}
function updateLesson() {
const section = sections.find((item) => item.id === state.active);
lessonIndex.textContent = `Section 0${section.index + 1} · ${section.name}`;
lessonTitle.textContent = section.name;
lessonLatin.textContent = section.latin;
taskText.textContent = section.matched ? section.completedTask : section.task;
alignmentStatus.textContent = section.matched ? "Aligned and revealed" : "Not aligned";
alignmentStatus.classList.toggle("complete", section.matched);
factList.innerHTML = "";
section.facts.forEach((fact, index) => {
const row = document.createElement("div");
row.className = `fact${section.matched ? "" : " locked"}`;
const dot = document.createElement("div");
dot.className = "fact-dot";
dot.style.background = section.matched
? ["#c09a43", "#73965d", "#df8135"][index]
: "#c7c9c2";
const copy = document.createElement("div");
const title = document.createElement("strong");
const description = document.createElement("span");
title.textContent = fact[0];
description.textContent = fact[1];
copy.append(title, description);
row.append(dot, copy);
factList.appendChild(row);
});
for (const item of labels) {
if (item.kind === "station") {
item.element.textContent =
`0${item.section.index + 1} · ${item.section.name}` +
(item.section.matched ? " · ALIGNED" : "");
}
}
}
function updateProgress() {
const count = sections.filter((section) => section.matched).length;
progressText.textContent = `${count} / ${sections.length}`;
progressFill.style.width = `${(count / sections.length) * 100}%`;
}
function focusSection(sectionId) {
const section = sections.find((item) => item.id === sectionId);
state.active = sectionId;
state.overview = false;
state.focusGoal.set(section.x, 2.25, 0.15);
state.radiusGoal = 9.2;
state.phiGoal = 0.96;
state.thetaGoal = 0;
document.querySelectorAll(".section-button").forEach((button) => {
button.classList.toggle("active", button.dataset.section === sectionId);
});
document.querySelector("#classroomView").classList.remove("active");
updateLesson();
}
function showClassroom() {
state.overview = true;
state.focusGoal.set(0, 2.25, -0.1);
state.radiusGoal = 15.2;
state.phiGoal = 1.02;
state.thetaGoal = 0;
document.querySelector("#classroomView").classList.add("active");
updateLesson();
}
document.querySelectorAll(".section-button").forEach((button) => {
button.addEventListener("click", () => focusSection(button.dataset.section));
});
document.querySelector("#classroomView").addEventListener("click", showClassroom);
// -------------------------------------------------------------------------
// Matching
// -------------------------------------------------------------------------
function returnWhole(section, duration = 550, delay = 0) {
const start = section.whole.position.clone();
tween(
duration,
(eased) => {
section.whole.position.lerpVectors(start, section.wholeHome, eased);
},
null,
delay
);
}
function matchSection(section) {
if (section.matched) return;
section.matched = true;
updateLesson();
updateProgress();
showToast(`${section.name} aligned — structure revealed.`);
// Briefly align the whole ingredient directly over its cut face.
const start = section.whole.position.clone();
const aligned = new THREE.Vector3(
section.target.position.x,
2.53,
section.target.position.z
);
tween(
360,
(eased) => {
section.whole.position.lerpVectors(start, aligned, eased);
},
() => {
// Return the whole ingredient to its pedestal as the cut face explodes.
returnWhole(section, 750, 420);
}
);
// Explode the structural layers along their growth axes.
section.parts.forEach((part, index) => {
const startPosition = part.object.position.clone();
tween(
760,
(eased) => {
part.object.position.lerpVectors(startPosition, part.exploded, eased);
},
null,
520 + index * 65
);
});
}
document.querySelector("#resetButton").addEventListener("click", () => {
tweens.length = 0;
for (const section of sections) {
section.matched = false;
section.whole.position.copy(section.wholeHome);
section.whole.scale.setScalar(1);
for (const part of section.parts) {
part.object.position.copy(part.base);
}
}
updateLesson();
updateProgress();
showToast("All stations reset.");
});
// -------------------------------------------------------------------------
// Pointer dragging and custom camera controls
// -------------------------------------------------------------------------
let hoveredWhole = null;
let dragging = null;
let orbiting = false;
let orbitPointer = { x: 0, y: 0 };
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 0, 1), -0.3);
const dragPoint = new THREE.Vector3();
const dragOffset = new THREE.Vector3();
const worldPosition = new THREE.Vector3();
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 findWholeRoot(object) {
let current = object;
while (current) {
if (current.userData.kind === "whole") return current;
current = current.parent;
}
return null;
}
function nearestTarget(event) {
const rect = renderer.domElement.getBoundingClientRect();
let best = null;
let bestDistance = Infinity;
for (const section of sections) {
const position = section.target.getWorldPosition(new THREE.Vector3());
position.project(camera);
const x = (position.x * 0.5 + 0.5) * rect.width;
const y = (-position.y * 0.5 + 0.5) * rect.height;
const px = event.clientX - rect.left;
const py = event.clientY - rect.top;
const distance = Math.hypot(x - px, y - py);
if (distance < bestDistance) {
bestDistance = distance;
best = section;
}
}
return {
section: best,
distance: bestDistance
};
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
setPointer(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(wholePick, true);
if (hits.length) {
const whole = findWholeRoot(hits[0].object);
if (whole && !whole.userData.section.matched) {
whole.getWorldPosition(worldPosition);
if (raycaster.ray.intersectPlane(dragPlane, dragPoint)) {
dragOffset.copy(worldPosition).sub(dragPoint);
dragging = {
whole,
section: whole.userData.section,
pointerId: event.pointerId
};
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
event.preventDefault();
return;
}
}
}
orbiting = true;
orbitPointer.x = event.clientX;
orbitPointer.y = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
setPointer(event);
if (dragging) {
raycaster.setFromCamera(pointer, camera);
if (raycaster.ray.intersectPlane(dragPlane, dragPoint)) {
const desiredWorld = dragPoint.clone().add(dragOffset);
desiredWorld.x = THREE.MathUtils.clamp(desiredWorld.x, -7.2, 7.2);
desiredWorld.y = THREE.MathUtils.clamp(desiredWorld.y, 2.0, 4.5);
desiredWorld.z = THREE.MathUtils.clamp(desiredWorld.z, -0.25, 1.05);
const local = dragging.whole.parent.worldToLocal(desiredWorld);
dragging.whole.position.copy(local);
}
return;
}
if (orbiting) {
const dx = event.clientX - orbitPointer.x;
const dy = event.clientY - orbitPointer.y;
state.thetaGoal = THREE.MathUtils.clamp(state.thetaGoal - dx * 0.005, -0.82, 0.82);
state.phiGoal = THREE.MathUtils.clamp(state.phiGoal - dy * 0.004, 0.62, 1.24);
orbitPointer.x = event.clientX;
orbitPointer.y = event.clientY;
return;
}
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(wholePick, true);
const whole = hits.length ? findWholeRoot(hits[0].object) : null;
if (whole !== hoveredWhole) {
hoveredWhole = whole;
renderer.domElement.style.cursor = whole && !whole.userData.section.matched
? "grab"
: "default";
}
});
function endPointer(event) {
if (dragging) {
const dropped = dragging;
dragging = null;
const nearest = nearestTarget(event);
if (nearest.distance < 145 && nearest.section) {
if (nearest.section === dropped.section) {
matchSection(dropped.section);
} else {
showToast(
`That is a ${nearest.section.name.toLowerCase()} cut face. Try the matching whole ingredient.`,
true
);
returnWhole(dropped.section);
}
} else {
showToast("Bring the ingredient closer to a cut face.", true);
returnWhole(dropped.section);
}
}
orbiting = false;
renderer.domElement.style.cursor = hoveredWhole ? "grab" : "default";
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
state.radiusGoal = THREE.MathUtils.clamp(
state.radiusGoal * Math.exp(event.deltaY * 0.001),
5.3,
19
);
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Labels and animation
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labels) {
const section = label.section;
let visible = false;
if (label.kind === "station") {
visible = state.overview || state.active === section.id;
} else if (!state.overview && state.active === section.id) {
visible =
label.kind === "part"
? section.matched
: !section.matched;
}
if (!visible) {
label.element.style.opacity = "0";
continue;
}
label.anchor.getWorldPosition(projected);
projected.project(camera);
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
const onScreen =
projected.z > -1 &&
projected.z < 1 &&
x > -120 &&
x < width + 120 &&
y > -60 &&
y < height + 60;
label.element.style.opacity = onScreen ? "1" : "0";
label.element.style.transform =
`translate(-50%, -50%) translate(${x.toFixed(1)}px, ${y.toFixed(1)}px)`;
}
}
const cameraOffset = new THREE.Vector3();
const one = new THREE.Vector3(1, 1, 1);
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const now = performance.now();
updateTweens(now);
const smoothing = 1 - Math.exp(-dt * 5.5);
state.focus.lerp(state.focusGoal, smoothing);
state.radius += (state.radiusGoal - state.radius) * smoothing;
state.phi += (state.phiGoal - state.phi) * smoothing;
state.theta += (state.thetaGoal - state.theta) * smoothing;
cameraOffset.set(
state.radius * Math.sin(state.phi) * Math.sin(state.theta),
state.radius * Math.cos(state.phi),
state.radius * Math.sin(state.phi) * Math.cos(state.theta)
);
camera.position.copy(state.focus).add(cameraOffset);
camera.lookAt(state.focus);
for (const section of sections) {
const isDragging = dragging && dragging.whole === section.whole;
const targetScale = isDragging
? 1.07
: hoveredWhole === section.whole
? 1.045
: 1;
section.whole.scale.lerp(one.setScalar(targetScale), 1 - Math.exp(-dt * 12));
if (!isDragging && !section.matched) {
section.whole.rotation.y += dt * 0.12;
}
}
updateLabels();
renderer.render(scene, camera);
}
// -------------------------------------------------------------------------
// Resize and initialization
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.75));
});
updateLesson();
updateProgress();
focusSection("onion");
animate();
</script>
</body>
</html>
```