@@ -10,58 +10,71 @@ varying vec4 finalColor;
1010
1111uniform sampler2D texture0;
1212uniform vec2 resolution;
13+
14+ // Fontsize less then 9 may be not complete
1315uniform float fontSize;
1416
15- float greyScale(in vec3 col) {
17+ float GreyScale(in vec3 col)
18+ {
1619 return dot (col, vec3 (0.2126 , 0.7152 , 0.0722 ));
1720}
1821
19- float character (float n, vec2 p)
22+ float GetCharacter (float n, vec2 p)
2023{
21- p = floor (p * vec2 (4.0 , - 4.0 ) + 2.5 );
22-
23- // Check if the coordinate is inside the 5x5 grid (0 to 4).
24- if (clamp (p.x, 0.0 , 4.0 ) == p.x && clamp (p.y, 0.0 , 4.0 ) == p.y) {
25-
26- if (int (mod (n / exp2 (p.x + 5.0 * p.y), 2.0 )) == 1 ) {
27- return 1.0 ; // The bit is on, so draw this part of the character.
24+ p = floor (p* vec2 (- 4.0 , 4.0 ) + 2.5 );
25+
26+ // Check if the calculated coordinate is inside the 5x5 grid (from 0.0 to 4.0)
27+ if (clamp (p.x, 0.0 , 4.0 ) == p.x && clamp (p.y, 0.0 , 4.0 ) == p.y)
28+ {
29+ float a = floor (p.x + 0.5 ) + 5.0 * floor (p.y + 0.5 );
30+
31+ // This checked if the 'a'-th bit of 'n' was set
32+ float shiftedN = floor (n/ pow (2.0 , a));
33+
34+ if (mod (shiftedN, 2.0 ) == 1.0 )
35+ {
36+ return 1.0 ; // The bit is on
2837 }
2938 }
3039
31- return 0.0 ; // The bit is off, or we are outside the grid.
40+ return 0.0 ; // The bit is off, or we are outside the grid
3241}
3342
3443// -----------------------------------------------------------------------------
3544// Main shader logic
3645// -----------------------------------------------------------------------------
46+
3747void main()
3848{
39- vec2 charPixelSize = vec2 (fontSize, fontSize * 1.8 );
40- vec2 uvCellSize = charPixelSize / resolution;
49+ vec2 charPixelSize = vec2 (fontSize, fontSize);
50+ vec2 uvCellSize = charPixelSize/ resolution;
4151
42- vec2 cellUV = floor (fragTexCoord / uvCellSize) * uvCellSize;
52+ // The cell size is based on the fontSize set by application
53+ vec2 cellUV = floor (fragTexCoord/ uvCellSize)* uvCellSize;
4354
4455 vec3 cellColor = texture2D (texture0, cellUV).rgb;
45- float gray = greyScale(cellColor);
4656
47- float n = 4096 ;
57+ // Gray is used to define what character will be selected to draw
58+ float gray = GreyScale(cellColor);
59+
60+ float n = 4096.0 ;
4861
49- // limited character set
62+ // Character set from https://www.shadertoy.com/view/lssGDj
63+ // Create new bitmaps https://thrill-project.com/archiv/coding/bitmap/
5064 if (gray > 0.2 ) n = 65600.0 ; // :
51- if (gray > 0.3 ) n = 163153 .0 ; // *
65+ if (gray > 0.3 ) n = 18725316 .0 ; // v
5266 if (gray > 0.4 ) n = 15255086.0 ; // o
5367 if (gray > 0.5 ) n = 13121101.0 ; // &
5468 if (gray > 0.6 ) n = 15252014.0 ; // 8
5569 if (gray > 0.7 ) n = 13195790.0 ; // @
5670 if (gray > 0.8 ) n = 11512810.0 ; // #
5771
58- vec2 localUV = (fragTexCoord - cellUV) / uvCellSize; // Range [0.0, 1.0]
59-
60- vec2 p = localUV * 2.0 - 1.0 ;
72+ vec2 localUV = (fragTexCoord - cellUV)/ uvCellSize; // Range [0.0, 1.0]
6173
62- float charShape = character(n, p);
74+ vec2 p = localUV * 2.0 - 1.0 ; // Range [-1.0, 1.0]
6375
64- vec3 final_col = cellColor * charShape;
76+ // cellColor and charShape will define the color of the char
77+ vec3 color = cellColor* GetCharacter(n, p);
6578
66- gl_FragColor = vec4 (final_col , 1.0 );
79+ gl_FragColor = vec4 (color , 1.0 );
6780}
0 commit comments