Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by canadialad · Mar 05, 2017 at 03:27 PM · mobilerendertexturescalingrender textureresolutions

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): alt text

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: alt text

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?

screen-shot-2017-03-02-at-75256-pm.png (181.5 kB)
screen-shot-2017-03-02-at-75952-pm.png (279.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Need Help Getting RenderTexture & Graphics.Blit Working: Boosting Mobile Performance 0 Answers

Constant speed across mobile devices using 2D physics velocity? 1 Answer

Camera "Don't Clear" flag doesn't work on Android ? 1 Answer

Prevent Render Texture clearing 1 Answer

Where can i find the RenderTexture component ? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges