-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkoch_snowflake.glsl
52 lines (45 loc) · 1012 Bytes
/
koch_snowflake.glsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
precision mediump float;
uniform float t; // time
uniform vec2 r; // resolution
const float PI = 3.14159265;
vec2 fold(vec2 p, float ang){
vec2 n=vec2(cos(-ang),sin(-ang));
p-=2.*min(0.,dot(p,n))*n;
return p;
}
vec2 koch_fold(vec2 pt) {
// Fold horizontally
pt.x = abs(pt.x);
pt.x-=.5;
//Fold across PI/6
pt = fold(pt,PI/6.);
return pt;
}
vec2 koch_curve(vec2 pt) {
//Fold and scale a few times
for(int i=0;i<5;i++){
pt*=3.;
pt.x-=1.5;
pt=koch_fold(pt);
}
return pt;
}
float d2hline(vec2 p){
p.x-=max(0.,min(1.,p.x));
return length(p)*5.;
}
float color(vec2 pt) {
pt = pt*.7;
pt -= vec2(.5,.3);
pt = fold(pt,-2.*PI/3.);
pt.x += 1.;
pt = fold(pt,-PI/3.);
pt = koch_curve(pt);
return d2hline(pt)/5.;
}
void main(void){
vec2 p = (gl_FragCoord.xy * 2.0 - r) / min(r.x, r.y);
vec3 destColor = vec3(1.0, 0.3, 0.7);
float f = color(p);
gl_FragColor = vec4(vec3(destColor*f), 1.0);
}