#Download ASTC files and show them as sprites
1 messages · Page 1 of 1 (latest)
Download ASTC files and show them as sprites
Why downloaded ASTC image is shown only if Compress() is called, and even then the image is not only rotated, but slided to a side and corner is pink?
Texture2D texture= new Texture2D(width, height, TextureFormat.ASTC_4x4, false);
byte[] data = request.downloadHandler.data;
texture.LoadRawTextureData(data);
//texture.LoadImage(data);
texture.Compress(true);
//texture.LoadImage(data);
// texture.LoadRawTextureData(data);
texture.Apply();
Width and Height is correct as I know resolution of image before downloading it.
Have you tried using
https://docs.unity3d.com/ScriptReference/Networking.DownloadHandlerTexture.html
The issue is that ASTC is not raw texture data. It's a compression format, so it probably adds additional bytes of information needed for compression/decompression. I'm not sure if unity recognizes that when you load the data with that method.
Yes I did, Texture handler only supports png and jpg, it does not see .ASTC files at all. Tried 😭
so for Unity you need to make .PNG file but with ASTC compression somehow? how that even work? So it is impossible to download compressed textures via webrequest?
so what you telling me, for mobile you need to use ASTC format compression, but file it self cannot be .astc ?
how is it rotated? -90, 90, 180?
The format is defined when you create the texture. It's not related to the file extension. Texture data would only contain color data for each pixel. While a compressed image file would typically contain several bytes of metadata in addition to color. You either need to make sure your backend passes the raw data or need to somehow dissect it on the client.
At least That's why assume.
this is original png. So it flipped and mirrored
I presume you did try some of the other ASTC TextureFormats
Is there any specific reason you can't load a PNG or JPG?
I assumed that ASTC don't really have a headed. So if I compress it in ASTC and say for unity that they this is texture = new Texture2D(width, height, TextureFormat.ASTC_4x4, false); of that format and load raw data to it, it will be fine. I geuss I am missing the point
Only reason is that PNG or JPG is by defautl is decompressed to ARPG , which uses tons of memory for mobile. So I then need to compress it to ASTC on device. Which is simple, but the compressing eats quite a lot of CPU power.
The memory used by a texture is defined by it's format. You can discard the loaded PNG file after creating a texture from it.
I am discarding png. Only thing I have in memory is Sprite.
This is example on Windows. I download PNG, compress it to DXT5, create Sprite out of it. Delete PNG and have only sprite in memory.
But if you dont compress PNG it will be not DXT5 but RGBA, which takes 4 times more memory. On pc that doesn't matter what so ever, but mobile webgl is as weak as it can be. and compressing takes quite some time.
by the way, I am super thankfull for you to looking at this
So the image you have shown above is a Sprite, correct?
Well, there's no way to load ASTC compressed images at runtime. You'd either need to look up some library or implement the importer yourself.
Yes it is a sprite. well sometimes I transfrom it to texture if I need to put it on 3D object. There are quite a lot of these images downlaoded.
And it would probably not be any faster than compressing a PNG at runtime.
Just to cover all the bases try assigning the texture to a material and the material to a plane then screenshot that
it is darker as I used lit material. It behaves the same.
OK, just ruling out that Sprite.Create is doing something silly
Here's a forum post of someone having a similar issue and a possible fix:
https://forum.unity.com/threads/loading-astc-texture-from-file.945125/
interesting enought editor preview shows image kinda correct. Same with dds and astc:
Thank you , I will investigate immediatly.
so 16-byte header would explain the incorrect display.
Because in the editor it is decoded properly taking the header into account.
Thank you, striping 16 bytes remove weird artifacts. And I can load it as raw data.
Will do testing how much that helps with performance now. Thank you again.
My guess you will not know if what needs to be stripped for DXT formats for pc users?
Wil need to google it. There's no guarantee the header is fixed. Maybe it's size is defined somewhere in the header itself.🤷♂️
That's just a guess though. I don't know much about image compression.
Hello again, I would like to get some guidance once again. This time with DXT. I successfully loaded resolution and compression, removed header. But loading raw data I get some weird data: \
this how this should look like.
I mean I personally like new look, but Not sure what exactly I should do. I tried some things, like flipping data array and stuff, but it doesnt seem to work nicely. Any keywords what I should look for would be great!
This would probably require an in-depth understanding of the DXT compression algorithm.
Could it be that something is shifted? as seems that RGBA data is incorrect bet location of pixel is correct? or that is out of question?
I don't know enough about compression to say anything productive. Might want to google on how the algorithm works and how it is implemented in unity.