Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Bug Loading ASTC texture from file?

Discussion in 'Editor & General Support' started by ivoras, Aug 4, 2020.

  1. ivoras

    ivoras

    Joined:
    May 21, 2020
    Posts:
    66
    I'm not sure this is a bug or not, but it does seem strange.
    I'm loading ASTC compressed textures (compressed with https://github.com/ARM-software/astc-encoder on the server side), using this code:

    Code (CSharp):
    1.                 Texture2D tex = new Texture2D(1, 1);
    2.                 if (!ImageConversion.LoadImage(tex, rawFileBytes, true)) {
    3.                     RLog.Log(this.name, $"Cannot LoadImage on {url}");
    4.                     yield break;
    5.                 } else {
    6.                     RLog.Log(this.name, $"Successfully LoadImage'd on {url}");
    7.                 }
    8.                 _renderTexture.texture = tex;
    9.                 _renderTexture.SetNativeSize();
    10.  
    Where _renderTexture is actually a RawImage type.

    But, while LoadImage() returns true, the texture isn't actually working, the question mark texture is shown instead. Tested in the editor and on a couple of Android devices (this is for a mobile app).

    The same ASTC file loads fine in the editor, flipped vertically.

    This same code path as shown above is used with JPEG and PNG files and works ok.
     
    Last edited: Aug 4, 2020
    JeffGoing likes this.
  2. ivoras

    ivoras

    Joined:
    May 21, 2020
    Posts:
    66
    For future reference, LoadImage() is the wrong API for this. LoadRawTextureData() is the correct one. Also, ASTC files produced by https://github.com/ARM-software/astc-encoder have a 16-byte header which needs to be stripped and are upside-down so -yflip needs to be used. The app needs to know the image dimensions and the ASTC block format in advance before it can show the texture.
     
  3. RickyAvina

    RickyAvina

    Joined:
    Jan 19, 2017
    Posts:
    4
    Is there any way to know what format Unity expects? As in how exactly do I go from the ASTC file produces by the astc-encoder to a format recognized by Unity?
     
  4. ivoras

    ivoras

    Joined:
    May 21, 2020
    Posts:
    66
    Exactly as I've described: use the "-yflip" command line argument and strip the first 16 bytes of the file. That's it.
     
    JeffGoing likes this.
  5. JeffGoing

    JeffGoing

    Joined:
    May 17, 2021
    Posts:
    1
    I encounter this problem too... The API LoadImage not works.