Prompt specification
Build or customize the Three.js 3D scene "Demand Shift and Capacity Constraint" 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: Demand Shift and Capacity Constraint
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/demand-shift-and-capacity-constraint-x3144
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows supply and demand schedules in a fictional market. Create two linked toy markets with transparent buyer preferences and limited supply. Change one assumption at a time and compare outcomes, clearly labeling this illustrative model. Low-poly.
Working Three.js Starter (demand-shift-and-capacity-constraint-x3144):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Two Markets — Illustrative Supply & Demand Lab</title>
<style>
:root {
--ink: #18343d;
--muted: #65777b;
--paper: #f3efe6;
--card: #fffdf8;
--line: #d9d5ca;
--coral: #ef6a5b;
--teal: #249b91;
--gold: #e8b84a;
--blue: #527eb4;
--purple: #8b6bb4;
--shadow: 0 14px 40px rgba(27, 47, 52, 0.09);
}
* { box-sizing: border-box; }
body {
margin: 0;
color: var(--ink);
background:
radial-gradient(circle at 12% 0%, rgba(232, 184, 74, 0.13), transparent 28rem),
radial-gradient(circle at 90% 10%, rgba(36, 155, 145, 0.10), transparent 32rem),
var(--paper);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
line-height: 1.45;
}
button, input, select { font: inherit; }
button { color: inherit; }
.page {
width: min(1540px, calc(100% - 32px));
margin: 0 auto;
padding: 28px 0 48px;
}
.masthead {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 28px;
margin-bottom: 20px;
}
.eyebrow {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 9px;
margin-bottom: 8px;
color: var(--teal);
font-size: 0.76rem;
font-weight: 850;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.model-badge {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 6px 10px;
border: 1px solid rgba(232, 184, 74, 0.62);
border-radius: 999px;
color: #6b5414;
background: rgba(255, 244, 204, 0.86);
letter-spacing: 0.08em;
}
.model-badge::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--gold);
box-shadow: 0 0 0 4px rgba(232, 184, 74, 0.16);
}
h1 {
margin: 0;
max-width: 850px;
font-size: clamp(2rem, 4vw, 4.15rem);
line-height: 0.99;
letter-spacing: -0.055em;
}
.intro {
max-width: 820px;
margin: 13px 0 0;
color: var(--muted);
font-size: 1.02rem;
}
.linked-badge {
flex: 0 0 auto;
display: grid;
place-items: center;
width: 112px;
height: 112px;
border: 1px solid rgba(24, 52, 61, 0.12);
border-radius: 50%;
background: rgba(255, 253, 248, 0.64);
box-shadow: inset 0 0 0 9px rgba(36, 155, 145, 0.05);
color: var(--teal);
font-size: 0.7rem;
font-weight: 850;
letter-spacing: 0.12em;
text-align: center;
text-transform: uppercase;
}
.linked-badge span {
display: block;
margin-top: 3px;
color: var(--ink);
font-size: 1.05rem;
letter-spacing: -0.02em;
text-transform: none;
}
.scenario-panel {
position: relative;
overflow: hidden;
padding: 19px;
border-radius: 19px;
color: #f8fbf9;
background: var(--ink);
box-shadow: var(--shadow);
}
.scenario-panel::after {
content: "";
position: absolute;
right: -70px;
top: -100px;
width: 260px;
height: 260px;
border: 45px solid rgba(255, 255, 255, 0.035);
border-radius: 50%;
pointer-events: none;
}
.scenario-heading {
position: relative;
z-index: 1;
display: flex;
justify-content: space-between;
align-items: flex-end;
gap: 20px;
margin-bottom: 14px;
}
.scenario-heading h2 {
margin: 0;
font-size: 1.08rem;
letter-spacing: -0.02em;
}
.scenario-heading p {
margin: 3px 0 0;
color: #b9c8c9;
font-size: 0.84rem;
}
.held-note {
flex: 0 0 auto;
padding: 6px 10px;
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 999px;
color: #d4dfdf;
font-size: 0.72rem;
font-weight: 750;
}
.assumption-grid {
position: relative;
z-index: 1;
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
.assumption-card {
min-width: 0;
padding: 13px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 13px;
background: rgba(255, 255, 255, 0.055);
transition: border-color 160ms ease, background 160ms ease;
}
.assumption-card.active {
border-color: rgba(232, 184, 74, 0.88);
background: rgba(232, 184, 74, 0.10);
box-shadow: inset 0 0 0 1px rgba(232, 184, 74, 0.12);
}
.assumption-title {
display: flex;
width: 100%;
align-items: center;
justify-content: space-between;
gap: 10px;
padding: 0;
border: 0;
background: transparent;
color: inherit;
cursor: pointer;
text-align: left;
}
.assumption-title strong {
font-size: 0.88rem;
}
.state-chip {
flex: 0 0 auto;
padding: 3px 7px;
border-radius: 999px;
color: #b9c8c9;
background: rgba(255, 255, 255, 0.08);
font-size: 0.61rem;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.assumption-card.active .state-chip {
color: #ffe7a4;
background: rgba(232, 184, 74, 0.17);
}
.assumption-values {
display: grid;
grid-template-columns: 48px 1fr;
gap: 8px;
align-items: center;
margin-top: 12px;
}
.market-tag {
display: grid;
place-items: center;
width: 36px;
height: 30px;
border-radius: 8px;
color: #c7d2d3;
background: rgba(255, 255, 255, 0.08);
font-size: 0.68rem;
font-weight: 900;
}
.assumption-card.active .market-tag:first-child {
color: #d7eeee;
background: rgba(36, 155, 145, 0.28);
}
.assumption-card.active .market-tag:last-child {
color: #3e3210;
background: var(--gold);
}
.control-row {
min-width: 0;
display: flex;
align-items: center;
gap: 10px;
}
.control-value {
min-width: 56px;
color: #fff;
font-size: 0.84rem;
font-weight: 800;
white-space: nowrap;
}
input[type="range"] {
width: 100%;
accent-color: var(--gold);
cursor: pointer;
}
input[type="range"]:disabled { cursor: not-allowed; opacity: 0.45; }
select {
width: 100%;
padding: 6px 8px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 7px;
color: #fff;
background: #24444e;
font-size: 0.78rem;
font-weight: 750;
}
select:disabled { opacity: 0.45; }
.comparison-strip {
position: relative;
z-index: 1;
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 10px 18px;
margin-top: 13px;
padding-top: 13px;
border-top: 1px solid rgba(255, 255, 255, 0.11);
color: #c8d4d5;
font-size: 0.78rem;
}
.comparison-strip strong { color: #fff; }
.comparison-strip .arrow { color: var(--gold); font-weight: 900; }
.delta-up { color: #8de0c6; }
.delta-down { color: #ffad9f; }
.markets {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 18px;
margin-top: 18px;
}
.market-card {
min-width: 0;
overflow: hidden;
border: 1px solid rgba(24, 52, 61, 0.11);
border-radius: 18px;
background: var(--card);
box-shadow: var(--shadow);
}
.market-card.b {
border-color: rgba(232, 184, 74, 0.62);
}
.market-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
padding: 15px 17px 13px;
border-bottom: 1px solid var(--line);
}
.market-name {
display: flex;
align-items: center;
gap: 11px;
}
.market-letter {
display: grid;
place-items: center;
width: 38px;
height: 38px;
border-radius: 11px;
color: white;
background: var(--teal);
font-size: 0.95rem;
font-weight: 900;
box-shadow: 0 6px 15px rgba(36, 155, 145, 0.22);
}
.market-card.b .market-letter {
color: #3d3212;
background: var(--gold);
box-shadow: 0 6px 15px rgba(232, 184, 74, 0.23);
}
.market-header h2 {
margin: 0;
font-size: 1.02rem;
letter-spacing: -0.02em;
}
.market-header p {
margin: 1px 0 0;
color: var(--muted);
font-size: 0.72rem;
}
.difference-chip {
max-width: 48%;
padding: 5px 9px;
border-radius: 999px;
color: #6a5317;
background: #fff0bd;
font-size: 0.68rem;
font-weight: 800;
text-align: right;
}
.kpis {
display: grid;
grid-template-columns: repeat(4, 1fr);
border-bottom: 1px solid var(--line);
}
.kpi {
min-width: 0;
padding: 11px 12px;
border-right: 1px solid var(--line);
}
.kpi:last-child { border-right: 0; }
.kpi-label {
display: block;
overflow: hidden;
margin-bottom: 3px;
color: var(--muted);
font-size: 0.61rem;
font-weight: 800;
letter-spacing: 0.07em;
text-overflow: ellipsis;
text-transform: uppercase;
white-space: nowrap;
}
.kpi-value {
display: block;
overflow: hidden;
font-size: clamp(0.93rem, 1.6vw, 1.24rem);
font-weight: 900;
letter-spacing: -0.04em;
text-overflow: ellipsis;
white-space: nowrap;
}
.stage {
position: relative;
height: 330px;
overflow: hidden;
background: #eee9de;
}
.stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
.stage canvas:active { cursor: grabbing; }
.stage-tip {
position: absolute;
right: 11px;
bottom: 9px;
padding: 5px 8px;
border: 1px solid rgba(24, 52, 61, 0.11);
border-radius: 999px;
color: #617276;
background: rgba(255, 253, 248, 0.82);
backdrop-filter: blur(6px);
font-size: 0.62rem;
font-weight: 750;
pointer-events: none;
}
.section-label {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 12px 16px 4px;
}
.section-label h3 {
margin: 0;
font-size: 0.82rem;
letter-spacing: -0.01em;
}
.chart-legend {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 10px;
color: var(--muted);
font-size: 0.62rem;
font-weight: 700;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 5px;
}
.legend-line {
width: 16px;
height: 3px;
border-radius: 4px;
background: var(--coral);
}
.legend-line.supply { background: var(--teal); }
.chart-wrap {
padding: 0 10px;
}
.chart-wrap svg {
display: block;
width: 100%;
height: auto;
max-height: 245px;
}
.table-wrap {
overflow-x: auto;
border-top: 1px solid var(--line);
}
table {
width: 100%;
border-collapse: collapse;
font-size: 0.71rem;
white-space: nowrap;
}
th {
padding: 8px 10px;
color: var(--muted);
background: #f7f3eb;
font-size: 0.6rem;
font-weight: 850;
letter-spacing: 0.06em;
text-align: right;
text-transform: uppercase;
}
th:first-child, td:first-child { text-align: left; }
td {
padding: 7px 10px;
border-top: 1px solid #e9e4d9;
text-align: right;
}
tbody tr {
cursor: pointer;
transition: background 120ms ease;
}
tbody tr:hover, tbody tr.selected { background: #fff6d9; }
.buyer-name {
display: inline-flex;
align-items: center;
gap: 7px;
font-weight: 850;
}
.buyer-dot {
width: 8px;
height: 8px;
border-radius: 50%;
box-shadow: 0 0 0 2px rgba(24, 52, 61, 0.07);
}
.allocation-pill {
display: inline-block;
min-width: 31px;
padding: 2px 6px;
border-radius: 999px;
color: #fff;
background: var(--teal);
font-weight: 850;
text-align: center;
}
.notes {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 12px;
margin-top: 18px;
}
.note {
padding: 15px 16px;
border: 1px solid rgba(24, 52, 61, 0.10);
border-radius: 13px;
background: rgba(255, 253, 248, 0.68);
}
.note h3 {
margin: 0 0 5px;
font-size: 0.79rem;
}
.note p {
margin: 0;
color: var(--muted);
font-size: 0.75rem;
}
.disclaimer {
margin: 17px 2px 0;
color: #6b7779;
font-size: 0.72rem;
text-align: center;
}
.disclaimer strong { color: var(--ink); }
@media (max-width: 1000px) {
.markets { grid-template-columns: 1fr; }
.stage { height: 390px; }
.assumption-grid { grid-template-columns: 1fr; }
.notes { grid-template-columns: 1fr; }
}
@media (max-width: 600px) {
.page { width: min(100% - 18px, 1540px); padding-top: 17px; }
.masthead { gap: 12px; }
.linked-badge { display: none; }
.scenario-panel { padding: 14px; }
.scenario-heading { align-items: flex-start; }
.held-note { display: none; }
.stage { height: 310px; }
.kpis { grid-template-columns: repeat(2, 1fr); }
.kpi:nth-child(2) { border-right: 0; }
.kpi:nth-child(-n+2) { border-bottom: 1px solid var(--line); }
.market-header { align-items: flex-start; }
.difference-chip { max-width: 44%; }
.section-label { align-items: flex-start; flex-direction: column; }
.chart-legend { justify-content: flex-start; }
}
</style>
</head>
<body>
<main class="page">
<header class="masthead">
<div>
<div class="eyebrow">
<span class="model-badge">Illustrative model</span>
<span>Supply × demand sandbox</span>
</div>
<h1>Two markets.<br>One assumption changed.</h1>
<p class="intro">
Market A is the reference. Market B uses the same six buyers and the same
fixed-cost seller, but changes exactly one assumption at a time.
</p>
</div>
<div class="linked-badge" aria-label="Markets are linked">
Linked<br>roster
<span>A ↔ B</span>
</div>
</header>
<section class="scenario-panel" aria-labelledby="scenario-title">
<div class="scenario-heading">
<div>
<h2 id="scenario-title">Choose the one assumption to test</h2>
<p>Switching tabs pins every other assumption back to Market A.</p>
</div>
<div class="held-note">All other assumptions held fixed</div>
</div>
<div class="assumption-grid">
<article class="assumption-card active" data-assumption="confidence">
<button class="assumption-title" type="button" data-activate="confidence">
<strong>Buyer confidence</strong>
<span class="state-chip">Applied</span>
</button>
<div class="assumption-values">
<span class="market-tag">A</span>
<div class="control-row">
<span class="control-value">1.00×</span>
</div>
</div>
<div class="assumption-values">
<span class="market-tag">B</span>
<div class="control-row">
<input id="confidence-control" type="range" min="0.75" max="1.6" step="0.05" value="1.25" />
<span class="control-value" id="confidence-value">1.25×</span>
</div>
</div>
</article>
<article class="assumption-card" data-assumption="supply">
<button class="assumption-title" type="button" data-activate="supply">
<strong>Supply capacity</strong>
<span class="state-chip">Held fixed</span>
</button>
<div class="assumption-values">
<span class="market-tag">A</span>
<div class="control-row">
<span class="control-value">48 units</span>
</div>
</div>
<div class="assumption-values">
<span class="market-tag">B</span>
<div class="control-row">
<input id="supply-control" type="range" min="24" max="72" step="6" value="48" disabled />
<span class="control-value" id="supply-value">48 units</span>
</div>
</div>
</article>
<article class="assumption-card" data-assumption="rationing">
<button class="assumption-title" type="button" data-activate="rationing">
<strong>Rationing rule</strong>
<span class="state-chip">Held fixed</span>
</button>
<div class="assumption-values">
<span class="market-tag">A</span>
<div class="control-row">
<span class="control-value">Value first</span>
</div>
</div>
<div class="assumption-values">
<span class="market-tag">B</span>
<div class="control-row">
<select id="rationing-control" disabled>
<option value="value">Value first</option>
<option value="equal">Equal shares</option>
<option value="proportional">Pro rata</option>
</select>
</div>
</div>
</article>
</div>
<div class="comparison-strip" id="comparison-strip" aria-live="polite"></div>
</section>
<section class="markets" aria-label="Linked market comparison">
<article class="market-card" id="market-a">
<header class="market-header">
<div class="market-name">
<div class="market-letter">A</div>
<div>
<h2>Reference market</h2>
<p>Baseline preferences, capacity and rationing</p>
</div>
</div>
<div class="difference-chip">Shared buyer roster</div>
</header>
<div class="kpis" id="kpis-a"></div>
<div class="stage" id="stage-a">
<div class="stage-tip">Drag to orbit · Scroll to zoom · Tap a buyer</div>
</div>
<div class="section-label">
<h3>Supply & demand schedule</h3>
<div class="chart-legend">
<span class="legend-item"><span class="legend-line"></span>Demand, segmented by buyer</span>
<span class="legend-item"><span class="legend-line supply"></span>Supply capacity</span>
</div>
</div>
<div class="chart-wrap" id="chart-a"></div>
<div class="table-wrap">
<table aria-label="Market A buyer preferences and allocations">
<thead>
<tr>
<th>Buyer</th>
<th>Max units</th>
<th>Base WTP</th>
<th>Effective WTP</th>
<th>Gets</th>
</tr>
</thead>
<tbody id="buyers-a"></tbody>
</table>
</div>
</article>
<article class="market-card b" id="market-b">
<header class="market-header">
<div class="market-name">
<div class="market-letter">B</div>
<div>
<h2>Changed-assumption market</h2>
<p id="market-b-subtitle">Only buyer confidence differs</p>
</div>
</div>
<div class="difference-chip" id="difference-chip">1 assumption changed</div>
</header>
<div class="kpis" id="kpis-b"></div>
<div class="stage" id="stage-b">
<div class="stage-tip">Drag to orbit · Scroll to zoom · Tap a buyer</div>
</div>
<div class="section-label">
<h3>Supply & demand schedule</h3>
<div class="chart-legend">
<span class="legend-item"><span class="legend-line"></span>Demand, segmented by buyer</span>
<span class="legend-item"><span class="legend-line supply"></span>Supply capacity</span>
</div>
</div>
<div class="chart-wrap" id="chart-b"></div>
<div class="table-wrap">
<table aria-label="Market B buyer preferences and allocations">
<thead>
<tr>
<th>Buyer</th>
<th>Max units</th>
<th>Base WTP</th>
<th>Effective WTP</th>
<th>Gets</th>
</tr>
</thead>
<tbody id="buyers-b"></tbody>
</table>
</div>
</article>
</section>
<section class="notes" aria-label="Model notes">
<article class="note">
<h3>Demand schedule</h3>
<p>
Each horizontal step is one fictional buyer's demand for up to its stated
quantity. Effective willingness to pay is base WTP × buyer confidence.
</p>
</article>
<article class="note">
<h3>Supply schedule</h3>
<p>
One fixed-cost seller has a hard unit capacity, so aggregate supply is a
vertical schedule. Capacity changes the quantity, not seller quality.
</p>
</article>
<article class="note">
<h3>Price and rationing</h3>
<p>
Price is the marginal accepted WTP when capacity binds. Eligible buyers
then receive units under the selected rule. Buyer surplus equals WTP minus
price for units received.
</p>
</article>
</section>
<p class="disclaimer">
<strong>Illustrative only — not a forecast.</strong>
Buyers, prices, quantities and rules are fictional and omit many real-market
details such as production costs, budgets, quality, market power and learning.
</p>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const BUYERS = [
{ name: "Ava", short: "AV", units: 14, wtp: 104, color: "#ef6a5b" },
{ name: "Ben", short: "BN", units: 12, wtp: 91, color: "#e8b84a" },
{ name: "Cleo", short: "CL", units: 10, wtp: 80, color: "#249b91" },
{ name: "Dev", short: "DV", units: 9, wtp: 68, color: "#527eb4" },
{ name: "Eli", short: "EL", units: 8, wtp: 54, color: "#8b6bb4" },
{ name: "Fina", short: "FN", units: 7, wtp: 41, color: "#d66d9d" }
];
const BASELINE = Object.freeze({
confidence: 1,
supply: 48,
rationing: "value"
});
const drafts = {
confidence: 1.25,
supply: 36,
rationing: "equal"
};
let activeAssumption = "confidence";
let selectedBuyer = 0;
let updateQueued = false;
const labels = {
confidence: "buyer confidence",
supply: "supply capacity",
rationing: "rationing rule"
};
const ruleLabels = {
value: "Value first",
equal: "Equal shares",
proportional: "Pro rata"
};
function currentBConfig() {
return {
confidence: BASELINE.confidence,
supply: BASELINE.supply,
rationing: BASELINE.rationing,
[activeAssumption]: drafts[activeAssumption]
};
}
function money(value) {
return new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
maximumFractionDigits: 0
}).format(Math.round(value));
}
function signedMoney(value) {
if (value === 0) return "no change";
return `${value > 0 ? "+" : "−"}${money(Math.abs(value))}`;
}
function compactNumber(value) {
if (Math.abs(value) < 1000) return String(Math.round(value));
return `${(value / 1000).toFixed(1)}k`;
}
function computeMarket(config) {
const effective = BUYERS.map(b => b.wtp * config.confidence);
const totalDemand = BUYERS.reduce((sum, b) => sum + b.units, 0);
const traded = Math.min(config.supply, totalDemand);
const expandedValues = [];
BUYERS.forEach((buyer, index) => {
for (let n = 0; n < buyer.units; n++) expandedValues.push(effective[index]);
});
expandedValues.sort((a, b) => b - a);
let price;
if (config.supply > totalDemand) {
price = 0;
} else if (config.supply === totalDemand) {
price = Math.min(...effective);
} else {
price = expandedValues[config.supply - 1];
}
const eligible = BUYERS
.map((buyer, index) => ({ index, value: effective[index], units: buyer.units }))
.filter(buyer => buyer.value >= price - 0.0001);
const allocation = new Array(BUYERS.length).fill(0);
if (traded === totalDemand) {
BUYERS.forEach((buyer, index) => allocation[index] = buyer.units);
} else if (config.rationing === "value") {
let remaining = traded;
const ranked = [...eligible].sort((a, b) => b.value - a.value);
for (const buyer of ranked) {
const amount = Math.min(remaining, buyer.units);
allocation[buyer.index] = amount;
remaining -= amount;
if (remaining <= 0) break;
}
} else if (config.rationing === "equal") {
const base = Math.floor(traded / Math.max(1, eligible.length));
for (const buyer of eligible) {
allocation[buyer.index] = Math.min(base, buyer.units);
}
let remaining = traded - allocation.reduce((sum, n) => sum + n, 0);
while (remaining > 0) {
const candidates = eligible
.filter(buyer => allocation[buyer.index] < buyer.units)
.sort((a, b) =>
allocation[a.index] - allocation[b.index] ||
b.value - a.value
);
if (!candidates.length) break;
allocation[candidates[0].index] += 1;
remaining -= 1;
}
} else {
const eligibleDemand = eligible.reduce((sum, buyer) => sum + buyer.units, 0);
const remainders = [];
eligible.forEach(buyer => {
const exact = traded * buyer.units / Math.max(1, eligibleDemand);
const whole = Math.floor(exact);
allocation[buyer.index] = whole;
remainders.push({ index: buyer.index, fraction: exact - whole, value: buyer.value });
});
remainders.sort((a, b) => b.fraction - a.fraction || b.value - a.value);
let remaining = traded - allocation.reduce((sum, n) => sum + n, 0);
for (let i = 0; i < remainders.length && remaining > 0; i++, remaining--) {
allocation[remainders[i].index] += 1;
}
}
const buyerSurplus = BUYERS.reduce((sum, buyer, index) => {
return sum + allocation[index] * Math.max(0, effective[index] - price);
}, 0);
const revenue = price * traded;
return {
config,
effective,
totalDemand,
traded,
unusedSupply: Math.max(0, config.supply - traded),
unmetDemand: Math.max(0, totalDemand - traded),
price,
allocation,
buyerSurplus,
revenue
};
}
function renderKPIs(container, result) {
const values = [
["Uniform price", money(result.price)],
["Units traded", `${result.traded}`],
["Buyer surplus", money(result.buyerSurplus)],
["Unmet demand", `${result.unmetDemand}u`]
];
container.innerHTML = values.map(([label, value]) => `
<div class="kpi">
<span class="kpi-label">${label}</span>
<span class="kpi-value">${value}</span>
</div>
`).join("");
}
function renderBuyerTable(tbody, result) {
tbody.innerHTML = BUYERS.map((buyer, index) => `
<tr data-buyer="${index}" class="${selectedBuyer === index ? "selected" : ""}">
<td>
<span class="buyer-name">
<span class="buyer-dot" style="background:${buyer.color}"></span>
${buyer.name}
</span>
</td>
<td>${buyer.units}</td>
<td>${money(buyer.wtp)}</td>
<td>${money(result.effective[index])}</td>
<td><span class="allocation-pill">${result.allocation[index]}u</span></td>
</tr>
`).join("");
}
function renderChart(container, result) {
const W = 520;
const H = 230;
const M = { left: 52, right: 23, top: 18, bottom: 34 };
const plotW = W - M.left - M.right;
const plotH = H - M.top - M.bottom;
const maxValue = Math.max(...result.effective);
const yMax = Math.max(120, Math.ceil(maxValue / 20) * 20);
const xMax = Math.max(result.totalDemand, result.config.supply, 72);
const x = value => M.left + value / xMax * plotW;
const y = value => M.top + plotH - value / yMax * plotH;
const ranked = BUYERS
.map((buyer, index) => ({ ...buyer, index, effective: result.effective[index] }))
.sort((a, b) => b.effective - a.effective);
let quantity = 0;
let segments = "";
ranked.forEach((buyer, rank) => {
const start = quantity;
const end = quantity + buyer.units;
quantity = end;
segments += `
<line x1="${x(start)}" y1="${y(buyer.effective)}"
x2="${x(end)}" y2="${y(buyer.effective)}"
stroke="${buyer.color}" stroke-width="4" stroke-linecap="round" />
`;
if (rank < ranked.length - 1) {
const next = ranked[rank + 1];
segments += `
<line x1="${x(end)}" y1="${y(buyer.effective)}"
x2="${x(end)}" y2="${y(next.effective)}"
stroke="${buyer.color}" stroke-width="3.2" />
`;
} else {
segments += `
<line x1="${x(end)}" y1="${y(buyer.effective)}"
x2="${x(end)}" y2="${y(0)}"
stroke="${buyer.color}" stroke-width="3.2" />
`;
}
});
let buyerDots = "";
quantity = 0;
ranked.forEach((buyer, rank) => {
const end = quantity + buyer.units;
const selected = buyer.index === selectedBuyer;
buyerDots += `
<circle cx="${x(end)}" cy="${y(buyer.effective)}" r="${selected ? 6.5 : 4.2}"
fill="#fffdf8" stroke="${buyer.color}" stroke-width="${selected ? 3 : 2}">
<title>${buyer.name}: ${money(buyer.effective)} effective WTP</title>
</circle>
`;
quantity = end;
});
const yTicks = [0, yMax / 2, yMax];
const axisTicks = yTicks.map(value => `
<line x1="${M.left}" y1="${y(value)}" x2="${W - M.right}" y2="${y(value)}"
stroke="#d8d5cc" stroke-width="1" />
<text x="${M.left - 8}" y="${y(value) + 4}" text-anchor="end"
fill="#738184" font-size="10">${value === 0 ? "$0" : "$" + value}</text>
`).join("");
const quantityTicks = [...new Set([0, result.traded, result.config.supply])]
.filter(value => value <= xMax)
.map(value => `
<text x="${x(value)}" y="${H - 10}" text-anchor="middle"
fill="#738184" font-size="10">${value}</text>
`).join("");
const markerX = result.config.supply > result.totalDemand
? x(result.traded)
: x(result.config.supply);
container.innerHTML = `
<svg viewBox="0 0 ${W} ${H}" role="img"
aria-label="Supply and demand schedule. Price ${money(result.price)}, ${result.traded} units traded.">
<rect x="${M.left}" y="${M.top}" width="${plotW}" height="${plotH}"
rx="8" fill="#faf7ef" />
${axisTicks}
<line x1="${M.left}" y1="${y(result.price)}" x2="${W - M.right}" y2="${y(result.price)}"
stroke="#e1a735" stroke-width="1.5" stroke-dasharray="6 5" />
<text x="${W - M.right - 3}" y="${y(result.price) - 5}" text-anchor="end"
fill="#9b741e" font-size="10" font-weight="700">
PRICE ${money(result.price)}
</text>
<line x1="${x(result.config.supply)}" y1="${M.top}"
x2="${x(result.config.supply)}" y2="${y(0)}"
stroke="#249b91" stroke-width="6" stroke-linecap="round" />
<text x="${x(result.config.supply)}" y="${M.top + 11}" text-anchor="middle"
fill="#187d75" font-size="10" font-weight="800">
SUPPLY ${result.config.supply}
</text>
${segments}
${buyerDots}
<circle cx="${markerX}" cy="${y(result.price)}" r="7"
fill="#fffdf8" stroke="#18343d" stroke-width="3">
<title>Market outcome: ${money(result.price)}, ${result.traded} units</title>
</circle>
<line x1="${M.left}" y1="${y(0)}" x2="${W - M.right}" y2="${y(0)}"
stroke="#849194" stroke-width="1.2" />
<line x1="${M.left}" y1="${M.top}" x2="${M.left}" y2="${y(0)}"
stroke="#849194" stroke-width="1.2" />
${quantityTicks}
<text x="${M.left + plotW / 2}" y="${H - 0.5}" text-anchor="middle"
fill="#52666b" font-size="10" font-weight="700">QUANTITY · UNITS</text>
<text x="13" y="${M.top + plotH / 2}" text-anchor="middle"
fill="#52666b" font-size="10" font-weight="700"
transform="rotate(-90 13 ${M.top + plotH / 2})">PRICE</text>
</svg>
`;
}
function updateComparison(a, b) {
const priceDelta = b.price - a.price;
const unitsDelta = b.traded - a.traded;
const surplusDelta = b.buyerSurplus - a.buyerSurplus;
const unmetDelta = b.unmetDemand - a.unmetDemand;
const chipClass = value => value > 0 ? "delta-up" : value < 0 ? "delta-down" : "";
document.getElementById("comparison-strip").innerHTML = `
<span><strong>A</strong> ${money(a.price)} · ${a.traded}u · surplus ${money(a.buyerSurplus)}</span>
<span class="arrow">→</span>
<span><strong>B</strong> ${money(b.price)} · ${b.traded}u · surplus ${money(b.buyerSurplus)}</span>
<span>Price <strong class="${chipClass(priceDelta)}">${signedMoney(priceDelta)}</strong></span>
<span>Units <strong class="${chipClass(unitsDelta)}">${unitsDelta > 0 ? "+" : ""}${unitsDelta}</strong></span>
<span>Surplus <strong class="${chipClass(surplusDelta)}">${signedMoney(surplusDelta)}</strong></span>
<span>Unmet <strong class="${chipClass(-unmetDelta)}">${unmetDelta > 0 ? "+" : ""}${unmetDelta}u</strong></span>
`;
const description = activeAssumption === "confidence"
? `Buyer confidence ${drafts.confidence.toFixed(2)}× in B; base WTP stays visible`
: activeAssumption === "supply"
? `Supply capacity ${drafts.supply} units in B; buyer preferences stay fixed`
: `${ruleLabels[drafts.rationing]} rationing in B; price and capacity stay fixed`;
document.getElementById("market-b-subtitle").textContent = description;
document.getElementById("difference-chip").textContent =
`Only ${labels[activeAssumption]} differs`;
}
function updateControlState() {
document.querySelectorAll(".assumption-card").forEach(card => {
const key = card.dataset.assumption;
const active = key === activeAssumption;
card.classList.toggle("active", active);
card.querySelector(".state-chip").textContent = active ? "Applied" : "Held fixed";
});
const confidence = document.getElementById("confidence-control");
const supply = document.getElementById("supply-control");
const rationing = document.getElementById("rationing-control");
confidence.disabled = activeAssumption !== "confidence";
supply.disabled = activeAssumption !== "supply";
rationing.disabled = activeAssumption !== "rationing";
confidence.value = activeAssumption === "confidence" ? drafts.confidence : BASELINE.confidence;
supply.value = activeAssumption === "supply" ? drafts.supply : BASELINE.supply;
rationing.value = activeAssumption === "rationing" ? drafts.rationing : BASELINE.rationing;
document.getElementById("confidence-value").textContent =
`${Number(confidence.value).toFixed(2)}×`;
document.getElementById("supply-value").textContent =
`${supply.value} units`;
}
function setActiveAssumption(key) {
if (!labels[key]) return;
activeAssumption = key;
updateControlState();
scheduleUpdate();
}
function scheduleUpdate() {
if (updateQueued) return;
updateQueued = true;
requestAnimationFrame(() => {
updateQueued = false;
renderAll();
});
}
function renderAll() {
const a = computeMarket(BASELINE);
const b = computeMarket(currentBConfig());
renderKPIs(document.getElementById("kpis-a"), a);
renderKPIs(document.getElementById("kpis-b"), b);
renderBuyerTable(document.getElementById("buyers-a"), a);
renderBuyerTable(document.getElementById("buyers-b"), b);
renderChart(document.getElementById("chart-a"), a);
renderChart(document.getElementById("chart-b"), b);
updateComparison(a, b);
if (viewports.a) updateViewport(viewports.a, a);
if (viewports.b) updateViewport(viewports.b, b);
}
// ---------- Low-poly Three.js scene ----------
const viewports = {};
function roundedRectPath(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 textSprite(text, options = {}) {
const {
background = "#18343d",
foreground = "#ffffff",
width = 1.7,
height = 0.43,
fontSize = 42
} = options;
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRectPath(ctx, 7, 9, 498, 110, 24);
ctx.fillStyle = background;
ctx.fill();
ctx.fillStyle = foreground;
ctx.font = `800 ${fontSize}px system-ui, sans-serif`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 66, 474);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
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 material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
flatShading: true,
...options
});
}
function disposeObject(root) {
root.traverse(object => {
if (object.geometry) object.geometry.dispose();
if (object.material) {
const materials = Array.isArray(object.material) ? object.material : [object.material];
materials.forEach(mat => {
if (mat.map) mat.map.dispose();
mat.dispose();
});
}
});
root.clear();
}
function markBuyer(object, index) {
object.traverse(child => {
child.userData.buyerIndex = index;
});
}
function makeBuyer(buyer, index, result) {
const group = new THREE.Group();
const buyerColor = new THREE.Color(buyer.color);
const body = new THREE.Mesh(
new THREE.ConeGeometry(0.25, 0.68, 6),
material(buyerColor)
);
body.position.y = 0.59;
body.castShadow = true;
group.add(body);
const head = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.19, 0),
material("#d8a67f")
);
head.position.y = 1.10;
head.castShadow = true;
group.add(head);
const legGeometry = new THREE.BoxGeometry(0.12, 0.30, 0.14);
const legMaterial = material("#314d56");
[-0.10, 0.10].forEach(x => {
const leg = new THREE.Mesh(legGeometry, legMaterial);
leg.position.set(x, 0.16, 0);
leg.castShadow = true;
group.add(leg);
});
const armGeometry = new THREE.BoxGeometry(0.10, 0.36, 0.10);
[-0.29, 0.29].forEach(x => {
const arm = new THREE.Mesh(armGeometry, buyerColor);
arm.position.set(x, 0.65, 0);
arm.rotation.z = x < 0 ? -0.22 : 0.22;
arm.castShadow = true;
group.add(arm);
});
const barHeight = Math.max(0.65, result.effective[index] / 38);
const bar = new THREE.Mesh(
new THREE.BoxGeometry(0.10, barHeight, 0.10),
material(buyer.color, {
transparent: true,
opacity: 0.53,
depthWrite: false
})
);
bar.position.set(0.42, 0.20 + barHeight / 2, 0.12);
group.add(bar);
const cap = new THREE.Mesh(
new THREE.OctahedronGeometry(0.13, 0),
material(buyer.color)
);
cap.position.set(0.42, 0.20 + barHeight, 0.12);
group.add(cap);
const label = textSprite(
`${buyer.short} ${money(result.effective[index])} · ${result.allocation[index]}u`,
{
background: buyer.color,
foreground: "#ffffff",
width: 1.78,
height: 0.40,
fontSize: 34
}
);
label.position.set(0, 1.48 + barHeight * 0.18, 0);
group.add(label);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.35, 0.035, 4, 14),
material("#f2c84b", { emissive: "#8b6811", emissiveIntensity: 0.25 })
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.035;
ring.visible = index === selectedBuyer;
ring.userData.buyerIndex = index;
group.add(ring);
const allocation = result.allocation[index];
if (allocation > 0) {
const visibleUnits = Math.min(6, Math.ceil(allocation / 2));
const cubeGeometry = new THREE.BoxGeometry(0.13, 0.13, 0.13);
const cubeMaterial = material("#e9b846");
for (let n = 0; n < visibleUnits; n++) {
const cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.position.set(-0.31 + (n % 3) * 0.16, 0.08 + Math.floor(n / 3) * 0.15, 0.25);
cube.castShadow = true;
group.add(cube);
}
}
markBuyer(group, index);
return group;
}
function makeSupply(result) {
const group = new THREE.Group();
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(1.15, 1.28, 0.18, 8),
material("#d7e4dd")
);
platform.position.y = 0.10;
platform.receiveShadow = true;
group.add(platform);
const visibleCrates = Math.ceil(result.config.supply / 3);
const crateGeometry = new THREE.BoxGeometry(0.43, 0.38, 0.43);
for (let n = 0; n < visibleCrates; n++) {
const column = n % 4;
const row = Math.floor(n / 4);
const crate = new THREE.Mesh(
crateGeometry,
material(n % 3 === 0 ? "#249b91" : n % 3 === 1 ? "#55b3a8" : "#d7e4dd")
);
crate.position.set(
-0.64 + column * 0.43,
0.38,
-0.84 + row * 0.42
);
crate.castShadow = true;
crate.receiveShadow = true;
group.add(crate);
}
const label = textSprite(`SELLER · ${result.config.supply} UNIT CAP`, {
background: "#249b91",
foreground: "#ffffff",
width: 2.45,
height: 0.44,
fontSize: 34
});
label.position.set(0, 3.0, 0);
group.add(label);
const sublabel = textSprite("FIXED-COST SUPPLY", {
background: "#e9f3ed",
foreground: "#187d75",
width: 1.8,
height: 0.34,
fontSize: 31
});
sublabel.position.set(0, 2.57, 0);
group.add(sublabel);
return group;
}
function makePriceSign(result) {
const group = new THREE.Group();
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.07, 1.4, 6),
material("#50666c")
);
post.position.y = 0.70;
post.castShadow = true;
group.add(post);
const label = textSprite(`${money(result.price)} PRICE`, {
background: "#e8b84a",
foreground: "#3e3210",
width: 1.72,
height: 0.46,
fontSize: 38
});
label.position.set(0, 1.45, 0);
group.add(label);
return group;
}
function updateViewport(viewport, result) {
disposeObject(viewport.dynamic);
const supply = makeSupply(result);
supply.position.set(-2.65, 0.04, 0.15);
viewport.dynamic.add(supply);
const priceSign = makePriceSign(result);
priceSign.position.set(-0.20, 0, 1.72);
viewport.dynamic.add(priceSign);
const positions = [
[1.35, -2.45],
[2.42, -1.55],
[1.48, -0.65],
[2.52, 0.25],
[1.52, 1.15],
[2.55, 2.05]
];
BUYERS.forEach((buyer, index) => {
const person = makeBuyer(buyer, index, result);
person.position.set(positions[index][0], 0.03, positions[index][1]);
person.rotation.y = -0.55 + index * 0.10;
viewport.dynamic.add(person);
});
const buyersLabel = textSprite("TRANSPARENT BUYER PREFERENCES", {
background: "#e9f3ed",
foreground: "#18343d",
width: 2.65,
height: 0.38,
fontSize: 32
});
buyersLabel.position.set(2.0, 4.22, 0.2);
viewport.dynamic.add(buyersLabel);
}
function createViewport(id) {
const stage = document.getElementById(`stage-${id}`);
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.domElement.setAttribute("aria-label", `Interactive low-poly market ${id.toUpperCase()}`);
stage.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color("#eee9de");
const camera = new THREE.OrthographicCamera(-7, 7, 5, -5, 0.1, 100);
const target = new THREE.Vector3(0, 0.75, 0);
const controls = {
theta: 0.74,
phi: 0.93,
radius: 13,
zoom: 1
};
function updateCamera() {
const sinPhi = Math.sin(controls.phi);
camera.position.set(
target.x + controls.radius * sinPhi * Math.sin(controls.theta),
target.y + controls.radius * Math.cos(controls.phi),
target.z + controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(target);
camera.zoom = controls.zoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = Math.max(1, stage.clientWidth);
const height = Math.max(1, stage.clientHeight);
renderer.setSize(width, height, false);
const aspect = width / height;
const halfHeight = 4.65;
camera.left = -halfHeight * aspect;
camera.right = halfHeight * aspect;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
resize();
updateCamera();
scene.add(new THREE.HemisphereLight("#fff8e9", "#8ea5a2", 2.25));
const sun = new THREE.DirectionalLight("#fff4db", 3.1);
sun.position.set(5, 10, 6);
sun.castShadow = true;
sun.shadow.mapSize.set(1024, 1024);
sun.shadow.camera.left = -8;
sun.shadow.camera.right = 8;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -8;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 25;
sun.shadow.bias = -0.0007;
scene.add(sun);
const fill = new THREE.DirectionalLight("#9fd9dc", 1.0);
fill.position.set(-7, 4, -5);
scene.add(fill);
const ground = new THREE.Mesh(
new THREE.CylinderGeometry(5.65, 5.85, 0.28, 20),
material("#e5dfd2")
);
ground.position.y = -0.18;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(10, 20, "#aeb9b3", "#cbd0c8");
grid.position.y = -0.025;
grid.material.transparent = true;
grid.material.opacity = 0.18;
scene.add(grid);
const dynamic = new THREE.Group();
scene.add(dynamic);
let dragging = false;
let pointerStart = { x: 0, y: 0 };
let previous = { x: 0, y: 0 };
let moved = false;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
moved = false;
pointerStart = { x: event.clientX, y: event.clientY };
previous = { x: event.clientX, y: event.clientY };
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
if (Math.hypot(event.clientX - pointerStart.x, event.clientY - pointerStart.y) > 4) {
moved = true;
}
controls.theta -= dx * 0.007;
controls.phi = THREE.MathUtils.clamp(controls.phi + dy * 0.005, 0.48, 1.25);
previous = { x: event.clientX, y: event.clientY };
updateCamera();
});
renderer.domElement.addEventListener("pointerup", event => {
dragging = false;
if (moved) return;
const rect = renderer.domElement.getBoundingClientRect();
const pointer = new THREE.Vector2(
((event.clientX - rect.left) / rect.width) * 2 - 1,
-((event.clientY - rect.top) / rect.height) * 2 + 1
);
const raycaster = new THREE.Raycaster();
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(dynamic.children, true);
const hit = hits.find(item => {
let object = item.object;
while (object) {
if (object.userData.buyerIndex !== undefined) return true;
object = object.parent;
}
return false;
});
if (hit) {
let object = hit.object;
while (object && object.userData.buyerIndex === undefined) object = object.parent;
if (object) {
selectedBuyer = object.userData.buyerIndex;
scheduleUpdate();
}
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
});
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
controls.zoom = THREE.MathUtils.clamp(
controls.zoom * Math.exp(-event.deltaY * 0.001),
0.72,
1.75
);
updateCamera();
}, { passive: false });
return {
id,
renderer,
scene,
camera,
dynamic,
controls,
updateCamera
};
}
// ---------- UI events ----------
document.querySelectorAll("[data-activate]").forEach(button => {
button.addEventListener("click", () => {
setActiveAssumption(button.dataset.activate);
});
});
document.getElementById("confidence-control").addEventListener("input", event => {
drafts.confidence = Number(event.target.value);
updateControlState();
scheduleUpdate();
});
document.getElementById("supply-control").addEventListener("input", event => {
drafts.supply = Number(event.target.value);
updateControlState();
scheduleUpdate();
});
document.getElementById("rationing-control").addEventListener("change", event => {
drafts.rationing = event.target.value;
updateControlState();
scheduleUpdate();
});
document.querySelectorAll(".table-wrap").forEach(wrapper => {
wrapper.addEventListener("click", event => {
const row = event.target.closest("tr[data-buyer]");
if (!row) return;
selectedBuyer = Number(row.dataset.buyer);
scheduleUpdate();
});
});
// ---------- Start ----------
updateControlState();
viewports.a = createViewport("a");
viewports.b = createViewport("b");
renderAll();
function animate() {
requestAnimationFrame(animate);
viewports.a.renderer.render(viewports.a.scene, viewports.a.camera);
viewports.b.renderer.render(viewports.b.scene, viewports.b.camera);
}
animate();
</script>
</body>
</html>
```