Advertisement
JakimPL

MicroFractal

Sep 1st, 2020
1,880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define rot(a) mat2(cos(a),sin(a),-sin(a),cos(a))
  2.  
  3. float orbit;
  4. float map (vec3 p)
  5. {
  6.     p.yz *= rot(iTime * 0.02);
  7.     p.xz *= rot(iTime * 0.05);  
  8.     p = abs(p) - 3.0;
  9.    
  10.     if (p.x < p.z)
  11.         p.xz = p.zx;
  12.     if (p.y < p.z)
  13.         p.yz = p.zy;
  14.     if (p.x<p.y)
  15.         p.xy = p.yx;
  16.    
  17.     float s = 3.0;
  18.     vec3  off = p * 0.3;
  19.    
  20.     for (int i = 0; i < 3; i++)
  21.     {
  22.         p = 1.0 - abs(p - 0.5);
  23.         float k = -1.01 * max(1.38 / dot(p, p), 1.38);
  24.         s *= abs(k);
  25.         p *= k;
  26.         p += off;
  27.         p.zx *= rot(-1.01);
  28.     }
  29.    
  30.     orbit = log(s);
  31.     float a = 0.2;
  32.     p -= clamp(p, -a, a);
  33.     return length(p)/s;
  34. }
  35.  
  36. vec3 calcNormal(vec3 pos)
  37. {
  38.     vec2 e = vec2(1, -1) * 0.02;
  39.     return 1.5 * normalize(
  40.         e.xyy * map(pos + 1.1 * e.xyy) + e.yyx * map(pos + 0.88 * e.yyx) +
  41.         e.yxy * map(pos + 0.9 * e.yxy) + e.xxx * map(pos + 1.08 * e.xxx)
  42.     );
  43. }
  44.  
  45. float march(vec3 ro, vec3 rd, float near, float far)
  46. {
  47.     float t = near,d;
  48.     for(int i = 0; i < 25; i++)
  49.     {
  50.         t += d = map(ro + rd * t);
  51.         if (d < 0.05) return t;
  52.         if (t >= far) return far;
  53.     }
  54.     return far;
  55. }
  56.  
  57. float calcShadow(vec3 light, vec3 ld, float len)
  58. {
  59.     float depth = march(light, ld, 0.0, len);  
  60.     return step(len - depth, 0.1);
  61. }
  62.  
  63. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  64. {
  65.     vec2 uv = (fragCoord * 2.0 - iResolution.xy) / iResolution.y;
  66.     vec3 ro = vec3(0, 0, 9.0 + sin(0.3 * iTime + 0.4 * sin(0.2 * iTime)));
  67.     vec3 rd = normalize(vec3(uv, -2.0));
  68.     vec3 col = vec3(0);
  69.     const float maxd = 50.0;
  70.     float t = march(ro, rd, 0.0, maxd);
  71.     if(t < maxd)
  72.     {
  73.         vec3 p = ro + rd * t;
  74.         col = 2.*clamp(mix(vec3(1),
  75.                 2.0 * (cos(vec3(2, 5, 12) + orbit * 2.0 + p * 0.3) * 0.5 + 0.5),
  76.                 0.7), 0.0, 1.0);
  77.         vec3 n = calcNormal(p);      
  78.         vec3 lightPos = vec3(10);
  79.         vec3 li = lightPos - p;
  80.         float len = length(li);
  81.         li /= len;
  82.         float dif = clamp(dot(n, li), 0.5, 1.0);
  83.         float sha = calcShadow(lightPos, -li, len);
  84.         col *= max(sha * dif, 0.4);
  85.         float rimd = pow(clamp(1.0 - dot(reflect(-li, n), -rd), 0.0, 1.0), 2.5);
  86.         float frn = rimd + 2.2 * (1.0 - rimd);
  87.         col *= frn * 0.7;
  88.         col *= max(0.5 + 0.5 * n.y, 0.0);
  89.         col *= exp2(-2.0 * pow(max(0.0, 1.0 - map(p + n * 0.3)/0.3), 2.0));
  90.         col += vec3(0.5, 0.4, 0.9) * pow(clamp(dot(reflect(rd, n), li), 0.0, 1.0), 10.0);
  91.         col = mix(vec3(0), col, exp(-t * t * 0.001));
  92.     }
  93.     fragColor.xyz = clamp(col,0.,1.);
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement