Prompt specification
Build or customize the Three.js 3D scene "Equal counts different spacing" 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: Equal counts different spacing
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/equal-counts-different-spacing-x0326
Scene Description Prompt:
Make a low-poly Three.js scene in a self-contained HTML file using cdnjs: equal counts different spacing. Slide equal bead groups into compact and spread-out arrangements; a counting cursor visits each bead once to demonstrate equal quantity despite different occupied lengths.
Working Three.js Starter (equal-counts-different-spacing-x0326):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Same Beads, Different Spacing</title>
<style>
:root {
color-scheme: dark;
--bg: #07121d;
--panel: rgba(10, 25, 39, 0.84);
--border: rgba(153, 215, 231, 0.16);
--text: #edf9fb;
--muted: #91aeb9;
--cyan: #56dfd1;
--blue: #4e9bea;
--amber: #ffd166;
--coral: #ff8066;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
button {
font: inherit;
}
#stage,
#stage canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#stage canvas {
display: block;
touch-action: none;
cursor: grab;
}
body.dragging #stage canvas {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 48%, transparent 36%, rgba(1, 7, 13, 0.48) 100%),
linear-gradient(180deg, rgba(3, 10, 17, 0.22), transparent 28%, transparent 72%, rgba(3, 10, 17, 0.4));
}
.hero {
position: fixed;
top: 26px;
left: 28px;
z-index: 5;
width: min(440px, calc(100vw - 56px));
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 11px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 22px;
height: 2px;
border-radius: 2px;
background: currentColor;
content: "";
}
h1 {
max-width: 420px;
margin: 0;
font-size: clamp(30px, 4.1vw, 54px);
line-height: 0.98;
letter-spacing: -0.055em;
}
.hero p {
max-width: 410px;
margin: 15px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.stats {
position: fixed;
top: 24px;
right: 24px;
z-index: 6;
width: 310px;
padding: 16px;
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(16px);
pointer-events: none;
}
.stats-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
}
.stats-title span:first-child {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.equation {
padding: 4px 9px;
border: 1px solid rgba(86, 223, 209, 0.28);
border-radius: 999px;
color: var(--cyan);
font-size: 13px;
font-weight: 850;
transition: 180ms ease;
}
.equation.complete {
color: #07121d;
background: var(--cyan);
box-shadow: 0 0 24px rgba(86, 223, 209, 0.34);
}
.group-row {
display: grid;
grid-template-columns: 10px 1fr auto;
align-items: center;
gap: 10px;
min-height: 57px;
border-top: 1px solid rgba(153, 215, 231, 0.1);
}
.group-dot {
width: 8px;
height: 8px;
border-radius: 50%;
box-shadow: 0 0 14px currentColor;
}
.group-dot.a {
color: var(--cyan);
background: var(--cyan);
}
.group-dot.b {
color: var(--amber);
background: var(--amber);
}
.group-name {
min-width: 0;
}
.group-name strong {
display: block;
font-size: 13px;
}
.group-name small {
display: block;
overflow: hidden;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
text-overflow: ellipsis;
white-space: nowrap;
}
.group-count {
text-align: right;
}
.group-count b {
font-size: 21px;
line-height: 1;
}
.group-count small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
.progress-track {
height: 4px;
overflow: hidden;
margin-top: 11px;
border-radius: 999px;
background: rgba(255, 255, 255, 0.07);
}
.progress-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--cyan), var(--amber));
transition: width 140ms ease;
}
#status {
min-height: 32px;
margin: 10px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.controls {
position: fixed;
left: 50%;
bottom: 25px;
z-index: 8;
display: flex;
gap: 9px;
width: max-content;
max-width: calc(100vw - 24px);
padding: 8px;
border: 1px solid var(--border);
border-radius: 18px;
background: rgba(8, 21, 34, 0.9);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.34);
backdrop-filter: blur(18px);
transform: translateX(-50%);
}
.control-btn {
min-width: 142px;
padding: 10px 15px;
border: 1px solid transparent;
border-radius: 12px;
color: var(--text);
background: transparent;
cursor: pointer;
transition:
background 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
.control-btn:hover:not(:disabled) {
background: rgba(255, 255, 255, 0.055);
transform: translateY(-1px);
}
.control-btn:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 2px;
}
.control-btn strong,
.control-btn span {
display: block;
}
.control-btn strong {
font-size: 12px;
}
.control-btn span {
margin-top: 3px;
color: var(--muted);
font-size: 9px;
white-space: nowrap;
}
.control-btn[data-layout="compact"] {
border-color: rgba(86, 223, 209, 0.2);
}
.control-btn[data-layout="spread"] {
border-color: rgba(255, 209, 102, 0.2);
}
.control-btn[aria-pressed="true"] {
background: rgba(86, 223, 209, 0.12);
border-color: rgba(86, 223, 209, 0.42);
}
.control-btn[data-layout="spread"][aria-pressed="true"] {
background: rgba(255, 209, 102, 0.11);
border-color: rgba(255, 209, 102, 0.42);
}
.control-btn.count {
min-width: 160px;
color: #06121d;
background: var(--cyan);
}
.control-btn.count:hover:not(:disabled) {
background: #78ebdf;
}
.control-btn.count span {
color: rgba(6, 18, 29, 0.64);
}
.control-btn:disabled {
cursor: not-allowed;
opacity: 0.46;
}
.scene-label {
position: fixed;
top: 0;
left: 0;
z-index: 4;
display: flex;
align-items: center;
gap: 9px;
padding: 8px 10px;
border: 1px solid rgba(153, 215, 231, 0.13);
border-radius: 12px;
background: rgba(6, 18, 29, 0.74);
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.18);
backdrop-filter: blur(8px);
pointer-events: none;
will-change: transform;
}
.scene-label .letter {
display: grid;
width: 27px;
height: 27px;
place-items: center;
border-radius: 8px;
font-size: 12px;
font-weight: 900;
}
.scene-label.a .letter {
color: #06121d;
background: var(--cyan);
}
.scene-label.b .letter {
color: #211606;
background: var(--amber);
}
.scene-label b,
.scene-label small {
display: block;
}
.scene-label b {
font-size: 10px;
}
.scene-label small {
margin-top: 2px;
color: var(--muted);
font-size: 9px;
}
#cursorBadge {
position: fixed;
top: 0;
left: 0;
z-index: 9;
display: none;
min-width: 31px;
padding: 5px 8px;
border: 1px solid rgba(255, 255, 255, 0.25);
border-radius: 999px;
color: #07121d;
background: #f4ffff;
box-shadow: 0 7px 22px rgba(0, 0, 0, 0.35);
font-size: 12px;
font-weight: 900;
text-align: center;
pointer-events: none;
will-change: transform;
}
.camera-hint {
position: fixed;
right: 25px;
bottom: 27px;
z-index: 4;
color: rgba(176, 203, 212, 0.6);
font-size: 10px;
letter-spacing: 0.04em;
pointer-events: none;
}
@media (max-width: 760px) {
.hero {
top: 17px;
left: 17px;
width: calc(100vw - 34px);
}
h1 {
max-width: 330px;
font-size: 34px;
}
.hero p {
display: none;
}
.stats {
top: 104px;
right: 12px;
left: 12px;
width: auto;
padding: 11px 13px;
border-radius: 14px;
}
.group-row {
min-height: 48px;
}
.group-count b {
font-size: 18px;
}
.scene-label {
display: none;
}
.controls {
bottom: 12px;
gap: 5px;
padding: 6px;
}
.control-btn {
min-width: 0;
flex: 1;
padding: 9px 10px;
}
.control-btn span {
display: none;
}
.control-btn.count {
min-width: 128px;
}
.camera-hint {
display: none;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<div class="vignette"></div>
<header class="hero">
<div class="eyebrow">Equal quantity, unequal length</div>
<h1>Same beads.<br />Different spacing.</h1>
<p>
Slide the rows into compact and spread-out arrangements. Then let the
cursor visit every bead exactly once.
</p>
</header>
<section class="stats" aria-label="Counting results">
<div class="stats-title">
<span>Live tally</span>
<span class="equation" id="equation">8 = 8</span>
</div>
<div class="group-row">
<span class="group-dot a"></span>
<div class="group-name">
<strong>Group A</strong>
<small id="layoutA">Compact · 5.5 occupied units</small>
</div>
<div class="group-count">
<b id="countA">0</b>
<small>/ 8 counted</small>
</div>
</div>
<div class="group-row">
<span class="group-dot b"></span>
<div class="group-name">
<strong>Group B</strong>
<small id="layoutB">Spread · 8.3 occupied units</small>
</div>
<div class="group-count">
<b id="countB">0</b>
<small>/ 8 counted</small>
</div>
</div>
<div class="progress-track">
<div class="progress-fill" id="progressFill"></div>
</div>
<p id="status" aria-live="polite">
Ready: count all 16 beads, once each.
</p>
</section>
<div class="scene-label a" id="labelA">
<span class="letter">A</span>
<div>
<b class="arrangement">Compact</b>
<small class="span">8 beads · 5.5 units</small>
</div>
</div>
<div class="scene-label b" id="labelB">
<span class="letter">B</span>
<div>
<b class="arrangement">Spread</b>
<small class="span">8 beads · 8.3 units</small>
</div>
</div>
<div id="cursorBadge">0</div>
<nav class="controls" aria-label="Scene controls">
<button class="control-btn" data-layout="compact" aria-pressed="true">
<strong>Compact</strong>
<span>A tight · B wide</span>
</button>
<button class="control-btn" data-layout="spread" aria-pressed="false">
<strong>Spread out</strong>
<span>A wide · B tight</span>
</button>
<button class="control-btn count" id="countButton">
<strong id="countButtonText">Count every bead</strong>
<span id="countButtonSubtext">16 visits · no repeats</span>
</button>
</nav>
<div class="camera-hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const BEAD_COUNT = 8;
const BEAD_RADIUS = 0.24;
const BEAD_Y = 0.3;
const COMPACT_SPACING = 0.72;
const SPREAD_SPACING = 1.12;
const REDUCED_MOTION = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
const stage = document.querySelector("#stage");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07121d);
scene.fog = new THREE.Fog(0x07121d, 18, 48);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0.2, 0);
let cameraTheta = 0;
let cameraPhi = 0.92;
let zoomFactor = 1;
let baseCameraRadius = 15;
function updateCamera() {
const radius = baseCameraRadius * zoomFactor;
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + radius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + radius * Math.cos(cameraPhi),
cameraTarget.z + radius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
// Lighting
scene.add(new THREE.HemisphereLight(0x9eeaff, 0x17202b, 1.65));
const keyLight = new THREE.DirectionalLight(0xfff1d7, 2.7);
keyLight.position.set(-4, 9, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.0006;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x42d8dc, 18, 15, 2);
cyanLight.position.set(-5, 3, -2);
scene.add(cyanLight);
const amberLight = new THREE.PointLight(0xffa84b, 14, 14, 2);
amberLight.position.set(5, 2.5, 3);
scene.add(amberLight);
// Ground and grid
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(50, 50),
new THREE.MeshStandardMaterial({
color: 0x0a1824,
roughness: 0.96,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.08;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(26, 26, 0x244455, 0x132b39);
grid.position.y = -0.068;
grid.material.transparent = true;
grid.material.opacity = 0.4;
scene.add(grid);
const rowZ = [-1.1, 1.1];
const groupColors = [0x56dfd1, 0xffc857];
const palettes = [
[0x58e0c4, 0x43c9df, 0x4f91e9],
[0xffd166, 0xffa34f, 0xff7161]
];
const groups = [
{
name: "A",
z: rowZ[0],
color: groupColors[0],
beads: [],
length: 0
},
{
name: "B",
z: rowZ[1],
color: groupColors[1],
beads: [],
length: 0
}
];
function occupiedLength(spacing) {
return spacing * (BEAD_COUNT - 1) + BEAD_RADIUS * 2;
}
function spacingFor(groupIndex, mode) {
if (mode === "compact") {
return groupIndex === 0 ? COMPACT_SPACING : SPREAD_SPACING;
}
return groupIndex === 0 ? SPREAD_SPACING : COMPACT_SPACING;
}
// Tracks
const trackMaterial = new THREE.MeshStandardMaterial({
color: 0x173747,
roughness: 0.66,
metalness: 0.28
});
const railHighlightMaterial = new THREE.MeshStandardMaterial({
color: 0x2b6172,
roughness: 0.42,
metalness: 0.4
});
for (const group of groups) {
const track = new THREE.Mesh(
new THREE.BoxGeometry(9.2, 0.12, 0.13),
trackMaterial
);
track.position.set(0, 0, group.z);
track.receiveShadow = true;
scene.add(track);
const highlight = new THREE.Mesh(
new THREE.BoxGeometry(9.15, 0.025, 0.026),
railHighlightMaterial
);
highlight.position.set(0, 0.072, group.z - 0.043);
scene.add(highlight);
for (const side of [-1, 1]) {
const cap = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.13, 0.14, 8),
railHighlightMaterial
);
cap.position.set(side * 4.64, 0.015, group.z);
cap.castShadow = true;
scene.add(cap);
}
}
// Faceted beads
const beadGeometry = new THREE.IcosahedronGeometry(BEAD_RADIUS, 0);
for (let groupIndex = 0; groupIndex < groups.length; groupIndex++) {
const group = groups[groupIndex];
const spacing = spacingFor(groupIndex, "compact");
for (let index = 0; index < BEAD_COUNT; index++) {
const color = palettes[groupIndex][index % palettes[groupIndex].length];
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.38,
metalness: 0.13,
flatShading: true,
emissive: color,
emissiveIntensity: 0.075
});
const bead = new THREE.Mesh(beadGeometry, material);
bead.position.set(
(index - (BEAD_COUNT - 1) / 2) * spacing,
BEAD_Y,
group.z
);
bead.castShadow = true;
bead.receiveShadow = true;
bead.userData.groupIndex = groupIndex;
bead.userData.index = index;
bead.userData.pulseStart = null;
group.beads.push(bead);
scene.add(bead);
}
}
// Occupied-length bars beneath each row
function createLengthBar(group, groupIndex) {
const root = new THREE.Group();
root.position.set(0, 0.045, group.z + 0.62);
const bar = new THREE.Mesh(
new THREE.BoxGeometry(1, 0.035, 0.055),
new THREE.MeshBasicMaterial({ color: group.color })
);
const capGeometry = new THREE.BoxGeometry(0.045, 0.07, 0.11);
const capA = new THREE.Mesh(capGeometry, bar.material);
const capB = new THREE.Mesh(capGeometry, bar.material);
root.add(bar, capA, capB);
scene.add(root);
group.lengthBar = { root, bar, capA, capB };
}
function setLengthVisual(group, length) {
const visual = group.lengthBar;
visual.bar.scale.x = length;
visual.capA.position.x = -length / 2;
visual.capB.position.x = length / 2;
group.length = length;
}
groups.forEach(createLengthBar);
groups.forEach((group, index) => {
setLengthVisual(
group,
occupiedLength(spacingFor(index, "compact"))
);
});
// Small floating low-poly accents
const accentGeometry = new THREE.TetrahedronGeometry(0.09, 0);
const accents = [];
for (let index = 0; index < 20; index++) {
const color = index % 2 === 0 ? 0x275268 : 0x4a3e2b;
const accent = new THREE.Mesh(
accentGeometry,
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.55
})
);
const angle = index * 2.39996;
const radius = 5.2 + (index % 4) * 0.42;
accent.position.set(
Math.cos(angle) * radius,
1.25 + (index % 6) * 0.5,
-2.5 - (index % 5) * 0.55
);
accent.rotation.set(angle, angle * 0.4, angle * 0.7);
accents.push(accent);
scene.add(accent);
}
// Counting cursor
const cursor = new THREE.Group();
cursor.visible = false;
const cursorMaterial = new THREE.MeshStandardMaterial({
color: groupColors[0],
emissive: groupColors[0],
emissiveIntensity: 0.75,
roughness: 0.3,
metalness: 0.15,
flatShading: true
});
const cursorCone = new THREE.Mesh(
new THREE.ConeGeometry(0.13, 0.32, 5),
cursorMaterial
);
cursorCone.rotation.z = Math.PI;
cursorCone.position.y = 0.7;
cursor.add(cursorCone);
const cursorRingMaterial = new THREE.MeshBasicMaterial({
color: groupColors[0],
transparent: true,
opacity: 0.9
});
const cursorRing = new THREE.Mesh(
new THREE.TorusGeometry(0.36, 0.024, 5, 24),
cursorRingMaterial
);
cursorRing.rotation.x = Math.PI / 2;
cursorRing.position.y = BEAD_Y;
cursor.add(cursorRing);
scene.add(cursor);
const cursorBadge = document.querySelector("#cursorBadge");
function setCursorColor(groupIndex) {
cursorMaterial.color.setHex(groupColors[groupIndex]);
cursorMaterial.emissive.setHex(groupColors[groupIndex]);
cursorRingMaterial.color.setHex(groupColors[groupIndex]);
}
// UI references
const layoutButtons = [...document.querySelectorAll("[data-layout]")];
const countButton = document.querySelector("#countButton");
const countButtonText = document.querySelector("#countButtonText");
const countButtonSubtext = document.querySelector("#countButtonSubtext");
const countA = document.querySelector("#countA");
const countB = document.querySelector("#countB");
const layoutA = document.querySelector("#layoutA");
const layoutB = document.querySelector("#layoutB");
const status = document.querySelector("#status");
const progressFill = document.querySelector("#progressFill");
const equation = document.querySelector("#equation");
const labelA = document.querySelector("#labelA");
const labelB = document.querySelector("#labelB");
let layoutMode = "compact";
let layoutTween = null;
const counting = {
active: false,
index: 0,
total: 0,
groupCounts: [0, 0],
visited: new Set()
};
const sequence = [];
for (let groupIndex = 0; groupIndex < groups.length; groupIndex++) {
for (let index = 0; index < BEAD_COUNT; index++) {
sequence.push({ groupIndex, index });
}
}
let segmentFrom = new THREE.Vector3();
let segmentTo = new THREE.Vector3();
let segmentStart = 0;
let segmentDuration = 500;
let segmentCounted = false;
function arrangementName(spacing) {
return spacing === COMPACT_SPACING ? "Compact" : "Spread";
}
function updateUI() {
const aSpacing = spacingFor(0, layoutMode);
const bSpacing = spacingFor(1, layoutMode);
const aLength = occupiedLength(aSpacing);
const bLength = occupiedLength(bSpacing);
layoutA.textContent =
`${arrangementName(aSpacing)} · ${aLength.toFixed(1)} occupied units`;
layoutB.textContent =
`${arrangementName(bSpacing)} · ${bLength.toFixed(1)} occupied units`;
labelA.querySelector(".arrangement").textContent =
arrangementName(aSpacing);
labelB.querySelector(".arrangement").textContent =
arrangementName(bSpacing);
labelA.querySelector(".span").textContent =
`8 beads · ${aLength.toFixed(1)} units`;
labelB.querySelector(".span").textContent =
`8 beads · ${bLength.toFixed(1)} units`;
for (const button of layoutButtons) {
button.setAttribute(
"aria-pressed",
String(button.dataset.layout === layoutMode)
);
button.disabled = counting.active;
}
countA.textContent = counting.groupCounts[0];
countB.textContent = counting.groupCounts[1];
progressFill.style.width =
`${(counting.total / sequence.length) * 100}%`;
countButton.disabled = layoutTween !== null || counting.active;
if (counting.active) {
countButtonText.textContent =
`Counting ${counting.total}/${sequence.length}`;
countButtonSubtext.textContent = "One visit per bead";
status.textContent =
`Cursor visit ${counting.total + 1}: ` +
`${groups[sequence[counting.index].groupIndex].name}, ` +
`bead ${sequence[counting.index].index + 1}.`;
} else if (counting.total === sequence.length) {
countButtonText.textContent = "Count again";
countButtonSubtext.textContent = "16 visits · no repeats";
status.textContent =
"Complete: 8 + 8 beads were visited once each. Equal quantity, different occupied lengths.";
} else if (layoutTween) {
countButtonText.textContent = "Sliding…";
countButtonSubtext.textContent = "Wait for the rows to settle";
status.textContent = "The equal bead groups are changing spacing.";
} else {
countButtonText.textContent = "Count every bead";
countButtonSubtext.textContent = "16 visits · no repeats";
status.textContent = "Ready: count all 16 beads, once each.";
}
equation.classList.toggle("complete", counting.total === sequence.length);
}
function resetCount() {
counting.active = false;
counting.index = 0;
counting.total = 0;
counting.groupCounts = [0, 0];
counting.visited.clear();
cursor.visible = false;
cursorBadge.style.display = "none";
updateUI();
}
function startLayoutTween(mode) {
if (counting.active || mode === layoutMode) {
return;
}
layoutMode = mode;
resetCount();
const fromPositions = [];
const toPositions = [];
const fromLengths = [];
const toLengths = [];
for (let groupIndex = 0; groupIndex < groups.length; groupIndex++) {
const group = groups[groupIndex];
const spacing = spacingFor(groupIndex, mode);
fromLengths.push(group.length);
toLengths.push(occupiedLength(spacing));
for (let index = 0; index < BEAD_COUNT; index++) {
fromPositions.push(group.beads[index].position.clone());
toPositions.push(
new THREE.Vector3(
(index - (BEAD_COUNT - 1) / 2) * spacing,
BEAD_Y,
group.z
)
);
}
}
layoutTween = {
start: performance.now(),
duration: REDUCED_MOTION ? 1 : 900,
fromPositions,
toPositions,
fromLengths,
toLengths
};
updateUI();
}
function beginCounting() {
if (layoutTween) {
return;
}
counting.active = true;
counting.index = 0;
counting.total = 0;
counting.groupCounts = [0, 0];
counting.visited.clear();
cursor.visible = true;
cursorBadge.style.display = "block";
cursorBadge.textContent = "0";
setCursorColor(0);
const first = sequence[0];
const firstBead = groups[first.groupIndex].beads[first.index];
segmentFrom.set(0, 0, 5.2);
segmentTo.set(firstBead.position.x, 0, firstBead.position.z);
segmentStart = performance.now();
segmentDuration = REDUCED_MOTION ? 100 : 720;
segmentCounted = false;
updateUI();
}
layoutButtons.forEach((button) => {
button.addEventListener("click", () => {
startLayoutTween(button.dataset.layout);
});
});
countButton.addEventListener("click", beginCounting);
// Custom camera controls, implemented directly without addons.
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
document.body.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - lastPointerX;
const deltaY = event.clientY - lastPointerY;
cameraTheta -= deltaX * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - deltaY * 0.005,
0.62,
1.18
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateCamera();
});
function stopDragging(event) {
dragging = false;
document.body.classList.remove("dragging");
if (
event &&
renderer.domElement.hasPointerCapture &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
zoomFactor = THREE.MathUtils.clamp(
zoomFactor * Math.exp(event.deltaY * 0.001),
0.78,
1.45
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraTheta = 0;
cameraPhi = 0.92;
zoomFactor = 1;
updateCamera();
});
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
function onResize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
const halfFov = THREE.MathUtils.degToRad(camera.fov / 2);
baseCameraRadius = Math.max(
14.5,
10.2 / (2 * Math.tan(halfFov) * camera.aspect)
);
updateCamera();
}
window.addEventListener("resize", onResize);
onResize();
function smoothStep(t) {
return t * t * (3 - 2 * t);
}
function projectElement(element, worldPosition, offsetX = 0, offsetY = 0) {
const projected = worldPosition.clone().project(camera);
const x = (projected.x * 0.5 + 0.5) * window.innerWidth + offsetX;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight + offsetY;
element.style.transform =
`translate(${x}px, ${y}px) translate(${offsetX ? "0" : "-100%"}, -50%)`;
}
const labelAnchorA = new THREE.Vector3(-4.55, 0.52, groups[0].z);
const labelAnchorB = new THREE.Vector3(-4.55, 0.52, groups[1].z);
const badgePosition = new THREE.Vector3();
let previousTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
// Smoothly slide every bead between layout targets.
if (layoutTween) {
const raw = THREE.MathUtils.clamp(
(now - layoutTween.start) / layoutTween.duration,
0,
1
);
const eased = smoothStep(raw);
for (let groupIndex = 0; groupIndex < groups.length; groupIndex++) {
const group = groups[groupIndex];
for (let index = 0; index < BEAD_COUNT; index++) {
const flatIndex = groupIndex * BEAD_COUNT + index;
group.beads[index].position.lerpVectors(
layoutTween.fromPositions[flatIndex],
layoutTween.toPositions[flatIndex],
eased
);
}
const length = THREE.MathUtils.lerp(
layoutTween.fromLengths[groupIndex],
layoutTween.toLengths[groupIndex],
eased
);
setLengthVisual(group, length);
}
if (raw >= 1) {
layoutTween = null;
updateUI();
}
}
// Pulse each bead exactly when the cursor arrives.
for (const group of groups) {
for (const bead of group.beads) {
if (bead.userData.pulseStart === null) continue;
const pulse = (now - bead.userData.pulseStart) / 360;
if (pulse >= 1) {
bead.scale.setScalar(1);
bead.userData.pulseStart = null;
} else {
bead.scale.setScalar(1 + Math.sin(pulse * Math.PI) * 0.24);
}
}
}
// Move the cursor through one unique bead at a time.
if (counting.active) {
const elapsed = now - segmentStart;
const t = THREE.MathUtils.clamp(elapsed / segmentDuration, 0, 1);
const eased = smoothStep(t);
cursor.position.lerpVectors(segmentFrom, segmentTo, eased);
cursorCone.position.y = 0.7 + Math.sin(now * 0.012) * 0.045;
cursorRing.rotation.z += delta * 0.7;
cursorRing.scale.setScalar(1 + Math.sin(now * 0.009) * 0.045);
if (t >= 1 && !segmentCounted) {
segmentCounted = true;
const item = sequence[counting.index];
const key = `${item.groupIndex}:${item.index}`;
if (!counting.visited.has(key)) {
counting.visited.add(key);
counting.groupCounts[item.groupIndex] += 1;
counting.total += 1;
const bead = groups[item.groupIndex].beads[item.index];
bead.userData.pulseStart = now;
cursorBadge.textContent = String(counting.total);
}
if (counting.index < sequence.length - 1) {
counting.index += 1;
const nextItem = sequence[counting.index];
const nextBead =
groups[nextItem.groupIndex].beads[nextItem.index];
segmentFrom.copy(cursor.position);
segmentTo.set(nextBead.position.x, 0, nextBead.position.z);
segmentStart = now;
segmentDuration = REDUCED_MOTION ? 90 : 390;
segmentCounted = false;
setCursorColor(nextItem.groupIndex);
} else {
counting.active = false;
cursorBadge.textContent = String(counting.total);
}
updateUI();
}
}
for (let index = 0; index < accents.length; index++) {
const accent = accents[index];
accent.rotation.x += delta * (0.12 + (index % 3) * 0.025);
accent.rotation.y += delta * (0.16 + (index % 4) * 0.025);
accent.position.y += Math.sin(now * 0.0008 + index) * delta * 0.035;
}
projectElement(labelA, labelAnchorA);
projectElement(labelB, labelAnchorB);
if (cursor.visible) {
badgePosition.copy(cursor.position);
badgePosition.y += 1.05;
projectElement(cursorBadge, badgePosition, 0, 0);
cursorBadge.style.transform =
`translate(${cursorBadge.style.left || "0"}, ${cursorBadge.style.top || "0"})`;
}
// Position the badge manually after projection.
if (cursor.visible) {
const projected = badgePosition.project(camera);
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
cursorBadge.style.left = `${x}px`;
cursorBadge.style.top = `${y}px`;
cursorBadge.style.transform = "translate(-50%, -100%)";
}
renderer.render(scene, camera);
}
updateUI();
requestAnimationFrame(animate);
</script>
</body>
</html>
```