aperture.fs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #version 150
  2. /*
  3. CRT Shader by EasyMode
  4. License: GPL
  5. */
  6. #define SHARPNESS_IMAGE 1.0
  7. #define SHARPNESS_EDGES 3.0
  8. #define GLOW_WIDTH 0.5
  9. #define GLOW_HEIGHT 0.5
  10. #define GLOW_HALATION 0.1
  11. #define GLOW_DIFFUSION 0.05
  12. #define MASK_COLORS 2.0
  13. #define MASK_STRENGTH 0.3
  14. #define MASK_SIZE 1.0
  15. #define SCANLINE_SIZE_MIN 0.5
  16. #define SCANLINE_SIZE_MAX 1.5
  17. #define GAMMA_INPUT 2.4
  18. #define GAMMA_OUTPUT 2.4
  19. #define BRIGHTNESS 1.5
  20. uniform sampler2D source[];
  21. uniform vec4 sourceSize[];
  22. uniform vec4 targetSize;
  23. in Vertex {
  24. vec2 texCoord;
  25. };
  26. out vec4 fragColor;
  27. #define FIX(c) max(abs(c), 1e-5)
  28. #define PI 3.141592653589
  29. #define TEX2D(c) pow(texture(source[0], c).rgb, vec3(GAMMA_INPUT))
  30. #define saturate(c) clamp(c, 0.0, 1.0)
  31. mat3 get_color_matrix(sampler2D tex, vec2 co, vec2 dx)
  32. {
  33. return mat3(TEX2D(co - dx), TEX2D(co), TEX2D(co + dx));
  34. }
  35. vec3 blur(mat3 m, float dist, float rad)
  36. {
  37. vec3 x = vec3(dist - 1.0, dist, dist + 1.0) / rad;
  38. vec3 w = exp2(x * x * -1.0);
  39. return (m[0] * w.x + m[1] * w.y + m[2] * w.z) / (w.x + w.y + w.z);
  40. }
  41. vec3 filter_gaussian(sampler2D tex, vec2 co, vec2 tex_size)
  42. {
  43. vec2 dx = vec2(1.0 / tex_size.x, 0.0);
  44. vec2 dy = vec2(0.0, 1.0 / tex_size.y);
  45. vec2 pix_co = co * tex_size;
  46. vec2 tex_co = (floor(pix_co) + 0.5) / tex_size;
  47. vec2 dist = (fract(pix_co) - 0.5) * -1.0;
  48. mat3 line0 = get_color_matrix(tex, tex_co - dy, dx);
  49. mat3 line1 = get_color_matrix(tex, tex_co, dx);
  50. mat3 line2 = get_color_matrix(tex, tex_co + dy, dx);
  51. mat3 column = mat3(blur(line0, dist.x, GLOW_WIDTH),
  52. blur(line1, dist.x, GLOW_WIDTH),
  53. blur(line2, dist.x, GLOW_WIDTH));
  54. return blur(column, dist.y, GLOW_HEIGHT);
  55. }
  56. vec3 filter_lanczos(sampler2D tex, vec2 co, vec2 tex_size, float sharp)
  57. {
  58. tex_size.x *= sharp;
  59. vec2 dx = vec2(1.0 / tex_size.x, 0.0);
  60. vec2 pix_co = co * tex_size - vec2(0.5, 0.0);
  61. vec2 tex_co = (floor(pix_co) + vec2(0.5, 0.001)) / tex_size;
  62. vec2 dist = fract(pix_co);
  63. vec4 coef = PI * vec4(dist.x + 1.0, dist.x, dist.x - 1.0, dist.x - 2.0);
  64. coef = FIX(coef);
  65. coef = 2.0 * sin(coef) * sin(coef / 2.0) / (coef * coef);
  66. coef /= dot(coef, vec4(1.0));
  67. vec4 col1 = vec4(TEX2D(tex_co), 1.0);
  68. vec4 col2 = vec4(TEX2D(tex_co + dx), 1.0);
  69. return (mat4(col1, col1, col2, col2) * coef).rgb;
  70. }
  71. vec3 get_scanline_weight(float x, vec3 col)
  72. {
  73. vec3 beam = mix(vec3(SCANLINE_SIZE_MIN), vec3(SCANLINE_SIZE_MAX), col);
  74. vec3 x_mul = 2.0 / beam;
  75. vec3 x_offset = x_mul * 0.5;
  76. return smoothstep(0.0, 1.0, 1.0 - abs(x * x_mul - x_offset)) * x_offset;
  77. }
  78. vec3 get_mask_weight(float x)
  79. {
  80. float i = mod(floor(x * targetSize.x * sourceSize[0].x / (sourceSize[0].x * MASK_SIZE)), MASK_COLORS);
  81. if (i == 0.0) return mix(vec3(1.0, 0.0, 1.0), vec3(1.0, 0.0, 0.0), MASK_COLORS - 2.0);
  82. else if (i == 1.0) return vec3(0.0, 1.0, 0.0);
  83. else return vec3(0.0, 0.0, 1.0);
  84. }
  85. void main() {
  86. vec3 col_glow = filter_gaussian(source[0], texCoord, sourceSize[0].xy);
  87. vec3 col_soft = filter_lanczos(source[0], texCoord, sourceSize[0].xy, SHARPNESS_IMAGE);
  88. vec3 col_sharp = filter_lanczos(source[0], texCoord, sourceSize[0].xy, SHARPNESS_EDGES);
  89. vec3 col = sqrt(col_sharp * col_soft);
  90. col *= get_scanline_weight(fract(texCoord.y * sourceSize[0].y), col_soft);
  91. col_glow = saturate(col_glow - col);
  92. col += col_glow * col_glow * GLOW_HALATION;
  93. col = mix(col, col * get_mask_weight(texCoord.x) * MASK_COLORS, MASK_STRENGTH);
  94. col += col_glow * GLOW_DIFFUSION;
  95. col = pow(col * BRIGHTNESS, vec3(1.0 / GAMMA_OUTPUT));
  96. fragColor = vec4(col, 1.0);
  97. }