Prompt specification
Build or customize the Three.js 3D scene "Sloped Pavement Tile With Drain Inlet" 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: Sloped Pavement Tile With Drain Inlet
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/sloped-pavement-tile-with-drain-inlet-x3981
Scene Description Prompt:
A low-poly sloping pavement tile, curb, and drain; show the actual downhill route across its surface. Three.js from cdnjs, one HTML file.
Working Three.js Starter (sloped-pavement-tile-with-drain-inlet-x3981):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Downhill Pavement Tile</title>
<style>
:root {
--ink: #203436;
--muted: #647577;
--paper: rgba(248, 250, 246, 0.9);
--line: rgba(32, 52, 54, 0.14);
--route: #f4c542;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce5e3;
color: var(--ink);
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;
}
.ui {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 10;
}
.panel {
position: absolute;
top: 24px;
left: 24px;
width: min(390px, calc(100vw - 48px));
padding: 20px 22px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--paper);
box-shadow: 0 18px 55px rgba(37, 57, 58, 0.12);
backdrop-filter: blur(14px);
}
.eyebrow {
margin-bottom: 8px;
color: #738183;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(26px, 4vw, 40px);
font-weight: 750;
letter-spacing: -0.045em;
line-height: 0.98;
}
.description {
margin: 12px 0 0;
max-width: 31rem;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin-top: 18px;
}
.metric {
min-width: 0;
padding: 11px 10px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.48);
}
.metric strong,
.metric small {
display: block;
}
.metric strong {
font-size: 17px;
font-weight: 800;
letter-spacing: -0.025em;
}
.metric small {
margin-top: 3px;
overflow: hidden;
color: #7a8586;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-overflow: ellipsis;
text-transform: uppercase;
white-space: nowrap;
}
.controls {
position: absolute;
top: 24px;
right: 24px;
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
appearance: none;
border: 1px solid var(--line);
border-radius: 999px;
padding: 10px 14px;
background: var(--paper);
color: var(--ink);
box-shadow: 0 8px 28px rgba(37, 57, 58, 0.1);
cursor: pointer;
font: inherit;
font-size: 12px;
font-weight: 750;
backdrop-filter: blur(12px);
}
button:hover {
background: #fff;
transform: translateY(-1px);
}
.legend {
position: absolute;
right: 24px;
bottom: 24px;
display: flex;
align-items: center;
gap: 18px;
padding: 12px 15px;
border: 1px solid var(--line);
border-radius: 999px;
background: var(--paper);
box-shadow: 0 12px 35px rgba(37, 57, 58, 0.1);
backdrop-filter: blur(12px);
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
color: #59696b;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.02em;
}
.swatch {
width: 26px;
height: 7px;
border-radius: 999px;
background: var(--route);
box-shadow: 0 0 0 3px rgba(244, 197, 66, 0.2);
}
.drain-swatch {
width: 18px;
height: 12px;
border: 2px solid #47595b;
border-radius: 2px;
background: repeating-linear-gradient(
90deg,
#47595b 0 2px,
#bac1bd 2px 4px
);
}
.hint {
position: absolute;
left: 24px;
bottom: 24px;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 999px;
background: var(--paper);
color: #687678;
font-size: 11px;
font-weight: 650;
backdrop-filter: blur(12px);
}
@media (max-width: 700px) {
.panel {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
padding: 16px;
}
.description {
display: none;
}
.metrics {
margin-top: 14px;
}
.controls {
top: auto;
right: 14px;
bottom: 62px;
}
.legend {
right: 14px;
bottom: 14px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene"></canvas>
<div class="ui">
<section class="panel">
<div class="eyebrow">Pavement drainage study</div>
<h1>Downhill<br />access route</h1>
<p class="description">
A low-poly pavement tile with a continuous 10% fall. The highlighted
route follows the faceted surface from the high edge to the curb drain.
</p>
<div class="metrics">
<div class="metric">
<strong>10%</strong>
<small>Downhill fall</small>
</div>
<div class="metric">
<strong id="route-length">15.3 m</strong>
<small>Route length</small>
</div>
<div class="metric">
<strong>1.60 m</strong>
<small>Total drop</small>
</div>
</div>
</section>
<div class="controls">
<button id="toggle-route" type="button">Hide route</button>
<button id="reset-camera" type="button">Reset view</button>
</div>
<div class="hint">Drag to orbit · Scroll or pinch to zoom · Double-click to reset</div>
<div class="legend">
<div class="legend-item">
<span class="swatch"></span>
Downhill route
</div>
<div class="legend-item">
<span class="drain-swatch"></span>
Curb drain
</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";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce5e3);
scene.fog = new THREE.Fog(0xdce5e3, 30, 58);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemisphere = new THREE.HemisphereLight(0xf7fbff, 0x68736e, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff6df, 3.4);
sun.position.set(-9, 16, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -18;
sun.shadow.camera.right = 18;
sun.shadow.camera.top = 18;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.bias = -0.00025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb9d9df, 1.1);
fill.position.set(12, 8, -12);
scene.add(fill);
// -------------------------------------------------------------------------
// Shared dimensions
// -------------------------------------------------------------------------
const TILE_W = 12;
const TILE_L = 16;
const X_MIN = -TILE_W / 2;
const X_MAX = TILE_W / 2;
const Z_MIN = -TILE_L / 2;
const Z_MAX = TILE_L / 2;
const GRID_X = 12;
const GRID_Z = 16;
const CELL_X = TILE_W / GRID_X;
const CELL_Z = TILE_L / GRID_Z;
const BOTTOM_Y = -0.36;
// A slightly faceted but consistently downhill surface.
const gridHeight = [];
for (let j = 0; j <= GRID_Z; j++) {
gridHeight[j] = [];
for (let i = 0; i <= GRID_X; i++) {
const x = X_MIN + i * CELL_X;
const z = Z_MIN + j * CELL_Z;
gridHeight[j][i] =
1.15 -
0.1 * z +
0.055 * Math.sin(i * 1.37 + j * 0.71) +
0.025 * Math.sin(j * 1.91 - i * 0.43);
}
}
// Exact height of the rendered triangular surface.
function heightAt(x, z) {
const gx = THREE.MathUtils.clamp(
(x - X_MIN) / CELL_X,
0,
GRID_X - 0.000001
);
const gz = THREE.MathUtils.clamp(
(z - Z_MIN) / CELL_Z,
0,
GRID_Z - 0.000001
);
const i = Math.floor(gx);
const j = Math.floor(gz);
const u = gx - i;
const v = gz - j;
const h00 = gridHeight[j][i];
const h10 = gridHeight[j][i + 1];
const h01 = gridHeight[j + 1][i];
const h11 = gridHeight[j + 1][i + 1];
if (v <= u) {
return h00 * (1 - u) + h10 * (u - v) + h11 * v;
}
return h00 * (1 - v) + h11 * u + h01 * (v - u);
}
// -------------------------------------------------------------------------
// Low-poly pavement surface
// -------------------------------------------------------------------------
let seed = 73421;
function random() {
seed |= 0;
seed = (seed + 0x6d2b79f5) | 0;
let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
}
const topPositions = [];
const topColors = [];
const topPalette = [
0xa9ada8,
0xb4b3aa,
0xaeb1ac,
0xb9b5aa,
0xa5aaa6,
0xc0bbb0
];
function addColoredTriangle(a, b, c, color) {
topPositions.push(...a, ...b, ...c);
for (let i = 0; i < 3; i++) {
topColors.push(color.r, color.g, color.b);
}
}
for (let j = 0; j < GRID_Z; j++) {
for (let i = 0; i < GRID_X; i++) {
const x0 = X_MIN + i * CELL_X;
const x1 = x0 + CELL_X;
const z0 = Z_MIN + j * CELL_Z;
const z1 = z0 + CELL_Z;
const p00 = [x0, gridHeight[j][i], z0];
const p10 = [x1, gridHeight[j][i + 1], z0];
const p01 = [x0, gridHeight[j + 1][i], z1];
const p11 = [x1, gridHeight[j + 1][i + 1], z1];
const base = new THREE.Color(
topPalette[Math.floor(random() * topPalette.length)]
);
base.offsetHSL(
(random() - 0.5) * 0.012,
(random() - 0.5) * 0.025,
(random() - 0.5) * 0.055
);
addColoredTriangle(p00, p11, p10, base);
addColoredTriangle(p00, p01, p11, base);
}
}
const topGeometry = new THREE.BufferGeometry();
topGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(topPositions, 3)
);
topGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(topColors, 3)
);
topGeometry.computeVertexNormals();
const topMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.96,
metalness: 0,
flatShading: true,
side: THREE.DoubleSide
});
const pavement = new THREE.Mesh(topGeometry, topMaterial);
pavement.receiveShadow = true;
scene.add(pavement);
// Tile thickness and faceted side walls.
const sidePositions = [];
const sideColors = [];
function addSideQuad(a, b, c, d) {
const color = new THREE.Color(0x777d79);
color.offsetHSL(0, 0, (random() - 0.5) * 0.09);
sidePositions.push(...a, ...b, ...c);
sidePositions.push(...a, ...c, ...d);
for (let i = 0; i < 6; i++) {
sideColors.push(color.r, color.g, color.b);
}
}
for (let i = 0; i < GRID_X; i++) {
const x0 = X_MIN + i * CELL_X;
const x1 = x0 + CELL_X;
addSideQuad(
[x0, gridHeight[0][i], Z_MIN],
[x1, gridHeight[0][i + 1], Z_MIN],
[x1, BOTTOM_Y, Z_MIN],
[x0, BOTTOM_Y, Z_MIN]
);
addSideQuad(
[x1, gridHeight[GRID_Z][i + 1], Z_MAX],
[x0, gridHeight[GRID_Z][i], Z_MAX],
[x0, BOTTOM_Y, Z_MAX],
[x1, BOTTOM_Y, Z_MAX]
);
}
for (let j = 0; j < GRID_Z; j++) {
const z0 = Z_MIN + j * CELL_Z;
const z1 = z0 + CELL_Z;
addSideQuad(
[X_MAX, gridHeight[j][GRID_X], z0],
[X_MAX, gridHeight[j + 1][GRID_X], z1],
[X_MAX, BOTTOM_Y, z1],
[X_MAX, BOTTOM_Y, z0]
);
addSideQuad(
[X_MIN, gridHeight[j + 1][0], z1],
[X_MIN, gridHeight[j][0], z0],
[X_MIN, BOTTOM_Y, z0],
[X_MIN, BOTTOM_Y, z1]
);
}
const sideGeometry = new THREE.BufferGeometry();
sideGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(sidePositions, 3)
);
sideGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(sideColors, 3)
);
sideGeometry.computeVertexNormals();
const sides = new THREE.Mesh(
sideGeometry,
new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.94,
flatShading: true,
side: THREE.DoubleSide
})
);
sides.castShadow = true;
sides.receiveShadow = true;
scene.add(sides);
// -------------------------------------------------------------------------
// Expansion joints
// -------------------------------------------------------------------------
const jointPositions = [];
for (let j = 0; j <= GRID_Z; j++) {
for (let i = 0; i < GRID_X; i++) {
jointPositions.push(
X_MIN + i * CELL_X,
gridHeight[j][i] + 0.012,
Z_MIN + j * CELL_Z,
X_MIN + (i + 1) * CELL_X,
gridHeight[j][i + 1] + 0.012,
Z_MIN + j * CELL_Z
);
}
}
for (let i = 0; i <= GRID_X; i++) {
for (let j = 0; j < GRID_Z; j++) {
jointPositions.push(
X_MIN + i * CELL_X,
gridHeight[j][i] + 0.012,
Z_MIN + j * CELL_Z,
X_MIN + i * CELL_X,
gridHeight[j + 1][i] + 0.012,
Z_MIN + (j + 1) * CELL_Z
);
}
}
const jointGeometry = new THREE.BufferGeometry();
jointGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(jointPositions, 3)
);
scene.add(
new THREE.LineSegments(
jointGeometry,
new THREE.LineBasicMaterial({
color: 0x4f5958,
transparent: true,
opacity: 0.3
})
)
);
// -------------------------------------------------------------------------
// Curbs
// -------------------------------------------------------------------------
const curbMaterial = new THREE.MeshStandardMaterial({
color: 0xc7c5ba,
roughness: 0.88,
metalness: 0,
flatShading: true
});
const curbAccentMaterial = new THREE.MeshStandardMaterial({
color: 0xb7b6ad,
roughness: 0.9,
metalness: 0,
flatShading: true
});
function makeCurbSegment({
x = 0,
z0,
z1,
width,
height = 0.3,
lift = 0,
material = curbMaterial
}) {
const length = z1 - z0;
const centerZ = (z0 + z1) * 0.5;
const yA = heightAt(x, z0);
const yB = heightAt(x, z1);
const centerY = (yA + yB) * 0.5;
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, length + 0.025),
material
);
mesh.position.set(x, centerY + height * 0.5 + lift, centerZ);
mesh.rotation.x = Math.atan((yA - yB) / length);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// Raised curbs along both long edges.
for (const x of [X_MIN - 0.16, X_MAX + 0.16]) {
for (let j = 0; j < GRID_Z; j++) {
makeCurbSegment({
x,
z0: Z_MIN + j * CELL_Z,
z1: Z_MIN + (j + 1) * CELL_Z,
width: 0.34,
material: j % 3 === 0 ? curbAccentMaterial : curbMaterial
});
}
}
// Lower curb, interrupted to leave an opening for the drain.
const curbZ = 7.91;
for (let x0 = -6; x0 < 6; x0++) {
if (x0 >= 0 && x0 < 3) continue;
makeCurbSegment({
x: x0 + 0.5,
z0: curbZ - 0.15,
z1: curbZ + 0.15,
width: 1.02,
material: x0 % 2 === 0 ? curbMaterial : curbAccentMaterial
});
}
// -------------------------------------------------------------------------
// Drain: dark recess, metal frame, and low-poly grate bars
// -------------------------------------------------------------------------
const DRAIN_X = 1.8;
const DRAIN_Z = 7.37;
const DRAIN_W = 2.25;
const DRAIN_D = 0.8;
function makeSurfacePolygon(corners, offset, material) {
const positions = [];
for (const corner of corners) {
positions.push(
corner[0],
heightAt(corner[0], corner[1]) + offset,
corner[1]
);
}
positions.push(
corners[0][0],
heightAt(corners[0][0], corners[0][1]) + offset,
corners[0][1],
corners[2][0],
heightAt(corners[2][0], corners[2][1]) + offset,
corners[2][1],
corners[1][0],
heightAt(corners[1][0], corners[1][1]) + offset,
corners[1][1]
);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.computeVertexNormals();
return new THREE.Mesh(geometry, material);
}
const drainCorners = [
[DRAIN_X - DRAIN_W / 2, DRAIN_Z - DRAIN_D / 2],
[DRAIN_X + DRAIN_W / 2, DRAIN_Z - DRAIN_D / 2],
[DRAIN_X + DRAIN_W / 2, DRAIN_Z + DRAIN_D / 2],
[DRAIN_X - DRAIN_W / 2, DRAIN_Z + DRAIN_D / 2]
];
const basin = makeSurfacePolygon(
drainCorners,
0.018,
new THREE.MeshBasicMaterial({
color: 0x182224,
side: THREE.DoubleSide
})
);
scene.add(basin);
const drainMetal = new THREE.MeshStandardMaterial({
color: 0x3c494b,
roughness: 0.56,
metalness: 0.58,
flatShading: true
});
// Side rails.
makeCurbSegment({
x: DRAIN_X - DRAIN_W / 2,
z0: DRAIN_Z - DRAIN_D / 2,
z1: DRAIN_Z + DRAIN_D / 2,
width: 0.12,
height: 0.09,
lift: 0.025,
material: drainMetal
});
makeCurbSegment({
x: DRAIN_X + DRAIN_W / 2,
z0: DRAIN_Z - DRAIN_D / 2,
z1: DRAIN_Z + DRAIN_D / 2,
width: 0.12,
height: 0.09,
lift: 0.025,
material: drainMetal
});
// Grate bars.
for (let i = 0; i < 8; i++) {
const x = DRAIN_X - 0.88 + i * 0.251;
makeCurbSegment({
x,
z0: DRAIN_Z - DRAIN_D * 0.43,
z1: DRAIN_Z + DRAIN_D * 0.43,
width: 0.07,
height: 0.075,
lift: 0.045,
material: drainMetal
});
}
// -------------------------------------------------------------------------
// Surface-conforming downhill route
// -------------------------------------------------------------------------
const routeGroup = new THREE.Group();
scene.add(routeGroup);
function routeAt(t) {
const z = -7.35 + 14.32 * t;
const x =
-3.45 +
5.25 * t +
0.85 * Math.sin(Math.PI * 2 * t) -
0.28 * Math.sin(Math.PI * 4 * t);
return new THREE.Vector3(x, 0, z);
}
function routeTangent(t) {
const dx =
5.25 +
0.85 * Math.PI * 2 * Math.cos(Math.PI * 2 * t) -
0.28 * Math.PI * 4 * Math.cos(Math.PI * 4 * t);
const dz = 14.32;
return new THREE.Vector3(dx, 0, dz).normalize();
}
function makeRouteRibbon(width, lift, material) {
const positions = [];
const count = 150;
const half = width * 0.5;
for (let i = 0; i < count; i++) {
const t = i / (count - 1);
const p = routeAt(t);
const tangent = routeTangent(t);
const perpendicular = new THREE.Vector3(
-tangent.z,
0,
tangent.x
).normalize();
const left = p.clone().addScaledVector(perpendicular, half);
const right = p.clone().addScaledVector(perpendicular, -half);
const leftY = heightAt(left.x, left.z) + lift;
const rightY = heightAt(right.x, right.z) + lift;
positions.push(
left.x, leftY, left.z,
right.x, rightY, right.z
);
}
const indices = [];
for (let i = 0; i < count - 1; i++) {
const a = i * 2;
const b = a + 1;
const c = a + 2;
const d = a + 3;
indices.push(a, c, b, b, c, d);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, material);
mesh.renderOrder = 2;
routeGroup.add(mesh);
return mesh;
}
makeRouteRibbon(
0.72,
0.028,
new THREE.MeshBasicMaterial({
color: 0x233d40,
side: THREE.DoubleSide
})
);
makeRouteRibbon(
0.5,
0.047,
new THREE.MeshStandardMaterial({
color: 0xf4c542,
emissive: 0x5d4600,
emissiveIntensity: 0.16,
roughness: 0.72,
metalness: 0,
side: THREE.DoubleSide
})
);
// Flat arrows lying directly on the pavement.
const arrowShape = new THREE.Shape();
arrowShape.moveTo(-0.12, -0.28);
arrowShape.lineTo(0.12, -0.28);
arrowShape.lineTo(0.12, 0);
arrowShape.lineTo(0.31, 0);
arrowShape.lineTo(0, 0.39);
arrowShape.lineTo(-0.31, 0);
arrowShape.lineTo(-0.12, 0);
arrowShape.closePath();
const arrowSourceGeometry = new THREE.ShapeGeometry(arrowShape);
const arrowMaterial = new THREE.MeshBasicMaterial({
color: 0xfff2b8,
side: THREE.DoubleSide
});
const arrowTimes = [0.08, 0.19, 0.30, 0.41, 0.52, 0.63, 0.74, 0.85, 0.94];
for (const t of arrowTimes) {
const p = routeAt(t);
const tangent = routeTangent(t);
const perpendicular = new THREE.Vector3(
-tangent.z,
0,
tangent.x
).normalize();
const geometry = arrowSourceGeometry.clone();
const position = geometry.attributes.position;
for (let i = 0; i < position.count; i++) {
const localX = position.getX(i);
const localY = position.getY(i);
const x =
p.x +
perpendicular.x * localX +
tangent.x * localY;
const z =
p.z +
perpendicular.z * localX +
tangent.z * localY;
position.setXYZ(i, x, heightAt(x, z) + 0.072, z);
}
position.needsUpdate = true;
geometry.computeVertexNormals();
const arrow = new THREE.Mesh(geometry, arrowMaterial);
arrow.renderOrder = 3;
routeGroup.add(arrow);
}
// Entry marker.
const startPoint = routeAt(0);
const startMarker = new THREE.Mesh(
new THREE.CylinderGeometry(0.19, 0.19, 0.075, 8),
new THREE.MeshStandardMaterial({
color: 0xe76f51,
roughness: 0.7,
flatShading: true
})
);
startMarker.position.set(
startPoint.x,
heightAt(startPoint.x, startPoint.z) + 0.045,
startPoint.z
);
startMarker.castShadow = true;
routeGroup.add(startMarker);
// Moving survey beacon.
const movingMarker = new THREE.Group();
const markerHead = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.16, 1),
new THREE.MeshStandardMaterial({
color: 0xe85f45,
emissive: 0x7a1e12,
emissiveIntensity: 0.35,
roughness: 0.5,
flatShading: true
})
);
markerHead.position.y = 0.31;
markerHead.castShadow = true;
movingMarker.add(markerHead);
const markerStem = new THREE.Mesh(
new THREE.CylinderGeometry(0.025, 0.025, 0.26, 6),
new THREE.MeshBasicMaterial({ color: 0x244145 })
);
markerStem.position.y = 0.14;
movingMarker.add(markerStem);
routeGroup.add(movingMarker);
// Calculate the actual curved route length.
let measuredLength = 0;
let previous = routeAt(0);
for (let i = 1; i <= 200; i++) {
const current = routeAt(i / 200);
measuredLength += current.distanceTo(previous);
previous = current;
}
document.querySelector("#route-length").textContent =
`${measuredLength.toFixed(1)} m`;
// -------------------------------------------------------------------------
// Ground and scene base
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
new THREE.MeshStandardMaterial({
color: 0xb8c5c0,
roughness: 1,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = BOTTOM_Y - 0.025;
ground.receiveShadow = true;
scene.add(ground);
// -------------------------------------------------------------------------
// Custom camera controls — no examples/addons used
// -------------------------------------------------------------------------
const cameraState = {
theta: 0.59,
phi: 0.94,
radius: 24,
target: new THREE.Vector3(0, 0.65, 0)
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
const cosPhi = Math.cos(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * cosPhi,
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.theta = 0.59;
cameraState.phi = 0.94;
cameraState.radius = 24;
updateCamera();
}
const pointers = new Map();
let pinchDistance = 0;
function pointerDistance() {
const values = [...pointers.values()];
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 2) {
pinchDistance = pointerDistance();
}
});
canvas.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
previous.x = event.clientX;
previous.y = event.clientY;
if (pointers.size === 1) {
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.005,
0.32,
1.43
);
updateCamera();
} else if (pointers.size === 2) {
const distance = pointerDistance();
if (pinchDistance > 0 && distance > 0) {
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * (pinchDistance / distance),
12,
38
);
updateCamera();
}
pinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
pinchDistance = 0;
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener("lostpointercapture", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
12,
38
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
document
.querySelector("#reset-camera")
.addEventListener("click", resetCamera);
const routeButton = document.querySelector("#toggle-route");
routeButton.addEventListener("click", () => {
routeGroup.visible = !routeGroup.visible;
routeButton.textContent = routeGroup.visible
? "Hide route"
: "Show route";
});
updateCamera();
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height, false);
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
const routeT = (elapsed * 0.052) % 1;
const markerPosition = routeAt(routeT);
movingMarker.position.set(
markerPosition.x,
heightAt(markerPosition.x, markerPosition.z) + 0.02,
markerPosition.z
);
const pulse = 1 + Math.sin(elapsed * 5) * 0.08;
markerHead.scale.setScalar(pulse);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```