Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Transparent RenderTextures

Discussion in 'Graphics Experimental Previews' started by garryjnewman, Jun 18, 2019.

  1. garryjnewman

    garryjnewman

    Joined:
    Sep 11, 2015
    Posts:
    33
    We render to a render texture to get the player model rendered over the UI. This worked in the old pipeline by setting the clear colour to fully transparent.

    In the new pipeline this doesn't see to work:

    ef303f43-25fe-4a49-ba23-b91be884c4bc.jpg

    Is there a hidden setting to do this?
     
  2. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    883
    Just set the alpha of background color of the camera to 0
     
  3. garryjnewman

    garryjnewman

    Joined:
    Sep 11, 2015
    Posts:
    33
    I am doing that
     
  4. Zipe92

    Zipe92

    Joined:
    Mar 5, 2015
    Posts:
    27
    Setting the alpha of the background color to 0 worked until Unity 2018.3, in the HDRP for Unity 2019.1 it doesn't work (or at least it didn't work last time I tried it, 2 months ago).

    I thought it was a bug and I sent a bug report to Unity, they answered me: "we plan to use the alpha channel of the color buffer for future features, we cannot support the output of alpha channel in a render texture. For now, the issue will be present but you can expect new features that will account for the desired behavior in this case"

    You can also find my bug report in the issue tracker: https://issuetracker.unity3d.com/is...ble-is-ignored-when-using-hdrp-5-dot-10-dot-0

    If someone has a fast workaround would be fantastic, currently I'm stuck with Unity 2018.3 for this problem.
     
    PixelJ and garryjnewman like this.
  5. garryjnewman

    garryjnewman

    Joined:
    Sep 11, 2015
    Posts:
    33
    I've managed to hack it for now, it's not ideal but I wrote a greenscreen shader which sets alpha to 0 if it's bright green. It's not ideal but it's a stop gap until there's a real solution.

    c9d9f721-1b45-4373-9484-ec471847015a.png
     
  6. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    649
    We're creating prerendered images, so I'm rendering the camera twice: once with black and once with white. Then I compare the pixels ... if the black background has a pixel == to black and the white background == white, then I know its pure transparent. If both black and white textures have the same pixel color, I know its pure opaque. Im faking alpha value by just taking the difference from the grayscale values. Its good enough to make my textures work.

    But yes. This is absolutely stupid. Really ... there should be a better way of handling this. Considering Unity is moving to doing more things than just games, this feels like a step backwards ... hopefully they actually give us a built in way to get the real alpha values per pixel for a transparent background. I don't care how, it should just be an option somewhere, some how.
     
  7. PixelJ

    PixelJ

    Unity Technologies

    Joined:
    Nov 1, 2018
    Posts:
    216
    I just ran into this. Using universal it looks fine in the editor but doesn't work in a build... sigh
     
    Zipe92 likes this.
  8. PixelJ

    PixelJ

    Unity Technologies

    Joined:
    Nov 1, 2018
    Posts:
    216
    Could you share the shader and what you're attaching it to?
     
    salmanhayat16 and Zipe92 like this.
  9. garryjnewman

    garryjnewman

    Joined:
    Sep 11, 2015
    Posts:
    33
    I render to a RT with a fixed colour background, then render that RT via the UI system using a custom shader that draws transparent if matches that colour.
     
  10. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Yeah that's the general advice I've been dishing out too, it's cos HDRP just reserves camera alpha if I'm not mistaken, assuming you're on HDRP that is and not builtin or other.
     
  11. PixelJ

    PixelJ

    Unity Technologies

    Joined:
    Nov 1, 2018
    Posts:
    216
    That's exactly what I ended up doing! Thanks!
     
  12. karl_kent

    karl_kent

    Joined:
    Feb 1, 2018
    Posts:
    10
    nielsr2 likes this.
  13. ventzs

    ventzs

    Joined:
    Oct 30, 2012
    Posts:
    2
    Any update here? The issuetracker is closed for this one, but i still can't find a solution from unity in the final 19.3 release.
    My list of roadblocks for HDRP is not getting any shorter. I don't want workarounds for what were standard features for years.
     
  14. Grimreaper358

    Grimreaper358

    Joined:
    Apr 8, 2013
    Posts:
    789
    @garryjnewman @karl_kent @ventzs

    To use alpha with render texture you need to be using a version of HDRP with 2019.3. So HDRP 7.x.x then you go to the HDRP asset and change the Color Buffer Format to R16, By default all projects are using R11.

    Unity_2020-01-12_10-47-31.png
     
  15. Bartosz-Bieszka

    Bartosz-Bieszka

    Joined:
    Sep 12, 2015
    Posts:
    20
    and do not using Antialiasing and bloom post process, will work fine :)
     
  16. salmanhayat16

    salmanhayat16

    Joined:
    Nov 19, 2018
    Posts:
    10
    Could you tell how did you do thst?
     
  17. Onsterion

    Onsterion

    Joined:
    Feb 21, 2014
    Posts:
    215
    Any news about this ?
     
  18. Grimreaper358

    Grimreaper358

    Joined:
    Apr 8, 2013
    Posts:
    789
  19. Onsterion

    Onsterion

    Joined:
    Feb 21, 2014
    Posts:
    215
  20. etdofresh_unity

    etdofresh_unity

    Joined:
    Jun 24, 2018
    Posts:
    2
    Hey all! I'm pretty invested in URP, and when I got to making render textures... this totally bummed me out. This problem took an hour or two out of my dev time :(. But thanks to garry's cool workaround of "green screening" the render texture. I took that approach... here is my 2D Sprite Lit Shader graph that I apply to Raw Image UI... [it's weird I couldn't get 2D Unlit shader to work here... but it's probably something I'm doing...] I guess if you have green, you could apply another color too! I've seen much more complex logic too... but this what I did just to get started. Good luck devs!
    upload_2020-12-20_22-41-19.png
    [Red arrow pointing to Raw Image UI with transparency!]
     
    Hellfim and prokhorvlg like this.
  21. etdofresh_unity

    etdofresh_unity

    Joined:
    Jun 24, 2018
    Posts:
    2
    Err. So, yeah, I couldn't choose any color close to green... like yellow... sooo, I over engineered it as I tend to do [using DGordon's idea]... maybe this'll help someone? But, hey! I can choose any color now! Maybe I'll find a better solution as I stumble across future problems in my project! upload_2020-12-20_23-9-38.png
     
  22. decorix

    decorix

    Joined:
    Jun 13, 2021
    Posts:
    2
    I'm using HDRP and unity latest version 2021.2.11f1

    Is alpha rendering broken? if you wanted to render a sequence png format.

    for rendering out to png I tried with time line or Unity recorder?



    > Project Settings > Graphics > Scriptable Render Pipeline Asset)), go to the Rendering section, and set the Color Buffer Format to R16G16B16A16.

    If you have character and just want to render out png sequence with alpha. Where is it defined? why is it so difficult in Unity?

    -in the camera it self I read that you need to set to black and alpha 0. did that nothing.
     
  23. Hellfim

    Hellfim

    Joined:
    Sep 11, 2014
    Posts:
    123
    Hi, guys! I was trying to solve the same issue of rendering from camera with enabled post processes into a RenderTexture and display it. Solution for UnityUI is pretty simple - just add an above shader by etdofresh_unity which removes green color to a RawImage which displays a RenderTexture your camera renders into.
    If you want to render texture with UI Toolkit, then it's much harder as it doesn't have a support for custom shaders yet. I've managed to find a solution which was good enough for me and I also set up a demo project so you can check it.

    https://github.com/Hellfim/TransparentBackgroundRenderer

    Main logic behind this is rendering from camera with solid green color to a source RenderTexture, then copying this result into target RenderTexture via a shader which switches solid green color with zero alpha.
     
    unity_3Vgvc20-AT9hew likes this.
  24. Neosword

    Neosword

    Joined:
    Sep 29, 2018
    Posts:
    1
    Thank you it works.