Advertisement
luluco250

Gaussian Shader Test

Mar 14th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. //Toggle Effects
  2. #define DO_VIGNETTE 1
  3. #define DO_GAUSSIANBLUR 1
  4.  
  5. //Effect Configurations
  6. #define GAUSSIAN_SAMPLES 6
  7.  
  8. const float double_pi = 6.283185307179586476925286766559;
  9. const float sigma = float(GAUSSIAN_SAMPLES) / 2.0;
  10. const vec2 image_res = vec2(3860, 2160);
  11. vec2 ps = vec2(1.0) / iResolution.xy;
  12. float ar = iResolution.x / iResolution.y;
  13.  
  14. //Functions
  15. vec2 scaleUV(vec2, vec2);
  16. vec3 crop(sampler2D, vec2);
  17. float gaussian_function(float);
  18. float gaussian_function(vec2);
  19. float gaussian_get_accum();
  20.  
  21. //Effects
  22. vec3 GaussianBlur(sampler2D, vec2, vec2);
  23. vec3 GaussianBlur(sampler2D, vec2);
  24. void Vignette(inout vec3, vec2);
  25.  
  26. //Main Function
  27. void main() {
  28.     vec2 uv = gl_FragCoord.xy * ps;
  29.     vec3 col = GaussianBlur(iChannel0, uv);
  30.     Vignette(col, uv);
  31.     gl_FragColor = vec4(col, 1.0);
  32. }
  33.  
  34. vec2 scaleUV(vec2 uv, vec2 scale) {
  35.     return (uv - 0.5) * scale + 0.5;
  36. }
  37.  
  38. vec3 crop(sampler2D sp, vec2 uv) {
  39.     return vec3(0.0);
  40. }
  41.  
  42. float gaussian_function(float x) {
  43.     const float first_part = 1.0 / sqrt(double_pi * pow(sigma, 2.0));
  44.     const float second_part_a = 1.0 / (2.0 * pow(sigma, 2.0));
  45.     float second_part_b = pow(x, 2.0) * second_part_a;
  46.     return first_part * exp(-second_part_a);
  47. }
  48.  
  49. float gaussian_function(vec2 i) {
  50.     const float first_part = 1.0 / (double_pi * pow(sigma, 2.0));
  51.     const float second_part_a = 1.0 / (2.0 * pow(sigma, 2.0));
  52.     float second_part_b = (pow(i.x, 2.0) + pow(i.y, 2.0)) * second_part_a;
  53.     return first_part * exp(-second_part_b);
  54. }
  55.  
  56. float gaussian_get_accum() {
  57.     float accum = 0.0;
  58.     for (int i = -GAUSSIAN_SAMPLES; i <= GAUSSIAN_SAMPLES; ++i)
  59.         accum += gaussian_function(float(i));
  60.     return 1.0 / accum;
  61. }
  62.  
  63. vec3 GaussianBlur(sampler2D sp, vec2 uv, vec2 dir) {
  64.     #if !DO_GAUSSIANBLUR
  65.     return texture2D(sp, uv).rgb;
  66.     #endif
  67.     vec3 col = vec3(0.0);
  68.     float gauss_weight = 0.0;
  69.     float accum = 0.0;
  70.     for (int i = -GAUSSIAN_SAMPLES; i <= GAUSSIAN_SAMPLES; ++i) {
  71.         gauss_weight = gaussian_function(float(i));
  72.         col += texture2D(sp, uv + ps * dir * vec2(i)).rgb * gauss_weight;
  73.         accum += gauss_weight;
  74.     }
  75.     return col / accum;
  76. }
  77.  
  78. vec3 GaussianBlur(sampler2D sp, vec2 uv) {
  79.     #if !DO_GAUSSIANBLUR
  80.     return texture2D(sp, uv).rgb;
  81.     #endif
  82.     vec3 col = vec3(0.0);
  83.     float gauss_weight = 0.0;
  84.     float accum = 0.0;
  85.     for (int x = -GAUSSIAN_SAMPLES; x <= GAUSSIAN_SAMPLES; ++x) {
  86.         for (int y = -GAUSSIAN_SAMPLES; y <= GAUSSIAN_SAMPLES; ++y) {
  87.             gauss_weight = gaussian_function(vec2(x, y));
  88.             col += texture2D(sp, uv + ps * vec2(x, y)).rgb * gauss_weight;
  89.             accum += gauss_weight;
  90.         }
  91.     }
  92.     return col / accum;
  93. }
  94.  
  95. void Vignette(inout vec3 col, vec2 uv) {
  96.     #if DO_VIGNETTE
  97.     col *= 1.0 - distance(uv, vec2(0.5));
  98.     #endif
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement