// The sky (GLSL ES 1.00, for WebGL 1), in two passes. js/sky.js fetches it, compiles it twice, sets the
// uniforms each frame and draws one triangle covering the screen in each:
// - the sky itself: the light, the clouds, the weather and the moon's halo, all soft, so drawn at reduced
//   resolution into a texture, with how much of the night's light shows through at each point (its alpha);
// - the night's sharp points, compiled with COMPOSITE defined: that texture stretched to the full screen,
//   with the moon's disc and the stars added at full resolution behind whatever covers them
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;   // a few older phones: the sky is a little coarser, but still drawn
#endif

// ---- Uniforms (set by js/sky.js) ----------------------------------------------------------------------------
uniform vec2 r;         // the size being drawn, in pixels
uniform float t;        // seconds
uniform float w;        // warmth of the light, 0..1.5
uniform vec3 glowAt;    // glow centre xy, strength
uniform vec4 tone;      // exposure, gold, violet, spread
// Time-of-day palette (from SKYCLOCK); edge0 is the page background colour
uniform vec3 zen0, low0, rose0, peach0, cloud0, edge0;
uniform vec2 night;     // stars, glow strength
uniform vec3 moonSun;   // direction of the sunlight falling on the moon (x: right, z: toward the viewer)
uniform vec3 moonAt;    // where the moon is (x in screen widths, y up the visible screen) and how far it is up
uniform float moonTurn; // 1, or -1 where the moon is seen upside down (from the southern hemisphere)
uniform float streaks;  // 0..1: long, fine streaks of high cloud take the place of the billowed clouds
uniform float overcast; // 0..1: a soft veil of cloud covers the whole sky
uniform vec4 fade;      // edge fade: top length, bottom length (fractions of height), top and bottom strength
uniform vec3 txt;       // centre of the lettering (x, y) and how much to quiet the sky behind it
uniform vec2 view;      // bottom and top of the visible screen, as fractions of the canvas height
uniform vec3 weather;   // passing weather, 0..1 each: a grey deck of cloud, the gloom of rain, snow
uniform vec2 flash;     // lightning: brightness 0..1, and where across the sky (0..1) it strikes
uniform float aurora;   // the northern lights, 0..1

const mat2 OCTAVE = mat2(1.6, 1.2, -1.2, 1.6);   // turns and enlarges each octave of noise
const float CLOUD_SCALE = 1.05;
const float MOON_RADIUS = 0.032;                 // in units of the screen's shorter side
const float STAR_GRID = 70.0;                    // cells for stars along the screen's shorter side
const float STAR_CUTOFF = 0.86;                  // the share of cells left without a star
// The passing weather's colours
const vec3 DECK_GREY_LOW = vec3(0.84, 0.80, 0.82);   // an overcast deck, low down...
const vec3 DECK_GREY_TOP = vec3(0.70, 0.70, 0.78);   // ...and overhead, in full daylight
const float GLOOM_GREY = 0.22;                       // how far rain greys the sky...
const float GLOOM_DIM = 0.06;                        // ...and dims it
const vec3 SNOW_PEARL = vec3(0.93, 0.92, 0.97);      // a snow sky's pearly white...
const float SNOW_PEARLING = 0.65;                    // ...and how far snow turns the grey to it

// ---- Noise ----------------------------------------------------------------------------------------------
// Pseudo-random 0..1 for a point
float hash(vec2 p){ return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453); }

// Pseudo-random gradient, -1..1 in x and y
vec2 hash2(vec2 p){
  p = vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)));
  return -1.0 + 2.0 * fract(sin(p) * 43758.5453123);
}

// 2D simplex noise, about -1..1
float noise(vec2 p){
  const float SKEW = 0.366025404, UNSKEW = 0.211324865;   // (sqrt(3) - 1) / 2, (3 - sqrt(3)) / 6
  vec2 i = floor(p + (p.x + p.y) * SKEW);
  vec2 a = p - i + (i.x + i.y) * UNSKEW;
  vec2 o = (a.x > a.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
  vec2 b = a - o + UNSKEW;
  vec2 c = a - 1.0 + 2.0 * UNSKEW;
  vec3 h = max(0.5 - vec3(dot(a, a), dot(b, b), dot(c, c)), 0.0);
  vec3 n = h * h * h * h * vec3(dot(a, hash2(i)), dot(b, hash2(i + o)), dot(c, hash2(i + 1.0)));
  return dot(n, vec3(70.0));
}

// Fractal noise drifting with the wind: up to 8 octaves, each finer, starting at `weight` and fainter by
// `decay` each time. Ridged, it adds up |noise| instead, for sharper, billowed edges
float fractal(vec2 v, float drift, float weight, float decay, int octaves, bool ridged){
  float total = 0.0;
  for (int i = 0; i < 8; i++){
    if (i == octaves) break;
    float n = noise(v);
    total += weight * (ridged ? abs(n) : n); v = OCTAVE * v + drift; weight *= decay;
  }
  return total;
}

float square(float x){ return x * x; }
// Heights up the canvas, 0..1, to and from heights up the visible screen, 0..1 (the canvas reaches beyond
// it on phones, under the browser's bars)
float toView(float y){ return (y - view.x) / (view.y - view.x); }
float fromView(float y){ return mix(view.x, view.y, y); }
float luma(vec3 c){ return dot(c, vec3(0.299, 0.587, 0.114)); }
// The colour of the hour, at the brightness of grey: what tinges passing weather, so a storm at sunset stays
// warm under its grey
vec3 hourTint(){ return low0 / max(luma(low0), 0.05); }

// Eased 0..1 with no visible start or end (smootherstep)
float smoother(float x){ return x * x * x * (x * (x * 6.0 - 15.0) + 10.0); }

// ---- Layers of the sky, from the back -------------------------------------------------------------------
// pos: 0..1 across the canvas, from the bottom left. aspect: the canvas measured in its shorter side

// Zenith overhead, paler toward the horizon; deepens a touch as the glow warms
vec3 skyGradient(float y){
  vec3 zenith = mix(zen0, zen0 * vec3(1.06, 0.87, 0.91), w * 0.6);
  zenith = mix(zenith, vec3(0.25, 0.27, 0.60) * (0.35 + 0.65 * (1.0 - night.x)), tone.z * 0.55);
  return mix(low0, zenith, smoothstep(0.0, 1.0, y));
}

// Where the moon is on the screen, in units of its radius, from its centre
vec2 moonPoint(vec2 unit, vec2 aspect){
  return (unit - vec2(aspect.x * moonAt.x, aspect.y * fromView(moonAt.y))) / MOON_RADIUS;
}

// One of the moon's seas: a soft rounded patch around `at`, `size` across and up, in the moon's radius
float sea(vec2 mq, vec2 at, vec2 size, float wobble){
  vec2 q = (mq - at) / size;
  return exp(-2.0 * (dot(q, q) + wobble));
}

// The moon's largest seas, the familiar dark patches of the full moon: 0..1, how dark at this point of its
// face. Neighbouring seas run together, as on the real moon, with soft shores. As seen from the north;
// upside down from the south (moonTurn)
float moonSeas(vec2 mq){
  mq *= moonTurn;
  float wobble = 0.25 * noise(mq * 2.5 + 1.7);   // so no shore is a perfect curve
  float seas = sea(mq, vec2(-0.30, 0.36), vec2(0.40, 0.34), wobble)            // Rains (Imbrium)
             + sea(mq, vec2(-0.60, 0.00), vec2(0.32, 0.55), wobble)            // Storms (Procellarum)
             + sea(mq, vec2(0.16, 0.40), vec2(0.24, 0.22), wobble)             // Serenity...
             + sea(mq, vec2(0.28, 0.22), vec2(0.18, 0.18), wobble)             // ...running into...
             + sea(mq, vec2(0.36, 0.06), vec2(0.28, 0.24), wobble)             // ...Tranquillity
             + sea(mq, vec2(0.70, 0.30), vec2(0.15, 0.14), wobble)             // Crises
             + sea(mq, vec2(0.56, -0.22), vec2(0.18, 0.22), wobble)            // Fertility
             + sea(mq, vec2(-0.18, -0.42), vec2(0.28, 0.20), wobble);          // Clouds (Nubium)
  return smoothstep(0.2, 0.9, seas);
}

// Tonight's moon, high on the visible screen: a small sphere lit from the sun's direction, so it shows
// tonight's phase. The light thins gradually toward the shadow, whose edge is soft and a little uneven
// where the low sun catches the moon's hills, and the seas show on its face. The dark side is just visible
// in earthshine, so the moon reads as a sphere in shadow rather than a cut shape. disc: how much of this
// point the moon covers
vec3 moonLight(vec2 mq, float full, out float disc){
  float md = length(mq);
  float edge = 1.5 / (min(r.x, r.y) * MOON_RADIUS);   // a pixel and a half: a clean edge at any resolution
  disc = (1.0 - smoothstep(1.0 - edge, 1.0, md)) * moonAt.z;
  if (disc <= 0.0) return vec3(0.0);   // (off the moon, its shading isn't worked out at all)
  float facing = dot(vec3(mq, sqrt(max(0.0, 1.0 - md * md))), moonSun) + 0.07 * noise(mq * 4.0 + 3.0);
  float lit = pow(smoothstep(-0.1, 0.55, facing), 0.8) * (1.0 - 0.2 * moonSeas(mq));
  lit *= 1.0 + 0.3 * (1.0 - full);   // a thin crescent shines a little brighter, so it never fades out of sight
  return disc * (vec3(1.0, 0.97, 0.90) * lit * 0.95 + vec3(0.06, 0.065, 0.09) * (1.0 - full));
}

// How fully the moon shows: like the stars it comes out as the light goes, but in full well before them,
// by blue hour, as the real moon does
float moonShows(){ return min(1.0, night.x / 0.35); }

// The moon's halo: a soft light close around it, brighter as the moon is fuller (but a little around even a
// thin crescent, none at new moon), catching the clouds that pass in front of it, and fainter wider out
float moonHalo(vec2 mq, float full){
  float md = length(mq);
  float bright = smoothstep(0.0, 0.08, full) * (0.25 + 0.75 * full * full);
  return (0.20 * exp(-max(md - 1.0, 0.0) * 0.9) + 0.05 * exp(-md * 0.15)) * bright * moonAt.z;
}

// A twinkling star in a few of the cells of a fine grid, 0..1
float starLight(vec2 unit){
  vec2 sp = unit * STAR_GRID, cell = floor(sp), fc = fract(sp);
  float h = hash(cell);
  if (h <= STAR_CUTOFF) return 0.0;
  vec2 at = vec2(hash(cell + 3.1), hash(cell + 7.7)) * 0.7 + 0.15;
  float twinkle = 0.65 + 0.35 * sin(t * (0.8 + h * 2.5) + h * 40.0);
  return (1.0 - smoothstep(0.0, 0.17, length(fc - at))) * twinkle * (h - STAR_CUTOFF) / (1.0 - STAR_CUTOFF);
}

// The night's light at a point: twinkling stars and tonight's moon, added in the second pass behind the
// clouds. Fewer stars show in the light of a full moon. unit: this point in units of the screen's shorter
// side; mq: on the moon (moonPoint); full: how much of the moon's face is lit
vec3 nightLight(vec2 pos, vec2 unit, vec2 mq, float full){
  float disc;
  vec3 moon = moonLight(mq, full, disc);
  float star = starLight(unit);
  return vec3(0.90, 0.92, 1.0) * star * (1.0 - disc) * (1.0 - 0.45 * full * moonAt.z) * 1.4 * night.x * smoothstep(0.08, 0.5, pos.y)
       + moon * moonShows();
}

// The northern lights: a faint curtain hanging high in the visible sky, brightest along its rippling lower
// edge in green and fading upward into violet, combed into fine vertical rays that slowly shift
vec3 addAurora(vec3 sky, vec2 pos, vec2 aspect){
  float y = toView(pos.y);
  float x = pos.x * aspect.x, drift = t * 0.02;
  float edge = 0.7 + 0.07 * noise(vec2(x * 0.9 + drift, 2.0)) + 0.03 * noise(vec2(x * 3.0 - drift, 5.0));
  float above = y - edge;
  float curtain = smoothstep(-0.03, 0.02, above) * exp(-max(above, 0.0) * 6.0);
  float rays = 0.55 + 0.45 * noise(vec2(x * 16.0 + 2.0 * noise(vec2(x * 2.0, drift)), drift * 3.0));
  float folds = 0.6 + 0.4 * noise(vec2(x * 0.7 - drift * 0.5, 8.0));   // brighter here, fainter there
  vec3 colour = mix(vec3(0.35, 1.0, 0.62), vec3(0.62, 0.42, 1.0), smoothstep(0.0, 0.16, above));
  return sky + colour * curtain * rays * folds * aurora * 0.6;
}

// The rose light radiates from the name, as if a low sun sat just behind it. It spreads wider than tall
// and leans downward, the way sunset light hangs over a horizon. warm: how much it colours this point
vec3 addGlow(vec3 sky, vec2 pos, vec2 aspect, out float warm){
  vec2 g = vec2((pos.x - glowAt.x) * aspect.x * 0.7, (pos.y - glowAt.y) * aspect.y * (pos.y < glowAt.y ? 0.8 : 1.25));
  float glow = exp(-dot(g, g) * max(0.3, 4.3 - w * 1.3 - tone.w * 1.8)) * glowAt.z * night.y * (1.0 - 0.15 * weather.y);
  float band = exp(-pos.y * 4.5) * (0.4 + 0.6 * glowAt.z);   // and a warm band along the horizon
  warm = clamp((glow * 0.95 + band * 0.35) * (0.72 + w * 0.55), 0.0, 1.0);
  vec3 rose = mix(rose0, rose0 * vec3(1.04, 0.93, 0.85), w * 0.7);
  rose = mix(rose, vec3(1.0, 0.80, 0.62), tone.y * 0.75);
  rose = mix(rose, vec3(1.0, 0.56, 0.80), tone.z * 0.85 * night.x);   // at night, the lavender light is pink too
  vec3 peach = mix(peach0, peach0 * vec3(1.0, 1.02, 0.83), tone.y * 0.6);
  vec3 light = mix(rose, peach, band);
  // Under a clear blue sky by day the glow brightens and warms it, as sunlight through haze does, rather than
  // tinting it pink, which over blue reads as a dull mauve. From golden hour on, the sky takes its colours
  float clearDay = smoothstep(0.5, 0.65, zen0.b - zen0.r);
  sky = mix(mix(sky, light, warm * 0.62), sky + (1.0 - sky) * light * warm * 0.5, clearDay);
  sky += vec3(0.09, 0.045, 0.05) * glow;   // a faint bloom right around the source
  return sky;
}

// In daylight, bright white clouds sit behind white lettering, so the sky is quieted (clouds thinned, sky
// deepened a little) in a soft oval around the text, the way the light naturally falls off away from the
// sun. 0..1: how much to quiet this point
float textQuiet(vec2 pos, vec2 aspect){
  vec2 tq = (pos - txt.xy) * aspect;
  float above = exp(-square(tq.y / 0.5) * 1.4);   // fades out above the name, but reaches down to the signature
  return exp(-square(tq.x / 0.66) * 1.4) * (pos.y > txt.y ? above : 1.0) * txt.z;
}

// The overcast sky behind the clouds. At the overcast dawn, a soft grey that warms through peach to amber
// where the sun lights it from just below the horizon. In overcast weather, a plain grey, a little paler
// low down and as bright as the light. The clouds then close over it
vec3 overcastDeck(float y){
  vec3 dawn = mix(mix(peach0, low0, smoothstep(0.02, 0.45, y)), zen0, smoothstep(0.4, 0.95, y));
  vec3 grey = mix(DECK_GREY_LOW, DECK_GREY_TOP, y) * clamp(luma(cloud0) / 1.1, 0.45, 1.0);
  grey = mix(grey, SNOW_PEARL * clamp(luma(cloud0), 0.45, 1.05), weather.z * SNOW_PEARLING);   // snow: pearlier
  return mix(dawn, mix(grey, mix(low0, zen0, y), 0.4), weather.x);   // tinged with the colours of the hour
}

// The grey of clouds closing into a deck, as bright as the light (a dawn's at the overcast moment, dimmer
// at night, a little brighter at noon), tinged with the hour, and pearly under snow. shade: the cloud's own
// light and shade
vec3 weatherGrey(float y, float shade){
  vec3 grey = mix(vec3(0.62, 0.60, 0.62), vec3(0.58, 0.61, 0.67), y) * (0.72 + 0.5 * shade);
  grey *= mix(1.0, clamp(luma(cloud0) / 0.92, 0.5, 1.15), weather.x);   // 0.92: the overcast moment's cloud
  grey *= mix(vec3(1.0), hourTint(), 0.25 * weather.x);
  return mix(grey, SNOW_PEARL * (0.82 + 0.22 * shade), weather.z * SNOW_PEARLING);
}

// Clouds: white where the blue sky lights them, pink where the setting sun catches them. uv: the cloud
// plane, in perspective
vec3 addClouds(vec3 sky, vec2 uv, float y, float warm, vec3 deck, float covered, float quiet, inout float through){
  float time = t * 0.007;
  float warp = fractal(uv * CLOUD_SCALE * 0.5, 0.0, 0.1, 0.4, 7, false);
  vec2 v = uv * CLOUD_SCALE - warp + time;
  // Ridged noise gives the cloud bodies their billowed structure; smooth noise decides coverage
  float billows = fractal(v, time, 0.8, 0.7, 8, true);
  float coverage = fractal(v, time, 0.7, 0.6, 8, false);
  coverage *= billows + coverage;
  // Light and shade inside the clouds
  float time2 = time * 2.0, time3 = time * 3.0;
  float light = fractal(uv * CLOUD_SCALE * 2.0 - warp + time2, time2, 0.4, 0.6, 7, false);
  light += fractal(uv * CLOUD_SCALE * 3.0 - warp + time3, time3, 0.4, 0.6, 7, true);

  float shade = clamp(0.5 + 0.3 * light, 0.0, 1.0);
  vec3 cloudLit = mix(cloud0, mix(rose0 * vec3(1.12, 1.08, 1.07), rose0 * vec3(1.17, 1.03, 0.93), w * 0.6), warm);
  cloudLit = mix(cloudLit, cloudLit * vec3(1.04, 0.96, 0.82), tone.y * warm);
  vec3 cloudCol = cloudLit * shade;
  cloudCol = mix(cloudCol, cloudCol * vec3(0.78, 0.80, 0.95), (1.0 - shade) * 0.6);   // bluish shadows

  // Overcast: the same clouds thicken into a grey deck over the sky, darker than the light behind it,
  // with brighter gaps between the billows and thinner low down, where the amber glows through
  cloudCol = mix(cloudCol, mix(weatherGrey(y, shade), deck * (0.8 + 0.3 * shade), 0.35), covered);
  float cover = 0.18 + 8.0 * coverage * billows + covered * mix(0.1, 0.6, smoothstep(0.1, 0.7, y)) + 0.2 * weather.x;
  float alpha = clamp(cover + light, 0.0, 1.0) * (1.0 - streaks);   // giving way to the streaks
  alpha *= smoothstep(0.0, 0.12, y) * 0.25 + 0.75;   // haze thins clouds at the very bottom
  alpha *= 1.0 - 0.7 * quiet;
  // A fair night is clearer: fewer, thinner clouds, so the stars show between and through them
  float clearNight = night.x * (1.0 - covered);
  alpha = clamp(alpha - 0.15 * clearNight, 0.0, 1.0) * (1.0 - 0.35 * clearNight);
  through *= 1.0 - alpha + alpha * 0.45 * (1.0 - covered);
  return mix(sky, clamp(sky * 0.45 * (1.0 - covered) + cloudCol, 0.0, 1.0), alpha);
}

// High cloud before sunset: long, fine fibres slanting across the sky, lit gold low down and salmon higher
// up, with clear sky between the streaks
vec3 addStreaks(vec3 col, vec2 uv, float y, float quiet, inout float through){
  vec2 slant = mat2(0.93, 0.37, -0.37, 0.93) * uv;          // tilted so the streaks slant down to the right
  float drift = t * 0.004;
  slant.y += 0.22 * noise(slant * vec2(0.35, 0.9) + 11.0);   // the streaks curve and fan a little, as high winds comb them
  // Broad bands decide where the streaks are...
  vec2 bv = vec2(slant.x * 0.3 + drift, slant.y * 2.2);
  float bands = 0.0, bw = 0.6;
  for (int i = 0; i < 4; i++){ bands += bw * noise(bv); bv = OCTAVE * bv + vec2(drift, 0.0); bw *= 0.5; }
  bands = smoothstep(-0.25, 0.4, bands);
  // ...and fine fibres, drawn out along the slant, give them their combed texture
  vec2 fv = vec2(slant.x * 1.6 + drift * 2.0, slant.y * 15.0);
  fv.y += 1.5 * noise(fv * vec2(0.5, 0.12));                 // fibres wander, so no two run quite parallel
  float fibres = 0.0, fw = 0.55;
  for (int i = 0; i < 5; i++){ fibres += fw * (1.0 - abs(noise(fv))); fv = fv * vec2(1.7, 2.0) + 3.7; fw *= 0.55; }
  // The sun just below the horizon warms the bottom of the sky gold
  col = mix(col, peach0, streaks * 0.5 * exp(-y * 6.0));
  float wisp = bands * smoothstep(0.45, 1.05, fibres) * (1.0 - 0.6 * quiet);
  // Gold near the horizon, salmon above, a dusky rose highest up; darker where the fibres are thin
  vec3 wispCol = mix(peach0 * vec3(1.05, 1.0, 0.85), cloud0, smoothstep(0.05, 0.55, y));
  wispCol = mix(wispCol, cloud0 * vec3(0.82, 0.78, 0.92), smoothstep(0.6, 1.0, y));
  wispCol *= 0.82 + 0.25 * fibres;
  through *= (1.0 - streaks * 0.5 * exp(-y * 6.0)) * (1.0 - clamp(wisp, 0.0, 1.0) * streaks);
  return mix(col, wispCol, clamp(wisp, 0.0, 1.0) * streaks);
}

// Rain: the light softens and greys under the clouds, never gloomy
vec3 addGloom(vec3 col){
  col = mix(col, vec3(luma(col)) * mix(vec3(0.96, 0.98, 1.03), hourTint(), 0.35), weather.y * GLOOM_GREY);
  return col * (1.0 - GLOOM_DIM * weather.y);
}

// Lightning: the clouds glow softly from within, far off toward one side of the sky
vec3 addFlash(vec3 col, vec2 pos, vec2 uv, vec2 aspect){
  float near = exp(-square((pos.x - flash.y) * aspect.x * 1.6)) * (0.6 + 0.4 * (1.0 - pos.y));
  float lit = 0.35 + 0.65 * near * (0.7 + 0.3 * noise(uv * 1.3 + 5.0));
  return col + vec3(0.62, 0.65, 0.78) * flash.x * lit * 0.35;
}

// Melt into the browser's own bars: Safari fills the strips above and below the page with one flat colour
// (the page background, set from the clock), so the sky eases into exactly that colour, over a
// long eased ramp so the change of colour is never noticed as a band
vec3 fadeToEdge(vec3 col, float y, inout float through){
  float top = fade.x > 0.0 ? smoothstep(1.0 - fade.x, 1.0, y) : 0.0;
  float bottom = fade.y > 0.0 ? 1.0 - smoothstep(0.0, fade.y, y) : 0.0;
  // On computers (no fade at the bottom) the top is only a tint, so it follows passing weather; on phones
  // it must match the bars exactly
  vec3 edge = fade.w > 0.0 ? edge0 : mix(edge0, addGloom(overcastDeck(1.0)), weather.x);
  float toEdge = max(smoother(top) * fade.z, smoother(bottom) * fade.w);
  through *= 1.0 - toEdge;
  return mix(col, edge, toEdge);
}

// The first pass. `through`: how much of the night's light (the moon's disc and the stars, drawn in the
// second pass) shows through at this point; each layer in front of it takes its share, from the deck of
// cloud to the fade into the browser's bars
#ifndef COMPOSITE
void main(){
  vec2 pos = gl_FragCoord.xy / r;
  // Measure in units of the screen's shorter side, so a tall phone shows the same sky as a wide laptop
  vec2 aspect = r / min(r.x, r.y);
  // Mild perspective: clouds flatten and shrink toward the horizon at the bottom
  float depth = 1.0 / (0.55 + pos.y * 0.9);
  vec2 uv = vec2((pos.x - 0.5) * aspect.x * depth, depth * 1.6 * sqrt(aspect.y));
  float through = 1.0;

  vec3 sky = skyGradient(pos.y);
  if (aurora > 0.001) sky = addAurora(sky, pos, aspect);
  float quiet = textQuiet(pos, aspect);
  vec3 deck = overcastDeck(pos.y);
  float covered = max(overcast, weather.x);
  float decked = max(overcast, (0.85 - 0.25 * weather.z) * weather.x);   // snow keeps some of the hour's colour
  sky = mix(sky, deck, decked);
  through *= 1.0 - decked;
  float warm;
  sky = addGlow(sky, pos, aspect, warm);
  through *= 1.0 - warm * 0.62;

  // The billowed clouds aren't drawn at all while the high streaks fully take their place
  vec3 col = sky;
  if (streaks < 0.999) col = addClouds(sky, uv, pos.y, warm, deck, covered, quiet, through);
  if (streaks > 0.001) col = addStreaks(col, uv, pos.y, quiet, through);
  if (weather.y > 0.001){ col = addGloom(col); through *= 1.0 - GLOOM_DIM * weather.y; }
  if (flash.x > 0.001) col = addFlash(col, pos, uv, aspect);
  // The moon's halo over the clouds, dimmed behind a deck of cloud
  if (night.x > 0.001){
    vec2 mq = moonPoint(gl_FragCoord.xy / min(r.x, r.y), aspect);
    col += vec3(0.62, 0.66, 0.86) * moonHalo(mq, 0.5 + 0.5 * moonSun.z) * moonShows() * (1.0 - 0.7 * weather.x);
  }
  col = mix(col, col * vec3(0.80, 0.78, 0.80), quiet * 0.75);   // a touch warm, so it reads as shade, not grey
  through *= 1.0 - 0.15 * quiet;

  col = mix(col, col * vec3(1.12, 1.01, 0.76), tone.y * 0.8);  // golden light honeys the whole sky
  float light = tone.x * (1.0 - 0.12 * pow(length(pos - 0.5) * 1.3, 2.2));   // exposure, and a soft vignette
  col *= light;
  through *= light;
  col += (hash(gl_FragCoord.xy + fract(t)) - 0.5) * 0.008;  // grain keeps the gradient from banding
  col = fadeToEdge(col, pos.y, through);
  gl_FragColor = vec4(col, clamp(through, 0.0, 1.0));
}

#else
// The second pass: the soft sky stretched to the full screen, and the night's sharp points added behind
// whatever covers them
uniform sampler2D soft;   // the first pass

void main(){
  vec2 pos = gl_FragCoord.xy / r;
  vec4 sky = texture2D(soft, pos);
  vec3 col = sky.rgb;
  if (night.x > 0.001 && sky.a > 0.002){
    vec2 aspect = r / min(r.x, r.y), unit = gl_FragCoord.xy / min(r.x, r.y);
    col += nightLight(pos, unit, moonPoint(unit, aspect), 0.5 + 0.5 * moonSun.z) * sky.a;
  }
  gl_FragColor = vec4(col, 1.0);
}
#endif
