Files
video-shader-toys/shaders/greenscreen-key/shader.slang
2026-05-02 20:24:40 +10:00

41 lines
1.3 KiB
Plaintext

float3 safeNormalize(float3 value)
{
float len = max(length(value), 0.0001);
return value / len;
}
float luma709(float3 color)
{
return dot(color, float3(0.2126, 0.7152, 0.0722));
}
float4 shadeVideo(ShaderContext context)
{
float4 src = context.sourceColor;
float3 color = saturate(src.rgb);
float3 keyColor = safeNormalize(max(screenColor.rgb, float3(0.0001, 0.0001, 0.0001)));
float3 sampleColor = safeNormalize(max(color, float3(0.0001, 0.0001, 0.0001)));
float chromaDistance = length(sampleColor - keyColor);
float matteCenter = threshold - erodeDilate;
float matteFeather = max(softness + edgeSoftness, 0.0005);
float alpha = smoothstep(matteCenter - matteFeather, matteCenter + matteFeather, chromaDistance);
alpha = saturate((alpha - clipBlack) / max(clipWhite - clipBlack, 0.0001));
alpha = saturate(alpha + edgeBoost);
float greenExcess = max(0.0, color.g - max(color.r, color.b));
float spillReduction = greenExcess * despill;
float3 despilled = color;
despilled.g = max(0.0, despilled.g - spillReduction);
float neutral = luma709(despilled);
despilled.rb += spillReduction * 0.25;
despilled = lerp(float3(neutral, neutral, neutral), despilled, 0.92);
float3 premultiplied = saturate(despilled) * alpha;
return float4(premultiplied, alpha);
}