.
This commit is contained in:
84
PresenterConsole/assets/shader/ascii.shader
Normal file
84
PresenterConsole/assets/shader/ascii.shader
Normal file
@@ -0,0 +1,84 @@
|
||||
// #type vertex
|
||||
#version 460 core
|
||||
|
||||
layout (location = 0) in vec2 aPos;
|
||||
layout (location = 1) in vec2 aUV;
|
||||
|
||||
layout (location = 0) out vec2 oUV;
|
||||
|
||||
void main()
|
||||
{
|
||||
oUV = aUV;
|
||||
gl_Position = vec4(aPos, 0.0, 1.0);
|
||||
}
|
||||
|
||||
// #type fragment
|
||||
#version 460 core
|
||||
|
||||
precision highp float;
|
||||
|
||||
// All components are in the range [0…1], including hue.
|
||||
vec3 rgb2hsv(vec3 c)
|
||||
{
|
||||
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
|
||||
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
|
||||
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
|
||||
|
||||
float d = q.x - min(q.w, q.y);
|
||||
float e = 1.0e-10;
|
||||
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
|
||||
}
|
||||
|
||||
// 16 Windows color palette (approximated)
|
||||
const vec3 windowsPalette[7] = vec3[](
|
||||
vec3(rgb2hsv(vec3(0.0, 0.0, 1.0)).rg, 0.0), // Blue (FOREGROUND_BLUE)
|
||||
vec3(rgb2hsv(vec3(0.0, 1.0, 0.0)).rg, 0.0), // Green (FOREGROUND_GREEN)
|
||||
vec3(rgb2hsv(vec3(0.0, 1.0, 1.0)).rg, 0.0), // Cyan (FOREGROUND_GREEN | FOREGROUND_BLUE)
|
||||
vec3(rgb2hsv(vec3(1.0, 0.0, 0.0)).rg, 0.0), // Red (FOREGROUND_RED)
|
||||
vec3(rgb2hsv(vec3(1.0, 0.0, 1.0)).rg, 0.0), // Magenta (FOREGROUND_RED | FOREGROUND_BLUE)
|
||||
vec3(rgb2hsv(vec3(1.0, 1.0, 0.0)).rg, 0.0), // Yellow (FOREGROUND_RED | FOREGROUND_GREEN)
|
||||
vec3(rgb2hsv(vec3(1.0, 1.0, 1.0)).rg, 0.0) // White (all colors set)
|
||||
);
|
||||
|
||||
// Find the closest color in the Windows palette
|
||||
int findClosestColor(vec3 color) {
|
||||
int closestIndex = 0;
|
||||
float minDistance = distance(color, windowsPalette[0]);
|
||||
|
||||
for (int i = 1; i < 7; i++) {
|
||||
float dist = distance(color, windowsPalette[i]);
|
||||
if (dist < minDistance) {
|
||||
minDistance = dist;
|
||||
closestIndex = i;
|
||||
}
|
||||
}
|
||||
|
||||
return closestIndex + 1;
|
||||
}
|
||||
|
||||
float calculateLuminosity(vec3 color) {
|
||||
// Standard luminosity calculation
|
||||
float luminosity = dot(color, vec3(0.299, 0.587, 0.114));
|
||||
return clamp(luminosity, 0.0, 1.0);
|
||||
}
|
||||
|
||||
uniform sampler2D uInputTexture;
|
||||
|
||||
layout (location = 0) in vec2 iUV;
|
||||
layout (location = 0) out vec4 FragColor;
|
||||
|
||||
void main() {
|
||||
vec3 pixelColor = texture(uInputTexture, iUV).rgb;
|
||||
vec3 pixelColorHsv = rgb2hsv(pixelColor);
|
||||
|
||||
// Find closest color index (4 bits)
|
||||
int colorIndex = findClosestColor(vec3(pixelColorHsv.rg, 0));
|
||||
|
||||
// Calculate luminosity (4 bits)
|
||||
float luminosity = calculateLuminosity(pixelColor);
|
||||
|
||||
// Combine into a single byte-like value
|
||||
// High 4 bits: color index
|
||||
// Low 4 bits: luminosity
|
||||
FragColor = vec4(luminosity, colorIndex / 255.0, 0, 1);
|
||||
}
|
||||
Reference in New Issue
Block a user