Search Unity

How to make runtime compressed textures not look like crap?

Discussion in 'Scripting' started by SunnySunshine, Dec 15, 2021.

  1. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    976
    I want to be able to compress textures at runtime.

    I've noticed that compressing textures using Texture2D.Compress produces very poor results. The texture will look pixely and noisey.

    When importing a texture through Unity editor, results look much better.

    Out of curiosity, I tried compressing a texture outside of unity into DDS format (DXT5) using a CLI tool called compressonator, and then importing it into Unity to see what would happen. Unity does indeed recognize that the texture is already compressed DXT5, and thus will not allow you to compress it further. The texture looks just as good as when Unity editor compresses a texture.

    Ok, great. So I thought maybe I could just load this external DDS at runtime, then, and get the same result? By skipping the 128 byte header, you can read DDS textures using Texture2D.LoadRawTextureData, as was explained in this Unity answers post. However, when reading a texture this way, it will have the same bad quality as compressing a texture through Unity API. Pixely and noisy.

    How can that be? Why would importing a raw DDS through Unity editor produce a better result than reading it through script?

    Code (CSharp):
    1. var folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
    2. var path = System.IO.Path.Combine(folder, "some_dds_file.dds");
    3. var bytes = System.IO.File.ReadAllBytes(path);
    4.  
    5. var DDS_HEADER_SIZE = 128;
    6.  
    7. byte[] dxtBytes = new byte[bytes.Length - DDS_HEADER_SIZE];
    8. Buffer.BlockCopy(bytes, DDS_HEADER_SIZE, dxtBytes, 0, bytes.Length - DDS_HEADER_SIZE);
    9.  
    10. var tex = new Texture2D(2048, 2048, TextureFormat.DXT5, false);
    11. tex.LoadRawTextureData(dxtBytes);
    12. tex.Apply();
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Did you try the high quality argument?

    I'll assume then that you did not compare Compress and its editor equivalent in the docs? There seems to be some reasonable confirmations (and light discussion) of the differences, without much depth or options to address it.
     
  3. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    976
    Yes. Still looks bad.

    As the editor equivalent is not available at runtime, I'm not sure how that would help. The docs don't really say that much what's going on, simply that the EditorUtility is better but that's about it.
     
  4. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    976
    Nevermind, typo in script. Was applying the wrong texture on material when comparing. :facepalm:

    However, the loaded DDS file will be flipped. Quality looks great though (the external one, not unity runtime compressed that feature is still awful).
     
    Kurt-Dekker likes this.