Advertisement
LaggerMC

julia.frag

Jan 26th, 2021
1,204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.80 KB | None | 0 0
  1. #define MAX_ITERATIONS 500
  2. #define C_MAP 0
  3. #define TILE 0
  4. #define MANDELBROT 0
  5.  
  6. float colormap_red(float x) {
  7.     if (x < 0.7) {
  8.         return 4.0 * x - 1.5;
  9.     } else {
  10.         return -4.0 * x + 4.5;
  11.     }
  12. }
  13.  
  14. float colormap_green(float x) {
  15.     if (x < 0.5) {
  16.         return 4.0 * x - 0.5;
  17.     } else {
  18.         return -4.0 * x + 3.5;
  19.     }
  20. }
  21.  
  22. float colormap_blue(float x) {
  23.     if (x < 0.3) {
  24.        return 4.0 * x + 0.5;
  25.     } else {
  26.        return -4.0 * x + 2.5;
  27.     }
  28. }
  29.  
  30. vec4 colormap_jet(float x) {
  31.     float r = clamp(colormap_red(x), 0.0, 1.0);
  32.     float g = clamp(colormap_green(x), 0.0, 1.0);
  33.     float b = clamp(colormap_blue(x), 0.0, 1.0);
  34.     return vec4(r, g, b, 1.0);
  35. }
  36. vec2 squareImaginary(vec2 n) {
  37.     return vec2((n.x * n.x) - (n.y * n.y), 2.0 * n.x * n.y);
  38. }
  39.  
  40. vec3 iterate(vec2 z, vec2 c) {
  41.  
  42.     int iterations = 0;
  43.  
  44.     while (length(z) < 2.0 && iterations < MAX_ITERATIONS) {
  45.    
  46.         z = squareImaginary(z) + c;
  47.         iterations++;
  48.     }
  49.    
  50.     z = squareImaginary(z); iterations++;    // a couple of extra iterations helps
  51.     z = squareImaginary(z); iterations++;    // decrease the size of the error term.
  52.     float l = length(z);
  53.     float m = float(MAX_ITERATIONS);
  54.     float i = float(iterations);
  55.     float mu = i - (log (log (l)))/ log (2.0);
  56.  
  57.     vec3 col = colormap_jet(mu * .03).rgb;
  58.     // vec3 col = vec3(0.0);
  59.     // if (iterations >= MAX_ITERATIONS) col = vec3(1.0);
  60.     return col;
  61. }
  62.  
  63. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  64. {
  65.     float t = iTime;
  66.  
  67.     // Define window of the complex plane to render Julias
  68.     float x = 0.;
  69.     vec2 jRealRange = vec2(-2.0 + x, 2.0 - x);
  70.     vec2 jCompRange = jRealRange * iResolution.y / iResolution.x;
  71.     // Define window of the complex plane to render Mandelbrot
  72.     vec2 mRealRange = vec2(-2.5, 1.0);
  73.     vec2 mCompRange = mRealRange * iResolution.y / iResolution.x;
  74.    
  75.     // Normalized pixel coordinates from 0 to 1
  76.     vec2 st  = fragCoord/iResolution.xy;
  77.  
  78.  
  79.     // Coordinates scaled and shifted to complex plane
  80.     vec2 uv = st;
  81.  
  82.     #if TILE==1
  83.     // Number of tiles
  84.     // float n = 1.;
  85.     float n = floor(exp(mod(0.2 * t, 5.))) + 1.0;
  86.     uv *= n;
  87.     uv = fract(uv);
  88.     // vec2 c = floor(uv) + 0.5;
  89.     #endif
  90.  
  91.     #if MANDELBROT==0
  92.     uv *= length(jRealRange);
  93.     uv.y *= iResolution.y / iResolution.x;
  94.     uv -= vec2(length(jRealRange) / 2.0, length(jCompRange) / 2.0);
  95.  
  96.     #else
  97.     uv.x *= 2.0;
  98.     uv.x = fract(uv.x);
  99.     if (fragCoord.x >= iResolution.x / 2.0) {
  100.         uv *= length(jRealRange);
  101.         uv.y *= iResolution.y / iResolution.x * 2.0;
  102.         uv -= vec2(length(jRealRange) / 2.0, length(jCompRange));
  103.     }
  104.     else {
  105.         uv *= length(mRealRange);
  106.         uv.y *= iResolution.y / iResolution.x * 2.0;
  107.         uv -= vec2(length(mRealRange) / 2.0 + 0.5, length(mCompRange));
  108.     }
  109.     #endif
  110.  
  111.  
  112.     #if C_MAP==1 && TILE==1
  113.     // c value map
  114.     vec2 c = st;
  115.     c *= length(mRealRange);
  116.     c.y *= iResolution.y / iResolution.x;
  117.     c -= vec2(length(mRealRange) / 2.0 + 0.5, length(mCompRange) / 2.0);
  118.  
  119.     #elif MANDELBROT==1
  120.  
  121.     vec2 c = iMouse.xy / iResolution.xy;
  122.     c.x *= 2.0;
  123.     c.x = fract(c.x);
  124.     c *= length(mRealRange);
  125.     c.y *= iResolution.y / iResolution.x * 2.0;
  126.     c -= vec2(length(mRealRange) / 2.0 + 0.5, length(mCompRange));
  127.    
  128.     #else
  129.  
  130.     // Overwrite c val
  131.     float r = 0.5;
  132.     vec2 c = vec2(r * cos(iTime), r * sin(iTime));
  133.     // vec2 c = vec2(2.0, 2.0);
  134.  
  135.     #endif
  136.  
  137.     #if MANDELBROT==0
  138.     vec3 col = iterate(uv, c);
  139.     #else
  140.     vec3 col = vec3(0.0);
  141.     if (fragCoord.x >= iResolution.x / 2.0) {
  142.         col = iterate(uv, c);
  143.     }
  144.     else {
  145.         col = iterate(vec2(0.), uv);
  146.     }
  147.  
  148.     #endif
  149.  
  150.     // Output to screen
  151.     fragColor = vec4(col,1.0);
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement