Apollonian fractal

By: Gijs Bellaard

Apollonian fractal


The Apollonian fractal is made by repeatedly performing a repetition and an unit sphere inversion, after which we take the distance to a plane. Due to the sphere inversions the distance field is not proper, which is why we return only a quarter of the distance. This makes sure the raymarcher does not overstep through the surface of the fractal, saving us from artifacts.


// the amount of iterations
const int ITERATIONS = 10;

// radius of the sphere inversion
const float RADIUS = 1.0;

// normal vector of the plane
const float N = normalize(vec3(0, 1, 0));

// returns the distance to the plane through the origin with normal vector n
float distancePlane(vec3 p, vec3 n)
    return abs(dot(p, n));

float distanceApollonian(vec3 p)
    float s = 1.;

    for(int i=0; i<ITERATIONS; i++)
        // repetition
        p = mod(p + 1.0, 2.0) - 1.0;
        // sphere Inversion
        float k = RADIUS / dot(p, p);
        p *= k;
        s *= k;
    // distance to a plane
    float d = distancePlane(p,N);
    // conservative distance
    return 0.25 * d / s;