Crystal Cave

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

Iridescent octahedron crystals with volumetric fog, raymarched shadows, and ambient occlusion

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)


// Crystal Cave - Iridescent octahedrons with volumetric fog and raymarched shadows
// Patterns: SDF octahedrons, smin blending, iridescent materials, soft shadows, AO

float sdOctahedron(vec3 p, float s) {
    p = abs(p);
    float m = p.x + p.y + p.z - s;
    vec3 q;
    if(3.0 * p.x < m) q = p.xyz;
    else if(3.0 * p.y < m) q = p.yzx;
    else if(3.0 * p.z < m) q = p.zxy;
    else return m * 0.57735027;
    float k = clamp(0.5 * (q.z - q.y + s), 0.0, s);
    return length(vec3(q.x, q.y - s + k, q.z - k));
}

float smin(float a, float b, float k) {
    float h = max(k - abs(a - b), 0.0) / k;
    return min(a, b) - h * h * k * 0.25;
}

// Simplex noise for fog
vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
vec4 mod289(vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
vec4 permute(vec4 x) { return mod289(((x*34.0)+1.0)*x); }
vec4 taylorInvSqrt(vec4 r) { return 1.79284291400159 - 0.85373472095314 * r; }

float snoise(vec3 v) {
    const vec2 C = vec2(1.0/6.0, 1.0/3.0);
    const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
    vec3 i = floor(v + dot(v, C.yyy));
    vec3 x0 = v - i + dot(i, C.xxx);
    vec3 g = step(x0.yzx, x0.xyz);
    vec3 l = 1.0 - g;
    vec3 i1 = min(g.xyz, l.zxy);
    vec3 i2 = max(g.xyz, l.zxy);
    vec3 x1 = x0 - i1 + C.xxx;
    vec3 x2 = x0 - i2 + C.yyy;
    vec3 x3 = x0 - D.yyy;
    i = mod289(i);
    vec4 p = permute(permute(permute(
        i.z + vec4(0.0, i1.z, i2.z, 1.0))
        + i.y + vec4(0.0, i1.y, i2.y, 1.0))
        + i.x + vec4(0.0, i1.x, i2.x, 1.0));
    float n_ = 0.142857142857;
    vec3 ns = n_ * D.wyz - D.xzx;
    vec4 j = p - 49.0 * floor(p * ns.z * ns.z);
    vec4 x_ = floor(j * ns.z);
    vec4 y_ = floor(j - 7.0 * x_);
    vec4 x = x_ *ns.x + ns.yyyy;
    vec4 y = y_ *ns.x + ns.yyyy;
    vec4 h = 1.0 - abs(x) - abs(y);
    vec4 b0 = vec4(x.xy, y.xy);
    vec4 b1 = vec4(x.zw, y.zw);
    vec4 s0 = floor(b0)*2.0 + 1.0;
    vec4 s1 = floor(b1)*2.0 + 1.0;
    vec4 sh = -step(h, vec4(0.0));
    vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy;
    vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww;
    vec3 p0 = vec3(a0.xy, h.x);
    vec3 p1 = vec3(a0.zw, h.y);
    vec3 p2 = vec3(a1.xy, h.z);
    vec3 p3 = vec3(a1.zw, h.w);
    vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2,p2), dot(p3,p3)));
    p0 *= norm.x; p1 *= norm.y; p2 *= norm.z; p3 *= norm.w;
    vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
    m = m * m;
    return 42.0 * dot(m*m, vec4(dot(p0,x0), dot(p1,x1), dot(p2,x2), dot(p3,x3)));
}

float fbm(vec3 p) {
    return snoise(p) * 0.5 + snoise(p * 2.0) * 0.25 + snoise(p * 4.0) * 0.125;
}

// Scene map
float map(vec3 p) {
    float t = iTime * 0.2;
    
    // Central crystal
    vec3 pc = p;
    pc.y += sin(t) * 0.2;
    float d = sdOctahedron(pc, 0.6);
    
    // Orbiting crystals
    for(int i = 0; i < 4; i++) {
        float angle = t * (1.0 + float(i) * 0.3) + float(i) * 1.57;
        float radius = 1.2 + float(i) * 0.4;
        vec3 pos = vec3(cos(angle) * radius, sin(angle * 0.5) * 0.3, sin(angle) * radius);
        float ds = sdOctahedron(p - pos, 0.25);
        d = smin(d, ds, 0.25);
    }
    
    // Floor with displacement
    float floor_h = -1.5 + fbm(p * 0.3 + vec3(0.0, 0.0, t * 0.1)) * 0.4;
    d = min(d, p.y - floor_h);
    
    return d;
}

float getShadow(vec3 p, vec3 lightDir) {
    float sh = 0.01;
    for(int i = 0; i < 20; i++) {
        vec3 sp = p + lightDir * sh;
        float d = map(sp);
        if(d < 0.001) return 0.0;
        sh += d;
        if(sh > 10.0) break;
    }
    return 1.0;
}

float getAO(vec3 p, vec3 n) {
    float occ = 0.0;
    float w = 1.0;
    for(int i = 0; i < 4; i++) {
        float h = 0.02 + float(i) * 0.04;
        float d = map(p + n * h);
        occ += (h - d) * w;
        w *= 0.5;
    }
    return clamp(1.0 - 2.0 * occ, 0.0, 1.0);
}

vec3 iridescent(float angle, float t) {
    float a = angle * 2.5 + t;
    return vec3(
        0.5 + 0.5 * cos(a),
        0.5 + 0.5 * cos(a + 2.09),
        0.5 + 0.5 * cos(a + 4.18)
    ) * 0.8 + 0.2;
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
    
    // Camera
    float t = iTime * 0.15;
    vec3 ro = vec3(cos(t) * 2.5, 0.8, sin(t) * 2.5);
    vec3 lookAt = vec3(0.0, -0.2, 0.0);
    vec3 fwd = normalize(lookAt - ro);
    vec3 right = normalize(cross(fwd, vec3(0.0, 1.0, 0.0)));
    vec3 up = cross(right, fwd);
    vec3 rd = normalize(fwd + uv.x * right + uv.y * up);
    
    // Raymarch
    float dist = 0.0;
    vec3 p;
    bool hit = false;
    for(int i = 0; i < 70; i++) {
        p = ro + rd * dist;
        float d = map(p);
        if(d < 0.001) { hit = true; break; }
        dist += d;
        if(dist > 15.0) break;
    }
    
    vec3 col = vec3(0.0);
    
    if(hit) {
        // Normal
        vec2 e = vec2(0.001, 0.0);
        vec3 n = normalize(vec3(
            map(p + e.xyy) - map(p - e.xyy),
            map(p + e.yxy) - map(p - e.yxy),
            map(p + e.yyx) - map(p - e.yyx)
        ));
        
        vec3 lightDir = normalize(vec3(0.5, 0.8, 0.3));
        vec3 viewDir = -rd;
        
        // Iridescent material
        float viewAngle = dot(n, viewDir);
        vec3 baseColor = iridescent(viewAngle, iTime);
        
        // Lighting
        float diff = max(0.0, dot(n, lightDir));
        vec3 halfVec = normalize(viewDir + lightDir);
        float spec = pow(max(0.0, dot(n, halfVec)), 32.0);
        
        // Shadow and AO
        float shadow = getShadow(p, lightDir);
        float ao = getAO(p, n);
        
        col = baseColor * (0.3 + diff * shadow * 0.7) + vec3(spec * shadow);
        col *= ao;
    }
    
    // Vignette
    float vignette = 1.0 - length(uv) * 0.4;
    col *= vignette;
    
    // Color grading
    col = pow(col, vec3(0.85));
    
    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