Volumetric clouds

glint · Examples · Tutorials · Gallery

Raymarched 3D noise clouds lit by a low sun: density marching with light sampling towards the sun and Henyey–Greenstein scattering.

Volumetric clouds: 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;
uniform vec2 mouse;
uniform vec3 spectrum;
uniform vec3 spectrumIntegrated;
uniform float beat;
uniform float coverage;
uniform vec3 sunColor;

in vec2 v_texcoord;
out vec4 fragColor;

float hash(vec3 p) { p = fract(p * 0.3183099 + 0.1); p *= 17.0; return fract(p.x * p.y * p.z * (p.x + p.y + p.z)); }
float noise(vec3 x) {
    vec3 i = floor(x), f = fract(x);
    f = f * f * (3.0 - 2.0 * f);
    return mix(mix(mix(hash(i), hash(i + vec3(1, 0, 0)), f.x), mix(hash(i + vec3(0, 1, 0)), hash(i + vec3(1, 1, 0)), f.x), f.y),
               mix(mix(hash(i + vec3(0, 0, 1)), hash(i + vec3(1, 0, 1)), f.x), mix(hash(i + vec3(0, 1, 1)), hash(i + vec3(1, 1, 1)), f.x), f.y), f.z);
}
float fbm(vec3 p) {
    float v = 0.0, a = 0.5;
    for (int i = 0; i < 5; i++) { v += a * noise(p); p = p * 2.03 + vec3(0.1, 0.2, 0.3); a *= 0.5; }
    return v;
}
// Cloud layer between y = 1 and y = 3, drifting with the wind.
float density(vec3 p) {
    float layer = smoothstep(1.0, 1.6, p.y) * smoothstep(3.0, 2.2, p.y);
    vec3 q = p * 0.6 + vec3(time * 0.15, 0.0, time * 0.05);
    return clamp((fbm(q) - (1.0 - coverage)) * 3.0 * layer, 0.0, 1.0);
}
float hg(float c, float g) { return (1.0 - g * g) / (4.0 * 3.14159 * pow(1.0 + g * g - 2.0 * g * c, 1.5)); }

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    vec3 ro = vec3(0.0, 0.2, 0.0);
    float yaw = (mouse.x - 0.5) * 3.0, pitch = (mouse.y - 0.3) * 1.2 + 0.25;
    vec3 fw = normalize(vec3(sin(yaw), pitch, cos(yaw)));
    vec3 rt = normalize(cross(fw, vec3(0, 1, 0))), up = cross(rt, fw);
    vec3 rd = normalize(uv.x * rt + uv.y * up + 1.4 * fw);
    vec3 sun = normalize(vec3(0.3, 0.15, 1.0));
    float mu = dot(rd, sun);
    vec3 sky = mix(vec3(0.75, 0.55, 0.45), vec3(0.2, 0.35, 0.7), clamp(rd.y * 2.0, 0.0, 1.0));
    sky += sunColor * pow(max(mu, 0.0), 300.0) * 4.0 + sunColor * pow(max(mu, 0.0), 8.0) * 0.3;
    vec3 col = sky;
    if (rd.y > 0.0) {
        // March through the slab, front to back.
        float t0 = (1.0 - ro.y) / rd.y, t1 = (3.0 - ro.y) / rd.y;
        float t = t0, dt = (t1 - t0) / 48.0;
        float T = 1.0;
        vec3 acc = vec3(0.0);
        float phase = mix(hg(mu, 0.6), hg(mu, -0.2), 0.3);
        for (int i = 0; i < 48; i++) {
            vec3 p = ro + rd * (t + dt * fract(sin(dot(uv, vec2(12.9, 78.2))) * 437.5));
            float d = density(p);
            if (d > 0.01) {
                // Light reaching this point: a few steps towards the sun.
                float ld = 0.0;
                for (int j = 1; j <= 4; j++) ld += density(p + sun * 0.25 * float(j));
                float light = exp(-ld * 1.2);
                vec3 lum = sunColor * light * phase * 6.0 + vec3(0.35, 0.45, 0.6) * 0.35;
                float a = 1.0 - exp(-d * dt * 1.5);
                acc += T * a * lum;
                T *= 1.0 - a;
                if (T < 0.02) break;
            }
            t += dt;
        }
        col = sky * T + acc;
        col = mix(col, sky, 1.0 - exp(-0.0015 * t0 * t0));
    }
    fragColor = vec4(pow(col, vec3(0.8)), 1.0);
}

More Showpieces examples

Deep glow · Prism · Menger sponge · Mandelbulb · Ocean · KIFS tunnel · Truchet weave · Voronoi crystals