monte_carlo_blur.glsl 1011 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. uniform sampler2D tex;
  2. uniform float radius;
  3. uniform int iterations;
  4. uniform float brightness;
  5. uniform float width;
  6. uniform float height;
  7. uniform bool use_base_pixel;
  8. float srand(vec2 a) {
  9. return sin(dot(a, vec2(1233.224, 1743.335)));
  10. }
  11. float rand(inout float r) {
  12. r = fract(3712.65 * r + 0.61432);
  13. return (r - 0.5) * 2.0;
  14. }
  15. void main() {
  16. vec2 uv = cogl_tex_coord0_in.st;
  17. vec2 p = 16 * radius / vec2(width, height);
  18. float r = srand(uv);
  19. vec2 rv;
  20. int count = 0;
  21. vec4 c = vec4(0.);
  22. for (int i = 0; i < iterations; i++) {
  23. rv.x = rand(r);
  24. rv.y = rand(r);
  25. vec2 new_uv = uv + rv * p;
  26. if (new_uv.x > 2. / width && new_uv.y > 2. / height && new_uv.x < 1. - 3. / width && new_uv.y < 1. - 3. / height) {
  27. c += texture2D(tex, new_uv);
  28. count += 1;
  29. }
  30. }
  31. if (count == 0 || use_base_pixel) {
  32. c += texture2D(tex, uv);
  33. count += 1;
  34. }
  35. c.xyz *= brightness;
  36. cogl_color_out = c / count;
  37. }