Crystalline Matrix

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

Floating crystal shards in infinite grid with iridescent materials.

Tags: 3D, Raymarching, Crystal, Abstract

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)

// Crystalline Matrix - Floating crystal shards in infinite grid
// Refractive materials with caustic lighting

#define MAX_STEPS 64
#define MAX_DIST 20.0
#define SURF_DIST 0.001

// Rotation matrix
mat2 rot(float a) {
    float s = sin(a), c = cos(a);
    return mat2(c, -s, s, c);
}

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

// Octahedron SDF
float sdOctahedron(vec3 p, float s) {
    p = abs(p);
    return (p.x + p.y + p.z - s) * 0.57735027;
}

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

// Scene SDF
float map(vec3 p, float t) {
    // Grid repetition
    vec3 q = opRep(p, vec3(3.0, 2.5, 3.0));
    
    // Rotate each cell differently based on position
    float cellHash = fract(sin(dot(floor((p + 0.5 * vec3(3.0, 2.5, 3.0)) / vec3(3.0, 2.5, 3.0)), vec3(12.9898, 78.233, 45.164))) * 43758.5453);
    
    q.xz *= rot(t * 0.5 + cellHash * 6.28);
    q.yz *= rot(t * 0.3 + cellHash * 3.14);
    
    // Crystal shape (octahedron + box blend)
    float octa = sdOctahedron(q, 0.6);
    float box = sdBox(q, vec3(0.4, 0.8, 0.4));
    
    // Blend based on cell
    float blend = cellHash;
    return mix(octa, box, blend);
}

// Normal calculation
vec3 calcNormal(vec3 p, float t) {
    float d = map(p, t);
    vec2 e = vec2(0.001, 0.0);
    return normalize(vec3(
        map(p + e.xyy, t) - d,
        map(p + e.yxy, t) - d,
        map(p + e.yyx, t) - d
    ));
}

// Raymarch
float rayMarch(vec3 ro, vec3 rd, float t) {
    float d = 0.0;
    for(int i = 0; i < MAX_STEPS; i++) {
        vec3 p = ro + rd * d;
        float dS = map(p, t);
        d += dS;
        if(dS < SURF_DIST || d > MAX_DIST) break;
    }
    return d;
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
    float t = iTime * 0.2;
    
    // Camera
    vec3 ro = vec3(0.0, 0.0, -5.0);
    vec3 rd = normalize(vec3(uv, 1.0));
    
    // Rotate camera
    ro.xz *= rot(t * 0.3);
    rd.xz *= rot(t * 0.3);
    ro.xy *= rot(sin(t * 0.2) * 0.2);
    rd.xy *= rot(sin(t * 0.2) * 0.2);
    
    // Raymarch
    float d = rayMarch(ro, rd, t);
    
    vec3 col = vec3(0.02, 0.03, 0.05);
    
    if(d < MAX_DIST) {
        vec3 p = ro + rd * d;
        vec3 n = calcNormal(p, t);
        
        // Lighting
        vec3 lightDir = normalize(vec3(0.5, 1.0, 0.5));
        float diff = max(0.0, dot(n, lightDir));
        
        // Specular
        vec3 viewDir = -rd;
        vec3 halfDir = normalize(lightDir + viewDir);
        float spec = pow(max(0.0, dot(n, halfDir)), 64.0);
        
        // Iridescent material
        float fresnel = 1.0 - max(0.0, dot(n, viewDir));
        vec3 irid = 0.5 + 0.5 * cos(vec3(0.0, 0.5, 1.0) * 6.28 + fresnel * 4.0 - t);
        
        col = vec3(0.1, 0.15, 0.2) * diff;
        col += irid * 0.6;
        col += vec3(1.0) * spec * 0.5;
        
        // Distance fog
        col = mix(col, vec3(0.02, 0.03, 0.05), smoothstep(5.0, 15.0, d));
    }
    
    // Vignette
    col *= 1.0 - length(uv) * 0.3;
    col = pow(col, vec3(0.95));
    
    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