Search Unity

Question Graphics.Blit(RenderTexture A, RenderTexture B, Material Post) alpha compositing fails

Discussion in 'Universal Render Pipeline' started by mycatsarefat369, Mar 24, 2023.

  1. mycatsarefat369

    mycatsarefat369

    Joined:
    Aug 9, 2021
    Posts:
    10
    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:
    upload_2023-3-24_7-48-29.png to upload_2023-3-24_7-49-5.png
    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

    Code (CSharp):
    1. public RenderTexture A, B;
    2. public Texture C;
    3. public Material _Post;
    4.  
    5. public void CallRender()
    6. {
    7.     if(initialized)
    8.     {
    9.         if(!rtSwitch) {Graphics.CopyTexture(B, C);}
    10.         else {Graphics.CopyTexture(A, C);}
    11.     }
    12. }
    13.  
    14. public void Fade(float multi)
    15. {
    16.     if(rtSwitch) {_Post.SetTexture("_ProcessTex", A);}
    17.     else {_Post.SetTexture("_ProcessTex", B);}
    18.     _Post.SetFloat("_Alpha", Mathf.Sqrt(multi));
    19.  
    20.     if(rtSwitch) {Graphics.CopyTexture(admin.EmptyTexture, B); Graphics.Blit(A, B, _Post, pass:0); Graphics.CopyTexture(B, A);}
    21.     else {Graphics.CopyTexture(admin.EmptyTexture, A); Graphics.Blit(B, A, _Post, pass:0); Graphics.CopyTexture(A, B);}
    22.     rtSwitch = !rtSwitch;
    23.  
    24.     CallRender();
    25.  
    26.     if(!rtSwitch) {Graphics.CopyTexture(A, C);}
    27.     else {Graphics.CopyTexture(B, C);}
    28. }
     

    Attached Files:

    Last edited: Mar 24, 2023
  2. mycatsarefat369

    mycatsarefat369

    Joined:
    Aug 9, 2021
    Posts:
    10
    upload_2023-3-24_8-8-30.png
    It only let me upload 5 images but this is the shader. All it really does is multiply onto the alpha of any texture.