Prompt specification
Build or customize the Three.js 3D scene "Two Offers on a Negotiation Board" 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: Two Offers on a Negotiation Board
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-offers-on-a-negotiation-board-x3165
Scene Description Prompt:
Use Three.js from cdnjs to place goods and token stacks on opposing trays with visible minimum requirements. Show which proposed trades satisfy both sides before any agreement animation occurs. Deliver one self-contained HTML file with low-poly forms.
Working Three.js Starter (two-offers-on-a-negotiation-board-x3165):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Fair Exchange — Three.js Trading Scene</title>
<style>
:root {
--ink: #24312e;
--muted: #6f7771;
--paper: rgba(250, 247, 238, 0.9);
--paper-solid: #faf7ee;
--line: rgba(45, 61, 55, 0.16);
--green: #39745b;
--green-soft: #e1eee6;
--rust: #a35f3e;
--rust-soft: #f5e5dc;
--gold: #d6a63d;
--shadow: 0 18px 50px rgba(43, 47, 39, 0.13);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8e1d3;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
button:focus-visible {
outline: 3px solid rgba(57, 116, 91, 0.3);
outline-offset: 3px;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 43%, transparent 42%, rgba(76, 69, 54, 0.08) 100%),
linear-gradient(to bottom, rgba(255, 255, 255, 0.14), transparent 25%);
}
.topbar {
position: fixed;
top: 22px;
left: 26px;
right: 26px;
z-index: 5;
display: flex;
align-items: flex-start;
justify-content: space-between;
pointer-events: none;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
}
.brand-mark {
position: relative;
width: 45px;
height: 45px;
border: 1px solid rgba(36, 49, 46, 0.2);
border-radius: 14px;
background: rgba(250, 247, 238, 0.76);
box-shadow: 0 8px 24px rgba(45, 52, 44, 0.08);
backdrop-filter: blur(12px);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
background: var(--ink);
border-radius: 3px;
}
.brand-mark::before {
width: 21px;
height: 3px;
left: 11px;
top: 12px;
transform: rotate(-8deg);
}
.brand-mark::after {
width: 3px;
height: 18px;
left: 21px;
top: 13px;
}
.brand-copy small {
display: block;
margin-bottom: 3px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.brand-copy h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(21px, 2.3vw, 31px);
font-weight: 500;
letter-spacing: -0.025em;
}
.scene-help {
display: flex;
align-items: center;
gap: 9px;
padding: 10px 13px;
border: 1px solid rgba(36, 49, 46, 0.13);
border-radius: 999px;
background: rgba(250, 247, 238, 0.66);
color: #5d6761;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.04em;
backdrop-filter: blur(10px);
}
.scene-help span {
display: block;
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 4px rgba(57, 116, 91, 0.12);
}
.palette {
position: fixed;
top: 112px;
left: 26px;
z-index: 8;
width: 176px;
padding: 15px;
border: 1px solid rgba(36, 49, 46, 0.14);
border-radius: 18px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(16px);
}
.panel-kicker {
margin: 0 0 5px;
color: var(--muted);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.palette h2 {
margin: 0 0 5px;
font-family: Georgia, "Times New Roman", serif;
font-size: 18px;
font-weight: 500;
}
.palette-copy {
margin: 0 0 13px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.supply {
position: relative;
width: 100%;
min-height: 65px;
margin-top: 9px;
padding: 10px;
display: flex;
align-items: center;
gap: 11px;
border: 1px solid rgba(36, 49, 46, 0.12);
border-radius: 13px;
background: rgba(255, 255, 255, 0.48);
color: var(--ink);
cursor: grab;
user-select: none;
touch-action: none;
transition:
transform 160ms ease,
border-color 160ms ease,
background 160ms ease,
opacity 160ms ease;
}
.supply:hover:not(:disabled) {
transform: translateY(-2px);
border-color: rgba(57, 116, 91, 0.42);
background: rgba(255, 255, 255, 0.78);
}
.supply.selected {
border-color: var(--green);
background: var(--green-soft);
box-shadow: 0 0 0 3px rgba(57, 116, 91, 0.09);
}
.supply:active:not(:disabled) {
cursor: grabbing;
}
.supply:disabled {
opacity: 0.43;
cursor: not-allowed;
}
.supply strong {
display: block;
font-size: 12px;
}
.supply small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
}
.supply-count {
margin-left: auto;
align-self: flex-start;
min-width: 20px;
padding: 3px 6px;
border-radius: 999px;
background: rgba(36, 49, 46, 0.07);
color: #5c6761;
font-size: 10px;
font-weight: 800;
text-align: center;
}
.supply-icon {
position: relative;
flex: 0 0 37px;
width: 37px;
height: 37px;
}
.crate-icon {
width: 31px;
height: 27px;
margin-top: 5px;
border: 2px solid #725037;
border-radius: 3px;
background:
linear-gradient(90deg, transparent 44%, #725037 45%, #725037 55%, transparent 56%),
#b98450;
transform: rotate(-3deg);
box-shadow: inset 0 -5px rgba(74, 45, 27, 0.15);
}
.token-icon {
width: 32px;
height: 32px;
}
.token-icon i {
position: absolute;
left: 4px;
width: 28px;
height: 11px;
border: 2px solid #9b7020;
border-radius: 50%;
background: #e4b94e;
box-shadow: inset 0 2px rgba(255, 255, 255, 0.35);
}
.token-icon i:nth-child(1) {
top: 3px;
}
.token-icon i:nth-child(2) {
top: 10px;
}
.token-icon i:nth-child(3) {
top: 17px;
}
.palette-note {
margin: 12px 1px 0;
padding-top: 11px;
border-top: 1px solid var(--line);
color: #7b817c;
font-size: 10px;
line-height: 1.45;
}
.text-button {
width: 100%;
margin-top: 10px;
padding: 9px 10px;
border: 1px solid rgba(36, 49, 46, 0.13);
border-radius: 10px;
background: transparent;
color: #53635b;
font-size: 10px;
font-weight: 800;
cursor: pointer;
}
.text-button:hover:not(:disabled) {
background: rgba(255, 255, 255, 0.55);
}
.text-button:disabled {
opacity: 0.45;
cursor: not-allowed;
}
.tray-tag {
position: fixed;
z-index: 4;
width: 232px;
padding: 12px 13px;
border: 1px solid rgba(36, 49, 46, 0.15);
border-radius: 14px;
background: rgba(250, 247, 238, 0.88);
box-shadow: 0 12px 30px rgba(48, 51, 43, 0.1);
pointer-events: none;
transform: translate(-50%, -100%);
backdrop-filter: blur(12px);
transition:
border-color 180ms ease,
background 180ms ease;
}
.tray-tag::after {
content: "";
position: absolute;
left: 50%;
bottom: -7px;
width: 12px;
height: 12px;
border-right: 1px solid rgba(36, 49, 46, 0.15);
border-bottom: 1px solid rgba(36, 49, 46, 0.15);
background: rgba(250, 247, 238, 0.88);
transform: translateX(-50%) rotate(45deg);
}
.tray-tag.pass {
border-color: rgba(57, 116, 91, 0.5);
background: rgba(235, 245, 237, 0.92);
}
.tray-tag.short {
border-color: rgba(163, 95, 62, 0.5);
background: rgba(249, 237, 228, 0.92);
}
.tag-head {
display: flex;
align-items: center;
gap: 7px;
margin-bottom: 8px;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.side-dot {
width: 8px;
height: 8px;
border-radius: 3px;
background: var(--green);
transform: rotate(45deg);
}
.tray-tag.right .side-dot {
background: var(--rust);
}
.tag-state {
margin-left: auto;
padding: 3px 6px;
border-radius: 999px;
background: rgba(36, 49, 46, 0.07);
color: var(--muted);
font-size: 8px;
}
.tray-tag.pass .tag-state {
background: var(--green-soft);
color: var(--green);
}
.tray-tag.short .tag-state {
background: var(--rust-soft);
color: var(--rust);
}
.tag-minimum {
color: #5e6862;
font-size: 10px;
line-height: 1.4;
}
.tag-minimum strong {
color: var(--ink);
}
.tag-counts {
display: flex;
gap: 7px;
margin-top: 9px;
}
.tag-counts span {
flex: 1;
padding: 6px 7px;
border-radius: 8px;
background: rgba(36, 49, 46, 0.055);
color: #6c756f;
font-size: 9px;
}
.tag-counts b {
display: block;
margin-top: 2px;
color: var(--ink);
font-size: 12px;
}
.console {
position: fixed;
z-index: 7;
left: 50%;
bottom: 20px;
width: min(920px, calc(100vw - 40px));
padding: 17px 18px 16px;
border: 1px solid rgba(36, 49, 46, 0.15);
border-radius: 20px;
background: rgba(250, 247, 238, 0.94);
box-shadow: var(--shadow);
transform: translateX(-50%);
backdrop-filter: blur(18px);
}
.console-top {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 18px;
margin-bottom: 12px;
}
.console-title h2 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: 21px;
font-weight: 500;
}
.console-title p {
margin: 4px 0 0;
color: var(--muted);
font-size: 10px;
}
#verdict {
max-width: 330px;
padding: 8px 11px;
border-radius: 10px;
background: rgba(36, 49, 46, 0.055);
color: #66706a;
font-size: 10px;
font-weight: 700;
line-height: 1.4;
text-align: right;
}
#verdict.good {
background: var(--green-soft);
color: var(--green);
}
#verdict.bad {
background: var(--rust-soft);
color: var(--rust);
}
.checks {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
}
.check-card {
min-width: 0;
padding: 11px 12px;
border: 1px solid rgba(36, 49, 46, 0.1);
border-radius: 13px;
background: rgba(255, 255, 255, 0.48);
transition:
border-color 160ms ease,
background 160ms ease;
}
.check-card.pass {
border-color: rgba(57, 116, 91, 0.32);
background: rgba(231, 241, 233, 0.78);
}
.check-card.short {
border-color: rgba(163, 95, 62, 0.3);
background: rgba(247, 234, 225, 0.72);
}
.check-head {
display: flex;
align-items: center;
gap: 9px;
}
.check-number {
display: grid;
width: 25px;
height: 25px;
flex: 0 0 25px;
place-items: center;
border-radius: 8px;
background: var(--green);
color: white;
font-size: 9px;
font-weight: 900;
}
.check-card.right .check-number {
background: var(--rust);
}
.check-name {
min-width: 0;
}
.check-name strong {
display: block;
overflow: hidden;
font-size: 11px;
text-overflow: ellipsis;
white-space: nowrap;
}
.check-name small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 8px;
}
.status {
margin-left: auto;
padding: 4px 7px;
border-radius: 999px;
background: rgba(36, 49, 46, 0.07);
color: #778079;
font-size: 8px;
font-weight: 900;
letter-spacing: 0.07em;
}
.status.pass {
background: var(--green-soft);
color: var(--green);
}
.status.short {
background: var(--rust-soft);
color: var(--rust);
}
.mini-req {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-top: 9px;
}
.mini-req span {
padding: 6px 8px;
border-radius: 8px;
background: rgba(36, 49, 46, 0.045);
color: #707972;
font-size: 9px;
}
.mini-req b {
float: right;
color: var(--ink);
}
.actions {
display: flex;
align-items: center;
justify-content: flex-end;
gap: 9px;
margin-top: 13px;
}
.action-button {
min-height: 38px;
padding: 0 16px;
border: 1px solid rgba(36, 49, 46, 0.14);
border-radius: 11px;
background: transparent;
color: #53605a;
font-size: 10px;
font-weight: 850;
cursor: pointer;
transition:
transform 150ms ease,
background 150ms ease,
opacity 150ms ease;
}
.action-button:hover:not(:disabled) {
transform: translateY(-1px);
background: rgba(255, 255, 255, 0.6);
}
.action-button.primary {
border-color: var(--green);
background: var(--green);
color: white;
box-shadow: 0 8px 18px rgba(57, 116, 91, 0.2);
}
.action-button.primary:hover:not(:disabled) {
background: #2f654e;
}
.action-button:disabled {
opacity: 0.38;
cursor: not-allowed;
transform: none;
box-shadow: none;
}
.action-button#resetBtn {
margin-right: auto;
border-color: transparent;
}
.toast {
position: fixed;
z-index: 30;
top: 82px;
left: 50%;
max-width: min(420px, calc(100vw - 30px));
padding: 10px 14px;
border: 1px solid rgba(36, 49, 46, 0.14);
border-radius: 999px;
background: rgba(36, 49, 46, 0.9);
color: white;
box-shadow: 0 12px 30px rgba(25, 33, 30, 0.2);
font-size: 11px;
font-weight: 700;
text-align: center;
pointer-events: none;
opacity: 0;
transform: translate(-50%, -8px);
transition:
opacity 180ms ease,
transform 180ms ease;
}
.toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
.drag-ghost {
position: fixed;
z-index: 40;
width: 78px;
height: 78px;
display: grid;
place-items: center;
border: 1px solid rgba(36, 49, 46, 0.18);
border-radius: 18px;
background: rgba(250, 247, 238, 0.94);
box-shadow: 0 16px 35px rgba(39, 46, 40, 0.2);
pointer-events: none;
transform: translate(-50%, -50%) rotate(-4deg);
}
.drag-ghost[hidden] {
display: none;
}
.celebration {
position: fixed;
z-index: 12;
top: 42%;
left: 50%;
display: flex;
align-items: center;
gap: 14px;
padding: 15px 19px 15px 15px;
border: 1px solid rgba(57, 116, 91, 0.25);
border-radius: 18px;
background: rgba(244, 250, 245, 0.96);
box-shadow: 0 20px 55px rgba(36, 57, 48, 0.19);
opacity: 0;
pointer-events: none;
transform: translate(-50%, -35%) scale(0.92);
transition:
opacity 350ms ease,
transform 450ms cubic-bezier(0.2, 0.85, 0.25, 1.2);
}
.celebration.show {
opacity: 1;
transform: translate(-50%, -50%) scale(1);
}
.celebration-seal {
display: grid;
width: 48px;
height: 48px;
place-items: center;
border-radius: 15px;
background: var(--green);
color: white;
font-size: 25px;
font-weight: 700;
}
.celebration small {
display: block;
margin-bottom: 3px;
color: var(--green);
font-size: 8px;
font-weight: 900;
letter-spacing: 0.16em;
}
.celebration strong {
display: block;
font-family: Georgia, "Times New Roman", serif;
font-size: 19px;
font-weight: 500;
}
.celebration span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
@media (max-width: 850px) {
.topbar {
top: 15px;
left: 15px;
right: 15px;
}
.scene-help {
display: none;
}
.palette {
top: 86px;
left: 14px;
width: 146px;
padding: 11px;
}
.palette h2 {
font-size: 15px;
}
.palette-copy,
.palette-note {
display: none;
}
.supply {
min-height: 53px;
}
.supply-icon {
transform: scale(0.83);
}
.tray-tag {
width: 190px;
padding: 10px;
}
.tag-minimum {
font-size: 9px;
}
.console {
bottom: 10px;
width: calc(100vw - 20px);
padding: 13px;
}
.console-title h2 {
font-size: 18px;
}
#verdict {
max-width: 230px;
}
.check-card {
padding: 9px;
}
.check-name small {
display: none;
}
.mini-req {
margin-top: 7px;
}
.actions {
margin-top: 9px;
}
}
@media (max-width: 620px) {
.brand-copy h1 {
font-size: 20px;
}
.brand-copy small {
font-size: 8px;
}
.brand-mark {
width: 38px;
height: 38px;
}
.palette {
top: 75px;
width: 116px;
}
.panel-kicker,
.palette h2 {
display: none;
}
.supply {
margin-top: 6px;
min-height: 48px;
padding: 6px;
}
.supply > div:nth-child(2) strong {
font-size: 10px;
}
.supply > div:nth-child(2) small {
display: none;
}
.supply-icon {
transform: scale(0.68);
transform-origin: left center;
margin-right: -10px;
}
.tray-tag {
width: 162px;
}
.tag-head {
font-size: 7px;
}
.tag-minimum {
font-size: 8px;
}
.tag-counts {
gap: 4px;
}
.tag-counts span {
padding: 5px;
font-size: 8px;
}
.checks {
gap: 6px;
}
.check-head {
gap: 5px;
}
.check-number {
width: 20px;
height: 20px;
flex-basis: 20px;
}
.check-name strong {
font-size: 9px;
}
.status {
padding: 3px 4px;
font-size: 7px;
}
.mini-req {
grid-template-columns: 1fr;
}
.mini-req span {
padding: 4px 6px;
}
.console-top {
gap: 8px;
}
#verdict {
max-width: 165px;
font-size: 8px;
}
.actions {
gap: 5px;
}
.action-button {
flex: 1;
padding: 0 7px;
font-size: 8px;
}
.action-button#resetBtn {
flex: 0 0 auto;
}
}
</style>
</head>
<body>
<div id="stage" aria-label="Interactive 3D trading scene"></div>
<div class="vignette"></div>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div class="brand-copy">
<small>Common ground · trading post</small>
<h1>Two trays. One fair exchange.</h1>
</div>
</div>
<div class="scene-help">
<span></span>
Drag the scene to orbit · scroll to zoom
</div>
</header>
<aside class="palette" aria-label="Tradeable supplies">
<p class="panel-kicker">Offer supplies</p>
<h2>Build a proposal</h2>
<p class="palette-copy">
Drag a bundle onto either tray, or select one and click a tray.
</p>
<button
class="supply"
id="goodsSupply"
type="button"
aria-label="Drag a goods bundle to a tray"
aria-pressed="false"
>
<div class="supply-icon crate-icon" aria-hidden="true"></div>
<div>
<strong>Goods bundle</strong>
<small>Crate or grain sack</small>
</div>
<span class="supply-count" id="goodsAvailable">4</span>
</button>
<button
class="supply"
id="tokenSupply"
type="button"
aria-label="Drag a token stack to a tray"
aria-pressed="false"
>
<div class="supply-icon token-icon" aria-hidden="true"><i></i><i></i><i></i></div>
<div>
<strong>Token stack</strong>
<small>Accepted local token</small>
</div>
<span class="supply-count" id="tokensAvailable">10</span>
</button>
<p class="palette-note">
Each side must meet its own visible minimum before agreement can begin.
</p>
<button class="text-button" id="sampleBtn" type="button">
Fill a balanced example
</button>
</aside>
<div class="tray-tag left" id="leftTag" aria-live="polite">
<div class="tag-head">
<span class="side-dot"></span>
River Hamlet
<span class="tag-state" id="leftTagState">Pending</span>
</div>
<div class="tag-minimum">
Minimum:
<strong>2 goods + 5 token stacks</strong>
</div>
<div class="tag-counts">
<span>Goods on tray<b id="leftTagGoods">0 / 2</b></span>
<span>Token stacks<b id="leftTagTokens">0 / 5</b></span>
</div>
</div>
<div class="tray-tag right" id="rightTag" aria-live="polite">
<div class="tag-head">
<span class="side-dot"></span>
Ironworks Guild
<span class="tag-state" id="rightTagState">Pending</span>
</div>
<div class="tag-minimum">
Minimum:
<strong>1 good + 3 token stacks</strong>
</div>
<div class="tag-counts">
<span>Goods on tray<b id="rightTagGoods">0 / 1</b></span>
<span>Token stacks<b id="rightTagTokens">0 / 3</b></span>
</div>
</div>
<section class="console" aria-label="Trade proposal review">
<div class="console-top">
<div class="console-title">
<h2>Proposal review</h2>
<p>Verify each side against its minimum before agreeing.</p>
</div>
<div id="verdict" aria-live="polite">
Place supplies on the trays, then evaluate the proposal.
</div>
</div>
<div class="checks">
<article class="check-card" id="leftCheck">
<div class="check-head">
<span class="check-number">01</span>
<div class="check-name">
<strong>River Hamlet</strong>
<small>Minimum on its own tray</small>
</div>
<span class="status" id="leftStatus">Pending</span>
</div>
<div class="mini-req">
<span>Goods <b id="leftGoods">0 / 2</b></span>
<span>Token stacks <b id="leftTokens">0 / 5</b></span>
</div>
</article>
<article class="check-card right" id="rightCheck">
<div class="check-head">
<span class="check-number">02</span>
<div class="check-name">
<strong>Ironworks Guild</strong>
<small>Minimum on its own tray</small>
</div>
<span class="status" id="rightStatus">Pending</span>
</div>
<div class="mini-req">
<span>Goods <b id="rightGoods">0 / 1</b></span>
<span>Token stacks <b id="rightTokens">0 / 3</b></span>
</div>
</article>
</div>
<div class="actions">
<button class="action-button" id="resetBtn" type="button">Reset proposal</button>
<button class="action-button" id="evaluateBtn" type="button">Evaluate proposal</button>
<button class="action-button primary" id="agreeBtn" type="button" disabled>
Agree after evaluation
</button>
</div>
</section>
<div class="toast" id="toast" role="status" aria-live="polite"></div>
<div class="drag-ghost" id="dragGhost" hidden>
<div class="supply-icon" id="dragIcon"></div>
</div>
<div class="celebration" id="celebration">
<div class="celebration-seal">✓</div>
<div>
<small>Agreement</small>
<strong>Trade sealed fairly</strong>
<span>Both minimums were verified before the exchange.</span>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM references
// -------------------------------------------------------------------------
const $ = (id) => document.getElementById(id);
const stage = $("stage");
const toast = $("toast");
const dragGhost = $("dragGhost");
const dragIcon = $("dragIcon");
const celebration = $("celebration");
const goodsSupply = $("goodsSupply");
const tokenSupply = $("tokenSupply");
const requirements = {
left: {
name: "River Hamlet",
goods: 2,
tokens: 5,
accent: 0x39745b
},
right: {
name: "Ironworks Guild",
goods: 1,
tokens: 3,
accent: 0xa35f3e
}
};
const totalInventory = {
goods: 4,
tokens: 10
};
const state = {
phase: "proposal",
inventory: { ...totalInventory },
evaluation: null,
selected: null,
sides: {
left: {
counts: { goods: 0, tokens: 0 },
items: { goods: [], tokens: [] }
},
right: {
counts: { goods: 0, tokens: 0 },
items: { goods: [], tokens: [] }
}
}
};
// -------------------------------------------------------------------------
// Three.js scene
// -------------------------------------------------------------------------
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.08;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8e1d3);
scene.fog = new THREE.Fog(0xe8e1d3, 16, 34);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
80
);
const cameraTarget = new THREE.Vector3(0, 0.62, 0);
const orbit = {
theta: 0.28,
phi: 1.09,
radius: 14.8
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
cameraTarget.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
cameraTarget.y + orbit.radius * Math.cos(orbit.phi),
cameraTarget.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xfff9e9, 0x7a806e, 2.0);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff1d0, 3.1);
sun.position.set(-7, 12, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -11;
sun.shadow.camera.right = 11;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -8;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 32;
sun.shadow.bias = -0.0005;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb9d2cc, 0.65);
fill.position.set(8, 5, -6);
scene.add(fill);
const materials = {
ground: new THREE.MeshStandardMaterial({
color: 0xd8d1c0,
roughness: 0.96
}),
clearing: new THREE.MeshStandardMaterial({
color: 0xe2dac8,
roughness: 0.94
}),
stone: new THREE.MeshStandardMaterial({
color: 0xb9b3a2,
roughness: 0.95
}),
wood: new THREE.MeshStandardMaterial({
color: 0x9b7047,
roughness: 0.78
}),
woodDark: new THREE.MeshStandardMaterial({
color: 0x65462f,
roughness: 0.86
}),
woodLight: new THREE.MeshStandardMaterial({
color: 0xb48656,
roughness: 0.75
}),
crate: new THREE.MeshStandardMaterial({
color: 0xb87f49,
roughness: 0.78
}),
crateBand: new THREE.MeshStandardMaterial({
color: 0x704b31,
roughness: 0.8
}),
sack: new THREE.MeshStandardMaterial({
color: 0xc7aa73,
roughness: 0.92
}),
sackTie: new THREE.MeshStandardMaterial({
color: 0x80603a,
roughness: 0.9
}),
gold: new THREE.MeshStandardMaterial({
color: 0xd6a63d,
metalness: 0.48,
roughness: 0.34,
emissive: 0x5a3500,
emissiveIntensity: 0.12
}),
goldLight: new THREE.MeshStandardMaterial({
color: 0xe9c45d,
metalness: 0.42,
roughness: 0.3
}),
goldDark: new THREE.MeshStandardMaterial({
color: 0x9e7022,
metalness: 0.52,
roughness: 0.42
}),
hill: new THREE.MeshStandardMaterial({
color: 0x9ba68d,
roughness: 1
}),
hillDark: new THREE.MeshStandardMaterial({
color: 0x7f927c,
roughness: 1
}),
leaf: new THREE.MeshStandardMaterial({
color: 0x5f7d65,
roughness: 0.95
}),
trunk: new THREE.MeshStandardMaterial({
color: 0x806044,
roughness: 1
})
};
function enableShadows(object) {
object.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
return object;
}
function makeBox(width, height, depth, material, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
return mesh;
}
// Ground and low-poly landscape.
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 50),
materials.ground
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.035;
ground.receiveShadow = true;
scene.add(ground);
const clearing = new THREE.Mesh(
new THREE.CylinderGeometry(5.35, 5.55, 0.07, 48),
materials.clearing
);
clearing.position.set(0, 0.005, 0.25);
clearing.receiveShadow = true;
scene.add(clearing);
for (let i = 0; i < 7; i++) {
const stone = new THREE.Mesh(
new THREE.CylinderGeometry(0.31, 0.36, 0.07, 7),
materials.stone
);
stone.position.set(
-0.72 + i * 0.24,
0.08,
1.38 + Math.sin(i * 1.8) * 0.12
);
stone.rotation.y = i * 0.63;
stone.receiveShadow = true;
scene.add(stone);
}
function addTree(x, z, scale = 1) {
const tree = new THREE.Group();
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.1, 0.15, 1.25, 6),
materials.trunk
);
trunk.position.y = 0.62;
tree.add(trunk);
const crownA = new THREE.Mesh(
new THREE.ConeGeometry(0.58, 1.35, 7),
materials.leaf
);
crownA.position.y = 1.55;
tree.add(crownA);
const crownB = new THREE.Mesh(
new THREE.ConeGeometry(0.43, 1.0, 7),
materials.hillDark
);
crownB.position.y = 2.05;
tree.add(crownB);
tree.position.set(x, 0, z);
tree.scale.setScalar(scale);
enableShadows(tree);
scene.add(tree);
}
addTree(-7.0, -4.9, 1.25);
addTree(-5.8, -6.3, 0.92);
addTree(5.9, -5.8, 1.12);
addTree(7.4, -4.2, 0.8);
function addHill(x, z, sx, sy, material) {
const hill = new THREE.Mesh(
new THREE.DodecahedronGeometry(1.55, 0),
material
);
hill.position.set(x, 0.15, z);
hill.scale.set(sx, sy, 1);
hill.receiveShadow = true;
scene.add(hill);
}
addHill(-5.8, -9.2, 2.0, 0.9, materials.hillDark);
addHill(-1.0, -10.3, 2.7, 0.75, materials.hill);
addHill(4.4, -9.6, 2.5, 0.9, materials.hillDark);
addHill(8.2, -10.8, 2.7, 0.72, materials.hill);
// -------------------------------------------------------------------------
// Tray construction
// -------------------------------------------------------------------------
const trays = {};
const dropMeshes = [];
function createTray(side, xPosition, accentColor) {
const group = new THREE.Group();
group.position.set(xPosition, 0, 0);
group.rotation.y = side === "left" ? 0.075 : -0.075;
scene.add(group);
const accent = new THREE.MeshStandardMaterial({
color: accentColor,
roughness: 0.75
});
// Four wooden planks form the tray floor.
for (let i = 0; i < 4; i++) {
const plank = makeBox(
3.02,
0.14,
0.46,
i % 2 === 0 ? materials.woodLight : materials.wood,
0,
0.78,
-0.69 + i * 0.46
);
group.add(plank);
}
// Raised rims.
group.add(makeBox(3.18, 0.26, 0.13, materials.woodDark, 0, 0.98, -1.02));
group.add(makeBox(3.18, 0.26, 0.13, materials.woodDark, 0, 0.98, 1.02));
group.add(makeBox(0.13, 0.26, 2.08, materials.woodDark, -1.53, 0.98, 0));
group.add(makeBox(0.13, 0.26, 2.08, materials.woodDark, 1.53, 0.98, 0));
// Six-sided legs.
for (const x of [-1.28, 1.28]) {
for (const z of [-0.76, 0.76]) {
const leg = new THREE.Mesh(
new THREE.CylinderGeometry(0.12, 0.16, 0.72, 6),
materials.woodDark
);
leg.position.set(x, 0.37, z);
group.add(leg);
}
}
// Cross supports.
group.add(makeBox(2.65, 0.12, 0.12, materials.woodDark, 0, 0.31, -0.76));
group.add(makeBox(2.65, 0.12, 0.12, materials.woodDark, 0, 0.31, 0.76));
// Colored front plaque differentiates the parties.
group.add(makeBox(0.8, 0.19, 0.075, accent, 0, 0.92, 1.13));
// Invisible, raycastable drop surface.
const dropMaterial = new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false
});
const dropMesh = new THREE.Mesh(
new THREE.PlaneGeometry(2.98, 1.96),
dropMaterial
);
dropMesh.rotation.x = -Math.PI / 2;
dropMesh.position.y = 1.015;
dropMesh.userData.side = side;
group.add(dropMesh);
dropMeshes.push(dropMesh);
enableShadows(group);
trays[side] = group;
}
createTray("left", -3.05, requirements.left.accent);
createTray("right", 3.05, requirements.right.accent);
// -------------------------------------------------------------------------
// Goods and token stack models
// -------------------------------------------------------------------------
function createGoods(index) {
const group = new THREE.Group();
if (index % 2 === 0) {
const crate = makeBox(0.62, 0.48, 0.5, materials.crate, 0, 0.24, 0);
group.add(crate);
const bandA = makeBox(
0.09,
0.52,
0.54,
materials.crateBand,
0,
0.24,
0
);
group.add(bandA);
const bandB = makeBox(
0.66,
0.08,
0.54,
materials.crateBand,
0,
0.17,
0
);
group.add(bandB);
const topBand = makeBox(
0.1,
0.51,
0.54,
materials.crateBand,
0,
0.24,
0
);
topBand.visible = false;
group.add(topBand);
} else {
const sack = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.34, 0),
materials.sack
);
sack.scale.set(1.0, 1.12, 0.92);
sack.position.y = 0.31;
group.add(sack);
const neck = new THREE.Mesh(
new THREE.CylinderGeometry(0.11, 0.15, 0.16, 6),
materials.sackTie
);
neck.position.y = 0.64;
group.add(neck);
const tie = new THREE.Mesh(
new THREE.TorusGeometry(0.12, 0.025, 5, 10),
materials.sackTie
);
tie.rotation.x = Math.PI / 2;
tie.position.y = 0.65;
group.add(tie);
}
enableShadows(group);
return group;
}
function createTokenStack(index) {
const group = new THREE.Group();
for (let i = 0; i < 4; i++) {
const token = new THREE.Mesh(
new THREE.CylinderGeometry(0.22, 0.22, 0.075, 10),
i % 2 === 0 ? materials.gold : materials.goldLight
);
token.position.y = 0.04 + i * 0.073;
token.rotation.y = index * 0.43 + i * 0.28;
group.add(token);
}
enableShadows(group);
return group;
}
const goodSlots = [
[-1.00, -0.52],
[-0.34, -0.50],
[-0.90, 0.18],
[-0.25, 0.20],
[-0.66, 0.66]
];
const tokenSlots = [
[0.42, -0.68],
[0.95, -0.52],
[0.42, -0.13],
[0.96, 0.03],
[0.42, 0.52],
[0.96, 0.66],
[0.12, 0.00],
[1.22, 0.25]
];
function layoutTray(side) {
const data = state.sides[side];
data.items.goods.forEach((item, index) => {
const slot = goodSlots[index % goodSlots.length];
item.position.set(slot[0], 1.015, slot[1]);
item.rotation.y = (index % 2 === 0 ? -1 : 1) * 0.12;
item.scale.setScalar(1);
});
data.items.tokens.forEach((item, index) => {
const slot = tokenSlots[index % tokenSlots.length];
item.position.set(slot[0], 1.015, slot[1]);
item.rotation.y = index * 0.42;
item.scale.setScalar(1);
});
}
// -------------------------------------------------------------------------
// Proposal state and interface
// -------------------------------------------------------------------------
let toastTimer = 0;
function showToast(message) {
clearTimeout(toastTimer);
toast.textContent = message;
toast.classList.add("show");
toastTimer = setTimeout(() => toast.classList.remove("show"), 2600);
}
function setSelected(kind) {
if (state.phase !== "proposal") {
state.selected = null;
updateUI();
return;
}
if (state.inventory[kind] <= 0) {
showToast(`No ${kind === "goods" ? "goods bundles" : "token stacks"} remain.`);
return;
}
state.selected = state.selected === kind ? null : kind;
updateUI();
}
function addSupply(side, kind, silent = false) {
if (state.phase !== "proposal") {
if (!silent) showToast("This proposal is already sealed.");
return false;
}
if (state.inventory[kind] <= 0) {
if (!silent) {
showToast(`No ${kind === "goods" ? "goods bundles" : "token stacks"} remain.`);
}
return false;
}
const sideState = state.sides[side];
sideState.counts[kind] += 1;
state.inventory[kind] -= 1;
state.evaluation = null;
const item =
kind === "goods"
? createGoods(sideState.items.goods.length)
: createTokenStack(sideState.items.tokens.length);
trays[side].add(item);
sideState.items[kind].push(item);
layoutTray(side);
if (state.inventory[kind] === 0 && state.selected === kind) {
state.selected = null;
}
updateUI();
if (!silent) {
const sideName = requirements[side].name;
showToast(
`${kind === "goods" ? "Goods bundle" : "Token stack"} placed on ${sideName}’s tray.`
);
}
return true;
}
function evaluateProposal() {
if (state.phase !== "proposal") return;
const left =
state.sides.left.counts.goods >= requirements.left.goods &&
state.sides.left.counts.tokens >= requirements.left.tokens;
const right =
state.sides.right.counts.goods >= requirements.right.goods &&
state.sides.right.counts.tokens >= requirements.right.tokens;
state.evaluation = {
left,
right,
both: left && right
};
updateUI();
if (state.evaluation.both) {
showToast("Both sides satisfy their minimums. Agreement is now unlocked.");
} else {
const shortSide = !left ? "River Hamlet" : "Ironworks Guild";
showToast(`${shortSide} still has a minimum requirement to meet.`);
}
}
function resetProposal() {
if (state.phase === "animating") return;
for (const side of ["left", "right"]) {
for (const kind of ["goods", "tokens"]) {
for (const item of state.sides[side].items[kind]) {
item.parent?.remove(item);
}
state.sides[side].items[kind].length = 0;
state.sides[side].counts[kind] = 0;
}
}
state.inventory = { ...totalInventory };
state.evaluation = null;
state.phase = "proposal";
state.selected = null;
celebration.classList.remove("show");
clearAgreementCues();
updateUI();
}
function fillBalancedExample() {
if (state.phase !== "proposal") return;
resetProposal();
addSupply("left", "goods", true);
addSupply("left", "goods", true);
addSupply("left", "tokens", true);
addSupply("left", "tokens", true);
addSupply("left", "tokens", true);
addSupply("left", "tokens", true);
addSupply("left", "tokens", true);
addSupply("right", "goods", true);
addSupply("right", "tokens", true);
addSupply("right", "tokens", true);
addSupply("right", "tokens", true);
showToast("Balanced proposal loaded. Evaluate it to verify both sides.");
}
function statusFor(side) {
if (state.phase === "complete") return "Sealed";
if (state.phase === "animating") return "Sealing";
if (!state.evaluation) return "Pending";
return state.evaluation[side] ? "Pass" : "Short";
}
function updateUI() {
$("goodsAvailable").textContent = state.inventory.goods;
$("tokensAvailable").textContent = state.inventory.tokens;
goodsSupply.disabled =
state.phase !== "proposal" || state.inventory.goods <= 0;
tokenSupply.disabled =
state.phase !== "proposal" || state.inventory.tokens <= 0;
goodsSupply.classList.toggle("selected", state.selected === "goods");
tokenSupply.classList.toggle("selected", state.selected === "tokens");
goodsSupply.setAttribute("aria-pressed", String(state.selected === "goods"));
tokenSupply.setAttribute("aria-pressed", String(state.selected === "tokens"));
for (const side of ["left", "right"]) {
const requirement = requirements[side];
const counts = state.sides[side].counts;
const status = statusFor(side);
const passed = state.evaluation?.[side] === true;
const failed = state.evaluation?.[side] === false;
$(`${side}Goods`).textContent = `${counts.goods} / ${requirement.goods}`;
$(`${side}Tokens`).textContent = `${counts.tokens} / ${requirement.tokens}`;
$(`${side}TagGoods`).textContent = `${counts.goods} / ${requirement.goods}`;
$(`${side}TagTokens`).textContent = `${counts.tokens} / ${requirement.tokens}`;
const statusElement = $(`${side}Status`);
statusElement.textContent = status;
statusElement.className = `status${passed ? " pass" : ""}${failed ? " short" : ""}`;
const tag = $(`${side}Tag`);
tag.classList.toggle("pass", passed || state.phase === "complete");
tag.classList.toggle("short", failed);
$(`${side}TagState`).textContent = status;
}
const leftCheck = $("leftCheck");
const rightCheck = $("rightCheck");
leftCheck.classList.toggle("pass", state.evaluation?.left === true);
leftCheck.classList.toggle("short", state.evaluation?.left === false);
rightCheck.classList.toggle("pass", state.evaluation?.right === true);
rightCheck.classList.toggle("short", state.evaluation?.right === false);
const verdict = $("verdict");
verdict.className = "";
if (state.phase === "complete") {
verdict.textContent =
"Agreement complete. Both sides were verified before the exchange began.";
verdict.classList.add("good");
} else if (state.phase === "animating") {
verdict.textContent =
"Both sides passed. The agreement and exchange are now underway.";
verdict.classList.add("good");
} else if (state.evaluation?.both) {
verdict.textContent =
"Both sides satisfy their minimums. Agreement is unlocked.";
verdict.classList.add("good");
} else if (state.evaluation) {
const shortSide = !state.evaluation.left
? "River Hamlet"
: "Ironworks Guild";
verdict.textContent = `${shortSide} is short. Adjust the proposal and evaluate again.`;
verdict.classList.add("bad");
} else {
verdict.textContent =
"Place supplies on the trays, then evaluate the proposal.";
}
$("evaluateBtn").disabled = state.phase !== "proposal";
$("sampleBtn").disabled = state.phase !== "proposal";
$("resetBtn").disabled = state.phase === "animating";
$("resetBtn").textContent =
state.phase === "complete" ? "Start new round" : "Reset proposal";
$("agreeBtn").disabled =
state.phase !== "proposal" || state.evaluation?.both !== true;
$("agreeBtn").textContent =
state.phase === "complete"
? "Agreement sealed"
: state.phase === "animating"
? "Agreement in progress"
: state.evaluation?.both
? "Agree & exchange"
: "Agree after evaluation";
}
// -------------------------------------------------------------------------
// Raycasting and placement
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function getTrayAt(clientX, clientY) {
const rect = renderer.domElement.getBoundingClientRect();
if (
clientX < rect.left ||
clientX > rect.right ||
clientY < rect.top ||
clientY > rect.bottom
) {
return null;
}
pointer.x = ((clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((clientY - rect.top) / rect.height) * 2 + 1;
camera.updateMatrixWorld();
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(dropMeshes, false);
return hits.length ? hits[0].object.userData.side : null;
}
// Supply drag state.
let dragState = null;
function supplyIconMarkup(kind) {
return kind === "goods"
? '<div class="crate-icon"></div>'
: '<div class="token-icon"><i></i><i></i><i></i></div>';
}
function beginSupplyDrag(event, kind) {
if (state.phase !== "proposal" || state.inventory[kind] <= 0) return;
event.preventDefault();
dragState = {
kind,
startX: event.clientX,
startY: event.clientY,
moved: false
};
dragIcon.innerHTML = supplyIconMarkup(kind);
dragGhost.hidden = false;
dragGhost.style.left = `${event.clientX}px`;
dragGhost.style.top = `${event.clientY}px`;
}
window.addEventListener("pointermove", (event) => {
if (!dragState) return;
const distance = Math.hypot(
event.clientX - dragState.startX,
event.clientY - dragState.startY
);
if (distance > 6) dragState.moved = true;
if (dragState.moved) {
dragGhost.style.left = `${event.clientX}px`;
dragGhost.style.top = `${event.clientY}px`;
}
});
window.addEventListener("pointerup", (event) => {
if (!dragState) return;
const finishedDrag = dragState;
dragState = null;
dragGhost.hidden = true;
if (finishedDrag.moved) {
const side = getTrayAt(event.clientX, event.clientY);
if (side) {
addSupply(side, finishedDrag.kind);
} else {
showToast("Drop the bundle directly onto one of the two wooden trays.");
}
} else {
setSelected(finishedDrag.kind);
}
});
window.addEventListener("pointercancel", () => {
dragState = null;
dragGhost.hidden = true;
});
goodsSupply.addEventListener("pointerdown", (event) =>
beginSupplyDrag(event, "goods")
);
tokenSupply.addEventListener("pointerdown", (event) =>
beginSupplyDrag(event, "tokens")
);
// Keyboard-accessible selection.
goodsSupply.addEventListener("click", (event) => {
if (event.detail === 0) setSelected("goods");
});
tokenSupply.addEventListener("click", (event) => {
if (event.detail === 0) setSelected("tokens");
});
// Direct camera controls implemented without an addon.
let canvasPointer = null;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
renderer.domElement.setPointerCapture(event.pointerId);
canvasPointer = {
x: event.clientX,
y: event.clientY,
theta: orbit.theta,
phi: orbit.phi,
moved: false
};
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!canvasPointer) return;
const dx = event.clientX - canvasPointer.x;
const dy = event.clientY - canvasPointer.y;
if (Math.hypot(dx, dy) > 4) canvasPointer.moved = true;
if (canvasPointer.moved) {
orbit.theta = canvasPointer.theta - dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
canvasPointer.phi + dy * 0.0045,
0.58,
1.36
);
updateCamera();
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!canvasPointer) return;
const wasClick = !canvasPointer.moved;
canvasPointer = null;
if (wasClick && state.selected) {
const side = getTrayAt(event.clientX, event.clientY);
if (side) {
addSupply(side, state.selected);
} else {
showToast("Click directly on a wooden tray to place the selected bundle.");
}
}
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.008,
10.5,
20
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
// -------------------------------------------------------------------------
// Agreement animation
// -------------------------------------------------------------------------
let agreementCues = null;
let animation = null;
function clearAgreementCues() {
if (!agreementCues) return;
agreementCues.traverse((child) => {
if (child.geometry) child.geometry.dispose();
if (child.material) {
if (Array.isArray(child.material)) {
child.material.forEach((material) => material.dispose());
} else {
child.material.dispose();
}
}
});
agreementCues.parent?.remove(agreementCues);
agreementCues = null;
}
function createAgreementCues() {
clearAgreementCues();
const group = new THREE.Group();
scene.add(group);
const lineMaterial = new THREE.MeshBasicMaterial({
color: 0xe0b34b,
transparent: true,
opacity: 0
});
for (const z of [-0.32, 0.32]) {
const curve = new THREE.CatmullRomCurve3([
new THREE.Vector3(-2.95, 1.25, z),
new THREE.Vector3(0, 2.15, z),
new THREE.Vector3(2.95, 1.25, z)
]);
const tube = new THREE.Mesh(
new THREE.TubeGeometry(curve, 36, 0.026, 5, false),
lineMaterial
);
group.add(tube);
}
const seal = new THREE.Group();
seal.position.set(0, 1.3, 0);
seal.scale.setScalar(0.001);
const sealMaterial = new THREE.MeshStandardMaterial({
color: 0xe1b54b,
metalness: 0.58,
roughness: 0.28,
emissive: 0x754600,
emissiveIntensity: 0.25,
transparent: true,
opacity: 0
});
const core = new THREE.Mesh(
new THREE.OctahedronGeometry(0.52, 0),
sealMaterial
);
seal.add(core);
const ringMaterial = new THREE.MeshBasicMaterial({
color: 0xf4d47b,
transparent: true,
opacity: 0
});
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.72, 0.035, 6, 28),
ringMaterial
);
ring.rotation.x = Math.PI / 2;
seal.add(ring);
group.add(seal);
agreementCues = {
group,
lineMaterial,
seal,
sealMaterial,
ringMaterial
};
}
function startAgreement() {
if (
state.phase !== "proposal" ||
state.evaluation?.both !== true
) {
showToast("Evaluate the proposal before agreeing.");
return;
}
state.phase = "animating";
state.selected = null;
createAgreementCues();
const moving = [];
for (const side of ["left", "right"]) {
const other = side === "left" ? "right" : "left";
for (const kind of ["goods", "tokens"]) {
const items = state.sides[side].items[kind];
items.forEach((item, index) => {
const startLocal = item.position.clone();
const startRotation = item.rotation.clone();
let targetLocal;
if (kind === "goods") {
targetLocal = new THREE.Vector3(
-0.86 + (index % 2) * 0.58,
1.04,
-0.52 + Math.floor(index / 2) * 0.52
);
} else {
targetLocal = new THREE.Vector3(
0.28 + (index % 3) * 0.52,
1.04,
-0.58 + Math.floor(index / 3) * 0.55
);
}
const targetWorld = trays[other].localToWorld(targetLocal.clone());
const targetInOriginalParent =
trays[side].worldToLocal(targetWorld.clone());
moving.push({
item,
startLocal,
targetLocal: targetInOriginalParent,
startRotation,
delay: 0.18 + index * 0.1
});
});
}
}
animation = {
time: 0,
moving
};
updateUI();
showToast("Both sides passed. The trade is now being sealed.");
}
function easeOutBack(value) {
const c1 = 1.70158;
const c3 = c1 + 1;
return 1 + c3 * Math.pow(value - 1, 3) + c1 * Math.pow(value - 1, 2);
}
function updateAgreementAnimation(delta) {
if (!animation) return;
animation.time += delta;
const time = animation.time;
for (const move of animation.moving) {
const progress = THREE.MathUtils.clamp(
(time - move.delay) / 1.55,
0,
1
);
const eased = progress * progress * (3 - 2 * progress);
move.item.position.lerpVectors(
move.startLocal,
move.targetLocal,
eased
);
move.item.position.y += Math.sin(Math.PI * progress) * 1.7;
move.item.rotation.y =
move.startRotation.y + eased * Math.PI * 1.4;
move.item.rotation.z =
move.startRotation.z + Math.sin(Math.PI * progress) * 0.12;
const landingScale =
progress < 1
? 0.94 + Math.sin(Math.PI * progress) * 0.08
: 1;
move.item.scale.setScalar(landingScale);
}
if (agreementCues) {
const sealProgress = THREE.MathUtils.clamp((time - 0.42) / 1.35, 0, 1);
const sealScale = easeOutBack(sealProgress);
agreementCues.seal.scale.setScalar(Math.max(0.001, sealScale));
agreementCues.seal.position.y = 1.25 + sealProgress * 0.66;
agreementCues.seal.rotation.y = time * 1.8;
agreementCues.seal.rotation.z = Math.sin(time * 2) * 0.08;
agreementCues.sealMaterial.opacity = Math.min(1, sealProgress * 1.8);
agreementCues.ringMaterial.opacity = Math.min(
0.9,
sealProgress * 1.5
);
const lineProgress = THREE.MathUtils.clamp((time - 0.2) / 1.1, 0, 1);
agreementCues.lineMaterial.opacity =
Math.sin(lineProgress * Math.PI) * 0.85;
}
if (time > 3.05 && state.phase === "animating") {
state.phase = "complete";
animation = null;
celebration.classList.add("show");
updateUI();
showToast("Trade complete. Both requirements were satisfied before agreement.");
}
}
// -------------------------------------------------------------------------
// UI events
// -------------------------------------------------------------------------
$("evaluateBtn").addEventListener("click", evaluateProposal);
$("agreeBtn").addEventListener("click", startAgreement);
$("resetBtn").addEventListener("click", resetProposal);
$("sampleBtn").addEventListener("click", fillBalancedExample);
// -------------------------------------------------------------------------
// Floating labels and render loop
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function positionLabel(side, element) {
trays[side].localToWorld(projected.set(0, 2.65, 0));
projected.project(camera);
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.visibility =
projected.z > 1 || projected.z < -1 ? "hidden" : "visible";
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
updateAgreementAnimation(delta);
scene.updateMatrixWorld();
camera.updateMatrixWorld();
positionLabel("left", $("leftTag"));
positionLabel("right", $("rightTag"));
renderer.render(scene, camera);
}
updateUI();
animate();
</script>
</body>
</html>
```