monte_carlo_blur.glsl 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. uniform bool prefer_closer_pixels;
  9. float srand(vec2 a) {
  10. return sin(dot(a, vec2(1233.224, 1743.335)));
  11. }
  12. float rand(inout float r) {
  13. r = fract(3712.65 * r + 0.61432);
  14. return (r - 0.5) * 2.0;
  15. }
  16. void main() {
  17. vec2 uv = cogl_tex_coord0_in.st;
  18. vec2 p = 16 * radius / vec2(width, height);
  19. float r = srand(uv);
  20. vec2 rv;
  21. vec2 dir;
  22. vec2 new_uv;
  23. int strength;
  24. int count = 0;
  25. vec4 c = vec4(0.);
  26. for (int i = 0; i < iterations; i++) {
  27. rv.x = rand(r);
  28. rv.y = rand(r) * 3.141592;
  29. dir = vec2(cos(rv.y), sin(rv.y));
  30. new_uv = uv + rv.x * dir * p;
  31. if (new_uv.x > 2. / width && new_uv.y > 2. / height && new_uv.x < 1. - 3. / width && new_uv.y < 1. - 3. / height) {
  32. strength = prefer_closer_pixels ? (iterations - i)^2 : 1;
  33. c += strength * texture2D(tex, new_uv);
  34. count += strength;
  35. }
  36. }
  37. if (count == 0 || use_base_pixel) {
  38. strength = prefer_closer_pixels ? (iterations + 1)^2 : 1;
  39. c += strength * texture2D(tex, uv);
  40. count += strength;
  41. }
  42. c.xyz *= brightness;
  43. cogl_color_out = c / count;
  44. }