mlaa_neigh3.frag 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. uniform sampler2D blendMap;
  2. uniform sampler2D colorMap;
  3. out vec4 FragColor;
  4. void main() {
  5. vec2 uv = gl_FragCoord.xy / screen;
  6. vec2 uv_left = uv + vec2(-1., 0.) / screen;
  7. vec2 uv_top = uv + vec2(0., 1.) / screen;
  8. vec2 uv_right = uv + vec2(1., 0.) / screen;
  9. vec2 uv_bottom = uv + vec2(0., -1.) / screen;
  10. // Fetch the blending weights for current pixel:
  11. vec4 topLeft = texture(blendMap, uv);
  12. float bottom = texture(blendMap, uv_bottom).g;
  13. float right = texture(blendMap, uv_right).a;
  14. vec4 a = vec4(topLeft.r, bottom, topLeft.b, right);
  15. // Up to 4 lines can be crossing a pixel (one in each edge). So, we perform
  16. // a weighted average, where the weight of each line is 'a' cubed, which
  17. // favors blending and works well in practice.
  18. vec4 w = a * a * a;
  19. // There is some blending weight with a value greater than 0.0?
  20. float sum = dot(w, vec4(1.0));
  21. if (sum < 1e-5)
  22. discard;
  23. vec4 color = vec4(0.0);
  24. // Add the contributions of the possible 4 lines that can cross this pixel:
  25. vec4 C = texture(colorMap, uv);
  26. vec4 Cleft = texture(colorMap, uv_left);
  27. vec4 Ctop = texture(colorMap, uv_top);
  28. vec4 Cright = texture(colorMap, uv_right);
  29. vec4 Cbottom = texture(colorMap, uv_bottom);
  30. color = mix(C, Ctop, a.r) * w.r + color;
  31. color = mix(C, Cbottom, a.g) * w.g + color;
  32. color = mix(C, Cleft, a.b) * w.b + color;
  33. color = mix(C, Cright, a.a) * w.a + color;
  34. // Normalize the resulting color and we are finished!
  35. FragColor = vec4(color / sum);
  36. }