Prompt specification
Build or customize the Three.js 3D scene "Terraced garden linked to a water source" 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: Terraced garden linked to a water source
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/terraced-garden-linked-to-a-water-source-x2323
Scene Description Prompt:
Create a standalone voxel scene in one HTML file using Three.js from cdnjs. Build a compact historically informed Andalusi garden with terraces, channels, and a supply cistern. Preserve downhill connections and identify the chosen reference period and place.
Working Three.js Starter (terraced-garden-linked-to-a-water-source-x2323):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Generalife — Andalusi Garden in Voxels</title>
<style>
:root {
color-scheme: light;
--ink: #23342f;
--muted: #627068;
--paper: rgba(249, 246, 235, 0.92);
--paper-solid: #f7f3e7;
--line: rgba(35, 52, 47, 0.16);
--water: #238f97;
--water-dark: #176c76;
--gold: #b98735;
--shadow: 0 14px 38px rgba(32, 43, 38, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 70% 12%, #f5f0dc 0, transparent 32%),
linear-gradient(180deg, #dce8df 0%, #c8d7cf 55%, #b7c4ba 100%);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene,
#labels {
position: fixed;
inset: 0;
}
#scene {
z-index: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
#labels {
z-index: 3;
pointer-events: none;
overflow: hidden;
}
.card {
position: fixed;
z-index: 5;
border: 1px solid rgba(255, 255, 255, 0.65);
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
.identity {
top: 18px;
left: 18px;
width: min(380px, calc(100vw - 36px));
padding: 17px 18px 16px;
border-radius: 14px;
}
.eyebrow {
margin-bottom: 7px;
color: var(--water-dark);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(25px, 3vw, 36px);
font-weight: 500;
line-height: 1;
letter-spacing: -0.025em;
}
.subtitle {
margin-top: 7px;
color: var(--muted);
font-family: Georgia, "Times New Roman", serif;
font-size: 14px;
font-style: italic;
}
.reference {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-top: 14px;
}
.reference span {
padding: 5px 8px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(255, 255, 255, 0.48);
color: #405049;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.055em;
text-transform: uppercase;
}
.reference .period {
border-color: rgba(35, 143, 151, 0.25);
background: rgba(35, 143, 151, 0.1);
color: var(--water-dark);
}
.legend {
right: 18px;
bottom: 18px;
width: min(330px, calc(100vw - 36px));
padding: 15px;
border-radius: 14px;
}
.legend-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 11px;
}
.legend h2,
.detail h2 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: 18px;
font-weight: 500;
}
.water-key {
display: inline-flex;
align-items: center;
gap: 6px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
text-transform: uppercase;
}
.water-key::before {
width: 18px;
height: 8px;
border-radius: 2px;
background: var(--water);
content: "";
box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.32);
}
.flow {
position: relative;
display: grid;
gap: 5px;
margin: 0;
padding: 0;
list-style: none;
}
.flow li {
position: relative;
display: grid;
grid-template-columns: 25px 1fr;
align-items: center;
min-height: 27px;
color: #42534b;
font-size: 11px;
font-weight: 650;
}
.flow li:not(:last-child)::after {
position: absolute;
top: 21px;
left: 11px;
width: 2px;
height: 10px;
background: rgba(35, 143, 151, 0.48);
content: "";
}
.flow .number {
display: grid;
width: 22px;
height: 22px;
place-items: center;
border-radius: 50%;
background: rgba(35, 143, 151, 0.12);
color: var(--water-dark);
font-size: 9px;
font-weight: 850;
}
.detail {
margin-top: 12px;
padding-top: 12px;
border-top: 1px solid var(--line);
}
.detail p {
margin: 6px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.note {
margin-top: 11px;
padding: 9px 10px;
border-left: 3px solid var(--gold);
background: rgba(185, 135, 53, 0.08);
color: #68583f;
font-size: 10px;
line-height: 1.45;
}
.tools {
position: fixed;
z-index: 6;
top: 18px;
right: 18px;
display: flex;
gap: 7px;
}
.tool {
min-height: 36px;
padding: 0 12px;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 10px;
background: var(--paper);
box-shadow: 0 8px 22px rgba(32, 43, 38, 0.12);
color: var(--ink);
cursor: pointer;
font-size: 11px;
font-weight: 750;
backdrop-filter: blur(10px);
}
.tool:hover,
.tool.active {
border-color: rgba(35, 143, 151, 0.3);
background: rgba(35, 143, 151, 0.12);
color: var(--water-dark);
}
.hint {
position: fixed;
z-index: 4;
bottom: 18px;
left: 18px;
padding: 8px 11px;
border-radius: 9px;
background: rgba(249, 246, 235, 0.75);
color: #53625b;
font-size: 10px;
font-weight: 650;
letter-spacing: 0.02em;
pointer-events: none;
backdrop-filter: blur(8px);
}
.compass {
position: fixed;
z-index: 4;
top: 66px;
left: 26px;
display: grid;
width: 38px;
height: 38px;
place-items: center;
border: 1px solid rgba(35, 52, 47, 0.2);
border-radius: 50%;
background: rgba(249, 246, 235, 0.7);
color: #394a43;
font-family: Georgia, serif;
font-size: 12px;
font-weight: bold;
pointer-events: none;
}
.compass::after {
position: absolute;
top: 4px;
border-right: 4px solid transparent;
border-bottom: 9px solid var(--water-dark);
border-left: 4px solid transparent;
content: "";
}
.world-label {
position: absolute;
min-width: 92px;
padding: 6px 8px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 8px;
background: rgba(249, 246, 235, 0.88);
box-shadow: 0 5px 16px rgba(32, 43, 38, 0.14);
color: var(--ink);
cursor: pointer;
pointer-events: auto;
text-align: left;
transform: translate(-50%, -100%);
transition: opacity 150ms ease, background 150ms ease;
backdrop-filter: blur(8px);
}
.world-label::after {
position: absolute;
bottom: -5px;
left: calc(50% - 5px);
width: 8px;
height: 8px;
border-right: 1px solid rgba(255, 255, 255, 0.72);
border-bottom: 1px solid rgba(255, 255, 255, 0.72);
background: rgba(249, 246, 235, 0.88);
content: "";
transform: rotate(45deg);
}
.world-label strong {
display: block;
font-family: Georgia, "Times New Roman", serif;
font-size: 12px;
font-weight: 600;
line-height: 1.1;
}
.world-label small {
display: block;
margin-top: 2px;
color: var(--water-dark);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.world-label:hover,
.world-label.selected {
background: #fffaf0;
outline: 2px solid rgba(35, 143, 151, 0.28);
}
#loading {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
background: #dce5dc;
color: var(--water-dark);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
.identity {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 13px 14px;
}
.identity h1 {
font-size: 25px;
}
.subtitle {
font-size: 12px;
}
.reference {
margin-top: 9px;
}
.tools {
top: auto;
right: 10px;
bottom: 10px;
flex-direction: column;
}
.tool {
min-height: 34px;
padding: 0 10px;
}
.legend {
right: 10px;
bottom: 62px;
width: 235px;
max-height: 43vh;
overflow: auto;
padding: 12px;
}
.legend .note {
display: none;
}
.hint {
display: none;
}
.compass {
top: 133px;
left: 17px;
width: 32px;
height: 32px;
}
.world-label {
min-width: 74px;
padding: 5px 6px;
}
.world-label strong {
font-size: 10px;
}
.world-label small {
font-size: 7px;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive voxel reconstruction of a Nasrid garden"></div>
<div id="labels" aria-label="Garden features"></div>
<section class="identity card">
<div class="eyebrow">Historically informed voxel diorama</div>
<h1>Generalife</h1>
<div class="subtitle">A terraced Andalusi garden of water, shade and cultivated earth</div>
<div class="reference">
<span class="period">c. 1330–1350 · Nasrid period</span>
<span>Granada · al-Andalus</span>
</div>
</section>
<div class="tools" aria-label="Scene controls">
<button class="tool active" id="flowButton" type="button">Flow on</button>
<button class="tool active" id="labelsButton" type="button">Labels on</button>
<button class="tool" id="resetButton" type="button">Reset view</button>
</div>
<div class="compass" title="North is toward the rear hillside">N</div>
<aside class="legend card">
<div class="legend-head">
<h2>Gravity-fed water route</h2>
<span class="water-key">acequia</span>
</div>
<ol class="flow">
<li><span class="number">1</span><span>Darro-fed source flume · schematic</span></li>
<li><span class="number">2</span><span>Supply / balancing cistern</span></li>
<li><span class="number">3</span><span>Upper terrace acequia</span></li>
<li><span class="number">4</span><span>Stepped cascade to middle court</span></li>
<li><span class="number">5</span><span>Court of the Water Channel</span></li>
<li><span class="number">6</span><span>Cascade to the lower hortus</span></li>
<li><span class="number">7</span><span>Lower channel and terminal pool</span></li>
</ol>
<div class="detail">
<h2 id="detailTitle">A composite, not a survey</h2>
<p id="detailText">
The compact arrangement combines features associated with the Generalife:
a planted upper terrace, the long water court, arcaded shade structures,
enclosed beds, cypresses and orange trees.
</p>
</div>
<div class="note">
The reference is mid-14th-century Nasrid Granada. The Generalife’s documented
acequia was supplied by the Darro; the cistern and exact miniature layout here
are interpretive rather than claims about one surviving chamber.
</div>
</aside>
<div class="hint">Drag to orbit · Shift/right-drag to pan · Scroll to zoom · R to reset</div>
<div id="loading">Constructing the garden…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const labelsHost = document.getElementById("labels");
const loading = document.getElementById("loading");
const detailTitle = document.getElementById("detailTitle");
const detailText = document.getElementById("detailText");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd8e4dc);
scene.fog = new THREE.Fog(0xd8e4dc, 58, 92);
const camera = new THREE.OrthographicCamera(-25, 25, 18, -18, 0.1, 150);
const cameraTarget = new THREE.Vector3(0, 2.4, 0.3);
const homeView = {
azimuth: 0.72,
polar: 0.84,
zoom: 1
};
const view = { ...homeView };
const hemi = new THREE.HemisphereLight(0xfff8df, 0x53675b, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffedc5, 3.3);
sun.position.set(18, 29, 17);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -28;
sun.shadow.camera.right = 28;
sun.shadow.camera.top = 28;
sun.shadow.camera.bottom = -28;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 80;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x91cbd2, 0.55);
fill.position.set(-18, 11, -13);
scene.add(fill);
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0,
...options
});
const MAT = {
base: material(0x786c50),
soil: material(0x8c7954),
soilDark: material(0x67583f),
stoneA: material(0xc6b58e),
stoneB: material(0xb29f78),
stoneC: material(0xd9cbaa),
plaster: material(0xf0e7d3),
plasterShade: material(0xd9cfb8),
roof: material(0x8e4f39),
wood: material(0x69462f),
leaf: material(0x365f3d),
leafLight: material(0x4d7748),
leafDark: material(0x294a35),
herb: material(0x6f8951),
orange: material(0xd77925, { roughness: 0.55 }),
flowerA: material(0xc95c52),
flowerB: material(0xd6a43d),
tile: material(0x2c6470),
gold: material(0xb88a3f),
mountain: material(0x879783),
mountainDark: material(0x71816d),
ground: material(0xb8b292),
water: material(0x299aa1, {
roughness: 0.24,
metalness: 0.03,
transparent: true,
opacity: 0.88,
emissive: 0x0c3d42,
emissiveIntensity: 0.24
}),
waterDeep: material(0x1b747e, {
roughness: 0.28,
transparent: true,
opacity: 0.92
}),
foam: material(0xd9ffff, {
roughness: 0.2,
emissive: 0x8ad9d6,
emissiveIntensity: 0.35
})
};
const geometryCache = new Map();
function box(w, h, d, x, y, z, mat, parent = scene, shadow = true) {
const key = `${w}|${h}|${d}`;
let geometry = geometryCache.get(key);
if (!geometry) {
geometry = new THREE.BoxGeometry(w, h, d);
geometryCache.set(key, geometry);
}
const mesh = new THREE.Mesh(geometry, mat);
mesh.position.set(x, y, z);
mesh.castShadow = shadow;
mesh.receiveShadow = shadow;
parent.add(mesh);
return mesh;
}
let seed = 1492;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
const T = {
upper: 5.0,
middle: 3.2,
lower: 1.4
};
const waterY = {
upper: 5.34,
middle: 3.58,
lower: 1.78
};
function masonryFront(z, top, xMin, xMax, rows) {
const bw = 1.04;
const bh = 0.42;
const gap = 0.025;
for (let row = 0; row < rows; row++) {
const offset = row % 2 ? bw * 0.48 : 0;
for (let x = xMin + offset; x < xMax; x += bw) {
const width = Math.min(bw, xMax - x);
if (width < 0.2) continue;
const mat = random() > 0.55 ? MAT.stoneA : MAT.stoneB;
box(
width - gap,
bh,
0.34,
x + width * 0.5,
top - 0.23 - row * (bh + gap),
z,
mat
);
}
}
}
function masonrySide(x, zMin, zMax, top, rows) {
const bd = 1.04;
const bh = 0.42;
const gap = 0.025;
for (let row = 0; row < rows; row++) {
const offset = row % 2 ? bd * 0.48 : 0;
for (let z = zMin + offset; z < zMax; z += bd) {
const depth = Math.min(bd, zMax - z);
if (depth < 0.2) continue;
const mat = random() > 0.5 ? MAT.stoneA : MAT.stoneB;
box(
0.34,
bh,
depth - gap,
x,
top - 0.23 - row * (bh + gap),
z + depth * 0.5,
mat
);
}
}
}
function buildTerraces() {
box(45, 0.45, 41, 0, -0.35, 0, MAT.ground);
// Solid stepped platforms.
box(28, T.upper, 8, 0, T.upper * 0.5, -7, MAT.stoneB);
box(28, T.middle, 7, 0, T.middle * 0.5, 0.5, MAT.stoneB);
box(28, T.lower, 7, 0, T.lower * 0.5, 7.5, MAT.stoneB);
// Thin planted surfaces emphasize the voxel tiers.
box(27.7, 0.08, 7.75, 0, T.upper + 0.04, -7, MAT.soil);
box(27.7, 0.08, 6.75, 0, T.middle + 0.04, 0.5, MAT.soil);
box(27.7, 0.08, 6.75, 0, T.lower + 0.04, 7.5, MAT.soil);
// Exposed block masonry on terrace faces.
masonryFront(-2.81, T.upper, -14, 14, 11);
masonryFront(4.19, T.middle, -14, 14, 7);
masonryFront(11.19, T.lower, -14, 14, 3);
masonrySide(-14.18, -11, -3, T.upper, 10);
masonrySide(14.18, -11, -3, T.upper, 10);
masonrySide(-14.18, -3, 4, T.middle, 6);
masonrySide(14.18, -3, 4, T.middle, 6);
masonrySide(-14.18, 4, 11, T.lower, 3);
masonrySide(14.18, 4, 11, T.lower, 3);
// Stairs preserve circulation independently of the water route.
for (let i = 0; i < 8; i++) {
const top = T.upper - (i + 1) * ((T.upper - T.middle) / 8);
const z = -3.15 + (i + 0.5) * 0.47;
box(2.8, 0.22, 0.49, -12.1, top - 0.11, z, MAT.stoneC);
}
for (let i = 0; i < 8; i++) {
const top = T.middle - (i + 1) * ((T.middle - T.lower) / 8);
const z = 4.12 + (i + 0.5) * 0.47;
box(2.8, 0.22, 0.49, 12.0, top - 0.11, z, MAT.stoneC);
}
// A low parapet along the exposed upper edges.
for (let x = -13.6; x <= 13.6; x += 1.2) {
box(0.52, 0.5, 0.48, x, T.upper + 0.25, -3.15, MAT.plaster);
box(0.52, 0.5, 0.48, x, T.upper + 0.25, -10.75, MAT.plaster);
}
// Rear, rear-left and rear-right retaining walls.
box(8.8, 1.25, 0.38, 7.4, T.upper + 0.625, -10.87, MAT.plaster);
box(6.5, 1.25, 0.38, 12.1, T.upper + 0.625, -10.87, MAT.plaster);
box(0.38, 1.25, 8, -13.85, T.upper + 0.625, -7, MAT.plaster);
}
function addChannelX(x1, x2, z, y, width = 0.78) {
const length = Math.abs(x2 - x1);
const center = (x1 + x2) * 0.5;
box(length + 0.18, 0.12, width + 0.34, center, y - 0.10, z, MAT.stoneB);
box(length, 0.07, width, center, y, z, MAT.water, scene, false);
box(length, 0.27, 0.14, center, y + 0.02, z - width * 0.5 - 0.07, MAT.stoneC);
box(length, 0.27, 0.14, center, y + 0.02, z + width * 0.5 + 0.07, MAT.stoneC);
}
function addChannelZ(x, z1, z2, y, width = 0.65) {
const length = Math.abs(z2 - z1);
const center = (z1 + z2) * 0.5;
box(width + 0.28, 0.12, length + 0.14, x, y - 0.10, center, MAT.stoneB);
box(width, 0.07, length, x, y, center, MAT.water, scene, false);
box(0.13, 0.27, length, x - width * 0.5 - 0.07, y + 0.02, center, MAT.stoneC);
box(0.13, 0.27, length, x + width * 0.5 + 0.07, y + 0.02, center, MAT.stoneC);
}
function cascadeZ(x, z0, z1, y0, y1, width = 0.76, steps = 10) {
const direction = Math.sign(z1 - z0);
const length = Math.abs(z1 - z0) / steps;
let previousY = y0;
for (let i = 0; i < steps; i++) {
const zA = z0 + direction * i * length;
const zB = z0 + direction * (i + 1) * length;
const y = y0 + (y1 - y0) * ((i + 1) / steps);
const z = (zA + zB) * 0.5;
box(width, 0.07, length + 0.04, x, y, z, MAT.water, scene, false);
box(0.14, 0.34, length + 0.04, x - width * 0.5 - 0.07, y + 0.13, z, MAT.stoneC);
box(0.14, 0.34, length + 0.04, x + width * 0.5 + 0.07, y + 0.13, z, MAT.stoneC);
const riserHeight = Math.abs(y - previousY) + 0.06;
box(
width,
riserHeight,
0.07,
x,
(y + previousY) * 0.5,
zA + direction * 0.01,
MAT.water,
scene,
false
);
previousY = y;
}
}
function buildWaterSystem() {
// Schematic Darro-fed flume entering from the rear hillside.
addChannelZ(-11.5, -11.15, -9.55, 6.18, 0.52);
// Supply / balancing cistern.
const cx = -11.5;
const cz = -9.4;
box(3.4, 0.22, 3.4, cx, T.upper + 0.16, cz, MAT.stoneB);
box(0.34, 1.35, 3.4, cx - 1.53, T.upper + 0.82, cz, MAT.stoneC);
box(0.34, 1.35, 3.4, cx + 1.53, T.upper + 0.82, cz, MAT.stoneC);
box(3.4, 1.35, 0.34, cx, T.upper + 0.82, cz - 1.53, MAT.stoneC);
// Front wall has a centered outlet.
box(1.12, 1.35, 0.34, cx - 0.96, T.upper + 0.82, cz + 1.53, MAT.stoneC);
box(1.12, 1.35, 0.34, cx + 0.96, T.upper + 0.82, cz + 1.53, MAT.stoneC);
box(0.56, 0.38, 0.34, cx, T.upper + 1.31, cz + 1.53, MAT.stoneC);
box(2.68, 0.10, 2.68, cx, 6.18, cz, MAT.waterDeep, scene, false);
// The cistern overflow drops into the upper acequia.
cascadeZ(cx, cz + 1.65, -7.0, 6.18, waterY.upper, 0.58, 5);
// Upper terrace channel: west to east.
addChannelX(-11.5, 9.5, -7.0, waterY.upper);
// Upper cascade: east edge, falling toward the middle court.
cascadeZ(9.5, -7.0, -1.5, waterY.upper, waterY.middle, 0.76, 10);
// Court channel: east to west.
addChannelX(-9.5, 9.5, -1.5, waterY.middle);
// Lower cascade: west edge, falling toward the lower hortus.
cascadeZ(-9.5, -1.5, 6.5, waterY.middle, waterY.lower, 0.76, 12);
// Lower channel: west to east, ending in a small pool.
addChannelX(-9.5, 10.1, 6.5, waterY.lower);
const poolX = 11.55;
const poolZ = 6.5;
box(3.55, 0.13, 3.0, poolX, waterY.lower - 0.11, poolZ, MAT.stoneB);
box(3.25, 0.08, 2.7, poolX, waterY.lower, poolZ, MAT.waterDeep, scene, false);
box(0.18, 0.36, 3.05, poolX - 1.73, waterY.lower + 0.09, poolZ, MAT.stoneC);
box(0.18, 0.36, 3.05, poolX + 1.73, waterY.lower + 0.09, poolZ, MAT.stoneC);
box(3.55, 0.36, 0.18, poolX, waterY.lower + 0.09, poolZ - 1.44, MAT.stoneC);
box(3.55, 0.36, 0.18, poolX, waterY.lower + 0.09, poolZ + 1.44, MAT.stoneC);
}
function merlons(x1, x2, z, y) {
for (let x = x1; x <= x2; x += 1.15) {
box(0.52, 0.48, 0.52, x, y + 0.24, z, MAT.plaster);
}
}
function pavilion(cx, z, width, baseY) {
const half = width * 0.5;
const columnCount = Math.max(4, Math.round(width / 1.35));
const spacing = width / columnCount;
box(width + 0.9, 0.18, 1.55, cx, baseY + 0.09, z, MAT.stoneC);
box(width, 2.15, 0.28, cx, baseY + 1.22, z - 0.53, MAT.plasterShade);
for (let i = 0; i <= columnCount; i++) {
const x = cx - half + i * spacing;
box(0.36, 2.38, 0.42, x, baseY + 1.28, z + 0.42, MAT.plaster);
box(0.55, 0.18, 0.58, x, baseY + 0.18, z + 0.42, MAT.stoneC);
box(0.54, 0.15, 0.55, x, baseY + 2.49, z + 0.42, MAT.stoneC);
}
box(width + 0.75, 0.26, 1.25, cx, baseY + 2.64, z + 0.12, MAT.plaster);
box(width + 0.9, 0.20, 1.45, cx, baseY + 2.86, z + 0.12, MAT.roof);
merlons(cx - half, cx + half, z + 0.16, baseY + 2.95);
// Small glazed tile bands.
for (let x = cx - half + 0.35; x < cx + half; x += 0.72) {
box(0.42, 0.24, 0.06, x, baseY + 2.51, z + 0.76, MAT.tile);
box(0.13, 0.24, 0.065, x + 0.25, baseY + 2.51, z + 0.765, MAT.gold);
}
}
function arcade(x1, x2, z, baseY) {
const count = Math.max(3, Math.round((x2 - x1) / 1.5));
const spacing = (x2 - x1) / count;
for (let i = 0; i <= count; i++) {
const x = x1 + i * spacing;
box(0.36, 2.15, 0.42, x, baseY + 1.08, z, MAT.plaster);
box(0.56, 0.18, 0.58, x, baseY + 0.09, z, MAT.stoneC);
box(0.54, 0.16, 0.54, x, baseY + 2.23, z, MAT.stoneC);
}
box(x2 - x1 + 0.75, 0.22, 0.72, (x1 + x2) * 0.5, baseY + 2.39, z, MAT.plaster);
for (let x = x1 + 0.25; x < x2; x += 0.68) {
box(0.38, 0.20, 0.055, x, baseY + 2.27, z + 0.38, MAT.tile);
}
}
function buildArchitecture() {
pavilion(2.3, -9.35, 7.4, T.upper);
// Arcades flank the middle court and its acequia.
arcade(-6.8, 6.8, -2.68, T.middle);
arcade(-6.8, 6.8, -0.31, T.middle);
// A smaller shaded structure in the lower garden.
pavilion(-1.7, 9.0, 5.4, T.lower);
// Pale paving makes circulation legible against the planted soil.
for (let x = -11; x <= 11; x += 1.15) {
box(0.82, 0.035, 0.72, x, T.middle + 0.11, -2.82, MAT.stoneC, scene, false);
box(0.82, 0.035, 0.72, x, T.middle + 0.11, -0.16, MAT.stoneC, scene, false);
}
}
function raisedBed(x, z, w, d, baseY) {
box(w + 0.24, 0.20, d + 0.24, x, baseY + 0.10, z, MAT.stoneB);
box(w, 0.08, d, x, baseY + 0.24, z, MAT.soilDark, scene, false);
const count = Math.max(3, Math.floor(w * d * 2.6));
for (let i = 0; i < count; i++) {
const px = x + (random() - 0.5) * (w - 0.3);
const pz = z + (random() - 0.5) * (d - 0.3);
const h = 0.13 + random() * 0.16;
const shrub = random() > 0.45 ? MAT.herb : MAT.leafLight;
box(0.18, h, 0.18, px, baseY + 0.28 + h * 0.5, pz, shrub);
}
}
function hedge(x, z, w, d, baseY) {
const length = Math.max(w, d);
const horizontal = w >= d;
for (let i = 0; i < length; i += 0.48) {
const offset = -length * 0.5 + i + 0.22;
const px = horizontal ? x + offset : x;
const pz = horizontal ? z : z + offset;
const mat = i % 2 < 0.48 ? MAT.leaf : MAT.leafLight;
box(0.52, 0.58, d, px, baseY + 0.38, pz, mat);
box(0.52, 0.58, w, px, baseY + 0.38, pz, mat);
}
}
function cypress(x, z, baseY, height = 3.2) {
box(0.18, 0.8, 0.18, x, baseY + 0.4, z, MAT.wood);
const levels = 7;
for (let i = 0; i < levels; i++) {
const t = i / (levels - 1);
const size = 0.94 * (1 - t * 0.64);
const y = baseY + 0.72 + i * (height / levels);
const mat = i % 2 ? MAT.leafDark : MAT.leaf;
box(size, height / levels + 0.16, size, x, y, z, mat);
}
}
function orangeTree(x, z, baseY) {
box(0.25, 1.25, 0.25, x, baseY + 0.63, z, MAT.wood);
box(0.18, 0.65, 0.18, x - 0.30, baseY + 1.25, z, MAT.wood);
box(0.18, 0.65, 0.18, x + 0.30, baseY + 1.25, z, MAT.wood);
box(1.25, 0.88, 1.05, x, baseY + 1.65, z, MAT.leaf);
box(0.82, 0.72, 1.22, x - 0.38, baseY + 1.70, z + 0.08, MAT.leafLight);
box(0.76, 0.75, 0.82, x + 0.42, baseY + 1.72, z - 0.06, MAT.leafDark);
const fruits = [
[-0.43, 1.44, 0.54],
[0.38, 1.48, 0.52],
[-0.18, 1.91, 0.56],
[0.55, 1.87, -0.32],
[-0.54, 1.94, -0.23]
];
fruits.forEach(([fx, fy, fz]) => {
box(0.15, 0.15, 0.15, x + fx, baseY + fy, z + fz, MAT.orange);
});
}
function buildPlanting() {
const upper = T.upper + 0.08;
const middle = T.middle + 0.08;
const lower = T.lower + 0.08;
// Upper hortus beds.
raisedBed(-6.9, -4.65, 3.3, 1.45, upper);
raisedBed(-2.8, -4.65, 3.0, 1.45, upper);
raisedBed(1.6, -4.65, 3.0, 1.45, upper);
raisedBed(5.7, -4.65, 3.1, 1.45, upper);
hedge(-4.9, -5.65, 8.5, 0.28, upper);
hedge(3.7, -5.65, 8.4, 0.28, upper);
// Orange trees and cypresses around the upper pavilion.
orangeTree(-8.4, -8.95, upper);
orangeTree(-0.8, -8.9, upper);
orangeTree(5.3, -8.85, upper);
cypress(-10.0, -8.9, upper, 3.5);
cypress(8.0, -9.0, upper, 3.5);
cypress(10.4, -8.9, upper, 3.1);
// Middle court: restrained planting leaves the acequia visible.
raisedBed(-11.25, 0.7, 1.5, 2.2, middle);
raisedBed(11.25, 0.7, 1.5, 2.2, middle);
cypress(-11.2, -0.8, middle, 3.0);
cypress(11.2, -0.8, middle, 3.0);
orangeTree(-8.5, 1.55, middle);
orangeTree(7.8, 1.55, middle);
// Lower hortus.
raisedBed(-5.9, 4.95, 3.2, 1.25, lower);
raisedBed(-1.8, 4.95, 3.0, 1.25, lower);
raisedBed(2.5, 4.95, 3.2, 1.25, lower);
raisedBed(6.6, 4.95, 2.7, 1.25, lower);
raisedBed(-5.5, 8.35, 3.1, 1.35, lower);
raisedBed(-1.2, 8.35, 3.1, 1.35, lower);
raisedBed(3.1, 8.35, 3.1, 1.35, lower);
orangeTree(-8.0, 8.4, lower);
orangeTree(-6.9, 10.0, lower);
orangeTree(0.4, 8.35, lower);
orangeTree(6.2, 8.45, lower);
orangeTree(7.0, 10.0, lower);
cypress(-10.7, 8.5, lower, 3.6);
cypress(-10.7, 10.1, lower, 3.4);
cypress(8.8, 4.95, lower, 3.1);
hedge(-3.4, 9.42, 6.6, 0.25, lower);
}
function buildBackground() {
// Blocky Alhambra-sierra silhouettes give the source flume a context.
const hillMat = [MAT.mountain, MAT.mountainDark];
for (let i = 0; i < 22; i++) {
const x = -25 + i * 2.35 + (random() - 0.5) * 1.4;
const z = -18.5 - random() * 5.5;
const h = 3.0 + random() * 5.5;
const w = 2.6 + random() * 4.0;
const d = 2.8 + random() * 4.2;
box(w, h, d, x, h * 0.5 - 0.15, z, hillMat[i % 2], scene, false);
}
// Sparse cubic shrubs in the middle distance.
for (let i = 0; i < 14; i++) {
const x = -24 + random() * 48;
const z = -13.2 - random() * 5;
const size = 0.6 + random() * 1.2;
box(size, size * 0.8, size, x, size * 0.4, z, MAT.mountainDark, scene, false);
}
}
buildTerraces();
buildWaterSystem();
buildArchitecture();
buildPlanting();
buildBackground();
/*
* Animated foam follows the complete hydraulic route.
* The polyline mirrors the visible cistern, channels, cascades and pool.
*/
const flowPoints = [
new THREE.Vector3(-11.5, 6.25, -11.15),
new THREE.Vector3(-11.5, 6.25, -9.4),
new THREE.Vector3(-11.5, 6.25, -7.9)
];
for (let i = 1; i <= 5; i++) {
flowPoints.push(
new THREE.Vector3(
-11.5,
6.18 + (waterY.upper - 6.18) * (i / 5),
-7.9 + 0.9 * (i / 5)
)
);
}
flowPoints.push(new THREE.Vector3(9.5, waterY.upper + 0.08, -7.0));
for (let i = 1; i <= 10; i++) {
flowPoints.push(
new THREE.Vector3(
9.5,
waterY.upper + (waterY.middle - waterY.upper) * (i / 10),
-7.0 + 5.5 * (i / 10)
)
);
}
flowPoints.push(new THREE.Vector3(-9.5, waterY.middle + 0.08, -1.5));
for (let i = 1; i <= 12; i++) {
flowPoints.push(
new THREE.Vector3(
-9.5,
waterY.middle + (waterY.lower - waterY.middle) * (i / 12),
-1.5 + 8.0 * (i / 12)
)
);
}
flowPoints.push(
new THREE.Vector3(9.7, waterY.lower + 0.08, 6.5),
new THREE.Vector3(11.55, waterY.lower + 0.08, 6.5)
);
const segmentLengths = [];
let totalFlowLength = 0;
for (let i = 0; i < flowPoints.length - 1; i++) {
const length = flowPoints[i].distanceTo(flowPoints[i + 1]);
segmentLengths.push(length);
totalFlowLength += length;
}
const foamGroup = new THREE.Group();
scene.add(foamGroup);
const foamMarkers = [];
for (let i = 0; i < 20; i++) {
const marker = box(0.13, 0.055, 0.13, 0, 0, 0, MAT.foam, foamGroup, false);
foamMarkers.push(marker);
}
function sampleFlow(distance) {
let remaining = ((distance % totalFlowLength) + totalFlowLength) % totalFlowLength;
for (let i = 0; i < segmentLengths.length; i++) {
if (remaining <= segmentLengths[i]) {
const t = segmentLengths[i] === 0 ? 0 : remaining / segmentLengths[i];
return new THREE.Vector3().lerpVectors(flowPoints[i], flowPoints[i + 1], t);
}
remaining -= segmentLengths[i];
}
return flowPoints[0].clone();
}
let flowEnabled = true;
const labelData = [
{
title: "Darro source",
sub: "schematic flume",
position: [-11.5, 7.05, -10.8],
text: "The rear flume represents the Darro-fed supply associated with the Generalife acequia. Its position is diagrammatic rather than a reconstruction of the mountain intake."
},
{
title: "Supply cistern",
sub: "interpretive",
position: [-11.5, 7.05, -9.35],
text: "This balancing cistern stores and steadies the incoming flow. It is included to make the miniature’s supply legible; no equivalent cistern is asserted for this exact Generalife location."
},
{
title: "Upper acequia",
sub: "terrace channel",
position: [-0.3, 6.25, -7.0],
text: "Water leaves the cistern and travels across the upper terrace. The level channel and stone troughs evoke the practical acequia system used to irrigate Andalusi gardens."
},
{
title: "First cascade",
sub: "gravity drop",
position: [9.5, 5.45, -4.25],
text: "The channel crosses the terrace edge and drops by gravity to the middle garden. The scene contains no pump: every blue connection descends toward the viewer."
},
{
title: "Court of the Water Channel",
sub: "middle terrace",
position: [-0.4, 4.45, -1.5],
text: "The long middle channel, shaded arcades and geometric beds refer to the Generalife’s celebrated Patio de la Acequia and its intimate relationship between architecture, water and cultivation."
},
{
title: "Second cascade",
sub: "terrace transition",
position: [-9.5, 3.0, 2.5],
text: "At the west end, the acequia drops again into the lower hortus. Keeping the route continuous preserves the essential downhill relationship between the terraces."
},
{
title: "Lower hortus",
sub: "productive garden",
position: [2.1, 2.65, 6.5],
text: "The lowest terrace combines irrigated beds, fruit trees, hedges and shade. Historically, such enclosed productive gardens supplied aromatic plants, fruit and ornamental species."
},
{
title: "Terminal pool",
sub: "lower collection basin",
position: [11.55, 2.85, 6.5],
text: "The lower channel terminates in a small pool. In historic Andalusi gardens, such water was valued aesthetically, for cooling and for sustaining irrigation; this basin is an interpretive simplification."
}
];
labelsHost.innerHTML = labelData.map((item, index) => `
<button class="world-label" type="button" data-label="${index}">
<strong>${item.title}</strong>
<small>${item.sub}</small>
</button>
`).join("");
const labelElements = [...labelsHost.querySelectorAll(".world-label")];
labelElements.forEach((element, index) => {
element.addEventListener("click", (event) => {
event.stopPropagation();
labelElements.forEach((label) => label.classList.remove("selected"));
element.classList.add("selected");
detailTitle.textContent = labelData[index].title;
detailText.textContent = labelData[index].text;
});
});
let labelsVisible = true;
function updateLabels() {
if (!labelsVisible) return;
const width = window.innerWidth;
const height = window.innerHeight;
labelData.forEach((item, index) => {
const projected = new THREE.Vector3(...item.position).project(camera);
const element = labelElements[index];
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.2 &&
projected.x < 1.2 &&
projected.y > -1.2 &&
projected.y < 1.2;
element.style.display = visible ? "block" : "none";
if (visible) {
element.style.left = `${(projected.x * 0.5 + 0.5) * width}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * height}px`;
}
});
}
function updateCamera() {
const distance = 52;
const sinPolar = Math.sin(view.polar);
camera.position.set(
cameraTarget.x + distance * sinPolar * Math.sin(view.azimuth),
cameraTarget.y + distance * Math.cos(view.polar),
cameraTarget.z + distance * sinPolar * Math.cos(view.azimuth)
);
camera.lookAt(cameraTarget);
camera.zoom = view.zoom;
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = Math.max(31, 37 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
resize();
updateCamera();
/*
* Custom camera controls: orbit, pan and zoom are implemented here
* without importing any Three.js addons.
*/
const pointers = new Map();
let dragMode = "orbit";
let lastX = 0;
let lastY = 0;
let pinchDistance = 0;
const canvas = renderer.domElement;
canvas.tabIndex = 0;
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
dragMode = event.button === 2 || event.shiftKey ? "pan" : "orbit";
lastX = event.clientX;
lastY = event.clientY;
} else if (pointers.size === 2) {
const points = [...pointers.values()];
pinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size >= 2) {
const points = [...pointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (pinchDistance > 0 && distance > 0) {
view.zoom = THREE.MathUtils.clamp(
view.zoom * (distance / pinchDistance),
0.62,
2.35
);
}
pinchDistance = distance;
return;
}
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
if (dragMode === "orbit") {
view.azimuth -= dx * 0.005;
view.polar = THREE.MathUtils.clamp(view.polar - dy * 0.0045, 0.30, 1.34);
} else {
const scale = (camera.top - camera.bottom) / camera.zoom / window.innerHeight;
const right = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 0);
right.y = 0;
right.normalize();
const forward = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 2);
forward.y = 0;
forward.normalize();
cameraTarget.addScaledVector(right, -dx * scale);
cameraTarget.addScaledVector(forward, dy * scale);
cameraTarget.x = THREE.MathUtils.clamp(cameraTarget.x, -16, 16);
cameraTarget.z = THREE.MathUtils.clamp(cameraTarget.z, -14, 14);
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) pinchDistance = 0;
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
view.zoom = THREE.MathUtils.clamp(
view.zoom * Math.exp(-event.deltaY * 0.001),
0.62,
2.35
);
},
{ passive: false }
);
function resetView() {
view.azimuth = homeView.azimuth;
view.polar = homeView.polar;
view.zoom = homeView.zoom;
cameraTarget.set(0, 2.4, 0.3);
}
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") {
resetView();
} else if (event.key === "ArrowLeft") {
view.azimuth -= 0.08;
} else if (event.key === "ArrowRight") {
view.azimuth += 0.08;
} else if (event.key === "ArrowUp") {
view.polar = THREE.MathUtils.clamp(view.polar - 0.06, 0.30, 1.34);
} else if (event.key === "ArrowDown") {
view.polar = THREE.MathUtils.clamp(view.polar + 0.06, 0.30, 1.34);
} else if (event.key === "+" || event.key === "=") {
view.zoom = THREE.MathUtils.clamp(view.zoom * 1.08, 0.62, 2.35);
} else if (event.key === "-") {
view.zoom = THREE.MathUtils.clamp(view.zoom / 1.08, 0.62, 2.35);
}
});
document.getElementById("resetButton").addEventListener("click", resetView);
document.getElementById("flowButton").addEventListener("click", (event) => {
flowEnabled = !flowEnabled;
foamGroup.visible = flowEnabled;
event.currentTarget.textContent = flowEnabled ? "Flow on" : "Flow off";
event.currentTarget.classList.toggle("active", flowEnabled);
});
document.getElementById("labelsButton").addEventListener("click", (event) => {
labelsVisible = !labelsVisible;
labelsHost.style.display = labelsVisible ? "block" : "none";
event.currentTarget.textContent = labelsVisible ? "Labels on" : "Labels off";
event.currentTarget.classList.toggle("active", labelsVisible);
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
updateCamera();
if (flowEnabled) {
foamMarkers.forEach((marker, index) => {
const offset = index / foamMarkers.length;
const distance = (elapsed * 2.1 + offset * totalFlowLength) % totalFlowLength;
const point = sampleFlow(distance);
marker.position.copy(point);
marker.rotation.y = elapsed * 1.8 + index;
});
}
updateLabels();
renderer.render(scene, camera);
}
animate();
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 400);
});
</script>
</body>
</html>
```