Prompt specification
Build or customize the Three.js 3D scene "Faidherbe Bridge arch and deck fragment" 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: Faidherbe Bridge arch and deck fragment
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/faidherbe-bridge-arch-and-deck-fragment-x2949
Scene Description Prompt:
A voxel Faidherbe Bridge span in Saint-Louis, Senegal, with metal frame, deck, and river below. Three.js from cdnjs, one HTML file.
Working Three.js Starter (faidherbe-bridge-arch-and-deck-fragment-x2949):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Faidherbe Bridge — Saint-Louis, Senegal</title>
<style>
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b9d8df;
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;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
#vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 42%, transparent 48%, rgba(20, 35, 42, 0.22) 115%);
}
#title {
position: fixed;
top: 26px;
left: 28px;
z-index: 5;
color: #17343b;
pointer-events: none;
text-shadow: 0 1px 12px rgba(255, 255, 255, 0.35);
}
#title .eyebrow {
margin-bottom: 7px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.28em;
text-transform: uppercase;
opacity: 0.72;
}
#title h1 {
margin: 0;
font-size: clamp(25px, 4vw, 48px);
line-height: 0.95;
letter-spacing: -0.045em;
}
#title p {
margin: 10px 0 0;
font-size: 12px;
font-weight: 650;
letter-spacing: 0.12em;
text-transform: uppercase;
opacity: 0.72;
}
#controls {
position: fixed;
right: 22px;
top: 22px;
z-index: 10;
display: flex;
gap: 8px;
}
button {
border: 1px solid rgba(255, 255, 255, 0.45);
border-radius: 10px;
padding: 10px 13px;
color: #f8fbfb;
background: rgba(21, 49, 56, 0.72);
backdrop-filter: blur(12px);
box-shadow: 0 7px 24px rgba(19, 36, 40, 0.16);
font: 700 11px/1 Inter, ui-sans-serif, system-ui, sans-serif;
letter-spacing: 0.04em;
cursor: pointer;
transition: background 160ms ease, transform 160ms ease;
}
button:hover {
background: rgba(21, 49, 56, 0.92);
transform: translateY(-1px);
}
button.active {
color: #17343b;
background: #f0cf67;
}
#hint {
position: fixed;
left: 50%;
bottom: 20px;
z-index: 8;
transform: translateX(-50%);
padding: 9px 13px;
border: 1px solid rgba(255, 255, 255, 0.35);
border-radius: 999px;
color: rgba(255, 255, 255, 0.9);
background: rgba(20, 43, 49, 0.55);
backdrop-filter: blur(10px);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
}
#loading {
position: fixed;
inset: 0;
z-index: 30;
display: grid;
place-items: center;
color: #17343b;
background: #b9d8df;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
transition: opacity 400ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 640px) {
#title {
top: 18px;
left: 18px;
}
#title p {
font-size: 9px;
}
#controls {
top: auto;
right: 14px;
bottom: 48px;
}
button {
padding: 9px 10px;
font-size: 9px;
}
#hint {
bottom: 14px;
font-size: 8px;
}
}
</style>
</head>
<body>
<div id="vignette"></div>
<header id="title">
<div class="eyebrow">Saint-Louis · Senegal</div>
<h1>Faidherbe Bridge</h1>
<p>Voxel steel arch study · 1897</p>
</header>
<nav id="controls" aria-label="Scene controls">
<button id="rotate" type="button">Auto rotate</button>
<button id="reset" type="button">Reset view</button>
</nav>
<div id="hint">Drag to orbit · Scroll or pinch to zoom</div>
<div id="loading">Building the bridge…</div>
<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(0xb9d8df);
scene.fog = new THREE.Fog(0xb9d8df, 48, 105);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
180
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
document.body.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xe6f5ff, 0x73513b, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffedc7, 3.3);
sun.position.set(-24, 38, 26);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -42;
sun.shadow.camera.right = 42;
sun.shadow.camera.top = 42;
sun.shadow.camera.bottom = -42;
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(0x9bc9e6, 0.7);
fill.position.set(30, 16, -25);
scene.add(fill);
// -------------------------------------------------------------------------
// Voxel batching
// -------------------------------------------------------------------------
const blockBatches = new Map();
const materialOptions = {
iron: {
color: 0x344950,
roughness: 0.43,
metalness: 0.72
},
ironDark: {
color: 0x25383e,
roughness: 0.5,
metalness: 0.7
},
ironLight: {
color: 0x708087,
roughness: 0.4,
metalness: 0.68
},
road: {
color: 0x3f4849,
roughness: 0.93,
metalness: 0.03
},
walkway: {
color: 0xc8bda4,
roughness: 0.88,
metalness: 0.02
},
yellow: {
color: 0xe8c452,
roughness: 0.72,
metalness: 0.08
},
stone: {
color: 0xb8aa8c,
roughness: 0.93,
metalness: 0.02
},
stoneLight: {
color: 0xd1c4a7,
roughness: 0.9,
metalness: 0.02
},
stoneDark: {
color: 0x8f876f,
roughness: 0.95,
metalness: 0.02
},
sand: {
color: 0xb99462,
roughness: 1,
metalness: 0
},
earth: {
color: 0x8c714e,
roughness: 1,
metalness: 0
},
grass: {
color: 0x7f8d55,
roughness: 1,
metalness: 0
},
grassLight: {
color: 0x9da264,
roughness: 1,
metalness: 0
},
water: {
color: 0x387b89,
roughness: 0.32,
metalness: 0.24
},
waterLight: {
color: 0x4f929c,
roughness: 0.29,
metalness: 0.25
},
waterDark: {
color: 0x2d6978,
roughness: 0.36,
metalness: 0.22
},
foam: {
color: 0xb5d9d4,
roughness: 0.48,
metalness: 0.05
},
trunk: {
color: 0x745238,
roughness: 1,
metalness: 0
},
leaf: {
color: 0x526f46,
roughness: 1,
metalness: 0
},
leafLight: {
color: 0x71834d,
roughness: 1,
metalness: 0
},
cream: {
color: 0xe4d9bd,
roughness: 0.88,
metalness: 0
},
coral: {
color: 0xb7664f,
roughness: 0.88,
metalness: 0
},
blue: {
color: 0x5e8990,
roughness: 0.85,
metalness: 0
},
red: {
color: 0xa64e43,
roughness: 0.85,
metalness: 0
},
skin: {
color: 0x9c6d4b,
roughness: 0.9,
metalness: 0
}
};
function addBlock(type, x, y, z, sx = 1, sy = 1, sz = 1) {
if (!blockBatches.has(type)) blockBatches.set(type, []);
const matrix = new THREE.Matrix4();
matrix.compose(
new THREE.Vector3(x, y, z),
new THREE.Quaternion(),
new THREE.Vector3(sx, sy, sz)
);
blockBatches.get(type).push(matrix);
}
function addStairBeam(type, a, b, thickness = 0.19, step = 0.32) {
const length = a.distanceTo(b);
const count = Math.max(1, Math.ceil(length / step));
const grid = thickness * 0.82;
for (let i = 0; i <= count; i++) {
const p = new THREE.Vector3().lerpVectors(a, b, i / count);
p.x = Math.round(p.x / grid) * grid;
p.y = Math.round(p.y / grid) * grid;
p.z = Math.round(p.z / grid) * grid;
addBlock(type, p.x, p.y, p.z, thickness, thickness, thickness);
}
}
function flushVoxels() {
const geometry = new THREE.BoxGeometry(1, 1, 1);
for (const [type, matrices] of blockBatches) {
const options = materialOptions[type];
const material = new THREE.MeshStandardMaterial(options);
const mesh = new THREE.InstancedMesh(geometry, material, matrices.length);
matrices.forEach((matrix, index) => {
mesh.setMatrixAt(index, matrix);
});
mesh.instanceMatrix.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
}
}
// Deterministic randomness keeps the composition stable between reloads.
let randomSeed = 1937;
function random() {
randomSeed =
(randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
// -------------------------------------------------------------------------
// River and banks
// -------------------------------------------------------------------------
for (let ix = -16; ix < 16; ix++) {
for (let iz = -31; iz < 31; iz++) {
const x = ix + 0.5;
const z = iz + 0.5;
const ripple = Math.sin(z * 0.58 + x * 0.27) + Math.sin(x * 0.43 - z * 0.16);
let type = "water";
if (ripple > 1.05) type = "waterLight";
else if (ripple < -1.05) type = "waterDark";
const lift = (random() - 0.5) * 0.055;
addBlock(type, x, -0.12 + lift, z, 0.98, 0.2, 0.98);
if (
ripple > 1.38 &&
(ix + iz) % 3 === 0
) {
addBlock(
"foam",
x,
0.015 + lift,
z + 0.22,
0.48 + random() * 0.5,
0.035,
0.14
);
}
}
}
for (const side of [-1, 1]) {
for (let x = 16; x <= 37; x += 2) {
for (let z = -30; z <= 30; z += 2) {
const edge = Math.abs(z) > 23 || x > 29;
const topType = edge ? "sand" : random() > 0.45 ? "grass" : "grassLight";
const height = 0.78 + random() * 0.28;
addBlock(
"earth",
side * (x + 1),
0.12,
z + 1,
1.98,
1.3,
1.98
);
addBlock(
topType,
side * (x + 1),
height,
z + 1,
1.98,
0.25,
1.98
);
}
}
// Stone quay along the river's edge.
for (let z = -30; z < 30; z += 1.5) {
addBlock(
z % 3 === 0 ? "stoneLight" : "stone",
side * 16.15,
0.35,
z + 0.75,
0.75,
1.2,
1.45
);
}
}
// -------------------------------------------------------------------------
// Bridge foundations
// -------------------------------------------------------------------------
for (const x of [-25.5, -15.6, 15.6, 25.5]) {
for (let y = 0.25; y < 4.35; y += 0.48) {
const width = y < 1 ? 2.0 : y < 3.7 ? 1.65 : 1.35;
for (let z = -2.7; z <= 2.7; z += 1.15) {
const type = random() > 0.68 ? "stoneLight" :
random() > 0.42 ? "stone" : "stoneDark";
addBlock(
type,
x + (random() - 0.5) * 0.08,
y,
z,
width,
0.43,
1.08
);
}
}
addBlock("stoneLight", x, 4.2, 0, 1.8, 0.35, 6.5);
}
// -------------------------------------------------------------------------
// Deck, sidewalks, road markings, and railings
// -------------------------------------------------------------------------
for (let x = -27; x < 27; x += 1) {
addBlock("road", x + 0.5, 4.35, 0, 0.98, 0.38, 4.35);
addBlock("walkway", x + 0.5, 4.52, -2.72, 0.98, 0.42, 1.0);
addBlock("walkway", x + 0.5, 4.52, 2.72, 0.98, 0.42, 1.0);
// Longitudinal deck girders.
addBlock("ironDark", x + 0.5, 4.02, -2.05, 0.98, 0.32, 0.25);
addBlock("ironDark", x + 0.5, 4.02, 2.05, 0.98, 0.32, 0.25);
}
for (let x = -26.5; x < 27; x += 1.5) {
addBlock("yellow", x, 4.56, 0, 0.72, 0.035, 0.085);
}
for (let x = -26; x <= 26; x += 2) {
addBlock("ironDark", x, 3.95, 0, 0.28, 0.36, 6.45);
}
for (const z of [-3.35, 3.35]) {
for (let x = -27; x <= 27; x += 1.5) {
addBlock("ironLight", x, 5.12, z, 0.13, 1.25, 0.13);
}
for (let x = -26.75; x < 27; x += 1.5) {
addBlock("ironLight", x + 0.75, 5.68, z, 1.5, 0.12, 0.14);
addBlock("iron", x + 0.75, 5.15, z, 1.5, 0.09, 0.1);
}
}
// -------------------------------------------------------------------------
// Main Faidherbe-style steel arch
// -------------------------------------------------------------------------
const mainHalfSpan = 15.25;
const archBase = 5.0;
const archRise = 10.3;
const archSides = [-3.68, 3.68];
function mainArchHeight(x) {
const t = Math.min(1, Math.abs(x) / mainHalfSpan);
return archBase + archRise * (1 - Math.pow(t, 1.68));
}
for (const z of archSides) {
// Curved upper and lower chords.
for (let x = -mainHalfSpan; x <= mainHalfSpan + 0.01; x += 0.5) {
const y = mainArchHeight(x);
addBlock("iron", x, y + 0.86, z, 0.55, 0.43, 0.55);
addBlock("ironDark", x, y, z, 0.46, 0.38, 0.46);
}
// Vertical web members between the two arch chords.
for (let x = -14.75; x <= 14.75; x += 1) {
const bottom = mainArchHeight(x);
const top = bottom + 0.86;
addBlock(
"ironLight",
x,
(bottom + top) * 0.5,
z,
0.17,
top - bottom,
0.18
);
}
// Horizontal lower chord above the roadway.
for (let x = -15.25; x <= 15.25; x += 0.5) {
addBlock("ironDark", x, 4.86, z, 0.5, 0.22, 0.32);
}
// Hangers and diagonal lattice between the deck and arch.
for (let x = -14.5; x <= 14.5; x += 1.5) {
const archY = mainArchHeight(x);
const height = archY - 4.86;
addBlock(
"ironLight",
x,
4.86 + height * 0.5,
z,
0.14,
height,
0.14
);
if (x < 14.4 && mainArchHeight(x + 1.5) > 6) {
addStairBeam(
"iron",
new THREE.Vector3(x, 4.92, z),
new THREE.Vector3(x + 1.5, mainArchHeight(x + 1.5), z),
0.16,
0.29
);
}
}
}
// Cross beams over and beneath the deck.
for (let x = -15; x <= 15; x += 3) {
const archY = mainArchHeight(x);
addBlock("ironLight", x, archY + 0.86, 0, 0.24, 0.22, 7.4);
addBlock("ironDark", x, 4.68, 0, 0.28, 0.28, 7.2);
}
// -------------------------------------------------------------------------
// Smaller approach spans
// -------------------------------------------------------------------------
function buildSmallSpan(start, end, side) {
const midpoint = (start + end) * 0.5;
const halfSpan = (end - start) * 0.5;
for (const z of archSides) {
for (let x = start; x <= end + 0.01; x += 0.5) {
const t = (x - midpoint) / halfSpan;
const y = 5.15 + 2.4 * (1 - t * t);
addBlock("iron", x, y, z, 0.38, 0.34, 0.38);
addBlock("ironDark", x, y - 0.36, z, 0.3, 0.26, 0.3);
}
for (let x = start + 0.5; x < end; x += 1) {
const t = (x - midpoint) / halfSpan;
const y = 5.15 + 2.4 * (1 - t * t);
addBlock("ironLight", x, y - 0.18, z, 0.13, 0.55, 0.13);
addBlock("ironLight", x, 5.0, z, 0.13, 0.6, 0.13);
}
}
}
buildSmallSpan(-25.2, -16, -1);
buildSmallSpan(16, 25.2, 1);
// -------------------------------------------------------------------------
// Lamps and tiny figures for scale
// -------------------------------------------------------------------------
for (let x = -24; x <= 24; x += 6) {
for (const z of [-3.05, 3.05]) {
addBlock("ironDark", x, 5.82, z, 0.13, 2.2, 0.13);
addBlock("yellow", x, 6.94, z, 0.42, 0.32, 0.42);
}
}
function addPerson(x, z, shirt) {
addBlock("ironDark", x - 0.08, 4.88, z, 0.11, 0.42, 0.13);
addBlock("ironDark", x + 0.08, 4.88, z, 0.11, 0.42, 0.13);
addBlock(shirt, x, 5.23, z, 0.34, 0.45, 0.24);
addBlock("skin", x, 5.56, z, 0.27, 0.27, 0.27);
}
addPerson(-11.5, -2.72, "red");
addPerson(7.2, 2.72, "blue");
addPerson(12.8, -2.72, "cream");
addPerson(-20.2, 2.72, "red");
addPerson(20.5, -2.72, "blue");
// -------------------------------------------------------------------------
// Trees, buildings, and a distant Saint-Louis landmark
// -------------------------------------------------------------------------
function addTree(x, z, scale = 1) {
const trunkHeight = 2.8 * scale;
for (let y = 0.65; y < trunkHeight; y += 0.42) {
addBlock("trunk", x, y, z, 0.28 * scale, 0.45, 0.28 * scale);
}
const crownY = trunkHeight + 0.35;
addBlock("leaf", x, crownY, z, 1.75 * scale, 1.0 * scale, 1.65 * scale);
addBlock("leafLight", x - 0.48 * scale, crownY + 0.38, z + 0.12, 1.25 * scale, 0.85 * scale, 1.3 * scale);
addBlock("leaf", x + 0.42 * scale, crownY + 0.2, z - 0.35, 1.15 * scale, 0.95 * scale, 1.25 * scale);
}
[
[21, -24, 1.05],
[28, -20, 0.9],
[21, 18, 1.0],
[30, 22, 1.1],
[-22, -21, 0.95],
[-29, 16, 1.05],
[-20, 25, 0.85]
].forEach(([x, z, scale]) => addTree(x, z, scale));
function addBuilding(x, z, width, depth, height, wall, roof) {
addBlock(wall, x, height * 0.5 + 0.65, z, width, height, depth);
addBlock(roof, x, height + 0.85, z, width + 0.35, 0.4, depth + 0.35);
// Simple window blocks.
for (let wx = x - width * 0.28; wx <= x + width * 0.28; wx += width * 0.56) {
addBlock("ironDark", wx, height * 0.52 + 0.7, z + depth * 0.5 + 0.02,
0.42, 0.55, 0.08);
}
}
addBuilding(27, -12, 4.2, 3.8, 3.6, "cream", "coral");
addBuilding(31, -5, 3.3, 4.8, 4.5, "coral", "ironDark");
addBuilding(25, 13, 4.8, 3.5, 3.0, "blue", "cream");
addBuilding(33, 18, 3.6, 3.8, 4.2, "cream", "coral");
addBuilding(-27, -11, 4.2, 4.5, 3.8, "coral", "cream");
addBuilding(-30, 13, 4.7, 3.7, 3.3, "cream", "blue");
// Small block-built tower.
for (let y = 0.8; y < 7.2; y += 0.8) {
addBlock("cream", 32, y, -21, 1.55, 0.76, 1.55);
}
addBlock("coral", 32, 7.65, -21, 1.85, 0.55, 1.85);
addBlock("ironDark", 32, 8.35, -21, 0.7, 0.85, 0.7);
// Convert all static voxel data into instanced meshes.
flushVoxels();
// -------------------------------------------------------------------------
// Small river boat
// -------------------------------------------------------------------------
const boat = new THREE.Group();
function makeBox(color, size, position, parent = boat) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: color === 0x405c5c ? 0.28 : 0.03
})
);
mesh.position.copy(position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
makeBox(0x8e4e38, new THREE.Vector3(1.7, 0.48, 4.8), new THREE.Vector3(0, 0.25, 0));
makeBox(0x405c5c, new THREE.Vector3(1.35, 0.28, 3.7), new THREE.Vector3(0, 0.62, -0.15));
makeBox(0xe4dcc5, new THREE.Vector3(1.25, 0.95, 1.45), new THREE.Vector3(0, 1.15, 0.75));
makeBox(0x385d67, new THREE.Vector3(1.28, 0.34, 1.48), new THREE.Vector3(0, 1.72, 0.75));
makeBox(0x9c6d4b, new THREE.Vector3(0.36, 0.75, 0.36), new THREE.Vector3(0, 1.08, -1.2));
for (let i = 0; i < 5; i++) {
makeBox(
0xaecfc9,
new THREE.Vector3(0.7 + i * 0.08, 0.05, 0.34),
new THREE.Vector3(0, 0.08, -2.6 - i * 0.65)
);
}
boat.position.set(2.2, 0, -10);
scene.add(boat);
// -------------------------------------------------------------------------
// Direct camera controls — no external controls/addons
// -------------------------------------------------------------------------
const target = new THREE.Vector3(0, 6.2, 0);
const cameraState = {
radius: 49,
theta: 0.72,
phi: 0.98,
minRadius: 24,
maxRadius: 82
};
let autoRotate = false;
let dragging = false;
let pointerCount = 0;
let previousPinchDistance = 0;
const pointers = new Map();
function updateCamera() {
const { radius, theta, phi } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(target);
}
function getPinchDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
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 });
pointerCount = pointers.size;
dragging = true;
if (pointerCount === 2) {
previousPinchDistance = getPinchDistance();
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
const deltaX = event.clientX - previous.x;
const deltaY = event.clientY - previous.y;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
cameraState.theta -= deltaX * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - deltaY * 0.005,
0.28,
1.43
);
} else if (pointers.size === 2) {
const distance = getPinchDistance();
if (previousPinchDistance > 0 && distance > 0) {
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * (previousPinchDistance / distance),
cameraState.minRadius,
cameraState.maxRadius
);
}
previousPinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
pointerCount = pointers.size;
dragging = pointerCount > 0;
previousPinchDistance = 0;
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
cameraState.minRadius,
cameraState.maxRadius
);
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
document.getElementById("reset").addEventListener("click", () => {
cameraState.radius = 49;
cameraState.theta = 0.72;
cameraState.phi = 0.98;
target.set(0, 6.2, 0);
});
const rotateButton = document.getElementById("rotate");
rotateButton.addEventListener("click", () => {
autoRotate = !autoRotate;
rotateButton.classList.toggle("active", autoRotate);
rotateButton.textContent = autoRotate ? "Pause rotation" : "Auto rotate";
});
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let firstFrame = true;
function animate() {
const elapsed = clock.getElapsedTime();
const delta = Math.min(clock.getDelta(), 0.04);
if (autoRotate && !dragging) {
cameraState.theta += delta * 0.11;
}
boat.position.z = -10 + Math.sin(elapsed * 0.24) * 6.5;
boat.position.y = Math.sin(elapsed * 1.15) * 0.035;
boat.rotation.z = Math.sin(elapsed * 0.85) * 0.012;
updateCamera();
renderer.render(scene, camera);
if (firstFrame) {
firstFrame = false;
document.getElementById("loading").classList.add("hidden");
setTimeout(() => document.getElementById("loading")?.remove(), 450);
}
}
updateCamera();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```