Loading ASTC texture from file?

I’m not sure this is a bug or not, but it does seem strange.
I’m loading ASTC compressed textures (compressed with GitHub - ARM-software/astc-encoder: The Arm ASTC Encoder, a compressor for the Adaptive Scalable Texture Compression data format. on the server side), using this code:

                Texture2D tex = new Texture2D(1, 1);
                if (!ImageConversion.LoadImage(tex, rawFileBytes, true)) {
                    RLog.Log(this.name, $"Cannot LoadImage on {url}");
                    yield break;
                } else {
                    RLog.Log(this.name, $"Successfully LoadImage'd on {url}");
                }
                _renderTexture.texture = tex;
                _renderTexture.SetNativeSize();

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.

1 Like

For future reference, LoadImage() is the wrong API for this. LoadRawTextureData() is the correct one. Also, ASTC files produced by GitHub - ARM-software/astc-encoder: The Arm ASTC Encoder, a compressor for the Adaptive Scalable Texture Compression data format. 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.

4 Likes

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?

Exactly as I’ve described: use the “-yflip” command line argument and strip the first 16 bytes of the file. That’s it.

1 Like

I encounter this problem too… The API LoadImage not works.