Prompt specification
Build or customize the Three.js 3D scene "Sand-table bridge village" 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: Sand-table bridge village
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/sand-table-bridge-village-x0391
Scene Description Prompt:
Make one HTML file using Three.js from cdnjs for this stylized scene: Build a miniature river crossing with block arches, roads, and toy carts; carts follow the completed bridge while incomplete crossings remain visibly disconnected.
Working Three.js Starter (sand-table-bridge-village-x0391):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>The Little River Crossing</title>
<style>
:root {
--ink: #29433d;
--paper: rgba(255, 250, 238, 0.9);
--line: rgba(41, 67, 61, 0.14);
--green: #3d8c73;
--orange: #d66b4b;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9dfcc;
color: var(--ink);
font-family: Inter, ui-rounded, "Avenir Next", system-ui, -apple-system, sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active { cursor: grabbing; }
.panel {
position: fixed;
top: 22px;
left: 22px;
width: min(350px, calc(100vw - 44px));
padding: 20px 22px;
border: 1px solid rgba(255, 255, 255, 0.58);
border-radius: 20px;
background: var(--paper);
box-shadow: 0 18px 50px rgba(67, 54, 36, 0.15);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
pointer-events: none;
user-select: none;
}
.eyebrow {
margin-bottom: 6px;
color: var(--green);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(27px, 4vw, 38px);
line-height: 1;
letter-spacing: -0.045em;
}
.panel p {
margin: 11px 0 15px;
color: #61716b;
font-size: 13px;
line-height: 1.55;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.chip {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 7px 10px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(255, 255, 255, 0.48);
color: #52655e;
font-size: 11px;
font-weight: 700;
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 3px rgba(61, 140, 115, 0.12);
}
.dot.warning {
background: var(--orange);
box-shadow: 0 0 0 3px rgba(214, 107, 75, 0.12);
}
.controls {
position: fixed;
right: 22px;
bottom: 22px;
display: flex;
gap: 8px;
}
button {
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 12px;
padding: 10px 13px;
background: rgba(255, 250, 238, 0.9);
box-shadow: 0 8px 24px rgba(67, 54, 36, 0.12);
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
backdrop-filter: blur(12px);
transition: transform 150ms ease, background 150ms ease;
}
button:hover {
transform: translateY(-2px);
background: #fffaf0;
}
button:active { transform: translateY(0); }
.hint {
position: fixed;
left: 22px;
bottom: 22px;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.42);
border-radius: 10px;
background: rgba(255, 250, 238, 0.72);
color: #68766f;
font-size: 11px;
pointer-events: none;
user-select: none;
}
@media (max-width: 700px) {
.panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 16px 17px;
}
.panel p { display: none; }
.hint { display: none; }
.controls {
right: 12px;
bottom: 12px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive miniature river crossing scene"></canvas>
<section class="panel">
<div class="eyebrow">A miniature traffic study</div>
<h1>The Little Crossing</h1>
<p>
Toy carts use the finished stone bridge. The unfinished crossings remain
visibly split, with barriers keeping traffic on the open route.
</p>
<div class="legend">
<span class="chip"><span class="dot"></span><span id="trafficStatus">Completed bridge in use</span></span>
<span class="chip"><span class="dot warning"></span>Incomplete spans disconnected</span>
</div>
</section>
<div class="hint">Drag to orbit · Scroll to zoom</div>
<div class="controls">
<button id="pauseButton" type="button">Pause carts</button>
<button id="resetButton" type="button">Reset view</button>
</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 pauseButton = document.querySelector("#pauseButton");
const resetButton = document.querySelector("#resetButton");
const trafficStatus = document.querySelector("#trafficStatus");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9dfcc);
scene.fog = new THREE.Fog(0xe9dfcc, 33, 61);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0.45, 0.3);
let cameraTheta = 0.69;
let cameraPhi = 0.76;
let cameraRadius = window.innerWidth < 700 ? 34 : 27;
let cameraChangedByUser = false;
function updateCamera() {
const horizontal = Math.sin(cameraPhi) * cameraRadius;
camera.position.set(
cameraTarget.x + horizontal * Math.sin(cameraTheta),
cameraTarget.y + Math.cos(cameraPhi) * cameraRadius,
cameraTarget.z + horizontal * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xfff4dc, 0x658273, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe7b7, 3.2);
sun.position.set(-9, 18, 11);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -19;
sun.shadow.camera.right = 19;
sun.shadow.camera.top = 19;
sun.shadow.camera.bottom = -19;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 48;
sun.shadow.normalBias = 0.035;
scene.add(sun);
scene.add(sun.target);
const fill = new THREE.DirectionalLight(0xb8dddb, 0.65);
fill.position.set(12, 8, -12);
scene.add(fill);
// -------------------------------------------------------------------------
// Materials and helpers
// -------------------------------------------------------------------------
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
flatShading: true,
...options
});
const materials = {
base: material(0xcba47c),
baseEdge: material(0xa97c59),
grass: material(0x82aa72),
grassDark: material(0x6d9564),
sand: material(0xd7bf8d),
road: material(0xc77d61),
roadEdge: material(0xb96e56),
water: material(0x59b9bd, { roughness: 0.28, metalness: 0.02 }),
waterDeep: material(0x348e9a, { roughness: 0.45 }),
wood: material(0x95613d),
woodLight: material(0xc28a55),
darkWood: material(0x68452f),
wheel: material(0x3d3932),
cream: material(0xf8e7c3),
leaf1: material(0x4f8963),
leaf2: material(0x6fa56d),
leaf3: material(0x87b66f),
trunk: material(0x80583c),
roof: material(0xb85f4d),
roofDark: material(0x93463b),
window: material(0x8bc7c9, { roughness: 0.25 }),
orange: material(0xe1744f),
red: material(0xc85445),
blue: material(0x4d7e9b),
teal: material(0x3e8d83),
hay: material(0xd2ab55)
};
const stoneMaterials = [
material(0xe6c89d),
material(0xf0d5aa),
material(0xdcb98a),
material(0xebcda0),
material(0xe0bf91)
];
const unfinishedStone = [
material(0xc5a77e),
material(0xd0b187),
material(0xb99972)
];
function addMesh(geometry, mat, x = 0, y = 0, z = 0, parent = scene) {
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 box(w, h, d, mat, x = 0, y = 0, z = 0, parent = scene) {
return addMesh(new THREE.BoxGeometry(w, h, d), mat, x, y, z, parent);
}
function cylinder(rt, rb, h, segments, mat, x = 0, y = 0, z = 0, parent = scene) {
return addMesh(
new THREE.CylinderGeometry(rt, rb, h, segments),
mat,
x,
y,
z,
parent
);
}
function makeRoundedShape(w, h, r) {
const shape = new THREE.Shape();
const x = -w / 2;
const y = -h / 2;
shape.moveTo(x + r, y);
shape.lineTo(x + w - r, y);
shape.quadraticCurveTo(x + w, y, x + w, y + r);
shape.lineTo(x + w, y + h - r);
shape.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
shape.lineTo(x + r, y + h);
shape.quadraticCurveTo(x, y + h, x, y + h - r);
shape.lineTo(x, y + r);
shape.quadraticCurveTo(x, y, x + r, y);
return shape;
}
// -------------------------------------------------------------------------
// Diorama base, banks, and river
// -------------------------------------------------------------------------
const baseGeometry = new THREE.ExtrudeGeometry(makeRoundedShape(27.5, 18.5, 0.85), {
depth: 0.72,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: 0.12,
bevelThickness: 0.1,
curveSegments: 12
});
baseGeometry.rotateX(Math.PI / 2);
addMesh(baseGeometry, materials.base, 0, -0.11, 0);
const lowerGeometry = new THREE.ExtrudeGeometry(makeRoundedShape(27.15, 18.15, 0.8), {
depth: 0.34,
bevelEnabled: true,
bevelSegments: 2,
bevelSize: 0.08,
bevelThickness: 0.07,
curveSegments: 10
});
lowerGeometry.rotateX(Math.PI / 2);
addMesh(lowerGeometry, materials.baseEdge, 0, -0.73, 0);
box(10.1, 0.26, 17.9, materials.grass, -8.3, 0.13, 0);
box(10.1, 0.26, 17.9, materials.grass, 8.3, 0.13, 0);
box(0.38, 0.22, 17.9, materials.sand, -3.34, 0.12, 0);
box(0.38, 0.22, 17.9, materials.sand, 3.34, 0.12, 0);
box(6.5, 0.1, 17.9, materials.waterDeep, 0, 0.035, 0);
box(6.46, 0.065, 17.84, materials.water, 0, 0.095, 0);
const shadowReceiver = new THREE.Mesh(
new THREE.CircleGeometry(38, 64),
new THREE.ShadowMaterial({ color: 0x6a5540, opacity: 0.18 })
);
shadowReceiver.rotation.x = -Math.PI / 2;
shadowReceiver.position.y = -0.9;
shadowReceiver.receiveShadow = true;
scene.add(shadowReceiver);
// -------------------------------------------------------------------------
// Roads, approaches, and ramps
// -------------------------------------------------------------------------
const ROAD_TOP = 0.39;
const BRIDGE_TOP = 1.72;
const BRIDGE_Z = 2.0;
const ROAD_WIDTH = 1.65;
box(2.25, 0.12, 15.7, materials.road, -7.2, 0.28, -0.15);
box(2.25, 0.12, 15.7, materials.road, 7.2, 0.28, -0.15);
box(14.4, 0.12, 1.65, materials.road, 0, 0.28, 7.2);
function makeRamp(x0, x1, y0, y1, z, width = ROAD_WIDTH, mat = materials.road) {
const shape = new THREE.Shape();
shape.moveTo(x0, 0.18);
shape.lineTo(x1, 0.18);
shape.lineTo(x1, y1);
shape.lineTo(x0, y0);
shape.closePath();
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: width,
bevelEnabled: false,
curveSegments: 1
});
geometry.translate(0, 0, -width / 2);
return addMesh(geometry, mat, 0, 0, z);
}
function addRampDashes(x0, x1, y0, y1, z, count = 8) {
const angle = Math.atan2(y1 - y0, x1 - x0);
for (let i = 0; i < count; i++) {
const t = (i + 0.5) / count;
const dash = box(
0.33,
0.018,
0.085,
materials.cream,
THREE.MathUtils.lerp(x0, x1, t),
THREE.MathUtils.lerp(y0, y1, t) + 0.015,
z
);
dash.rotation.z = angle;
}
}
// Main open route approaches.
makeRamp(-7.2, -3.7, ROAD_TOP, BRIDGE_TOP, BRIDGE_Z);
makeRamp(3.7, 7.2, BRIDGE_TOP, ROAD_TOP, BRIDGE_Z);
addRampDashes(-7.2, -3.7, ROAD_TOP, BRIDGE_TOP, BRIDGE_Z);
addRampDashes(3.7, 7.2, BRIDGE_TOP, ROAD_TOP, BRIDGE_Z);
// The two unfinished crossings deliberately lead to gaps.
const incompleteZ = [-3.45, 5.55];
for (const z of incompleteZ) {
makeRamp(-7.2, -3.7, ROAD_TOP, BRIDGE_TOP, z);
makeRamp(3.7, 7.2, BRIDGE_TOP, ROAD_TOP, z);
addRampDashes(-7.2, -3.7, ROAD_TOP, BRIDGE_TOP, z, 6);
addRampDashes(3.7, 7.2, BRIDGE_TOP, ROAD_TOP, z, 6);
}
function addRoadDashes() {
for (let z = -7.1; z < 7.1; z += 1.2) {
box(0.09, 0.018, 0.48, materials.cream, -7.2, 0.348, z);
if (z < 1.3 || z > 2.7) {
box(0.09, 0.018, 0.48, materials.cream, 7.2, 0.348, z);
}
}
for (let x = -6.4; x < 6.5; x += 1.2) {
box(0.48, 0.018, 0.09, materials.cream, x, 0.348, 7.2);
}
}
addRoadDashes();
// -------------------------------------------------------------------------
// Block arches and bridge decks
// -------------------------------------------------------------------------
function archBlock(centerX, z, springY, innerR, outerR, a0, a1, width, mat) {
const shape = new THREE.Shape();
shape.moveTo(innerR * Math.cos(a0), innerR * Math.sin(a0));
shape.lineTo(outerR * Math.cos(a0), outerR * Math.sin(a0));
shape.lineTo(outerR * Math.cos(a1), outerR * Math.sin(a1));
shape.lineTo(innerR * Math.cos(a1), innerR * Math.sin(a1));
shape.closePath();
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: width,
bevelEnabled: true,
bevelSegments: 1,
bevelSize: 0.025,
bevelThickness: 0.025,
curveSegments: 1
});
geometry.translate(0, 0, -width / 2);
return addMesh(geometry, mat, centerX, springY, z);
}
function buildArch(centerX, z, mats, springY = 0.63, segments = 9) {
for (let i = 0; i < segments; i++) {
const a0 = (i / segments) * Math.PI + 0.012;
const a1 = ((i + 1) / segments) * Math.PI - 0.012;
archBlock(
centerX,
z,
springY,
0.58,
0.89,
a0,
a1,
1.55,
mats[i % mats.length]
);
}
}
function buildPiers(z, xs, mat) {
for (const x of xs) {
box(0.48, 0.25, 1.52, mat, x, 0.275, z);
box(0.43, 0.27, 1.57, stoneMaterials[1], x, 0.52, z);
}
}
// Completed center bridge.
buildPiers(BRIDGE_Z, [-2.88, -1, 1, 2.88], stoneMaterials[0]);
buildArch(-2, BRIDGE_Z, stoneMaterials);
buildArch(0, BRIDGE_Z, stoneMaterials);
buildArch(2, BRIDGE_Z, stoneMaterials);
for (let i = 0; i < 10; i++) {
const x = -3.7 + (i + 0.5) * 0.74;
box(0.71, 0.2, 1.68, stoneMaterials[(i + 1) % stoneMaterials.length], x, 1.62, BRIDGE_Z);
for (const side of [-1, 1]) {
box(
0.69,
0.27,
0.18,
stoneMaterials[(i + 2) % stoneMaterials.length],
x,
1.86,
BRIDGE_Z + side * 0.77
);
}
}
// Front unfinished bridge: two arches survive, but the middle span is absent.
const frontZ = incompleteZ[0];
buildPiers(frontZ, [-2.88, 2.88], unfinishedStone[0]);
buildArch(-2, frontZ, unfinishedStone, 0.57);
buildArch(2, frontZ, unfinishedStone, 0.57);
for (const side of [-1, 1]) {
box(2.58, 0.2, 1.68, unfinishedStone[1], side * 2.4, 1.62, frontZ);
box(0.4, 0.35, 1.74, unfinishedStone[0], side * 3.45, 1.5, frontZ);
}
// Rear unfinished crossing: only short disconnected stubs remain.
const rearZ = incompleteZ[1];
for (const side of [-1, 1]) {
box(1.25, 0.2, 1.68, unfinishedStone[1], side * 3.075, 1.62, rearZ);
box(0.48, 0.54, 1.74, unfinishedStone[0], side * 3.35, 1.41, rearZ);
}
// -------------------------------------------------------------------------
// Construction barriers, signs, crates, and flags
// -------------------------------------------------------------------------
function stripeTexture() {
const c = document.createElement("canvas");
c.width = 256;
c.height = 64;
const ctx = c.getContext("2d");
ctx.fillStyle = "#fff0cc";
ctx.fillRect(0, 0, c.width, c.height);
ctx.strokeStyle = "#e16b4c";
ctx.lineWidth = 19;
for (let x = -80; x < 330; x += 62) {
ctx.beginPath();
ctx.moveTo(x, c.height + 8);
ctx.lineTo(x + 54, -8);
ctx.stroke();
}
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.wrapS = THREE.RepeatWrapping;
texture.repeat.set(2.5, 1);
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
const barrierTexture = stripeTexture();
const barrierMaterial = new THREE.MeshStandardMaterial({
map: barrierTexture,
roughness: 0.8
});
function makeBarrier(x, z, deckY = BRIDGE_TOP) {
const group = new THREE.Group();
group.position.set(x, deckY, z);
box(0.12, 0.29, 1.25, barrierMaterial, 0, 0.47, 0, group);
box(0.13, 0.78, 0.13, materials.darkWood, 0, 0.37, -0.48, group);
box(0.13, 0.78, 0.13, materials.darkWood, 0, 0.37, 0.48, group);
scene.add(group);
return group;
}
makeBarrier(-1.06, frontZ);
makeBarrier(1.06, frontZ);
makeBarrier(-2.43, rearZ);
makeBarrier(2.43, rearZ);
function makeCone(x, y, z) {
cylinder(0.05, 0.18, 0.42, 10, materials.orange, x, y + 0.21, z);
const band = cylinder(0.09, 0.12, 0.1, 10, materials.cream, x, y + 0.25, z);
band.receiveShadow = true;
}
makeCone(-1.45, BRIDGE_TOP, frontZ + 0.55);
makeCone(1.42, BRIDGE_TOP, frontZ - 0.54);
makeCone(-2.7, BRIDGE_TOP, rearZ + 0.48);
makeCone(2.75, BRIDGE_TOP, rearZ - 0.45);
function makeCrate(x, y, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, y, z);
group.scale.setScalar(scale);
box(0.58, 0.48, 0.58, materials.woodLight, 0, 0.24, 0, group);
box(0.62, 0.07, 0.09, materials.darkWood, 0, 0.16, 0.31, group);
box(0.62, 0.07, 0.09, materials.darkWood, 0, 0.38, 0.31, group);
box(0.09, 0.07, 0.62, materials.darkWood, 0.31, 0.16, 0, group);
box(0.09, 0.07, 0.62, materials.darkWood, 0.31, 0.38, 0, group);
scene.add(group);
}
makeCrate(-4.75, 0.26, -4.6, 0.9);
makeCrate(-4.22, 0.26, -4.45, 0.72);
makeCrate(4.45, 0.26, 6.45, 0.85);
function makeLabel(text, width = 2.2, background = "#fff3d7", foreground = "#31584d") {
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 = foreground;
ctx.lineWidth = 9;
ctx.strokeRect(8, 8, c.width - 16, c.height - 16);
ctx.fillStyle = foreground;
ctx.font = "800 55px 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;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({ map: texture, transparent: true })
);
sprite.scale.set(width, width * 0.25, 1);
return sprite;
}
const openLabel = makeLabel("OPEN BRIDGE", 2.25, "#e6f3df", "#34755f");
openLabel.position.set(-4.25, 2.63, BRIDGE_Z);
scene.add(openLabel);
const unfinishedLabel = makeLabel("SPAN MISSING", 2.55, "#ffe8cc", "#a64f39");
unfinishedLabel.position.set(0, 2.75, frontZ);
scene.add(unfinishedLabel);
const rearLabel = makeLabel("WORK ZONE", 2.1, "#ffe8cc", "#a64f39");
rearLabel.position.set(0, 2.68, rearZ);
scene.add(rearLabel);
const flags = [];
function makeFlag(x, z) {
const group = new THREE.Group();
group.position.set(x, BRIDGE_TOP, z);
cylinder(0.035, 0.045, 1.2, 8, materials.darkWood, 0, 0.6, 0, group);
const flagShape = new THREE.Shape();
flagShape.moveTo(0, 0);
flagShape.lineTo(0.54, 0.14);
flagShape.lineTo(0, 0.29);
flagShape.closePath();
const flagMat = new THREE.MeshStandardMaterial({
color: 0x3d8c73,
side: THREE.DoubleSide,
roughness: 0.8
});
const flag = addMesh(
new THREE.ShapeGeometry(flagShape),
flagMat,
0.02,
0.98,
0,
group
);
group.userData.flag = flag;
flags.push(group);
scene.add(group);
}
makeFlag(-3.95, BRIDGE_Z);
makeFlag(3.95, BRIDGE_Z);
// -------------------------------------------------------------------------
// Trees, bushes, houses, rocks, and river details
// -------------------------------------------------------------------------
let seed = 9281;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
function makeTree(x, z, scale = 1, leaf = materials.leaf1) {
const group = new THREE.Group();
group.position.set(x, 0.26, z);
group.scale.setScalar(scale);
group.rotation.y = random() * Math.PI * 2;
cylinder(0.14, 0.19, 1.12, 7, materials.trunk, 0, 0.56, 0, group);
const crown = addMesh(
new THREE.DodecahedronGeometry(0.72, 0),
leaf,
0,
1.48,
0,
group
);
crown.scale.set(1, 1.15, 1);
const top = addMesh(
new THREE.ConeGeometry(0.48, 0.9, 7),
leaf,
0.05,
2.02,
0,
group
);
top.rotation.y = 0.3;
scene.add(group);
}
const trees = [
[-11.2, -6.7, 1.05, materials.leaf1],
[-10.5, -1.8, 0.82, materials.leaf3],
[-11.2, 3.6, 1.12, materials.leaf2],
[-9.8, 6.5, 0.92, materials.leaf1],
[-4.9, -7.2, 0.85, materials.leaf3],
[-4.7, 4.7, 0.76, materials.leaf2],
[10.8, -6.8, 1.0, materials.leaf2],
[11.2, -0.8, 0.83, materials.leaf1],
[10.7, 3.1, 1.08, materials.leaf3],
[9.7, 6.5, 0.78, materials.leaf2],
[4.8, -7.3, 0.92, materials.leaf1]
];
for (const [x, z, s, leaf] of trees) makeTree(x, z, s, leaf);
function makeBush(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0.26, z);
group.scale.setScalar(scale);
addMesh(new THREE.DodecahedronGeometry(0.36, 0), materials.leaf2, -0.22, 0.25, 0, group);
addMesh(new THREE.DodecahedronGeometry(0.43, 0), materials.leaf1, 0.2, 0.31, 0.05, group);
addMesh(new THREE.DodecahedronGeometry(0.29, 0), materials.leaf3, 0.02, 0.49, 0.04, group);
scene.add(group);
}
[
[-12.1, -4.8, 1.0],
[-5.0, -1.7, 0.8],
[-8.8, 5.7, 0.9],
[11.7, -3.3, 1.0],
[5.0, 4.3, 0.85],
[9.6, 6.8, 0.7]
].forEach(([x, z, s]) => makeBush(x, z, s));
function makeHouse(x, z, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0.26, z);
group.rotation.y = rotation;
box(1.8, 1.35, 1.5, materials.cream, 0, 0.68, 0, group);
const roof = addMesh(
new THREE.ConeGeometry(1.42, 0.9, 4),
materials.roof,
0,
1.79,
0,
group
);
roof.rotation.y = Math.PI / 4;
roof.scale.z = 0.88;
box(0.42, 0.78, 0.08, materials.darkWood, 0.36, 0.42, 0.79, group);
box(0.34, 0.34, 0.08, materials.window, -0.49, 0.83, 0.79, group);
box(0.06, 0.34, 0.09, materials.cream, -0.49, 0.83, 0.835, group);
box(0.34, 0.34, 0.08, materials.window, -0.49, 0.83, -0.79, group);
box(0.31, 0.62, 0.31, materials.roofDark, 0.45, 2.1, -0.25, group);
scene.add(group);
}
makeHouse(-10.35, -4.15, 0.08);
makeHouse(10.15, -3.85, -0.1);
function makeRock(x, y, z, size, mat = materials.sand) {
const rock = addMesh(
new THREE.DodecahedronGeometry(size, 0),
mat,
x,
y,
z
);
rock.scale.set(1.25, 0.8, 1);
rock.rotation.set(random(), random(), random());
}
[
[-2.95, 0.27, -6.8, 0.34],
[-2.9, 0.26, -1.6, 0.25],
[-3.0, 0.25, 3.8, 0.29],
[2.92, 0.26, -5.4, 0.3],
[2.95, 0.25, 0.7, 0.23],
[2.88, 0.26, 4.2, 0.32],
[-11.9, 0.3, 1.5, 0.31],
[12.0, 0.29, 0.4, 0.27]
].forEach(([x, y, z, s]) => makeRock(x, y, z, s));
// Small river ripples.
const ripples = [];
for (let i = 0; i < 22; i++) {
const rippleMaterial = new THREE.MeshBasicMaterial({
color: 0xd7ffff,
transparent: true,
opacity: 0.18,
depthWrite: false
});
const ripple = addMesh(
new THREE.TorusGeometry(0.24, 0.018, 5, 22),
rippleMaterial,
-2.55 + random() * 5.1,
0.14,
-8.1 + random() * 16.2
);
ripple.rotation.x = Math.PI / 2;
ripple.scale.set(1.25 + random() * 0.9, 0.42 + random() * 0.25, 1);
ripple.castShadow = false;
ripple.receiveShadow = false;
ripple.userData.phase = random() * Math.PI * 2;
ripple.userData.baseScale = ripple.scale.x;
ripples.push(ripple);
}
// A tiny duck in the river.
const duck = new THREE.Group();
duck.position.set(1.35, 0.22, -6.5);
duck.rotation.y = -0.45;
const duckBody = addMesh(
new THREE.SphereGeometry(0.24, 12, 8),
materials.hay,
0,
0,
0,
duck
);
duckBody.scale.set(1.25, 0.72, 0.88);
addMesh(new THREE.SphereGeometry(0.13, 10, 7), materials.hay, 0.18, 0.19, 0, duck);
const beak = addMesh(
new THREE.ConeGeometry(0.065, 0.18, 8),
materials.orange,
0.3,
0.18,
0,
duck
);
beak.rotation.z = -Math.PI / 2;
scene.add(duck);
// -------------------------------------------------------------------------
// Cart route: the only bridge crossed is the completed center bridge.
// -------------------------------------------------------------------------
const route = new THREE.CurvePath();
const v = (x, y, z) => new THREE.Vector3(x, y, z);
route.add(new THREE.LineCurve3(v(-7.2, 0.405, -6.7), v(-7.2, 0.405, 1.2)));
route.add(
new THREE.QuadraticBezierCurve3(
v(-7.2, 0.405, 1.2),
v(-7.2, 0.405, 2),
v(-6.25, 0.755, 2)
)
);
route.add(new THREE.LineCurve3(v(-6.25, 0.755, 2), v(-3.7, 1.745, 2)));
route.add(new THREE.LineCurve3(v(-3.7, 1.745, 2), v(3.7, 1.745, 2)));
route.add(new THREE.LineCurve3(v(3.7, 1.745, 2), v(6.25, 0.755, 2)));
route.add(
new THREE.QuadraticBezierCurve3(
v(6.25, 0.755, 2),
v(7.2, 0.405, 2),
v(7.2, 0.405, 1.2)
)
);
// Continue around the rear service road and return to the start.
route.add(new THREE.LineCurve3(v(7.2, 0.405, 1.2), v(7.2, 0.405, 6.35)));
route.add(
new THREE.QuadraticBezierCurve3(
v(7.2, 0.405, 6.35),
v(7.2, 0.405, 7.2),
v(6.25, 0.405, 7.2)
)
);
route.add(new THREE.LineCurve3(v(6.25, 0.405, 7.2), v(-6.25, 0.405, 7.2)));
route.add(
new THREE.QuadraticBezierCurve3(
v(-6.25, 0.405, 7.2),
v(-7.2, 0.405, 7.2),
v(-7.2, 0.405, 6.35)
)
);
route.add(new THREE.LineCurve3(v(-7.2, 0.405, 6.35), v(-7.2, 0.405, -6.7)));
function makeCart(accent, cargoColor, index) {
const group = new THREE.Group();
const wheels = [];
box(0.98, 0.15, 0.64, materials.woodLight, 0, 0.39, 0, group);
box(0.98, 0.3, 0.08, accent, 0, 0.55, 0.33, group);
box(0.98, 0.3, 0.08, accent, 0, 0.55, -0.33, group);
box(0.08, 0.3, 0.64, accent, 0.48, 0.55, 0, group);
box(0.08, 0.23, 0.64, accent, -0.48, 0.51, 0, group);
for (const x of [-0.33, 0.33]) {
for (const z of [-0.39, 0.39]) {
const wheelGeometry = new THREE.CylinderGeometry(0.19, 0.19, 0.1, 14);
wheelGeometry.rotateX(Math.PI / 2);
const wheel = addMesh(wheelGeometry, materials.wheel, x, 0.2, z, group);
wheels.push(wheel);
const hub = addMesh(
new THREE.CylinderGeometry(0.07, 0.07, 0.115, 10),
materials.cream,
x,
0.2,
z,
group
);
hub.rotation.x = Math.PI / 2;
}
}
// Pull handle.
box(0.66, 0.075, 0.075, materials.darkWood, 0.77, 0.5, 0.22, group);
box(0.66, 0.075, 0.075, materials.darkWood, 0.77, 0.5, -0.22, group);
box(0.08, 0.075, 0.55, materials.darkWood, 1.07, 0.5, 0, group);
// Chunky toy cargo.
for (let i = 0; i < 3; i++) {
const cargo = addMesh(
new THREE.DodecahedronGeometry(0.16 + (index % 2) * 0.015, 0),
cargoColor,
-0.28 + i * 0.27,
0.67 + (i === 1 ? 0.1 : 0),
i === 1 ? 0.08 : -0.08,
group
);
cargo.rotation.set(random(), random(), random());
}
group.userData.wheels = wheels;
scene.add(group);
return group;
}
const carts = [
makeCart(materials.teal, materials.red, 0),
makeCart(materials.blue, materials.hay, 1),
makeCart(materials.orange, materials.cream, 2)
];
const cartPhases = [0.11, 0.39, 0.68];
const routeLength = route.getLength();
// -------------------------------------------------------------------------
// Custom orbit-style camera controls
// -------------------------------------------------------------------------
let dragging = false;
let previousX = 0;
let previousY = 0;
let dragDistance = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
dragDistance = 0;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
dragDistance += Math.abs(dx) + Math.abs(dy);
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(cameraPhi - dy * 0.005, 0.34, 1.27);
cameraChangedByUser = true;
updateCamera();
});
canvas.addEventListener("pointerup", (event) => {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", () => {
dragging = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
16,
42
);
cameraChangedByUser = true;
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
cameraTheta = 0.69;
cameraPhi = 0.76;
cameraRadius = window.innerWidth < 700 ? 34 : 27;
cameraChangedByUser = false;
updateCamera();
});
resetButton.addEventListener("click", () => {
cameraTheta = 0.69;
cameraPhi = 0.76;
cameraRadius = window.innerWidth < 700 ? 34 : 27;
cameraChangedByUser = false;
updateCamera();
});
let trafficPaused = false;
pauseButton.addEventListener("click", () => {
trafficPaused = !trafficPaused;
pauseButton.textContent = trafficPaused ? "Resume carts" : "Pause carts";
trafficStatus.textContent = trafficPaused
? "Carts paused on the route"
: "Completed bridge in use";
});
window.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") cameraTheta += 0.08;
else if (event.key === "ArrowRight") cameraTheta -= 0.08;
else if (event.key === "ArrowUp") cameraPhi = Math.max(0.34, cameraPhi - 0.06);
else if (event.key === "ArrowDown") cameraPhi = Math.min(1.27, cameraPhi + 0.06);
else if (event.key === "+" || event.key === "=") cameraRadius = Math.max(16, cameraRadius - 1);
else if (event.key === "-") cameraRadius = Math.min(42, cameraRadius + 1);
else return;
cameraChangedByUser = true;
updateCamera();
});
window.addEventListener("resize", () => {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(width, height, false);
if (!cameraChangedByUser) {
cameraRadius = width < 700 ? 34 : 27;
updateCamera();
}
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let trafficTime = 0;
let visualTime = 0;
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
visualTime += delta;
if (!trafficPaused) trafficTime += delta;
carts.forEach((cart, index) => {
const progress = (cartPhases[index] + trafficTime * 0.026) % 1;
const point = route.getPointAt(progress);
const tangent = route.getTangentAt(progress).normalize();
cart.position.copy(point);
const yaw = Math.atan2(-tangent.z, tangent.x);
const horizontalLength = Math.hypot(tangent.x, tangent.z);
const pitch = Math.atan2(tangent.y, horizontalLength);
cart.rotation.set(0, yaw, pitch);
const spin = -(progress * routeLength) / 0.19;
cart.userData.wheels.forEach((wheel) => {
wheel.rotation.z = spin;
});
});
ripples.forEach((ripple) => {
const phase = ripple.userData.phase;
ripple.material.opacity = 0.12 + 0.1 * (0.5 + 0.5 * Math.sin(visualTime * 1.5 + phase));
const pulse = 1 + 0.08 * Math.sin(visualTime * 1.25 + phase);
ripple.scale.x = ripple.userData.baseScale * pulse;
});
duck.position.y = 0.22 + Math.sin(visualTime * 2.1) * 0.018;
duck.rotation.z = Math.sin(visualTime * 1.7) * 0.025;
flags.forEach((flag, index) => {
flag.userData.flag.rotation.y = Math.sin(visualTime * 2.4 + index) * 0.16;
flag.userData.flag.rotation.z = Math.sin(visualTime * 3.1 + index) * 0.025;
});
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```