0.00
60.0 fps

Basic Mandelbrot

Basic, unoptimized Mandelbrot.

Log in to post a comment.

#version 300 es
precision highp float;

uniform float iTime;
uniform float iLoop;
uniform vec2  iResolution;

out vec4 fragColor;

uniform float m_scale;         // value=30, min = 0.001, max = 50, step = 0.001
uniform float offset_x;        // value = 1.763, min = -2, max = 2, step = 0.0001
uniform float offset_y;        // value = 0, min = -2, max = 2, step = 0.0001
uniform float max_iterations;    // value=60, min=1, max=1000, step=1

float mandelbrot(vec2 pos)
{
    vec2 c = pos;
    vec2 z = vec2(0.);
    float iterations = 0.;
    
    while (z.x * z.x + z.y * z.y < 4.)
    {
        z = vec2(z.x * z.x - z.y * z.y + c.x, 2. * z.x * z.y + c.y);
        iterations++;
        if (iterations >= max_iterations) break;
    }
    
    return iterations + 1. - log(log2(length(z)));
    //return iterations;
}

vec3 HSVtoRGB(float h, float s, float v)
{
    float r, g, b, f, p, q, t;
    int i;
    i = int(h * 6.);
    f = h * 6. - float(i);
    p = v * (1. - s);
    q = v * (1. - f * s);
    t = v * (1. - (1. - f) * s);
    switch (i % 6)
    {
        case 0: r = v, g = t, b = p; break;
        case 1: r = q, g = v, b = p; break;
        case 2: r = p, g = v, b = t; break;
        case 3: r = p, g = q, b = v; break;
        case 4: r = t, g = p, b = v; break;
        case 5: r = v, g = p, b = q; break;
    }
    return vec3(r, g, b);
}

void main() {
    // Screen coordinate (from [-aspect, -1] to [aspect, 1])
    vec2 pos = (2. * gl_FragCoord.xy - iResolution) / iResolution.y;

    float m_scale2 = m_scale * ((cos(iLoop*6.283185) + 1.) * 0.5) + 0.5;

    vec2 m = vec2(pos.x / m_scale2 - offset_x, pos.y / m_scale2 - offset_y);
    float iterations = mandelbrot(m);
    float value = 1. - iterations / max_iterations;
    value *= value;
        
    float h = value;
    float s = 1.;
    float v = (iterations < max_iterations) ? 1. - value*value : 0.;

    vec3 col = HSVtoRGB(h, s, v);

    fragColor = vec4(col, 1.);
}