Prompt specification
Build or customize the Three.js 3D scene "Sell First Buy Later Stall" 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: Sell First Buy Later Stall
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/sell-first-buy-later-stall-x3162
Scene Description Prompt:
Animate a voxel craftsperson selling an item for tokens, then buying food; synchronize goods, tokens, and inventory. Three.js from cdnjs, one HTML file.
Working Three.js Starter (sell-first-buy-later-stall-x3162):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Marketplace Exchange</title>
<style>
:root {
--ink: #18323b;
--muted: #597078;
--panel: rgba(255, 255, 255, 0.9);
--line: rgba(24, 50, 59, 0.12);
--gold: #f4b942;
--green: #2aa879;
--red: #e56558;
--blue: #4588d8;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #cce9ec;
color: var(--ink);
font-family:
Inter, ui-rounded, "SF Pro Rounded", system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 5;
}
.brand {
position: absolute;
top: 22px;
left: 24px;
display: flex;
align-items: center;
gap: 12px;
filter: drop-shadow(0 2px 8px rgba(24, 50, 59, 0.12));
}
.brand-mark {
position: relative;
width: 42px;
height: 42px;
border-radius: 13px;
background: var(--ink);
box-shadow: 0 8px 20px rgba(24, 50, 59, 0.2);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
width: 9px;
height: 9px;
background: var(--gold);
box-shadow: 13px 0 var(--gold), 0 13px #e8f1ed, 13px 13px #e8f1ed;
top: 10px;
left: 10px;
}
.brand-mark::after {
width: 5px;
height: 5px;
top: 15px;
left: 15px;
background: #ffffff;
box-shadow: none;
}
.brand h1 {
margin: 0;
font-size: 19px;
letter-spacing: -0.03em;
}
.brand p {
margin: 3px 0 0;
color: var(--muted);
font-size: 12px;
font-weight: 650;
}
.panel {
background: var(--panel);
border: 1px solid rgba(255, 255, 255, 0.8);
box-shadow:
0 18px 50px rgba(38, 70, 76, 0.14),
inset 0 0 0 1px var(--line);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.status {
position: absolute;
top: 22px;
right: 22px;
width: 292px;
padding: 17px;
border-radius: 22px;
}
.status-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 15px;
}
.eyebrow {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.live {
display: inline-flex;
align-items: center;
gap: 6px;
color: var(--green);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.live::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 4px rgba(42, 168, 121, 0.13);
animation: livePulse 1.5s ease-in-out infinite;
}
@keyframes livePulse {
50% {
box-shadow: 0 0 0 7px rgba(42, 168, 121, 0.03);
}
}
.wallet {
display: flex;
align-items: center;
gap: 13px;
padding: 13px 14px;
border-radius: 16px;
background: linear-gradient(135deg, #fff4c9, #ffe7a0);
border: 1px solid rgba(196, 139, 30, 0.14);
}
.coin-icon {
position: relative;
width: 34px;
height: 34px;
flex: 0 0 auto;
border: 5px solid #d99518;
background: var(--gold);
box-shadow:
inset 0 0 0 3px #ffe38a,
0 3px 0 #b87912;
transform: rotate(-7deg);
}
.coin-icon::after {
content: "";
position: absolute;
width: 5px;
height: 12px;
left: 10px;
top: 6px;
background: #d99518;
}
.wallet strong {
display: block;
font-size: 27px;
line-height: 1;
letter-spacing: -0.04em;
}
.wallet span {
display: block;
margin-top: 4px;
color: #886821;
font-size: 11px;
font-weight: 750;
}
.inventory-title {
margin: 18px 0 9px;
}
.inventory {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
}
.item {
min-width: 0;
padding: 11px 8px 9px;
border-radius: 14px;
background: rgba(244, 248, 246, 0.9);
border: 1px solid var(--line);
text-align: center;
}
.item-name {
display: block;
overflow: hidden;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.04em;
text-overflow: ellipsis;
text-transform: uppercase;
white-space: nowrap;
}
.item-count {
display: block;
margin-top: 3px;
font-size: 20px;
font-weight: 850;
}
.item-visual {
position: relative;
width: 28px;
height: 28px;
margin: 0 auto 7px;
}
.mini-lantern {
position: absolute;
left: 7px;
top: 5px;
width: 15px;
height: 17px;
background: #62b4ef;
border: 3px solid #294e69;
box-shadow: 0 0 10px rgba(69, 136, 216, 0.5);
}
.mini-lantern::before {
content: "";
position: absolute;
left: 2px;
top: -7px;
width: 6px;
height: 5px;
border: 3px solid #294e69;
border-bottom: 0;
}
.mini-food {
position: absolute;
left: 4px;
top: 9px;
width: 21px;
height: 13px;
border-radius: 3px 3px 5px 5px;
background: #d99042;
border-bottom: 4px solid #a8612d;
box-shadow: inset 0 4px #efc16d;
}
.mini-food::before {
content: "";
position: absolute;
left: 7px;
top: -4px;
width: 9px;
height: 6px;
border-radius: 4px 4px 0 0;
background: #e76550;
}
.mini-empty {
position: absolute;
inset: 5px;
border: 2px dashed #aab9b8;
border-radius: 5px;
}
.count-pulse {
animation: countPulse 0.42s ease;
}
@keyframes countPulse {
0% { transform: scale(1); }
45% { transform: scale(1.38); color: var(--green); }
100% { transform: scale(1); }
}
.story {
position: absolute;
bottom: 22px;
left: 22px;
width: 350px;
padding: 17px;
border-radius: 22px;
}
.phase-row {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 14px;
}
.phase-row h2 {
margin: 5px 0 0;
font-size: 18px;
letter-spacing: -0.03em;
}
.phase-row p {
max-width: 210px;
margin: 6px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.step-badge {
display: grid;
width: 38px;
height: 38px;
flex: 0 0 auto;
place-items: center;
border-radius: 12px;
background: #e9f3ee;
color: var(--green);
font-size: 13px;
font-weight: 850;
}
.progress-track {
position: relative;
height: 7px;
margin-top: 16px;
overflow: hidden;
border-radius: 99px;
background: #e7eeeb;
}
.progress-fill {
position: absolute;
inset: 0 auto 0 0;
width: 0;
border-radius: inherit;
background: linear-gradient(90deg, #42b98d, #f2bd4d);
transition: width 0.08s linear;
}
.phases {
display: flex;
justify-content: space-between;
margin-top: 8px;
color: #8a9999;
font-size: 9px;
font-weight: 750;
}
.phases span.active {
color: var(--green);
}
.ledger {
display: flex;
flex-direction: column;
gap: 7px;
margin: 15px 0 0;
padding: 13px 0 0;
border-top: 1px solid var(--line);
list-style: none;
}
.ledger li {
position: relative;
padding-left: 16px;
color: var(--muted);
font-size: 10px;
line-height: 1.4;
}
.ledger li::before {
content: "";
position: absolute;
left: 0;
top: 5px;
width: 6px;
height: 6px;
border-radius: 2px;
background: var(--green);
}
.controls {
position: absolute;
bottom: 22px;
left: 50%;
display: flex;
gap: 8px;
padding: 7px;
border-radius: 17px;
transform: translateX(-50%);
pointer-events: auto;
}
button {
min-height: 39px;
padding: 0 15px;
border: 0;
border-radius: 12px;
background: transparent;
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 800;
cursor: pointer;
transition:
transform 0.15s ease,
background 0.15s ease;
}
button:hover {
background: rgba(31, 91, 76, 0.08);
transform: translateY(-1px);
}
button.primary {
background: var(--ink);
color: white;
box-shadow: 0 7px 16px rgba(24, 50, 59, 0.2);
}
button.primary:hover {
background: #254851;
}
#speedBtn {
min-width: 54px;
background: rgba(31, 91, 76, 0.08);
}
.toast {
position: absolute;
top: 88px;
left: 50%;
padding: 10px 15px;
border-radius: 12px;
background: rgba(24, 50, 59, 0.9);
color: white;
font-size: 11px;
font-weight: 750;
opacity: 0;
transform: translate(-50%, -8px);
transition:
opacity 0.2s ease,
transform 0.2s ease;
}
.toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
.loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #cce9ec;
color: var(--ink);
font-size: 13px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
transition: opacity 0.4s ease;
}
.loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
.brand {
top: 12px;
left: 12px;
}
.brand h1 {
font-size: 15px;
}
.brand p {
display: none;
}
.brand-mark {
width: 36px;
height: 36px;
border-radius: 11px;
}
.status {
top: 58px;
right: 12px;
width: 218px;
padding: 13px;
border-radius: 18px;
}
.wallet {
padding: 10px 11px;
}
.wallet strong {
font-size: 22px;
}
.inventory-title {
margin-top: 13px;
}
.story {
right: 12px;
bottom: 70px;
left: 12px;
width: auto;
padding: 13px;
border-radius: 18px;
}
.phase-row p,
.ledger {
display: none;
}
.controls {
bottom: 12px;
}
button {
min-height: 36px;
padding: 0 12px;
}
.toast {
top: 62px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Animated voxel marketplace"></canvas>
<div class="hud">
<header class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<h1>Little Loop Market</h1>
<p>A synchronized voxel trade story</p>
</div>
</header>
<aside class="status panel">
<div class="status-head">
<div>
<div class="eyebrow">Craftsperson</div>
<div class="eyebrow" style="margin-top: 4px; color: var(--ink)">Mika</div>
</div>
<div class="live">Live trade</div>
</div>
<div class="wallet">
<div class="coin-icon" aria-hidden="true"></div>
<div>
<strong id="tokenCount">0</strong>
<span>market tokens</span>
</div>
</div>
<div class="inventory-title eyebrow">Inventory</div>
<div class="inventory">
<div class="item">
<div class="item-visual">
<div class="mini-lantern" id="lanternVisual"></div>
</div>
<span class="item-name">Lantern</span>
<strong class="item-count" id="craftedCount">1</strong>
</div>
<div class="item">
<div class="item-visual">
<div class="mini-empty" id="foodVisual"></div>
</div>
<span class="item-name">Food</span>
<strong class="item-count" id="foodCount">0</strong>
</div>
<div class="item">
<div class="item-visual">
<div class="mini-food" id="foodIcon" style="display: none"></div>
<div class="mini-empty" id="emptyFoodIcon"></div>
</div>
<span class="item-name">Ration</span>
<strong class="item-count" id="rationCount">0</strong>
</div>
</div>
</aside>
<section class="story panel">
<div class="phase-row">
<div>
<div class="eyebrow">Current exchange</div>
<h2 id="phaseTitle">Preparing a lantern</h2>
<p id="phaseText">Mika is about to offer a freshly made voxel lantern.</p>
</div>
<div class="step-badge" id="stepBadge">1/5</div>
</div>
<div class="progress-track">
<div class="progress-fill" id="progressFill"></div>
</div>
<div class="phases">
<span class="active">Offer</span>
<span>Sell</span>
<span>Travel</span>
<span>Pay</span>
<span>Food</span>
</div>
<ul class="ledger" id="ledger">
<li>1 crafted lantern ready to trade</li>
</ul>
</section>
<nav class="controls panel" aria-label="Animation controls">
<button id="pauseBtn" class="primary" type="button">Pause</button>
<button id="replayBtn" type="button">Replay</button>
<button id="speedBtn" type="button">1×</button>
</nav>
<div class="toast" id="toast" role="status"></div>
</div>
<div class="loading" id="loading">Building the little market…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.background = new THREE.Color("#cce9ec");
scene.fog = new THREE.Fog("#cce9ec", 19, 38);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
70
);
const hemi = new THREE.HemisphereLight("#eafcff", "#7b7657", 2.25);
scene.add(hemi);
const sun = new THREE.DirectionalLight("#fff2cf", 3.3);
sun.position.set(-6, 12, 8);
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 = 32;
sun.shadow.bias = -0.0008;
scene.add(sun);
const fill = new THREE.DirectionalLight("#b9ddff", 0.75);
fill.position.set(8, 5, -5);
scene.add(fill);
const materialCache = new Map();
function material(color, options = {}) {
const key = color + JSON.stringify(options);
if (materialCache.has(key)) return materialCache.get(key);
const m = new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.02,
...options
});
materialCache.set(key, m);
return m;
}
function box(parent, size, position, color, options = {}) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material(color, options)
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeTextSprite(text, background = "#18323b", foreground = "#ffffff") {
const c = document.createElement("canvas");
c.width = 512;
c.height = 128;
const ctx = c.getContext("2d");
ctx.fillStyle = background;
ctx.fillRect(0, 0, c.width, c.height);
ctx.strokeStyle = "rgba(255,255,255,.25)";
ctx.lineWidth = 5;
ctx.strokeRect(7, 7, c.width - 14, c.height - 14);
ctx.fillStyle = foreground;
ctx.font = "800 49px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, c.width / 2, c.height / 2 + 2);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
})
);
sprite.scale.set(2.65, 0.66, 1);
return sprite;
}
function addVoxelTree(x, z, scale = 1) {
const tree = new THREE.Group();
tree.position.set(x, 0, z);
tree.scale.setScalar(scale);
scene.add(tree);
box(tree, [0.55, 2.4, 0.55], [0, 1.2, 0], "#78523a");
box(tree, [2.4, 0.85, 1.6], [0, 2.25, 0], "#397f62");
box(tree, [1.45, 0.9, 1.8], [-0.55, 3.0, 0.05], "#4b9a70");
box(tree, [1.55, 0.85, 1.4], [0.65, 3.05, -0.05], "#64ad79");
box(tree, [0.85, 0.65, 0.85], [0.15, 3.65, 0], "#73ba7c");
}
function makeVoxelHouse(x, z, wallColor, roofColor, width = 3) {
const house = new THREE.Group();
house.position.set(x, 0, z);
scene.add(house);
box(house, [width, 2.3, 2.2], [0, 1.15, 0], wallColor);
box(house, [width + 0.25, 0.3, 2.45], [0, 2.35, 0], "#f2d6a5");
box(house, [width + 0.35, 0.28, 1.55], [0, 2.85, -0.1], roofColor);
box(house, [0.55, 1.25, 0.1], [-0.45, 0.65, 1.14], "#694b39");
box(house, [0.7, 0.65, 0.1], [0.7, 1.25, 1.14], "#95d8e5");
box(house, [0.6, 0.45, 0.1], [0.7, 1.25, 1.21], "#d7f5f5");
}
function makeStall(x, title, accentColor, stallType) {
const group = new THREE.Group();
group.position.set(x, 0, -1.35);
scene.add(group);
box(group, [3.65, 0.92, 1.08], [0, 0.5, 0.43], "#8a5a3b");
box(group, [3.82, 0.15, 1.24], [0, 1.03, 0.43], "#c48751");
box(group, [3.4, 0.16, 0.65], [0, 1.55, -0.85], "#ad7144");
box(group, [3.4, 0.12, 0.12], [0, 1.78, -0.85], "#754b34");
for (const px of [-1.65, 1.65]) {
box(group, [0.16, 3.1, 0.16], [px, 1.55, -0.82], "#6e4933");
box(group, [0.16, 2.65, 0.16], [px, 1.32, 0.92], "#6e4933");
}
const stripeWidth = 0.48;
for (let i = 0; i < 8; i++) {
const color = i % 2 === 0 ? accentColor : "#fff1d5";
box(
group,
[stripeWidth, 0.19, 2.05],
[(i - 3.5) * stripeWidth, 3.03, 0.05],
color
);
}
box(group, [3.95, 0.16, 0.2], [0, 2.87, 1.07], "#70462e");
const sign = makeTextSprite(title, stallType === "craft" ? "#284f67" : "#8d493d");
sign.position.set(0, 3.62, 0.1);
sign.scale.set(2.55, 0.64, 1);
group.add(sign);
const lanternLight = new THREE.PointLight("#ffd178", 0.65, 4, 2);
lanternLight.position.set(0, 2.55, 0.45);
group.add(lanternLight);
if (stallType === "craft") {
box(group, [0.32, 0.35, 0.32], [-1.05, 1.27, 0.35], "#4b7a92");
box(group, [0.22, 0.55, 0.22], [-0.25, 1.37, 0.22], "#8c5c3f");
box(group, [0.45, 0.28, 0.38], [0.65, 1.24, 0.3], "#c76e4b");
box(group, [0.32, 0.23, 0.32], [1.15, 1.23, 0.3], "#e1b45f");
} else {
box(group, [0.55, 0.32, 0.5], [-0.8, 1.26, 0.3], "#c57842");
box(group, [0.48, 0.4, 0.48], [0, 1.3, 0.25], "#dd9a50");
box(group, [0.6, 0.27, 0.45], [0.82, 1.24, 0.35], "#b8663d");
}
return group;
}
function makePerson({
shirt,
pants,
skin,
hair,
accent,
hat = false,
scarf = false
}) {
const root = new THREE.Group();
const torso = new THREE.Group();
root.add(torso);
box(torso, [0.72, 0.78, 0.39], [0, 1.08, 0], shirt);
box(torso, [0.58, 0.58, 0.08], [0, 0.99, 0.225], accent);
const head = new THREE.Group();
head.position.set(0, 1.75, 0);
root.add(head);
box(head, [0.58, 0.56, 0.51], [0, 0, 0], skin);
box(head, [0.61, 0.17, 0.54], [0, 0.25, -0.015], hair);
box(head, [0.12, 0.13, 0.04], [-0.13, 0.03, 0.272], "#26343a");
box(head, [0.12, 0.13, 0.04], [0.13, 0.03, 0.272], "#26343a");
box(head, [0.16, 0.05, 0.04], [0, -0.13, 0.27], "#9a5e4f");
if (hat) {
box(head, [0.68, 0.12, 0.62], [0, 0.33, 0], "#79533d");
box(head, [0.48, 0.25, 0.45], [0, 0.48, -0.02], "#93654a");
}
if (scarf) {
box(torso, [0.76, 0.18, 0.46], [0, 1.43, 0], "#f16f51");
box(torso, [0.18, 0.42, 0.08], [0.22, 1.2, 0.28], "#f16f51");
}
const legs = [];
for (const side of [-1, 1]) {
const leg = new THREE.Group();
leg.position.set(side * 0.18, 0.72, 0);
root.add(leg);
box(leg, [0.25, 0.62, 0.27], [0, -0.31, 0], pants);
box(leg, [0.29, 0.16, 0.39], [0, -0.61, 0.06], "#3e3d38");
legs.push(leg);
}
const arms = [];
for (const side of [-1, 1]) {
const arm = new THREE.Group();
arm.position.set(side * 0.48, 1.38, 0);
root.add(arm);
box(arm, [0.22, 0.63, 0.25], [0, -0.3, 0], shirt);
box(arm, [0.24, 0.2, 0.25], [0, -0.66, 0.02], skin);
const hand = new THREE.Group();
hand.position.set(0, -0.72, 0.035);
arm.add(hand);
arms.push({ arm, hand });
}
scene.add(root);
return {
root,
head,
torso,
leftLeg: legs[0],
rightLeg: legs[1],
leftArm: arms[0].arm,
rightArm: arms[1].arm,
rightHand: arms[1].hand,
leftHand: arms[0].hand
};
}
// Market diorama.
box(scene, [15, 0.65, 9.5], [0, -0.35, -0.3], "#a46f49");
box(scene, [15.2, 0.12, 9.7], [0, -0.015, -0.3], "#7eaa69");
box(scene, [12.6, 0.08, 1.3], [0, 0.07, 0.55], "#d9bd8b");
for (let i = 0; i < 11; i++) {
box(
scene,
[0.82, 0.06, 0.64],
[-5.6 + i * 1.12, 0.1, 0.55 + Math.sin(i) * 0.025],
i % 2 ? "#d7b983" : "#e4c997"
);
}
const craftX = -2.75;
const foodX = 2.65;
const playerStartZ = 0.55;
makeStall(craftX, "CRAFT GOODS", "#4c8da9", "craft");
makeStall(foodX, "FRESH FOOD", "#cf765e", "food");
addVoxelTree(-6.1, -3.3, 1.15);
addVoxelTree(6.0, -3.6, 1.05);
addVoxelTree(-6.6, 2.2, 0.78);
addVoxelTree(6.6, 2.5, 0.85);
makeVoxelHouse(-5.0, -5.0, "#e7c996", "#4e7480", 3.4);
makeVoxelHouse(4.8, -5.2, "#d9b58b", "#965b51", 3.8);
// Decorative crates and produce.
box(scene, [0.8, 0.75, 0.8], [-5.0, 0.38, 0.9], "#a06b43");
box(scene, [0.75, 0.7, 0.75], [-4.7, 1.1, 0.8], "#bd8351");
box(scene, [0.72, 0.68, 0.72], [5.45, 0.34, 0.7], "#9a643f");
box(scene, [0.68, 0.65, 0.68], [5.25, 0.98, 0.6], "#b97b47");
for (let i = 0; i < 7; i++) {
box(
scene,
[0.22, 0.18, 0.22],
[4.65 + (i % 3) * 0.26, 1.2, -0.55 + Math.floor(i / 3) * 0.3],
i % 2 ? "#d85c4f" : "#e5a847"
);
}
const player = makePerson({
shirt: "#4c91a8",
pants: "#435566",
skin: "#dca077",
hair: "#4a332c",
accent: "#b36e42",
scarf: true
});
const craftMerchant = makePerson({
shirt: "#87975e",
pants: "#5c5945",
skin: "#b87955",
hair: "#332722",
accent: "#6b4a34",
hat: true
});
const foodMerchant = makePerson({
shirt: "#bb765e",
pants: "#665344",
skin: "#e2aa7e",
hair: "#684332",
accent: "#f0d09b",
hat: true
});
player.root.position.set(craftX, 0.08, playerStartZ);
player.root.rotation.y = Math.PI;
craftMerchant.root.position.set(craftX, 0.08, -2.25);
foodMerchant.root.position.set(foodX, 0.08, -2.25);
function makeLantern() {
const group = new THREE.Group();
box(group, [0.32, 0.4, 0.32], [0, 0, 0], "#64c8f1", {
emissive: "#3b9ed1",
emissiveIntensity: 0.75
});
box(group, [0.43, 0.1, 0.43], [0, 0.23, 0], "#315c72");
box(group, [0.43, 0.1, 0.43], [0, -0.23, 0], "#315c72");
box(group, [0.09, 0.2, 0.09], [-0.2, 0, 0], "#315c72");
box(group, [0.09, 0.2, 0.09], [0.2, 0, 0], "#315c72");
box(group, [0.26, 0.07, 0.07], [0, 0.34, 0], "#315c72");
const sparkles = [];
for (let i = 0; i < 4; i++) {
const sparkle = box(
group,
[0.055, 0.055, 0.055],
[0, 0, 0],
"#e9fbff",
{ emissive: "#9eeaff", emissiveIntensity: 1.6 }
);
sparkle.castShadow = false;
sparkles.push(sparkle);
}
group.userData.sparkles = sparkles;
scene.add(group);
return group;
}
function makeBread() {
const group = new THREE.Group();
box(group, [0.46, 0.2, 0.3], [0, -0.04, 0], "#c8793d");
box(group, [0.34, 0.18, 0.27], [-0.08, 0.11, 0], "#e2a355");
box(group, [0.25, 0.13, 0.25], [0.12, 0.1, 0.02], "#edb767");
box(group, [0.12, 0.08, 0.12], [-0.05, 0.21, 0], "#d7654b");
scene.add(group);
return group;
}
function makeToken() {
const group = new THREE.Group();
box(group, [0.2, 0.25, 0.065], [0, 0, 0], "#e5a82e");
box(group, [0.09, 0.12, 0.075], [0, 0, 0], "#ffda67", {
emissive: "#b87512",
emissiveIntensity: 0.2
});
scene.add(group);
return group;
}
const lantern = makeLantern();
const bread = makeBread();
const craftTokens = [makeToken(), makeToken(), makeToken()];
const paymentTokens = [makeToken(), makeToken()];
const lanternCounterTarget = new THREE.Vector3(craftX, 1.22, -0.9);
const itemOffset = new THREE.Vector3(0, 0.18, 0);
const effects = [];
function addEffect(text, color, position) {
const sprite = makeTextSprite(text, color, "#ffffff");
sprite.position.copy(position);
sprite.scale.set(1.7, 0.43, 1);
sprite.material.opacity = 0;
scene.add(sprite);
effects.push({
sprite,
age: 0,
start: position.clone()
});
}
function updateEffects(dt) {
for (let i = effects.length - 1; i >= 0; i--) {
const effect = effects[i];
effect.age += dt;
const u = Math.min(effect.age / 1.4, 1);
effect.sprite.position.y = effect.start.y + u * 1.25;
effect.sprite.material.opacity =
u < 0.12 ? u / 0.12 : 1 - Math.max(0, (u - 0.45) / 0.55);
if (u >= 1) {
scene.remove(effect.sprite);
effect.sprite.material.map.dispose();
effect.sprite.material.dispose();
effects.splice(i, 1);
}
}
}
const state = {
tokens: 0,
crafted: 1,
food: 0,
merchantCrafted: 0,
merchantTokens: 3,
merchantFood: 0,
foodMerchantTokens: 0
};
const committed = {
craft: false,
tokens: false,
payment: false,
food: false
};
let timeline = 0;
let playing = true;
let speed = 1;
const duration = 11.2;
const tokenCountEl = document.querySelector("#tokenCount");
const craftedCountEl = document.querySelector("#craftedCount");
const foodCountEl = document.querySelector("#foodCount");
const rationCountEl = document.querySelector("#rationCount");
const foodVisual = document.querySelector("#foodVisual");
const foodIcon = document.querySelector("#foodIcon");
const emptyFoodIcon = document.querySelector("#emptyFoodIcon");
const phaseTitle = document.querySelector("#phaseTitle");
const phaseText = document.querySelector("#phaseText");
const stepBadge = document.querySelector("#stepBadge");
const progressFill = document.querySelector("#progressFill");
const phaseEls = [...document.querySelectorAll(".phases span")];
const ledger = document.querySelector("#ledger");
const toast = document.querySelector("#toast");
const pauseBtn = document.querySelector("#pauseBtn");
const replayBtn = document.querySelector("#replayBtn");
const speedBtn = document.querySelector("#speedBtn");
let toastTimer;
function pulse(element) {
element.classList.remove("count-pulse");
void element.offsetWidth;
element.classList.add("count-pulse");
}
function setCount(element, value) {
if (element.textContent !== String(value)) {
element.textContent = value;
pulse(element);
}
}
function updateInventory() {
setCount(tokenCountEl, state.tokens);
setCount(craftedCountEl, state.crafted);
setCount(foodCountEl, state.food);
setCount(rationCountEl, state.food);
foodVisual.style.background = state.food ? "#d99042" : "";
foodVisual.style.border = state.food ? "3px solid #8f542d" : "";
foodVisual.style.boxShadow = state.food ? "inset 0 0 0 2px #efc16d" : "";
foodIcon.style.display = state.food ? "block" : "none";
emptyFoodIcon.style.display = state.food ? "none" : "block";
}
function showToast(message) {
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 1500);
}
function addLedger(message) {
const li = document.createElement("li");
li.textContent = message;
ledger.appendChild(li);
}
function commitCraft() {
committed.craft = true;
state.crafted = 0;
state.merchantCrafted += 1;
updateInventory();
addLedger("Lantern handed over • merchant inventory +1");
addEffect("LANTERN SOLD", "#356b80", new THREE.Vector3(craftX, 2.55, -0.6));
showToast("Lantern exchanged for 3 tokens");
}
function commitTokens() {
committed.tokens = true;
state.tokens += 3;
state.merchantTokens -= 3;
updateInventory();
addLedger("Payment received • wallet +3 tokens");
addEffect("+3 TOKENS", "#287c5f", new THREE.Vector3(craftX + 0.35, 2.55, 0.35));
showToast("3 tokens added to your wallet");
}
function commitPayment() {
committed.payment = true;
state.tokens -= 2;
state.foodMerchantTokens += 2;
updateInventory();
addLedger("Paid 2 tokens to the food merchant");
addEffect("−2 TOKENS", "#a85a48", new THREE.Vector3(foodX - 0.35, 2.55, 0.35));
showToast("2 tokens paid for food");
}
function commitFood() {
committed.food = true;
state.food += 1;
state.merchantFood -= 1;
updateInventory();
addLedger("Food received • inventory +1 ration");
addEffect("+1 FOOD", "#9a673b", new THREE.Vector3(foodX - 0.25, 2.55, 0.4));
showToast("Food added to your inventory");
}
function processCommits() {
if (!committed.craft && timeline >= 3.2) commitCraft();
if (!committed.tokens && timeline >= 4.8) commitTokens();
if (!committed.payment && timeline >= 8.4) commitPayment();
if (!committed.food && timeline >= 9.8) commitFood();
}
function phaseInfo(t) {
if (t < 1.5) {
return {
title: "Preparing a lantern",
text: "Mika is about to offer a freshly made voxel lantern.",
step: 1,
index: 0
};
}
if (t < 3.2) {
return {
title: "Selling the lantern",
text: "The crafted good moves from Mika's hand to the merchant's counter.",
step: 2,
index: 1
};
}
if (t < 4.8) {
return {
title: "Receiving payment",
text: "Three golden tokens travel from the craft merchant to Mika.",
step: 2,
index: 1
};
}
if (t < 7.2) {
return {
title: "Walking to fresh food",
text: "Mika carries the newly received tokens across the market square.",
step: 3,
index: 2
};
}
if (t < 8.4) {
return {
title: "Paying for food",
text: "Two tokens are handed to the food merchant.",
step: 4,
index: 3
};
}
if (t < 9.8) {
return {
title: "Receiving food",
text: "The merchant passes a warm ration to Mika in exchange for the tokens.",
step: 5,
index: 4
};
}
return {
title: "Exchange complete",
text: "Tokens spent and food received are now synchronized in the inventory.",
step: 5,
index: 4
};
}
function updatePhaseUI(t) {
const info = phaseInfo(t);
if (phaseTitle.textContent !== info.title) {
phaseTitle.textContent = info.title;
phaseText.textContent = info.text;
stepBadge.textContent = `${info.step}/5`;
phaseEls.forEach((element, index) => {
element.classList.toggle("active", index === info.index);
});
}
progressFill.style.width = `${Math.min(t / duration, 1) * 100}%`;
}
function resetTimeline() {
timeline = 0;
playing = true;
pauseBtn.textContent = "Pause";
Object.assign(state, {
tokens: 0,
crafted: 1,
food: 0,
merchantCrafted: 0,
merchantTokens: 3,
merchantFood: 0,
foodMerchantTokens: 0
});
Object.keys(committed).forEach((key) => {
committed[key] = false;
});
player.root.position.set(craftX, 0.08, playerStartZ);
player.root.rotation.y = Math.PI;
player.leftLeg.rotation.x = 0;
player.rightLeg.rotation.x = 0;
ledger.innerHTML = "<li>1 crafted lantern ready to trade</li>";
updateInventory();
updatePhaseUI(0);
for (const effect of effects) {
scene.remove(effect.sprite);
effect.sprite.material.map.dispose();
effect.sprite.material.dispose();
}
effects.length = 0;
}
pauseBtn.addEventListener("click", () => {
if (timeline >= duration) {
resetTimeline();
return;
}
playing = !playing;
pauseBtn.textContent = playing ? "Pause" : "Resume";
});
replayBtn.addEventListener("click", resetTimeline);
speedBtn.addEventListener("click", () => {
speed = speed === 1 ? 1.5 : speed === 1.5 ? 2 : 1;
speedBtn.textContent = `${speed}×`;
});
const clamp = THREE.MathUtils.clamp;
const smooth = (u) => {
const x = clamp(u, 0, 1);
return x * x * (3 - 2 * x);
};
function handPosition(hand, target = new THREE.Vector3()) {
return hand.getWorldPosition(target);
}
function updateCharacters(t, realTime) {
player.root.position.y = 0.08;
craftMerchant.root.position.y = 0.08 + Math.sin(realTime * 2.1) * 0.015;
foodMerchant.root.position.y = 0.08 + Math.sin(realTime * 2.1 + 1.2) * 0.015;
player.head.rotation.y = Math.sin(realTime * 0.8) * 0.05;
craftMerchant.head.rotation.y = Math.sin(realTime * 0.7 + 1) * 0.05;
foodMerchant.head.rotation.y = Math.sin(realTime * 0.9 + 2) * 0.05;
craftMerchant.rightArm.rotation.x = -0.78;
foodMerchant.rightArm.rotation.x = -0.78;
player.leftArm.rotation.x = 0.05;
player.leftLeg.rotation.x = 0;
player.rightLeg.rotation.x = 0;
if (t < 1.3) {
player.rightArm.rotation.x = THREE.MathUtils.lerp(
0.05,
-1.25,
smooth(t / 1.3)
);
} else if (t < 3.25) {
player.rightArm.rotation.x = -1.25;
} else if (t < 4.95) {
player.rightArm.rotation.x = THREE.MathUtils.lerp(
-1.25,
-0.92,
smooth((t - 3.25) / 0.45)
);
} else if (t < 6.7) {
player.rightArm.rotation.x = THREE.MathUtils.lerp(
-0.92,
0.02,
smooth((t - 4.95) / 0.5)
);
} else if (t < 7.2) {
player.rightArm.rotation.x = THREE.MathUtils.lerp(
0.02,
-1.1,
smooth((t - 6.7) / 0.5)
);
} else {
player.rightArm.rotation.x = -1.1;
}
if (t >= 5 && t < 7.2) {
const u = clamp((t - 5) / 2.2, 0, 1);
const eased = smooth(u);
player.root.position.x = THREE.MathUtils.lerp(craftX, foodX, eased);
player.root.position.z =
playerStartZ + Math.sin(u * Math.PI) * 0.12;
player.root.rotation.y = Math.PI - (Math.PI / 2) * eased;
const walkStrength = Math.sin(u * Math.PI);
const stride = Math.sin(u * Math.PI * 9) * 0.58 * walkStrength;
player.leftLeg.rotation.x = stride;
player.rightLeg.rotation.x = -stride;
player.leftArm.rotation.x = -stride * 0.55;
player.root.position.y += Math.abs(Math.sin(u * Math.PI * 9)) * 0.045;
} else if (t >= 7.2) {
player.root.position.x = foodX;
player.root.position.z = playerStartZ;
player.root.rotation.y = THREE.MathUtils.lerp(
Math.PI / 2,
Math.PI,
smooth((t - 7.2) / 0.7)
);
}
}
const playerHand = new THREE.Vector3();
const merchantHand = new THREE.Vector3();
const foodMerchantHand = new THREE.Vector3();
const tokenStart = new THREE.Vector3();
const tokenEnd = new THREE.Vector3();
function updateGoods(t, realTime) {
scene.updateMatrixWorld(true);
handPosition(player.rightHand, playerHand);
handPosition(craftMerchant.rightHand, merchantHand);
handPosition(foodMerchant.rightHand, foodMerchantHand);
// The lantern is held, transferred, then remains on the craft counter.
if (t < 1.5) {
lantern.position.copy(playerHand).add(itemOffset);
lantern.rotation.y = Math.sin(realTime * 2) * 0.06;
} else if (t < 3.2) {
const u = smooth((t - 1.5) / 1.7);
lantern.position
.copy(playerHand)
.add(itemOffset)
.lerp(lanternCounterTarget, u);
lantern.position.y += Math.sin(u * Math.PI) * 0.3;
lantern.rotation.y = u * Math.PI * 0.35;
} else {
lantern.position.copy(lanternCounterTarget);
lantern.rotation.y = Math.PI * 0.35;
}
lantern.userData.sparkles.forEach((sparkle, index) => {
const angle = realTime * 1.6 + (index / 4) * Math.PI * 2;
sparkle.position.set(
Math.cos(angle) * 0.32,
Math.sin(angle * 1.4) * 0.23,
Math.sin(angle) * 0.32
);
});
// Three tokens move from the craft merchant to Mika.
craftTokens.forEach((token, index) => {
token.visible = t >= 3.2 && t < 4.8;
if (!token.visible) return;
const localDuration = 1.6 - index * 0.12;
const u = clamp((t - 3.2 - index * 0.1) / localDuration, 0, 1);
const eased = smooth(u);
tokenStart.copy(merchantHand).add(new THREE.Vector3(0, 0.1, 0));
tokenEnd.copy(playerHand).add(new THREE.Vector3(0, 0.14, 0));
tokenStart.x += (index - 1) * 0.13;
token.position.copy(tokenStart).lerp(tokenEnd, eased);
token.position.y += Math.sin(u * Math.PI) * 0.75;
token.rotation.y = u * Math.PI * 2 + index;
});
// The food is visible in the merchant's hand until the exchange.
if (t < 8.4) {
bread.position.copy(foodMerchantHand).add(itemOffset);
bread.rotation.y = Math.sin(realTime * 1.8) * 0.08;
} else if (t < 9.8) {
const u = smooth((t - 8.4) / 1.4);
bread.position
.copy(foodMerchantHand)
.add(itemOffset)
.lerp(playerHand.clone().add(itemOffset), u);
bread.position.y += Math.sin(u * Math.PI) * 0.28;
bread.rotation.y = u * Math.PI * 0.5;
} else {
bread.position.copy(playerHand).add(itemOffset);
bread.rotation.y = Math.PI * 0.5;
}
// Two tokens move from Mika to the food merchant.
paymentTokens.forEach((token, index) => {
token.visible = t >= 7.2 && t < 8.4;
if (!token.visible) return;
const localDuration = 1.2 - index * 0.1;
const u = clamp((t - 7.2 - index * 0.08) / localDuration, 0, 1);
const eased = smooth(u);
tokenStart.copy(playerHand).add(new THREE.Vector3(0, 0.12, 0));
tokenEnd.copy(foodMerchantHand).add(new THREE.Vector3(0, 0.1, 0));
tokenStart.x += (index - 0.5) * 0.13;
token.position.copy(tokenStart).lerp(tokenEnd, eased);
token.position.y += Math.sin(u * Math.PI) * 0.65;
token.rotation.y = -u * Math.PI * 2 + index;
});
}
// Direct camera controls, without add-ons.
const orbit = {
theta: 0.56,
phi: 1.02,
radius: 14.8,
target: new THREE.Vector3(-0.05, 1.05, -0.45),
dragging: false,
pointerX: 0,
pointerY: 0
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
canvas.addEventListener("pointerdown", (event) => {
orbit.dragging = true;
orbit.pointerX = event.clientX;
orbit.pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!orbit.dragging) return;
const dx = event.clientX - orbit.pointerX;
const dy = event.clientY - orbit.pointerY;
orbit.theta -= dx * 0.006;
orbit.phi = clamp(orbit.phi - dy * 0.005, 0.48, 1.35);
orbit.pointerX = event.clientX;
orbit.pointerY = event.clientY;
});
function endDrag(event) {
orbit.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();
orbit.radius = clamp(orbit.radius + event.deltaY * 0.01, 9.5, 21);
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
orbit.theta = 0.56;
orbit.phi = 1.02;
orbit.radius = 14.8;
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
renderer.setSize(window.innerWidth, window.innerHeight);
if (window.innerWidth < 760) {
orbit.radius = Math.max(orbit.radius, 16.3);
orbit.target.set(0, 1.05, -0.55);
} else {
orbit.target.set(-0.05, 1.05, -0.45);
}
});
const clock = new THREE.Clock();
let firstFrame = true;
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const realTime = clock.elapsedTime;
if (playing) {
timeline = Math.min(timeline + dt * speed, duration);
if (timeline >= duration) {
playing = false;
pauseBtn.textContent = "Replay";
}
}
processCommits();
updatePhaseUI(timeline);
updateCharacters(timeline, realTime);
updateGoods(timeline, realTime);
updateEffects(dt);
updateCamera();
renderer.render(scene, camera);
if (firstFrame) {
firstFrame = false;
document.querySelector("#loading").classList.add("hidden");
}
}
updateInventory();
updatePhaseUI(0);
updateCamera();
animate();
</script>
</body>
</html>
```