Prompt specification
Build or customize the Three.js 3D scene "Three Traders With Mismatched Wants" 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: Three Traders With Mismatched Wants
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/three-traders-with-mismatched-wants-x3122
Scene Description Prompt:
I'd like to see this as a stylized Three.js scene: three traders with mismatched wants. Arrange three stalls in a triangle with visible offer and demand icons. Attempt pairwise swaps and highlight why a direct exchange fails despite available goods. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (three-traders-with-mismatched-wants-x3122):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>The Triangle of Unmet Wants</title>
<style>
:root {
--ink: #203438;
--muted: #68777a;
--paper: #fffaf1;
--offer: #dff4e8;
--offer-ink: #176c56;
--want: #ffe2e9;
--want-ink: #a93b5c;
--accent: #e76f51;
--shadow: 0 18px 50px rgba(44, 53, 50, 0.14);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #f2ede4;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
#ui {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 5;
}
.glass {
background: rgba(255, 251, 243, 0.88);
border: 1px solid rgba(255, 255, 255, 0.88);
box-shadow: var(--shadow);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.brand {
position: absolute;
top: 26px;
left: 26px;
width: 340px;
padding: 23px 24px 21px;
border-radius: 24px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 11px;
color: #6f7c7d;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 23px;
height: 3px;
border-radius: 3px;
background: var(--accent);
}
h1 {
margin: 0;
font-size: clamp(29px, 3vw, 42px);
line-height: 0.98;
letter-spacing: -0.055em;
font-weight: 850;
}
.intro {
margin: 15px 0 17px;
color: #566669;
font-size: 14px;
line-height: 1.52;
}
.legend {
display: flex;
gap: 9px;
flex-wrap: wrap;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 7px 10px;
border-radius: 999px;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.legend .offer {
color: var(--offer-ink);
background: var(--offer);
}
.legend .want {
color: var(--want-ink);
background: var(--want);
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: currentColor;
}
.test-panel {
position: absolute;
top: 26px;
right: 26px;
width: 365px;
padding: 20px;
border-radius: 24px;
pointer-events: auto;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 15px;
}
.panel-heading strong {
font-size: 12px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
#attemptCount {
padding: 6px 9px;
border-radius: 999px;
background: #eee8dd;
color: #647174;
font-size: 11px;
font-weight: 800;
}
.pair-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin-bottom: 14px;
}
.pair-button {
min-width: 0;
padding: 11px 7px 10px;
border: 1px solid #ded6c9;
border-radius: 14px;
background: rgba(255, 255, 255, 0.72);
color: var(--ink);
cursor: pointer;
text-align: center;
transition: transform 160ms ease, border-color 160ms ease,
background 160ms ease, box-shadow 160ms ease;
}
.pair-button:hover {
transform: translateY(-2px);
border-color: #b8a995;
box-shadow: 0 7px 16px rgba(50, 54, 49, 0.08);
}
.pair-button span {
display: block;
font-size: 14px;
font-weight: 850;
}
.pair-button small {
display: block;
margin-top: 3px;
overflow: hidden;
color: #7b8585;
font-size: 10px;
text-overflow: ellipsis;
white-space: nowrap;
}
.pair-button.tested {
border-color: #b9d6c8;
background: #edf7f0;
}
.pair-button.tested::after {
content: "✓";
float: right;
color: var(--offer-ink);
font-size: 12px;
font-weight: 900;
}
.result {
min-height: 153px;
padding: 15px 16px;
border-radius: 17px;
background: #f4efe6;
border: 1px solid #e4dbcd;
}
.result-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 10px;
}
#resultTitle {
margin: 0;
font-size: 16px;
font-weight: 850;
letter-spacing: -0.02em;
}
.status-pill {
flex: 0 0 auto;
padding: 5px 8px;
border-radius: 999px;
color: #7b8585;
background: #e7e0d6;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.status-pill.fail {
color: var(--want-ink);
background: var(--want);
}
#resultBody {
color: #566669;
font-size: 13px;
line-height: 1.47;
}
#resultBody p { margin: 7px 0 0; }
.result-line {
display: block;
margin: 5px 0;
font-weight: 700;
}
.result-line.ok { color: var(--offer-ink); }
.result-line.no { color: var(--want-ink); }
.elsewhere {
margin-top: 10px !important;
padding-top: 9px;
border-top: 1px solid #ddd4c7;
color: #7b6118;
font-size: 12px;
}
.actions {
display: flex;
gap: 9px;
margin-top: 13px;
}
.action-button {
flex: 1;
padding: 11px 12px;
border: 0;
border-radius: 13px;
background: var(--ink);
color: white;
cursor: pointer;
font: inherit;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.07em;
text-transform: uppercase;
transition: transform 160ms ease, opacity 160ms ease;
}
.action-button:hover { transform: translateY(-1px); }
.action-button:disabled { opacity: 0.55; cursor: wait; transform: none; }
#cycleSummary {
display: none;
margin-top: 13px;
padding: 14px 15px;
border: 1px solid #b9d8c8;
border-radius: 15px;
background: #e9f6ed;
color: #245b49;
font-size: 12px;
line-height: 1.45;
}
#cycleSummary.show { display: block; }
#cycleSummary strong {
display: block;
margin-bottom: 4px;
font-size: 13px;
}
#hint {
position: absolute;
left: 50%;
bottom: 22px;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 10px;
padding: 10px 14px;
border-radius: 999px;
color: #657274;
background: rgba(255, 251, 243, 0.82);
border: 1px solid rgba(255, 255, 255, 0.8);
box-shadow: 0 8px 28px rgba(40, 49, 46, 0.1);
font-size: 11px;
font-weight: 700;
white-space: nowrap;
}
.mouse {
position: relative;
width: 17px;
height: 23px;
border: 2px solid #7d8989;
border-radius: 9px;
}
.mouse::after {
content: "";
position: absolute;
top: 4px;
left: 50%;
width: 2px;
height: 5px;
border-radius: 2px;
background: #7d8989;
transform: translateX(-50%);
}
#loader {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #f2ede4;
transition: opacity 450ms ease, visibility 450ms ease;
}
#loader.hidden {
opacity: 0;
visibility: hidden;
}
.loader-mark {
width: 42px;
height: 42px;
border: 4px solid #d9d0c2;
border-top-color: var(--accent);
border-radius: 50%;
animation: spin 850ms linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (max-width: 900px) {
.brand {
top: 12px;
left: 12px;
width: calc(100% - 24px);
padding: 15px 17px;
border-radius: 19px;
}
h1 {
max-width: 360px;
font-size: 28px;
}
.intro { display: none; }
.eyebrow { margin-bottom: 7px; }
.test-panel {
top: auto;
right: 12px;
bottom: 12px;
width: calc(100% - 24px);
padding: 15px;
border-radius: 19px;
}
.result { min-height: 108px; }
#hint { display: none; }
}
@media (max-width: 520px) {
.brand { padding: 13px 15px; }
h1 { font-size: 24px; }
.legend span { padding: 5px 8px; font-size: 9px; }
.test-panel { padding: 13px; }
.panel-heading { margin-bottom: 10px; }
.result { padding: 12px; min-height: 98px; }
#resultTitle { font-size: 14px; }
#resultBody { font-size: 12px; }
.elsewhere { font-size: 11px; }
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive Three.js barter market"></div>
<div id="ui">
<section class="brand glass">
<div class="eyebrow">Barter lab · 03 traders</div>
<h1>The triangle of<br>unmet wants</h1>
<p class="intro">
Every trader has something to offer and something they need.
Test a direct swap: one side may fit, but the other side still blocks it.
</p>
<div class="legend" aria-label="Icon legend">
<span class="offer"><i class="dot"></i> Offer</span>
<span class="want"><i class="dot"></i> Wants</span>
</div>
</section>
<section class="test-panel glass" aria-label="Pairwise swap tester">
<div class="panel-heading">
<strong>Pairwise test</strong>
<span id="attemptCount">0 / 3 tested</span>
</div>
<div class="pair-grid">
<button class="pair-button" data-pair="0" aria-label="Test Mina and Theo">
<span>A ↔ B</span><small>Mina / Theo</small>
</button>
<button class="pair-button" data-pair="1" aria-label="Test Mina and Ira">
<span>A ↔ C</span><small>Mina / Ira</small>
</button>
<button class="pair-button" data-pair="2" aria-label="Test Theo and Ira">
<span>B ↔ C</span><small>Theo / Ira</small>
</button>
</div>
<div class="result" aria-live="polite">
<div class="result-top">
<h2 id="resultTitle">Choose a direct exchange</h2>
<span id="statusPill" class="status-pill">Ready</span>
</div>
<div id="resultBody">
Click two stalls in the scene, or choose a pair above.
Each attempted swap exposes one unmatched want.
</div>
</div>
<div class="actions">
<button id="autoButton" class="action-button">Auto-test all pairs</button>
</div>
<div id="cycleSummary">
<strong>A three-way cycle clears every want.</strong>
Mina’s apples go to Ira, Ira’s fish go to Theo, and Theo’s bread goes to Mina.
No two-person trade can do this alone.
</div>
</section>
<div id="hint">
<span class="mouse"></span>
Drag to orbit · Scroll to zoom · Click stalls to trade
</div>
</div>
<div id="loader"><div class="loader-mark"></div></div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const loader = document.getElementById("loader");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.06;
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf2ede4);
scene.fog = new THREE.Fog(0xf2ede4, 22, 43);
const camera = new THREE.OrthographicCamera(-10, 10, 7, -7, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 1.15, 0);
let cameraTheta = 0.48;
let cameraPhi = 0.82;
let cameraRadius = 18;
let cameraZoom = 1.08;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
camera.zoom = cameraZoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
let span = 13.5;
if (aspect < 1) {
span = Math.max(15.5, 11.3 / aspect);
}
camera.left = -span * aspect / 2;
camera.right = span * aspect / 2;
camera.top = span / 2;
camera.bottom = -span / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
resize();
updateCamera();
const hemi = new THREE.HemisphereLight(0xfff8e8, 0x78909a, 2.25);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff1d4, 3.2);
sun.position.set(-7, 12, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0005;
scene.add(sun);
const rim = new THREE.DirectionalLight(0xaed8e8, 1.0);
rim.position.set(8, 6, -9);
scene.add(rim);
const traders = [
{
id: "A",
name: "Mina",
offer: "apples",
want: "bread",
position: new THREE.Vector3(-4.55, 0, 2.25),
canopy: 0xef8354,
canopyLight: 0xffe8d7,
structure: 0x315d59,
shirt: 0x2a9d8f,
skin: 0xc9825b,
hair: 0x332c29
},
{
id: "B",
name: "Theo",
offer: "bread",
want: "fish",
position: new THREE.Vector3(4.55, 0, 2.25),
canopy: 0xe9c46a,
canopyLight: 0xfff5d6,
structure: 0x38586b,
shirt: 0x457b9d,
skin: 0xe2aa7e,
hair: 0x4a3328
},
{
id: "C",
name: "Ira",
offer: "fish",
want: "apples",
position: new THREE.Vector3(0, 0, -3.75),
canopy: 0x8d78b5,
canopyLight: 0xf0e8fa,
structure: 0x4a4565,
shirt: 0x9b5de5,
skin: 0x8f5b43,
hair: 0x201d24
}
];
const pairConfigs = [
{
ids: [0, 1],
acceptedParty: 0,
acceptedOfferer: 1,
rejectedParty: 1,
rejectedOfferer: 0,
elsewhere: 2
},
{
ids: [0, 2],
acceptedParty: 2,
acceptedOfferer: 0,
rejectedParty: 0,
rejectedOfferer: 2,
elsewhere: 1
},
{
ids: [1, 2],
acceptedParty: 1,
acceptedOfferer: 2,
rejectedParty: 2,
rejectedOfferer: 1,
elsewhere: 0
}
];
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function drawGood(ctx, good, centerX, centerY, size) {
ctx.save();
ctx.translate(centerX, centerY);
ctx.scale(size / 100, size / 100);
ctx.lineCap = "round";
ctx.lineJoin = "round";
if (good === "apples") {
ctx.fillStyle = "#e84d4d";
ctx.beginPath();
ctx.arc(-22, 10, 31, 0, Math.PI * 2);
ctx.arc(22, 10, 31, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = "#f46a62";
ctx.beginPath();
ctx.ellipse(-24, -1, 12, 8, -0.35, 0, Math.PI * 2);
ctx.fill();
ctx.strokeStyle = "#70412d";
ctx.lineWidth = 8;
ctx.beginPath();
ctx.moveTo(0, -14);
ctx.quadraticCurveTo(3, -34, 10, -40);
ctx.stroke();
ctx.fillStyle = "#438a54";
ctx.beginPath();
ctx.ellipse(21, -34, 19, 8, -0.48, 0, Math.PI * 2);
ctx.fill();
} else if (good === "bread") {
ctx.fillStyle = "#c98243";
roundedRect(ctx, -43, -27, 86, 56, 23);
ctx.fill();
ctx.fillStyle = "#e9aa5d";
roundedRect(ctx, -38, -23, 76, 45, 20);
ctx.fill();
ctx.strokeStyle = "#9b5f34";
ctx.lineWidth = 7;
for (let i = -1; i <= 1; i++) {
ctx.beginPath();
ctx.moveTo(i * 19 - 9, 17);
ctx.lineTo(i * 19 + 10, -16);
ctx.stroke();
}
} else {
ctx.fillStyle = "#58a9bd";
ctx.beginPath();
ctx.ellipse(4, 2, 42, 24, 0, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = "#8bd2df";
ctx.beginPath();
ctx.moveTo(-31, 2);
ctx.lineTo(-52, -18);
ctx.lineTo(-52, 22);
ctx.closePath();
ctx.fill();
ctx.fillStyle = "#64b8c7";
ctx.beginPath();
ctx.moveTo(-3, -3);
ctx.lineTo(-22, -31);
ctx.lineTo(17, -22);
ctx.closePath();
ctx.fill();
ctx.fillStyle = "#183c46";
ctx.beginPath();
ctx.arc(27, -5, 5, 0, Math.PI * 2);
ctx.fill();
}
ctx.restore();
}
function canvasTexture(canvas) {
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
texture.generateMipmaps = false;
return texture;
}
function makeBadgeTexture(kind, good) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 210;
const ctx = canvas.getContext("2d");
const isOffer = kind === "offer";
const fill = isOffer ? "#dff4e8" : "#ffe2e9";
const border = isOffer ? "#176c56" : "#a93b5c";
ctx.shadowColor = "rgba(37, 49, 45, 0.14)";
ctx.shadowBlur = 16;
ctx.shadowOffsetY = 7;
roundedRect(ctx, 10, 10, 492, 190, 42);
ctx.fillStyle = fill;
ctx.fill();
ctx.shadowColor = "transparent";
ctx.lineWidth = 8;
ctx.strokeStyle = border;
ctx.stroke();
drawGood(ctx, good, 82, 108, 116);
ctx.fillStyle = border;
ctx.font = "800 25px system-ui, sans-serif";
ctx.letterSpacing = "2px";
ctx.fillText(isOffer ? "OFFER" : "WANTS", 154, 84);
ctx.fillStyle = "#25383b";
ctx.font = "900 39px system-ui, sans-serif";
ctx.fillText(good.toUpperCase(), 153, 140);
return canvasTexture(canvas);
}
function makeNameTexture(trader) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 150;
const ctx = canvas.getContext("2d");
roundedRect(ctx, 8, 8, 496, 134, 32);
ctx.fillStyle = "#263e40";
ctx.fill();
ctx.lineWidth = 5;
ctx.strokeStyle = "rgba(255,255,255,0.35)";
ctx.stroke();
ctx.fillStyle = trader.canopyLight;
ctx.beginPath();
ctx.arc(67, 75, 36, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = "#263e40";
ctx.font = "900 37px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(trader.id, 67, 76);
ctx.textAlign = "left";
ctx.fillStyle = "#fffaf0";
ctx.font = "850 43px system-ui, sans-serif";
ctx.fillText(trader.name.toUpperCase(), 125, 76);
return canvasTexture(canvas);
}
function makeItemTexture(good) {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 256;
const ctx = canvas.getContext("2d");
ctx.shadowColor = "rgba(30,45,42,0.17)";
ctx.shadowBlur = 12;
ctx.shadowOffsetY = 6;
ctx.fillStyle = "rgba(255,252,244,0.94)";
ctx.beginPath();
ctx.arc(128, 128, 105, 0, Math.PI * 2);
ctx.fill();
ctx.shadowColor = "transparent";
ctx.strokeStyle = "rgba(50,65,60,0.16)";
ctx.lineWidth = 6;
ctx.stroke();
drawGood(ctx, good, 128, 128, 160);
return canvasTexture(canvas);
}
function makeMarkerTexture(type) {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 256;
const ctx = canvas.getContext("2d");
const good = type === "check";
ctx.fillStyle = good ? "#2f9471" : "#d84b67";
ctx.shadowColor = "rgba(30,40,36,0.18)";
ctx.shadowBlur = 15;
ctx.shadowOffsetY = 6;
ctx.beginPath();
ctx.arc(128, 128, 102, 0, Math.PI * 2);
ctx.fill();
ctx.shadowColor = "transparent";
ctx.strokeStyle = "white";
ctx.lineWidth = 24;
ctx.lineCap = "round";
ctx.lineJoin = "round";
ctx.beginPath();
if (good) {
ctx.moveTo(73, 132);
ctx.lineTo(113, 170);
ctx.lineTo(185, 88);
} else {
ctx.moveTo(82, 82);
ctx.lineTo(174, 174);
ctx.moveTo(174, 82);
ctx.lineTo(82, 174);
}
ctx.stroke();
return canvasTexture(canvas);
}
const itemTextures = {
apples: makeItemTexture("apples"),
bread: makeItemTexture("bread"),
fish: makeItemTexture("fish")
};
const checkTexture = makeMarkerTexture("check");
const crossTexture = makeMarkerTexture("cross");
function standard(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0,
flatShading: true,
...options
});
}
function box(parent, width, height, depth, color, x, y, z, options = {}) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
standard(color, options)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinder(parent, radiusTop, radiusBottom, height, color, x, y, z, segments = 12) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusTop, radiusBottom, height, segments),
standard(color)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function sphere(parent, radius, color, x, y, z, scale = [1, 1, 1]) {
const mesh = new THREE.Mesh(
new THREE.SphereGeometry(radius, 16, 10),
standard(color)
);
mesh.position.set(x, y, z);
mesh.scale.set(...scale);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeSprite(texture, width, height) {
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, height, 1);
sprite.renderOrder = 20;
return sprite;
}
function makeOfferModel(good) {
const group = new THREE.Group();
if (good === "apples") {
sphere(group, 0.23, 0xe94f4b, -0.13, 0, 0, [1, 0.95, 0.9]);
sphere(group, 0.23, 0xf05c51, 0.13, 0, 0, [1, 0.95, 0.9]);
const stem = cylinder(group, 0.025, 0.035, 0.22, 0x70412d, 0.01, 0.23, 0, 7);
stem.rotation.z = -0.2;
const leaf = sphere(group, 0.13, 0x4c9560, 0.13, 0.27, 0, [1.3, 0.35, 0.65]);
leaf.rotation.z = -0.4;
} else if (good === "bread") {
const loaf = new THREE.Mesh(
new THREE.CapsuleGeometry(0.2, 0.45, 4, 8),
standard(0xd99a52)
);
loaf.rotation.z = Math.PI / 2;
loaf.castShadow = true;
group.add(loaf);
for (let i = -1; i <= 1; i++) {
const slash = box(group, 0.035, 0.02, 0.23, 0x9f6137, i * 0.17, 0.19, 0);
slash.rotation.y = -0.28;
}
} else {
const body = sphere(group, 0.28, 0x58a9bd, 0.04, 0, 0, [1.25, 0.65, 0.55]);
body.rotation.z = -0.12;
const tail = new THREE.Mesh(
new THREE.ConeGeometry(0.2, 0.36, 3),
standard(0x77c3d0)
);
tail.position.set(-0.39, 0, 0);
tail.rotation.z = Math.PI / 2;
tail.rotation.y = Math.PI / 2;
tail.castShadow = true;
group.add(tail);
sphere(group, 0.035, 0x173944, 0.25, 0.055, 0.135);
}
return group;
}
function makeCharacter(trader) {
const character = new THREE.Group();
const torso = cylinder(character, 0.34, 0.43, 0.78, trader.shirt, 0, 1.22, 0, 10);
torso.rotation.y = -0.04;
sphere(character, 0.29, trader.skin, 0, 1.98, 0.015, [1, 1.08, 0.95]);
const hair = new THREE.Mesh(
new THREE.SphereGeometry(0.30, 14, 8, 0, Math.PI * 2, 0, Math.PI / 2),
standard(trader.hair)
);
hair.position.set(0, 2.08, 0.005);
hair.scale.set(1, 0.83, 1);
hair.castShadow = true;
character.add(hair);
sphere(character, 0.025, 0x20292a, -0.09, 2.01, 0.272, [1, 1.2, 0.5]);
sphere(character, 0.025, 0x20292a, 0.09, 2.01, 0.272, [1, 1.2, 0.5]);
sphere(character, 0.032, 0xd78968, 0, 1.92, 0.292, [0.8, 0.8, 0.6]);
const leftArm = cylinder(character, 0.085, 0.11, 0.68, trader.skin, -0.39, 1.25, 0.04, 8);
leftArm.rotation.z = -0.25;
const rightArm = cylinder(character, 0.085, 0.11, 0.68, trader.skin, 0.39, 1.25, 0.04, 8);
rightArm.rotation.z = 0.25;
if (trader.id === "A") {
cylinder(character, 0.45, 0.45, 0.055, 0xe8c875, 0, 2.27, 0, 16);
cylinder(character, 0.27, 0.31, 0.24, 0xe8c875, 0, 2.39, 0, 14);
} else if (trader.id === "B") {
const cap = sphere(character, 0.31, 0xd85d4f, 0, 2.16, 0, [1, 0.75, 1]);
cap.position.y = 2.2;
}
return character;
}
function makeRing(radius, color, opacity = 0) {
const mesh = new THREE.Mesh(
new THREE.TorusGeometry(radius, 0.045, 8, 48),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity,
depthWrite: false
})
);
mesh.rotation.x = Math.PI / 2;
mesh.position.y = 0.045;
mesh.renderOrder = 4;
return mesh;
}
const stalls = [];
function createStall(trader) {
const group = new THREE.Group();
group.position.copy(trader.position);
group.userData.traderId = trader.id;
scene.add(group);
const frameMat = standard(trader.structure);
for (const x of [-1.34, 1.34]) {
for (const z of [-0.75, 0.76]) {
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.065, 0.075, 3.0, 8),
frameMat
);
post.position.set(x, 1.5, z);
post.castShadow = true;
group.add(post);
}
}
const counter = box(group, 2.75, 1.18, 0.82, trader.structure, 0, 0.72, 0.55);
counter.receiveShadow = true;
box(group, 2.95, 0.16, 1.02, 0xfff3df, 0, 1.39, 0.55);
box(group, 2.85, 0.18, 0.12, trader.canopy, 0, 1.0, 0.99);
const stripeWidth = 3.2 / 8;
for (let i = 0; i < 8; i++) {
const color = i % 2 === 0 ? trader.canopy : trader.canopyLight;
box(group, stripeWidth + 0.012, 0.13, 2.02, color,
-1.6 + stripeWidth * (i + 0.5), 3.02, 0);
box(group, stripeWidth + 0.012, 0.31, 0.12, color,
-1.6 + stripeWidth * (i + 0.5), 2.81, 1.0);
}
const character = makeCharacter(trader);
character.position.set(0, 0.08, -0.48);
group.add(character);
const offerModel = makeOfferModel(trader.offer);
offerModel.position.set(0.58, 1.63, 0.57);
offerModel.scale.setScalar(0.9);
group.add(offerModel);
const nameSprite = makeSprite(makeNameTexture(trader), 1.75, 0.51);
nameSprite.position.set(0, 0.85, 1.02);
group.add(nameSprite);
const offerSprite = makeSprite(
makeBadgeTexture("offer", trader.offer),
2.08, 0.85
);
offerSprite.position.set(-1.02, 3.83, 0.23);
group.add(offerSprite);
const wantSprite = makeSprite(
makeBadgeTexture("want", trader.want),
2.08, 0.85
);
wantSprite.position.set(1.02, 3.83, 0.23);
group.add(wantSprite);
const hoverRing = makeRing(1.62, 0x335c60, 0);
const selectRing = makeRing(1.62, 0xf0b84a, 0);
const outcomeRing = makeRing(1.78, 0x2f9471, 0);
group.add(hoverRing, selectRing, outcomeRing);
group.traverse(child => {
child.userData.traderId = trader.id;
});
const stall = {
trader,
group,
character,
hoverRing,
selectRing,
outcomeRing
};
stalls.push(stall);
return stall;
}
traders.forEach(createStall);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(70, 70),
standard(0xf2ede4)
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.34;
ground.receiveShadow = true;
scene.add(ground);
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(7.45, 7.58, 0.30, 64),
standard(0xe8dfd0)
);
platform.position.y = -0.15;
platform.receiveShadow = true;
platform.castShadow = true;
scene.add(platform);
const platformEdge = new THREE.Mesh(
new THREE.TorusGeometry(7.39, 0.045, 8, 96),
standard(0xcfc2af)
);
platformEdge.rotation.x = Math.PI / 2;
platformEdge.position.y = 0.015;
scene.add(platformEdge);
const guidePoints = traders.map(t => t.position.clone().setY(0.035));
guidePoints.push(guidePoints[0].clone());
const guide = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(guidePoints),
new THREE.LineDashedMaterial({
color: 0xb9ab99,
dashSize: 0.22,
gapSize: 0.17,
transparent: true,
opacity: 0.42
})
);
guide.computeLineDistances();
scene.add(guide);
const decorativeColors = [0xe9b7a3, 0xc4d8d1, 0xd8c7e4, 0xe8d59f];
for (let i = 0; i < 16; i++) {
const angle = (i / 16) * Math.PI * 2 + 0.17;
const radius = 7.05 + (i % 2) * 0.15;
const pebble = new THREE.Mesh(
new THREE.SphereGeometry(0.09 + (i % 3) * 0.025, 8, 6),
standard(decorativeColors[i % decorativeColors.length])
);
pebble.position.set(
Math.cos(angle) * radius,
0.07,
Math.sin(angle) * radius
);
pebble.scale.y = 0.55;
pebble.castShadow = true;
scene.add(pebble);
}
const resultTitle = document.getElementById("resultTitle");
const resultBody = document.getElementById("resultBody");
const statusPill = document.getElementById("statusPill");
const attemptCount = document.getElementById("attemptCount");
const cycleSummary = document.getElementById("cycleSummary");
const autoButton = document.getElementById("autoButton");
const pairButtons = [...document.querySelectorAll(".pair-button")];
const attemptedPairs = new Set();
const outcomeMap = new Map();
let selectedId = null;
let hoveredId = null;
let attemptVisual = null;
let cycleGroup = null;
let cyclePaths = [];
let cycleBuilt = false;
let autoRunning = false;
function traderById(id) {
return traders.find(t => t.id === id);
}
function setSelection(id) {
selectedId = id;
if (id === null) {
resultTitle.textContent = "Choose a direct exchange";
statusPill.textContent = "Ready";
statusPill.className = "status-pill";
resultBody.innerHTML =
"Click two stalls in the scene, or choose a pair above. " +
"Each attempted swap exposes one unmatched want.";
} else {
const trader = traderById(id);
resultTitle.textContent = `${trader.id} · ${trader.name} selected`;
statusPill.textContent = "Pick a partner";
statusPill.className = "status-pill";
resultBody.innerHTML =
`Now choose another stall. <span class="result-line ok">${trader.name} offers ${trader.offer}.</span>` +
`<span class="result-line no">${trader.name} wants ${trader.want}.</span>`;
}
}
function clearAttemptVisual() {
if (attemptVisual) {
scene.remove(attemptVisual.group);
attemptVisual = null;
}
}
function createAttemptVisual(config) {
clearAttemptVisual();
const group = new THREE.Group();
scene.add(group);
const a = traders[config.ids[0]].position.clone().setY(0.24);
const b = traders[config.ids[1]].position.clone().setY(0.24);
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([a, b]),
new THREE.LineDashedMaterial({
color: 0xd84b67,
dashSize: 0.24,
gapSize: 0.15,
transparent: true,
opacity: 0.7
})
);
line.computeLineDistances();
group.add(line);
const acceptedStart = traders[config.acceptedOfferer].position.clone()
.add(new THREE.Vector3(0, 3.18, 0.35));
const acceptedEnd = traders[config.acceptedParty].position.clone()
.add(new THREE.Vector3(0, 1.83, 0.48));
const rejectedStart = traders[config.rejectedOfferer].position.clone()
.add(new THREE.Vector3(0, 3.18, 0.35));
const midpoint = acceptedStart.clone().add(rejectedStart).multiplyScalar(0.5);
midpoint.y = 1.22;
const acceptedToken = makeSprite(
itemTextures[traders[config.acceptedOfferer].offer],
0.78, 0.78
);
acceptedToken.position.copy(acceptedStart);
group.add(acceptedToken);
const rejectedToken = makeSprite(
itemTextures[traders[config.rejectedOfferer].offer],
0.78, 0.78
);
rejectedToken.position.copy(rejectedStart);
group.add(rejectedToken);
const cross = makeSprite(crossTexture, 0.88, 0.88);
cross.position.copy(midpoint);
cross.visible = false;
group.add(cross);
const check = makeSprite(checkTexture, 0.76, 0.76);
check.position.copy(acceptedEnd).add(new THREE.Vector3(0, 0.45, 0));
check.visible = false;
group.add(check);
attemptVisual = {
group,
line,
acceptedToken,
rejectedToken,
cross,
check,
acceptedStart,
acceptedEnd,
rejectedStart,
midpoint,
time: 0
};
}
function attemptPair(index) {
const config = pairConfigs[index];
const acceptedParty = traders[config.acceptedParty];
const acceptedOfferer = traders[config.acceptedOfferer];
const rejectedParty = traders[config.rejectedParty];
const rejectedOfferer = traders[config.rejectedOfferer];
const elsewhere = traders[config.elsewhere];
attemptedPairs.add(index);
selectedId = null;
outcomeMap.clear();
outcomeMap.set(acceptedParty.id, 0x2f9471);
outcomeMap.set(rejectedParty.id, 0xd84b67);
createAttemptVisual(config);
resultTitle.textContent = `${traders[config.ids[0]].id} ↔ ${traders[config.ids[1]].id} · direct swap fails`;
statusPill.textContent = "One-sided match";
statusPill.className = "status-pill fail";
resultBody.innerHTML =
`<span class="result-line ok">✓ ${acceptedParty.name} accepts ${acceptedOfferer.name}’s ${acceptedOfferer.offer}.</span>` +
`<span class="result-line no">✕ ${rejectedParty.name} wants ${rejectedParty.want}, but ${rejectedOfferer.name} offers ${rejectedOfferer.offer}.</span>` +
`<p class="elsewhere">The wanted ${rejectedParty.want} is available at ${elsewhere.name}’s stall, but not from this trading partner.</p>`;
attemptCount.textContent = `${attemptedPairs.size} / 3 tested`;
pairButtons.forEach((button, i) => {
button.classList.toggle("tested", attemptedPairs.has(i));
});
if (attemptedPairs.size === 3) {
outcomeMap.clear();
traders.forEach(t => outcomeMap.set(t.id, 0x2f9471));
cycleSummary.classList.add("show");
clearAttemptVisual();
buildCycle();
}
}
function buildCycle() {
if (cycleBuilt) {
cycleGroup.visible = true;
return;
}
cycleBuilt = true;
cycleGroup = new THREE.Group();
scene.add(cycleGroup);
const center = new THREE.Vector3(0, 0, 0);
const routes = [
{ from: 0, to: 2, good: "apples", color: 0xe76f51 },
{ from: 2, to: 1, good: "fish", color: 0x2a9d8f },
{ from: 1, to: 0, good: "bread", color: 0xd69b38 }
];
cyclePaths = [];
for (const route of routes) {
const start = traders[route.from].position.clone().setY(0.24);
const end = traders[route.to].position.clone().setY(0.24);
const midpoint = start.clone().add(end).multiplyScalar(0.5);
const outward = midpoint.clone().sub(center).normalize();
const control = midpoint.clone().add(outward.multiplyScalar(2.35)).setY(0.3);
const curve = new THREE.QuadraticBezierCurve3(start, control, end);
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(curve.getPoints(64)),
new THREE.LineBasicMaterial({
color: route.color,
transparent: true,
opacity: 0.82
})
);
cycleGroup.add(line);
const tangent = curve.getTangent(0.94).normalize();
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.17, 0.43, 3),
new THREE.MeshBasicMaterial({ color: route.color })
);
arrow.position.copy(curve.getPoint(0.94));
arrow.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
tangent
);
cycleGroup.add(arrow);
const particle = new THREE.Mesh(
new THREE.SphereGeometry(0.095, 10, 8),
new THREE.MeshBasicMaterial({ color: route.color })
);
cycleGroup.add(particle);
cyclePaths.push({ curve, particle, offset: cyclePaths.length / 3 });
}
cycleGroup.visible = true;
}
pairButtons.forEach(button => {
button.addEventListener("click", () => {
if (!autoRunning) {
attemptPair(Number(button.dataset.pair));
}
});
});
autoButton.addEventListener("click", () => {
if (autoRunning) return;
autoRunning = true;
autoButton.disabled = true;
autoButton.textContent = "Testing…";
const order = [0, 1, 2];
order.forEach((pair, i) => {
window.setTimeout(() => {
attemptPair(pair);
if (i === order.length - 1) {
window.setTimeout(() => {
autoRunning = false;
autoButton.disabled = false;
autoButton.textContent = "Replay all pairs";
}, 700);
}
}, i * 1150);
});
});
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerStart = null;
let dragging = false;
function pickTrader(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(stalls.map(s => s.group), true);
for (const hit of hits) {
let object = hit.object;
while (object && !object.userData.traderId) {
object = object.parent;
}
if (object?.userData.traderId) {
return object.userData.traderId;
}
}
return null;
}
renderer.domElement.addEventListener("pointerdown", event => {
if (!event.isPrimary || event.button !== 0) return;
pointerStart = { x: event.clientX, y: event.clientY };
dragging = false;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (pointerStart) {
const dx = event.clientX - pointerStart.x;
const dy = event.clientY - pointerStart.y;
if (Math.abs(dx) + Math.abs(dy) > 5) {
dragging = true;
}
if (dragging) {
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(cameraPhi + dy * 0.0045, 0.54, 1.30);
updateCamera();
renderer.domElement.style.cursor = "grabbing";
}
} else {
hoveredId = pickTrader(event);
renderer.domElement.style.cursor = hoveredId ? "pointer" : "grab";
}
});
renderer.domElement.addEventListener("pointerup", event => {
if (!pointerStart) return;
if (!dragging) {
const id = pickTrader(event);
if (id) {
if (selectedId === null) {
setSelection(id);
} else if (selectedId === id) {
setSelection(null);
} else {
const pairIndex = pairConfigs.findIndex(config =>
config.ids.some(index => traders[index].id === selectedId) &&
config.ids.some(index => traders[index].id === id)
);
attemptPair(pairIndex);
}
}
}
pointerStart = null;
dragging = false;
renderer.domElement.style.cursor = hoveredId ? "pointer" : "grab";
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerStart = null;
dragging = false;
});
renderer.domElement.addEventListener("pointerleave", () => {
hoveredId = null;
});
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraZoom = THREE.MathUtils.clamp(
cameraZoom - event.deltaY * 0.0009,
0.72,
1.65
);
updateCamera();
}, { passive: false });
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
stalls.forEach((stall, index) => {
stall.character.position.y = 0.08 + Math.sin(elapsed * 1.8 + index * 1.7) * 0.018;
stall.hoverRing.material.opacity = hoveredId === stall.trader.id ? 0.34 : 0;
stall.selectRing.material.opacity = selectedId === stall.trader.id ? 0.95 : 0;
const pulse = 1 + Math.sin(elapsed * 3.2) * 0.025;
stall.selectRing.scale.setScalar(selectedId === stall.trader.id ? pulse : 1);
const outcomeColor = outcomeMap.get(stall.trader.id);
stall.outcomeRing.material.opacity = outcomeColor !== undefined ? 0.9 : 0;
if (outcomeColor !== undefined) {
stall.outcomeRing.material.color.setHex(outcomeColor);
const outcomePulse = 1 + Math.sin(elapsed * 3.4 + index) * 0.018;
stall.outcomeRing.scale.setScalar(outcomePulse);
}
});
if (attemptVisual) {
const visual = attemptVisual;
visual.time += delta;
const t = visual.time;
const acceptedProgress = THREE.MathUtils.clamp(t / 1.35, 0, 1);
visual.acceptedToken.position.lerpVectors(
visual.acceptedStart,
visual.acceptedEnd,
acceptedProgress
);
visual.acceptedToken.position.y += Math.sin(acceptedProgress * Math.PI) * 0.85;
let rejectedProgress = THREE.MathUtils.clamp(t / 1.35, 0, 1);
if (t > 1.35) {
rejectedProgress = 1 - THREE.MathUtils.clamp((t - 1.35) / 1.25, 0, 1);
}
visual.rejectedToken.position.lerpVectors(
visual.rejectedStart,
visual.midpoint,
rejectedProgress
);
visual.rejectedToken.position.y += Math.sin(rejectedProgress * Math.PI) * 0.7;
visual.cross.visible = t > 0.55;
visual.cross.material.opacity = t < 0.8
? THREE.MathUtils.clamp((t - 0.55) / 0.25, 0, 1)
: 0.94;
visual.check.visible = t > 1.08;
visual.check.material.opacity = THREE.MathUtils.clamp((t - 1.08) / 0.35, 0, 1);
visual.acceptedToken.material.opacity = t > 2.0
? 1 - THREE.MathUtils.clamp((t - 2.0) / 0.9, 0, 1)
: 1;
visual.rejectedToken.material.opacity = t > 2.45
? 1 - THREE.MathUtils.clamp((t - 2.45) / 0.55, 0, 1)
: 1;
visual.line.material.opacity = 0.58 + Math.sin(elapsed * 4) * 0.15;
}
if (cycleGroup?.visible) {
cyclePaths.forEach(path => {
const progress = (elapsed * 0.105 + path.offset) % 1;
path.particle.position.copy(path.curve.getPoint(progress));
path.particle.position.y += 0.16 + Math.sin(progress * Math.PI * 2) * 0.08;
});
}
renderer.render(scene, camera);
}
animate();
window.setTimeout(() => loader.classList.add("hidden"), 350);
</script>
</body>
</html>
```