Prompt specification
Build or customize the Three.js 3D scene "Warehouse decimal inventory diorama" 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: Warehouse decimal inventory diorama
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/warehouse-decimal-inventory-diorama-x1719
Scene Description Prompt:
Compose loose items, ten-item bundles, and hundred-item crates with transparent count insets; connect packaging hierarchy to place value without treating physical box size as the number itself. Use Three.js from cdnjs in a single HTML file, with visible voxel blocks.
Working Three.js Starter (warehouse-decimal-inventory-diorama-x1719):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Packaging Studio — Place Value</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(10, 22, 39, 0.78);
--panel-strong: rgba(12, 27, 47, 0.92);
--line: rgba(171, 205, 255, 0.16);
--text: #f3f7ff;
--muted: #9db0ca;
--amber: #ffc857;
--cyan: #56d8ff;
--violet: #b58cff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#scene,
#labels {
position: fixed;
inset: 0;
}
#scene {
z-index: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
#labels {
z-index: 2;
overflow: hidden;
pointer-events: none;
}
.glass {
border: 1px solid var(--line);
background: var(--panel);
box-shadow: 0 16px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(16px) saturate(130%);
-webkit-backdrop-filter: blur(16px) saturate(130%);
}
#title-panel {
position: fixed;
z-index: 4;
top: 20px;
left: 20px;
width: min(430px, calc(100vw - 40px));
padding: 17px 19px 16px;
border-radius: 18px;
pointer-events: none;
}
.eyebrow {
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 8px;
font-size: clamp(23px, 2.6vw, 35px);
line-height: 1.04;
letter-spacing: -0.045em;
}
#title-panel p {
margin: 0;
max-width: 390px;
color: var(--muted);
font-size: 13px;
line-height: 1.52;
}
#title-panel strong {
color: var(--text);
font-weight: 650;
}
#controls {
position: fixed;
z-index: 5;
top: 20px;
right: 20px;
width: 244px;
padding: 13px;
border-radius: 16px;
}
.control-label {
margin: 1px 2px 8px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.view-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 6px;
}
.view-button,
.option-button {
border: 1px solid rgba(170, 205, 255, 0.14);
border-radius: 9px;
background: rgba(255, 255, 255, 0.045);
color: #c8d6e9;
cursor: pointer;
transition:
background 150ms ease,
border-color 150ms ease,
color 150ms ease,
transform 150ms ease;
}
.view-button {
padding: 8px 7px;
font-size: 11px;
font-weight: 650;
}
.view-button:hover,
.option-button:hover {
color: white;
background: rgba(255, 255, 255, 0.09);
transform: translateY(-1px);
}
.view-button.active {
color: #05111d;
border-color: var(--cyan);
background: var(--cyan);
}
.option-button {
width: 100%;
margin-top: 7px;
padding: 9px 10px;
text-align: left;
font-size: 11px;
font-weight: 650;
}
.option-button[aria-pressed="true"] {
color: var(--cyan);
border-color: rgba(86, 216, 255, 0.5);
background: rgba(86, 216, 255, 0.1);
}
#readout {
position: fixed;
z-index: 4;
left: 50%;
bottom: 20px;
width: min(900px, calc(100vw - 40px));
padding: 15px 18px 14px;
border-radius: 18px;
transform: translateX(-50%);
pointer-events: none;
}
.readout-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
}
#value-equation {
display: flex;
align-items: center;
gap: 11px;
min-width: 0;
white-space: nowrap;
}
#value-number {
color: var(--amber);
font-size: clamp(30px, 4vw, 46px);
font-weight: 820;
line-height: 0.95;
letter-spacing: -0.05em;
}
#value-name {
color: #d9e5f5;
font-size: 13px;
font-weight: 700;
}
#place-value {
color: var(--cyan);
font-size: 12px;
font-weight: 750;
}
#hierarchy {
display: flex;
align-items: center;
gap: 8px;
color: #c9d7e9;
font-size: 12px;
font-weight: 680;
white-space: nowrap;
}
#hierarchy .arrow {
color: #607895;
}
#explanation {
margin: 10px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.principle {
display: flex;
align-items: center;
gap: 8px;
margin-top: 10px;
padding-top: 10px;
border-top: 1px solid rgba(170, 205, 255, 0.11);
color: #c9d6e8;
font-size: 11px;
line-height: 1.4;
}
.principle b {
flex: 0 0 auto;
padding: 3px 7px;
border: 1px solid rgba(255, 200, 87, 0.3);
border-radius: 999px;
color: var(--amber);
background: rgba(255, 200, 87, 0.08);
font-size: 10px;
}
#hint {
position: fixed;
z-index: 3;
left: 22px;
bottom: 176px;
color: rgba(194, 211, 232, 0.66);
font-size: 10px;
letter-spacing: 0.02em;
pointer-events: none;
}
.count-inset {
position: absolute;
top: 0;
left: 0;
min-width: 142px;
padding: 10px 12px 9px;
overflow: hidden;
border: 1px solid rgba(187, 215, 255, 0.22);
border-radius: 13px;
background: rgba(7, 18, 33, 0.64);
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px) saturate(140%);
-webkit-backdrop-filter: blur(12px) saturate(140%);
transform: translate(-50%, -50%);
transition:
border-color 160ms ease,
background 160ms ease,
opacity 160ms ease;
will-change: left, top;
}
.count-inset::after {
content: "";
position: absolute;
left: 50%;
bottom: -25px;
width: 1px;
height: 25px;
background: linear-gradient(rgba(187, 215, 255, 0.45), transparent);
}
.count-inset.active {
border-color: rgba(86, 216, 255, 0.68);
background: rgba(8, 22, 40, 0.86);
}
.count-inset.loose.active {
border-color: rgba(255, 200, 87, 0.72);
}
.count-inset.crate.active {
border-color: rgba(181, 140, 255, 0.75);
}
.inset-eyebrow {
color: #9fb2ca;
font-size: 8px;
font-weight: 850;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.inset-main {
display: flex;
align-items: baseline;
gap: 6px;
margin-top: 2px;
}
.inset-number {
font-size: 29px;
font-weight: 830;
line-height: 1;
letter-spacing: -0.045em;
}
.inset-unit {
color: #d7e2f1;
font-size: 11px;
font-weight: 700;
}
.inset-note {
margin-top: 4px;
color: #8fa4bf;
font-size: 9px;
line-height: 1.25;
}
.loose .inset-number {
color: var(--amber);
}
.bundle .inset-number {
color: var(--cyan);
}
.crate .inset-number {
color: var(--violet);
}
@media (max-width: 760px) {
#title-panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px 15px;
}
#title-panel p {
display: none;
}
#controls {
top: 102px;
right: 12px;
width: 180px;
padding: 9px;
}
.control-label {
margin-bottom: 5px;
}
.view-button {
padding: 6px 4px;
font-size: 9px;
}
.option-button {
padding: 7px;
font-size: 9px;
}
#readout {
bottom: 12px;
width: calc(100vw - 24px);
padding: 12px 13px;
}
.readout-top {
align-items: flex-start;
flex-direction: column;
gap: 9px;
}
#hierarchy {
flex-wrap: wrap;
white-space: normal;
}
#hint {
display: none;
}
.count-inset {
min-width: 112px;
padding: 8px 9px;
}
.inset-number {
font-size: 23px;
}
.inset-note {
display: none;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive 3D place-value model"></div>
<div id="labels" aria-hidden="true"></div>
<section id="title-panel" class="glass">
<div class="eyebrow">Packaging Studio</div>
<h1>Same blocks.<br>New place value.</h1>
<p>
Every yellow voxel is <strong>one item</strong>. Group ten items into a
sleeve, then group ten sleeves into a crate.
<strong>Counts—not package dimensions—give the value.</strong>
</p>
</section>
<section id="controls" class="glass" aria-label="Scene controls">
<div class="control-label">Focus a level</div>
<div class="view-grid">
<button class="view-button active" data-view="all">All levels</button>
<button class="view-button" data-view="loose">Loose items</button>
<button class="view-button" data-view="bundle">Ten-bundle</button>
<button class="view-button" data-view="crate">Hundred-crate</button>
</div>
<button id="separate-button" class="option-button" aria-pressed="false">
Separate crate bundles
</button>
<button id="labels-button" class="option-button" aria-pressed="true">
Show count insets
</button>
</section>
<section id="readout" class="glass" aria-live="polite">
<div class="readout-top">
<div id="value-equation">
<span id="value-number">1 → 10 → 100</span>
<span id="value-name">ones → tens → hundreds</span>
<span id="place-value">place value</span>
</div>
<div id="hierarchy" aria-label="Place-value hierarchy">
<span>10 ones</span>
<span class="arrow">→</span>
<span>1 ten</span>
<span class="arrow">→</span>
<span>10 tens</span>
<span class="arrow">→</span>
<span>1 hundred</span>
</div>
</div>
<p id="explanation">
Ten ones can be packaged as one ten. Ten of those ten-bundles can be
packaged as one hundred.
</p>
<div class="principle">
<b>Important</b>
<span>
A package is not “100” because it is physically large. The count inset
and the grouping structure identify its value.
</span>
</div>
</section>
<div id="hint">Drag to orbit · Scroll to zoom · Click a package · Keys 1–3, 0, E</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.querySelector("#scene");
const labelsHost = document.querySelector("#labels");
const COLORS = {
bg: 0x07111f,
amber: 0xffc857,
cyan: 0x56d8ff,
violet: 0xb58cff,
platform: 0x101d30
};
const scene = new THREE.Scene();
scene.background = new THREE.Color(COLORS.bg);
scene.fog = new THREE.Fog(COLORS.bg, 19, 38);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute("tabindex", "0");
sceneHost.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xa9cfff, 0x101522, 2.2);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(5, 11, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x36c9ff, 32, 15, 2);
cyanLight.position.set(-5, 3.5, -4);
scene.add(cyanLight);
const violetLight = new THREE.PointLight(0x9f6dff, 36, 16, 2);
violetLight.position.set(6, 4, -3);
scene.add(violetLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(38, 28),
new THREE.MeshStandardMaterial({
color: 0x091525,
roughness: 0.9,
metalness: 0.05
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.075;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(28, 28, 0x27425e, 0x15283d);
grid.position.y = -0.065;
grid.material.transparent = true;
grid.material.opacity = 0.55;
scene.add(grid);
const voxelGeometry = new THREE.BoxGeometry(0.28, 0.28, 0.28);
const voxelEdgesGeometry = new THREE.EdgesGeometry(voxelGeometry, 20);
const voxelMaterial = new THREE.MeshStandardMaterial({
color: COLORS.amber,
roughness: 0.32,
metalness: 0.08,
emissive: 0x3b2100,
emissiveIntensity: 0.18
});
const voxelEdgeMaterial = new THREE.LineBasicMaterial({
color: 0xffe5a7,
transparent: true,
opacity: 0.72
});
const pickables = [];
const stages = {};
const crateBundleGroups = [];
const selectionFrames = [];
function makeVoxel(parent, x, y, z, stage) {
const voxel = new THREE.Mesh(voxelGeometry, voxelMaterial);
voxel.position.set(x, y, z);
voxel.castShadow = true;
voxel.receiveShadow = true;
voxel.userData.stage = stage;
const edges = new THREE.LineSegments(
voxelEdgesGeometry,
voxelEdgeMaterial
);
edges.scale.setScalar(1.006);
voxel.add(edges);
parent.add(voxel);
pickables.push(voxel);
return voxel;
}
function makeStageBase(parent, width, depth, accent) {
const base = new THREE.Mesh(
new THREE.BoxGeometry(width, 0.13, depth),
new THREE.MeshStandardMaterial({
color: COLORS.platform,
roughness: 0.62,
metalness: 0.18
})
);
base.position.y = 0;
base.receiveShadow = true;
parent.add(base);
const baseEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(base.geometry),
new THREE.LineBasicMaterial({
color: accent,
transparent: true,
opacity: 0.22
})
);
base.add(baseEdges);
const points = [
new THREE.Vector3(-width / 2, 0.078, -depth / 2),
new THREE.Vector3(width / 2, 0.078, -depth / 2),
new THREE.Vector3(width / 2, 0.078, depth / 2),
new THREE.Vector3(-width / 2, 0.078, depth / 2)
];
const frameMaterial = new THREE.LineBasicMaterial({
color: accent,
transparent: true,
opacity: 0.24
});
const frame = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(points),
frameMaterial
);
parent.add(frame);
selectionFrames.push({ frame, stage: parent.userData.stage });
}
function makeSleeve(parent, width, height, depth, accent, opacity = 0.055) {
const material = new THREE.MeshStandardMaterial({
color: accent,
transparent: true,
opacity,
roughness: 0.2,
metalness: 0.05,
depthWrite: false,
side: THREE.DoubleSide
});
const sleeve = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
sleeve.position.y = 0.1 + height / 2;
sleeve.userData.stage = parent.userData.stage;
parent.add(sleeve);
pickables.push(sleeve);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(sleeve.geometry),
new THREE.LineBasicMaterial({
color: accent,
transparent: true,
opacity: 0.46
})
);
edges.position.copy(sleeve.position);
parent.add(edges);
return sleeve;
}
function makeCrateShell(parent, width, height, depth) {
const shellMaterial = new THREE.MeshStandardMaterial({
color: COLORS.violet,
transparent: true,
opacity: 0.105,
roughness: 0.22,
metalness: 0.05,
depthWrite: false,
side: THREE.DoubleSide
});
const edgeMaterial = new THREE.LineBasicMaterial({
color: COLORS.violet,
transparent: true,
opacity: 0.58
});
const wallThickness = 0.035;
const walls = [
{
geometry: new THREE.BoxGeometry(width, height, wallThickness),
position: new THREE.Vector3(0, 0.1 + height / 2, depth / 2)
},
{
geometry: new THREE.BoxGeometry(width, height, wallThickness),
position: new THREE.Vector3(0, 0.1 + height / 2, -depth / 2)
},
{
geometry: new THREE.BoxGeometry(wallThickness, height, depth),
position: new THREE.Vector3(width / 2, 0.1 + height / 2, 0)
},
{
geometry: new THREE.BoxGeometry(wallThickness, height, depth),
position: new THREE.Vector3(-width / 2, 0.1 + height / 2, 0)
}
];
for (const wall of walls) {
const mesh = new THREE.Mesh(wall.geometry, shellMaterial);
mesh.position.copy(wall.position);
mesh.userData.stage = "crate";
parent.add(mesh);
pickables.push(mesh);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(wall.geometry),
edgeMaterial
);
edges.position.copy(wall.position);
parent.add(edges);
}
}
function makePickProxy(parent, stage, width, height, depth) {
const proxy = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
})
);
proxy.position.y = 0.1 + height / 2;
proxy.userData.stage = stage;
parent.add(proxy);
pickables.push(proxy);
}
// Loose items: each visible cube represents exactly one item.
const looseRoot = new THREE.Group();
looseRoot.position.set(-5.25, 0, 0);
looseRoot.userData.stage = "loose";
scene.add(looseRoot);
stages.loose = looseRoot;
makeStageBase(looseRoot, 2.25, 2.15, COLORS.amber);
const loosePositions = [
[-0.65, -0.52],
[-0.25, -0.52],
[0.15, -0.52],
[0.46, 0.03],
[-0.08, 0.22]
];
for (const [x, z] of loosePositions) {
makeVoxel(looseRoot, x, 0.245, z, "loose");
}
makePickProxy(looseRoot, "loose", 1.8, 0.8, 1.7);
// One ten-item bundle: ten individually visible unit voxels.
const bundleRoot = new THREE.Group();
bundleRoot.position.set(-1.55, 0, 0);
bundleRoot.userData.stage = "bundle";
scene.add(bundleRoot);
stages.bundle = bundleRoot;
makeStageBase(bundleRoot, 2.15, 2.4, COLORS.cyan);
for (let row = 0; row < 5; row++) {
for (let column = 0; column < 2; column++) {
makeVoxel(
bundleRoot,
(column - 0.5) * 0.31,
0.245,
(row - 2) * 0.31,
"bundle"
);
}
}
makeSleeve(bundleRoot, 0.72, 0.58, 1.68, COLORS.cyan, 0.07);
makePickProxy(bundleRoot, "bundle", 0.9, 0.85, 1.85);
// A hundred-crate built from ten visible ten-item bundles.
const crateRoot = new THREE.Group();
crateRoot.position.set(3.35, 0, 0);
crateRoot.userData.stage = "crate";
scene.add(crateRoot);
stages.crate = crateRoot;
makeStageBase(crateRoot, 4.25, 3.05, COLORS.violet);
makeCrateShell(crateRoot, 3.65, 1.25, 2.5);
let bundleIndex = 0;
for (let row = 0; row < 2; row++) {
for (let column = 0; column < 5; column++) {
const group = new THREE.Group();
const baseX = (column - 2) * 0.68;
const baseZ = (row - 0.5) * 0.88;
group.position.set(baseX, 0, baseZ);
group.userData.stage = "crate";
group.userData.basePosition = group.position.clone();
group.userData.explodeOffset = new THREE.Vector3(
(column - 2) * 0.17,
0.08 + (row === 0 ? 0 : 0.08),
(row - 0.5) * 0.34
);
for (let innerRow = 0; innerRow < 5; innerRow++) {
for (let innerColumn = 0; innerColumn < 2; innerColumn++) {
makeVoxel(
group,
(innerColumn - 0.5) * 0.31,
0.245,
(innerRow - 2) * 0.31,
"crate"
);
}
}
makeSleeve(group, 0.72, 0.58, 1.68, COLORS.cyan, 0.035);
crateRoot.add(group);
crateBundleGroups.push(group);
bundleIndex++;
}
}
makePickProxy(crateRoot, "crate", 4.0, 1.5, 2.9);
// DOM count insets, projected from world-space anchor points.
const labelDefinitions = [
{
stage: "loose",
root: stages.loose,
anchor: new THREE.Vector3(-5.25, 1.25, -0.15),
eyebrow: "Loose item",
number: "1",
unit: "item",
note: "Each unboxed voxel is one."
},
{
stage: "bundle",
root: stages.bundle,
anchor: new THREE.Vector3(-1.55, 1.25, -0.15),
eyebrow: "Ten-item bundle",
number: "10",
unit: "items",
note: "10 ones grouped as 1 ten."
},
{
stage: "crate",
root: stages.crate,
anchor: new THREE.Vector3(3.35, 1.75, -0.25),
eyebrow: "Hundred-item crate",
number: "100",
unit: "items",
note: "10 bundles of 10 = 1 hundred."
}
];
for (const definition of labelDefinitions) {
const element = document.createElement("div");
element.className = `count-inset ${definition.stage}`;
element.innerHTML = `
<div class="inset-eyebrow">${definition.eyebrow}</div>
<div class="inset-main">
<span class="inset-number">${definition.number}</span>
<span class="inset-unit">${definition.unit}</span>
</div>
<div class="inset-note">${definition.note}</div>
`;
labelsHost.appendChild(element);
definition.element = element;
}
// Direct camera controls: orbit, zoom, and focus transitions.
const orbit = {
theta: 0.67,
phi: 0.78,
radius: 15.5,
target: new THREE.Vector3(-0.85, 0.55, 0)
};
const desired = {
theta: orbit.theta,
phi: orbit.phi,
radius: orbit.radius,
target: orbit.target.clone()
};
function updateCamera() {
orbit.theta += (desired.theta - orbit.theta) * 0.09;
orbit.phi += (desired.phi - orbit.phi) * 0.09;
orbit.radius += (desired.radius - orbit.radius) * 0.09;
orbit.target.lerp(desired.target, 0.09);
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);
}
function allViewRadius() {
return window.innerWidth / window.innerHeight < 0.9 ? 22 : 15.5;
}
function setView(view) {
document.querySelectorAll(".view-button").forEach((button) => {
button.classList.toggle("active", button.dataset.view === view);
});
for (const label of labelDefinitions) {
label.element.classList.toggle(
"active",
view === "all" || label.stage === view
);
}
const number = document.querySelector("#value-number");
const name = document.querySelector("#value-name");
const placeValue = document.querySelector("#place-value");
const explanation = document.querySelector("#explanation");
if (view === "loose") {
desired.target.set(-5.25, 0.45, 0);
desired.radius = window.innerWidth < 760 ? 9.5 : 7.3;
number.textContent = "1";
name.textContent = "loose item";
placeValue.textContent = "= 1 one";
explanation.textContent =
"A single visible voxel is one item, so it has a place value of one.";
} else if (view === "bundle") {
desired.target.set(-1.55, 0.45, 0);
desired.radius = window.innerWidth < 760 ? 10 : 7.8;
number.textContent = "10";
name.textContent = "items in one bundle";
placeValue.textContent = "= 1 ten";
explanation.textContent =
"Ten one-item voxels are grouped into a bundle. The grouping changes the place value from ones to one ten.";
} else if (view === "crate") {
desired.target.set(3.35, 0.6, 0);
desired.radius = window.innerWidth < 760 ? 13 : 10.2;
number.textContent = "100";
name.textContent = "items in one crate";
placeValue.textContent = "= 1 hundred";
explanation.textContent =
"The crate contains ten bundles, and every bundle contains ten items: 10 tens make 1 hundred.";
} else {
desired.target.set(-0.85, 0.55, 0);
desired.radius = allViewRadius();
number.textContent = "1 → 10 → 100";
name.textContent = "ones → tens → hundreds";
placeValue.textContent = "place value";
explanation.textContent =
"Ten ones can be packaged as one ten. Ten of those ten-bundles can be packaged as one hundred.";
}
}
document.querySelectorAll(".view-button").forEach((button) => {
button.addEventListener("click", () => setView(button.dataset.view));
});
const separateButton = document.querySelector("#separate-button");
let separated = false;
separateButton.addEventListener("click", () => {
separated = !separated;
separateButton.setAttribute("aria-pressed", String(separated));
separateButton.textContent = separated
? "Rejoin crate bundles"
: "Separate crate bundles";
});
const labelsButton = document.querySelector("#labels-button");
let labelsVisible = true;
labelsButton.addEventListener("click", () => {
labelsVisible = !labelsVisible;
labelsButton.setAttribute("aria-pressed", String(labelsVisible));
labelsButton.textContent = labelsVisible
? "Show count insets"
: "Hide count insets";
labelsHost.style.display = labelsVisible ? "block" : "none";
});
// Raycasting for package selection.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function getHit(clientX, clientY) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
return raycaster.intersectObjects(pickables, false)[0];
}
let drag = null;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
drag = {
id: event.pointerId,
startX: event.clientX,
startY: event.clientY,
lastX: event.clientX,
lastY: event.clientY,
theta: desired.theta,
phi: desired.phi,
moved: false
};
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!drag) {
const hit = getHit(event.clientX, event.clientY);
renderer.domElement.style.cursor = hit ? "pointer" : "grab";
return;
}
const dx = event.clientX - drag.lastX;
const dy = event.clientY - drag.lastY;
const totalDistance = Math.hypot(
event.clientX - drag.startX,
event.clientY - drag.startY
);
if (totalDistance > 4) drag.moved = true;
desired.theta = drag.theta - dx * 0.006;
desired.phi = THREE.MathUtils.clamp(
drag.phi - dy * 0.005,
0.35,
1.25
);
drag.lastX = event.clientX;
drag.lastY = event.clientY;
});
function finishPointer(event) {
if (!drag) return;
if (!drag.moved) {
const hit = getHit(event.clientX, event.clientY);
if (hit?.object.userData.stage) {
setView(hit.object.userData.stage);
}
}
drag = null;
renderer.domElement.style.cursor = "grab";
}
renderer.domElement.addEventListener("pointerup", finishPointer);
renderer.domElement.addEventListener("pointercancel", finishPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
const factor = Math.exp(event.deltaY * 0.001);
desired.radius = THREE.MathUtils.clamp(
desired.radius * factor,
5.2,
28
);
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if (key === "1") setView("loose");
if (key === "2") setView("bundle");
if (key === "3") setView("crate");
if (key === "0") setView("all");
if (key === "e") {
separateButton.click();
}
});
function updateLabels() {
if (!labelsVisible) return;
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labelDefinitions) {
const worldPosition = label.root.localToWorld(
label.anchor.clone()
);
const projected = worldPosition.clone().project(camera);
const onScreen =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.15 &&
projected.x < 1.15 &&
projected.y > -1.15 &&
projected.y < 1.15;
label.element.style.opacity = onScreen ? "1" : "0";
label.element.style.left = `${(projected.x * 0.5 + 0.5) * width}px`;
label.element.style.top = `${(-projected.y * 0.5 + 0.5) * height}px`;
}
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
for (const group of crateBundleGroups) {
const target = separated
? group.userData.basePosition.clone().add(group.userData.explodeOffset)
: group.userData.basePosition;
group.position.lerp(target, 0.085);
}
for (const entry of selectionFrames) {
const active =
document.querySelector(".view-button.active")?.dataset.view;
const isSelected =
active === "all" || active === entry.stage;
entry.frame.material.opacity = isSelected
? 0.62 + Math.sin(elapsed * 2.2) * 0.12
: 0.16;
}
updateCamera();
scene.updateMatrixWorld(true);
updateLabels();
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```