Hello, I am having issues with Graphics.Blit, specifically with alpha compositing.
I have two RenderTextures A, B, and a Material Post. Graphics.Blit(A, B, Post) is used to add post-processing effects to A, and then blit the resulting image onto B. The post-processing basically multiplies A by a certain alpha value, making it transparent by a certain percent (e.g. making the texture 50% transparent). This indeed works when I render A through Post on a SpriteRenderer, and alpha values 0-1 work as they should.
However, when blitting onto B, which is supposed to be fully transparent per Graphics.CopyTexture(EmptyTexture, B) where EmptyTexture is a 960x720 image that’s fully transparent, instead of B becoming that slighty transparent rendition of A, it gains a bit of a black tint. I believe that even though A is being blit onto a fully transparent B, there’s yet another black layer underneath that’s causing B to gain this black tint.
When I process A at 99% alpha, and take the resulting B and get its alpha to 99%, and process that B to A, and repeat about 10 or 100 times, this happens:
to
For some reason, even though the alpha reaches its goal (0.99^100 is approximately 0.366), the rectangle gradually loses its hue, probably due to the black background involved in the Graphics.Blit process. I was hoping there would be a workaround for this, whether it be some configuration I needed to apply, or an alternative to Graphics.Blit.
I attached a video to demonstrate the effect which I am trying to get:
Without the dark hue of course
public RenderTexture A, B;
public Texture C;
public Material _Post;
public void CallRender()
{
if(initialized)
{
if(!rtSwitch) {Graphics.CopyTexture(B, C);}
else {Graphics.CopyTexture(A, C);}
}
}
public void Fade(float multi)
{
if(rtSwitch) {_Post.SetTexture("_ProcessTex", A);}
else {_Post.SetTexture("_ProcessTex", B);}
_Post.SetFloat("_Alpha", Mathf.Sqrt(multi));
if(rtSwitch) {Graphics.CopyTexture(admin.EmptyTexture, B); Graphics.Blit(A, B, _Post, pass:0); Graphics.CopyTexture(B, A);}
else {Graphics.CopyTexture(admin.EmptyTexture, A); Graphics.Blit(B, A, _Post, pass:0); Graphics.CopyTexture(A, B);}
rtSwitch = !rtSwitch;
CallRender();
if(!rtSwitch) {Graphics.CopyTexture(A, C);}
else {Graphics.CopyTexture(B, C);}
}