Prompt specification
Build or customize the Three.js 3D scene "Households With Different Consumption Baskets" 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: Households With Different Consumption Baskets
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/households-with-different-consumption-baskets-x3179
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing price changes in fixed consumption baskets. Build three household counters using different fixed baskets and the same price changes. Show why their cost changes differ without claiming one basket represents everyone. Voxel style.
Working Three.js Starter (households-with-different-consumption-baskets-x3179):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="light">
<title>One Price Shock, Three Fixed Baskets</title>
<style>
:root {
--paper: #f4f0e6;
--paper-deep: #e9e3d6;
--ink: #18332f;
--muted: #66736d;
--line: #d8d1c3;
--coral: #df6848;
--teal: #287767;
--gold: #d9a441;
--shadow: 0 12px 30px rgba(24, 51, 47, 0.09);
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--paper);
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
#app {
display: grid;
grid-template-rows: auto minmax(0, 1fr) auto;
height: 100dvh;
min-height: 620px;
overflow: hidden;
}
header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
padding: 19px 28px 18px;
border-bottom: 1px solid var(--line);
background: rgba(244, 240, 230, 0.96);
z-index: 5;
}
.title-group {
display: flex;
align-items: center;
gap: 16px;
min-width: 0;
}
.voxel-mark {
position: relative;
width: 48px;
height: 44px;
flex: 0 0 auto;
}
.voxel-mark span {
position: absolute;
display: block;
width: 20px;
height: 20px;
background: var(--ink);
box-shadow: inset -5px -5px 0 rgba(255, 255, 255, 0.12);
}
.voxel-mark span:nth-child(1) {
left: 1px;
top: 17px;
}
.voxel-mark span:nth-child(2) {
left: 22px;
top: 17px;
background: var(--coral);
}
.voxel-mark span:nth-child(3) {
left: 12px;
top: 1px;
background: var(--gold);
}
.eyebrow {
margin: 0 0 3px;
color: var(--coral);
font-size: 0.71rem;
font-weight: 850;
letter-spacing: 0.15em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.25rem, 2.1vw, 2rem);
line-height: 1.08;
letter-spacing: -0.035em;
}
.subtitle {
margin: 5px 0 0;
color: var(--muted);
font-size: 0.86rem;
}
.header-note {
max-width: 310px;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 12px;
background: #fffaf0;
color: #53625c;
font-size: 0.76rem;
line-height: 1.35;
text-align: right;
}
.header-note strong {
color: var(--ink);
}
main {
display: grid;
grid-template-columns: minmax(0, 1fr) 390px;
min-height: 0;
}
#stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background: #e9e5da;
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
outline: none;
}
#scene:active {
cursor: grabbing;
}
.stage-toolbar {
position: absolute;
top: 14px;
left: 15px;
right: 15px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
pointer-events: none;
z-index: 2;
}
.scene-instruction {
padding: 8px 10px;
border: 1px solid rgba(24, 51, 47, 0.13);
border-radius: 9px;
background: rgba(255, 250, 240, 0.82);
color: #5b6862;
font-size: 0.7rem;
backdrop-filter: blur(8px);
}
.reset-view {
pointer-events: auto;
border: 1px solid rgba(24, 51, 47, 0.17);
border-radius: 9px;
padding: 8px 11px;
background: rgba(255, 250, 240, 0.9);
font-size: 0.72rem;
font-weight: 750;
cursor: pointer;
box-shadow: 0 4px 12px rgba(24, 51, 47, 0.08);
}
.reset-view:hover {
background: #fff;
}
.stage-legend {
position: absolute;
left: 15px;
bottom: 15px;
display: flex;
flex-wrap: wrap;
gap: 13px;
padding: 8px 11px;
border: 1px solid rgba(24, 51, 47, 0.13);
border-radius: 9px;
background: rgba(255, 250, 240, 0.86);
color: #5b6862;
font-size: 0.69rem;
backdrop-filter: blur(8px);
pointer-events: none;
z-index: 2;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 6px;
}
.legend-swatch {
width: 12px;
height: 12px;
border-radius: 3px;
background: #b9b5aa;
}
.legend-swatch.selected {
background: var(--coral);
}
aside {
min-height: 0;
overflow-y: auto;
padding: 20px 20px 28px;
border-left: 1px solid var(--line);
background: #f8f4eb;
scrollbar-width: thin;
}
.section-label {
margin: 0 0 9px;
color: var(--coral);
font-size: 0.68rem;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.market-card,
.household-card,
.method-card,
.takeaway {
border: 1px solid var(--line);
border-radius: 15px;
background: #fffdf7;
box-shadow: 0 4px 14px rgba(24, 51, 47, 0.035);
}
.market-card {
padding: 15px;
}
.market-card h2,
.method-card h2 {
margin: 0;
font-size: 1rem;
letter-spacing: -0.02em;
}
.market-card > p {
margin: 6px 0 12px;
color: var(--muted);
font-size: 0.77rem;
line-height: 1.45;
}
.market-chips {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.market-chip {
padding: 8px 6px;
border-radius: 9px;
font-size: 0.65rem;
font-weight: 750;
line-height: 1.2;
text-align: center;
}
.market-chip b {
display: block;
margin-top: 3px;
font-size: 0.78rem;
}
.market-chip.up {
background: #fae5dc;
color: #a5412b;
}
.market-chip.down {
background: #dceee8;
color: #17624f;
}
#householdCards {
display: grid;
gap: 12px;
margin-top: 14px;
}
.household-card {
overflow: hidden;
border-top: 4px solid var(--accent);
}
.household-card .card-body {
padding: 14px 14px 13px;
}
.household-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 10px;
}
.household-id {
display: flex;
align-items: center;
gap: 9px;
}
.letter {
display: grid;
width: 29px;
height: 29px;
place-items: center;
flex: 0 0 auto;
border-radius: 8px;
background: var(--accent);
color: white;
font-size: 0.82rem;
font-weight: 900;
box-shadow: inset -4px -4px 0 rgba(0, 0, 0, 0.12);
}
.household-card h3 {
margin: 0;
font-size: 0.91rem;
letter-spacing: -0.015em;
}
.basket-summary {
margin: 3px 0 0 38px;
color: var(--muted);
font-size: 0.67rem;
}
.fixed-tag {
padding: 5px 7px;
border-radius: 6px;
background: #eeeadf;
color: #65716c;
font-size: 0.59rem;
font-weight: 800;
letter-spacing: 0.07em;
white-space: nowrap;
text-transform: uppercase;
}
.cost-row {
display: flex;
align-items: baseline;
gap: 8px;
margin-top: 13px;
}
.cost-old {
color: #8a918d;
font-size: 0.79rem;
font-weight: 650;
}
.cost-arrow {
color: #a3aaa5;
font-size: 0.75rem;
}
.cost-current {
font-size: 1.5rem;
font-weight: 850;
letter-spacing: -0.045em;
}
.delta-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-top: 2px;
color: var(--coral);
font-size: 0.78rem;
font-weight: 800;
}
.delta-percent {
padding: 3px 7px;
border-radius: 999px;
background: #fae5dc;
}
.cost-track {
height: 6px;
margin-top: 11px;
overflow: hidden;
border-radius: 999px;
background: #e7e2d7;
}
.cost-fill {
display: block;
width: 100%;
height: 100%;
border-radius: inherit;
background: var(--accent);
transform-origin: left;
transition: width 80ms linear;
}
.driver {
margin: 10px 0 0;
padding: 8px 9px;
border-radius: 8px;
background: #f2eee4;
color: #5d6964;
font-size: 0.68rem;
line-height: 1.35;
}
.driver strong {
color: var(--ink);
}
details {
border-top: 1px solid var(--line);
}
summary {
padding: 10px 14px;
color: #4f5f59;
font-size: 0.7rem;
font-weight: 800;
cursor: pointer;
list-style: none;
}
summary::-webkit-details-marker {
display: none;
}
summary::after {
content: "+";
float: right;
font-size: 0.9rem;
}
details[open] summary::after {
content: "−";
}
.breakdown {
padding: 0 14px 13px;
}
.breakdown-head,
.breakdown-row {
display: grid;
grid-template-columns: 1.25fr 0.78fr 0.67fr;
gap: 7px;
align-items: center;
}
.breakdown-head {
padding-bottom: 6px;
color: #8a918d;
font-size: 0.59rem;
font-weight: 800;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.breakdown-row {
min-height: 27px;
border-top: 1px solid #eee9df;
font-size: 0.65rem;
}
.item-name {
display: flex;
align-items: center;
gap: 6px;
font-weight: 700;
}
.item-dot {
width: 8px;
height: 8px;
flex: 0 0 auto;
border-radius: 2px;
background: var(--item-color);
}
.quantity {
color: #63706a;
}
.impact {
text-align: right;
font-weight: 800;
}
.impact.up {
color: #b64b31;
}
.impact.down {
color: #24705c;
}
.impact.zero {
color: #929995;
}
.takeaway {
margin-top: 14px;
padding: 13px 14px;
background: var(--ink);
color: white;
border-color: var(--ink);
}
.takeaway span {
display: block;
margin-bottom: 4px;
color: #a9c2bb;
font-size: 0.61rem;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.takeaway strong {
display: block;
font-size: 0.83rem;
line-height: 1.4;
}
.method-card {
margin-top: 14px;
padding: 14px;
}
.method-card p,
.method-card li {
color: #5e6b65;
font-size: 0.72rem;
line-height: 1.48;
}
.method-card ol {
margin: 10px 0 12px;
padding-left: 18px;
}
.method-card li + li {
margin-top: 4px;
}
.caveat {
margin: 0 !important;
padding-top: 10px;
border-top: 1px solid var(--line);
color: #53625c !important;
}
.caveat strong {
color: var(--ink);
}
footer {
display: grid;
grid-template-columns: auto minmax(240px, 650px) auto;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 13px 26px 14px;
border-top: 1px solid var(--line);
background: #f8f4eb;
z-index: 5;
}
.control-group {
display: flex;
align-items: center;
gap: 8px;
}
.control-button {
min-height: 37px;
padding: 8px 13px;
border: 1px solid #cfc8ba;
border-radius: 9px;
background: #fffdf7;
font-size: 0.73rem;
font-weight: 800;
cursor: pointer;
}
.control-button:hover {
border-color: #9ba8a2;
background: white;
}
.control-button.primary {
border-color: var(--ink);
background: var(--ink);
color: white;
}
.slider-wrap {
display: grid;
grid-template-columns: 1fr auto;
align-items: center;
gap: 13px;
}
.slider-label {
grid-column: 1 / -1;
display: flex;
justify-content: space-between;
margin-bottom: -4px;
color: #69756f;
font-size: 0.65rem;
font-weight: 750;
}
#progress {
width: 100%;
height: 7px;
margin: 0;
border-radius: 999px;
outline: none;
appearance: none;
background:
linear-gradient(
to right,
var(--coral) 0%,
var(--coral) var(--fill, 100%),
#ddd7ca var(--fill, 100%),
#ddd7ca 100%
);
cursor: pointer;
}
#progress::-webkit-slider-thumb {
width: 19px;
height: 19px;
border: 3px solid #fffdf7;
border-radius: 50%;
appearance: none;
background: var(--ink);
box-shadow: 0 2px 8px rgba(24, 51, 47, 0.24);
}
#progress::-moz-range-thumb {
width: 14px;
height: 14px;
border: 3px solid #fffdf7;
border-radius: 50%;
background: var(--ink);
box-shadow: 0 2px 8px rgba(24, 51, 47, 0.24);
}
#progressValue {
min-width: 42px;
color: var(--ink);
font-size: 0.77rem;
font-weight: 850;
text-align: right;
}
noscript {
position: fixed;
inset: 20px;
display: grid;
place-items: center;
background: var(--paper);
z-index: 20;
}
@media (max-width: 1050px) {
#app {
height: auto;
min-height: 100dvh;
overflow: visible;
}
main {
display: block;
}
#stage {
height: 64dvh;
min-height: 470px;
}
aside {
overflow: visible;
border-top: 1px solid var(--line);
border-left: 0;
}
#householdCards {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
footer {
position: sticky;
bottom: 0;
}
}
@media (max-width: 760px) {
header {
align-items: flex-start;
padding: 15px 16px;
}
.header-note {
display: none;
}
.subtitle {
font-size: 0.75rem;
}
#stage {
height: 58dvh;
min-height: 410px;
}
aside {
padding: 16px;
}
#householdCards {
grid-template-columns: 1fr;
}
.market-chips {
grid-template-columns: repeat(3, 1fr);
}
footer {
grid-template-columns: 1fr;
gap: 8px;
padding: 9px 14px 11px;
}
.footer-buttons {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}
.control-button {
padding-inline: 8px;
}
}
</style>
</head>
<body>
<div id="app">
<header>
<div class="title-group">
<div class="voxel-mark" aria-hidden="true">
<span></span><span></span><span></span>
</div>
<div>
<p class="eyebrow">Fixed-basket price lab</p>
<h1>One price shock. Three different bills.</h1>
<p class="subtitle">Every counter gets the same product-price changes, but keeps a different basket fixed.</p>
</div>
</div>
<div class="header-note">
<strong>Hypothetical fixed baskets.</strong><br>
None is presented as a representative household.
</div>
</header>
<main>
<section id="stage" aria-label="Interactive 3D comparison of three household counters">
<canvas
id="scene"
tabindex="0"
aria-label="Three voxel-style household counters, each holding a different fixed basket. Drag to orbit and scroll to zoom."
></canvas>
<div class="stage-toolbar">
<div class="scene-instruction">Drag to orbit · scroll or pinch to zoom</div>
<button class="reset-view" id="resetView" type="button">Reset view</button>
</div>
<div class="stage-legend" aria-hidden="true">
<span class="legend-item">
<span class="legend-swatch"></span>
Baseline basket cost
</span>
<span class="legend-item">
<span class="legend-swatch selected"></span>
Cost at selected point
</span>
<span>Bar height = absolute basket cost</span>
</div>
</section>
<aside>
<section class="market-card">
<p class="section-label">The shared shock</p>
<h2>Same product prices for all three counters</h2>
<p>
These are the price changes applied to every basket. Quantities remain fixed;
shoppers are not assumed to substitute products.
</p>
<div class="market-chips" id="marketChips"></div>
</section>
<div id="householdCards"></div>
<div class="takeaway">
<span>At the full price shock</span>
<strong id="takeawayText"></strong>
</div>
<section class="method-card">
<p class="section-label">How to read this</p>
<h2>Different weights, different cost changes</h2>
<ol>
<li>Start with a fixed list of quantities for each counter.</li>
<li>Apply the same percentage change to each product price.</li>
<li>Add each quantity’s new cost to get the basket total.</li>
</ol>
<p class="caveat">
<strong>This is not a claim about which household is typical.</strong>
Each basket is a hypothetical example used to show why expenditure changes
can differ. The example is not an overall cost-of-living index, and it does
not describe every household’s needs, income, or purchasing behavior.
</p>
</section>
</aside>
</main>
<footer>
<div class="control-group footer-buttons">
<button class="control-button" id="baseline" type="button">Baseline</button>
<button class="control-button primary" id="replay" type="button">Replay shock</button>
<button class="control-button" id="full" type="button">Full changes</button>
</div>
<div class="slider-wrap">
<div class="slider-label">
<span>How far through the price changes?</span>
<span>Quantities never change</span>
</div>
<input
id="progress"
type="range"
min="0"
max="100"
step="1"
value="100"
aria-label="Progress through the shared price changes"
>
<output id="progressValue" for="progress">100%</output>
</div>
<div class="control-group">
<span class="fixed-tag">One shared market shock</span>
</div>
</footer>
</div>
<noscript>This visualization requires JavaScript.</noscript>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Shared price data and hypothetical fixed baskets
// -------------------------------------------------------------------------
const goods = [
{ id: "bread", name: "Bread", short: "BREAD", base: 4.00, change: 0.08, color: "#d9a441" },
{ id: "milk", name: "Milk", short: "MILK", base: 3.50, change: -0.04, color: "#70a8d4" },
{ id: "eggs", name: "Eggs", short: "EGGS", base: 4.00, change: 0.12, color: "#e7d3a3" },
{ id: "coffee", name: "Coffee", short: "COFFEE", base: 12.00, change: 0.20, color: "#8b5a3c" },
{ id: "apples", name: "Apples", short: "APPLES", base: 2.50, change: -0.06, color: "#d85d46" },
{ id: "power", name: "Electricity",short: "POWER", base: 0.18, change: 0.15, color: "#d8b53e" }
];
const households = [
{
id: "A",
name: "Pantry-first",
note: "More bread, eggs and power; no coffee.",
accent: "#df6848",
accentHex: 0xdf6848,
quantities: { bread: 6, milk: 2, eggs: 3, coffee: 0, apples: 2, power: 140 }
},
{
id: "B",
name: "Coffee-forward",
note: "A small basket with a large coffee allocation.",
accent: "#287767",
accentHex: 0x287767,
quantities: { bread: 1, milk: 2, eggs: 1, coffee: 4, apples: 2, power: 80 }
},
{
id: "C",
name: "Fruit & power-heavy",
note: "Many apples and a larger electricity quantity.",
accent: "#6978b8",
accentHex: 0x6978b8,
quantities: { bread: 3, milk: 3, eggs: 2, coffee: 1, apples: 12, power: 150 }
}
];
for (const household of households) {
household.baseTotal = goods.reduce(
(sum, good) => sum + good.base * household.quantities[good.id],
0
);
household.delta = goods.reduce(
(sum, good) =>
sum + good.base * good.change * household.quantities[good.id],
0
);
}
const money = value => `$${value.toFixed(2)}`;
const signedMoney = value =>
`${value >= 0 ? "+" : "−"}$${Math.abs(value).toFixed(2)}`;
const signedPercent = value =>
`${value >= 0 ? "+" : "−"}${Math.abs(value).toFixed(1)}%`;
// -------------------------------------------------------------------------
// Build the explanatory HTML
// -------------------------------------------------------------------------
const marketChips = document.querySelector("#marketChips");
const householdCards = document.querySelector("#householdCards");
marketChips.innerHTML = goods.map(good => `
<div class="market-chip ${good.change >= 0 ? "up" : "down"}">
${good.short}
<b>${signedPercent(good.change * 100)}</b>
</div>
`).join("");
householdCards.innerHTML = households.map((household, index) => {
const fullCurrent = household.baseTotal + household.delta;
const fullPercent = (household.delta / household.baseTotal) * 100;
const drivers = goods
.map(good => ({
good,
impact: good.base * good.change * household.quantities[good.id]
}))
.filter(item => item.impact > 0)
.sort((a, b) => b.impact - a.impact);
const biggestDriver = drivers[0];
const summary = Object.entries(household.quantities)
.map(([id, quantity]) => {
const good = goods.find(item => item.id === id);
return `${quantity} ${good.short.toLowerCase()}`;
})
.join(" · ");
return `
<article class="household-card" style="--accent:${household.accent}">
<div class="card-body">
<div class="household-head">
<div class="household-id">
<div class="letter">${household.id}</div>
<div>
<h3>${household.name}</h3>
<p class="basket-summary">${summary}</p>
</div>
</div>
<span class="fixed-tag">Fixed qty</span>
</div>
<div class="cost-row" aria-live="polite">
<span class="cost-old" data-old>${money(household.baseTotal)}</span>
<span class="cost-arrow">→</span>
<strong class="cost-current" data-current>${money(fullCurrent)}</strong>
</div>
<div class="delta-row">
<span data-delta>${signedMoney(household.delta)}</span>
<span class="delta-percent" data-percent>${signedPercent(fullPercent)}</span>
</div>
<div class="cost-track" aria-hidden="true">
<span class="cost-fill" data-fill></span>
</div>
<p class="driver">
<strong>Biggest upward driver:</strong>
${biggestDriver.good.name} ${signedMoney(biggestDriver.impact)}
at the full shock.
</p>
</div>
<details ${index === 0 ? "open" : ""}>
<summary>See the item-by-item contribution</summary>
<div class="breakdown">
<div class="breakdown-head">
<span>Item × fixed quantity</span>
<span>Unit price</span>
<span style="text-align:right">Impact</span>
</div>
${goods.map(good => {
const quantity = household.quantities[good.id];
const impact = good.base * good.change * quantity;
const impactClass = impact > 0 ? "up" : impact < 0 ? "down" : "zero";
return `
<div class="breakdown-row">
<span class="item-name">
<span class="item-dot" style="--item-color:${good.color}"></span>
${good.name}
</span>
<span class="quantity">${quantity} × ${money(good.base)}</span>
<span class="impact ${impactClass}" data-impact="${good.id}">
${impact === 0 ? "$0.00" : signedMoney(impact)}
</span>
</div>
`;
}).join("")}
</div>
</details>
</article>
`;
}).join("");
// -------------------------------------------------------------------------
// Three.js renderer, scene, lights, and camera
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const stage = document.querySelector("#stage");
const renderer = new THREE.WebGLRenderer({
canvas,
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.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e5da);
scene.fog = new THREE.Fog(0xe9e5da, 24, 52);
const camera = new THREE.PerspectiveCamera(37, 1, 0.1, 100);
const target = new THREE.Vector3(0, 2.0, -0.25);
const hemisphere = new THREE.HemisphereLight(0xfff8e9, 0x75817a, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff3db, 3.25);
keyLight.position.set(-8, 14, 10);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -13;
keyLight.shadow.camera.right = 13;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 40;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xb8d8d1, 0.85);
fillLight.position.set(9, 7, -8);
scene.add(fillLight);
const materialCache = new Map();
function material(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.02
})
);
}
return materialCache.get(color);
}
function box(width, height, depth, color, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material(color)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
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 createLabel(
text,
{
background = "#18332f",
foreground = "#ffffff",
border = null,
canvasWidth = 640,
canvasHeight = 160,
depthTest = false,
fontSize = 56
} = {}
) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = canvasWidth;
labelCanvas.height = canvasHeight;
const ctx = labelCanvas.getContext("2d");
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const spriteMaterial = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest,
depthWrite: false
});
const sprite = new THREE.Sprite(spriteMaterial);
sprite.renderOrder = 20;
function draw(value) {
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
if (background && background !== "transparent") {
roundedRect(
ctx,
5,
5,
canvasWidth - 10,
canvasHeight - 10,
25
);
ctx.fillStyle = background;
ctx.fill();
if (border) {
ctx.lineWidth = 5;
ctx.strokeStyle = border;
ctx.stroke();
}
}
ctx.fillStyle = foreground;
ctx.font = `800 ${fontSize}px system-ui, sans-serif`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(
value,
canvasWidth / 2,
canvasHeight / 2 + 2,
canvasWidth - 36
);
texture.needsUpdate = true;
}
draw(text);
sprite.userData.setText = draw;
return sprite;
}
// Ground and voxel grid
box(38, 0.16, 26, 0xd8d2c5, 0, -0.17, -1.5);
for (let x = -18; x <= 18; x += 2) {
box(0.025, 0.012, 22, 0xc9c3b7, x, -0.082, -1.5);
}
for (let z = -12; z <= 10; z += 2) {
box(34, 0.012, 0.025, 0xc9c3b7, 0, -0.081, z);
}
// Shared market-price board
box(13.6, 1.18, 0.18, 0x18332f, 0, 4.62, -2.8);
const boardTitle = createLabel("SAME LOCAL PRICE CHANGES", {
background: "transparent",
foreground: "#f6f0e4",
canvasWidth: 900,
canvasHeight: 120,
fontSize: 48
});
boardTitle.position.set(0, 5.48, -2.56);
boardTitle.scale.set(4.6, 0.61, 1);
scene.add(boardTitle);
goods.forEach((good, index) => {
const rising = good.change >= 0;
const chip = createLabel(
`${good.short} ${signedPercent(good.change * 100)}`,
{
background: rising ? "#f5d3c3" : "#d6ebe3",
foreground: rising ? "#9b3e29" : "#17614f",
canvasWidth: 520,
canvasHeight: 150,
fontSize: 52
}
);
chip.position.set(-5.4 + index * 2.16, 4.57, -2.58);
chip.scale.set(1.94, 0.56, 1);
scene.add(chip);
});
// -------------------------------------------------------------------------
// Voxel item builders
// -------------------------------------------------------------------------
function createItem(good, x, y, z, quantity) {
const group = new THREE.Group();
group.position.set(x, y, z);
scene.add(group);
if (good.id === "bread") {
box(0.49, 0.25, 0.29, 0xd8a34d, 0, 0.125, 0, group);
for (let i = -1; i <= 1; i++) {
box(0.035, 0.018, 0.22, 0xf2d58e, i * 0.12, 0.258, 0, group);
}
}
if (good.id === "milk") {
box(0.34, 0.48, 0.31, 0xf5f1e7, 0, 0.24, 0, group);
box(0.35, 0.12, 0.315, 0x5f9fc8, 0, 0.26, 0, group);
box(0.22, 0.08, 0.18, 0xe5dfd2, 0, 0.52, 0, group);
}
if (good.id === "eggs") {
box(0.53, 0.15, 0.37, 0xba8e63, 0, 0.075, 0, group);
box(0.14, 0.18, 0.14, 0xeadfc8, -0.14, 0.23, 0, group);
box(0.14, 0.18, 0.14, 0xf1e6d1, 0, 0.23, 0, group);
box(0.14, 0.18, 0.14, 0xeadfc8, 0.14, 0.23, 0, group);
}
if (good.id === "coffee") {
box(0.42, 0.47, 0.3, 0x8b5a3c, 0, 0.235, 0, group);
box(0.43, 0.14, 0.31, 0x4b3024, 0, 0.27, 0, group);
box(0.25, 0.08, 0.22, 0xc49b71, 0, 0.51, 0, group);
}
if (good.id === "apples") {
box(0.27, 0.27, 0.27, 0xd6533f, -0.12, 0.135, 0, group);
box(0.24, 0.25, 0.24, 0xe36a4d, 0.15, 0.125, 0.02, group);
box(0.06, 0.07, 0.06, 0x52733b, -0.12, 0.3, 0, group);
box(0.05, 0.06, 0.05, 0x52733b, 0.15, 0.29, 0.02, group);
}
if (good.id === "power") {
box(0.45, 0.47, 0.3, 0xd8b53e, 0, 0.235, 0, group);
box(0.13, 0.08, 0.17, 0x263b37, 0, 0.51, 0, group);
const upper = box(0.065, 0.23, 0.025, 0x263b37, 0.025, 0.26, 0.16, group);
upper.rotation.z = -0.55;
const lower = box(0.065, 0.21, 0.025, 0xf6e7ad, -0.025, 0.2, 0.164, group);
lower.rotation.z = -0.55;
}
const quantityLabel = createLabel(`x${quantity}`, {
background: "#fff9ed",
foreground: "#18332f",
border: "#d6cebd",
canvasWidth: 220,
canvasHeight: 110,
fontSize: 58
});
quantityLabel.position.set(0, 0.78, 0.11);
quantityLabel.scale.set(0.55, 0.275, 1);
group.add(quantityLabel);
}
// -------------------------------------------------------------------------
// Household counters and cost bars
// -------------------------------------------------------------------------
const visuals = new Map();
function addHouseholdCounter(household, index) {
const x = (index - 1) * 5.35;
const group = new THREE.Group();
group.position.x = x;
scene.add(group);
// Platform
box(4.75, 0.16, 3.9, 0xcfc8ba, 0, -0.01, -0.28, group);
box(4.42, 0.08, 3.58, 0xe4ded1, 0, 0.09, -0.28, group);
// Counter body
box(3.55, 0.18, 1.62, 0x6e756f, 0, 0.18, 0, group);
box(3.72, 1.13, 1.7, 0xe8dfcf, 0, 0.78, 0, group);
box(3.94, 0.21, 2.0, 0xf7f0e3, 0, 1.42, 0, group);
// Voxel front panels
for (let column = 0; column < 6; column++) {
for (let row = 0; row < 2; row++) {
const color = (column + row) % 2 === 0
? household.accentHex
: 0xf7f0e3;
box(
0.47,
0.33,
0.055,
color,
-1.38 + column * 0.55,
0.49 + row * 0.46,
0.875,
group
);
}
}
// Letter plaque
box(0.78, 0.48, 0.09, household.accentHex, 0, 0.86, 0.91, group);
const letter = createLabel(household.id, {
background: household.accent,
foreground: "#ffffff",
canvasWidth: 180,
canvasHeight: 140,
fontSize: 86
});
letter.position.set(0, 0.88, 0.98);
letter.scale.set(0.49, 0.38, 1);
group.add(letter);
// Fixed basket tray
box(3.55, 0.10, 1.42, 0xd7ccb8, 0, 1.585, 0.08, group);
box(3.25, 0.05, 1.18, 0xf1e6d4, 0, 1.66, 0.08, group);
const fixedLabel = createLabel("FIXED QUANTITIES", {
background: "#fff9ed",
foreground: "#18332f",
border: "#d3cabb",
canvasWidth: 560,
canvasHeight: 120,
fontSize: 45
});
fixedLabel.position.set(0, 1.67, 1.09);
fixedLabel.scale.set(1.45, 0.31, 1);
group.add(fixedLabel);
// Only items in this fixed basket appear on the tray.
const activeGoods = goods.filter(good => household.quantities[good.id] > 0);
const spacing = activeGoods.length > 5 ? 0.59 : 0.68;
const startX = -((activeGoods.length - 1) * spacing) / 2;
activeGoods.forEach((good, itemIndex) => {
createItem(
good,
startX + itemIndex * spacing,
1.69,
0.02,
household.quantities[good.id]
);
});
// Cost bars behind the counter
const oldHeight = household.baseTotal / 50;
const oldBar = box(0.68, oldHeight, 0.54, 0xb7b2a6, -0.77, oldHeight / 2, -1.54, group);
oldBar.scale.y = 1;
const currentBar = box(
0.68,
oldHeight,
0.54,
household.accentHex,
0.77,
oldHeight / 2,
-1.54,
group
);
const oldLabel = createLabel(`${money(household.baseTotal)} BASE`, {
background: "#f3eee3",
foreground: "#59665f",
border: "#cfc8ba",
canvasWidth: 560,
canvasHeight: 130,
fontSize: 48
});
oldLabel.position.set(-0.77, oldHeight + 0.34, -1.5);
oldLabel.scale.set(1.62, 0.38, 1);
group.add(oldLabel);
const currentLabel = createLabel(money(household.baseTotal + household.delta), {
background: household.accent,
foreground: "#ffffff",
canvasWidth: 480,
canvasHeight: 140,
fontSize: 60
});
currentLabel.position.set(0.77, oldHeight + 0.34, -1.5);
currentLabel.scale.set(1.4, 0.41, 1);
group.add(currentLabel);
const title = createLabel(`${household.id} · ${household.name.toUpperCase()}`, {
background: "#18332f",
foreground: "#ffffff",
canvasWidth: 850,
canvasHeight: 145,
fontSize: 49
});
title.position.set(0, 3.23, -1.48);
title.scale.set(3.25, 0.55, 1);
group.add(title);
visuals.set(household.id, {
currentBar,
currentLabel,
oldHeight
});
}
households.forEach(addHouseholdCounter);
// -------------------------------------------------------------------------
// Update all UI and 3D values from the shared scenario progress
// -------------------------------------------------------------------------
const progressInput = document.querySelector("#progress");
const progressValue = document.querySelector("#progressValue");
const takeawayText = document.querySelector("#takeawayText");
let progress = 1;
let replayState = null;
function setProgress(value) {
progress = THREE.MathUtils.clamp(value, 0, 1);
const percentage = Math.round(progress * 100);
progressInput.value = String(percentage);
progressInput.style.setProperty("--fill", `${percentage}%`);
progressValue.value = `${percentage}%`;
progressValue.textContent = `${percentage}%`;
for (const household of households) {
const currentCost = household.baseTotal + progress * household.delta;
const currentPercent = ((currentCost / household.baseTotal) - 1) * 100;
const card = householdCards.querySelectorAll(".household-card")[
households.indexOf(household)
];
card.querySelector("[data-old]").textContent = money(household.baseTotal);
card.querySelector("[data-current]").textContent = money(currentCost);
card.querySelector("[data-delta]").textContent = signedMoney(currentCost - household.baseTotal);
card.querySelector("[data-percent]").textContent = signedPercent(currentPercent);
card.querySelector("[data-fill]").style.width =
`${Math.min(100, (currentCost / household.baseTotal) * 100)}%`;
for (const good of goods) {
const impact =
good.base * good.change * household.quantities[good.id] * progress;
const impactElement = card.querySelector(`[data-impact="${good.id}"]`);
impactElement.textContent = impact === 0 ? "$0.00" : signedMoney(impact);
impactElement.classList.toggle("up", impact > 0);
impactElement.classList.toggle("down", impact < 0);
impactElement.classList.toggle("zero", impact === 0);
}
const visual = visuals.get(household.id);
const currentHeight = currentCost / 50;
visual.currentBar.scale.y = currentHeight;
visual.currentBar.position.y = currentHeight / 2;
visual.currentLabel.position.y = currentHeight + 0.34;
visual.currentLabel.userData.setText(money(currentCost));
}
const percentages = households.map(household =>
((household.baseTotal + household.delta) / household.baseTotal - 1) * 100
);
const highestIndex = percentages.indexOf(Math.max(...percentages));
const lowestIndex = percentages.indexOf(Math.min(...percentages));
takeawayText.textContent =
`Basket ${households[highestIndex].id} rises ${percentages[highestIndex].toFixed(1)}%, ` +
`while Basket ${households[lowestIndex].id} rises ${percentages[lowestIndex].toFixed(1)}%, ` +
`because their quantities put different weights on the same price changes.`;
}
progressInput.addEventListener("input", event => {
replayState = null;
setProgress(Number(event.target.value) / 100);
});
document.querySelector("#baseline").addEventListener("click", () => {
replayState = null;
setProgress(0);
});
document.querySelector("#full").addEventListener("click", () => {
replayState = null;
setProgress(1);
});
document.querySelector("#replay").addEventListener("click", () => {
setProgress(0);
replayState = {
start: performance.now(),
duration: 2400
};
});
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, and reset
// -------------------------------------------------------------------------
const defaultTheta = 0.02;
const defaultPhi = 1.10;
let desiredTheta = defaultTheta;
let desiredPhi = defaultPhi;
let currentTheta = defaultTheta;
let currentPhi = defaultPhi;
let desiredDistance = 18;
let currentDistance = 18;
let hasInteracted = false;
function fitDistance() {
const halfFov = THREE.MathUtils.degToRad(camera.fov / 2);
return Math.max(16.5, 7.5 / (Math.tan(halfFov) * camera.aspect));
}
function resetView() {
desiredTheta = defaultTheta;
desiredPhi = defaultPhi;
desiredDistance = fitDistance();
hasInteracted = false;
}
document.querySelector("#resetView").addEventListener("click", resetView);
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
canvas.addEventListener("pointerdown", event => {
if (!event.isPrimary) return;
dragging = true;
hasInteracted = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!dragging || !event.isPrimary) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
desiredTheta -= dx * 0.006;
desiredPhi = THREE.MathUtils.clamp(desiredPhi - dy * 0.005, 0.72, 1.38);
});
function endDrag(event) {
if (!dragging) return;
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
hasInteracted = true;
desiredDistance = THREE.MathUtils.clamp(
desiredDistance * Math.exp(event.deltaY * 0.001),
11,
43
);
},
{ passive: false }
);
canvas.addEventListener("keydown", event => {
if (!["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown", "+", "-"].includes(event.key)) {
return;
}
event.preventDefault();
hasInteracted = true;
if (event.key === "ArrowLeft") desiredTheta -= 0.08;
if (event.key === "ArrowRight") desiredTheta += 0.08;
if (event.key === "ArrowUp") desiredPhi = THREE.MathUtils.clamp(desiredPhi - 0.06, 0.72, 1.38);
if (event.key === "ArrowDown") desiredPhi = THREE.MathUtils.clamp(desiredPhi + 0.06, 0.72, 1.38);
if (event.key === "+") desiredDistance = Math.max(11, desiredDistance - 1);
if (event.key === "-") desiredDistance = Math.min(43, desiredDistance + 1);
});
function resize() {
const width = Math.max(1, stage.clientWidth);
const height = Math.max(1, stage.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
if (!hasInteracted) {
desiredDistance = fitDistance();
}
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
resize();
currentDistance = desiredDistance;
setProgress(1);
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
function animate(now) {
requestAnimationFrame(animate);
if (replayState) {
const elapsed = now - replayState.start;
const t = THREE.MathUtils.clamp(elapsed / replayState.duration, 0, 1);
const eased = t < 0.5
? 4 * t * t * t
: 1 - Math.pow(-2 * t + 2, 3) / 2;
setProgress(eased);
if (t >= 1) {
replayState = null;
setProgress(1);
}
}
currentTheta = THREE.MathUtils.lerp(currentTheta, desiredTheta, 0.11);
currentPhi = THREE.MathUtils.lerp(currentPhi, desiredPhi, 0.11);
currentDistance = THREE.MathUtils.lerp(currentDistance, desiredDistance, 0.1);
const sinPhi = Math.sin(currentPhi);
camera.position.set(
target.x + currentDistance * sinPhi * Math.sin(currentTheta),
target.y + currentDistance * Math.cos(currentPhi),
target.z + currentDistance * sinPhi * Math.cos(currentTheta)
);
camera.lookAt(target);
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```