Skip to content

Instantly share code, notes, and snippets.

@solsarratea
Created June 9, 2020 02:47
Show Gist options
  • Save solsarratea/31fe6816eaf6ebf7905c0e5ea2d61cda to your computer and use it in GitHub Desktop.
Save solsarratea/31fe6816eaf6ebf7905c0e5ea2d61cda to your computer and use it in GitHub Desktop.
K iterations of Koch curve
// Drawing Koch Curve - k-iterations
const int iterations = 7;
float scale = 3.;
void reflection(inout vec2 pos, float angle){
vec2 normal = vec2(sin(angle),cos(angle));
float d = dot(pos, normal);
pos -= normal*min(0.,d)*2.;
d /= pow(scale, float(iterations));
//return smoothstep(0.2,0.,abs(d));
}//
float line (vec2 pos){
float d = length(pos-vec2(clamp(pos.x,-1.,1.),0.));
return smoothstep(0.1 * float(iterations),0.,abs(d));
}
void main() {
vec2 pos = uv();
pos*=1./3.;
pos.x +=.5;
vec4 color = vec4(0.);
float angle = PI-PI/3.;
// Recover uv values; Repeat it over segments;
for (int i =0; i< iterations;i++){
pos *= scale; //scale
pos.x -= 1.5; //transalte
pos.x = abs(pos.x);
pos.x -= .5;
reflection(pos,angle);
}
color += line(pos);
//color.rb += pos;
gl_FragColor = color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment