Prompt specification
Build or customize the Three.js 3D scene "Reversible Two-Activity Time Dial" 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: Reversible Two-Activity Time Dial
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/reversible-two-activity-time-dial-x3173
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows opportunity costs of mutually exclusive choices. Model a tabletop dial split between reading and gardening with twelve movable blocks. Every added block on one activity must remove a block from the other. Low-poly.
Working Three.js Starter (reversible-two-activity-time-dial-x3173):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>The Opportunity Cost Dial</title>
<style>
:root {
--reading: #7066c7;
--reading-dark: #4d438e;
--gardening: #609b68;
--gardening-dark: #3d7048;
--ink: #292721;
--paper: rgba(255, 252, 245, 0.9);
--line: rgba(60, 50, 38, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e1d3;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: default;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
pointer-events: auto;
background: var(--paper);
border: 1px solid rgba(255, 255, 255, 0.72);
box-shadow:
0 18px 45px rgba(69, 53, 32, 0.13),
inset 0 0 0 1px rgba(79, 63, 41, 0.05);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.title-panel {
position: absolute;
top: 24px;
left: 24px;
width: min(390px, calc(100vw - 48px));
padding: 21px 23px 19px;
border-radius: 20px;
}
.eyebrow {
margin-bottom: 8px;
color: #8a7558;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(27px, 3.2vw, 42px);
font-weight: 500;
line-height: 0.98;
letter-spacing: -0.035em;
}
.subtitle {
max-width: 320px;
margin: 12px 0 15px;
color: #665d51;
font-size: 13px;
line-height: 1.55;
}
.hint-row {
display: flex;
flex-wrap: wrap;
gap: 7px;
}
.hint {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 7px 9px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(255, 255, 255, 0.48);
color: #6e6254;
font-size: 10px;
font-weight: 700;
}
.hint svg {
width: 13px;
height: 13px;
stroke: currentColor;
}
.balance-panel {
position: absolute;
top: 24px;
right: 24px;
display: flex;
align-items: stretch;
overflow: hidden;
border-radius: 18px;
}
.metric {
min-width: 128px;
padding: 16px 18px;
}
.metric + .metric {
border-left: 1px solid var(--line);
}
.metric-label {
display: flex;
align-items: center;
gap: 7px;
color: #766c60;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
}
.dot.reading {
background: var(--reading);
}
.dot.gardening {
background: var(--gardening);
}
.metric strong {
display: block;
margin-top: 3px;
font-family: Georgia, "Times New Roman", serif;
font-size: 32px;
font-weight: 500;
line-height: 1;
}
.total-choice {
display: grid;
min-width: 82px;
place-items: center;
padding: 12px;
background: rgba(91, 75, 52, 0.06);
color: #766a5b;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-align: center;
}
.total-choice span {
display: block;
margin-bottom: 2px;
color: var(--ink);
font-family: Georgia, "Times New Roman", serif;
font-size: 22px;
font-weight: 500;
letter-spacing: 0;
}
.controls-panel {
position: absolute;
bottom: 24px;
left: 50%;
display: grid;
grid-template-columns: minmax(220px, 1fr) 150px minmax(220px, 1fr);
gap: 10px;
width: min(790px, calc(100vw - 48px));
padding: 12px;
transform: translateX(-50%);
border-radius: 22px;
}
.activity-card {
padding: 13px 14px 12px;
border: 1px solid var(--line);
border-radius: 15px;
background: rgba(255, 255, 255, 0.44);
}
.activity-card.reading {
border-top: 3px solid var(--reading);
}
.activity-card.gardening {
border-top: 3px solid var(--gardening);
}
.card-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.card-name {
font-size: 11px;
font-weight: 850;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.card-count {
color: #71675b;
font-family: Georgia, "Times New Roman", serif;
font-size: 24px;
line-height: 1;
}
.track {
height: 5px;
margin: 11px 0 12px;
overflow: hidden;
border-radius: 999px;
background: rgba(62, 51, 38, 0.09);
}
.fill {
height: 100%;
border-radius: inherit;
transition: width 420ms cubic-bezier(0.22, 1, 0.36, 1);
}
.reading .fill {
background: var(--reading);
}
.gardening .fill {
background: var(--gardening);
}
.actions {
display: flex;
gap: 7px;
}
button {
flex: 1;
min-height: 34px;
border: 0;
border-radius: 10px;
color: white;
cursor: pointer;
font: inherit;
font-size: 11px;
font-weight: 800;
transition:
transform 150ms ease,
opacity 150ms ease,
filter 150ms ease;
}
button:hover:not(:disabled) {
transform: translateY(-1px);
filter: brightness(1.05);
}
button:active:not(:disabled) {
transform: translateY(1px);
}
button:focus-visible {
outline: 3px solid rgba(201, 154, 82, 0.5);
outline-offset: 2px;
}
button:disabled {
cursor: not-allowed;
opacity: 0.3;
}
.reading button {
background: var(--reading);
}
.gardening button {
background: var(--gardening);
}
.opportunity {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 10px;
text-align: center;
}
.opportunity small {
color: #927d5f;
font-size: 8px;
font-weight: 850;
letter-spacing: 0.17em;
}
.exchange {
margin: 3px 0;
color: #b1874f;
font-family: Georgia, "Times New Roman", serif;
font-size: 28px;
line-height: 1;
}
.opportunity strong {
font-family: Georgia, "Times New Roman", serif;
font-size: 17px;
font-weight: 500;
}
.opportunity p {
margin: 5px 0 0;
color: #786d60;
font-size: 9px;
line-height: 1.35;
}
.toast {
position: absolute;
top: 48%;
left: 50%;
padding: 12px 18px;
transform: translate(-50%, -50%) translateY(8px);
border-radius: 999px;
background: rgba(43, 39, 32, 0.88);
box-shadow: 0 12px 30px rgba(45, 35, 22, 0.2);
color: #fffaf0;
font-size: 12px;
font-weight: 700;
opacity: 0;
pointer-events: none;
transition:
opacity 180ms ease,
transform 180ms ease;
}
.toast.show {
transform: translate(-50%, -50%) translateY(0);
opacity: 1;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
@media (max-width: 760px) {
.title-panel {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
padding: 16px 18px;
}
h1 {
font-size: 30px;
}
.subtitle {
margin: 9px 0 11px;
font-size: 11px;
}
.balance-panel {
top: 160px;
right: 14px;
}
.metric {
min-width: 102px;
padding: 12px 14px;
}
.metric strong {
font-size: 26px;
}
.total-choice {
min-width: 66px;
}
.controls-panel {
bottom: 12px;
grid-template-columns: 1fr 1fr;
width: calc(100vw - 24px);
padding: 9px;
}
.opportunity {
grid-column: 1 / -1;
grid-row: 1;
min-height: 72px;
}
.reading-card {
grid-column: 1;
grid-row: 2;
}
.gardening-card {
grid-column: 2;
grid-row: 2;
}
.activity-card {
padding: 10px;
}
.card-name {
font-size: 9px;
}
.card-count {
font-size: 20px;
}
button {
padding: 0 5px;
font-size: 10px;
}
}
@media (max-height: 650px) and (min-width: 761px) {
.title-panel {
padding: 16px 20px;
}
.subtitle {
margin: 8px 0 10px;
}
.controls-panel {
bottom: 14px;
}
}
</style>
</head>
<body>
<canvas
id="scene"
aria-label="A tabletop opportunity cost dial split between reading and gardening"
></canvas>
<div class="hud">
<section class="panel title-panel">
<div class="eyebrow">A fixed day · two possible uses</div>
<h1>The Opportunity<br>Cost Dial</h1>
<p class="subtitle">
Twelve choice blocks share the same limited time. Give one activity
another block and the other activity must give one up.
</p>
<div class="hint-row">
<span class="hint">
<svg viewBox="0 0 24 24" fill="none" stroke-width="2">
<path d="M8 11V7a2 2 0 0 1 4 0v4"/>
<path d="M12 11V5a2 2 0 0 1 4 0v6"/>
<path d="M16 11V8a2 2 0 0 1 4 0v6c0 4-3 7-7 7h-1c-3 0-5-2-6-4l-2-4a2 2 0 0 1 3-2l2 2"/>
</svg>
Drag blocks across the line
</span>
<span class="hint">
<svg viewBox="0 0 24 24" fill="none" stroke-width="2">
<path d="M12 2v20M2 12h20"/>
<circle cx="12" cy="12" r="9"/>
</svg>
Drag the table to orbit
</span>
</div>
</section>
<section class="panel balance-panel" aria-label="Current allocation">
<div class="metric">
<div class="metric-label">
<span class="dot reading"></span>
Reading
</div>
<strong id="readingCount">6</strong>
</div>
<div class="total-choice">
<span>12</span>
total blocks
</div>
<div class="metric">
<div class="metric-label">
<span class="dot gardening"></span>
Gardening
</div>
<strong id="gardeningCount">6</strong>
</div>
</section>
<section class="panel controls-panel" aria-label="Allocation controls">
<article class="activity-card reading">
<div class="card-heading">
<span class="card-name">Reading</span>
<span class="card-count" id="readingCardCount">6 blocks</span>
</div>
<div class="track"><div class="fill" id="readingFill"></div></div>
<div class="actions">
<button id="addReading" type="button">+ Add</button>
<button id="removeReading" type="button">− Remove</button>
</div>
</article>
<article class="opportunity">
<small>THE TRADE</small>
<div class="exchange">↔</div>
<strong>1 block for 1 block</strong>
<p id="tradeText">
Every gardening block gained costs one reading block.
</p>
</article>
<article class="activity-card gardening">
<div class="card-heading">
<span class="card-name">Gardening</span>
<span class="card-count" id="gardeningCardCount">6 blocks</span>
</div>
<div class="track"><div class="fill" id="gardeningFill"></div></div>
<div class="actions">
<button id="addGardening" type="button">+ Add</button>
<button id="removeGardening" type="button">− Remove</button>
</div>
</article>
</section>
<div id="toast" class="toast" role="status" aria-live="polite"></div>
<div id="summary" class="sr-only" aria-live="polite"></div>
</div>
<noscript>This interactive visualization requires JavaScript.</noscript>
<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.getElementById("scene");
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.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e1d3);
scene.fog = new THREE.Fog(0xe9e1d3, 12, 25);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const BOARD_Y = 0.62;
const BLOCK_CENTER_Y = BOARD_Y + 0.155;
const TOTAL_BLOCKS = 12;
const colors = {
reading: 0x7066c7,
readingDark: 0x4d438e,
gardening: 0x609b68,
gardeningDark: 0x3d7048,
brass: 0xc59a54,
wood: 0x96613e,
woodDark: 0x6f422b
};
const standardMaterial = (options) =>
new THREE.MeshStandardMaterial({
flatShading: true,
...options
});
const floorMaterial = standardMaterial({
color: 0xd8cebd,
roughness: 1
});
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
floorMaterial
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.27;
floor.receiveShadow = true;
scene.add(floor);
// Low-poly table and pedestal.
const tableBase = new THREE.Mesh(
new THREE.CylinderGeometry(4.3, 4.48, 0.5, 12),
standardMaterial({
color: colors.woodDark,
roughness: 0.9
})
);
tableBase.position.y = 0;
tableBase.castShadow = true;
tableBase.receiveShadow = true;
scene.add(tableBase);
const tableTop = new THREE.Mesh(
new THREE.CylinderGeometry(4.08, 4.3, 0.18, 12),
standardMaterial({
color: colors.wood,
roughness: 0.82
})
);
tableTop.position.y = 0.34;
tableTop.castShadow = true;
tableTop.receiveShadow = true;
scene.add(tableTop);
// Dial foundation.
const dialBase = new THREE.Mesh(
new THREE.CylinderGeometry(2.86, 2.95, 0.18, 32),
standardMaterial({
color: 0x5f5143,
roughness: 0.86
})
);
dialBase.position.y = 0.51;
dialBase.castShadow = true;
dialBase.receiveShadow = true;
scene.add(dialBase);
// The two semicircular board sections.
const readingBoard = new THREE.Mesh(
new THREE.CircleGeometry(2.56, 32, Math.PI / 2, Math.PI),
standardMaterial({
color: 0xded9ef,
roughness: 0.94,
side: THREE.DoubleSide
})
);
readingBoard.rotation.x = -Math.PI / 2;
readingBoard.position.y = BOARD_Y;
readingBoard.receiveShadow = true;
scene.add(readingBoard);
const gardeningBoard = new THREE.Mesh(
new THREE.CircleGeometry(2.56, 32, -Math.PI / 2, Math.PI),
standardMaterial({
color: 0xd9e8d5,
roughness: 0.94,
side: THREE.DoubleSide
})
);
gardeningBoard.rotation.x = -Math.PI / 2;
gardeningBoard.position.y = BOARD_Y;
gardeningBoard.receiveShadow = true;
scene.add(gardeningBoard);
const brassMaterial = standardMaterial({
color: colors.brass,
metalness: 0.62,
roughness: 0.32
});
const outerRing = new THREE.Mesh(
new THREE.TorusGeometry(2.66, 0.055, 6, 48),
brassMaterial
);
outerRing.rotation.x = Math.PI / 2;
outerRing.position.y = BOARD_Y + 0.045;
outerRing.castShadow = true;
scene.add(outerRing);
// Twelve scale marks around the dial.
const tickGeometry = new THREE.BoxGeometry(0.045, 0.035, 0.18);
const readingTickMaterial = standardMaterial({
color: colors.readingDark,
roughness: 0.58
});
const gardeningTickMaterial = standardMaterial({
color: colors.gardeningDark,
roughness: 0.58
});
for (let i = 0; i < 12; i++) {
const angle = (i / 12) * Math.PI * 2;
const x = Math.cos(angle) * 2.48;
const z = Math.sin(angle) * 2.48;
const tick = new THREE.Mesh(
tickGeometry,
x < 0 ? readingTickMaterial : gardeningTickMaterial
);
tick.position.set(x, BOARD_Y + 0.048, z);
tick.rotation.y = Math.atan2(x, z);
tick.castShadow = true;
scene.add(tick);
}
// Brass divider and center pivot.
const divider = new THREE.Mesh(
new THREE.BoxGeometry(0.035, 0.045, 4.92),
brassMaterial
);
divider.position.y = BOARD_Y + 0.045;
divider.castShadow = true;
scene.add(divider);
const knobMaterial = standardMaterial({
color: colors.brass,
emissive: 0x9b5c1e,
emissiveIntensity: 0,
metalness: 0.65,
roughness: 0.3
});
const centerKnob = new THREE.Mesh(
new THREE.CylinderGeometry(0.16, 0.19, 0.11, 12),
knobMaterial
);
centerKnob.position.y = BOARD_Y + 0.085;
centerKnob.castShadow = true;
scene.add(centerKnob);
// Lighting.
scene.add(new THREE.HemisphereLight(0xfff6e5, 0x665344, 2.2));
const keyLight = new THREE.DirectionalLight(0xfff3d8, 3.4);
keyLight.position.set(-4.5, 8, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 22;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xbfd6ff, 1.35);
rimLight.position.set(5, 4, -6);
scene.add(rimLight);
function roundedRect(ctx, x, y, width, height, radius) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx.lineTo(x + width, y + height - radius);
ctx.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
ctx.lineTo(x + radius, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
}
function createLabel(text, color) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 128;
const ctx = labelCanvas.getContext("2d");
roundedRect(ctx, 6, 6, 500, 116, 28);
ctx.fillStyle = "rgba(255, 252, 245, 0.92)";
ctx.fill();
ctx.strokeStyle = color;
ctx.lineWidth = 6;
ctx.stroke();
ctx.fillStyle = color;
ctx.font = "800 43px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 64);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(1.68, 0.42, 1);
sprite.renderOrder = 20;
return sprite;
}
const readingLabel = createLabel("READING", "#51468f");
readingLabel.position.set(-2.48, 1.03, 0);
scene.add(readingLabel);
const gardeningLabel = createLabel("GARDENING", "#3f7448");
gardeningLabel.position.set(2.48, 1.03, 0);
scene.add(gardeningLabel);
// Shared geometries and materials for the decision blocks.
const blockGeometry = new THREE.BoxGeometry(0.52, 0.28, 0.46);
const trimGeometry = new THREE.BoxGeometry(0.55, 0.055, 0.49);
const topGeometry = new THREE.BoxGeometry(0.4, 0.025, 0.34);
const pageGeometry = new THREE.BoxGeometry(0.18, 0.025, 0.24);
const spineGeometry = new THREE.BoxGeometry(0.018, 0.03, 0.25);
const stemGeometry = new THREE.CylinderGeometry(0.024, 0.034, 0.17, 5);
const leafGeometry = new THREE.SphereGeometry(0.1, 6, 4);
const trimReadingMaterial = standardMaterial({
color: colors.readingDark,
roughness: 0.72
});
const trimGardeningMaterial = standardMaterial({
color: colors.gardeningDark,
roughness: 0.72
});
const topMaterial = standardMaterial({
color: 0xf3ecd9,
roughness: 0.78
});
const paperMaterial = standardMaterial({
color: 0xfffaf0,
roughness: 0.9
});
const stemMaterial = standardMaterial({
color: 0x315f37,
roughness: 0.85
});
const leafMaterial = standardMaterial({
color: 0x4b884d,
roughness: 0.85
});
const blocks = [];
const blockGroups = [];
const targets = Array.from(
{ length: TOTAL_BLOCKS },
() => new THREE.Vector3()
);
for (let i = 0; i < TOTAL_BLOCKS; i++) {
const group = new THREE.Group();
const bodyMaterial = standardMaterial({
color: colors.reading,
roughness: 0.72,
metalness: 0.015,
emissive: 0x17122e,
emissiveIntensity: 0
});
const body = new THREE.Mesh(blockGeometry, bodyMaterial);
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const trim = new THREE.Mesh(
trimGeometry,
i < 6 ? trimReadingMaterial : trimGardeningMaterial
);
trim.position.y = -0.125;
trim.castShadow = true;
group.add(trim);
const top = new THREE.Mesh(topGeometry, topMaterial);
top.position.y = 0.151;
top.receiveShadow = true;
group.add(top);
const readingIcon = new THREE.Group();
const leftPage = new THREE.Mesh(pageGeometry, paperMaterial);
leftPage.position.set(-0.09, 0.182, 0);
leftPage.rotation.y = 0.17;
const rightPage = new THREE.Mesh(pageGeometry, paperMaterial);
rightPage.position.set(0.09, 0.182, 0);
rightPage.rotation.y = -0.17;
const spine = new THREE.Mesh(spineGeometry, trimReadingMaterial);
spine.position.set(0, 0.19, 0);
readingIcon.add(leftPage, rightPage, spine);
group.add(readingIcon);
const gardeningIcon = new THREE.Group();
const stem = new THREE.Mesh(stemGeometry, stemMaterial);
stem.position.y = 0.24;
stem.castShadow = true;
const leftLeaf = new THREE.Mesh(leafGeometry, leafMaterial);
leftLeaf.position.set(-0.055, 0.25, 0);
leftLeaf.scale.set(0.8, 0.3, 1.45);
leftLeaf.rotation.z = 0.55;
const rightLeaf = new THREE.Mesh(leafGeometry, leafMaterial);
rightLeaf.position.set(0.055, 0.255, 0);
rightLeaf.scale.set(0.8, 0.3, 1.45);
rightLeaf.rotation.z = -0.55;
gardeningIcon.add(stem, leftLeaf, rightLeaf);
group.add(gardeningIcon);
const block = {
index: i,
group,
body,
bodyMaterial,
trim,
readingIcon,
gardeningIcon,
side: i < 6 ? "reading" : "gardening",
hovered: false,
selected: false
};
group.userData.blockIndex = i;
group.traverse((object) => {
object.userData.blockIndex = i;
});
blocks.push(block);
blockGroups.push(group);
scene.add(group);
}
function updateBlockAppearance(block) {
const reading = block.side === "reading";
block.bodyMaterial.color.setHex(
reading ? colors.reading : colors.gardening
);
block.bodyMaterial.emissive.setHex(
reading ? 0x17122e : 0x092812
);
block.bodyMaterial.emissiveIntensity = block.selected
? 0.52
: block.hovered
? 0.24
: 0;
block.trim.material = reading
? trimReadingMaterial
: trimGardeningMaterial;
block.readingIcon.visible = reading;
block.gardeningIcon.visible = !reading;
}
blocks.forEach(updateBlockAppearance);
function layoutPositions(count) {
if (count === 0) return [];
const columns = count <= 6 ? 2 : 3;
const rows = Math.ceil(count / columns);
const positions = [];
for (let row = 0; row < rows; row++) {
const itemsInRow = Math.min(columns, count - row * columns);
let distances;
if (itemsInRow === 1) {
distances = [1.02];
} else if (itemsInRow === 2) {
distances = [0.68, 1.36];
} else {
distances = [0.68, 1.36, 2.04];
}
const z = (row - (rows - 1) / 2) * 0.72;
distances.forEach((distance) => {
positions.push({ distance, z });
});
}
return positions;
}
function computeLayout() {
const readingBlocks = blocks.filter((block) => block.side === "reading");
const gardeningBlocks = blocks.filter((block) => block.side === "gardening");
const readingPositions = layoutPositions(readingBlocks.length);
const gardeningPositions = layoutPositions(gardeningBlocks.length);
readingBlocks.forEach((block, index) => {
const position = readingPositions[index];
targets[block.index].set(
-position.distance,
BLOCK_CENTER_Y,
position.z
);
});
gardeningBlocks.forEach((block, index) => {
const position = gardeningPositions[index];
targets[block.index].set(
position.distance,
BLOCK_CENTER_Y,
position.z
);
});
}
computeLayout();
blocks.forEach((block) => {
block.group.position.copy(targets[block.index]);
});
const readingCountElement = document.getElementById("readingCount");
const gardeningCountElement = document.getElementById("gardeningCount");
const readingCardCount = document.getElementById("readingCardCount");
const gardeningCardCount = document.getElementById("gardeningCardCount");
const readingFill = document.getElementById("readingFill");
const gardeningFill = document.getElementById("gardeningFill");
const addReadingButton = document.getElementById("addReading");
const removeReadingButton = document.getElementById("removeReading");
const addGardeningButton = document.getElementById("addGardening");
const removeGardeningButton = document.getElementById("removeGardening");
const tradeText = document.getElementById("tradeText");
const toast = document.getElementById("toast");
const summary = document.getElementById("summary");
let toastTimer;
let knobPulse = 0;
function counts() {
return {
reading: blocks.filter((block) => block.side === "reading").length,
gardening: blocks.filter((block) => block.side === "gardening").length
};
}
function updateUI() {
const allocation = counts();
readingCountElement.textContent = allocation.reading;
gardeningCountElement.textContent = allocation.gardening;
readingCardCount.textContent = `${allocation.reading} blocks`;
gardeningCardCount.textContent = `${allocation.gardening} blocks`;
readingFill.style.width = `${(allocation.reading / TOTAL_BLOCKS) * 100}%`;
gardeningFill.style.width = `${(allocation.gardening / TOTAL_BLOCKS) * 100}%`;
addReadingButton.disabled = allocation.gardening === 0;
removeReadingButton.disabled = allocation.reading === 0;
addGardeningButton.disabled = allocation.reading === 0;
removeGardeningButton.disabled = allocation.gardening === 0;
tradeText.textContent =
`Every gardening block gained costs one reading block.`;
summary.textContent =
`${allocation.reading} blocks assigned to reading and ` +
`${allocation.gardening} blocks assigned to gardening. ` +
`There are always 12 blocks in total.`;
}
function announce(message) {
toast.textContent = message;
toast.classList.remove("show");
void toast.offsetWidth;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
toast.classList.remove("show");
}, 2200);
}
function capitalize(text) {
return text.charAt(0).toUpperCase() + text.slice(1);
}
function transferBlock(fromSide, toSide) {
const sourceBlocks = blocks.filter((block) => block.side === fromSide);
if (sourceBlocks.length === 0) return;
// Moving one block necessarily removes one from the opposite activity.
const block = sourceBlocks[sourceBlocks.length - 1];
block.side = toSide;
updateBlockAppearance(block);
computeLayout();
updateUI();
knobPulse = 1;
announce(
`${capitalize(toSide)} gained 1 block — ${fromSide} gave up 1.`
);
}
addReadingButton.addEventListener("click", () => {
transferBlock("gardening", "reading");
});
removeReadingButton.addEventListener("click", () => {
transferBlock("reading", "gardening");
});
addGardeningButton.addEventListener("click", () => {
transferBlock("reading", "gardening");
});
removeGardeningButton.addEventListener("click", () => {
transferBlock("gardening", "reading");
});
updateUI();
// Custom camera controls, implemented without external addons.
const cameraState = {
theta: 0.56,
targetTheta: 0.56,
phi: 0.78,
targetPhi: 0.78,
radius: 8.35,
targetRadius: 8.35,
target: new THREE.Vector3(0, 0.72, 0)
};
function updateCamera(dt) {
const damping = 1 - Math.exp(-dt * 8);
cameraState.theta +=
(cameraState.targetTheta - cameraState.theta) * damping;
cameraState.phi +=
(cameraState.targetPhi - cameraState.phi) * damping;
cameraState.radius +=
(cameraState.targetRadius - cameraState.radius) * damping;
const sinPhi = Math.sin(cameraState.phi);
const cosPhi = Math.cos(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * cosPhi,
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.targetTheta = 0.56;
cameraState.targetPhi = 0.78;
cameraState.targetRadius = 8.35;
}
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius + event.deltaY * 0.005,
5.5,
11.5
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
// Pointer interactions: block dragging and table orbiting.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(
new THREE.Vector3(0, 1, 0),
-BLOCK_CENTER_Y
);
const planeHit = new THREE.Vector3();
let dragState = null;
let orbitState = null;
let hoveredBlock = null;
function setPointer(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 findBlock(intersections) {
if (!intersections.length) return null;
let object = intersections[0].object;
while (object) {
if (
Number.isInteger(object.userData.blockIndex) &&
blocks[object.userData.blockIndex]
) {
return blocks[object.userData.blockIndex];
}
object = object.parent;
}
return null;
}
function setHovered(block) {
if (hoveredBlock === block) return;
if (hoveredBlock) {
hoveredBlock.hovered = false;
updateBlockAppearance(hoveredBlock);
}
hoveredBlock = block;
if (hoveredBlock) {
hoveredBlock.hovered = true;
updateBlockAppearance(hoveredBlock);
}
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
setPointer(event);
const intersections = raycaster.intersectObjects(blockGroups, true);
const block = findBlock(intersections);
if (block) {
blocks.forEach((item) => {
item.selected = item === block;
updateBlockAppearance(item);
});
dragState = {
block,
pointerId: event.pointerId,
startClientX: event.clientX,
startClientY: event.clientY,
moved: false
};
canvas.style.cursor = "grabbing";
canvas.setPointerCapture(event.pointerId);
event.preventDefault();
} else {
orbitState = {
pointerId: event.pointerId,
x: event.clientX,
y: event.clientY
};
setHovered(null);
canvas.style.cursor = "grabbing";
canvas.setPointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointermove", (event) => {
if (dragState && event.pointerId === dragState.pointerId) {
setPointer(event);
if (raycaster.ray.intersectPlane(dragPlane, planeHit)) {
let x = planeHit.x;
let z = planeHit.z;
// Keep the dragged block on the tabletop.
const radius = Math.hypot(x, z);
if (radius > 2.3) {
x = (x / radius) * 2.3;
z = (z / radius) * 2.3;
}
dragState.block.group.position.set(
x,
BLOCK_CENTER_Y + 0.29,
z
);
dragState.moved =
Math.hypot(
event.clientX - dragState.startClientX,
event.clientY - dragState.startClientY
) > 4;
}
event.preventDefault();
return;
}
if (orbitState && event.pointerId === orbitState.pointerId) {
const deltaX = event.clientX - orbitState.x;
const deltaY = event.clientY - orbitState.y;
cameraState.targetTheta -= deltaX * 0.006;
cameraState.targetPhi = THREE.MathUtils.clamp(
cameraState.targetPhi - deltaY * 0.005,
0.36,
1.28
);
orbitState.x = event.clientX;
orbitState.y = event.clientY;
event.preventDefault();
return;
}
if (!dragState && !orbitState) {
setPointer(event);
const intersections = raycaster.intersectObjects(blockGroups, true);
const block = findBlock(intersections);
setHovered(block);
canvas.style.cursor = block ? "grab" : "default";
}
});
function finishPointer(event) {
if (dragState && event.pointerId === dragState.pointerId) {
setPointer(event);
if (raycaster.ray.intersectPlane(dragPlane, planeHit)) {
const destinationSide =
planeHit.x < 0 ? "reading" : "gardening";
if (
Math.abs(planeHit.x) > 0.18 &&
destinationSide !== dragState.block.side
) {
const oldSide = dragState.block.side;
dragState.block.side = destinationSide;
updateBlockAppearance(dragState.block);
computeLayout();
updateUI();
knobPulse = 1;
announce(
`${capitalize(destinationSide)} gained 1 block — ` +
`${oldSide} gave up 1.`
);
} else {
computeLayout();
}
}
dragState.block.selected = false;
updateBlockAppearance(dragState.block);
dragState = null;
}
if (orbitState && event.pointerId === orbitState.pointerId) {
orbitState = null;
}
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
canvas.style.cursor = hoveredBlock ? "grab" : "default";
}
canvas.addEventListener("pointerup", finishPointer);
canvas.addEventListener("pointercancel", finishPointer);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
const clock = new THREE.Clock();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const damping = 1 - Math.exp(-dt * 10);
updateCamera(dt);
blocks.forEach((block) => {
const isDragged = dragState?.block === block;
const desiredScale = isDragged ? 1.09 : 1;
if (!isDragged) {
block.group.position.lerp(targets[block.index], damping);
block.group.rotation.y +=
(0 - block.group.rotation.y) * damping;
}
const currentScale = block.group.scale.x;
const nextScale =
currentScale + (desiredScale - currentScale) * damping;
block.group.scale.setScalar(nextScale);
});
if (knobPulse > 0) {
knobPulse = Math.max(0, knobPulse - dt * 1.7);
knobMaterial.emissiveIntensity = knobPulse * 0.85;
} else {
knobMaterial.emissiveIntensity = 0;
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```