Prompt specification
Build or customize the Three.js 3D scene "Goods and Generic Currency Tokens" 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: Goods and Generic Currency Tokens
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/goods-and-generic-currency-tokens-x3161
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows currency tokens as a medium of exchange. Show unbranded tokens beside bread and cloth on separate trays. Distinguish quantities and unit prices without using real currencies or suggesting actual purchasing power. Stylized solid forms.
Working Three.js Starter (goods-and-generic-currency-tokens-x3161):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tokens Mediate the Exchange</title>
<style>
:root {
--paper: #eee9df;
--ink: #252723;
--muted: #6f7169;
--line: rgba(37, 39, 35, 0.14);
--card: rgba(255, 255, 255, 0.58);
--teal: #2f8079;
--teal-soft: #d8e8e3;
--bread: #c9854f;
--cloth: #526f78;
--shadow: 0 18px 50px rgba(67, 58, 43, 0.08);
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--paper);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow-x: hidden;
}
button {
font: inherit;
}
.app {
width: min(100%, 1600px);
min-height: 100vh;
margin: 0 auto;
display: flex;
flex-direction: column;
}
.header {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 40px;
padding: 34px 44px 22px;
}
.eyebrow {
margin-bottom: 12px;
color: var(--teal);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
max-width: 720px;
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(38px, 4.6vw, 72px);
font-weight: 400;
line-height: 0.96;
letter-spacing: -0.045em;
}
.intro {
max-width: 440px;
margin: 18px 0 0;
color: var(--muted);
font-size: 15px;
line-height: 1.65;
}
.legend {
flex: 0 0 auto;
display: grid;
gap: 10px;
min-width: 205px;
padding: 18px 20px;
border: 1px solid var(--line);
border-radius: 18px;
background: rgba(255, 255, 255, 0.26);
}
.legend-row {
display: flex;
align-items: center;
gap: 11px;
color: #555850;
font-size: 12px;
letter-spacing: 0.02em;
}
.swatch {
width: 14px;
height: 14px;
flex: 0 0 auto;
border-radius: 4px;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}
.swatch.goods {
background: var(--bread);
}
.swatch.tokens {
border-radius: 50%;
background: var(--teal);
}
.swatch.trays {
background: #d2c9ba;
}
.stage {
position: relative;
min-height: 460px;
height: clamp(460px, 57vh, 680px);
overflow: hidden;
border-top: 1px solid var(--line);
border-bottom: 1px solid var(--line);
isolation: isolate;
}
.stage canvas {
position: absolute;
inset: 0;
z-index: 0;
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
.stage canvas:active {
cursor: grabbing;
}
.label-layer {
position: absolute;
inset: 0;
z-index: 3;
pointer-events: none;
}
.scene-label {
position: absolute;
min-width: 92px;
padding: 9px 11px 10px;
border: 1px solid rgba(37, 39, 35, 0.12);
border-radius: 12px;
background: rgba(249, 247, 241, 0.88);
box-shadow: 0 8px 24px rgba(49, 44, 35, 0.08);
text-align: left;
transform: translate(-50%, -100%);
backdrop-filter: blur(8px);
transition: left 80ms linear, top 80ms linear;
}
.scene-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 13px;
background: rgba(37, 39, 35, 0.26);
}
.scene-label .label-type {
display: block;
margin-bottom: 3px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.scene-label strong {
display: block;
font-family: Georgia, "Times New Roman", serif;
font-size: 15px;
font-weight: 400;
line-height: 1.1;
}
.scene-label em {
display: block;
margin-top: 3px;
color: var(--teal);
font-size: 9px;
font-style: normal;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.scene-hint {
position: absolute;
z-index: 4;
left: 22px;
bottom: 18px;
color: rgba(37, 39, 35, 0.52);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
pointer-events: none;
}
.blank-note {
position: absolute;
z-index: 4;
right: 22px;
bottom: 18px;
color: rgba(37, 39, 35, 0.52);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
pointer-events: none;
}
.ledger {
display: grid;
grid-template-columns: minmax(280px, 1fr) minmax(280px, 1fr) minmax(260px, 0.82fr);
gap: 16px;
padding: 22px 44px 34px;
}
.card {
padding: 18px 19px 17px;
border: 1px solid var(--line);
border-radius: 19px;
background: var(--card);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
}
.card-title {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 15px;
font-family: Georgia, "Times New Roman", serif;
font-size: 20px;
}
.card-title::before {
content: "";
width: 10px;
height: 10px;
border-radius: 50%;
background: var(--card-color, var(--teal));
}
.metric {
display: grid;
grid-template-columns: minmax(120px, 1fr) auto;
align-items: center;
gap: 16px;
min-height: 46px;
border-top: 1px solid var(--line);
}
.metric-label {
font-size: 12px;
font-weight: 750;
}
.metric-label small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
font-weight: 600;
letter-spacing: 0.04em;
}
.stepper {
display: flex;
align-items: center;
gap: 5px;
padding: 4px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.58);
}
.stepper button {
width: 27px;
height: 27px;
padding: 0;
border: 0;
border-radius: 8px;
background: #f4f0e8;
color: var(--ink);
cursor: pointer;
font-size: 17px;
line-height: 1;
transition: background 150ms ease, transform 150ms ease;
}
.stepper button:hover:not(:disabled) {
background: var(--teal-soft);
transform: translateY(-1px);
}
.stepper button:disabled {
cursor: not-allowed;
opacity: 0.3;
}
.stepper output {
min-width: 28px;
text-align: center;
font-size: 13px;
font-weight: 800;
}
.total-row {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 14px;
padding-top: 13px;
border-top: 1px solid var(--line);
}
.total-label {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.total-value {
margin-top: 3px;
font-family: Georgia, "Times New Roman", serif;
font-size: 24px;
}
.mini-tokens {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 4px;
max-width: 105px;
}
.mini-token {
width: 11px;
height: 11px;
border-radius: 50%;
background: var(--teal);
box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.23);
}
.model-note {
display: flex;
flex-direction: column;
justify-content: space-between;
padding: 21px 22px;
}
.model-note .note-number {
color: var(--teal);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.model-note h2 {
margin: 17px 0 8px;
font-family: Georgia, "Times New Roman", serif;
font-size: 24px;
font-weight: 400;
line-height: 1.08;
}
.model-note p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.6;
}
.model-note footer {
margin-top: 22px;
padding-top: 13px;
border-top: 1px solid var(--line);
color: #52564f;
font-size: 10px;
font-weight: 750;
line-height: 1.5;
}
@media (max-width: 1050px) {
.header {
align-items: flex-start;
}
.legend {
display: none;
}
.ledger {
grid-template-columns: 1fr 1fr;
}
.model-note {
grid-column: 1 / -1;
min-height: 170px;
}
}
@media (max-width: 680px) {
.header {
padding: 25px 20px 19px;
}
.intro {
font-size: 13px;
}
.stage {
min-height: 430px;
height: 58vh;
}
.ledger {
grid-template-columns: 1fr;
padding: 18px 20px 28px;
}
.model-note {
grid-column: auto;
}
.scene-label {
min-width: 76px;
padding: 7px 8px;
}
.scene-label strong {
font-size: 12px;
}
.scene-label em {
font-size: 7px;
}
.scene-hint,
.blank-note {
bottom: 12px;
font-size: 7px;
}
.scene-hint {
left: 12px;
}
.blank-note {
right: 12px;
}
}
@media (prefers-reduced-motion: reduce) {
* {
transition: none !important;
}
}
</style>
</head>
<body>
<main class="app">
<header class="header">
<div>
<div class="eyebrow">Medium of exchange / abstract ledger</div>
<h1>Tokens mediate<br />the exchange.</h1>
<p class="intro">
Goods remain on their own trays while unbranded tokens sit on adjacent
trays. Change the quantity or abstract unit price to see the token
requirement change.
</p>
</div>
<aside class="legend" aria-label="Scene legend">
<div class="legend-row">
<span class="swatch goods"></span>
<span>Solid goods</span>
</div>
<div class="legend-row">
<span class="swatch tokens"></span>
<span>Blank exchange tokens</span>
</div>
<div class="legend-row">
<span class="swatch trays"></span>
<span>Separate trays</span>
</div>
</aside>
</header>
<section class="stage" id="stage" aria-label="Interactive three-dimensional exchange model">
<div class="label-layer" id="labelLayer">
<div class="scene-label" id="labelBreadGoods">
<span class="label-type">Goods tray</span>
<strong>Bread</strong>
<em id="sceneBreadGoods">3 pieces</em>
</div>
<div class="scene-label" id="labelBreadTokens">
<span class="label-type">Token tray</span>
<strong>Tokens</strong>
<em id="sceneBreadTokens">6 tokens</em>
</div>
<div class="scene-label" id="labelClothGoods">
<span class="label-type">Goods tray</span>
<strong>Cloth</strong>
<em id="sceneClothGoods">2 pieces</em>
</div>
<div class="scene-label" id="labelClothTokens">
<span class="label-type">Token tray</span>
<strong>Tokens</strong>
<em id="sceneClothTokens">6 tokens</em>
</div>
</div>
<div class="scene-hint">Drag to orbit · scroll to zoom</div>
<div class="blank-note">No denomination · no currency mark</div>
</section>
<section class="ledger" aria-label="Abstract exchange controls">
<article class="card" style="--card-color: var(--bread)">
<div class="card-title">Bread</div>
<div class="metric">
<div class="metric-label">
Quantity
<small>pieces shown</small>
</div>
<div class="stepper" aria-label="Bread quantity">
<button type="button" data-kind="bread" data-field="quantity" data-delta="-1" aria-label="Decrease bread quantity">−</button>
<output id="breadQuantity">3</output>
<button type="button" data-kind="bread" data-field="quantity" data-delta="1" aria-label="Increase bread quantity">+</button>
</div>
</div>
<div class="metric">
<div class="metric-label">
Unit price
<small>abstract tokens per piece</small>
</div>
<div class="stepper" aria-label="Bread unit price">
<button type="button" data-kind="bread" data-field="price" data-delta="-1" aria-label="Decrease bread unit price">−</button>
<output id="breadPrice">2</output>
<button type="button" data-kind="bread" data-field="price" data-delta="1" aria-label="Increase bread unit price">+</button>
</div>
</div>
<div class="total-row" aria-live="polite">
<div>
<div class="total-label">Tokens required</div>
<div class="total-value" id="breadTotal">6 tokens</div>
</div>
<div class="mini-tokens" id="breadDots" aria-hidden="true"></div>
</div>
</article>
<article class="card" style="--card-color: var(--cloth)">
<div class="card-title">Cloth</div>
<div class="metric">
<div class="metric-label">
Quantity
<small>folded pieces shown</small>
</div>
<div class="stepper" aria-label="Cloth quantity">
<button type="button" data-kind="cloth" data-field="quantity" data-delta="-1" aria-label="Decrease cloth quantity">−</button>
<output id="clothQuantity">2</output>
<button type="button" data-kind="cloth" data-field="quantity" data-delta="1" aria-label="Increase cloth quantity">+</button>
</div>
</div>
<div class="metric">
<div class="metric-label">
Unit price
<small>abstract tokens per piece</small>
</div>
<div class="stepper" aria-label="Cloth unit price">
<button type="button" data-kind="cloth" data-field="price" data-delta="-1" aria-label="Decrease cloth unit price">−</button>
<output id="clothPrice">3</output>
<button type="button" data-kind="cloth" data-field="price" data-delta="1" aria-label="Increase cloth unit price">+</button>
</div>
</div>
<div class="total-row" aria-live="polite">
<div>
<div class="total-label">Tokens required</div>
<div class="total-value" id="clothTotal">6 tokens</div>
</div>
<div class="mini-tokens" id="clothDots" aria-hidden="true"></div>
</div>
</article>
<aside class="card model-note">
<div>
<div class="note-number">How to read it</div>
<h2>Count. Price. Exchange.</h2>
<p>
Quantity is the number of solid goods. Unit price is an abstract
number of blank tokens assigned to each piece. The adjacent token
tray shows the resulting exchange requirement.
</p>
</div>
<footer>
This is an exchange model only. The tokens do not represent real
money, real denominations, relative purchasing power, or value
outside the diagram.
</footer>
</aside>
</section>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeee9df);
scene.fog = new THREE.Fog(0xeee9df, 19, 32);
const camera = new THREE.OrthographicCamera(-8, 8, 5, -5, 0.1, 60);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(stage.clientWidth, stage.clientHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Three-dimensional model of bread and cloth exchanged for blank tokens"
);
stage.prepend(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xfffaf0, 0x8e8a80, 2.0);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xfff6df, 3.1);
keyLight.position.set(-7, 12, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 32;
keyLight.shadow.bias = -0.00035;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xcce4df, 1.15);
rimLight.position.set(9, 7, -8);
scene.add(rimLight);
const materials = {
ground: new THREE.MeshStandardMaterial({ color: 0xeee9df, roughness: 1 }),
table: new THREE.MeshStandardMaterial({ color: 0xd6cfc1, roughness: 0.9 }),
tableEdge: new THREE.MeshStandardMaterial({ color: 0xc7bead, roughness: 0.92 }),
tray: new THREE.MeshStandardMaterial({ color: 0xe9e2d5, roughness: 0.88 }),
trayInside: new THREE.MeshStandardMaterial({ color: 0xcfc7b9, roughness: 0.92 }),
trayRim: new THREE.MeshStandardMaterial({ color: 0xbdb5a8, roughness: 0.84 }),
bread: new THREE.MeshStandardMaterial({ color: 0xc7834d, roughness: 0.78 }),
breadLight: new THREE.MeshStandardMaterial({ color: 0xd3975e, roughness: 0.8 }),
breadScore: new THREE.MeshStandardMaterial({ color: 0x9d5b35, roughness: 0.9 }),
cloth: new THREE.MeshStandardMaterial({ color: 0x526f78, roughness: 0.9 }),
clothLight: new THREE.MeshStandardMaterial({ color: 0x64838a, roughness: 0.88 }),
clothDark: new THREE.MeshStandardMaterial({ color: 0x405d66, roughness: 0.92 }),
clothSeam: new THREE.MeshStandardMaterial({ color: 0xa7c1c0, roughness: 0.92 }),
tokenSide: new THREE.MeshStandardMaterial({ color: 0x256e69, roughness: 0.48, metalness: 0.18 }),
tokenTop: new THREE.MeshStandardMaterial({ color: 0x3a9189, roughness: 0.42, metalness: 0.22 }),
tokenRing: new THREE.MeshStandardMaterial({ color: 0x54a39a, roughness: 0.34, metalness: 0.28 }),
arrow: new THREE.MeshStandardMaterial({ color: 0x9e978b, roughness: 0.9 })
};
function roundedRectShape(width, depth, radius) {
const x = -width / 2;
const y = -depth / 2;
const r = Math.min(radius, width / 2, depth / 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 + depth - r);
shape.quadraticCurveTo(x + width, y + depth, x + width - r, y + depth);
shape.lineTo(x + r, y + depth);
shape.quadraticCurveTo(x, y + depth, x, y + depth - r);
shape.lineTo(x, y + r);
shape.quadraticCurveTo(x, y, x + r, y);
shape.closePath();
return shape;
}
function roundedPrism(width, depth, height, radius, material, bevel = 0.035) {
const geometry = new THREE.ExtrudeGeometry(roundedRectShape(width, depth, radius), {
depth: height,
steps: 1,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: bevel,
bevelThickness: bevel,
curveSegments: 12
});
geometry.rotateX(-Math.PI / 2);
geometry.translate(0, -height / 2, 0);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
const ground = new THREE.Mesh(new THREE.PlaneGeometry(80, 80), materials.ground);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.48;
ground.receiveShadow = true;
scene.add(ground);
const table = new THREE.Group();
const tableTop = roundedPrism(15.8, 7.0, 0.34, 0.38, materials.table, 0.055);
tableTop.position.set(0.2, -0.22, 0.15);
table.add(tableTop);
const tableUnderlay = roundedPrism(15.35, 6.55, 0.16, 0.3, materials.tableEdge, 0.03);
tableUnderlay.position.set(0.2, -0.38, 0.15);
table.add(tableUnderlay);
scene.add(table);
function makeTray(width, depth, x, z) {
const group = new THREE.Group();
group.position.set(x, 0, z);
const base = roundedPrism(width, depth, 0.14, 0.17, materials.tray, 0.035);
base.position.y = 0.08;
group.add(base);
const inside = roundedPrism(
width - 0.27,
depth - 0.27,
0.025,
0.12,
materials.trayInside,
0.008
);
inside.position.y = 0.195;
group.add(inside);
const frontBack = roundedPrism(
width - 0.08,
0.15,
0.18,
0.065,
materials.trayRim,
0.028
);
frontBack.position.set(0, 0.25, depth / 2 - 0.075);
group.add(frontBack);
const back = frontBack.clone();
back.position.z = -depth / 2 + 0.075;
group.add(back);
const left = roundedPrism(
0.15,
depth - 0.16,
0.18,
0.065,
materials.trayRim,
0.028
);
left.position.set(-width / 2 + 0.075, 0.25, 0);
group.add(left);
const right = left.clone();
right.position.x = width / 2 - 0.075;
group.add(right);
scene.add(group);
return group;
}
const breadTray = makeTray(4.0, 3.2, -4.0, 0.2);
const breadTokenTray = makeTray(2.0, 3.2, -0.55, 0.2);
const clothTray = makeTray(3.2, 3.2, 2.4, 0.2);
const clothTokenTray = makeTray(2.0, 3.2, 5.55, 0.2);
const breadGroup = new THREE.Group();
const clothGroup = new THREE.Group();
const breadTokensGroup = new THREE.Group();
const clothTokensGroup = new THREE.Group();
breadTray.add(breadGroup);
clothTray.add(clothGroup);
breadTokenTray.add(breadTokensGroup);
clothTokenTray.add(clothTokensGroup);
function clearGroup(group) {
while (group.children.length) {
const child = group.children.pop();
child.traverse((node) => {
if (node.geometry) node.geometry.dispose();
});
}
}
function makeLoaf(index) {
const loaf = new THREE.Group();
const body = new THREE.Mesh(
new THREE.SphereGeometry(1, 32, 18),
index % 2 === 0 ? materials.bread : materials.breadLight
);
body.scale.set(0.59, 0.32, 0.38);
body.position.y = 0.49;
body.castShadow = true;
body.receiveShadow = true;
loaf.add(body);
const scores = [
[-0.19, 0.755, -0.07, 0.12],
[0.0, 0.808, 0.02, -0.08],
[0.19, 0.752, 0.10, 0.14]
];
for (const [x, y, z, angle] of scores) {
const score = new THREE.Mesh(
new THREE.SphereGeometry(1, 20, 10),
materials.breadScore
);
score.scale.set(0.23, 0.014, 0.035);
score.position.set(x, y, z);
score.rotation.y = angle;
score.castShadow = true;
loaf.add(score);
}
loaf.rotation.y = (index - 1) * 0.08;
return loaf;
}
function makeClothPiece(index) {
const piece = new THREE.Group();
const layerColors = [
materials.cloth,
materials.clothLight,
materials.clothDark
];
for (let i = 0; i < 3; i++) {
const layer = roundedPrism(
0.93 - i * 0.035,
0.64 - i * 0.025,
0.105,
0.09,
layerColors[(i + index) % layerColors.length],
0.024
);
layer.position.set(i * 0.018, 0.25 + i * 0.108, -i * 0.016);
piece.add(layer);
}
const seam = roundedPrism(0.69, 0.026, 0.012, 0.01, materials.clothSeam, 0.003);
seam.position.set(0.03, 0.566, 0.245);
piece.add(seam);
piece.rotation.y = index % 2 === 0 ? -0.05 : 0.06;
return piece;
}
function makeToken() {
const token = new THREE.Group();
const body = new THREE.Mesh(
new THREE.CylinderGeometry(0.285, 0.285, 0.10, 48),
[materials.tokenSide, materials.tokenTop, materials.tokenSide]
);
body.position.y = 0.05;
body.castShadow = true;
body.receiveShadow = true;
token.add(body);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.235, 0.017, 10, 48),
materials.tokenRing
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.101;
ring.castShadow = true;
token.add(ring);
return token;
}
const arrangements = {
1: [[0, 0]],
2: [[-0.58, 0], [0.58, 0]],
3: [[-1.02, 0], [0, 0], [1.02, 0]],
4: [[-0.57, -0.57], [0.57, -0.57], [-0.57, 0.57], [0.57, 0.57]]
};
function renderBread(quantity) {
clearGroup(breadGroup);
const positions = arrangements[quantity];
positions.forEach(([x, z], index) => {
const loaf = makeLoaf(index);
loaf.position.set(x, 0, z);
breadGroup.add(loaf);
});
}
function renderCloth(quantity) {
clearGroup(clothGroup);
const positions = arrangements[quantity];
positions.forEach(([x, z], index) => {
const cloth = makeClothPiece(index);
cloth.position.set(x, 0, z);
clothGroup.add(cloth);
});
}
function renderTokens(group, total) {
clearGroup(group);
const columns = total > 8 ? 4 : 2;
const rows = Math.ceil(total / columns);
const xSpacing = 0.435;
const zSpacing = 0.62;
for (let i = 0; i < total; i++) {
const row = Math.floor(i / columns);
const column = i % columns;
const rowCount = Math.min(columns, total - row * columns);
const x =
(column - (rowCount - 1) / 2) * xSpacing;
const z = (row - (rows - 1) / 2) * zSpacing;
const token = makeToken();
token.position.set(x, 0.205, z);
group.add(token);
}
}
function makeArrow(x, z) {
const arrow = new THREE.Group();
arrow.position.set(x, 0.03, z);
const shaft = new THREE.Mesh(
new THREE.BoxGeometry(0.34, 0.035, 0.075),
materials.arrow
);
shaft.position.x = 0.08;
shaft.castShadow = true;
arrow.add(shaft);
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.125, 0.25, 24),
materials.arrow
);
head.rotation.z = Math.PI / 2;
head.position.set(-0.18, 0.01, 0);
head.castShadow = true;
arrow.add(head);
scene.add(arrow);
}
makeArrow(-1.78, 0.2);
makeArrow(4.28, 0.2);
const state = {
bread: { quantity: 3, price: 2 },
cloth: { quantity: 2, price: 3 }
};
function plural(value, singular, pluralForm = singular + "s") {
return value === 1 ? singular : pluralForm;
}
function renderMiniTokens(element, total) {
element.replaceChildren();
for (let i = 0; i < total; i++) {
const token = document.createElement("span");
token.className = "mini-token";
element.appendChild(token);
}
}
function updateInterface() {
const breadTotal = state.bread.quantity * state.bread.price;
const clothTotal = state.cloth.quantity * state.cloth.price;
renderBread(state.bread.quantity);
renderCloth(state.cloth.quantity);
renderTokens(breadTokensGroup, breadTotal);
renderTokens(clothTokensGroup, clothTotal);
document.getElementById("breadQuantity").textContent = state.bread.quantity;
document.getElementById("breadPrice").textContent = state.bread.price;
document.getElementById("clothQuantity").textContent = state.cloth.quantity;
document.getElementById("clothPrice").textContent = state.cloth.price;
document.getElementById("breadTotal").textContent =
`${breadTotal} ${plural(breadTotal, "token")}`;
document.getElementById("clothTotal").textContent =
`${clothTotal} ${plural(clothTotal, "token")}`;
document.getElementById("sceneBreadGoods").textContent =
`${state.bread.quantity} ${plural(state.bread.quantity, "piece")}`;
document.getElementById("sceneBreadTokens").textContent =
`${breadTotal} ${plural(breadTotal, "token")}`;
document.getElementById("sceneClothGoods").textContent =
`${state.cloth.quantity} ${plural(state.cloth.quantity, "piece")}`;
document.getElementById("sceneClothTokens").textContent =
`${clothTotal} ${plural(clothTotal, "token")}`;
renderMiniTokens(document.getElementById("breadDots"), breadTotal);
renderMiniTokens(document.getElementById("clothDots"), clothTotal);
document.querySelectorAll("[data-kind]").forEach((button) => {
const kind = button.dataset.kind;
const field = button.dataset.field;
const delta = Number(button.dataset.delta);
const next = state[kind][field] + delta;
button.disabled = next < 1 || next > 4;
});
}
document.querySelectorAll("[data-kind]").forEach((button) => {
button.addEventListener("click", () => {
const kind = button.dataset.kind;
const field = button.dataset.field;
const delta = Number(button.dataset.delta);
state[kind][field] = Math.min(4, Math.max(1, state[kind][field] + delta));
updateInterface();
});
});
updateInterface();
let target = new THREE.Vector3(0.25, 0.32, 0.1);
let azimuth = 0.56;
let elevation = 0.78;
let zoom = 1;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
function updateCamera() {
const radius = 18;
camera.position.set(
target.x + radius * Math.sin(azimuth) * Math.cos(elevation),
target.y + radius * Math.sin(elevation),
target.z + radius * Math.cos(azimuth) * Math.cos(elevation)
);
camera.lookAt(target);
camera.zoom = zoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = stage.clientWidth;
const height = stage.clientHeight;
const aspect = width / height;
const viewHeight = Math.max(8.8, 14.8 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
updateCamera();
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
azimuth -= deltaX * 0.006;
elevation = THREE.MathUtils.clamp(elevation + deltaY * 0.004, 0.35, 1.16);
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (event.pointerId !== undefined && renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
zoom = THREE.MathUtils.clamp(
zoom * Math.exp(-event.deltaY * 0.001),
0.72,
1.32
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
azimuth = 0.56;
elevation = 0.78;
zoom = 1;
updateCamera();
});
const labelAnchors = [
{
element: document.getElementById("labelBreadGoods"),
position: new THREE.Vector3(-4.0, 1.42, 0.2)
},
{
element: document.getElementById("labelBreadTokens"),
position: new THREE.Vector3(-0.55, 1.08, 0.2)
},
{
element: document.getElementById("labelClothGoods"),
position: new THREE.Vector3(2.4, 1.24, 0.2)
},
{
element: document.getElementById("labelClothTokens"),
position: new THREE.Vector3(5.55, 1.08, 0.2)
}
];
function updateLabels() {
const width = stage.clientWidth;
const height = stage.clientHeight;
for (const item of labelAnchors) {
const projected = item.position.clone().project(camera);
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
item.element.style.left = `${x}px`;
item.element.style.top = `${y}px`;
item.element.style.visibility =
projected.z > -1 && projected.z < 1 ? "visible" : "hidden";
}
}
new ResizeObserver(resize).observe(stage);
resize();
renderer.setAnimationLoop(() => {
updateLabels();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```