Prompt specification
Build or customize the Three.js 3D scene "Town square with diverse trade stalls" 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: Town square with diverse trade stalls
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/town-square-with-diverse-trade-stalls-x2375
Scene Description Prompt:
Build a low-poly medieval-inspired square with grain, cloth, and pottery stalls using explicitly fictional local measurement units. One HTML file with Three.js from cdnjs.
Working Three.js Starter (town-square-with-diverse-trade-stalls-x2375):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>The Fold & Grain — Medieval Market Square</title>
<style>
:root {
--ink: #2d241d;
--paper: #f3e6c8;
--paper-soft: rgba(247, 235, 207, 0.91);
--line: rgba(64, 43, 27, 0.28);
--red: #8c3f32;
--gold: #c58b3b;
--green: #526c58;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #c8d6ce;
color: var(--ink);
font-family: Georgia, "Times New Roman", serif;
}
button {
font: inherit;
}
#app,
#viewport {
position: fixed;
inset: 0;
}
#viewport {
touch-action: none;
cursor: grab;
}
#viewport.dragging {
cursor: grabbing;
}
#viewport.pickable {
cursor: pointer;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--line);
background: var(--paper-soft);
box-shadow:
0 12px 35px rgba(43, 31, 22, 0.14),
inset 0 0 0 3px rgba(255, 255, 255, 0.24);
backdrop-filter: blur(12px);
}
.brand {
top: 22px;
left: 22px;
min-width: 290px;
padding: 16px 19px 15px;
border-radius: 3px 16px 3px 3px;
}
.eyebrow {
margin-bottom: 5px;
color: var(--red);
font-family: system-ui, sans-serif;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(23px, 2.4vw, 34px);
font-weight: 600;
line-height: 1;
letter-spacing: -0.035em;
}
.subtitle {
margin-top: 8px;
color: #6d5c49;
font-size: 13px;
font-style: italic;
}
.unit-card {
top: 22px;
right: 22px;
width: 268px;
padding: 15px 17px;
border-radius: 3px 3px 15px 3px;
}
.unit-card h2 {
margin: 0 0 10px;
font-family: system-ui, sans-serif;
font-size: 10px;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.unit-row {
display: flex;
justify-content: space-between;
gap: 20px;
padding: 5px 0;
border-top: 1px solid rgba(65, 44, 28, 0.12);
font-size: 13px;
}
.unit-row strong {
white-space: nowrap;
font-weight: 700;
}
.fiction-note {
margin-top: 10px;
padding-top: 9px;
border-top: 1px solid var(--line);
color: #7c3d32;
font-family: system-ui, sans-serif;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.07em;
line-height: 1.45;
text-transform: uppercase;
}
.details {
left: 22px;
bottom: 22px;
width: 285px;
padding: 15px 17px;
border-radius: 3px 3px 15px 15px;
}
.details h2 {
margin: 0;
font-size: 21px;
font-weight: 600;
}
.details p {
margin: 8px 0 0;
color: #6a5948;
font-size: 13px;
line-height: 1.5;
}
.measurements {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin-top: 12px;
}
.measurements span {
padding: 5px 7px;
border: 1px solid rgba(67, 45, 29, 0.17);
background: rgba(255, 255, 255, 0.32);
font-family: system-ui, sans-serif;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.04em;
}
.toolbar {
position: fixed;
z-index: 15;
bottom: 22px;
left: 50%;
display: flex;
gap: 5px;
padding: 6px;
transform: translateX(-50%);
border: 1px solid var(--line);
border-radius: 14px;
background: var(--paper-soft);
box-shadow: 0 12px 35px rgba(43, 31, 22, 0.16);
backdrop-filter: blur(12px);
}
.toolbar button {
min-width: 92px;
padding: 10px 13px;
border: 0;
border-radius: 9px;
background: transparent;
color: var(--ink);
cursor: pointer;
font-family: system-ui, sans-serif;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
transition: 160ms ease;
}
.toolbar button:hover {
background: rgba(97, 73, 49, 0.09);
}
.toolbar button.active {
background: var(--ink);
color: var(--paper);
}
.toolbar button:first-child {
min-width: 80px;
}
.help {
right: 22px;
bottom: 22px;
padding: 11px 14px;
border-radius: 12px 3px 3px 3px;
color: #685746;
font-family: system-ui, sans-serif;
font-size: 10px;
font-weight: 650;
letter-spacing: 0.035em;
line-height: 1.55;
text-align: right;
}
.help b {
color: var(--ink);
}
#labels {
position: fixed;
z-index: 5;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
top: 0;
left: 0;
padding: 7px 10px;
transform: translate(-50%, -100%);
border: 1px solid rgba(255, 255, 255, 0.42);
border-radius: 3px;
background: rgba(42, 33, 26, 0.84);
box-shadow: 0 6px 18px rgba(0, 0, 0, 0.18);
color: #fff0d1;
cursor: pointer;
pointer-events: auto;
font-family: system-ui, sans-serif;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
white-space: nowrap;
transition: opacity 150ms ease, background 150ms ease;
}
.world-label::after {
position: absolute;
bottom: -6px;
left: 50%;
width: 10px;
height: 10px;
transform: translateX(-50%) rotate(45deg);
background: rgba(42, 33, 26, 0.84);
content: "";
}
.world-label:hover {
background: rgba(119, 55, 42, 0.94);
}
.world-label.hidden {
opacity: 0;
pointer-events: none;
}
#loading {
position: fixed;
z-index: 100;
inset: 0;
display: grid;
place-items: center;
background: #d7dfd5;
transition: opacity 400ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
.loading-card {
padding: 20px 28px;
border: 1px solid var(--line);
background: var(--paper);
text-align: center;
}
.loading-card strong {
display: block;
font-size: 23px;
font-weight: 500;
}
.loading-card span {
display: block;
margin-top: 7px;
color: #796654;
font-family: system-ui, sans-serif;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.14em;
text-transform: uppercase;
}
@media (max-width: 850px) {
.brand {
top: 12px;
left: 12px;
min-width: 0;
padding: 12px 14px;
}
.brand .subtitle {
display: none;
}
.unit-card {
top: 12px;
right: 12px;
width: 220px;
padding: 11px 13px;
}
.unit-row {
font-size: 11px;
}
.details {
bottom: 84px;
left: 12px;
width: 235px;
padding: 12px 14px;
}
.details p {
font-size: 11px;
}
.help {
display: none;
}
.toolbar {
bottom: 12px;
width: calc(100% - 24px);
justify-content: center;
}
.toolbar button {
min-width: 0;
flex: 1;
padding: 10px 7px;
font-size: 9px;
}
}
@media (max-width: 540px) {
.unit-card {
top: 84px;
width: 190px;
}
.unit-card .fiction-note {
font-size: 8px;
}
.details {
width: 205px;
}
.measurements {
display: none;
}
.world-label {
font-size: 8px;
}
}
</style>
</head>
<body>
<div id="app">
<div id="viewport"></div>
<div id="labels"></div>
<header class="panel brand">
<div class="eyebrow">The First Dawn Market</div>
<h1>The Fold & Grain</h1>
<div class="subtitle">A low-poly medieval town square</div>
</header>
<aside class="panel unit-card">
<h2>Measure of this place</h2>
<div class="unit-row"><span>Length</span><strong>1 ell = 10 mites</strong></div>
<div class="unit-row"><span>Basket</span><strong>6 ell³</strong></div>
<div class="unit-row"><span>Load</span><strong>4 baskets</strong></div>
<div class="unit-row"><span>Canonical stall</span><strong>9 × 5 ells</strong></div>
<div class="fiction-note">
These names and conversions are invented for this fictional square.
</div>
</aside>
<aside id="detailCard" class="panel details" aria-live="polite">
<div class="eyebrow" id="detailEyebrow">Market overview</div>
<h2 id="detailTitle">Three trades, one square</h2>
<p id="detailText">
Choose a stall to inspect its wares. All dimensions use the square’s
fictional ell and mite.
</p>
<div class="measurements" id="detailMeasures">
<span>9 ell stalls</span>
<span>12 builders</span>
<span>3 local trades</span>
</div>
</aside>
<nav class="toolbar" aria-label="Market locations">
<button type="button" data-focus="overview" class="active">Square</button>
<button type="button" data-focus="grain">Grain</button>
<button type="button" data-focus="cloth">Cloth</button>
<button type="button" data-focus="pottery">Pottery</button>
</nav>
<div class="panel help">
<b>Drag</b> orbit · <b>Shift-drag</b> pan<br />
<b>Wheel</b> zoom · <b>Click</b> a stall<br />
Touch: drag to orbit, pinch to zoom
</div>
<div id="loading">
<div class="loading-card">
<strong>The Fold & Grain</strong>
<span>Raising the market awnings…</span>
</div>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Scene and renderer
// ------------------------------------------------------------
const viewport = document.querySelector("#viewport");
const loading = document.querySelector("#loading");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xc8d7ce);
scene.fog = new THREE.Fog(0xc8d7ce, 55, 115);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
220
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
viewport.appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xe9f2e9, 0x725744, 2.1);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffe3b1, 3.25);
sun.position.set(-24, 38, 25);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -48;
sun.shadow.camera.right = 48;
sun.shadow.camera.top = 45;
sun.shadow.camera.bottom = -45;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 110;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xa9c5d8, 0.55);
fill.position.set(30, 18, -25);
scene.add(fill);
// One scene unit is one fictional ell.
// A mite is represented by 0.1 scene units.
const ELL = 1;
const MITE = 0.1;
// ------------------------------------------------------------
// Materials and geometry helpers
// ------------------------------------------------------------
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0,
flatShading: true,
...options
});
}
const materials = {
earth: material(0x776c50),
grass: material(0x697951),
stone: material(0x8b8475),
darkStone: material(0x625e54),
wood: material(0x70503a),
lightWood: material(0x9b7048),
darkWood: material(0x493326),
beam: material(0x47352b),
plasterA: material(0xd5c39d),
plasterB: material(0xc2ae88),
plasterC: material(0xb8a986),
roofRed: material(0x87483b),
roofBrown: material(0x66504a),
roofSlate: material(0x555b59),
grain: material(0xd8ad4d),
grainDark: material(0xad782d),
sack: material(0xb99b69),
sackDark: material(0x9c7d52),
clay: material(0xb86443),
clayLight: material(0xd18a5a),
clayDark: material(0x8c4937),
blueCloth: material(0x4c7180),
redCloth: material(0x914b3c),
goldCloth: material(0xc49b49),
creamCloth: material(0xddcfad),
greenCloth: material(0x607458),
iron: material(0x3f4140, { metalness: 0.35, roughness: 0.55 }),
window: material(0xd29a47, {
emissive: 0x9e5821,
emissiveIntensity: 0.35,
roughness: 0.45
})
};
function box(parent, size, position, mat, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
mat
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinder(
parent,
radiusTop,
radiusBottom,
height,
position,
mat,
segments = 8,
castShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
segments
),
mat
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function lowSphere(parent, radius, position, mat, detail = 0) {
const mesh = new THREE.Mesh(
new THREE.IcosahedronGeometry(radius, detail),
mat
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function torus(parent, radius, tube, position, mat, segments = 16) {
const mesh = new THREE.Mesh(
new THREE.TorusGeometry(radius, tube, 5, segments),
mat
);
mesh.position.set(position[0], position[1], position[2]);
mesh.rotation.x = Math.PI / 2;
mesh.castShadow = true;
parent.add(mesh);
return mesh;
}
function makeQuadGeometry(vertices, indices) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(vertices, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function makeRoofGeometry(width, depth, eaveHeight, rise) {
const w = width / 2;
const d = depth / 2;
const geometry = makeQuadGeometry(
[
-w, eaveHeight, -d,
w, eaveHeight, -d,
0, eaveHeight + rise, -d,
-w, eaveHeight, d,
w, eaveHeight, d,
0, eaveHeight + rise, d
],
[
0, 2, 1,
3, 4, 5,
0, 3, 5,
0, 5, 2,
1, 2, 5,
1, 5, 4
]
);
return geometry;
}
function makeSignTexture(title, subtitle) {
const canvas = document.createElement("canvas");
canvas.width = 768;
canvas.height = 220;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#3e2b20";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = "#b98b4d";
ctx.lineWidth = 9;
ctx.strokeRect(13, 13, canvas.width - 26, canvas.height - 26);
ctx.strokeStyle = "rgba(225,190,125,.42)";
ctx.lineWidth = 2;
ctx.strokeRect(29, 29, canvas.width - 58, canvas.height - 58);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#f0d9a6";
ctx.font = "600 62px Georgia";
ctx.fillText(title.toUpperCase(), canvas.width / 2, 92);
ctx.fillStyle = "#cba86f";
ctx.font = "700 22px Arial";
ctx.fillText(subtitle.toUpperCase(), canvas.width / 2, 157);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
function makeSign(parent, title, subtitle, position, width = 5.6) {
const texture = makeSignTexture(title, subtitle);
const sign = new THREE.Mesh(
new THREE.PlaneGeometry(width, width * 220 / 768),
new THREE.MeshStandardMaterial({
map: texture,
roughness: 0.9,
side: THREE.DoubleSide
})
);
sign.position.set(position[0], position[1], position[2]);
sign.castShadow = true;
parent.add(sign);
return sign;
}
// ------------------------------------------------------------
// Ground and cobbled square
// ------------------------------------------------------------
const outside = new THREE.Mesh(
new THREE.PlaneGeometry(220, 220),
materials.grass
);
outside.rotation.x = -Math.PI / 2;
outside.position.y = -0.13;
outside.receiveShadow = true;
scene.add(outside);
const earth = new THREE.Mesh(
new THREE.PlaneGeometry(82, 82),
materials.earth
);
earth.rotation.x = -Math.PI / 2;
earth.position.y = -0.055;
earth.receiveShadow = true;
scene.add(earth);
let randomSeed = 97231;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
const cobbleGeometry = new THREE.BoxGeometry(1, 1, 1);
const cobbleMaterial = material(0xffffff);
const cobbleCount = 40 * 40;
const cobbles = new THREE.InstancedMesh(
cobbleGeometry,
cobbleMaterial,
cobbleCount
);
const dummy = new THREE.Object3D();
const cobbleColor = new THREE.Color();
let cobbleIndex = 0;
for (let row = 0; row < 40; row++) {
for (let column = 0; column < 40; column++) {
const x = (column - 19.5) * 2.02 + (random() - 0.5) * 0.15;
const z = (row - 19.5) * 2.02 + (random() - 0.5) * 0.15;
dummy.position.set(
x,
0.02 + random() * 0.05,
z
);
dummy.rotation.y = (random() - 0.5) * 0.12;
dummy.scale.set(
1.65 + random() * 0.35,
0.13 + random() * 0.12,
1.35 + random() * 0.4
);
dummy.updateMatrix();
cobbles.setMatrixAt(cobbleIndex, dummy.matrix);
const hue = 0.095 + random() * 0.025;
const lightness = 0.37 + random() * 0.11;
cobbleColor.setHSL(hue, 0.12, lightness);
cobbles.setColorAt(cobbleIndex, cobbleColor);
cobbleIndex++;
}
}
cobbles.instanceMatrix.needsUpdate = true;
cobbles.instanceColor.needsUpdate = true;
cobbles.receiveShadow = true;
scene.add(cobbles);
// A subtle circular market inlay around the well.
const inlay = new THREE.Mesh(
new THREE.RingGeometry(4.4, 4.75, 40),
material(0xa99b7c)
);
inlay.rotation.x = -Math.PI / 2;
inlay.position.set(0, 0.16, -2.5);
inlay.receiveShadow = true;
scene.add(inlay);
// ------------------------------------------------------------
// Timber-framed buildings
// ------------------------------------------------------------
const buildingMaterials = [
materials.plasterA,
materials.plasterB,
materials.plasterC
];
const roofMaterials = [
materials.roofRed,
materials.roofBrown,
materials.roofSlate
];
function makeBuilding({
x,
z,
width,
height,
depth,
rotation = 0,
wall,
roof,
sign = null
}) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
box(group, [width, height, depth], [0, height / 2, 0], wall);
const roofMesh = new THREE.Mesh(
makeRoofGeometry(width + 0.8, depth + 0.7, height, 2.9),
roof
);
roofMesh.castShadow = true;
roofMesh.receiveShadow = true;
group.add(roofMesh);
const front = depth / 2 + 0.07;
box(group, [width, 0.22, 0.2], [0, 0.15, front], materials.beam);
box(group, [width, 0.2, 0.2], [0, height * 0.52, front], materials.beam);
box(group, [width, 0.22, 0.2], [0, height - 0.12, front], materials.beam);
const verticalCount = Math.max(3, Math.floor(width / 2.6));
for (let i = 0; i <= verticalCount; i++) {
const px = -width / 2 + (i / verticalCount) * width;
box(
group,
[0.18, height, 0.19],
[px, height / 2, front],
materials.beam
);
}
const braceLength = Math.min(width * 0.23, 2.5);
const leftBrace = box(
group,
[braceLength, 0.16, 0.18],
[-width * 0.3, height * 0.76, front + 0.02],
materials.beam
);
leftBrace.rotation.z = 0.62;
const rightBrace = box(
group,
[braceLength, 0.16, 0.18],
[width * 0.3, height * 0.76, front + 0.02],
materials.beam
);
rightBrace.rotation.z = -0.62;
box(
group,
[1.55, 2.65, 0.18],
[0, 1.33, front + 0.05],
materials.darkWood
);
const windowXs = [-width * 0.3, width * 0.3];
for (const wx of windowXs) {
box(
group,
[1.45, 1.45, 0.18],
[wx, height * 0.69, front + 0.04],
materials.beam
);
box(
group,
[1.1, 1.08, 0.1],
[wx, height * 0.69, front + 0.15],
materials.window,
false
);
box(
group,
[0.08, 1.12, 0.12],
[wx, height * 0.69, front + 0.23],
materials.beam
);
box(
group,
[1.14, 0.08, 0.12],
[wx, height * 0.69, front + 0.23],
materials.beam
);
}
if (sign) {
makeSign(group, sign.title, sign.subtitle, [0, height - 0.7, front + 0.15], 4.5);
}
scene.add(group);
return group;
}
makeBuilding({
x: -27,
z: -27,
width: 10.5,
height: 8.3,
depth: 8,
wall: buildingMaterials[0],
roof: roofMaterials[0],
sign: { title: "Malt House", subtitle: "Malt, meal and straw" }
});
makeBuilding({
x: -15.5,
z: -27.5,
width: 11.5,
height: 10.5,
depth: 8.5,
wall: buildingMaterials[2],
roof: roofMaterials[1]
});
makeBuilding({
x: -2.5,
z: -28,
width: 11,
height: 8.8,
depth: 8,
wall: buildingMaterials[0],
roof: roofMaterials[2],
sign: { title: "Guildhall", subtitle: "By the first bell" }
});
makeBuilding({
x: 11,
z: -27,
width: 10.5,
height: 10.2,
depth: 8.3,
wall: buildingMaterials[1],
roof: roofMaterials[0]
});
makeBuilding({
x: 24,
z: -27.5,
width: 11.5,
height: 8.1,
depth: 8,
wall: buildingMaterials[2],
roof: roofMaterials[1],
sign: { title: "Bell Inn", subtitle: "Rooms above the square" }
});
makeBuilding({
x: -33,
z: -12,
width: 11,
height: 9.1,
depth: 8,
rotation: Math.PI / 2,
wall: buildingMaterials[1],
roof: roofMaterials[2]
});
makeBuilding({
x: 33,
z: -12,
width: 11,
height: 8.6,
depth: 8,
rotation: -Math.PI / 2,
wall: buildingMaterials[0],
roof: roofMaterials[1]
});
// ------------------------------------------------------------
// Market stalls
// ------------------------------------------------------------
const stallGroups = {};
const stallOutlines = {};
const pickables = [];
const animatedPeople = [];
function makeCanopy(parent, width, depth, accent, pale = materials.creamCloth) {
const strips = 10;
const stripWidth = width / strips;
for (let i = 0; i < strips; i++) {
const x0 = -width / 2 + i * stripWidth;
const x1 = x0 + stripWidth + 0.018;
const y0 = 5.35 + 0.95 * (1 - Math.abs(x0) / (width / 2));
const y1 = 5.35 + 0.95 * (1 - Math.abs(x1) / (width / 2));
const geometry = makeQuadGeometry(
[
x0, y0, -depth / 2,
x1, y1, -depth / 2,
x0, y0, depth / 2,
x1, y1, depth / 2
],
[0, 2, 1, 1, 2, 3]
);
const clothMaterial = i % 2 === 0 ? accent : pale;
const strip = new THREE.Mesh(
geometry,
new THREE.MeshStandardMaterial({
color: clothMaterial.color,
roughness: 0.94,
flatShading: true,
side: THREE.DoubleSide
})
);
strip.castShadow = true;
strip.receiveShadow = true;
parent.add(strip);
const valance = box(
parent,
[stripWidth, 0.58, 0.07],
[(x0 + x1) / 2, 5.1, depth / 2 + 0.03],
clothMaterial
);
valance.rotation.z = Math.atan2(y1 - y0, x1 - x0);
}
}
function makeStall(id, title, subtitle, position, rotation, accent) {
const group = new THREE.Group();
group.position.set(position[0], 0, position[1]);
group.rotation.y = rotation;
const width = 9;
const depth = 5;
const postPositions = [
[-4.25, 2.65, -2.15],
[4.25, 2.65, -2.15],
[-4.25, 2.65, 2.15],
[4.25, 2.65, 2.15]
];
for (const [x, y, z] of postPositions) {
box(group, [0.24, y, 0.24], [x, y / 2, z], materials.darkWood);
}
box(group, [8.8, 0.2, 0.2], [0, 4.95, -2.15], materials.darkWood);
box(group, [8.8, 0.2, 0.2], [0, 4.95, 2.15], materials.darkWood);
// Counter and individual plank top.
box(group, [8.7, 1.25, 2.55], [0, 0.68, 0.55], materials.wood);
for (let i = 0; i < 10; i++) {
box(
group,
[0.84, 0.13, 2.75],
[-3.95 + i * 0.875, 1.37, 0.55],
i % 2 ? materials.lightWood : materials.wood
);
}
makeCanopy(group, width + 0.6, depth + 0.6, accent);
makeSign(group, title, subtitle, [0, 4.95, 2.82], 5.7);
// Rear shelf.
box(group, [8.4, 0.16, 0.85], [0, 2.7, -1.65], materials.lightWood);
box(group, [8.4, 0.16, 0.85], [0, 4.05, -1.65], materials.lightWood);
const hit = new THREE.Mesh(
new THREE.BoxGeometry(10.2, 8.2, 6.2),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
})
);
hit.position.y = 3.1;
hit.userData.commodity = id;
group.add(hit);
pickables.push(hit);
const outline = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(10.2, 0.08, 6.2)),
new THREE.LineBasicMaterial({
color: accent.color,
transparent: true,
opacity: 0.9
})
);
outline.position.y = 0.18;
outline.visible = false;
group.add(outline);
scene.add(group);
stallGroups[id] = group;
stallOutlines[id] = outline;
return group;
}
const grainStall = makeStall(
"grain",
"Golden sheaf",
"Grain, meal and seed",
[-13.4, 8.5],
0.06,
materials.goldCloth
);
const clothStall = makeStall(
"cloth",
"Blue thread",
"Wool, linen and dye",
[0, 11.8],
-0.025,
materials.blueCloth
);
const potteryStall = makeStall(
"pottery",
"Red earth",
"Clay cups and jars",
[13.4, 8.5],
-0.06,
materials.redCloth
);
function makeSack(parent, position, scale = 1, dark = false) {
const sack = new THREE.Group();
sack.position.set(position[0], position[1], position[2]);
sack.scale.setScalar(scale);
const body = lowSphere(
sack,
0.78,
[0, 0.65, 0],
dark ? materials.sackDark : materials.sack,
1
);
body.scale.set(0.76, 1, 0.68);
cylinder(sack, 0.2, 0.32, 0.38, [0, 1.36, 0], materials.sackDark, 7);
const tie = torus(sack, 0.23, 0.035, [0, 1.42, 0], materials.darkWood, 8);
tie.scale.y = 0.5;
parent.add(sack);
return sack;
}
function makeCrate(parent, position, scale = 1) {
const crate = new THREE.Group();
crate.position.set(position[0], position[1], position[2]);
crate.scale.setScalar(scale);
box(crate, [1.7, 0.18, 1.3], [0, 0.12, 0], materials.lightWood);
box(crate, [1.7, 0.16, 1.3], [0, 0.85, 0], materials.lightWood);
for (const x of [-0.72, 0, 0.72]) {
box(crate, [0.13, 0.85, 1.35], [x, 0.48, 0], materials.wood);
}
for (const z of [-0.56, 0.56]) {
box(crate, [1.78, 0.85, 0.12], [0, 0.48, z], materials.wood);
}
parent.add(crate);
return crate;
}
// Grain wares.
makeSack(grainStall, [-3.2, 0, -0.7], 1.15);
makeSack(grainStall, [-1.7, 0, -0.85], 1.05, true);
makeSack(grainStall, [3.2, 0, -0.7], 1.12, true);
for (const x of [-2.7, -0.3, 2.1]) {
const mound = lowSphere(
grainStall,
0.53,
[x, 1.58, 0.52],
materials.grain,
1
);
mound.scale.set(1, 0.38, 0.8);
}
makeCrate(grainStall, [3.35, 0.02, 0.58], 0.9);
for (let i = 0; i < 14; i++) {
const kernel = lowSphere(
grainStall,
0.075,
[-3.6 + random() * 7.2, 1.5 + random() * 0.08, 0.1 + random() * 0.9],
i % 3 ? materials.grain : materials.grainDark,
0
);
kernel.scale.y = 0.55;
}
// Rolled and draped cloth.
const fabricMaterials = [
materials.blueCloth,
materials.redCloth,
materials.goldCloth,
materials.greenCloth,
materials.creamCloth
];
for (let row = 0; row < 2; row++) {
for (let i = 0; i < 6; i++) {
const roll = cylinder(
clothStall,
0.25,
0.25,
2.4,
[-3.4 + i * 1.35, 1.75 + row * 0.58, -1.55],
fabricMaterials[(i + row) % fabricMaterials.length],
8
);
roll.rotation.z = Math.PI / 2;
}
}
function makeDrapedCloth(parent, width, height, position, mat) {
const geometry = new THREE.PlaneGeometry(width, height, 14, 7);
const positions = geometry.attributes.position;
for (let i = 0; i < positions.count; i++) {
const x = positions.getX(i);
const y = positions.getY(i);
positions.setZ(i, Math.sin(x * 4.2 + y * 1.4) * 0.075);
}
geometry.computeVertexNormals();
const cloth = new THREE.Mesh(
geometry,
new THREE.MeshStandardMaterial({
color: mat.color,
roughness: 0.95,
side: THREE.DoubleSide,
flatShading: true
})
);
cloth.position.set(position[0], position[1], position[2]);
cloth.castShadow = true;
cloth.receiveShadow = true;
parent.add(cloth);
}
makeDrapedCloth(clothStall, 3.2, 2.1, [-2.4, 0.9, 1.85], materials.blueCloth);
makeDrapedCloth(clothStall, 2.5, 1.9, [0.7, 0.88, 1.86], materials.redCloth);
makeDrapedCloth(clothStall, 2.2, 1.8, [3, 0.82, 1.86], materials.goldCloth);
for (let i = 0; i < 4; i++) {
const bolt = cylinder(
clothStall,
0.32,
0.32,
2.5,
[-3 + i * 2, 2.9, 0.1],
fabricMaterials[(i + 1) % fabricMaterials.length],
9
);
bolt.rotation.z = Math.PI / 2;
}
// Pottery wares.
const potPoints = [
new THREE.Vector2(0, 0),
new THREE.Vector2(0.42, 0),
new THREE.Vector2(0.62, 0.18),
new THREE.Vector2(0.72, 0.62),
new THREE.Vector2(0.62, 1.03),
new THREE.Vector2(0.4, 1.28),
new THREE.Vector2(0.34, 1.42),
new THREE.Vector2(0.23, 1.44),
new THREE.Vector2(0.23, 1.3),
new THREE.Vector2(0.36, 1.25),
new THREE.Vector2(0.54, 0.98),
new THREE.Vector2(0.59, 0.62),
new THREE.Vector2(0.46, 0.18),
new THREE.Vector2(0, 0.15)
];
const potGeometry = new THREE.LatheGeometry(potPoints, 10);
function makePot(parent, position, scale, mat) {
const pot = new THREE.Group();
pot.position.set(position[0], position[1], position[2]);
pot.scale.setScalar(scale);
const body = new THREE.Mesh(potGeometry, mat);
body.castShadow = true;
body.receiveShadow = true;
pot.add(body);
const rim = torus(pot, 0.23, 0.045, [0, 1.38, 0], materials.clayDark, 10);
rim.scale.y = 0.65;
parent.add(pot);
return pot;
}
const clayMaterials = [
materials.clay,
materials.clayLight,
materials.clayDark
];
for (let row = 0; row < 2; row++) {
for (let i = 0; i < 6; i++) {
makePot(
potteryStall,
[-3.45 + i * 1.38, 2.82 + row * 1.36, -1.62],
0.48 + random() * 0.22,
clayMaterials[(i + row) % clayMaterials.length]
);
}
}
makePot(potteryStall, [-2.5, 1.45, 0.45], 0.82, materials.clay);
makePot(potteryStall, [-0.8, 1.45, 0.55], 0.67, materials.clayLight);
makePot(potteryStall, [0.8, 1.45, 0.4], 0.9, materials.clayDark);
makePot(potteryStall, [2.5, 1.45, 0.7], 0.58, materials.clay);
// Potter's wheel.
cylinder(potteryStall, 0.62, 0.75, 1.25, [3.65, 0.62, -0.15], materials.wood, 10);
cylinder(potteryStall, 0.82, 0.82, 0.16, [3.65, 1.32, -0.15], materials.darkWood, 12);
torus(potteryStall, 0.55, 0.09, [3.65, 1.43, -0.15], materials.iron, 14);
// ------------------------------------------------------------
// Well, barrels, crates and miscellaneous props
// ------------------------------------------------------------
function makeBarrel(parent, position, scale = 1) {
const barrel = new THREE.Group();
barrel.position.set(position[0], position[1], position[2]);
barrel.scale.setScalar(scale);
cylinder(barrel, 0.65, 0.57, 1.65, [0, 0.83, 0], materials.lightWood, 10);
torus(barrel, 0.64, 0.045, [0, 0.3, 0], materials.iron, 12);
torus(barrel, 0.64, 0.045, [0, 1.35, 0], materials.iron, 12);
parent.add(barrel);
return barrel;
}
const well = new THREE.Group();
well.position.set(0, 0, -2.5);
cylinder(well, 2.1, 2.25, 1.15, [0, 0.58, 0], materials.stone, 12);
cylinder(well, 1.55, 1.55, 0.12, [0, 1.2, 0], materials.darkStone, 12);
for (const x of [-1.65, 1.65]) {
box(well, [0.25, 5.2, 0.25], [x, 2.6, 0], materials.darkWood);
}
const wellRoof = new THREE.Mesh(
makeRoofGeometry(4.4, 3.4, 5.15, 1.45),
materials.roofBrown
);
wellRoof.castShadow = true;
well.add(wellRoof);
cylinder(well, 0.16, 0.16, 2.3, [0, 4.25, 0], materials.darkWood, 8);
const bucket = cylinder(
well,
0.38,
0.3,
0.6,
[0, 2.5, 0],
materials.lightWood,
8
);
bucket.rotation.z = 0.04;
scene.add(well);
makeBarrel(grainStall, [-4.1, 0, -1.65], 0.85);
makeBarrel(clothStall, [4.1, 0, -1.65], 0.8);
makeBarrel(potteryStall, [-4.15, 0, -1.6], 0.75);
makeCrate(grainStall, [-4.2, 0.02, 1.25], 0.72);
makeCrate(potteryStall, [4.25, 0.02, 1.3], 0.68);
// Hay bundles.
for (let i = 0; i < 4; i++) {
const hay = cylinder(
grainStall,
0.35,
0.35,
1.5,
[-3.8 + i * 0.62, 0.42, -2.35],
materials.grainDark,
7
);
hay.rotation.z = Math.PI / 2;
}
// ------------------------------------------------------------
// Well-dressed villagers
// ------------------------------------------------------------
function makePerson(position, robeMaterial, rotation = 0, scale = 1) {
const person = new THREE.Group();
person.position.set(position[0], 0, position[1]);
person.rotation.y = rotation;
person.scale.setScalar(scale);
const robe = new THREE.Mesh(
new THREE.ConeGeometry(0.48, 1.45, 7),
robeMaterial
);
robe.position.y = 0.73;
robe.castShadow = true;
person.add(robe);
cylinder(person, 0.28, 0.34, 0.75, [0, 1.52, 0], robeMaterial, 7);
lowSphere(person, 0.27, [0, 2.05, 0], material(0xc8926c), 1);
const hood = new THREE.Mesh(
new THREE.ConeGeometry(0.33, 0.56, 7),
robeMaterial
);
hood.position.set(0, 2.17, -0.06);
hood.castShadow = true;
person.add(hood);
const armMaterial = material(0xc8926c);
const leftArm = cylinder(
person,
0.1,
0.12,
0.82,
[-0.37, 1.47, 0],
armMaterial,
6
);
leftArm.rotation.z = -0.28;
const rightArm = cylinder(
person,
0.1,
0.12,
0.82,
[0.37, 1.47, 0],
armMaterial,
6
);
rightArm.rotation.z = 0.28;
scene.add(person);
animatedPeople.push({
group: person,
baseY: person.position.y,
phase: random() * Math.PI * 2
});
return person;
}
makePerson([-7.5, 2.5], materials.blueCloth, 0.8, 0.95);
makePerson([6.7, 1.2], materials.redCloth, -1.1, 1.03);
makePerson([-2.5, -9.5], materials.greenCloth, 2.5, 0.92);
makePerson([10.7, -7.2], materials.goldCloth, -0.4, 0.9);
makePerson([-16.5, 0.3], materials.creamCloth, 1.6, 1.02);
makePerson([18.4, -1.5], materials.blueCloth, 0.2, 0.94);
// ------------------------------------------------------------
// Trees, banners and distant scenery
// ------------------------------------------------------------
function makeTree(x, z, scale = 1) {
const tree = new THREE.Group();
tree.position.set(x, 0, z);
tree.scale.setScalar(scale);
cylinder(tree, 0.24, 0.36, 4.4, [0, 2.2, 0], materials.wood, 7);
const crownMaterial = material(0x647452);
const crownA = lowSphere(tree, 1.7, [0, 4.5, 0], crownMaterial, 1);
crownA.scale.set(1.15, 1.05, 1);
const crownB = lowSphere(tree, 1.15, [-0.8, 4.0, 0.25], crownMaterial, 0);
const crownC = lowSphere(tree, 1.05, [0.75, 4.15, -0.15], crownMaterial, 0);
crownB.scale.set(1, 0.85, 1);
crownC.scale.set(1, 0.85, 1);
scene.add(tree);
}
makeTree(-29, 12, 1.25);
makeTree(-24, 17, 1.05);
makeTree(28, 12, 1.2);
makeTree(33, 17, 1.05);
makeTree(-17, -20, 0.9);
makeTree(18, -20, 0.95);
function makeBanner(x, z, color, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
cylinder(group, 0.08, 0.1, 7.2, [0, 3.6, 0], materials.darkWood, 8);
const geometry = new THREE.PlaneGeometry(1.45, 3.5, 8, 12);
const positions = geometry.attributes.position;
for (let i = 0; i < positions.count; i++) {
const px = positions.getX(i);
const py = positions.getY(i);
positions.setZ(i, Math.sin(px * 4 + py * 0.8) * 0.08);
}
geometry.computeVertexNormals();
const banner = new THREE.Mesh(
geometry,
new THREE.MeshStandardMaterial({
color: color.color,
side: THREE.DoubleSide,
roughness: 0.95,
flatShading: true
})
);
banner.position.set(0.78, 5.3, 0);
banner.castShadow = true;
group.add(banner);
const finial = lowSphere(group, 0.18, [0, 7.25, 0], materials.goldCloth, 0);
finial.scale.y = 1.4;
scene.add(group);
}
makeBanner(-22, -20.5, materials.redCloth, 0.08);
makeBanner(21, -20.5, materials.blueCloth, -0.08);
// Distant low-poly clouds.
const cloudMaterial = new THREE.MeshBasicMaterial({
color: 0xf1eee1,
transparent: true,
opacity: 0.72
});
const cloudData = [
[-31, 25, -48, 1.2],
[8, 29, -58, 1.5],
[34, 24, -46, 1.1]
];
for (const [x, y, z, s] of cloudData) {
const cloud = new THREE.Group();
cloud.position.set(x, y, z);
cloud.scale.setScalar(s);
lowSphere(cloud, 2.2, [0, 0, 0], cloudMaterial, 1);
lowSphere(cloud, 1.55, [-2.1, -0.2, 0], cloudMaterial, 1);
lowSphere(cloud, 1.7, [2.1, -0.1, 0.2], cloudMaterial, 1);
scene.add(cloud);
}
// ------------------------------------------------------------
// HTML world labels
// ------------------------------------------------------------
const labelsRoot = document.querySelector("#labels");
const markerData = {
grain: { label: "Grain · 9 ells", text: "GRAIN" },
cloth: { label: "Cloth · 9 ells", text: "CLOTH" },
pottery: { label: "Pottery · 9 ells", text: "POTTERY" }
};
const markers = {};
for (const [id, data] of Object.entries(markerData)) {
const marker = document.createElement("button");
marker.type = "button";
marker.className = "world-label";
marker.textContent = data.label;
marker.addEventListener("click", () => focusStall(id));
labelsRoot.appendChild(marker);
markers[id] = marker;
}
// ------------------------------------------------------------
// Custom camera controls
// ------------------------------------------------------------
const desiredCamera = {
target: new THREE.Vector3(0, 1.7, 0),
radius: 47,
theta: 0.53,
phi: 0.94
};
const currentCamera = {
target: desiredCamera.target.clone(),
radius: desiredCamera.radius,
theta: desiredCamera.theta,
phi: desiredCamera.phi
};
camera.position.set(27, 29, 38);
camera.lookAt(currentCamera.target);
const pointers = new Map();
let dragMode = "orbit";
let dragDistance = 0;
let gesture = null;
let lastPicked = null;
function updateCamera(delta) {
const amount = 1 - Math.pow(0.0008, delta);
currentCamera.target.lerp(desiredCamera.target, amount);
currentCamera.radius = THREE.MathUtils.lerp(
currentCamera.radius,
desiredCamera.radius,
amount
);
currentCamera.theta = THREE.MathUtils.lerp(
currentCamera.theta,
desiredCamera.theta,
amount
);
currentCamera.phi = THREE.MathUtils.lerp(
currentCamera.phi,
desiredCamera.phi,
amount
);
const sinPhi = Math.sin(currentCamera.phi);
camera.position.set(
currentCamera.target.x +
currentCamera.radius * sinPhi * Math.sin(currentCamera.theta),
currentCamera.target.y +
currentCamera.radius * Math.cos(currentCamera.phi),
currentCamera.target.z +
currentCamera.radius * sinPhi * Math.cos(currentCamera.theta)
);
camera.lookAt(currentCamera.target);
camera.updateMatrixWorld();
}
function panCamera(dx, dy) {
const scale = currentCamera.radius * 0.00135;
const right = new THREE.Vector3().setFromMatrixColumn(
camera.matrixWorld,
0
);
right.y = 0;
right.normalize();
const forward = currentCamera.target.clone().sub(camera.position);
forward.y = 0;
forward.normalize();
desiredCamera.target.addScaledVector(right, -dx * scale);
desiredCamera.target.addScaledVector(forward, dy * scale);
desiredCamera.target.x = THREE.MathUtils.clamp(
desiredCamera.target.x,
-30,
30
);
desiredCamera.target.z = THREE.MathUtils.clamp(
desiredCamera.target.z,
-25,
24
);
}
function midpointOfPointerMap() {
const values = [...pointers.values()];
return {
x: (values[0].x + values[1].x) / 2,
y: (values[0].y + values[1].y) / 2
};
}
function distanceOfPointerMap() {
const values = [...pointers.values()];
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
startX: event.clientX,
startY: event.clientY
});
dragMode = event.button === 2 || event.shiftKey ? "pan" : "orbit";
dragDistance = 0;
if (pointers.size === 2) {
gesture = {
midpoint: midpointOfPointerMap(),
distance: distanceOfPointerMap()
};
}
viewport.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) {
if (event.pointerType === "mouse") {
updateHover(event.clientX, event.clientY);
}
return;
}
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
pointers.set(event.pointerId, {
...previous,
x: event.clientX,
y: event.clientY
});
dragDistance += Math.abs(dx) + Math.abs(dy);
if (pointers.size === 2) {
const midpoint = midpointOfPointerMap();
const distance = distanceOfPointerMap();
if (gesture) {
desiredCamera.radius = THREE.MathUtils.clamp(
desiredCamera.radius *
(gesture.distance / Math.max(distance, 1)),
12,
82
);
panCamera(
midpoint.x - gesture.midpoint.x,
midpoint.y - gesture.midpoint.y
);
}
gesture = { midpoint, distance };
} else if (dragMode === "pan") {
panCamera(dx, dy);
} else {
desiredCamera.theta -= dx * 0.006;
desiredCamera.phi = THREE.MathUtils.clamp(
desiredCamera.phi - dy * 0.005,
0.34,
1.43
);
}
});
function releasePointer(event) {
const wasClick =
pointers.size === 1 &&
dragDistance < 7 &&
dragMode !== "pan";
pointers.delete(event.pointerId);
if (pointers.size < 2) {
gesture = null;
}
if (pointers.size === 0) {
viewport.classList.remove("dragging");
if (wasClick) {
const id = pickAt(event.clientX, event.clientY);
if (id) {
focusStall(id);
}
}
}
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener(
"contextmenu",
(event) => event.preventDefault()
);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredCamera.radius = THREE.MathUtils.clamp(
desiredCamera.radius * Math.exp(event.deltaY * 0.001),
12,
82
);
},
{ passive: false }
);
// ------------------------------------------------------------
// Picking
// ------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointerNDC = new THREE.Vector2();
function pickAt(clientX, clientY) {
const rect = renderer.domElement.getBoundingClientRect();
pointerNDC.x = ((clientX - rect.left) / rect.width) * 2 - 1;
pointerNDC.y = -((clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointerNDC, camera);
const hits = raycaster.intersectObjects(pickables, false);
return hits.length ? hits[0].object.userData.commodity : null;
}
function updateHover(clientX, clientY) {
const id = pickAt(clientX, clientY);
viewport.classList.toggle("pickable", Boolean(id));
lastPicked = id;
}
// ------------------------------------------------------------
// Focus controls and details
// ------------------------------------------------------------
const detailEyebrow = document.querySelector("#detailEyebrow");
const detailTitle = document.querySelector("#detailTitle");
const detailText = document.querySelector("#detailText");
const detailMeasures = document.querySelector("#detailMeasures");
const toolbarButtons = [...document.querySelectorAll("[data-focus]")];
const stallDetails = {
grain: {
eyebrow: "Trade 01 · grain",
title: "Golden Sheaf",
text:
"Sacks of pale rye, dark barley and warm meal. The stall measures nine ells across, with each grain sack held at two ells.",
measures: ["9 ell counter", "2 ell sack", "18 sack capacity"]
},
cloth: {
eyebrow: "Trade 02 · cloth",
title: "Blue Thread",
text:
"Bolts of wool, linen and dyed cloth fill the sheltered counter. A full bolt is one ell long and one quarter ells around.",
measures: ["9 ell counter", "1 ell bolt", "24 fold capacity"]
},
pottery: {
eyebrow: "Trade 03 · pottery",
title: "Red Earth",
text:
"Low-fired jars, cups and cooking pots rest on two shelves. The turning wheel has a diameter of one ell.",
measures: ["9 ell counter", "1 ell wheel", "36 vessel capacity"]
}
};
function setToolbarSelection(id) {
for (const button of toolbarButtons) {
button.classList.toggle(
"active",
button.dataset.focus === id
);
}
}
function focusStall(id) {
const group = stallGroups[id];
if (!group) return;
for (const [key, outline] of Object.entries(stallOutlines)) {
outline.visible = key === id;
}
const worldPosition = new THREE.Vector3();
group.getWorldPosition(worldPosition);
desiredCamera.target.set(worldPosition.x, 2.5, worldPosition.z);
desiredCamera.radius = 25;
desiredCamera.theta = 0.48;
desiredCamera.phi = 0.96;
const details = stallDetails[id];
detailEyebrow.textContent = details.eyebrow;
detailTitle.textContent = details.title;
detailText.textContent = details.text;
detailMeasures.innerHTML = details.measures
.map((value) => `<span>${value}</span>`)
.join("");
setToolbarSelection(id);
}
function showOverview() {
for (const outline of Object.values(stallOutlines)) {
outline.visible = false;
}
desiredCamera.target.set(0, 1.7, 0);
desiredCamera.radius = 47;
desiredCamera.theta = 0.53;
desiredCamera.phi = 0.94;
detailEyebrow.textContent = "Market overview";
detailTitle.textContent = "Three trades, one square";
detailText.textContent =
"Choose a stall to inspect its wares. All dimensions use the square’s fictional ell and mite.";
detailMeasures.innerHTML =
"<span>9 ell stalls</span><span>12 builders</span><span>3 local trades</span>";
setToolbarSelection("overview");
}
for (const button of toolbarButtons) {
button.addEventListener("click", () => {
if (button.dataset.focus === "overview") {
showOverview();
} else {
focusStall(button.dataset.focus);
}
});
}
addEventListener("keydown", (event) => {
if (event.key === "0" || event.key.toLowerCase() === "r") {
showOverview();
} else if (event.key === "1") {
focusStall("grain");
} else if (event.key === "2") {
focusStall("cloth");
} else if (event.key === "3") {
focusStall("pottery");
}
});
// ------------------------------------------------------------
// Animation, labels and resize
// ------------------------------------------------------------
const projected = new THREE.Vector3();
function updateLabels() {
scene.updateMatrixWorld();
for (const [id, marker] of Object.entries(markers)) {
projected.set(0, 7.75, 0);
stallGroups[id].localToWorld(projected);
projected.project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.1 &&
projected.x < 1.1 &&
projected.y > -1.1 &&
projected.y < 1.1;
marker.classList.toggle("hidden", !visible);
if (visible) {
const x = (projected.x * 0.5 + 0.5) * innerWidth;
const y = (-projected.y * 0.5 + 0.5) * innerHeight;
marker.style.left = `${x}px`;
marker.style.top = `${y}px`;
}
}
}
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
}
addEventListener("resize", resize);
const clock = new THREE.Clock();
let firstFrame = true;
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
updateCamera(delta);
updateLabels();
for (const person of animatedPeople) {
person.group.position.y =
person.baseY + Math.sin(elapsed * 1.7 + person.phase) * 0.018;
}
renderer.render(scene, camera);
if (firstFrame) {
firstFrame = false;
requestAnimationFrame(() => loading.classList.add("hidden"));
}
});
</script>
</body>
</html>
```