Files
video-shader-toys/shaders/text-overlay/shader.slang
Aiden 7e4ab5cbd8
Some checks failed
CI / Native Windows Build And Tests (pull_request) Failing after 18s
CI / React UI Build (pull_request) Has been cancelled
CI / Windows Release Package (pull_request) Has been cancelled
V1 text, needs improvements
2026-05-05 23:57:02 +10:00

47 lines
1.9 KiB
Plaintext

float alphaOver(float baseAlpha, float overAlpha)
{
return overAlpha + baseAlpha * (1.0 - overAlpha);
}
float4 compositeOver(float4 baseColor, float4 overColor)
{
float outAlpha = alphaOver(baseColor.a, overColor.a);
float3 outRgb = overColor.rgb + baseColor.rgb * (1.0 - overColor.a);
return float4(outRgb, outAlpha);
}
float4 shadeVideo(ShaderContext context)
{
float2 resolution = max(context.outputResolution, float2(1.0, 1.0));
float aspect = resolution.x / resolution.y;
float2 textSize = float2(0.72 * scale, 0.09 * scale * aspect);
float2 safeTextSize = max(textSize, float2(0.0001, 0.0001));
float2 textUv = (context.uv - position) / safeTextSize;
bool insideTextRect = textUv.x >= 0.0 && textUv.x <= 1.0 && textUv.y >= 0.0 && textUv.y <= 1.0;
float mask = insideTextRect ? sampleTitleText(textUv) : 0.0;
float edge = 0.02;
float aa = max(fwidth(mask) * 1.5, 0.002);
float fill = smoothstep(edge - aa, edge + aa, mask);
float shadowRadius = min((outlineWidth + softness) * 0.025, 0.018);
float shadow = 0.0;
if (shadowRadius > 0.0001)
{
shadow = max(shadow, sampleTitleText(textUv + float2(shadowRadius, shadowRadius)));
shadow = max(shadow, sampleTitleText(textUv + float2(-shadowRadius, shadowRadius)));
shadow = max(shadow, sampleTitleText(textUv + float2(shadowRadius, -shadowRadius)));
shadow = max(shadow, sampleTitleText(textUv + float2(-shadowRadius, -shadowRadius)));
}
shadow = smoothstep(edge - aa, edge + aa, shadow) * (0.35 + softness);
float outlineAlpha = saturate(shadow * (1.0 - fill)) * outlineColor.a;
float fillAlpha = fill * fillColor.a;
float textAlpha = max(fillAlpha, outlineAlpha);
if (textAlpha <= 0.0001)
return context.sourceColor;
float4 base = context.sourceColor;
float4 outlineLayer = float4(outlineColor.rgb * outlineAlpha, outlineAlpha);
float4 fillLayer = float4(fillColor.rgb * fillAlpha, fillAlpha);
return saturate(compositeOver(compositeOver(base, outlineLayer), fillLayer));
}