Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved RenderTexture resize object without breaking camera aspect

Discussion in 'Scripting' started by drew_lietzenmayer1, Jul 21, 2023.

  1. drew_lietzenmayer1

    drew_lietzenmayer1

    Joined:
    Apr 20, 2022
    Posts:
    8
    Hello,

    I am trying to render my camera onto multiple objects that are of a specific size of which are rectangles. I am resizing a quad to get the size and shape I need. However, when I do this, it skews the image. Is there a way to keep the image on the RenderTexture to always project what the camera sees? I've attached a couple images showing the camera view vs the Render Texture output.

    Thank you!
     

    Attached Files:

  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    Sorry but it's not clear what exact behaviour you want / expect. It should be clear that when you resize / rescale an object to a different aspect ratio than the camera's rendertexture, you either need to crop the image or repeat the image.

    Whatever you want can be done either by changing the UV coordinates of your quad, or change the texture scale / texture offset of the material that your quad uses to compensate. Since your rendertexture has a greater width than height and your quad is essentially reversed, you would need to crop quite a bit.

    Maybe explain what you actually want to achieve here. Do you talk about portal rendering here? In that case you would need to adjust the UV coordinates of the quad. Dave Kircher mentioned this case in his talk over here at about 8:52. Though we are not here to guess what you may want, you have to tell us.
     
  3. drew_lietzenmayer1

    drew_lietzenmayer1

    Joined:
    Apr 20, 2022
    Posts:
    8
    Thank you for responding. Didn't realize I was being so vague, I apologize.

    So I have a quad in game that is supposed to represent a monitor that displays a scene going on elsewhere in the game. That monitor is not a square, so I made the quad and added the render texture to it. That projects exactly what the second camera sees. However, when I scale the quad to a rectangle it then skews the output. I want to be able to scale the quad (or whatever object I place it on, shouldn't really matter) without the output skewing like is seen in my attached screenshots.

    Does that make sense?
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    No, it does not :)

    As I said what exact behaviour do you expect? You can't put a 10m long things into a 2m hole. Since you want no distortion, just scaling it uniformly doesn't work, as the view would not be high enough and doesn't even fill the quad in height. So you would need to either crop the texture at the sides (left and right) or if you want to have the full view visible, you either need letter box the image (like cinema movies on a 4:3 monitor).

    If you want to crop the image, you can just adjust the texture offset and texture scale in the x direction to compensate the distortion. It's just trivial math to work it out when you know your texture size and your quad size.

    If you want to letterbox, it's probably easier to actually keep the quad at the same dimension as the rendertexture and just scale it uniformly so the width fits the target rectangle. Use a second rectangle as the background that would serve as the letter boxes on the top and bottom.
     
  5. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    530
    There is also a third option of adjusting camera aspect ratio so that it matches with object on which you want to put it's output.
     
    orionsyndrome and Bunny83 like this.
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    Yes, that's true, but also requires some adjustments. So the rendertexture needs to be created with the desired aspect ratio as well.
     
  7. drew_lietzenmayer1

    drew_lietzenmayer1

    Joined:
    Apr 20, 2022
    Posts:
    8
    Thanks for the responses! I'm seeing where my disconnect was. I basically took the resolution (aspect ratio) of the camera, used a raw image instead of a quad, set the rect transform's width and height to the cameras resolution, and then uniformly scaled the raw image which prevented the skewing. But @Bunny83 is 100% correct. Both have to be the same otherwise it'll skew in one way or the other. I was making it harder than it needed to be. :)
     
    Last edited: Jul 24, 2023
    Bunny83 likes this.