EncodeToPNG and a transparent background

Hi!
I’m trying to take a snapshot of a 3d object on a scene with a transparent background, but I have an empty png. I use the same code as everyone else to take the picture (and I use the TextureFormat.ARGB32). The only things that change is my background color. I put Color.clear with the clearFlags SolidColor or Color. It’s working if a put an other color, but not with the color clear.

customCam.backgroundColor = Color.clear;
customCam.clearFlags = CameraClearFlags.Color;

I try to use the clearFlags nothing, and it’s the same result : empty png.

customCam.clearFlags = CameraClearFlags.Nothing;

So, how do I take a snapshot or a screenshot with a transparent background?

Are you trying to use RenderTexture? (a Unity Pro feature)

It is looking like Color.clear doesnt work. You can use the Texture2D.GetPixels and SetPixels. run through all the pixels, if anything is the background color then make it Color.clear. Set the pixels back and apply it, then do your png setup.

Yes, it’s a good idea, but it implies that the background is a different color from the 3d object. And I can not be sure of it.

Have you tried

Yes and it’s not working… The png file is style empty!

I found the answer… It’s not working with a orthographic camera, so I will use the perspective one! It’s not so good looking, but it will work…

Thanks everyone!

You can make a perspective camera look like an orthographic camera, by making the field of view really small, and moving the camera back to compensate.

–Eric