Bellaard.com

Tree fractal

By: Gijs Bellaard

Tree fractal

Description

The Tree fractal

GLSL Code


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

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

//The angle the branches make
const vec3 ANGLE = vec3(0,0,0.1);

//The size of a section
const vec3 SECTION = vec3(.1,1,.1);

//Precomputations
const vec3 m = getRotationMatrix(ANGLE);
const vec3 g = m*vec3(0,1,0);
const vec3 n = normalize(vec3(g.x,0,g.z));

float distanceTree(vec3 p){
    float s = 1.;
    float dis = 1e20;
    
    for(int i=0; i<ITERATIONS; i++){
        //Distance to a box
        dis = min(dis, distanceBox(p,SECTION)/s);

        //Fold
        p -= 2.*min(0.,dot(p,n))*n;
        
        //Offset
        p.y -= SECTION.y;
        
        //Scaling
        p *= SCALE;
        s *= SCALE;
        
        //Offset
        p -= g*SECTION.y;
        
        //Rotating
        p *= m;
    }

    return dis; 
}