Gallery

Drifting Starfield

Live parameters

Density
3
Motion
1.00
0.020
Color
1.00
1.00
GLSL source
#define DRIFT_SPEED 0.02
#define TWINKLE_SPEED 1.0
#define BG_BRIGHTNESS 1.0
#define NEBULA_STRENGTH 1.0
const vec3 BG_COLOR = vec3(0.02, 0.03, 0.07);
const vec3 NEBULA_TINT = vec3(0.3, 0.1, 0.5);
const int LAYERS = 3;

float hash(vec2 p) {
  return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);
}

float stars(vec2 uv, float density, float twinkle) {
  vec2 g = floor(uv);
  vec2 f = fract(uv);
  float h = hash(g);
  if (h < 1.0 - density) return 0.0;
  vec2 c = vec2(hash(g + 1.3), hash(g + 7.7));
  float d = length(f - c);
  float s = smoothstep(0.08, 0.0, d);
  return s * (0.6 + 0.4 * sin(iTime * twinkle + h * 30.0));
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
  uv += vec2(iTime * DRIFT_SPEED, 0.0);

  vec3 col = BG_COLOR * BG_BRIGHTNESS;
  // nebula tint
  float n = 0.0;
  vec2 q = uv * 2.0;
  for (int i = 0; i < 4; i++) {
    n += sin(q.x * 1.7 + iTime * 0.1) * cos(q.y * 1.3) * 0.25;
    q *= 1.6;
  }
  col += NEBULA_TINT * smoothstep(0.0, 1.0, n) * 0.4 * NEBULA_STRENGTH;

  if (LAYERS >= 1) col += stars(uv * 60.0, 0.04, 5.0 * TWINKLE_SPEED);
  if (LAYERS >= 2) col += stars(uv * 30.0, 0.02, 2.5 * TWINKLE_SPEED) * 1.6;
  if (LAYERS >= 3) col += stars(uv * 12.0, 0.005, 1.0 * TWINKLE_SPEED) * 2.5;
  if (LAYERS >= 4) col += stars(uv * 90.0, 0.06, 7.0 * TWINKLE_SPEED) * 0.7;
  if (LAYERS >= 5) col += stars(uv * 6.0, 0.003, 0.5 * TWINKLE_SPEED) * 3.0;

  fragColor = vec4(col, 1.0);
}