mirror of
https://github.com/raysan5/raylib.git
synced 2025-10-06 18:06:28 +00:00
Add ascii post processing (#5213)
This commit is contained in:
67
examples/shaders/resources/shaders/glsl100/ascii.fs
Normal file
67
examples/shaders/resources/shaders/glsl100/ascii.fs
Normal file
@@ -0,0 +1,67 @@
|
||||
#version 100
|
||||
|
||||
precision mediump float;
|
||||
|
||||
// Input from the vertex shader
|
||||
varying vec2 fragTexCoord;
|
||||
|
||||
// Output color for the screen
|
||||
varying vec4 finalColor;
|
||||
|
||||
uniform sampler2D texture0;
|
||||
uniform vec2 resolution;
|
||||
uniform float fontSize;
|
||||
|
||||
float greyScale(in vec3 col) {
|
||||
return dot(col, vec3(0.2126, 0.7152, 0.0722));
|
||||
}
|
||||
|
||||
float character(float 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) {
|
||||
|
||||
if (int(mod(n / exp2(p.x + 5.0 * p.y), 2.0)) == 1) {
|
||||
return 1.0; // The bit is on, so draw this part of the character.
|
||||
}
|
||||
}
|
||||
|
||||
return 0.0; // The bit is off, or we are outside the grid.
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Main shader logic
|
||||
// -----------------------------------------------------------------------------
|
||||
void main()
|
||||
{
|
||||
vec2 charPixelSize = vec2(fontSize, fontSize * 1.8);
|
||||
vec2 uvCellSize = charPixelSize / resolution;
|
||||
|
||||
vec2 cellUV = floor(fragTexCoord / uvCellSize) * uvCellSize;
|
||||
|
||||
vec3 cellColor = texture2D(texture0, cellUV).rgb;
|
||||
float gray = greyScale(cellColor);
|
||||
|
||||
float n = 4096;
|
||||
|
||||
// limited character set
|
||||
if (gray > 0.2) n = 65600.0; // :
|
||||
if (gray > 0.3) n = 163153.0; // *
|
||||
if (gray > 0.4) n = 15255086.0; // o
|
||||
if (gray > 0.5) n = 13121101.0; // &
|
||||
if (gray > 0.6) n = 15252014.0; // 8
|
||||
if (gray > 0.7) n = 13195790.0; // @
|
||||
if (gray > 0.8) n = 11512810.0; // #
|
||||
|
||||
vec2 localUV = (fragTexCoord - cellUV) / uvCellSize; // Range [0.0, 1.0]
|
||||
|
||||
vec2 p = localUV * 2.0 - 1.0;
|
||||
|
||||
float charShape = character(n, p);
|
||||
|
||||
vec3 final_col = cellColor * charShape;
|
||||
|
||||
gl_FragColor = vec4(final_col, 1.0);
|
||||
}
|
65
examples/shaders/resources/shaders/glsl120/ascii.fs
Normal file
65
examples/shaders/resources/shaders/glsl120/ascii.fs
Normal file
@@ -0,0 +1,65 @@
|
||||
#version 120
|
||||
|
||||
// Input from the vertex shader
|
||||
varying vec2 fragTexCoord;
|
||||
|
||||
// Output color for the screen
|
||||
varying vec4 finalColor;
|
||||
|
||||
uniform sampler2D texture0;
|
||||
uniform vec2 resolution;
|
||||
uniform float fontSize;
|
||||
|
||||
float greyScale(in vec3 col) {
|
||||
return dot(col, vec3(0.2126, 0.7152, 0.0722));
|
||||
}
|
||||
|
||||
float character(float 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) {
|
||||
|
||||
if (int(mod(n / exp2(p.x + 5.0 * p.y), 2.0)) == 1) {
|
||||
return 1.0; // The bit is on, so draw this part of the character.
|
||||
}
|
||||
}
|
||||
|
||||
return 0.0; // The bit is off, or we are outside the grid.
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Main shader logic
|
||||
// -----------------------------------------------------------------------------
|
||||
void main()
|
||||
{
|
||||
vec2 charPixelSize = vec2(fontSize, fontSize * 1.8);
|
||||
vec2 uvCellSize = charPixelSize / resolution;
|
||||
|
||||
vec2 cellUV = floor(fragTexCoord / uvCellSize) * uvCellSize;
|
||||
|
||||
vec3 cellColor = texture2D(texture0, cellUV).rgb;
|
||||
float gray = greyScale(cellColor);
|
||||
|
||||
float n = 4096;
|
||||
|
||||
// limited character set
|
||||
if (gray > 0.2) n = 65600.0; // :
|
||||
if (gray > 0.3) n = 163153.0; // *
|
||||
if (gray > 0.4) n = 15255086.0; // o
|
||||
if (gray > 0.5) n = 13121101.0; // &
|
||||
if (gray > 0.6) n = 15252014.0; // 8
|
||||
if (gray > 0.7) n = 13195790.0; // @
|
||||
if (gray > 0.8) n = 11512810.0; // #
|
||||
|
||||
vec2 localUV = (fragTexCoord - cellUV) / uvCellSize; // Range [0.0, 1.0]
|
||||
|
||||
vec2 p = localUV * 2.0 - 1.0;
|
||||
|
||||
float charShape = character(n, p);
|
||||
|
||||
vec3 final_col = cellColor * charShape;
|
||||
|
||||
gl_FragColor = vec4(final_col, 1.0);
|
||||
}
|
66
examples/shaders/resources/shaders/glsl330/ascii.fs
Normal file
66
examples/shaders/resources/shaders/glsl330/ascii.fs
Normal file
@@ -0,0 +1,66 @@
|
||||
#version 330
|
||||
|
||||
// Input from the vertex shader
|
||||
in vec2 fragTexCoord;
|
||||
|
||||
// Output color for the screen
|
||||
out vec4 finalColor;
|
||||
|
||||
uniform sampler2D texture0;
|
||||
uniform vec2 resolution;
|
||||
uniform float fontSize;
|
||||
|
||||
float greyScale(in vec3 col) {
|
||||
return dot(col, vec3(0.2126, 0.7152, 0.0722));
|
||||
}
|
||||
|
||||
float character(float 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) {
|
||||
|
||||
if (int(mod(n / exp2(p.x + 5.0 * p.y), 2.0)) == 1) {
|
||||
return 1.0; // The bit is on, so draw this part of the character.
|
||||
}
|
||||
}
|
||||
|
||||
return 0.0; // The bit is off, or we are outside the grid.
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Main shader logic
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
void main()
|
||||
{
|
||||
vec2 charPixelSize = vec2(fontSize, fontSize * 1.8);
|
||||
vec2 uvCellSize = charPixelSize / resolution;
|
||||
|
||||
vec2 cellUV = floor(fragTexCoord / uvCellSize) * uvCellSize;
|
||||
|
||||
vec3 cellColor = texture(texture0, cellUV).rgb;
|
||||
float gray = greyScale(cellColor);
|
||||
|
||||
float n = 4096;
|
||||
|
||||
// limited character set
|
||||
if (gray > 0.2) n = 65600.0; // :
|
||||
if (gray > 0.3) n = 163153.0; // *
|
||||
if (gray > 0.4) n = 15255086.0; // o
|
||||
if (gray > 0.5) n = 13121101.0; // &
|
||||
if (gray > 0.6) n = 15252014.0; // 8
|
||||
if (gray > 0.7) n = 13195790.0; // @
|
||||
if (gray > 0.8) n = 11512810.0; // #
|
||||
|
||||
vec2 localUV = (fragTexCoord - cellUV) / uvCellSize; // Range [0.0, 1.0]
|
||||
|
||||
vec2 p = localUV * 2.0 - 1.0;
|
||||
|
||||
float charShape = character(n, p);
|
||||
|
||||
vec3 final_col = cellColor * charShape;
|
||||
|
||||
finalColor = vec4(final_col, 1.0);
|
||||
}
|
Reference in New Issue
Block a user