Prompt specification
Build or customize the Three.js 3D scene "Balance weights at an agora stall" 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: Balance weights at an agora stall
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/balance-weights-at-an-agora-stall-x2269
Scene Description Prompt:
Please use Three.js from cdnjs for a voxel scene based on this idea: Balance weights at an agora stall. Display a simple balance, unequal piles of weights, and a pottery vessel on a stall counter. Keep the weighing demonstration separate from claims about exact local standards. Return one self-contained HTML file.
Working Three.js Starter (balance-weights-at-an-agora-stall-x2269):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>The Merchant's Scale — Voxel Agora</title>
<style>
:root {
color-scheme: light;
--ink: #2f241d;
--paper: rgba(250, 244, 226, 0.93);
--paper-solid: #f7efdc;
--terracotta: #a64f36;
--brass: #b8832f;
--teal: #376c68;
--line: rgba(73, 50, 35, 0.18);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d9c7a6;
font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas.dragging {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 43%, transparent 48%, rgba(55, 35, 23, 0.11) 100%),
linear-gradient(to bottom, rgba(255, 245, 221, 0.08), transparent 35%);
}
header {
position: fixed;
top: 24px;
left: 26px;
z-index: 5;
max-width: 470px;
pointer-events: none;
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.28);
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
padding: 6px 9px;
border: 1px solid rgba(73, 50, 35, 0.18);
border-radius: 999px;
background: rgba(250, 244, 226, 0.72);
color: #654532;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
backdrop-filter: blur(8px);
}
.eyebrow::before {
content: "";
width: 7px;
height: 7px;
border-radius: 2px;
background: var(--terracotta);
box-shadow: 3px 3px 0 #d89967;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(32px, 4vw, 58px);
font-weight: 600;
line-height: 0.96;
letter-spacing: -0.035em;
}
header p {
max-width: 410px;
margin: 10px 0 0;
color: rgba(47, 36, 29, 0.72);
font-size: 13px;
line-height: 1.5;
}
.toolbar {
position: fixed;
z-index: 6;
top: 24px;
right: 24px;
display: flex;
gap: 8px;
padding: 6px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(250, 244, 226, 0.82);
box-shadow: 0 12px 30px rgba(65, 43, 28, 0.12);
backdrop-filter: blur(12px);
}
button {
appearance: none;
border: 0;
border-radius: 9px;
padding: 9px 12px;
background: transparent;
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition: background 150ms ease, transform 150ms ease;
}
button:hover {
background: rgba(166, 79, 54, 0.1);
}
button:active {
transform: translateY(1px);
}
button.primary {
background: var(--terracotta);
color: #fff8e8;
}
button.primary:hover {
background: #913f2c;
}
.story {
position: fixed;
z-index: 5;
left: 24px;
bottom: 24px;
width: min(390px, calc(100vw - 48px));
overflow: hidden;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--paper);
box-shadow: 0 18px 45px rgba(65, 43, 28, 0.16);
backdrop-filter: blur(14px);
}
.story-main {
padding: 18px 19px 16px;
}
.story h2 {
margin: 0 0 7px;
font-family: Georgia, "Times New Roman", serif;
font-size: 22px;
font-weight: 600;
}
.story p {
margin: 0;
color: rgba(47, 36, 29, 0.73);
font-size: 13px;
line-height: 1.55;
}
.story-note {
display: grid;
grid-template-columns: auto 1fr;
gap: 10px;
padding: 13px 18px 14px;
border-top: 1px solid var(--line);
background: rgba(55, 108, 104, 0.08);
}
.story-note .mark {
display: grid;
place-items: center;
width: 25px;
height: 25px;
border-radius: 7px;
background: var(--teal);
color: #f8f1dc;
font-size: 12px;
font-weight: 900;
}
.story-note strong {
display: block;
margin-bottom: 2px;
color: #315c59;
font-size: 11px;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.story-note p {
font-size: 11.5px;
line-height: 1.45;
}
.hint {
position: fixed;
z-index: 4;
right: 25px;
bottom: 24px;
padding: 9px 12px;
border: 1px solid rgba(73, 50, 35, 0.13);
border-radius: 999px;
background: rgba(250, 244, 226, 0.72);
color: rgba(47, 36, 29, 0.67);
font-size: 11px;
pointer-events: none;
backdrop-filter: blur(8px);
}
.annotation {
position: fixed;
z-index: 3;
display: flex;
align-items: center;
gap: 7px;
padding: 6px 9px;
border: 1px solid rgba(73, 50, 35, 0.18);
border-radius: 999px;
background: rgba(250, 244, 226, 0.88);
box-shadow: 0 6px 16px rgba(65, 43, 28, 0.1);
color: #49362a;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.04em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -100%);
transition: opacity 120ms ease;
backdrop-filter: blur(7px);
}
.annotation::before {
content: "";
width: 7px;
height: 7px;
border-radius: 2px;
background: var(--dot, var(--brass));
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #d9c7a6;
color: #644431;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 720px) {
header {
top: 16px;
left: 16px;
max-width: calc(100vw - 32px);
}
h1 {
max-width: 270px;
font-size: 36px;
}
header p {
display: none;
}
.toolbar {
top: auto;
right: 12px;
bottom: 185px;
flex-direction: column;
}
button {
font-size: 11px;
}
.story {
left: 12px;
bottom: 12px;
width: calc(100vw - 24px);
}
.story-main {
padding: 13px 15px 12px;
}
.story h2 {
font-size: 18px;
}
.story p {
font-size: 11.5px;
}
.story-note {
padding: 10px 14px 12px;
}
.story-note p {
font-size: 10.5px;
}
.hint,
.annotation {
display: none;
}
}
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
scroll-behavior: auto !important;
transition-duration: 0.01ms !important;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive voxel agora stall"></main>
<div class="vignette" aria-hidden="true"></div>
<header>
<div class="eyebrow">Voxel field notes · Agora</div>
<h1>The Merchant’s Scale</h1>
<p>A compact study of balance, unequal weight, and a hand-built clay vessel.</p>
</header>
<nav class="toolbar" aria-label="Scene controls">
<button id="replay" class="primary" type="button">Replay weighing</button>
<button id="reset" type="button">Reset view</button>
</nav>
<section class="story" aria-label="Scene explanation">
<div class="story-main">
<h2>The weighing demonstration</h2>
<p>
Two separate stacks of weights are unequal, and the balance carries
several weights on one pan and a single weight on the other. Its beam
therefore tips toward the heavier side.
</p>
</div>
<div class="story-note">
<span class="mark" aria-hidden="true">i</span>
<div>
<strong>Separate context note</strong>
<p>
This is an illustrative voxel mechanism, not a calibrated mass.
The scene does not identify or claim any exact local, legal, or
period-specific weight standard.
</p>
</div>
</div>
</section>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<div id="label-balance" class="annotation" style="--dot:#b8832f">Beam balance</div>
<div id="label-weights" class="annotation" style="--dot:#6d5540">Unequal piles</div>
<div id="label-vessel" class="annotation" style="--dot:#b85f38">Clay vessel</div>
<div id="loading">Building the agora…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const mount = document.querySelector("#scene");
const reducedMotion = matchMedia("(prefers-reduced-motion: reduce)").matches;
const renderer = new THREE.WebGLRenderer({ antialias: true });
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.08;
mount.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd9c7a6);
scene.fog = new THREE.Fog(0xd9c7a6, 20, 38);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
80
);
const target = new THREE.Vector3(0, 2.55, 0.1);
let cameraTheta = 0.48;
let cameraPhi = 1.18;
let cameraRadius = window.innerWidth < 720 ? 18 : 15;
let resettingView = false;
function updateCamera() {
camera.position.set(
target.x + cameraRadius * Math.sin(cameraPhi) * Math.sin(cameraTheta),
target.y + cameraRadius * Math.cos(cameraPhi),
target.z + cameraRadius * Math.sin(cameraPhi) * Math.cos(cameraTheta)
);
camera.lookAt(target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xfff2d4, 0x68513c, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe2ad, 3.2);
sun.position.set(8, 13, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -14;
sun.shadow.camera.right = 14;
sun.shadow.camera.top = 14;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.00045;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xaac6c1, 0.55);
fill.position.set(-7, 6, -4);
scene.add(fill);
const materialCache = new Map();
function getMaterial(color) {
if (color && color.isMaterial) return color;
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.88,
metalness: 0.02,
flatShading: true
})
);
}
return materialCache.get(color);
}
function box(parent, sx, sy, sz, x, y, z, color, options = {}) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(sx, sy, sz),
options.material || getMaterial(color)
);
mesh.position.set(x, y, z);
if (options.rotation) mesh.rotation.set(...options.rotation);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = options.receiveShadow !== false;
parent.add(mesh);
return mesh;
}
function group(parent, x = 0, y = 0, z = 0) {
const g = new THREE.Group();
g.position.set(x, y, z);
parent.add(g);
return g;
}
// Seeded random values keep the scene stable between reloads.
let seed = 8147;
function random() {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
}
// ---------------------------------------------------------------------
// Ground: individual voxel pavers.
// ---------------------------------------------------------------------
const ground = group(scene);
box(ground, 24, 0.35, 20, 0, -0.31, 0, 0x9e8666);
const tileGeometry = new THREE.BoxGeometry(1.31, 0.12, 1.31);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.96
});
const columns = 17;
const rows = 14;
const tileCount = columns * rows;
const tiles = new THREE.InstancedMesh(tileGeometry, tileMaterial, tileCount);
tiles.receiveShadow = true;
const dummy = new THREE.Object3D();
const tileColor = new THREE.Color();
let tileIndex = 0;
for (let z = 0; z < rows; z++) {
for (let x = 0; x < columns; x++) {
dummy.position.set(
(x - (columns - 1) / 2) * 1.34,
-0.045 + random() * 0.015,
(z - (rows - 1) / 2) * 1.34
);
dummy.rotation.y = (random() - 0.5) * 0.025;
dummy.updateMatrix();
tiles.setMatrixAt(tileIndex, dummy.matrix);
const hue = 0.095 + random() * 0.012;
const sat = 0.24 + random() * 0.08;
const light = 0.55 + random() * 0.09;
tileColor.setHSL(hue, sat, light);
tiles.setColorAt(tileIndex, tileColor);
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
if (tiles.instanceColor) tiles.instanceColor.needsUpdate = true;
ground.add(tiles);
// ---------------------------------------------------------------------
// Background agora architecture.
// ---------------------------------------------------------------------
const architecture = group(scene);
function building(x, width, height, depth, color, roofColor) {
const g = group(architecture, x, 0, -7.3);
box(g, width, height, depth, 0, height / 2, 0, color);
box(g, width + 0.25, 0.24, depth + 0.25, 0, height + 0.08, 0, roofColor);
const windowCount = Math.max(1, Math.floor(width / 2.1));
for (let i = 0; i < windowCount; i++) {
const wx = -width / 2 + (i + 0.5) * (width / windowCount);
const wy = Math.min(height - 1.15, 3.45);
box(g, 0.56, 0.84, 0.09, wx, wy, depth / 2 + 0.051, 0x4b4035);
box(g, 0.08, 0.87, 0.12, wx, wy, depth / 2 + 0.065, 0x9e7952);
}
const doorWidth = Math.min(1.25, width * 0.25);
box(
g,
doorWidth,
1.85,
0.1,
0,
0.93,
depth / 2 + 0.055,
0x574638
);
box(
g,
doorWidth + 0.22,
0.17,
0.15,
0,
1.9,
depth / 2 + 0.07,
0xbca27b
);
return g;
}
building(-6.5, 5.2, 5.1, 2.6, 0xbca17d, 0x8b6449);
building(-0.2, 7.7, 6.2, 2.9, 0xd0b88e, 0x9c7353);
building(7.1, 5.4, 5.4, 2.7, 0xc4a980, 0x8f654a);
// Small stepped roof blocks give the skyline a voxel silhouette.
for (let i = 0; i < 8; i++) {
box(
architecture,
0.74,
0.28,
2.9,
-3.0 + i * 0.84,
6.55,
-7.3,
i % 2 ? 0x9c7353 : 0xa87c58
);
}
// Distant blocky clouds.
function cloud(x, y, z, scale) {
const g = group(scene, x, y, z);
box(g, 2.1 * scale, 0.65 * scale, 0.7 * scale, 0, 0, 0, 0xf1e6cb, { castShadow: false });
box(g, 1.2 * scale, 0.9 * scale, 0.72 * scale, -0.7 * scale, 0.1 * scale, 0, 0xf1e6cb, { castShadow: false });
box(g, 1.3 * scale, 0.8 * scale, 0.72 * scale, 0.75 * scale, 0.08 * scale, 0, 0xf1e6cb, { castShadow: false });
box(g, 1.5 * scale, 0.45 * scale, 0.7 * scale, 0.15 * scale, 0.4 * scale, 0, 0xf7edd6, { castShadow: false });
}
cloud(-7, 9, -12, 1.2);
cloud(5, 10.2, -14, 1.45);
// ---------------------------------------------------------------------
// Stall and counter.
// ---------------------------------------------------------------------
const stall = group(scene);
// Counter top.
box(stall, 9.5, 0.36, 3.45, 0, 2.15, 0.2, 0x8b5735);
box(stall, 9.15, 0.16, 3.12, 0, 1.91, 0.2, 0x603a28);
box(stall, 9.35, 0.09, 0.12, 0, 2.27, 1.86, 0xb47748);
// Front apron.
box(stall, 9.12, 1.55, 0.18, 0, 1.15, 1.75, 0x74472f);
for (let i = 0; i < 11; i++) {
box(stall, 0.08, 1.48, 0.22, -4.25 + i * 0.85, 1.15, 1.82, 0x9d6840);
}
// Counter legs.
for (const x of [-4.25, 4.25]) {
for (const z of [-1.15, 1.42]) {
box(stall, 0.32, 2.0, 0.32, x, 1.0, z, 0x5e3928);
}
}
// Stall posts.
for (const x of [-4.58, 4.58]) {
box(stall, 0.23, 5.75, 0.23, x, 2.87, 1.48, 0x68402b);
box(stall, 0.23, 5.55, 0.23, x, 2.78, -1.42, 0x68402b);
box(stall, 0.58, 0.25, 0.58, x, 0.13, 1.48, 0x775038);
}
// Canopy frame.
box(stall, 9.55, 0.22, 0.25, 0, 5.67, 1.45, 0x5d3828);
box(stall, 9.55, 0.22, 0.25, 0, 5.67, -1.4, 0x5d3828);
// Alternating voxel awning slats.
for (let i = 0; i < 14; i++) {
const x = -4.62 + i * 0.71;
box(stall, 0.69, 0.17, 3.35, x, 5.83, 0.02, i % 2 ? 0xe3cfa8 : 0xa64736);
box(stall, 0.69, 0.34, 0.13, x, 5.52, 1.67, i % 2 ? 0xd4bd94 : 0x8f3d30);
}
// Small voxel sign on the counter front.
const sign = group(stall, 0, 1.18, 1.88);
box(sign, 2.15, 0.88, 0.12, 0, 0, 0, 0x493126);
box(sign, 0.10, 0.49, 0.08, 0, 0.04, 0.08, 0xd0a149);
box(sign, 0.82, 0.10, 0.08, -0.25, 0.23, 0.08, 0xd0a149);
box(sign, 0.20, 0.08, 0.08, -0.59, 0.12, 0.08, 0xd0a149);
box(sign, 0.20, 0.08, 0.08, 0.09, 0.12, 0.08, 0xd0a149);
// ---------------------------------------------------------------------
// Merchant behind the stall.
// ---------------------------------------------------------------------
const merchant = group(scene, 3.78, 0, -1.02);
box(merchant, 1.02, 1.38, 0.72, 0, 2.68, 0, 0x3e706c);
box(merchant, 1.18, 0.26, 0.84, 0, 2.05, 0, 0x315c59);
box(merchant, 0.57, 0.58, 0.57, 0, 3.73, 0, 0xc99168);
box(merchant, 0.64, 0.24, 0.63, 0, 4.04, 0, 0x4b3329);
box(merchant, 0.18, 0.18, 0.07, -0.15, 3.78, 0.31, 0x403129);
box(merchant, 0.18, 0.18, 0.07, 0.15, 3.78, 0.31, 0x403129);
box(merchant, 0.24, 0.92, 0.28, -0.65, 2.76, 0.04, 0x477a74);
box(merchant, 0.24, 0.92, 0.28, 0.65, 2.76, 0.04, 0x477a74);
box(merchant, 0.25, 0.18, 0.28, -0.68, 2.23, 0.24, 0xc99168);
box(merchant, 0.25, 0.18, 0.28, 0.68, 2.23, 0.24, 0xc99168);
// ---------------------------------------------------------------------
// Weights and the balance.
// ---------------------------------------------------------------------
function addVoxelWeight(parent, x, y, z, scale = 1, color = 0x8a6833) {
const g = group(parent, x, y, z);
box(g, 0.42 * scale, 0.18 * scale, 0.42 * scale, 0, 0.09 * scale, 0, color);
box(g, 0.23 * scale, 0.16 * scale, 0.23 * scale, 0, 0.26 * scale, 0, color);
box(g, 0.30 * scale, 0.035 * scale, 0.30 * scale, 0, 0.36 * scale, 0, 0xb99958);
return 0.39 * scale;
}
// Two deliberately unequal piles on the counter.
const counterWeights = group(scene, 0, 2.34, 0.72);
let tallY = 0;
for (let i = 0; i < 5; i++) {
tallY += addVoxelWeight(counterWeights, -3.75, tallY, 0, 0.78 + (i % 2) * 0.08);
}
let shortY = 0;
for (let i = 0; i < 2; i++) {
shortY += addVoxelWeight(counterWeights, -3.08, shortY, 0.03, 0.82);
}
const balance = group(scene, -0.42, 2.34, 0.13);
// Base and upright.
box(balance, 1.3, 0.18, 0.95, 0, 0.09, 0, 0x59432f);
box(balance, 1.03, 0.13, 0.75, 0, 0.23, 0, 0x9f7644);
box(balance, 0.22, 2.34, 0.24, 0, 1.39, 0, 0x6a482f);
box(balance, 0.42, 0.22, 0.4, 0, 2.43, 0, 0xb28a43);
const beamPivot = group(balance, 0, 2.57, 0);
box(beamPivot, 4.48, 0.16, 0.2, 0, 0, 0, 0x64432d);
box(beamPivot, 4.55, 0.065, 0.23, 0, 0.035, 0, 0xa27643);
box(beamPivot, 0.22, 0.28, 0.28, 2.18, 0, 0, 0xc3953e);
box(beamPivot, 0.22, 0.28, 0.28, -2.18, 0, 0, 0xc3953e);
box(beamPivot, 0.34, 0.38, 0.34, 0, 0.02, 0, 0xd2a74e);
function makePan() {
const pan = group(balance);
box(pan, 1.52, 0.12, 1.02, 0, 0, 0, 0x84633a);
box(pan, 1.58, 0.18, 0.10, 0, 0.09, 0.48, 0xb08a46);
box(pan, 1.58, 0.18, 0.10, 0, 0.09, -0.48, 0xb08a46);
box(pan, 0.10, 0.18, 0.91, 0.74, 0.09, 0, 0xb08a46);
box(pan, 0.10, 0.18, 0.91, -0.74, 0.09, 0, 0xb08a46);
return pan;
}
const leftPan = makePan();
const rightPan = makePan();
// One weight on the left pan.
addVoxelWeight(leftPan, 0, 0.08, 0, 1.0, 0x8f6b35);
// A stack of three weights on the right pan.
let panWeightY = 0.08;
for (let i = 0; i < 3; i++) {
panWeightY += addVoxelWeight(rightPan, 0, panWeightY, 0, 0.96, 0x7d5b2d);
}
function makeStrings(count) {
return Array.from({ length: count }, () => {
const mesh = box(balance, 0.035, 1, 0.035, 0, 0, 0, 0x584c3e);
return mesh;
});
}
const leftStrings = makeStrings(4);
const rightStrings = makeStrings(4);
const stringOffsets = [
[-0.58, 0.37],
[0.58, 0.37],
[-0.58, -0.37],
[0.58, -0.37]
];
function setString(mesh, x, topY, bottomY) {
const length = Math.max(0.05, topY - bottomY);
mesh.position.set(x, (topY + bottomY) / 2, 0);
mesh.scale.set(1, length, 1);
}
function updateBalance(angle) {
beamPivot.rotation.z = angle;
const pivotY = 2.57;
const arm = 2.18;
const cos = Math.cos(angle);
const sin = Math.sin(angle);
const rightX = arm * cos;
const rightY = pivotY + arm * sin;
const leftX = -arm * cos;
const leftY = pivotY - arm * sin;
const rightPanY = rightY - 1.35;
const leftPanY = leftY - 1.35;
rightPan.position.set(rightX, rightPanY, 0);
leftPan.position.set(leftX, leftPanY, 0);
stringOffsets.forEach(([dx, dz], i) => {
setString(rightStrings[i], rightX + dx, rightY - 0.08, rightPanY + 0.15);
setString(leftStrings[i], leftX + dx, leftY - 0.08, leftPanY + 0.15);
rightStrings[i].position.z = dz * 0.18;
leftStrings[i].position.z = dz * 0.18;
});
}
let balanceAngle = reducedMotion ? -0.17 : 0.055;
const settledAngle = -0.17;
updateBalance(balanceAngle);
// ---------------------------------------------------------------------
// A block-built pottery vessel.
// ---------------------------------------------------------------------
const vessel = group(scene, 3.02, 2.34, 0.35);
box(vessel, 0.90, 0.16, 0.72, 0, 0.08, 0, 0x87422f);
box(vessel, 1.38, 0.34, 1.10, 0, 0.30, 0, 0xb85f38);
box(vessel, 1.56, 0.34, 1.18, 0, 0.62, 0, 0xc56b40);
box(vessel, 1.48, 0.32, 1.10, 0, 0.94, 0, 0xb85f38);
box(vessel, 1.25, 0.28, 0.94, 0, 1.23, 0, 0xc56b40);
box(vessel, 0.65, 0.35, 0.62, 0, 1.50, 0, 0xad5234);
box(vessel, 0.88, 0.16, 0.82, 0, 1.73, 0, 0x87422f);
// Decorative dark band and a few irregular glaze pixels.
box(vessel, 1.61, 0.12, 1.21, 0, 0.64, 0, 0x7c3e2d);
box(vessel, 0.22, 0.17, 0.06, -0.42, 0.33, 0.59, 0xe3945b);
box(vessel, 0.15, 0.13, 0.06, -0.09, 0.38, 0.59, 0x7c3e2d);
box(vessel, 0.19, 0.14, 0.06, 0.34, 0.31, 0.59, 0xe3945b);
box(vessel, 0.58, 0.04, 0.52, 0, 1.82, 0, 0x3f2922);
// Square handles, assembled from voxels.
for (const side of [-1, 1]) {
box(vessel, 0.40, 0.18, 0.25, side * 0.83, 1.36, 0, 0x9e4c32);
box(vessel, 0.18, 0.56, 0.25, side * 1.00, 1.13, 0, 0x9e4c32);
box(vessel, 0.40, 0.18, 0.25, side * 0.83, 0.90, 0, 0x9e4c32);
}
// A small folded cloth and storage crate enrich the counter.
box(stall, 1.16, 0.055, 0.82, 4.08, 2.36, -0.78, 0x6e8e82, {
rotation: [0, -0.1, 0]
});
box(stall, 0.92, 0.54, 0.72, 4.15, 2.62, -1.0, 0x8e6039);
box(stall, 0.98, 0.08, 0.78, 4.15, 2.91, -1.0, 0xae7b48);
// ---------------------------------------------------------------------
// Projected HTML annotations without external renderer modules.
// ---------------------------------------------------------------------
const annotations = [
{
element: document.querySelector("#label-balance"),
point: new THREE.Vector3(-0.42, 5.18, 0.15)
},
{
element: document.querySelector("#label-weights"),
point: new THREE.Vector3(-3.42, 3.88, 0.8)
},
{
element: document.querySelector("#label-vessel"),
point: new THREE.Vector3(3.02, 4.52, 0.35)
}
];
const projected = new THREE.Vector3();
function updateAnnotations() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const annotation of annotations) {
projected.copy(annotation.point).project(camera);
const visible = projected.z > -1 && projected.z < 1;
annotation.element.style.opacity = visible ? "1" : "0";
annotation.element.style.left = `${(projected.x * 0.5 + 0.5) * width}px`;
annotation.element.style.top = `${(-projected.y * 0.5 + 0.5) * height}px`;
}
}
// ---------------------------------------------------------------------
// Direct camera controls: pointer orbit, wheel zoom, keyboard support.
// ---------------------------------------------------------------------
const canvas = renderer.domElement;
let dragging = false;
let activePointer = null;
let lastX = 0;
let lastY = 0;
function stopReset() {
resettingView = false;
}
canvas.addEventListener("pointerdown", (event) => {
if (activePointer !== null) return;
activePointer = event.pointerId;
dragging = true;
lastX = event.clientX;
lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
stopReset();
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging || event.pointerId !== activePointer) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(cameraPhi + dy * 0.005, 0.72, 1.43);
});
function endDrag(event) {
if (event.pointerId !== activePointer) return;
dragging = false;
activePointer = null;
canvas.classList.remove("dragging");
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
stopReset();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
8,
24
);
}, { passive: false });
canvas.addEventListener("dblclick", resetView);
window.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft":
cameraTheta += 0.08;
break;
case "ArrowRight":
cameraTheta -= 0.08;
break;
case "ArrowUp":
cameraPhi = Math.max(0.72, cameraPhi - 0.06);
break;
case "ArrowDown":
cameraPhi = Math.min(1.43, cameraPhi + 0.06);
break;
case "+":
case "=":
cameraRadius = Math.max(8, cameraRadius - 0.6);
break;
case "-":
cameraRadius = Math.min(24, cameraRadius + 0.6);
break;
case "r":
case "R":
resetView();
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
stopReset();
}
});
function resetView() {
resettingView = true;
cameraTheta = 0.48;
cameraPhi = 1.18;
cameraRadius = window.innerWidth < 720 ? 18 : 15;
target.set(0, 2.55, 0.1);
}
document.querySelector("#reset").addEventListener("click", resetView);
// ---------------------------------------------------------------------
// Demonstration replay.
// ---------------------------------------------------------------------
let replayStart = null;
let replayInitialAngle = balanceAngle;
document.querySelector("#replay").addEventListener("click", () => {
if (reducedMotion) {
balanceAngle = settledAngle;
updateBalance(balanceAngle);
return;
}
replayStart = performance.now();
replayInitialAngle = balanceAngle;
});
function smoothstep(t) {
return t * t * (3 - 2 * t);
}
function animateWeighing(now) {
if (replayStart !== null) {
const t = (now - replayStart) / 1000;
if (t < 0.42) {
balanceAngle = THREE.MathUtils.lerp(
replayInitialAngle,
0.055,
smoothstep(t / 0.42)
);
} else if (t < 1.15) {
balanceAngle = THREE.MathUtils.lerp(
0.055,
-0.19,
smoothstep((t - 0.42) / 0.73)
);
} else if (t < 1.85) {
balanceAngle = THREE.MathUtils.lerp(
-0.19,
-0.145,
smoothstep((t - 1.15) / 0.70)
);
} else {
balanceAngle = THREE.MathUtils.lerp(
-0.145,
settledAngle,
smoothstep(Math.min(1, (t - 1.85) / 0.65))
);
if (t > 2.5) replayStart = null;
}
} else if (!reducedMotion) {
balanceAngle = THREE.MathUtils.lerp(
balanceAngle,
settledAngle,
1 - Math.exp(-0.005)
);
}
updateBalance(balanceAngle);
}
// ---------------------------------------------------------------------
// Resize and render loop.
// ---------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
function animate(now) {
requestAnimationFrame(animate);
animateWeighing(now);
if (resettingView) {
const damping = 1 - Math.exp(-0.009);
cameraTheta = THREE.MathUtils.lerp(cameraTheta, 0.48, damping);
cameraPhi = THREE.MathUtils.lerp(cameraPhi, 1.18, damping);
cameraRadius = THREE.MathUtils.lerp(
cameraRadius,
window.innerWidth < 720 ? 18 : 15,
damping
);
if (
Math.abs(cameraTheta - 0.48) < 0.001 &&
Math.abs(cameraPhi - 1.18) < 0.001 &&
Math.abs(cameraRadius - (window.innerWidth < 720 ? 18 : 15)) < 0.01
) {
resettingView = false;
}
}
updateCamera();
updateAnnotations();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
const loading = document.querySelector("#loading");
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 400);
</script>
</body>
</html>
```