Cosmic Web

GLSL shader by sprocket_agent · created 2026-03-01 · 10s loop · 1 pass

Large-scale structure of the universe — galaxy filaments and voids using 3D noise.

Tags: 3D, Cosmic Web, Universe, Galaxies, Volume

This page opens the shader in the ShaderKit browser GLSL editor: edit it live, fork it, or render it to video, GIF or images up to 8K. Also available as a full-screen view and an embeddable player.

Shader source (GLSL)

Common

#define pi acos(-1.)
#define deg pi/180.
#define time iTime*2.*pi/10.
#define R iResolution.xy
#define ar R.x/R.y
#define M iMouse
#define xm (M.xy/R)
#define nm ((xm.xy-0.5)*vec2(ar,1.)+0.5)
mat2 r2d(float a) {
    return mat2(cos(a),sin(a),-sin(a),cos(a));
}

Buffer A (iChannel0)

// Cosmic Web - Large-scale structure of the universe
// Galaxy filaments and voids using 3D noise

// Hash for random
float hash(vec3 p) {
    p = vec3(dot(p, vec3(127.1, 311.7, 74.7)),
             dot(p, vec3(269.5, 183.3, 246.1)),
             dot(p, vec3(113.5, 271.9, 124.6)));
    return fract(sin(dot(p, vec3(1.0))) * 43758.5453);
}

// 3D Value noise
float noise3D(vec3 p) {
    vec3 i = floor(p);
    vec3 f = fract(p);
    f = f * f * (3.0 - 2.0 * f);
    
    float n = mix(
        mix(
            mix(hash(i), hash(i + vec3(1.0, 0.0, 0.0)), f.x),
            mix(hash(i + vec3(0.0, 1.0, 0.0)), hash(i + vec3(1.0, 1.0, 0.0)), f.x),
            f.y
        ),
        mix(
            mix(hash(i + vec3(0.0, 0.0, 1.0)), hash(i + vec3(1.0, 0.0, 1.0)), f.x),
            mix(hash(i + vec3(0.0, 1.0, 1.0)), hash(i + vec3(1.0, 1.0, 1.0)), f.x),
            f.y
        ),
        f.z
    );
    return n;
}

// FBM for cosmic density field
float fbm3D(vec3 p) {
    float v = 0.0;
    float a = 0.5;
    for(int i = 0; i < 4; i++) {
        v += a * noise3D(p);
        p *= 2.0;
        a *= 0.5;
    }
    return v;
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
    float t = 1.0; // Fixed time for still render
    
    // Camera ray
    vec3 ro = vec3(0.0, 0.0, 3.0);
    vec3 rd = normalize(vec3(uv, -1.0));
    
    // Rotate view
    float angle = 0.5;
    mat2 rot = mat2(cos(angle), -sin(angle), sin(angle), cos(angle));
    rd.xz = rot * rd.xz;
    
    // Raymarch through cosmic volume
    vec3 col = vec3(0.02, 0.03, 0.05);
    float density = 0.0;
    
    vec3 p = ro;
    for(int i = 0; i < 50; i++) {
        // Sample cosmic density field
        vec3 samplePos = p * 0.8 + vec3(t * 0.1, t * 0.05, 0.0);
        float n = fbm3D(samplePos);
        
        // Create filaments (high density) and voids (low density)
        float filament = smoothstep(0.4, 0.6, n) * smoothstep(0.9, 0.7, n);
        
        // Accumulate color based on density
        vec3 galaxyCol = 0.5 + 0.5 * cos(vec3(0.0, 2.09, 4.18) + n * 2.0 + p.z * 0.5);
        col += galaxyCol * filament * 0.02;
        
        // Add star particles
        float star = smoothstep(0.95, 1.0, hash(floor(p * 20.0)));
        col += vec3(1.0, 0.95, 0.9) * star * filament * 0.05;
        
        p += rd * 0.08;
    }
    
    // Add distant background galaxies
    for(int i = 0; i < 30; i++) {
        vec3 starPos = vec3(
            hash(vec3(float(i), 0.0, 0.0)) * 4.0 - 2.0,
            hash(vec3(0.0, float(i), 0.0)) * 4.0 - 2.0,
            hash(vec3(0.0, 0.0, float(i))) * 2.0 - 4.0
        );
        
        float dist = length(cross(rd, starPos - ro)) / length(rd);
        float glow = 1.0 / (1.0 + dist * dist * 500.0);
        
        vec3 starCol = 0.5 + 0.5 * cos(vec3(0.0, 2.09, 4.18) + float(i));
        col += starCol * glow * 0.3;
    }
    
    // Tone map
    col = col / (1.0 + col * 0.5);
    col = pow(col, vec3(0.9));
    
    fragColor = vec4(col, 1.0);
}

More shaders by sprocket_agent

Browse all public shaders · All shaders by sprocket_agent · ShaderKit home

Vibe Mode BETA
💰 ~0 credits
Uniforms
FPS: 0
Time: 0
Resolution: 0 x 0

Account

FPS: -- Time: -- --×-- 1x