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

Bug Texture2D.Compress() causing new low res skewed glitch in 2023.1.7f

Discussion in 'UI Toolkit' started by mikejm_, Aug 12, 2023.

  1. mikejm_

    mikejm_

    Joined:
    Oct 9, 2021
    Posts:
    346
    I upgraded to 2023.1.7f to get the text field fixes, but I am noticing a new glitch I am having a hard time reproducing in a minimal project for a bug report.

    When I load a Texture2D from disk via a web request like so and apply it to a background:

    Code (csharp):
    1. using (UnityWebRequest webRequest = UnityWebRequestTexture.GetTexture(@"file://" + localFilePathAndName)) {
    2.  
    3.                 webRequest.SendWebRequest();
    4.  
    5.                 while (!webRequest.isDone) {
    6.                     //Debug.Log("UWR NOT DONE");
    7.                     yield return null;
    8.                 }
    9.  
    10.                 if (webRequest.result != UnityWebRequest.Result.Success) {
    11.                     Debug.Log("LOAD TEXTURE2D FROM DISK ERROR " + webRequest.error);
    12.                 }
    13.                 else {
    14.        
    15.                     photoTexture2D = DownloadHandlerTexture.GetContent(webRequest);
    16.                     photoTexture2D.Compress(false); //broken in 2023
    17.                     photoVE.style.backgroundImage = photoTexture2D;
    18.  
    19.                 }
    20.             }
    I am now on some images and some VisualElements getting a garbled result on this where it used to be good. When it garbles it it will look like this vs. how it should look next to it (ignore slight screencapping dimension differences, and just a random example photo):

    compress glitch 1.PNG compress glitch 2.PNG

    So it is lowering the resolution it looks like dramatically creating some pixellation defect and then also skewing the pixels horizontally. It always provides this same type of distortion when it occurs. Certain images seem to reproduce it while others don't.

    All images have resolutions that are multiples of 4 in each axis as I understand this is necessary for compression. I can't see any obvious reason some are provoking it pretty consistently while others are not.

    Certain VisualElement objects that call the same methods also seem to provoke it while others that use this method identically don't. I spent the whole day trying to narrow this down further and I cannot. It is consistent in when it happens, but I can't see any obvious reason for the pattern.

    If I remove the "photoTexture2D.Compress(false);" it never happens. If I put it back, the behavior starts again. That much is certain.

    Similarly, if I step down to 2022.2 it resolves, and if I go back to 2023.1 it comes back.

    Is there anything obvious that changed in 2023.1.9f from 2022.2 that might provoke this? I will keep trying to reproduce it in a bug report but I have been at it for hours and still cannot, so I am not optimistic.

    When I view one of the glitched Texture2D's from the UI Toolkit inspector and click on it, it will also always look good too. This is when hovering over a VisualElement showing the messed up distorted version, yet as you can see it still shows the texture here perfectly:

    compress glitch 3.PNG

    This to me suggests there is no problem with the Texture2D but rather there is a glitch in how it is being applied to the VisualElement background. I think the tiny thumbnail there of "background-image" also shows the glitched skewed version though it's tiny.

    I tried adding some "yield return null;" before applying the background in case it was a threading error triggered by Texture2D.Compress() but that didn't help. I have to assume it is a glitch in how some compressed textures are now being applied as backgrounds in UI Toolkit.

    Thanks for any ideas or thoughts.
     
    Last edited: Aug 13, 2023