bilateralV.frag 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // From http://http.developer.nvidia.com/GPUGems3/gpugems3_ch40.html
  2. uniform sampler2D tex;
  3. uniform sampler2D depth;
  4. uniform vec2 pixel;
  5. out vec4 FragColor;
  6. void main()
  7. {
  8. float sigma = 5.;
  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 - float(i) * pixel.y)).x - pixel_depth));
  23. sum += texture(tex, vec2(X, Y - float(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 + float(i) * pixel.y)).x - pixel_depth));
  26. sum += texture(tex, vec2(X, Y + float(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. }