Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define DE_ITER 10
- #define MARCHING_ITER 64
- #define MARCHING_THRESHOLD 10e-6
- vec3 camera = vec3(0., 1., -5.);
- float d_sierpinski_tetrahedron(vec3 z)
- {
- float Scale = 0.5;
- vec3 a1 = vec3(1,1,1);
- vec3 a2 = vec3(-1,-1,1);
- vec3 a3 = vec3(1,-1,-1);
- vec3 a4 = vec3(-1,1,-1);
- vec3 c;
- int n = 0;
- float dist, d;
- while (n < DE_ITER) {
- c = a1; dist = length(z-a1);
- d = length(z-a2); if (d < dist) { c = a2; dist=d; }
- d = length(z-a3); if (d < dist) { c = a3; dist=d; }
- d = length(z-a4); if (d < dist) { c = a4; dist=d; }
- z = Scale*z-c*(Scale-1.0);
- n++;
- }
- return length(z) * pow(Scale, float(-n));
- }
- float d_plane(vec3 p, vec3 e1, vec3 e2, vec3 a)
- {
- e1 = normalize(e1);
- e2 = normalize(e2);
- vec3 pp = dot(p, e1) * e1 + dot(p, e2) * e2 + a;
- return length(p - pp);
- }
- float d_sphere(vec3 p, vec3 c, float r)
- {
- return length(p - c) - r;
- }
- float DE(vec3 p)
- {
- // return d_sphere(p, vec3(cos(time), sin(time), cos(time)), 1.);
- return d_sierpinski_tetrahedron(p);
- // return min(d_plane(p, vec3(1., 0., 0.), vec3(0., 0., 1.), vec3(0.)), d_sphere(p, vec3(cos(time), sin(time), cos(time)), 1.));
- }
- void main()
- {
- vec2 uv = (in_uv_coords - vec2(0.5, 0.5)) * 2;
- vec3 dir = normalize(vec3(uv, 1.));
- int i;
- float d = 0;
- bool ended = false;
- for(i = 0; i < MARCHING_ITER; i++)
- {
- vec3 p = camera + d * dir;
- float dd = DE(p);
- d += dd;
- if(abs(dd) < MARCHING_THRESHOLD)
- {
- ended = true;
- break;
- }
- }
- if(ended) gl_FragColor = vec4(1., 1., 1., 0.) * (1. - (float(i) / float(MARCHING_ITER)));
- else gl_FragColor = vec4(0., 0., 0., 0.);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement