Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

(LWRP) Need transparent background for camera

Discussion in 'Universal Render Pipeline' started by joshcamas, Jun 10, 2019.

  1. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,276
    Hello,

    I have a little script that generates icons for objects by creating a camera and renders it to a render texture. However I need these icons to be transparent. However, due to some fun changes with LWRP, this seems to no longer be possible using the old way. Is there any way to achieve this goal?

    Josh
     
  2. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,276
    Alright, I figured it out. Turns out while rendering transparency doesn't work when rendering cameras normally (solid color), it does for render texture. Not sure why. But it works! :D :D
     
  3. tomweiland

    tomweiland

    Joined:
    Dec 15, 2017
    Posts:
    19
    Could you elaborate on how you pulled this off? I have the exact same problem, for the same reason (I'd like to make icons for items). Using URP.

    I've tried
    Code (CSharp):
    1. GetComponent<Camera>().backgroundColor = Color.clear;
    2. Texture2D screenshot = ScreenCapture.CaptureScreenshotAsTexture();
    As well as
    Code (CSharp):
    1. Camera camera = GetComponent<Camera>();
    2. int w = Screen.width;
    3. int h = Screen.height;
    4. RenderTexture rt = new RenderTexture(w, h, 32);
    5. camera.targetTexture = rt;
    6. Texture2D screenShot = new Texture2D(w, h, TextureFormat.ARGB32, false);
    7. CameraClearFlags clearFlags = camera.clearFlags;
    8. if (isAlphaBackground)
    9. {
    10.     camera.clearFlags = CameraClearFlags.SolidColor;
    11.     camera.backgroundColor = Color.clear;
    12. }
    13. camera.Render();
    14. RenderTexture.active = rt;
    15. screenShot.ReadPixels(new Rect(0, 0, w, h), 0, 0);
    16. screenShot.Apply();
    17. camera.targetTexture = null;
    18. RenderTexture.active = null;
    19. DestroyImmediate(rt);
    20. camera.clearFlags = clearFlags;
    21. return screenShot;
    But both of these just produce screenshots with a black opaque background. The second (render texture) method has the additional problem of not including post processing effects in the screenshot (UI is also not rendered, but not a necessity).

    Any tips/pointers would be greatly appreciated :)
     
  4. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,276
    I remember doing this a long time ago in builtin. Now I've switched to URP, and still have my cameras correctly render - HOWEVER: Post processing must be disabled. Any unity post processing will not include the alpha channel.
    However you mentioned it doesn't include post processing effects, which is odd.
     
  5. tomweiland

    tomweiland

    Joined:
    Dec 15, 2017
    Posts:
    19
    I came back to this recently and also noticed that turning post processing off makes the background render as transparent, but I need my post processing effects :(
     
  6. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,276
    Here is a thread that goes into detail with the problem. I found a solution, which involved modifying a few URP post processing shaders. It's not perfect, but it can work in certain circumstances: https://forum.unity.com/threads/pos...cameras-is-currently-very-problematic.1028533
     
    tomweiland likes this.