Prompt specification
Build or customize the Three.js 3D scene "Cup loan and return cycle" 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: Cup loan and return cycle
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/cup-loan-and-return-cycle-x0494
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring reusable-cup deposits and returns. Animate a visitor receiving a cup and deposit token, then returning the cup for the token's value; counters distinguish sold drinks from cups currently borrowed. Voxel style.
Working Three.js Starter (cup-loan-and-return-cycle-x0494):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Re:Use Cup Deposit & Return</title>
<style>
:root {
--ink: #173f3b;
--muted: #5d7771;
--paper: rgba(248, 251, 245, 0.9);
--line: rgba(23, 63, 59, 0.14);
--teal: #247f72;
--coral: #ed735d;
--gold: #e9b949;
--green: #4c9d72;
--shadow: 0 18px 45px rgba(28, 68, 61, 0.14);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce9e2;
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;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
z-index: 10;
pointer-events: none;
}
.panel {
pointer-events: auto;
background: var(--paper);
border: 1px solid rgba(255, 255, 255, 0.7);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#intro {
top: 22px;
left: 22px;
width: min(340px, calc(100vw - 44px));
padding: 20px;
border-radius: 20px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
color: var(--teal);
font: 800 11px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 9px;
height: 9px;
border-radius: 2px;
background: var(--coral);
box-shadow: 14px 0 0 var(--gold);
margin-right: 14px;
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 36px);
line-height: 0.98;
letter-spacing: -0.055em;
}
.subtitle {
margin: 12px 0 16px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
#status {
min-height: 42px;
display: flex;
align-items: center;
gap: 10px;
padding: 10px 12px;
border-radius: 11px;
background: rgba(36, 127, 114, 0.08);
color: var(--teal);
font: 750 12px/1.35 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
#status::before {
content: "";
flex: 0 0 auto;
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--coral);
box-shadow: 0 0 0 5px rgba(237, 115, 93, 0.12);
}
#stats {
top: 22px;
right: 22px;
display: grid;
grid-template-columns: repeat(2, minmax(142px, 1fr));
gap: 10px;
width: 314px;
}
.metric {
min-height: 112px;
padding: 15px;
border-radius: 16px;
background: var(--paper);
border: 1px solid rgba(255, 255, 255, 0.72);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
transition: background 180ms ease, transform 180ms ease;
}
.metric.bump { animation: bump 320ms ease; }
@keyframes bump {
45% { transform: translateY(-4px) scale(1.025); }
}
.metric-label {
color: var(--muted);
font: 800 10px/1.3 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.metric-value {
margin-top: 10px;
font: 850 30px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: -0.06em;
}
.metric-note {
margin-top: 7px;
color: var(--muted);
font-size: 10px;
}
#borrowedMetric.active {
background: rgba(237, 115, 93, 0.93);
color: white;
}
#borrowedMetric.active .metric-label,
#borrowedMetric.active .metric-note {
color: rgba(255, 255, 255, 0.78);
}
#timeline {
left: 50%;
bottom: 20px;
width: min(720px, calc(100vw - 32px));
transform: translateX(-50%);
padding: 14px 16px;
border-radius: 18px;
}
.timeline-row {
display: flex;
align-items: center;
gap: 10px;
}
.controls {
display: flex;
gap: 7px;
flex: 0 0 auto;
}
button {
border: 0;
border-radius: 10px;
padding: 9px 12px;
color: white;
background: var(--ink);
cursor: pointer;
font-size: 11px;
font-weight: 800;
transition: transform 140ms ease, opacity 140ms ease, background 140ms ease;
}
button:hover { transform: translateY(-2px); }
button:active { transform: translateY(0); }
button:disabled { opacity: 0.45; cursor: default; transform: none; }
#replayButton { background: var(--coral); }
#viewButton { background: rgba(23, 63, 59, 0.13); color: var(--ink); }
.progress-wrap {
position: relative;
flex: 1;
height: 8px;
overflow: hidden;
border-radius: 10px;
background: rgba(23, 63, 59, 0.1);
}
#progress {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--coral), var(--gold));
transition: width 80ms linear;
}
.timeline-caption {
display: flex;
justify-content: space-between;
gap: 16px;
margin-top: 9px;
color: var(--muted);
font: 700 10px/1.3 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
#hint {
right: 22px;
bottom: 23px;
padding: 9px 12px;
border-radius: 11px;
color: var(--muted);
background: rgba(248, 251, 245, 0.72);
border: 1px solid rgba(255, 255, 255, 0.65);
font: 700 10px/1.3 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
@media (max-width: 860px) {
#intro {
width: 270px;
padding: 15px;
}
.subtitle { display: none; }
#stats {
width: 250px;
grid-template-columns: repeat(2, 1fr);
}
.metric {
min-height: 92px;
padding: 11px;
}
.metric-value { font-size: 24px; }
.metric-note { display: none; }
#hint { display: none; }
}
@media (max-width: 620px) {
#intro {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
}
#intro h1 { font-size: 24px; }
#intro .subtitle { display: none; }
#stats {
top: 136px;
left: 10px;
right: 10px;
width: auto;
gap: 6px;
}
.metric {
min-height: 72px;
padding: 9px;
border-radius: 12px;
}
.metric-label {
font-size: 8px;
letter-spacing: 0.06em;
}
.metric-value {
margin-top: 7px;
font-size: 21px;
}
#timeline {
bottom: 9px;
width: calc(100vw - 18px);
padding: 11px;
}
.timeline-caption span:first-child { display: none; }
button { padding: 8px 10px; font-size: 10px; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel café showing a reusable cup deposit and return"></canvas>
<section id="intro" class="hud panel">
<div class="eyebrow">Circular cup club</div>
<h1>RE:USE<br>Café</h1>
<p class="subtitle">
A drink costs one reusable cup and a $2.00 deposit token.
Bring the cup back and the token’s value is redeemed.
</p>
<div id="status" aria-live="polite">01 · Visitor arrives</div>
</section>
<aside id="stats" class="hud" aria-label="Cup transaction counters">
<article id="soldMetric" class="metric">
<div class="metric-label">Drinks sold</div>
<div id="soldValue" class="metric-value">00</div>
<div class="metric-note">Cumulative drinks handed over</div>
</article>
<article id="borrowedMetric" class="metric">
<div class="metric-label">Cups currently borrowed</div>
<div id="borrowedValue" class="metric-value">00</div>
<div class="metric-note">Out with a visitor right now</div>
</article>
<article class="metric">
<div class="metric-label">Token value</div>
<div class="metric-value">$2.00</div>
<div class="metric-note">Refunded when the cup returns</div>
</article>
<article class="metric">
<div class="metric-label">Value redeemed</div>
<div id="redeemedValue" class="metric-value">$0.00</div>
<div class="metric-note">Completed cup returns</div>
</article>
</aside>
<section id="timeline" class="hud panel">
<div class="timeline-row">
<div class="controls">
<button id="pauseButton" type="button">Pause</button>
<button id="replayButton" type="button">New cycle</button>
<button id="viewButton" type="button">Reset view</button>
</div>
<div class="progress-wrap" aria-hidden="true">
<div id="progress"></div>
</div>
</div>
<div class="timeline-caption">
<span>DRINK → CUP + TOKEN → RETURN → $2.00</span>
<span id="timecode">00.0 / 12.4 SEC</span>
</div>
</section>
<div id="hint" class="hud">DRAG TO ORBIT · SCROLL TO ZOOM</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM and renderer
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const statusElement = document.querySelector("#status");
const progressElement = document.querySelector("#progress");
const timecodeElement = document.querySelector("#timecode");
const soldValue = document.querySelector("#soldValue");
const borrowedValue = document.querySelector("#borrowedValue");
const redeemedValue = document.querySelector("#redeemedValue");
const borrowedMetric = document.querySelector("#borrowedMetric");
const soldMetric = document.querySelector("#soldMetric");
const pauseButton = document.querySelector("#pauseButton");
const replayButton = document.querySelector("#replayButton");
const viewButton = document.querySelector("#viewButton");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
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.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce9e2);
scene.fog = new THREE.Fog(0xdce9e2, 19, 34);
const camera = new THREE.OrthographicCamera(-8, 8, 5, -5, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 1.1, -0.1);
let cameraAzimuth = 0.54;
let cameraPolar = 1.02;
let cameraZoom = 1;
const cameraRadius = 16;
// -------------------------------------------------------------------------
// Lights
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xf5fff4, 0x73958b, 2.15));
const sun = new THREE.DirectionalLight(0xfff3d1, 3.25);
sun.position.set(-6, 11, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.00025;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x8ac8c0, 1.15);
rimLight.position.set(7, 5, -6);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Small voxel construction helpers
// -------------------------------------------------------------------------
const materialCache = new Map();
function material(color) {
if (color && color.isMaterial) return color;
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.83,
metalness: 0.02,
flatShading: true
})
);
}
return materialCache.get(color);
}
function box(w, h, d, color, x, y, z, parent = scene, outlined = false) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(w, h, d),
material(color)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
if (outlined) {
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x244b45,
transparent: true,
opacity: 0.25
});
mesh.add(new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry),
edgeMaterial
));
}
return mesh;
}
function canvasTexture(width = 1024, height = 512) {
const image = document.createElement("canvas");
image.width = width;
image.height = height;
const texture = new THREE.CanvasTexture(image);
texture.colorSpace = THREE.SRGBColorSpace;
texture.magFilter = THREE.NearestFilter;
texture.minFilter = THREE.NearestFilter;
return {
image,
context: image.getContext("2d"),
texture
};
}
function signPanel(width, height, x, y, z, draw) {
const data = canvasTexture();
draw(data.context, data.image.width, data.image.height);
const panel = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
new THREE.MeshStandardMaterial({
map: data.texture,
roughness: 0.9
})
);
panel.position.set(x, y, z);
scene.add(panel);
return data;
}
function smooth01(t) {
t = THREE.MathUtils.clamp(t, 0, 1);
return t * t * (3 - 2 * t);
}
function lerp(a, b, t) {
return a + (b - a) * t;
}
// -------------------------------------------------------------------------
// Café architecture and tiled floor
// -------------------------------------------------------------------------
box(15.5, 0.22, 11.5, 0xb6c9bf, 0, -0.18, 0.3);
const tileGeometry = new THREE.BoxGeometry(0.96, 0.055, 0.96);
const tileMatricesA = [];
const tileMatricesB = [];
const dummy = new THREE.Object3D();
for (let x = 0; x < 15; x++) {
for (let z = 0; z < 11; z++) {
dummy.position.set(x - 7, -0.025, z - 5.05);
dummy.updateMatrix();
if ((x + z) % 2 === 0) {
tileMatricesA.push(dummy.matrix.clone());
} else {
tileMatricesB.push(dummy.matrix.clone());
}
}
}
const tileA = new THREE.InstancedMesh(
tileGeometry,
material(0xe8eee4),
tileMatricesA.length
);
const tileB = new THREE.InstancedMesh(
tileGeometry,
material(0xdce7dc),
tileMatricesB.length
);
tileMatricesA.forEach((matrix, i) => tileA.setMatrixAt(i, matrix));
tileMatricesB.forEach((matrix, i) => tileB.setMatrixAt(i, matrix));
tileA.receiveShadow = true;
tileB.receiveShadow = true;
scene.add(tileA, tileB);
box(13.8, 5.25, 0.25, 0xf3eedc, 0, 2.58, -4.38);
box(0.25, 4.65, 8.15, 0xe8e1cc, -6.95, 2.28, -0.32);
// Window and frame.
box(3.15, 1.85, 0.09, 0x78a6a2, -4.15, 3.45, -4.21);
box(0.1, 1.85, 0.14, 0xf8f1dc, -4.15, 3.45, -4.13);
box(3.15, 0.1, 0.14, 0xf8f1dc, -4.15, 3.45, -4.13);
box(3.35, 0.12, 0.22, 0xd9c69f, -4.15, 2.49, -4.08);
// Shelf with miniature cups.
box(3.25, 0.16, 0.55, 0xb6855e, -4.15, 2.22, -3.92);
for (let i = 0; i < 7; i++) {
const x = -5.35 + i * 0.4;
box(0.22, 0.34, 0.22, i % 2 ? 0x2d8e80 : 0xef7a64, x, 2.47, -3.88);
box(0.25, 0.08, 0.25, 0x264f49, x, 2.68, -3.88);
}
// Main wall sign.
signPanel(5.7, 1.65, -0.9, 3.85, -4.225, (ctx, w, h) => {
ctx.fillStyle = "#173f3b";
ctx.fillRect(0, 0, w, h);
ctx.strokeStyle = "#e9b949";
ctx.lineWidth = 18;
ctx.strokeRect(24, 24, w - 48, h - 48);
ctx.fillStyle = "#f7f2dd";
ctx.textAlign = "center";
ctx.font = "900 126px monospace";
ctx.fillText("RE:USE", w / 2, 205);
ctx.fillStyle = "#e9b949";
ctx.font = "700 44px monospace";
ctx.fillText("CUPS ARE CURRENCY", w / 2, 310);
ctx.fillStyle = "#ef7962";
ctx.fillRect(78, 360, 62, 62);
ctx.fillStyle = "#173f3b";
ctx.font = "900 43px monospace";
ctx.fillText("$2", 109, 406);
});
// Live metrics board.
const boardData = canvasTexture(1024, 512);
const board = new THREE.Mesh(
new THREE.PlaneGeometry(3.25, 1.62),
new THREE.MeshStandardMaterial({
map: boardData.texture,
roughness: 0.85
})
);
board.position.set(3.45, 3.35, -4.225);
scene.add(board);
// -------------------------------------------------------------------------
// Counter, register and deposit signs
// -------------------------------------------------------------------------
box(7.45, 1.47, 1.48, 0x3e8176, 0, 0.74, -0.05);
box(7.78, 0.18, 1.76, 0xf1e8cd, 0, 1.56, -0.05);
box(7.12, 0.13, 0.06, 0x245c55, 0, 0.38, 0.71);
for (let i = -3; i <= 3; i++) {
box(0.025, 1.12, 0.035, 0x70aa9d, i * 0.98, 0.75, 0.715);
}
signPanel(3.05, 0.76, -0.25, 0.88, 0.724, (ctx, w, h) => {
ctx.fillStyle = "#f3eedc";
ctx.fillRect(0, 0, w, h);
ctx.fillStyle = "#173f3b";
ctx.textAlign = "center";
ctx.font = "900 76px monospace";
ctx.fillText("RE:USE CAFÉ", w / 2, 126);
ctx.fillStyle = "#ed735d";
ctx.font = "700 38px monospace";
ctx.fillText("CUP DEPOSIT • $2.00", w / 2, 213);
});
// Handover pads.
box(0.62, 0.018, 0.62, 0xefd27d, 0.12, 1.658, -0.1);
box(0.62, 0.018, 0.62, 0xefd27d, -0.82, 1.658, -0.1);
// Register.
box(0.95, 0.22, 0.72, 0x274b46, 2.75, 1.76, -0.12);
box(0.68, 0.46, 0.12, 0x365f58, 2.75, 1.98, -0.37);
const registerScreen = new THREE.Mesh(
new THREE.PlaneGeometry(0.48, 0.27),
new THREE.MeshBasicMaterial({ color: 0x84d4aa })
);
registerScreen.position.set(2.75, 1.99, -0.302);
scene.add(registerScreen);
// Cup return / washing station.
box(1.25, 0.18, 0.95, 0x79948a, 4.2, 1.74, -0.18);
box(0.95, 0.6, 0.68, 0x8eb2a7, 4.2, 1.38, -0.18);
signPanel(1.35, 0.42, 4.2, 1.35, 0.173, (ctx, w, h) => {
ctx.fillStyle = "#173f3b";
ctx.fillRect(0, 0, w, h);
ctx.fillStyle = "#f5efd9";
ctx.textAlign = "center";
ctx.font = "900 46px monospace";
ctx.fillText("CUP RETURN", w / 2, 67);
});
// Waiting bench.
box(2.15, 0.2, 0.66, 0xb77c58, 4.1, 0.64, 2.72);
box(2.15, 0.78, 0.18, 0xc78b63, 4.1, 1.05, 3.02);
box(0.18, 0.64, 0.58, 0x6c5548, 3.22, 0.32, 2.72);
box(0.18, 0.64, 0.58, 0x6c5548, 4.98, 0.32, 2.72);
// Voxel plant.
box(0.72, 0.68, 0.72, 0xd98663, -5.55, 0.34, -2.55);
box(0.15, 1.12, 0.15, 0x426e52, -5.55, 1.17, -2.55);
box(0.78, 0.48, 0.52, 0x4f8d63, -5.35, 1.45, -2.55);
box(0.55, 0.62, 0.48, 0x66a96f, -5.9, 1.7, -2.52);
box(0.66, 0.44, 0.58, 0x3f7b58, -5.26, 1.97, -2.55);
// Pendant lights.
for (const x of [-2.7, 1.0, 4.6]) {
box(0.05, 1.05, 0.05, 0x385c55, x, 4.55, -0.25);
box(0.78, 0.25, 0.78, 0x304f49, x, 4.0, -0.25);
box(0.58, 0.08, 0.58, 0xffd884, x, 3.84, -0.25);
}
// -------------------------------------------------------------------------
// Voxel characters
// -------------------------------------------------------------------------
function createPerson({
shirt,
pants,
hair,
skin = 0xdca77f,
facing = -1,
apron = false,
backpack = false
}) {
const root = new THREE.Group();
scene.add(root);
box(0.76, 0.78, 0.46, shirt, 0, 1.14, 0, root, true);
box(0.62, 0.18, 0.42, pants, 0, 0.76, 0, root);
box(0.19, 0.18, 0.19, skin, 0, 1.61, 0, root);
box(0.66, 0.65, 0.62, skin, 0, 1.98, 0, root, true);
box(0.69, 0.18, 0.65, hair, 0, 2.31, 0, root);
box(0.12, 0.3, 0.62, hair, -0.28, 2.13, 0, root);
const eyeZ = facing * 0.326;
box(0.09, 0.1, 0.04, 0x173f3b, -0.14, 2.04, eyeZ, root);
box(0.09, 0.1, 0.04, 0x173f3b, 0.14, 2.04, eyeZ, root);
box(0.18, 0.045, 0.04, 0x9e5b4d, 0, 1.87, eyeZ, root);
const legs = [];
for (const x of [-0.2, 0.2]) {
const leg = new THREE.Group();
leg.position.set(x, 0.78, 0);
root.add(leg);
box(0.27, 0.68, 0.31, pants, 0, -0.34, 0, leg);
box(0.3, 0.16, 0.43, 0x263f3b, 0, -0.71, 0.05, leg);
legs.push(leg);
}
function makeArm(x, shirtColor) {
const arm = new THREE.Group();
arm.position.set(x, 1.53, 0);
root.add(arm);
box(0.28, 0.68, 0.31, shirtColor, 0, -0.33, 0, arm, true);
box(0.27, 0.22, 0.29, skin, 0, -0.72, 0, arm);
const hand = new THREE.Object3D();
hand.position.set(0, -0.83, 0);
arm.add(hand);
return { arm, hand };
}
const left = makeArm(-0.52, shirt);
const right = makeArm(0.52, shirt);
if (apron) {
box(0.64, 0.76, 0.055, 0xf3e7c8, 0, 1.08, 0.255, root);
box(0.38, 0.23, 0.06, 0x2b786d, 0, 1.31, 0.288, root);
}
if (backpack) {
box(0.62, 0.72, 0.28, 0x5f7d75, 0, 1.19, 0.34, root, true);
box(0.43, 0.22, 0.29, 0xe6b84f, 0, 1.28, 0.49, root);
}
return {
root,
legs,
leftArm: left.arm,
rightArm: right.arm,
leftHand: left.hand,
rightHand: right.hand,
facing
};
}
const visitor = createPerson({
shirt: 0xed735d,
pants: 0x315b68,
hair: 0x49382f,
facing: -1,
backpack: true
});
const cashier = createPerson({
shirt: 0x2f8a7d,
pants: 0x244b47,
hair: 0x342d29,
facing: 1,
apron: true
});
cashier.root.position.set(-0.35, 0.15, -0.95);
// -------------------------------------------------------------------------
// Cup, deposit token and redeemed value
// -------------------------------------------------------------------------
function makeCup() {
const group = new THREE.Group();
box(0.45, 0.34, 0.45, 0x2c9687, 0, 0.17, 0, group, true);
box(0.52, 0.2, 0.52, 0x3ca596, 0, 0.43, 0, group, true);
box(0.58, 0.1, 0.58, 0x267e74, 0, 0.57, 0, group);
box(0.64, 0.08, 0.64, 0x244c47, 0, 0.66, 0, group, true);
box(0.19, 0.07, 0.13, 0x244c47, 0, 0.64, -0.36, group);
box(0.4, 0.07, 0.015, 0xf3ead2, 0, 0.31, 0.283, group);
const logoData = canvasTexture(256, 128);
logoData.context.fillStyle = "#f3ead2";
logoData.context.fillRect(0, 0, 256, 128);
logoData.context.fillStyle = "#173f3b";
logoData.context.textAlign = "center";
logoData.context.font = "900 73px monospace";
logoData.context.fillText("R", 128, 91);
const logo = new THREE.Mesh(
new THREE.PlaneGeometry(0.22, 0.11),
new THREE.MeshStandardMaterial({ map: logoData.texture })
);
logo.position.set(0, 0.31, 0.296);
group.add(logo);
scene.add(group);
return group;
}
function makeToken() {
const group = new THREE.Group();
box(0.45, 0.13, 0.45, 0xe9b949, 0, 0, 0, group, true);
box(0.31, 0.015, 0.31, 0xf4d978, 0, 0.071, 0, group);
const data = canvasTexture(256, 256);
data.context.clearRect(0, 0, 256, 256);
data.context.fillStyle = "#173f3b";
data.context.textAlign = "center";
data.context.font = "900 104px monospace";
data.context.fillText("$2", 128, 150);
const label = new THREE.Mesh(
new THREE.PlaneGeometry(0.3, 0.3),
new THREE.MeshBasicMaterial({
map: data.texture,
transparent: true,
depthWrite: false
})
);
label.rotation.x = -Math.PI / 2;
label.position.y = 0.086;
group.add(label);
scene.add(group);
return group;
}
function makeCash() {
const group = new THREE.Group();
box(0.66, 0.08, 0.39, 0x65a978, 0, 0, 0, group, true);
box(0.11, 0.09, 0.4, 0xe9dfc5, 0, 0.006, 0, group);
const data = canvasTexture(256, 128);
data.context.fillStyle = "#65a978";
data.context.fillRect(0, 0, 256, 128);
data.context.fillStyle = "#173f3b";
data.context.textAlign = "center";
data.context.font = "900 73px monospace";
data.context.fillText("$2", 128, 91);
const label = new THREE.Mesh(
new THREE.PlaneGeometry(0.4, 0.2),
new THREE.MeshBasicMaterial({ map: data.texture })
);
label.rotation.x = -Math.PI / 2;
label.position.y = 0.047;
group.add(label);
group.visible = false;
scene.add(group);
return group;
}
const cup = makeCup();
const token = makeToken();
const cash = makeCash();
const cupStart = new THREE.Vector3(0.12, 1.69, -0.1);
const tokenStart = new THREE.Vector3(-0.82, 1.69, -0.1);
const cupReturn = new THREE.Vector3(0.12, 1.69, -0.1);
const tokenReturn = new THREE.Vector3(-0.82, 1.69, -0.1);
const cashStart = new THREE.Vector3(-0.82, 1.72, -0.1);
let cupReturnStart = null;
let tokenReturnStart = null;
// -------------------------------------------------------------------------
// Voxel particles
// -------------------------------------------------------------------------
const particles = [];
function burst(position, color, count = 12) {
for (let i = 0; i < count; i++) {
const size = 0.055 + Math.random() * 0.055;
const particle = box(
size,
size,
size,
color,
position.x,
position.y,
position.z
);
particle.userData.velocity = new THREE.Vector3(
(Math.random() - 0.5) * 2.4,
1.1 + Math.random() * 2.1,
(Math.random() - 0.5) * 2.0
);
particle.userData.life = 0.7 + Math.random() * 0.5;
particle.userData.maxLife = particle.userData.life;
particles.push(particle);
}
}
function updateParticles(dt) {
for (let i = particles.length - 1; i >= 0; i--) {
const particle = particles[i];
particle.userData.life -= dt;
particle.userData.velocity.y -= 4.3 * dt;
particle.position.addScaledVector(particle.userData.velocity, dt);
particle.rotation.x += dt * 5;
particle.rotation.z += dt * 4;
const scale = Math.max(0, particle.userData.life / particle.userData.maxLife);
particle.scale.setScalar(scale);
if (particle.userData.life <= 0) {
scene.remove(particle);
particle.geometry.dispose();
particles.splice(i, 1);
}
}
}
// -------------------------------------------------------------------------
// Counters and in-world board
// -------------------------------------------------------------------------
let drinksSold = 0;
let cupsBorrowed = 0;
let valueRedeemed = 0;
function drawWorldBoard() {
const ctx = boardData.context;
const w = boardData.image.width;
const h = boardData.image.height;
ctx.fillStyle = "#173f3b";
ctx.fillRect(0, 0, w, h);
ctx.strokeStyle = "#73b3a2";
ctx.lineWidth = 12;
ctx.strokeRect(18, 18, w - 36, h - 36);
ctx.fillStyle = "#a8cfc2";
ctx.font = "800 43px monospace";
ctx.fillText("LIVE CUP CLUB", 46, 74);
ctx.fillStyle = "#f4edda";
ctx.font = "900 65px monospace";
ctx.fillText(`SOLD ${String(drinksSold).padStart(2, "0")}`, 46, 176);
ctx.fillStyle = cupsBorrowed > 0 ? "#ef7962" : "#e9b949";
ctx.fillText(`ON LOAN ${String(cupsBorrowed).padStart(2, "0")}`, 46, 270);
ctx.fillStyle = "#a8cfc2";
ctx.font = "800 43px monospace";
ctx.fillText("REDEEMED", 46, 350);
ctx.fillStyle = "#f4edda";
ctx.font = "900 65px monospace";
ctx.fillText(
`$${valueRedeemed.toFixed(2)}`,
610,
350
);
boardData.texture.needsUpdate = true;
}
function updateCounters(bumpSold = false, bumpReturn = false) {
soldValue.textContent = String(drinksSold).padStart(2, "0");
borrowedValue.textContent = String(cupsBorrowed).padStart(2, "0");
redeemedValue.textContent = `$${valueRedeemed.toFixed(2)}`;
borrowedMetric.classList.toggle("active", cupsBorrowed > 0);
if (bumpSold) {
soldMetric.classList.remove("bump");
void soldMetric.offsetWidth;
soldMetric.classList.add("bump");
}
if (bumpReturn) {
borrowedMetric.classList.remove("bump");
void borrowedMetric.offsetWidth;
borrowedMetric.classList.add("bump");
}
drawWorldBoard();
}
// -------------------------------------------------------------------------
// Timeline state
// -------------------------------------------------------------------------
const DURATION = 12.4;
let timeline = 0;
let playing = true;
let completed = false;
let phaseKey = "";
let soldMilestone = false;
let returnedMilestone = false;
function setPhase(key, text) {
if (key === phaseKey) return;
phaseKey = key;
statusElement.textContent = text;
}
function resetCycle() {
timeline = 0;
playing = true;
completed = false;
phaseKey = "";
soldMilestone = false;
returnedMilestone = false;
cupReturnStart = null;
tokenReturnStart = null;
cupsBorrowed = 0;
scene.attach(cup);
scene.attach(token);
scene.attach(cash);
cup.position.copy(cupStart);
cup.rotation.set(0, 0, 0);
cup.visible = true;
token.position.copy(tokenStart);
token.rotation.set(0, 0.18, 0);
token.visible = true;
cash.visible = false;
cash.position.copy(cashStart);
cash.rotation.set(0, 0, 0);
visitor.root.position.set(-5.3, 0, 1.55);
visitor.leftArm.rotation.set(0, 0, 0);
visitor.rightArm.rotation.set(0, 0, 0);
visitor.legs.forEach(leg => leg.rotation.set(0, 0, 0));
cashier.root.position.set(-0.35, 0.15, -0.95);
cashier.leftArm.rotation.set(0, 0, 0);
cashier.rightArm.rotation.set(0, 0, 0);
cashier.legs.forEach(leg => leg.rotation.set(0, 0, 0));
for (const particle of particles) {
scene.remove(particle);
particle.geometry.dispose();
}
particles.length = 0;
pauseButton.textContent = "Pause";
pauseButton.disabled = false;
updateCounters();
updateCycle(0, 0);
}
function updateVisitor(t) {
let x = -0.35;
let z = 1.55;
let walking = 0;
if (t < 1.5) {
const p = smooth01(t / 1.5);
x = lerp(-5.3, -0.35, p);
walking = t < 1.35 ? 1 : 0;
} else if (t < 3.0) {
x = -0.35;
} else if (t < 4.8) {
const p = smooth01((t - 3.0) / 1.8);
x = lerp(-0.35, 4.0, p);
z = 1.55 + Math.sin(p * Math.PI) * 0.22;
walking = 1;
} else if (t < 6.0) {
x = 4.0;
z = 1.75;
} else if (t < 7.8) {
const p = smooth01((t - 6.0) / 1.8);
x = lerp(4.0, -0.35, p);
z = lerp(1.75, 1.55, p);
walking = 1;
} else if (t < 10.5) {
x = -0.35;
z = 1.55;
} else {
const p = smooth01((t - 10.5) / 1.9);
x = lerp(-0.35, 6.8, p);
walking = 1;
}
visitor.root.position.set(x, 0, z);
const stride = Math.sin(t * 11.5) * 0.52 * walking;
visitor.legs[0].rotation.x = stride;
visitor.legs[1].rotation.x = -stride;
let rightAmount = 0;
let leftAmount = 0;
if (t >= 1.5 && t < 3.0) {
const p = smooth01((t - 1.5) / 0.5);
rightAmount = p;
leftAmount = p;
} else if (t >= 3.0 && t < 3.5) {
const p = 1 - smooth01((t - 3.0) / 0.5);
rightAmount = p;
leftAmount = p;
} else if (t >= 7.6 && t < 8.3) {
const p = smooth01((t - 7.6) / 0.7);
rightAmount = p;
leftAmount = p;
} else if (t >= 8.3 && t < 9.5) {
rightAmount = 1;
leftAmount = 1;
} else if (t >= 9.5 && t < 10.5) {
rightAmount = 0.18;
leftAmount = 1;
} else if (t >= 10.5 && t < 11.0) {
leftAmount = 1 - smooth01((t - 10.5) / 0.5);
}
visitor.rightArm.rotation.x = rightAmount * 1.46;
visitor.leftArm.rotation.x = leftAmount * 1.46;
let cashierRight = 0;
let cashierLeft = 0;
if (t >= 1.5 && t < 3.0) {
const p = smooth01((t - 1.5) / 0.5);
cashierRight = p;
cashierLeft = p;
} else if (t >= 3.0 && t < 3.5) {
const p = 1 - smooth01((t - 3.0) / 0.5);
cashierRight = p;
cashierLeft = p;
} else if (t >= 7.8 && t < 8.3) {
const p = smooth01((t - 7.8) / 0.5);
cashierRight = p;
cashierLeft = p;
} else if (t >= 8.3 && t < 9.5) {
cashierRight = 1;
cashierLeft = 1;
} else if (t >= 9.5 && t < 10.2) {
cashierRight = 0.2;
cashierLeft = 0.8;
} else if (t >= 10.2 && t < 10.7) {
const p = 1 - smooth01((t - 10.2) / 0.5);
cashierRight = 0.2 * p;
cashierLeft = 0.8 * p;
}
cashier.rightArm.rotation.x = -cashierRight * 1.46;
cashier.leftArm.rotation.x = -cashierLeft * 1.46;
visitor.root.position.y = Math.abs(Math.sin(t * 11.5)) * 0.035 * walking;
}
const worldHandPosition = new THREE.Vector3();
function updateObjects(t) {
scene.updateMatrixWorld(true);
// Cup pickup.
if (t < 2.0) {
cup.position.copy(cupStart);
} else if (t < 3.0) {
const p = smooth01((t - 2.0) / 1.0);
visitor.rightHand.getWorldPosition(worldHandPosition);
cup.position.lerpVectors(cupStart, worldHandPosition, p);
cup.position.y += Math.sin(p * Math.PI) * 0.33;
cup.rotation.y = p * 0.15;
} else if (t < 8.3) {
if (cup.parent !== visitor.rightHand) {
visitor.rightHand.add(cup);
cup.position.set(0, 0, 0);
cup.rotation.set(0, 0, 0);
}
cup.rotation.x = -visitor.rightArm.rotation.x;
cup.rotation.z = 0;
} else if (t < 9.5) {
if (!cupReturnStart) {
scene.attach(cup);
cupReturnStart = cup.position.clone();
}
const p = smooth01((t - 8.3) / 1.2);
cup.position.lerpVectors(cupReturnStart, cupReturn, p);
cup.position.y += Math.sin(p * Math.PI) * 0.33;
cup.rotation.x = 0;
cup.rotation.y = 0.15 * (1 - p);
} else {
cup.position.copy(cupReturn);
cup.rotation.set(0, 0.15, 0);
cup.visible = true;
}
// Deposit token pickup and return.
if (t < 2.0) {
token.position.copy(tokenStart);
} else if (t < 3.0) {
const p = smooth01((t - 2.0) / 1.0);
visitor.leftHand.getWorldPosition(worldHandPosition);
token.position.lerpVectors(tokenStart, worldHandPosition, p);
token.position.y += Math.sin(p * Math.PI) * 0.28;
token.rotation.y = 0.18 + p * 0.4;
} else if (t < 8.3) {
if (token.parent !== visitor.leftHand) {
visitor.leftHand.add(token);
token.position.set(0, 0, 0);
token.rotation.set(0, 0.18, 0);
}
token.rotation.x = -visitor.leftArm.rotation.x;
token.rotation.z = 0;
} else if (t < 9.5) {
if (!tokenReturnStart) {
scene.attach(token);
tokenReturnStart = token.position.clone();
}
const p = smooth01((t - 8.3) / 1.2);
token.position.lerpVectors(tokenReturnStart, tokenReturn, p);
token.position.y += Math.sin(p * Math.PI) * 0.28;
token.rotation.x = 0;
token.rotation.y = 0.5 * (1 - p);
} else {
token.position.copy(tokenReturn);
token.visible = false;
}
// The cashier returns the token's monetary value.
if (t < 9.5) {
cash.visible = false;
} else if (t < 10.5) {
cash.visible = true;
if (cash.parent !== scene) {
scene.attach(cash);
}
const p = smooth01((t - 9.5) / 1.0);
visitor.leftHand.getWorldPosition(worldHandPosition);
cash.position.lerpVectors(cashStart, worldHandPosition, p);
cash.position.y += Math.sin(p * Math.PI) * 0.3;
cash.rotation.y = p * 0.4;
} else {
if (cash.parent !== visitor.leftHand) {
visitor.leftHand.add(cash);
cash.position.set(0, 0, 0);
cash.rotation.set(0, 0, 0);
}
cash.rotation.x = -visitor.leftArm.rotation.x;
cash.visible = true;
}
}
function updateCycle(t, dt) {
updateVisitor(t);
updateObjects(t);
if (t >= 3.0 && !soldMilestone) {
soldMilestone = true;
drinksSold++;
cupsBorrowed++;
updateCounters(true, false);
visitor.rightHand.getWorldPosition(worldHandPosition);
worldHandPosition.y += 0.4;
burst(worldHandPosition, 0xe9b949, 13);
}
if (t >= 9.5 && !returnedMilestone) {
returnedMilestone = true;
cupsBorrowed = Math.max(0, cupsBorrowed - 1);
valueRedeemed += 2;
updateCounters(false, true);
burst(cashStart.clone().add(new THREE.Vector3(0, 0.3, 0)), 0x65a978, 16);
}
if (t < 1.5) {
setPhase("arrival", "01 · Visitor arrives");
} else if (t < 3.0) {
setPhase("handover", "02 · Cup + $2.00 deposit token");
} else if (t < 4.8) {
setPhase("loan", "03 · Cup is currently borrowed");
} else if (t < 6.0) {
setPhase("wait", "04 · Visitor takes a short stroll");
} else if (t < 7.8) {
setPhase("return", "05 · Visitor returns with the cup");
} else if (t < 9.5) {
setPhase("counter-return", "06 · Cup and token are returned");
} else if (t < 10.5) {
setPhase("redeem", "07 · Token value redeemed: $2.00");
} else if (t < DURATION) {
setPhase("leave", "08 · Complete — borrowed cup count is zero");
} else {
setPhase("complete", "COMPLETE · $2.00 redeemed · cup returned");
}
progressElement.style.width = `${(t / DURATION) * 100}%`;
timecodeElement.textContent = `${t.toFixed(1).padStart(4, "0")} / ${DURATION.toFixed(1)} SEC`;
if (t >= DURATION && !completed) {
completed = true;
playing = false;
pauseButton.textContent = "Paused";
pauseButton.disabled = true;
}
if (playing) updateParticles(dt);
}
// -------------------------------------------------------------------------
// Direct camera controls: drag to orbit, wheel/pinch to zoom
// -------------------------------------------------------------------------
let dragging = false;
let previousX = 0;
let previousY = 0;
const pointers = new Map();
let pinchDistance = 0;
canvas.addEventListener("pointerdown", event => {
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
} else if (pointers.size === 2) {
const points = [...pointers.values()];
pinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
});
canvas.addEventListener("pointermove", event => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 2) {
const points = [...pointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (pinchDistance > 0) {
cameraZoom = THREE.MathUtils.clamp(
cameraZoom * (distance / pinchDistance),
0.68,
1.75
);
}
pinchDistance = distance;
return;
}
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
cameraAzimuth -= dx * 0.006;
cameraPolar = THREE.MathUtils.clamp(
cameraPolar - dy * 0.005,
0.58,
1.32
);
previousX = event.clientX;
previousY = event.clientY;
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) pinchDistance = 0;
if (pointers.size === 0) dragging = false;
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("pointerleave", endPointer);
canvas.addEventListener("wheel", event => {
event.preventDefault();
cameraZoom = THREE.MathUtils.clamp(
cameraZoom * Math.exp(-event.deltaY * 0.001),
0.68,
1.75
);
}, { passive: false });
function resetView() {
cameraAzimuth = 0.54;
cameraPolar = 1.02;
cameraZoom = 1;
}
function updateCamera() {
const sinPolar = Math.sin(cameraPolar);
camera.position.set(
cameraTarget.x + cameraRadius * sinPolar * Math.sin(cameraAzimuth),
cameraTarget.y + cameraRadius * Math.cos(cameraPolar),
cameraTarget.z + cameraRadius * sinPolar * Math.cos(cameraAzimuth)
);
camera.lookAt(cameraTarget);
camera.zoom = cameraZoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
// Keep the full café visible on narrow displays.
const viewHeight = Math.max(7.4, 15.5 / aspect);
camera.left = -viewHeight * aspect / 2;
camera.right = viewHeight * aspect / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
resize();
// -------------------------------------------------------------------------
// UI controls
// -------------------------------------------------------------------------
pauseButton.addEventListener("click", () => {
if (completed) return;
playing = !playing;
pauseButton.textContent = playing ? "Pause" : "Resume";
});
replayButton.addEventListener("click", resetCycle);
viewButton.addEventListener("click", resetView);
window.addEventListener("keydown", event => {
if (event.code === "Space") {
event.preventDefault();
pauseButton.click();
}
if (event.key.toLowerCase() === "r") {
resetCycle();
}
});
// -------------------------------------------------------------------------
// Start
// -------------------------------------------------------------------------
drawWorldBoard();
resetCycle();
updateCamera();
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const dt = Math.min(clock.getDelta(), 0.05);
if (playing && !completed) {
timeline = Math.min(DURATION, timeline + dt);
}
updateCycle(timeline, dt);
updateCamera();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```