- Home /
Rendering screenshot using multiple cameras and one rendertexture
Hi everyone,
I want to be able to take a screenshot with arbitrary dimensions. All enabled cameras must be used. What I have so far is this:
public static Texture2D getScreenshot(int a_Width, int a_Height)
{
List<Camera> cameras = new List<Camera>(Camera.allCameras);
// sort cameras using Depth
cameras.Sort(new CameraComparer());
RenderTexture renderTexture = RenderTexture.GetTemporary(a_Width, a_Height, 24);
RenderTexture.active = renderTexture;
foreach (Camera camera in cameras)
{
if (camera.enabled)
{
float fov = camera.fov;
camera.targetTexture = renderTexture;
camera.Render();
camera.targetTexture = null;
camera.fov = fov;
}
}
Texture2D result = new Texture2D(a_Width, a_Height, TextureFormat.ARGB32, false);
result.ReadPixels(new Rect(0.0f, 0.0f, a_Width, a_Height), 0, 0, false);
result.Apply();
RenderTexture.active = null;
RenderTexture.ReleaseTemporary(renderTexture);
return result;
}
The testcase I am using right now has two cameras. One that renders the world, and a second one that renders the sky and clouds (rendered first with a more distant far clipping plane). The result I am getting is the following:
The sky seems to be rendered upside down. But that's not the only thing that's going on ,the sky is a sphere, so it seems odd that the world is also mirrored. Probably from a previous render to the rendertexture ? Also, the sky seems to be too white.
Does anyone have any experience in this area ?
Any help would be appreciated.
Cheers,
Ben
I am new to Unity, but I am confused why you need RenderTexture.active AND camera.targetTexture. I would think camera.targetTexture was enough, but it does not appear so.
Your answer
![](https://koobas.hobune.stream/wayback/20220613071412im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Wierd Screen Shot Problem, texture of screen shot looks different in editor and in system. 1 Answer
Problem with screenshot on render texture 0 Answers
Taking huge screenshots using camera array? 3 Answers
Taking screenshot from a second camera 0 Answers
Render Splitscreen to RenderTexture 0 Answers