- Home /
Problem with Texture Scaling for Mobile Resolutions
I am currently having an issue dealing with a RenderTexture's plane not scaling correctly to fit the contents within the screen. My current setup is as follows: I have two camera's. One to render to the screen and another to capture the "Game Area" and render it to a texture. I then want to scale the plane with the RenderTexture applied to it such that I completely fit the "Game Area" to the width of the screen.
To clarify what I mean by "Game Area", the RenderTexture MUST be a square, but my background image is not. So I fit the largest dimension (either width or height) completely within the RenderTexture camera's bounds. The "Game Area" is the portion of the square that represents my background image. All the dead space on both the left and right is not part of the "Game Area".
This is my current scene for clarification (they are further apart than this):
On the left of that image is my Screen Camera with RenderTexture plane and on the right is the RenderTexture Camera with background image for testing. The size of the background image is 1600x2560 for reference. I am currently scaling the plane on the left with the following code:
private void FitTargetToScreen() {
float gameAreaRatio = fGameAreaHeight / fGameAreaWidth;
float gameAreaScrnWidth = fRTWidth * gameAreaRatio;
float renderTargetRatio = gameAreaScrnWidth / Screen.width;
Vector3 newScale = Vector3.zero;
newScale.x = transform.localScale.x * renderTargetRatio;
newScale.y = 1.0f;
newScale.z = transform.localScale.z * renderTargetRatio;
transform.localScale = newScale;
}
Due to the nature of RenderTextures being power-of-two squares, I can just use the dimensions of the texture as I fit the entire height of the texture perfectly within the bounds of the RenderTexture camera at runtime. I first calculate the portion of the texture's width the "Game Area" takes up (gameAreaRatio). Due to my RenderTexture plane being a square as well (having a 1:1 mapping), I simply multiply the width of the plane (in screen coordinates) by this gameAreaRatio to get the portion of the "Game Area" in the plane (gameAreaScrnWidth). I then find the relation between the screen's width and the "Game Area's" width to find how much I need to scale the plane in order to completely fit the width of the "Game Area" to the screen's width. In my head, the math is correct, but the output of this code is not what I'm looking for.
This is the output:
On different resolutions, the gap between the "Game Area" and the screen's edges is different. What am I missing/doing wrong in my math to calculate the scalar for the RenderTexture plane?
Answer by canadialad · Mar 21, 2017 at 01:44 AM
While this is still a problem I'd like to find a solution to, I have found a different solution that solves what I was ultimately trying to do. Namely, I was trying to split the screen in to two uneven rectangles, one larger one to display the "GameArea" and another to display the UI (at the bottom with about only a 10th of the screen's height being used).
How I solved it was using two Cameras with different viewportRects: one with full screen width and 90% screen height (Game Area) and one to display full screen width and only 10% screen height (UI).
If you have any idea as to what the math issue is for the original question, I am still open to possible solutions.