Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

Rendering screenshot using multiple cameras and one rendertexture

Discussion in 'Editor & General Support' started by BenMeijering, May 14, 2012.

  1. BenMeijering

    BenMeijering

    Joined:
    Dec 1, 2011
    Posts:
    8
    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:

    Code (csharp):
    1.     public static Texture2D getScreenshot(int a_Width, int a_Height)
    2.     {
    3.         List<Camera> cameras = new List<Camera>(Camera.allCameras);
    4.     // sort cameras using Depth
    5.         cameras.Sort(new CameraComparer());
    6.         RenderTexture renderTexture = RenderTexture.GetTemporary(a_Width, a_Height, 24);
    7.         RenderTexture.active = renderTexture;  
    8.  
    9.         foreach (Camera camera in cameras)
    10.         {
    11.             if (camera.enabled)
    12.             {
    13.                 float fov = camera.fov;
    14.                 camera.targetTexture = renderTexture;
    15.                 camera.Render();
    16.                 camera.targetTexture = null;
    17.                 camera.fov = fov;
    18.             }
    19.         }
    20.  
    21.         Texture2D result = new Texture2D(a_Width, a_Height, TextureFormat.ARGB32, false);
    22.         result.ReadPixels(new Rect(0.0f, 0.0f, a_Width, a_Height), 0, 0, false);
    23.         result.Apply();
    24.  
    25.         RenderTexture.active = null;
    26.  
    27.         RenderTexture.ReleaseTemporary(renderTexture);
    28.  
    29.         return result;
    30.     }
    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
     
  2. philip-mk

    philip-mk

    Joined:
    Aug 1, 2012
    Posts:
    2
    I was having the same problem and I fixed it by creating a new RenderTexture(width, height, 24) instead of using a GetTemporary() one. Just don't forget to Destroy(renderTexture) when you are done with it.
     
unityunity