Prompt specification
Build or customize the Three.js 3D scene "Reusable cup deposit token pair" 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: Reusable cup deposit token pair
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/reusable-cup-deposit-token-pair-x0493
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows reusable-cup deposits and returns. Show a sturdy cup beside a matching deposit token and return-slot outline; use a shared symbol to make their association clear without relying on color. Stylized solid forms.
Working Three.js Starter (reusable-cup-deposit-token-pair-x0493):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Cup Deposit & Return</title>
<style>
:root {
--ink: #252a27;
--muted: #70756f;
--paper: #eeeae1;
--panel: rgba(249, 247, 240, 0.9);
--line: rgba(37, 42, 39, 0.18);
}
* {
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;
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.topbar {
position: fixed;
z-index: 10;
top: 0;
left: 0;
right: 0;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 24px;
padding: 26px 30px;
pointer-events: none;
}
.brand {
max-width: 430px;
}
.eyebrow {
margin: 0 0 8px;
color: var(--muted);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(30px, 4vw, 54px);
font-weight: 750;
letter-spacing: -0.055em;
line-height: 0.98;
}
.subtitle {
margin: 12px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.45;
}
.view-controls {
display: flex;
padding: 4px;
border: 1px solid var(--line);
border-radius: 999px;
background: var(--panel);
box-shadow: 0 8px 30px rgba(35, 39, 36, 0.07);
backdrop-filter: blur(10px);
pointer-events: auto;
}
.view-controls button {
border: 0;
border-radius: 999px;
padding: 9px 14px;
background: transparent;
color: var(--muted);
cursor: pointer;
font-size: 12px;
font-weight: 700;
transition:
color 160ms ease,
background 160ms ease;
}
.view-controls button:hover,
.view-controls button[aria-pressed="true"] {
color: var(--ink);
background: rgba(37, 42, 39, 0.08);
}
.scene-label {
position: fixed;
z-index: 5;
min-width: 116px;
padding: 9px 11px 8px;
border: 1px solid var(--line);
border-radius: 10px;
background: var(--panel);
box-shadow: 0 8px 24px rgba(35, 39, 36, 0.07);
text-align: center;
pointer-events: none;
transform: translate(-50%, -100%);
backdrop-filter: blur(8px);
transition: opacity 120ms linear;
}
.scene-label::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 17px;
background: rgba(37, 42, 39, 0.42);
}
.scene-label strong {
display: block;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.scene-label small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
}
.bottom-bar {
position: fixed;
z-index: 10;
left: 30px;
right: 30px;
bottom: 24px;
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 24px;
pointer-events: none;
}
.legend {
max-width: 420px;
padding: 13px 15px;
border: 1px solid var(--line);
border-radius: 12px;
background: var(--panel);
box-shadow: 0 8px 30px rgba(35, 39, 36, 0.06);
backdrop-filter: blur(10px);
}
.legend strong {
display: block;
margin-bottom: 4px;
font-size: 11px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.legend p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.4;
}
.actions {
display: flex;
gap: 8px;
pointer-events: auto;
}
.actions button {
min-height: 43px;
padding: 0 17px;
border: 1px solid var(--ink);
border-radius: 999px;
background: var(--ink);
color: #f8f6ef;
cursor: pointer;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.02em;
transition:
transform 150ms ease,
opacity 150ms ease,
background 150ms ease;
}
.actions button.secondary {
background: var(--panel);
color: var(--ink);
border-color: var(--line);
}
.actions button:hover {
transform: translateY(-2px);
}
.actions button:disabled {
cursor: wait;
opacity: 0.55;
transform: none;
}
.help {
position: fixed;
z-index: 8;
top: 28px;
left: 50%;
padding: 8px 11px;
border-radius: 999px;
background: rgba(249, 247, 240, 0.68);
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
pointer-events: none;
transform: translateX(-50%);
}
@media (max-width: 760px) {
.topbar {
padding: 19px 18px;
}
.subtitle,
.help {
display: none;
}
.view-controls {
position: fixed;
right: 12px;
bottom: 75px;
}
.view-controls button {
padding: 8px 10px;
font-size: 10px;
}
.bottom-bar {
left: 12px;
right: 12px;
bottom: 12px;
}
.legend {
display: none;
}
.actions {
width: 100%;
}
.actions button {
flex: 1;
padding: 0 10px;
font-size: 11px;
}
.scene-label {
min-width: 90px;
padding: 7px 8px;
}
.scene-label small {
display: none;
}
}
</style>
</head>
<body>
<canvas
id="scene"
tabindex="0"
aria-label="Interactive three-dimensional reusable cup deposit and return system"
></canvas>
<header class="topbar">
<div class="brand">
<p class="eyebrow">Reuse system / 01</p>
<h1>Cup deposit<br />& return</h1>
<p class="subtitle">
A sturdy reusable cup, its matching token, and a clearly marked return slot.
</p>
</div>
<nav class="view-controls" aria-label="Camera views">
<button type="button" data-view="overview" aria-pressed="true">Overview</button>
<button type="button" data-view="deposit" aria-pressed="false">Deposit</button>
<button type="button" data-view="return" aria-pressed="false">Return</button>
</nav>
</header>
<div class="help">DRAG TO ORBIT · SCROLL TO ZOOM</div>
<div id="label-cup" class="scene-label">
<strong>Reusable cup</strong>
<small>Thick rim and solid handle</small>
</div>
<div id="label-token" class="scene-label">
<strong>Deposit token</strong>
<small>Matches the return slot</small>
</div>
<div id="label-return" class="scene-label">
<strong>Return slot</strong>
<small>Same symbol on every part</small>
</div>
<footer class="bottom-bar">
<div class="legend">
<strong>Shared return mark</strong>
<p>
The loop-arrow symbol appears on the cup, token, and station, so the
connection is made by shape—not color.
</p>
</div>
<div class="actions">
<button id="deposit-action" class="secondary" type="button">
Preview deposit
</button>
<button id="return-action" type="button">Preview return</button>
</div>
</footer>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const reduceMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
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.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e5dc);
scene.fog = new THREE.Fog(0xe9e5dc, 18, 34);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
80
);
const clock = new THREE.Clock();
const materials = {
floor: new THREE.MeshStandardMaterial({
color: 0xded8cc,
roughness: 0.95
}),
platform: new THREE.MeshStandardMaterial({
color: 0xf1ede3,
roughness: 0.82
}),
platformEdge: new THREE.MeshStandardMaterial({
color: 0xb8b1a4,
roughness: 0.88
}),
cup: new THREE.MeshStandardMaterial({
color: 0xc9c1b2,
roughness: 0.68,
metalness: 0.02
}),
cupBadge: new THREE.MeshStandardMaterial({
color: 0xddd6c8,
roughness: 0.7
}),
dark: new THREE.MeshStandardMaterial({
color: 0x252a27,
roughness: 0.62
}),
softDark: new THREE.MeshStandardMaterial({
color: 0x73776f,
roughness: 0.8
}),
station: new THREE.MeshStandardMaterial({
color: 0x3b4540,
roughness: 0.72
}),
stationPanel: new THREE.MeshStandardMaterial({
color: 0x59635d,
roughness: 0.76
}),
light: new THREE.MeshStandardMaterial({
color: 0xf3efe4,
roughness: 0.72
}),
token: new THREE.MeshStandardMaterial({
color: 0xd8cfbb,
roughness: 0.58,
metalness: 0.03
}),
tokenInset: new THREE.MeshStandardMaterial({
color: 0xc2b8a4,
roughness: 0.7
}),
arrow: new THREE.MeshStandardMaterial({
color: 0x9f9b90,
roughness: 0.9
})
};
function roundedRectangleShape(width, height, radius) {
const x = -width / 2;
const y = -height / 2;
const r = Math.min(radius, width / 2, height / 2);
const shape = new THREE.Shape();
shape.moveTo(x + r, y);
shape.lineTo(x + width - r, y);
shape.quadraticCurveTo(x + width, y, x + width, y + r);
shape.lineTo(x + width, y + height - r);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
shape.lineTo(x + r, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - r);
shape.lineTo(x, y + r);
shape.quadraticCurveTo(x, y, x + r, y);
shape.closePath();
return shape;
}
function roundedBox(width, height, depth, radius, material) {
const geometry = new THREE.ExtrudeGeometry(
roundedRectangleShape(width, height, radius),
{
depth,
bevelEnabled: true,
bevelThickness: 0.035,
bevelSize: 0.035,
bevelSegments: 3,
curveSegments: 12
}
);
geometry.center();
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
/*
The exact same geometry is reused on the cup, token, and return station.
It is a circular return arrow with a solid triangular arrowhead.
*/
function createReturnSymbol(material) {
const group = new THREE.Group();
const radius = 0.27;
const arcLength = Math.PI * 1.58;
const arc = new THREE.Mesh(
new THREE.TorusGeometry(radius, 0.055, 12, 64, arcLength),
material
);
arc.castShadow = true;
group.add(arc);
const endAngle = arcLength;
const end = new THREE.Vector3(
Math.cos(endAngle) * radius,
Math.sin(endAngle) * radius,
0
);
const tangent = new THREE.Vector3(
-Math.sin(endAngle),
Math.cos(endAngle),
0
);
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.115, 0.22, 3),
material
);
head.position.copy(end).addScaledVector(tangent, 0.065);
head.rotation.z = endAngle;
head.castShadow = true;
group.add(head);
return group;
}
function addFlatArrow(x, z, scale = 1) {
const shape = new THREE.Shape();
shape.moveTo(-0.24 * scale, 0.16 * scale);
shape.lineTo(-0.06 * scale, 0.16 * scale);
shape.lineTo(0.13 * scale, 0);
shape.lineTo(-0.06 * scale, -0.16 * scale);
shape.lineTo(-0.24 * scale, -0.16 * scale);
shape.lineTo(0.01 * scale, 0);
shape.closePath();
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: 0.025,
bevelEnabled: false
});
const mesh = new THREE.Mesh(geometry, materials.arrow);
mesh.rotation.x = -Math.PI / 2;
mesh.position.set(x, 0.018, z);
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// Lighting
scene.add(new THREE.HemisphereLight(0xfffbf0, 0x8a8173, 2.1));
const keyLight = new THREE.DirectionalLight(0xfff4dd, 3.1);
keyLight.position.set(-5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.00025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xdce8e4, 1.05);
fillLight.position.set(8, 5, -7);
scene.add(fillLight);
// Ground and display platform
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
materials.floor
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.4;
floor.receiveShadow = true;
scene.add(floor);
const platformEdge = roundedBox(
11.08,
4.66,
0.13,
0.38,
materials.platformEdge
);
platformEdge.rotation.x = -Math.PI / 2;
platformEdge.position.y = -0.31;
scene.add(platformEdge);
const platform = roundedBox(
10.9,
4.5,
0.3,
0.35,
materials.platform
);
platform.rotation.x = -Math.PI / 2;
platform.position.y = -0.15;
scene.add(platform);
// Cup deposit pad
const cupPad = new THREE.Mesh(
new THREE.CylinderGeometry(1.3, 1.34, 0.12, 64),
materials.platform
);
cupPad.position.set(-3.15, 0.06, 0.15);
cupPad.castShadow = true;
cupPad.receiveShadow = true;
scene.add(cupPad);
const cupPadRing = new THREE.Mesh(
new THREE.TorusGeometry(1.17, 0.035, 8, 64),
materials.softDark
);
cupPadRing.rotation.x = Math.PI / 2;
cupPadRing.position.set(-3.15, 0.125, 0.15);
scene.add(cupPadRing);
// Sturdy reusable cup
const cup = new THREE.Group();
const cupBasePosition = new THREE.Vector3(-3.15, 0.075, 0.15);
cup.position.copy(cupBasePosition);
cup.rotation.y = 0.48;
scene.add(cup);
const cupProfile = [
new THREE.Vector2(0, 0.08),
new THREE.Vector2(0.53, 0.08),
new THREE.Vector2(0.64, 0.14),
new THREE.Vector2(0.72, 0.38),
new THREE.Vector2(0.76, 1.73),
new THREE.Vector2(0.82, 2.05),
new THREE.Vector2(0.82, 2.17),
new THREE.Vector2(0.78, 2.23),
new THREE.Vector2(0.66, 2.23),
new THREE.Vector2(0.64, 2.12),
new THREE.Vector2(0.67, 1.9),
new THREE.Vector2(0.62, 0.36),
new THREE.Vector2(0.53, 0.28),
new THREE.Vector2(0, 0.28)
];
const cupBody = new THREE.Mesh(
new THREE.LatheGeometry(cupProfile, 64),
materials.cup
);
cupBody.castShadow = true;
cupBody.receiveShadow = true;
cup.add(cupBody);
const cupRim = new THREE.Mesh(
new THREE.TorusGeometry(0.74, 0.1, 12, 64),
materials.cup
);
cupRim.rotation.x = Math.PI / 2;
cupRim.position.y = 2.19;
cupRim.castShadow = true;
cup.add(cupRim);
const cupBaseRing = new THREE.Mesh(
new THREE.TorusGeometry(0.56, 0.065, 10, 64),
materials.cup
);
cupBaseRing.rotation.x = Math.PI / 2;
cupBaseRing.position.y = 0.2;
cupBaseRing.castShadow = true;
cup.add(cupBaseRing);
for (const y of [0.53, 0.74]) {
const rib = new THREE.Mesh(
new THREE.TorusGeometry(y === 0.53 ? 0.69 : 0.72, 0.026, 8, 64),
materials.cupBadge
);
rib.rotation.x = Math.PI / 2;
rib.position.y = y;
cup.add(rib);
}
const handleCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(0.67, 1.83, 0),
new THREE.Vector3(1.12, 1.98, 0),
new THREE.Vector3(1.48, 1.74, 0),
new THREE.Vector3(1.59, 1.28, 0),
new THREE.Vector3(1.48, 0.83, 0),
new THREE.Vector3(1.12, 0.62, 0),
new THREE.Vector3(0.66, 0.73, 0)
]);
const handle = new THREE.Mesh(
new THREE.TubeGeometry(handleCurve, 48, 0.135, 12, false),
materials.cup
);
handle.castShadow = true;
handle.receiveShadow = true;
cup.add(handle);
const upperMount = new THREE.Mesh(
new THREE.SphereGeometry(0.16, 20, 16),
materials.cup
);
upperMount.scale.set(1, 0.72, 1);
upperMount.position.set(0.7, 1.84, 0);
upperMount.castShadow = true;
cup.add(upperMount);
const lowerMount = upperMount.clone();
lowerMount.position.set(0.68, 0.73, 0);
cup.add(lowerMount);
const cupBadge = new THREE.Mesh(
new THREE.CircleGeometry(0.39, 48),
materials.cupBadge
);
cupBadge.position.set(0, 1.18, 0.735);
cup.add(cupBadge);
const cupBadgeEdge = new THREE.Mesh(
new THREE.TorusGeometry(0.39, 0.025, 8, 48),
materials.softDark
);
cupBadgeEdge.position.set(0, 1.18, 0.752);
cup.add(cupBadgeEdge);
const cupSymbol = createReturnSymbol(materials.dark);
cupSymbol.position.set(0, 1.18, 0.79);
cup.add(cupSymbol);
// Token stand
const tokenBase = roundedBox(
1.42,
0.18,
0.84,
0.12,
materials.platformEdge
);
tokenBase.position.set(0, 0.09, 0.32);
scene.add(tokenBase);
const tokenStem = roundedBox(
0.28,
0.82,
0.2,
0.07,
materials.softDark
);
tokenStem.position.set(0, 0.58, 0.26);
scene.add(tokenStem);
// Matching deposit token
const tokenPivot = new THREE.Group();
const tokenStartPosition = new THREE.Vector3(0, 1.34, 0.35);
tokenPivot.position.copy(tokenStartPosition);
tokenPivot.rotation.y = 0.48;
scene.add(tokenPivot);
const tokenGeometry = new THREE.CylinderGeometry(
0.58,
0.58,
0.18,
64
);
tokenGeometry.rotateX(Math.PI / 2);
const tokenBody = new THREE.Mesh(tokenGeometry, materials.token);
tokenBody.castShadow = true;
tokenBody.receiveShadow = true;
tokenPivot.add(tokenBody);
const tokenInset = new THREE.Mesh(
new THREE.CircleGeometry(0.43, 48),
materials.tokenInset
);
tokenInset.position.z = 0.096;
tokenPivot.add(tokenInset);
const tokenRim = new THREE.Mesh(
new THREE.TorusGeometry(0.51, 0.045, 10, 64),
materials.dark
);
tokenRim.position.z = 0.11;
tokenRim.castShadow = true;
tokenPivot.add(tokenRim);
const tokenSymbol = createReturnSymbol(materials.dark);
tokenSymbol.position.z = 0.14;
tokenPivot.add(tokenSymbol);
// Return station
const stationBase = roundedBox(
3.05,
0.28,
1.75,
0.16,
materials.platformEdge
);
stationBase.position.set(3.05, 0.14, -0.15);
scene.add(stationBase);
const stationBody = roundedBox(
2.65,
2.75,
1.25,
0.32,
materials.station
);
stationBody.position.set(3.05, 1.4, -0.15);
scene.add(stationBody);
const stationHood = roundedBox(
2.95,
0.55,
1.55,
0.18,
materials.station
);
stationHood.position.set(3.05, 2.65, -0.15);
scene.add(stationHood);
const stationPanel = roundedBox(
2.32,
2.32,
0.1,
0.24,
materials.stationPanel
);
stationPanel.position.set(3.05, 1.37, 0.49);
scene.add(stationPanel);
const slotRecess = new THREE.Mesh(
new THREE.CircleGeometry(0.54, 64),
materials.dark
);
slotRecess.position.set(3.05, 1.29, 0.552);
scene.add(slotRecess);
const slotOutline = new THREE.Mesh(
new THREE.TorusGeometry(0.62, 0.065, 12, 64),
materials.light
);
slotOutline.position.set(3.05, 1.29, 0.585);
slotOutline.castShadow = true;
scene.add(slotOutline);
const stationSymbol = createReturnSymbol(materials.light);
stationSymbol.scale.setScalar(0.72);
stationSymbol.position.set(3.05, 2.22, 0.59);
scene.add(stationSymbol);
const downArrowShape = new THREE.Shape();
downArrowShape.moveTo(-0.12, 0.22);
downArrowShape.lineTo(0.12, 0.22);
downArrowShape.lineTo(0.12, -0.02);
downArrowShape.lineTo(0.29, -0.02);
downArrowShape.lineTo(0, -0.34);
downArrowShape.lineTo(-0.29, -0.02);
downArrowShape.lineTo(-0.12, -0.02);
downArrowShape.closePath();
const downArrow = new THREE.Mesh(
new THREE.ExtrudeGeometry(downArrowShape, {
depth: 0.055,
bevelEnabled: true,
bevelThickness: 0.018,
bevelSize: 0.018,
bevelSegments: 2
}),
materials.light
);
downArrow.position.set(3.05, 0.54, 0.57);
downArrow.castShadow = true;
scene.add(downArrow);
// Ground arrows guide the token toward the return slot.
addFlatArrow(0.78, 0.38, 0.72);
addFlatArrow(1.22, 0.38, 0.72);
addFlatArrow(1.66, 0.38, 0.72);
// Camera controls implemented directly without external controls modules.
const cameraState = {
yaw: 0.48,
pitch: 1.13,
radius: 11.4,
target: new THREE.Vector3(0, 1.05, 0)
};
let fitScale = 1;
let cameraTween = null;
let currentView = "overview";
function getFitScale() {
return Math.max(1, Math.min(2.4, 1.1 / camera.aspect));
}
function cameraPresets() {
return {
overview: {
yaw: 0.48,
pitch: 1.13,
radius: 11.4 * fitScale,
target: new THREE.Vector3(0, 1.05, 0)
},
deposit: {
yaw: 0.42,
pitch: 1.08,
radius: 7.5 * fitScale,
target: new THREE.Vector3(-1.45, 1.08, 0.15)
},
return: {
yaw: 0.36,
pitch: 1.09,
radius: 7.1 * fitScale,
target: new THREE.Vector3(2.25, 1.35, -0.05)
}
};
}
function updateCamera() {
const horizontal = Math.sin(cameraState.pitch) * cameraState.radius;
camera.position.set(
cameraState.target.x + horizontal * Math.sin(cameraState.yaw),
cameraState.target.y + Math.cos(cameraState.pitch) * cameraState.radius,
cameraState.target.z + horizontal * Math.cos(cameraState.yaw)
);
camera.lookAt(cameraState.target);
}
function setView(name, instant = false) {
const preset = cameraPresets()[name];
if (!preset) return;
currentView = name;
document.querySelectorAll("[data-view]").forEach((button) => {
button.setAttribute(
"aria-pressed",
String(button.dataset.view === name)
);
});
if (instant || reduceMotion) {
cameraState.yaw = preset.yaw;
cameraState.pitch = preset.pitch;
cameraState.radius = preset.radius;
cameraState.target.copy(preset.target);
cameraTween = null;
return;
}
cameraTween = {
start: performance.now(),
duration: 850,
from: {
yaw: cameraState.yaw,
pitch: cameraState.pitch,
radius: cameraState.radius,
target: cameraState.target.clone()
},
to: preset
};
}
document.querySelectorAll("[data-view]").forEach((button) => {
button.addEventListener("click", () => {
setView(button.dataset.view);
});
});
let pointerDown = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
pointerDown = true;
pointerX = event.clientX;
pointerY = event.clientY;
cameraTween = null;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!pointerDown) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch - dy * 0.005,
0.48,
1.45
);
});
function stopDragging(event) {
pointerDown = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraTween = null;
const minRadius = 4.5 * Math.max(1, fitScale * 0.75);
const maxRadius = 18 * fitScale;
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
minRadius,
maxRadius
);
},
{ passive: false }
);
canvas.addEventListener("keydown", (event) => {
cameraTween = null;
if (event.key === "ArrowLeft") cameraState.yaw += 0.08;
else if (event.key === "ArrowRight") cameraState.yaw -= 0.08;
else if (event.key === "ArrowUp") {
cameraState.pitch = Math.max(0.48, cameraState.pitch - 0.06);
} else if (event.key === "ArrowDown") {
cameraState.pitch = Math.min(1.45, cameraState.pitch + 0.06);
} else if (event.key === "+" || event.key === "=") {
cameraState.radius = Math.max(4.5, cameraState.radius - 0.5);
} else if (event.key === "-" || event.key === "_") {
cameraState.radius = Math.min(18 * fitScale, cameraState.radius + 0.5);
} else if (event.key.toLowerCase() === "r") {
setView("overview");
} else {
return;
}
event.preventDefault();
});
// Deposit and return previews
const depositButton = document.querySelector("#deposit-action");
const returnButton = document.querySelector("#return-action");
let depositAction = null;
let returnAction = null;
function easeOutCubic(value) {
return 1 - Math.pow(1 - value, 3);
}
function smoothstep(value, min, max) {
const x = THREE.MathUtils.clamp(
(value - min) / (max - min),
0,
1
);
return x * x * (3 - 2 * x);
}
function quadraticBezier(a, b, c, t) {
const inverse = 1 - t;
return new THREE.Vector3()
.addScaledVector(a, inverse * inverse)
.addScaledVector(b, 2 * inverse * t)
.addScaledVector(c, t * t);
}
depositButton.addEventListener("click", () => {
if (depositAction || returnAction) return;
setView("deposit");
depositAction = {
start: performance.now(),
duration: reduceMotion ? 1 : 1450
};
depositButton.disabled = true;
returnButton.disabled = true;
});
returnButton.addEventListener("click", () => {
if (depositAction || returnAction) return;
setView("return");
returnAction = {
start: performance.now(),
duration: reduceMotion ? 1 : 2600
};
depositButton.disabled = true;
returnButton.disabled = true;
});
function updateDepositAction(now) {
if (!depositAction) return;
const elapsed = now - depositAction.start;
const phase = THREE.MathUtils.clamp(
elapsed / depositAction.duration,
0,
1
);
if (phase < 0.2) {
const u = phase / 0.2;
cup.position.set(
cupBasePosition.x - 0.5 * u,
cupBasePosition.y + 0.34 * u,
cupBasePosition.z
);
cup.rotation.z = 0.09 * u;
} else if (phase < 0.76) {
const u = (phase - 0.2) / 0.56;
const eased = easeOutCubic(u);
cup.position.set(
cupBasePosition.x - 0.5 * (1 - eased),
cupBasePosition.y + 0.34 * (1 - eased),
cupBasePosition.z
);
cup.rotation.z = 0.09 * (1 - eased);
} else {
const u = (phase - 0.76) / 0.24;
const bounce = Math.sin(u * Math.PI) * (1 - u) * 0.12;
cup.position.copy(cupBasePosition);
cup.position.y += bounce;
cup.rotation.z = 0;
}
tokenPivot.position.y =
tokenStartPosition.y + Math.sin(phase * Math.PI) * 0.09;
if (phase >= 1) {
cup.position.copy(cupBasePosition);
cup.rotation.z = 0;
tokenPivot.position.copy(tokenStartPosition);
depositAction = null;
depositButton.disabled = false;
returnButton.disabled = false;
}
}
function updateReturnAction(now) {
if (!returnAction) return;
const elapsed = now - returnAction.start;
const travel = THREE.MathUtils.clamp(elapsed / 1750, 0, 1);
const start = tokenStartPosition;
const control = new THREE.Vector3(1.55, 2.15, 0.9);
const end = new THREE.Vector3(3.05, 1.29, 0.57);
tokenPivot.position.copy(
quadraticBezier(start, control, end, travel)
);
tokenPivot.rotation.y =
0.48 + Math.sin(travel * Math.PI) * 0.28;
const scale = 1 - smoothstep(travel, 0.77, 1);
tokenPivot.scale.setScalar(Math.max(0.015, scale));
if (elapsed > 2300) {
tokenPivot.position.copy(tokenStartPosition);
tokenPivot.rotation.set(0, 0.48, 0);
tokenPivot.scale.setScalar(1);
returnAction = null;
depositButton.disabled = false;
returnButton.disabled = false;
}
}
// Projected scene labels
const labels = {
cup: {
element: document.querySelector("#label-cup"),
anchor: new THREE.Vector3()
},
token: {
element: document.querySelector("#label-token"),
anchor: new THREE.Vector3()
},
return: {
element: document.querySelector("#label-return"),
anchor: new THREE.Vector3()
}
};
function positionLabel(label, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
label.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;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
function updateLabels() {
labels.cup.anchor.set(0, 2.72, 0);
cup.localToWorld(labels.cup.anchor);
labels.token.anchor.set(0, 0.78, 0);
tokenPivot.localToWorld(labels.token.anchor);
labels.return.anchor.set(3.05, 3.28, -0.15);
positionLabel(labels.cup, labels.cup.anchor);
positionLabel(labels.token, labels.token.anchor);
positionLabel(labels.return, labels.return.anchor);
}
function updateCameraTween(now) {
if (!cameraTween) return;
const elapsed = now - cameraTween.start;
const t = THREE.MathUtils.clamp(
elapsed / cameraTween.duration,
0,
1
);
const eased = t < 0.5
? 4 * t * t * t
: 1 - Math.pow(-2 * t + 2, 3) / 2;
cameraState.yaw = THREE.MathUtils.lerp(
cameraTween.from.yaw,
cameraTween.to.yaw,
eased
);
cameraState.pitch = THREE.MathUtils.lerp(
cameraTween.from.pitch,
cameraTween.to.pitch,
eased
);
cameraState.radius = THREE.MathUtils.lerp(
cameraTween.from.radius,
cameraTween.to.radius,
eased
);
cameraState.target.lerpVectors(
cameraTween.from.target,
cameraTween.to.target,
eased
);
if (t >= 1) cameraTween = null;
}
function onResize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.fov = width < 700 ? 46 : 38;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
fitScale = getFitScale();
if (currentView === "overview" && !cameraTween) {
cameraState.radius = cameraPresets().overview.radius;
}
}
window.addEventListener("resize", onResize);
onResize();
cameraState.radius = cameraPresets().overview.radius;
updateCamera();
function animate(now) {
const elapsed = clock.getElapsedTime();
updateCameraTween(now);
updateDepositAction(now);
updateReturnAction(now);
updateCamera();
// A restrained pulse on the return path reinforces the return direction.
const pulse = 0.78 + Math.sin(elapsed * 1.7) * 0.12;
materials.arrow.color.setRGB(pulse, pulse, pulse * 0.96);
renderer.render(scene, camera);
updateLabels();
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```