bilateralV.frag 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // From http://http.developer.nvidia.com/GPUGems3/gpugems3_ch40.html
  2. uniform sampler2D tex;
  3. uniform sampler2D depth;
  4. uniform vec2 pixel;
  5. uniform float sigma = 5.;
  6. out vec4 FragColor;
  7. void main()
  8. {
  9. vec2 uv = gl_FragCoord.xy * pixel;
  10. float X = uv.x;
  11. float Y = uv.y;
  12. float g0, g1, g2;
  13. g0 = 1.0 / (sqrt(2.0 * 3.14) * sigma);
  14. g1 = exp(-0.5 / (sigma * sigma));
  15. g2 = g1 * g1;
  16. vec4 sum = texture(tex, vec2(X, Y)) * g0;
  17. float pixel_depth = texture(depth, vec2(X, Y)).x;
  18. g0 *= g1;
  19. g1 *= g2;
  20. float tmp_weight, total_weight = g0;
  21. for (int i = 1; i < 9; i++) {
  22. tmp_weight = max(0.0, 1.0 - .001 * abs(texture(depth, vec2(X, Y - i * pixel.y)).x - pixel_depth));
  23. sum += texture(tex, vec2(X, Y - i * pixel.y)) * g0 * tmp_weight;
  24. total_weight += g0 * tmp_weight;
  25. tmp_weight = max(0.0, 1.0 - .001 * abs(texture(depth, vec2(X, Y + i * pixel.y)).x - pixel_depth));
  26. sum += texture(tex, vec2(X, Y + i * pixel.y)) * g0 * tmp_weight;
  27. total_weight += g0 * tmp_weight;
  28. g0 *= g1;
  29. g1 *= g2;
  30. }
  31. FragColor = sum / total_weight;
  32. }