Prompt specification
Build or customize the Three.js 3D scene "Scaling a recipe token mixture" 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: Scaling a recipe token mixture
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/scaling-a-recipe-token-mixture-x1730
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts a stylized solid recipe-ratio table using fictional ingredient tokens. Put one base mixture in a shallow tray, with its two ingredients distinguished by both shape and color. Beside it, provide trays for double and triple batches. Animate copies of the tokens moving into those trays so the total counts grow while the same ingredient ratio is preserved. Allow switching between the three batch sizes and show the counts directly beside each tray. Keep the original base mixture visible throughout the comparison, and label the tokens as a mathematical model rather than real quantities for preparing food.
Working Three.js Starter (scaling-a-recipe-token-mixture-x1730):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Solid Ratio Table — Mathematical Batch Model</title>
<style>
:root {
--ink: #17383b;
--muted: #58706f;
--paper: #f6f4eb;
--line: rgba(23, 56, 59, 0.16);
--teal: #19b8aa;
--coral: #ff705c;
--shadow: 0 18px 50px rgba(24, 51, 51, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d9e3de;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.ui {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
pointer-events: auto;
background: rgba(249, 248, 242, 0.9);
border: 1px solid rgba(255, 255, 255, 0.75);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.brand {
position: absolute;
top: 26px;
left: 28px;
width: min(380px, calc(100vw - 56px));
padding: 21px 23px 20px;
border-radius: 22px;
}
.eyebrow {
color: #49706d;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 4px;
font-size: clamp(28px, 3vw, 42px);
line-height: 0.98;
letter-spacing: -0.045em;
}
.brand p {
margin: 10px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.legend {
position: absolute;
top: 26px;
right: 28px;
width: 285px;
padding: 18px 19px;
border-radius: 20px;
}
.legend-row {
display: grid;
grid-template-columns: 30px 1fr auto;
align-items: center;
gap: 10px;
margin-top: 12px;
}
.shape {
display: inline-block;
width: 24px;
height: 24px;
box-shadow:
inset -4px -5px 8px rgba(0, 0, 0, 0.13),
0 5px 10px rgba(0, 0, 0, 0.12);
}
.shape.orb {
border-radius: 50%;
background: var(--teal);
}
.shape.cube {
width: 22px;
height: 22px;
margin-left: 1px;
border-radius: 5px;
background: var(--coral);
}
.legend-row b,
.legend-row small {
display: block;
}
.legend-row b {
font-size: 13px;
}
.legend-row small {
margin-top: 2px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.legend-row strong {
font-size: 18px;
font-variant-numeric: tabular-nums;
}
.ratio-chip {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 15px;
padding: 10px 12px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.48);
font-size: 12px;
}
.ratio-chip strong {
font-size: 17px;
letter-spacing: 0.03em;
}
.disclaimer {
margin: 12px 0 0;
color: #6b5650;
font-size: 10px;
line-height: 1.45;
}
.tray-label {
position: absolute;
width: 184px;
padding: 10px 11px 9px;
transform: translate(-50%, -50%);
border: 1px solid rgba(255, 255, 255, 0.82);
border-radius: 14px;
background: rgba(248, 248, 242, 0.91);
box-shadow: 0 12px 30px rgba(27, 54, 53, 0.15);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
transition:
opacity 180ms ease,
border-color 180ms ease,
box-shadow 180ms ease;
will-change: left, top;
}
.tray-label.active {
border-color: rgba(25, 184, 170, 0.68);
box-shadow:
0 12px 32px rgba(27, 54, 53, 0.16),
0 0 0 3px rgba(25, 184, 170, 0.11);
}
.label-title,
.label-foot {
display: flex;
align-items: center;
justify-content: space-between;
}
.label-title {
margin-bottom: 8px;
}
.label-title span {
font-size: 9px;
font-weight: 850;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.label-title b {
color: #2a6f69;
font-size: 13px;
}
.counts {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 6px;
}
.count-chip {
display: grid;
grid-template-columns: 12px 1fr auto;
align-items: center;
gap: 5px;
padding: 6px 7px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.53);
font-size: 10px;
}
.count-chip i {
width: 9px;
height: 9px;
box-shadow: inset -2px -2px 3px rgba(0, 0, 0, 0.13);
}
.count-chip i.orb {
border-radius: 50%;
background: var(--teal);
}
.count-chip i.cube {
border-radius: 2px;
background: var(--coral);
}
.count-chip strong {
font-size: 15px;
font-variant-numeric: tabular-nums;
}
.label-foot {
margin-top: 8px;
color: var(--muted);
font-size: 10px;
}
.label-foot strong {
color: var(--ink);
font-size: 12px;
}
.label-foot em {
color: #2a6f69;
font-style: normal;
font-weight: 800;
}
.controls {
position: absolute;
left: 50%;
bottom: 24px;
width: min(720px, calc(100vw - 40px));
transform: translateX(-50%);
padding: 12px 14px;
border-radius: 20px;
}
.controls-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
margin: 0 3px 10px;
}
.controls-title {
display: flex;
align-items: baseline;
gap: 9px;
}
.controls-title span {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.controls-title strong {
font-size: 13px;
font-variant-numeric: tabular-nums;
}
.status {
overflow: hidden;
color: var(--muted);
font-size: 10px;
text-align: right;
text-overflow: ellipsis;
white-space: nowrap;
}
.button-row {
display: grid;
grid-template-columns: repeat(3, 1fr) auto;
gap: 8px;
}
.batch-button,
.replay-button {
min-height: 46px;
border: 1px solid var(--line);
border-radius: 13px;
cursor: pointer;
transition:
transform 150ms ease,
color 150ms ease,
background 150ms ease,
box-shadow 150ms ease;
}
.batch-button {
color: var(--ink);
background: rgba(255, 255, 255, 0.52);
}
.batch-button:hover,
.replay-button:hover {
transform: translateY(-2px);
}
.batch-button:active,
.replay-button:active {
transform: translateY(0);
}
.batch-button.active {
color: #f7fffc;
border-color: transparent;
background: var(--ink);
box-shadow: 0 8px 20px rgba(23, 56, 59, 0.2);
}
.batch-button b,
.batch-button small {
display: block;
}
.batch-button b {
font-size: 13px;
}
.batch-button small {
margin-top: 2px;
opacity: 0.68;
font-size: 9px;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.replay-button {
padding: 0 17px;
color: #f7fffc;
border-color: transparent;
background: #2c7e76;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.03em;
}
.camera-hint {
position: absolute;
bottom: 31px;
left: 28px;
color: rgba(23, 56, 59, 0.7);
font-size: 10px;
line-height: 1.5;
}
@media (max-width: 820px) {
.brand {
top: 14px;
left: 14px;
width: 290px;
padding: 16px 17px;
}
.brand p {
display: none;
}
.legend {
top: 14px;
right: 14px;
width: 230px;
padding: 14px;
}
.legend .disclaimer {
font-size: 9px;
}
.controls {
bottom: 12px;
width: calc(100vw - 24px);
padding: 10px;
}
.controls-top {
margin-bottom: 8px;
}
.status {
max-width: 55%;
}
.button-row {
grid-template-columns: repeat(3, 1fr);
}
.replay-button {
grid-column: 1 / -1;
min-height: 34px;
}
.camera-hint {
display: none;
}
.tray-label {
width: 148px;
padding: 8px;
}
.label-title {
margin-bottom: 6px;
}
.count-chip {
padding: 4px 5px;
}
}
@media (max-width: 590px) {
.brand {
width: calc(100vw - 28px);
}
.brand h1 {
font-size: 27px;
}
.legend {
top: 112px;
left: 14px;
right: auto;
width: 218px;
}
.legend-row {
margin-top: 8px;
}
.ratio-chip {
margin-top: 9px;
padding: 7px 9px;
}
.disclaimer {
display: none;
}
.tray-label {
width: 130px;
}
.count-chip {
grid-template-columns: 9px 1fr auto;
gap: 3px;
}
.count-chip strong {
font-size: 13px;
}
.controls-top {
gap: 8px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="ui">
<header class="brand panel">
<div class="eyebrow">Solid ratio table · dimensionless model</div>
<h1>Batch Ratio Study</h1>
<p>
One fixed base mixture is compared with two scaled trays.
Every landing bundle adds two token A and three token B.
</p>
</header>
<aside class="legend panel">
<div class="eyebrow">Fictional token system</div>
<div class="legend-row">
<span class="shape orb" aria-hidden="true"></span>
<div>
<b>Nova orb</b>
<small>Shape token A</small>
</div>
<strong>2</strong>
</div>
<div class="legend-row">
<span class="shape cube" aria-hidden="true"></span>
<div>
<b>Ember block</b>
<small>Shape token B</small>
</div>
<strong>3</strong>
</div>
<div class="ratio-chip">
<span>Base ratio</span>
<strong>A : B = 2 : 3</strong>
</div>
<p class="disclaimer">
Mathematical tokens only. Counts are dimensionless and are not
real quantities, ingredients, or instructions for preparing food.
</p>
</aside>
<div
class="tray-label active"
id="label-0"
aria-label="Base tray counts"
>
<div class="label-title">
<span>Base mixture</span>
<b>1×</b>
</div>
<div class="counts">
<div class="count-chip">
<i class="orb"></i><span>A</span><strong id="count-a-0">2</strong>
</div>
<div class="count-chip">
<i class="cube"></i><span>B</span><strong id="count-b-0">3</strong>
</div>
</div>
<div class="label-foot">
<span>Total <strong id="count-total-0">5</strong></span>
<em>2 : 3</em>
</div>
</div>
<div
class="tray-label"
id="label-1"
aria-label="Double batch tray counts"
>
<div class="label-title">
<span>Double batch</span>
<b>2×</b>
</div>
<div class="counts">
<div class="count-chip">
<i class="orb"></i><span>A</span><strong id="count-a-1">0</strong>
</div>
<div class="count-chip">
<i class="cube"></i><span>B</span><strong id="count-b-1">0</strong>
</div>
</div>
<div class="label-foot">
<span>Total <strong id="count-total-1">0</strong></span>
<em>2 : 3</em>
</div>
</div>
<div
class="tray-label"
id="label-2"
aria-label="Triple batch tray counts"
>
<div class="label-title">
<span>Triple batch</span>
<b>3×</b>
</div>
<div class="counts">
<div class="count-chip">
<i class="orb"></i><span>A</span><strong id="count-a-2">0</strong>
</div>
<div class="count-chip">
<i class="cube"></i><span>B</span><strong id="count-b-2">0</strong>
</div>
</div>
<div class="label-foot">
<span>Total <strong id="count-total-2">0</strong></span>
<em>2 : 3</em>
</div>
</div>
<section class="controls panel" aria-label="Batch size controls">
<div class="controls-top">
<div class="controls-title">
<span>Batch scale</span>
<strong id="grand-total">5 tokens on table</strong>
</div>
<div class="status" id="status-text" aria-live="polite">
Base fixed · every bundle preserves 2:3
</div>
</div>
<div class="button-row">
<button class="batch-button active" data-mode="1" aria-pressed="true">
<b>1× · Base</b>
<small>2 A + 3 B</small>
</button>
<button class="batch-button" data-mode="2" aria-pressed="false">
<b>2× · Double</b>
<small>4 A + 6 B</small>
</button>
<button class="batch-button" data-mode="3" aria-pressed="false">
<b>3× · Triple</b>
<small>6 A + 9 B</small>
</button>
<button class="replay-button" id="replay-button">
Replay growth
</button>
</div>
</section>
<div class="camera-hint">
Drag to orbit · scroll to zoom · double-click to reset view
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, scene, camera
// -------------------------------------------------------------------------
const host = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
host.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd9e3de);
scene.fog = new THREE.Fog(0xd9e3de, 28, 64);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, -0.1, 0);
const cameraState = {
yaw: 0.20,
phi: 1.03,
radius: 18.5,
userAdjusted: false
};
function updateCamera() {
const horizontal = Math.sin(cameraState.phi) * cameraState.radius;
camera.position.set(
cameraTarget.x + horizontal * Math.sin(cameraState.yaw),
cameraTarget.y + Math.cos(cameraState.phi) * cameraState.radius,
cameraTarget.z + horizontal * Math.cos(cameraState.yaw)
);
camera.lookAt(cameraTarget);
}
updateCamera();
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xf8fff7, 0x60716b, 1.65));
const keyLight = new THREE.DirectionalLight(0xfff4dc, 3.25);
keyLight.position.set(7, 14, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 11;
keyLight.shadow.camera.bottom = -11;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.normalBias = 0.035;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xb9fff4, 1.1);
rimLight.position.set(-8, 8, -7);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Geometry helpers
// -------------------------------------------------------------------------
function roundedRectShape(width, height, radius) {
const x = -width / 2;
const y = -height / 2;
const r = Math.min(radius, width / 2, height / 2);
const shape = new THREE.Shape();
shape.moveTo(x + r, y);
shape.lineTo(x + width - r, y);
shape.quadraticCurveTo(x + width, y, x + width, y + r);
shape.lineTo(x + width, y + height - r);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
shape.lineTo(x + r, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - r);
shape.lineTo(x, y + r);
shape.quadraticCurveTo(x, y, x + r, y);
return shape;
}
function roundedSlab(width, depth, height, radius, bevel = 0.05) {
const geometry = new THREE.ExtrudeGeometry(
roundedRectShape(width, depth, radius),
{
depth: height,
bevelEnabled: true,
bevelSegments: 3,
steps: 1,
bevelSize: bevel,
bevelThickness: bevel * 0.65,
curveSegments: 12
}
);
geometry.rotateX(-Math.PI / 2);
geometry.center();
return geometry;
}
function roundedRing(
outerWidth,
outerDepth,
innerWidth,
innerDepth,
height,
radius,
innerRadius,
bevel = 0.035
) {
const shape = roundedRectShape(outerWidth, outerDepth, radius);
const hole = roundedRectShape(innerWidth, innerDepth, innerRadius);
shape.holes.push(hole);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: height,
bevelEnabled: true,
bevelSegments: 3,
steps: 1,
bevelSize: bevel,
bevelThickness: bevel * 0.6,
curveSegments: 12
});
geometry.rotateX(-Math.PI / 2);
geometry.center();
return geometry;
}
// -------------------------------------------------------------------------
// Solid table and background
// -------------------------------------------------------------------------
const tableMaterial = new THREE.MeshStandardMaterial({
color: 0xe7dbc4,
roughness: 0.68,
metalness: 0.03
});
const tableEdgeMaterial = new THREE.MeshStandardMaterial({
color: 0xc8b99f,
roughness: 0.75,
metalness: 0.02
});
const legMaterial = new THREE.MeshStandardMaterial({
color: 0x315557,
roughness: 0.56,
metalness: 0.12
});
const tabletop = new THREE.Mesh(
roundedSlab(16.8, 7.4, 0.72, 0.72, 0.12),
tableMaterial
);
tabletop.receiveShadow = true;
tabletop.castShadow = true;
scene.add(tabletop);
const underTable = new THREE.Mesh(
roundedSlab(15.8, 6.55, 0.22, 0.52, 0.06),
tableEdgeMaterial
);
underTable.position.y = -0.41;
underTable.castShadow = true;
underTable.receiveShadow = true;
scene.add(underTable);
for (const x of [-6.55, 6.55]) {
for (const z of [-2.35, 2.35]) {
const leg = new THREE.Mesh(
roundedSlab(1.08, 1.08, 3.65, 0.16, 0.07),
legMaterial
);
leg.position.set(x, -2.16, z);
leg.castShadow = true;
leg.receiveShadow = true;
scene.add(leg);
}
}
const crossbar = new THREE.Mesh(
new THREE.BoxGeometry(12.5, 0.48, 0.48),
legMaterial
);
crossbar.position.set(0, -2.7, 2.32);
crossbar.castShadow = true;
scene.add(crossbar);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
new THREE.MeshStandardMaterial({
color: 0xb9c8c1,
roughness: 0.94,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -4.05;
ground.receiveShadow = true;
scene.add(ground);
// -------------------------------------------------------------------------
// Small standing model sign
// -------------------------------------------------------------------------
function makeTextTexture(lines, background = "#17383b") {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 256;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = background;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#bcefe6";
ctx.font = "700 34px system-ui, sans-serif";
ctx.fillText(lines[0], 512, 67);
ctx.fillStyle = "#ffffff";
ctx.font = "800 92px system-ui, sans-serif";
ctx.fillText(lines[1], 512, 147);
ctx.fillStyle = "#bcefe6";
ctx.font = "600 25px system-ui, sans-serif";
ctx.fillText(lines[2], 512, 211);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(
8,
renderer.capabilities.getMaxAnisotropy()
);
return texture;
}
const signTexture = makeTextTexture(
["DIMENSIONLESS MATH MODEL", "A : B = 2 : 3", "NOT REAL FOOD QUANTITIES"],
"#17383b"
);
const sign = new THREE.Mesh(
new THREE.PlaneGeometry(4.8, 1.2),
new THREE.MeshBasicMaterial({
map: signTexture,
toneMapped: false
})
);
sign.position.set(0, 1.38, -2.63);
scene.add(sign);
for (const x of [-1.95, 1.95]) {
const post = new THREE.Mesh(
roundedSlab(0.13, 0.13, 0.95, 0.04, 0.02),
legMaterial
);
post.position.set(x, 0.78, -2.68);
post.castShadow = true;
scene.add(post);
}
// -------------------------------------------------------------------------
// Trays
// -------------------------------------------------------------------------
const trayX = [-5.1, 0, 5.1];
const trayZ = -0.12;
const trays = trayX.map((x, index) => {
const group = new THREE.Group();
group.position.set(x, 0, trayZ);
scene.add(group);
const floorMaterial = new THREE.MeshStandardMaterial({
color: index === 0 ? 0xd6e2dc : 0xdbe4df,
roughness: 0.76,
metalness: 0.02
});
const floor = new THREE.Mesh(
roundedSlab(3.83, 2.78, 0.10, 0.34, 0.025),
floorMaterial
);
floor.position.y = 0.43;
floor.receiveShadow = true;
group.add(floor);
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0x34575a,
roughness: 0.48,
metalness: 0.15,
emissive: 0x000000,
emissiveIntensity: 0
});
const rim = new THREE.Mesh(
roundedRing(
4.22,
3.16,
3.72,
2.68,
0.20,
0.46,
0.31,
0.038
),
rimMaterial
);
rim.position.y = 0.49;
rim.castShadow = true;
rim.receiveShadow = true;
group.add(rim);
return {
index,
x,
group,
rimMaterial,
meshes: [],
current: { orb: 0, cube: 0 },
scale: index + 1
};
});
// -------------------------------------------------------------------------
// Token geometry and materials
// -------------------------------------------------------------------------
const orbGeometry = new THREE.IcosahedronGeometry(0.28, 2);
const cubeGeometry = roundedSlab(
0.43,
0.43,
0.43,
0.075,
0.035
);
const cubeEdges = new THREE.EdgesGeometry(cubeGeometry, 28);
const orbMaterial = new THREE.MeshStandardMaterial({
color: 0x19b8aa,
roughness: 0.30,
metalness: 0.09,
emissive: 0x075c54,
emissiveIntensity: 0.08
});
const cubeMaterial = new THREE.MeshStandardMaterial({
color: 0xff705c,
roughness: 0.36,
metalness: 0.06,
emissive: 0x6f160c,
emissiveIntensity: 0.05
});
const cubeEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x7e261c,
transparent: true,
opacity: 0.33
});
function makeToken(type) {
const mesh =
type === "orb"
? new THREE.Mesh(orbGeometry, orbMaterial)
: new THREE.Mesh(cubeGeometry, cubeMaterial);
if (type === "cube") {
mesh.add(new THREE.LineSegments(cubeEdges, cubeEdgeMaterial));
}
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.userData.type = type;
return mesh;
}
function layoutPosition(tray, type, index, count) {
if (type === "orb") {
let spacing;
if (count <= 1) spacing = 0;
else if (count === 2) spacing = 1.03;
else spacing = Math.min(0.62, 2.78 / (count - 1));
return new THREE.Vector3(
tray.x + (index - (count - 1) / 2) * spacing,
0.79,
trayZ - 0.73
);
}
const twoRows = count > 5;
const firstRowCount = twoRows ? Math.ceil(count / 2) : count;
const row = twoRows && index >= firstRowCount ? 1 : 0;
const positionInRow = twoRows
? row === 0
? index
: index - firstRowCount
: index;
const rowCount = twoRows
? row === 0
? firstRowCount
: count - firstRowCount
: count;
let spacing;
if (rowCount <= 1) spacing = 0;
else if (rowCount === 2) spacing = 0.76;
else if (rowCount === 3) spacing = 0.82;
else if (rowCount === 4) spacing = 0.73;
else spacing = 0.65;
return new THREE.Vector3(
tray.x +
(positionInRow - (rowCount - 1) / 2) * spacing,
0.73,
trayZ + (twoRows ? (row === 0 ? 0.18 : 1.03) : 0.58)
);
}
// -------------------------------------------------------------------------
// Base tray: this is never removed
// -------------------------------------------------------------------------
const baseTray = trays[0];
const baseTokens = {
orb: [],
cube: []
};
const baseSettleAnimations = [];
for (const type of ["orb", "cube"]) {
const count = type === "orb" ? 2 : 3;
for (let i = 0; i < count; i++) {
const token = makeToken(type);
const destination = layoutPosition(baseTray, type, i, count);
token.position.copy(destination);
token.scale.setScalar(0.01);
scene.add(token);
baseTokens[type].push(token);
baseSettleAnimations.push({
mesh: token,
destination,
start: performance.now() / 1000 + i * 0.07,
duration: 0.7
});
}
}
baseTray.current = { orb: 2, cube: 3 };
// -------------------------------------------------------------------------
// Copy animation system
//
// Tokens arrive in five-token bundles:
// 2 orbs + 3 cubes. Counts therefore remain in the 2:3 ratio even while
// a batch is being built.
// -------------------------------------------------------------------------
const flyingTokens = [];
let sequenceId = 0;
let currentMode = 1;
let buildingTray = null;
let userChoseBatch = false;
function easeInOutCubic(t) {
return t < 0.5
? 4 * t * t * t
: 1 - Math.pow(-2 * t + 2, 3) / 2;
}
function quadraticBezier(p0, p1, p2, t, target) {
const inverse = 1 - t;
target.set(
inverse * inverse * p0.x + 2 * inverse * t * p1.x + t * t * p2.x,
inverse * inverse * p0.y + 2 * inverse * t * p1.y + t * t * p2.y,
inverse * inverse * p0.z + 2 * inverse * t * p1.z + t * t * p2.z
);
return target;
}
function fillTray(trayIndex, onComplete) {
const tray = trays[trayIndex];
const scale = trayIndex + 1;
const now = performance.now() / 1000;
const groupRemaining = new Array(scale).fill(5);
let totalRemaining = scale * 5;
for (let group = 0; group < scale; group++) {
for (const type of ["orb", "cube"]) {
const perGroup = type === "orb" ? 2 : 3;
const typeOffset = type === "orb" ? group * 2 : group * 3;
for (let i = 0; i < perGroup; i++) {
const destinationIndex = typeOffset + i;
const destination = layoutPosition(
tray,
type,
destinationIndex,
perGroup * scale
);
const source = baseTokens[type][
i % baseTokens[type].length
].position.clone();
source.y += 1.38;
source.x += Math.sin((group + 1) * 2.4 + i) * 0.13;
source.z += Math.cos((group + 1) * 1.7 + i) * 0.12;
const control = source.clone().lerp(destination, 0.52);
control.y = 3.35 + Math.sin(group * 1.4 + i * 0.65) * 0.65;
control.z = 0.95 + Math.cos(group + i) * 0.35;
const mesh = makeToken(type);
mesh.position.copy(source);
mesh.scale.setScalar(0.28);
scene.add(mesh);
tray.meshes.push(mesh);
flyingTokens.push({
mesh,
type,
group,
trayIndex,
source,
control,
destination,
start:
now +
group * 0.30 +
(type === "orb" ? 0 : 0.035) +
i * 0.012,
duration: 0.88,
landed: false
});
}
}
}
function finishToken(animation) {
animation.landed = true;
animation.mesh.position.copy(animation.destination);
animation.mesh.scale.setScalar(1);
groupRemaining[animation.group] -= 1;
// Update only after an entire 2A + 3B bundle has landed.
if (groupRemaining[animation.group] === 0) {
trays[animation.trayIndex].current.orb += 2;
trays[animation.trayIndex].current.cube += 3;
updateCounts();
}
totalRemaining -= 1;
if (totalRemaining === 0 && onComplete) {
onComplete();
}
}
tray.pendingFinish = finishToken;
}
function clearExtraTrays() {
flyingTokens.length = 0;
for (const tray of trays.slice(1)) {
for (const mesh of tray.meshes) {
scene.remove(mesh);
}
tray.meshes.length = 0;
tray.current.orb = 0;
tray.current.cube = 0;
tray.pendingFinish = null;
}
buildingTray = null;
updateCounts();
}
function setBatch(mode) {
sequenceId += 1;
const thisSequence = sequenceId;
clearExtraTrays();
currentMode = mode;
document.querySelectorAll(".batch-button").forEach((button) => {
const active = Number(button.dataset.mode) === mode;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
const activeTray = mode - 1;
trays.forEach((tray, index) => {
tray.group.userData.active = index === activeTray;
});
if (mode === 2 || mode === 3) {
buildingTray = 1;
window.setTimeout(() => {
if (thisSequence !== sequenceId) return;
fillTray(1, () => {
if (thisSequence !== sequenceId) return;
if (mode === 3) {
buildingTray = 2;
window.setTimeout(() => {
if (thisSequence !== sequenceId) return;
fillTray(2, () => {
if (thisSequence !== sequenceId) return;
buildingTray = null;
updateStatus();
});
}, 100);
} else {
buildingTray = null;
updateStatus();
}
});
}, 140);
}
updateStatus();
}
// -------------------------------------------------------------------------
// Counts and status
// -------------------------------------------------------------------------
function updateCounts() {
let grandTotal = 0;
trays.forEach((tray, index) => {
const orb = tray.current.orb;
const cube = tray.current.cube;
const total = orb + cube;
grandTotal += total;
document.getElementById(`count-a-${index}`).textContent = orb;
document.getElementById(`count-b-${index}`).textContent = cube;
document.getElementById(`count-total-${index}`).textContent = total;
const label = document.getElementById(`label-${index}`);
label.classList.toggle(
"active",
index === currentMode - 1 ||
index === buildingTray
);
});
document.getElementById("grand-total").textContent =
`${grandTotal} tokens on table`;
}
function updateStatus() {
const status = document.getElementById("status-text");
if (buildingTray !== null) {
status.textContent =
buildingTray === 1
? "Copy bundles landing in the double tray · each adds 2A + 3B"
: "Copy bundles landing in the triple tray · each adds 2A + 3B";
} else {
status.textContent =
currentMode === 1
? "Base fixed · every bundle preserves 2:3"
: "Comparison complete · base remains visible";
}
}
document.querySelectorAll(".batch-button").forEach((button) => {
button.addEventListener("click", () => {
userChoseBatch = true;
setBatch(Number(button.dataset.mode));
});
});
document.getElementById("replay-button").addEventListener("click", () => {
userChoseBatch = true;
setBatch(3);
});
// -------------------------------------------------------------------------
// Custom camera controls: orbit, zoom, reset
// -------------------------------------------------------------------------
const canvas = renderer.domElement;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.yaw -= dx * 0.0055;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.004,
0.58,
1.24
);
cameraState.userAdjusted = true;
});
function endDrag(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.009,
11,
34
);
cameraState.userAdjusted = true;
},
{ passive: false }
);
function resetCamera() {
cameraState.yaw = 0.20;
cameraState.phi = 1.03;
cameraState.userAdjusted = false;
resize();
}
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
// -------------------------------------------------------------------------
// DOM count labels projected beside their trays
// -------------------------------------------------------------------------
const labelAnchors = trays.map((tray) =>
new THREE.Vector3(tray.x, 0.58, trayZ + 1.94)
);
const projected = new THREE.Vector3();
function positionLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
labelAnchors.forEach((anchor, index) => {
projected.copy(anchor).project(camera);
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
const visible = projected.z > -1 && projected.z < 1;
const label = document.getElementById(`label-${index}`);
label.style.left = `${x}px`;
label.style.top = `${y}px`;
label.style.opacity = visible ? "1" : "0";
});
}
// -------------------------------------------------------------------------
// Resize
// -------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height, false);
camera.aspect = aspect;
camera.updateProjectionMatrix();
if (!cameraState.userAdjusted) {
cameraState.radius =
18.5 * Math.max(1, 1.35 / Math.max(aspect, 0.45));
}
updateCamera();
}
window.addEventListener("resize", resize);
resize();
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
const flightPoint = new THREE.Vector3();
function animate() {
requestAnimationFrame(animate);
const time = clock.getElapsedTime();
const now = performance.now() / 1000;
// Base tray settling animation.
for (let i = baseSettleAnimations.length - 1; i >= 0; i--) {
const animation = baseSettleAnimations[i];
const t = THREE.MathUtils.clamp(
(now - animation.start) / animation.duration,
0,
1
);
const eased = 1 - Math.pow(1 - t, 3);
const overshoot =
t > 0.72
? Math.sin(((t - 0.72) / 0.28) * Math.PI) * 0.08
: 0;
animation.mesh.scale.setScalar(Math.max(0.01, eased + overshoot));
animation.mesh.rotation.y = (1 - eased) * 1.4;
if (t >= 1) {
animation.mesh.scale.setScalar(1);
animation.mesh.rotation.y = 0;
baseSettleAnimations.splice(i, 1);
}
}
// Flying copy tokens.
for (let i = flyingTokens.length - 1; i >= 0; i--) {
const animation = flyingTokens[i];
if (animation.landed) {
flyingTokens.splice(i, 1);
continue;
}
const t = THREE.MathUtils.clamp(
(now - animation.start) / animation.duration,
0,
1
);
if (t <= 0) {
animation.mesh.scale.setScalar(0.28);
continue;
}
const eased = easeInOutCubic(t);
quadraticBezier(
animation.source,
animation.control,
animation.destination,
eased,
flightPoint
);
animation.mesh.position.copy(flightPoint);
let scale;
if (t < 0.78) {
scale = 0.28 + (t / 0.78) * 0.72;
} else {
scale =
1 +
Math.sin(((t - 0.78) / 0.22) * Math.PI) * 0.11;
}
animation.mesh.scale.setScalar(scale);
animation.mesh.rotation.x = eased * 3.2;
animation.mesh.rotation.y = eased * 5.4;
animation.mesh.rotation.z = eased * 2.1;
if (t >= 1) {
const finish = trays[animation.trayIndex].pendingFinish;
if (finish) finish(animation);
}
}
// Selected and currently filling tray highlights.
trays.forEach((tray, index) => {
const selected = index === currentMode - 1;
const building = index === buildingTray;
tray.rimMaterial.emissive.setHex(
selected || building ? 0x0b665d : 0x000000
);
tray.rimMaterial.emissiveIntensity = building
? 0.22 + Math.sin(time * 4.2) * 0.05
: selected
? 0.12 + Math.sin(time * 2.7) * 0.025
: 0;
});
updateCamera();
positionLabels();
renderer.render(scene, camera);
}
updateCounts();
updateStatus();
animate();
// Automatically demonstrate the complete 1× → 2× → 3× comparison.
// Any manual selection cancels this demonstration.
window.setTimeout(() => {
if (!userChoseBatch) {
setBatch(3);
}
}, 1200);
</script>
</body>
</html>
```