Prompt specification
Build or customize the Three.js 3D scene "Portion transfer comparison" 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: Portion transfer comparison
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/portion-transfer-comparison-x0142
Scene Description Prompt:
Transfer equal food-cube volumes between wide and tall voxel containers; heights change but counts stay equal. Three.js from cdnjs, one HTML file.
Working Three.js Starter (portion-transfer-comparison-x0142):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Pantry — Equal Volume Transfer</title>
<style>
:root {
color-scheme: dark;
--bg: #071019;
--panel: rgba(10, 20, 31, 0.82);
--panel-border: rgba(168, 220, 255, 0.14);
--text: #edf7ff;
--muted: #91a6b8;
--cyan: #63dcff;
--violet: #a78bfa;
--orange: #ffad4a;
--green: #87df9a;
}
* {
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,
input {
font: inherit;
}
button {
color: inherit;
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
#vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 58% 45%, transparent 35%, rgba(1, 6, 12, 0.5) 100%),
linear-gradient(180deg, rgba(5, 12, 20, 0.05), rgba(3, 8, 14, 0.24));
}
.topbar {
position: fixed;
z-index: 10;
top: 0;
left: 0;
right: 0;
height: 76px;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 26px;
pointer-events: none;
background: linear-gradient(180deg, rgba(4, 10, 17, 0.78), transparent);
}
.brand {
display: flex;
align-items: center;
gap: 12px;
}
.brand-mark {
position: relative;
width: 30px;
height: 30px;
transform: rotate(-8deg);
}
.brand-mark i {
position: absolute;
width: 13px;
height: 13px;
border-radius: 3px;
background: var(--cyan);
box-shadow: 0 0 18px rgba(99, 220, 255, 0.45);
}
.brand-mark i:nth-child(1) {
left: 0;
top: 0;
}
.brand-mark i:nth-child(2) {
right: 0;
top: 0;
background: var(--violet);
}
.brand-mark i:nth-child(3) {
left: 8px;
bottom: 0;
background: var(--orange);
}
.brand-name {
font-size: 13px;
font-weight: 800;
letter-spacing: 0.18em;
}
.brand-subtitle {
margin-top: 3px;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.04em;
}
.top-status {
display: flex;
align-items: center;
gap: 9px;
padding: 8px 12px;
border: 1px solid var(--panel-border);
border-radius: 999px;
background: rgba(8, 17, 27, 0.66);
color: #bdd0de;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.08em;
backdrop-filter: blur(14px);
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 12px rgba(135, 223, 154, 0.8);
}
.panel {
position: fixed;
z-index: 20;
top: 92px;
bottom: 22px;
left: 22px;
width: 340px;
overflow: auto;
padding: 22px;
border: 1px solid var(--panel-border);
border-radius: 22px;
background: var(--panel);
box-shadow:
0 24px 80px rgba(0, 0, 0, 0.38),
inset 0 1px rgba(255, 255, 255, 0.04);
backdrop-filter: blur(22px) saturate(125%);
scrollbar-width: thin;
scrollbar-color: rgba(141, 189, 220, 0.28) transparent;
}
.eyebrow {
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
}
h1 {
margin: 9px 0 10px;
font-size: clamp(28px, 3vw, 36px);
line-height: 1.04;
letter-spacing: -0.045em;
}
h1 span {
color: var(--cyan);
}
.intro {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.6;
}
.divider {
height: 1px;
margin: 18px 0;
background: linear-gradient(90deg, var(--panel-border), transparent);
}
.control-label {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 9px;
color: #c9d8e4;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.value-chip {
padding: 4px 8px;
border: 1px solid rgba(99, 220, 255, 0.2);
border-radius: 7px;
background: rgba(99, 220, 255, 0.08);
color: var(--cyan);
letter-spacing: 0.03em;
text-transform: none;
}
.segmented {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-bottom: 17px;
}
.segment {
min-height: 40px;
padding: 8px 5px;
border: 1px solid rgba(172, 214, 239, 0.12);
border-radius: 11px;
background: rgba(255, 255, 255, 0.025);
color: #8197a9;
cursor: pointer;
font-size: 11px;
font-weight: 700;
transition: 160ms ease;
}
.segment:hover {
border-color: rgba(99, 220, 255, 0.3);
color: #dcefff;
}
.segment.active {
border-color: rgba(99, 220, 255, 0.45);
background: rgba(99, 220, 255, 0.1);
color: var(--cyan);
box-shadow: inset 0 0 18px rgba(99, 220, 255, 0.04);
}
.range-wrap {
padding: 12px 13px 9px;
border: 1px solid rgba(172, 214, 239, 0.1);
border-radius: 12px;
background: rgba(0, 0, 0, 0.12);
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 5px 0 3px;
border-radius: 999px;
appearance: none;
background: linear-gradient(
90deg,
var(--cyan) 0%,
var(--cyan) var(--fill, 50%),
rgba(146, 177, 197, 0.18) var(--fill, 50%)
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
border: 3px solid #102332;
border-radius: 50%;
appearance: none;
background: var(--cyan);
box-shadow: 0 0 0 1px var(--cyan), 0 0 14px rgba(99, 220, 255, 0.45);
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 3px solid #102332;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 0 1px var(--cyan), 0 0 14px rgba(99, 220, 255, 0.45);
}
.primary {
width: 100%;
min-height: 48px;
margin-top: 12px;
border: 0;
border-radius: 12px;
background: linear-gradient(135deg, #70e3ff, #55bdf1);
color: #07111a;
cursor: pointer;
font-size: 12px;
font-weight: 850;
letter-spacing: 0.02em;
box-shadow: 0 10px 28px rgba(63, 186, 232, 0.2);
transition:
transform 150ms ease,
filter 150ms ease;
}
.primary:hover:not(:disabled) {
transform: translateY(-1px);
filter: brightness(1.08);
}
.primary:disabled,
.segment:disabled,
.secondary:disabled {
cursor: wait;
opacity: 0.52;
}
.secondary-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 9px;
}
.secondary {
min-height: 36px;
padding: 7px;
border: 1px solid rgba(172, 214, 239, 0.13);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
color: #a7bbca;
cursor: pointer;
font-size: 10px;
font-weight: 750;
transition: 150ms ease;
}
.secondary:hover:not(:disabled) {
border-color: rgba(99, 220, 255, 0.3);
color: var(--text);
}
.secondary.active {
border-color: rgba(167, 139, 250, 0.42);
background: rgba(167, 139, 250, 0.1);
color: #c9baff;
}
.container-cards {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
.container-card {
position: relative;
overflow: hidden;
min-height: 117px;
padding: 12px;
border: 1px solid rgba(172, 214, 239, 0.1);
border-radius: 13px;
background: rgba(255, 255, 255, 0.022);
}
.container-card::after {
position: absolute;
right: -16px;
bottom: -26px;
width: 72px;
height: 72px;
border-radius: 50%;
background: var(--accent);
opacity: 0.08;
filter: blur(2px);
content: "";
}
.container-name {
display: flex;
align-items: center;
gap: 6px;
color: var(--accent);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.13em;
}
.container-name i {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 10px var(--accent);
}
.container-count {
margin-top: 9px;
font-size: 25px;
font-weight: 780;
letter-spacing: -0.04em;
}
.container-meta {
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
.bar {
height: 3px;
margin-top: 12px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.07);
}
.bar span {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
background: var(--accent);
box-shadow: 0 0 10px var(--accent);
transition: width 400ms ease;
}
.ledger {
display: grid;
grid-template-columns: 1fr auto 1fr;
align-items: center;
gap: 8px;
padding: 12px;
border: 1px solid rgba(135, 223, 154, 0.16);
border-radius: 13px;
background: rgba(135, 223, 154, 0.045);
}
.ledger-cell {
text-align: center;
}
.ledger-cell span {
display: block;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.13em;
}
.ledger-cell strong {
display: block;
margin-top: 3px;
color: var(--green);
font-size: 19px;
}
.ledger-equals {
color: #648076;
font-size: 14px;
}
.total-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 9px;
color: var(--muted);
font-size: 10px;
}
.total-row strong {
color: #dceaf4;
font-size: 11px;
}
.note {
margin: 14px 0 0;
color: #74899a;
font-size: 9px;
line-height: 1.55;
}
.scene-label {
position: fixed;
z-index: 8;
top: 0;
left: 0;
min-width: 135px;
padding: 9px 11px;
border: 1px solid rgba(172, 214, 239, 0.15);
border-radius: 11px;
background: rgba(6, 14, 22, 0.72);
box-shadow: 0 10px 28px rgba(0, 0, 0, 0.22);
pointer-events: none;
backdrop-filter: blur(12px);
transform: translate(-50%, -100%);
}
.scene-label::after {
position: absolute;
bottom: -5px;
left: calc(50% - 5px);
width: 9px;
height: 9px;
border-right: 1px solid rgba(172, 214, 239, 0.15);
border-bottom: 1px solid rgba(172, 214, 239, 0.15);
background: rgba(6, 14, 22, 0.72);
content: "";
transform: rotate(45deg);
}
.label-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 15px;
color: var(--accent);
font-size: 8px;
font-weight: 850;
letter-spacing: 0.14em;
}
.label-head span:last-child {
color: #8196a7;
}
.scene-label strong {
display: block;
margin-top: 5px;
font-size: 14px;
}
.scene-label small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 8px;
}
.camera-help {
position: fixed;
z-index: 8;
right: 22px;
bottom: 20px;
padding: 8px 11px;
border: 1px solid rgba(172, 214, 239, 0.1);
border-radius: 999px;
background: rgba(6, 14, 22, 0.54);
color: #718799;
font-size: 9px;
pointer-events: none;
backdrop-filter: blur(10px);
}
#toast {
position: fixed;
z-index: 40;
left: 50%;
bottom: 25px;
max-width: min(420px, calc(100vw - 32px));
padding: 10px 14px;
border: 1px solid rgba(255, 173, 74, 0.24);
border-radius: 10px;
background: rgba(18, 22, 25, 0.92);
color: #ffd29a;
font-size: 11px;
opacity: 0;
pointer-events: none;
transform: translate(-50%, 12px);
transition: 180ms ease;
}
#toast.visible {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 760px) {
.topbar {
height: 62px;
padding: 0 15px;
}
.brand-subtitle,
.top-status span:last-child {
display: none;
}
.panel {
top: auto;
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
max-height: 47vh;
padding: 16px;
border-radius: 18px;
}
h1 {
margin-top: 6px;
font-size: 25px;
}
.intro {
font-size: 11px;
}
.divider {
margin: 13px 0;
}
.scene-label {
min-width: 112px;
padding: 7px 9px;
}
.camera-help {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel food container scene"></canvas>
<div id="vignette"></div>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"><i></i><i></i><i></i></div>
<div>
<div class="brand-name">VOXEL PANTRY</div>
<div class="brand-subtitle">Equal-volume transfer laboratory</div>
</div>
</div>
<div class="top-status">
<i class="status-dot"></i>
<span id="topStatus">CONSERVATION ACTIVE</span>
</div>
</header>
<aside class="panel">
<div class="eyebrow">TRANSFER LAB / 01</div>
<h1>Same cubes.<br><span>New height.</span></h1>
<p class="intro">
Move identical food cubes between a wide and a tall container.
The footprint changes the fill height, while every cube transferred out is matched by one transferred in.
</p>
<div class="divider"></div>
<div class="control-label">
<span>Transfer direction</span>
</div>
<div class="segmented" role="group" aria-label="Transfer direction">
<button class="segment active" id="toTall" type="button">Wide → Tall</button>
<button class="segment" id="toWide" type="button">Tall → Wide</button>
</div>
<div class="control-label">
<span>Batch volume</span>
<span class="value-chip" id="batchValue">12 cubes</span>
</div>
<div class="range-wrap">
<input
id="batchRange"
type="range"
min="1"
max="24"
step="1"
value="12"
aria-label="Number of cubes to transfer"
>
</div>
<button class="primary" id="transferButton" type="button">
Transfer 12 cubes to tall
</button>
<div class="secondary-row">
<button class="secondary" id="equalizeButton" type="button">
Equalize counts
</button>
<button class="secondary" id="resetButton" type="button">
Reset equal fill
</button>
</div>
<div class="secondary-row">
<button class="secondary" id="autoButton" type="button">
Auto cycle: off
</button>
<button class="secondary" id="homeButton" type="button">
Reset camera
</button>
</div>
<div class="divider"></div>
<div class="control-label">
<span>Container state</span>
<span class="value-chip" id="totalChip">72 total</span>
</div>
<div class="container-cards">
<div class="container-card" style="--accent: var(--cyan)">
<div class="container-name"><i></i> WIDE</div>
<div class="container-count" id="wideCount">36</div>
<div class="container-meta" id="wideMeta">2 layers · 6 × 4 footprint</div>
<div class="bar"><span id="wideBar"></span></div>
</div>
<div class="container-card" style="--accent: var(--violet)">
<div class="container-name"><i></i> TALL</div>
<div class="container-count" id="tallCount">36</div>
<div class="container-meta" id="tallMeta">4 layers · 3 × 3 footprint</div>
<div class="bar"><span id="tallBar"></span></div>
</div>
</div>
<div class="divider"></div>
<div class="control-label">
<span>Latest transfer ledger</span>
</div>
<div class="ledger" aria-live="polite">
<div class="ledger-cell">
<span>REMOVED</span>
<strong id="removedCount">0</strong>
</div>
<div class="ledger-equals">=</div>
<div class="ledger-cell">
<span>ADDED</span>
<strong id="addedCount">0</strong>
</div>
</div>
<div class="total-row">
<span>Total before → after</span>
<strong id="conservationText">72 → 72</strong>
</div>
<p class="note">
Equal volume = equal number of identical cubes. The cyan and violet gauges show how
the same quantity occupies a shorter height in the wide container.
</p>
</aside>
<div class="scene-label" id="wideLabel" style="--accent: var(--cyan)">
<div class="label-head"><span>WIDE</span><span>6 × 4</span></div>
<strong>36 cubes</strong>
<small>2 layers · height 1.64</small>
</div>
<div class="scene-label" id="tallLabel" style="--accent: var(--violet)">
<div class="label-head"><span>TALL</span><span>3 × 3</span></div>
<strong>36 cubes</strong>
<small>4 layers · height 3.28</small>
</div>
<div class="camera-help">Drag to orbit · Scroll or pinch to zoom</div>
<div id="toast" role="status"></div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const canvas = $("scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x071019, 1);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.18;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071019);
scene.fog = new THREE.FogExp2(0x071019, 0.027);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemisphere = new THREE.HemisphereLight(0xa7ddff, 0x101722, 2.0);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffedcf, 3.2);
keyLight.position.set(-5, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.00025;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x4edcff, 35, 18, 2);
cyanLight.position.set(-5, 4, 3);
scene.add(cyanLight);
const violetLight = new THREE.PointLight(0x9f7cff, 38, 18, 2);
violetLight.position.set(5, 5, -2);
scene.add(violetLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
new THREE.MeshStandardMaterial({
color: 0x071019,
roughness: 0.94,
metalness: 0.04
})
);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(38, 38, 0x245166, 0x142b38);
grid.position.y = 0.012;
grid.material.transparent = true;
grid.material.opacity = 0.32;
scene.add(grid);
// Subtle atmospheric particles.
const particleCount = 150;
const particlePositions = new Float32Array(particleCount * 3);
for (let i = 0; i < particleCount; i++) {
particlePositions[i * 3] = (Math.random() - 0.5) * 28;
particlePositions[i * 3 + 1] = 1 + Math.random() * 11;
particlePositions[i * 3 + 2] = (Math.random() - 0.5) * 22;
}
const particleGeometry = new THREE.BufferGeometry();
particleGeometry.setAttribute(
"position",
new THREE.BufferAttribute(particlePositions, 3)
);
const particles = new THREE.Points(
particleGeometry,
new THREE.PointsMaterial({
color: 0x9bdcff,
size: 0.025,
transparent: true,
opacity: 0.28,
depthWrite: false
})
);
scene.add(particles);
// -------------------------------------------------------------------------
// Direct camera controls; no addon modules are used.
// -------------------------------------------------------------------------
const orbit = {
theta: 0.56,
phi: 1.08,
radius: 15.4,
target: new THREE.Vector3(0, 2.15, 0),
desiredTheta: 0.56,
desiredPhi: 1.08,
desiredRadius: 15.4
};
let dragging = false;
let previousX = 0;
let previousY = 0;
function fitCamera() {
const aspect = window.innerWidth / window.innerHeight;
orbit.desiredRadius = Math.max(15.4, 10.7 / aspect);
orbit.target.y = aspect < 0.9 ? 3.0 : 2.15;
}
fitCamera();
orbit.radius = orbit.desiredRadius;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
orbit.desiredTheta -= dx * 0.006;
orbit.desiredPhi = THREE.MathUtils.clamp(
orbit.desiredPhi - dy * 0.005,
0.48,
1.43
);
});
function stopDragging(event) {
dragging = false;
if (event && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.desiredRadius = THREE.MathUtils.clamp(
orbit.desiredRadius + event.deltaY * 0.009,
9.5,
25
);
},
{ passive: false }
);
$("homeButton").addEventListener("click", () => {
orbit.desiredTheta = 0.56;
orbit.desiredPhi = 1.08;
fitCamera();
});
// -------------------------------------------------------------------------
// Voxel container definitions
// -------------------------------------------------------------------------
const CELL = 0.82;
const CUBE_SIZE = 0.72;
const FLOOR_Y = 0.28;
const TOTAL_CUBES = 72;
const wide = {
name: "wide",
cols: 6,
rows: 4,
maxLayers: 5,
height: 4.35,
x: -3.15,
color: 0x63dcff,
cssColor: "#63dcff"
};
const tall = {
name: "tall",
cols: 3,
rows: 3,
maxLayers: 8,
height: 6.85,
x: 3.0,
color: 0xa78bfa,
cssColor: "#a78bfa"
};
const glassMaterial = new THREE.MeshPhysicalMaterial({
color: 0x8cdcff,
transparent: true,
opacity: 0.055,
roughness: 0.08,
metalness: 0,
side: THREE.DoubleSide,
depthWrite: false
});
const frameMaterials = new Map();
function getFrameMaterial(color) {
if (!frameMaterials.has(color)) {
frameMaterials.set(
color,
new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.45,
metalness: 0.42,
roughness: 0.3,
transparent: true,
opacity: 0.88
})
);
}
return frameMaterials.get(color);
}
function rectangleLine(width, depth, y, color, opacity = 0.42) {
const x = width / 2;
const z = depth / 2;
const points = [
new THREE.Vector3(-x, y, -z),
new THREE.Vector3(x, y, -z),
new THREE.Vector3(x, y, -z),
new THREE.Vector3(x, y, z),
new THREE.Vector3(x, y, z),
new THREE.Vector3(-x, y, z),
new THREE.Vector3(-x, y, z),
new THREE.Vector3(-x, y, -z)
];
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
});
return new THREE.LineSegments(geometry, material);
}
function makeContainer(def) {
const group = new THREE.Group();
group.position.x = def.x;
scene.add(group);
const width = def.cols * CELL + 0.18;
const depth = def.rows * CELL + 0.18;
const height = def.height;
const frameMaterial = getFrameMaterial(def.color);
// Base slab.
const base = new THREE.Mesh(
new THREE.BoxGeometry(width, 0.16, depth),
new THREE.MeshStandardMaterial({
color: def.color,
emissive: def.color,
emissiveIntensity: 0.08,
transparent: true,
opacity: 0.13,
roughness: 0.65
})
);
base.position.y = 0.18;
base.receiveShadow = true;
group.add(base);
// Transparent glass walls.
const wallThickness = 0.035;
const wallY = FLOOR_Y + height / 2;
const wallSpecs = [
[width, wallThickness, 0, 0, wallY, depth / 2],
[width, wallThickness, 0, 0, wallY, -depth / 2],
[wallThickness, height, -width / 2, 0, wallY, 0],
[wallThickness, height, width / 2, 0, wallY, 0]
];
for (const [w, h, d, x, y, z] of wallSpecs) {
const wall = new THREE.Mesh(
new THREE.BoxGeometry(w, h, d),
glassMaterial
);
wall.position.set(x, y, z);
group.add(wall);
}
// Outer frame.
const frameGeometry = new THREE.EdgesGeometry(
new THREE.BoxGeometry(width + 0.025, height, depth + 0.025)
);
const frame = new THREE.LineSegments(
frameGeometry,
new THREE.LineBasicMaterial({
color: def.color,
transparent: true,
opacity: 0.54
})
);
frame.position.y = wallY;
group.add(frame);
// Stronger corner posts.
const postGeometry = new THREE.BoxGeometry(0.065, height, 0.065);
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
const post = new THREE.Mesh(postGeometry, frameMaterial);
post.position.set(
sx * width / 2,
wallY,
sz * depth / 2
);
group.add(post);
}
}
// Floor voxel grid.
const gridVertices = [];
for (let c = 0; c <= def.cols; c++) {
const x = -width / 2 + (c / def.cols) * width;
gridVertices.push(
x, FLOOR_Y + 0.008, -depth / 2,
x, FLOOR_Y + 0.008, depth / 2
);
}
for (let r = 0; r <= def.rows; r++) {
const z = -depth / 2 + (r / def.rows) * depth;
gridVertices.push(
-width / 2, FLOOR_Y + 0.008, z,
width / 2, FLOOR_Y + 0.008, z
);
}
const floorGridGeometry = new THREE.BufferGeometry();
floorGridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridVertices, 3)
);
group.add(
new THREE.LineSegments(
floorGridGeometry,
new THREE.LineBasicMaterial({
color: def.color,
transparent: true,
opacity: 0.24
})
)
);
// Horizontal voxel layer guides.
for (let layer = 1; layer <= def.maxLayers; layer++) {
group.add(
rectangleLine(
width,
depth,
FLOOR_Y + layer * CELL,
def.color,
0.13
)
);
}
// Height gauge.
const gaugeX = width / 2 + 0.25;
const gaugeZ = depth / 2 + 0.14;
const gaugePoints = [
new THREE.Vector3(gaugeX, FLOOR_Y, gaugeZ),
new THREE.Vector3(gaugeX, FLOOR_Y + def.maxLayers * CELL, gaugeZ)
];
group.add(
new THREE.Line(
new THREE.BufferGeometry().setFromPoints(gaugePoints),
new THREE.LineBasicMaterial({
color: def.color,
transparent: true,
opacity: 0.42
})
)
);
for (let layer = 0; layer <= def.maxLayers; layer++) {
const tick = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(gaugeX - 0.08, FLOOR_Y + layer * CELL, gaugeZ),
new THREE.Vector3(gaugeX + 0.08, FLOOR_Y + layer * CELL, gaugeZ)
]),
new THREE.LineBasicMaterial({
color: def.color,
transparent: true,
opacity: 0.48
})
);
group.add(tick);
}
const gaugeFill = new THREE.Mesh(
new THREE.BoxGeometry(0.075, 1, 0.075),
new THREE.MeshBasicMaterial({
color: def.color,
transparent: true,
opacity: 0.88
})
);
gaugeFill.position.set(gaugeX, FLOOR_Y, gaugeZ);
group.add(gaugeFill);
const anchor = new THREE.Object3D();
anchor.position.set(0, FLOOR_Y + height + 0.58, 0);
group.add(anchor);
def.group = group;
def.width = width;
def.depth = depth;
def.anchor = anchor;
def.gaugeFill = gaugeFill;
def.area = def.cols * def.rows;
def.capacity = def.area * def.maxLayers;
return def;
}
makeContainer(wide);
makeContainer(tall);
// -------------------------------------------------------------------------
// Food cubes
// -------------------------------------------------------------------------
const cubeGeometry = new THREE.BoxGeometry(CUBE_SIZE, CUBE_SIZE, CUBE_SIZE);
const cubeEdgeGeometry = new THREE.EdgesGeometry(cubeGeometry);
const cubeEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x5b351f,
transparent: true,
opacity: 0.54
});
const cubeMaterials = [
new THREE.MeshStandardMaterial({
color: 0xffa84c,
roughness: 0.55,
metalness: 0.02
}),
new THREE.MeshStandardMaterial({
color: 0xffc45d,
roughness: 0.58,
metalness: 0.01
}),
new THREE.MeshStandardMaterial({
color: 0xf1814d,
roughness: 0.6,
metalness: 0.01
}),
new THREE.MeshStandardMaterial({
color: 0x92cf68,
roughness: 0.62,
metalness: 0
})
];
const garnishGeometry = new THREE.CircleGeometry(0.065, 10);
const garnishMaterial = new THREE.MeshStandardMaterial({
color: 0x4c8d42,
roughness: 0.85,
side: THREE.DoubleSide
});
const allCubes = [];
const wideCubes = [];
const tallCubes = [];
for (let i = 0; i < TOTAL_CUBES; i++) {
const group = new THREE.Group();
const mesh = new THREE.Mesh(
cubeGeometry,
cubeMaterials[i % cubeMaterials.length]
);
mesh.castShadow = true;
mesh.receiveShadow = true;
group.add(mesh);
const edges = new THREE.LineSegments(cubeEdgeGeometry, cubeEdgeMaterial);
group.add(edges);
const garnish = new THREE.Mesh(garnishGeometry, garnishMaterial);
garnish.rotation.x = -Math.PI / 2;
garnish.position.set(0.11, CUBE_SIZE / 2 + 0.002, -0.09);
group.add(garnish);
group.userData.seed = i;
scene.add(group);
allCubes.push(group);
}
function slotPosition(def, index) {
const area = def.area;
const layer = Math.floor(index / area);
const withinLayer = index % area;
const column = withinLayer % def.cols;
const row = Math.floor(withinLayer / def.cols);
return new THREE.Vector3(
def.x + (column - (def.cols - 1) / 2) * CELL,
FLOOR_Y + CUBE_SIZE / 2 + layer * CELL,
(row - (def.rows - 1) / 2) * CELL
);
}
function assignCube(cube, owner, index) {
cube.userData.owner = owner;
const def = owner === wide ? wide : tall;
cube.position.copy(slotPosition(def, index));
}
function resetCubes(immediate = false) {
transitions.length = 0;
wideCubes.length = 0;
tallCubes.length = 0;
allCubes.forEach((cube, index) => {
const owner = index < TOTAL_CUBES / 2 ? wide : tall;
const list = owner === wide ? wideCubes : tallCubes;
const target = slotPosition(owner, list.length);
cube.userData.owner = owner;
list.push(cube);
if (immediate) {
cube.position.copy(target);
cube.rotation.set(0, 0, 0);
} else {
startTransition(cube, target, 650, index * 4, 0.8);
}
});
updateReadouts();
updateGauges();
}
// -------------------------------------------------------------------------
// Animation helpers
// -------------------------------------------------------------------------
const transitions = [];
const pulses = [];
function startTransition(cube, target, duration = 850, delay = 0, arc = 1.7) {
transitions.push({
cube,
from: cube.position.clone(),
to: target.clone(),
start: performance.now() + delay,
duration,
arc
});
}
function createPulse(position, color) {
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.72,
depthWrite: false,
side: THREE.DoubleSide
});
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.35, 0.022, 8, 36),
material
);
ring.rotation.x = Math.PI / 2;
ring.position.copy(position);
scene.add(ring);
pulses.push({
ring,
material,
start: performance.now(),
duration: 700
});
}
function updateTransitions(now) {
for (let i = transitions.length - 1; i >= 0; i--) {
const transition = transitions[i];
if (now < transition.start) continue;
const raw = (now - transition.start) / transition.duration;
const t = THREE.MathUtils.clamp(raw, 0, 1);
const eased =
t < 0.5
? 4 * t * t * t
: 1 - Math.pow(-2 * t + 2, 3) / 2;
transition.cube.position.lerpVectors(
transition.from,
transition.to,
eased
);
transition.cube.position.y +=
Math.sin(Math.PI * t) * transition.arc;
transition.cube.rotation.y = Math.sin(Math.PI * t) * Math.PI * 0.8;
transition.cube.rotation.x = Math.sin(Math.PI * t) * Math.PI * 0.18;
if (raw >= 1) {
transition.cube.position.copy(transition.to);
transition.cube.rotation.set(0, 0, 0);
transitions.splice(i, 1);
}
}
}
function updatePulses(now) {
for (let i = pulses.length - 1; i >= 0; i--) {
const pulse = pulses[i];
const t = (now - pulse.start) / pulse.duration;
if (t >= 1) {
scene.remove(pulse.ring);
pulse.ring.geometry.dispose();
pulse.material.dispose();
pulses.splice(i, 1);
continue;
}
const eased = 1 - Math.pow(1 - t, 3);
pulse.ring.scale.setScalar(1 + eased * 1.5);
pulse.material.opacity = 0.72 * (1 - t);
}
}
// -------------------------------------------------------------------------
// Transfer mechanics
// -------------------------------------------------------------------------
let directionToTall = true;
let busy = false;
let autoTimer = null;
let lastTotal = TOTAL_CUBES;
function batchSize() {
return Number($("batchRange").value);
}
function setDirection(toTall) {
directionToTall = toTall;
$("toTall").classList.toggle("active", toTall);
$("toWide").classList.toggle("active", !toTall);
updateTransferButton();
}
function updateTransferButton() {
const n = batchSize();
const destination = directionToTall ? "tall" : "wide";
$("transferButton").textContent = busy
? `Moving ${n} cubes…`
: `Transfer ${n} cubes to ${destination}`;
}
function updateRangeFill() {
const range = $("batchRange");
const percentage =
((Number(range.value) - Number(range.min)) /
(Number(range.max) - Number(range.min))) *
100;
range.style.setProperty("--fill", `${percentage}%`);
}
function layerCount(def, count) {
return count === 0 ? 0 : Math.ceil(count / def.area);
}
function updateGauges() {
for (const def of [wide, tall]) {
const count = def === wide ? wideCubes.length : tallCubes.length;
const height = layerCount(def, count) * CELL;
const clampedHeight = Math.min(height, def.maxLayers * CELL);
def.gaugeFill.scale.y = Math.max(0.001, clampedHeight);
def.gaugeFill.position.y = FLOOR_Y + clampedHeight / 2;
}
}
function updateReadouts() {
const wideCount = wideCubes.length;
const tallCount = tallCubes.length;
const total = wideCount + tallCount;
const wideLayers = layerCount(wide, wideCount);
const tallLayers = layerCount(tall, tallCount);
$("wideCount").textContent = wideCount;
$("tallCount").textContent = tallCount;
$("totalChip").textContent = `${total} total`;
$("wideMeta").textContent =
`${wideLayers} layer${wideLayers === 1 ? "" : "s"} · 6 × 4 footprint`;
$("tallMeta").textContent =
`${tallLayers} layer${tallLayers === 1 ? "" : "s"} · 3 × 3 footprint`;
$("wideBar").style.width =
`${(wideCount / wide.capacity) * 100}%`;
$("tallBar").style.width =
`${(tallCount / tall.capacity) * 100}%`;
$("conservationText").textContent = `${lastTotal} → ${total}`;
const difference = Math.abs(wideCount - tallCount);
$("equalizeButton").disabled = busy || difference < 2;
updateSceneLabels(wideCount, tallCount, wideLayers, tallLayers);
}
function updateSceneLabels(wideCount, tallCount, wideLayers, tallLayers) {
$("wideLabel").innerHTML = `
<div class="label-head"><span>WIDE</span><span>6 × 4</span></div>
<strong>${wideCount} cubes</strong>
<small>${wideLayers} layers · height ${(wideLayers * CELL).toFixed(2)}</small>
`;
$("tallLabel").innerHTML = `
<div class="label-head"><span>TALL</span><span>3 × 3</span></div>
<strong>${tallCount} cubes</strong>
<small>${tallLayers} layers · height ${(tallLayers * CELL).toFixed(2)}</small>
`;
}
let toastTimer = null;
function showToast(message) {
$("toast").textContent = message;
$("toast").classList.add("visible");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
$("toast").classList.remove("visible");
}, 2600);
}
function performTransfer(source, destination, amount) {
if (busy) return;
amount = Math.min(Math.floor(amount), source.length);
if (amount < 1) {
showToast("There are not enough cubes in the source container.");
return;
}
if (destination.length + amount > destination.capacity) {
showToast("The destination container is at capacity.");
return;
}
busy = true;
lastTotal = source.length + destination.length;
const moving = source.splice(source.length - amount, amount);
const destinationStart = destination.length;
moving.forEach((cube, index) => {
cube.userData.owner = destination;
destination.push(cube);
const target = slotPosition(destination, destinationStart + index);
startTransition(cube, target, 880, index * 48, 1.85);
createPulse(target, destination.color);
});
$("removedCount").textContent = amount;
$("addedCount").textContent = amount;
updateReadouts();
updateGauges();
updateTransferButton();
$("transferButton").disabled = true;
$("batchRange").disabled = true;
$("resetButton").disabled = true;
$("toTall").disabled = true;
$("toWide").disabled = true;
$("topStatus").textContent = "TRANSFER IN MOTION";
const lastDelay = (amount - 1) * 48;
setTimeout(() => {
busy = false;
$("transferButton").disabled = false;
$("batchRange").disabled = false;
$("resetButton").disabled = false;
$("toTall").disabled = false;
$("toWide").disabled = false;
$("topStatus").textContent = "CONSERVATION ACTIVE";
updateTransferButton();
updateReadouts();
}, 900 + lastDelay);
}
function equalizeCounts() {
if (busy) return;
const difference = wideCubes.length - tallCubes.length;
if (difference === 0) {
showToast("The two container counts are already equal.");
return;
}
const amount = Math.floor(Math.abs(difference) / 2);
if (difference > 0) {
performTransfer(wideCubes, tallCubes, amount);
} else {
performTransfer(tallCubes, wideCubes, amount);
}
}
$("batchRange").addEventListener("input", () => {
$("batchValue").textContent = `${batchSize()} cubes`;
updateRangeFill();
updateTransferButton();
});
$("toTall").addEventListener("click", () => setDirection(true));
$("toWide").addEventListener("click", () => setDirection(false));
$("transferButton").addEventListener("click", () => {
if (directionToTall) {
performTransfer(wideCubes, tallCubes, batchSize());
} else {
performTransfer(tallCubes, wideCubes, batchSize());
}
});
$("equalizeButton").addEventListener("click", equalizeCounts);
$("resetButton").addEventListener("click", () => {
if (busy) return;
lastTotal = TOTAL_CUBES;
$("removedCount").textContent = "0";
$("addedCount").textContent = "0";
resetCubes(false);
showToast("Both containers reset to 36 cubes.");
});
function scheduleAuto() {
clearTimeout(autoTimer);
if (!$("autoButton").classList.contains("active")) return;
autoTimer = setTimeout(() => {
if (!$("autoButton").classList.contains("active")) return;
const n = Math.min(batchSize(), 12);
if (directionToTall) {
performTransfer(wideCubes, tallCubes, n);
} else {
performTransfer(tallCubes, wideCubes, n);
}
setTimeout(() => {
if (!$("autoButton").classList.contains("active")) return;
setDirection(!directionToTall);
scheduleAuto();
}, 1100 + n * 48);
}, 1450);
}
$("autoButton").addEventListener("click", () => {
const active = $("autoButton").classList.toggle("active");
$("autoButton").textContent = `Auto cycle: ${active ? "on" : "off"}`;
scheduleAuto();
});
// -------------------------------------------------------------------------
// Projected HTML labels
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function positionLabel(element, anchor) {
anchor.getWorldPosition(projected);
projected.project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.opacity = visible ? "1" : "0";
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
fitCamera();
});
const clock = new THREE.Clock();
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const damping = 1 - Math.exp(-delta * 8);
orbit.theta +=
(orbit.desiredTheta - orbit.theta) * damping;
orbit.phi +=
(orbit.desiredPhi - orbit.phi) * damping;
orbit.radius +=
(orbit.desiredRadius - orbit.radius) * damping;
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
updateTransitions(now);
updatePulses(now);
particles.rotation.y += delta * 0.012;
particles.position.y = Math.sin(now * 0.00015) * 0.15;
renderer.render(scene, camera);
positionLabel($("wideLabel"), wide.anchor);
positionLabel($("tallLabel"), tall.anchor);
}
updateRangeFill();
setDirection(true);
resetCubes(true);
requestAnimationFrame(animate);
</script>
</body>
</html>
```