Refactored as requested

This commit is contained in:
maiconpintoabreu 2025-09-30 11:30:10 +01:00
parent 035628a470
commit 476986ca08
4 changed files with 42 additions and 53 deletions

View File

@ -14,30 +14,30 @@ uniform vec2 resolution;
// Fontsize less then 9 may be not complete // Fontsize less then 9 may be not complete
uniform float fontSize; uniform float fontSize;
float greyScale(in vec3 col) float GreyScale(in vec3 col)
{ {
return dot(col, vec3(0.2126, 0.7152, 0.0722)); return dot(col, vec3(0.2126, 0.7152, 0.0722));
} }
float character(float n, vec2 p) float GetCharacter(float n, vec2 p)
{ {
p = floor(p*vec2(-4.0, 4.0) + 2.5); p = floor(p*vec2(-4.0, 4.0) + 2.5);
// Check if the calculated coordinate is inside the 5x5 grid (from 0.0 to 4.0). // Check if the calculated coordinate is inside the 5x5 grid (from 0.0 to 4.0)
if (clamp(p.x, 0.0, 4.0) == p.x && clamp(p.y, 0.0, 4.0) == p.y) if (clamp(p.x, 0.0, 4.0) == p.x && clamp(p.y, 0.0, 4.0) == p.y)
{ {
float a = floor(p.x + 0.5) + 5.0*floor(p.y + 0.5); float a = floor(p.x + 0.5) + 5.0*floor(p.y + 0.5);
// This checked if the 'a'-th bit of 'n' was set. // This checked if the 'a'-th bit of 'n' was set
float shifted_n = floor(n/pow(2.0, a)); float shiftedN = floor(n/pow(2.0, a));
if (mod(shifted_n, 2.0) == 1.0) if (mod(shiftedN, 2.0) == 1.0)
{ {
return 1.0; // The bit is on. return 1.0; // The bit is on
} }
} }
return 0.0; // The bit is off, or we are outside the grid. return 0.0; // The bit is off, or we are outside the grid
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -47,15 +47,15 @@ float character(float n, vec2 p)
void main() void main()
{ {
vec2 charPixelSize = vec2(fontSize, fontSize); vec2 charPixelSize = vec2(fontSize, fontSize);
vec2 uvCellSize = charPixelSize / resolution; vec2 uvCellSize = charPixelSize/resolution;
// The cell size is based on the fontSize set by application // The cell size is based on the fontSize set by application
vec2 cellUV = floor(fragTexCoord / uvCellSize)*uvCellSize; vec2 cellUV = floor(fragTexCoord/uvCellSize)*uvCellSize;
vec3 cellColor = texture2D(texture0, cellUV).rgb; vec3 cellColor = texture2D(texture0, cellUV).rgb;
// Gray is used to define what character will be selected to draw // Gray is used to define what character will be selected to draw
float gray = greyScale(cellColor); float gray = GreyScale(cellColor);
float n = 4096.0; float n = 4096.0;
@ -73,10 +73,8 @@ void main()
vec2 p = localUV*2.0 - 1.0; // Range [-1.0, 1.0] vec2 p = localUV*2.0 - 1.0; // Range [-1.0, 1.0]
float charShape = character(n, p);
// cellColor and charShape will define the color of the char // cellColor and charShape will define the color of the char
vec3 color = cellColor*charShape; vec3 color = cellColor*GetCharacter(n, p);
gl_FragColor = vec4(color, 1.0); gl_FragColor = vec4(color, 1.0);
} }

View File

@ -12,30 +12,30 @@ uniform vec2 resolution;
// Fontsize less then 9 may be not complete // Fontsize less then 9 may be not complete
uniform float fontSize; uniform float fontSize;
float greyScale(in vec3 col) float GreyScale(in vec3 col)
{ {
return dot(col, vec3(0.2126, 0.7152, 0.0722)); return dot(col, vec3(0.2126, 0.7152, 0.0722));
} }
float character(float n, vec2 p) float GetCharacter(float n, vec2 p)
{ {
p = floor(p*vec2(-4.0, 4.0) + 2.5); p = floor(p*vec2(-4.0, 4.0) + 2.5);
// Check if the calculated coordinate is inside the 5x5 grid (from 0.0 to 4.0). // Check if the calculated coordinate is inside the 5x5 grid (from 0.0 to 4.0)
if (clamp(p.x, 0.0, 4.0) == p.x && clamp(p.y, 0.0, 4.0) == p.y) if (clamp(p.x, 0.0, 4.0) == p.x && clamp(p.y, 0.0, 4.0) == p.y)
{ {
float a = floor(p.x + 0.5) + 5.0*floor(p.y + 0.5); float a = floor(p.x + 0.5) + 5.0*floor(p.y + 0.5);
// This checked if the 'a'-th bit of 'n' was set. // This checked if the 'a'-th bit of 'n' was set
float shifted_n = floor(n/pow(2.0, a)); float shiftedN = floor(n/pow(2.0, a));
if (mod(shifted_n, 2.0) == 1.0) if (mod(shiftedN, 2.0) == 1.0)
{ {
return 1.0; // The bit is on. return 1.0; // The bit is on
} }
} }
return 0.0; // The bit is off, or we are outside the grid. return 0.0; // The bit is off, or we are outside the grid
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -53,7 +53,7 @@ void main()
vec3 cellColor = texture2D(texture0, cellUV).rgb; vec3 cellColor = texture2D(texture0, cellUV).rgb;
// Gray is used to define what character will be selected to draw // Gray is used to define what character will be selected to draw
float gray = greyScale(cellColor); float gray = GreyScale(cellColor);
float n = 4096.0; float n = 4096.0;
@ -71,10 +71,8 @@ void main()
vec2 p = localUV*2.0 - 1.0; // Range [-1.0, 1.0] vec2 p = localUV*2.0 - 1.0; // Range [-1.0, 1.0]
float charShape = character(n, p);
// cellColor and charShape will define the color of the char // cellColor and charShape will define the color of the char
vec3 color = cellColor*charShape; vec3 color = cellColor*GetCharacter(n, p);
gl_FragColor = vec4(color, 1.0); gl_FragColor = vec4(color, 1.0);
} }

View File

@ -12,23 +12,26 @@ uniform vec2 resolution;
// Fontsize less then 9 may be not complete // Fontsize less then 9 may be not complete
uniform float fontSize; uniform float fontSize;
float greyScale(in vec3 col) float GreyScale(in vec3 col)
{ {
return dot(col, vec3(0.2126, 0.7152, 0.0722)); return dot(col, vec3(0.2126, 0.7152, 0.0722));
} }
float character(int n, vec2 p) float GetCharacter(int n, vec2 p)
{ {
p = floor(p*vec2(-4.0, 4.0) + 2.5); p = floor(p*vec2(-4.0, 4.0) + 2.5);
// Check if the coordinate is inside the 5x5 grid (0 to 4). // 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 (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)); int a = int(round(p.x) + 5.0*round(p.y));
if (((n >> a) & 1) == 1) return 1.0; if (((n >> a) & 1) == 1)
{
return 1.0;
}
} }
return 0.0; // The bit is off, or we are outside the grid. return 0.0; // The bit is off, or we are outside the grid
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -38,15 +41,15 @@ float character(int n, vec2 p)
void main() void main()
{ {
vec2 charPixelSize = vec2(fontSize, fontSize); vec2 charPixelSize = vec2(fontSize, fontSize);
vec2 uvCellSize = charPixelSize / resolution; vec2 uvCellSize = charPixelSize/resolution;
// The cell size is based on the fontSize set by application // The cell size is based on the fontSize set by application
vec2 cellUV = floor(fragTexCoord / uvCellSize)*uvCellSize; vec2 cellUV = floor(fragTexCoord/uvCellSize)*uvCellSize;
vec3 cellColor = texture(texture0, cellUV).rgb; vec3 cellColor = texture(texture0, cellUV).rgb;
// Gray is used to define what character will be selected to draw // Gray is used to define what character will be selected to draw
float gray = greyScale(cellColor); float gray = GreyScale(cellColor);
int n = 4096; int n = 4096;
@ -64,9 +67,7 @@ void main()
vec2 p = localUV*2.0 - 1.0; // Range [-1.0, 1.0] vec2 p = localUV*2.0 - 1.0; // Range [-1.0, 1.0]
float charShape = character(n, p); vec3 color = cellColor*GetCharacter(n, p);
vec3 color = cellColor*charShape;
finalColor = vec4(color, 1.0); finalColor = vec4(color, 1.0);
} }

View File

@ -48,14 +48,14 @@ int main(void)
int fontSizeLoc = GetShaderLocation(shader, "fontSize"); int fontSizeLoc = GetShaderLocation(shader, "fontSize");
// Set the character size for the ASCII effect // Set the character size for the ASCII effect
// fontsize should be 9 or more // Fontsize should be 9 or more
float fontSize = 9.0f; float fontSize = 9.0f;
// Send the updated values to the shader // Send the updated values to the shader
float resolution[2] = { (float)screenWidth, (float)screenHeight }; float resolution[2] = { (float)screenWidth, (float)screenHeight };
SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2); SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2);
Vector2 circlePos = (Vector2){40.0f, (float)screenHeight * 0.5f}; Vector2 circlePos = (Vector2){40.0f, (float)screenHeight*0.5f};
float circleSpeed = 1.0f; float circleSpeed = 1.0f;
// RenderTexture to apply the postprocessing later // RenderTexture to apply the postprocessing later
@ -70,19 +70,11 @@ int main(void)
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
if (IsKeyPressed(KEY_LEFT) && fontSize > 9.0) if (IsKeyPressed(KEY_LEFT) && fontSize > 9.0) fontSize -= 1; // Reduce fontSize
{
fontSize -= 1; // Reduce fontSize
}
if (IsKeyPressed(KEY_RIGHT) && fontSize < 15.0) if (IsKeyPressed(KEY_RIGHT) && fontSize < 15.0) fontSize += 1; // Increase fontSize
{
fontSize += 1; // Increase fontSize
}
if (circlePos.x > 200.0f || circlePos.x < 40.0f) { if (circlePos.x > 200.0f || circlePos.x < 40.0f) circleSpeed *= -1; // Revert speed
circleSpeed *= -1;
}
circlePos.x += circleSpeed; circlePos.x += circleSpeed;
@ -106,8 +98,8 @@ int main(void)
BeginShaderMode(shader); BeginShaderMode(shader);
// Draw the scene texture (that we rendered earlier) to the screen. // Draw the scene texture (that we rendered earlier) to the screen
// The shader will process every pixel of this texture. // The shader will process every pixel of this texture
DrawTextureRec(target.texture, DrawTextureRec(target.texture,
(Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height }, (Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height },
(Vector2){ 0, 0 }, (Vector2){ 0, 0 },