Prompt specification
Build or customize the Three.js 3D scene "Coastal Town Day-Night Teaching Diorama" 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: Coastal Town Day-Night Teaching Diorama
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/coastal-town-day-night-teaching-diorama-x3807
Scene Description Prompt:
Make a voxel Three.js scene in a self-contained HTML file using cdnjs: coastal town day-night teaching diorama. Build an original small coastal town linked to a globe inset. Advance time and keep local illumination consistent with the town's marked position in the schematic model.
Working Three.js Starter (coastal-town-day-night-teaching-diorama-x3807):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mirewick Bay — Coastal Day–Night Diorama</title>
<style>
:root {
--bg: #06111d;
--panel: #0c1d2c;
--panel-2: #10283a;
--line: rgba(176, 215, 231, 0.16);
--text: #eef8fb;
--muted: #94afbd;
--gold: #ffd36a;
--cyan: #78d9e8;
}
* { box-sizing: border-box; }
html, body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body { overflow: hidden; }
button, input { font: inherit; }
button {
color: var(--text);
border: 1px solid var(--line);
background: rgba(255, 255, 255, 0.055);
border-radius: 10px;
cursor: pointer;
transition: background 150ms ease, border-color 150ms ease, transform 150ms ease;
}
button:hover {
background: rgba(255, 255, 255, 0.1);
border-color: rgba(176, 215, 231, 0.35);
}
button:active { transform: translateY(1px); }
#app {
height: 100dvh;
display: grid;
grid-template-rows: auto minmax(0, 1fr) auto;
background:
radial-gradient(circle at 30% 0%, rgba(43, 101, 126, 0.18), transparent 36%),
var(--bg);
}
header {
min-height: 76px;
padding: 15px 22px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
border-bottom: 1px solid var(--line);
background: rgba(5, 16, 27, 0.82);
backdrop-filter: blur(15px);
z-index: 3;
}
.title-wrap {
display: flex;
align-items: center;
gap: 13px;
}
.title-icon {
width: 38px;
height: 38px;
position: relative;
border: 1px solid rgba(255, 211, 106, 0.55);
background: rgba(255, 211, 106, 0.08);
border-radius: 11px;
overflow: hidden;
}
.title-icon::before {
content: "";
position: absolute;
left: 7px;
right: 7px;
top: 16px;
height: 8px;
background: var(--gold);
clip-path: polygon(0 0, 100% 0, 78% 100%, 22% 100%);
}
.title-icon::after {
content: "";
position: absolute;
left: 16px;
top: 7px;
width: 5px;
height: 10px;
background: #eff8f8;
border: 2px solid var(--gold);
}
h1 {
font-size: 17px;
letter-spacing: 0.13em;
margin: 0 0 4px;
text-transform: uppercase;
}
.subtitle {
color: var(--muted);
font-size: 11px;
letter-spacing: 0.07em;
}
.header-status {
display: flex;
align-items: center;
gap: 9px;
color: var(--muted);
font-size: 12px;
text-align: right;
}
.status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--gold);
box-shadow: 0 0 14px var(--gold);
}
.workspace {
min-height: 0;
display: grid;
grid-template-columns: minmax(0, 1fr) 350px;
gap: 12px;
padding: 12px;
}
#stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
border-radius: 17px;
border: 1px solid var(--line);
background: #0b2031;
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.28);
}
#stage canvas, #globeViewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.stage-top {
position: absolute;
top: 16px;
left: 16px;
right: 16px;
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 15px;
pointer-events: none;
z-index: 2;
}
.glass-pill {
padding: 8px 11px;
border: 1px solid rgba(255, 255, 255, 0.13);
background: rgba(5, 17, 28, 0.55);
backdrop-filter: blur(9px);
border-radius: 9px;
color: #d9e9ef;
font-size: 11px;
letter-spacing: 0.05em;
}
.stage-help {
position: absolute;
left: 16px;
bottom: 15px;
color: rgba(232, 245, 250, 0.65);
font-size: 10px;
letter-spacing: 0.06em;
pointer-events: none;
text-shadow: 0 1px 4px #00101a;
}
#townLabel, #globePinLabel {
position: absolute;
pointer-events: none;
white-space: nowrap;
transform: translate(-50%, -100%);
z-index: 2;
}
#townLabel {
padding: 7px 10px;
color: #fff6d8;
background: rgba(34, 26, 10, 0.8);
border: 1px solid rgba(255, 211, 106, 0.7);
border-radius: 8px;
font-size: 10px;
letter-spacing: 0.09em;
box-shadow: 0 5px 18px rgba(0, 0, 0, 0.22);
}
#townLabel::after {
content: "";
position: absolute;
left: 50%;
bottom: -6px;
width: 10px;
height: 10px;
background: rgba(34, 26, 10, 0.8);
border-right: 1px solid rgba(255, 211, 106, 0.7);
border-bottom: 1px solid rgba(255, 211, 106, 0.7);
transform: translateX(-50%) rotate(45deg);
}
aside {
min-height: 0;
display: grid;
grid-template-rows: auto minmax(245px, 1fr) auto;
gap: 10px;
}
.card {
border-radius: 16px;
border: 1px solid var(--line);
background:
linear-gradient(145deg, rgba(255, 255, 255, 0.035), transparent 55%),
var(--panel);
overflow: hidden;
}
.globe-card {
min-height: 0;
display: grid;
grid-template-rows: auto minmax(0, 1fr);
position: relative;
}
.card-head {
padding: 13px 14px 10px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 10px;
border-bottom: 1px solid var(--line);
}
.card-title {
font-size: 11px;
font-weight: 700;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.card-note {
margin-top: 4px;
color: var(--muted);
font-size: 10px;
line-height: 1.4;
}
.tiny-button {
padding: 6px 8px;
font-size: 9px;
letter-spacing: 0.07em;
white-space: nowrap;
}
#globeViewport {
position: relative;
min-height: 245px;
overflow: hidden;
background:
radial-gradient(circle at 52% 42%, rgba(56, 118, 147, 0.23), transparent 49%),
radial-gradient(circle at 50% 50%, #112b3c 0, #071725 72%);
}
#globePinLabel {
color: #fff3c4;
padding: 5px 7px;
border-radius: 6px;
background: rgba(43, 31, 9, 0.85);
border: 1px solid rgba(255, 211, 106, 0.7);
font-size: 9px;
letter-spacing: 0.07em;
}
.globe-foot {
position: absolute;
bottom: 10px;
left: 12px;
right: 12px;
display: flex;
justify-content: space-between;
gap: 8px;
color: rgba(222, 239, 246, 0.62);
font-size: 9px;
letter-spacing: 0.04em;
pointer-events: none;
}
.sun-key {
display: flex;
align-items: center;
gap: 5px;
}
.sun-key i {
width: 7px;
height: 7px;
border-radius: 50%;
background: #ffd25b;
box-shadow: 0 0 8px #ffd25b;
}
.readout-card {
padding: 12px 14px;
}
.readout-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.readout {
min-width: 0;
padding: 9px 10px;
border-radius: 10px;
background: rgba(255, 255, 255, 0.035);
border: 1px solid rgba(255, 255, 255, 0.055);
}
.readout span {
display: block;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
margin-bottom: 4px;
}
.readout strong {
display: block;
font-size: 13px;
font-weight: 650;
color: #f2fbfd;
}
.teaching-note {
margin-top: 9px;
padding-top: 9px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
line-height: 1.55;
}
.teaching-note b { color: var(--gold); font-weight: 600; }
footer {
border-top: 1px solid var(--line);
background: rgba(6, 17, 29, 0.94);
padding: 13px 20px 15px;
z-index: 3;
}
.timeline {
display: grid;
grid-template-columns: auto minmax(220px, 1fr) auto auto;
align-items: center;
gap: 16px;
max-width: 1500px;
margin: auto;
}
.play-button {
min-width: 92px;
height: 43px;
padding: 0 15px;
color: #17202a;
background: var(--gold);
border-color: var(--gold);
font-weight: 750;
font-size: 12px;
}
.play-button:hover { background: #ffe09a; }
.range-wrap {
display: grid;
grid-template-columns: 94px minmax(120px, 1fr) 94px;
align-items: center;
gap: 10px;
}
.time-value {
color: #f4fbfd;
font-size: 18px;
font-variant-numeric: tabular-nums;
font-weight: 700;
letter-spacing: 0.03em;
}
.time-value small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 8px;
font-weight: 600;
letter-spacing: 0.1em;
}
.range-end {
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
text-align: right;
}
input[type="range"] {
width: 100%;
height: 7px;
margin: 0;
appearance: none;
border-radius: 999px;
outline: none;
background:
linear-gradient(90deg,
#101e3a 0%,
#101e3a 16%,
#bb6e68 21%,
#d9b875 27%,
#78c6dc 38%,
#78c6dc 72%,
#d9b875 78%,
#bb6e68 84%,
#101e3a 90%,
#101e3a 100%);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
appearance: none;
width: 18px;
height: 18px;
border-radius: 50%;
border: 3px solid #fff4d2;
background: var(--gold);
box-shadow: 0 0 0 5px rgba(255, 211, 106, 0.14);
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
border-radius: 50%;
border: 3px solid #fff4d2;
background: var(--gold);
box-shadow: 0 0 0 5px rgba(255, 211, 106, 0.14);
}
.utility {
display: flex;
gap: 7px;
}
.utility button {
height: 36px;
padding: 0 10px;
font-size: 10px;
letter-spacing: 0.05em;
}
@media (max-width: 900px) {
body { overflow: auto; }
#app {
height: auto;
min-height: 100dvh;
}
.workspace {
grid-template-columns: 1fr;
grid-template-rows: 60vh 360px;
}
aside {
grid-template-columns: 1fr;
grid-template-rows: 1fr auto;
}
.timeline {
grid-template-columns: auto 1fr;
}
.range-wrap {
grid-column: 1 / -1;
grid-row: 2;
}
.utility {
justify-self: end;
}
}
@media (max-width: 520px) {
header { padding: 12px 14px; }
.header-status { display: none; }
.workspace { padding: 7px; gap: 7px; }
footer { padding: 12px; }
.stage-top .glass-pill:last-child { display: none; }
.range-wrap { grid-template-columns: 80px 1fr; }
.range-end { display: none; }
}
</style>
</head>
<body>
<div id="app">
<header>
<div class="title-wrap">
<div class="title-icon" aria-hidden="true"></div>
<div>
<h1>Mirewick Bay</h1>
<div class="subtitle">A voxel daylight observatory · 48.4° N, 4.1° W</div>
</div>
</div>
<div class="header-status">
<span class="status-dot"></span>
<span id="headerPhase">Local sunlight linked to globe</span>
</div>
</header>
<main class="workspace">
<section id="stage" aria-label="Interactive voxel coastal town">
<div class="stage-top">
<div class="glass-pill" id="scenePhase">DUSK · SOLAR ALTITUDE +1°</div>
<div class="glass-pill">ORIGINAL TEACHING DIORAMA</div>
</div>
<div id="townLabel">MIREWICK BAY · YOU ARE HERE</div>
<div class="stage-help">DRAG TO ORBIT · WHEEL TO ZOOM · SCRUB TIME BELOW</div>
</section>
<aside>
<section class="card globe-card">
<div class="card-head">
<div>
<div class="card-title">Schematic Globe</div>
<div class="card-note">The gold marker and the bay share one solar vector.</div>
</div>
<button class="tiny-button" id="locateButton">LINK ↗</button>
</div>
<div id="globeViewport">
<div id="globePinLabel">MIREWICK BAY</div>
<div class="globe-foot">
<span class="sun-key"><i></i> SOLAR DIRECTION</span>
<span>DRAG TO ORBIT</span>
</div>
</div>
</section>
<section class="card readout-card">
<div class="readout-grid">
<div class="readout">
<span>Local solar time</span>
<strong id="localTime">19:20</strong>
</div>
<div class="readout">
<span>Approx. UTC</span>
<strong id="utcTime">19:36</strong>
</div>
<div class="readout">
<span>Solar altitude</span>
<strong id="altitude">+1.5°</strong>
</div>
<div class="readout">
<span>Solar azimuth</span>
<strong id="azimuth">296°</strong>
</div>
</div>
<div class="teaching-note">
<b>Teaching model:</b> local hour angle, latitude, and seasonal declination
determine the sun. The same vector lights the town and moves the day–night
terminator on the globe.
</div>
</section>
</aside>
</main>
<footer>
<div class="timeline">
<button class="play-button" id="playButton">❚❚ Pause</button>
<div class="range-wrap">
<div class="time-value">
<span id="timeValue">19:20</span>
<small>LOCAL SOLAR TIME</small>
</div>
<input
id="timeRange"
type="range"
min="0"
max="1439"
step="1"
value="1160"
aria-label="Local solar time"
>
<div class="range-end">
00:00<br>24:00
</div>
</div>
<div class="utility">
<button id="speedButton">1× SPEED</button>
<button id="resetButton">RESET VIEW</button>
</div>
</div>
</footer>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
/* -------------------------------------------------------------------------- */
/* Constants and shared helpers */
/* -------------------------------------------------------------------------- */
const DEG = Math.PI / 180;
const LATITUDE = 48.4 * DEG;
const LONGITUDE = -4.1 * DEG;
const DAY_OF_YEAR = 138; // May 18, used for the seasonal solar declination.
const stage = document.getElementById("stage");
const globeViewport = document.getElementById("globeViewport");
const townLabel = document.getElementById("townLabel");
const globePinLabel = document.getElementById("globePinLabel");
const clamp = THREE.MathUtils.clamp;
const lerp = THREE.MathUtils.lerp;
const smoothstep = THREE.MathUtils.smoothstep;
function seededRandom(seed = 4821) {
let state = seed >>> 0;
return () => {
state = (1664525 * state + 1013904223) >>> 0;
return state / 4294967296;
};
}
const random = seededRandom();
function latitudeLongitudeToVector(latitude, longitude, radius = 1) {
const cosLat = Math.cos(latitude);
return new THREE.Vector3(
cosLat * Math.cos(longitude) * radius,
Math.sin(latitude) * radius,
cosLat * Math.sin(longitude) * radius
);
}
/* -------------------------------------------------------------------------- */
/* Main renderer and scene */
/* -------------------------------------------------------------------------- */
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color("#10283a");
scene.fog = new THREE.Fog("#10283a", 34, 70);
const camera = new THREE.PerspectiveCamera(43, 1, 0.1, 120);
const hemisphereLight = new THREE.HemisphereLight("#b9e9ff", "#26302a", 0.8);
scene.add(hemisphereLight);
const sunLight = new THREE.DirectionalLight("#fff0c2", 3);
sunLight.castShadow = true;
sunLight.shadow.mapSize.set(2048, 2048);
sunLight.shadow.camera.left = -17;
sunLight.shadow.camera.right = 17;
sunLight.shadow.camera.top = 17;
sunLight.shadow.camera.bottom = -17;
sunLight.shadow.camera.near = 1;
sunLight.shadow.camera.far = 80;
sunLight.shadow.bias = -0.00035;
sunLight.shadow.normalBias = 0.025;
scene.add(sunLight);
scene.add(sunLight.target);
sunLight.target.position.set(0, 1.4, 0);
const moonLight = new THREE.DirectionalLight("#789bd6", 0);
scene.add(moonLight);
scene.add(moonLight.target);
moonLight.target.position.copy(sunLight.target.position);
const geometryCache = new Map();
function boxGeometry(width, height, depth) {
const key = `${width}:${height}:${depth}`;
if (!geometryCache.has(key)) {
geometryCache.set(key, new THREE.BoxGeometry(width, height, depth));
}
return geometryCache.get(key);
}
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.83,
metalness: 0,
...options
});
}
function addBox(parent, width, height, depth, material, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(boxGeometry(width, height, depth), material);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
const materials = {
base: standardMaterial("#172532"),
ocean: standardMaterial("#1c6682", {
roughness: 0.28,
metalness: 0.18
}),
cliff: standardMaterial("#756e61"),
cliffLight: standardMaterial("#8a7e6c"),
grass: standardMaterial("#668c61"),
grassLight: standardMaterial("#759867"),
sand: standardMaterial("#bbaa79"),
road: standardMaterial("#4d5657"),
curb: standardMaterial("#a7a08b"),
wood: standardMaterial("#705344"),
woodLight: standardMaterial("#9a7354"),
darkWood: standardMaterial("#3e3935"),
white: standardMaterial("#ede5cf"),
red: standardMaterial("#ae5145"),
teal: standardMaterial("#387d80"),
navy: standardMaterial("#314e68"),
gold: standardMaterial("#d9a94f"),
dark: standardMaterial("#27343b"),
foliage: standardMaterial("#416c4d"),
foliageLight: standardMaterial("#5b8056"),
trunk: standardMaterial("#604b3b"),
window: standardMaterial("#ffdc91", {
emissive: "#ffad42",
emissiveIntensity: 0.02,
roughness: 0.4
}),
lamp: standardMaterial("#fff0aa", {
emissive: "#ffb83e",
emissiveIntensity: 0.05
}),
beacon: standardMaterial("#ffcf5d", {
emissive: "#ffad24",
emissiveIntensity: 1.5,
metalness: 0.1,
roughness: 0.35
})
};
const windowMaterial = materials.window;
const lampMaterial = materials.lamp;
const townPointLights = [];
const waveMeshes = [];
const houseGroups = [];
/* -------------------------------------------------------------------------- */
/* Diorama base, ocean, terrain, and roads */
/* -------------------------------------------------------------------------- */
addBox(scene, 29, 0.7, 22, materials.base, 0, -1.08, 0.2);
addBox(scene, 28.6, 0.42, 21.6, materials.ocean, 0, -0.76, 0.2);
function isLand(nx, nz) {
if (nz <= -4) return nx >= -2 && nx <= 2;
if (nz <= 0) return nx >= -4 && nx <= 3;
if (nz === 1) return nx >= -3 && nx <= 2;
if (nz === 2) return nx >= -2 && nx <= 1;
return false;
}
for (let nx = -5; nx <= 4; nx++) {
for (let nz = -5; nz <= 2; nz++) {
if (!isLand(nx, nz)) continue;
const x = nx * 1.34;
const z = nz * 1.18;
const cliffMaterial = (nx + nz + 11) % 3 === 0
? materials.cliffLight
: materials.cliff;
const grassMaterial = (nx * 3 + nz) % 4 === 0
? materials.grassLight
: materials.grass;
addBox(scene, 1.36, 2.0, 1.20, cliffMaterial, x, 0.45, z);
addBox(scene, 1.37, 0.18, 1.21, grassMaterial, x, 1.54, z);
if (nz === 2) {
addBox(scene, 1.37, 0.34, 0.2, materials.sand, x, 1.35, z + 0.56);
}
}
}
// Plateau roads.
addBox(scene, 10.6, 0.045, 0.72, materials.road, 0, 1.655, -1.16);
addBox(scene, 0.70, 0.048, 5.0, materials.road, 0.20, 1.658, -0.30);
for (let x = -4.8; x <= 4.8; x += 0.58) {
addBox(scene, 0.25, 0.012, 0.045, materials.curb, x, 1.684, -0.79);
addBox(scene, 0.25, 0.012, 0.045, materials.curb, x, 1.684, -1.53);
}
// Harbor quay and piers.
addBox(scene, 6.8, 0.28, 0.32, materials.wood, 0.2, -0.30, 2.45);
addBox(scene, 0.92, 0.18, 4.0, materials.wood, -0.65, -0.27, 4.35);
addBox(scene, 0.80, 0.18, 2.8, materials.wood, 2.25, -0.27, 3.80);
for (let z = 2.6; z <= 6.1; z += 0.42) {
addBox(scene, 0.94, 0.08, 0.34, materials.woodLight, -0.65, -0.12, z);
}
for (let z = 2.6; z <= 5.1; z += 0.42) {
addBox(scene, 0.82, 0.08, 0.34, materials.woodLight, 2.25, -0.12, z);
}
/* -------------------------------------------------------------------------- */
/* Buildings and town details */
/* -------------------------------------------------------------------------- */
function makeHouse(x, z, width, depth, wallHeight, wallMaterial, roofMaterial) {
const group = new THREE.Group();
group.position.set(x, 1.69, z);
scene.add(group);
addBox(group, width + 0.12, 0.14, depth + 0.12, materials.curb, 0, 0.04, 0);
addBox(group, width, wallHeight, depth, wallMaterial, 0, wallHeight / 2 + 0.1, 0);
const roofThickness = 0.16;
const roofDepth = depth + 0.30;
const roofWidth = width * 0.76;
const leftRoof = addBox(
group, roofWidth, roofThickness, roofDepth, roofMaterial,
-width * 0.22, wallHeight + 0.23, 0
);
leftRoof.rotation.z = 0.56;
const rightRoof = addBox(
group, roofWidth, roofThickness, roofDepth, roofMaterial,
width * 0.22, wallHeight + 0.23, 0
);
rightRoof.rotation.z = -0.56;
addBox(group, 0.16, 0.16, roofDepth + 0.05, roofMaterial, 0, wallHeight + 0.49, 0);
// Front door.
addBox(group, 0.28, 0.58, 0.055, materials.darkWood,
-width * 0.19, 0.44, depth / 2 + 0.025);
// Front windows.
addBox(group, 0.29, 0.32, 0.045, windowMaterial,
width * 0.22, 0.63, depth / 2 + 0.028);
addBox(group, 0.29, 0.32, 0.045, windowMaterial,
width * 0.43, 0.63, depth / 2 + 0.028);
// Side window.
addBox(group, 0.04, 0.30, 0.28, windowMaterial,
-width / 2 - 0.025, 0.62, -depth * 0.12);
// Chimney.
addBox(group, 0.22, 0.56, 0.23, materials.cliffLight,
width * 0.26, wallHeight + 0.55, -depth * 0.18);
houseGroups.push(group);
return group;
}
makeHouse(-3.55, -2.62, 1.12, 1.04, 1.15, materials.white, materials.red);
makeHouse(-1.62, -2.68, 1.25, 1.10, 1.30, materials.teal, materials.navy);
makeHouse(1.48, -2.65, 1.18, 1.06, 1.22, materials.white, materials.red);
makeHouse(3.30, -2.28, 1.05, 1.00, 1.04, materials.navy, materials.teal);
makeHouse(-3.52, 0.20, 1.16, 1.08, 1.24, materials.teal, materials.red);
makeHouse(-1.72, 0.22, 1.06, 1.02, 1.08, materials.white, materials.navy);
makeHouse(1.55, 0.25, 1.22, 1.10, 1.28, materials.navy, materials.red);
makeHouse(3.42, 0.10, 1.08, 1.02, 1.10, materials.white, materials.teal);
// A compact market shelter.
const market = new THREE.Group();
market.position.set(-0.75, 1.69, 0.55);
scene.add(market);
addBox(market, 1.15, 0.13, 0.70, materials.woodLight, 0, 0.86, 0);
for (const px of [-0.48, 0.48]) {
for (const pz of [-0.26, 0.26]) {
addBox(market, 0.07, 0.85, 0.07, materials.darkWood, px, 0.43, pz);
}
}
for (let i = 0; i < 4; i++) {
const awning = addBox(market, 0.28, 0.09, 0.82, i % 2 ? materials.white : materials.red,
-0.43 + i * 0.285, 0.91, 0);
awning.rotation.z = 0.03;
}
addBox(market, 0.40, 0.20, 0.35, materials.gold, -0.24, 1.00, 0);
addBox(market, 0.32, 0.18, 0.30, materials.red, 0.20, 0.99, 0);
// Lighthouse.
const lighthouse = new THREE.Group();
lighthouse.position.set(-4.70, 1.43, -1.75);
scene.add(lighthouse);
addBox(lighthouse, 1.05, 0.30, 1.05, materials.cliffLight, 0, 0.15, 0);
for (let i = 0; i < 4; i++) {
addBox(lighthouse, 0.63 - i * 0.045, 0.38, 0.63 - i * 0.045,
i % 2 === 0 ? materials.white : materials.red,
0, 0.50 + i * 0.38, 0);
}
addBox(lighthouse, 0.74, 0.16, 0.74, materials.dark, 0, 2.04, 0);
addBox(lighthouse, 0.48, 0.46, 0.48, lampMaterial, 0, 2.35, 0);
addBox(lighthouse, 0.68, 0.13, 0.68, materials.navy, 0, 2.65, 0);
addBox(lighthouse, 0.27, 0.26, 0.27, materials.gold, 0, 2.84, 0);
const lighthouseLight = new THREE.PointLight("#ffc55d", 0, 5, 2);
lighthouseLight.position.set(-4.7, 3.85, -1.75);
scene.add(lighthouseLight);
// Voxel trees.
function makeTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 1.68, z);
group.scale.setScalar(scale);
scene.add(group);
addBox(group, 0.18, 0.92, 0.18, materials.trunk, 0, 0.45, 0);
addBox(group, 0.86, 0.62, 0.86, materials.foliage, 0, 1.03, 0);
addBox(group, 0.62, 0.55, 0.62, materials.foliageLight, 0, 1.53, 0);
addBox(group, 0.36, 0.42, 0.36, materials.foliage, 0.05, 1.93, -0.03);
}
makeTree(-4.05, -3.15, 0.88);
makeTree(-3.15, -3.55, 0.72);
makeTree(2.70, -3.40, 0.82);
makeTree(4.10, -1.35, 0.70);
makeTree(-4.15, 1.05, 0.72);
makeTree(3.85, 1.05, 0.76);
// Street lamps.
function makeLamp(x, z) {
addBox(scene, 0.09, 1.42, 0.09, materials.dark, x, 2.36, z);
addBox(scene, 0.28, 0.11, 0.28, materials.dark, x, 3.10, z);
addBox(scene, 0.18, 0.25, 0.18, lampMaterial, x, 2.99, z);
const light = new THREE.PointLight("#ffb64d", 0, 4.6, 2);
light.position.set(x, 2.88, z);
scene.add(light);
townPointLights.push(light);
}
makeLamp(-2.68, -0.72);
makeLamp(-0.12, -0.72);
makeLamp(2.55, -0.72);
makeLamp(-0.72, 1.40);
// Voxel boats.
function makeBoat(x, z, hullMaterial, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, -0.40, z);
group.rotation.y = rotation;
scene.add(group);
addBox(group, 0.72, 0.24, 1.45, hullMaterial, 0, 0, 0);
addBox(group, 0.52, 0.23, 0.88, hullMaterial, 0, 0.19, -0.08);
addBox(group, 0.39, 0.11, 0.62, materials.woodLight, 0, 0.36, -0.06);
addBox(group, 0.065, 1.35, 0.065, materials.wood, 0, 0.91, 0.02);
addBox(group, 0.52, 0.40, 0.055, materials.white, 0.08, 1.12, 0.02);
}
makeBoat(1.15, 4.75, materials.red, -0.22);
makeBoat(3.12, 3.58, materials.teal, 0.44);
makeBoat(-1.50, 5.72, materials.navy, 0.08);
// Harbor buoy.
addBox(scene, 0.22, 0.48, 0.22, materials.gold, 1.85, -0.25, 5.75);
addBox(scene, 0.32, 0.09, 0.32, materials.white, 1.85, -0.03, 5.75);
/* -------------------------------------------------------------------------- */
/* Town marker */
/* -------------------------------------------------------------------------- */
const townAnchor = new THREE.Group();
townAnchor.position.set(0.20, 1.69, -1.16);
scene.add(townAnchor);
const beaconMaterial = materials.beacon;
const markerCornerMaterial = new THREE.MeshBasicMaterial({
color: "#ffd66d",
transparent: true,
opacity: 0.95,
depthWrite: false
});
function addCornerMarker(parent, x, z) {
addBox(parent, 0.38, 0.025, 0.07, markerCornerMaterial, x - 0.15, 0.03, z);
addBox(parent, 0.07, 0.025, 0.38, markerCornerMaterial, x, 0.03, z - 0.15);
}
addCornerMarker(townAnchor, -0.34, -0.34);
addCornerMarker(townAnchor, 0.34, -0.34);
addCornerMarker(townAnchor, -0.34, 0.34);
addCornerMarker(townAnchor, 0.34, 0.34);
const floatingBeacon = addBox(townAnchor, 0.22, 0.22, 0.22, beaconMaterial, 0, 1.05, 0);
const beaconBeamMaterial = new THREE.MeshBasicMaterial({
color: "#ffd15d",
transparent: true,
opacity: 0.16,
depthWrite: false
});
addBox(townAnchor, 0.055, 2.1, 0.055, beaconBeamMaterial, 0, 1.05, 0);
/* -------------------------------------------------------------------------- */
/* Ocean flecks, clouds, and stars */
/* -------------------------------------------------------------------------- */
const waveMaterial = new THREE.MeshBasicMaterial({
color: "#c9f4f2",
transparent: true,
opacity: 0.45,
depthWrite: false
});
for (let i = 0; i < 55; i++) {
const width = 0.30 + random() * 0.85;
const wave = addBox(
scene, width, 0.025, 0.045, waveMaterial,
(random() - 0.5) * 27,
-0.535 + random() * 0.018,
(random() - 0.5) * 19
);
wave.castShadow = false;
wave.receiveShadow = false;
wave.userData.phase = random() * Math.PI * 2;
wave.userData.baseX = wave.position.x;
wave.userData.baseOpacity = 0.2 + random() * 0.4;
waveMeshes.push(wave);
}
const cloudMaterial = new THREE.MeshBasicMaterial({
color: "#eef7f6",
transparent: true,
opacity: 0.65,
depthWrite: false
});
const cloudGroups = [];
function makeCloud(x, y, z, scale) {
const group = new THREE.Group();
group.position.set(x, y, z);
group.scale.setScalar(scale);
scene.add(group);
addBox(group, 1.8, 0.52, 0.85, cloudMaterial, 0, 0, 0);
addBox(group, 1.1, 0.65, 0.75, cloudMaterial, -0.65, 0.12, 0);
addBox(group, 1.25, 0.78, 0.90, cloudMaterial, 0.55, 0.16, 0);
addBox(group, 0.72, 0.48, 0.68, cloudMaterial, 1.20, -0.02, 0);
group.traverse(object => {
if (object.isMesh) {
object.castShadow = false;
object.receiveShadow = false;
}
});
cloudGroups.push(group);
}
makeCloud(-8, 10.5, -6, 1.25);
makeCloud(7, 12.0, -9, 1.65);
makeCloud(2, 9.5, 7, 1.0);
const starMaterial = new THREE.MeshBasicMaterial({
color: "#eaf5ff",
transparent: true,
opacity: 0,
depthWrite: false
});
const stars = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.055, 0.055, 0.055),
starMaterial,
110
);
const dummy = new THREE.Object3D();
for (let i = 0; i < 110; i++) {
const theta = random() * Math.PI * 2;
const y = random() * 0.9 + 0.1;
const radial = Math.sqrt(1 - y * y);
const radius = 40 + random() * 8;
dummy.position.set(
Math.cos(theta) * radial * radius,
y * radius,
Math.sin(theta) * radial * radius
);
dummy.rotation.set(random(), random(), random());
dummy.scale.setScalar(0.6 + random() * 1.6);
dummy.updateMatrix();
stars.setMatrixAt(i, dummy.matrix);
}
stars.frustumCulled = false;
scene.add(stars);
/* -------------------------------------------------------------------------- */
/* Globe inset */
/* -------------------------------------------------------------------------- */
const globeRenderer = new THREE.WebGLRenderer({
alpha: true,
antialias: true,
powerPreference: "high-performance"
});
globeRenderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
globeRenderer.outputColorSpace = THREE.SRGBColorSpace;
globeRenderer.toneMapping = THREE.ACESFilmicToneMapping;
globeRenderer.toneMappingExposure = 1.1;
globeRenderer.setClearColor(0x000000, 0);
globeViewport.prepend(globeRenderer.domElement);
const globeScene = new THREE.Scene();
const globeCamera = new THREE.PerspectiveCamera(34, 1, 0.1, 30);
globeScene.add(new THREE.AmbientLight("#8ab4cf", 0.25));
const globeSunLight = new THREE.DirectionalLight("#fff0c6", 2.2);
globeScene.add(globeSunLight);
globeScene.add(globeSunLight.target);
function makePixelGlobeTexture() {
const width = 512;
const height = 256;
const sourceCanvas = document.createElement("canvas");
sourceCanvas.width = width;
sourceCanvas.height = height;
const sourceContext = sourceCanvas.getContext("2d");
sourceContext.fillStyle = "#174b68";
sourceContext.fillRect(0, 0, width, height);
function textureX(longitude) {
return ((180 - longitude) / 360) * width;
}
function textureY(latitude) {
return ((90 - latitude) / 180) * height;
}
function polygon(points, color) {
sourceContext.beginPath();
sourceContext.moveTo(textureX(points[0][0]), textureY(points[0][1]));
for (let i = 1; i < points.length; i++) {
sourceContext.lineTo(textureX(points[i][0]), textureY(points[i][1]));
}
sourceContext.closePath();
sourceContext.fillStyle = color;
sourceContext.fill();
}
polygon([
[-168, 70], [-145, 72], [-125, 60], [-112, 50], [-96, 49],
[-82, 25], [-65, 45], [-52, 57], [-76, 72], [-115, 75]
], "#557f59");
polygon([
[-81, 12], [-65, 10], [-51, 2], [-35, -8], [-43, -28],
[-55, -55], [-70, -48], [-78, -18]
], "#648858");
polygon([
[-11, 36], [-9, 58], [-2, 62], [8, 71], [30, 70], [48, 60],
[70, 75], [108, 76], [145, 61], [180, 66], [180, 36], [148, 28],
[123, 20], [106, 4], [82, 8], [69, 24], [47, 29], [30, 40],
[15, 36], [8, 44], [1, 42], [-5, 36]
], "#638c5e");
polygon([
[-17, 35], [7, 37], [32, 31], [51, 12], [43, -12], [30, -35],
[12, -35], [-2, -18], [-16, 4]
], "#6d8d59");
polygon([
[113, -11], [131, -12], [153, -25], [146, -39], [121, -35], [112, -22]
], "#658657");
polygon([
[-54, 60], [-42, 61], [-20, 76], [-40, 83], [-62, 76]
], "#8ba172");
// A small schematic British coast helps place the fictional town marker.
polygon([
[-8, 50], [-6, 59], [1, 58], [2, 52], [-2, 50]
], "#739466");
const sourceData = sourceContext.getImageData(0, 0, width, height);
const outputCanvas = document.createElement("canvas");
outputCanvas.width = width;
outputCanvas.height = height;
const outputContext = outputCanvas.getContext("2d");
const pixelSize = 4;
for (let y = 0; y < height; y += pixelSize) {
for (let x = 0; x < width; x += pixelSize) {
const sampleX = Math.min(width - 1, x + Math.floor(pixelSize / 2));
const sampleY = Math.min(height - 1, y + Math.floor(pixelSize / 2));
const index = (sampleY * width + sampleX) * 4;
outputContext.fillStyle = `rgb(${sourceData.data[index]}, ${sourceData.data[index + 1]}, ${sourceData.data[index + 2]})`;
outputContext.fillRect(x, y, pixelSize, pixelSize);
}
}
const texture = new THREE.CanvasTexture(outputCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.magFilter = THREE.NearestFilter;
texture.minFilter = THREE.LinearMipmapLinearFilter;
texture.anisotropy = Math.min(4, globeRenderer.capabilities.getMaxAnisotropy());
return texture;
}
const globeTexture = makePixelGlobeTexture();
const globeRadius = 1.48;
const globeMesh = new THREE.Mesh(
new THREE.SphereGeometry(globeRadius, 64, 40),
new THREE.MeshStandardMaterial({
map: globeTexture,
roughness: 0.86,
metalness: 0
})
);
globeScene.add(globeMesh);
const atmosphere = new THREE.Mesh(
new THREE.SphereGeometry(globeRadius * 1.045, 48, 32),
new THREE.MeshBasicMaterial({
color: "#62bddd",
transparent: true,
opacity: 0.13,
side: THREE.BackSide,
depthWrite: false
})
);
globeScene.add(atmosphere);
const gridMaterial = new THREE.LineBasicMaterial({
color: "#b5e3ec",
transparent: true,
opacity: 0.22
});
function addGlobeGrid(latitude, radius, opacityMaterial = gridMaterial) {
const points = [];
for (let longitude = 0; longitude <= 360; longitude += 3) {
points.push(latitudeLongitudeToVector(
latitude * DEG,
longitude * DEG,
radius
));
}
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
opacityMaterial
);
globeScene.add(line);
}
for (const latitude of [-60, -30, 0, 30, 60]) {
addGlobeGrid(latitude, globeRadius * 1.006);
}
for (let longitude = 0; longitude < 180; longitude += 30) {
const points = [];
for (let latitude = -90; latitude <= 90; latitude += 2) {
points.push(latitudeLongitudeToVector(
latitude * DEG,
longitude * DEG,
globeRadius * 1.006
));
}
globeScene.add(new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
gridMaterial
));
}
const townGlobePosition = latitudeLongitudeToVector(
LATITUDE,
LONGITUDE,
globeRadius * 1.018
);
const markerMaterial = new THREE.MeshBasicMaterial({ color: "#ffd361" });
const markerPin = new THREE.Mesh(new THREE.SphereGeometry(0.065, 16, 12), markerMaterial);
markerPin.position.copy(townGlobePosition);
globeScene.add(markerPin);
const markerRing = new THREE.Mesh(
new THREE.TorusGeometry(0.11, 0.018, 10, 32),
markerMaterial
);
markerRing.position.copy(townGlobePosition);
markerRing.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 0, 1),
townGlobePosition.clone().normalize()
);
globeScene.add(markerRing);
const markerStem = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
townGlobePosition.clone(),
townGlobePosition.clone().multiplyScalar(1.18)
]),
new THREE.LineBasicMaterial({ color: "#ffd361" })
);
globeScene.add(markerStem);
const globeSunOrb = new THREE.Mesh(
new THREE.SphereGeometry(0.095, 16, 12),
new THREE.MeshBasicMaterial({ color: "#ffe08a" })
);
globeScene.add(globeSunOrb);
/* -------------------------------------------------------------------------- */
/* Direct camera controls: no external controls module */
/* -------------------------------------------------------------------------- */
class OrbitController {
constructor(element, cameraObject, options) {
this.element = element;
this.camera = cameraObject;
this.target = new THREE.Vector3();
this.desiredTarget = new THREE.Vector3();
this.theta = options.theta;
this.phi = options.phi;
this.radius = options.radius;
this.desiredTheta = options.theta;
this.desiredPhi = options.phi;
this.desiredRadius = options.radius;
this.dragging = false;
this.pointerX = 0;
this.pointerY = 0;
element.addEventListener("pointerdown", event => {
this.dragging = true;
this.pointerX = event.clientX;
this.pointerY = event.clientY;
element.setPointerCapture(event.pointerId);
});
element.addEventListener("pointermove", event => {
if (!this.dragging) return;
const dx = event.clientX - this.pointerX;
const dy = event.clientY - this.pointerY;
this.pointerX = event.clientX;
this.pointerY = event.clientY;
this.desiredTheta -= dx * 0.006;
this.desiredPhi = clamp(this.desiredPhi - dy * 0.005, 0.28, 1.42);
});
const endDrag = event => {
this.dragging = false;
if (element.hasPointerCapture(event.pointerId)) {
element.releasePointerCapture(event.pointerId);
}
};
element.addEventListener("pointerup", endDrag);
element.addEventListener("pointercancel", endDrag);
element.addEventListener("wheel", event => {
event.preventDefault();
const factor = Math.exp(event.deltaY * 0.001);
this.desiredRadius = clamp(this.desiredRadius * factor, options.minRadius, options.maxRadius);
}, { passive: false });
element.addEventListener("contextmenu", event => event.preventDefault());
this.update(1);
}
update(delta) {
const damping = 1 - Math.exp(-10 * delta);
this.theta = lerp(this.theta, this.desiredTheta, damping);
this.phi = lerp(this.phi, this.desiredPhi, damping);
this.radius = lerp(this.radius, this.desiredRadius, damping);
this.target.lerp(this.desiredTarget, damping);
const sinPhi = Math.sin(this.phi);
this.camera.position.set(
this.target.x + this.radius * sinPhi * Math.sin(this.theta),
this.target.y + this.radius * Math.cos(this.phi),
this.target.z + this.radius * sinPhi * Math.cos(this.theta)
);
this.camera.lookAt(this.target);
}
}
const mainControls = new OrbitController(renderer.domElement, camera, {
theta: 0.66,
phi: 0.92,
radius: 29,
minRadius: 15,
maxRadius: 47
});
const globeControls = new OrbitController(globeRenderer.domElement, globeCamera, {
theta: 1.55,
phi: 1.05,
radius: 4.35,
minRadius: 3.25,
maxRadius: 6.2
});
/* -------------------------------------------------------------------------- */
/* Solar model */
/* -------------------------------------------------------------------------- */
const declination =
23.44 * DEG * Math.sin((2 * Math.PI * (DAY_OF_YEAR - 81)) / 365);
function calculateSolarState(minutes) {
const hours = minutes / 60;
const hourAngle = (hours - 12) * 15 * 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);
const vector = new THREE.Vector3(east, up, -north).normalize();
return {
east,
north,
up,
altitude: Math.asin(clamp(up, -1, 1)),
azimuth: (Math.atan2(east, north) / DEG + 360) % 360,
mainVector: vector
};
}
function localFrameToEarthFrame(solarState) {
// ECEF axes: X toward longitude 0, Y toward the north pole,
// and Z toward longitude 90°E.
const east = new THREE.Vector3(
-Math.sin(LONGITUDE),
0,
Math.cos(LONGITUDE)
);
const north = new THREE.Vector3(
-Math.sin(LATITUDE) * Math.cos(LONGITUDE),
Math.cos(LATITUDE),
-Math.sin(LATITUDE) * Math.sin(LONGITUDE)
);
const up = latitudeLongitudeToVector(LATITUDE, LONGITUDE, 1);
return new THREE.Vector3()
.addScaledVector(east, solarState.east)
.addScaledVector(north, solarState.north)
.addScaledVector(up, solarState.up)
.normalize();
}
/* -------------------------------------------------------------------------- */
/* Time state and UI */
/* -------------------------------------------------------------------------- */
let solarMinutes = 19 * 60 + 20;
let playing = !window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let speedMultiplier = 1;
let linkPulse = 0;
let currentSolarState = calculateSolarState(solarMinutes);
const playButton = document.getElementById("playButton");
const speedButton = document.getElementById("speedButton");
const timeRange = document.getElementById("timeRange");
const timeValue = document.getElementById("timeValue");
const localTime = document.getElementById("localTime");
const utcTime = document.getElementById("utcTime");
const altitude = document.getElementById("altitude");
const azimuth = document.getElementById("azimuth");
const scenePhase = document.getElementById("scenePhase");
const headerPhase = document.getElementById("headerPhase");
function formatTime(minutes) {
const normalized = ((minutes % 1440) + 1440) % 1440;
const hours = Math.floor(normalized / 60);
const mins = Math.floor(normalized % 60);
return `${String(hours).padStart(2, "0")}:${String(mins).padStart(2, "0")}`;
}
function updatePlayButton() {
playButton.textContent = playing ? "❚❚ Pause" : "▶ Play";
}
playButton.addEventListener("click", () => {
playing = !playing;
updatePlayButton();
});
speedButton.addEventListener("click", () => {
speedMultiplier = speedMultiplier === 1 ? 4 : 1;
speedButton.textContent = `${speedMultiplier}× SPEED`;
});
timeRange.addEventListener("input", event => {
solarMinutes = Number(event.target.value);
});
document.getElementById("resetButton").addEventListener("click", () => {
mainControls.desiredTarget.set(0, 1.4, 0);
mainControls.desiredRadius = 29;
mainControls.desiredTheta = 0.66;
mainControls.desiredPhi = 0.92;
});
document.getElementById("locateButton").addEventListener("click", () => {
mainControls.desiredTarget.copy(townAnchor.position).add(new THREE.Vector3(0, 0.7, 0));
mainControls.desiredRadius = 22;
mainControls.desiredTheta = 0.42;
mainControls.desiredPhi = 0.88;
linkPulse = 1;
});
updatePlayButton();
/* -------------------------------------------------------------------------- */
/* Dynamic lighting, sky, water, and interface readouts */
/* -------------------------------------------------------------------------- */
const nightSky = new THREE.Color("#071426");
const daySky = new THREE.Color("#91cfe4");
const twilightSky = new THREE.Color("#d98770");
const nightWater = new THREE.Color("#102f43");
const dayWater = new THREE.Color("#1c6682");
const warmSun = new THREE.Color("#ff9b55");
const whiteSun = new THREE.Color("#fff2c9");
function updateSimulation(elapsedSeconds) {
if (playing) {
solarMinutes = (solarMinutes + elapsedSeconds * 18 * speedMultiplier) % 1440;
}
currentSolarState = calculateSolarState(solarMinutes);
const altitudeDegrees = currentSolarState.altitude / DEG;
const daylight = smoothstep(altitudeDegrees, -5, 12);
const lampFactor = 1 - smoothstep(altitudeDegrees, -4, 5);
const twilightFactor = Math.max(0, 1 - Math.abs(altitudeDegrees) / 12);
const sky = nightSky.clone().lerp(daySky, daylight);
sky.lerp(twilightSky, twilightFactor * 0.43);
scene.background.copy(sky);
scene.fog.color.copy(sky);
hemisphereLight.intensity = 0.25 + daylight * 0.73;
hemisphereLight.color.copy(new THREE.Color("#7f9fbb")).lerp(
new THREE.Color("#c4efff"),
daylight
);
sunLight.position.copy(sunLight.target.position).addScaledVector(
currentSolarState.mainVector,
36
);
sunLight.intensity = daylight * 3.35;
sunLight.color.copy(warmSun).lerp(whiteSun, smoothstep(altitudeDegrees, 3, 26));
moonLight.position.copy(sunLight.target.position).addScaledVector(
currentSolarState.mainVector,
-28
);
moonLight.intensity = (1 - daylight) * 0.32;
materials.ocean.color.copy(nightWater).lerp(dayWater, daylight);
waveMaterial.opacity = 0.12 + daylight * 0.46;
cloudMaterial.opacity = 0.22 + daylight * 0.48;
cloudMaterial.color.set(daylight > 0.35 ? "#edf6f5" : "#60758b");
starMaterial.opacity = (1 - daylight) * 0.95;
windowMaterial.emissiveIntensity = 0.02 + lampFactor * 2.0;
lampMaterial.emissiveIntensity = 0.04 + lampFactor * 2.5;
lighthouseLight.intensity = lampFactor * 16;
for (const light of townPointLights) {
light.intensity = lampFactor * 15;
}
// The globe is illuminated by the same physical solar state.
const earthSunVector = localFrameToEarthFrame(currentSolarState);
globeSunLight.position.copy(earthSunVector).multiplyScalar(5);
globeSunOrb.position.copy(earthSunVector).multiplyScalar(2.45);
globeSunLight.intensity = 2.25;
const utcHours = (solarMinutes / 60 - LONGITUDE / DEG / 15 + 48) % 24;
const utcMinutes = Math.round(utcHours * 60);
const utcNormalized = (utcMinutes + 1440) % 1440;
const formattedLocal = formatTime(solarMinutes);
const formattedUTC = formatTime(utcNormalized);
timeValue.textContent = formattedLocal;
localTime.textContent = formattedLocal;
utcTime.textContent = formattedUTC;
altitude.textContent = `${altitudeDegrees >= 0 ? "+" : ""}${altitudeDegrees.toFixed(1)}°`;
azimuth.textContent = `${Math.round(currentSolarState.azimuth)}°`;
let phase = "NIGHT";
if (altitudeDegrees >= 4) phase = "DAYLIGHT";
else if (altitudeDegrees > -6) phase = "TWILIGHT";
scenePhase.textContent = `${phase} · SOLAR ALTITUDE ${altitudeDegrees >= 0 ? "+" : ""}${altitudeDegrees.toFixed(0)}°`;
headerPhase.textContent = `${phase.toLowerCase()} · globe and bay synchronized`;
timeRange.value = String(Math.floor(solarMinutes));
}
/* -------------------------------------------------------------------------- */
/* Resize handling */
/* -------------------------------------------------------------------------- */
function resizeMain() {
const width = Math.max(1, stage.clientWidth);
const height = Math.max(1, stage.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
function resizeGlobe() {
const width = Math.max(1, globeViewport.clientWidth);
const height = Math.max(1, globeViewport.clientHeight);
globeRenderer.setSize(width, height, false);
globeCamera.aspect = width / height;
globeCamera.updateProjectionMatrix();
}
const mainResizeObserver = new ResizeObserver(resizeMain);
mainResizeObserver.observe(stage);
const globeResizeObserver = new ResizeObserver(resizeGlobe);
globeResizeObserver.observe(globeViewport);
resizeMain();
resizeGlobe();
/* -------------------------------------------------------------------------- */
/* Projected labels and animation */
/* -------------------------------------------------------------------------- */
const projected = new THREE.Vector3();
function positionLabel(element, worldPosition, cameraObject, width, height, offsetY = 0) {
projected.copy(worldPosition).project(cameraObject);
const visible = projected.z > -1 && projected.z < 1 &&
Math.abs(projected.x) < 1.25 && Math.abs(projected.y) < 1.25;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height + offsetY;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
let previousTime = performance.now();
function animate(now) {
const delta = Math.min(0.05, (now - previousTime) / 1000);
previousTime = now;
updateSimulation(delta);
mainControls.update(delta);
globeControls.update(delta);
const elapsed = now * 0.001;
// Gentle animated voxel water.
for (const wave of waveMeshes) {
wave.position.x = wave.userData.baseX + Math.sin(elapsed * 0.35 + wave.userData.phase) * 0.34;
wave.material.opacity = wave.userData.baseOpacity *
(0.55 + 0.45 * Math.sin(elapsed * 0.8 + wave.userData.phase));
}
// Slow cloud drift.
cloudGroups[0].position.x = -8 + Math.sin(elapsed * 0.035) * 1.4;
cloudGroups[1].position.x = 7 + Math.sin(elapsed * 0.027 + 1) * 1.8;
cloudGroups[2].position.x = 2 + Math.sin(elapsed * 0.043 + 2) * 1.2;
// Marker pulse and link animation.
const pulse = 1 + Math.sin(elapsed * 3.2) * 0.10 + linkPulse * 0.22;
markerRing.scale.setScalar(pulse);
floatingBeacon.rotation.y = elapsed * 0.8;
floatingBeacon.rotation.x = elapsed * 0.45;
floatingBeacon.position.y = 1.05 + Math.sin(elapsed * 2.2) * 0.06;
beaconBeamMaterial.opacity = 0.10 + 0.08 * (0.5 + 0.5 * Math.sin(elapsed * 2.2));
linkPulse = Math.max(0, linkPulse - delta * 0.7);
positionLabel(
townLabel,
townAnchor.position.clone().add(new THREE.Vector3(0, 1.6, 0)),
camera,
stage.clientWidth,
stage.clientHeight,
-4
);
positionLabel(
globePinLabel,
townGlobePosition.clone().multiplyScalar(1.15),
globeCamera,
globeViewport.clientWidth,
globeViewport.clientHeight,
-4
);
renderer.render(scene, camera);
globeRenderer.render(globeScene, globeCamera);
requestAnimationFrame(animate);
}
updateSimulation(0);
requestAnimationFrame(animate);
</script>
</body>
</html>
```