Sierpinski Tetrahedron

By: Gijs Bellaard

Sierpinski Tetrahedron


The Sierpinski Tetrahedron is made by repeatedly replacing the four corners of all the tetrahedrons with half-sized tetrahedrons.


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

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

//Fold normals
const vec3 N1 = normalize(vec3(1,1,0));
const vec3 N2 = normalize(vec3(1,0,1));
const vec3 N3 = normalize(vec3(0,1,1));

float distanceSierpinskiTetrahedron(vec3 p){
    float s = 1.;
    for(int n=0; n<ITERATIONS; n++) {
        p -= 2.*min(0.,dot(p,N1))*N1; 
        p -= 2.*min(0.,dot(p,N2))*N2; 
        p -= 2.*min(0.,dot(p,N3))*N3; 
        p *= SCALE;
        s *= SCALE;
        p -= -1.;
    //Distance to a tetrahedron
    float d = distanceTetrahedron(p);
    return d/s;