- Home /
Why Camera clear color alpha channel is ignored?
Dear community
I'm struggling to get camera to use alpha channel of clear color. It looks to me it doesn't matter what you set clear color alpha value to, unity will still use 255 instead.
This is primarily for render to texture. I want to use an off-screen render camera to render something to a texture. I need the background of my render texture to remain transparent when this render texture is rendered back into the main scene (a quad with simple material referencing my render texture).
These are my steps so far:
Create a RenderTexture asset, 2D, ARGB32
Create a Camera and set it to render to my RenderTexture
Set camera Clear Flags to Solid Color
Set Background color to say RGBA(255,0,0,0), note alpha=0
Now if I check my render texture in inspector it is showing solid red background as if my clear color was RGBA(255,0,0,255). The non-trasparent background makes it impossible to use as an overlay on top of some other content :(
Any ideas please?
Deferred or Forward pass rendering?
I think this only works with Forward.
No difference. Both deferred and forward rendering produce the same result with alpha channel completely ignored :(
Answer by JonPQ · Aug 24, 2017 at 10:44 PM
I had to do this before... my solution was to not render transparent, but to render it magenta... the old school color for transparency. Then I ran a script to search through the texture2d pixels... and swap out the pixels for my desired transparent color.
Thanks. Technically this might work but will be extremely expensive I'm afraid.
I wonder whats the reason behind this limitation? At the lower level all graphics APIs support 4 channel clear color. Why on earth Unity would suppress that?
probably if the camera is clearing the screen. there is no reason to have any transparency, as there can't possibly be anything behind it. They could add it as an option if you are doing a render to texture.... that would be nice. For me... performance was not an issue with the magenta trick, as I was running it as an editor script, to generate sprites that looked exactly the same as the models in scene... for nice LOD
another option you might employ is same deal... render background as magenta.... Which is still fast. then render the sprite or object with a custom shader... that replaces magenta with transparent alpha in pixel shader. Though I wouldn't call that an efficient shader. Depends if you need to do this on-the-fly every frame or not, for which option is best.
Answer by DomeCreator · Feb 24, 2021 at 03:14 PM
Hi guys, @voidcoder I'm facing the same problem for others reasons but I was wondering you'll find a way to do that without killing perfs ?
Your answer
Follow this Question
Related Questions
Rendering a Pygame surface in Unity 0 Answers
Capture RenderTexture pixels multiple times in one frame? 1 Answer
Multiple Render Texture and Ordering 0 Answers
Render texture and shader issues 0 Answers