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. Dismiss Notice

Resolved Camera doesn't properly render to higher resolution RenderTexture anymore

Discussion in 'General Graphics' started by MystFTG, Feb 8, 2021.

  1. MystFTG

    MystFTG

    Joined:
    Apr 28, 2013
    Posts:
    26
    Hello everyone,

    I'm a bit at a loss since switching to the latest LTS 2019.4.19f1 version from 2019.4.14f1.

    We're using a RenderTexture in A4 aspect ratio and a relatively high resolution to create screenshots from the MainCamera and put it into a document. That way, even people running the software at a low resolution, will get a high quality document.
    This has worked great until very recently and there are no bugs or errors in the console that are new.

    What happens now is that using camera.targetTexture = renderTexture just slaps the camera render at the current ingame resolution onto the mugh higher resolution RenderTexture, which of course doesn't work.
    Currently the only workaround I've found is using the camera.pixelHeight/Width as the RenderTexture size, but of course that results in a very low resolution.

    I'm unsure if this might be related to this issue mentioned for 2019.4.19f1 here:
    https://issuetracker.unity3d.com/is...calling-totexture2d-method-on-a-rendertexture
    Or maybe it's something else I've missed during the updates.

    Here's some code, which worked until very recently in multiple programs. The page pixel size (A4) is around 4k, but that's not working atm. The aspect ratio of the render texture is of course different from the screen aspect ratio, as we're working with documents.
    Code (CSharp):
    1. RenderTexture rt = new RenderTexture(pageWidth, pageHeight, 32, RenderTextureFormat.ARGB32);
    2. cam.targetTexture = rt;
    3. cam.Render();
    4. RenderTexture.active = rt;
    5. Texture2D tex = new Texture2D(pageWidth, pageHeight, TextureFormat.RGB24, false);
    6. tex.Apply();
    7. byte[] imageBytes = tex.EncodeToJPG(jpgQuality);
    The unexpected behavior occurs after cam.targetTexture = rt; so when I preview this texture in the editor, it's already "broken". The exact same code works in 2019.4.14f1.

    Looking through the fixes and known issues of the LTS versions 2019.4.15f1 to 19f1 I couldn't find anything related to my problem, other than the issue above. If my issue is the same bug, than this means the case is closed. Otherwise, any help is highly appreciated. Maybe there were some changes I'm not aware of?

    Testing different versions, the behavior seems to appear when going from version 219.4.14f1 to version 2019.4.15f1 and is still present in 19f1.

    Thanks! :)
     
  2. MystFTG

    MystFTG

    Joined:
    Apr 28, 2013
    Posts:
    26
    The issue still exists in the 2019.4.20f1 update that was just released. The bug linked above is also still present as a "known issue", but I'm still not sure if my issue is related.
     
  3. BattleAngelAlita

    BattleAngelAlita

    Joined:
    Nov 20, 2016
    Posts:
    400
    Did you try use Graphics.SetRenderTarget?
    As workaround you can make a few screenshots with different sub-pixel camera offset, and then combine them in to one big texture.
     
    MystFTG likes this.
  4. MystFTG

    MystFTG

    Joined:
    Apr 28, 2013
    Posts:
    26
    Thank you for your reply.

    I'm not sure using Graphics.SetRenderTarget would do anything, because I'm already using RenderTexture.active, which, according to the documentation, does the same thing.
    (from: https://docs.unity3d.com/ScriptReference/Graphics.SetRenderTarget.html)

    How would I start working on the second suggestion? Do you mean zooming the camera in, to different positions, making screenshots and stitching them together? That might work, although I still wouldn't be able to achieve the same quality I got with a high resolution RenderTexture if the program is running at a relatively low resolution on the customer's system. But it would be a workaround (albeit a somewhat time consuming one) if this issue doesn't get fixed.

    Thanks for the suggestion. I'll look into it.
    If it ends up being too finicky, I'll consider reverting to 2019.4.14f1 where the issue wasn't present.

    I'm still unsure to why this happened. We've been using this technique in multiple programs for a few years now, starting with Unity 2017 and suddenly it doesn't work anymore. I was hoping for 20f1 to fix the issue, but I guess it'll have to wait...or maybe it's not a bug?
     
  5. BattleAngelAlita

    BattleAngelAlita

    Joined:
    Nov 20, 2016
    Posts:
    400
    That doesn't mean they work same under the hood.
     
    MystFTG likes this.
  6. MystFTG

    MystFTG

    Joined:
    Apr 28, 2013
    Posts:
    26
    Of course, that might be the case.

    So I gave it a shot, but unfortunately the texture that's being generated with Graphics.SetRenderTarget has the same issues as the texture made using RenderTexture.active using any Unity2019.4 version above 14f1.
     
  7. manutoo

    manutoo

    Joined:
    Jul 13, 2010
    Posts:
    455
    @MystFTG ,
    if you can, you probably should file a bug report ; the issue linked in your OP could be linked to yours, but it's not sure.

    I'm on 2019.4.13 and now I don't want to update anymore, as I use the Render Texture in a similar way..! :p
     
    MystFTG likes this.
  8. MystFTG

    MystFTG

    Joined:
    Apr 28, 2013
    Posts:
    26
    @manutoo
    Thanks for the suggestion, I just created a bug report after some further testing.
    2018.4.31 works without issues.
    2019.4.14 also works, after that it doesn't work anymore.
    2020.1.13 still works as well, but after that it's broken.
    2020.2 and 2021 don't work.
     
    manutoo likes this.
  9. manutoo

    manutoo

    Joined:
    Jul 13, 2010
    Posts:
    455
    @MystFTG ,
    finally, I updated to 2019.4.20 and for my case, it seems to be working normally, here's the code in case it helps you to find a way to bypass the issue (not pure Unity but hopefully you'll get it :) ) :
    Code (CSharp):
    1.         RenderTexture CurrentRenderTexture = RenderTexture.active,
    2.                   RenderTex = new RenderTexture(CToo.m_Ini.GetInt("3dEngine", "RenderTexW", 8192), CToo.m_Ini.GetInt("3dEngine", "RenderTexH", 4096), 24, RenderTextureFormat.ARGB32);
    3.  
    4.         m_Cam3D.m_UnityCam.targetTexture = RenderTex;
    5.         RenderTexture.active = RenderTex;
    6.  
    7.         //=== Change Camera Aspect Ratio
    8.         m_Cam3D.m_UnityCam.aspect = AspectRatio;
    9.  
    10.         yield return null;
    11.         yield return new WaitForEndOfFrame();
    12.  
    13.         //=== Create a new Texture2D with the camera's texture, using its height and width
    14.         //Texture2D Image = new Texture2D(RenderTex.width, RenderTex.height, TextureFormat.RGB24, false);
    15.         Texture2D Image = new Texture2D(RenderTex.width, RenderTex.height, TextureFormat.RGBA32, false);
    16.  
    17.         RenderTexture.active = RenderTex;
    18.         Image.ReadPixels(new Rect(0, 0, RenderTex.width, RenderTex.height), 0, 0);
    19.         //Image.ReadPixels(new Rect(0, 0, m_Cam3D.m_UnityCam.pixelWidth, m_Cam3D.m_UnityCam.pixelHeight), 0, 0, false);
    20.         Image.Apply();
    21.  
    22.         string FileName = CTooFile.GetNumeratedName(CGlobal.es_GameNameShort + "_2dCourt.png");
    23.  
    24.         Texture2D ScaledImage = Image.Scale(CToo.m_Ini.GetFloat("3dEngine", "RenderTexCoeff", 0.5f));
    25.  
    26.         System.IO.File.WriteAllBytes(FileName , ScaledImage.EncodeToPNG());
    27.         System.IO.File.WriteAllBytes(CTooFile.PutExt(FileName, ".jpg"), ScaledImage.EncodeToJPG(97));
    28.  
    29.         Destroy(Image);
    30.         Destroy(ScaledImage);
    31.  
    32.         Image = ScaledImage = null;
     
    Fibonaccov and MystFTG like this.
  10. MystFTG

    MystFTG

    Joined:
    Apr 28, 2013
    Posts:
    26
    Thanks for the update, glad it worked out for you.
    So far I'm not having any luck with 2019.4.20.

    What resolution are you setting the RenderTexture to?
    CToo.m_Ini.GetInt("3dEngine", "RenderTexW", 8192), CToo.m_Ini.GetInt("3dEngine", "RenderTexH", 4096)
    Looks like 8k?
    Is it different from the screen resolution or editor window resolution in your project? That's where the error happens for me.

    Here's a super simplified version of my code. It seems to be fairly similar to what you're doing.
    Code (CSharp):
    1. RenderTexture targetTexture = new RenderTexture(3840, 2160, 24, RenderTextureFormat.ARGB32);
    2. Texture2D texture = new Texture2D(3840, 2160, TextureFormat.RGB24, false);
    3. Camera.main.targetTexture = targetTexture;
    4. Camera.main.Render();
    5. RenderTexture.active = targetTexture;
    6. texture.ReadPixels(new Rect(0, 0, 3840, 2160), 0, 0);
    7. texture.Apply();
    As soon as the camera renders to the targetTexture I get the wrong image in newer Unity versions if the screen resolution isn't the same as the RenderTexture resolution. Of course there may be an error in my code, but then I'm wondering why it works in 2018 and until 2019.4.14/2020.1.13.

    Going back to 2019.4.14 would mean having some older bugs come back that got fixed and I'd love to avoid that.
     
  11. manutoo

    manutoo

    Joined:
    Jul 13, 2010
    Posts:
    455
    Yes, the render texture is 8k ; I only tried in the editor (resolution = ~1900x1200), though.

    If you pick power-of-2 dimensions for your render texture, does it still fail ?
     
  12. MystFTG

    MystFTG

    Joined:
    Apr 28, 2013
    Posts:
    26
    I just tested it and it fails regardless of the render texture size unless it's the same as the camera (screen/window) resolution.
     
  13. manutoo

    manutoo

    Joined:
    Jul 13, 2010
    Posts:
    455
  14. MystFTG

    MystFTG

    Joined:
    Apr 28, 2013
    Posts:
    26
    Unfortunately there are no changes.
    Looks like I'll have to go back to 14f1 until the bug is getting fixed...if it's a bug.

    I'll try to work a bit on BattleAngelAlita's suggested workaround in the meantime and keep this thread updated if there are any changes to the bug report.

    Thanks for the help so far everybody!
     
  15. MystFTG

    MystFTG

    Joined:
    Apr 28, 2013
    Posts:
    26
    Just wanted to post a little update on this:
    The issue I linked to above (https://issuetracker.unity3d.com/is...calling-totexture2d-method-on-a-rendertexture) has been fixed, but the update for Unity 2019 (2019.4.22f1) hasn't been released as of today.

    I have however tested the demo I submitted for my own bug report in the other versions that have been fixed and it seems to be working again. I'm also still waiting for an official confirmation from Unity, but it seems that my issue was the same as the one in the other bug report. Just in case anyone was wondering or has the same issue running Unity 2019.

    Edit: And they just released the new version, everything works again just as it did until .4.14f1
     
    Last edited: Mar 10, 2021
    manutoo likes this.