Mandelbrot zoom

glint · Examples · Tutorials · Gallery

A slow dive into Seahorse Valley, as deep as 32-bit floats allow before the pixels show, then back out again.

Mandelbrot zoom: 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;

in vec2 v_texcoord;
out vec4 fragColor;

float hash(float n) { return fract(sin(n * 127.1) * 43758.5453); }
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
vec2 hash2(float n) { return fract(sin(vec2(n * 127.1, n * 269.5)) * 43758.5453); }
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, s, -s, c); }
vec3 pal(float t) { return 0.5 + 0.5 * cos(6.28318 * (t + vec3(0.0, 0.33, 0.67))); }

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    // Zoom in and out again (floats run out of precision at about 1/1000 of the start).
    float depth = 3.5 * (1.0 - cos(time * 0.12)) ;
    float scale = 2.5 * exp(-depth);
    vec2 centre = vec2(-0.743643887, 0.131825904);
    vec2 c = centre + rot(time * 0.05) * uv * scale;
    vec2 z = vec2(0.0);
    float n = 0.0;
    const float MAX = 320.0;
    for (float i = 0.0; i < MAX; i++) {
        z = vec2(z.x * z.x - z.y * z.y, 2.0 * z.x * z.y) + c;
        if (dot(z, z) > 256.0) break;
        n++;
    }
    float sn = n - log2(log2(dot(z, z))) + 4.0;
    vec3 col = n >= MAX ? vec3(0.0) : 0.5 + 0.5 * cos(3.0 + sn * 0.08 + vec3(0.0, 0.6, 1.0));
    fragColor = vec4(col, 1.0);
}

More More GLSL examples

Rain on the window · 2D distance field playground · Gyroid cave · Julia set · Lava lamp · Circuit board · Snowfall · Ink in water