Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug DirectX RenderTexture bug (only in build)

Discussion in 'General Graphics' started by dn9090, Dec 1, 2022.

  1. dn9090

    dn9090

    Joined:
    Dec 1, 2022
    Posts:
    1
    Hi there!

    At the moment I am working on a bug that only occurs under DirectX11 and only in the build. I'm creating "screenshots" with code that looks like this:
    Code (CSharp):
    1. // Awake
    2. texture = new Texture2D(maxResolution, maxResolution, TextureFormat.RGBA32, false);
    3. renderTexture = new RenderTexture(maxResolution, maxResolution, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);
    4.  
    5. // Update
    6. renderCamera.targetTexture = renderTexture;
    7. renderCamera.Render();
    8.  
    9. RenderTexture.active = renderTexture;
    10. texture.ReadPixels(new Rect(rect.x, rect.y, rect.width, rect.height), 0, 0, false);
    11.  
    12. // Export
    13. var raw = texture.GetRawTextureData();
    14. var bytes = ImageConversion.EncodeArrayToPNG(raw, texture.graphicsFormat, (uint)texture.width, (uint)texture.height);
    15. stream.Write(bytes, 0, bytes.Length);
    16.  
    The problem is that in the editor everything looks fine, but in the build the PNG images get weird discolorations (see images below). Now it gets even weirder: when I set the Graphic API to Vulkan, the images are also correct in the build. It seems to be a DirectX11 issue, but I can't find the underlying problem (because in the editor everything is as it should be). Does anyone have any ideas or have encountered the bug themselves?
    vulkan-good-1.png directx11-bad-1.png vulkan-good-2.png directx11-bad-2.png

    (I'm using the URP and I tried Unity 2019.4 to 2021.3)