Prompt specification
Build or customize the Three.js 3D scene "Bread and Pot Exchange Table" 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: Bread and Pot Exchange Table
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/bread-and-pot-exchange-table-x3134
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring comparative advantage and production opportunity costs. Show two fictional producers choosing bread or pottery with explicit productivity rates. Calculate opportunity costs in labels before allowing an exchange at a user-selected ratio. Voxel style.
Working Three.js Starter (bread-and-pot-exchange-table-x3134):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Comparative Edge — Voxel Trade Lab</title>
<style>
:root {
--ink: #13272d;
--muted: #66777a;
--paper: rgba(248, 246, 235, 0.94);
--panel: rgba(17, 39, 45, 0.93);
--line: rgba(255, 255, 255, 0.14);
--bread: #e4b04f;
--bread-dark: #9b6329;
--pottery: #d96f4c;
--pottery-dark: #913f32;
--green: #77bd9b;
--green-dark: #3f765e;
--cream: #fff4cf;
--bad: #e06b65;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #c9dfd8;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#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;
z-index: 2;
background:
radial-gradient(circle at 50% 42%, transparent 45%, rgba(15, 42, 43, 0.13) 100%),
linear-gradient(to bottom, rgba(255, 255, 255, 0.12), transparent 28%);
}
.topbar {
position: fixed;
z-index: 20;
top: 20px;
left: 24px;
right: 24px;
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 20px;
pointer-events: none;
}
.brand {
display: flex;
gap: 12px;
align-items: flex-start;
}
.brand-mark {
width: 42px;
height: 42px;
flex: 0 0 auto;
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 3px;
padding: 5px;
border-radius: 10px;
background: var(--ink);
box-shadow: 0 8px 30px rgba(19, 39, 45, 0.18);
transform: rotate(-2deg);
}
.brand-mark span {
background: var(--bread);
border-radius: 2px;
}
.brand-mark span:nth-child(2),
.brand-mark span:nth-child(6) {
background: var(--pottery);
}
.brand-mark span:nth-child(4),
.brand-mark span:nth-child(8) {
background: var(--green);
}
.eyebrow {
margin: 1px 0 4px;
color: #416166;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
max-width: 620px;
font-size: clamp(25px, 3vw, 42px);
line-height: 0.98;
letter-spacing: -0.045em;
}
.subtitle {
margin: 9px 0 0;
color: #536d70;
font-size: 13px;
font-weight: 600;
}
.top-actions {
display: flex;
align-items: center;
gap: 9px;
pointer-events: auto;
}
.step-badge,
.view-button {
min-height: 38px;
border: 1px solid rgba(19, 39, 45, 0.13);
border-radius: 9px;
background: rgba(248, 246, 235, 0.86);
box-shadow: 0 8px 24px rgba(19, 39, 45, 0.09);
color: var(--ink);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.step-badge {
display: flex;
align-items: center;
padding: 0 13px;
}
.view-button {
padding: 0 12px;
cursor: pointer;
}
.view-button:hover {
background: #fff9e7;
}
#labels {
position: fixed;
inset: 0;
z-index: 5;
pointer-events: none;
overflow: hidden;
}
.world-label {
position: absolute;
width: 0;
height: 0;
will-change: transform;
}
.label-card {
position: absolute;
left: 0;
top: 0;
width: 232px;
transform: translate(-50%, -100%);
overflow: hidden;
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 13px;
background: rgba(17, 39, 45, 0.91);
color: white;
box-shadow: 0 15px 40px rgba(19, 39, 45, 0.2);
backdrop-filter: blur(10px);
}
.label-card::after {
content: "";
position: absolute;
left: 50%;
bottom: -7px;
width: 12px;
height: 12px;
background: rgba(17, 39, 45, 0.91);
transform: translateX(-50%) rotate(45deg);
}
.label-head {
display: flex;
align-items: center;
justify-content: space-between;
padding: 12px 13px 0;
}
.label-kicker {
display: flex;
align-items: center;
gap: 7px;
color: #b7c9c8;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.zone-dot {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--green);
box-shadow: 0 0 0 3px rgba(119, 189, 155, 0.13);
}
.pottery-zone .zone-dot {
background: var(--pottery);
box-shadow: 0 0 0 3px rgba(217, 111, 76, 0.15);
}
.locked-pill {
padding: 4px 6px;
border-radius: 5px;
background: rgba(255, 255, 255, 0.08);
color: #9fb4b4;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.label-name {
margin: 4px 13px 0;
font-size: 22px;
line-height: 1;
letter-spacing: -0.035em;
}
.label-rate {
margin: 7px 13px 12px;
color: #d8e3df;
font-size: 11px;
font-weight: 700;
line-height: 1.35;
}
.label-rate strong {
color: white;
}
.oc-details {
display: grid;
gap: 6px;
padding: 10px 13px 12px;
border-top: 1px solid var(--line);
background: rgba(255, 255, 255, 0.045);
color: #d8e4df;
font-size: 10px;
line-height: 1.35;
}
.oc-details b {
color: white;
}
.advantage {
display: inline-flex;
width: fit-content;
margin-top: 3px;
padding: 5px 7px;
border-radius: 5px;
background: var(--green-dark);
color: #effff6;
font-size: 8px;
font-weight: 900;
letter-spacing: 0.08em;
}
.pottery-zone .advantage {
background: var(--pottery-dark);
color: #fff0e9;
}
.market-label .label-card {
width: 214px;
text-align: center;
}
.market-label .label-head {
justify-content: center;
}
.market-label .label-rate {
text-align: center;
}
.dock {
position: fixed;
z-index: 20;
left: 50%;
bottom: 18px;
width: min(1160px, calc(100% - 36px));
max-height: 50vh;
overflow: auto;
transform: translateX(-50%);
border: 1px solid rgba(255, 255, 255, 0.46);
border-radius: 18px;
background: var(--paper);
box-shadow: 0 25px 70px rgba(19, 39, 45, 0.23);
backdrop-filter: blur(18px);
}
.control-grid {
display: grid;
grid-template-columns: 1fr 1.25fr 1fr;
gap: 0;
padding: 14px;
}
.control-block {
min-width: 0;
padding: 8px 18px;
border-right: 1px solid rgba(19, 39, 45, 0.11);
}
.control-block:first-child {
padding-left: 8px;
}
.control-block:last-child {
padding-right: 8px;
border-right: 0;
}
.control-heading {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 4px;
}
.step-number {
display: grid;
width: 26px;
height: 26px;
flex: 0 0 auto;
place-items: center;
border-radius: 7px;
background: var(--ink);
color: white;
font-size: 10px;
font-weight: 900;
}
.control-heading h2,
.control-heading h3 {
margin: 0;
font-size: 13px;
letter-spacing: -0.01em;
}
.control-copy {
min-height: 32px;
margin: 5px 0 10px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.primary-button,
.trade-button {
width: 100%;
min-height: 41px;
border: 0;
border-radius: 9px;
cursor: pointer;
font-size: 11px;
font-weight: 900;
letter-spacing: 0.035em;
transition:
transform 150ms ease,
background 150ms ease,
opacity 150ms ease;
}
.primary-button {
background: var(--ink);
color: white;
}
.primary-button:hover:not(:disabled),
.trade-button:hover:not(:disabled) {
transform: translateY(-1px);
}
.primary-button:disabled {
cursor: default;
opacity: 0.66;
}
.trade-button {
background: var(--green-dark);
color: white;
}
.trade-button:disabled {
background: #a7afaa;
color: #edf0eb;
cursor: not-allowed;
}
.trade-button.invalid {
background: #b76b62;
}
.ratio-output {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 8px;
margin-top: 9px;
}
.ratio-output label {
color: var(--muted);
font-size: 10px;
font-weight: 700;
}
.ratio-output output {
color: var(--ink);
font-size: 17px;
font-weight: 900;
letter-spacing: -0.03em;
}
.ratio-output output small {
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0;
}
input[type="range"] {
width: 100%;
height: 22px;
margin: 4px 0 0;
accent-color: var(--green-dark);
cursor: pointer;
}
input[type="range"]:disabled {
cursor: not-allowed;
opacity: 0.42;
}
.ratio-hint {
min-height: 14px;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
}
.ratio-hint.valid {
color: var(--green-dark);
}
.ratio-hint.invalid {
color: #a34d46;
}
.insight-grid {
display: grid;
grid-template-columns: 1fr 1fr 1.15fr;
gap: 9px;
padding: 0 14px 14px;
}
.insight-card {
min-height: 104px;
padding: 12px 13px;
border: 1px solid rgba(19, 39, 45, 0.1);
border-radius: 11px;
background: rgba(255, 255, 255, 0.58);
}
.insight-card.alex {
border-top: 3px solid var(--green);
}
.insight-card.blair {
border-top: 3px solid var(--pottery);
}
.insight-card.market {
border-top: 3px solid var(--bread);
}
.insight-title {
display: flex;
justify-content: space-between;
gap: 8px;
margin-bottom: 7px;
font-size: 11px;
font-weight: 900;
}
.insight-title span:last-child {
color: var(--muted);
font-size: 8px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.equation {
display: flex;
justify-content: space-between;
gap: 10px;
margin: 4px 0;
color: #435b5e;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 9px;
}
.equation b {
color: var(--ink);
}
.edge-tag {
display: inline-block;
margin-top: 6px;
padding: 4px 6px;
border-radius: 5px;
background: rgba(63, 118, 94, 0.12);
color: var(--green-dark);
font-size: 8px;
font-weight: 900;
letter-spacing: 0.08em;
}
.blair .edge-tag {
background: rgba(145, 63, 50, 0.1);
color: var(--pottery-dark);
}
.locked-copy {
display: flex;
min-height: 55px;
align-items: center;
color: #879493;
font-size: 10px;
font-weight: 700;
}
.costs-only {
display: none;
}
body.costs-revealed .costs-only {
display: block;
}
body.costs-revealed .locked-copy {
display: none;
}
.market-copy {
color: #53696b;
font-size: 10px;
line-height: 1.5;
}
.market-copy strong {
color: var(--ink);
}
.ledger {
margin-top: 6px;
padding-top: 6px;
border-top: 1px solid rgba(19, 39, 45, 0.1);
color: #53696b;
font-size: 9px;
line-height: 1.45;
}
#live {
position: fixed;
width: 1px;
height: 1px;
overflow: hidden;
clip: rect(0 0 0 0);
white-space: nowrap;
}
#loading {
position: fixed;
z-index: 100;
inset: 0;
display: grid;
place-items: center;
background: #c9dfd8;
color: var(--ink);
font-size: 12px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
transition: opacity 300ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 800px) {
.topbar {
top: 12px;
left: 12px;
right: 12px;
}
.brand-mark {
display: none;
}
h1 {
font-size: 25px;
}
.subtitle,
.view-button {
display: none;
}
.step-badge {
padding: 0 9px;
font-size: 9px;
}
.dock {
bottom: 8px;
width: calc(100% - 16px);
max-height: 57vh;
border-radius: 14px;
}
.control-grid {
grid-template-columns: 1fr;
gap: 9px;
padding: 10px;
}
.control-block,
.control-block:first-child,
.control-block:last-child {
padding: 9px;
border-right: 0;
border-bottom: 1px solid rgba(19, 39, 45, 0.1);
}
.control-block:last-child {
border-bottom: 0;
}
.control-copy {
min-height: 0;
}
.insight-grid {
grid-template-columns: 1fr;
padding: 0 10px 10px;
}
.insight-card {
min-height: 0;
}
.label-card {
width: 176px;
}
.label-name {
font-size: 17px;
}
.label-rate {
font-size: 9px;
}
.oc-details {
font-size: 8px;
}
.market-label .label-card {
width: 164px;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive voxel economy scene"></div>
<div class="vignette"></div>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<span></span><span></span><span></span>
<span></span><span></span><span></span>
<span></span><span></span><span></span>
</div>
<div>
<div class="eyebrow">Voxel economics / trade lab</div>
<h1>Comparative advantage, in motion.</h1>
<p class="subtitle">Calculate the cost, choose the rate, then move the goods.</p>
</div>
</div>
<div class="top-actions">
<div id="stepBadge" class="step-badge">1 / Calculate costs</div>
<button id="resetView" class="view-button" type="button">Reset view</button>
</div>
</header>
<div id="labels">
<div id="alexLabel" class="world-label producer-label"></div>
<div id="blairLabel" class="world-label producer-label pottery-zone"></div>
<div id="marketLabel" class="world-label market-label"></div>
</div>
<section class="dock" aria-label="Economic controls">
<div class="control-grid">
<article class="control-block">
<div class="control-heading">
<span class="step-number">01</span>
<h2>Calculate costs</h2>
</div>
<p class="control-copy">
Compare what each producer gives up when making the other good.
</p>
<button id="analyzeButton" class="primary-button" type="button">
Calculate opportunity costs
</button>
</article>
<article class="control-block">
<div class="control-heading">
<span class="step-number">02</span>
<h3>Choose the exchange ratio</h3>
</div>
<div class="ratio-output">
<label for="ratio">Bread received per pottery</label>
<output id="ratioOutput" for="ratio">1.00 <small>bread / pottery</small></output>
</div>
<input
id="ratio"
type="range"
min="0.25"
max="2.25"
step="0.05"
value="1"
disabled
aria-label="Bread received per pottery"
/>
<div id="ratioHint" class="ratio-hint">Costs must be calculated before selecting a rate.</div>
</article>
<article class="control-block">
<div class="control-heading">
<span class="step-number">03</span>
<h3>Make the exchange</h3>
</div>
<p id="tradeLogic" class="control-copy">
Unlock the market after the opportunity-cost labels are calculated.
</p>
<button id="tradeButton" class="trade-button" type="button" disabled>
Trade is locked
</button>
</article>
</div>
<div class="insight-grid">
<article class="insight-card alex">
<div class="insight-title">
<span>Alex's opportunity cost</span>
<span>Producer A</span>
</div>
<div class="locked-copy">Hidden until costs are calculated.</div>
<div class="costs-only">
<div class="equation"><span>1 bread</span><b>= 0.50 pottery</b></div>
<div class="equation"><span>1 pottery</span><b>= 2.00 bread</b></div>
<span class="edge-tag">Comparative advantage: bread</span>
</div>
</article>
<article class="insight-card blair">
<div class="insight-title">
<span>Blair's opportunity cost</span>
<span>Producer B</span>
</div>
<div class="locked-copy">Hidden until costs are calculated.</div>
<div class="costs-only">
<div class="equation"><span>1 bread</span><b>= 3.00 pottery</b></div>
<div class="equation"><span>1 pottery</span><b>= 0.33 bread</b></div>
<span class="edge-tag">Comparative advantage: pottery</span>
</div>
</article>
<article class="insight-card market">
<div class="insight-title">
<span>Market check</span>
<span>Both must gain</span>
</div>
<div class="locked-copy">The exchange range appears after cost calculation.</div>
<div id="marketInsight" class="costs-only market-copy"></div>
</article>
</div>
</section>
<div id="live" aria-live="polite"></div>
<div id="loading">Building the trade lab…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene and renderer
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xc9dfd8);
scene.fog = new THREE.Fog(0xc9dfd8, 22, 43);
const camera = new THREE.PerspectiveCamera(
38,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance",
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.domElement.setAttribute(
"aria-label",
"Voxel scene with two producers, Alex and Blair, and a central market"
);
document.querySelector("#scene").appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xf5fff8, 0x536d66, 2.25);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff0cf, 3.1);
sun.position.set(-7, 13, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -14;
sun.shadow.camera.right = 14;
sun.shadow.camera.top = 13;
sun.shadow.camera.bottom = -13;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0004;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x9fd7d0, 1.0);
rim.position.set(10, 7, -10);
scene.add(rim);
// -------------------------------------------------------------------------
// Voxel helpers
// -------------------------------------------------------------------------
const staticMaterials = new Map();
function materialFor(color) {
if (!staticMaterials.has(color)) {
staticMaterials.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.88,
metalness: 0,
flatShading: true,
})
);
}
return staticMaterials.get(color);
}
function addBox(
parent,
width,
height,
depth,
color,
x = 0,
y = 0,
z = 0,
outlined = false
) {
const geometry = new THREE.BoxGeometry(width, height, depth);
const mesh = new THREE.Mesh(geometry, materialFor(color));
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
if (outlined) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry),
new THREE.LineBasicMaterial({
color: 0x244147,
transparent: true,
opacity: 0.26,
})
);
mesh.add(edges);
}
return mesh;
}
function addSign(parent, text, x, y, z, width = 1.8, background = "#13272d") {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.fillStyle = background;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = "rgba(255,255,255,0.22)";
ctx.lineWidth = 8;
ctx.strokeRect(5, 5, canvas.width - 10, canvas.height - 10);
ctx.fillStyle = "#fff4cf";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "900 34px monospace";
const lines = text.split("\n");
lines.forEach((line, index) => {
const yPosition =
lines.length === 1 ? 64 : 43 + index * 42;
ctx.fillText(line, 128, yPosition);
});
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.magFilter = THREE.NearestFilter;
texture.minFilter = THREE.NearestFilter;
const sign = new THREE.Mesh(
new THREE.PlaneGeometry(width, width * 0.5),
new THREE.MeshBasicMaterial({
map: texture,
toneMapped: false,
})
);
sign.position.set(x, y, z);
parent.add(sign);
return sign;
}
function addTree(x, z, scale = 1) {
const tree = new THREE.Group();
tree.position.set(x, 0.13, z);
tree.scale.setScalar(scale);
addBox(tree, 0.28, 1.3, 0.28, 0x76513a, 0, 0.65, 0);
addBox(tree, 1.2, 0.7, 1.2, 0x4f8b69, 0, 1.45, 0);
addBox(tree, 0.85, 0.55, 0.85, 0x66a67b, 0, 1.95, 0);
addBox(tree, 0.45, 0.4, 0.45, 0x7fba86, 0, 2.33, 0);
scene.add(tree);
}
function addCloud(x, y, z, scale = 1) {
const cloud = new THREE.Group();
cloud.position.set(x, y, z);
cloud.scale.setScalar(scale);
addBox(cloud, 2.1, 0.5, 0.75, 0xf4f0dc, 0, 0, 0);
addBox(cloud, 0.8, 0.7, 0.75, 0xf8f4e3, -0.8, -0.1, 0);
addBox(cloud, 0.95, 0.85, 0.75, 0xfff8e8, 0.8, -0.05, 0);
scene.add(cloud);
}
// -------------------------------------------------------------------------
// Floating voxel terrain
// -------------------------------------------------------------------------
const world = new THREE.Group();
scene.add(world);
addBox(world, 14.4, 0.72, 9.4, 0x4f6e68, 0, -0.48, 0, true);
addBox(world, 14.65, 0.18, 9.65, 0x3e5c57, 0, -0.84, 0);
const greenTiles = [0x76b394, 0x82ba9a, 0x6da98c, 0x8cc1a1];
const clayTiles = [0xd2a071, 0xd9aa7a, 0xc99567, 0xe0b184];
const pathTiles = [0xd5c3a0, 0xcbb995, 0xddcba8];
let tileSeed = 41;
function tileRandom() {
tileSeed = (tileSeed * 9301 + 49297) % 233280;
return tileSeed / 233280;
}
for (let x = 0; x < 14; x++) {
for (let z = 0; z < 9; z++) {
let color;
if (x === 6 || x === 7 || (x === 5 && z > 5)) {
color = pathTiles[Math.floor(tileRandom() * pathTiles.length)];
} else if (x < 6) {
color = greenTiles[Math.floor(tileRandom() * greenTiles.length)];
} else {
color = clayTiles[Math.floor(tileRandom() * clayTiles.length)];
}
addBox(
world,
0.98,
0.18,
0.98,
color,
-6.5 + x,
0.02,
-4 + z
);
}
}
// Low perimeter blocks give the diorama a constructed edge.
for (let x = -7; x <= 7; x++) {
addBox(world, 0.95, 0.36, 0.28, 0x6d8e82, x, 0.13, -4.25);
addBox(world, 0.95, 0.36, 0.28, 0x6d8e82, x, 0.13, 4.25);
}
for (let z = -3; z <= 3; z++) {
addBox(world, 0.28, 0.36, 0.95, 0x6d8e82, -7.25, 0.13, z);
addBox(world, 0.28, 0.36, 0.95, 0x6d8e82, 7.25, 0.13, z);
}
// -------------------------------------------------------------------------
// Alex: bread producer
// -------------------------------------------------------------------------
const alexSite = new THREE.Group();
alexSite.position.set(-4.25, 0.12, -1.25);
world.add(alexSite);
addBox(alexSite, 3.25, 0.26, 2.5, 0xc5ad79, 0, 0.13, 0, true);
addBox(alexSite, 2.5, 1.65, 1.7, 0xf0d99f, 0, 1.08, 0, true);
addBox(alexSite, 2.8, 0.32, 1.95, 0x9a5e38, 0, 2.05, 0, true);
addBox(alexSite, 2.25, 0.28, 1.55, 0xb46e3f, 0, 2.34, 0);
addBox(alexSite, 1.65, 0.22, 1.2, 0xc17a45, 0, 2.58, 0);
addBox(alexSite, 0.55, 0.95, 0.08, 0x334b48, 0.55, 0.85, 0.89);
addBox(alexSite, 0.38, 0.38, 0.1, 0x8bc9c7, -0.62, 1.32, 0.89);
addBox(alexSite, 0.28, 0.08, 0.12, 0xfff3bd, -0.62, 1.32, 0.96);
addBox(alexSite, 0.28, 0.08, 0.12, 0xfff3bd, -0.62, 1.32, 0.96).rotation.z = Math.PI / 2;
addSign(alexSite, "ALEX\nBREAD", 0, 2.55, 0.79, 1.8, "#285a4a");
// Brick oven and chimney.
addBox(alexSite, 0.92, 1.05, 0.9, 0x9f4e39, -1.7, 0.83, 0.6, true);
addBox(alexSite, 0.52, 0.42, 0.12, 0x2c2d2b, -1.7, 0.63, 1.08);
addBox(alexSite, 0.3, 0.95, 0.3, 0x754838, -1.9, 1.78, 0.45);
addBox(alexSite, 0.42, 0.16, 0.42, 0x4a3b35, -1.9, 2.29, 0.45);
// Loaves on a display table.
addBox(alexSite, 1.45, 0.18, 0.75, 0x8a623c, 1.45, 0.6, 0.6);
for (let i = 0; i < 3; i++) {
addBox(alexSite, 0.34, 0.23, 0.24, 0xe7b45c, 0.95 + i * 0.42, 0.79, 0.6, true);
}
// Grain field.
const wheat = new THREE.Group();
wheat.position.set(-4.9, 0.14, 2.35);
world.add(wheat);
addBox(wheat, 2.6, 0.12, 1.35, 0x9d7247, 0, 0.06, 0);
for (let row = 0; row < 3; row++) {
for (let col = 0; col < 6; col++) {
addBox(wheat, 0.08, 0.55, 0.08, 0xc99a3f, -1.05 + col * 0.42, 0.37, -0.45 + row * 0.45);
addBox(wheat, 0.18, 0.25, 0.18, 0xf0c75f, -1.05 + col * 0.42, 0.69, -0.45 + row * 0.45);
}
}
// -------------------------------------------------------------------------
// Blair: pottery producer
// -------------------------------------------------------------------------
const blairSite = new THREE.Group();
blairSite.position.set(4.1, 0.12, -1.25);
world.add(blairSite);
addBox(blairSite, 3.25, 0.26, 2.5, 0xb27e58, 0, 0.13, 0, true);
addBox(blairSite, 2.5, 1.65, 1.7, 0xd9825d, 0, 1.08, 0, true);
addBox(blairSite, 2.8, 0.32, 1.95, 0x7e4438, 0, 2.05, 0, true);
addBox(blairSite, 2.25, 0.28, 1.55, 0x9d5140, 0, 2.34, 0);
addBox(blairSite, 1.65, 0.22, 1.2, 0xb85f48, 0, 2.58, 0);
addBox(blairSite, 0.55, 0.95, 0.08, 0x3b3332, 0.55, 0.85, 0.89);
addBox(blairSite, 0.38, 0.38, 0.1, 0xa9d4cb, -0.62, 1.32, 0.89);
addBox(blairSite, 0.28, 0.08, 0.12, 0xfff3bd, -0.62, 1.32, 0.96);
addSign(blairSite, "BLAIR\nPOTTERY", 0, 2.55, 0.79, 1.8, "#7b3f35");
// Voxel kiln.
addBox(blairSite, 1.0, 1.2, 1.0, 0xa64f3d, -1.68, 0.91, 0.48, true);
addBox(blairSite, 0.58, 0.68, 0.12, 0x352f2d, -1.68, 0.68, 1.02);
addBox(blairSite, 0.62, 0.3, 0.62, 0x6c3a32, -1.68, 1.65, 0.48);
addBox(blairSite, 0.26, 0.8, 0.26, 0x704137, -1.9, 2.12, 0.35);
// Pottery display table and block-built vessels.
addBox(blairSite, 1.55, 0.18, 0.8, 0x77503a, 1.45, 0.6, 0.58);
function addPot(parent, x, y, z, color = 0xc96949) {
addBox(parent, 0.38, 0.28, 0.38, color, x, y + 0.14, z, true);
addBox(parent, 0.22, 0.22, 0.22, color, x, y + 0.39, z);
addBox(parent, 0.3, 0.08, 0.3, 0xe09a67, x, y + 0.53, z);
}
addPot(blairSite, 0.98, 0.69, 0.58);
addPot(blairSite, 1.43, 0.69, 0.58, 0xd97954);
addPot(blairSite, 1.88, 0.69, 0.58, 0xb95c43);
// Clay plot.
const clayPlot = new THREE.Group();
clayPlot.position.set(4.7, 0.14, 2.35);
world.add(clayPlot);
addBox(clayPlot, 2.6, 0.12, 1.35, 0x9e6349, 0, 0.06, 0);
for (let row = 0; row < 3; row++) {
for (let col = 0; col < 6; col++) {
addBox(
clayPlot,
0.28,
0.18,
0.28,
(row + col) % 2 ? 0xc27353 : 0xae6047,
-1.05 + col * 0.42,
0.21,
-0.45 + row * 0.45
);
}
}
// -------------------------------------------------------------------------
// Market, landscaping, and specialization markers
// -------------------------------------------------------------------------
const market = new THREE.Group();
market.position.set(0, 0.13, 1.55);
world.add(market);
addBox(market, 3.0, 0.25, 1.8, 0xdbc9a7, 0, 0.13, 0, true);
addBox(market, 0.16, 1.65, 0.16, 0x506b68, 0, 1.05, 0);
addBox(market, 1.9, 0.14, 0.16, 0x506b68, 0, 1.62, 0);
addBox(market, 0.58, 0.12, 0.5, 0xe3b451, -0.85, 1.39, 0, true);
addBox(market, 0.58, 0.12, 0.5, 0xd66c4d, 0.85, 1.39, 0, true);
addBox(market, 0.14, 0.7, 0.14, 0x506b68, -0.85, 0.78, 0);
addBox(market, 0.14, 0.7, 0.14, 0x506b68, 0.85, 0.78, 0);
// Central exchange tokens.
addBox(market, 0.45, 0.25, 0.3, 0xe3b451, -1.05, 0.38, 0.45, true);
addBox(market, 0.35, 0.38, 0.35, 0xd66c4d, 1.02, 0.45, 0.4, true);
addTree(-6.1, 2.8, 0.9);
addTree(-1.8, -3.1, 0.75);
addTree(1.9, -3.1, 0.75);
addTree(6.1, 2.8, 0.88);
addTree(-6.2, -3.0, 0.65);
addTree(6.25, -3.0, 0.65);
addCloud(-5.7, 6.1, -3.2, 0.9);
addCloud(4.8, 6.7, -4.4, 0.7);
addCloud(0.4, 7.4, -5.7, 0.55);
function createMarker(x, z, width, depth, color) {
const marker = new THREE.Group();
marker.position.set(x, 0.29, z);
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.9,
});
const thickness = 0.08;
const height = 0.08;
const north = new THREE.Mesh(
new THREE.BoxGeometry(width, height, thickness),
material
);
north.position.z = -depth / 2;
const south = north.clone();
south.position.z = depth / 2;
const west = new THREE.Mesh(
new THREE.BoxGeometry(thickness, height, depth),
material
);
west.position.x = -width / 2;
const east = west.clone();
east.position.x = width / 2;
marker.add(north, south, west, east);
marker.visible = false;
scene.add(marker);
return marker;
}
const breadMarker = createMarker(-4.9, 2.35, 2.85, 1.6, 0xf0c75f);
const potteryMarker = createMarker(4.55, 0.25, 1.9, 1.25, 0xf18b68);
// -------------------------------------------------------------------------
// DOM world labels projected from Three.js coordinates
// -------------------------------------------------------------------------
const producers = {
alex: {
name: "Alex",
bread: 2,
pottery: 1,
color: "#77bd9b",
},
blair: {
name: "Blair",
bread: 1,
pottery: 3,
color: "#d96f4c",
},
};
const alexLabel = document.querySelector("#alexLabel");
const blairLabel = document.querySelector("#blairLabel");
const marketLabel = document.querySelector("#marketLabel");
const anchors = [
{ element: alexLabel, position: new THREE.Vector3(-4.25, 4.0, -1.25) },
{ element: blairLabel, position: new THREE.Vector3(4.1, 4.0, -1.25) },
{ element: marketLabel, position: new THREE.Vector3(0, 3.35, 1.55) },
];
function formatNumber(number) {
return Number.isInteger(number)
? number.toFixed(0)
: number.toFixed(2);
}
function producerLabelMarkup(producer) {
const ocBread = producer.pottery / producer.bread;
const ocPottery = producer.bread / producer.pottery;
const advantage =
producer.name === "Alex" ? "BREAD" : "POTTERY";
return `
<div class="label-card">
<div class="label-head">
<div class="label-kicker">
<span class="zone-dot"></span>
Producer ${producer.name === "Alex" ? "A" : "B"}
</div>
<span class="locked-pill">costs locked</span>
</div>
<h3 class="label-name">${producer.name}</h3>
<div class="label-rate">
<strong>${producer.bread} bread</strong> or
<strong>${producer.pottery} pottery</strong> per hour
</div>
<div class="oc-details">
<div><b>OC:</b> 1 bread = ${formatNumber(ocBread)} pottery</div>
<div><b>OC:</b> 1 pottery = ${formatNumber(ocPottery)} bread</div>
<span class="advantage">COMPARATIVE EDGE: ${advantage}</span>
</div>
</div>
`;
}
function updateProducerLabels() {
alexLabel.innerHTML = producerLabelMarkup(producers.alex);
blairLabel.innerHTML = producerLabelMarkup(producers.blair);
alexLabel.querySelector(".locked-pill").textContent = "OC calculated";
}
let costsRevealed = false;
function updateMarketLabel(ratio, valid) {
if (!costsRevealed) {
marketLabel.innerHTML = `
<div class="label-card">
<div class="label-head">
<div class="label-kicker">Exchange market</div>
</div>
<h3 class="label-name">Calculate first</h3>
<div class="label-rate">Unlock the mutually beneficial rate.</div>
</div>
`;
return;
}
marketLabel.innerHTML = `
<div class="label-card">
<div class="label-head">
<div class="label-kicker">Exchange market</div>
</div>
<h3 class="label-name">1 : ${ratio.toFixed(2)}</h3>
<div class="label-rate">
1 pottery ↔ ${ratio.toFixed(2)} bread<br>
<strong>${valid ? "MUTUAL GAIN" : "OUTSIDE FAIR RANGE"}</strong>
</div>
</div>
`;
}
updateProducerLabels();
updateMarketLabel(1, true);
function projectLabels() {
camera.updateMatrixWorld();
for (const anchor of anchors) {
const projected = anchor.position.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.25 &&
projected.x < 1.25 &&
projected.y > -1.25 &&
projected.y < 1.25;
anchor.element.style.opacity = visible ? "1" : "0";
const x = (projected.x * 0.5 + 0.5) * innerWidth;
const y = (-projected.y * 0.5 + 0.5) * innerHeight;
anchor.element.style.transform = `translate(${x}px, ${y}px)`;
}
}
// -------------------------------------------------------------------------
// Custom camera controls: drag to orbit, wheel/pinch to zoom
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 0.9, 0);
const desired = {
azimuth: 0.68,
polar: 0.94,
radius: 18.2,
};
const current = {
azimuth: desired.azimuth,
polar: desired.polar,
radius: desired.radius,
};
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
desired.azimuth -= dx * 0.006;
desired.polar = THREE.MathUtils.clamp(
desired.polar - dy * 0.005,
0.42,
1.25
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
});
function stopDragging(event) {
dragging = false;
renderer.domElement.classList.remove("dragging");
if (event && 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();
desired.radius = THREE.MathUtils.clamp(
desired.radius + event.deltaY * 0.012,
11,
28
);
},
{ passive: false }
);
function resetCamera() {
desired.azimuth = 0.68;
desired.polar = 0.94;
desired.radius = 18.2;
}
document.querySelector("#resetView").addEventListener("click", resetCamera);
renderer.domElement.addEventListener("dblclick", resetCamera);
// -------------------------------------------------------------------------
// Economic state and UI
// -------------------------------------------------------------------------
const analyzeButton = document.querySelector("#analyzeButton");
const ratioInput = document.querySelector("#ratio");
const ratioOutput = document.querySelector("#ratioOutput");
const ratioHint = document.querySelector("#ratioHint");
const tradeButton = document.querySelector("#tradeButton");
const tradeLogic = document.querySelector("#tradeLogic");
const marketInsight = document.querySelector("#marketInsight");
const stepBadge = document.querySelector("#stepBadge");
const live = document.querySelector("#live");
let currentRatio = Number(ratioInput.value);
let tradeAnimation = null;
let tradeCount = 0;
// Blair exchanges 1 pottery for X bread from Alex.
//
// Alex benefits if X < 2 bread, the domestic cost of 1 pottery.
// Blair benefits if 1/X < 3 pottery, the domestic cost of 1 bread.
// Therefore the mutually beneficial range is 1/3 < X < 2.
function rateIsValid(ratio) {
return ratio > 1 / 3 + 0.0001 && ratio < 2 - 0.0001;
}
function updateTradeState() {
ratioOutput.innerHTML = `${currentRatio.toFixed(2)} <small>bread / pottery</small>`;
const valid = rateIsValid(currentRatio);
ratioHint.classList.toggle("valid", costsRevealed && valid);
ratioHint.classList.toggle("invalid", costsRevealed && !valid);
if (!costsRevealed) {
ratioHint.textContent = "Costs must be calculated before selecting a rate.";
tradeLogic.textContent =
"Unlock the market after the opportunity-cost labels are calculated.";
tradeButton.textContent = "Trade is locked";
tradeButton.disabled = true;
tradeButton.classList.remove("invalid");
stepBadge.textContent = "1 / Calculate costs";
updateMarketLabel(currentRatio, valid);
return;
}
if (!valid) {
ratioHint.textContent =
`Outside the mutual-gain range: ${(1 / 3).toFixed(2)}–2.00 bread per pottery.`;
tradeLogic.textContent =
"One side would lose at this rate. Adjust the ratio to make both producers better off.";
tradeButton.textContent = "Outside mutual-benefit range";
tradeButton.disabled = true;
tradeButton.classList.add("invalid");
stepBadge.textContent = "Adjust the rate";
} else {
ratioHint.textContent =
"Mutual-gain range: 0.33–2.00 bread per pottery.";
tradeLogic.textContent =
"Both sides gain: Alex gets pottery below 2.00 bread each; Blair gets bread below 3.00 pottery each.";
tradeButton.textContent = `Trade 10 pottery ↔ ${(currentRatio * 10).toFixed(1)} bread`;
tradeButton.disabled = Boolean(tradeAnimation);
tradeButton.classList.remove("invalid");
stepBadge.textContent = tradeCount ? "Trade completed" : "3 / Make a trade";
}
updateMarketLabel(currentRatio, valid);
updateMarketInsight(valid);
}
function updateMarketInsight(valid) {
const alexSaving = 2 - currentRatio;
const blairSaving = 3 - 1 / currentRatio;
const ledger =
tradeCount > 0
? `<div class="ledger"><strong>Last trade:</strong> Blair sent 10 pottery; Alex sent ${(currentRatio * 10).toFixed(1)} bread.</div>`
: "";
marketInsight.innerHTML = `
At <strong>1 pottery = ${currentRatio.toFixed(2)} bread</strong>:
<br>Alex's cost: ${currentRatio.toFixed(2)} vs 2.00 bread
<br>Blair's cost: ${(1 / currentRatio).toFixed(2)} vs 3.00 pottery
<br><strong>${valid ? "Both producers gain." : "This rate does not make both producers gain."}</strong>
${ledger}
`;
}
analyzeButton.addEventListener("click", () => {
costsRevealed = true;
document.body.classList.add("costs-revealed");
analyzeButton.disabled = true;
analyzeButton.textContent = "Costs calculated ✓";
ratioInput.disabled = false;
breadMarker.visible = true;
potteryMarker.visible = true;
updateProducerLabels();
updateTradeState();
live.textContent =
"Opportunity costs calculated. Alex has the comparative advantage in bread, and Blair has the comparative advantage in pottery.";
});
ratioInput.addEventListener("input", () => {
currentRatio = Number(ratioInput.value);
updateTradeState();
});
// -------------------------------------------------------------------------
// Trade animation
// -------------------------------------------------------------------------
function createCargo(type) {
const group = new THREE.Group();
addBox(group, 0.72, 0.12, 0.52, 0x8b613d, 0, 0, 0, true);
if (type === "bread") {
addBox(group, 0.34, 0.22, 0.24, 0xe8b65f, -0.2, 0.17, 0, true);
addBox(group, 0.34, 0.22, 0.24, 0xf0c873, 0.2, 0.17, 0, true);
addBox(group, 0.28, 0.16, 0.2, 0xd79b47, 0, 0.36, 0);
} else {
addBox(group, 0.27, 0.31, 0.27, 0xc96849, -0.18, 0.22, 0, true);
addBox(group, 0.16, 0.16, 0.16, 0xd97853, -0.18, 0.45, 0);
addBox(group, 0.27, 0.31, 0.27, 0xb95a41, 0.18, 0.22, 0, true);
addBox(group, 0.16, 0.16, 0.16, 0xd97853, 0.18, 0.45, 0);
}
return group;
}
function disposeGroup(group) {
group.traverse((object) => {
if (object.geometry) object.geometry.dispose();
if (object.material) {
const materials = Array.isArray(object.material)
? object.material
: [object.material];
for (const material of materials) {
if (!staticMaterials.has(material.color?.getHex())) {
material.dispose();
}
}
}
});
}
function beginTrade() {
if (!costsRevealed || !rateIsValid(currentRatio) || tradeAnimation) {
return;
}
tradeCount += 1;
tradeButton.disabled = true;
tradeButton.textContent = "Goods are moving…";
tradeLogic.textContent = "The market is exchanging bread and pottery.";
stepBadge.textContent = "Trade in motion";
const cargoItems = [];
for (let i = 0; i < 4; i++) {
const breadCargo = createCargo("bread");
breadCargo.position.set(-3.35, 0.93, 1.0 + i * 0.43);
scene.add(breadCargo);
const potteryCargo = createCargo("pottery");
potteryCargo.position.set(3.35, 0.93, 1.0 + i * 0.43);
scene.add(potteryCargo);
cargoItems.push(
{
group: breadCargo,
start: new THREE.Vector3(-3.35, 0.93, 1.0 + i * 0.43),
end: new THREE.Vector3(3.35, 0.93, 1.0 + i * 0.43),
delay: i * 0.13,
},
{
group: potteryCargo,
start: new THREE.Vector3(3.35, 0.93, 1.0 + i * 0.43),
end: new THREE.Vector3(-3.35, 0.93, 1.0 + i * 0.43),
delay: i * 0.13,
}
);
}
tradeAnimation = {
startTime: performance.now(),
duration: 1450,
items: cargoItems,
};
}
tradeButton.addEventListener("click", beginTrade);
function updateTradeAnimation(now) {
if (!tradeAnimation) return;
const elapsed = now - tradeAnimation.startTime;
const duration = tradeAnimation.duration;
let allFinished = true;
for (const item of tradeAnimation.items) {
const local = (elapsed - item.delay) / duration;
if (local <= 0) {
item.group.visible = false;
allFinished = false;
continue;
}
if (local >= 1) {
item.group.visible = false;
continue;
}
allFinished = false;
item.group.visible = true;
const t = 1 - Math.pow(1 - local, 3);
item.group.position.lerpVectors(item.start, item.end, t);
item.group.position.y += Math.sin(Math.PI * local) * 1.55;
item.group.rotation.y = Math.sin(Math.PI * local) * Math.PI * 0.65;
item.group.rotation.z = Math.sin(Math.PI * local * 2) * 0.05;
}
if (elapsed > duration + 600) {
for (const item of tradeAnimation.items) {
scene.remove(item.group);
disposeGroup(item.group);
}
tradeAnimation = null;
stepBadge.textContent = "Trade completed";
tradeLogic.textContent =
"Exchange complete. Both producers received goods for less than their domestic opportunity cost.";
live.textContent =
`Trade complete at ${currentRatio.toFixed(2)} bread per pottery. Ten pottery were exchanged for ${(currentRatio * 10).toFixed(1)} bread.`;
updateTradeState();
} else if (!allFinished) {
stepBadge.textContent = "Trade in motion";
}
}
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
function animate(now) {
requestAnimationFrame(animate);
const damping = 0.11;
current.azimuth += (desired.azimuth - current.azimuth) * damping;
current.polar += (desired.polar - current.polar) * damping;
current.radius += (desired.radius - current.radius) * damping;
const sinPolar = Math.sin(current.polar);
camera.position.set(
cameraTarget.x + current.radius * sinPolar * Math.sin(current.azimuth),
cameraTarget.y + current.radius * Math.cos(current.polar),
cameraTarget.z + current.radius * sinPolar * Math.cos(current.azimuth)
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
updateTradeAnimation(now);
projectLabels();
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
updateTradeState();
requestAnimationFrame(animate);
requestAnimationFrame(() => {
document.querySelector("#loading").classList.add("hidden");
setTimeout(() => {
document.querySelector("#loading").remove();
}, 350);
});
</script>
</body>
</html>
```