asciiart.glsl 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. uniform sampler2D samp8; // textures
  2. uniform sampler2D samp9;
  3. const int char_width = 8;
  4. const int char_height = 13;
  5. const int char_count = 95;
  6. const int char_pixels = char_width*char_height;
  7. const vec2 char_dim = vec2(char_width, char_height);
  8. const vec2 font_scale = vec2(1.0/char_width/char_count, 1.0/char_height);
  9. out vec4 ocol0;
  10. in vec2 uv0;
  11. uniform vec4 resolution;
  12. void main()
  13. {
  14. vec2 char_pos = floor(uv0*resolution.xy/char_dim);
  15. vec2 pixel_offset = floor(uv0*resolution.xy) - char_pos*char_dim;
  16. float mindiff = char_width*char_height*100; // just a big number
  17. float minc = 0.0;
  18. vec4 mina = vec4(0.0, 0.0, 0.0, 0.0);
  19. vec4 minb = vec4(0.0, 0.0, 0.0, 0.0);
  20. for(int i=0; i<char_count; i++) {
  21. vec4 ff = vec4(0.0, 0.0, 0.0, 0.0);
  22. vec4 f = vec4(0.0, 0.0, 0.0, 0.0);
  23. vec4 ft = vec4(0.0, 0.0, 0.0, 0.0);
  24. vec4 t = vec4(0.0, 0.0, 0.0, 0.0);
  25. vec4 tt = vec4(0.0, 0.0, 0.0, 0.0);
  26. for(int x=0; x<char_width; x++) {
  27. for(int y=0; y<char_height; y++) {
  28. vec2 tex_pos = char_pos*char_dim + vec2(x,y) + 0.5;
  29. vec4 tex = texture(samp9, tex_pos * resolution.zw);
  30. vec2 font_pos = vec2(x+i*char_width, y) + 0.5;
  31. vec4 font = texture(samp8, font_pos * font_scale);
  32. // generates sum of texture and font and their squares
  33. ff += font*font;
  34. f += font;
  35. ft += font*tex;
  36. t += tex;
  37. tt += tex*tex;
  38. }
  39. }
  40. /*
  41. The next lines are a bit harder, hf :-)
  42. The idea is to find the perfect char with the perfect background color and the perfect font color.
  43. As this is an equation with three unknowns, we can't just try all chars and color combinations.
  44. As criterion how "perfect" the selection is, we compare the "mean squared error" of the resulted colors of all chars.
  45. So, now the big issue: how to calculate the MSE without knowing the two colors ...
  46. In the next steps, "a" is the font color, "b" is the background color, "f" is the font value at this pixel, "t" is the texture value
  47. So the square error of one pixel is:
  48. e = ( t - a⋅f - b⋅(1-f) ) ^ 2
  49. In longer:
  50. e = a^2⋅f^2 - 2⋅a⋅b⋅f^2 + 2⋅a⋅b⋅f - 2⋅a⋅f⋅t + b^2⋅f^2 - 2⋅b^2⋅f + b^2 + 2⋅b⋅f⋅t - 2⋅b⋅t + t^2
  51. The sum of all errors is: (as shortcut, ff,f,ft,t,tt are now the sums like declared above, sum(1) is the count of pixels)
  52. sum(e) = a^2⋅ff - 2⋅a^2⋅ff + 2⋅a⋅b⋅f - 2⋅a⋅ft + b^2⋅ff - 2⋅b^2⋅f + b^2⋅sum(1) + 2⋅b⋅ft - 2⋅b⋅t + tt
  53. To find the minimum, we have to derive this by "a" and "b":
  54. d/da sum(e) = 2⋅a⋅ff + 2⋅b⋅f - 2⋅b⋅ff - 2⋅ft
  55. d/db sum(e) = 2⋅a⋅f - 2⋅a⋅ff - 4⋅b⋅f + 2⋅b⋅ff + 2⋅b⋅sum(1) + 2⋅ft - 2⋅t
  56. So, both equations must be zero at minimum and there is only one solution.
  57. */
  58. vec4 a = (f*ft - ff*t + f*t - ft*char_pixels) / (f*f - ff*char_pixels);
  59. vec4 b = (f*ft - ff*t) / (f*f - ff*char_pixels);
  60. vec4 diff = a*a*ff + 2.0*a*b*f - 2.0*a*b*ff - 2.0*a*ft + b*b *(-2.0*f + ff + char_pixels) + 2.0*b*ft - 2.0*b*t + tt;
  61. float diff_f = dot(diff, vec4(1.0, 1.0, 1.0, 1.0));
  62. if(diff_f < mindiff) {
  63. mindiff = diff_f;
  64. minc = i;
  65. mina = a;
  66. minb = b;
  67. }
  68. }
  69. vec2 font_pos_res = vec2(minc*char_width, 0) + pixel_offset + 0.5;
  70. vec4 col = texture(samp8, font_pos_res * font_scale);
  71. ocol0 = mina * col + minb * (vec4(1.0,1.0,1.0,1.0) - col);
  72. }