Mandelbrot & Julia

glint · Examples · Tutorials · Gallery

Smooth-coloured Mandelbrot set. Switch on “julia” and the mouse picks the Julia constant.

Mandelbrot & Julia: 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;
uniform vec2 mouse;
uniform vec2 center;
uniform float zoom;
uniform bool julia;
uniform int iterations;

in vec2 v_texcoord;
out vec4 fragColor;

void main() {
    vec2 p = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    vec2 z, c;
    if (julia) {
        z = p * 3.0 / zoom;
        c = (mouse - 0.5) * vec2(3.0, 2.0) + vec2(-0.5, 0.0);
    } else {
        z = vec2(0.0);
        c = center + p * 3.0 / zoom;
    }
    float n = 0.0;
    for (int i = 0; i < 1000; i++) {
        if (i >= iterations) break;
        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 += 1.0;
    }
    vec3 col = vec3(0.0);
    if (n < float(iterations)) {
        float sn = n - log2(log2(dot(z, z))) + 4.0;   // smooth iteration count
        col = 0.5 + 0.5 * cos(3.0 + sn * 0.15 + vec3(0.0, 0.6, 1.0) + time * 0.2);
    }
    fragColor = vec4(col, 1.0);
}

More Starters examples

Webcam kaleidoscope · Shadertoy style · Plasma · Tunnel · Reaction–diffusion · Game of Life · Instanced cubes · Wireframe waves