Crystal Lattice

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

3D raymarched crystal lattice with iridescent spheres and connecting rods. Repeating structure with soft shadows.

Tags: 3D, Raymarching, Crystal, Iridescent

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 Lattice - 3D raymarched lattice with iridescent materials
// Repeating crystal structure with soft shadows

float g_time;

// SDF functions
float sdSphere(vec3 p, float r) {
    return length(p) - r;
}

float sdBox(vec3 p, vec3 b) {
    vec3 d = abs(p) - b;
    return length(max(d, 0.0)) + min(max(d.x, max(d.y, d.z)), 0.0);
}

// Repeat space
vec3 opRep(vec3 p, vec3 c) {
    return mod(p + 0.5 * c, c) - 0.5 * c;
}

// Map function
float map(vec3 p) {
    vec3 rep = opRep(p, vec3(1.2));
    
    // Central sphere in each cell
    float sphere = sdSphere(rep, 0.25);
    
    // Connecting rods (cylinders via box approximation)
    float rodX = sdBox(rep - vec3(0.6, 0.0, 0.0), vec3(0.08, 0.08, 0.08));
    float rodY = sdBox(rep - vec3(0.0, 0.6, 0.0), vec3(0.08, 0.08, 0.08));
    float rodZ = sdBox(rep - vec3(0.0, 0.0, 0.6), vec3(0.08, 0.08, 0.08));
    
    float rods = min(min(rodX, rodY), rodZ);
    
    return min(sphere, rods);
}

// Get normal
vec3 getNormal(vec3 p) {
    vec2 e = vec2(0.001, 0.0);
    return 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)
    ));
}

// Raymarch
float raymarch(vec3 ro, vec3 rd) {
    float dist = 0.0;
    for(int i = 0; i < 100; i++) {
        vec3 p = ro + rd * dist;
        float d = map(p);
        if(d < 0.001) break;
        dist += d;
        if(dist > 20.0) break;
    }
    return dist;
}

// Soft shadow
float getShadow(vec3 p, vec3 lightDir) {
    float shadow = 1.0;
    float shadowDist = 0.02;
    for(int i = 0; i < 32; i++) {
        vec3 sp = p + lightDir * shadowDist;
        float d = map(sp);
        if(d < 0.001) return 0.0;
        shadow = min(shadow, 10.0 * d / shadowDist);
        shadowDist += d;
        if(shadowDist > 10.0) break;
    }
    return clamp(shadow, 0.0, 1.0);
}

// Iridescent material
vec3 iridescent(vec3 normal, vec3 viewDir, float t) {
    float angle = dot(normal, viewDir);
    float a = angle * 3.0 + 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)
    );
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
    g_time = 1.5; // Fixed time for still render
    
    // Camera
    vec3 ro = vec3(2.0, 1.5, 3.0);
    vec3 lookAt = vec3(0.0, 0.0, 0.0);
    vec3 forward = normalize(lookAt - ro);
    vec3 right = normalize(cross(vec3(0.0, 1.0, 0.0), forward));
    vec3 up = cross(forward, right);
    vec3 rd = normalize(forward + right * uv.x + up * uv.y);
    
    // Raymarch
    float dist = raymarch(ro, rd);
    vec3 col = vec3(0.05, 0.08, 0.12); // Background
    
    if(dist < 20.0) {
        vec3 p = ro + rd * dist;
        vec3 normal = getNormal(p);
        
        // Light
        vec3 lightDir = normalize(vec3(1.0, 2.0, 1.0));
        vec3 viewDir = -rd;
        
        // Iridescent base color
        vec3 baseColor = iridescent(normal, viewDir, g_time) * 1.5;
        
        // Diffuse
        float diff = max(0.0, dot(normal, lightDir));
        
        // Specular
        vec3 halfway = normalize(lightDir + viewDir);
        float spec = pow(max(0.0, dot(normal, halfway)), 64.0);
        
        // Shadow
        float shadow = getShadow(p, lightDir);
        
        // Combine
        col = baseColor * (0.3 + diff * 0.7 * shadow) + vec3(spec) * shadow;
        
        // Fog
        float fog = 1.0 - exp(-dist * 0.1);
        col = mix(col, vec3(0.05, 0.08, 0.12), fog);
    }
    
    // Tone map
    col = col / (1.0 + col * 0.5);
    col = pow(col, vec3(0.9)); // Slight gamma
    
    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