Prompt specification
Build or customize the Three.js 3D scene "Initial Stack and Annual Increment Blocks" 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: Initial Stack and Annual Increment Blocks
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/initial-stack-and-annual-increment-blocks-x3197
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing simple additive growth. Display a starting stack of ten blocks beside three identical increment trays. Label this as a toy arithmetic model with no real investment or return claims. Stylized solid forms.
Working Three.js Starter (initial-stack-and-annual-increment-blocks-x3197):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Additive Growth — Toy Arithmetic Model</title>
<style>
:root {
--ink: #18343b;
--muted: #65777b;
--paper: #f4efe6;
--panel: rgba(255, 255, 255, 0.88);
--line: rgba(24, 52, 59, 0.14);
--teal: #278f83;
--gold: #f2bf4d;
--coral: #ee7454;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
outline: none;
}
.top-copy {
position: fixed;
z-index: 4;
top: 28px;
left: 32px;
max-width: 430px;
pointer-events: none;
}
.eyebrow {
margin: 0 0 8px;
color: var(--teal);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(2.2rem, 4.2vw, 4.35rem);
font-weight: 780;
letter-spacing: -0.055em;
line-height: 0.96;
}
.top-copy p:last-child {
max-width: 370px;
margin: 14px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.disclaimer {
position: fixed;
z-index: 4;
top: 28px;
right: 28px;
width: min(340px, calc(100vw - 56px));
padding: 14px 16px;
border: 1px solid rgba(24, 52, 59, 0.14);
border-radius: 15px;
background: rgba(255, 255, 255, 0.72);
box-shadow: 0 12px 35px rgba(24, 52, 59, 0.07);
backdrop-filter: blur(14px);
color: #52666b;
font-size: 11px;
line-height: 1.5;
}
.disclaimer strong {
display: block;
margin-bottom: 3px;
color: var(--ink);
font-size: 11px;
letter-spacing: 0.04em;
}
.gesture-note {
position: fixed;
z-index: 4;
top: 30px;
left: 50%;
transform: translateX(-50%);
padding: 8px 12px;
border: 1px solid rgba(24, 52, 59, 0.11);
border-radius: 999px;
background: rgba(255, 255, 255, 0.68);
color: #617579;
font-size: 10px;
letter-spacing: 0.02em;
white-space: nowrap;
pointer-events: none;
backdrop-filter: blur(10px);
}
.scene-label {
position: fixed;
z-index: 3;
min-width: 106px;
padding: 8px 11px 9px;
transform: translate(-50%, 0);
border: 1px solid rgba(24, 52, 59, 0.13);
border-radius: 12px;
background: rgba(255, 255, 255, 0.86);
box-shadow: 0 8px 24px rgba(24, 52, 59, 0.08);
text-align: center;
pointer-events: none;
backdrop-filter: blur(8px);
transition: opacity 120ms linear;
}
.scene-label span {
display: block;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.scene-label strong {
display: block;
margin-top: 2px;
color: var(--ink);
font-size: 13px;
font-weight: 750;
}
.control-dock {
position: fixed;
z-index: 6;
right: 24px;
bottom: 22px;
left: 24px;
display: grid;
grid-template-columns: minmax(300px, 1.05fr) minmax(360px, 1fr) auto;
align-items: center;
gap: 18px;
max-width: 980px;
margin: 0 auto;
padding: 14px;
border: 1px solid rgba(24, 52, 59, 0.14);
border-radius: 20px;
background: var(--panel);
box-shadow: 0 18px 55px rgba(24, 52, 59, 0.13);
backdrop-filter: blur(18px);
}
.equation-panel {
min-width: 0;
}
.equation-title {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.equation {
display: flex;
align-items: center;
gap: 9px;
white-space: nowrap;
font-variant-numeric: tabular-nums;
}
.equation span {
display: inline-flex;
min-width: 30px;
height: 30px;
align-items: center;
justify-content: center;
border-radius: 9px;
background: #e8f1ef;
color: var(--teal);
font-size: 14px;
font-weight: 800;
}
.equation .base {
background: #fde9e2;
color: #cc5d40;
}
.equation .result {
min-width: 38px;
background: var(--ink);
color: #fff;
}
.equation b {
color: #7a898c;
font-size: 13px;
font-weight: 500;
}
.tray-controls {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 8px;
}
.tray-button {
position: relative;
display: grid;
min-height: 62px;
grid-template-columns: 1fr auto;
grid-template-rows: auto auto;
gap: 2px 8px;
padding: 10px 11px;
overflow: hidden;
border: 1px solid rgba(39, 143, 131, 0.2);
border-radius: 13px;
background: #f7faf8;
color: var(--ink);
cursor: pointer;
text-align: left;
transition:
transform 140ms ease,
border-color 140ms ease,
background 140ms ease,
box-shadow 140ms ease;
}
.tray-button:hover:not(:disabled) {
transform: translateY(-2px);
border-color: rgba(39, 143, 131, 0.55);
background: #eef7f4;
box-shadow: 0 8px 18px rgba(39, 143, 131, 0.11);
}
.tray-button:active:not(:disabled) {
transform: translateY(0);
}
.tray-button:focus-visible,
.reset-button:focus-visible {
outline: 3px solid rgba(39, 143, 131, 0.25);
outline-offset: 2px;
}
.tray-button:disabled {
opacity: 0.45;
cursor: not-allowed;
}
.tray-name {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.11em;
}
.tray-count {
grid-column: 1;
font-size: 11px;
font-weight: 650;
color: #5e7074;
}
.tray-count b {
color: var(--ink);
font-size: 15px;
}
.plus-token {
display: flex;
width: 30px;
height: 30px;
grid-column: 2;
grid-row: 1 / 3;
align-items: center;
justify-content: center;
align-self: center;
border-radius: 50%;
background: var(--gold);
box-shadow: inset 0 -2px 0 rgba(24, 52, 59, 0.08);
color: var(--ink);
font-size: 11px;
font-weight: 900;
}
.reset-button {
min-height: 62px;
padding: 0 18px;
border: 1px solid rgba(24, 52, 59, 0.16);
border-radius: 13px;
background: transparent;
color: var(--muted);
cursor: pointer;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.03em;
transition:
color 140ms ease,
background 140ms ease;
}
.reset-button:hover {
background: rgba(24, 52, 59, 0.05);
color: var(--ink);
}
#toast {
position: fixed;
z-index: 8;
top: 22%;
left: 50%;
padding: 10px 15px;
transform: translate(-50%, -8px);
border-radius: 999px;
background: var(--ink);
box-shadow: 0 10px 30px rgba(24, 52, 59, 0.2);
color: white;
font-size: 12px;
font-weight: 650;
opacity: 0;
pointer-events: none;
transition:
opacity 180ms ease,
transform 180ms ease;
}
#toast.visible {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 900px) {
.gesture-note {
display: none;
}
.top-copy {
top: 20px;
left: 20px;
}
.disclaimer {
top: 20px;
right: 20px;
}
.control-dock {
right: 14px;
bottom: 14px;
left: 14px;
grid-template-columns: minmax(250px, 0.9fr) minmax(330px, 1fr) auto;
gap: 10px;
padding: 11px;
}
.tray-button {
padding: 9px;
}
}
@media (max-width: 700px) {
.top-copy {
top: 16px;
left: 16px;
}
.top-copy p:last-child {
display: none;
}
.eyebrow {
font-size: 9px;
}
h1 {
font-size: 2.35rem;
}
.disclaimer {
top: 82px;
right: auto;
left: 16px;
width: min(270px, calc(100vw - 32px));
padding: 9px 11px;
font-size: 9px;
}
.disclaimer strong {
font-size: 9px;
}
.control-dock {
right: 8px;
bottom: 8px;
left: 8px;
grid-template-columns: 1fr auto;
gap: 8px;
padding: 9px;
border-radius: 16px;
}
.equation-panel {
grid-column: 1 / -1;
}
.equation {
gap: 6px;
}
.equation span {
min-width: 27px;
height: 27px;
}
.tray-controls {
grid-column: 1;
gap: 5px;
}
.tray-button {
min-height: 55px;
padding: 8px;
}
.tray-name {
font-size: 8px;
}
.tray-count {
font-size: 9px;
}
.tray-count b {
font-size: 13px;
}
.plus-token {
width: 25px;
height: 25px;
font-size: 9px;
}
.reset-button {
min-height: 55px;
padding: 0 11px;
font-size: 10px;
}
.scene-label {
min-width: 84px;
padding: 6px 8px;
}
.scene-label span {
font-size: 7px;
}
.scene-label strong {
font-size: 11px;
}
}
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
scroll-behavior: auto !important;
transition-duration: 0.01ms !important;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<header class="top-copy">
<p class="eyebrow">Playable math / 01</p>
<h1>Additive growth</h1>
<p>
Begin with ten blocks. Add one block to any matching tray and watch the
total change.
</p>
</header>
<aside class="disclaimer">
<strong>Toy arithmetic model only</strong>
Blocks represent simple addition. This scene is not investment advice,
a forecast, or a claim about real-world returns.
</aside>
<div class="gesture-note">
Drag to orbit · Scroll or pinch to zoom · Click a tray for +1
</div>
<div id="label-start" class="scene-label">
<span>Starting stack</span>
<strong>10 blocks</strong>
</div>
<div id="label-a" class="scene-label">
<span>Tray A</span>
<strong>0 added</strong>
</div>
<div id="label-b" class="scene-label">
<span>Tray B</span>
<strong>0 added</strong>
</div>
<div id="label-c" class="scene-label">
<span>Tray C</span>
<strong>0 added</strong>
</div>
<section class="control-dock" aria-label="Addition controls">
<div class="equation-panel">
<span class="equation-title">Running equation</span>
<div id="equation" class="equation" aria-live="polite">
<span class="base">10</span>
<b>+</b>
<span id="eq-a">0</span>
<b>+</b>
<span id="eq-b">0</span>
<b>+</b>
<span id="eq-c">0</span>
<b>=</b>
<span id="eq-total" class="result">10</span>
</div>
</div>
<div class="tray-controls">
<button class="tray-button" type="button" data-tray="0" aria-label="Add one block to tray A">
<span class="tray-name">TRAY A</span>
<span class="tray-count"><b id="count-a">0</b> blocks</span>
<span class="plus-token" aria-hidden="true">+1</span>
</button>
<button class="tray-button" type="button" data-tray="1" aria-label="Add one block to tray B">
<span class="tray-name">TRAY B</span>
<span class="tray-count"><b id="count-b">0</b> blocks</span>
<span class="plus-token" aria-hidden="true">+1</span>
</button>
<button class="tray-button" type="button" data-tray="2" aria-label="Add one block to tray C">
<span class="tray-name">TRAY C</span>
<span class="tray-count"><b id="count-c">0</b> blocks</span>
<span class="plus-token" aria-hidden="true">+1</span>
</button>
</div>
<button id="reset" class="reset-button" type="button">Reset</button>
</section>
<div id="toast" role="status" aria-live="polite"></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 reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
const BASE_COUNT = 10;
const MAX_PER_TRAY = 36;
const state = {
counts: [0, 0, 0],
total: BASE_COUNT
};
// ---------------------------------------------------------------------
// Renderer, scene, camera
// ---------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf4efe6);
scene.fog = new THREE.Fog(0xf4efe6, 30, 55);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional toy showing ten starting blocks and three addition trays"
);
sceneHost.appendChild(renderer.domElement);
const cameraTarget = new THREE.Vector3(0.25, 1.1, 0);
let cameraYaw = 0.22;
let cameraPitch = 0.58;
let cameraRadius = 15;
function initialCameraDistance() {
const aspect = window.innerWidth / window.innerHeight;
if (aspect < 0.72) return 28;
if (aspect < 1.05) return 21;
return 15;
}
cameraRadius = initialCameraDistance();
function updateCamera() {
const horizontalRadius = Math.sin(cameraPitch) * cameraRadius;
camera.position.set(
cameraTarget.x + horizontalRadius * Math.sin(cameraYaw),
cameraTarget.y + Math.cos(cameraPitch) * cameraRadius,
cameraTarget.z + horizontalRadius * Math.cos(cameraYaw)
);
camera.lookAt(cameraTarget);
}
updateCamera();
// ---------------------------------------------------------------------
// Lighting and materials
// ---------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xfff7e8, 0x7f9699, 1.75);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff4df, 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 = 10;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xb8e4df, 1.25);
fillLight.position.set(-7, 5, -5);
scene.add(fillLight);
const materials = {
ground: new THREE.MeshStandardMaterial({
color: 0xe9e1d5,
roughness: 0.96,
metalness: 0
}),
plinth: new THREE.MeshStandardMaterial({
color: 0x18343b,
roughness: 0.72,
metalness: 0.03
}),
tray: new THREE.MeshStandardMaterial({
color: 0x278f83,
roughness: 0.66,
metalness: 0.02
}),
trayFloor: new THREE.MeshStandardMaterial({
color: 0x1d706a,
roughness: 0.78,
metalness: 0
}),
plus: new THREE.MeshStandardMaterial({
color: 0xffdd75,
roughness: 0.62,
metalness: 0
}),
startBlock: new THREE.MeshStandardMaterial({
color: 0xee7454,
roughness: 0.67,
metalness: 0.01
}),
trayBlock: new THREE.MeshStandardMaterial({
color: 0xf2bf4d,
roughness: 0.64,
metalness: 0.01
})
};
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
materials.ground
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.015;
ground.receiveShadow = true;
scene.add(ground);
// ---------------------------------------------------------------------
// Shared geometry
// ---------------------------------------------------------------------
const startBlockGeometry = new THREE.BoxGeometry(0.62, 0.62, 0.62);
const trayBlockGeometry = new THREE.BoxGeometry(0.42, 0.42, 0.42);
const trayFloorGeometry = new THREE.BoxGeometry(1.62, 0.18, 1.5);
const traySideGeometry = new THREE.BoxGeometry(0.14, 0.46, 1.5);
const trayEndGeometry = new THREE.BoxGeometry(1.34, 0.46, 0.14);
const plusHorizontalGeometry = new THREE.BoxGeometry(0.28, 0.07, 0.035);
const plusVerticalGeometry = new THREE.BoxGeometry(0.07, 0.28, 0.035);
function makeMesh(geometry, material, parent, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// ---------------------------------------------------------------------
// Starting stack: ten blocks arranged as a 2 × 5 tower
// ---------------------------------------------------------------------
const startRoot = new THREE.Group();
startRoot.position.x = -4.15;
scene.add(startRoot);
const startPlinth = makeMesh(
new THREE.CylinderGeometry(1.34, 1.44, 0.28, 48),
materials.plinth,
startRoot,
0,
0.14,
0
);
startPlinth.receiveShadow = true;
const dropAnimations = [];
function queueDrop(mesh, targetY, delay = 0) {
const fromY = targetY + 2.1;
mesh.position.y = fromY;
mesh.scale.setScalar(0.03);
dropAnimations.push({
mesh,
fromY,
targetY,
start: performance.now() + delay,
duration: reducedMotion ? 1 : 620
});
}
for (let layer = 0; layer < 5; layer++) {
for (let column = 0; column < 2; column++) {
const y = 0.28 + 0.31 + layer * 0.655;
const x = column === 0 ? -0.34 : 0.34;
const z = column === 0 ? -0.025 : 0.025;
const block = makeMesh(
startBlockGeometry,
materials.startBlock,
startRoot,
x,
y,
z
);
queueDrop(block, y, layer * 75 + column * 25);
}
}
// ---------------------------------------------------------------------
// Three identical increment trays
// ---------------------------------------------------------------------
const trayRoots = [];
const trayContents = [];
const trayPickMeshes = [];
const trayXs = [-0.75, 1.95, 4.65];
function createTray(x, index) {
const root = new THREE.Group();
root.position.x = x;
root.userData.trayIndex = index;
scene.add(root);
makeMesh(
new THREE.CylinderGeometry(1.18, 1.25, 0.16, 48),
materials.plinth,
root,
0,
0.08,
0
);
makeMesh(
trayFloorGeometry,
materials.trayFloor,
root,
0,
0.25,
0
);
makeMesh(
traySideGeometry,
materials.tray,
root,
-0.74,
0.52,
0
);
makeMesh(
traySideGeometry,
materials.tray,
root,
0.74,
0.52,
0
);
makeMesh(
trayEndGeometry,
materials.tray,
root,
0,
0.52,
-0.68
);
makeMesh(
trayEndGeometry,
materials.tray,
root,
0,
0.52,
0.68
);
// A small physical +1 mark on the front of every tray.
makeMesh(
plusHorizontalGeometry,
materials.plus,
root,
0,
0.52,
0.767
);
makeMesh(
plusVerticalGeometry,
materials.plus,
root,
0,
0.52,
0.767
);
const content = new THREE.Group();
root.add(content);
root.traverse((object) => {
if (object.isMesh) {
object.userData.trayIndex = index;
trayPickMeshes.push(object);
}
});
trayRoots.push(root);
trayContents.push(content);
}
trayXs.forEach(createTray);
// ---------------------------------------------------------------------
// UI state
// ---------------------------------------------------------------------
const countElements = [
document.querySelector("#count-a"),
document.querySelector("#count-b"),
document.querySelector("#count-c")
];
const equationElements = [
document.querySelector("#eq-a"),
document.querySelector("#eq-b"),
document.querySelector("#eq-c")
];
const trayButtons = [...document.querySelectorAll("[data-tray]")];
const labelElements = [
document.querySelector("#label-a"),
document.querySelector("#label-b"),
document.querySelector("#label-c")
];
const totalElement = document.querySelector("#eq-total");
const toast = document.querySelector("#toast");
let toastTimer;
function showToast(message) {
clearTimeout(toastTimer);
toast.textContent = message;
toast.classList.add("visible");
toastTimer = setTimeout(() => {
toast.classList.remove("visible");
}, 1700);
}
function updateEquation() {
state.total =
BASE_COUNT + state.counts[0] + state.counts[1] + state.counts[2];
state.counts.forEach((count, index) => {
countElements[index].textContent = count;
equationElements[index].textContent = count;
labelElements[index].innerHTML = `
<span>Tray ${String.fromCharCode(65 + index)}</span>
<strong>${count} added</strong>
`;
trayButtons[index].disabled = count >= MAX_PER_TRAY;
});
totalElement.textContent = state.total;
}
function trayBlockPosition(index) {
const layer = Math.floor(index / 9);
const slot = index % 9;
const row = Math.floor(slot / 3);
const column = slot % 3;
return new THREE.Vector3(
(column - 1) * 0.43,
0.55 + layer * 0.45,
(row - 1) * 0.43
);
}
function addBlockToTray(index) {
const count = state.counts[index];
if (count >= MAX_PER_TRAY) {
showToast("This tray is full. Reset to begin again.");
return;
}
const position = trayBlockPosition(count);
const block = makeMesh(
trayBlockGeometry,
materials.trayBlock,
trayContents[index],
position.x,
position.y,
position.z
);
queueDrop(block, position.y);
state.counts[index] += 1;
updateEquation();
const trayName = String.fromCharCode(65 + index);
showToast(
`Tray ${trayName} +1 · New total: ${state.total}`
);
}
trayButtons.forEach((button) => {
button.addEventListener("click", () => {
addBlockToTray(Number(button.dataset.tray));
});
});
document.querySelector("#reset").addEventListener("click", () => {
trayContents.forEach((content) => {
while (content.children.length) {
content.remove(content.children[0]);
}
});
state.counts = [0, 0, 0];
updateEquation();
showToast("Trays reset to zero.");
});
updateEquation();
// ---------------------------------------------------------------------
// Direct camera controls: orbit, wheel zoom, and touch pinch zoom
// ---------------------------------------------------------------------
const pointers = new Map();
let dragDistance = 0;
let multiTouchGesture = false;
let lastPinchDistance = 0;
const raycaster = new THREE.Raycaster();
const pointerNDC = new THREE.Vector2();
function getTrayUnderPointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointerNDC.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointerNDC.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointerNDC, camera);
const hit = raycaster.intersectObjects(trayPickMeshes, false)[0];
return hit ? hit.object.userData.trayIndex : null;
}
function currentPinchDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
dragDistance = 0;
multiTouchGesture = false;
} else {
multiTouchGesture = true;
lastPinchDistance = currentPinchDistance();
}
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) {
const hoveredTray = getTrayUnderPointer(event);
renderer.domElement.style.cursor =
hoveredTray === null ? "grab" : "pointer";
return;
}
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1 && !multiTouchGesture) {
cameraYaw -= dx * 0.0055;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + dy * 0.0045,
0.28,
1.18
);
dragDistance += Math.hypot(dx, dy);
} else if (pointers.size >= 2) {
const distance = currentPinchDistance();
if (lastPinchDistance > 0 && distance > 0) {
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * (lastPinchDistance / distance),
8.5,
34
);
}
lastPinchDistance = distance;
multiTouchGesture = true;
dragDistance += Math.hypot(dx, dy);
}
});
function endPointer(event, cancelled = false) {
const wasMultiTouch = multiTouchGesture || pointers.size > 1;
pointers.delete(event.pointerId);
if (pointers.size === 1) {
multiTouchGesture = true;
lastPinchDistance = 0;
} else if (pointers.size === 0) {
multiTouchGesture = false;
dragDistance = 0;
}
renderer.domElement.style.cursor = "grab";
if (!cancelled && !wasMultiTouch && dragDistance < 7) {
const trayIndex = getTrayUnderPointer(event);
if (trayIndex !== null) {
addBlockToTray(trayIndex);
}
}
}
renderer.domElement.addEventListener("pointerup", (event) => {
endPointer(event);
});
renderer.domElement.addEventListener("pointercancel", (event) => {
endPointer(event, true);
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
8.5,
34
);
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
// ---------------------------------------------------------------------
// Projected HTML labels
// ---------------------------------------------------------------------
const startLabelAnchor = new THREE.Vector3(-4.15, 4.0, 0);
const trayLabelAnchors = trayXs.map(
(x) => new THREE.Vector3(x, 0.48, 1.12)
);
const startLabel = document.querySelector("#label-start");
function positionLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const onScreen =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.2 &&
Math.abs(projected.y) < 1.2;
element.style.left =
`${((projected.x + 1) / 2) * window.innerWidth}px`;
element.style.top =
`${((-projected.y + 1) / 2) * window.innerHeight}px`;
element.style.opacity = onScreen ? "1" : "0";
}
// ---------------------------------------------------------------------
// Animation loop
// ---------------------------------------------------------------------
function updateDropAnimations(now) {
for (let i = dropAnimations.length - 1; i >= 0; i--) {
const animation = dropAnimations[i];
if (!animation.mesh.parent) {
dropAnimations.splice(i, 1);
continue;
}
const rawT = (now - animation.start) / animation.duration;
if (rawT < 0) continue;
const t = Math.min(rawT, 1);
const eased = 1 - Math.pow(1 - t, 3);
animation.mesh.position.y = THREE.MathUtils.lerp(
animation.fromY,
animation.targetY,
eased
);
const c1 = 1.70158;
const c3 = c1 + 1;
const back = 1 + c3 * Math.pow(t - 1, 3) + c1 * Math.pow(t - 1, 2);
const scale = 0.03 + 0.97 * back;
animation.mesh.scale.setScalar(scale);
animation.mesh.rotation.y = (1 - eased) * -0.28;
if (t >= 1) {
animation.mesh.position.y = animation.targetY;
animation.mesh.scale.setScalar(1);
animation.mesh.rotation.y = 0;
dropAnimations.splice(i, 1);
}
}
}
function animate(now) {
requestAnimationFrame(animate);
updateCamera();
updateDropAnimations(now);
positionLabel(startLabel, startLabelAnchor);
trayLabelAnchors.forEach((anchor, index) => {
positionLabel(labelElements[index], anchor);
});
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
</script>
</body>
</html>
```