I am trying to replicate blend modes. Currently I have recreated a screen blend mode, but it doesn't seem to work really well with transparent images.
I have two textures
Image 1 (Foreground)
Image 2 (Background)
And the result I get is this
The Compute Shader code
#pragma kernel Blend
RWTexture2D<float4> Result;
Texture2D<float4> TopLayer;
Texture2D<float4> BottomLayer;
[numthreads(8, 8, 1)]
void Blend(uint3 id : SV_DispatchThreadID)
{
if (TopLayer[id.xy].w == 0 && BottomLayer[id.xy].w > 0)
{
Result[id.xy] = BottomLayer[id.xy];
}
else if (BottomLayer[id.xy].w == 0 && TopLayer[id.xy].w > 0)
{
Result[id.xy] = TopLayer[id.xy];
}
else if (BottomLayer[id.xy].w == 0 && TopLayer[id.xy].w == 0)
{
Result[id.xy] = float4(0, 0, 0, 0);
}
else
{
float alpha_final = BottomLayer[id.xy].w + TopLayer[id.xy].w - BottomLayer[id.xy].w * TopLayer[id.xy].w;
float4 tex = 1 - (1 - TopLayer[id.xy]) * (1 - BottomLayer[id.xy]);
Result[id.xy] = float4(tex.x, tex.y, tex.z, alpha_final);
}
}
How do I blend images with different alphas properly?


