Prompt specification
Build or customize the Three.js 3D scene "Every Box Has a Ball Versus One Shared Ball" 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: Every Box Has a Ball Versus One Shared Ball
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/every-box-has-a-ball-versus-one-shared-ball-x3408
Scene Description Prompt:
Compare a ball for every box with one shared ball in a low-poly quantifier puzzle. Make one HTML file with Three.js from cdnjs.
Working Three.js Starter (every-box-has-a-ball-versus-one-shared-ball-x3408):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>One Ball or Four? — Quantifier Puzzle</title>
<style>
:root {
--ink: #26333b;
--muted: #6f797d;
--paper: #f4f0e8;
--panel: #fffdf8;
--line: #ded8cc;
--coral: #f06f52;
--coral-dark: #c94d35;
--teal: #2fb7ad;
--teal-dark: #197e78;
--yellow: #f4c654;
--shadow: 0 14px 40px rgba(50, 58, 56, 0.10);
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--paper);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button {
font: inherit;
}
#app {
min-height: 100dvh;
display: grid;
grid-template-rows: auto minmax(390px, 1fr) auto;
overflow: hidden;
}
.topbar {
position: relative;
z-index: 10;
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
padding: 17px clamp(18px, 3vw, 42px);
background: rgba(255, 253, 248, 0.9);
border-bottom: 1px solid var(--line);
backdrop-filter: blur(14px);
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
position: relative;
width: 42px;
height: 42px;
flex: 0 0 auto;
border-radius: 13px;
background: var(--ink);
box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.18);
}
.brand-mark::before {
content: "";
position: absolute;
left: 10px;
top: 9px;
width: 20px;
height: 20px;
background: var(--coral);
border-radius: 50%;
transform: rotate(-12deg);
clip-path: polygon(
50% 0%,
88% 18%,
100% 58%,
69% 91%,
29% 100%,
0 68%,
10% 26%
);
}
.brand-mark::after {
content: "";
position: absolute;
right: 7px;
bottom: 6px;
width: 10px;
height: 10px;
border-radius: 3px;
background: var(--yellow);
transform: rotate(15deg);
}
.brand-copy small {
display: block;
margin-bottom: 2px;
color: var(--coral-dark);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.brand-copy h1 {
margin: 0;
font-size: clamp(17px, 2vw, 24px);
line-height: 1.05;
letter-spacing: -0.04em;
}
.view-controls {
display: flex;
align-items: center;
gap: 5px;
padding: 5px;
border: 1px solid var(--line);
border-radius: 13px;
background: #eee9df;
}
.view-controls button {
min-height: 34px;
padding: 7px 12px;
border: 0;
border-radius: 9px;
color: var(--muted);
background: transparent;
cursor: pointer;
font-size: 12px;
font-weight: 750;
transition: 160ms ease;
}
.view-controls button:hover {
color: var(--ink);
background: rgba(255, 255, 255, 0.55);
}
.view-controls button.active {
color: var(--ink);
background: var(--panel);
box-shadow: 0 2px 7px rgba(40, 45, 42, 0.10);
}
#stage {
position: relative;
min-height: 390px;
overflow: hidden;
isolation: isolate;
background:
radial-gradient(circle at 50% 55%, rgba(255, 255, 255, 0.9), transparent 52%),
var(--paper);
}
#stage canvas {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
outline: none;
}
#stage canvas:active {
cursor: grabbing;
}
.scene-label {
position: absolute;
z-index: 3;
top: 20px;
display: grid;
grid-template-columns: 1fr auto;
grid-template-areas:
"eyebrow count"
"title count";
column-gap: 18px;
width: min(310px, calc(50% - 38px));
padding: 13px 15px;
text-align: left;
border: 1px solid rgba(70, 77, 73, 0.13);
border-radius: 15px;
color: var(--ink);
background: rgba(255, 253, 248, 0.86);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
cursor: pointer;
transition:
transform 180ms ease,
border-color 180ms ease,
box-shadow 180ms ease;
}
.scene-label:hover,
.scene-label:focus-visible {
transform: translateY(-3px);
outline: none;
border-color: rgba(70, 77, 73, 0.30);
box-shadow: 0 18px 44px rgba(50, 58, 56, 0.16);
}
.scene-label.left {
left: clamp(14px, 3vw, 42px);
border-top: 4px solid var(--coral);
}
.scene-label.right {
right: clamp(14px, 3vw, 42px);
border-top: 4px solid var(--teal);
}
.scene-label .eyebrow {
grid-area: eyebrow;
color: var(--muted);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.scene-label strong {
grid-area: title;
margin-top: 3px;
font-size: clamp(14px, 1.6vw, 19px);
letter-spacing: -0.025em;
}
.count-badge {
grid-area: count;
align-self: center;
min-width: 54px;
padding: 8px 10px;
border-radius: 12px;
text-align: center;
color: transparent;
background: rgba(38, 51, 59, 0.08);
font-size: 13px;
font-weight: 850;
opacity: 0;
transform: scale(0.8);
transition: 300ms cubic-bezier(0.2, 1.4, 0.4, 1);
}
.revealed .count-badge {
color: var(--ink);
opacity: 1;
transform: scale(1);
}
.left .count-badge {
background: rgba(240, 111, 82, 0.15);
}
.right .count-badge {
background: rgba(47, 183, 173, 0.15);
}
.center-badge {
position: absolute;
z-index: 2;
left: 50%;
top: 24px;
display: grid;
place-items: center;
width: 35px;
height: 35px;
border: 1px solid rgba(70, 77, 73, 0.13);
border-radius: 50%;
color: #8a918f;
background: rgba(255, 253, 248, 0.8);
font-size: 18px;
font-weight: 800;
transform: translateX(-50%);
backdrop-filter: blur(10px);
}
.camera-hint {
position: absolute;
z-index: 3;
left: 50%;
bottom: 13px;
padding: 7px 11px;
border-radius: 999px;
color: #747d7d;
background: rgba(255, 253, 248, 0.72);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.03em;
transform: translateX(-50%);
pointer-events: none;
backdrop-filter: blur(8px);
}
.puzzle-panel {
position: relative;
z-index: 10;
display: grid;
grid-template-columns: minmax(250px, 1fr) minmax(360px, 1.15fr);
align-items: center;
gap: 30px;
padding: 19px clamp(18px, 3vw, 42px) 22px;
border-top: 1px solid var(--line);
background: var(--panel);
box-shadow: 0 -10px 35px rgba(50, 58, 56, 0.05);
}
.question-kicker {
display: block;
margin-bottom: 4px;
color: var(--coral-dark);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.question-block h2 {
margin: 0 0 5px;
font-size: clamp(18px, 2.1vw, 25px);
letter-spacing: -0.035em;
}
.feedback {
min-height: 20px;
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.feedback.good {
color: var(--teal-dark);
font-weight: 700;
}
.feedback.near {
color: var(--coral-dark);
font-weight: 700;
}
.answer-area {
display: flex;
align-items: center;
justify-content: flex-end;
gap: 10px;
}
.answers {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
flex: 1;
}
.answer-button {
position: relative;
min-height: 54px;
padding: 11px 13px;
border: 1px solid var(--line);
border-radius: 13px;
color: var(--ink);
background: #f7f3eb;
cursor: pointer;
font-size: 12px;
font-weight: 800;
text-align: left;
transition: 160ms ease;
}
.answer-button:hover:not(:disabled) {
transform: translateY(-2px);
border-color: #aaa99f;
background: #fff;
box-shadow: 0 7px 18px rgba(45, 50, 48, 0.08);
}
.answer-button:focus-visible {
outline: 3px solid rgba(47, 183, 173, 0.25);
outline-offset: 2px;
}
.answer-button.correct {
border-color: var(--teal);
color: #12665f;
background: rgba(47, 183, 173, 0.12);
}
.answer-button.incorrect {
border-color: var(--coral);
color: #a53c29;
background: rgba(240, 111, 82, 0.11);
}
.answer-button:disabled {
cursor: default;
}
.try-again {
min-height: 40px;
padding: 8px 12px;
border: 0;
border-radius: 10px;
color: var(--ink);
background: #ebe6dc;
cursor: pointer;
font-size: 11px;
font-weight: 800;
opacity: 0;
pointer-events: none;
transform: translateX(5px);
transition: 180ms ease;
}
.try-again.visible {
opacity: 1;
pointer-events: auto;
transform: translateX(0);
}
@media (max-width: 760px) {
#app {
grid-template-rows: auto minmax(460px, 1fr) auto;
overflow: auto;
}
.topbar {
align-items: flex-start;
flex-direction: column;
gap: 12px;
}
.view-controls {
width: 100%;
overflow-x: auto;
}
.view-controls button {
flex: 1 0 auto;
}
.scene-label {
top: 10px;
width: calc(50% - 22px);
padding: 10px 11px;
}
.scene-label.left {
left: 10px;
}
.scene-label.right {
right: 10px;
}
.scene-label .eyebrow {
font-size: 8px;
}
.scene-label strong {
font-size: 13px;
}
.count-badge {
min-width: 43px;
padding: 6px;
font-size: 11px;
}
.center-badge {
top: 91px;
}
.puzzle-panel {
grid-template-columns: 1fr;
gap: 13px;
padding: 17px;
}
.answer-area {
align-items: stretch;
flex-direction: column;
}
.answers {
grid-template-columns: 1fr;
}
.try-again {
align-self: flex-end;
}
}
</style>
</head>
<body>
<div id="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div class="brand-copy">
<small>Quantifier Lab</small>
<h1>One ball. Or four?</h1>
</div>
</div>
<nav class="view-controls" aria-label="Scene views">
<button id="compareView" class="active" type="button">Compare</button>
<button id="individualView" type="button">Every box</button>
<button id="sharedView" type="button">Shared ball</button>
</nav>
</header>
<main id="stage">
<div class="scene-label left" id="leftScene">
<span class="eyebrow">Option A</span>
<strong>A ball for every box</strong>
<span class="count-badge" id="leftCount">4 balls</span>
</div>
<div class="center-badge" aria-hidden="true">?</div>
<div class="scene-label right" id="rightScene">
<span class="eyebrow">Option B</span>
<strong>One shared ball</strong>
<span class="count-badge" id="rightCount">1 ball</span>
</div>
<div class="camera-hint">Drag to orbit · Scroll or pinch to zoom</div>
</main>
<section class="puzzle-panel" aria-labelledby="questionTitle">
<div class="question-block">
<span class="question-kicker">Count & compare</span>
<h2 id="questionTitle">Which setup uses fewer balls?</h2>
<p id="feedback" class="feedback" aria-live="polite">
Look at both scenes and make your choice.
</p>
</div>
<div class="answer-area">
<div class="answers">
<button class="answer-button" id="answerIndividual" type="button">
One ball in each box
</button>
<button class="answer-button" id="answerShared" type="button">
One ball shared by all boxes
</button>
</div>
<button id="tryAgain" class="try-again" type="button">Try again</button>
</div>
</section>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf4f0e8);
scene.fog = new THREE.Fog(0xf4f0e8, 24, 58);
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 100);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D puzzle comparing four individual balls with one shared ball."
);
stage.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xfffaf0, 0x84918f, 2.35);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff3d7, 3.4);
keyLight.position.set(-5, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -15;
keyLight.shadow.camera.right = 15;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xa8e7df, 1.25);
rimLight.position.set(10, 6, -7);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0xebe6dc,
roughness: 0.95,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.13;
floor.receiveShadow = true;
scene.add(floor);
const individualGroup = new THREE.Group();
individualGroup.position.x = -4.2;
scene.add(individualGroup);
const sharedGroup = new THREE.Group();
sharedGroup.position.x = 4.2;
scene.add(sharedGroup);
function makePlatform(group, color) {
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(3.08, 3.2, 0.18, 12),
new THREE.MeshStandardMaterial({
color,
roughness: 0.83,
metalness: 0,
flatShading: true
})
);
platform.position.y = -0.01;
platform.receiveShadow = true;
platform.castShadow = true;
group.add(platform);
const lowerRing = new THREE.Mesh(
new THREE.CylinderGeometry(3.22, 3.27, 0.08, 12),
new THREE.MeshStandardMaterial({
color: 0xcfc8bb,
roughness: 1,
flatShading: true
})
);
lowerRing.position.y = -0.13;
lowerRing.receiveShadow = true;
group.add(lowerRing);
}
makePlatform(individualGroup, 0xf0d7c6);
makePlatform(sharedGroup, 0xd2e8df);
const woodMaterial = new THREE.MeshStandardMaterial({
color: 0xc8945d,
roughness: 0.86,
metalness: 0,
flatShading: true
});
const lightWoodMaterial = new THREE.MeshStandardMaterial({
color: 0xe5c38f,
roughness: 0.84,
metalness: 0,
flatShading: true
});
const insideMaterial = new THREE.MeshStandardMaterial({
color: 0x8e6748,
roughness: 0.95,
flatShading: true
});
const coralMaterial = new THREE.MeshStandardMaterial({
color: 0xf06f52,
roughness: 0.42,
metalness: 0.02,
flatShading: true
});
const tealMaterial = new THREE.MeshStandardMaterial({
color: 0x35c3b8,
roughness: 0.35,
metalness: 0.03,
flatShading: true
});
const yellowMaterial = new THREE.MeshStandardMaterial({
color: 0xf4c654,
roughness: 0.45,
flatShading: true
});
const boxPositions = [
[-0.95, -1.05],
[0.95, -1.05],
[-0.95, 1.05],
[0.95, 1.05]
];
function makeCrate(group, x, z, accentMaterial) {
const crate = new THREE.Group();
crate.position.set(x, 0, z);
const width = 1.5;
const depth = 1.15;
const height = 0.68;
const thickness = 0.11;
const bottom = new THREE.Mesh(
new THREE.BoxGeometry(width, 0.14, depth),
insideMaterial
);
bottom.position.y = 0.17;
bottom.castShadow = true;
bottom.receiveShadow = true;
crate.add(bottom);
const frontBackGeometry = new THREE.BoxGeometry(
width,
height,
thickness
);
for (const side of [-1, 1]) {
const wall = new THREE.Mesh(frontBackGeometry, woodMaterial);
wall.position.set(0, 0.17 + height / 2, side * depth / 2);
wall.castShadow = true;
wall.receiveShadow = true;
crate.add(wall);
const trim = new THREE.Mesh(
new THREE.BoxGeometry(width + 0.04, 0.08, thickness + 0.04),
accentMaterial
);
trim.position.set(0, 0.17 + height, side * depth / 2);
trim.castShadow = true;
crate.add(trim);
}
const sideGeometry = new THREE.BoxGeometry(
thickness,
height,
depth - thickness * 2
);
for (const side of [-1, 1]) {
const wall = new THREE.Mesh(sideGeometry, lightWoodMaterial);
wall.position.set(side * width / 2, 0.17 + height / 2, 0);
wall.castShadow = true;
wall.receiveShadow = true;
crate.add(wall);
const trim = new THREE.Mesh(
new THREE.BoxGeometry(
thickness + 0.04,
0.08,
depth - thickness * 2 + 0.04
),
accentMaterial
);
trim.position.set(side * width / 2, 0.17 + height, 0);
trim.castShadow = true;
crate.add(trim);
}
group.add(crate);
return crate;
}
const individualBalls = [];
boxPositions.forEach(([x, z], index) => {
makeCrate(
individualGroup,
x,
z,
index % 2 === 0 ? coralMaterial : yellowMaterial
);
const ball = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.43, 1),
index % 2 === 0 ? coralMaterial : yellowMaterial
);
ball.position.set(x, 1.01, z);
ball.castShadow = true;
ball.receiveShadow = true;
ball.userData.phase = index * 1.35;
individualGroup.add(ball);
individualBalls.push(ball);
});
boxPositions.forEach(([x, z], index) => {
makeCrate(
sharedGroup,
x,
z,
index % 2 === 0 ? tealMaterial : yellowMaterial
);
});
const sharedPedestal = new THREE.Mesh(
new THREE.CylinderGeometry(0.55, 0.68, 0.28, 10),
new THREE.MeshStandardMaterial({
color: 0x6cbfb4,
roughness: 0.58,
flatShading: true
})
);
sharedPedestal.position.y = 0.2;
sharedPedestal.castShadow = true;
sharedPedestal.receiveShadow = true;
sharedGroup.add(sharedPedestal);
const sharedBall = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.58, 1),
tealMaterial
);
sharedBall.position.set(0, 1.34, 0);
sharedBall.castShadow = true;
sharedBall.receiveShadow = true;
sharedGroup.add(sharedBall);
const haloMaterial = new THREE.MeshBasicMaterial({
color: 0x4ad8cc,
transparent: true,
opacity: 0.62,
depthWrite: false
});
const sharedHalo = new THREE.Mesh(
new THREE.TorusGeometry(0.79, 0.025, 4, 32),
haloMaterial
);
sharedHalo.position.copy(sharedBall.position);
sharedHalo.rotation.x = Math.PI / 2;
sharedGroup.add(sharedHalo);
const rayMaterial = new THREE.LineDashedMaterial({
color: 0x48bfb5,
dashSize: 0.18,
gapSize: 0.13,
transparent: true,
opacity: 0.58
});
boxPositions.forEach(([x, z]) => {
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 1.34, 0),
new THREE.Vector3(x, 0.85, z)
]);
const ray = new THREE.Line(geometry, rayMaterial);
ray.computeLineDistances();
sharedGroup.add(ray);
});
// Small low-poly ground decorations.
const decorationGeometry = new THREE.TetrahedronGeometry(0.13);
const decorationMaterials = [
new THREE.MeshStandardMaterial({
color: 0xd8cfbf,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xb8d8ce,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xe8c8b7,
roughness: 1,
flatShading: true
})
];
[
[-8.6, -3.1],
[-7.9, 2.5],
[8.2, -2.6],
[7.8, 2.6],
[-11, 1.2],
[11.3, 0.8]
].forEach(([x, z], index) => {
const decoration = new THREE.Mesh(
decorationGeometry,
decorationMaterials[index % decorationMaterials.length]
);
decoration.position.set(x, 0.1, z);
decoration.rotation.set(index * 0.7, index * 0.4, index * 0.2);
decoration.castShadow = true;
scene.add(decoration);
});
// Custom camera controls: no addons or example modules are used.
const view = {
theta: 0.08,
phi: 0.91,
radius: 10,
target: new THREE.Vector3(0, 0.45, 0)
};
const desiredView = {
theta: view.theta,
phi: view.phi,
radius: view.radius,
target: view.target.clone()
};
let viewMode = "compare";
let dragging = false;
let previousX = 0;
let previousY = 0;
const verticalHalfFov = THREE.MathUtils.degToRad(camera.fov / 2);
const tanHalfFov = Math.tan(verticalHalfFov);
function compareRadius() {
return Math.max(
9.8,
15.5 / (2 * tanHalfFov * Math.max(camera.aspect, 0.45))
);
}
function focusRadius() {
return Math.max(
7.4,
7.2 / (2 * tanHalfFov * Math.max(camera.aspect, 0.45))
);
}
function setView(mode) {
viewMode = mode;
document.querySelectorAll(".view-controls button").forEach((button) => {
button.classList.remove("active");
});
if (mode === "compare") {
document.getElementById("compareView").classList.add("active");
desiredView.target.set(0, 0.45, 0);
desiredView.radius = compareRadius();
} else if (mode === "individual") {
document.getElementById("individualView").classList.add("active");
desiredView.target.set(individualGroup.position.x, 0.45, 0);
desiredView.radius = focusRadius();
} else {
document.getElementById("sharedView").classList.add("active");
desiredView.target.set(sharedGroup.position.x, 0.45, 0);
desiredView.radius = focusRadius();
}
}
document.getElementById("compareView").addEventListener("click", () => {
setView("compare");
});
document.getElementById("individualView").addEventListener("click", () => {
setView("individual");
});
document.getElementById("sharedView").addEventListener("click", () => {
setView("shared");
});
document.getElementById("leftScene").addEventListener("click", () => {
setView("individual");
});
document.getElementById("rightScene").addEventListener("click", () => {
setView("shared");
});
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.focus({ preventScroll: true });
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
desiredView.theta -= dx * 0.006;
desiredView.phi = THREE.MathUtils.clamp(
desiredView.phi - dy * 0.005,
0.46,
1.22
);
previousX = event.clientX;
previousY = event.clientY;
});
function stopDragging(event) {
dragging = false;
if (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();
desiredView.radius = THREE.MathUtils.clamp(
desiredView.radius + event.deltaY * 0.008,
6.3,
36
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
setView("compare");
});
renderer.domElement.addEventListener("keydown", (event) => {
const key = event.key;
if (key === "ArrowLeft") desiredView.theta += 0.08;
else if (key === "ArrowRight") desiredView.theta -= 0.08;
else if (key === "ArrowUp") {
desiredView.phi = Math.max(0.46, desiredView.phi - 0.06);
} else if (key === "ArrowDown") {
desiredView.phi = Math.min(1.22, desiredView.phi + 0.06);
} else if (key === "+" || key === "=") {
desiredView.radius = Math.max(6.3, desiredView.radius - 0.7);
} else if (key === "-") {
desiredView.radius = Math.min(36, desiredView.radius + 0.7);
} else return;
event.preventDefault();
});
const feedback = document.getElementById("feedback");
const leftScene = document.getElementById("leftScene");
const rightScene = document.getElementById("rightScene");
const answerIndividual = document.getElementById("answerIndividual");
const answerShared = document.getElementById("answerShared");
const tryAgain = document.getElementById("tryAgain");
let answered = false;
const bursts = [];
function spawnBurst(origin) {
const colors = [0xf06f52, 0xf4c654, 0x35c3b8, 0xfff4d5];
for (let i = 0; i < 28; i++) {
const mesh = new THREE.Mesh(
new THREE.TetrahedronGeometry(0.09),
new THREE.MeshBasicMaterial({
color: colors[i % colors.length]
})
);
mesh.position.copy(origin);
scene.add(mesh);
const angle = Math.random() * Math.PI * 2;
const speed = 1.8 + Math.random() * 2.4;
bursts.push({
mesh,
velocity: new THREE.Vector3(
Math.cos(angle) * speed,
2.4 + Math.random() * 2.8,
Math.sin(angle) * speed
),
spin: new THREE.Vector3(
Math.random() * 6,
Math.random() * 6,
Math.random() * 6
),
life: 1.6
});
}
}
function revealCounts() {
leftScene.classList.add("revealed");
rightScene.classList.add("revealed");
}
answerIndividual.addEventListener("click", () => {
if (answered) return;
answered = true;
revealCounts();
answerIndividual.classList.add("incorrect");
answerShared.classList.add("correct");
answerIndividual.disabled = true;
answerShared.disabled = true;
tryAgain.classList.add("visible");
feedback.className = "feedback near";
feedback.textContent =
"Close! Four boxes each get a separate ball, so that setup uses 4 balls. The shared setup uses only 1.";
});
answerShared.addEventListener("click", () => {
if (answered) return;
answered = true;
revealCounts();
answerShared.classList.add("correct");
answerIndividual.disabled = true;
answerShared.disabled = true;
tryAgain.classList.add("visible");
feedback.className = "feedback good";
feedback.textContent =
"Correct! One shared ball can serve all four boxes: 1 ball instead of 4.";
spawnBurst(
new THREE.Vector3(
sharedGroup.position.x,
sharedBall.position.y,
sharedBall.position.z
)
);
});
tryAgain.addEventListener("click", () => {
answered = false;
leftScene.classList.remove("revealed");
rightScene.classList.remove("revealed");
answerIndividual.classList.remove("incorrect");
answerShared.classList.remove("correct");
answerIndividual.disabled = false;
answerShared.disabled = false;
tryAgain.classList.remove("visible");
feedback.className = "feedback";
feedback.textContent = "Look at both scenes and make your choice.";
});
function resize() {
const width = stage.clientWidth;
const height = stage.clientHeight;
camera.aspect = width / Math.max(height, 1);
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
if (viewMode === "compare") {
desiredView.radius = compareRadius();
} else {
desiredView.radius = focusRadius();
}
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
resize();
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
const smoothing = 1 - Math.exp(-delta * 6);
view.theta = THREE.MathUtils.lerp(
view.theta,
desiredView.theta,
smoothing
);
view.phi = THREE.MathUtils.lerp(
view.phi,
desiredView.phi,
smoothing
);
view.radius = THREE.MathUtils.lerp(
view.radius,
desiredView.radius,
smoothing
);
view.target.lerp(desiredView.target, smoothing);
const sinPhi = Math.sin(view.phi);
camera.position.set(
view.target.x + view.radius * sinPhi * Math.sin(view.theta),
view.target.y + view.radius * Math.cos(view.phi),
view.target.z + view.radius * sinPhi * Math.cos(view.theta)
);
camera.lookAt(view.target);
individualBalls.forEach((ball) => {
ball.position.y =
1.01 + Math.sin(elapsed * 2.1 + ball.userData.phase) * 0.065;
ball.rotation.y += delta * 0.55;
ball.rotation.x += delta * 0.16;
});
sharedBall.position.y =
1.34 + Math.sin(elapsed * 2.35) * 0.075;
sharedBall.rotation.y -= delta * 0.7;
sharedBall.rotation.z += delta * 0.18;
sharedHalo.position.y = sharedBall.position.y;
sharedHalo.rotation.z += delta * 0.55;
haloMaterial.opacity = 0.5 + Math.sin(elapsed * 3) * 0.13;
rayMaterial.opacity = 0.43 + Math.sin(elapsed * 2.3) * 0.13;
for (let i = bursts.length - 1; i >= 0; i--) {
const burst = bursts[i];
burst.life -= delta;
burst.velocity.y -= 5.2 * delta;
burst.mesh.position.addScaledVector(burst.velocity, delta);
burst.mesh.rotation.x += burst.spin.x * delta;
burst.mesh.rotation.y += burst.spin.y * delta;
burst.mesh.rotation.z += burst.spin.z * delta;
burst.mesh.scale.setScalar(Math.max(0.05, burst.life));
if (burst.life <= 0) {
scene.remove(burst.mesh);
burst.mesh.geometry.dispose();
burst.mesh.material.dispose();
bursts.splice(i, 1);
}
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```