Prompt specification
Build or customize the Three.js 3D scene "Shared Streetlight Contribution Tray" 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: Shared Streetlight Contribution Tray
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/shared-streetlight-contribution-tray-x3185
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring a low-poly streetlight illuminating four miniature doorways around a small shared courtyard. Beside each doorway, place a separate contribution cup containing a visibly different number of tokens. The lamp's light should fall across the same courtyard and entrances regardless of how the tokens are redistributed. Let the viewer move a few tokens between cups while leaving the lamp and building geometry fixed. Keep contribution counts readable, but avoid turning the illuminated ground into four privately colored sectors.
Working Three.js Starter (shared-streetlight-contribution-tray-x3185):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Shared Courtyard Contributions</title>
<style>
:root {
color-scheme: dark;
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #111827;
}
body {
color: #f4f0e7;
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
#labels {
position: fixed;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.cup-label {
position: absolute;
display: flex;
align-items: baseline;
gap: 7px;
min-width: 112px;
padding: 7px 10px;
border: 1px solid rgba(58, 52, 43, 0.24);
border-radius: 9px;
background: rgba(249, 245, 235, 0.94);
color: #282820;
box-shadow: 0 5px 18px rgba(0, 0, 0, 0.22);
transform: translate(-50%, -100%);
white-space: nowrap;
will-change: left, top;
}
.cup-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 12px;
background: rgba(249, 245, 235, 0.72);
}
.cup-label .place {
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
opacity: 0.65;
}
.cup-label strong {
font-size: 19px;
line-height: 1;
}
.cup-label small {
font-size: 10px;
opacity: 0.68;
}
.title-card,
.transfer-card {
position: fixed;
z-index: 5;
border: 1px solid rgba(255, 255, 255, 0.13);
background: rgba(18, 24, 34, 0.84);
box-shadow: 0 16px 45px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.title-card {
top: 18px;
left: 18px;
width: min(360px, calc(100vw - 36px));
padding: 15px 17px;
border-radius: 13px;
}
.eyebrow {
margin: 0 0 5px;
color: #d9c9a6;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 21px;
line-height: 1.15;
font-weight: 720;
}
.title-card p {
margin: 8px 0 0;
color: rgba(244, 240, 231, 0.67);
font-size: 12px;
line-height: 1.45;
}
.transfer-card {
top: 18px;
right: 18px;
width: 304px;
padding: 15px;
border-radius: 13px;
}
.transfer-card h2 {
margin: 0 0 12px;
font-size: 14px;
font-weight: 700;
}
.total-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 13px;
padding: 9px 10px;
border-radius: 8px;
background: rgba(255, 255, 255, 0.055);
font-size: 11px;
color: rgba(244, 240, 231, 0.68);
}
.total-row strong {
color: #f7e6bc;
font-size: 15px;
}
.field-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-bottom: 9px;
}
.field-row.amount-row {
grid-template-columns: 1fr 118px;
}
label {
display: grid;
gap: 5px;
color: rgba(244, 240, 231, 0.62);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.04em;
text-transform: uppercase;
}
select,
button {
min-height: 36px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 8px;
font: inherit;
}
select {
width: 100%;
padding: 0 9px;
background: #202936;
color: #f6f1e8;
font-size: 12px;
text-transform: none;
letter-spacing: normal;
}
button {
cursor: pointer;
font-size: 12px;
font-weight: 750;
transition: background 150ms ease, transform 150ms ease;
}
button:hover {
transform: translateY(-1px);
}
#transferButton {
width: 100%;
background: #ead8b4;
color: #26251f;
border-color: transparent;
}
#transferButton:hover {
background: #f5e5c4;
}
#resetButton {
width: 100%;
margin-top: 8px;
background: transparent;
color: rgba(244, 240, 231, 0.68);
}
#resetButton:hover {
background: rgba(255, 255, 255, 0.06);
}
.counts {
margin: 13px 0 0;
padding-top: 11px;
border-top: 1px solid rgba(255, 255, 255, 0.09);
color: rgba(244, 240, 231, 0.58);
font-size: 10px;
line-height: 1.65;
}
.hint {
position: fixed;
z-index: 4;
left: 50%;
bottom: 17px;
max-width: calc(100vw - 30px);
padding: 9px 14px;
border: 1px solid rgba(255, 255, 255, 0.11);
border-radius: 999px;
background: rgba(15, 21, 30, 0.76);
color: rgba(244, 240, 231, 0.72);
font-size: 11px;
text-align: center;
transform: translateX(-50%);
backdrop-filter: blur(10px);
pointer-events: none;
}
#status {
position: fixed;
z-index: 6;
left: 18px;
bottom: 18px;
max-width: min(410px, calc(100vw - 36px));
padding: 10px 12px;
border-left: 3px solid #dfc89b;
border-radius: 6px;
background: rgba(18, 24, 34, 0.86);
color: rgba(244, 240, 231, 0.78);
font-size: 11px;
line-height: 1.4;
pointer-events: none;
}
#loading {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
background: #111827;
color: #e6d4af;
font-size: 13px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
@media (max-width: 760px) {
.title-card {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 12px 14px;
}
.title-card p {
display: none;
}
.transfer-card {
top: auto;
right: 10px;
bottom: 53px;
width: min(304px, calc(100vw - 20px));
}
.counts {
display: none;
}
#status {
display: none;
}
.hint {
bottom: 10px;
width: calc(100vw - 20px);
border-radius: 9px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly courtyard with four contribution cups"></canvas>
<div id="labels" aria-hidden="true"></div>
<header class="title-card">
<p class="eyebrow">A shared place</p>
<h1>Courtyard Contributions</h1>
<p>
One common pool of light. Redistribute the tokens while the doorway
buildings and streetlamp remain fixed.
</p>
</header>
<aside class="transfer-card" aria-label="Token transfer controls">
<h2>Move contributions</h2>
<div class="total-row">
<span>Total contributions</span>
<strong id="totalCount">40</strong>
</div>
<div class="field-row">
<label>
From
<select id="sourceSelect">
<option value="0">North — 3</option>
<option value="1">East — 7</option>
<option value="2">South — 12</option>
<option value="3">West — 18</option>
</select>
</label>
<label>
Into
<select id="destinationSelect">
<option value="0">North</option>
<option value="1">East</option>
<option value="2">South</option>
<option value="3">West</option>
</select>
</label>
</div>
<div class="field-row amount-row">
<label>
Amount
<select id="amountSelect">
<option value="1">1 token</option>
<option value="3">3 tokens</option>
<option value="5">5 tokens</option>
</select>
</label>
<button id="transferButton" type="button">Transfer</button>
</div>
<button id="resetButton" type="button">Reset camera view</button>
<div class="counts" id="countsSummary"></div>
</aside>
<div id="status" role="status" aria-live="polite">
Select a cup, then use the transfer panel or drag it onto another cup.
</div>
<div class="hint">
Drag a cup onto another cup to move 1 token · Shift-drag moves 5 ·
Drag the background to orbit · Scroll to zoom
</div>
<div id="loading">Building the courtyard…</div>
<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 labelsLayer = document.querySelector("#labels");
const sourceSelect = document.querySelector("#sourceSelect");
const destinationSelect = document.querySelector("#destinationSelect");
const amountSelect = document.querySelector("#amountSelect");
const transferButton = document.querySelector("#transferButton");
const resetButton = document.querySelector("#resetButton");
const totalCount = document.querySelector("#totalCount");
const countsSummary = document.querySelector("#countsSummary");
const statusElement = document.querySelector("#status");
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.16;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x111827);
scene.fog = new THREE.Fog(0x111827, 13, 31);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
80
);
const cameraTarget = new THREE.Vector3(0, 0.95, 0);
const orbit = {
yaw: 0.69,
pitch: 0.73,
radius: 15.2
};
function updateCamera() {
const horizontal = orbit.radius * Math.cos(orbit.pitch);
camera.position.set(
cameraTarget.x + Math.sin(orbit.yaw) * horizontal,
cameraTarget.y + Math.sin(orbit.pitch) * orbit.radius,
cameraTarget.z + Math.cos(orbit.yaw) * horizontal
);
camera.lookAt(cameraTarget);
}
updateCamera();
// Soft, uniform fill. The visible warm pool below comes from one
// shared streetlamp light rather than separate entrance lights.
const hemisphere = new THREE.HemisphereLight(
0x9baec6,
0x4b4033,
0.72
);
scene.add(hemisphere);
const ambient = new THREE.AmbientLight(0xffffff, 0.12);
scene.add(ambient);
const materials = {
outside: new THREE.MeshStandardMaterial({
color: 0x252c35,
roughness: 1
}),
wall: new THREE.MeshStandardMaterial({
color: 0xb6aa98,
roughness: 0.93
}),
wallTrim: new THREE.MeshStandardMaterial({
color: 0xd1c5b1,
roughness: 0.88
}),
roof: new THREE.MeshStandardMaterial({
color: 0x4f5556,
roughness: 0.86
}),
recess: new THREE.MeshStandardMaterial({
color: 0x252a2d,
roughness: 1
}),
door: new THREE.MeshStandardMaterial({
color: 0x405b60,
roughness: 0.72,
metalness: 0.06
}),
metal: new THREE.MeshStandardMaterial({
color: 0x30383c,
roughness: 0.46,
metalness: 0.72
}),
token: new THREE.MeshStandardMaterial({
color: 0xb8783f,
roughness: 0.3,
metalness: 0.67
}),
selection: new THREE.MeshBasicMaterial({
color: 0xf1dfb6,
transparent: true,
opacity: 0.9
})
};
function addBox(parent, width, height, depth, x, y, z, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// A single procedural stone texture covers the entire courtyard.
// There are no colored quadrants or cup-specific ground materials.
function makeCourtyardTexture() {
const size = 1024;
const tiles = 8;
const tileSize = size / tiles;
const canvasTexture = document.createElement("canvas");
canvasTexture.width = canvasTexture.height = size;
const context = canvasTexture.getContext("2d");
const colors = [
"#817b70",
"#878176",
"#7b766d",
"#8b8478"
];
context.fillStyle = "#57534c";
context.fillRect(0, 0, size, size);
let seed = 173;
const random = () => {
seed = (seed * 9301 + 49297) % 233280;
return seed / 233280;
};
for (let row = 0; row < tiles; row++) {
for (let column = 0; column < tiles; column++) {
const x = column * tileSize;
const y = row * tileSize;
const gap = 3;
context.fillStyle = colors[(row + column * 2) % colors.length];
context.fillRect(
x + gap,
y + gap,
tileSize - gap * 2,
tileSize - gap * 2
);
for (let speck = 0; speck < 10; speck++) {
const shade = random() > 0.5
? "rgba(255,255,255,0.035)"
: "rgba(0,0,0,0.045)";
context.fillStyle = shade;
context.fillRect(
x + 8 + random() * (tileSize - 16),
y + 8 + random() * (tileSize - 16),
1 + random() * 3,
1 + random() * 3
);
}
}
}
const texture = new THREE.CanvasTexture(canvasTexture);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
const outsideFloor = new THREE.Mesh(
new THREE.PlaneGeometry(50, 50),
materials.outside
);
outsideFloor.rotation.x = -Math.PI / 2;
outsideFloor.position.y = -0.2;
outsideFloor.receiveShadow = true;
scene.add(outsideFloor);
const courtyard = new THREE.Mesh(
new THREE.PlaneGeometry(10.6, 10.6),
new THREE.MeshStandardMaterial({
map: makeCourtyardTexture(),
roughness: 0.96,
metalness: 0
})
);
courtyard.rotation.x = -Math.PI / 2;
courtyard.position.y = 0;
courtyard.receiveShadow = true;
scene.add(courtyard);
function makeNumberTexture(number) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 128;
labelCanvas.height = 80;
const context = labelCanvas.getContext("2d");
context.fillStyle = "#d8c9ac";
context.fillRect(0, 0, 128, 80);
context.fillStyle = "#34332c";
context.font = "bold 42px sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(String(number).padStart(2, "0"), 64, 42);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
const locations = [
{ name: "North", position: [0, 0, -5], rotation: 0 },
{ name: "East", position: [5, 0, 0], rotation: -Math.PI / 2 },
{ name: "South", position: [0, 0, 5], rotation: Math.PI },
{ name: "West", position: [-5, 0, 0], rotation: Math.PI / 2 }
];
const counts = [3, 7, 12, 18];
const totalTokens = counts.reduce((sum, count) => sum + count, 0);
const cups = [];
const hitTargets = [];
const tokenGeometry = new THREE.CylinderGeometry(
0.145,
0.145,
0.043,
12,
1,
false
);
const glassMaterial = new THREE.MeshStandardMaterial({
color: 0xc6d1cf,
roughness: 0.18,
metalness: 0.05,
transparent: true,
opacity: 0.43,
side: THREE.DoubleSide,
depthWrite: false
});
const invisibleHitMaterial = new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
});
function createCup(parent, id) {
const group = new THREE.Group();
group.position.set(1.04, 0, 0.66);
parent.add(group);
const body = new THREE.Mesh(
new THREE.CylinderGeometry(0.235, 0.18, 0.36, 12, 1, true),
glassMaterial
);
body.position.y = 0.2;
body.renderOrder = 2;
group.add(body);
const bottom = new THREE.Mesh(
new THREE.CylinderGeometry(0.18, 0.18, 0.025, 12),
glassMaterial
);
bottom.position.y = 0.045;
bottom.renderOrder = 2;
group.add(bottom);
const rimGeometry = new THREE.TorusGeometry(0.235, 0.018, 4, 12);
for (const height of [0.055, 0.36]) {
const rim = new THREE.Mesh(rimGeometry, materials.metal);
rim.rotation.x = Math.PI / 2;
rim.position.y = height;
rim.castShadow = true;
group.add(rim);
}
const tokenStack = new THREE.Group();
group.add(tokenStack);
const selectionRing = new THREE.Mesh(
new THREE.TorusGeometry(0.295, 0.017, 4, 18),
materials.selection
);
selectionRing.rotation.x = Math.PI / 2;
selectionRing.position.y = 0.025;
selectionRing.visible = false;
group.add(selectionRing);
const hitTarget = new THREE.Mesh(
new THREE.CylinderGeometry(0.31, 0.31, 1.18, 8),
invisibleHitMaterial
);
hitTarget.position.y = 0.56;
hitTarget.userData.cupId = id;
group.add(hitTarget);
hitTargets.push(hitTarget);
const label = document.createElement("div");
label.className = "cup-label";
label.innerHTML = `
<span class="place">${locations[id].name}</span>
<strong>${counts[id]}</strong>
<small>tokens</small>
`;
labelsLayer.appendChild(label);
const cup = {
id,
group,
tokenStack,
selectionRing,
label,
labelCount: label.querySelector("strong")
};
cups.push(cup);
return cup;
}
function createFacade(location, id) {
const group = new THREE.Group();
group.position.fromArray(location.position);
group.rotation.y = location.rotation;
scene.add(group);
const width = 2.75;
const height = 2.72;
const doorWidth = 1.08;
const doorHeight = 1.82;
const depth = 0.42;
const sideWidth = (width - doorWidth) / 2;
// The wall is assembled around the opening so the doorway reads
// as a recessed miniature entrance.
addBox(
group,
sideWidth,
height,
depth,
-(doorWidth / 2 + sideWidth / 2),
height / 2,
0,
materials.wall
);
addBox(
group,
sideWidth,
height,
depth,
doorWidth / 2 + sideWidth / 2,
height / 2,
0,
materials.wall
);
addBox(
group,
width,
height - doorHeight,
depth,
0,
doorHeight + (height - doorHeight) / 2,
0,
materials.wall
);
addBox(
group,
doorWidth,
doorHeight,
0.07,
0,
doorHeight / 2,
0.2,
materials.recess
);
addBox(
group,
0.96,
1.68,
0.07,
0,
0.87,
0.245,
materials.door
);
// Door frame.
addBox(
group,
0.1,
1.9,
0.12,
-0.58,
0.95,
0.275,
materials.wallTrim
);
addBox(
group,
0.1,
1.9,
0.12,
0.58,
0.95,
0.275,
materials.wallTrim
);
addBox(
group,
1.25,
0.11,
0.12,
0,
1.87,
0.275,
materials.wallTrim
);
// Threshold and roof cap.
addBox(
group,
1.28,
0.09,
0.62,
0,
0.045,
0.12,
materials.wallTrim
);
addBox(
group,
width + 0.2,
0.18,
0.62,
0,
height + 0.08,
0.01,
materials.roof
);
addBox(
group,
width + 0.06,
0.11,
0.48,
0,
0.08,
0,
materials.wallTrim
);
// Small neutral number plaque.
const plaque = new THREE.Mesh(
new THREE.PlaneGeometry(0.34, 0.21),
new THREE.MeshStandardMaterial({
map: makeNumberTexture(id + 1),
roughness: 0.9
})
);
plaque.position.set(0, 2.2, 0.218);
group.add(plaque);
// Low-poly handle.
const handle = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.045, 0),
materials.metal
);
handle.position.set(0.31, 0.88, 0.315);
handle.castShadow = true;
group.add(handle);
createCup(group, id);
}
locations.forEach(createFacade);
// One fixed low-poly streetlamp and one shared point light.
const lamp = new THREE.Group();
lamp.position.set(-0.66, 0, 0.24);
scene.add(lamp);
const lampBase = new THREE.Mesh(
new THREE.CylinderGeometry(0.31, 0.4, 0.18, 8),
materials.metal
);
lampBase.position.y = 0.09;
lampBase.castShadow = true;
lampBase.receiveShadow = true;
lamp.add(lampBase);
const pole = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.12, 3.48, 8),
materials.metal
);
pole.position.set(-0.53, 1.78, 0);
pole.castShadow = true;
lamp.add(pole);
const arm = addBox(
lamp,
1.18,
0.11,
0.12,
-0.13,
3.52,
0,
materials.metal
);
arm.rotation.z = -0.06;
const lampHousing = new THREE.Mesh(
new THREE.CylinderGeometry(0.18, 0.39, 0.34, 8),
materials.metal
);
lampHousing.position.set(0.4, 3.36, 0);
lampHousing.castShadow = true;
lamp.add(lampHousing);
const bulbMaterial = new THREE.MeshStandardMaterial({
color: 0xffe1a5,
emissive: 0xffbd5c,
emissiveIntensity: 4.2,
roughness: 0.25
});
const bulb = new THREE.Mesh(
new THREE.SphereGeometry(0.145, 8, 6),
bulbMaterial
);
bulb.position.set(0.4, 3.1, 0);
lamp.add(bulb);
const sharedLampLight = new THREE.PointLight(
0xffd49a,
210,
18,
2
);
sharedLampLight.position.set(0.4, 3.03, 0);
sharedLampLight.castShadow = true;
sharedLampLight.shadow.mapSize.set(1024, 1024);
sharedLampLight.shadow.camera.near = 0.15;
sharedLampLight.shadow.camera.far = 17;
sharedLampLight.shadow.bias = -0.0007;
sharedLampLight.shadow.normalBias = 0.025;
lamp.add(sharedLampLight);
function renderTokenStack(cup) {
cup.tokenStack.clear();
const count = counts[cup.id];
for (let index = 0; index < count; index++) {
const token = new THREE.Mesh(
tokenGeometry,
materials.token
);
const offsetX = Math.sin(index * 2.17) * 0.012;
const offsetZ = Math.cos(index * 1.73) * 0.012;
token.position.set(
offsetX,
0.085 + index * 0.052,
offsetZ
);
token.rotation.y = index * 0.47;
token.rotation.z = Math.sin(index * 1.9) * 0.018;
token.castShadow = true;
token.receiveShadow = true;
cup.tokenStack.add(token);
}
}
let selectedCup = 0;
function setSelectedCup(id) {
selectedCup = id;
cups.forEach((cup, index) => {
cup.selectionRing.visible = index === id;
});
sourceSelect.value = String(id);
}
function updateReadouts() {
cups.forEach((cup) => {
cup.labelCount.textContent = counts[cup.id];
renderTokenStack(cup);
});
[...sourceSelect.options].forEach((option, index) => {
option.textContent =
`${locations[index].name} — ${counts[index]}`;
});
totalCount.textContent = counts.reduce((sum, count) => sum + count, 0);
countsSummary.textContent = locations
.map((location, index) => `${location.name}: ${counts[index]}`)
.join(" · ");
}
function performTransfer(from, to, requestedAmount) {
if (from === to) {
statusElement.textContent =
"Choose two different cups to move contributions.";
return;
}
const moved = Math.min(requestedAmount, counts[from]);
if (moved <= 0) {
statusElement.textContent =
`${locations[from].name} does not currently contain any tokens.`;
return;
}
counts[from] -= moved;
counts[to] += moved;
updateReadouts();
setSelectedCup(to);
statusElement.textContent =
`Moved ${moved} token${moved === 1 ? "" : "s"} from ` +
`${locations[from].name} to ${locations[to].name}. ` +
`The shared light and doorway geometry are unchanged.`;
}
updateReadouts();
setSelectedCup(0);
transferButton.addEventListener("click", () => {
performTransfer(
Number(sourceSelect.value),
Number(destinationSelect.value),
Number(amountSelect.value)
);
});
resetButton.addEventListener("click", () => {
orbit.yaw = 0.69;
orbit.pitch = 0.73;
orbit.radius = 15.2;
updateCamera();
});
// A visible token follows a direct cup-to-cup drag.
const draggedToken = new THREE.Mesh(
tokenGeometry,
new THREE.MeshStandardMaterial({
color: 0xd29455,
roughness: 0.25,
metalness: 0.65,
emissive: 0x4a2508,
emissiveIntensity: 0.35
})
);
draggedToken.visible = false;
draggedToken.castShadow = true;
scene.add(draggedToken);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), -0.66);
const dragPoint = new THREE.Vector3();
function updatePointer(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
}
function pickCup(event) {
updatePointer(event);
const intersections = raycaster.intersectObjects(
hitTargets,
false
);
return intersections.length
? intersections[0].object.userData.cupId
: null;
}
let pointerAction = null;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
event.preventDefault();
const cupId = pickCup(event);
const now = performance.now();
pointerAction = {
mode: cupId === null ? "orbit" : "cup",
source: cupId,
startX: event.clientX,
startY: event.clientY,
lastX: event.clientX,
lastY: event.clientY,
startTime: now,
moved: false,
amount: event.shiftKey ? 5 : 1
};
canvas.setPointerCapture(event.pointerId);
if (cupId !== null) {
setSelectedCup(cupId);
draggedToken.visible = false;
canvas.style.cursor = "grabbing";
} else {
canvas.style.cursor = "grabbing";
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointerAction) {
const hoveredCup = pickCup(event);
canvas.style.cursor = hoveredCup === null ? "grab" : "pointer";
return;
}
const deltaX = event.clientX - pointerAction.lastX;
const deltaY = event.clientY - pointerAction.lastY;
pointerAction.lastX = event.clientX;
pointerAction.lastY = event.clientY;
const travel = Math.hypot(
event.clientX - pointerAction.startX,
event.clientY - pointerAction.startY
);
if (pointerAction.mode === "cup") {
if (travel > 6) {
pointerAction.moved = true;
updatePointer(event);
if (raycaster.ray.intersectPlane(dragPlane, dragPoint)) {
draggedToken.position.copy(dragPoint);
draggedToken.position.y = 0.68;
draggedToken.rotation.y += 0.12;
draggedToken.visible = true;
}
}
} else {
orbit.yaw -= deltaX * 0.006;
orbit.pitch += deltaY * 0.005;
orbit.pitch = THREE.MathUtils.clamp(
orbit.pitch,
0.28,
1.18
);
updateCamera();
}
});
function finishPointer(event) {
if (!pointerAction) return;
if (pointerAction.mode === "cup") {
if (pointerAction.moved) {
const targetCup = pickCup(event);
if (targetCup !== null && targetCup !== pointerAction.source) {
performTransfer(
pointerAction.source,
targetCup,
pointerAction.amount
);
} else {
statusElement.textContent =
"Drop the token on another cup to transfer it.";
}
} else {
statusElement.textContent =
`${locations[pointerAction.source].name} selected. ` +
`Drag it onto another cup or use the transfer panel.`;
}
draggedToken.visible = false;
}
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
pointerAction = null;
canvas.style.cursor = "grab";
}
canvas.addEventListener("pointerup", finishPointer);
canvas.addEventListener("pointercancel", finishPointer);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
9,
22
);
updateCamera();
}, { passive: false });
canvas.addEventListener("dblclick", () => {
orbit.yaw = 0.69;
orbit.pitch = 0.73;
orbit.radius = 15.2;
updateCamera();
});
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
const projectedPosition = new THREE.Vector3();
function updateCupLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
cups.forEach((cup) => {
const anchorHeight = 0.36 + counts[cup.id] * 0.052 + 0.13;
projectedPosition.set(0, anchorHeight, 0);
cup.group.localToWorld(projectedPosition);
projectedPosition.project(camera);
const visible =
projectedPosition.z > -1 &&
projectedPosition.z < 1 &&
projectedPosition.x > -1.12 &&
projectedPosition.x < 1.12 &&
projectedPosition.y > -1.12 &&
projectedPosition.y < 1.12;
cup.label.style.display = visible ? "flex" : "none";
if (visible) {
const x = (projectedPosition.x * 0.5 + 0.5) * width;
const y = (-projectedPosition.y * 0.5 + 0.5) * height;
cup.label.style.left = `${x}px`;
cup.label.style.top = `${y}px`;
}
});
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
renderer.setAnimationLoop(() => {
updateCupLabels();
renderer.render(scene, camera);
});
document.querySelector("#loading").remove();
</script>
</body>
</html>
```