Bellaard.com

Sierpinski Tetrahedron

By: Gijs Bellaard

Sierpinski Tetrahedron

Description

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

GLSL Code


//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++) {
        //Folds
        p -= 2.*min(0.,dot(p,N1))*N1; 
        p -= 2.*min(0.,dot(p,N2))*N2; 
        p -= 2.*min(0.,dot(p,N3))*N3; 
       
        //Scaling
        p *= SCALE;
        s *= SCALE;
        
        //Offset
        p -= -1.;
    }
    
    //Distance to a tetrahedron
    float d = distanceTetrahedron(p);
    
    return d/s; 
}