371 lines
11 KiB
JavaScript
371 lines
11 KiB
JavaScript
// wind-layer.js - NOAA GFS 10m global GPU wind streamlines and O(1) point sampler
|
|
|
|
import * as THREE from "three";
|
|
import { CONFIG } from "./constants.js";
|
|
|
|
const WIND_META_ENDPOINT = "/api/v1/earth/wind/meta";
|
|
const PARTICLE_COUNT = 3600;
|
|
const TRAIL_SEGMENTS = 8;
|
|
const TRAIL_NODE_COUNT = TRAIL_SEGMENTS + 1;
|
|
const STREAMLINE_RADIUS = CONFIG.earthRadius + 1.6;
|
|
const MAX_PARTICLE_AGE = 140;
|
|
const ADVECT_SCALE = 0.056;
|
|
const DEG2RAD = Math.PI / 180;
|
|
|
|
let _earthObj = null;
|
|
let _group = null;
|
|
let _lines = null;
|
|
let _positions = null;
|
|
let _colors = null;
|
|
let _particles = null; // { lat, lon, age, maxAge, trailX, trailY, trailZ, trailLon }
|
|
let _visible = false;
|
|
|
|
let _gridMeta = null;
|
|
let _gridWidth = 0;
|
|
let _gridHeight = 0;
|
|
let _latMin = -90;
|
|
let _latMax = 90;
|
|
let _lonMin = -180;
|
|
let _lonMax = 180;
|
|
let _uGrid = null;
|
|
let _vGrid = null;
|
|
let _refreshPromise = null;
|
|
|
|
function bearingToCardinal(deg) {
|
|
const dirs = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"];
|
|
const idx = Math.round(((deg % 360) + 360) % 360 / 45) % 8;
|
|
return dirs[idx];
|
|
}
|
|
|
|
function writeSphericalXYZ(lat, lon, radius, xArr, yArr, zArr, idx) {
|
|
const phi = (90 - lat) * DEG2RAD;
|
|
const theta = (lon + 180) * DEG2RAD;
|
|
const sinPhi = Math.sin(phi);
|
|
xArr[idx] = -radius * sinPhi * Math.cos(theta);
|
|
yArr[idx] = radius * Math.cos(phi);
|
|
zArr[idx] = radius * sinPhi * Math.sin(theta);
|
|
}
|
|
|
|
function speedToColorRgb(speedMs) {
|
|
// Dedicated luminous cool-light palette (frost-cyan -> sky-cyan -> aurora-ice),
|
|
// intentionally free of orange/amber/magenta/green so streamlines never mimic satellites.
|
|
const t = Math.max(0, Math.min(1, speedMs / 24.0));
|
|
if (t < 0.45) {
|
|
const k = t / 0.45;
|
|
return [0.50 + 0.14 * k, 0.84 + 0.10 * k, 0.98 + 0.02 * k];
|
|
}
|
|
const k = (t - 0.45) / 0.55;
|
|
return [0.64 + 0.34 * k, 0.94 + 0.06 * k, 1.0];
|
|
}
|
|
|
|
function resetParticleTrail(particle, lat, lon) {
|
|
writeSphericalXYZ(lat, lon, STREAMLINE_RADIUS, particle.trailX, particle.trailY, particle.trailZ, 0);
|
|
const x0 = particle.trailX[0];
|
|
const y0 = particle.trailY[0];
|
|
const z0 = particle.trailZ[0];
|
|
for (let i = 0; i < TRAIL_NODE_COUNT; i += 1) {
|
|
particle.trailX[i] = x0;
|
|
particle.trailY[i] = y0;
|
|
particle.trailZ[i] = z0;
|
|
particle.trailLon[i] = lon;
|
|
}
|
|
}
|
|
|
|
function writeZeroParticleSegments(index) {
|
|
const base = index * TRAIL_SEGMENTS * 6;
|
|
for (let s = 0; s < TRAIL_SEGMENTS * 6; s += 1) {
|
|
_positions[base + s] = 0;
|
|
_colors[base + s] = 0;
|
|
}
|
|
}
|
|
|
|
function spawnParticle(index) {
|
|
const rawLat = (Math.asin(Math.random() * 2 - 1) * 180) / Math.PI;
|
|
const lat = Math.max(-82, Math.min(82, rawLat));
|
|
const lon = Math.random() * 360 - 180;
|
|
const existing = _particles[index];
|
|
if (existing) {
|
|
existing.lat = lat;
|
|
existing.lon = lon;
|
|
existing.age = Math.floor(Math.random() * MAX_PARTICLE_AGE);
|
|
existing.maxAge = MAX_PARTICLE_AGE;
|
|
resetParticleTrail(existing, lat, lon);
|
|
} else {
|
|
const particle = {
|
|
lat,
|
|
lon,
|
|
age: Math.floor(Math.random() * MAX_PARTICLE_AGE),
|
|
maxAge: MAX_PARTICLE_AGE,
|
|
trailX: new Float32Array(TRAIL_NODE_COUNT),
|
|
trailY: new Float32Array(TRAIL_NODE_COUNT),
|
|
trailZ: new Float32Array(TRAIL_NODE_COUNT),
|
|
trailLon: new Float32Array(TRAIL_NODE_COUNT),
|
|
};
|
|
resetParticleTrail(particle, lat, lon);
|
|
_particles[index] = particle;
|
|
}
|
|
}
|
|
|
|
function initParticleBuffers() {
|
|
if (_particles) return;
|
|
_particles = new Array(PARTICLE_COUNT);
|
|
_positions = new Float32Array(PARTICLE_COUNT * TRAIL_SEGMENTS * 2 * 3);
|
|
_colors = new Float32Array(PARTICLE_COUNT * TRAIL_SEGMENTS * 2 * 3);
|
|
for (let i = 0; i < PARTICLE_COUNT; i += 1) {
|
|
spawnParticle(i);
|
|
}
|
|
}
|
|
|
|
export function sampleWindAtLatLon(lat, lon) {
|
|
if (!_uGrid || !_vGrid || _gridWidth < 2 || _gridHeight < 2) return null;
|
|
if (!Number.isFinite(lat) || !Number.isFinite(lon)) return null;
|
|
|
|
const clampedLat = Math.max(_latMin, Math.min(_latMax, lat));
|
|
const wrappedLon = (((lon + 180) % 360) + 360) % 360 - 180;
|
|
|
|
const yNorm = (clampedLat - _latMin) / Math.max(1e-5, _latMax - _latMin);
|
|
const xNorm = (wrappedLon - _lonMin) / Math.max(1e-5, _lonMax - _lonMin);
|
|
const fy = Math.max(0, Math.min(_gridHeight - 1, yNorm * (_gridHeight - 1)));
|
|
const fx = Math.max(0, Math.min(_gridWidth - 1, xNorm * (_gridWidth - 1)));
|
|
|
|
const r0 = Math.floor(fy);
|
|
const r1 = Math.min(_gridHeight - 1, r0 + 1);
|
|
const c0 = Math.floor(fx);
|
|
const c1 = Math.min(_gridWidth - 1, c0 + 1);
|
|
const dr = fy - r0;
|
|
const dc = fx - c0;
|
|
|
|
const i00 = r0 * _gridWidth + c0;
|
|
const i01 = r0 * _gridWidth + c1;
|
|
const i10 = r1 * _gridWidth + c0;
|
|
const i11 = r1 * _gridWidth + c1;
|
|
|
|
const u =
|
|
(_uGrid[i00] * (1 - dc) + _uGrid[i01] * dc) * (1 - dr) +
|
|
(_uGrid[i10] * (1 - dc) + _uGrid[i11] * dc) * dr;
|
|
const v =
|
|
(_vGrid[i00] * (1 - dc) + _vGrid[i01] * dc) * (1 - dr) +
|
|
(_vGrid[i10] * (1 - dc) + _vGrid[i11] * dc) * dr;
|
|
|
|
const speedMs = Math.hypot(u, v);
|
|
const directionDeg = ((Math.atan2(u, v) * 180) / Math.PI + 360) % 360;
|
|
return {
|
|
uMs: u,
|
|
vMs: v,
|
|
speedMs: Number(speedMs.toFixed(1)),
|
|
directionDeg: Number(directionDeg.toFixed(0)),
|
|
cardinal: bearingToCardinal(directionDeg),
|
|
};
|
|
}
|
|
|
|
export function createWindFieldLayer(earthObj) {
|
|
_earthObj = earthObj;
|
|
if (_group) return _group;
|
|
|
|
initParticleBuffers();
|
|
// Plain Object3D keeps groupOrder=0 so wind streamlines never mask higher-order layers like satellites.
|
|
_group = new THREE.Object3D();
|
|
_group.name = "earth-wind-streamlines";
|
|
_group.visible = false;
|
|
_group.raycast = () => {};
|
|
|
|
const geom = new THREE.BufferGeometry();
|
|
geom.setAttribute("position", new THREE.BufferAttribute(_positions, 3));
|
|
geom.setAttribute("color", new THREE.BufferAttribute(_colors, 3));
|
|
|
|
const mat = new THREE.LineBasicMaterial({
|
|
vertexColors: true,
|
|
transparent: true,
|
|
opacity: 0.76,
|
|
blending: THREE.AdditiveBlending,
|
|
depthTest: true,
|
|
depthWrite: false,
|
|
});
|
|
|
|
_lines = new THREE.LineSegments(geom, mat);
|
|
_lines.name = "earth-wind-lines";
|
|
_lines.renderOrder = 1.04;
|
|
_lines.raycast = () => {};
|
|
_lines.frustumCulled = false;
|
|
|
|
_group.add(_lines);
|
|
if (earthObj) {
|
|
earthObj.add(_group);
|
|
}
|
|
return _group;
|
|
}
|
|
|
|
export function getShowWindField() {
|
|
return _visible;
|
|
}
|
|
|
|
export function toggleWindField(visible) {
|
|
_visible = Boolean(visible);
|
|
if (_group) {
|
|
_group.visible = Boolean(_visible && _uGrid);
|
|
}
|
|
if (_visible && !_uGrid) {
|
|
void refreshWindField({ forceReload: true }).catch(() => {});
|
|
}
|
|
return _visible;
|
|
}
|
|
|
|
export async function refreshWindField({ forceReload = false } = {}) {
|
|
if (_refreshPromise) return _refreshPromise;
|
|
_refreshPromise = (async () => {
|
|
try {
|
|
const metaRes = await fetch(WIND_META_ENDPOINT, { cache: "no-store" });
|
|
if (!metaRes.ok) return null;
|
|
const meta = await metaRes.json();
|
|
const prevVersion = _gridMeta?.version;
|
|
_gridMeta = meta;
|
|
if (!meta?.available || !meta?.grid_url) {
|
|
_uGrid = null;
|
|
_vGrid = null;
|
|
if (_group) _group.visible = false;
|
|
return null;
|
|
}
|
|
if (!forceReload && prevVersion && meta.version === prevVersion && _uGrid) {
|
|
return _gridMeta;
|
|
}
|
|
const gridRes = await fetch(meta.grid_url, { cache: "no-store" });
|
|
if (!gridRes.ok) return null;
|
|
const grid = await gridRes.json();
|
|
if (!Array.isArray(grid?.u) || !Array.isArray(grid?.v) || !grid.width || !grid.height) {
|
|
return null;
|
|
}
|
|
_gridWidth = Number(grid.width);
|
|
_gridHeight = Number(grid.height);
|
|
_latMin = Number(grid.lat_min ?? -90);
|
|
_latMax = Number(grid.lat_max ?? 90);
|
|
_lonMin = Number(grid.lon_min ?? -180);
|
|
_lonMax = Number(grid.lon_max ?? 180);
|
|
_uGrid = Float32Array.from(grid.u);
|
|
_vGrid = Float32Array.from(grid.v);
|
|
initParticleBuffers();
|
|
for (let i = 0; i < PARTICLE_COUNT; i += 1) {
|
|
spawnParticle(i);
|
|
}
|
|
// Pre-advect TRAIL_SEGMENTS steps so wind curves render cleanly from frame 1.
|
|
for (let step = 0; step < TRAIL_SEGMENTS; step += 1) {
|
|
stepParticlesAndWriteBuffers(1.0);
|
|
}
|
|
if (_group) {
|
|
_group.visible = Boolean(_visible && _uGrid);
|
|
}
|
|
return meta;
|
|
} catch {
|
|
return null;
|
|
} finally {
|
|
_refreshPromise = null;
|
|
}
|
|
})();
|
|
return _refreshPromise;
|
|
}
|
|
|
|
function stepParticlesAndWriteBuffers(dt) {
|
|
if (!_uGrid || !_particles || !_positions || !_colors) return;
|
|
|
|
for (let i = 0; i < PARTICLE_COUNT; i += 1) {
|
|
const p = _particles[i];
|
|
const sample = sampleWindAtLatLon(p.lat, p.lon);
|
|
if (!sample || p.age >= p.maxAge) {
|
|
spawnParticle(i);
|
|
writeZeroParticleSegments(i);
|
|
continue;
|
|
}
|
|
|
|
const prevLat = p.lat;
|
|
const prevLon = p.lon;
|
|
const cosLat = Math.max(0.2, Math.cos((prevLat * Math.PI) / 180));
|
|
const dLat = sample.vMs * ADVECT_SCALE * dt;
|
|
const dLon = (sample.uMs / cosLat) * ADVECT_SCALE * dt;
|
|
|
|
p.lat = prevLat + dLat;
|
|
p.lon = (((prevLon + dLon + 180) % 360) + 360) % 360 - 180;
|
|
p.age += dt;
|
|
|
|
if (p.lat < -82 || p.lat > 82 || Math.abs(dLon) > 15) {
|
|
spawnParticle(i);
|
|
writeZeroParticleSegments(i);
|
|
continue;
|
|
}
|
|
|
|
for (let s = 0; s < TRAIL_SEGMENTS; s += 1) {
|
|
p.trailX[s] = p.trailX[s + 1];
|
|
p.trailY[s] = p.trailY[s + 1];
|
|
p.trailZ[s] = p.trailZ[s + 1];
|
|
p.trailLon[s] = p.trailLon[s + 1];
|
|
}
|
|
writeSphericalXYZ(p.lat, p.lon, STREAMLINE_RADIUS, p.trailX, p.trailY, p.trailZ, TRAIL_SEGMENTS);
|
|
p.trailLon[TRAIL_SEGMENTS] = p.lon;
|
|
|
|
const lifeFade = 0.15 + 0.85 * Math.sin((p.age / p.maxAge) * Math.PI);
|
|
const [r, g, b] = speedToColorRgb(sample.speedMs);
|
|
const particleBase = i * TRAIL_SEGMENTS * 6;
|
|
|
|
for (let s = 0; s < TRAIL_SEGMENTS; s += 1) {
|
|
const lon0 = p.trailLon[s];
|
|
const lon1 = p.trailLon[s + 1];
|
|
const o = particleBase + s * 6;
|
|
|
|
if (Math.abs(lon1 - lon0) > 120) {
|
|
_positions[o] = 0;
|
|
_positions[o + 1] = 0;
|
|
_positions[o + 2] = 0;
|
|
_positions[o + 3] = 0;
|
|
_positions[o + 4] = 0;
|
|
_positions[o + 5] = 0;
|
|
_colors[o] = 0;
|
|
_colors[o + 1] = 0;
|
|
_colors[o + 2] = 0;
|
|
_colors[o + 3] = 0;
|
|
_colors[o + 4] = 0;
|
|
_colors[o + 5] = 0;
|
|
continue;
|
|
}
|
|
|
|
_positions[o] = p.trailX[s];
|
|
_positions[o + 1] = p.trailY[s];
|
|
_positions[o + 2] = p.trailZ[s];
|
|
_positions[o + 3] = p.trailX[s + 1];
|
|
_positions[o + 4] = p.trailY[s + 1];
|
|
_positions[o + 5] = p.trailZ[s + 1];
|
|
|
|
const f0 = Math.pow(s / TRAIL_SEGMENTS, 1.15) * lifeFade;
|
|
const f1 = Math.pow((s + 1) / TRAIL_SEGMENTS, 1.15) * lifeFade;
|
|
_colors[o] = r * f0;
|
|
_colors[o + 1] = g * f0;
|
|
_colors[o + 2] = b * f0;
|
|
_colors[o + 3] = r * f1;
|
|
_colors[o + 4] = g * f1;
|
|
_colors[o + 5] = b * f1;
|
|
}
|
|
}
|
|
|
|
if (_lines) {
|
|
_lines.geometry.attributes.position.needsUpdate = true;
|
|
_lines.geometry.attributes.color.needsUpdate = true;
|
|
}
|
|
}
|
|
|
|
export function updateWindField(deltaTimeMs = 16.67) {
|
|
if (!_visible || !_group || !_lines || !_uGrid || !_particles) return;
|
|
_group.visible = true;
|
|
const dt = Math.min(3.0, Math.max(0.2, deltaTimeMs / 16.67));
|
|
stepParticlesAndWriteBuffers(dt);
|
|
}
|
|
|
|
export function disposeWindField() {
|
|
if (_group && _earthObj) {
|
|
_earthObj.remove(_group);
|
|
}
|
|
_lines?.geometry?.dispose?.();
|
|
_lines?.material?.dispose?.();
|
|
_group = null;
|
|
_lines = null;
|
|
_uGrid = null;
|
|
_vGrid = null;
|
|
_particles = null;
|
|
}
|