Prompt specification
Build or customize the Three.js 3D scene "Market With and Without Shared 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: Market With and Without Shared Tokens
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/market-with-and-without-shared-tokens-x3163
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing currency tokens as a medium of exchange. Build three fictional shops and identical customer needs. Toggle barter and token exchange, displaying successful transactions under fixed rules without claiming universal historical superiority. Voxel style.
Working Three.js Starter (market-with-and-without-shared-tokens-x3163):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Common Ground — Token Exchange Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #0b1019;
--panel: #121a27;
--panel-2: #182334;
--line: #2a394d;
--text: #edf4ff;
--muted: #91a4ba;
--gold: #ffc857;
--gold-dark: #b7791f;
--blue: #65c8ff;
--green: #73d49b;
--purple: #b69cff;
--red: #ff7c78;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
height: 100%;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow: hidden;
}
button {
font: inherit;
}
.app {
display: grid;
grid-template-rows: 72px minmax(0, 1fr);
height: 100dvh;
}
header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 0 24px;
background: #0e1622;
border-bottom: 1px solid var(--line);
z-index: 5;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
position: relative;
width: 34px;
height: 34px;
flex: 0 0 auto;
}
.brand-mark i {
position: absolute;
display: block;
width: 13px;
height: 13px;
background: var(--gold);
box-shadow: inset -3px -3px 0 #b7791f;
}
.brand-mark i:nth-child(1) {
left: 1px;
top: 15px;
}
.brand-mark i:nth-child(2) {
left: 12px;
top: 6px;
background: var(--blue);
box-shadow: inset -3px -3px 0 #277da6;
}
.brand-mark i:nth-child(3) {
left: 12px;
top: 21px;
background: var(--green);
box-shadow: inset -3px -3px 0 #347a51;
}
.brand h1 {
margin: 0;
font-size: 16px;
line-height: 1.1;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.brand p {
margin: 4px 0 0;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.header-facts {
display: flex;
align-items: center;
gap: 9px;
color: var(--muted);
font-size: 12px;
white-space: nowrap;
}
.header-facts span {
padding: 7px 9px;
border: 1px solid var(--line);
border-radius: 7px;
background: #111b29;
}
main {
display: grid;
grid-template-columns: minmax(0, 1fr) 370px;
min-height: 0;
}
#stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background:
radial-gradient(circle at 48% 32%, #24344a 0, #131d2b 42%, #0b111b 100%);
}
#sceneMount {
position: absolute;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.hud {
position: absolute;
z-index: 2;
pointer-events: none;
}
.hud-top {
top: 18px;
left: 18px;
right: 18px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 14px;
}
.mode-badge,
.shared-need {
background: rgba(12, 19, 30, 0.88);
border: 1px solid rgba(108, 132, 160, 0.35);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
backdrop-filter: blur(10px);
}
.mode-badge {
display: flex;
align-items: center;
gap: 10px;
padding: 10px 13px;
border-radius: 10px;
font-size: 12px;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.status-dot {
width: 9px;
height: 9px;
border-radius: 2px;
background: var(--gold);
box-shadow: 0 0 12px rgba(255, 200, 87, 0.65);
}
.mode-badge strong {
display: block;
font-size: 13px;
}
.mode-badge small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.04em;
text-transform: none;
}
.shared-need {
max-width: 330px;
padding: 11px 13px;
border-radius: 10px;
}
.shared-need .eyebrow {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.shared-need strong {
display: block;
margin-top: 5px;
font-size: 13px;
}
.need-icons {
display: flex;
align-items: center;
gap: 7px;
margin-top: 8px;
color: var(--muted);
font-size: 11px;
}
.mini-item {
display: inline-block;
width: 12px;
height: 12px;
border-radius: 2px;
box-shadow: inset -3px -3px rgba(0, 0, 0, 0.2);
}
.mini-item.bread {
background: #d9a75f;
}
.mini-item.apple {
background: #e85d5d;
}
.mini-item.cloth {
background: #9c80e8;
}
.legend {
left: 18px;
bottom: 18px;
display: flex;
flex-wrap: wrap;
gap: 8px;
max-width: 520px;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
padding: 8px 10px;
border-radius: 8px;
background: rgba(12, 19, 30, 0.84);
border: 1px solid rgba(108, 132, 160, 0.28);
color: #bdcadd;
font-size: 10px;
}
.legend-token {
width: 11px;
height: 11px;
border-radius: 50%;
background: var(--gold);
box-shadow: inset -2px -2px #9d691d;
}
.legend-payload {
width: 11px;
height: 11px;
border-radius: 2px;
background: var(--blue);
}
.legend-good {
width: 11px;
height: 11px;
border-radius: 2px;
background: var(--green);
}
.camera-tip {
right: 18px;
bottom: 18px;
padding: 8px 10px;
border-radius: 8px;
background: rgba(12, 19, 30, 0.78);
color: var(--muted);
font-size: 10px;
border: 1px solid rgba(108, 132, 160, 0.22);
}
aside {
min-height: 0;
overflow-y: auto;
background: var(--panel);
border-left: 1px solid var(--line);
scrollbar-width: thin;
scrollbar-color: #34465c transparent;
}
.panel-section {
padding: 18px;
border-bottom: 1px solid var(--line);
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin: 0 0 12px;
color: #dce8f7;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.section-title .count {
color: var(--muted);
font-size: 10px;
font-weight: 600;
letter-spacing: 0.03em;
text-transform: none;
}
.mode-switch {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.mode-button {
position: relative;
display: flex;
align-items: center;
gap: 10px;
min-height: 66px;
padding: 10px;
color: var(--muted);
background: #0f1723;
border: 1px solid var(--line);
border-radius: 10px;
cursor: pointer;
text-align: left;
transition: 160ms ease;
}
.mode-button:hover {
border-color: #526780;
color: var(--text);
}
.mode-button.active {
color: var(--text);
background: #1a2838;
border-color: var(--gold);
box-shadow: inset 0 -3px 0 rgba(255, 200, 87, 0.75);
}
.mode-button b {
display: block;
font-size: 12px;
}
.mode-button small {
display: block;
margin-top: 3px;
font-size: 9px;
line-height: 1.3;
}
.mode-icon {
position: relative;
display: grid;
place-items: center;
width: 34px;
height: 34px;
flex: 0 0 auto;
border-radius: 8px;
background: #0a111b;
border: 1px solid #314155;
}
.mode-icon.barter i {
position: absolute;
width: 11px;
height: 11px;
border-radius: 2px;
}
.mode-icon.barter i:first-child {
left: 6px;
background: var(--blue);
box-shadow: inset -2px -2px #2b789e;
}
.mode-icon.barter i:last-child {
right: 6px;
background: var(--green);
box-shadow: inset -2px -2px #397a51;
}
.mode-icon.token i {
width: 19px;
height: 19px;
border-radius: 50%;
background: var(--gold);
box-shadow: inset -4px -4px #9d691d, 0 0 0 3px #6f4c15;
}
.run-row {
display: grid;
grid-template-columns: 1fr auto auto;
gap: 8px;
margin-top: 12px;
}
.action {
min-height: 39px;
padding: 0 14px;
border-radius: 8px;
border: 1px solid var(--line);
background: #182536;
color: var(--text);
cursor: pointer;
font-size: 11px;
font-weight: 700;
transition: 150ms ease;
}
.action:hover {
filter: brightness(1.15);
}
.action.primary {
color: #201707;
background: var(--gold);
border-color: var(--gold);
box-shadow: inset 0 -3px 0 #b7791f;
}
.action.small {
min-width: 48px;
padding: 0 10px;
}
.progress-track {
height: 5px;
margin-top: 12px;
overflow: hidden;
border-radius: 5px;
background: #0b121c;
}
#progressFill {
width: 0;
height: 100%;
background: linear-gradient(90deg, var(--gold-dark), var(--gold));
transition: width 120ms linear;
}
#status {
min-height: 34px;
margin: 9px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.stat {
padding: 12px;
border-radius: 9px;
background: #0f1723;
border: 1px solid var(--line);
}
.stat span {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.stat strong {
display: block;
margin-top: 5px;
font-size: 22px;
line-height: 1;
}
.customer-card {
padding: 13px;
background: #0f1824;
border: 1px solid var(--line);
border-radius: 10px;
}
.customer-card p {
margin: 0;
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
.customer-card strong {
color: var(--text);
}
.inventory-line {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin-top: 10px;
}
.inventory-chip {
display: flex;
align-items: center;
gap: 5px;
padding: 5px 7px;
border-radius: 5px;
background: #192638;
color: #c9d6e5;
font-size: 9px;
}
.chip-dot {
width: 8px;
height: 8px;
border-radius: 2px;
}
.rule-list {
margin: 0;
padding: 0;
list-style: none;
}
.rule-list li {
position: relative;
padding: 0 0 10px 18px;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.rule-list li:last-child {
padding-bottom: 0;
}
.rule-list li::before {
content: "";
position: absolute;
left: 0;
top: 5px;
width: 7px;
height: 7px;
border-radius: 2px;
background: var(--gold);
}
.rule-list b {
color: var(--text);
}
.ledger {
display: flex;
flex-direction: column;
gap: 7px;
max-height: 330px;
overflow-y: auto;
padding-right: 3px;
scrollbar-width: thin;
scrollbar-color: #34465c transparent;
}
.ledger-empty {
padding: 22px 12px;
border: 1px dashed #34465c;
border-radius: 9px;
color: var(--muted);
text-align: center;
font-size: 10px;
line-height: 1.5;
}
.transaction {
padding: 10px;
border: 1px solid var(--line);
border-left: 3px solid var(--green);
border-radius: 8px;
background: #101925;
animation: ledger-in 220ms ease-out;
}
@keyframes ledger-in {
from {
opacity: 0;
transform: translateY(-5px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.transaction-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
}
.transaction-head strong {
font-size: 10px;
}
.success {
padding: 3px 5px;
border-radius: 4px;
color: #8fe6b4;
background: rgba(115, 212, 155, 0.12);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.transaction-route {
margin-top: 5px;
color: var(--muted);
font-size: 9px;
}
.transaction-exchange {
display: flex;
align-items: center;
gap: 7px;
margin-top: 7px;
color: #d5dfeb;
font-size: 9px;
}
.exchange-swatch {
width: 10px;
height: 10px;
flex: 0 0 auto;
border-radius: 2px;
}
.exchange-arrow {
color: #64788f;
}
.caveat {
padding: 13px;
border-radius: 9px;
background: rgba(101, 200, 255, 0.07);
border: 1px solid rgba(101, 200, 255, 0.18);
color: #a9bfd1;
font-size: 10px;
line-height: 1.55;
}
.caveat strong {
color: var(--blue);
}
@media (max-width: 900px) {
body {
overflow: auto;
}
.app {
height: auto;
min-height: 100dvh;
grid-template-rows: 68px auto;
}
main {
grid-template-columns: 1fr;
}
#stage {
height: 62dvh;
min-height: 460px;
}
aside {
overflow: visible;
border-left: 0;
border-top: 1px solid var(--line);
}
.header-facts span:nth-child(2) {
display: none;
}
}
@media (max-width: 560px) {
header {
padding: 0 14px;
}
.header-facts {
display: none;
}
.hud-top {
left: 10px;
right: 10px;
top: 10px;
}
.shared-need {
display: none;
}
.legend {
left: 10px;
right: 10px;
bottom: 10px;
}
.camera-tip {
display: none;
}
.panel-section {
padding: 15px;
}
}
</style>
</head>
<body>
<div class="app">
<header>
<div class="brand">
<div class="brand-mark" aria-hidden="true"><i></i><i></i><i></i></div>
<div>
<h1>Common Ground</h1>
<p>Token exchange lab</p>
</div>
</div>
<div class="header-facts">
<span>3 fictional shops</span>
<span>3 customers</span>
<span>9 fixed exchanges</span>
</div>
</header>
<main>
<section id="stage" aria-label="Interactive voxel exchange simulation">
<div id="sceneMount"></div>
<div class="hud hud-top">
<div class="mode-badge">
<span class="status-dot" aria-hidden="true"></span>
<div>
<strong id="hudMode">Token exchange</strong>
<small id="hudSubmode">One brass token per purchase</small>
</div>
</div>
<div class="shared-need">
<div class="eyebrow">Identical customer need</div>
<strong>One complete ration kit</strong>
<div class="need-icons">
<span class="mini-item bread"></span><span>bread</span><span>+</span>
<span class="mini-item apple"></span><span>apple</span><span>+</span>
<span class="mini-item cloth"></span><span>cloth</span>
</div>
</div>
</div>
<div class="hud legend" aria-label="Scene legend">
<div class="legend-item"><span class="legend-token"></span>Brass token</div>
<div class="legend-item"><span class="legend-payload"></span>Exact barter input</div>
<div class="legend-item"><span class="legend-good"></span>Received good</div>
</div>
<div class="hud camera-tip">Drag: orbit · Wheel: zoom · Arrow keys: rotate</div>
</section>
<aside>
<section class="panel-section">
<h2 class="section-title">
Exchange system
<span class="count">toggle to replay</span>
</h2>
<div class="mode-switch" role="group" aria-label="Exchange mode">
<button class="mode-button" id="barterButton" aria-pressed="false">
<span class="mode-icon barter" aria-hidden="true"><i></i><i></i></span>
<span>
<b>Barter</b>
<small>Exact goods for goods</small>
</span>
</button>
<button class="mode-button active" id="tokenButton" aria-pressed="true">
<span class="mode-icon token" aria-hidden="true"><i></i></span>
<span>
<b>Tokens</b>
<small>One accepted medium</small>
</span>
</button>
</div>
<div class="run-row">
<button class="action primary" id="runButton">Run exchanges</button>
<button class="action small" id="speedButton" title="Change simulation speed">1×</button>
<button class="action small" id="resetButton">Reset</button>
</div>
<div class="progress-track" aria-hidden="true">
<div id="progressFill"></div>
</div>
<p id="status" aria-live="polite">Ready to simulate.</p>
</section>
<section class="panel-section">
<h2 class="section-title">Live result</h2>
<div class="stats">
<div class="stat">
<span>Successful</span>
<strong id="completedStat">0 / 9</strong>
</div>
<div class="stat">
<span id="spentLabel">Tokens spent</span>
<strong id="spentStat">0</strong>
</div>
</div>
</section>
<section class="panel-section">
<h2 class="section-title">Shared starting position</h2>
<div class="customer-card">
<p>
<strong>Nia, Bo, and Kit have the same need:</strong>
one ration kit containing 1 bread, 1 apple, and 1 cloth.
</p>
<p style="margin-top:8px">
Each also starts with <strong>3 brass tokens</strong>,
<strong>1 flour</strong>, <strong>1 potato</strong>,
and <strong>1 thread</strong>.
</p>
<div class="inventory-line">
<span class="inventory-chip"><span class="chip-dot" style="background:#ffc857"></span>3 tokens</span>
<span class="inventory-chip"><span class="chip-dot" style="background:#e9e2d2"></span>1 flour</span>
<span class="inventory-chip"><span class="chip-dot" style="background:#a97945"></span>1 potato</span>
<span class="inventory-chip"><span class="chip-dot" style="background:#65c8ff"></span>1 thread</span>
</div>
</div>
</section>
<section class="panel-section">
<h2 class="section-title">
Fixed rules
<span class="count" id="ruleModeLabel">token mode</span>
</h2>
<ul class="rule-list" id="ruleList"></ul>
</section>
<section class="panel-section">
<h2 class="section-title">
Successful ledger
<span class="count" id="ledgerCount">0 records</span>
</h2>
<div class="ledger" id="ledger" aria-live="polite">
<div class="ledger-empty">
Run the simulation to record each successful transaction.
</div>
</div>
</section>
<section class="panel-section">
<div class="caveat">
<strong>A bounded fictional comparison.</strong>
This model does not claim that token exchange is universally or
historically superior. Real outcomes also depend on liquidity,
credit, trust, institutions, trade rules, and social context.
</div>
</section>
</aside>
</main>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Fixed scenario
// -------------------------------------------------------------------------
const COLORS = {
token: 0xffc857,
flour: 0xe9e2d2,
potato: 0xa97945,
thread: 0x65c8ff,
bread: 0xd9a75f,
apple: 0xe85d5d,
cloth: 0x9c80e8
};
const ITEM_COLORS = {
token: "#ffc857",
flour: "#e9e2d2",
potato: "#a97945",
thread: "#65c8ff",
bread: "#d9a75f",
apple: "#e85d5d",
cloth: "#9c80e8"
};
const ITEM_NAMES = {
token: "brass token",
flour: "flour",
potato: "potato",
thread: "thread",
bread: "bread",
apple: "apple",
cloth: "cloth"
};
const shopDefinitions = [
{
name: "Moonrise Bakery",
short: "Bakery",
x: -4,
color: 0xd79b55,
trim: 0xf0c36b,
inputType: "flour",
outputType: "bread"
},
{
name: "Lumen Grocer",
short: "Grocer",
x: 0,
color: 0x4f9b72,
trim: 0x73d49b,
inputType: "potato",
outputType: "apple"
},
{
name: "Blockwork Tailor",
short: "Tailor",
x: 4,
color: 0x7259aa,
trim: 0xb69cff,
inputType: "thread",
outputType: "cloth"
}
];
const customerDefinitions = [
{ name: "Nia", shirt: 0xe86f61, hair: 0x3b251e, start: new THREE.Vector3(-4, 0, 3.25) },
{ name: "Bo", shirt: 0x4fa6c9, hair: 0x191919, start: new THREE.Vector3(0, 0, 3.25) },
{ name: "Kit", shirt: 0x8bc95c, hair: 0x684526, start: new THREE.Vector3(4, 0, 3.25) }
];
let mode = "token";
let speed = 1;
let running = false;
let elapsed = 0;
let transactionIndex = 0;
let completed = 0;
let lastPhase = "";
let transferVisual = null;
const TRANSACTION_DURATION = 1.82;
const MOVE_DURATION = 0.66;
const PAY_DURATION = 0.52;
const RECEIVE_DURATION = 0.54;
const stage = document.getElementById("stage");
const sceneMount = document.getElementById("sceneMount");
const ledger = document.getElementById("ledger");
const statusElement = document.getElementById("status");
const progressFill = document.getElementById("progressFill");
const completedStat = document.getElementById("completedStat");
const spentStat = document.getElementById("spentStat");
const spentLabel = document.getElementById("spentLabel");
const ledgerCount = document.getElementById("ledgerCount");
const ruleList = document.getElementById("ruleList");
const ruleModeLabel = document.getElementById("ruleModeLabel");
const hudMode = document.getElementById("hudMode");
const hudSubmode = document.getElementById("hudSubmode");
const barterButton = document.getElementById("barterButton");
const tokenButton = document.getElementById("tokenButton");
const runButton = document.getElementById("runButton");
const resetButton = document.getElementById("resetButton");
const speedButton = document.getElementById("speedButton");
// -------------------------------------------------------------------------
// Three.js scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x111a27);
scene.fog = new THREE.Fog(0x111a27, 19, 37);
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 1.6));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.domElement.setAttribute("aria-label", "Voxel-style exchange simulation");
renderer.domElement.tabIndex = 0;
sceneMount.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-8, 8, 5, -5, 0.1, 80);
const cameraState = {
azimuth: 0.67,
elevation: 0.63,
zoom: 1,
viewSize: 10.6
};
function updateCamera() {
const radius = 20;
const horizontal = Math.cos(cameraState.elevation) * radius;
camera.position.set(
Math.sin(cameraState.azimuth) * horizontal,
Math.sin(cameraState.elevation) * radius,
Math.cos(cameraState.azimuth) * horizontal
);
camera.lookAt(0, 0.55, 0);
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
}
function resizeScene() {
const width = Math.max(1, stage.clientWidth);
const height = Math.max(1, stage.clientHeight);
const aspect = width / height;
const size = cameraState.viewSize;
camera.left = (-size * aspect) / 2;
camera.right = (size * aspect) / 2;
camera.top = size / 2;
camera.bottom = -size / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
new ResizeObserver(resizeScene).observe(stage);
resizeScene();
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xbdd8ff, 0x263044, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe6b7, 3.1);
sun.position.set(-7, 13, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0006;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x70baff, 1.25);
rimLight.position.set(8, 6, -9);
scene.add(rimLight);
const materialCache = new Map();
function material(color, metalness = 0, roughness = 0.86) {
const key = `${color}-${metalness}-${roughness}`;
if (!materialCache.has(key)) {
materialCache.set(
key,
new THREE.MeshStandardMaterial({
color,
metalness,
roughness,
flatShading: true
})
);
}
return materialCache.get(key);
}
function addBox(parent, width, height, depth, color, x, y, z, mat = null) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
mat || material(color)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeLabel(text, background, foreground = "#f6f8fb", width = 2.8, height = 0.7) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const context = canvas.getContext("2d");
context.fillStyle = background;
context.fillRect(0, 0, canvas.width, canvas.height);
context.fillStyle = "rgba(255,255,255,0.10)";
context.fillRect(8, 8, canvas.width - 16, canvas.height - 16);
context.fillStyle = foreground;
context.font = "800 40px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, canvas.width / 2, canvas.height / 2 + 2);
const texture = new THREE.CanvasTexture(canvas);
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(width, height, 1);
sprite.renderOrder = 20;
return sprite;
}
function makeItem(type) {
const group = new THREE.Group();
if (type === "token") {
const coin = new THREE.Mesh(
new THREE.CylinderGeometry(0.21, 0.21, 0.075, 8),
material(COLORS.token, 0.72, 0.3)
);
coin.rotation.x = Math.PI / 2;
coin.castShadow = true;
group.add(coin);
const inset = addBox(group, 0.1, 0.1, 0.018, 0xd59021, 0, 0, 0.05);
inset.rotation.z = Math.PI / 4;
return group;
}
if (type === "bread") {
addBox(group, 0.55, 0.22, 0.3, COLORS.bread, 0, 0, 0);
addBox(group, 0.4, 0.08, 0.07, 0xf0c889, 0, 0.13, 0.08);
addBox(group, 0.07, 0.09, 0.31, 0xf0c889, -0.13, 0.12, 0);
addBox(group, 0.07, 0.09, 0.31, 0xf0c889, 0.13, 0.12, 0);
return group;
}
if (type === "apple") {
addBox(group, 0.36, 0.36, 0.36, COLORS.apple, 0, 0, 0);
addBox(group, 0.07, 0.14, 0.07, 0x6f4326, 0, 0.24, 0);
addBox(group, 0.17, 0.06, 0.1, 0x73b95d, 0.1, 0.21, 0);
return group;
}
if (type === "cloth") {
addBox(group, 0.48, 0.1, 0.38, COLORS.cloth, 0, -0.08, 0);
addBox(group, 0.42, 0.1, 0.34, 0xb29bfa, 0.03, 0.02, 0);
addBox(group, 0.36, 0.08, 0.3, 0x8063ca, 0.05, 0.11, 0);
return group;
}
if (type === "flour") {
addBox(group, 0.4, 0.46, 0.33, COLORS.flour, 0, 0, 0);
addBox(group, 0.32, 0.08, 0.34, 0xb9d3e6, 0, 0.12, 0);
addBox(group, 0.18, 0.12, 0.02, 0x68859d, 0, -0.03, 0.18);
return group;
}
if (type === "potato") {
const potato = addBox(group, 0.42, 0.28, 0.34, COLORS.potato, 0, 0, 0);
potato.rotation.z = 0.12;
addBox(group, 0.06, 0.06, 0.03, 0x704722, 0.11, 0.09, 0.18);
return group;
}
if (type === "thread") {
const spool = new THREE.Mesh(
new THREE.CylinderGeometry(0.19, 0.19, 0.3, 8),
material(COLORS.thread)
);
spool.rotation.z = Math.PI / 2;
spool.castShadow = true;
group.add(spool);
addBox(group, 0.04, 0.42, 0.42, 0xb8d5e4, -0.17, 0, 0);
addBox(group, 0.04, 0.42, 0.42, 0xb8d5e4, 0.17, 0, 0);
return group;
}
return group;
}
// Ground platform and voxel street tiles.
addBox(scene, 14.4, 0.5, 8.2, 0x273447, 0, -0.25, 0);
addBox(scene, 13.8, 0.08, 7.6, 0x35445a, 0, 0.04, 0);
const tileColors = [0x3b4a60, 0x405169, 0x35445a, 0x46556b];
for (let row = 0; row < 4; row++) {
for (let column = 0; column < 9; column++) {
if ((row + column) % 4 === 0) continue;
const x = -6 + column * 1.5;
const z = -2.7 + row * 1.65;
addBox(
scene,
1.22,
0.045,
1.28,
tileColors[(row + column) % tileColors.length],
x,
0.105,
z
);
}
}
addBox(scene, 13.2, 0.035, 0.7, 0x5a6677, 0, 0.15, 2.25);
addBox(scene, 13.2, 0.035, 0.18, 0xf0c36b, 0, 0.175, 1.93);
addBox(scene, 13.2, 0.035, 0.18, 0xf0c36b, 0, 0.175, 2.57);
// Small background voxel skyline.
const skyline = [
[-6.3, 1.4, -3.55, 0.8, 2.8, 0.7, 0x27374c],
[-5.25, 1.05, -3.6, 0.65, 2.1, 0.65, 0x314158],
[5.7, 1.7, -3.55, 0.95, 3.4, 0.7, 0x27374c],
[6.45, 1.05, -3.58, 0.6, 2.1, 0.6, 0x314158]
];
for (const [x, y, z, w, h, d, color] of skyline) {
addBox(scene, w, h, d, color, x, y / 2 + 0.15, z);
}
const shops = [];
function buildShop(definition, index) {
const group = new THREE.Group();
group.position.x = definition.x;
scene.add(group);
// Building shell.
addBox(group, 2.7, 2.45, 2.05, definition.color, 0, 1.32, -1.65);
addBox(group, 2.95, 0.24, 2.25, definition.trim, 0, 2.66, -1.65);
addBox(group, 2.55, 0.2, 1.95, 0x253247, 0, 2.86, -1.65);
// Door and windows.
addBox(group, 0.64, 1.2, 0.08, 0x1a2738, -0.66, 0.76, -0.59);
addBox(group, 0.5, 0.06, 0.1, definition.trim, -0.66, 0.78, -0.53);
addBox(group, 0.72, 0.68, 0.08, 0x9bd7ef, 0.63, 1.35, -0.59);
addBox(group, 0.06, 0.7, 0.1, definition.trim, 0.63, 1.35, -0.53);
addBox(group, 0.74, 0.06, 0.1, definition.trim, 0.63, 1.35, -0.53);
// Awning, built from alternating voxel blocks.
for (let stripe = 0; stripe < 7; stripe++) {
addBox(
group,
0.39,
0.12,
0.72,
stripe % 2 === 0 ? definition.trim : 0xf4f0df,
-1.17 + stripe * 0.39,
1.9,
-0.34
);
}
// Counter.
addBox(group, 1.7, 0.92, 0.66, 0x26364a, 0, 0.55, -0.25);
addBox(group, 1.84, 0.13, 0.78, definition.trim, 0, 1.08, -0.25);
// Shop sign.
const sign = makeLabel(
definition.name.toUpperCase(),
`#${new THREE.Color(definition.trim).getHexString()}`,
"#172233",
3.05,
0.65
);
sign.position.set(0, 3.35, -1.55);
group.add(sign);
// Distinctive roof voxel.
const roofShape = index === 0
? [[0, 0.35, 0, 0.8, 0.7, 0.8]]
: index === 1
? [[-0.25, 0.34, 0, 0.6, 0.68, 0.6], [0.28, 0.28, 0, 0.5, 0.56, 0.5]]
: [[-0.28, 0.3, 0, 0.55, 0.6, 0.55], [0.28, 0.3, 0, 0.55, 0.6, 0.55], [0, 0.58, 0, 0.55, 0.55, 0.55]];
for (const [x, y, z, w, h, d] of roofShape) {
addBox(group, w, h, d, definition.trim, x, 3.1 + y, -1.65 + z);
}
const displayItem = makeItem(definition.outputType);
displayItem.scale.setScalar(0.8);
displayItem.position.set(0, 1.52, -0.2);
group.add(displayItem);
const shop = {
...definition,
group,
displayItem,
counterPosition: new THREE.Vector3(definition.x, 1.32, -0.2),
customerPosition: new THREE.Vector3(definition.x, 0, 0.85),
index
};
shops.push(shop);
}
shopDefinitions.forEach(buildShop);
// -------------------------------------------------------------------------
// Customers
// -------------------------------------------------------------------------
const customers = [];
function buildCustomer(definition, index) {
const group = new THREE.Group();
group.position.copy(definition.start);
scene.add(group);
// Legs and shoes.
addBox(group, 0.2, 0.48, 0.25, 0x263244, -0.16, 0.25, 0);
addBox(group, 0.2, 0.48, 0.25, 0x263244, 0.16, 0.25, 0);
addBox(group, 0.23, 0.13, 0.35, 0x151d28, -0.16, 0.06, 0.05);
addBox(group, 0.23, 0.13, 0.35, 0x151d28, 0.16, 0.06, 0.05);
// Torso and arms.
addBox(group, 0.62, 0.66, 0.38, definition.shirt, 0, 0.82, 0);
addBox(group, 0.18, 0.52, 0.2, definition.shirt, -0.4, 0.78, 0);
addBox(group, 0.18, 0.52, 0.2, definition.shirt, 0.4, 0.78, 0);
// Head, hair, and face.
addBox(group, 0.44, 0.42, 0.4, 0xe8b78f, 0, 1.38, 0.01);
addBox(group, 0.47, 0.14, 0.43, definition.hair, 0, 1.58, 0);
addBox(group, 0.12, 0.22, 0.12, definition.hair, -0.22, 1.47, -0.01);
addBox(group, 0.06, 0.06, 0.025, 0x18202b, -0.1, 1.4, 0.216);
addBox(group, 0.06, 0.06, 0.025, 0x18202b, 0.1, 1.4, 0.216);
// Basket on the side.
const basket = new THREE.Group();
basket.position.set(0.46, 0.54, 0.12);
group.add(basket);
addBox(basket, 0.42, 0.07, 0.34, 0x6f4e31, 0, -0.12, 0);
addBox(basket, 0.07, 0.25, 0.07, 0x8b633e, -0.18, 0, 0);
addBox(basket, 0.07, 0.25, 0.07, 0x8b633e, 0.18, 0, 0);
addBox(basket, 0.42, 0.07, 0.07, 0x8b633e, 0, 0.11, -0.14);
// Backpack and visible identical starting inventory.
addBox(group, 0.47, 0.5, 0.18, 0x35475d, 0, 0.84, -0.27);
const tokenMeshes = [];
for (let tokenIndex = 0; tokenIndex < 3; tokenIndex++) {
const token = makeItem("token");
token.scale.setScalar(0.48);
token.position.set(-0.14 + tokenIndex * 0.14, 0.94, -0.39);
group.add(token);
tokenMeshes.push(token);
}
const payloadMeshes = {};
for (const type of ["flour", "potato", "thread"]) {
const payload = makeItem(type);
payload.scale.setScalar(0.38);
payload.position.set(-0.15 + Object.keys(payloadMeshes).length * 0.15, 0.7, -0.39);
group.add(payload);
payloadMeshes[type] = payload;
}
const nameLabel = makeLabel(
definition.name.toUpperCase(),
"#121d2b",
"#eaf2ff",
1.12,
0.36
);
nameLabel.position.set(0, 1.98, 0);
group.add(nameLabel);
const customer = {
...definition,
group,
basket,
tokenMeshes,
payloadMeshes,
basketItems: [],
tokenCount: 3,
barterCount: { flour: 1, potato: 1, thread: 1 },
index
};
customers.push(customer);
}
customerDefinitions.forEach(buildCustomer);
// -------------------------------------------------------------------------
// Camera controls implemented directly, without Three.js addons.
// -------------------------------------------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.azimuth -= deltaX * 0.006;
cameraState.elevation = THREE.MathUtils.clamp(
cameraState.elevation + deltaY * 0.004,
0.32,
1.08
);
updateCamera();
});
function endDrag(event) {
dragging = false;
renderer.domElement.style.cursor = "grab";
if (event.pointerId !== undefined && renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.style.cursor = "grab";
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * (event.deltaY > 0 ? 0.91 : 1.1),
0.72,
1.65
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
let handled = true;
if (key === "arrowleft") cameraState.azimuth += 0.09;
else if (key === "arrowright") cameraState.azimuth -= 0.09;
else if (key === "arrowup") {
cameraState.elevation = Math.min(1.08, cameraState.elevation + 0.06);
} else if (key === "arrowdown") {
cameraState.elevation = Math.max(0.32, cameraState.elevation - 0.06);
} else if (key === "+" || key === "=") {
cameraState.zoom = Math.min(1.65, cameraState.zoom * 1.08);
} else if (key === "-" || key === "_") {
cameraState.zoom = Math.max(0.72, cameraState.zoom * 0.92);
} else {
handled = false;
}
if (handled) {
event.preventDefault();
updateCamera();
}
});
// -------------------------------------------------------------------------
// Simulation and UI
// -------------------------------------------------------------------------
function updateRules() {
const isToken = mode === "token";
ruleModeLabel.textContent = isToken ? "token mode" : "barter mode";
hudMode.textContent = isToken ? "Token exchange" : "Direct barter";
hudSubmode.textContent = isToken
? "One brass token per purchase"
: "Each shop accepts one exact input";
spentLabel.textContent = isToken ? "Tokens spent" : "Barter goods traded";
barterButton.classList.toggle("active", !isToken);
tokenButton.classList.toggle("active", isToken);
barterButton.setAttribute("aria-pressed", String(!isToken));
tokenButton.setAttribute("aria-pressed", String(isToken));
ruleList.innerHTML = isToken
? `
<li><b>Customer need:</b> every customer must obtain 1 bread, 1 apple, and 1 cloth.</li>
<li><b>Payment rule:</b> every shop accepts exactly 1 brass token for any one of its displayed goods.</li>
<li><b>Starting funds:</b> every customer receives exactly 3 tokens, enough for the three fixed visits.</li>
<li><b>Visit order:</b> Moonrise Bakery → Lumen Grocer → Blockwork Tailor.</li>
`
: `
<li><b>Customer need:</b> every customer must obtain 1 bread, 1 apple, and 1 cloth.</li>
<li><b>Payment rule:</b> Bakery accepts 1 flour, Grocer accepts 1 potato, Tailor accepts 1 thread.</li>
<li><b>Exact-match rule:</b> a transaction succeeds only when the customer has the shop's exact listed input.</li>
<li><b>Visit order:</b> Moonrise Bakery → Lumen Grocer → Blockwork Tailor.</li>
`;
}
function removeTransferVisual() {
if (!transferVisual) return;
scene.remove(transferVisual);
transferVisual.traverse((object) => {
if (object.isMesh) object.geometry.dispose();
});
transferVisual = null;
}
function resetCustomerInventory(customer) {
customer.tokenCount = 3;
customer.barterCount = { flour: 1, potato: 1, thread: 1 };
customer.basketItems.forEach((item) => customer.basket.remove(item));
customer.basketItems = [];
customer.tokenMeshes.forEach((mesh, index) => {
mesh.visible = index < customer.tokenCount;
});
for (const type of Object.keys(customer.payloadMeshes)) {
customer.payloadMeshes[type].visible = customer.barterCount[type] > 0;
}
}
function resetSimulation() {
running = false;
elapsed = 0;
transactionIndex = 0;
completed = 0;
lastPhase = "";
removeTransferVisual();
customers.forEach((customer) => {
customer.group.position.copy(customer.start);
customer.group.rotation.y = 0;
resetCustomerInventory(customer);
});
ledger.innerHTML = `
<div class="ledger-empty">
Run the simulation to record each successful transaction.
</div>
`;
ledgerCount.textContent = "0 records";
completedStat.textContent = "0 / 9";
spentStat.textContent = "0";
progressFill.style.width = "0%";
statusElement.textContent = "Ready to simulate.";
}
function startSimulation() {
resetSimulation();
running = true;
statusElement.textContent = "Simulation running with fixed rules…";
}
function addLedgerRecord(transaction) {
const customer = customers[transaction.customerIndex];
const shop = shops[transaction.shopIndex];
const paidType = mode === "token" ? "token" : shop.inputType;
const paidText = mode === "token"
? "1 brass token"
: `1 ${ITEM_NAMES[shop.inputType]}`;
const receivedText = `1 ${ITEM_NAMES[shop.outputType]}`;
const empty = ledger.querySelector(".ledger-empty");
if (empty) empty.remove();
const record = document.createElement("div");
record.className = "transaction";
record.innerHTML = `
<div class="transaction-head">
<strong>${customer.name} → ${shop.name}</strong>
<span class="success">Successful</span>
</div>
<div class="transaction-route">Fixed visit ${transactionIndex + 1} of 9 · rule check passed</div>
<div class="transaction-exchange">
<span class="exchange-swatch" style="background:${ITEM_COLORS[paidType]}"></span>
<span>Paid ${paidText}</span>
<span class="exchange-arrow">→</span>
<span class="exchange-swatch" style="background:${ITEM_COLORS[shop.outputType]}"></span>
<span>Received ${receivedText}</span>
</div>
`;
ledger.prepend(record);
ledgerCount.textContent = `${completed} record${completed === 1 ? "" : "s"}`;
}
function canTransact(transaction) {
const customer = customers[transaction.customerIndex];
const shop = shops[transaction.shopIndex];
return mode === "token"
? customer.tokenCount >= 1
: customer.barterCount[shop.inputType] >= 1;
}
function finalizeTransaction(transaction) {
removeTransferVisual();
const customer = customers[transaction.customerIndex];
const shop = shops[transaction.shopIndex];
const success = canTransact(transaction);
if (!success) {
statusElement.textContent = `${customer.name} could not trade at ${shop.name}.`;
return;
}
if (mode === "token") {
customer.tokenCount -= 1;
} else {
customer.barterCount[shop.inputType] -= 1;
}
customer.tokenMeshes.forEach((mesh, index) => {
mesh.visible = index < customer.tokenCount;
});
customer.payloadMeshes[shop.inputType].visible =
customer.barterCount[shop.inputType] > 0;
// Add a miniature permanent copy of the acquired good to the basket.
const basketItem = makeItem(shop.outputType);
basketItem.scale.setScalar(0.3);
const slot = customer.basketItems.length;
basketItem.position.set(
slot % 2 === 0 ? -0.08 : 0.08,
0.02 + Math.floor(slot / 2) * 0.16,
0
);
customer.basket.add(basketItem);
customer.basketItems.push(basketItem);
completed += 1;
addLedgerRecord(transaction);
completedStat.textContent = `${completed} / 9`;
spentStat.textContent = String(completed);
progressFill.style.width = `${(completed / 9) * 100}%`;
customer.group.position.copy(shop.customerPosition);
customer.group.rotation.y = Math.PI;
}
function beginPayment(transaction) {
removeTransferVisual();
const customer = customers[transaction.customerIndex];
const shop = shops[transaction.shopIndex];
const type = mode === "token" ? "token" : shop.inputType;
transferVisual = makeItem(type);
transferVisual.scale.setScalar(mode === "token" ? 1 : 0.9);
scene.add(transferVisual);
}
function beginReceipt(transaction) {
removeTransferVisual();
const customer = customers[transaction.customerIndex];
const shop = shops[transaction.shopIndex];
transferVisual = makeItem(shop.outputType);
transferVisual.scale.setScalar(0.9);
scene.add(transferVisual);
}
function easeInOut(value) {
return value < 0.5
? 2 * value * value
: 1 - Math.pow(-2 * value + 2, 2) / 2;
}
function lerpAngle(a, b, t) {
let difference = ((b - a + Math.PI) % (Math.PI * 2)) - Math.PI;
if (difference < -Math.PI) difference += Math.PI * 2;
return a + difference * t;
}
function updateTransaction(transaction, localTime) {
const customer = customers[transaction.customerIndex];
const shop = shops[transaction.shopIndex];
const end = shop.customerPosition;
if (localTime < MOVE_DURATION) {
if (lastPhase !== "move") {
removeTransferVisual();
lastPhase = "move";
}
const progress = easeInOut(THREE.MathUtils.clamp(localTime / MOVE_DURATION, 0, 1));
const start = customer.group.position.clone();
const waypoint = new THREE.Vector3(start.x, 0, 1.7);
if (progress < 0.5) {
customer.group.position.lerpVectors(start, waypoint, progress * 2);
} else {
customer.group.position.lerpVectors(waypoint, end, (progress - 0.5) * 2);
}
const movementAngle = Math.atan2(end.x - start.x, end.z - start.z);
customer.group.rotation.y = lerpAngle(customer.group.rotation.y, movementAngle, 0.14);
statusElement.textContent =
`${customer.name} is walking to ${shop.name}…`;
return;
}
customer.group.position.copy(end);
customer.group.rotation.y = Math.PI;
if (localTime < MOVE_DURATION + PAY_DURATION) {
if (lastPhase !== "pay") {
beginPayment(transaction);
lastPhase = "pay";
}
const progress = THREE.MathUtils.clamp(
(localTime - MOVE_DURATION) / PAY_DURATION,
0,
1
);
const eased = easeInOut(progress);
const from = new THREE.Vector3(end.x, 1.12, end.z - 0.32);
const to = shop.counterPosition.clone();
transferVisual.position.lerpVectors(from, to, eased);
transferVisual.position.y += Math.sin(progress * Math.PI) * 0.45;
transferVisual.rotation.y = eased * Math.PI * 1.5;
const paidName = mode === "token"
? "a brass token"
: ITEM_NAMES[shop.inputType];
statusElement.textContent =
`${customer.name} is paying ${paidName} at ${shop.name}…`;
return;
}
if (localTime < MOVE_DURATION + PAY_DURATION + RECEIVE_DURATION) {
if (lastPhase !== "receive") {
beginReceipt(transaction);
lastPhase = "receive";
}
const progress = THREE.MathUtils.clamp(
(localTime - MOVE_DURATION - PAY_DURATION) / RECEIVE_DURATION,
0,
1
);
const eased = easeInOut(progress);
const from = shop.counterPosition.clone();
const to = new THREE.Vector3();
customer.basket.getWorldPosition(to);
to.y += 0.13;
transferVisual.position.lerpVectors(from, to, eased);
transferVisual.position.y += Math.sin(progress * Math.PI) * 0.38;
transferVisual.rotation.y = eased * Math.PI * 1.25;
statusElement.textContent =
`${shop.name} is giving ${customer.name} 1 ${ITEM_NAMES[shop.outputType]}…`;
}
}
function updateSimulation(delta) {
if (!running) return;
elapsed += delta * speed;
while (transactionIndex < 9) {
const transactionIndexForEvent = transactionIndex;
const customerIndex = Math.floor(transactionIndexForEvent / shopDefinitions.length);
const shopIndex = transactionIndexForEvent % shopDefinitions.length;
const transaction = { customerIndex, shopIndex };
const localTime = elapsed - transactionIndex * TRANSACTION_DURATION;
if (localTime < 0) break;
if (localTime >= TRANSACTION_DURATION) {
finalizeTransaction(transaction);
transactionIndex += 1;
lastPhase = "";
continue;
}
updateTransaction(transaction, localTime);
break;
}
if (transactionIndex >= 9) {
running = false;
completed = 9;
completedStat.textContent = "9 / 9";
progressFill.style.width = "100%";
statusElement.textContent =
"Simulation complete · all 9 fixed transactions succeeded.";
}
}
// -------------------------------------------------------------------------
// Interaction
// -------------------------------------------------------------------------
function setMode(nextMode) {
if (mode === nextMode) return;
mode = nextMode;
updateRules();
startSimulation();
}
barterButton.addEventListener("click", () => setMode("barter"));
tokenButton.addEventListener("click", () => setMode("token"));
runButton.addEventListener("click", startSimulation);
resetButton.addEventListener("click", resetSimulation);
speedButton.addEventListener("click", () => {
const speeds = [1, 1.5, 2];
speed = speeds[(speeds.indexOf(speed) + 1) % speeds.length];
speedButton.textContent = `${speed}×`;
});
updateRules();
resetSimulation();
// Begin in token mode so successful exchanges are visible immediately.
window.setTimeout(startSimulation, 550);
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let sceneTime = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
sceneTime += delta;
updateSimulation(delta);
for (let index = 0; index < shops.length; index++) {
const shop = shops[index];
shop.displayItem.position.y = 1.52 + Math.sin(sceneTime * 2 + index) * 0.055;
shop.displayItem.rotation.y = sceneTime * 0.45 + index;
}
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```