Night sky

glint · Examples · Tutorials · Gallery

Stars wheeling slowly round the pole star, the Milky Way made from layered noise, a shooting star now and then, and the hills in silhouette.

Night sky: a GLSL shader

An example in glint, a free live shader editor in the browser: the GLSL code floats over the running picture and recompiles as you type.

The code

One pass, in GLSL.

fragment shader

#version 300 es
precision highp float;

uniform float time;
uniform vec2 resolution;

in vec2 v_texcoord;
out vec4 fragColor;

float hash(float n) { return fract(sin(n * 127.1) * 43758.5453); }
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
vec2 hash2(float n) { return fract(sin(vec2(n * 127.1, n * 269.5)) * 43758.5453); }
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, s, -s, c); }
vec3 pal(float t) { return 0.5 + 0.5 * cos(6.28318 * (t + vec3(0.0, 0.33, 0.67))); }

float noise(vec2 p) {
    vec2 i = floor(p), f = fract(p);
    f = f * f * (3.0 - 2.0 * f);
    return mix(mix(hash(i), hash(i + vec2(1, 0)), f.x), mix(hash(i + vec2(0, 1)), hash(i + 1.0), f.x), f.y);
}
float fbm(vec2 p) { float v = 0.0, a = 0.5; for (int i = 0; i < 6; i++) { v += a * noise(p); p *= 2.03; a *= 0.5; } return v; }

float stars(vec2 p, float scale) {
    vec2 g = p * scale;
    vec2 id = floor(g), f = fract(g) - 0.5;
    vec2 at = (hash2(hash(id) * 99.0) - 0.5) * 0.7;
    float b = pow(hash(id + 5.0), 6.0) * 1.6;
    return b * smoothstep(0.08, 0.0, length(f - at)) * (0.7 + 0.3 * sin(time * 3.0 + hash(id) * 50.0));
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    // The sky turns round a point above the top of the screen.
    vec2 s = rot(time * 0.01) * (uv - vec2(0.3, 0.6));
    vec3 col = mix(vec3(0.06, 0.07, 0.16), vec3(0.0, 0.0, 0.03), uv.y + 0.5);
    // Milky Way: a band of noise, with dark dust lanes.
    float band = exp(-pow(dot(s, normalize(vec2(1.0, 0.6))) * 1.8, 2.0));
    float mw = fbm(s * 4.0) * band;
    col += vec3(0.6, 0.55, 0.75) * mw * 1.1 - vec3(0.2) * smoothstep(0.55, 0.75, fbm(s * 7.0 + 3.0)) * band * 0.4;
    col += stars(s, 60.0) + stars(s + 3.0, 120.0) * 0.7 + stars(s + 7.0, 30.0) * vec3(1.0, 0.8, 0.7) + stars(s + 11.0, 200.0) * band * 0.8;
    // A shooting star every few seconds.
    float k = floor(time / 4.0), ft = fract(time / 4.0);
    vec2 a = (hash2(k) - vec2(0.3, -0.1)) * vec2(1.2, 0.4), dir = normalize(vec2(-1.0, -0.4));
    vec2 head = a + dir * ft * 1.5;
    vec2 rel = uv - head;
    float along = dot(rel, -dir), across = length(rel + dir * along);
    col += vec3(1.0, 0.95, 0.8) * smoothstep(0.003, 0.0, across) * step(0.0, along) * exp(-along * 12.0) * step(ft, 0.35);
    // Hills.
    float hill = -0.22 + fbm(vec2(uv.x * 2.0, 0.0)) * 0.18;
    col = mix(col, vec3(0.005, 0.008, 0.012), step(uv.y, hill));
    fragColor = vec4(col, 1.0);
}

More More GLSL examples

Circuit board · Snowfall · Ink in water · Infinite spheres · Dithered retro · Chrome balls · Stained glass · DNA helix