- Home /
How to get a camera to render to render texture at a custom aspect ratio.
I am rendering to a render texture to display in game.
I want this to be at 4:3 aspect ratio whilst the game is 16:9
If I set the render texture to a 4:3 ratio and the camera viewport to a 4:3 ratio I would expect to get a 4:3 image. Instead I am getting a letterboxed 16:9 image stretched to fit 4:3.
My current solution is to use a shader to crop the image and then stretch the shape.
This is obviously bad.
Has anyone got a better solution to this? Ideally I want to change the aspect ratio of the camera and then have it render everything in the viewport to a render texture without the letterboxing.
Changing the aspect ratio (in code) of the camera doesn't do anything other than break calculations.
Field of view is tied to the aspect ratio of the camera so I can't edit it to change the shape of the frustrum.
The image output is being used very specifically so 4:3 for both output and camera is necessary and black bars cannot be easily hidden.
Cheers folks.
Answer by PWilsonPennant · May 23, 2019 at 09:59 AM
I eventually figured out how to do this so I'll leave the answer here in case anyone else stumbles across it.
Change the camera viewport width to get the aspect ratio you need.
after applying the mainTexture to the material set mainTextureScale to:
.material.mainTextureScale = new Vector2(m_Camera.rect.width, m_Camera.rect.height);
where m_Camera is the name of your camera. This will stretch the texture to fit the quad (no more letterboxing) you can then transform the quad as you see fit.
Your answer
Follow this Question
Related Questions
How to force the game to be stretched to fit the screen? 1 Answer
Changing camera aspect ratio with non-fullscreen viewport 0 Answers
Two cameras rendering to same viewport rect sometimes causes lower camera to render incorrectly 1 Answer
Ortho camera viewport setup? 3 Answers
RenderTarget in scripts 0 Answers