Prompt specification
Build or customize the Three.js 3D scene "Index Weighting Explorer" 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: Index Weighting Explorer
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/index-weighting-explorer-x3180
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows price changes in fixed consumption baskets. Create a miniature market with adjustable basket weights and visible unit prices. Recalculate a clearly defined toy index and show how changing weights changes its interpretation. Voxel style.
Working Three.js Starter (index-weighting-explorer-x3180):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Block & Basket — Fixed Basket Market Lab</title>
<style>
:root {
--ink: #102c31;
--panel: #17373b;
--panel-2: #21454a;
--paper: #f8f2df;
--muted: #aac1bd;
--mint: #8fe0be;
--yellow: #ffd76b;
--coral: #ff8d78;
--blue: #82cde9;
--line: rgba(255, 255, 255, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
}
body {
overflow: hidden;
color: var(--paper);
background: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input,
select {
font: inherit;
}
button {
cursor: pointer;
}
button:focus-visible,
input:focus-visible,
select:focus-visible,
canvas:focus-visible {
outline: 3px solid var(--yellow);
outline-offset: 3px;
}
.app {
display: grid;
grid-template-columns: minmax(0, 1fr) 430px;
width: 100%;
height: 100%;
}
.stage {
position: relative;
min-width: 0;
overflow: hidden;
background: #c9dfd8;
}
#scene {
position: absolute;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.stage-header {
position: absolute;
top: 24px;
left: 24px;
z-index: 3;
display: flex;
align-items: flex-start;
gap: 13px;
max-width: 460px;
color: var(--ink);
pointer-events: none;
}
.logo {
display: grid;
grid-template-columns: repeat(2, 15px);
grid-template-rows: repeat(2, 15px);
gap: 3px;
flex: 0 0 auto;
margin-top: 3px;
transform: rotate(-4deg);
}
.logo span {
display: block;
background: var(--ink);
box-shadow: 3px 3px 0 rgba(16, 44, 49, 0.18);
}
.logo span:nth-child(2) {
background: var(--coral);
}
.logo span:nth-child(3) {
background: var(--yellow);
}
.logo span:nth-child(4) {
background: var(--mint);
}
.eyebrow {
margin: 0 0 6px;
color: #456b67;
font-size: 11px;
font-weight: 900;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.stage-header h1 {
margin: 0;
font-size: clamp(27px, 3.1vw, 44px);
line-height: 0.95;
letter-spacing: -0.055em;
}
.stage-header p {
margin: 9px 0 0;
color: #486763;
font-size: 12px;
font-weight: 650;
}
.stats {
position: absolute;
top: 24px;
right: 24px;
z-index: 4;
display: grid;
grid-template-columns: repeat(3, minmax(92px, 1fr));
gap: 7px;
padding: 7px;
border: 1px solid rgba(16, 44, 49, 0.12);
border-radius: 8px;
background: rgba(248, 242, 223, 0.92);
box-shadow: 6px 6px 0 rgba(16, 44, 49, 0.13);
backdrop-filter: blur(8px);
}
.stat {
min-width: 96px;
padding: 9px 11px;
border-radius: 4px;
background: rgba(255, 255, 255, 0.53);
}
.stat:first-child {
background: var(--ink);
color: var(--paper);
}
.stat-label {
display: block;
margin-bottom: 3px;
color: inherit;
opacity: 0.67;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.stat strong {
display: block;
font-size: 25px;
line-height: 1;
letter-spacing: -0.04em;
}
.stat small {
display: block;
margin-top: 5px;
color: inherit;
opacity: 0.62;
font-size: 9px;
font-weight: 750;
}
.positive {
color: #d74f42;
}
.negative {
color: #167c66;
}
.formula-card {
position: absolute;
right: 24px;
bottom: 22px;
left: 24px;
z-index: 3;
display: grid;
grid-template-columns: 1.2fr 0.85fr 1.25fr;
gap: 22px;
padding: 18px 20px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 8px;
color: var(--paper);
background: rgba(16, 44, 49, 0.93);
box-shadow: 7px 7px 0 rgba(16, 44, 49, 0.14);
backdrop-filter: blur(10px);
pointer-events: none;
}
.formula-card h2 {
margin: 0 0 8px;
font-size: 12px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.formula-main {
margin: 0 0 7px;
color: var(--yellow);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: clamp(14px, 1.45vw, 21px);
font-weight: 900;
line-height: 1.3;
}
.formula-card p {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.cost-row {
display: flex;
justify-content: space-between;
gap: 12px;
margin-bottom: 7px;
font-size: 11px;
}
.cost-row span {
color: var(--muted);
}
.cost-row strong {
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
color: var(--paper);
}
.story-title {
color: var(--mint);
font-size: 11px;
font-weight: 900;
}
.story + .story {
margin-top: 7px;
}
.scene-help {
position: absolute;
bottom: 190px;
left: 25px;
z-index: 3;
padding: 7px 10px;
border-radius: 4px;
color: #365a57;
background: rgba(248, 242, 223, 0.78);
font-size: 10px;
font-weight: 750;
pointer-events: none;
}
.panel {
position: relative;
z-index: 6;
height: 100%;
overflow-y: auto;
padding: 24px 22px 28px;
border-left: 1px solid rgba(255, 255, 255, 0.08);
background:
linear-gradient(rgba(255, 255, 255, 0.018) 1px, transparent 1px),
var(--panel);
background-size: 100% 28px;
scrollbar-color: #51716d var(--panel);
scrollbar-width: thin;
}
.panel-head {
padding-bottom: 20px;
border-bottom: 1px solid var(--line);
}
.panel .eyebrow {
color: var(--mint);
}
.panel h1 {
margin: 0;
font-size: 34px;
line-height: 1;
letter-spacing: -0.055em;
}
.panel-intro {
margin: 12px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.6;
}
.panel-intro strong {
color: var(--paper);
}
.section {
padding: 20px 0;
border-bottom: 1px solid var(--line);
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 12px;
}
.section-title h2 {
margin: 0;
color: var(--paper);
font-size: 11px;
font-weight: 900;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.section-title span {
color: #799995;
font-size: 10px;
}
.basket-buttons {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
}
.basket-button {
min-height: 48px;
padding: 8px 6px;
border: 1px solid var(--line);
border-radius: 4px;
color: var(--muted);
background: var(--panel-2);
font-size: 10px;
font-weight: 850;
line-height: 1.25;
transition: transform 120ms ease, background 120ms ease;
}
.basket-button:hover {
transform: translateY(-2px);
}
.basket-button.active {
border-color: var(--mint);
color: var(--ink);
background: var(--mint);
}
.method-box {
margin-top: 11px;
padding: 11px;
border-left: 3px solid var(--yellow);
border-radius: 3px;
background: rgba(0, 0, 0, 0.12);
}
.method-box code {
display: block;
margin-bottom: 6px;
color: var(--yellow);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 12px;
font-weight: 900;
line-height: 1.5;
}
.method-box p {
margin: 0;
color: var(--muted);
font-size: 10px;
line-height: 1.55;
}
.market-actions {
display: flex;
gap: 7px;
margin-bottom: 12px;
}
.action-button {
flex: 1;
min-height: 38px;
padding: 9px 10px;
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 4px;
color: var(--ink);
background: var(--yellow);
box-shadow: 3px 3px 0 rgba(0, 0, 0, 0.22);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.action-button:hover {
transform: translate(1px, 1px);
box-shadow: 2px 2px 0 rgba(0, 0, 0, 0.22);
}
.action-button.secondary {
color: var(--paper);
background: transparent;
box-shadow: none;
}
#item-list {
display: grid;
gap: 9px;
}
.item-card {
position: relative;
overflow: hidden;
padding: 12px;
border: 1px solid var(--line);
border-radius: 5px;
background: rgba(255, 255, 255, 0.045);
}
.item-card::before {
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 4px;
background: var(--accent);
content: "";
}
.item-head {
display: grid;
grid-template-columns: 37px 1fr auto;
align-items: center;
gap: 9px;
margin-bottom: 10px;
}
.item-code {
display: grid;
place-items: center;
width: 37px;
height: 37px;
border-radius: 3px;
color: var(--ink);
background: var(--accent);
box-shadow: 3px 3px 0 rgba(0, 0, 0, 0.17);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
font-weight: 950;
}
.item-name {
font-size: 13px;
font-weight: 900;
}
.item-meta {
margin-top: 2px;
color: #829e9a;
font-size: 9px;
font-weight: 650;
}
.item-meta .changed {
color: var(--yellow);
}
.share {
min-width: 51px;
text-align: right;
}
.share strong {
display: block;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 14px;
}
.share span {
display: block;
margin-top: 2px;
color: #829e9a;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.price-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 11px;
}
.fixed-qty {
color: var(--muted);
font-size: 9px;
font-weight: 750;
}
.fixed-qty strong {
margin-left: 5px;
padding: 3px 6px;
border-radius: 3px;
color: var(--ink);
background: var(--accent);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
}
.price-controls {
display: flex;
align-items: stretch;
gap: 4px;
}
.price-controls button {
width: 29px;
border: 1px solid var(--line);
border-radius: 3px;
color: var(--paper);
background: var(--panel-2);
font-size: 18px;
font-weight: 900;
line-height: 1;
}
.price-controls button:hover {
color: var(--ink);
background: var(--accent);
}
.price-input-wrap {
display: flex;
align-items: center;
width: 78px;
height: 31px;
padding-left: 7px;
border: 1px solid var(--line);
border-radius: 3px;
color: var(--muted);
background: rgba(0, 0, 0, 0.12);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 12px;
font-weight: 900;
}
.price-input-wrap input {
width: 100%;
min-width: 0;
padding: 0 3px 0 2px;
border: 0;
outline: 0;
color: var(--paper);
background: transparent;
font: inherit;
text-align: right;
appearance: textfield;
}
.price-input-wrap input::-webkit-inner-spin-button,
.price-input-wrap input::-webkit-outer-spin-button {
margin: 0;
appearance: none;
}
.weight-label {
display: flex;
justify-content: space-between;
margin-bottom: 5px;
color: var(--muted);
font-size: 9px;
font-weight: 750;
}
.weight-label output {
color: var(--paper);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-weight: 900;
}
input[type="range"] {
width: 100%;
height: 16px;
margin: 0;
accent-color: var(--accent);
cursor: ew-resize;
}
.influence-track {
height: 3px;
margin-top: 7px;
overflow: hidden;
border-radius: 4px;
background: rgba(255, 255, 255, 0.08);
}
.influence-track span {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
background: var(--accent);
transition: width 120ms ease;
}
.panel-footer {
padding-top: 18px;
color: #789a95;
font-size: 9px;
line-height: 1.6;
}
.panel-footer strong {
color: var(--mint);
}
#toast {
position: fixed;
right: 450px;
bottom: 28px;
z-index: 20;
max-width: 320px;
padding: 10px 14px;
border-radius: 4px;
color: var(--ink);
background: var(--yellow);
box-shadow: 4px 4px 0 rgba(0, 0, 0, 0.2);
font-size: 11px;
font-weight: 850;
opacity: 0;
transform: translateY(12px);
transition: opacity 160ms ease, transform 160ms ease;
pointer-events: none;
}
#toast.show {
opacity: 1;
transform: translateY(0);
}
@media (max-width: 1180px) {
.app {
grid-template-columns: minmax(0, 1fr) 390px;
}
.stage-header p {
display: none;
}
.stats {
grid-template-columns: repeat(3, 88px);
}
.stat {
min-width: 0;
padding: 8px;
}
.stat strong {
font-size: 21px;
}
.formula-card {
gap: 14px;
padding: 15px;
}
}
@media (max-width: 900px) {
body {
overflow: auto;
}
.app {
display: block;
height: auto;
}
.stage {
height: 72vh;
min-height: 560px;
}
.panel {
height: auto;
overflow: visible;
border-top: 1px solid var(--line);
border-left: 0;
}
#toast {
right: 18px;
bottom: 18px;
}
}
@media (max-width: 680px) {
.stage {
height: 650px;
}
.stage-header {
top: 14px;
left: 14px;
}
.stage-header h1 {
font-size: 28px;
}
.stats {
top: 88px;
right: auto;
left: 14px;
grid-template-columns: repeat(3, 1fr);
width: calc(100% - 28px);
}
.formula-card {
right: 14px;
bottom: 14px;
left: 14px;
grid-template-columns: 1fr 1fr;
}
.formula-story {
grid-column: 1 / -1;
}
.scene-help {
display: none;
}
.panel {
padding: 22px 15px;
}
}
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
scroll-behavior: auto !important;
transition-duration: 0.01ms !important;
animation-duration: 0.01ms !important;
}
}
</style>
</head>
<body>
<main class="app">
<section class="stage" aria-label="Interactive voxel market">
<div id="scene"></div>
<header class="stage-header">
<div class="logo" aria-hidden="true">
<span></span><span></span><span></span><span></span>
</div>
<div>
<p class="eyebrow">Pixel Pantry / Market Lab</p>
<h1>Block & Basket</h1>
<p>Same goods. Same quantities. A different way to read inflation.</p>
</div>
</header>
<div class="stats" aria-live="polite">
<div class="stat">
<span class="stat-label">Your lens</span>
<strong id="index-main">100.0</strong>
<small id="index-change">0.0% from base</small>
</div>
<div class="stat">
<span class="stat-label">Equal lens</span>
<strong id="index-equal">100.0</strong>
<small>Same fixed basket</small>
</div>
<div class="stat">
<span class="stat-label">Lens gap</span>
<strong id="index-gap">0.0</strong>
<small id="gap-label">points</small>
</div>
</div>
<div class="scene-help">
Drag to orbit · wheel to zoom · click a stall for +$0.10 · shift-click for −$0.10
</div>
<section class="formula-card" aria-label="Index explanation">
<div>
<h2>The toy index</h2>
<p class="formula-main">
I = 100 × Σ(qᵢwᵢpᵢ) / Σ(qᵢwᵢp⁰ᵢ)
</p>
<p>
q = fixed quantity · w = lens weight · p = current unit price ·
p⁰ = base unit price
</p>
</div>
<div class="formula-cost">
<h2>Weighted basket cost</h2>
<div class="cost-row">
<span>Current</span><strong id="weighted-current">$0.00</strong>
</div>
<div class="cost-row">
<span>Reference</span><strong id="weighted-base">$0.00</strong>
</div>
<div class="cost-row">
<span>Difference</span><strong id="weighted-difference">+$0.00</strong>
</div>
</div>
<div class="formula-story">
<h2>Interpretation</h2>
<p class="story" id="story-main">Adjust a lens weight to change the story.</p>
<p class="story" id="story-driver">The market is currently balanced.</p>
</div>
</section>
</section>
<aside class="panel" aria-label="Basket and market controls">
<header class="panel-head">
<p class="eyebrow">Fixed Consumption Basket</p>
<h1>Build your lens.</h1>
<p class="panel-intro">
Quantities stay locked. Move each <strong>lens weight</strong> to decide
how much a good should matter to the toy index.
</p>
</header>
<section class="section">
<div class="section-title">
<h2>1. Choose a fixed basket</h2>
<span>Quantities cannot be edited</span>
</div>
<div class="basket-buttons" id="basket-buttons">
<button class="basket-button active" data-basket="everyday">
EVERYDAY
<br>4·2·3·2·1
</button>
<button class="basket-button" data-basket="fresh">
FRESH
<br>6·1·4·2·1
</button>
<button class="basket-button" data-basket="pantry">
PANTRY
<br>2·5·2·3·2
</button>
</div>
<div class="method-box">
<code>I = 100 × Σ(q × w × p) / Σ(q × w × p⁰)</code>
<p>
The index compares the current weighted cost of a fixed basket with
its reference cost. Weights change emphasis, not the goods in the basket.
</p>
</div>
</section>
<section class="section">
<div class="section-title">
<h2>2. Change market prices</h2>
<span>Visible on every stall</span>
</div>
<div class="market-actions">
<button class="action-button" id="market-tick">Random market tick</button>
<button class="action-button secondary" id="reset-all">Reset everything</button>
</div>
<div id="item-list"></div>
</section>
<footer class="panel-footer">
<strong>Equal-lens comparison:</strong>
100 × Σ(q × p) / Σ(q × p⁰). It uses the same fixed quantities but gives
every good equal weight. This is a teaching toy, not a real price index.
Base prices never change.
</footer>
</aside>
</main>
<div id="toast" role="status" aria-live="polite"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// State
// -------------------------------------------------------------------------
const goods = [
{
id: "apples",
name: "Apples",
unit: "per apple",
code: "AP",
base: 1.20,
price: 1.35,
weight: 45,
qty: 4,
color: "#ff806d",
signColor: "#f5a092",
productColor: "#e95648"
},
{
id: "bread",
name: "Bread",
unit: "per loaf",
code: "BR",
base: 3.60,
price: 3.45,
weight: 30,
qty: 2,
color: "#e7b768",
signColor: "#f1cd8d",
productColor: "#b9783c"
},
{
id: "milk",
name: "Milk",
unit: "per carton",
code: "MI",
base: 2.75,
price: 2.90,
weight: 25,
qty: 3,
color: "#82cde9",
signColor: "#a8dff0",
productColor: "#e9f2e9"
},
{
id: "eggs",
name: "Eggs",
unit: "per dozen",
code: "EG",
base: 2.40,
price: 2.30,
weight: 15,
qty: 2,
color: "#d8c9a2",
signColor: "#ead9b0",
productColor: "#f1dfb4"
},
{
id: "cheese",
name: "Cheese",
unit: "per block",
code: "CH",
base: 6.80,
price: 7.10,
weight: 10,
qty: 1,
color: "#ffd76b",
signColor: "#ffe497",
productColor: "#f5c542"
}
];
const initialState = goods.map((good) => ({
...good,
initialPrice: good.price,
initialWeight: good.weight
}));
const baskets = {
everyday: [4, 2, 3, 2, 1],
fresh: [6, 1, 4, 2, 1],
pantry: [2, 5, 2, 3, 2]
};
let activeBasket = "everyday";
const itemList = document.querySelector("#item-list");
const basketButtons = document.querySelector("#basket-buttons");
const toastElement = document.querySelector("#toast");
const indexMain = document.querySelector("#index-main");
const indexChange = document.querySelector("#index-change");
const indexEqual = document.querySelector("#index-equal");
const indexGap = document.querySelector("#index-gap");
const gapLabel = document.querySelector("#gap-label");
const weightedCurrentElement = document.querySelector("#weighted-current");
const weightedBaseElement = document.querySelector("#weighted-base");
const weightedDifferenceElement = document.querySelector("#weighted-difference");
const storyMain = document.querySelector("#story-main");
const storyDriver = document.querySelector("#story-driver");
// -------------------------------------------------------------------------
// Formatting and calculations
// -------------------------------------------------------------------------
const clamp = THREE.MathUtils.clamp;
const roundMoney = (value) => Math.round(value * 100) / 100;
const money = (value) => `$${value.toFixed(2)}`;
const signedMoney = (value) => `${value >= 0 ? "+" : "−"}$${Math.abs(value).toFixed(2)}`;
const signedNumber = (value, digits = 1) =>
`${value >= 0 ? "+" : "−"}${Math.abs(value).toFixed(digits)}`;
function setPrice(item, value) {
item.price = roundMoney(clamp(value, item.base * 0.6, item.base * 1.5));
item.pulse = 1;
}
function buildItemCards() {
itemList.innerHTML = goods
.map(
(item, index) => `
<article class="item-card" data-index="${index}" style="--accent:${item.color}">
<div class="item-head">
<div class="item-code">${item.code}</div>
<div>
<div class="item-name">${item.name}</div>
<div class="item-meta">
${item.unit} · base <span data-base-price></span>
<span data-price-change></span>
</div>
</div>
<div class="share">
<strong data-share>0%</strong>
<span>influence</span>
</div>
</div>
<div class="price-row">
<div class="fixed-qty">FIXED QTY <strong data-qty>0</strong></div>
<div class="price-controls">
<button data-action="dec" aria-label="Decrease ${item.name} price">−</button>
<label class="price-input-wrap">
<span>$</span>
<input
data-price-input
type="number"
min="0.25"
step="0.05"
inputmode="decimal"
aria-label="${item.name} current unit price"
/>
</label>
<button data-action="inc" aria-label="Increase ${item.name} price">+</button>
</div>
</div>
<label class="weight-label" for="weight-${index}">
<span>Lens weight</span>
<output data-weight-label>0</output>
</label>
<input
id="weight-${index}"
data-weight-input
type="range"
min="0"
max="100"
step="1"
value="${item.weight}"
aria-label="${item.name} lens weight"
/>
<div class="influence-track">
<span data-influence-bar></span>
</div>
</article>
`
)
.join("");
}
function calculateIndex() {
const equalCurrent = goods.reduce(
(sum, item) => sum + item.qty * item.price,
0
);
const equalBase = goods.reduce(
(sum, item) => sum + item.qty * item.base,
0
);
const equalIndex = (equalCurrent / equalBase) * 100;
const weightSum = goods.reduce((sum, item) => sum + item.weight, 0);
const influenceTotal = goods.reduce(
(sum, item) => sum + item.qty * item.weight,
0
);
let weightedCurrent = 0;
let weightedBase = 0;
let weightedIndex = null;
const contributions = [];
if (weightSum > 0) {
for (const item of goods) {
const normalizedWeight = item.weight / weightSum;
const currentCost = item.qty * normalizedWeight * item.price;
const referenceCost = item.qty * normalizedWeight * item.base;
weightedCurrent += currentCost;
weightedBase += referenceCost;
contributions.push({
item,
change: currentCost - referenceCost
});
}
weightedIndex = (weightedCurrent / weightedBase) * 100;
}
const dominant = contributions.reduce((largest, entry) => {
if (!largest) return entry;
return Math.abs(entry.change) > Math.abs(largest.change) ? entry : largest;
}, null);
return {
equalIndex,
weightedCurrent,
weightedBase,
weightedIndex,
influenceTotal,
dominant
};
}
function updateUI() {
const result = calculateIndex();
goods.forEach((item, index) => {
const card = itemList.querySelector(`[data-index="${index}"]`);
const influence = item.qty * item.weight;
const share = result.influenceTotal
? (influence / result.influenceTotal) * 100
: 0;
const priceChanged = Math.abs(item.price - item.base) >= 0.005;
card.querySelector("[data-qty]").textContent = item.qty;
card.querySelector("[data-base-price]").textContent = money(item.base);
card.querySelector("[data-price-input]").value = item.price.toFixed(2);
card.querySelector("[data-weight-input]").value = item.weight;
card.querySelector("[data-weight-label]").textContent = item.weight;
card.querySelector("[data-share]").textContent = `${share.toFixed(0)}%`;
card.querySelector("[data-influence-bar]").style.width = `${share}%`;
const changeElement = card.querySelector("[data-price-change]");
if (priceChanged) {
const percent = ((item.price - item.base) / item.base) * 100;
changeElement.className = "changed";
changeElement.textContent = ` · ${signedNumber(percent)}%`;
} else {
changeElement.className = "";
changeElement.textContent = "";
}
});
basketButtons.querySelectorAll("[data-basket]").forEach((button) => {
button.classList.toggle("active", button.dataset.basket === activeBasket);
});
if (result.weightedIndex === null) {
indexMain.textContent = "—";
indexChange.textContent = "set a weight";
indexEqual.textContent = result.equalIndex.toFixed(1);
indexGap.textContent = "—";
gapLabel.textContent = "no weighted lens";
weightedCurrentElement.textContent = "$0.00";
weightedBaseElement.textContent = "$0.00";
weightedDifferenceElement.textContent = "$0.00";
storyMain.textContent =
"All lens weights are zero. Raise at least one weight to create your interpretation.";
storyDriver.textContent =
"The equal-weight lens still shows how the fixed basket has changed.";
return;
}
const changeFromBase = result.weightedIndex - 100;
const gap = result.weightedIndex - result.equalIndex;
indexMain.textContent = result.weightedIndex.toFixed(1);
indexChange.textContent = `${signedNumber(changeFromBase)}% from base`;
indexEqual.textContent = result.equalIndex.toFixed(1);
indexGap.textContent = signedNumber(gap);
gapLabel.textContent = "points vs equal lens";
indexMain.classList.toggle("positive", changeFromBase > 0.05);
indexMain.classList.toggle("negative", changeFromBase < -0.05);
indexGap.classList.toggle("positive", gap > 0.05);
indexGap.classList.toggle("negative", gap < -0.05);
weightedCurrentElement.textContent = money(result.weightedCurrent);
weightedBaseElement.textContent = money(result.weightedBase);
weightedDifferenceElement.textContent = signedMoney(
result.weightedCurrent - result.weightedBase
);
if (Math.abs(gap) < 0.15) {
storyMain.textContent =
"Your lens and the equal-weight fixed basket tell almost the same story.";
} else if (gap > 0) {
storyMain.textContent =
`Your lens reads ${gap.toFixed(1)} points hotter than the equal lens because the goods you emphasize are pulling upward.`;
} else {
storyMain.textContent =
`Your lens reads ${Math.abs(gap).toFixed(1)} points cooler than the equal lens because the goods you emphasize are pulling downward.`;
}
if (
result.dominant &&
Math.abs(result.dominant.change) >= 0.01
) {
const direction =
result.dominant.change >= 0 ? "raises" : "lowers";
storyDriver.textContent =
`${result.dominant.item.name} ${direction} the weighted cost most, by ${signedMoney(result.dominant.change)}.`;
} else {
storyDriver.textContent =
"No single good is materially changing the weighted basket right now.";
}
updatePriceSigns();
}
let toastTimer;
function showToast(message) {
toastElement.textContent = message;
toastElement.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
toastElement.classList.remove("show");
}, 2200);
}
// -------------------------------------------------------------------------
// UI events
// -------------------------------------------------------------------------
itemList.addEventListener("click", (event) => {
const button = event.target.closest("button[data-action]");
if (!button) return;
const card = button.closest("[data-index]");
const index = Number(card.dataset.index);
const item = goods[index];
const direction = button.dataset.action === "inc" ? 1 : -1;
setPrice(item, item.price + direction * 0.1);
updateUI();
showToast(
`${item.name}: ${money(item.price)} per ${item.unit.replace("per ", "")}.`
);
});
itemList.addEventListener("change", (event) => {
if (!event.target.matches("[data-price-input]")) return;
const card = event.target.closest("[data-index]");
const index = Number(card.dataset.index);
const item = goods[index];
const value = Number.parseFloat(event.target.value);
if (Number.isFinite(value)) {
setPrice(item, value);
}
updateUI();
});
itemList.addEventListener("input", (event) => {
if (!event.target.matches("[data-weight-input]")) return;
const card = event.target.closest("[data-index]");
const index = Number(card.dataset.index);
goods[index].weight = Number(event.target.value);
updateUI();
});
basketButtons.addEventListener("click", (event) => {
const button = event.target.closest("[data-basket]");
if (!button) return;
activeBasket = button.dataset.basket;
const quantities = baskets[activeBasket];
goods.forEach((item, index) => {
item.qty = quantities[index];
});
updateUI();
showToast(
`${activeBasket[0].toUpperCase() + activeBasket.slice(1)} basket selected. Quantities are now fixed.`
);
});
document.querySelector("#market-tick").addEventListener("click", () => {
for (const item of goods) {
const randomChange = (Math.random() - 0.46) * 0.12;
setPrice(item, item.base * (1 + randomChange));
}
updateUI();
showToast("Market tick! Prices, signs and both index lenses were recalculated.");
});
document.querySelector("#reset-all").addEventListener("click", () => {
activeBasket = "everyday";
initialState.forEach((initial, index) => {
goods[index].qty = baskets.everyday[index];
goods[index].price = initial.initialPrice;
goods[index].weight = initial.initialWeight;
});
updateUI();
showToast("Market, weights and basket returned to their starting values.");
});
// -------------------------------------------------------------------------
// Three.js scene
// -------------------------------------------------------------------------
const sceneHost = document.querySelector("#scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color("#c9dfd8");
scene.fog = new THREE.Fog("#c9dfd8", 18, 36);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Voxel market. Drag to orbit, scroll to zoom, and click stalls to change prices."
);
sceneHost.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-8, 8, 6, -6, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 1.05, -0.35);
const orbit = {
theta: 0.62,
phi: 0.9,
zoom: 1.03,
radius: 18
};
function updateCamera() {
const spherical = new THREE.Spherical(
orbit.radius,
orbit.phi,
orbit.theta
);
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
camera.zoom = orbit.zoom;
camera.updateProjectionMatrix();
}
const hemisphere = new THREE.HemisphereLight("#fff9df", "#6d7b5d", 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight("#fff2cb", 3.1);
sun.position.set(-7, 13, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const rimLight = new THREE.DirectionalLight("#b9e8ff", 1.0);
rimLight.position.set(9, 6, -8);
scene.add(rimLight);
const materialCache = new Map();
function getMaterial(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0
})
);
}
return materialCache.get(color);
}
function box(
width,
height,
depth,
color,
x = 0,
y = 0,
z = 0,
parent = scene
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
getMaterial(color)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function createGroundAndTiles() {
box(42, 0.3, 32, "#a9c7a7", 0, -0.83, 0);
box(15.2, 0.62, 10.2, "#b78355", 0, -0.42, 0);
box(15.6, 0.18, 10.6, "#7f583f", 0, -0.67, 0);
const tileGeometry = new THREE.BoxGeometry(0.98, 0.09, 0.98);
const tileMaterial = new THREE.MeshStandardMaterial({
roughness: 0.95
});
const columns = 14;
const rows = 9;
const tiles = new THREE.InstancedMesh(
tileGeometry,
tileMaterial,
columns * rows
);
tiles.receiveShadow = true;
const matrix = new THREE.Matrix4();
const tileColors = ["#eee0be", "#e7d8b4", "#f2e7ca", "#d9cfab"];
let instance = 0;
for (let row = 0; row < rows; row++) {
for (let column = 0; column < columns; column++) {
matrix.makeTranslation(
(column - (columns - 1) / 2) * 1.02,
0,
(row - (rows - 1) / 2) * 1.02
);
tiles.setMatrixAt(instance, matrix);
tiles.setColorAt(
instance,
new THREE.Color(tileColors[(column + row * 3) % tileColors.length])
);
instance++;
}
}
scene.add(tiles);
// Small border bricks.
for (let i = 0; i < 15; i++) {
box(0.76, 0.18, 0.3, "#d4b47c", -6.6 + i, 0.04, -4.76);
box(0.76, 0.18, 0.3, "#d4b47c", -6.6 + i, 0.04, 4.76);
}
}
function createTree(x, z, scale = 1) {
const tree = new THREE.Group();
tree.position.set(x, 0, z);
tree.scale.setScalar(scale);
scene.add(tree);
box(0.42, 2.3, 0.42, "#815238", 0, 1.12, 0, tree);
box(1.7, 1.25, 1.45, "#679b69", 0, 2.45, 0, tree);
box(1.15, 0.95, 1.1, "#7eae70", -0.4, 3.18, 0.05, tree);
box(1.0, 0.82, 0.9, "#91bd78", 0.58, 3.05, -0.15, tree);
box(0.75, 0.62, 0.72, "#5f9265", 0.1, 3.62, 0, tree);
}
function createPerson(x, z, shirtColor, skinColor = "#d99b72", rotation = 0) {
const person = new THREE.Group();
person.position.set(x, 0, z);
person.rotation.y = rotation;
scene.add(person);
box(0.24, 0.52, 0.28, "#344b55", -0.15, 0.28, 0, person);
box(0.24, 0.52, 0.28, "#344b55", 0.15, 0.28, 0, person);
box(0.66, 0.7, 0.38, shirtColor, 0, 0.82, 0, person);
box(0.22, 0.58, 0.24, shirtColor, -0.43, 0.76, 0, person);
box(0.22, 0.58, 0.24, shirtColor, 0.43, 0.76, 0, person);
box(0.46, 0.46, 0.43, skinColor, 0, 1.42, 0, person);
box(0.55, 0.12, 0.52, "#334c52", 0, 1.68, 0, person);
}
function createCloud(x, y, z, scale = 1) {
const cloud = new THREE.Group();
cloud.position.set(x, y, z);
cloud.scale.setScalar(scale);
scene.add(cloud);
box(2.2, 0.7, 0.8, "#f5f4e4", 0, 0, 0, cloud);
box(1.2, 1.0, 0.85, "#f5f4e4", -0.8, 0.22, 0, cloud);
box(1.3, 1.2, 0.85, "#f5f4e4", 0.65, 0.28, 0, cloud);
box(1.5, 0.7, 0.8, "#f5f4e4", 1.35, -0.08, 0, cloud);
return cloud;
}
function createProducts(item, group) {
const products = new THREE.Group();
group.add(products);
if (item.id === "apples") {
const positions = [
[-0.55, 1.4, 0.22],
[0, 1.48, 0.35],
[0.55, 1.4, 0.22],
[-0.28, 1.4, -0.25],
[0.3, 1.4, -0.22]
];
for (const [x, y, z] of positions) {
box(0.37, 0.37, 0.37, item.productColor, x, y, z, products);
box(0.09, 0.12, 0.09, "#547649", x, y + 0.23, z, products);
}
}
if (item.id === "bread") {
for (let i = 0; i < 3; i++) {
const x = (i - 1) * 0.55;
box(0.7, 0.3, 0.43, item.productColor, x, 1.43, 0.22, products);
box(0.46, 0.035, 0.07, "#e0a75f", x, 1.59, 0.23, products);
box(0.46, 0.035, 0.07, "#e0a75f", x, 1.59, 0.12, products);
}
}
if (item.id === "milk") {
for (let i = 0; i < 3; i++) {
const x = (i - 1) * 0.55;
box(0.38, 0.58, 0.4, item.productColor, x, 1.55, 0.2, products);
box(0.3, 0.16, 0.32, "#74b7d4", x, 1.91, 0.2, products);
box(0.23, 0.25, 0.025, "#5f9fbb", x, 1.53, 0.41, products);
}
}
if (item.id === "eggs") {
box(0.95, 0.16, 0.64, "#9a7147", 0, 1.3, 0.2, products);
for (let row = 0; row < 2; row++) {
for (let column = 0; column < 3; column++) {
box(
0.2,
0.25,
0.2,
item.productColor,
(column - 1) * 0.28,
1.5,
0.2 + (row - 0.5) * 0.25,
products
);
}
}
}
if (item.id === "cheese") {
box(0.75, 0.55, 0.65, item.productColor, -0.2, 1.48, 0.2, products);
box(0.52, 0.38, 0.45, "#ffe28b", 0.48, 1.41, 0.1, products);
box(0.09, 0.09, 0.025, "#c88c2d", -0.35, 1.58, 0.535, products);
box(0.08, 0.08, 0.025, "#c88c2d", 0.02, 1.38, 0.535, products);
}
return products;
}
const priceSigns = new Map();
const visualGoods = new Map();
const interactiveMeshes = [];
function makePriceSign(item) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 280;
const context = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(8, renderer.capabilities.getMaxAnisotropy());
const material = new THREE.MeshBasicMaterial({ map: texture });
const mesh = new THREE.Mesh(new THREE.PlaneGeometry(1.96, 1.07), material);
mesh.position.set(0, 2.35, -0.48);
const sign = { canvas, context, texture, mesh };
priceSigns.set(item.id, sign);
return sign;
}
function drawPriceSign(item) {
const sign = priceSigns.get(item.id);
if (!sign) return;
const { canvas, context } = sign;
context.fillStyle = "#fff8e6";
context.fillRect(0, 0, canvas.width, canvas.height);
context.fillStyle = item.signColor;
context.fillRect(0, 0, canvas.width, 78);
context.strokeStyle = "#17373b";
context.lineWidth = 12;
context.strokeRect(6, 6, canvas.width - 12, canvas.height - 12);
context.fillStyle = "#17373b";
context.font = "900 43px Arial, sans-serif";
context.textAlign = "center";
context.fillText(item.name.toUpperCase(), 256, 53);
context.fillStyle = "#102c31";
context.font = "900 86px monospace";
context.fillText(money(item.price), 256, 169);
context.fillStyle = "#456761";
context.font = "700 25px Arial, sans-serif";
context.fillText("CURRENT UNIT PRICE", 256, 210);
context.fillStyle = "#6b817d";
context.font = "700 21px monospace";
context.fillText(`BASE ${money(item.base)} • ${item.unit.toUpperCase()}`, 256, 249);
sign.texture.needsUpdate = true;
}
function createStall(item, index) {
const stall = new THREE.Group();
stall.position.set((index - 2) * 2.72, 0, -2.45);
scene.add(stall);
// Counter and top.
box(2.22, 0.85, 1.22, "#8c593b", 0, 0.58, 0.12, stall);
box(2.35, 0.16, 1.34, "#d19a62", 0, 1.08, 0.12, stall);
box(1.86, 0.62, 0.08, "#f0d59a", 0, 0.62, 0.75, stall);
// Back wall and posts.
box(2.26, 1.75, 0.12, "#f1e5c6", 0, 1.95, -0.68, stall);
box(0.12, 2.6, 0.12, "#5e7666", -1.06, 1.43, -0.53, stall);
box(0.12, 2.6, 0.12, "#5e7666", 1.06, 1.43, -0.53, stall);
box(0.12, 2.6, 0.12, "#5e7666", -1.06, 1.43, 0.62, stall);
box(0.12, 2.6, 0.12, "#5e7666", 1.06, 1.43, 0.62, stall);
// Striped voxel canopy.
for (let stripe = 0; stripe < 5; stripe++) {
const color = stripe % 2 === 0 ? "#fff4d8" : item.color;
box(
0.49,
0.23,
1.72,
color,
(stripe - 2) * 0.49,
2.89,
0.02,
stall
);
box(
0.49,
0.26,
0.2,
color,
(stripe - 2) * 0.49,
2.63,
0.78,
stall
);
}
// Sign board and dynamic canvas plane.
box(2.13, 1.2, 0.13, "#28494a", 0, 2.35, -0.54, stall);
const sign = makePriceSign(item);
sign.mesh.position.z = -0.46;
stall.add(sign.mesh);
// A small inventory block under the counter.
box(0.58, 0.43, 0.65, item.color, -0.72, 0.25, 0.85, stall);
box(0.58, 0.43, 0.65, item.color, 0.72, 0.25, 0.85, stall);
const products = createProducts(item, stall);
visualGoods.set(item.id, { stall, products, pulse: 0 });
stall.traverse((object) => {
if (object.isMesh) {
object.userData.itemId = item.id;
interactiveMeshes.push(object);
}
});
drawPriceSign(item);
}
function createDecorativeMarketDetails() {
// Signposts with tiny illuminated blocks.
for (const x of [-6.2, 6.2]) {
box(0.16, 3.4, 0.16, "#526c61", x, 1.7, -3.55);
box(0.65, 0.65, 0.65, "#ffe6a2", x, 3.35, -3.55);
box(0.34, 0.34, 0.34, "#fff5c9", x, 3.82, -3.55);
}
// Market entrance blocks.
box(0.72, 0.72, 0.72, "#ff8d78", -6.35, 0.38, 3.65);
box(0.72, 0.72, 0.72, "#8fe0be", 6.35, 0.38, 3.65);
box(0.72, 0.72, 0.72, "#ffd76b", -5.45, 0.38, 3.65);
box(0.72, 0.72, 0.72, "#82cde9", 5.45, 0.38, 3.65);
// Small crates.
for (const [x, z] of [[-6.1, 0.1], [-5.3, 0.2], [5.7, -0.1]]) {
box(0.7, 0.52, 0.7, "#a77343", x, 0.28, z);
box(0.76, 0.09, 0.76, "#d2a067", x, 0.58, z);
}
}
function updatePriceSigns() {
for (const item of goods) {
drawPriceSign(item);
}
}
createGroundAndTiles();
createTree(-7.1, 1.7, 1.05);
createTree(7.15, 1.65, 0.95);
createTree(-6.7, -3.75, 0.72);
createTree(6.8, -3.65, 0.76);
goods.forEach(createStall);
createDecorativeMarketDetails();
createPerson(-4.7, 1.65, "#ff8d78", "#d99b72", 0.45);
createPerson(-1.5, 2.45, "#82cde9", "#9e674c", -0.25);
createPerson(1.1, 1.25, "#ffd76b", "#e0aa7e", 0.65);
createPerson(4.25, 2.1, "#8fe0be", "#704a3b", -0.5);
const clouds = [
createCloud(-7.5, 6.3, -7.5, 0.85),
createCloud(6.3, 7.1, -8.5, 0.7),
createCloud(1.2, 6.6, -9.2, 0.55)
];
// -------------------------------------------------------------------------
// Direct camera controls — no addons
// -------------------------------------------------------------------------
const pointer = new THREE.Vector2();
const raycaster = new THREE.Raycaster();
const drag = {
active: false,
startX: 0,
startY: 0,
lastX: 0,
lastY: 0,
distance: 0
};
renderer.domElement.addEventListener("pointerdown", (event) => {
drag.active = true;
drag.startX = event.clientX;
drag.startY = event.clientY;
drag.lastX = event.clientX;
drag.lastY = event.clientY;
drag.distance = 0;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!drag.active) return;
const deltaX = event.clientX - drag.lastX;
const deltaY = event.clientY - drag.lastY;
drag.distance += Math.abs(deltaX) + Math.abs(deltaY);
orbit.theta -= deltaX * 0.007;
orbit.phi = clamp(orbit.phi - deltaY * 0.006, 0.56, 1.28);
drag.lastX = event.clientX;
drag.lastY = event.clientY;
updateCamera();
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!drag.active) return;
const wasClick = drag.distance < 7;
drag.active = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
if (!wasClick) return;
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hit = raycaster.intersectObjects(interactiveMeshes, false)[0];
if (!hit?.object.userData.itemId) return;
const item = goods.find(
(candidate) => candidate.id === hit.object.userData.itemId
);
if (!item) return;
const change = event.shiftKey ? -0.1 : 0.1;
setPrice(item, item.price + change);
updateUI();
showToast(
`${item.name} ${change > 0 ? "up" : "down"} $0.10 → ${money(item.price)}`
);
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.zoom = clamp(
orbit.zoom * Math.exp(-event.deltaY * 0.001),
0.72,
1.95
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
orbit.theta = 0.62;
orbit.phi = 0.9;
orbit.zoom = 1.03;
updateCamera();
});
renderer.domElement.addEventListener("keydown", (event) => {
let handled = true;
if (event.key === "ArrowLeft") orbit.theta += 0.08;
else if (event.key === "ArrowRight") orbit.theta -= 0.08;
else if (event.key === "ArrowUp") {
orbit.phi = clamp(orbit.phi - 0.06, 0.56, 1.28);
} else if (event.key === "ArrowDown") {
orbit.phi = clamp(orbit.phi + 0.06, 0.56, 1.28);
} else handled = false;
if (handled) {
event.preventDefault();
updateCamera();
}
});
function resize() {
const width = sceneHost.clientWidth;
const height = sceneHost.clientHeight;
const aspect = width / Math.max(1, height);
const frustumHeight = 12.4;
camera.left = (-frustumHeight * aspect) / 2;
camera.right = (frustumHeight * aspect) / 2;
camera.top = frustumHeight / 2;
camera.bottom = -frustumHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(sceneHost);
resize();
updateCamera();
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
for (const item of goods) {
const visual = visualGoods.get(item.id);
if (!visual) continue;
visual.pulse *= 0.94;
const pulse = Math.sin(elapsed * 10) * 0.035 * visual.pulse;
visual.products.scale.setScalar(1 + pulse);
}
clouds[0].position.x = -7.5 + Math.sin(elapsed * 0.12) * 0.4;
clouds[1].position.x = 6.3 + Math.cos(elapsed * 0.1) * 0.35;
renderer.render(scene, camera);
}
// -------------------------------------------------------------------------
// Initialize
// -------------------------------------------------------------------------
buildItemCards();
updateUI();
animate();
</script>
</body>
</html>
```