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();
}