pixellate.fs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #version 150
  2. uniform sampler2D source[];
  3. uniform vec4 sourceSize[];
  4. uniform vec4 targetSize;
  5. in Vertex {
  6. vec2 texCoord;
  7. };
  8. out vec4 fragColor;
  9. void main() {
  10. vec2 texelSize = 1.0 * sourceSize[0].zw;
  11. vec2 range = vec2(abs(dFdx(texCoord.x)), abs(dFdy(texCoord.y)));
  12. range = range / 2.0 * 0.999;
  13. float left = texCoord.x - range.x;
  14. float top = texCoord.y + range.y;
  15. float right = texCoord.x + range.x;
  16. float bottom = texCoord.y - range.y;
  17. vec4 topLeftColor = pow(texture(source[0], vec2(left, top)), vec4(2.2));
  18. vec4 bottomRightColor = pow(texture(source[0], vec2(right, bottom)), vec4(2.2));
  19. vec4 bottomLeftColor = pow(texture(source[0], vec2(left, bottom)), vec4(2.2));
  20. vec4 topRightColor = pow(texture(source[0], vec2(right, top)), vec4(2.2));
  21. vec2 border = clamp(round(texCoord / texelSize) * texelSize, vec2(left, bottom), vec2(right, top));
  22. float totalArea = 4.0 * range.x * range.y;
  23. vec4 averageColor;
  24. averageColor = ((border.x - left) * (top - border.y) / totalArea) * topLeftColor;
  25. averageColor += ((right - border.x) * (border.y - bottom) / totalArea) * bottomRightColor;
  26. averageColor += ((border.x - left) * (border.y - bottom) / totalArea) * bottomLeftColor;
  27. averageColor += ((right - border.x) * (top - border.y) / totalArea) * topRightColor;
  28. fragColor = pow(averageColor, vec4(1.0/2.2));
  29. }