asciiart.glsl 3.4 KB

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