Prompt specification
Build or customize the Three.js 3D scene "Hillside terrace cross-section" 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: Hillside terrace cross-section
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/hillside-terrace-cross-section-x1109
Scene Description Prompt:
Compare stylized hillside cross-sections before and after terracing, with equal total elevation and visible retaining edges. Three.js from cdnjs, one HTML file.
Working Three.js Starter (hillside-terrace-cross-section-x1109):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Terracing Comparison — Three.js</title>
<style>
:root {
--ink: #25362f;
--muted: #6e786f;
--paper: #f3f0e7;
--panel: rgba(255, 255, 255, 0.68);
--line: rgba(48, 70, 59, 0.14);
--green: #57745d;
--gold: #b98143;
--wall: #d9c39c;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background:
radial-gradient(circle at 50% 0%, #fffdf7 0%, var(--paper) 60%, #e9e4d8 100%);
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow: hidden;
}
button,
input {
font: inherit;
}
.app {
display: flex;
flex-direction: column;
min-height: 100vh;
height: 100vh;
padding: 20px;
gap: 16px;
}
.topbar {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 24px;
padding: 4px 6px 0;
flex: 0 0 auto;
}
.heading {
max-width: 760px;
}
.eyebrow {
margin: 0 0 7px;
color: var(--green);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(27px, 3.2vw, 48px);
font-weight: 500;
letter-spacing: -0.035em;
line-height: 1.02;
}
.subtitle {
margin: 10px 0 0;
max-width: 690px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.controls {
display: flex;
align-items: center;
justify-content: flex-end;
gap: 9px;
flex-wrap: wrap;
}
.control {
display: inline-flex;
align-items: center;
min-height: 38px;
gap: 8px;
padding: 8px 12px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(255, 255, 255, 0.72);
color: var(--ink);
font-size: 12px;
font-weight: 650;
cursor: pointer;
user-select: none;
backdrop-filter: blur(12px);
transition:
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
.control:hover {
border-color: rgba(48, 70, 59, 0.3);
background: rgba(255, 255, 255, 0.96);
}
.control:active {
transform: translateY(1px);
}
.control input {
position: absolute;
opacity: 0;
pointer-events: none;
}
.switch-track {
position: relative;
width: 27px;
height: 16px;
flex: 0 0 auto;
border-radius: 99px;
background: #c8cec8;
transition: background 160ms ease;
}
.switch-track::after {
content: "";
position: absolute;
top: 3px;
left: 3px;
width: 10px;
height: 10px;
border-radius: 50%;
background: white;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
transition: transform 160ms ease;
}
.control input:checked + .switch-track {
background: var(--green);
}
.control input:checked + .switch-track::after {
transform: translateX(11px);
}
button.control {
border-color: rgba(48, 70, 59, 0.22);
}
.comparison {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 14px;
flex: 1 1 auto;
min-height: 0;
}
.panel {
position: relative;
display: flex;
flex-direction: column;
min-width: 0;
min-height: 0;
overflow: hidden;
border: 1px solid rgba(56, 73, 64, 0.13);
border-radius: 22px;
background: var(--panel);
box-shadow:
0 18px 50px rgba(64, 68, 56, 0.08),
inset 0 1px 0 rgba(255, 255, 255, 0.9);
backdrop-filter: blur(14px);
}
.panel-heading {
position: relative;
z-index: 3;
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
padding: 16px 18px 12px;
border-bottom: 1px solid var(--line);
background: rgba(255, 255, 255, 0.35);
}
.panel-title {
display: flex;
align-items: center;
gap: 10px;
min-width: 0;
}
.panel-number {
display: grid;
place-items: center;
width: 27px;
height: 27px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--ink);
color: white;
font-size: 11px;
font-weight: 800;
}
.panel.after .panel-number {
background: var(--gold);
}
.panel h2 {
margin: 0;
font-size: 14px;
letter-spacing: -0.01em;
}
.panel-kicker {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.same-height {
flex: 0 0 auto;
padding: 6px 9px;
border: 1px solid rgba(74, 101, 82, 0.18);
border-radius: 999px;
background: rgba(232, 241, 231, 0.8);
color: #48604f;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.04em;
white-space: nowrap;
}
.viewport {
position: relative;
flex: 1 1 auto;
min-height: 280px;
overflow: hidden;
background:
radial-gradient(circle at 50% 35%, rgba(255, 255, 255, 0.96), rgba(241, 239, 230, 0.76)),
linear-gradient(180deg, #f7f5ee, #ebe7dc);
}
.viewport canvas {
position: absolute;
inset: 0;
z-index: 0;
width: 100%;
height: 100%;
display: block;
cursor: grab;
outline: none;
touch-action: none;
}
.viewport canvas:active {
cursor: grabbing;
}
.viewport canvas:focus-visible {
box-shadow: inset 0 0 0 2px rgba(87, 116, 93, 0.55);
}
.datum {
position: absolute;
z-index: 2;
left: 16px;
display: flex;
align-items: center;
gap: 7px;
pointer-events: none;
color: #68736b;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.datum::before {
content: "";
width: 18px;
height: 1px;
background: currentColor;
opacity: 0.65;
}
.datum.top {
top: 16px;
}
.datum.bottom {
bottom: 16px;
}
.datum strong {
color: var(--ink);
font-size: 11px;
}
.view-note {
position: absolute;
z-index: 2;
right: 16px;
bottom: 16px;
padding: 7px 10px;
border: 1px solid rgba(60, 75, 67, 0.12);
border-radius: 9px;
background: rgba(255, 255, 255, 0.72);
color: #68736b;
font-size: 10px;
font-weight: 700;
pointer-events: none;
backdrop-filter: blur(8px);
}
.panel-footer {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 10px;
padding: 13px 16px 15px;
border-top: 1px solid var(--line);
background: rgba(255, 255, 255, 0.38);
}
.metric {
min-width: 0;
}
.metric-label {
display: block;
margin-bottom: 3px;
color: #8a918b;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.metric-value {
display: block;
overflow: hidden;
color: #415047;
font-size: 12px;
font-weight: 750;
text-overflow: ellipsis;
white-space: nowrap;
}
.footer-hint {
display: flex;
justify-content: center;
align-items: center;
gap: 10px;
flex: 0 0 auto;
min-height: 18px;
color: #7c857f;
font-size: 10px;
font-weight: 650;
letter-spacing: 0.03em;
}
.footer-hint span {
display: inline-flex;
align-items: center;
gap: 5px;
}
.footer-hint i {
width: 4px;
height: 4px;
border-radius: 50%;
background: #9ca49e;
}
@media (max-width: 900px) {
body {
overflow: auto;
}
.app {
height: auto;
min-height: 100vh;
padding: 14px;
}
.topbar {
align-items: flex-start;
flex-direction: column;
gap: 14px;
}
.controls {
justify-content: flex-start;
}
.comparison {
grid-template-columns: 1fr;
}
.panel {
min-height: 520px;
}
.viewport {
min-height: 390px;
}
}
@media (max-width: 520px) {
.panel-heading {
align-items: flex-start;
}
.same-height {
padding: 5px 7px;
font-size: 9px;
}
.panel-footer {
grid-template-columns: 1fr;
}
.footer-hint span:nth-child(2),
.footer-hint i {
display: none;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="heading">
<p class="eyebrow">Landscape study / section comparison</p>
<h1>Same elevation. A more buildable slope.</h1>
<p class="subtitle">
Natural and terraced hillside sections share the same 84 m footprint,
−30 m base, and 64 m total rise. Drag either view to compare their geometry.
</p>
</div>
<div class="controls" aria-label="Visualization controls">
<label class="control">
<input id="guidesToggle" type="checkbox" checked>
<span class="switch-track"></span>
Elevation guides
</label>
<label class="control">
<input id="wallsToggle" type="checkbox" checked>
<span class="switch-track"></span>
Retaining edges
</label>
<button class="control" id="resetCamera" type="button">Reset view</button>
</div>
</header>
<main class="comparison">
<section class="panel before" aria-labelledby="beforeTitle">
<div class="panel-heading">
<div class="panel-title">
<span class="panel-number">01</span>
<div>
<h2 id="beforeTitle">Before terracing</h2>
<span class="panel-kicker">Continuous hillside</span>
</div>
</div>
<span class="same-height">64 m total rise</span>
</div>
<div class="viewport" id="beforeViewport">
<div class="datum top">Peak datum <strong>+34 m</strong></div>
<div class="datum bottom">Base datum <strong>−30 m</strong></div>
<div class="view-note">Original graded profile</div>
</div>
<div class="panel-footer">
<div class="metric">
<span class="metric-label">Profile</span>
<span class="metric-value">Continuous, non-horizontal grade</span>
</div>
<div class="metric">
<span class="metric-label">Footprint</span>
<span class="metric-value">84 m horizontal section</span>
</div>
</div>
</section>
<section class="panel after" aria-labelledby="afterTitle">
<div class="panel-heading">
<div class="panel-title">
<span class="panel-number">02</span>
<div>
<h2 id="afterTitle">After terracing</h2>
<span class="panel-kicker">Seven level benches</span>
</div>
</div>
<span class="same-height">64 m total rise</span>
</div>
<div class="viewport" id="afterViewport">
<div class="datum top">Peak datum <strong>+34 m</strong></div>
<div class="datum bottom">Base datum <strong>−30 m</strong></div>
<div class="view-note">Highlighted retaining edges</div>
</div>
<div class="panel-footer">
<div class="metric">
<span class="metric-label">Profile</span>
<span class="metric-value">7 level benches + 7 risers</span>
</div>
<div class="metric">
<span class="metric-label">Structure</span>
<span class="metric-value">Visible stone retaining walls</span>
</div>
</div>
</section>
</main>
<footer class="footer-hint">
<span>Drag to orbit</span>
<i></i>
<span>Scroll to zoom</span>
<i></i>
<span>Views are synchronized</span>
</footer>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const D = {
xMin: -42,
xMax: 42,
yMin: -30,
yMax: 34,
depth: 30
};
const TOTAL_RISE = D.yMax - D.yMin;
const TERRACE_COUNT = 7;
const TREAD_WIDTH = (D.xMax - D.xMin) / TERRACE_COUNT;
const STEP_HEIGHT = TOTAL_RISE / TERRACE_COUNT;
const cameraState = {
yaw: 0.34,
pitch: 0.40,
zoom: 1
};
const initialCameraState = { ...cameraState };
const views = [];
const clamp = THREE.MathUtils.clamp;
const lerp = THREE.MathUtils.lerp;
function hashNoise(i, j = 0) {
const value = Math.sin(i * 127.1 + j * 311.7) * 43758.5453123;
return value - Math.floor(value);
}
function naturalProfile(x) {
const t = clamp((x - D.xMin) / (D.xMax - D.xMin), 0, 1);
return (
D.yMax -
TOTAL_RISE * Math.pow(t, 0.78) +
Math.sin(Math.PI * t) * 1.2
);
}
function terraceHigh(index) {
return D.yMax - index * STEP_HEIGHT;
}
function makeStandardMaterial(color, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.9,
metalness: 0,
flatShading: true,
...extra
});
}
function addQuad(positions, a, b, c, d) {
positions.push(
...a, ...b, ...c,
...a, ...c, ...d
);
}
function geometryFromPositions(positions) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.computeVertexNormals();
return geometry;
}
function createNaturalTop() {
const positions = [];
const colors = [];
const nx = 42;
const nz = 8;
const base = new THREE.Color("#78956c");
function surfaceY(x, z) {
const t = (x - D.xMin) / (D.xMax - D.xMin);
const v = (z + D.depth / 2) / D.depth;
const envelope = Math.sin(Math.PI * t) * Math.sin(Math.PI * v);
const variation =
0.65 * Math.sin(x * 0.34 + z * 0.72) +
0.35 * Math.sin(x * 0.71 - z * 0.38);
return naturalProfile(x) + variation * envelope;
}
function pushTriangle(a, b, c, color) {
positions.push(...a, ...b, ...c);
for (let i = 0; i < 3; i++) {
colors.push(color.r, color.g, color.b);
}
}
for (let iz = 0; iz < nz; iz++) {
const z0 = lerp(-D.depth / 2, D.depth / 2, iz / nz);
const z1 = lerp(-D.depth / 2, D.depth / 2, (iz + 1) / nz);
for (let ix = 0; ix < nx; ix++) {
const x0 = lerp(D.xMin, D.xMax, ix / nx);
const x1 = lerp(D.xMin, D.xMax, (ix + 1) / nx);
const a = [x0, surfaceY(x0, z0), z0];
const b = [x0, surfaceY(x0, z1), z1];
const c = [x1, surfaceY(x1, z1), z1];
const d = [x1, surfaceY(x1, z0), z0];
const noise = hashNoise(ix, iz) - 0.5;
const color = base.clone().offsetHSL(
noise * 0.018,
noise * 0.035,
noise * 0.075
);
pushTriangle(a, b, c, color);
pushTriangle(a, c, d, color.clone().offsetHSL(0, 0, -0.012));
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.computeVertexNormals();
return new THREE.Mesh(
geometry,
makeStandardMaterial("#ffffff", {
vertexColors: true,
side: THREE.DoubleSide
})
);
}
function createTerracedTop() {
const positions = [];
const colors = [];
const base = new THREE.Color("#829d70");
function pushTriangle(a, b, c, color) {
positions.push(...a, ...b, ...c);
for (let i = 0; i < 3; i++) {
colors.push(color.r, color.g, color.b);
}
}
for (let terrace = 0; terrace < TERRACE_COUNT; terrace++) {
const x0 = D.xMin + terrace * TREAD_WIDTH;
const x1 = x0 + TREAD_WIDTH;
const y = terraceHigh(terrace);
const xParts = 4;
const zParts = 3;
for (let ix = 0; ix < xParts; ix++) {
const xa = lerp(x0, x1, ix / xParts);
const xb = lerp(x0, x1, (ix + 1) / xParts);
for (let iz = 0; iz < zParts; iz++) {
const za = lerp(-D.depth / 2, D.depth / 2, iz / zParts);
const zb = lerp(-D.depth / 2, D.depth / 2, (iz + 1) / zParts);
const a = [xa, y, za];
const b = [xa, y, zb];
const c = [xb, y, zb];
const d = [xb, y, za];
const noise = hashNoise(terrace * 17 + ix, iz) - 0.5;
const color = base.clone().offsetHSL(
noise * 0.012,
noise * 0.025,
noise * 0.055
);
pushTriangle(a, b, c, color);
pushTriangle(a, c, d, color.clone().offsetHSL(0, 0, -0.01));
}
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.computeVertexNormals();
return new THREE.Mesh(
geometry,
makeStandardMaterial("#ffffff", {
vertexColors: true,
side: THREE.DoubleSide
})
);
}
function createNaturalBody() {
const positions = [];
const segments = 48;
const front = D.depth / 2;
const back = -D.depth / 2;
for (let i = 0; i < segments; i++) {
const x0 = lerp(D.xMin, D.xMax, i / segments);
const x1 = lerp(D.xMin, D.xMax, (i + 1) / segments);
const y0 = naturalProfile(x0);
const y1 = naturalProfile(x1);
addQuad(
positions,
[x0, y0, front],
[x1, y1, front],
[x1, D.yMin, front],
[x0, D.yMin, front]
);
addQuad(
positions,
[x0, y0, back],
[x1, y1, back],
[x1, D.yMin, back],
[x0, D.yMin, back]
);
}
addQuad(
positions,
[D.xMin, D.yMin, back],
[D.xMin, D.yMax, back],
[D.xMin, D.yMax, front],
[D.xMin, D.yMin, front]
);
addQuad(
positions,
[D.xMax, D.yMin, back],
[D.xMax, D.yMin, front],
[D.xMax, D.yMin, front],
[D.xMax, D.yMin, back]
);
addQuad(
positions,
[D.xMin, D.yMin, back],
[D.xMax, D.yMin, back],
[D.xMax, D.yMin, front],
[D.xMin, D.yMin, front]
);
return new THREE.Mesh(
geometryFromPositions(positions),
makeStandardMaterial("#b77e58", {
side: THREE.DoubleSide
})
);
}
function createTerracedBody() {
const positions = [];
const front = D.depth / 2;
const back = -D.depth / 2;
for (let terrace = 0; terrace < TERRACE_COUNT; terrace++) {
const x0 = D.xMin + terrace * TREAD_WIDTH;
const x1 = x0 + TREAD_WIDTH;
const y = terraceHigh(terrace);
addQuad(
positions,
[x0, y, front],
[x1, y, front],
[x1, D.yMin, front],
[x0, D.yMin, front]
);
addQuad(
positions,
[x0, y, back],
[x1, y, back],
[x1, D.yMin, back],
[x0, D.yMin, back]
);
}
addQuad(
positions,
[D.xMin, D.yMin, back],
[D.xMin, D.yMax, back],
[D.xMin, D.yMax, front],
[D.xMin, D.yMin, front]
);
addQuad(
positions,
[D.xMax, D.yMin, back],
[D.xMax, D.yMin, front],
[D.xMax, D.yMin, front],
[D.xMax, D.yMin, back]
);
addQuad(
positions,
[D.xMin, D.yMin, back],
[D.xMax, D.yMin, back],
[D.xMax, D.yMin, front],
[D.xMin, D.yMin, front]
);
return new THREE.Mesh(
geometryFromPositions(positions),
makeStandardMaterial("#b9825c", {
side: THREE.DoubleSide
})
);
}
function createLine(points, material) {
const geometry = new THREE.BufferGeometry().setFromPoints(
points.map((point) => new THREE.Vector3(...point))
);
const line = new THREE.Line(geometry, material);
if (material.isLineDashedMaterial) {
line.computeLineDistances();
}
return line;
}
function createNaturalDetails() {
const group = new THREE.Group();
const front = D.depth / 2 + 0.12;
const edgePoints = [];
for (let i = 0; i <= 60; i++) {
const x = lerp(D.xMin, D.xMax, i / 60);
edgePoints.push([x, naturalProfile(x), front]);
}
group.add(
createLine(
edgePoints,
new THREE.LineBasicMaterial({
color: "#3f5a47",
transparent: true,
opacity: 0.8
})
)
);
const strataMaterial = new THREE.LineBasicMaterial({
color: "#79583f",
transparent: true,
opacity: 0.23
});
for (let y = D.yMin + 8; y < D.yMax - 2; y += 8) {
let low = D.xMin;
let high = D.xMax;
for (let iteration = 0; iteration < 30; iteration++) {
const middle = (low + high) / 2;
if (naturalProfile(middle) >= y) {
low = middle;
} else {
high = middle;
}
}
group.add(
createLine(
[
[D.xMin + 0.3, y, front],
[low, y, front]
],
strataMaterial
)
);
}
return group;
}
function createTerraceDetails() {
const group = new THREE.Group();
const front = D.depth / 2 + 0.12;
const edgePoints = [];
for (let terrace = 0; terrace < TERRACE_COUNT; terrace++) {
const x0 = D.xMin + terrace * TREAD_WIDTH;
const x1 = x0 + TREAD_WIDTH;
const high = terraceHigh(terrace);
const low = high - STEP_HEIGHT;
edgePoints.push(
[x0, high, front],
[x1, high, front],
[x1, low, front]
);
group.add(
createLine(
[
[x0 + 0.5, high - 4.2, front],
[x1 - 0.5, high - 4.2, front]
],
new THREE.LineBasicMaterial({
color: "#79583f",
transparent: true,
opacity: 0.2
})
)
);
}
group.add(
createLine(
edgePoints,
new THREE.LineBasicMaterial({
color: "#3f5a47",
transparent: true,
opacity: 0.85
})
)
);
return group;
}
function createRetainingWalls() {
const group = new THREE.Group();
const wallMaterial = makeStandardMaterial("#cbb894");
const capMaterial = makeStandardMaterial("#ead4aa");
const edgeMaterial = makeStandardMaterial("#80684e");
const mortarPoints = [];
for (let terrace = 0; terrace < TERRACE_COUNT; terrace++) {
const x = D.xMin + (terrace + 1) * TREAD_WIDTH;
const high = terraceHigh(terrace);
const low = high - STEP_HEIGHT;
const centerY = (high + low) / 2;
const wall = new THREE.Mesh(
new THREE.BoxGeometry(1.0, STEP_HEIGHT + 0.12, D.depth + 1.1),
wallMaterial
);
wall.position.set(x, centerY, 0);
wall.castShadow = true;
wall.receiveShadow = true;
group.add(wall);
const cap = new THREE.Mesh(
new THREE.BoxGeometry(1.65, 0.42, D.depth + 1.55),
capMaterial
);
cap.position.set(x - 0.03, high + 0.06, 0);
cap.castShadow = true;
cap.receiveShadow = true;
group.add(cap);
const frontBand = new THREE.Mesh(
new THREE.BoxGeometry(0.72, STEP_HEIGHT + 0.14, 0.34),
edgeMaterial
);
frontBand.position.set(x, centerY, D.depth / 2 + 0.73);
frontBand.castShadow = true;
group.add(frontBand);
const visibleX = x + 0.51;
for (let row = 1; row < 4; row++) {
const y = lerp(low, high, row / 4);
mortarPoints.push(
visibleX, y, -D.depth / 2 - 0.4,
visibleX, y, D.depth / 2 + 0.4
);
}
for (let seam = -2; seam <= 2; seam++) {
const z = seam * 6;
mortarPoints.push(
visibleX, low + 0.1, z,
visibleX, high - 0.1, z
);
}
}
const mortarGeometry = new THREE.BufferGeometry();
mortarGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(mortarPoints, 3)
);
group.add(
new THREE.LineSegments(
mortarGeometry,
new THREE.LineBasicMaterial({
color: "#80684e",
transparent: true,
opacity: 0.55
})
)
);
return group;
}
function createElevationGuide() {
const group = new THREE.Group();
const guideZ = D.depth / 2 + 5;
const guideX = D.xMin - 6;
const dashedMaterial = new THREE.LineDashedMaterial({
color: "#58705e",
transparent: true,
opacity: 0.48,
dashSize: 1.35,
gapSize: 0.85
});
group.add(
createLine(
[
[D.xMin - 7, D.yMax, guideZ],
[D.xMax + 3, D.yMax, guideZ]
],
dashedMaterial
)
);
group.add(
createLine(
[
[guideX, D.yMin, guideZ],
[guideX, D.yMax, guideZ]
],
dashedMaterial
)
);
const tickPoints = [
guideX - 1, D.yMax, guideZ,
guideX + 1, D.yMax, guideZ,
guideX - 1, D.yMin, guideZ,
guideX + 1, D.yMin, guideZ
];
const tickGeometry = new THREE.BufferGeometry();
tickGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(tickPoints, 3)
);
group.add(
new THREE.LineSegments(
tickGeometry,
new THREE.LineBasicMaterial({
color: "#58705e",
transparent: true,
opacity: 0.58
})
)
);
return group;
}
function addGround(scene) {
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(190, 110),
new THREE.ShadowMaterial({
color: "#4f5e51",
opacity: 0.16
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = D.yMin - 0.18;
ground.receiveShadow = true;
scene.add(ground);
const baseline = createLine(
[
[D.xMin - 3, D.yMin - 0.05, D.depth / 2],
[D.xMax + 3, D.yMin - 0.05, D.depth / 2]
],
new THREE.LineBasicMaterial({
color: "#89968b",
transparent: true,
opacity: 0.28
})
);
scene.add(baseline);
}
function addLights(scene) {
scene.add(
new THREE.HemisphereLight("#fff9e9", "#776957", 2.15)
);
const key = new THREE.DirectionalLight("#fff4dc", 3.1);
key.position.set(-34, 76, 52);
key.castShadow = true;
key.shadow.mapSize.set(1024, 1024);
key.shadow.camera.left = -75;
key.shadow.camera.right = 75;
key.shadow.camera.top = 75;
key.shadow.camera.bottom = -75;
key.shadow.camera.near = 1;
key.shadow.camera.far = 180;
key.shadow.bias = -0.0008;
key.shadow.normalBias = 0.025;
scene.add(key);
const fill = new THREE.DirectionalLight("#cfe0d3", 1.1);
fill.position.set(50, 24, -38);
scene.add(fill);
}
function updateCamera(camera, width, height) {
const aspect = width / Math.max(height, 1);
const fitHeight = Math.max(82, 100 / aspect) / cameraState.zoom;
camera.left = -fitHeight * aspect / 2;
camera.right = fitHeight * aspect / 2;
camera.top = fitHeight / 2;
camera.bottom = -fitHeight / 2;
const target = new THREE.Vector3(-3, 1, 0);
const distance = 160;
camera.position.set(
target.x +
distance * Math.sin(cameraState.yaw) * Math.cos(cameraState.pitch),
target.y + distance * Math.sin(cameraState.pitch),
target.z +
distance * Math.cos(cameraState.yaw) * Math.cos(cameraState.pitch)
);
camera.lookAt(target);
camera.updateProjectionMatrix();
}
function updateAllCameras() {
for (const view of views) {
const width = view.viewport.clientWidth;
const height = view.viewport.clientHeight;
updateCamera(view.camera, width, height);
}
}
function attachCameraControls(view) {
const canvas = view.renderer.domElement;
let dragging = false;
let pointerId = null;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerId = event.pointerId;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(pointerId);
canvas.focus({ preventScroll: true });
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging || event.pointerId !== pointerId) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.yaw -= deltaX * 0.007;
cameraState.pitch = clamp(
cameraState.pitch + deltaY * 0.005,
0.12,
1.02
);
updateAllCameras();
});
function endDrag(event) {
if (!dragging || event.pointerId !== pointerId) return;
dragging = false;
if (canvas.hasPointerCapture(pointerId)) {
canvas.releasePointerCapture(pointerId);
}
pointerId = null;
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.zoom = clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.001),
0.65,
1.75
);
updateAllCameras();
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
Object.assign(cameraState, initialCameraState);
updateAllCameras();
});
canvas.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft":
cameraState.yaw += 0.08;
break;
case "ArrowRight":
cameraState.yaw -= 0.08;
break;
case "ArrowUp":
cameraState.pitch = clamp(cameraState.pitch + 0.06, 0.12, 1.02);
break;
case "ArrowDown":
cameraState.pitch = clamp(cameraState.pitch - 0.06, 0.12, 1.02);
break;
case "+":
case "=":
cameraState.zoom = clamp(cameraState.zoom + 0.08, 0.65, 1.75);
break;
case "-":
cameraState.zoom = clamp(cameraState.zoom - 0.08, 0.65, 1.75);
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
updateAllCameras();
}
});
}
function createView(viewportId, terraced) {
const viewport = document.getElementById(viewportId);
const renderer = new THREE.WebGLRenderer({
alpha: true,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const canvas = renderer.domElement;
canvas.tabIndex = 0;
canvas.setAttribute(
"aria-label",
terraced
? "Interactive three-dimensional terraced hillside section"
: "Interactive three-dimensional natural hillside section"
);
viewport.prepend(canvas);
const scene = new THREE.Scene();
const camera = new THREE.OrthographicCamera(-50, 50, 45, -45, 0.1, 400);
addLights(scene);
addGround(scene);
const model = new THREE.Group();
scene.add(model);
const body = terraced
? createTerracedBody()
: createNaturalBody();
const top = terraced
? createTerracedTop()
: createNaturalTop();
body.castShadow = true;
body.receiveShadow = true;
top.castShadow = true;
top.receiveShadow = true;
model.add(body, top);
const details = terraced
? createTerraceDetails()
: createNaturalDetails();
model.add(details);
let walls = null;
if (terraced) {
walls = createRetainingWalls();
model.add(walls);
}
const guide = createElevationGuide();
scene.add(guide);
const view = {
viewport,
renderer,
scene,
camera,
guide,
walls
};
views.push(view);
attachCameraControls(view);
const resize = () => {
const width = Math.max(1, viewport.clientWidth);
const height = Math.max(1, viewport.clientHeight);
renderer.setSize(width, height, false);
updateCamera(camera, width, height);
};
const observer = new ResizeObserver(resize);
observer.observe(viewport);
resize();
return view;
}
createView("beforeViewport", false);
createView("afterViewport", true);
document.getElementById("guidesToggle").addEventListener("change", (event) => {
for (const view of views) {
view.guide.visible = event.target.checked;
}
});
document.getElementById("wallsToggle").addEventListener("change", (event) => {
const afterView = views.find((view) => view.walls);
if (afterView) {
afterView.walls.visible = event.target.checked;
}
});
document.getElementById("resetCamera").addEventListener("click", () => {
Object.assign(cameraState, initialCameraState);
updateAllCameras();
});
window.addEventListener("resize", updateAllCameras);
function animate() {
for (const view of views) {
view.renderer.render(view.scene, view.camera);
}
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```