How To Render Transparent Objects To Render Texture

Hello,

I am trying to render some transparent objects to a render texture, but they end up being tinted by the cameras background color (even if alpha is set to 0).

As you can see, we have three white cubes going from opaque to transparent.


These are rendered to a RenderTexture by a camera with the Clear Flag set to red with an Alpha of 0.

This RenderTexture is then rendered on top of the main camera (which is just a blank black background). As you can see, the cubes are tinted red.

How can I render the cubes to a RenderTexture so that they’re transparent but aren’t affected by the red background color?

Thanks in advance

Why is the camera background set to red if you don’t want to use red?
Using white or black might be better.

This is just an example, I’m going to be using this for something a bit more complicated.
I’m trying to be as clear as possible. I don’t want the cubes to be affected by any background color at all.
Regardless of the cube’s color or the background.

Then use black I think. That has no color value

Yes, I already tried that. No matter what the background color is, the transparent object will always be tinted that color. When the color is black, the objects will be darker than they should be.

They are transparent, right? Black means the color is not changed. It might look ā€˜more black’ than the alternatives, but the new background should make the impact.
Does it look very different when you do a black background vs loading the objects directly in the other camera?
If so, can you provide screenshots?

Thank you for the suggestion, but this is an EXAMPLE

I’m going to be using this for something much more complicated.

I’m trying to simplify things to make my question as clear as possible.

I am not actually trying to render 3 transparent cubes. I am merely using them to illustrate the problem I am facing in a different project.

I don’t want the cubes to be affected by any background color at all.

I want the transparent cube’s color to be passed to the render texture unchanged.

I need this to work whether I have purple cubes with a yellow background, or rainbow cubes with a green background.

I don’t understand why.
Why would someone change the background of something which isn’t rendered?

Maybe just use an overlay shader if you just want them to work as an overlay

It is not for you to understand why. Like I said before, I am using this for something much more complicated in another project. All I need is the answer to the question I originally asked.

Also I don’t have an answer or any need to do this at the moment, I wonder why it’s happening.
It seems like it’s a common thing. I remember 3dsmax had the same issue.
It looks like rgb components are rendered using alpha blending and then alpha channel is added on top of that.

Try changing the environment lighting in Rendering/Lighting/Environment settings.

I tried changing that, but it doesn’t do anything. I don’t think the problem is with lighting as the problem happens with unlit materials as well.

Yeah, it’s weird. The especially strange thing is that it doesn’t seem to be blending the rgb normally. I looked up the standard equation to try and ā€˜undo’ the blending with a post-processing shader, but Unity seems to use a different equation.

If you set the background to black then divide rgb by the alpha value it should look okay.

Look up ā€œunpremultiply colorsā€.

You could also render them opaque and then somehow pass the alpha values to the alpha channel (although overlapping transparencies would be completely broken then).

Thanks for the suggestion!

I actually tried that already but was having some weird issues. It works fine with the cube example, but not what I need it for in the other project.

I set the camera to black like you said and made a new cube that uses a custom material.
9397034--1315193--p1.jpg

It’s basically just a transparent unlit material with a texture and color (code on the left). I also made a post-effect shader to fix the colors by dividing by the alpha (code on the right).
9397034--1315187--p3.png

I then set the background camera to the same color as the cube to emphasize the issue:9397034--1315190--p2.png

For some reason there’s a white rim where the alpha blends to zero. Do you have any idea why this might be happening or how to fix it?