Files
raylib/examples/shaders/resources/shaders/glsl330/ascii.fs
Maicon Santana a553fbd0c7 [examples] Fix shaders_ascii_rendering (#5219)
* Add ascii post processing

* Fix broken characters and add more comments

* Rename example

* Refactored as requested
2025-09-30 18:34:06 +02:00

73 lines
1.9 KiB
GLSL

#version 330
// Input from the vertex shader
in vec2 fragTexCoord;
// Output color for the screen
out vec4 finalColor;
uniform sampler2D texture0;
uniform vec2 resolution;
// Fontsize less then 9 may be not complete
uniform float fontSize;
float GreyScale(in vec3 col)
{
return dot(col, vec3(0.2126, 0.7152, 0.0722));
}
float GetCharacter(int n, vec2 p)
{
p = floor(p*vec2(-4.0, 4.0) + 2.5);
// Check if the coordinate is inside the 5x5 grid (0 to 4)
if (clamp(p.x, 0.0, 4.0) == p.x && clamp(p.y, 0.0, 4.0) == p.y)
{
int a = int(round(p.x) + 5.0*round(p.y));
if (((n >> a) & 1) == 1)
{
return 1.0;
}
}
return 0.0; // The bit is off, or we are outside the grid
}
// -----------------------------------------------------------------------------
// Main shader logic
// -----------------------------------------------------------------------------
void main()
{
vec2 charPixelSize = vec2(fontSize, fontSize);
vec2 uvCellSize = charPixelSize/resolution;
// The cell size is based on the fontSize set by application
vec2 cellUV = floor(fragTexCoord/uvCellSize)*uvCellSize;
vec3 cellColor = texture(texture0, cellUV).rgb;
// Gray is used to define what character will be selected to draw
float gray = GreyScale(cellColor);
int n = 4096;
// Character set from https://www.shadertoy.com/view/lssGDj
// Create new bitmaps https://thrill-project.com/archiv/coding/bitmap/
if (gray > 0.2) n = 65600; // :
if (gray > 0.3) n = 18725316; // v
if (gray > 0.4) n = 15255086; // o
if (gray > 0.5) n = 13121101; // &
if (gray > 0.6) n = 15252014; // 8
if (gray > 0.7) n = 13195790; // @
if (gray > 0.8) n = 11512810; // #
vec2 localUV = (fragTexCoord - cellUV)/uvCellSize; // Range [0.0, 1.0]
vec2 p = localUV*2.0 - 1.0; // Range [-1.0, 1.0]
vec3 color = cellColor*GetCharacter(n, p);
finalColor = vec4(color, 1.0);
}