Added shaders for UI and fixed UI renderer

This commit is contained in:
Antoine Pilote
2024-09-10 19:18:29 -04:00
parent 1949946cdd
commit 2c48590a4a
9 changed files with 799 additions and 105 deletions

View File

@@ -0,0 +1,116 @@
#shader vertex
#version 460 core
layout(location = 0) in vec3 Position;
layout(location = 1) in vec2 UV;
uniform mat4 u_Model;
uniform mat4 u_View;
out vec2 v_UV;
void main() {
v_UV = UV;
gl_Position = u_View * u_Model * vec4(Position, 1.0f);
}
#shader fragment
#version 460 core
in vec2 v_UV;
out vec4 FragColor;
uniform vec2 u_Size;
uniform vec4 u_Color;
uniform vec4 u_BorderColor;
uniform float u_Border;
uniform float u_BorderRadius;
uniform sampler2D u_BackgroundImage;
uniform float u_HasBackgroundImage;
float ShouldDiscard(vec2 coords, float border, vec2 dimensions, float radius)
{
vec2 circle_center = vec2(radius + border, radius + border) - vec2(0.5, 0.5);
float dst = length(coords - circle_center);
float delta = fwidth(dst);
if (coords.x < circle_center.x && coords.y < circle_center.y)
return 1.0 - smoothstep(radius - delta, radius, dst);
circle_center.x += dimensions.x - 2 * (radius + border) + 1.f;
dst = length(coords - circle_center);
delta = fwidth(dst);
if (coords.x > circle_center.x && coords.y < circle_center.y)
return 1.0 - smoothstep(radius - delta, radius, dst);
circle_center.y += dimensions.y - 2 * (radius + border) + 1.f;
dst = length(coords - circle_center);
delta = fwidth(dst);
if (coords.x > circle_center.x && coords.y > circle_center.y)
return 1.0 - smoothstep(radius - delta, radius, dst);
circle_center.x -= dimensions.x - 2 * (radius + border) + 1.f;
dst = length(coords - circle_center);
delta = fwidth(dst);
if (coords.x < circle_center.x && coords.y > circle_center.y)
return 1.0 - smoothstep(radius - delta, radius, dst);
return 1.f;
}
void main() {
vec2 coords = v_UV * u_Size;
vec2 center = u_Size / 2.0;
vec4 finalColor = u_Color;
vec4 textureColor = texture(u_BackgroundImage, v_UV);
finalColor.rgb = mix(finalColor, textureColor, u_HasBackgroundImage).rgb;
if (u_HasBackgroundImage == 1.0)
{
finalColor.a = textureColor.a;
}
float a = 1.0f;
if(u_BorderRadius >= 0.f)
{
a = min(ShouldDiscard(coords, 0, u_Size, u_BorderRadius + 2), finalColor.a);
if(a == 0.f)
discard;
}
if(u_Border > 0.f)
{
float borderAlpha = ShouldDiscard(coords, u_Border, u_Size, u_BorderRadius);
if(a == 1.f)
{
finalColor = mix(u_BorderColor, finalColor, borderAlpha);
if(coords.x < u_Border || coords.x > u_Size.x - u_Border ||
coords.y < u_Border || coords.y > u_Size.y - u_Border)
{
finalColor = u_BorderColor;
}
else
{
finalColor.a = mix(borderAlpha, textureColor.a, u_HasBackgroundImage);
}
FragColor = finalColor;
return;
}
else
{
finalColor = u_BorderColor;
finalColor.a = max(a, u_BorderColor.a);
FragColor = finalColor;
return;
}
}
finalColor.a = a;
FragColor = finalColor;
}

View File

@@ -0,0 +1,93 @@
#shader vertex
#version 460 core
layout(location = 0) in vec3 Position;
layout(location = 1) in vec2 UV;
uniform mat4 u_Model;
uniform mat4 u_View;
out vec2 v_UV;
void main() {
v_UV = UV;
float x = Position.x;
float y = Position.y;
float z = Position.z;
gl_Position = u_View * u_Model * vec4(x, y, z, 1.0f);
}
#shader fragment
#version 460 core
in sample vec2 v_UV;
out vec4 FragColor;
uniform sampler2D u_Atlas;
uniform sample vec2 u_TexturePos;
uniform vec2 u_TextureScale;
uniform vec4 u_FontColor;
uniform float u_PxRange;
float screenPxRange(vec2 coord) {
vec2 unitRange = vec2(u_PxRange) / vec2(textureSize(u_Atlas, 0));
vec2 screenTexSize = vec2(1.0) / fwidth(coord);
return max(0.5 * dot(unitRange, screenTexSize), 1.0);
}
float median(float r, float g, float b) {
return max(min(r, g), min(max(r, g), b));
}
float opacity(vec4 sdfSample)
{
float r = sdfSample.r;
float g = sdfSample.g;
float b = sdfSample.b;
float signed_dist = median(r, g, b) - 0.5;
float d = fwidth(signed_dist);
float opacity = smoothstep(-d, d, signed_dist);
return opacity;
}
void main() {
vec2 textSize = textureSize(u_Atlas, 0);
vec2 uv = vec2(mix(u_TexturePos.x / textSize.x, u_TextureScale.x / textSize.x, v_UV.x),
mix(u_TextureScale.y / textSize.y, u_TexturePos.y / textSize.y, 1.0 - v_UV.y));
const float subPixelAmount = 1.333;
vec2 unitRange = 1.0 / textSize;
vec2 uvLeft = uv - vec2(unitRange.x * subPixelAmount, 0);
vec2 uvRight = uv + vec2(unitRange.x * subPixelAmount, 0);
vec2 uvUp = uv + vec2(0, unitRange.y * subPixelAmount);
float middle = opacity(texture(u_Atlas, uv));
float left = opacity(texture(u_Atlas, uvLeft));
float right = opacity(texture(u_Atlas, uvRight));
float up = opacity(texture(u_Atlas, uvUp));
vec3 color = u_FontColor.rgb;
bool direction = false;
const float ratio = 1.6666;
const float curve_tolerance = 1.0;
const float h_tolerance = 0;
const bool subpixel = false;
const float subpixel_threshold = 1;
float curveAmount = up - middle;
bool mid = middle < (subpixel_threshold);
bool leftt = left < subpixel_threshold;
bool rightt = right < subpixel_threshold;
if(subpixel &&(middle < subpixel_threshold) && curveAmount < curve_tolerance)
{
color.r = left;
color.g = middle;
color.b = right;
}
FragColor = vec4(color.rgb, middle);
}