Guest User

frag shader code

a guest
Jul 27th, 2025
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifdef GL_ES
  2.     precision highp float;
  3. #endif
  4.  
  5. //
  6. varying vec2 pos;
  7. uniform float minx;
  8. uniform float maxx;
  9. uniform float miny;
  10. uniform float maxy;
  11.  
  12. const float MAX_ITERATIONS = 250.0;
  13. const float BOUNDARY = 4.0;
  14.  
  15. //temp placement here, but these are our complex number functions which we will use instead
  16. vec2 cx_mul(vec2 a, vec2 b) {return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);}
  17. vec2 cx_add(vec2 a, vec2 b) {return vec2(a.x + b.x, a.y + b.y);}
  18.  
  19. //creating our own power function by for looping cx_mul cuz i'm not smart enough to math this out yet
  20. vec2 cx_pow(vec2 a, float b) {
  21.     vec2 n = vec2(1.0, 0.0);
  22.     for (float i = 1.0; i <= b; i += 1.0){
  23.         n = cx_mul(n, a);
  24.     }
  25.  
  26.     return n;
  27. }
  28.  
  29. //performs our main iteration processing
  30. float doIterate(vec2 pixel){
  31.     //initialize our real and imaginary values of c and z
  32.     vec2 z = vec2(0.0, 0.0);
  33.     vec2 c = vec2(pixel.x, pixel.y);
  34.  
  35.     //perform iterations
  36.     for (float i = 0.0; i < MAX_ITERATIONS; i += 1.0){
  37.         z = cx_pow(z, 2.0);
  38.         z = cx_add(z, c);
  39.  
  40.         //check if the point is outside the defined boundary. If so, just return the number of
  41.         //iterations it took to exceed this boundary
  42.         if (z.x * z.x + z.y * z.y > BOUNDARY){
  43.             //return Z - the point at which our iteration stopped,
  44.             //and i - the final number of iterations we spent before stopping
  45.             return i / MAX_ITERATIONS;
  46.         }
  47.     }
  48.  
  49.     //at this point, we've hit our max iteration limit, so we assume the point to be in the set. We return
  50.     //-1 so that our coloring functions don't confuse a return of 0 with a point in the set.
  51.     //also return our max iteration number, since we no longer have access to i
  52.     return 1.0;
  53. }
  54.  
  55. /**
  56. * our render logic starts here. This will be performed for all pixels
  57. */
  58. void main(){
  59.     //adjust our min and max coordinates for our canvas
  60.     float x = ((maxx - minx) * pos.x) + minx;
  61.     float y = ((maxy - miny) * pos.y) + miny;
  62.  
  63.     //perform iteration
  64.     float isInSet = doIterate(vec2(x, y));
  65.  
  66.     //finally, color the point
  67.     gl_FragColor = vec4(isInSet, isInSet, isInSet, 1.0);
  68. }
Advertisement
Add Comment
Please, Sign In to add comment