How to render a camera to a PNG

Using RenderTexture, Texture2D and a camera to get a PNG. The other posts on the forum didn’t tie everything together, so after figuring it out, I thought i’d “give back.”

Anyway.

You have a camera, that camera’s target texture is set to a RenderTexture, and a texture2d. then you

thumbCam.Render(); RenderTexture.active = thumbnail; //thumbnail is a RenderTexture thumb_tex = new Texture2D(256, 256); thumb_tex.ReadPixels(thumbCam.pixelRect,0,0); RenderTexture.active = null; thumb_tex.Apply(); var png : byte[ ] = thumb_tex.EncodeToPNG();

Hope this helps… maybe it is something that could be cleaned up and added to the RenderTexture documentation?

First, I want to thank aaron for the post. This was very helpful putting the pieces together for me. I have to admit the description of the RenderTexture.active property totally threw me off.

However, now that I have this working, I’m seeing a very odd bug. My code largely follows aaron’s example, but for some reason on Windows the texture generated from the RenderTexture/Camera is vertically inverted (ie the entire texture is upside down). Doesn’t seem to matter what browser I view the project in. On OS X in Firefox, it looked fine

Any ideas?

Thanks,
Matt

That should DEFINITELY be added to Documentation!
Thanks :).

I dont have a solution, but the same problem! :frowning:

Thats no issue, thats how DX and OpenGL work.
OpenGL has top left as 0,0 point, DX has bottom left (or vice versa, don’t know out of head 100%)

So you need to detect the platform and corresponding scale the UV / material by 1,-1

As for the idea behind this itself: UT has posted a script that allows you to take screenshots on any unity version, not just Pro

Ah, alright… thanks! :slight_smile: