Add outline support to rounded corners shader.

This commit is contained in:
John Preston 2021-05-28 21:05:01 +04:00
parent f475fe28e4
commit 5389de6b96

View file

@ -104,23 +104,30 @@ ShaderPart FragmentRoundCorners() {
return { return {
.header = R"( .header = R"(
uniform vec4 roundRect; uniform vec4 roundRect;
uniform vec2 radiusOutline;
uniform vec4 roundBg; uniform vec4 roundBg;
uniform float roundRadius; uniform vec4 outlineFg;
float roundedCorner() { vec2 roundedCorner() {
vec2 rectHalf = roundRect.zw / 2; vec2 rectHalf = roundRect.zw / 2;
vec2 rectCenter = roundRect.xy + rectHalf; vec2 rectCenter = roundRect.xy + rectHalf;
vec2 fromRectCenter = abs(gl_FragCoord.xy - rectCenter); vec2 fromRectCenter = abs(gl_FragCoord.xy - rectCenter);
vec2 vectorRadius = vec2(roundRadius + 0.5, roundRadius + 0.5); vec2 vectorRadius = radiusOutline.xx + vec2(0.5, 0.5);
vec2 fromCenterWithRadius = fromRectCenter + vectorRadius; vec2 fromCenterWithRadius = fromRectCenter + vectorRadius;
vec2 fromRoundingCenter = max(fromCenterWithRadius, rectHalf) vec2 fromRoundingCenter = max(fromCenterWithRadius, rectHalf)
- rectHalf; - rectHalf;
float d = length(fromRoundingCenter) - roundRadius; float rounded = length(fromRoundingCenter) - radiusOutline.x;
return 1. - smoothstep(0., 1., d); float outline = rounded + radiusOutline.y;
return vec2(
1. - smoothstep(0., 1., rounded),
1. - (smoothstep(0., 1., outline) * outlineFg.a));
} }
)", )",
.body = R"( .body = R"(
float rounded = roundedCorner(); vec2 roundOutline = roundedCorner();
result = result * rounded + roundBg * (1. - rounded); result = result * roundOutline.y
+ vec4(outlineFg.rgb, 1) * (1. - roundOutline.y);
result = result * roundOutline.x + roundBg * (1. - roundOutline.x);
)", )",
}; };
} }