Prompt specification
Build or customize the Three.js 3D scene "Market measurement learning game" 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: Market measurement learning game
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/market-measurement-learning-game-x0560
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing produce weighing. Create stalls selling by item, mass, and volume with fictional values; users identify each measurement basis and complete orders using the corresponding visible counting or measuring tool. Low-poly.
Working Three.js Starter (market-measurement-learning-game-x0560):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>The Weigh Fair — Produce Measurement Market</title>
<style>
:root {
--ink: #24362e;
--muted: #718078;
--paper: #fff9e9;
--paper-2: #f5eedb;
--line: #d9d1bb;
--green: #315c47;
--gold: #e4aa3e;
--red: #d95d49;
--blue: #4f8e91;
--shadow: 0 14px 38px rgba(39, 55, 43, 0.17);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #cfe1d4;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
outline: none;
}
.topbar {
position: fixed;
z-index: 10;
top: 16px;
left: 18px;
right: 18px;
display: flex;
align-items: flex-start;
justify-content: space-between;
pointer-events: none;
}
.brand,
.wallet {
background: rgba(255, 249, 233, 0.92);
border: 1px solid rgba(255, 255, 255, 0.75);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
}
.brand {
min-width: 300px;
padding: 13px 18px 14px;
border-radius: 18px;
}
.eyebrow,
.panel-kicker {
margin: 0 0 5px;
color: var(--green);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.brand h1 {
margin: 0;
font-size: 29px;
line-height: 1;
letter-spacing: -0.045em;
}
.brand p {
margin: 7px 0 0;
color: var(--muted);
font-size: 12px;
}
.wallet {
display: flex;
align-items: center;
gap: 11px;
padding: 11px 15px;
border-radius: 16px;
pointer-events: auto;
}
.coin {
width: 29px;
height: 29px;
display: grid;
place-items: center;
flex: 0 0 auto;
border: 3px solid #c38b25;
border-radius: 50%;
background: #f4c75c;
color: #805b18;
font-size: 12px;
font-weight: 900;
box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.4);
}
.wallet small {
display: block;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#walletValue {
display: block;
margin-top: 2px;
font-size: 18px;
font-weight: 850;
}
.panel {
position: fixed;
z-index: 10;
background: rgba(255, 249, 233, 0.94);
border: 1px solid rgba(255, 255, 255, 0.8);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
}
#marketNav {
top: 118px;
left: 18px;
width: 255px;
padding: 15px;
border-radius: 20px;
}
.nav-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 10px;
}
.nav-heading .panel-kicker {
margin: 0;
}
.tiny-label {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.order-list {
display: grid;
gap: 8px;
}
.order-card {
position: relative;
width: 100%;
overflow: hidden;
padding: 12px 12px 11px 15px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(255, 255, 255, 0.55);
color: var(--ink);
text-align: left;
cursor: pointer;
transition: transform 150ms ease, border-color 150ms ease,
background 150ms ease;
}
.order-card::before {
content: "";
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 5px;
background: var(--card-color);
}
.order-card:hover {
transform: translateX(2px);
border-color: var(--card-color);
background: #fffdf6;
}
.order-card.active {
border-color: var(--card-color);
background: #fffdf6;
box-shadow: 0 5px 16px rgba(40, 53, 43, 0.09);
}
.order-card.paid {
opacity: 0.72;
}
.order-top,
.order-bottom {
display: flex;
align-items: center;
justify-content: space-between;
gap: 7px;
}
.order-top {
margin-bottom: 5px;
}
.order-number {
color: var(--muted);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.12em;
}
.status-chip {
padding: 4px 7px;
border-radius: 999px;
background: #ebe6d7;
color: #67726c;
font-size: 8px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.order-card.active .status-chip {
background: color-mix(in srgb, var(--card-color) 18%, white);
color: var(--card-color);
}
.order-card.paid .status-chip {
background: #dcebdc;
color: #3d6c46;
}
.order-name {
display: block;
margin-bottom: 4px;
font-size: 14px;
font-weight: 850;
}
.order-detail {
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
.basis-chip {
padding: 4px 6px;
border: 1px solid var(--line);
border-radius: 6px;
color: var(--green);
font-size: 8px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.market-view {
width: 100%;
margin-top: 10px;
padding: 9px;
border: 1px solid var(--line);
border-radius: 11px;
background: transparent;
color: var(--green);
font-size: 11px;
font-weight: 800;
cursor: pointer;
}
.market-view:hover {
background: var(--paper-2);
}
.fiction-note {
margin: 10px 2px 0;
color: #8b8d82;
font-size: 9px;
line-height: 1.45;
}
#taskPanel {
top: 92px;
right: 18px;
bottom: 18px;
width: 350px;
overflow-y: auto;
padding: 20px;
border-radius: 22px;
scrollbar-width: thin;
scrollbar-color: #c8bea5 transparent;
}
.task-top {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
}
#taskPanel h2 {
margin: 0;
font-size: 25px;
line-height: 1.05;
letter-spacing: -0.04em;
}
.order-number-badge {
display: grid;
place-items: center;
width: 43px;
height: 43px;
flex: 0 0 auto;
border-radius: 13px;
background: var(--stall-color, var(--green));
color: white;
font-size: 12px;
font-weight: 900;
box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.25);
}
.stall-subtitle {
margin: 7px 0 16px;
color: var(--muted);
font-size: 12px;
}
.receipt {
position: relative;
padding: 14px;
border: 1px dashed #c9bea4;
border-radius: 14px;
background: #fffdf6;
}
.receipt::after {
content: "";
position: absolute;
inset: 5px;
border: 1px solid rgba(201, 190, 164, 0.32);
border-radius: 10px;
pointer-events: none;
}
.receipt-row {
position: relative;
z-index: 1;
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin: 7px 0;
font-size: 12px;
}
.receipt-row span:first-child {
color: var(--muted);
}
.receipt-row strong {
text-align: right;
}
.receipt-total {
margin-top: 11px;
padding-top: 10px;
border-top: 1px solid var(--line);
font-size: 15px !important;
font-weight: 900;
}
.section {
margin-top: 20px;
}
.section-title {
display: flex;
align-items: center;
gap: 8px;
margin: 0 0 6px;
font-size: 13px;
font-weight: 900;
}
.step-number {
display: grid;
place-items: center;
width: 22px;
height: 22px;
border-radius: 7px;
background: var(--green);
color: white;
font-size: 10px;
}
.section-copy {
margin: 0 0 12px;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.basis-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.basis-button {
min-width: 0;
padding: 11px 5px 9px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.54);
color: var(--ink);
cursor: pointer;
transition: border-color 140ms ease, background 140ms ease,
transform 140ms ease;
}
.basis-button:hover:not(:disabled) {
transform: translateY(-2px);
border-color: var(--green);
background: white;
}
.basis-button.selected {
border-color: var(--stall-color, var(--green));
background: color-mix(in srgb, var(--stall-color) 13%, white);
box-shadow: inset 0 0 0 1px var(--stall-color);
}
.basis-button:disabled {
cursor: default;
opacity: 0.5;
}
.basis-button.selected:disabled {
opacity: 1;
}
.basis-icon {
display: grid;
place-items: center;
height: 31px;
margin-bottom: 6px;
border-radius: 9px;
background: #ece6d6;
color: var(--green);
font-size: 11px;
font-weight: 900;
letter-spacing: 0.05em;
}
.basis-button strong {
display: block;
font-size: 10px;
}
.basis-button small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 8px;
line-height: 1.2;
}
.feedback {
margin-top: 10px;
padding: 9px 11px;
border-radius: 10px;
background: #f8e4dc;
color: #944a38;
font-size: 10px;
font-weight: 700;
line-height: 1.4;
}
.correct-feedback {
background: #e3efdf;
color: #3e6847;
}
.tool-card {
padding: 13px;
border: 1px solid var(--line);
border-radius: 15px;
background: rgba(255, 255, 255, 0.55);
}
.tool-top {
display: flex;
align-items: center;
gap: 11px;
}
.tool-symbol {
display: grid;
place-items: center;
width: 49px;
height: 49px;
flex: 0 0 auto;
border-radius: 13px;
background: var(--stall-color, var(--green));
color: white;
font-size: 12px;
font-weight: 900;
letter-spacing: 0.05em;
box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.23);
}
.tool-title {
margin: 0 0 4px;
font-size: 13px;
font-weight: 900;
}
.tool-count {
margin: 0;
color: var(--muted);
font-size: 10px;
font-weight: 700;
}
.meter {
height: 9px;
overflow: hidden;
margin-top: 13px;
border-radius: 999px;
background: #e5decd;
}
#meterFill {
width: 0;
height: 100%;
border-radius: inherit;
background: var(--stall-color, var(--green));
transition: width 220ms ease;
}
.tool-hint {
margin: 10px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
.focus-button,
.primary-button,
.secondary-button {
width: 100%;
border-radius: 12px;
cursor: pointer;
font-weight: 850;
}
.focus-button {
margin-top: 12px;
padding: 10px;
border: 1px solid var(--line);
background: transparent;
color: var(--green);
font-size: 11px;
}
.focus-button:hover {
background: var(--paper-2);
}
.primary-button {
margin-top: 12px;
padding: 13px;
border: 0;
background: var(--green);
color: white;
box-shadow: 0 7px 16px rgba(49, 92, 71, 0.19);
font-size: 12px;
}
.primary-button:hover:not(:disabled) {
background: #244a39;
transform: translateY(-1px);
}
.primary-button:disabled {
background: #d6d0c1;
color: #8c8e85;
box-shadow: none;
cursor: not-allowed;
}
.secondary-button {
margin-top: 10px;
padding: 12px;
border: 1px solid var(--line);
background: transparent;
color: var(--green);
font-size: 11px;
}
.receipt-stamp {
margin: 16px 0;
padding: 17px;
border: 2px solid #56815d;
border-radius: 13px;
color: #477251;
text-align: center;
transform: rotate(-1deg);
}
.receipt-stamp strong {
display: block;
font-size: 23px;
letter-spacing: 0.04em;
}
.receipt-stamp span {
display: block;
margin-top: 5px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#hint {
position: fixed;
z-index: 8;
left: 50%;
bottom: 19px;
transform: translateX(-50%);
padding: 9px 13px;
border-radius: 999px;
background: rgba(36, 54, 46, 0.76);
color: white;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.02em;
pointer-events: none;
backdrop-filter: blur(8px);
white-space: nowrap;
}
#toast {
position: fixed;
z-index: 30;
left: 50%;
bottom: 64px;
max-width: min(430px, calc(100vw - 30px));
transform: translate(-50%, 15px);
padding: 12px 17px;
border-radius: 12px;
background: #294638;
color: white;
box-shadow: var(--shadow);
font-size: 11px;
font-weight: 750;
line-height: 1.4;
text-align: center;
opacity: 0;
pointer-events: none;
transition: opacity 180ms ease, transform 180ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#toast.error {
background: #914c3c;
}
.shake {
animation: shake 320ms ease;
}
@keyframes shake {
0%, 100% { transform: translateX(0); }
25% { transform: translateX(-5px); }
75% { transform: translateX(5px); }
}
@media (max-width: 1100px) {
#marketNav {
width: 225px;
}
#taskPanel {
width: 320px;
}
.brand {
min-width: 260px;
}
}
@media (max-width: 780px) {
.topbar {
top: 9px;
left: 9px;
right: 9px;
}
.brand {
min-width: 0;
padding: 10px 12px;
border-radius: 14px;
}
.brand h1 {
font-size: 21px;
}
.brand p,
.brand .eyebrow {
display: none;
}
.wallet {
padding: 8px 10px;
border-radius: 13px;
}
.coin {
width: 25px;
height: 25px;
}
#walletValue {
font-size: 15px;
}
#marketNav {
top: 67px;
left: 9px;
right: 9px;
width: auto;
padding: 9px;
overflow-x: auto;
border-radius: 16px;
}
.nav-heading,
.market-view,
.fiction-note {
display: none;
}
.order-list {
display: flex;
}
.order-card {
min-width: 185px;
padding: 9px 10px 9px 13px;
}
#taskPanel {
top: auto;
left: 9px;
right: 9px;
bottom: 9px;
width: auto;
max-height: 43vh;
padding: 15px;
border-radius: 17px;
}
#taskPanel h2 {
font-size: 21px;
}
#hint {
display: none;
}
#toast {
bottom: calc(43vh + 20px);
}
}
</style>
</head>
<body>
<div id="scene"></div>
<header class="topbar">
<div class="brand">
<p class="eyebrow">Low-poly produce exchange</p>
<h1>The Weigh Fair</h1>
<p>Read the stall. Choose the basis. Use the tool.</p>
</div>
<div class="wallet" aria-label="Market wallet">
<div class="coin">★</div>
<div>
<small>Market coins</small>
<span id="walletValue">0</span>
</div>
</div>
</header>
<aside id="marketNav" class="panel" aria-label="Order board"></aside>
<aside id="taskPanel" class="panel" aria-live="polite"></aside>
<div id="hint">Drag to orbit · Scroll to zoom · Click the highlighted produce tool</div>
<div id="toast" role="status"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Application data
// -------------------------------------------------------------------------
const stalls = [
{
id: "apple",
order: "01",
name: "Red Apples",
subtitle: "Red Orchard · count stall",
basis: "item",
basisText: "BY ITEM",
target: 6,
rate: 3,
unit: "item",
unitPlural: "pieces",
tool: "Counting tray",
toolHint:
"Tap each apple in the tray. Count the individual pieces until the order is full.",
color: "#d95745",
x: -7.2
},
{
id: "tomato",
order: "02",
name: "Ember Tomatoes",
subtitle: "Ember Field · mass stall",
basis: "mass",
basisText: "BY MASS",
target: 4,
rate: 7,
unit: "kg",
unitPlural: "kg",
tool: "Balance scale",
toolHint:
"Tap the balance once for each 1 kg tomato portion. Four clicks weigh this order.",
color: "#4f8e91",
x: 0
},
{
id: "gourd",
order: "03",
name: "Golden Gourds",
subtitle: "Sunny Acres · volume stall",
basis: "volume",
basisText: "BY VOLUME",
target: 3,
rate: 5,
unit: "L",
unitPlural: "litres",
tool: "Measuring jug",
toolHint:
"Tap the measuring jug to add one litre at a time. Watch the volume level rise.",
color: "#d99a32",
x: 7.2
}
];
const app = {
selectedId: "apple",
wallet: 0
};
for (const stall of stalls) {
stall.basisCorrect = false;
stall.feedback = "";
stall.measured = 0;
stall.completed = false;
}
const marketNav = document.getElementById("marketNav");
const taskPanel = document.getElementById("taskPanel");
const walletValue = document.getElementById("walletValue");
const toastElement = document.getElementById("toast");
// -------------------------------------------------------------------------
// Renderer, camera, lighting, and direct camera controls
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xcfe1d4);
scene.fog = new THREE.Fog(0xcfe1d4, 30, 58);
const camera = new THREE.PerspectiveCamera(
40,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
document.getElementById("scene").appendChild(renderer.domElement);
renderer.domElement.setAttribute("aria-label", "Interactive 3D produce market");
const cameraState = {
target: new THREE.Vector3(0, 1.2, 0),
yaw: 0.32,
pitch: 0.49,
radius: 29
};
let cameraGoal = {
target: new THREE.Vector3(0, 1.2, 0),
yaw: 0.32,
pitch: 0.49,
radius: 29
};
function applyCamera() {
const horizontal = Math.cos(cameraState.pitch) * cameraState.radius;
camera.position.set(
cameraState.target.x + Math.sin(cameraState.yaw) * horizontal,
cameraState.target.y + Math.sin(cameraState.pitch) * cameraState.radius,
cameraState.target.z + Math.cos(cameraState.yaw) * horizontal
);
camera.lookAt(cameraState.target);
}
applyCamera();
const hemisphere = new THREE.HemisphereLight(
0xfff4d6,
0x66836d,
2.05
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff4dc, 2.7);
sun.position.set(-9, 17, 11);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -22;
sun.shadow.camera.right = 22;
sun.shadow.camera.top = 18;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.bias = -0.0004;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xc9e9f0, 0.7);
fill.position.set(12, 8, -8);
scene.add(fill);
// -------------------------------------------------------------------------
// Geometry helpers
// -------------------------------------------------------------------------
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0,
flatShading: true,
...options
});
}
function addBox(parent, w, h, d, x, y, z, color, options = {}) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(w, h, d),
standardMaterial(color, options)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(
parent,
topRadius,
bottomRadius,
height,
radialSegments,
x,
y,
z,
color,
options = {}
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
topRadius,
bottomRadius,
height,
radialSegments
),
standardMaterial(color, options)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addIco(
parent,
radius,
detail,
x,
y,
z,
color,
options = {}
) {
const mesh = new THREE.Mesh(
new THREE.IcosahedronGeometry(radius, detail),
standardMaterial(color, options)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCrate(parent, x, z, width = 1.6, depth = 1.2) {
const group = new THREE.Group();
group.position.set(x, 1.48, z);
parent.add(group);
addBox(group, width, 0.13, depth, 0, 0.08, 0, "#9a6a3e");
const slatColor = "#b6814c";
addBox(group, width, 0.42, 0.11, 0, 0.25, depth / 2, slatColor);
addBox(group, width, 0.42, 0.11, 0, 0.25, -depth / 2, slatColor);
addBox(group, 0.11, 0.42, depth, width / 2, 0.25, 0, slatColor);
addBox(group, 0.11, 0.42, depth, -width / 2, 0.25, 0, slatColor);
return group;
}
function addApple(parent, x, y, z, interactive = false) {
const group = new THREE.Group();
group.position.set(x, y, z);
parent.add(group);
const body = addIco(group, 0.27, 1, 0, 0.29, 0, "#d94d3f");
body.scale.set(1, 0.92, 1);
addCylinder(
group,
0.035,
0.05,
0.23,
6,
0,
0.58,
0,
"#68422c"
);
const leaf = addBox(
group,
0.18,
0.025,
0.1,
0.1,
0.6,
0,
"#4e7b42"
);
leaf.rotation.z = -0.35;
if (interactive) {
group.userData.action = {
type: "measure",
measure: "count",
node: group
};
group.userData.used = false;
}
return group;
}
function addTomato(parent, x, y, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, y, z);
group.scale.setScalar(scale);
parent.add(group);
addIco(group, 0.29, 1, 0, 0.29, 0, "#d84b38");
addCylinder(
group,
0.14,
0.18,
0.08,
5,
0,
0.55,
0,
"#477344"
);
return group;
}
function addGourd(parent, x, y, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, y, z);
group.scale.setScalar(scale);
parent.add(group);
const body = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.32, 0),
standardMaterial("#dda438")
);
body.scale.set(0.82, 1.25, 0.82);
body.position.y = 0.35;
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
addCylinder(
group,
0.055,
0.075,
0.2,
6,
0,
0.76,
0,
"#557647"
);
return group;
}
// -------------------------------------------------------------------------
// Canvas-based signs and floating tool labels
// -------------------------------------------------------------------------
function roundedRect(ctx, x, y, w, h, radius) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.arcTo(x + w, y, x + w, y + h, radius);
ctx.arcTo(x + w, y + h, x, y + h, radius);
ctx.arcTo(x, y + h, x, y, radius);
ctx.arcTo(x, y, x + w, y, radius);
ctx.closePath();
}
function createStallSign(stall) {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 320;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#fff8e5";
ctx.fillRect(0, 0, 1024, 320);
ctx.fillStyle = stall.color;
ctx.fillRect(0, 0, 1024, 54);
ctx.fillStyle = "#fff9e9";
ctx.font = "900 25px system-ui";
ctx.textAlign = "left";
ctx.fillText(
`MEASUREMENT BASIS: ${stall.basisText}`,
38,
36
);
ctx.fillStyle = "#293b31";
ctx.font = "900 62px system-ui";
ctx.fillText(stall.name.toUpperCase(), 38, 136);
ctx.strokeStyle = "#d9cfb5";
ctx.lineWidth = 3;
ctx.beginPath();
ctx.moveTo(38, 170);
ctx.lineTo(986, 170);
ctx.stroke();
ctx.fillStyle = "#57665c";
ctx.font = "800 35px system-ui";
ctx.fillText(
`${stall.rate} COINS / ${stall.unit.toUpperCase()}`,
38,
228
);
ctx.fillStyle = "#8b897b";
ctx.font = "700 23px system-ui";
ctx.fillText("ALL MARKET RATES ARE FICTIONAL", 38, 281);
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,
side: THREE.DoubleSide
});
const sign = new THREE.Mesh(
new THREE.PlaneGeometry(4.95, 1.25),
material
);
sign.position.set(0, 3.34, 1.48);
sign.castShadow = false;
return sign;
}
function createToolLabel(parent, x, y, z, color, top, bottom) {
const canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 190;
const ctx = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false
})
);
sprite.position.set(x, y, z);
sprite.scale.set(2.35, 0.7, 1);
sprite.renderOrder = 8;
parent.add(sprite);
function update(newTop, newBottom) {
ctx.clearRect(0, 0, 640, 190);
ctx.fillStyle = "rgba(34, 52, 43, 0.94)";
roundedRect(ctx, 8, 8, 624, 174, 28);
ctx.fill();
ctx.fillStyle = color;
roundedRect(ctx, 8, 8, 624, 40, 25);
ctx.fill();
ctx.textAlign = "center";
ctx.fillStyle = "#ffffff";
ctx.font = "900 29px system-ui";
ctx.fillText(newTop.toUpperCase(), 320, 39);
ctx.fillStyle = "#fff8e7";
ctx.font = "900 44px system-ui";
ctx.fillText(newBottom.toUpperCase(), 320, 116);
ctx.fillStyle = "#b9c7bd";
ctx.font = "700 22px system-ui";
ctx.fillText("TAP TO USE THE TOOL", 320, 155);
texture.needsUpdate = true;
}
update(top, bottom);
return { sprite, update };
}
// -------------------------------------------------------------------------
// Environment
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.CylinderGeometry(16.5, 17, 0.35, 48),
standardMaterial("#a9bd8f")
);
ground.position.y = -0.2;
ground.receiveShadow = true;
scene.add(ground);
const plaza = new THREE.Mesh(
new THREE.CylinderGeometry(12.8, 13, 0.08, 40),
standardMaterial("#d7c9a7")
);
plaza.position.y = 0;
plaza.receiveShadow = true;
scene.add(plaza);
for (let i = 0; i < 22; i++) {
const angle = (i / 22) * Math.PI * 2;
const radius = 12.2 + (i % 3) * 0.27;
const stone = new THREE.Mesh(
new THREE.CylinderGeometry(
0.28 + (i % 2) * 0.05,
0.32,
0.08,
5
),
standardMaterial(i % 2 ? "#e5d9b8" : "#c8b993")
);
stone.position.set(
Math.sin(angle) * radius,
0.06,
Math.cos(angle) * radius
);
stone.rotation.y = angle;
stone.receiveShadow = true;
scene.add(stone);
}
function addTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.scale.setScalar(scale);
scene.add(group);
addCylinder(
group,
0.18,
0.26,
1.8,
7,
0,
0.9,
0,
"#806044"
);
const foliage = new THREE.Mesh(
new THREE.ConeGeometry(1.25, 3.3, 7),
standardMaterial("#55785a")
);
foliage.position.y = 2.65;
foliage.castShadow = true;
foliage.receiveShadow = true;
group.add(foliage);
const top = new THREE.Mesh(
new THREE.ConeGeometry(0.9, 2.4, 7),
standardMaterial("#698d63")
);
top.position.y = 3.9;
top.castShadow = true;
group.add(top);
}
[
[-13, -6, 1.2],
[-11.8, 7, 0.95],
[-3, -11, 1.15],
[3, -11.8, 1.05],
[10.8, -7, 1.25],
[13.3, 5.5, 1.0],
[7.7, 10, 0.82],
[-6.3, 10.5, 0.9]
].forEach((args) => addTree(...args));
for (let i = 0; i < 5; i++) {
const hill = new THREE.Mesh(
new THREE.ConeGeometry(6 + i, 8 + i, 7),
standardMaterial(i % 2 ? "#8eaa91" : "#7f9f87")
);
hill.position.set(-18 + i * 9, 2.8, -22 - (i % 2) * 2);
hill.rotation.y = i * 0.7;
scene.add(hill);
}
// -------------------------------------------------------------------------
// Stall construction and visible measurement tools
// -------------------------------------------------------------------------
function buildStall(stall) {
const group = new THREE.Group();
group.position.x = stall.x;
group.userData.stallId = stall.id;
scene.add(group);
const wood = "#a87543";
const darkWood = "#744d31";
const cream = "#fff0cc";
// Back wall and frame.
addBox(group, 5.45, 2.25, 0.16, 0, 2.28, -1.28, "#6d8f70");
addBox(group, 5.5, 0.22, 0.22, 0, 4.12, -1.2, darkWood);
addBox(group, 0.22, 4.15, 0.22, -2.55, 2.25, -1.2, darkWood);
addBox(group, 0.22, 4.15, 0.22, 2.55, 2.25, -1.2, darkWood);
addBox(group, 0.2, 3.6, 0.2, -2.55, 2.05, 1.15, darkWood);
addBox(group, 0.2, 3.6, 0.2, 2.55, 2.05, 1.15, darkWood);
// Counter.
addBox(group, 5.6, 1.1, 1.9, 0, 0.64, 0.62, wood);
addBox(group, 5.8, 0.25, 2.08, 0, 1.34, 0.64, "#c28a50");
addBox(group, 5.4, 0.15, 0.12, 0, 0.65, 1.59, cream);
addBox(group, 5.4, 0.07, 0.13, 0, 1.03, 1.60, stall.color);
// Low-poly pyramid canopy.
const roof = new THREE.Mesh(
new THREE.ConeGeometry(4.18, 1.5, 4),
standardMaterial(stall.color)
);
roof.position.set(0, 4.48, -0.15);
roof.rotation.y = Math.PI / 4;
roof.scale.z = 0.69;
roof.castShadow = true;
roof.receiveShadow = true;
group.add(roof);
// Scalloped valance.
for (let i = 0; i < 5; i++) {
addBox(
group,
1.08,
0.42,
0.12,
-2.16 + i * 1.08,
3.82,
1.25,
i % 2 === 0 ? stall.color : cream
);
}
group.add(createStallSign(stall));
// Gold beacon showing where the tool is.
const beacon = new THREE.Mesh(
new THREE.OctahedronGeometry(0.15),
new THREE.MeshStandardMaterial({
color: "#ffd464",
emissive: "#9b6810",
emissiveIntensity: 0.55,
flatShading: true
})
);
group.add(beacon);
const visual = {
beacon,
beaconBaseY: 2.75
};
if (stall.id === "apple") {
const tray = new THREE.Group();
tray.position.set(-0.55, 1.5, 0.73);
group.add(tray);
addBox(tray, 2.85, 0.12, 1.36, 0, 0.05, 0, "#b7844b");
addBox(tray, 2.95, 0.18, 0.1, 0, 0.16, 0.68, "#8c5e38");
addBox(tray, 2.95, 0.18, 0.1, 0, 0.16, -0.68, "#8c5e38");
addBox(tray, 0.1, 0.18, 1.36, 1.48, 0.16, 0, "#8c5e38");
addBox(tray, 0.1, 0.18, 1.36, -1.48, 0.16, 0, "#8c5e38");
visual.apples = [];
for (let row = 0; row < 3; row++) {
for (let col = 0; col < 3; col++) {
const apple = addApple(
tray,
-0.88 + col * 0.88,
0.1,
-0.39 + row * 0.39,
true
);
visual.apples.push(apple);
}
}
visual.label = createToolLabel(
group,
-0.55,
2.3,
0.8,
stall.color,
"Counting tray",
"0 / 6 pieces"
);
visual.beacon.position.set(-0.55, 2.85, 0.8);
// Extra decorative crate of apples.
const crate = addCrate(group, -2.1, -0.2, 0.8, 0.8);
addApple(crate, 0, 0.2, 0, false);
}
if (stall.id === "tomato") {
const crate = addCrate(group, -1.55, 0.7, 1.75, 1.25);
addTomato(crate, -0.47, 0.15, -0.24, 1.05);
addTomato(crate, 0.18, 0.15, -0.25, 1.08);
addTomato(crate, -0.4, 0.15, 0.28, 1.08);
addTomato(crate, 0.26, 0.15, 0.28, 1.02);
const scale = new THREE.Group();
scale.position.set(1.35, 1.5, 0.7);
group.add(scale);
addBox(scale, 0.95, 0.18, 0.76, 0, 0.1, 0, "#40594f");
addCylinder(
scale,
0.67,
0.67,
0.13,
16,
0,
0.28,
0,
"#b9c5bd"
);
const display = addBox(
scale,
0.75,
0.38,
0.1,
0,
0.44,
-0.32,
"#293c35"
);
display.rotation.x = -0.12;
visual.parts = [];
for (let i = 0; i < 4; i++) {
const portion = addTomato(
scale,
-0.28 + (i % 2) * 0.52,
0.35,
-0.15 + Math.floor(i / 2) * 0.32,
0.83
);
portion.visible = false;
visual.parts.push(portion);
}
scale.userData.action = {
type: "measure",
measure: "mass",
node: scale
};
visual.label = createToolLabel(
group,
1.35,
2.5,
0.75,
stall.color,
"Balance scale",
"0 / 4 kg"
);
visual.beacon.position.set(1.35, 3.05, 0.75);
}
if (stall.id === "gourd") {
const crate = addCrate(group, -1.55, 0.7, 1.75, 1.25);
addGourd(crate, -0.45, 0.12, -0.2, 1.05);
addGourd(crate, 0.28, 0.12, -0.18, 1.0);
addGourd(crate, -0.25, 0.12, 0.3, 0.9);
addGourd(crate, 0.38, 0.12, 0.3, 0.86);
const jug = new THREE.Group();
jug.position.set(1.35, 1.5, 0.7);
group.add(jug);
const glassMaterial = new THREE.MeshStandardMaterial({
color: "#d9f0e8",
transparent: true,
opacity: 0.32,
roughness: 0.2,
metalness: 0,
depthWrite: false,
flatShading: true
});
const glass = new THREE.Mesh(
new THREE.CylinderGeometry(0.52, 0.48, 1.42, 12, 1, true),
glassMaterial
);
glass.position.y = 0.78;
glass.castShadow = true;
jug.add(glass);
addCylinder(
jug,
0.49,
0.49,
0.08,
12,
0,
0.08,
0,
"#b8d4cb",
{ transparent: true, opacity: 0.55, depthWrite: false }
);
const fluid = new THREE.Mesh(
new THREE.CylinderGeometry(0.43, 0.43, 1, 12),
standardMaterial("#e4ad3d", {
transparent: true,
opacity: 0.82,
depthWrite: false
})
);
fluid.position.y = 0.11;
fluid.scale.y = 0.001;
jug.add(fluid);
const handle = new THREE.Mesh(
new THREE.TorusGeometry(0.34, 0.055, 6, 12, Math.PI * 1.55),
glassMaterial
);
handle.position.set(0.5, 0.87, 0);
handle.rotation.z = Math.PI / 2;
jug.add(handle);
for (let i = 0; i < 3; i++) {
addBox(
jug,
0.18,
0.025,
0.025,
0.16,
0.38 + i * 0.33,
0.49,
"#426e5a"
);
}
visual.chunks = [];
for (let i = 0; i < 3; i++) {
const chunk = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.13),
standardMaterial("#efbd4c")
);
chunk.position.set(
-0.15 + i * 0.15,
0.2 + i * 0.28,
0.02
);
chunk.visible = false;
jug.add(chunk);
visual.chunks.push(chunk);
}
jug.userData.action = {
type: "measure",
measure: "volume",
node: jug
};
visual.fluid = fluid;
visual.label = createToolLabel(
group,
1.35,
2.75,
0.7,
stall.color,
"Measuring jug",
"0 / 3 litres"
);
visual.beacon.position.set(1.35, 3.28, 0.7);
}
stall.group = group;
stall.visual = visual;
updateStallVisual(stall);
}
stalls.forEach(buildStall);
function updateStallVisual(stall) {
const visual = stall.visual;
if (stall.id === "apple") {
visual.apples.forEach((apple, index) => {
const used = index < stall.measured;
apple.visible = !used;
apple.userData.used = used;
});
visual.label.update(
stall.completed ? "Counting tray · complete" : "Counting tray",
`${stall.measured} / ${stall.target} pieces`
);
}
if (stall.id === "tomato") {
visual.parts.forEach((part, index) => {
part.visible = index < stall.measured;
});
visual.label.update(
stall.completed ? "Balance scale · complete" : "Balance scale",
`${stall.measured} / ${stall.target} kg`
);
}
if (stall.id === "gourd") {
const fraction = Math.max(stall.measured / stall.target, 0.001);
visual.fluid.scale.y = fraction;
visual.fluid.position.y = 0.1 + fraction * 0.5;
visual.chunks.forEach((chunk, index) => {
chunk.visible = index < stall.measured;
});
visual.label.update(
stall.completed ? "Measuring jug · complete" : "Measuring jug",
`${stall.measured} / ${stall.target} litres`
);
}
}
// -------------------------------------------------------------------------
// UI rendering
// -------------------------------------------------------------------------
function selectedStall() {
return stalls.find((stall) => stall.id === app.selectedId);
}
function renderNav() {
marketNav.innerHTML = `
<div class="nav-heading">
<p class="panel-kicker">Order board</p>
<span class="tiny-label">3 orders</span>
</div>
<div class="order-list">
${stalls.map((stall) => {
const status = stall.completed
? `Paid · ${stall.rate * stall.target}`
: stall.basisCorrect
? "In progress"
: "Ready";
return `
<button
class="order-card ${stall.id === app.selectedId ? "active" : ""} ${stall.completed ? "paid" : ""}"
style="--card-color:${stall.color}"
data-stall="${stall.id}"
>
<div class="order-top">
<span class="order-number">ORDER ${stall.order}</span>
<span class="status-chip">${status}</span>
</div>
<span class="order-name">${stall.name}</span>
<div class="order-bottom">
<span class="order-detail">
${stall.target} ${stall.unitPlural}<br>
${stall.rate} coins / ${stall.unit}
</span>
<span class="basis-chip">${stall.basis}</span>
</div>
</button>
`;
}).join("")}
</div>
<button class="market-view" id="marketView">↗ Market overview</button>
<p class="fiction-note">
Produce, quantities, and prices are fictional. Select a card or a stall
to begin.
</p>
`;
marketNav.querySelectorAll("[data-stall]").forEach((button) => {
button.addEventListener("click", () => {
selectStall(button.dataset.stall, true);
});
});
document.getElementById("marketView").addEventListener("click", () => {
focusMarket();
});
}
function basisButton(stall, basis, title, subtitle, icon) {
const selected = stall.basisCorrect && stall.basis === basis;
return `
<button
class="basis-button ${selected ? "selected" : ""}"
data-basis="${basis}"
aria-pressed="${selected}"
${stall.basisCorrect && !selected ? "disabled" : ""}
>
<span class="basis-icon">${icon}</span>
<strong>${title}</strong>
<small>${subtitle}</small>
</button>
`;
}
function renderTask() {
const stall = selectedStall();
if (!stall) {
taskPanel.innerHTML = `
<p class="panel-kicker">Market brief</p>
<h2>Choose a stall.</h2>
<p class="section-copy">
Each stall uses a different measurement basis. Identify it on the
order slip, then use the matching tool in the 3D market.
</p>
`;
return;
}
const total = stall.rate * stall.target;
if (stall.completed) {
taskPanel.innerHTML = `
<div class="task-top">
<div>
<p class="panel-kicker">Paid receipt</p>
<h2>${stall.name}</h2>
</div>
<div class="order-number-badge" style="--stall-color:${stall.color}">
${stall.order}
</div>
</div>
<p class="stall-subtitle">${stall.subtitle}</p>
<div class="receipt-stamp">
<strong>PAID ✓</strong>
<span>${total} market coins received</span>
</div>
<div class="receipt">
<div class="receipt-row">
<span>Measured quantity</span>
<strong>${stall.target} ${stall.unitPlural}</strong>
</div>
<div class="receipt-row">
<span>Selling basis</span>
<strong>${stall.basisText}</strong>
</div>
<div class="receipt-row">
<span>Rate</span>
<strong>${stall.rate} coins / ${stall.unit}</strong>
</div>
<div class="receipt-row receipt-total">
<span>Total</span>
<strong>${total} coins</strong>
</div>
</div>
<button class="primary-button" id="nextOrder">
${
stalls.some((entry) => !entry.completed)
? "Next unfinished order →"
: "View the finished market"
}
</button>
`;
document.getElementById("nextOrder").addEventListener("click", () => {
const next = stalls.find((entry) => !entry.completed);
if (next) {
selectStall(next.id, true);
} else {
focusMarket();
}
});
return;
}
const progress = (stall.measured / stall.target) * 100;
const toolIcon =
stall.basis === "item" ? "1·2·3" : stall.basis === "mass" ? "kg" : "L";
taskPanel.innerHTML = `
<div class="task-top">
<div>
<p class="panel-kicker">Current order</p>
<h2>${stall.name}</h2>
</div>
<div class="order-number-badge" style="--stall-color:${stall.color}">
${stall.order}
</div>
</div>
<p class="stall-subtitle">${stall.subtitle}</p>
<div class="receipt">
<div class="receipt-row">
<span>Please collect</span>
<strong>${stall.target} ${stall.unitPlural}</strong>
</div>
<div class="receipt-row">
<span>Fictional rate</span>
<strong>${stall.rate} coins / ${stall.unit}</strong>
</div>
<div class="receipt-row receipt-total">
<span>Order total</span>
<strong>${total} coins</strong>
</div>
</div>
<section class="section">
<h3 class="section-title">
<span class="step-number">1</span>
Identify the selling basis
</h3>
<p class="section-copy">
The stall sign names the basis. Choose the matching measurement below.
</p>
<div class="basis-grid">
${basisButton(stall, "item", "By item", "Count pieces", "●")}
${basisButton(stall, "mass", "By mass", "Weigh in kg", "kg")}
${basisButton(stall, "volume", "By volume", "Measure in L", "L")}
</div>
${
stall.feedback
? `<div class="feedback ${stall.basisCorrect ? "correct-feedback" : ""}">
${stall.feedback}
</div>`
: ""
}
</section>
${
stall.basisCorrect
? `
<section class="section">
<h3 class="section-title">
<span class="step-number">2</span>
Use the visible tool
</h3>
<div class="tool-card">
<div class="tool-top">
<div class="tool-symbol">${toolIcon}</div>
<div>
<p class="tool-title">${stall.tool}</p>
<p class="tool-count">
${stall.measured} / ${stall.target} ${stall.unitPlural}
</p>
</div>
</div>
<div class="meter">
<div id="meterFill" style="width:${progress}%"></div>
</div>
<p class="tool-hint">${stall.toolHint}</p>
</div>
<button class="focus-button" id="focusTool">
Focus the ${stall.tool.toLowerCase()}
</button>
<button
class="primary-button"
id="completeOrder"
${stall.measured < stall.target ? "disabled" : ""}
>
${
stall.measured < stall.target
? "Measure the order to continue"
: `Complete order · ${total} coins`
}
</button>
</section>
`
: ""
}
`;
taskPanel.querySelectorAll("[data-basis]").forEach((button) => {
button.addEventListener("click", () => {
chooseBasis(stall, button.dataset.basis);
});
});
if (stall.basisCorrect) {
document.getElementById("focusTool").addEventListener("click", () => {
focusStall(stall);
});
document.getElementById("completeOrder").addEventListener("click", () => {
completeOrder(stall);
});
}
}
function chooseBasis(stall, basis) {
if (stall.basisCorrect || stall.completed) return;
if (basis === stall.basis) {
stall.basisCorrect = true;
stall.feedback = `Correct. This stall sells ${stall.basisText.toLowerCase()}.`;
updateStallVisual(stall);
renderTask();
focusStall(stall);
showToast(
`Correct — use the ${stall.tool.toLowerCase()} to measure the order.`
);
} else {
stall.feedback =
`Not quite. Read the sign: this stall says “${stall.basisText}.” Try again.`;
renderTask();
taskPanel.classList.remove("shake");
void taskPanel.offsetWidth;
taskPanel.classList.add("shake");
showToast("That is the wrong measurement basis.", true);
}
}
function completeOrder(stall) {
if (
stall.completed ||
!stall.basisCorrect ||
stall.measured !== stall.target
) {
return;
}
stall.completed = true;
app.wallet += stall.rate * stall.target;
updateStallVisual(stall);
updateWallet();
renderNav();
renderTask();
launchCoins(stall);
showToast(
`Order complete! ${stall.rate * stall.target} coins added to your wallet.`
);
}
function updateWallet() {
walletValue.textContent = app.wallet;
}
function selectStall(id, shouldFocus = true) {
app.selectedId = id;
renderNav();
renderTask();
if (shouldFocus) {
focusStall(selectedStall());
}
}
// -------------------------------------------------------------------------
// Camera focus and interaction
// -------------------------------------------------------------------------
function focusStall(stall) {
const mobile = innerWidth < 780;
cameraGoal = {
target: new THREE.Vector3(
stall.x,
mobile ? 0.7 : 1.45,
mobile ? 0.55 : 0.45
),
yaw: 0.28,
pitch: mobile ? 0.43 : 0.46,
radius: mobile ? 14.5 : 12.8
};
}
function focusMarket() {
cameraGoal = {
target: new THREE.Vector3(0, 1.2, 0),
yaw: 0.32,
pitch: 0.49,
radius: innerWidth < 780 ? 31 : 29
};
}
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function findAction(object) {
let current = object;
while (current) {
if (current.userData?.action) {
return current.userData.action;
}
current = current.parent;
}
return null;
}
function findStallId(object) {
let current = object;
while (current) {
if (current.userData?.stallId) {
return current.userData.stallId;
}
current = current.parent;
}
return null;
}
function pickAt(clientX, clientY) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(scene.children, true);
for (const hit of hits) {
if (hit.object.isSprite) continue;
const action = findAction(hit.object);
if (
action?.measure === "count" &&
action.node.userData.used
) {
continue;
}
const stallId = findStallId(hit.object);
if (action || stallId) {
return { hit, action, stallId };
}
}
return null;
}
let drag = null;
renderer.domElement.addEventListener("pointerdown", (event) => {
drag = {
x: event.clientX,
y: event.clientY,
lastX: event.clientX,
lastY: event.clientY,
moved: false
};
cameraGoal = null;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (drag) {
const dx = event.clientX - drag.lastX;
const dy = event.clientY - drag.lastY;
if (
Math.abs(event.clientX - drag.x) +
Math.abs(event.clientY - drag.y) >
4
) {
drag.moved = true;
}
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.005,
0.22,
1.05
);
drag.lastX = event.clientX;
drag.lastY = event.clientY;
} else {
const result = pickAt(event.clientX, event.clientY);
renderer.domElement.style.cursor =
result?.action || result?.stallId ? "pointer" : "grab";
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
const wasDrag = drag?.moved;
drag = null;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
if (!wasDrag) {
const result = pickAt(event.clientX, event.clientY);
if (result) {
handleSceneInteraction(result);
}
}
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener("pointercancel", () => {
drag = null;
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraGoal = null;
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.016,
8,
40
);
},
{ passive: false }
);
function handleSceneInteraction(result) {
const { action, stallId } = result;
if (action?.type === "measure") {
const stall = stalls.find((entry) => entry.id === stallId);
if (!stall) return;
if (app.selectedId !== stall.id) {
selectStall(stall.id, true);
showToast(
`Select the selling basis for ${stall.name} before using its tool.`
);
return;
}
if (!stall.basisCorrect) {
showToast(
"First identify the selling basis on the order slip.",
true
);
return;
}
if (stall.completed) {
showToast("This order is already paid.");
return;
}
if (stall.measured >= stall.target) {
showToast("The order is measured. Complete it on the order slip.");
return;
}
stall.measured += 1;
updateStallVisual(stall);
renderTask();
if (stall.measured === stall.target) {
showToast(
`${stall.target} ${stall.unitPlural} measured. Complete the order.`
);
}
return;
}
if (stallId) {
selectStall(stallId, true);
}
}
// -------------------------------------------------------------------------
// Completion effects
// -------------------------------------------------------------------------
const coinParticles = [];
const coinGeometry = new THREE.CylinderGeometry(0.12, 0.12, 0.035, 12);
const coinMaterial = new THREE.MeshStandardMaterial({
color: "#f3c656",
emissive: "#775011",
emissiveIntensity: 0.28,
metalness: 0.65,
roughness: 0.35,
flatShading: true
});
function launchCoins(stall) {
for (let i = 0; i < 16; i++) {
const coin = new THREE.Mesh(coinGeometry, coinMaterial);
coin.position.set(
stall.x + (Math.random() - 0.5) * 1.2,
2.5 + Math.random() * 0.7,
1.1 + (Math.random() - 0.5) * 0.5
);
coin.rotation.x = Math.PI / 2;
scene.add(coin);
coinParticles.push({
mesh: coin,
velocity: new THREE.Vector3(
(Math.random() - 0.5) * 3.2,
3.4 + Math.random() * 2.4,
(Math.random() - 0.5) * 1.8
),
life: 2.5
});
}
}
// -------------------------------------------------------------------------
// Notifications and resize
// -------------------------------------------------------------------------
let toastTimer = null;
function showToast(message, error = false) {
clearTimeout(toastTimer);
toastElement.textContent = message;
toastElement.classList.toggle("error", error);
toastElement.classList.add("show");
toastTimer = setTimeout(() => {
toastElement.classList.remove("show");
}, 2900);
}
function onResize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
}
window.addEventListener("resize", onResize);
// -------------------------------------------------------------------------
// Initial UI and animation loop
// -------------------------------------------------------------------------
renderNav();
renderTask();
updateWallet();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
if (cameraGoal) {
cameraState.target.lerp(cameraGoal.target, 0.085);
cameraState.yaw += (cameraGoal.yaw - cameraState.yaw) * 0.085;
cameraState.pitch +=
(cameraGoal.pitch - cameraState.pitch) * 0.085;
cameraState.radius +=
(cameraGoal.radius - cameraState.radius) * 0.085;
if (
cameraState.target.distanceTo(cameraGoal.target) < 0.015 &&
Math.abs(cameraState.radius - cameraGoal.radius) < 0.02
) {
cameraGoal = null;
}
}
applyCamera();
for (const stall of stalls) {
const beacon = stall.visual.beacon;
beacon.rotation.y = elapsed * 1.7;
beacon.rotation.x = elapsed * 0.9;
const selected = stall.id === app.selectedId;
const pulse = selected
? 1.12 + Math.sin(elapsed * 4.2) * 0.15
: 0.76 + Math.sin(elapsed * 2.6 + stall.x) * 0.06;
beacon.scale.setScalar(pulse);
beacon.position.y =
stall.visual.beaconBaseY + Math.sin(elapsed * 2.5 + stall.x) * 0.08;
}
for (let i = coinParticles.length - 1; i >= 0; i--) {
const particle = coinParticles[i];
particle.life -= 0.016;
particle.velocity.y -= 7.5 * 0.016;
particle.mesh.position.addScaledVector(particle.velocity, 0.016);
particle.mesh.rotation.z += 0.09;
particle.mesh.rotation.y += 0.055;
if (particle.life <= 0) {
scene.remove(particle.mesh);
coinParticles.splice(i, 1);
}
}
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```