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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Render texture stretched as I resize the game view

Discussion in 'Scripting' started by illegallyblonde, Feb 13, 2021.

  1. illegallyblonde

    illegallyblonde

    Joined:
    Aug 22, 2015
    Posts:
    6
    HI all, first post here.

    I've been trying to follow Brackeys tutorial on creating portals but instead of the script shader, I'm using a graph shader similar to the one in this video. Thus I made my project use the URP, rather than the standard one I suspect the Brackeys video was using.

    Overall, I got it working. Except that when I resize the game view, the render texture seems to stretch out:

    Stretched out:
    portals_stretched.PNG

    Normal aspect ratio:
    portals_working.PNG

    Here's what the shader graph looks like: unlit_master_graph.PNG

    In the Brackeys video, he mentions a way to fix this:
    Code (CSharp):
    1. void Start()
    2. {
    3.         if (otherCamera.targetTexture != null)
    4.         {
    5.              otherCamera.targetTexture.Release();
    6.         }
    7.         RenderTexture renderTexture = new RenderTexture(Screen.width, Screen.height, 24);
    8.         otherCamera.targetTexture = renderTexture;
    9.         otherMaterial.mainTexture = renderTexture;
    10. }
    Where
    otherCamera
    is the camera whose target texture is the render texture, and
    otherMaterial
    is the material for the render texture.

    I tried this, but sadyl the result is just a black texture:
    black_texture.PNG

    I also tried other things like calling
    Create()
    on the render texture, but nothing seems to work.

    Any ideas?
     
  2. illegallyblonde

    illegallyblonde

    Joined:
    Aug 22, 2015
    Posts:
    6
    Ironically, just a few minutes after I posted this I figured out the solution:

    Code (CSharp):
    1. void Start()
    2. {
    3.         otherCamera.aspect = ((float) Screen.width) / Screen.height;
    4. }
    This fixes the problem for me. Note that this only works if you start the game with the new game view size, not when you change it while the game is running.
     
    datsfain likes this.
  3. spider853

    spider853

    Joined:
    Feb 16, 2018
    Posts:
    36
    I need to point out that the aspect solution doesn't always work, for example on pixel games where the target texture is a smaller size than the screen, you need somehow to fit the final textureRender on the screen with UV stretching to compensate but can't find any information on this. Looks like this is how Unity does it internally with cameras even if you render to texture
     
  4. Sobekan

    Sobekan

    Joined:
    Aug 4, 2013
    Posts:
    1
    I fixed this by right clicking my Input Texture2D and setting it to Main Texture.
    I also made sure there was no texture on the camera to begin with.

    upload_2022-2-10_12-41-35.png