Files
TREK/client/src/components/AtlasGlobe/globeShaders.ts
T
Maurice ce2b521ae7 feat(atlas): a neutral night sky and steadier globe details
- The globe, sky, glass and medals use black and neutral greys instead of
  blue tones; the atmosphere glow is a quarter of its former strength.
- No shooting stars on load: the visited set is compared only once the
  Atlas has loaded. Now and then a shooting star crosses on its own.
- The stars stand still while the Earth turns, and three constellations
  (Big Dipper, Orion, Cassiopeia) are drawn from their stars' real places,
  sized and tinted by brightness.
- Wishlist stripes are slices through the globe, so they stay on the land
  as it turns.
- Country names keep their own element while shown and hold their place
  against a near equal neighbour, so they no longer jump or flicker.
- The flat map fades into the sky towards its cut edges at the poles, and
  the view's latitude is held where those edges stay off screen.
2026-11-29 09:36:14 +01:00

429 lines
15 KiB
TypeScript

/**
* GLSL for the Atlas globe. PROJECT is the GPU twin of projectPoint() in
* globeMath.ts; a change to one belongs in the other.
*/
/**
* The flat map has edges at the poles, where the sphere was cut open. While the
* globe unrolls, land, sea and lines fade into the sky towards them, so the map
* ends in a haze instead of a hard rim. Needs u_t.
*/
const POLAR = /* glsl */ `
float polarFade(float lat) { return 1.0 - u_t * smoothstep(1.36, 1.5708, abs(lat)); }
`;
const PROJECT = /* glsl */ `
uniform vec2 u_center; // centre lon, lat in radians
uniform float u_t; // 0 globe .. 1 flat map
uniform float u_scale; // device px per radian
uniform vec2 u_origin; // device px of the centre
uniform vec2 u_viewport; // device px
const float PI = 3.14159265;
float wrapPi(float a) { return a - 2.0 * PI * floor((a + PI) / (2.0 * PI)); }
// xy: screen px, z: visibility (> 0 visible), w: shade
vec4 project(vec2 ll, vec2 ref) {
float k = 1.0 - u_t * 0.9995;
float phiR = u_center.y * (1.0 - u_t);
float cphi = cos(phiR), sphi = sin(phiR);
float dlr = wrapPi(ref.x - u_center.x);
float dl = dlr + wrapPi(ll.x - u_center.x - dlr);
float cl = cos(ll.y);
vec3 p = vec3(cl * sin(dl), sin(ll.y), cl * cos(dl));
float qy = p.y * cphi - p.z * sphi;
float qz = p.y * sphi + p.z * cphi;
float lam = atan(p.x, qz);
float phi = asin(clamp(qy, -1.0, 1.0));
float cr = cos(ref.y);
float lamR = atan(cr * sin(dlr), sin(ref.y) * sphi + cr * cos(dlr) * cphi);
lam = lamR + wrapPi(lam - lamR);
// Flat, longitude is squeezed by the cosine of the centre latitude, so shapes keep
// their proportions where the user looks instead of widening towards the poles.
float xs = mix(1.0, max(0.3, cos(u_center.y)), u_t);
float x = sin(k * lam) / k * (cos(phi) * (1.0 - u_t) + u_t) * xs;
float y = sin(phi) * (1.0 - u_t) + phi * u_t;
vec2 s = u_origin + vec2(x, -(y - u_center.y * u_t)) * u_scale;
float c = acos(clamp(cos(phi) * cos(lam), -1.0, 1.0));
float lim = min(PI, 0.5 * PI / k);
float vis = abs(lam) <= PI ? lim - c : -1.0;
float shade = max(0.0, cos(c)) * (1.0 - u_t) + u_t;
return vec4(s, vis, shade);
}
vec4 toClip(vec2 s) {
return vec4(s.x / u_viewport.x * 2.0 - 1.0, 1.0 - s.y / u_viewport.y * 2.0, 0.0, 1.0);
}
`;
export const FILL_VS = /* glsl */ `#version 300 es
precision highp float;
${PROJECT}
in vec2 a_ll; // radians
in vec2 a_ref; // radians
in float a_fid;
out float v_vis;
out float v_shade;
out vec2 v_ll;
flat out int v_fid;
void main() {
vec4 p = project(a_ll, a_ref);
v_vis = p.z;
v_shade = p.w;
v_ll = a_ll;
v_fid = int(a_fid + 0.5);
gl_Position = toClip(p.xy);
}
`;
export const FILL_FS = /* glsl */ `#version 300 es
precision highp float;
precision highp usampler2D;
in float v_vis;
in float v_shade;
in vec2 v_ll;
flat in int v_fid;
uniform sampler2D u_status; // rgb colour, a strength
uniform sampler2D u_fx; // r hatch, g reveal order, b outline flag
uniform int u_mode; // 0 land, 1 ocean, 2 region layer
uniform vec3 u_land;
uniform vec3 u_ocean;
uniform vec3 u_oceanDeep;
uniform float u_layerAlpha;
uniform float u_time;
uniform float u_revealStart;
uniform int u_hover;
uniform int u_selected;
uniform int u_inkId;
uniform vec2 u_inkLL;
uniform float u_inkR;
uniform float u_dpr;
uniform float u_scale; // device px per radian, as in PROJECT
uniform float u_t;
uniform float u_dimBase;
uniform float u_regionFade;
uniform vec3 u_sun; // unit vector to the sun, earth-fixed
uniform float u_night; // how strongly the night side darkens
uniform float u_shadeMin; // brightness at the limb (light mode keeps it high)
uniform vec4 u_hoverTint; // rgb + strength; selection uses 1.8x the strength
out vec4 outColor;
vec4 fetch(sampler2D tex, int id) { return texelFetch(tex, ivec2(id % 256, id / 256), 0); }
${POLAR}
void main() {
if (v_vis < 0.0) discard;
float edge = smoothstep(0.0, 0.004, v_vis);
float light = mix(u_shadeMin, 1.0, pow(v_shade, 0.65));
vec3 nrm = vec3(cos(v_ll.y) * cos(v_ll.x), cos(v_ll.y) * sin(v_ll.x), sin(v_ll.y));
float sunDot = dot(nrm, u_sun);
float day = smoothstep(-0.12, 0.1, sunDot);
float nightMul = mix(1.0 - u_night, 1.0, day);
if (u_mode == 1) {
vec3 col = mix(u_oceanDeep, u_ocean, pow(v_shade, 0.9));
col *= mix(1.0 - u_night * 0.7, 1.0, day);
// A faint glint where the sun stands over the sea.
col += vec3(0.45) * pow(max(0.0, sunDot), 40.0) * 0.25 * (1.0 - u_t) * v_shade;
outColor = vec4(col, edge * polarFade(v_ll.y));
return;
}
vec4 st = fetch(u_status, v_fid);
vec4 fx = fetch(u_fx, v_fid);
float reveal = clamp((u_time - u_revealStart - fx.g * 1.6) / 0.45, 0.0, 1.0);
float a = st.a * reveal;
if (v_fid == u_inkId) {
vec3 p1 = vec3(cos(v_ll.y) * cos(v_ll.x), cos(v_ll.y) * sin(v_ll.x), sin(v_ll.y));
vec3 p2 = vec3(cos(u_inkLL.y) * cos(u_inkLL.x), cos(u_inkLL.y) * sin(u_inkLL.x), sin(u_inkLL.y));
float d = acos(clamp(dot(p1, p2), -1.0, 1.0));
a *= 1.0 - smoothstep(u_inkR - 0.01, u_inkR, d);
}
// A country whose regions are on screen steps back so its regions read.
if (u_mode == 0 && fx.b > 0.5) a *= 1.0 - 0.32 * u_regionFade;
vec3 base = u_land * (u_mode == 2 ? 1.0 : u_dimBase);
vec3 col = mix(base, st.rgb, a);
if (fx.r > 0.5) {
// The stripes are slices through the globe itself, so they stay on the land
// while it turns; their spacing steps with the zoom to stay about 7 px.
vec3 p3 = vec3(cos(v_ll.y) * cos(v_ll.x), cos(v_ll.y) * sin(v_ll.x), sin(v_ll.y));
float per = exp2(floor(log2(7.0 * u_dpr / u_scale)));
float sl = dot(p3, vec3(0.6, 0.6, 0.53)) / per;
float w = max(fwidth(sl), 1e-4);
float h = 1.0 - smoothstep(0.115 - w * 0.5, 0.115 + w * 0.5, abs(fract(sl) - 0.115));
col = mix(col, st.rgb, h * 0.9 * reveal);
a = max(a, h * 0.9 * reveal);
}
if (v_fid == u_selected) col = mix(col, u_hoverTint.rgb, u_hoverTint.a * 1.8);
else if (v_fid == u_hover) col = mix(col, u_hoverTint.rgb, u_hoverTint.a);
col *= light;
// Night darkens the land; what you marked keeps most of its colour, like lights on.
col *= mix(nightMul, 1.0, a * 0.75);
float alpha = edge * polarFade(v_ll.y);
if (u_mode == 2) alpha *= u_layerAlpha * max(a, (v_fid == u_hover || v_fid == u_selected) ? 0.25 : 0.0);
outColor = vec4(col, alpha);
}
`;
export const PICK_FS = /* glsl */ `#version 300 es
precision highp float;
in float v_vis;
in float v_shade;
in vec2 v_ll;
flat in int v_fid;
uniform int u_idBase;
out vec4 outColor;
void main() {
if (v_vis < 0.0) discard;
int id = v_fid + 1;
outColor = vec4(float(id & 255) / 255.0, float((id >> 8) & 255) / 255.0, float((id >> 16) & 255) / 255.0, 1.0);
}
`;
/** Instanced screen-space lines: borders, coasts, outlines and flight arcs. */
export const LINE_VS = /* glsl */ `#version 300 es
precision highp float;
${PROJECT}
in vec2 a_corner; // x: 0 start .. 1 end, y: -1 .. 1 across
in vec2 a_a; // radians
in vec2 a_b; // radians
in vec2 a_h; // lift of a and b (arcs), share of the radius
in vec2 a_s; // position along the whole line, 0..1, at a and b
uniform float u_width; // device px
uniform float u_lift; // 1 lifts arcs above the surface
out float v_vis;
out float v_across;
out float v_s;
out float v_shade;
out float v_lat;
vec2 lifted(vec4 p, float h) {
vec2 d = p.xy - u_origin;
vec2 globe = u_origin + d * (1.0 + h);
vec2 flat2 = p.xy - vec2(0.0, h * u_scale * 0.35);
return mix(globe, flat2, u_t);
}
void main() {
vec2 mid = (a_a + a_b) * 0.5;
if (abs(a_a.x - a_b.x) > PI) mid.x = a_a.x;
vec4 pa = project(a_a, mid);
vec4 pb = project(a_b, mid);
vec2 sa = u_lift > 0.5 ? lifted(pa, a_h.x) : pa.xy;
vec2 sb = u_lift > 0.5 ? lifted(pb, a_h.y) : pb.xy;
vec2 dir = sb - sa;
float len = length(dir);
dir = len > 1e-5 ? dir / len : vec2(1.0, 0.0);
vec2 n = vec2(-dir.y, dir.x);
vec2 s = mix(sa, sb, a_corner.x) + n * a_corner.y * (u_width * 0.5 + 1.0) + dir * (a_corner.x * 2.0 - 1.0) * 0.5;
float hv = mix(a_h.x, a_h.y, a_corner.x);
v_vis = mix(pa.z, pb.z, a_corner.x) + (u_lift > 0.5 ? hv * 0.6 : 0.0);
v_across = a_corner.y * (u_width * 0.5 + 1.0);
v_s = mix(a_s.x, a_s.y, a_corner.x);
v_shade = mix(pa.w, pb.w, a_corner.x);
v_lat = mix(a_a.y, a_b.y, a_corner.x);
gl_Position = toClip(s);
}
`;
export const LINE_FS = /* glsl */ `#version 300 es
precision highp float;
in float v_vis;
in float v_across;
in float v_s;
in float v_shade;
in float v_lat;
uniform vec4 u_color;
uniform float u_width;
uniform float u_t;
uniform float u_progress; // draws the line up to this share (arcs drawing in)
uniform float u_glow;
out vec4 outColor;
${POLAR}
void main() {
if (v_vis < 0.0 || v_s > u_progress) discard;
float d = abs(v_across);
float core = 1.0 - smoothstep(u_width * 0.5 - 0.5, u_width * 0.5 + 0.5, d);
float a = core;
if (u_glow > 0.0) a = max(core, exp(-d * d / (u_width * u_width * 1.2)) * u_glow);
float limb = smoothstep(0.0, 0.03, v_vis);
outColor = vec4(u_color.rgb, u_color.a * a * limb * mix(0.55, 1.0, v_shade) * polarFade(v_lat));
}
`;
/** Full-screen quad for the backdrop: deep space, stars, atmosphere. */
export const SKY_VS = /* glsl */ `#version 300 es
precision highp float;
in vec2 a_pos;
out vec2 v_uv;
void main() {
v_uv = a_pos * 0.5 + 0.5;
gl_Position = vec4(a_pos, 0.0, 1.0);
}
`;
export const SKY_FS = /* glsl */ `#version 300 es
precision highp float;
in vec2 v_uv;
uniform vec2 u_viewport; // device px
uniform vec2 u_globe; // device px of the globe centre
uniform float u_radius; // device px
uniform float u_t;
uniform float u_time;
uniform vec3 u_space0;
uniform vec3 u_space1;
uniform vec3 u_glow;
uniform float u_dark;
out vec4 outColor;
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
// Three constellations from their stars' real places (a gnomonic view around
// each figure's middle, north up): the Big Dipper, Orion and Cassiopeia. Each
// star is x, y in units of the view's height and its magnitude.
const vec3 CST[20] = vec3[20](
vec3(0.1807, 0.0977, 1.8),
vec3(0.21, 0.0111, 2.4),
vec3(0.0945, -0.0566, 2.4),
vec3(0.0403, -0.0038, 3.3),
vec3(-0.0494, -0.0169, 1.8),
vec3(-0.1232, -0.0213, 2.2),
vec3(-0.21, -0.0977, 1.9),
vec3(-0.0874, 0.1263, 0.5),
vec3(0.0415, 0.1075, 1.6),
vec3(-0.0255, -0.0354, 1.8),
vec3(-0.0061, -0.0226, 1.7),
vec3(0.0121, -0.0072, 2.2),
vec3(-0.0557, -0.17, 2.1),
vec3(0.0874, -0.1445, 0.1),
vec3(-0.0015, 0.17, 3.4),
vec3(0.13, -0.0137, 2.3),
vec3(0.0514, -0.0792, 2.2),
vec3(0.0035, 0.0053, 2.2),
vec3(-0.0711, -0.0018, 2.7),
vec3(-0.13, 0.0792, 3.4)
);
// Their colour: warm for the cool giants (Betelgeuse, Dubhe, Schedar), blue white for the hot ones.
const vec3 CST_TINT[20] = vec3[20](
vec3(1.0, 0.9, 0.78),
vec3(0.94, 0.96, 1.0),
vec3(0.94, 0.96, 1.0),
vec3(0.95, 0.97, 1.0),
vec3(0.95, 0.97, 1.0),
vec3(0.95, 0.97, 1.0),
vec3(0.9, 0.94, 1.0),
vec3(1.0, 0.78, 0.6),
vec3(0.9, 0.94, 1.0),
vec3(0.9, 0.94, 1.0),
vec3(0.9, 0.94, 1.0),
vec3(0.9, 0.94, 1.0),
vec3(0.9, 0.94, 1.0),
vec3(0.86, 0.92, 1.0),
vec3(0.92, 0.95, 1.0),
vec3(1.0, 0.97, 0.9),
vec3(1.0, 0.88, 0.74),
vec3(0.9, 0.94, 1.0),
vec3(0.96, 0.97, 1.0),
vec3(0.9, 0.94, 1.0)
);
const ivec2 CSL[19] = ivec2[19](
ivec2(0, 1), ivec2(1, 2), ivec2(2, 3), ivec2(3, 0), ivec2(3, 4), ivec2(4, 5),
ivec2(5, 6), ivec2(7, 14), ivec2(14, 8), ivec2(7, 9), ivec2(8, 11), ivec2(9, 10),
ivec2(10, 11), ivec2(9, 12), ivec2(11, 13), ivec2(15, 16), ivec2(16, 17), ivec2(17, 18),
ivec2(18, 19)
);
/** Where a constellation's star stands on the screen, in device px. */
vec2 cstAt(int i) {
float h = u_viewport.y;
float w = u_viewport.x / h;
// px runs downwards from the top, the figures are drawn upwards.
vec2 at = i < 7 ? vec2(w - 0.36, 0.2) : i < 15 ? vec2(0.24, 0.74) : vec2(0.2, 0.2);
return (at + vec2(CST[i].x, -CST[i].y)) * h;
}
/** The share of a hairline between two stars at p, with a gap around each star. */
float cstLine(vec2 p, vec2 a, vec2 b, float gap, float width) {
vec2 ba = b - a;
float len = length(ba);
float h = dot(p - a, ba) / (len * len);
float g = gap / len;
if (h < g || h > 1.0 - g) return 0.0;
float d = length(p - a - ba * h);
// The line thins out towards its ends, so it reads as drawn, not as a wire.
float ends = smoothstep(g, g + 0.12, h) * smoothstep(1.0 - g, 1.0 - g - 0.12, h);
return (1.0 - smoothstep(width * 0.5, width, d)) * ends;
}
/** The figures: hairlines first, then their stars, sized and tinted by brightness, each with a soft halo. */
vec3 constellations(vec2 px) {
float unit = max(1.0, u_viewport.y / 1000.0);
float line = 0.0;
for (int i = 0; i < 19; i++) {
line = max(line, cstLine(px, cstAt(CSL[i].x), cstAt(CSL[i].y), 6.0 * unit, 1.1 * unit));
}
vec3 col = vec3(0.86, 0.88, 0.92) * line * 0.11;
for (int i = 0; i < 20; i++) {
float d = length(px - cstAt(i));
// Magnitude 0 is the brightest here, 3.5 the faintest that still shows.
float bright = clamp(1.0 - CST[i].z / 4.2, 0.18, 1.0);
float r = mix(0.7, 2.1, bright) * unit;
float tw = 0.86 + 0.14 * sin(u_time * (0.7 + float(i) * 0.13) + float(i) * 1.7);
float core = 1.0 - smoothstep(r * 0.4, r, d);
float halo = exp(-d / (r * 2.2)) * 0.35 * bright;
col += CST_TINT[i] * (core * bright + halo) * tw;
}
return col;
}
float stars(vec2 px, float cell, float density, float size) {
vec2 g = floor(px / cell);
vec2 f = fract(px / cell);
float h = hash(g);
if (h > density) return 0.0;
vec2 c = vec2(hash(g + 3.1), hash(g + 7.7)) * 0.8 + 0.1;
float d = length(f - c) * cell;
float tw = 0.65 + 0.35 * sin(u_time * (0.6 + h * 2.0) + h * 40.0);
return smoothstep(size, 0.0, d) * tw * (0.4 + 0.6 * hash(g + 1.3));
}
void main() {
vec2 px = v_uv * u_viewport;
px.y = u_viewport.y - px.y;
float r = length(px - u_globe);
vec3 col = mix(u_space1, u_space0, smoothstep(0.0, max(u_viewport.x, u_viewport.y) * 0.8, r));
float sky = (1.0 - u_t * 0.9) * u_dark;
// Milky way: a soft band across the sky. The stars are endlessly far away, so
// they stand still while the Earth turns in front of them.
vec2 q = px / u_viewport.y;
float band = exp(-pow((q.y - 0.35 - 0.45 * q.x + 0.25) * 3.2, 2.0));
float dust = hash(floor(px * 0.5)) * 0.5 + 0.5;
col += vec3(0.27, 0.27, 0.28) * band * 0.10 * sky * dust;
vec2 sp = px;
float s = stars(sp, 38.0, 0.20, 1.6) + stars(sp * 1.7 + 13.0, 31.0, 0.12, 1.1) * 0.7 + band * stars(sp * 2.3 + 5.0, 17.0, 0.35, 0.9) * 0.8;
col += vec3(0.93, 0.93, 0.94) * s * sky;
col += constellations(px) * sky;
// Atmosphere: a glow hugging the globe, gone once the map lies flat.
float atm = (1.0 - smoothstep(0.0, 0.7, u_t));
float outside = smoothstep(u_radius * 0.985, u_radius * 1.0, r);
float glow = exp(-max(0.0, r - u_radius) / (u_radius * 0.075)) * outside;
col += u_glow * glow * 0.55 * atm;
outColor = vec4(col, 1.0);
}
`;
/** A thin bright rim on the limb, drawn over the globe. */
export const RIM_FS = /* glsl */ `#version 300 es
precision highp float;
in vec2 v_uv;
uniform vec2 u_viewport;
uniform vec2 u_globe;
uniform float u_radius;
uniform float u_t;
uniform vec3 u_glow;
out vec4 outColor;
void main() {
vec2 px = v_uv * u_viewport;
px.y = u_viewport.y - px.y;
float r = length(px - u_globe);
float inner = smoothstep(u_radius * 0.82, u_radius, r) * (1.0 - smoothstep(u_radius, u_radius * 1.004, r));
float a = pow(inner, 3.0) * 0.55 * (1.0 - smoothstep(0.0, 0.5, u_t));
outColor = vec4(u_glow, a);
}
`;