Bellaard.com

Sierpinski Octahedron

By: Gijs Bellaard

Sierpinski Octahedron

Description

The Sierpinski Octahedron is made by repeatedly replacing the 6 corners of all the octahedrons with half-sized octahedrons.

GLSL Code


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

// the scaling factor between iterations
const float SCALE = 2;

//distance to a unit octahedron 
float distanceUnitOctahedron(in vec3 p)
{
    p = abs(p);
    return (p.x+p.y+p.z - 1.0)/sqrt(3.0);
}

float distanceSierpinskiOctahedron(vec3 p)
{
    float s = 1.;
    
    for(int i=0; i<ITERATIONS; i++)
    {     
        // folds
        p = abs(p);
        if(p.y>p.x) p.yx = p.xy;
        if(p.z>p.x) p.xz = p.zx;
        
        // scaling
        p   *= SCALE;
        s   *= SCALE;
        
        // offset
        p.x -= 1.;    
    }
    
    // distance to a unit octahedron
    float d = distanceUnitOctahedron(p);
    
    return d/s; 
}