#version 300 es
// Rezt's own: the deep-space background the glass bends: stars in three depths, and a faint warm nebula.
// With u_light (the landing page in a light system theme) it is the daytime version: warm cream paper with the same
// flowing wisps in peach and ember, and no stars.
precision highp float;

in vec2 v_uv;
out vec4 fragColor;

uniform vec2 u_res;      // size of this texture in pixels
uniform float u_unit;    // CSS pixels per texture pixel
uniform float u_scroll;
uniform float u_time;
uniform float u_motion;
uniform float u_light;

float hash21(vec2 p) {
  p = fract(p * vec2(123.34, 456.21));
  p += dot(p, p + 45.32);
  return fract(p.x * p.y);
}

float vnoise(vec2 p) {
  vec2 i = floor(p), f = fract(p);
  f = f * f * (3.0 - 2.0 * f);
  return mix(mix(hash21(i), hash21(i + vec2(1.0, 0.0)), f.x), mix(hash21(i + vec2(0.0, 1.0)), hash21(i + vec2(1.0, 1.0)), f.x), f.y);
}

float fbm(vec2 p) {
  float v = 0.0, a = 0.5;
  for (int i = 0; i < 4; i++) {
    v += a * vnoise(p);
    p = p * 2.03 + vec2(17.1, 9.2);
    a *= 0.5;
  }
  return v;
}

// One star per cell at most: a random spot, size and brightness, with a slow twinkle. Distances are in CSS pixels.
float stars(vec2 p, float cell, float density, float seed, float spread) {
  vec2 g = floor(p / cell), f = p - g * cell;
  float pick = hash21(g + seed);
  if (pick > density) return 0.0;
  vec2 spot = (vec2(hash21(g + seed + 1.7), hash21(g + seed + 9.3)) * 0.8 + 0.1) * cell;
  float size = mix(0.45, spread, pow(hash21(g + seed + 4.1), 3.0));
  float bright = 0.25 + 0.75 * pow(hash21(g + seed + 2.9), 2.2);
  float twinkle = 1.0 - 0.35 * u_motion * (0.5 + 0.5 * sin(u_time * (0.5 + 1.8 * hash21(g + seed + 6.6)) + pick * 60.0));
  vec2 d = f - spot;
  return bright * twinkle * exp(-dot(d, d) / (2.0 * size * size));
}

void main() {
  vec2 p = vec2(gl_FragCoord.x, u_res.y - gl_FragCoord.y) * u_unit;
  float t = u_time * 0.03 * u_motion;

  // Deep space: nearly black, the faintest warmth.
  vec3 c = vec3(0.006, 0.005, 0.007);

  // Ember nebula: thin, flowing wisps spread across the whole page, slowly folding over time. Ridges of a warped noise
  // field give long random filaments rather than blobs, so it stays minimal however far you scroll.
  vec2 q = vec2(p.x, p.y + u_scroll * 0.25) / 520.0;
  vec2 w = vec2(fbm(q * 0.9 + vec2(0.0, t)), fbm(q * 0.9 + vec2(5.2, 1.3) - t));
  float f = fbm(q + 1.8 * w + vec2(t * 0.6, 0.0));
  float ridge = pow(1.0 - abs(2.0 * f - 1.0), 4.0);               // thin bright veins along the field's middle
  float haze = smoothstep(0.42, 0.78, fbm(q * 0.5 - w * 0.6));     // where the veins gather into soft clouds
  c += vec3(0.85, 0.30, 0.11) * ridge * (0.20 + 0.40 * haze);
  c += vec3(0.30, 0.09, 0.04) * haze * 0.40;

  if (u_light > 0.5) {
    // Cream paper (#f4f0e8); the wisps tint it towards peach and ember instead of glowing on black.
    vec3 paper = vec3(0.957, 0.941, 0.910);
    vec3 l = paper;
    l = mix(l, vec3(0.99, 0.82, 0.70), clamp(haze * 0.42, 0.0, 1.0));
    l = mix(l, vec3(0.93, 0.50, 0.32), clamp(ridge * (0.10 + 0.30 * haze), 0.0, 1.0));
    l += (fract(sin(dot(gl_FragCoord.xy, vec2(12.9898, 78.233))) * 43758.5453) - 0.5) / 255.0;
    fragColor = vec4(l, 1.0);
    return;
  }

  // Stars in three depths: the farther a layer, the slower it moves with the page.
  float s1 = stars(vec2(p.x, p.y + u_scroll * 0.10), 64.0, 0.55, 3.1, 0.9);
  float s2 = stars(vec2(p.x + 400.0, p.y + u_scroll * 0.22), 118.0, 0.45, 11.7, 1.3);
  float s3 = stars(vec2(p.x + 900.0, p.y + u_scroll * 0.38), 260.0, 0.30, 27.3, 2.0);
  c += vec3(0.80, 0.84, 0.95) * s1 * 0.55 + vec3(1.0, 0.95, 0.88) * s2 * 0.8 + vec3(1.0, 0.90, 0.80) * s3;

  // A touch of noise keeps the dark gradients from banding.
  c += (fract(sin(dot(gl_FragCoord.xy, vec2(12.9898, 78.233))) * 43758.5453) - 0.5) / 255.0;
  fragColor = vec4(c, 1.0);
}
