Prompt specification
Build or customize the Three.js 3D scene "Workshop comparing sun and water clocks" 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: Workshop comparing sun and water clocks
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/workshop-comparing-sun-and-water-clocks-x2555
Scene Description Prompt:
A voxel historical courtyard comparing sundial and water-clock readings against one scene clock, with operating assumptions identified. Three.js from cdnjs, one HTML file.
Working Three.js Starter (workshop-comparing-sun-and-water-clocks-x2555):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>The Courtyard of Three Hours — Voxel Clock Laboratory</title>
<style>
:root {
color-scheme: dark;
--ink: #f5eddd;
--muted: #bdb9ae;
--panel: rgba(24, 31, 35, 0.86);
--panel-strong: rgba(18, 24, 28, 0.96);
--line: rgba(238, 221, 190, 0.2);
--gold: #e4b967;
--blue: #78c6d5;
--green: #95c89a;
--red: #e48978;
--shadow: 0 18px 60px rgba(0, 0, 0, 0.28);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #18232b;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input,
select {
font: inherit;
}
button {
color: var(--ink);
}
#app {
position: fixed;
inset: 0;
}
#app canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#app canvas.grabbing {
cursor: grabbing;
}
.glass {
background: var(--panel);
border: 1px solid var(--line);
box-shadow: var(--shadow);
backdrop-filter: blur(14px) saturate(120%);
-webkit-backdrop-filter: blur(14px) saturate(120%);
}
#intro {
position: fixed;
z-index: 5;
top: 18px;
left: 18px;
width: min(390px, calc(100vw - 36px));
padding: 18px 19px 16px;
border-radius: 18px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 9px;
color: var(--gold);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(29px, 3.1vw, 44px);
font-weight: 500;
line-height: 0.98;
letter-spacing: -0.025em;
}
#intro p {
margin: 12px 0 13px;
max-width: 355px;
color: #d2cec3;
font-size: 12px;
line-height: 1.55;
}
.assumption-chips {
display: flex;
flex-wrap: wrap;
gap: 6px;
}
.assumption-chips span {
padding: 5px 8px;
border: 1px solid rgba(228, 185, 103, 0.28);
border-radius: 999px;
background: rgba(228, 185, 103, 0.08);
color: #e8d8b9;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.055em;
text-transform: uppercase;
}
#readouts {
position: fixed;
z-index: 6;
top: 18px;
right: 18px;
width: 322px;
padding: 13px;
border-radius: 18px;
}
.readout-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin: 0 3px 10px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.readout-heading b {
color: var(--gold);
font-weight: 800;
}
.clock-card {
margin-top: 8px;
padding: 13px;
border: 1px solid rgba(255, 255, 255, 0.075);
border-radius: 13px;
background: rgba(255, 255, 255, 0.035);
cursor: pointer;
transition:
border-color 160ms ease,
transform 160ms ease,
background 160ms ease;
}
.clock-card:hover {
transform: translateY(-2px);
border-color: rgba(228, 185, 103, 0.35);
background: rgba(255, 255, 255, 0.06);
}
.clock-card.reference {
border-color: rgba(228, 185, 103, 0.35);
background: rgba(228, 185, 103, 0.07);
}
.clock-title {
display: flex;
align-items: center;
justify-content: space-between;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.clock-number {
margin-top: 5px;
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
font-size: 31px;
font-weight: 600;
letter-spacing: -0.06em;
font-variant-numeric: tabular-nums;
}
.clock-card.sundial .clock-number {
color: #f0cf91;
}
.clock-card.water .clock-number {
color: #91d2df;
}
.clock-subrow {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: 5px;
color: #a9aaa6;
font-size: 10px;
}
.delta {
padding: 3px 7px;
border-radius: 999px;
background: rgba(255, 255, 255, 0.07);
color: #c4c0b7;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 9px;
font-weight: 700;
white-space: nowrap;
}
.delta.ahead {
color: #b7dfb9;
background: rgba(149, 200, 154, 0.12);
}
.delta.behind {
color: #efb0a2;
background: rgba(228, 137, 120, 0.12);
}
#comparisonNote {
margin: 10px 4px 1px;
color: #aaa9a3;
font-size: 9px;
line-height: 1.45;
}
#controls {
position: fixed;
z-index: 7;
bottom: 18px;
left: 50%;
display: flex;
align-items: center;
gap: 12px;
width: min(900px, calc(100vw - 36px));
padding: 11px 13px;
border-radius: 17px;
transform: translateX(-50%);
}
.control-group {
display: flex;
align-items: center;
gap: 7px;
}
.control-group.grow {
min-width: 180px;
flex: 1;
}
.control-label {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
input[type="date"],
select,
.tool-button {
height: 34px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.055);
color: var(--ink);
outline: none;
}
input[type="date"] {
padding: 0 8px;
color-scheme: dark;
font-size: 11px;
}
select {
padding: 0 26px 0 9px;
font-size: 11px;
}
.tool-button {
padding: 0 11px;
cursor: pointer;
font-size: 10px;
font-weight: 750;
white-space: nowrap;
}
.tool-button:hover,
select:hover,
input[type="date"]:hover {
border-color: rgba(228, 185, 103, 0.45);
background: rgba(228, 185, 103, 0.09);
}
.tool-button.primary {
min-width: 76px;
border-color: rgba(228, 185, 103, 0.48);
background: rgba(228, 185, 103, 0.13);
color: #f2d59e;
}
input[type="range"] {
width: 100%;
min-width: 130px;
accent-color: var(--gold);
cursor: pointer;
}
.toggle {
display: flex;
align-items: center;
gap: 6px;
color: var(--muted);
font-size: 10px;
white-space: nowrap;
}
.toggle input {
accent-color: var(--gold);
}
.divider {
width: 1px;
height: 28px;
background: var(--line);
}
#cameraHint {
position: fixed;
z-index: 4;
right: 20px;
bottom: 79px;
color: rgba(255, 255, 255, 0.66);
font-size: 9px;
letter-spacing: 0.03em;
text-shadow: 0 1px 5px rgba(0, 0, 0, 0.75);
pointer-events: none;
}
#labels {
position: fixed;
z-index: 3;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
left: 0;
top: 0;
padding: 6px 9px;
border: 1px solid rgba(255, 255, 255, 0.22);
border-radius: 999px;
background: rgba(21, 27, 31, 0.82);
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
color: #f5eddd;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
white-space: nowrap;
transform: translate(-50%, -100%);
transition: opacity 120ms ease;
}
.world-label::after {
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 12px;
background: rgba(255, 255, 255, 0.45);
content: "";
}
#assumptionsPanel {
position: fixed;
z-index: 20;
top: 0;
right: 0;
width: min(450px, 100vw);
height: 100%;
overflow-y: auto;
padding: 26px;
border-left: 1px solid var(--line);
background: var(--panel-strong);
box-shadow: -24px 0 70px rgba(0, 0, 0, 0.38);
transform: translateX(105%);
transition: transform 260ms cubic-bezier(0.22, 0.72, 0.22, 1);
}
#assumptionsPanel.open {
transform: translateX(0);
}
#assumptionsPanel h2 {
margin: 5px 35px 8px 0;
font-family: Georgia, "Times New Roman", serif;
font-size: 32px;
font-weight: 500;
line-height: 1.03;
}
#assumptionsPanel h3 {
margin: 24px 0 8px;
color: var(--gold);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
#assumptionsPanel p,
#assumptionsPanel li {
color: #c9c6bd;
font-size: 12px;
line-height: 1.6;
}
#assumptionsPanel ul {
margin: 8px 0;
padding-left: 19px;
}
#assumptionsPanel li {
margin: 8px 0;
}
.assumption-box {
margin: 17px 0;
padding: 13px;
border: 1px solid rgba(228, 185, 103, 0.22);
border-radius: 12px;
background: rgba(228, 185, 103, 0.055);
}
.assumption-box strong {
display: block;
margin-bottom: 4px;
color: #f0d39b;
font-size: 11px;
}
.assumption-box span {
color: #c6c2b7;
font-size: 11px;
line-height: 1.5;
}
.metric-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 13px;
}
.metric {
padding: 10px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.035);
}
.metric span {
display: block;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.metric b {
display: block;
margin-top: 4px;
color: #f1e7d6;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 11px;
font-weight: 600;
}
#closeAssumptions {
position: absolute;
top: 18px;
right: 18px;
width: 34px;
height: 34px;
border: 1px solid var(--line);
border-radius: 50%;
background: rgba(255, 255, 255, 0.05);
color: var(--ink);
cursor: pointer;
font-size: 20px;
}
#scrim {
position: fixed;
z-index: 19;
inset: 0;
background: rgba(0, 0, 0, 0.32);
opacity: 0;
pointer-events: none;
transition: opacity 240ms ease;
}
#scrim.open {
opacity: 1;
pointer-events: auto;
}
@media (max-width: 900px) {
#intro {
width: 330px;
}
#intro p {
display: none;
}
#readouts {
width: 280px;
}
#controls {
flex-wrap: wrap;
}
.control-group.grow {
order: 5;
width: 100%;
flex-basis: 100%;
}
}
@media (max-width: 650px) {
#intro {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 12px 14px;
}
h1 {
font-size: 27px;
}
.assumption-chips {
display: none;
}
#readouts {
top: 108px;
right: 10px;
width: 225px;
padding: 9px;
}
.clock-card {
padding: 9px;
}
.clock-number {
font-size: 24px;
}
.clock-subrow {
align-items: flex-start;
flex-direction: column;
gap: 4px;
}
#comparisonNote {
display: none;
}
#controls {
bottom: 10px;
width: calc(100vw - 20px);
gap: 8px;
padding: 9px;
}
.control-label {
display: none;
}
.divider,
#cameraHint,
#labels {
display: none;
}
.tool-button {
padding: 0 8px;
}
}
</style>
</head>
<body>
<div id="app"></div>
<div id="labels" aria-hidden="true">
<div class="world-label" id="labelClock">I · Scene Clock</div>
<div class="world-label" id="labelSundial">II · Sundial</div>
<div class="world-label" id="labelWater">III · Clepsydra</div>
</div>
<header id="intro" class="glass">
<div class="eyebrow">Voxel Time Laboratory · Renaissance Courtyard</div>
<h1>The Courtyard<br>of Three Hours</h1>
<p>
One civil clock governs the courtyard. Compare it with an apparent-solar
sundial and a constant-flow water clock whose calibration slowly drifts.
</p>
<div class="assumption-chips">
<span>Scene: civil UTC+1</span>
<span>Dial: apparent solar</span>
<span>Water: reset at 06:00</span>
</div>
</header>
<aside id="readouts" class="glass" aria-label="Clock readings">
<div class="readout-heading">
<span>Live comparison</span>
<b id="dateLabel">21 Jun 2024</b>
</div>
<section class="clock-card reference" data-focus="clock">
<div class="clock-title">
<span>I · Scene Clock</span>
<span>Reference</span>
</div>
<div class="clock-number" id="sceneValue">12:30:00</div>
<div class="clock-subrow">
<span>Fixed civil time · UTC+1</span>
<span class="delta aligned">±0 min</span>
</div>
</section>
<section class="clock-card sundial" data-focus="sundial">
<div class="clock-title">
<span>II · Voxel Sundial</span>
<span>Apparent solar</span>
</div>
<div class="clock-number" id="sundialValue">12:13</div>
<div class="clock-subrow">
<span id="sundialDetail">True solar dial</span>
<span class="delta behind" id="sundialDelta">−17 min</span>
</div>
</section>
<section class="clock-card water" data-focus="water">
<div class="clock-title">
<span>III · Water Clock</span>
<span>Equal-hour scale</span>
</div>
<div class="clock-number" id="waterValue">12:13</div>
<div class="clock-subrow">
<span id="waterDetail">Constant flow · reset 06:00</span>
<span class="delta behind" id="waterDelta">−17 min</span>
</div>
</section>
<div id="comparisonNote">
Click a card to inspect its mechanism. The physical scene clock is the only
reference device; both other readings are derived models.
</div>
</aside>
<section id="controls" class="glass" aria-label="Simulation controls">
<div class="control-group">
<span class="control-label">Date</span>
<input id="dateInput" type="date" value="2024-06-21">
</div>
<div class="control-group grow">
<span class="control-label">Time</span>
<input id="timeSlider" type="range" min="0" max="1439.9" step="0.1" value="750">
</div>
<div class="control-group">
<button id="playButton" class="tool-button primary" type="button">Play</button>
<select id="speedSelect" aria-label="Playback speed">
<option value="1">1 min/s</option>
<option value="5" selected>5 min/s</option>
<option value="30">30 min/s</option>
<option value="120">120 min/s</option>
</select>
</div>
<div class="divider"></div>
<label class="toggle">
<input id="labelsToggle" type="checkbox" checked>
Labels
</label>
<button id="resetView" class="tool-button" type="button">Reset view</button>
<button id="topView" class="tool-button" type="button">Top</button>
<button id="assumptionsButton" class="tool-button" type="button">Assumptions</button>
</section>
<div id="cameraHint">Drag to orbit · wheel to zoom · click a readout to focus</div>
<div id="scrim"></div>
<aside id="assumptionsPanel" aria-label="Operating assumptions">
<button id="closeAssumptions" type="button" aria-label="Close assumptions">×</button>
<div class="eyebrow">Model card</div>
<h2>Operating<br>Assumptions</h2>
<p>
The scene is a comparative model, not a claim that these devices could
historically achieve modern precision.
</p>
<div class="assumption-box">
<strong>Canonical reference</strong>
<span>
The central mechanical clock is the single scene reference. Its digital
readout uses 24-hour civil time for Florence at a fixed UTC+1 offset.
Daylight saving time and historical time-zone changes are ignored.
</span>
</div>
<h3>1 · Sundial</h3>
<ul>
<li>
Assumed site: Florence, 43.7696° N, 11.2558° E.
</li>
<li>
The dial reads apparent solar time: civil time plus longitude correction
plus the equation of time.
</li>
<li>
The equation of time uses the standard approximate harmonic formula.
The Sun's declination uses the standard approximate seasonal formula.
</li>
<li>
The illustrated face is treated as a declination-compensated equatorial
dial. Its moving shadow indicator therefore follows solar hour angle.
</li>
<li>
Atmospheric refraction, the Sun's finite angular size, terrain shading,
and polar alignment error are ignored.
</li>
</ul>
<div class="assumption-box">
<strong>Why the two readings differ</strong>
<span>
A sundial follows the Sun. The scene clock follows civil time. Longitude
creates a constant offset; the equation of time creates a smaller,
date-dependent correction.
</span>
</div>
<h3>2 · Water Clock</h3>
<ul>
<li>
A constant-head gravity flow is assumed. The reservoir is refilled and
the reading is reset every day at 06:00 civil time.
</li>
<li>
At reset, the water clock is calibrated to the sundial's apparent-solar
reading at that instant.
</li>
<li>
Thereafter it accumulates equal hours at a constant rate. It cannot
follow the changing equation of time, so a small drift develops.
</li>
<li>
Evaporation, viscosity, temperature, capillary action, leaks, backlash,
irregular draining, and the finite accuracy of a real scale are ignored.
</li>
</ul>
<h3>3 · Scene and Synchronization</h3>
<ul>
<li>
All three mechanisms derive from the same simulated scene clock. Their
displayed offsets are modeled operating differences, not independent
live clock sources.
</li>
<li>
The central face is a conventional 12-hour dial; the numerical scene
reference is displayed on a 24-hour scale for unambiguous comparison.
</li>
<li>
Real-time cast shadows are illustrative. The sundial's explicit moving
marker is the time-bearing component under the assumptions above.
</li>
</ul>
<div class="metric-grid">
<div class="metric">
<span>Longitude correction</span>
<b id="metricLongitude">—</b>
</div>
<div class="metric">
<span>Equation of time</span>
<b id="metricEquation">—</b>
</div>
<div class="metric">
<span>Water vs sundial</span>
<b id="metricWaterDrift">—</b>
</div>
<div class="metric">
<span>Solar altitude</span>
<b id="metricAltitude">—</b>
</div>
</div>
<h3>Interpretation</h3>
<p>
In an ideal modern installation, all readings could be reconciled through
calibration, correction tables, and periodic maintenance. Here, the
visible mismatch is intentional: it exposes what each mechanism actually
knows about time.
</p>
</aside>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const app = document.querySelector("#app");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb9d3dc);
scene.fog = new THREE.Fog(0xb9d3dc, 30, 58);
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.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
app.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
44,
window.innerWidth / window.innerHeight,
0.1,
120
);
const cameraState = {
radius: 27,
phi: 1.18,
theta: 0.36,
target: new THREE.Vector3(0, 1.5, 0)
};
const LOC = {
name: "Florence",
latitude: 43.7696,
longitude: 11.2558,
timezone: 1
};
const state = {
date: "2024-06-21",
minutes: 750,
playing: false,
speed: 5,
labels: true
};
const ui = {
dateInput: document.querySelector("#dateInput"),
timeSlider: document.querySelector("#timeSlider"),
playButton: document.querySelector("#playButton"),
speedSelect: document.querySelector("#speedSelect"),
labelsToggle: document.querySelector("#labelsToggle"),
dateLabel: document.querySelector("#dateLabel"),
sceneValue: document.querySelector("#sceneValue"),
sundialValue: document.querySelector("#sundialValue"),
waterValue: document.querySelector("#waterValue"),
sundialDelta: document.querySelector("#sundialDelta"),
waterDelta: document.querySelector("#waterDelta"),
sundialDetail: document.querySelector("#sundialDetail"),
waterDetail: document.querySelector("#waterDetail"),
assumptionPanel: document.querySelector("#assumptionsPanel"),
assumptionsButton: document.querySelector("#assumptionsButton"),
closeAssumptions: document.querySelector("#closeAssumptions"),
scrim: document.querySelector("#scrim"),
metricLongitude: document.querySelector("#metricLongitude"),
metricEquation: document.querySelector("#metricEquation"),
metricWaterDrift: document.querySelector("#metricWaterDrift"),
metricAltitude: document.querySelector("#metricAltitude")
};
const labels = [
{
element: document.querySelector("#labelClock"),
anchor: new THREE.Vector3(0, 6.55, -7.25)
},
{
element: document.querySelector("#labelSundial"),
anchor: new THREE.Vector3(-5, 3.55, 2.45)
},
{
element: document.querySelector("#labelWater"),
anchor: new THREE.Vector3(5, 3.65, 2.15)
}
];
const unitBox = new THREE.BoxGeometry(1, 1, 1);
function standard(color, roughness = 0.88, metalness = 0) {
return new THREE.MeshStandardMaterial({
color,
roughness,
metalness
});
}
const materials = {
ground: standard(0x695f52),
floor: [
standard(0xaaa28e),
standard(0x9c9585),
standard(0xb1a993),
standard(0x928d7d),
standard(0xa59d89)
],
wall: [
standard(0x9d927d),
standard(0xaaa087),
standard(0x918771),
standard(0xb2a68e),
standard(0x9a8e77)
],
trim: standard(0x776e5d),
lightStone: standard(0xc0b398),
darkStone: standard(0x625b4d),
wood: standard(0x684b34),
roof: standard(0x9a5039),
roofDark: standard(0x793b2e),
copper: standard(0x9e6a36, 0.58, 0.38),
brass: standard(0xc9a35d, 0.42, 0.58),
dial: standard(0xc9b98f),
marker: standard(0x433b31),
foliage: [
standard(0x536b43),
standard(0x62784b),
standard(0x455d3d),
standard(0x718252)
],
terracotta: standard(0xa65d43),
water: new THREE.MeshStandardMaterial({
color: 0x4ba8bc,
roughness: 0.24,
metalness: 0.02,
transparent: true,
opacity: 0.78,
depthWrite: false
}),
waterBright: new THREE.MeshBasicMaterial({
color: 0x9de9f1
}),
window: new THREE.MeshStandardMaterial({
color: 0x2d241d,
emissive: 0xe69a44,
emissiveIntensity: 0.05,
roughness: 0.9
}),
clockFace: standard(0xe0d0a5),
black: standard(0x242321),
red: standard(0x8e3027)
};
function addBox(
parent,
width,
height,
depth,
x,
y,
z,
material,
castShadow = true,
receiveShadow = true
) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(width, height, depth);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
parent.add(mesh);
return mesh;
}
function deterministicIndex(value, length) {
const x = Math.sin(value * 12.9898) * 43758.5453;
return Math.abs(Math.floor(x)) % length;
}
// Lighting.
const hemisphere = new THREE.HemisphereLight(0xffefd2, 0x52606a, 1.25);
scene.add(hemisphere);
const ambient = new THREE.AmbientLight(0xfff0da, 0.35);
scene.add(ambient);
const sunLight = new THREE.DirectionalLight(0xfff1cf, 3.0);
sunLight.castShadow = true;
sunLight.shadow.mapSize.set(2048, 2048);
sunLight.shadow.camera.left = -18;
sunLight.shadow.camera.right = 18;
sunLight.shadow.camera.top = 18;
sunLight.shadow.camera.bottom = -18;
sunLight.shadow.camera.near = 1;
sunLight.shadow.camera.far = 75;
sunLight.shadow.bias = -0.00045;
sunLight.shadow.normalBias = 0.025;
sunLight.target.position.set(0, 0, 0);
scene.add(sunLight);
scene.add(sunLight.target);
const sunMarker = new THREE.Mesh(
new THREE.BoxGeometry(0.85, 0.85, 0.85),
new THREE.MeshBasicMaterial({ color: 0xffd875 })
);
scene.add(sunMarker);
const courtyardLantern = new THREE.PointLight(0xffae59, 0, 15, 2);
courtyardLantern.position.set(0, 4.2, -6.4);
scene.add(courtyardLantern);
// Foundation and tiled courtyard floor.
addBox(scene, 23, 0.36, 18, 0, -0.28, 0, materials.ground, false, true);
for (let gx = -10; gx <= 10; gx++) {
for (let gz = -8; gz <= 8; gz++) {
const material = materials.floor[
deterministicIndex(gx * 23 + gz * 41 + 100, materials.floor.length)
];
addBox(
scene,
0.94,
0.14,
0.94,
gx,
-0.07,
gz,
material,
false,
true
);
}
}
// Decorative paving bands.
for (let z = -6; z <= 6; z++) {
addBox(scene, 19.5, 0.025, 0.12, 0, 0.015, z, materials.lightStone, false, true);
}
// Rear masonry wall, leaving a tall central arch.
for (let row = 0; row < 11; row++) {
const y = 0.25 + row * 0.5;
for (let col = 0; col < 20; col++) {
const x = -9.75 + col * 1.03;
if (x > -2.65 && x < 2.65) continue;
const material = materials.wall[
deterministicIndex(row * 37 + col * 13 + 300, materials.wall.length)
];
addBox(scene, 0.96, 0.46, 0.72, x, y, -8.28, material);
}
}
// Wall above the arch.
for (let row = 11; row < 13; row++) {
const y = 0.25 + row * 0.5;
for (let col = 0; col < 20; col++) {
const x = -9.75 + col * 1.03;
const material = materials.wall[
deterministicIndex(row * 37 + col * 13 + 300, materials.wall.length)
];
addBox(scene, 0.96, 0.46, 0.72, x, y, -8.28, material);
}
}
// Stepped voxel arch ring.
for (let i = 0; i <= 12; i++) {
const angle = (i / 12) * Math.PI;
const x = Math.cos(angle) * 2.72;
const y = 3.02 + Math.sin(angle) * 2.72;
const block = addBox(
scene,
0.69,
0.57,
0.88,
x,
y,
-7.91,
materials.lightStone
);
block.rotation.z = angle - Math.PI / 2;
}
// Terracotta roof blocks.
for (let i = 0; i < 22; i++) {
const x = -10.5 + i;
addBox(
scene,
0.96,
0.25,
1.35,
x,
6.57,
-8.08,
i % 2 ? materials.roof : materials.roofDark
);
}
// Side walls.
for (const side of [-1, 1]) {
for (let z = -7.5; z <= 7; z++) {
const material = materials.wall[
deterministicIndex(z * 19 + side * 43 + 700, materials.wall.length)
];
addBox(scene, 0.7, 2.55, 0.94, side * 10.75, 1.275, z, material);
}
addBox(scene, 0.88, 0.22, 16, side * 10.75, 2.67, -0.25, materials.trim);
addBox(scene, 0.95, 0.35, 16, side * 10.75, 0.16, -0.25, materials.darkStone);
}
// Front corner piers.
for (const x of [-9.5, 9.5]) {
addBox(scene, 1.3, 0.4, 1.3, x, 0.2, 7.2, materials.trim);
addBox(scene, 0.95, 3.1, 0.95, x, 1.9, 7.2, materials.lightStone);
addBox(scene, 1.25, 0.35, 1.25, x, 3.58, 7.2, materials.trim);
addBox(scene, 1.1, 0.25, 1.1, x, 3.88, 7.2, materials.roof);
}
// Steps leading to the clock arch.
for (let i = 0; i < 3; i++) {
addBox(
scene,
5.1 - i * 0.35,
0.22,
1.0 + i * 0.25,
0,
0.11 + i * 0.22,
-6.55 + i * 0.45,
i === 2 ? materials.lightStone : materials.trim
);
}
// The single scene clock.
const clockGroup = new THREE.Group();
clockGroup.position.set(0, 0, -7.64);
scene.add(clockGroup);
addBox(clockGroup, 4.15, 4.9, 0.7, 0, 3.38, 0, materials.wood);
addBox(clockGroup, 4.55, 0.3, 0.85, 0, 5.93, 0, materials.trim);
addBox(clockGroup, 4.55, 0.32, 0.85, 0, 0.88, 0, materials.trim);
addBox(clockGroup, 3.7, 4.42, 0.16, 0, 3.42, 0.43, materials.darkStone);
addBox(clockGroup, 3.42, 4.12, 0.14, 0, 3.42, 0.55, materials.clockFace);
for (let i = 0; i < 12; i++) {
const angle = (i / 12) * Math.PI * 2;
const marker = addBox(
clockGroup,
0.17,
i % 3 === 0 ? 0.38 : 0.25,
0.11,
Math.sin(angle) * 1.38,
3.42 + Math.cos(angle) * 1.38,
0.66,
i % 3 === 0 ? materials.black : materials.copper
);
marker.rotation.z = -angle;
}
const hourPivot = new THREE.Group();
const minutePivot = new THREE.Group();
const secondPivot = new THREE.Group();
hourPivot.position.set(0, 3.42, 0.76);
minutePivot.position.set(0, 3.42, 0.79);
secondPivot.position.set(0, 3.42, 0.83);
addBox(hourPivot, 0.18, 1.0, 0.1, 0, 0.48, 0, materials.black);
addBox(minutePivot, 0.11, 1.38, 0.1, 0, 0.66, 0, materials.black);
addBox(secondPivot, 0.055, 1.52, 0.08, 0, 0.73, 0, materials.red);
clockGroup.add(hourPivot, minutePivot, secondPivot);
addBox(clockGroup, 0.3, 0.3, 0.16, 0, 3.42, 0.91, materials.brass);
// Warm window recess behind the clock.
addBox(scene, 2.8, 4.1, 0.18, 0, 3.2, -8.56, materials.window, false, false);
// Voxel sundial.
const sundial = new THREE.Group();
sundial.position.set(-5, 0, 2.45);
scene.add(sundial);
addBox(sundial, 3.0, 0.38, 3.0, 0, 0.19, 0, materials.trim);
addBox(sundial, 2.0, 0.38, 2.0, 0, 0.52, 0, materials.lightStone);
addBox(sundial, 1.35, 1.45, 1.35, 0, 1.35, 0, materials.wall[1]);
addBox(sundial, 1.75, 0.28, 1.75, 0, 2.16, 0, materials.trim);
const sundialDial = new THREE.Group();
sundialDial.position.set(0, 2.52, 0);
sundialDial.rotation.x = THREE.MathUtils.degToRad(LOC.latitude - 90);
sundial.add(sundialDial);
addBox(sundialDial, 3.45, 0.17, 3.45, 0, 0, 0, materials.lightStone);
addBox(sundialDial, 3.12, 0.08, 3.12, 0, 0.125, 0, materials.dial);
for (let hour = 6; hour <= 18; hour++) {
const hourAngle = THREE.MathUtils.degToRad((hour - 12) * 15);
const radius = 1.33;
const marker = addBox(
sundialDial,
hour % 3 === 0 ? 0.16 : 0.1,
0.075,
hour % 3 === 0 ? 0.4 : 0.27,
Math.sin(hourAngle) * radius,
0.205,
-Math.cos(hourAngle) * radius,
hour % 3 === 0 ? materials.black : materials.copper
);
marker.rotation.y = Math.PI - hourAngle;
}
// Polar-aligned voxel style.
for (let i = 0; i < 5; i++) {
addBox(
sundialDial,
0.23 - i * 0.018,
0.25,
0.23 - i * 0.018,
0,
0.31 + i * 0.22,
0,
materials.copper
);
}
const sundialShadowPivot = new THREE.Group();
sundialShadowPivot.position.set(0, 0.245, 0);
addBox(
sundialShadowPivot,
0.12,
0.055,
1.43,
0,
0,
0.715,
materials.black,
false,
false
);
sundialDial.add(sundialShadowPivot);
// Voxel clepsydra / water clock.
const waterClock = new THREE.Group();
waterClock.position.set(5, 0, 2.15);
scene.add(waterClock);
addBox(waterClock, 3.25, 0.38, 3.05, 0, 0.19, 0, materials.trim);
addBox(waterClock, 2.9, 0.25, 2.7, 0, 0.49, 0, materials.lightStone);
// Lower receiving basin.
addBox(waterClock, 1.9, 0.18, 1.8, -0.25, 0.68, 0.45, materials.darkStone);
addBox(waterClock, 0.18, 1.15, 1.8, -1.14, 1.22, 0.45, materials.lightStone);
addBox(waterClock, 0.18, 1.15, 1.8, 0.64, 1.22, 0.45, materials.lightStone);
addBox(waterClock, 1.95, 0.18, 0.18, -0.25, 1.78, -0.36, materials.lightStone);
addBox(waterClock, 1.95, 0.18, 0.18, -0.25, 1.78, 1.26, materials.lightStone);
const waterBlocks = [];
for (let i = 0; i < 15; i++) {
const block = addBox(
waterClock,
1.62,
0.16,
1.55,
-0.25,
0.83 + i * 0.16,
0.45,
materials.water,
false,
false
);
waterBlocks.push(block);
}
// Rear reservoir and constant-head outlet.
addBox(waterClock, 1.7, 1.75, 1.25, -0.3, 2.25, -0.78, materials.copper);
addBox(waterClock, 1.9, 0.2, 1.42, -0.3, 3.18, -0.78, materials.brass);
addBox(waterClock, 1.5, 0.12, 1.04, -0.3, 2.7, -0.78, materials.water, false, false);
addBox(waterClock, 0.25, 0.25, 1.35, -0.3, 2.03, 0.05, materials.brass);
addBox(waterClock, 0.18, 0.18, 0.8, -0.3, 1.83, 0.58, materials.brass);
// Vertical level gauge.
addBox(waterClock, 0.18, 2.7, 0.18, 1.18, 2.02, 0.55, materials.brass);
for (let i = 0; i < 10; i++) {
addBox(
waterClock,
i % 2 === 0 ? 0.42 : 0.29,
0.06,
0.1,
1.23,
0.82 + i * 0.27,
0.68,
i % 2 === 0 ? materials.copper : materials.black,
false,
false
);
}
const floatMarker = addBox(
waterClock,
0.62,
0.17,
0.42,
1.18,
2,
0.64,
materials.red
);
// Falling water droplets.
const droplets = [];
for (let i = 0; i < 5; i++) {
droplets.push(
addBox(
waterClock,
0.09,
0.17,
0.09,
-0.3,
1.6,
0.58,
materials.waterBright,
false,
false
)
);
}
// Planters and foliage.
function addPlanter(x, z, variant = 0) {
const planter = new THREE.Group();
planter.position.set(x, 0, z);
scene.add(planter);
addBox(planter, 1.35, 0.22, 1.35, 0, 0.11, 0, materials.darkStone);
addBox(planter, 1.05, 0.65, 1.05, 0, 0.49, 0, materials.terracotta);
addBox(planter, 1.2, 0.18, 1.2, 0, 0.85, 0, materials.terracotta);
addBox(planter, 0.25, 1.35, 0.25, 0, 1.55, 0, materials.wood);
const offsets = [
[-0.38, 2.05, -0.2],
[0.36, 2.12, 0.08],
[0, 2.48, -0.18],
[-0.2, 2.72, 0.08],
[0.32, 2.62, -0.28],
[-0.46, 2.48, 0.27]
];
offsets.forEach((offset, index) => {
addBox(
planter,
0.76,
0.7,
0.72,
offset[0],
offset[1],
offset[2],
materials.foliage[(index + variant) % materials.foliage.length]
);
});
}
addPlanter(-8.35, -3.7, 0);
addPlanter(8.35, -3.7, 2);
addPlanter(8.5, 5.4, 1);
// Benches.
function addBench(x, z, rotation = 0) {
const bench = new THREE.Group();
bench.position.set(x, 0, z);
bench.rotation.y = rotation;
scene.add(bench);
addBox(bench, 2.6, 0.18, 0.72, 0, 0.78, 0, materials.wood);
addBox(bench, 2.6, 0.5, 0.16, 0, 1.15, -0.3, materials.wood);
addBox(bench, 0.22, 0.75, 0.58, -1.03, 0.38, 0, materials.darkStone);
addBox(bench, 0.22, 0.75, 0.58, 1.03, 0.38, 0, materials.darkStone);
}
addBench(-7.1, 4.65, 0.08);
addBench(7.0, 5.2, -0.12);
// Small crates and architectural details.
addBox(scene, 0.8, 0.8, 0.8, -9.1, 0.4, 5.8, materials.wood);
addBox(scene, 0.68, 0.68, 0.68, -8.55, 1.14, 5.55, materials.wood);
addBox(scene, 0.7, 0.7, 0.7, 9.2, 0.35, 1.2, materials.terracotta);
// ------------------------------------------------------------------
// Time and solar calculations.
// ------------------------------------------------------------------
const DEG = Math.PI / 180;
const longitudeCorrection = 4 * (LOC.longitude - 15 * LOC.timezone);
function mod(value, divisor) {
return ((value % divisor) + divisor) % divisor;
}
function parseDateKey(key) {
const [year, month, day] = key.split("-").map(Number);
return new Date(Date.UTC(year, month - 1, day, 12));
}
function dateKeyFromUTC(date) {
return date.toISOString().slice(0, 10);
}
function addDaysToKey(key, amount) {
const date = parseDateKey(key);
date.setUTCDate(date.getUTCDate() + amount);
return dateKeyFromUTC(date);
}
function dayOfYear(date) {
const year = date.getUTCFullYear();
const start = Date.UTC(year, 0, 0);
const current = Date.UTC(
date.getUTCFullYear(),
date.getUTCMonth(),
date.getUTCDate()
);
return Math.floor((current - start) / 86400000);
}
function equationOfTime(key, localMinutes) {
const date = parseDateKey(key);
const doy = dayOfYear(date);
const utcHour = localMinutes / 60 - LOC.timezone;
const gamma =
(2 * Math.PI / 365) * (doy - 1 + (utcHour - 12) / 24);
return (
229.18 *
(0.000075 +
0.001868 * Math.cos(gamma) -
0.032077 * Math.sin(gamma) -
0.014615 * Math.cos(2 * gamma) -
0.040849 * Math.sin(2 * gamma))
);
}
function solarDeclination(key, localMinutes) {
const date = parseDateKey(key);
const doy = dayOfYear(date);
const utcHour = localMinutes / 60 - LOC.timezone;
const gamma =
(2 * Math.PI / 365) * (doy - 1 + (utcHour - 12) / 24);
return (
0.006918 -
0.399912 * Math.cos(gamma) +
0.070257 * Math.sin(gamma) -
0.006758 * Math.cos(2 * gamma) +
0.000907 * Math.sin(2 * gamma) -
0.002697 * Math.cos(3 * gamma) +
0.00148 * Math.sin(3 * gamma)
);
}
function apparentSolarTime(key, localMinutes) {
return mod(
localMinutes + longitudeCorrection + equationOfTime(key, localMinutes),
1440
);
}
function waterClockReading(key, localMinutes) {
const crossedMidnight = localMinutes < 360;
const startKey = crossedMidnight ? addDaysToKey(key, -1) : key;
const elapsed = mod(localMinutes - 360, 1440);
const calibratedStart = mod(
360 + longitudeCorrection + equationOfTime(startKey, 360),
1440
);
return {
reading: mod(calibratedStart + elapsed, 1440),
elapsed,
level: 1 - elapsed / 1440,
startKey
};
}
function signedDifference(a, b) {
return mod(a - b + 720, 1440) - 720;
}
function formatClock(minutes, includeSeconds = false) {
const normalized = mod(minutes, 1440);
const hour = Math.floor(normalized / 60);
const minute = Math.floor(normalized % 60);
const second = Math.floor((normalized % 1) * 60);
const base = `${String(hour).padStart(2, "0")}:${String(minute).padStart(2, "0")}`;
return includeSeconds
? `${base}:${String(second).padStart(2, "0")}`
: base;
}
function formatDelta(value) {
const rounded = Math.round(value);
if (Math.abs(rounded) < 1) return "±0 min";
return `${rounded > 0 ? "+" : "−"}${Math.abs(rounded)} min`;
}
function formatSignedMetric(value, suffix = "min") {
const rounded = Math.round(value);
if (Math.abs(rounded) < 1) return `±0 ${suffix}`;
return `${rounded > 0 ? "+" : "−"}${Math.abs(rounded)} ${suffix}`;
}
function formatDateLabel(key) {
const date = parseDateKey(key);
return new Intl.DateTimeFormat("en-GB", {
day: "2-digit",
month: "short",
year: "numeric",
timeZone: "UTC"
}).format(date);
}
function setDeltaClass(element, value) {
element.classList.remove("ahead", "behind", "aligned");
if (value > 0.5) element.classList.add("ahead");
else if (value < -0.5) element.classList.add("behind");
else element.classList.add("aligned");
}
// ------------------------------------------------------------------
// Camera controls implemented directly without Three.js addons.
// ------------------------------------------------------------------
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("grabbing");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousPointerX;
const dy = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraState.theta -= dx * 0.005;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.0045,
0.12,
1.48
);
});
function endDrag(event) {
dragging = false;
renderer.domElement.classList.remove("grabbing");
if (event?.pointerId !== undefined) {
try {
renderer.domElement.releasePointerCapture(event.pointerId);
} catch {
// Pointer capture may already have been released.
}
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
8,
45
);
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
function resetCamera() {
cameraState.radius = 27;
cameraState.phi = 1.18;
cameraState.theta = 0.36;
cameraState.target.set(0, 1.5, 0);
}
document.querySelector("#resetView").addEventListener("click", resetCamera);
document.querySelector("#topView").addEventListener("click", () => {
cameraState.radius = 30;
cameraState.phi = 0.08;
cameraState.theta = 0;
cameraState.target.set(0, 0, 0);
});
document.querySelectorAll("[data-focus]").forEach((card) => {
card.addEventListener("click", () => {
const focus = card.dataset.focus;
if (focus === "clock") {
cameraState.target.set(0, 3.25, -5.6);
cameraState.radius = 17;
cameraState.phi = 1.24;
cameraState.theta = 0.12;
} else if (focus === "sundial") {
cameraState.target.set(-5, 1.9, 2.45);
cameraState.radius = 10;
cameraState.phi = 1.05;
cameraState.theta = 0.2;
} else {
cameraState.target.set(5, 1.8, 2.15);
cameraState.radius = 10;
cameraState.phi = 1.04;
cameraState.theta = 0.18;
}
});
});
// ------------------------------------------------------------------
// UI behavior.
// ------------------------------------------------------------------
function updatePlayButton() {
ui.playButton.textContent = state.playing ? "Pause" : "Play";
}
ui.playButton.addEventListener("click", () => {
state.playing = !state.playing;
updatePlayButton();
});
ui.speedSelect.addEventListener("change", () => {
state.speed = Number(ui.speedSelect.value);
});
ui.timeSlider.addEventListener("input", () => {
state.minutes = Number(ui.timeSlider.value);
});
ui.dateInput.addEventListener("change", () => {
if (!ui.dateInput.value) return;
state.date = ui.dateInput.value;
state.minutes = 12 * 60 + 30;
});
ui.labelsToggle.addEventListener("change", () => {
state.labels = ui.labelsToggle.checked;
labels.forEach(({ element }) => {
element.style.display = state.labels ? "block" : "none";
});
});
function openAssumptions() {
ui.assumptionPanel.classList.add("open");
ui.scrim.classList.add("open");
}
function closeAssumptions() {
ui.assumptionPanel.classList.remove("open");
ui.scrim.classList.remove("open");
}
ui.assumptionsButton.addEventListener("click", openAssumptions);
ui.closeAssumptions.addEventListener("click", closeAssumptions);
ui.scrim.addEventListener("click", closeAssumptions);
window.addEventListener("keydown", (event) => {
if (event.key === "Escape") closeAssumptions();
if (
event.code === "Space" &&
!["INPUT", "SELECT", "TEXTAREA", "BUTTON"].includes(
document.activeElement?.tagName
)
) {
event.preventDefault();
state.playing = !state.playing;
updatePlayButton();
}
});
// ------------------------------------------------------------------
// Dynamic model updates.
// ------------------------------------------------------------------
const sunDirection = new THREE.Vector3();
const nightColor = new THREE.Color(0x172333);
const dayColor = new THREE.Color(0xb9d3dc);
const duskColor = new THREE.Color(0xd29c79);
const warmSun = new THREE.Color(0xffb36f);
const whiteSun = new THREE.Color(0xfff2d5);
const temporaryColor = new THREE.Color();
const projected = new THREE.Vector3();
let flowPhase = 0;
function updateSunAndLighting(solarMinutes, dateKey) {
const declination = solarDeclination(dateKey, state.minutes);
const hourAngle = ((solarMinutes - 720) / 4) * DEG;
const latitude = LOC.latitude * DEG;
const east =
-Math.cos(declination) * Math.sin(hourAngle);
const north =
Math.cos(latitude) * Math.sin(declination) -
Math.sin(latitude) *
Math.cos(declination) *
Math.cos(hourAngle);
const up =
Math.sin(latitude) * Math.sin(declination) +
Math.cos(latitude) *
Math.cos(declination) *
Math.cos(hourAngle);
sunDirection.set(east, up, -north).normalize();
sunLight.position.copy(sunDirection).multiplyScalar(32);
sunMarker.position
.copy(sunDirection)
.multiplyScalar(22)
.add(new THREE.Vector3(0, 2, 0));
const altitude = Math.asin(THREE.MathUtils.clamp(up, -1, 1));
const daylight = THREE.MathUtils.clamp((altitude + 0.08) / 0.3, 0, 1);
sunLight.intensity = 0.05 + daylight * 3.1;
sunLight.color.lerpColors(warmSun, whiteSun, daylight);
hemisphere.intensity = 0.48 + daylight * 0.95;
ambient.intensity = 0.18 + daylight * 0.22;
courtyardLantern.intensity = (1 - daylight) * 7.5;
materials.window.emissiveIntensity = 0.03 + (1 - daylight) * 1.7;
if (altitude > 0.08) {
temporaryColor.copy(dayColor);
} else if (altitude > -0.12) {
temporaryColor.copy(duskColor);
} else {
temporaryColor.copy(nightColor);
}
scene.background.copy(temporaryColor);
scene.fog.color.copy(temporaryColor);
sunMarker.visible = altitude > -0.12;
return altitude;
}
function updateClockHands() {
const normalized = mod(state.minutes, 720);
const minuteAngle = mod(state.minutes, 60) / 60 * Math.PI * 2;
const secondAngle = mod(state.minutes * 60, 60) / 60 * Math.PI * 2;
hourPivot.rotation.z = -(normalized / 720) * Math.PI * 2;
minutePivot.rotation.z = -minuteAngle;
secondPivot.rotation.z = -secondAngle;
}
function updateSundial(solarMinutes) {
const hourAngle = ((solarMinutes - 720) / 4) * DEG;
sundialShadowPivot.rotation.y = Math.PI - hourAngle;
}
function updateWaterClock(waterState) {
const visibleCount = Math.ceil(waterState.level * waterBlocks.length);
waterBlocks.forEach((block, index) => {
block.visible = index < visibleCount;
});
floatMarker.position.y = 0.83 + waterState.level * 2.55;
}
function updateReadouts(solarMinutes, waterState, altitude) {
const waterReading = waterState.reading;
const sundialDifference = signedDifference(solarMinutes, state.minutes);
const waterDifference = signedDifference(waterReading, state.minutes);
const waterDrift = signedDifference(waterReading, solarMinutes);
const equation = equationOfTime(state.date, state.minutes);
ui.dateLabel.textContent = formatDateLabel(state.date);
ui.sceneValue.textContent = formatClock(state.minutes, true);
ui.sundialValue.textContent = formatClock(solarMinutes);
ui.waterValue.textContent = formatClock(waterReading);
ui.sundialDelta.textContent = formatDelta(sundialDifference);
ui.waterDelta.textContent = formatDelta(waterDifference);
setDeltaClass(ui.sundialDelta, sundialDifference);
setDeltaClass(ui.waterDelta, waterDifference);
ui.sundialDetail.textContent =
`${formatSignedMetric(equation, "min EoT")} · apparent solar`;
ui.waterDetail.textContent =
`${Math.round(waterState.level * 100)}% water · reset 06:00`;
ui.metricLongitude.textContent = formatSignedMetric(
longitudeCorrection,
"min"
);
ui.metricEquation.textContent = formatSignedMetric(equation, "min");
ui.metricWaterDrift.textContent = formatSignedMetric(
waterDrift,
"min"
);
ui.metricAltitude.textContent = `${(altitude / DEG).toFixed(1)}°`;
ui.timeSlider.value = state.minutes;
}
function updateProjectedLabels() {
if (!state.labels) return;
labels.forEach(({ element, anchor }) => {
projected.copy(anchor).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.opacity = visible ? "1" : "0";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
});
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
// ------------------------------------------------------------------
// Animation loop.
// ------------------------------------------------------------------
let previousTime = performance.now();
function animate(now) {
const deltaSeconds = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (state.playing) {
state.minutes += deltaSeconds * state.speed;
while (state.minutes >= 1440) {
state.minutes -= 1440;
state.date = addDaysToKey(state.date, 1);
ui.dateInput.value = state.date;
}
flowPhase +=
deltaSeconds * (0.75 + Math.min(state.speed / 120, 1) * 0.45);
}
const solarMinutes = apparentSolarTime(state.date, state.minutes);
const waterState = waterClockReading(state.date, state.minutes);
const altitude = updateSunAndLighting(solarMinutes, state.date);
updateClockHands();
updateSundial(solarMinutes);
updateWaterClock(waterState);
updateReadouts(solarMinutes, waterState, altitude);
droplets.forEach((drop, index) => {
const phase = (flowPhase + index / droplets.length) % 1;
drop.position.y = 1.72 - phase * 0.96;
drop.position.x = -0.3 + Math.sin(index * 2.1) * 0.025;
drop.visible = waterState.level > 0.025;
});
updateCamera();
camera.updateMatrixWorld();
updateProjectedLabels();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
ui.dateInput.value = state.date;
updatePlayButton();
resize();
updateCamera();
requestAnimationFrame(animate);
</script>
</body>
</html>
```