Prompt specification
Build or customize the Three.js 3D scene "Recyclable-object toy classification desk" 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: Recyclable-object toy classification desk
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/recyclable-object-toy-classification-desk-x1895
Scene Description Prompt:
I'd like to see this as a voxel Three.js scene: recyclable-object toy classification desk. Compose fictional objects described by two numerical features and a perceptron display; state the labels synthetic and demonstrate how feature choice limits what the model can learn. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (recyclable-object-toy-classification-desk-x1895):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Loopworks — Synthetic Toy Classification Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #07101b;
--panel: rgba(9, 18, 31, 0.94);
--line: rgba(160, 207, 224, 0.18);
--muted: #8fa8b8;
--text: #eef8fb;
--cyan: #63efd2;
--cyan-dark: #153f3d;
--orange: #ffb06b;
--orange-dark: #4b2c1c;
--yellow: #f7dc73;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button, summary { font: inherit; }
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
outline: none;
cursor: grab;
}
#scene:active { cursor: grabbing; }
.topbar {
position: fixed;
top: 18px;
left: 18px;
z-index: 3;
width: min(560px, calc(100vw - 430px));
pointer-events: none;
}
.brand {
display: inline-flex;
align-items: center;
gap: 10px;
margin-bottom: 10px;
color: var(--cyan);
font: 700 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.15em;
}
.brand::before {
content: "";
width: 11px;
height: 11px;
background: var(--cyan);
box-shadow: 4px 4px 0 #1d6a65, 8px 0 0 rgba(99, 239, 210, 0.25);
}
h1 {
margin: 0;
max-width: 530px;
font-size: clamp(27px, 3.2vw, 48px);
line-height: 0.98;
letter-spacing: -0.045em;
text-shadow: 0 3px 22px rgba(0, 0, 0, 0.45);
}
.subtitle {
margin: 12px 0 0;
max-width: 510px;
color: #bfd0d9;
font-size: 13px;
line-height: 1.55;
text-shadow: 0 2px 12px #000;
}
.pill {
display: inline-flex;
align-items: center;
gap: 7px;
margin-top: 12px;
padding: 7px 10px;
border: 1px solid rgba(99, 239, 210, 0.35);
border-radius: 999px;
background: rgba(7, 24, 28, 0.76);
color: var(--cyan);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
}
.pill::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
}
.hint {
position: fixed;
left: 18px;
bottom: 18px;
z-index: 2;
padding: 9px 11px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(6, 13, 23, 0.76);
color: #a9bec9;
font: 600 10px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
pointer-events: none;
}
.panel {
position: fixed;
top: 16px;
right: 16px;
bottom: 16px;
z-index: 4;
width: 382px;
overflow: auto;
padding: 20px;
border: 1px solid var(--line);
border-radius: 16px;
background:
linear-gradient(145deg, rgba(21, 42, 55, 0.38), transparent 42%),
var(--panel);
box-shadow: 0 24px 70px rgba(0, 0, 0, 0.45);
backdrop-filter: blur(18px);
scrollbar-width: thin;
scrollbar-color: #294454 transparent;
}
.panel-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
}
.kicker {
color: var(--muted);
font: 700 10px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.14em;
text-transform: uppercase;
}
h2 {
margin: 5px 0 0;
font-size: 23px;
letter-spacing: -0.035em;
}
.live {
flex: none;
padding: 6px 8px;
border-radius: 6px;
background: var(--cyan-dark);
color: var(--cyan);
font: 800 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
}
.description {
margin: 12px 0 0;
color: #a9bdca;
font-size: 12px;
line-height: 1.55;
}
.synthetic-note {
margin: 15px 0;
padding: 11px 12px;
border-left: 3px solid var(--yellow);
border-radius: 3px 8px 8px 3px;
background: rgba(247, 220, 115, 0.08);
color: #e9e1ba;
font-size: 11px;
line-height: 1.5;
}
.synthetic-note strong {
color: var(--yellow);
font: 800 10px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.1em;
}
.control-label {
display: block;
margin-bottom: 8px;
color: #bcd0da;
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.segmented {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 6px;
padding: 4px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(2, 8, 15, 0.55);
}
.segmented button {
min-height: 42px;
border: 0;
border-radius: 7px;
background: transparent;
color: #8da8b7;
cursor: pointer;
font-size: 11px;
font-weight: 800;
transition: 160ms ease;
}
.segmented button:hover { color: #dff8f4; }
.segmented button.active {
background: #1b4b4a;
color: #b9fff0;
box-shadow: inset 0 0 0 1px rgba(99, 239, 210, 0.22);
}
.basis-copy {
min-height: 43px;
margin: 9px 1px 4px;
color: #91a9b8;
font-size: 10.5px;
line-height: 1.45;
}
.plot-wrap {
position: relative;
overflow: hidden;
margin-top: 10px;
border: 1px solid var(--line);
border-radius: 11px;
background: #07111d;
}
#plot {
display: block;
width: 100%;
height: 245px;
cursor: crosshair;
}
.legend {
position: absolute;
top: 9px;
right: 10px;
display: flex;
gap: 10px;
padding: 5px 7px;
border-radius: 6px;
background: rgba(4, 10, 18, 0.75);
color: #9fb5c2;
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.legend span::before {
content: "";
display: inline-block;
width: 7px;
height: 7px;
margin-right: 5px;
border-radius: 50%;
background: var(--cyan);
}
.legend .keep::before {
border-radius: 1px;
background: var(--orange);
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-top: 10px;
}
.status {
padding: 7px 9px;
border-radius: 6px;
background: var(--cyan-dark);
color: var(--cyan);
font: 800 9px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
}
.status.limit {
background: var(--orange-dark);
color: var(--orange);
}
.replay {
border: 1px solid rgba(99, 239, 210, 0.28);
border-radius: 7px;
padding: 8px 11px;
background: transparent;
color: var(--cyan);
cursor: pointer;
font: 800 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
}
.replay:hover { background: rgba(99, 239, 210, 0.09); }
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-top: 9px;
}
.metric {
padding: 9px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(1, 7, 13, 0.38);
}
.metric span {
display: block;
color: #75909f;
font: 700 8px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.metric strong {
display: block;
margin-top: 5px;
font: 700 15px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.equation {
margin-top: 8px;
padding: 9px 10px;
border: 1px solid var(--line);
border-radius: 8px;
background: #06101b;
color: #a9c2cf;
font: 600 10px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.explanation {
min-height: 48px;
margin: 9px 0 0;
color: #9eb4c0;
font-size: 10.5px;
line-height: 1.5;
}
.selected-card {
margin-top: 15px;
padding: 13px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(2, 9, 16, 0.43);
}
.selected-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.selected-name {
font-size: 14px;
font-weight: 800;
}
.toy-badge {
padding: 5px 7px;
border-radius: 5px;
background: var(--cyan-dark);
color: var(--cyan);
font: 800 8px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
}
.toy-badge.keep {
background: var(--orange-dark);
color: var(--orange);
}
.feature-values {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 10px;
}
.feature-value {
padding: 8px;
border-radius: 7px;
background: rgba(99, 239, 210, 0.055);
}
.feature-value span {
display: block;
color: #7893a1;
font: 700 8px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
text-transform: uppercase;
}
.feature-value strong {
display: block;
margin-top: 5px;
font: 700 14px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
details {
margin-top: 14px;
border-top: 1px solid var(--line);
padding-top: 12px;
}
summary {
color: #b9ced8;
cursor: pointer;
font-size: 11px;
font-weight: 800;
}
.table-wrap {
max-height: 205px;
overflow: auto;
margin-top: 10px;
border: 1px solid var(--line);
border-radius: 8px;
}
table {
width: 100%;
border-collapse: collapse;
font: 600 9px/1.35 ui-monospace, SFMono-Regular, Menlo, monospace;
}
th, td {
padding: 7px;
border-bottom: 1px solid rgba(160, 207, 224, 0.09);
text-align: left;
}
th {
position: sticky;
top: 0;
z-index: 1;
background: #0d1c2a;
color: #7794a4;
font-size: 8px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
tbody tr { cursor: pointer; }
tbody tr:hover { background: rgba(99, 239, 210, 0.06); }
tbody tr.selected { background: rgba(99, 239, 210, 0.1); }
td.label-recycle { color: var(--cyan); }
td.label-keep { color: var(--orange); }
.footer-note {
margin: 13px 0 0;
color: #6f8998;
font-size: 9.5px;
line-height: 1.5;
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #07101b;
color: var(--cyan);
font: 800 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
}
@media (max-width: 850px) {
.topbar {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
}
h1 { max-width: 370px; font-size: 29px; }
.subtitle { max-width: 360px; font-size: 11px; }
.pill { display: none; }
.panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
max-height: 49vh;
padding: 16px;
border-radius: 13px;
}
#plot { height: 205px; }
.hint { display: none; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel toy classification desk"></canvas>
<header class="topbar">
<div class="brand">LOOPWORKS / LAB 04</div>
<h1>Recyclable-object toy desk</h1>
<p class="subtitle">
Eight fictional voxel toys, two numerical features, one live perceptron.
Change the feature basis while keeping every label fixed.
</p>
<div class="pill">LABELS ARE SYNTHETIC</div>
</header>
<div class="hint">DRAG TO ORBIT · SCROLL TO ZOOM · CLICK A TOY OR PLOT POINT</div>
<aside class="panel">
<div class="panel-head">
<div>
<div class="kicker">Classification bench</div>
<h2>Perceptron display</h2>
</div>
<div class="live">LIVE</div>
</div>
<p class="description">
The perceptron learns a straight boundary from exactly two numbers.
The target labels never change; only the representation changes.
</p>
<div class="synthetic-note">
<strong>SYNTHETIC LABELS</strong><br>
“RECYCLE” and “KEEP” come from a fictional policy, not real material
composition or a real-world recyclability assessment.
</div>
<span class="control-label">Feature basis</span>
<div class="segmented" role="group" aria-label="Feature basis">
<button id="geometryButton" class="active" aria-pressed="true">Geometry</button>
<button id="surfaceButton" aria-pressed="false">Surface</button>
</div>
<div id="basisCopy" class="basis-copy"></div>
<div class="plot-wrap">
<canvas id="plot" aria-label="Two-dimensional perceptron scatter plot"></canvas>
<div class="legend">
<span>RECYCLE</span>
<span class="keep">KEEP</span>
</div>
</div>
<div class="status-row">
<div id="status" class="status">TRAINING</div>
<button id="replay" class="replay">↻ REPLAY</button>
</div>
<div class="metrics">
<div class="metric"><span>Epoch</span><strong id="epochMetric">0</strong></div>
<div class="metric"><span>Errors</span><strong id="errorMetric">0</strong></div>
<div class="metric"><span>Accuracy</span><strong id="accuracyMetric">0%</strong></div>
</div>
<div id="equation" class="equation">w₀x + w₁y + b = 0</div>
<p id="explanation" class="explanation"></p>
<div class="selected-card">
<div class="selected-top">
<div id="selectedName" class="selected-name">Pixel Rocket</div>
<div id="selectedLabel" class="toy-badge">RECYCLE</div>
</div>
<div class="feature-values">
<div class="feature-value">
<span id="featureXName">Blockiness</span>
<strong id="featureXValue">0.18</strong>
</div>
<div class="feature-value">
<span id="featureYName">Open space</span>
<strong id="featureYValue">0.20</strong>
</div>
</div>
</div>
<details>
<summary>View all fixed vectors</summary>
<div class="table-wrap">
<table>
<thead>
<tr>
<th>Fictional toy</th>
<th id="tableXHead">x</th>
<th id="tableYHead">y</th>
<th>Synthetic label</th>
</tr>
</thead>
<tbody id="featureTable"></tbody>
</table>
</div>
</details>
<p class="footer-note">
Geometry happens to preserve a cue to the fictional policy. Surface
discards it, creating a feature bottleneck. Nonlinear feature expansion
could recover information in the surface basis; that would be another
feature choice, not a change to the labels.
</p>
</aside>
<div id="loading">ASSEMBLING VOXEL LAB…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Synthetic dataset. Labels are assigned before either feature basis is used.
// Geometry is linearly separable; Surface deliberately creates conflicts.
// -------------------------------------------------------------------------
const toys = [
{
id: "rocket", name: "Pixel Rocket", label: "RECYCLE", labelY: 1,
geometry: [0.18, 0.20], surface: [0.18, 0.78],
position: [-5.15, 2.72, -1.25]
},
{
id: "loopbot", name: "Loop Bot", label: "RECYCLE", labelY: 1,
geometry: [0.26, 0.64], surface: [0.82, 0.16],
position: [-1.72, 2.72, -1.25]
},
{
id: "duck", name: "Gear Duck", label: "RECYCLE", labelY: 1,
geometry: [0.72, 0.20], surface: [0.22, 0.20],
position: [1.72, 2.72, -1.25]
},
{
id: "cat", name: "Cube Cat", label: "RECYCLE", labelY: 1,
geometry: [0.32, 0.42], surface: [0.78, 0.82],
position: [5.15, 2.72, -1.25]
},
{
id: "ufo", name: "Stack UFO", label: "KEEP", labelY: -1,
geometry: [0.78, 0.78], surface: [0.20, 0.22],
position: [-5.15, 2.72, 1.25]
},
{
id: "car", name: "Patch Car", label: "KEEP", labelY: -1,
geometry: [0.82, 0.34], surface: [0.84, 0.84],
position: [-1.72, 2.72, 1.25]
},
{
id: "owl", name: "Prism Owl", label: "KEEP", labelY: -1,
geometry: [0.48, 0.84], surface: [0.28, 0.78],
position: [1.72, 2.72, 1.25]
},
{
id: "dog", name: "Bolt Dog", label: "KEEP", labelY: -1,
geometry: [0.86, 0.18], surface: [0.80, 0.24],
position: [5.15, 2.72, 1.25]
}
];
const featureSpaces = {
geometry: {
title: "Geometry basis",
x: "Blockiness",
y: "Open space",
copy: "Two structural measurements retain a cue to the fictional labels, so one linear boundary can separate them."
},
surface: {
title: "Surface basis",
x: "Roughness",
y: "Warmth",
copy: "Two appearance measurements discard the structural cue. Nearby toys now have conflicting labels."
}
};
const state = {
space: "geometry",
selectedId: "rocket",
snapshots: [],
best: null,
index: 0,
playing: true,
lastStep: 0
};
const plotCanvas = document.getElementById("plot");
const plotContext = plotCanvas.getContext("2d");
const sceneCanvas = document.getElementById("scene");
let renderer;
let scene;
let camera;
let boardTexture;
let boardContext;
let selectionMarker;
let pickables = [];
let toyGroups = [];
const materialCache = new Map();
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const toyCell = 0.27;
const toyVoxelGeometry = new THREE.BoxGeometry(toyCell * 0.90, toyCell * 0.90, toyCell * 0.90);
const dummy = new THREE.Object3D();
function material(color) {
if (!materialCache.has(color)) {
materialCache.set(color, new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.04,
flatShading: true
}));
}
return materialCache.get(color);
}
function makeBlockSet(items) {
const groups = new Map();
for (const item of items) {
if (!groups.has(item.color)) groups.set(item.color, []);
groups.get(item.color).push(item);
}
const group = new THREE.Group();
for (const [color, groupItems] of groups) {
const mesh = new THREE.InstancedMesh(unitBox, material(color), groupItems.length);
mesh.castShadow = true;
mesh.receiveShadow = true;
groupItems.forEach((item, i) => {
dummy.position.set(item.x, item.y, item.z);
dummy.scale.set(item.sx ?? 1, item.sy ?? 1, item.sz ?? 1);
dummy.updateMatrix();
mesh.setMatrixAt(i, dummy.matrix);
});
mesh.instanceMatrix.needsUpdate = true;
group.add(mesh);
}
return group;
}
function addToyCells(cells, put) {
// Rocket
for (let y = 0; y <= 5; y++) {
for (let x = -1; x <= 1; x++) {
for (let z = -1; z <= 1; z++) put(x, y, z, "#e6edf3");
}
}
put(0, 6, 0, "#ff875f");
put(-2, 0, 0, "#4388ff");
put(2, 0, 0, "#4388ff");
put(-2, 1, 0, "#4388ff");
put(2, 1, 0, "#4388ff");
put(0, 4, 2, "#5be2ff");
put(0, 4, 3, "#15273a");
put(0, 1, 2, "#ff875f");
// Loop Bot
for (let y = 0; y <= 3; y++) {
for (let x = -2; x <= 2; x++) {
for (let z = -1; z <= 1; z++) {
if (!(y === 2 && Math.abs(x) === 2 && z === 0)) {
put(x + 8, y, z, "#4778ff");
}
}
}
}
put(6, 4, 0, "#9af58a");
put(6, 5, 0, "#9af58a");
put(7, 5, 0, "#ffcf62");
put(7, 3, 2, "#102138");
put(9, 3, 2, "#102138");
put(8, 1, 2, "#67f0d2");
// Gear Duck
for (let y = 0; y <= 2; y++) {
for (let x = -2; x <= 2; x++) {
for (let z = -1; z <= 1; z++) {
if (!(y === 0 && Math.abs(x) === 2 && Math.abs(z) === 1)) {
put(x + 16, y, z, "#ffd75c");
}
}
}
}
for (let y = 2; y <= 4; y++) {
for (let z = -1; z <= 1; z++) put(18, y, z, "#ffd75c");
}
put(18, 3, 2, "#ff8a45");
put(18, 3, 3, "#ff8a45");
put(13, 1, 0, "#203149");
put(19, 1, 0, "#203149");
put(16, 1, 2, "#fff0ae");
// Cube Cat
for (let y = 0; y <= 2; y++) {
for (let x = -1; x <= 1; x++) {
for (let z = -1; z <= 1; z++) put(x + 24, y, z, "#ff8d87");
}
}
for (let y = 2; y <= 4; y++) {
for (let x = -2; x <= 2; x++) {
for (let z = -1; z <= 1; z++) put(x + 24, y, z, "#ff8d87");
}
}
put(22, 5, 0, "#ff8d87");
put(26, 5, 0, "#ff8d87");
put(23, 5, 0, "#ff8d87");
put(25, 5, 0, "#ff8d87");
put(23, 3, 2, "#142237");
put(25, 3, 2, "#142237");
put(24, 2, 2, "#ffe1b8");
// Stack UFO
for (let y = 0; y <= 1; y++) {
for (let x = -3; x <= 3; x++) {
for (let z = -3; z <= 3; z++) {
if (Math.abs(x) + Math.abs(z) <= 4) put(x + 32, y, z, "#a66cff");
}
}
}
for (let y = 2; y <= 3; y++) {
for (let x = -2; x <= 2; x++) {
for (let z = -2; z <= 2; z++) {
if (Math.abs(x) + Math.abs(z) <= 3) put(x + 32, y, z, "#68e5ef");
}
}
}
put(32, 4, 0, "#f8ed7b");
for (let x = -3; x <= 3; x += 2) {
put(x + 32, 1, 4, "#fff07a");
}
// Patch Car
for (let x = -3; x <= 3; x++) {
for (let z = -1; z <= 1; z++) put(x + 40, 0, z, "#65d39c");
for (let z = -1; z <= 1; z++) put(x + 40, 1, z, "#65d39c");
}
for (let y = 2; y <= 3; y++) {
for (let x = -1; x <= 1; x++) {
for (let z = -1; z <= 1; z++) put(x + 40, y, z, "#b9f2c6");
}
}
put(37, 0, 2, "#142237");
put(43, 0, 2, "#142237");
put(37, 0, -2, "#142237");
put(43, 0, -2, "#142237");
put(40, 1, 2, "#fff4b0");
// Prism Owl
for (let y = 0; y <= 4; y++) {
for (let x = -2; x <= 2; x++) {
for (let z = -1; z <= 1; z++) put(x + 48, y, z, "#42c4b2");
}
}
put(46, 5, 0, "#42c4b2");
put(50, 5, 0, "#42c4b2");
put(47, 3, 2, "#ffe47b");
put(49, 3, 2, "#ffe47b");
put(48, 1, 2, "#ffb05d");
put(45, 2, 0, "#2d938f");
put(51, 2, 0, "#2d938f");
// Bolt Dog
for (let x = -3; x <= 1; x++) {
for (let z = -1; z <= 1; z++) put(x + 56, 1, z, "#d69a61");
}
for (let x = -3; x <= 0; x += 3) {
for (let z = -1; z <= 1; z++) put(x + 56, 0, z, "#8a583b");
}
for (let y = 2; y <= 5; y++) {
for (let z = -1; z <= 1; z++) put(59, y, z, "#e1ad70");
}
put(59, 2, 2, "#f3d3a3");
put(59, 2, 3, "#f3d3a3");
put(58, 4, 2, "#162337");
put(60, 4, 2, "#162337");
put(57, 3, 0, "#855034");
put(61, 3, 0, "#855034");
put(56, 1, 2, "#c27b4b");
}
function buildToyModel(id) {
const cells = [];
function put(x, y, z, color) {
cells.push({ x, y, z, color });
}
const offsets = {
rocket: 0,
loopbot: 8,
duck: 16,
cat: 24,
ufo: 32,
car: 40,
owl: 48,
dog: 56
};
// The model is authored in horizontal lanes, then shifted for each toy.
const localCells = [];
const localPut = (x, y, z, color) => localCells.push({ x, y, z, color });
addToyCells(localCells, localPut);
const offset = offsets[id];
for (const cell of localCells) {
if (cell.x >= offset && cell.x < offset + 8) {
put(cell.x - offset, cell.y, cell.z, cell.color);
}
}
const byColor = new Map();
for (const cell of cells) {
if (!byColor.has(cell.color)) byColor.set(cell.color, []);
byColor.get(cell.color).push(cell);
}
const group = new THREE.Group();
group.userData.toyId = id;
for (const [color, list] of byColor) {
const mesh = new THREE.InstancedMesh(toyVoxelGeometry, material(color), list.length);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.userData.toyId = id;
list.forEach((cell, i) => {
dummy.position.set(cell.x * toyCell, cell.y * toyCell, cell.z * toyCell);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
mesh.setMatrixAt(i, dummy.matrix);
});
mesh.instanceMatrix.needsUpdate = true;
group.add(mesh);
pickables.push(mesh);
}
return group;
}
function buildRoom() {
const floorBlocks = [];
for (let x = 0; x < 22; x++) {
for (let z = 0; z < 14; z++) {
const color = (x + z) % 5 === 0 ? "#182c3b" :
(x + z) % 2 === 0 ? "#132331" : "#10202d";
floorBlocks.push({
x: -10.5 + x, y: -0.18, z: -6.5 + z,
sx: 0.96, sy: 0.32, sz: 0.96, color
});
}
}
scene.add(makeBlockSet(floorBlocks));
const wallBlocks = [];
for (let x = 0; x < 22; x++) {
for (let y = 0; y < 8; y++) {
const color = (x + y) % 6 === 0 ? "#183346" :
(x + y) % 2 === 0 ? "#12283a" : "#102434";
wallBlocks.push({
x: -10.5 + x, y: 0.5 + y, z: -7.25,
sx: 0.96, sy: 0.96, sz: 0.42, color
});
}
}
scene.add(makeBlockSet(wallBlocks));
const deskBlocks = [];
for (let x = 0; x < 18; x++) {
for (let z = 0; z < 8; z++) {
deskBlocks.push({
x: -6.8 + x * 0.8, y: 2.47, z: -2.8 + z * 0.8,
sx: 0.76, sy: 0.46, sz: 0.76,
color: (x + z) % 4 === 0 ? "#3b6670" : "#315664"
});
}
}
// Front apron and tiled legs.
for (let x = 0; x < 18; x++) {
for (let y = 0; y < 3; y++) {
deskBlocks.push({
x: -6.8 + x * 0.8, y: 1.55 + y * 0.28, z: 3.12,
sx: 0.75, sy: 0.25, sz: 0.28,
color: "#294b58"
});
}
}
for (const x of [-6.1, 6.1]) {
for (const z of [-2.35, 2.35]) {
for (let y = 0; y < 8; y++) {
deskBlocks.push({
x, y: 0.2 + y * 0.28, z,
sx: 0.62, sy: 0.25, sz: 0.62,
color: y % 2 ? "#264653" : "#2e5360"
});
}
}
}
scene.add(makeBlockSet(deskBlocks));
// Two voxel recycling bins behind the left side of the desk.
const binBlocks = [];
for (const bin of [
{ x: -8.35, z: -2.65, color: "#247d72" },
{ x: -8.35, z: 0.25, color: "#39729a" }
]) {
for (let y = 0; y < 4; y++) {
for (let x = -1; x <= 1; x++) {
binBlocks.push({
x: bin.x + x * 0.48, y: 0.25 + y * 0.48, z: bin.z - 0.48,
sx: 0.45, sy: 0.45, sz: 0.42, color: bin.color
});
}
binBlocks.push({
x: bin.x - 0.72, y: 0.25 + y * 0.48, z: bin.z,
sx: 0.28, sy: 0.45, sz: 0.9, color: bin.color
});
binBlocks.push({
x: bin.x + 0.72, y: 0.25 + y * 0.48, z: bin.z,
sx: 0.28, sy: 0.45, sz: 0.9, color: bin.color
});
}
}
scene.add(makeBlockSet(binBlocks));
}
function buildBoard() {
const boardCanvas = document.createElement("canvas");
boardCanvas.width = 512;
boardCanvas.height = 256;
boardContext = boardCanvas.getContext("2d");
boardContext.imageSmoothingEnabled = false;
boardTexture = new THREE.CanvasTexture(boardCanvas);
boardTexture.colorSpace = THREE.SRGBColorSpace;
boardTexture.minFilter = THREE.NearestFilter;
boardTexture.magFilter = THREE.NearestFilter;
const screen = new THREE.Mesh(
new THREE.PlaneGeometry(7.2, 3.15),
new THREE.MeshBasicMaterial({ map: boardTexture })
);
screen.position.set(0, 5.05, -6.96);
scene.add(screen);
const frame = [];
for (let x = -9; x <= 9; x++) {
frame.push({ x: x * 0.42, y: 6.79, z: -6.76, sx: 0.38, sy: 0.28, sz: 0.3, color: "#f3b75f" });
frame.push({ x: x * 0.42, y: 3.31, z: -6.76, sx: 0.38, sy: 0.28, sz: 0.3, color: "#f3b75f" });
}
for (let y = 0; y < 9; y++) {
frame.push({ x: -3.98, y: 3.55 + y * 0.39, z: -6.76, sx: 0.28, sy: 0.35, sz: 0.3, color: "#f3b75f" });
frame.push({ x: 3.98, y: 3.55 + y * 0.39, z: -6.76, sx: 0.28, sy: 0.35, sz: 0.3, color: "#f3b75f" });
}
scene.add(makeBlockSet(frame));
}
function buildSelectionMarker() {
selectionMarker = new THREE.Group();
const markerMaterial = new THREE.MeshBasicMaterial({ color: 0x63efd2 });
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
for (let i = 0; i < 2; i++) {
const block = new THREE.Mesh(unitBox, markerMaterial);
block.scale.set(0.15, 0.05, 0.15);
block.position.set(sx * (0.93 - i * 0.18), 0, sz * 0.68);
selectionMarker.add(block);
}
}
}
scene.add(selectionMarker);
}
function initThree() {
renderer = new THREE.WebGLRenderer({
canvas: sceneCanvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
scene = new THREE.Scene();
scene.background = new THREE.Color("#091522");
scene.fog = new THREE.Fog("#091522", 19, 38);
camera = new THREE.PerspectiveCamera(
40, window.innerWidth / window.innerHeight, 0.1, 80
);
const orbit = {
target: new THREE.Vector3(-1.15, 2.75, -0.25),
radius: 19.5,
azimuth: 0.68,
polar: 1.11
};
function updateCamera() {
const sinPolar = Math.sin(orbit.polar);
camera.position.set(
orbit.target.x + orbit.radius * sinPolar * Math.sin(orbit.azimuth),
orbit.target.y + orbit.radius * Math.cos(orbit.polar),
orbit.target.z + orbit.radius * sinPolar * Math.cos(orbit.azimuth)
);
camera.lookAt(orbit.target);
}
updateCamera();
window.__updateCamera = updateCamera;
window.__orbit = orbit;
const hemisphere = new THREE.HemisphereLight(0xbcefff, 0x162333, 2.15);
scene.add(hemisphere);
const key = new THREE.DirectionalLight(0xfff0d2, 3.6);
key.position.set(-6, 13, 9);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -15;
key.shadow.camera.right = 15;
key.shadow.camera.top = 15;
key.shadow.camera.bottom = -15;
key.shadow.camera.near = 1;
key.shadow.camera.far = 40;
key.shadow.bias = -0.0004;
scene.add(key);
const cyanLight = new THREE.PointLight(0x58d7ff, 24, 18, 2);
cyanLight.position.set(-6, 6, 4);
scene.add(cyanLight);
const warmLight = new THREE.PointLight(0xffa45c, 17, 16, 2);
warmLight.position.set(7, 6, -2);
scene.add(warmLight);
buildRoom();
buildBoard();
buildSelectionMarker();
for (const toy of toys) {
const group = buildToyModel(toy.id);
group.position.set(...toy.position);
group.userData.baseY = toy.position[1];
group.userData.phase = toy.position[0] * 0.31 + toy.position[2] * 0.17;
scene.add(group);
toyGroups.push(group);
}
setupCameraControls();
setSelected("rocket");
}
// -------------------------------------------------------------------------
// Perceptron. The same fixed labels are used for every representation.
// -------------------------------------------------------------------------
function trainPerceptron(space) {
const learningRate = 0.075;
let weights = [0.025, -0.035];
let bias = -0.02;
const snapshots = [];
let best = null;
for (let epoch = 1; epoch <= 90; epoch++) {
let errors = 0;
for (const toy of toys) {
const [x, y] = toy[space];
const score = weights[0] * x + weights[1] * y + bias;
const prediction = score >= 0 ? 1 : -1;
if (prediction !== toy.labelY) {
weights[0] += learningRate * toy.labelY * x;
weights[1] += learningRate * toy.labelY * y;
bias += learningRate * toy.labelY;
errors++;
}
}
const snapshot = {
epoch,
weights: [...weights],
bias,
errors
};
snapshots.push(snapshot);
if (!best || errors < best.errors) {
best = { ...snapshot, weights: [...weights], index: snapshots.length - 1 };
}
if (errors === 0) break;
}
return { snapshots, best };
}
function setFeatureSpace(space, shouldReplay = true) {
state.space = space;
const result = trainPerceptron(space);
state.snapshots = result.snapshots;
state.best = result.best;
state.index = 0;
state.playing = shouldReplay;
state.lastStep = performance.now();
const metadata = featureSpaces[space];
document.getElementById("basisCopy").textContent = metadata.copy;
for (const button of [
document.getElementById("geometryButton"),
document.getElementById("surfaceButton")
]) {
const active = button.id.toLowerCase().startsWith(space);
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
}
document.getElementById("featureXName").textContent = metadata.x;
document.getElementById("featureYName").textContent = metadata.y;
document.getElementById("tableXHead").textContent = metadata.x;
document.getElementById("tableYHead").textContent = metadata.y;
updateFeatureTable();
updateSelectedCard();
updateTrainingUI();
}
function updateFeatureTable() {
const tbody = document.getElementById("featureTable");
tbody.replaceChildren();
for (const toy of toys) {
const row = document.createElement("tr");
if (toy.id === state.selectedId) row.classList.add("selected");
const values = toy[state.space];
row.innerHTML = `
<td>${toy.name}</td>
<td>${values[0].toFixed(2)}</td>
<td>${values[1].toFixed(2)}</td>
<td class="${toy.label === "RECYCLE" ? "label-recycle" : "label-keep"}">
${toy.label}
</td>
`;
row.addEventListener("click", () => setSelected(toy.id));
tbody.appendChild(row);
}
}
function updateTrainingUI() {
if (!state.snapshots.length) return;
const snapshot = state.snapshots[state.index];
const best = state.best;
const complete = !state.playing;
document.getElementById("epochMetric").textContent = snapshot.epoch;
document.getElementById("errorMetric").textContent = snapshot.errors;
document.getElementById("accuracyMetric").textContent =
`${Math.round((1 - snapshot.errors / toys.length) * 100)}%`;
const [w0, w1] = snapshot.weights;
document.getElementById("equation").textContent =
`${w0.toFixed(3)}x ${w1 >= 0 ? "+" : "−"} ${Math.abs(w1).toFixed(3)}y ` +
`${snapshot.bias >= 0 ? "+" : "−"} ${Math.abs(snapshot.bias).toFixed(3)} = 0`;
const status = document.getElementById("status");
const explanation = document.getElementById("explanation");
if (best.errors === 0) {
status.textContent = complete ? "LINEARLY SEPARABLE" : "TRAINING";
status.classList.remove("limit");
explanation.textContent =
"The same straight boundary now separates all eight fixed synthetic labels. " +
"The model succeeded because this feature basis retained a useful cue—not because the labels are real.";
} else {
status.textContent = complete ? "FEATURE BOTTLENECK" : "TRAINING";
status.classList.add("limit");
explanation.textContent =
`Even the best linear checkpoint misses ${best.errors} of ${toys.length} toys. ` +
"Conflicting labels occupy the same surface-feature region, so changing the weights cannot recover the missing structural information.";
}
drawPlot();
}
function updateSelectedCard() {
const toy = toys.find(item => item.id === state.selectedId);
const values = toy[state.space];
const metadata = featureSpaces[state.space];
document.getElementById("selectedName").textContent = toy.name;
document.getElementById("selectedLabel").textContent = `${toy.label} · SYNTHETIC`;
document.getElementById("selectedLabel").classList.toggle("keep", toy.label === "KEEP");
document.getElementById("featureXName").textContent = metadata.x;
document.getElementById("featureYName").textContent = metadata.y;
document.getElementById("featureXValue").textContent = values[0].toFixed(2);
document.getElementById("featureYValue").textContent = values[1].toFixed(2);
if (selectionMarker) {
selectionMarker.position.set(toy.position[0], 2.72, toy.position[2]);
}
updateFeatureTable();
}
function setSelected(id) {
if (!toys.some(toy => toy.id === id)) return;
state.selectedId = id;
updateSelectedCard();
drawPlot();
}
// -------------------------------------------------------------------------
// 2D perceptron display and the matching wall display.
// -------------------------------------------------------------------------
function resizeCanvas(canvas, context) {
const rect = canvas.getBoundingClientRect();
const dpr = Math.min(window.devicePixelRatio || 1, 2);
const width = Math.max(1, Math.round(rect.width * dpr));
const height = Math.max(1, Math.round(rect.height * dpr));
if (canvas.width !== width || canvas.height !== height) {
canvas.width = width;
canvas.height = height;
}
context.setTransform(dpr, 0, 0, dpr, 0, 0);
return { width: rect.width, height: rect.height };
}
function lineIntersections(weights, bias) {
const [a, b, c] = [weights[0], weights[1], bias];
const points = [];
function add(x, y) {
if (x >= -0.001 && x <= 1.001 && y >= -0.001 && y <= 1.001) {
const clampedX = Math.max(0, Math.min(1, x));
const clampedY = Math.max(0, Math.min(1, y));
if (!points.some(point => Math.hypot(point.x - clampedX, point.y - clampedY) < 0.0001)) {
points.push({ x: clampedX, y: clampedY });
}
}
}
if (Math.abs(b) > 0.00001) {
add(0, (-c) / b);
add(1, (-a - c) / b);
} else if (Math.abs(a) > 0.00001) {
add(-c / a, 0);
add(-c / a, 1);
}
return points;
}
function drawDecisionLine(context, snapshot, rect, options = {}) {
const intersections = lineIntersections(snapshot.weights, snapshot.bias);
if (intersections.length < 2) return;
context.save();
context.strokeStyle = options.color || "#f7dc73";
context.lineWidth = options.width || 2;
context.setLineDash(options.dash || []);
context.beginPath();
for (let i = 0; i < intersections.length; i++) {
const point = intersections[i];
const x = rect.x + point.x * rect.width;
const y = rect.y + (1 - point.y) * rect.height;
if (i === 0) context.moveTo(x, y);
else context.lineTo(x, y);
}
context.stroke();
context.restore();
}
function drawPlot() {
const { width, height } = resizeCanvas(plotCanvas, plotContext);
const ctx = plotContext;
ctx.clearRect(0, 0, width, height);
const background = ctx.createLinearGradient(0, 0, width, height);
background.addColorStop(0, "#081521");
background.addColorStop(1, "#050d17");
ctx.fillStyle = background;
ctx.fillRect(0, 0, width, height);
const plotRect = {
x: 43,
y: 24,
width: width - 62,
height: height - 58
};
ctx.strokeStyle = "rgba(147, 183, 199, 0.12)";
ctx.lineWidth = 1;
for (let i = 0; i <= 5; i++) {
const x = plotRect.x + plotRect.width * i / 5;
const y = plotRect.y + plotRect.height * i / 5;
ctx.beginPath();
ctx.moveTo(x, plotRect.y);
ctx.lineTo(x, plotRect.y + plotRect.height);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(plotRect.x, y);
ctx.lineTo(plotRect.x + plotRect.width, y);
ctx.stroke();
}
ctx.fillStyle = "#75919f";
ctx.font = "600 9px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.textAlign = "center";
for (let i = 0; i <= 5; i++) {
const value = i / 5;
ctx.fillText(value.toFixed(1), plotRect.x + plotRect.width * i / 5, height - 20);
ctx.fillText(value.toFixed(1), 27, plotRect.y + plotRect.height - plotRect.height * i / 5 + 3);
}
const metadata = featureSpaces[state.space];
ctx.font = "700 9px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.fillStyle = "#b5cbd5";
ctx.fillText(metadata.x.toUpperCase(), plotRect.x + plotRect.width / 2, height - 5);
ctx.save();
ctx.translate(10, plotRect.y + plotRect.height / 2);
ctx.rotate(-Math.PI / 2);
ctx.fillText(metadata.y.toUpperCase(), 0, 0);
ctx.restore();
const snapshot = state.snapshots[state.index];
if (snapshot) {
drawDecisionLine(ctx, snapshot, plotRect, {
color: "#f7dc73",
width: 2.2,
dash: state.playing ? [5, 4] : []
});
}
for (const toy of toys) {
const [xValue, yValue] = toy[state.space];
const x = plotRect.x + xValue * plotRect.width;
const y = plotRect.y + (1 - yValue) * plotRect.height;
const selected = toy.id === state.selectedId;
ctx.save();
ctx.shadowColor = toy.label === "RECYCLE" ? "#63efd2" : "#ffb06b";
ctx.shadowBlur = selected ? 14 : 7;
ctx.fillStyle = toy.label === "RECYCLE" ? "#63efd2" : "#ffb06b";
if (toy.label === "RECYCLE") {
ctx.beginPath();
ctx.arc(x, y, selected ? 8 : 5.5, 0, Math.PI * 2);
ctx.fill();
} else {
ctx.fillRect(x - (selected ? 7 : 5), y - (selected ? 7 : 5),
selected ? 14 : 10, selected ? 14 : 10);
}
ctx.restore();
if (selected) {
ctx.strokeStyle = "#ffffff";
ctx.lineWidth = 1.5;
ctx.beginPath();
ctx.arc(x, y, 12, 0, Math.PI * 2);
ctx.stroke();
}
}
drawBoard();
}
function drawBoard() {
if (!boardContext || !boardTexture) return;
const ctx = boardContext;
const width = 512;
const height = 256;
ctx.clearRect(0, 0, width, height);
ctx.fillStyle = "#06101a";
ctx.fillRect(0, 0, width, height);
const metadata = featureSpaces[state.space];
ctx.fillStyle = "#63efd2";
ctx.font = "bold 17px monospace";
ctx.fillText(metadata.title.toUpperCase(), 18, 26);
ctx.fillStyle = "#f7dc73";
ctx.font = "bold 12px monospace";
ctx.textAlign = "right";
ctx.fillText("SYNTHETIC", 494, 25);
ctx.textAlign = "left";
const rect = { x: 20, y: 43, width: 472, height: 170 };
ctx.strokeStyle = "#183040";
ctx.lineWidth = 1;
for (let i = 0; i <= 5; i++) {
const x = rect.x + rect.width * i / 5;
const y = rect.y + rect.height * i / 5;
ctx.beginPath();
ctx.moveTo(x, rect.y);
ctx.lineTo(x, rect.y + rect.height);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(rect.x, y);
ctx.lineTo(rect.x + rect.width, y);
ctx.stroke();
}
const snapshot = state.snapshots[state.index];
if (snapshot) {
drawDecisionLine(ctx, snapshot, rect, {
color: "#f7dc73",
width: 3,
dash: state.playing ? [8, 6] : []
});
}
for (const toy of toys) {
const values = toy[state.space];
const x = rect.x + values[0] * rect.width;
const y = rect.y + (1 - values[1]) * rect.height;
ctx.fillStyle = toy.label === "RECYCLE" ? "#63efd2" : "#ffb06b";
if (toy.label === "RECYCLE") {
ctx.beginPath();
ctx.arc(x, y, 6, 0, Math.PI * 2);
ctx.fill();
} else {
ctx.fillRect(x - 5, y - 5, 10, 10);
}
}
ctx.fillStyle = "#8ca7b6";
ctx.font = "12px monospace";
ctx.fillText(`x: ${metadata.x}`, 20, 237);
ctx.fillText(`y: ${metadata.y}`, 208, 237);
boardTexture.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Direct camera and object interaction; no addons are used.
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function pickToy(event) {
const rect = sceneCanvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(pickables, false);
return hits.length ? hits[0].object.userData.toyId : null;
}
function setupCameraControls() {
let dragging = false;
let moved = false;
let lastX = 0;
let lastY = 0;
sceneCanvas.addEventListener("pointerdown", event => {
if (event.button !== 0) return;
dragging = true;
moved = false;
lastX = event.clientX;
lastY = event.clientY;
sceneCanvas.setPointerCapture(event.pointerId);
sceneCanvas.style.cursor = "grabbing";
});
sceneCanvas.addEventListener("pointermove", event => {
if (dragging) {
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
if (Math.abs(dx) + Math.abs(dy) > 3) moved = true;
window.__orbit.azimuth -= dx * 0.006;
window.__orbit.polar = THREE.MathUtils.clamp(
window.__orbit.polar + dy * 0.005, 0.48, 1.36
);
lastX = event.clientX;
lastY = event.clientY;
window.__updateCamera();
} else {
const id = pickToy(event);
sceneCanvas.style.cursor = id ? "pointer" : "grab";
}
});
sceneCanvas.addEventListener("pointerup", event => {
dragging = false;
sceneCanvas.style.cursor = "grab";
if (sceneCanvas.hasPointerCapture(event.pointerId)) {
sceneCanvas.releasePointerCapture(event.pointerId);
}
if (!moved) {
const id = pickToy(event);
if (id) setSelected(id);
}
});
sceneCanvas.addEventListener("pointercancel", () => {
dragging = false;
sceneCanvas.style.cursor = "grab";
});
sceneCanvas.addEventListener("wheel", event => {
event.preventDefault();
window.__orbit.radius = THREE.MathUtils.clamp(
window.__orbit.radius + event.deltaY * 0.012, 12, 28
);
window.__updateCamera();
}, { passive: false });
}
plotCanvas.addEventListener("pointerdown", event => {
const rect = plotCanvas.getBoundingClientRect();
const x = (event.clientX - rect.left - 43) / (rect.width - 62);
const y = 1 - (event.clientY - rect.top - 24) / (rect.height - 58);
let nearest = null;
let nearestDistance = Infinity;
for (const toy of toys) {
const values = toy[state.space];
const distance = Math.hypot(values[0] - x, values[1] - y);
if (distance < nearestDistance) {
nearestDistance = distance;
nearest = toy;
}
}
if (nearest && nearestDistance < 0.09) setSelected(nearest.id);
});
document.getElementById("geometryButton").addEventListener("click", () => {
setFeatureSpace("geometry");
});
document.getElementById("surfaceButton").addEventListener("click", () => {
setFeatureSpace("surface");
});
document.getElementById("replay").addEventListener("click", () => {
const result = trainPerceptron(state.space);
state.snapshots = result.snapshots;
state.best = result.best;
state.index = 0;
state.playing = true;
state.lastStep = performance.now();
updateTrainingUI();
});
window.addEventListener("resize", () => {
renderer.setSize(window.innerWidth, window.innerHeight, false);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
drawPlot();
});
// -------------------------------------------------------------------------
// Animation loop.
// -------------------------------------------------------------------------
function animate(time) {
if (state.playing && time - state.lastStep > 230) {
state.lastStep = time;
if (state.index < state.snapshots.length - 1) {
state.index++;
} else {
// For a non-separable basis, present the best checkpoint rather than
// an arbitrary late oscillation.
if (state.best.errors > 0) state.index = state.best.index;
state.playing = false;
}
updateTrainingUI();
}
for (const group of toyGroups) {
group.position.y = group.userData.baseY +
Math.sin(time * 0.0013 + group.userData.phase) * 0.025;
}
renderer.render(scene, camera);
}
try {
initThree();
setFeatureSpace("geometry", true);
document.getElementById("loading").remove();
renderer.setAnimationLoop(animate);
} catch (error) {
console.error(error);
const loading = document.getElementById("loading");
loading.textContent = "WEBGL INITIALIZATION FAILED";
loading.style.color = "#ffb06b";
}
</script>
</body>
</html>
```