Prompt specification
Build or customize the Three.js 3D scene "Coastal weather station terrain scene" 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 weather station terrain scene
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/coastal-weather-station-terrain-scene-x1279
Scene Description Prompt:
Compare synthetic readings from voxel wind sensors on an exposed headland and sheltered courtyard above the ocean. Build standalone HTML with Three.js from cdnjs.
Working Three.js Starter (coastal-weather-station-terrain-scene-x1279):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Wind Survey — Headland vs. Courtyard</title>
<style>
:root {
--bg: #06131d;
--panel: rgba(8, 24, 35, 0.86);
--panel-border: rgba(144, 218, 226, 0.18);
--text: #e7f4f4;
--muted: #8ba7ad;
--cyan: #54d8d0;
--yellow: #ffd166;
--coral: #ff786b;
--headland: #64e2d7;
--courtyard: #ffd166;
--shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.glass {
background: var(--panel);
border: 1px solid var(--panel-border);
box-shadow: var(--shadow);
backdrop-filter: blur(18px) saturate(125%);
-webkit-backdrop-filter: blur(18px) saturate(125%);
}
#topbar {
position: fixed;
z-index: 20;
top: 0;
left: 0;
right: 0;
height: 72px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 12px 20px;
background: linear-gradient(
180deg,
rgba(4, 14, 22, 0.94),
rgba(4, 14, 22, 0.58) 70%,
transparent
);
pointer-events: none;
}
.brand,
.top-actions {
pointer-events: auto;
}
.eyebrow {
margin-bottom: 4px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(20px, 2.1vw, 29px);
font-weight: 650;
letter-spacing: -0.035em;
}
.top-actions {
display: flex;
align-items: center;
gap: 7px;
}
.view-button,
.preset-button,
.icon-button {
border: 1px solid rgba(163, 222, 226, 0.17);
background: rgba(13, 35, 46, 0.72);
border-radius: 9px;
cursor: pointer;
transition:
background 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
.view-button {
padding: 9px 12px;
color: #a9c4c8;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.02em;
}
.view-button:hover,
.view-button.active {
color: #efffff;
background: rgba(42, 113, 120, 0.45);
border-color: rgba(84, 216, 208, 0.55);
}
.live-pill {
display: flex;
align-items: center;
gap: 7px;
margin-left: 7px;
padding: 9px 11px;
border: 1px solid rgba(84, 216, 208, 0.24);
border-radius: 999px;
color: #a9e4df;
background: rgba(10, 49, 52, 0.4);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.11em;
}
.live-pill::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
animation: pulse 1.8s ease-in-out infinite;
}
@keyframes pulse {
50% {
opacity: 0.4;
transform: scale(0.78);
}
}
#panel {
position: fixed;
z-index: 15;
top: 84px;
left: 18px;
width: 326px;
max-height: calc(100vh - 104px);
overflow: auto;
border-radius: 15px;
scrollbar-width: thin;
scrollbar-color: rgba(84, 216, 208, 0.3) transparent;
transition: max-height 220ms ease;
}
#panel.collapsed {
max-height: 48px;
overflow: hidden;
}
.panel-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 15px 16px 12px;
border-bottom: 1px solid rgba(151, 217, 222, 0.12);
}
.panel-header h2 {
margin: 0;
font-size: 12px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.icon-button {
width: 28px;
height: 28px;
color: #a9c4c8;
}
.panel-content {
padding: 15px 16px 16px;
}
.section-title {
margin: 0 0 12px;
color: #91adb1;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.control-row {
margin-bottom: 15px;
}
.control-label {
display: flex;
align-items: baseline;
justify-content: space-between;
margin-bottom: 8px;
color: #bdd0d2;
font-size: 11px;
}
.control-label output {
color: #f2ffff;
font-variant-numeric: tabular-nums;
font-weight: 750;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 4px 0;
appearance: none;
border-radius: 999px;
outline: none;
background:
linear-gradient(
90deg,
var(--cyan) 0%,
var(--cyan) var(--range-progress, 50%),
rgba(169, 203, 207, 0.17) var(--range-progress, 50%)
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
appearance: none;
border: 3px solid #d9ffff;
border-radius: 50%;
background: #3fc8c2;
box-shadow: 0 0 0 4px rgba(84, 216, 208, 0.13);
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 3px solid #d9ffff;
border-radius: 50%;
background: #3fc8c2;
}
.presets {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
margin: -4px 0 16px;
}
.preset-button {
padding: 7px 5px;
color: #9db6ba;
font-size: 10px;
font-weight: 700;
}
.preset-button:hover {
color: white;
border-color: rgba(84, 216, 208, 0.45);
}
.divider {
height: 1px;
margin: 15px 0;
background: rgba(151, 217, 222, 0.12);
}
.sensor-card {
position: relative;
margin-top: 9px;
padding: 12px;
overflow: hidden;
border: 1px solid rgba(155, 213, 218, 0.12);
border-radius: 12px;
background: rgba(3, 15, 23, 0.35);
}
.sensor-card::before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 3px;
height: 100%;
background: var(--accent);
opacity: 0.9;
}
.sensor-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 9px;
}
.sensor-name {
color: #dcebed;
font-size: 11px;
font-weight: 750;
}
.sensor-location {
margin-top: 3px;
color: #78959a;
font-size: 9px;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.sensor-number {
color: var(--accent);
font-size: 25px;
font-weight: 720;
letter-spacing: -0.04em;
font-variant-numeric: tabular-nums;
}
.sensor-number small {
margin-left: 3px;
color: #7e9da2;
font-size: 10px;
font-weight: 600;
letter-spacing: 0;
}
.sensor-meta {
display: flex;
gap: 15px;
margin-top: 8px;
color: #819ca1;
font-size: 9px;
}
.sensor-meta b {
color: #c3d7d9;
font-weight: 700;
font-variant-numeric: tabular-nums;
}
.sparkline {
display: block;
width: 100%;
height: 35px;
margin-top: 7px;
}
.toggles {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px 10px;
}
.toggle {
display: flex;
align-items: center;
gap: 7px;
color: #9fb8bc;
font-size: 10px;
cursor: pointer;
user-select: none;
}
.toggle input {
width: 13px;
height: 13px;
margin: 0;
accent-color: var(--cyan);
}
.model-note {
margin: 13px 0 0;
color: #69868b;
font-size: 9px;
line-height: 1.55;
}
#comparison {
position: fixed;
z-index: 12;
right: 18px;
bottom: 18px;
width: 350px;
padding: 15px 16px 14px;
border-radius: 15px;
}
.comparison-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 13px;
}
.comparison-header h2 {
margin: 0;
font-size: 12px;
letter-spacing: 0.04em;
}
.comparison-header span {
color: #769398;
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.bar-row {
display: grid;
grid-template-columns: 104px 1fr 50px;
align-items: center;
gap: 9px;
margin: 9px 0;
color: #adc4c7;
font-size: 10px;
}
.bar-track {
height: 7px;
overflow: hidden;
border-radius: 999px;
background: rgba(178, 214, 217, 0.1);
}
.bar-fill {
width: 0;
height: 100%;
border-radius: inherit;
transition: width 180ms ease-out;
}
#headBar {
background: linear-gradient(90deg, #42bfb9, #80eadc);
box-shadow: 0 0 12px rgba(84, 216, 208, 0.3);
}
#courtBar {
background: linear-gradient(90deg, #d9a849, #ffe28d);
box-shadow: 0 0 12px rgba(255, 209, 102, 0.22);
}
.bar-value {
text-align: right;
color: #e8f3f3;
font-weight: 750;
font-variant-numeric: tabular-nums;
}
.comparison-stats {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-top: 13px;
padding-top: 12px;
border-top: 1px solid rgba(151, 217, 222, 0.12);
}
.comparison-stat {
text-align: center;
}
.comparison-stat strong {
display: block;
color: #efffff;
font-size: 15px;
font-variant-numeric: tabular-nums;
}
.comparison-stat span {
display: block;
margin-top: 3px;
color: #769398;
font-size: 8px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#compass {
position: fixed;
z-index: 10;
top: 92px;
right: 22px;
width: 94px;
height: 94px;
border: 1px solid rgba(151, 217, 222, 0.15);
border-radius: 50%;
background:
radial-gradient(circle, rgba(15, 43, 54, 0.82), rgba(6, 20, 29, 0.72));
box-shadow: var(--shadow);
}
#compass::before,
#compass::after {
content: "";
position: absolute;
background: rgba(151, 217, 222, 0.13);
}
#compass::before {
top: 9px;
bottom: 9px;
left: 50%;
width: 1px;
}
#compass::after {
left: 9px;
right: 9px;
top: 50%;
height: 1px;
}
.compass-n {
position: absolute;
z-index: 2;
top: 4px;
left: 50%;
transform: translateX(-50%);
color: #94b5b8;
font-size: 8px;
font-weight: 800;
}
#needle {
position: absolute;
z-index: 3;
top: 18px;
left: calc(50% - 5px);
width: 10px;
height: 29px;
transform-origin: 50% 29px;
transition: transform 180ms ease-out;
}
#needle::before {
content: "";
position: absolute;
top: 0;
left: 1px;
border-right: 4px solid transparent;
border-bottom: 18px solid var(--coral);
border-left: 4px solid transparent;
}
#needle::after {
content: "";
position: absolute;
bottom: 0;
left: 4px;
width: 2px;
height: 13px;
background: #c2d6d8;
}
#compassValue {
position: absolute;
right: 0;
bottom: -20px;
left: 0;
text-align: center;
color: #a7c3c5;
font-size: 9px;
font-variant-numeric: tabular-nums;
}
#legend {
position: fixed;
z-index: 9;
right: 18px;
bottom: 190px;
width: 168px;
padding: 11px 12px;
border-radius: 11px;
color: #91aaae;
font-size: 9px;
}
.legend-title {
margin-bottom: 8px;
color: #bcd0d2;
font-weight: 750;
}
.legend-gradient {
height: 6px;
border-radius: 999px;
background: linear-gradient(90deg, #4cd7c5, #ffd166, #ff6b5e);
}
.legend-scale {
display: flex;
justify-content: space-between;
margin-top: 5px;
font-variant-numeric: tabular-nums;
}
#labels {
position: fixed;
z-index: 8;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.site-label {
position: absolute;
min-width: 145px;
padding: 9px 11px;
border: 1px solid rgba(155, 221, 224, 0.22);
border-radius: 10px;
background: rgba(4, 18, 27, 0.83);
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.28);
transform: translate(-50%, -100%);
backdrop-filter: blur(8px);
transition: opacity 120ms ease;
}
.site-label::after {
content: "";
position: absolute;
bottom: -5px;
left: calc(50% - 5px);
width: 9px;
height: 9px;
border-right: 1px solid rgba(155, 221, 224, 0.22);
border-bottom: 1px solid rgba(155, 221, 224, 0.22);
background: rgba(4, 18, 27, 0.83);
transform: rotate(45deg);
}
.site-label small {
display: block;
margin-bottom: 3px;
color: #7f9ca1;
font-size: 8px;
font-weight: 750;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.site-label strong {
display: block;
color: #e9f5f5;
font-size: 11px;
}
.site-label span {
display: block;
margin-top: 4px;
color: #8ba8ad;
font-size: 9px;
}
.site-label b {
color: var(--label-accent);
font-size: 13px;
}
#labelHead {
--label-accent: var(--headland);
}
#labelCourt {
--label-accent: var(--courtyard);
}
#help {
position: fixed;
z-index: 8;
left: 18px;
bottom: 18px;
padding: 9px 12px;
border-radius: 9px;
color: #789399;
font-size: 9px;
letter-spacing: 0.02em;
}
kbd {
padding: 1px 4px;
border: 1px solid rgba(160, 208, 211, 0.18);
border-radius: 4px;
color: #b2c9cc;
background: rgba(174, 214, 218, 0.06);
font-family: inherit;
font-size: 8px;
}
@media (max-width: 800px) {
#topbar {
height: 64px;
padding: 10px 12px;
}
.live-pill,
.eyebrow {
display: none;
}
.view-button {
padding: 8px 9px;
font-size: 9px;
}
#panel {
top: 72px;
left: 10px;
width: min(310px, calc(100vw - 20px));
max-height: calc(100vh - 225px);
}
#comparison {
right: 10px;
bottom: 10px;
width: calc(100vw - 20px);
padding: 12px;
}
#legend {
right: 10px;
bottom: 181px;
width: 148px;
}
#compass {
top: 76px;
right: 13px;
width: 76px;
height: 76px;
}
#needle {
top: 16px;
transform-origin: 50% 22px;
}
#help {
display: none;
}
.comparison-stats {
margin-top: 8px;
padding-top: 8px;
}
}
@media (max-height: 650px) and (min-width: 801px) {
#panel {
max-height: calc(100vh - 94px);
}
#legend {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive Three.js coastal wind sensor comparison"></canvas>
<div id="labels">
<div id="labelHead" class="site-label">
<small>Exposed headland</small>
<strong>Coastal voxel array</strong>
<span><b id="labelHeadValue">—</b> m/s mean</span>
</div>
<div id="labelCourt" class="site-label">
<small>Sheltered courtyard</small>
<strong>Lee-side voxel array</strong>
<span><b id="labelCourtValue">—</b> m/s mean</span>
</div>
</div>
<header id="topbar">
<div class="brand">
<div class="eyebrow">Coastal microclimate / synthetic readings</div>
<h1>Voxel Wind Survey</h1>
</div>
<div class="top-actions">
<button class="view-button active" data-view="overview">Overview</button>
<button class="view-button" data-view="headland">Headland</button>
<button class="view-button" data-view="courtyard">Courtyard</button>
<div class="live-pill">Live model</div>
</div>
</header>
<aside id="panel" class="glass">
<div class="panel-header">
<h2>Survey controls</h2>
<button id="panelToggle" class="icon-button" aria-label="Collapse survey controls">−</button>
</div>
<div class="panel-content">
<h3 class="section-title">Atmospheric input</h3>
<div class="control-row">
<label class="control-label" for="speed">
<span>Open-water wind speed</span>
<output id="speedOutput">14.0 m/s</output>
</label>
<input id="speed" type="range" min="4" max="24" step="0.1" value="14" />
</div>
<div class="control-row">
<label class="control-label" for="direction">
<span>Wind direction <small>(from)</small></span>
<output id="directionOutput">275° W</output>
</label>
<input id="direction" type="range" min="0" max="359" step="1" value="275" />
</div>
<div class="presets">
<button class="preset-button" data-speed="8">Light</button>
<button class="preset-button" data-speed="14">Fresh</button>
<button class="preset-button" data-speed="21">Strong</button>
</div>
<div class="divider"></div>
<h3 class="section-title">Voxel sensor readings</h3>
<div class="sensor-card" style="--accent: var(--headland)">
<div class="sensor-title">
<div>
<div class="sensor-name">Exposed Headland</div>
<div class="sensor-location">27 voxels · open coast</div>
</div>
<div class="sensor-number">
<span id="headMean">—</span><small>m/s</small>
</div>
</div>
<div class="sensor-meta">
<span>Voxel peak <b id="headGust">—</b></span>
<span>Local <b id="headDirection">—</b></span>
</div>
<canvas id="headSpark" class="sparkline"></canvas>
</div>
<div class="sensor-card" style="--accent: var(--courtyard)">
<div class="sensor-title">
<div>
<div class="sensor-name">Sheltered Courtyard</div>
<div class="sensor-location">27 voxels · enclosed terrace</div>
</div>
<div class="sensor-number">
<span id="courtMean">—</span><small>m/s</small>
</div>
</div>
<div class="sensor-meta">
<span>Voxel peak <b id="courtGust">—</b></span>
<span>Local <b id="courtDirection">—</b></span>
</div>
<canvas id="courtSpark" class="sparkline"></canvas>
</div>
<div class="divider"></div>
<h3 class="section-title">Scene layers</h3>
<div class="toggles">
<label class="toggle"><input id="showParticles" type="checkbox" checked /> Wind particles</label>
<label class="toggle"><input id="showVectors" type="checkbox" checked /> Voxel vectors</label>
<label class="toggle"><input id="showLabels" type="checkbox" checked /> Site labels</label>
<label class="toggle"><input id="showFlow" type="checkbox" checked /> Flow arrow</label>
</div>
<p class="model-note">
Synthetic model: open-coast exposure, courtyard lee attenuation,
directional gusting, and deterministic voxel-scale turbulence.
Values are illustrative, not observational.
</p>
</div>
</aside>
<div id="compass" aria-label="Wind direction compass">
<span class="compass-n">N</span>
<div id="needle"></div>
<span id="compassValue">275° W</span>
</div>
<div id="legend" class="glass">
<div class="legend-title">Absolute voxel speed</div>
<div class="legend-gradient"></div>
<div class="legend-scale"><span>0</span><span>15</span><span>30 m/s</span></div>
</div>
<section id="comparison" class="glass">
<div class="comparison-header">
<h2>Mean speed comparison</h2>
<span>27 sensors each</span>
</div>
<div class="bar-row">
<span>Headland</span>
<div class="bar-track"><div id="headBar" class="bar-fill"></div></div>
<span id="headBarValue" class="bar-value">—</span>
</div>
<div class="bar-row">
<span>Courtyard</span>
<div class="bar-track"><div id="courtBar" class="bar-fill"></div></div>
<span id="courtBarValue" class="bar-value">—</span>
</div>
<div class="comparison-stats">
<div class="comparison-stat">
<strong id="differenceStat">—</strong>
<span>Speed difference</span>
</div>
<div class="comparison-stat">
<strong id="reductionStat">—</strong>
<span>Shelter reduction</span>
</div>
<div class="comparison-stat">
<strong id="ratioStat">—</strong>
<span>Exposure ratio</span>
</div>
</div>
</section>
<div id="help" class="glass">
<kbd>drag</kbd> orbit · <kbd>shift + drag</kbd> pan ·
<kbd>wheel</kbd> zoom · <kbd>R</kbd> reset
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM and shared state
// -------------------------------------------------------------------------
const $ = (id) => document.getElementById(id);
const canvas = $("scene");
const state = {
sourceSpeed: 14,
sourceDirection: 275,
time: 0,
statsTimer: 0,
showParticles: true,
showVectors: true,
showLabels: true,
showFlow: true
};
const HEAD_CENTER = new THREE.Vector3(-30, 13.25, -2);
const COURT_CENTER = new THREE.Vector3(27, 8.75, 2);
const COLOR_MAX = 30;
const lowColor = new THREE.Color(0x4cd7c5);
const midColor = new THREE.Color(0xffd166);
const highColor = new THREE.Color(0xff6b5e);
function speedColor(speed, target = new THREE.Color()) {
const t = THREE.MathUtils.clamp(speed / COLOR_MAX, 0, 1);
if (t < 0.55) {
return target.lerpColors(lowColor, midColor, t / 0.55);
}
return target.lerpColors(midColor, highColor, (t - 0.55) / 0.45);
}
function cardinal(degrees) {
const names = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
"S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"];
return names[Math.round(((degrees % 360) + 360) % 360 / 22.5) % 16];
}
function directionText(degrees) {
return `${Math.round(degrees)}° ${cardinal(degrees)}`;
}
function windFlow(degrees) {
const radians = THREE.MathUtils.degToRad(degrees);
// Meteorological direction is the direction from which the wind arrives.
return new THREE.Vector3(-Math.sin(radians), 0, -Math.cos(radians));
}
function seededRandom(seed) {
let value = seed >>> 0;
return () => {
value += 0x6d2b79f5;
let t = value;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const random = seededRandom(27491);
// -------------------------------------------------------------------------
// Renderer, camera, and lighting
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071722);
scene.fog = new THREE.FogExp2(0x071722, 0.0095);
const camera = new THREE.PerspectiveCamera(
44,
window.innerWidth / window.innerHeight,
0.1,
500
);
const cameraState = {
target: new THREE.Vector3(0, 6, 0),
radius: 102,
theta: 2.38,
phi: 1.02,
desiredTarget: new THREE.Vector3(0, 6, 0),
desiredRadius: 102,
desiredTheta: 2.38,
desiredPhi: 1.02
};
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);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0x9ed9e6, 0x14242d, 2.0);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffefd2, 2.5);
sun.position.set(-35, 75, -25);
sun.castShadow = true;
sun.shadow.mapSize.set(1024, 1024);
sun.shadow.camera.left = -75;
sun.shadow.camera.right = 75;
sun.shadow.camera.top = 60;
sun.shadow.camera.bottom = -60;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 180;
sun.shadow.bias = -0.0008;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x48b8d0, 1.1);
rim.position.set(50, 25, 45);
scene.add(rim);
// -------------------------------------------------------------------------
// Ocean and grid
// -------------------------------------------------------------------------
const oceanGeometry = new THREE.PlaneGeometry(260, 260, 34, 34);
const oceanMaterial = new THREE.MeshStandardMaterial({
color: 0x0b344b,
roughness: 0.3,
metalness: 0.28,
transparent: true,
opacity: 0.94
});
const ocean = new THREE.Mesh(oceanGeometry, oceanMaterial);
ocean.rotation.x = -Math.PI / 2;
ocean.position.y = -0.15;
ocean.receiveShadow = true;
scene.add(ocean);
const grid = new THREE.GridHelper(240, 48, 0x1e5968, 0x123b49);
grid.position.y = 0.03;
grid.material.transparent = true;
grid.material.opacity = 0.19;
scene.add(grid);
// -------------------------------------------------------------------------
// Voxel terrain
// -------------------------------------------------------------------------
const terrainGroup = new THREE.Group();
scene.add(terrainGroup);
const rockMaterials = [
new THREE.MeshStandardMaterial({ color: 0x40545a, roughness: 0.93 }),
new THREE.MeshStandardMaterial({ color: 0x4b5f62, roughness: 0.95 }),
new THREE.MeshStandardMaterial({ color: 0x34474e, roughness: 0.96 }),
new THREE.MeshStandardMaterial({ color: 0x59686a, roughness: 0.91 })
];
function addVoxelRock(x, z, width, height, depth, material, y = 0) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y + height / 2, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
terrainGroup.add(mesh);
return mesh;
}
// Exposed headland: a stepped, wind-swept promontory.
for (let ix = -4; ix <= 4; ix++) {
for (let iz = -3; iz <= 3; iz++) {
const ridge = Math.max(0, 1 - Math.hypot(ix / 4.5, iz / 3.4));
const height = 2.2 + ridge * 8.1 + random() * 1.4;
const material = rockMaterials[Math.floor(random() * rockMaterials.length)];
addVoxelRock(
HEAD_CENTER.x + ix * 3.05,
HEAD_CENTER.z + iz * 3.05,
3.0,
height,
3.0,
material
);
}
}
// Courtyard support terrace and rock foundation.
const courtRockMaterial = rockMaterials[1];
for (let ix = -2; ix <= 2; ix++) {
for (let iz = -2; iz <= 2; iz++) {
const height = 5.3 + random() * 0.7;
addVoxelRock(
27 + ix * 3.25,
2 + iz * 3.25,
3.2,
height,
3.2,
courtRockMaterial
);
}
}
const terraceMaterial = new THREE.MeshStandardMaterial({
color: 0x718083,
roughness: 0.84
});
const terrace = new THREE.Mesh(
new THREE.BoxGeometry(18, 1.1, 16),
terraceMaterial
);
terrace.position.set(27, 5.75, 2);
terrace.castShadow = true;
terrace.receiveShadow = true;
terrainGroup.add(terrace);
const courtyardFloor = new THREE.Mesh(
new THREE.BoxGeometry(14.5, 0.18, 12.5),
new THREE.MeshStandardMaterial({
color: 0x4c666c,
roughness: 0.9
})
);
courtyardFloor.position.set(27, 6.39, 2);
courtyardFloor.receiveShadow = true;
terrainGroup.add(courtyardFloor);
const wallMaterial = new THREE.MeshStandardMaterial({
color: 0x879496,
roughness: 0.82
});
const darkWallMaterial = new THREE.MeshStandardMaterial({
color: 0x64777a,
roughness: 0.88
});
// North wall and side walls; the southern side remains open toward the sea.
const backWall = new THREE.Mesh(
new THREE.BoxGeometry(17, 5.6, 0.7),
darkWallMaterial
);
backWall.position.set(27, 9.25, 9.5);
backWall.castShadow = true;
backWall.receiveShadow = true;
terrainGroup.add(backWall);
for (const x of [18.8, 35.2]) {
const sideWall = new THREE.Mesh(
new THREE.BoxGeometry(0.7, 5.6, 14.4),
wallMaterial
);
sideWall.position.set(x, 9.25, 2.3);
sideWall.castShadow = true;
sideWall.receiveShadow = true;
terrainGroup.add(sideWall);
}
// Architectural blocks and planters.
const architecturalMaterial = new THREE.MeshStandardMaterial({
color: 0x91a0a0,
roughness: 0.77
});
const lowBlock = new THREE.Mesh(
new THREE.BoxGeometry(3.8, 1.2, 2.0),
architecturalMaterial
);
lowBlock.position.set(22, 7.0, 5.5);
lowBlock.castShadow = true;
terrainGroup.add(lowBlock);
const planter = new THREE.Mesh(
new THREE.BoxGeometry(1.4, 1.0, 1.4),
new THREE.MeshStandardMaterial({
color: 0x425b4c,
roughness: 0.95
})
);
planter.position.set(32.6, 7.0, -1.6);
planter.castShadow = true;
terrainGroup.add(planter);
// Distant voxel islets.
const isletMaterial = new THREE.MeshStandardMaterial({
color: 0x263d45,
roughness: 0.95
});
[
[-66, -48, 4.0],
[-73, -40, 2.2],
[59, -55, 3.1],
[68, -49, 1.8],
[-8, -72, 2.4],
[8, 68, 3.4]
].forEach(([x, z, size]) => {
const islet = new THREE.Mesh(
new THREE.BoxGeometry(size, size * 0.65, size),
isletMaterial
);
islet.position.set(x, size * 0.25, z);
terrainGroup.add(islet);
});
// -------------------------------------------------------------------------
// Voxel sensor arrays
// -------------------------------------------------------------------------
function createSensor(type, center, groundY, accentColor) {
const group = new THREE.Group();
group.position.copy(center);
scene.add(group);
const spacing = 0.72;
const cubeSize = 0.54;
const extent = spacing * 2 + cubeSize;
const cubeCount = 27;
const cubes = [];
const vectorPositions = new Float32Array(cubeCount * 6);
const vectorColors = new Float32Array(cubeCount * 6);
const vectorGeometry = new THREE.BufferGeometry();
vectorGeometry.setAttribute(
"position",
new THREE.BufferAttribute(vectorPositions, 3).setUsage(THREE.DynamicDrawUsage)
);
vectorGeometry.setAttribute(
"color",
new THREE.BufferAttribute(vectorColors, 3).setUsage(THREE.DynamicDrawUsage)
);
const vectorLines = new THREE.LineSegments(
vectorGeometry,
new THREE.LineBasicMaterial({
vertexColors: true,
transparent: true,
opacity: 0.78,
depthWrite: false
})
);
group.add(vectorLines);
let index = 0;
for (let x = -1; x <= 1; x++) {
for (let y = -1; y <= 1; y++) {
for (let z = -1; z <= 1; z++) {
const offset = new THREE.Vector3(
x * spacing,
y * spacing,
z * spacing
);
const material = new THREE.MeshStandardMaterial({
color: accentColor,
emissive: accentColor,
emissiveIntensity: 0.24,
roughness: 0.35,
metalness: 0.12
});
const cube = new THREE.Mesh(
new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize),
material
);
cube.position.copy(offset);
cube.castShadow = true;
cube.receiveShadow = true;
group.add(cube);
const n = Math.sin((index + 1) * 12.9898 + (type === "head" ? 0.7 : 4.2));
const fraction = n - Math.floor(n);
const variation = (fraction * 2 - 1) *
(type === "head" ? 0.10 : 0.045);
cubes.push({
cube,
material,
offset,
variation,
reading: 0
});
index++;
}
}
}
const mastHeight = Math.max(
0.45,
center.y - groundY - extent / 2
);
const mast = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.12, mastHeight, 8),
new THREE.MeshStandardMaterial({
color: 0xb4c2c2,
roughness: 0.45,
metalness: 0.6
})
);
mast.position.y = -extent / 2 - mastHeight / 2;
mast.castShadow = true;
group.add(mast);
const foot = new THREE.Mesh(
new THREE.CylinderGeometry(0.55, 0.68, 0.18, 12),
new THREE.MeshStandardMaterial({
color: 0x687b80,
roughness: 0.7,
metalness: 0.3
})
);
foot.position.y = -extent / 2 - mastHeight + 0.08;
foot.castShadow = true;
group.add(foot);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(1.45, 0.035, 8, 48),
new THREE.MeshBasicMaterial({
color: accentColor,
transparent: true,
opacity: 0.48
})
);
ring.rotation.x = Math.PI / 2;
ring.position.y = -extent / 2 - 0.08;
group.add(ring);
return {
type,
group,
cubes,
vectorLines,
vectorPositions,
vectorColors,
readings: new Array(cubeCount).fill(0),
mean: 0,
peak: 0,
localDirection: 0
};
}
const headSensor = createSensor(
"head",
HEAD_CENTER,
10.9,
0x64e2d7
);
const courtSensor = createSensor(
"court",
COURT_CENTER,
6.3,
0xffd166
);
// -------------------------------------------------------------------------
// Synthetic readings
// -------------------------------------------------------------------------
const headHistory = [];
const courtHistory = [];
const HISTORY_LENGTH = 90;
function updateReadings() {
const flow = windFlow(state.sourceDirection);
const t = state.time;
const gustPulse =
1 +
0.055 * Math.sin(t * 1.67) +
0.026 * Math.sin(t * 4.13 + 0.8);
const headBase =
state.sourceSpeed *
(1.04 + 0.11 * Math.max(0, flow.x)) *
gustPulse;
const courtBase =
state.sourceSpeed *
(
0.37 -
0.09 * Math.max(0, flow.z) +
0.025 * Math.max(0, -flow.x)
) *
(
1 +
0.028 * Math.sin(t * 1.21 + 1.4) +
0.015 * Math.sin(t * 3.71)
);
const sites = [
{
sensor: headSensor,
base: headBase,
direction: state.sourceDirection + Math.sin(t * 0.43) * 7
},
{
sensor: courtSensor,
base: courtBase,
direction: state.sourceDirection + Math.sin(t * 0.51 + 2) * 2.5
}
];
const color = new THREE.Color();
for (const site of sites) {
let sum = 0;
let peak = 0;
site.sensor.localDirection = site.direction;
for (let i = 0; i < site.sensor.cubes.length; i++) {
const item = site.sensor.cubes[i];
const reading = Math.max(
0.1,
site.base * (1 + item.variation)
);
item.reading = reading;
site.sensor.readings[i] = reading;
sum += reading;
peak = Math.max(peak, reading);
speedColor(reading, color);
item.material.color.copy(color);
item.material.emissive.copy(color).multiplyScalar(0.28);
const localFlow = windFlow(site.direction);
const vectorLength = 0.45 + (reading / COLOR_MAX) * 1.35;
const endpoint = item.offset.clone().addScaledVector(
localFlow,
vectorLength
);
const p = i * 6;
site.sensor.vectorPositions[p] = item.offset.x;
site.sensor.vectorPositions[p + 1] = item.offset.y;
site.sensor.vectorPositions[p + 2] = item.offset.z;
site.sensor.vectorPositions[p + 3] = endpoint.x;
site.sensor.vectorPositions[p + 4] = endpoint.y;
site.sensor.vectorPositions[p + 5] = endpoint.z;
for (let endpointIndex = 0; endpointIndex < 2; endpointIndex++) {
const c = (p + endpointIndex * 3);
site.sensor.vectorColors[c] = color.r;
site.sensor.vectorColors[c + 1] = color.g;
site.sensor.vectorColors[c + 2] = color.b;
}
}
site.sensor.mean = sum / site.sensor.cubes.length;
site.sensor.peak = peak;
site.sensor.vectorLines.geometry.attributes.position.needsUpdate = true;
site.sensor.vectorLines.geometry.attributes.color.needsUpdate = true;
}
headHistory.push(headSensor.mean);
courtHistory.push(courtSensor.mean);
if (headHistory.length > HISTORY_LENGTH) headHistory.shift();
if (courtHistory.length > HISTORY_LENGTH) courtHistory.shift();
updateDashboard();
}
// -------------------------------------------------------------------------
// Wind particle field
// -------------------------------------------------------------------------
const PARTICLE_COUNT = 280;
const particlePositions = new Float32Array(PARTICLE_COUNT * 3);
const particleColors = new Float32Array(PARTICLE_COUNT * 3);
const trailPositions = new Float32Array(PARTICLE_COUNT * 6);
const particles = [];
for (let i = 0; i < PARTICLE_COUNT; i++) {
const particle = {
x: (random() - 0.5) * 165,
y: 2.5 + random() * 25,
z: (random() - 0.5) * 135,
previousX: 0,
previousY: 0,
previousZ: 0,
phase: random() * Math.PI * 2
};
particle.previousX = particle.x;
particle.previousY = particle.y;
particle.previousZ = particle.z;
particles.push(particle);
}
const particleGeometry = new THREE.BufferGeometry();
particleGeometry.setAttribute(
"position",
new THREE.BufferAttribute(particlePositions, 3).setUsage(THREE.DynamicDrawUsage)
);
particleGeometry.setAttribute(
"color",
new THREE.BufferAttribute(particleColors, 3).setUsage(THREE.DynamicDrawUsage)
);
const particleMaterial = new THREE.PointsMaterial({
size: 0.34,
sizeAttenuation: true,
vertexColors: true,
transparent: true,
opacity: 0.78,
depthWrite: false
});
const particlePoints = new THREE.Points(particleGeometry, particleMaterial);
particlePoints.frustumCulled = false;
scene.add(particlePoints);
const trailGeometry = new THREE.BufferGeometry();
trailGeometry.setAttribute(
"position",
new THREE.BufferAttribute(trailPositions, 3).setUsage(THREE.DynamicDrawUsage)
);
const trails = new THREE.LineSegments(
trailGeometry,
new THREE.LineBasicMaterial({
color: 0x8ae9df,
transparent: true,
opacity: 0.22,
depthWrite: false
})
);
trails.frustumCulled = false;
scene.add(trails);
const flowArrow = new THREE.ArrowHelper(
windFlow(state.sourceDirection),
new THREE.Vector3(0, 21, 12),
12,
0x70e1d8,
2.2,
1.1
);
scene.add(flowArrow);
function spatialWindFactor(x, z) {
let factor = 0.72;
const headDistance = Math.hypot(
x - HEAD_CENTER.x,
z - HEAD_CENTER.z
);
if (headDistance < 14) {
const blend = 1 - THREE.MathUtils.smoothstep(headDistance, 4, 14);
factor = THREE.MathUtils.lerp(factor, 1.15, blend);
}
const courtDistance = Math.hypot(
x - COURT_CENTER.x,
z - COURT_CENTER.z
);
if (courtDistance < 11) {
const blend = 1 - THREE.MathUtils.smoothstep(courtDistance, 3.5, 11);
factor = THREE.MathUtils.lerp(factor, 0.37, blend);
}
return factor;
}
function updateParticles(delta) {
const flow = windFlow(state.sourceDirection);
const particleColor = new THREE.Color();
for (let i = 0; i < particles.length; i++) {
const particle = particles[i];
particle.previousX = particle.x;
particle.previousY = particle.y;
particle.previousZ = particle.z;
const localFactor = spatialWindFactor(particle.x, particle.z);
const jitter = Math.sin(state.time * 2.2 + particle.phase) * 0.12;
const speed = state.sourceSpeed * localFactor;
particle.x += (flow.x + jitter * flow.z) * speed * delta * 1.22;
particle.z += (flow.z - jitter * flow.x) * speed * delta * 1.22;
particle.y += Math.sin(
state.time * 1.3 + particle.phase + particle.x * 0.025
) * delta * 0.28;
// Keep particles above the simplified terrain.
const headDistance = Math.hypot(
particle.x - HEAD_CENTER.x,
particle.z - HEAD_CENTER.z
);
if (headDistance < 13) {
particle.y = Math.max(particle.y, 11.8);
}
const courtDistance = Math.hypot(
particle.x - COURT_CENTER.x,
particle.z - COURT_CENTER.z
);
if (courtDistance < 9.5) {
particle.y = Math.max(particle.y, 6.6);
}
if (particle.x > 86) particle.x = -86;
if (particle.x < -86) particle.x = 86;
if (particle.z > 70) particle.z = -70;
if (particle.z < -70) particle.z = 70;
if (particle.y > 29) particle.y = 2.5;
if (particle.y < 1.2) particle.y = 2.5;
const p = i * 3;
particlePositions[p] = particle.x;
particlePositions[p + 1] = particle.y;
particlePositions[p + 2] = particle.z;
speedColor(speed, particleColor);
particleColors[p] = particleColor.r;
particleColors[p + 1] = particleColor.g;
particleColors[p + 2] = particleColor.b;
const t = i * 6;
trailPositions[t] = particle.previousX;
trailPositions[t + 1] = particle.previousY;
trailPositions[t + 2] = particle.previousZ;
trailPositions[t + 3] = particle.x;
trailPositions[t + 4] = particle.y;
trailPositions[t + 5] = particle.z;
}
particleGeometry.attributes.position.needsUpdate = true;
particleGeometry.attributes.color.needsUpdate = true;
trailGeometry.attributes.position.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Dashboard and charts
// -------------------------------------------------------------------------
function drawSparkline(element, data, color) {
const rect = element.getBoundingClientRect();
const width = Math.max(50, rect.width);
const height = Math.max(25, rect.height);
const dpr = Math.min(window.devicePixelRatio || 1, 2);
const pixelWidth = Math.round(width * dpr);
const pixelHeight = Math.round(height * dpr);
if (element.width !== pixelWidth || element.height !== pixelHeight) {
element.width = pixelWidth;
element.height = pixelHeight;
}
const context = element.getContext("2d");
context.setTransform(dpr, 0, 0, dpr, 0, 0);
context.clearRect(0, 0, width, height);
context.strokeStyle = "rgba(174, 214, 218, 0.12)";
context.lineWidth = 1;
context.beginPath();
context.moveTo(0, height - 2);
context.lineTo(width, height - 2);
context.stroke();
if (!data.length) return;
const minValue = Math.min(...data);
const maxValue = Math.max(...data);
const range = Math.max(1, maxValue - minValue);
const padding = 4;
const xAt = (index) =>
(index / Math.max(1, data.length - 1)) * width;
const yAt = (value) =>
height - padding -
((value - minValue) / range) * (height - padding * 2);
const gradient = context.createLinearGradient(0, 0, 0, height);
gradient.addColorStop(0, `${color}55`);
gradient.addColorStop(1, `${color}00`);
context.beginPath();
context.moveTo(xAt(0), height);
data.forEach((value, index) => {
context.lineTo(xAt(index), yAt(value));
});
context.lineTo(xAt(data.length - 1), height);
context.closePath();
context.fillStyle = gradient;
context.fill();
context.beginPath();
data.forEach((value, index) => {
const x = xAt(index);
const y = yAt(value);
if (index === 0) context.moveTo(x, y);
else context.lineTo(x, y);
});
context.strokeStyle = color;
context.lineWidth = 1.5;
context.stroke();
const lastX = xAt(data.length - 1);
const lastY = yAt(data[data.length - 1]);
context.beginPath();
context.arc(lastX, lastY, 2.2, 0, Math.PI * 2);
context.fillStyle = color;
context.fill();
}
function updateDashboard() {
$("headMean").textContent = headSensor.mean.toFixed(1);
$("courtMean").textContent = courtSensor.mean.toFixed(1);
$("headGust").textContent = `${headSensor.peak.toFixed(1)} m/s`;
$("courtGust").textContent = `${courtSensor.peak.toFixed(1)} m/s`;
$("headDirection").textContent = directionText(headSensor.localDirection);
$("courtDirection").textContent = directionText(courtSensor.localDirection);
$("labelHeadValue").textContent = headSensor.mean.toFixed(1);
$("labelCourtValue").textContent = courtSensor.mean.toFixed(1);
$("headBarValue").textContent = `${headSensor.mean.toFixed(1)}`;
$("courtBarValue").textContent = `${courtSensor.mean.toFixed(1)}`;
$("headBar").style.width =
`${THREE.MathUtils.clamp((headSensor.mean / COLOR_MAX) * 100, 0, 100)}%`;
$("courtBar").style.width =
`${THREE.MathUtils.clamp((courtSensor.mean / COLOR_MAX) * 100, 0, 100)}%`;
const difference = headSensor.mean - courtSensor.mean;
const reduction = (1 - courtSensor.mean / headSensor.mean) * 100;
const ratio = headSensor.mean / courtSensor.mean;
$("differenceStat").textContent = `${difference.toFixed(1)} m/s`;
$("reductionStat").textContent = `${reduction.toFixed(0)}%`;
$("ratioStat").textContent = `${ratio.toFixed(1)}×`;
drawSparkline($("headSpark"), headHistory, "#64e2d7");
drawSparkline($("courtSpark"), courtHistory, "#ffd166");
}
// -------------------------------------------------------------------------
// UI controls
// -------------------------------------------------------------------------
function updateRangeFill(input) {
const min = Number(input.min);
const max = Number(input.max);
const value = Number(input.value);
const progress = ((value - min) / (max - min)) * 100;
input.style.setProperty("--range-progress", `${progress}%`);
}
$("speed").addEventListener("input", (event) => {
state.sourceSpeed = Number(event.target.value);
$("speedOutput").textContent = `${state.sourceSpeed.toFixed(1)} m/s`;
updateRangeFill(event.target);
updateReadings();
});
$("direction").addEventListener("input", (event) => {
state.sourceDirection = Number(event.target.value);
$("directionOutput").textContent = directionText(state.sourceDirection);
$("compassValue").textContent = directionText(state.sourceDirection);
$("needle").style.transform = `rotate(${state.sourceDirection}deg)`;
updateRangeFill(event.target);
updateReadings();
});
document.querySelectorAll("[data-speed]").forEach((button) => {
button.addEventListener("click", () => {
state.sourceSpeed = Number(button.dataset.speed);
$("speed").value = state.sourceSpeed;
$("speedOutput").textContent = `${state.sourceSpeed.toFixed(1)} m/s`;
updateRangeFill($("speed"));
updateReadings();
});
});
$("showParticles").addEventListener("change", (event) => {
state.showParticles = event.target.checked;
particlePoints.visible = state.showParticles;
trails.visible = state.showParticles;
});
$("showVectors").addEventListener("change", (event) => {
state.showVectors = event.target.checked;
headSensor.vectorLines.visible = state.showVectors;
courtSensor.vectorLines.visible = state.showVectors;
});
$("showLabels").addEventListener("change", (event) => {
state.showLabels = event.target.checked;
$("labels").style.display = state.showLabels ? "block" : "none";
});
$("showFlow").addEventListener("change", (event) => {
state.showFlow = event.target.checked;
flowArrow.visible = state.showFlow;
});
$("panelToggle").addEventListener("click", () => {
const panel = $("panel");
panel.classList.toggle("collapsed");
$("panelToggle").textContent = panel.classList.contains("collapsed") ? "+" : "−";
});
// -------------------------------------------------------------------------
// Direct camera controls: orbit, pan, zoom, keyboard, and touch drag
// -------------------------------------------------------------------------
let dragging = false;
let dragButton = 0;
let lastPointerX = 0;
let lastPointerY = 0;
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
dragButton = event.button;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
if (event.shiftKey || dragButton === 2) {
const right = new THREE.Vector3(
Math.cos(cameraState.desiredTheta),
0,
-Math.sin(cameraState.desiredTheta)
);
const forward = new THREE.Vector3(
-Math.sin(cameraState.desiredTheta),
0,
-Math.cos(cameraState.desiredTheta)
);
const scale = cameraState.desiredRadius * 0.0014;
cameraState.desiredTarget.addScaledVector(right, -dx * scale);
cameraState.desiredTarget.addScaledVector(forward, -dy * scale);
} else {
cameraState.desiredTheta -= dx * 0.006;
cameraState.desiredPhi = THREE.MathUtils.clamp(
cameraState.desiredPhi - dy * 0.005,
0.28,
1.46
);
}
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.desiredRadius = THREE.MathUtils.clamp(
cameraState.desiredRadius * Math.exp(event.deltaY * 0.001),
18,
155
);
},
{ passive: false }
);
function resetView() {
cameraState.desiredTarget.set(0, 6, 0);
cameraState.desiredRadius = 102;
cameraState.desiredTheta = 2.38;
cameraState.desiredPhi = 1.02;
document.querySelectorAll("[data-view]").forEach((button) => {
button.classList.toggle("active", button.dataset.view === "overview");
});
}
canvas.addEventListener("dblclick", resetView);
window.addEventListener("keydown", (event) => {
if (["INPUT", "TEXTAREA", "SELECT"].includes(document.activeElement?.tagName)) {
return;
}
switch (event.key) {
case "ArrowLeft":
cameraState.desiredTheta += 0.09;
break;
case "ArrowRight":
cameraState.desiredTheta -= 0.09;
break;
case "ArrowUp":
cameraState.desiredPhi = THREE.MathUtils.clamp(
cameraState.desiredPhi - 0.07,
0.28,
1.46
);
break;
case "ArrowDown":
cameraState.desiredPhi = THREE.MathUtils.clamp(
cameraState.desiredPhi + 0.07,
0.28,
1.46
);
break;
case "+":
case "=":
cameraState.desiredRadius = Math.max(
18,
cameraState.desiredRadius * 0.9
);
break;
case "-":
cameraState.desiredRadius = Math.min(
155,
cameraState.desiredRadius * 1.1
);
break;
case "r":
case "R":
resetView();
break;
}
});
function setView(view) {
if (view === "headland") {
cameraState.desiredTarget.set(-28, 9.5, -2);
cameraState.desiredRadius = 34;
cameraState.desiredTheta = 1.25;
cameraState.desiredPhi = 1.08;
} else if (view === "courtyard") {
cameraState.desiredTarget.set(27, 8, 2);
cameraState.desiredRadius = 34;
cameraState.desiredTheta = 2.12;
cameraState.desiredPhi = 1.05;
} else {
resetView();
return;
}
document.querySelectorAll("[data-view]").forEach((button) => {
button.classList.toggle("active", button.dataset.view === view);
});
}
document.querySelectorAll("[data-view]").forEach((button) => {
button.addEventListener("click", () => setView(button.dataset.view));
});
// -------------------------------------------------------------------------
// Projected site labels
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function updateLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible = projected.z > -1 && projected.z < 1;
if (!visible) {
element.style.opacity = "0";
return;
}
element.style.opacity = "1";
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
const headLabelPosition = HEAD_CENTER.clone().add(new THREE.Vector3(0, 3.35, 0));
const courtLabelPosition = COURT_CENTER.clone().add(new THREE.Vector3(0, 3.35, 0));
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let frame = 0;
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
state.time += delta;
frame++;
// Smooth camera transitions.
const cameraBlend = 1 - Math.exp(-delta * 5.0);
cameraState.target.lerp(cameraState.desiredTarget, cameraBlend);
cameraState.radius = THREE.MathUtils.lerp(
cameraState.radius,
cameraState.desiredRadius,
cameraBlend
);
cameraState.theta = THREE.MathUtils.lerp(
cameraState.theta,
cameraState.desiredTheta,
cameraBlend
);
cameraState.phi = THREE.MathUtils.lerp(
cameraState.phi,
cameraState.desiredPhi,
cameraBlend
);
updateCamera();
// Low-frequency ocean displacement.
const oceanPosition = oceanGeometry.attributes.position;
for (let i = 0; i < oceanPosition.count; i++) {
const x = oceanPosition.getX(i);
const y = oceanPosition.getY(i);
const wave =
Math.sin(x * 0.085 + state.time * 0.72) * 0.085 +
Math.cos(y * 0.061 - state.time * 0.51) * 0.065;
oceanPosition.setZ(i, wave);
}
oceanPosition.needsUpdate = true;
if (frame % 3 === 0) oceanGeometry.computeVertexNormals();
if (state.showParticles) {
updateParticles(delta);
}
flowArrow.setDirection(windFlow(state.sourceDirection));
flowArrow.setLength(
8 + state.sourceSpeed * 0.38,
2.1,
1.05
);
state.statsTimer += delta;
if (state.statsTimer >= 0.12) {
state.statsTimer = 0;
updateReadings();
}
updateLabel($("labelHead"), headLabelPosition);
updateLabel($("labelCourt"), courtLabelPosition);
renderer.render(scene, camera);
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
}
window.addEventListener("resize", resize);
// Initialize the dashboard.
updateRangeFill($("speed"));
updateRangeFill($("direction"));
$("needle").style.transform = `rotate(${state.sourceDirection}deg)`;
updateReadings();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```