Camera.Render() producing black bars on 4:3 screen resolutions?

Hi,

I’m using the Camera.Render() method to render an image of a scene onto a RenderTexture.
I have set the size of the RenderTexture to 1920x1080. This is how it normally looks:

For some reason, this happens if you play the game on a 4:3 resolution:

Note how only some SpriteRenderers are shown just fine even when outside the 4:3 frustrum (i.e. the balloons), while the background is not.
Both the background and objects are rendered using a SpriteRenderer at z position 0 on the same LayerMask.

Here’s where I started losing my mind, though.

For whatever reason, changing the Occlusion Culling field on the camera through the inspector while the game is running results in the image rendering perfectly fine, and the black bars are suddenly gone.


The above screenshot was taken on 4:3 using the exact same setup. The only difference is that I set occlusion culling to the opposite of its initial value while the game was running.

And no, simply disabling occlusion culling altogether does not fix the problem.
You have to change the value at runtime through the inspector for some reason.

Can someone explain this black magic to me? How can I prevent the camera from rendering these black bars?

Here is the code I use for this; it’s quite simply, really.

public const int Width = 1920;
public const int Height = 1080;

public byte[] TakeScreenshot()
{
    RenderTexture rt = new RenderTexture(Width, Height, 24);
    Texture2D screenShot = new Texture2D(Width, Height, TextureFormat.RGB24, false);

    cam.targetTexture = rt;
    cam.aspect = (float)Width / Height;
    cam.Render();

    RenderTexture.active = rt;
    screenShot.ReadPixels(new Rect(0, 0, Width, Height), 0, 0);
    cam.targetTexture = null;
    RenderTexture.active = null;
    Destroy(rt);

    return screenShot.EncodeToPNG();
}

Any reason you’re not just using this?

You can trivially set your Game window to any arbitrary resolution, run the game and call the above.

BONUS: the above also gets IMGUI calls

I don’t see how using this instead would fix anything. I’m not going to force all players to use a 1920x1080 resolution, so the exact same issue would still happen. Besides, I’m not having any problems with UI.

I only want to capture specific layers, and I want the image to be the same resolution, regardless of your screen resolution; and my current method does that perfectly. The only issue I have is that the Camera.Render() method for some reason does not properly render the full image when you use a 4:3 screen resolution unless I manually click the Occlusion Culling field in the inspector at runtime. This makes no sense and shouldn’t happen. It should always capture a full 1920x1080 image. Your screen resolution should in no way affect the camera I’m using to render this image. It’s not using the main camera.

That’s what I specify in the code, but the expected result only happens if I manually update a property in the inspector.