- Home /
help needed trying to save render texture as png
I have two cameras.
1 - main camera which rotates around my scene and shows the GUI.
2 - small thumbnail camera in a fixed position doesn't show UI, and has it's target set to a render texture.
Camera #2 works perfectly as a picture-in-picture camera, but I can't figure out how to save the render texture to a png; using this camera's position and rotation, etc.
The code I have (below) is attached to my thumbnail (#2) camera and it will render a png, however it's not what camera #2 sees, it's whatever the main camera (#1) sees; which is a completely different point-of-view and it includes the UI.
I simply want to read the pixels of the render texture (which is assigned to thumbnail #2 camera and it looks perfect) and write them out. I don't understand what I'm doing wrong, or maybe this isn't possible.
Here's the code I've pieced together from stuff found on the web:
public IEnumerator TakeScreenShot()
{
int startPosX = (Screen.width - slotTexture.width) / 2;
int startPosY = (Screen.height - slotTexture.height) / 2;
Camera camOV = OVcamera.GetComponent<Camera>();
Texture2D imageOverview = new Texture2D(camOV.targetTexture.width, camOV.targetTexture.height);
RenderTexture currentRT = RenderTexture.active;
RenderTexture.active = camOV.targetTexture;
camOV.Render();
yield return new WaitForEndOfFrame();
imageOverview.ReadPixels(new Rect(startPosX, startPosY, slotTexture.width, slotTexture.height), 0, 0);
imageOverview.Apply();
RenderTexture.active = currentRT;
// Encode texture into PNG
byte[] bytes = imageOverview.EncodeToPNG();
path = Application.persistentDataPath + "/" + filename;
System.IO.File.WriteAllBytes(path, bytes);
}
Anyone have an idea how this can be done?
Seems like I should be able to have several cameras (in various locations) each with render textures assigned and then save them out.
Help!