Prompt specification
Build or customize the Three.js 3D scene "Community fair welcome area" 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: Community fair welcome area
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/community-fair-welcome-area-x0403
Scene Description Prompt:
A voxel community fair entrance where volunteers guide newcomers toward booths, seating, and information. Make one HTML file using Three.js from cdnjs.
Working Three.js Starter (community-fair-welcome-area-x0403):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Community Fair</title>
<style>
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b9dce8;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.card {
position: fixed;
z-index: 10;
border: 1px solid rgba(255, 255, 255, 0.7);
background: rgba(255, 255, 255, 0.86);
box-shadow: 0 12px 32px rgba(35, 70, 82, 0.16);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
#title-card {
top: 20px;
left: 20px;
padding: 16px 20px;
border-radius: 18px;
pointer-events: none;
}
#title-card .eyebrow {
margin-bottom: 5px;
color: #27877d;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.18em;
text-transform: uppercase;
}
#title-card h1 {
margin: 0;
color: #203e46;
font-size: clamp(22px, 3vw, 34px);
line-height: 1;
letter-spacing: -0.04em;
}
#title-card p {
margin: 8px 0 0;
color: #587077;
font-size: 13px;
}
#legend {
right: 20px;
bottom: 20px;
padding: 13px 16px;
border-radius: 14px;
color: #38545c;
font-size: 12px;
line-height: 1.8;
pointer-events: none;
}
#legend strong {
display: block;
margin-bottom: 4px;
color: #203e46;
font-size: 11px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.dot {
display: inline-block;
width: 10px;
height: 10px;
margin-right: 7px;
border-radius: 2px;
vertical-align: -1px;
}
#controls {
left: 20px;
bottom: 20px;
padding: 10px 12px;
border-radius: 14px;
color: #587077;
font-size: 12px;
pointer-events: none;
}
#reset {
position: fixed;
z-index: 20;
top: 20px;
right: 20px;
padding: 10px 14px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 12px;
background: rgba(255, 255, 255, 0.88);
box-shadow: 0 8px 22px rgba(35, 70, 82, 0.14);
color: #28525a;
font: 700 12px inherit;
cursor: pointer;
}
#reset:hover {
background: #fff;
transform: translateY(-1px);
}
@media (max-width: 680px) {
#title-card {
top: 12px;
left: 12px;
padding: 12px 15px;
}
#title-card p,
#legend {
display: none;
}
#reset {
top: 12px;
right: 12px;
}
#controls {
left: 12px;
bottom: 12px;
}
}
</style>
</head>
<body>
<div id="title-card" class="card">
<div class="eyebrow">Welcome to the neighborhood</div>
<h1>Community Fair</h1>
<p>Meet a volunteer, find a booth, and settle in.</p>
</div>
<div id="controls" class="card">
Drag to orbit · Shift-drag to pan · Scroll to zoom · Double-click to reset
</div>
<div id="legend" class="card">
<strong>Fair guide</strong>
<span class="dot" style="background:#f2bd43"></span>Volunteer
<span class="dot" style="background:#ef786d"></span>Newcomer
<span class="dot" style="background:#f5cf58"></span>Wayfinding
</div>
<button id="reset" type="button">Reset view</button>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb9dce8);
scene.fog = new THREE.Fog(0xb9dce8, 62, 105);
const camera = new THREE.PerspectiveCamera(
34,
window.innerWidth / window.innerHeight,
0.1,
180
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.18;
document.body.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xeaf8ff, 0x78976b, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1cf, 3.4);
sun.position.set(-18, 32, 20);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -31;
sun.shadow.camera.right = 31;
sun.shadow.camera.top = 31;
sun.shadow.camera.bottom = -31;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 80;
sun.shadow.bias = -0.00025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xbfe8ff, 0.65);
fill.position.set(22, 15, -25);
scene.add(fill);
// -------------------------------------------------------------------------
// Shared materials and voxel geometry helpers
// -------------------------------------------------------------------------
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
...options
});
const M = {
grass: material(0x83bb70),
grassDark: material(0x679d5d),
soil: material(0xb78662),
path: material(0xe9cda1),
pathEdge: material(0xd7b47f),
cream: material(0xfff0cf),
white: material(0xfffaf0),
dark: material(0x30464c),
wood: material(0x9b6848),
woodLight: material(0xc78d5c),
coral: material(0xef786d),
red: material(0xd95854),
yellow: material(0xf4c85b),
teal: material(0x2a9d8f),
tealDark: material(0x23786f),
blue: material(0x5fa8d3),
purple: material(0x9b7bc5),
pink: material(0xeaa1ae),
green: material(0x68ad68),
greenDark: material(0x437e4b),
trunk: material(0x85573d),
skin1: material(0xf0b184),
skin2: material(0x9d6348),
skin3: material(0xd99366),
hair1: material(0x4b332b),
hair2: material(0x2d292a),
hair3: material(0xa9653f),
pants: material(0x40556c),
route: material(0xf5cf58, { emissive: 0x6b5100, emissiveIntensity: 0.2 }),
lamp: material(0xffe28a, {
emissive: 0xffcc55,
emissiveIntensity: 1.1
}),
cloud: material(0xf7fbfc)
};
const geometryCache = new Map();
function box(parent, w, h, d, x, y, z, mat) {
const key = `${w},${h},${d}`;
let geometry = geometryCache.get(key);
if (!geometry) {
geometry = new THREE.BoxGeometry(w, h, d);
geometryCache.set(key, geometry);
}
const mesh = new THREE.Mesh(geometry, mat);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeLabel(text, width, height, bg = "#fff3d8", fg = "#24444b") {
const canvas = document.createElement("canvas");
canvas.width = 768;
canvas.height = 192;
const ctx = canvas.getContext("2d");
ctx.fillStyle = bg;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = fg;
ctx.globalAlpha = 0.2;
ctx.lineWidth = 12;
ctx.strokeRect(8, 8, canvas.width - 16, canvas.height - 16);
ctx.globalAlpha = 1;
let fontSize = 98;
ctx.font = `900 ${fontSize}px Arial, sans-serif`;
while (ctx.measureText(text).width > canvas.width - 70 && fontSize > 36) {
fontSize -= 2;
ctx.font = `900 ${fontSize}px Arial, sans-serif`;
}
ctx.fillStyle = fg;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + 3);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(8, renderer.capabilities.getMaxAnisotropy());
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
new THREE.MeshBasicMaterial({
map: texture,
side: THREE.DoubleSide
})
);
return mesh;
}
function addLabel(parent, text, width, height, x, y, z, bg, fg) {
const label = makeLabel(text, width, height, bg, fg);
label.position.set(x, y, z);
parent.add(label);
return label;
}
// -------------------------------------------------------------------------
// Fairground
// -------------------------------------------------------------------------
const fair = new THREE.Group();
scene.add(fair);
box(fair, 44, 1, 38, 0, -0.55, 0, M.soil);
box(fair, 43.5, 0.25, 37.5, 0, 0.02, 0, M.grass);
// Main walkway and branches.
box(fair, 7.2, 0.13, 30, 0, 0.19, 0.6, M.path);
box(fair, 35, 0.13, 4.8, 0, 0.2, -7.1, M.path);
box(fair, 13, 0.13, 3.2, 9.5, 0.2, -2.8, M.path);
box(fair, 11, 0.13, 3.2, -11, 0.2, 1.5, M.path);
box(fair, 8, 0.13, 3.2, 11, 0.2, 3.4, M.path);
// Subtle raised path borders.
box(fair, 0.22, 0.18, 29.5, -3.65, 0.18, 0.5, M.pathEdge);
box(fair, 0.22, 0.18, 29.5, 3.65, 0.18, 0.5, M.pathEdge);
// Entrance arch.
const gate = new THREE.Group();
gate.position.set(0, 0.24, 10.6);
fair.add(gate);
for (const side of [-1, 1]) {
box(gate, 1.15, 6.8, 1.15, side * 6.2, 3.4, 0, M.cream);
box(gate, 1.45, 0.6, 1.45, side * 6.2, 0.3, 0, M.coral);
box(gate, 1.45, 0.65, 1.45, side * 6.2, 6.45, 0, M.teal);
box(gate, 0.55, 0.9, 1.35, side * 6.2, 3.5, 0.04, M.yellow);
}
box(gate, 13.7, 0.7, 1.25, 0, 6.95, 0, M.teal);
box(gate, 12.2, 2.15, 0.5, 0, 8.15, 0.08, M.cream);
addLabel(gate, "COMMUNITY FAIR", 11.2, 1.45, 0, 8.18, 0.35);
// Pixel bunting under the entrance sign.
const flagColors = [M.coral, M.yellow, M.teal, M.blue, M.pink];
for (let i = 0; i < 12; i++) {
const x = -5.6 + i * 1.02;
const y = 6.38 - Math.abs(i - 5.5) * 0.055;
box(gate, 0.7, 0.62, 0.12, x, y, 0.7, flagColors[i % flagColors.length]);
box(gate, 0.38, 0.34, 0.14, x, y - 0.45, 0.71, flagColors[i % flagColors.length]);
}
// Back fence.
for (let x = -20; x <= 20; x += 2.8) {
box(fair, 0.18, 1.9, 0.18, x, 1.0, -15.8, M.wood);
}
box(fair, 40.5, 0.16, 0.16, 0, 0.75, -15.8, M.woodLight);
box(fair, 40.5, 0.16, 0.16, 0, 1.45, -15.8, M.woodLight);
// -------------------------------------------------------------------------
// Booth builder
// -------------------------------------------------------------------------
function createBooth(x, z, title, accent, kind) {
const booth = new THREE.Group();
booth.position.set(x, 0.24, z);
fair.add(booth);
box(booth, 5.2, 3.2, 0.32, 0, 1.85, -1.35, M.cream);
box(booth, 5.4, 0.3, 3.3, 0, 0.25, 0.25, M.wood);
for (const sx of [-2.25, 2.25]) {
for (const sz of [-1.15, 1.2]) {
box(booth, 0.2, 3.75, 0.2, sx, 2.1, sz, M.woodLight);
}
}
// Striped canopy.
const stripeMaterials = [accent, M.cream];
for (let i = 0; i < 5; i++) {
box(
booth,
1.08,
0.48,
3.5,
-2.16 + i * 1.08,
4.02,
0.25,
stripeMaterials[i % 2]
);
box(
booth,
1.08,
0.72,
0.14,
-2.16 + i * 1.08,
3.65,
2.03,
stripeMaterials[i % 2]
);
}
box(booth, 4.8, 1.15, 0.7, 0, 0.85, 1.22, accent);
box(booth, 5.05, 0.18, 0.92, 0, 1.51, 1.22, M.woodLight);
addLabel(booth, title, 3.95, 0.83, 0, 3.67, 2.12, "#fff3d8", "#24444b");
// Small shelf and booth-specific goods.
box(booth, 4.4, 0.16, 0.55, 0, 2.3, -0.95, M.woodLight);
if (kind === "food") {
box(booth, 0.7, 0.45, 0.55, -1.2, 1.78, 1.2, M.coral);
box(booth, 0.65, 0.55, 0.5, 0, 1.85, 1.2, M.yellow);
box(booth, 0.75, 0.4, 0.5, 1.2, 1.75, 1.2, M.green);
box(booth, 0.6, 0.4, 0.55, -1.3, 2.62, -0.9, M.red);
box(booth, 0.6, 0.4, 0.55, 0, 2.62, -0.9, M.pink);
} else if (kind === "craft") {
box(booth, 0.55, 0.55, 0.55, -1.4, 1.84, 1.2, M.pink);
box(booth, 0.45, 0.7, 0.45, -0.2, 1.9, 1.2, M.blue);
box(booth, 0.7, 0.4, 0.6, 1.2, 1.75, 1.2, M.yellow);
box(booth, 0.65, 0.5, 0.5, -0.7, 2.68, -0.9, M.purple);
} else if (kind === "games") {
for (let i = 0; i < 4; i++) {
box(
booth,
0.35,
0.35,
0.35,
-1.5 + i * 0.45,
1.72 + (i % 2) * 0.35,
1.2,
[M.coral, M.blue, M.yellow, M.teal][i]
);
}
box(booth, 0.5, 0.5, 0.5, 1.4, 1.78, 1.2, M.purple);
} else {
box(booth, 0.55, 0.5, 0.55, -1.3, 1.8, 1.2, M.coral);
box(booth, 0.55, 0.5, 0.55, 0, 1.8, 1.2, M.wood);
box(booth, 0.55, 0.5, 0.55, 1.3, 1.8, 1.2, M.blue);
box(booth, 0.55, 0.65, 0.55, -1, 2.75, -0.9, M.green);
box(booth, 0.55, 0.65, 0.55, 0.2, 2.75, -0.9, M.greenDark);
}
return booth;
}
createBooth(-12, -11.2, "FOOD", M.coral, "food");
createBooth(-4, -11.2, "CRAFTS", M.yellow, "craft");
createBooth(4, -11.2, "GAMES", M.purple, "games");
createBooth(12, -11.2, "PLANTS", M.green, "plants");
// Bunting over the booth row.
box(fair, 29, 0.04, 0.04, 0, 5.05, -8.6, M.dark);
for (let i = 0; i < 22; i++) {
const x = -13.5 + i * 1.29;
const y = 4.74 - Math.abs(i - 10.5) * 0.035;
box(fair, 0.62, 0.56, 0.08, x, y, -8.6, flagColors[i % flagColors.length]);
}
// -------------------------------------------------------------------------
// Information station
// -------------------------------------------------------------------------
const info = new THREE.Group();
info.position.set(10.5, 0.24, 0.3);
info.rotation.y = -0.12;
fair.add(info);
box(info, 4.1, 1.15, 1.35, 0, 0.8, 0, M.teal);
box(info, 4.4, 0.18, 1.6, 0, 1.46, 0, M.cream);
for (const x of [-1.45, 1.45]) {
box(info, 0.18, 3.2, 0.18, x, 1.9, -0.6, M.wood);
}
box(info, 3.55, 1.9, 0.3, 0, 3.05, -0.62, M.cream);
addLabel(info, "INFO", 3.1, 1.35, 0, 3.05, -0.44, "#fff3d8", "#277d75");
addLabel(info, "MAP • HELP", 2.4, 0.48, 0, 1.47, 0.82, "#ffffff", "#28746e");
// Small map blocks on the tabletop.
box(info, 0.55, 0.12, 0.45, -1.1, 1.62, 0, M.yellow);
box(info, 0.35, 0.15, 0.3, -0.35, 1.64, -0.15, M.coral);
box(info, 0.42, 0.15, 0.3, 0.35, 1.64, 0.1, M.blue);
box(info, 0.5, 0.12, 0.42, 1.15, 1.62, -0.05, M.purple);
// -------------------------------------------------------------------------
// Seating, signs, lamps, trees, and decorations
// -------------------------------------------------------------------------
function picnicTable(x, z, rotation = 0) {
const table = new THREE.Group();
table.position.set(x, 0.24, z);
table.rotation.y = rotation;
fair.add(table);
box(table, 4.1, 0.28, 1.55, 0, 1.32, 0, M.woodLight);
box(table, 3.8, 0.18, 0.4, 0, 0.6, 0, M.wood);
for (const xSide of [-1.45, 1.45]) {
box(table, 0.18, 1.25, 1.1, xSide, 0.65, 0, M.wood);
}
box(table, 3.8, 0.24, 0.55, 0, 0.83, -1.3, M.woodLight);
box(table, 3.8, 0.24, 0.55, 0, 0.83, 1.3, M.woodLight);
box(table, 0.14, 4.0, 0.14, 0, 2.0, 0, M.wood);
box(table, 4.8, 0.35, 4.8, 0, 4.0, 0, M.coral);
box(table, 3.9, 0.35, 3.9, 0, 4.3, 0, M.cream);
box(table, 3.0, 0.35, 3.0, 0, 4.6, 0, M.coral);
return table;
}
picnicTable(-11.3, 4.5, 0.08);
// Seating sign.
box(fair, 0.18, 3.2, 0.18, -8.2, 1.65, 1.7, M.wood);
box(fair, 3.0, 1.0, 0.18, -8.2, 3.0, 1.7, M.cream);
addLabel(fair, "SEATING", 2.75, 0.72, -8.2, 3.0, 1.81, "#fff3d8", "#24444b");
// Benches close to the information desk.
for (const z of [5.0, 7.3]) {
box(fair, 4.6, 0.25, 0.7, 12.2, 1.0, z, M.woodLight);
box(fair, 4.6, 0.85, 0.18, 12.2, 1.45, z - 0.35, M.wood);
for (const x of [10.5, 13.9]) {
box(fair, 0.22, 0.9, 0.6, x, 0.52, z, M.wood);
}
}
function createTree(x, z, scale = 1) {
const tree = new THREE.Group();
tree.position.set(x, 0.24, z);
tree.scale.setScalar(scale);
fair.add(tree);
box(tree, 0.75, 3.2, 0.75, 0, 1.6, 0, M.trunk);
box(tree, 3.6, 2.1, 3.6, 0, 3.65, 0, M.green);
box(tree, 2.7, 1.7, 2.7, 0.7, 4.75, -0.2, M.greenDark);
box(tree, 2.2, 1.5, 2.2, -0.75, 4.6, 0.35, M.green);
box(tree, 1.6, 1.2, 1.6, 0.2, 5.65, 0.1, M.greenDark);
}
createTree(-18, -12.5, 1.25);
createTree(18.2, -12.5, 1.25);
createTree(-18, 12.5, 1.15);
createTree(18.3, 12.5, 1.15);
createTree(-16.8, -3.7, 0.85);
createTree(17.2, -3.8, 0.85);
function createLamp(x, z) {
const lamp = new THREE.Group();
lamp.position.set(x, 0.24, z);
fair.add(lamp);
box(lamp, 0.22, 4.4, 0.22, 0, 2.2, 0, M.dark);
box(lamp, 0.9, 0.22, 0.9, 0, 0.11, 0, M.dark);
box(lamp, 0.72, 0.85, 0.72, 0, 4.6, 0, M.lamp);
box(lamp, 0.95, 0.18, 0.95, 0, 5.1, 0, M.dark);
box(lamp, 0.28, 0.25, 0.28, 0, 5.28, 0, M.dark);
}
createLamp(-7.8, 8.2);
createLamp(7.8, 8.2);
createLamp(-14.3, -6.2);
createLamp(14.3, -6.2);
function createBalloon(x, z, color, height) {
const balloon = new THREE.Group();
balloon.position.set(x, 0.24, z);
fair.add(balloon);
box(balloon, 0.025, height, 0.025, 0, height / 2, 0, M.dark);
box(balloon, 0.66, 0.78, 0.5, 0, height + 0.35, 0, color);
box(balloon, 0.32, 0.25, 0.32, 0, height + 0.82, 0, color);
box(balloon, 0.14, 0.16, 0.14, 0, height + 0.99, 0, color);
}
createBalloon(-7.5, 9.8, M.coral, 3.4);
createBalloon(-8.1, 9.8, M.yellow, 2.7);
createBalloon(7.7, 9.8, M.blue, 3.0);
createBalloon(8.3, 9.8, M.pink, 2.4);
// Small flower beds.
for (const side of [-1, 1]) {
for (let i = 0; i < 6; i++) {
const x = side * (15.2 + (i % 2) * 0.55);
const z = -1 + i * 1.7;
box(fair, 0.15, 0.45, 0.15, x, 0.48, z, M.greenDark);
box(
fair,
0.34,
0.28,
0.34,
x,
0.78,
z,
[M.coral, M.yellow, M.pink, M.purple][i % 4]
);
}
}
// Pixel arrows marking routes to the booths, information, and seating.
function pixelArrow(x, z, rotation, color = M.route) {
const arrow = new THREE.Group();
arrow.position.set(x, 0.29, z);
arrow.rotation.y = rotation;
fair.add(arrow);
box(arrow, 0.28, 0.06, 1.3, 0, 0, 0.25, color);
box(arrow, 0.28, 0.06, 0.65, -0.35, 0, 0.75, color);
box(arrow, 0.28, 0.06, 0.65, 0.35, 0, 0.75, color);
box(arrow, 0.28, 0.06, 0.75, 0, 0, 1.05, color);
}
for (const z of [7.8, 4.8, 1.8, -1.2, -4.2]) {
pixelArrow(0, z, Math.PI);
}
pixelArrow(2.2, -4.6, Math.PI / 2);
pixelArrow(-2.2, -4.6, -Math.PI / 2);
pixelArrow(7.0, -2.8, Math.PI / 2);
pixelArrow(-7.0, 1.5, -Math.PI / 2);
// Voxel clouds in the distance.
function createCloud(x, y, z, scale) {
const cloud = new THREE.Group();
cloud.position.set(x, y, z);
cloud.scale.setScalar(scale);
scene.add(cloud);
const parts = [
[-1.2, 0, 0, 1.8],
[0, 0.25, 0, 2.2],
[1.3, 0, 0, 1.6],
[-0.4, -0.45, 0, 1.7]
];
for (const [px, py, pz, size] of parts) {
const mesh = box(cloud, size, 1.1, 1.2, px, py, pz, M.cloud);
mesh.castShadow = false;
}
}
createCloud(-14, 14, -25, 1.25);
createCloud(12, 16, -28, 1.55);
createCloud(3, 19, -35, 1.0);
// -------------------------------------------------------------------------
// Voxel people
// -------------------------------------------------------------------------
const people = [];
const volunteers = [];
function createPerson({
x = 0,
z = 0,
shirt = M.coral,
pants = M.pants,
skin = M.skin1,
hair = M.hair1,
rotation = 0,
scale = 1,
type = "visitor",
pointing = null,
phase = 0,
movement = "static"
}) {
const root = new THREE.Group();
root.position.set(x, 0.25, z);
root.rotation.y = rotation;
root.scale.setScalar(scale);
fair.add(root);
// Legs.
const leftLeg = new THREE.Group();
const rightLeg = new THREE.Group();
leftLeg.position.set(-0.2, 1.0, 0);
rightLeg.position.set(0.2, 1.0, 0);
root.add(leftLeg, rightLeg);
box(leftLeg, 0.28, 0.72, 0.32, 0, -0.36, 0, pants);
box(rightLeg, 0.28, 0.72, 0.32, 0, -0.36, 0, pants);
box(leftLeg, 0.32, 0.18, 0.43, 0, -0.77, 0.07, M.dark);
box(rightLeg, 0.32, 0.18, 0.43, 0, -0.77, 0.07, M.dark);
// Torso.
box(root, 0.82, 0.9, 0.48, 0, 1.4, 0, shirt);
box(root, 0.86, 0.13, 0.5, 0, 1.05, 0, pants);
// Head.
box(root, 0.66, 0.68, 0.62, 0, 2.18, 0, skin);
box(root, 0.7, 0.23, 0.65, 0, 2.5, -0.015, hair);
box(root, 0.16, 0.3, 0.18, -0.27, 2.33, 0.29, hair);
box(root, 0.12, 0.12, 0.08, -0.16, 2.23, 0.33, M.dark);
box(root, 0.12, 0.12, 0.08, 0.16, 2.23, 0.33, M.dark);
box(root, 0.13, 0.12, 0.09, 0, 2.09, 0.34, skin);
// Arms.
const leftArm = new THREE.Group();
const rightArm = new THREE.Group();
leftArm.position.set(-0.52, 1.72, 0);
rightArm.position.set(0.52, 1.72, 0);
root.add(leftArm, rightArm);
box(leftArm, 0.25, 0.68, 0.28, 0, -0.34, 0, shirt);
box(rightArm, 0.25, 0.68, 0.28, 0, -0.34, 0, shirt);
box(leftArm, 0.25, 0.22, 0.27, 0, -0.75, 0, skin);
box(rightArm, 0.25, 0.22, 0.27, 0, -0.75, 0, skin);
leftArm.rotation.z = -0.12;
rightArm.rotation.z = 0.12;
if (pointing === "left") {
leftArm.rotation.z = -Math.PI / 2;
} else if (pointing === "right") {
rightArm.rotation.z = Math.PI / 2;
}
if (type === "volunteer") {
// Bright vest and badge make the guides easy to identify.
box(root, 0.48, 0.58, 0.06, -0.16, 1.43, 0.27, M.yellow);
box(root, 0.16, 0.2, 0.07, 0.22, 1.54, 0.27, M.cream);
box(root, 0.72, 0.16, 0.56, 0, 2.57, 0, M.tealDark);
box(root, 0.76, 0.1, 0.3, 0, 2.5, 0.22, M.yellow);
volunteers.push(root);
}
root.userData = {
type,
leftLeg,
rightLeg,
leftArm,
rightArm,
pointing,
phase,
movement,
baseY: 0.25,
baseX: x,
baseZ: z
};
people.push(root);
return root;
}
// Entrance volunteers point toward the information and seating areas.
createPerson({
x: -3.9,
z: 7.3,
shirt: M.teal,
skin: M.skin2,
hair: M.hair2,
rotation: 0.08,
pointing: "right",
type: "volunteer",
phase: 0.2
});
createPerson({
x: -8.2,
z: -5.6,
shirt: M.teal,
skin: M.skin1,
hair: M.hair3,
rotation: -0.08,
pointing: "right",
type: "volunteer",
phase: 1.7
});
createPerson({
x: 7.3,
z: -5.4,
shirt: M.teal,
skin: M.skin3,
hair: M.hair1,
rotation: 0.12,
pointing: "left",
type: "volunteer",
phase: 3.0
});
// Newcomers entering the fair.
createPerson({
x: 0.1,
z: 13.2,
shirt: M.coral,
skin: M.skin1,
hair: M.hair2,
rotation: Math.PI,
phase: 0.1,
movement: "walkIn"
});
createPerson({
x: -1.2,
z: 12.0,
shirt: M.yellow,
skin: M.skin3,
hair: M.hair3,
rotation: Math.PI,
scale: 0.72,
phase: 0.5,
movement: "walkIn"
});
createPerson({
x: 1.1,
z: 12.4,
shirt: M.purple,
skin: M.skin2,
hair: M.hair1,
rotation: Math.PI,
scale: 0.92,
phase: 0.8,
movement: "walkIn"
});
// Visitors exploring the booths and information desk.
createPerson({
x: 8.6,
z: -2.5,
shirt: M.pink,
skin: M.skin1,
hair: M.hair3,
rotation: Math.PI / 2,
movement: "stroll",
phase: 2.1
});
createPerson({
x: 2.2,
z: -8.2,
shirt: M.blue,
skin: M.skin2,
hair: M.hair2,
rotation: -0.3,
movement: "stroll",
phase: 3.7
});
createPerson({
x: -10.0,
z: 3.0,
shirt: M.green,
skin: M.skin3,
hair: M.hair1,
rotation: 0.5,
phase: 4.2
});
// A newcomer with a suitcase beside the entrance.
const suitcaseOwner = people[3];
box(suitcaseOwner, 0.48, 0.72, 0.38, 0.85, 0.42, 0, M.red);
box(suitcaseOwner, 0.09, 0.28, 0.09, 0.85, 0.9, 0, M.dark);
box(suitcaseOwner, 0.12, 0.12, 0.12, 0.85, 1.05, 0, M.dark);
// -------------------------------------------------------------------------
// Custom camera controls
// -------------------------------------------------------------------------
const controls = {
target: new THREE.Vector3(0, 1.2, 0),
azimuth: 0.53,
polar: 0.79,
radius: 47
};
const homeControls = {
target: controls.target.clone(),
azimuth: controls.azimuth,
polar: controls.polar,
radius: controls.radius
};
function updateCamera() {
const sinPolar = Math.sin(controls.polar);
camera.position.set(
controls.target.x +
controls.radius * sinPolar * Math.sin(controls.azimuth),
controls.target.y + controls.radius * Math.cos(controls.polar),
controls.target.z +
controls.radius * sinPolar * Math.cos(controls.azimuth)
);
camera.lookAt(controls.target);
}
function resetCamera() {
controls.target.copy(homeControls.target);
controls.azimuth = homeControls.azimuth;
controls.polar = homeControls.polar;
controls.radius = homeControls.radius;
updateCamera();
}
const canvas = renderer.domElement;
let drag = null;
canvas.addEventListener("pointerdown", (event) => {
drag = {
x: event.clientX,
y: event.clientY,
mode: event.shiftKey ? "pan" : "orbit"
};
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!drag) return;
const dx = event.clientX - drag.x;
const dy = event.clientY - drag.y;
drag.x = event.clientX;
drag.y = event.clientY;
if (drag.mode === "orbit") {
controls.azimuth -= dx * 0.006;
controls.polar = THREE.MathUtils.clamp(
controls.polar + dy * 0.005,
0.28,
1.38
);
} else {
const forward = camera.getWorldDirection(new THREE.Vector3());
forward.y = 0;
forward.normalize();
const right = new THREE.Vector3().crossVectors(
forward,
camera.up
).normalize();
const up = new THREE.Vector3(0, 1, 0)
.applyQuaternion(camera.quaternion)
.normalize();
const scale = controls.radius * 0.0012;
controls.target.addScaledVector(right, -dx * scale);
controls.target.addScaledVector(up, dy * scale);
}
updateCamera();
});
function endDrag(event) {
drag = null;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.radius = THREE.MathUtils.clamp(
controls.radius * Math.exp(event.deltaY * 0.001),
25,
72
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
document.getElementById("reset").addEventListener("click", resetCamera);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
updateCamera();
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
for (const person of people) {
const data = person.userData;
const phase = data.phase;
if (data.type === "volunteer") {
person.position.y = data.baseY + Math.sin(elapsed * 1.8 + phase) * 0.015;
if (data.pointing === "right") {
data.rightArm.rotation.z =
Math.PI / 2 + Math.sin(elapsed * 3.1 + phase) * 0.16;
} else if (data.pointing === "left") {
data.leftArm.rotation.z =
-Math.PI / 2 + Math.sin(elapsed * 3.1 + phase) * 0.16;
}
} else if (data.movement === "walkIn") {
const cycle = (elapsed * 0.055 + phase) % 1;
person.position.z = data.baseZ - cycle * 7.2;
person.position.x =
data.baseX + Math.sin(cycle * Math.PI * 2) * 0.18;
person.position.y = data.baseY + Math.abs(Math.sin(elapsed * 7)) * 0.035;
const swing = Math.sin(elapsed * 7 + phase) * 0.52;
data.leftLeg.rotation.x = swing;
data.rightLeg.rotation.x = -swing;
data.leftArm.rotation.x = -swing * 0.65;
data.rightArm.rotation.x = swing * 0.65;
} else if (data.movement === "stroll") {
person.position.x =
data.baseX + Math.sin(elapsed * 0.42 + phase) * 0.75;
person.position.z =
data.baseZ + Math.cos(elapsed * 0.36 + phase) * 0.25;
person.position.y = data.baseY + Math.abs(Math.sin(elapsed * 5)) * 0.02;
const swing = Math.sin(elapsed * 5 + phase) * 0.28;
data.leftLeg.rotation.x = swing;
data.rightLeg.rotation.x = -swing;
} else {
person.position.y =
data.baseY + Math.sin(elapsed * 1.6 + phase) * 0.012;
}
}
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```