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

Discussion Texture compression issue

Discussion in 'Asset Importing & Exporting' started by OblicaStudio, Jun 16, 2022.

  1. OblicaStudio

    OblicaStudio

    Joined:
    Feb 20, 2021
    Posts:
    30
    Hi,
    I am working on a image/texture intensive game (a jigsaw puzzle) and size of the mobile build is creating issue. I have images in JPG format, 1024x1024 pixels, and an average size is around 80 kb. But when I import it, Unity automatically increase the imported texture size to 500 kb (.5 Mb). I understand that file format (JPG, PNG etc) does not matter for Unity as it converts all images into its own. But despite having a POT texture and setting compression to Normal, why it is increasing the compressed (in its own internal format) size to almost 5 or 6 times then the JPG format. Does this means Unity compression is not efficient?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,019
    JPG is a lossy compression method, ie it does not preserve all data. That‘s always going to be superior in terms of size compared to lossless compression. Try picking a different compression style than „Normal“ (which might even mean „no compression“, not sure).

    Mobile supports PVR(TC) compression formats which are also lossy (which can also be visually noticable, depends on the type of content). I‘m pretty sure you can tweak the texture compression in that way in Unity too.
     
  3. OblicaStudio

    OblicaStudio

    Joined:
    Feb 20, 2021
    Posts:
    30
    Thanks for your reply. I have tried all compression type (Low, Normal and High quality) but resultant build size does not change neither individual texture size. So my question was though JPG is a lossy compression why Unity does not use this type of compression or why it does not have any option to select by developer, because I have checked on mobile and a JPG at 80 KB looks same as Unity's 500 KB texture (which it has converted from the same image). I am fine with whatever quality a JPG has at 80 KB but Unity is increasing the texture size to 500 KB on import. So why Unity is pushing us to use 420 KB extra for same quality we can get in 80 KB.

    Also do we have an option like images are compressed at the highest possible compression (e.g. 80 KB like JPG) in the build and at runtime when we load a particular image from the Resources, it decompress and presented in the actual size (e.g. 500 KB). Why I am asking is because I don't load all images at once, only one image for a particular jigsaw level. So I am fine if it takes 1 or 2 secs to decompress because I will gain on build size.
     
  4. g4ma

    g4ma

    Joined:
    Dec 18, 2018
    Posts:
    32
    Hello!

    The size shown by Unity is the one taken by your texture after having encoded it into a format the GPU can support - a GPU cannot simply display a JPEG, it has to have it into a suitable format.

    Your 1024*1024 image holds 1M pixels and the various formats available (BC*, ASTC, ETC etc.) usually have a fixed bits-per-pixel (bpp) ratio that will allow you to easily figure out the eventual size you will get.
    I would advise you to look a this page that lists all available formats and their respective bpp.
     
  5. OblicaStudio

    OblicaStudio

    Joined:
    Feb 20, 2021
    Posts:
    30
    Thanks. I have also tried Crunch compression and its great. Reduced size further with compromising on quality much (at least something acceptable for me). But when I try to access these crunched compressed images in my script, it throws the exception - crunch compressed textures can not be accessed from scripts. Any idea how I can do it because if somehow I would able to access these images (Crunched), then it will solve all of my problem related to build size.
     
  6. g4ma

    g4ma

    Joined:
    Dec 18, 2018
    Posts:
    32
    I am not aware of any limitations specific to crunch textures. But for any texture to be modified through script you need to have them available on the CPU side (rather than exclusively on the GPU side), this is done by checking Read/Write in the texture importer - see the attached picture.

    Also regarding crunch textures it does not change anything to the size of the texture once uploaded to the GPU - it only makes your texture smaller in the game package ondisk.
     

    Attached Files:

  7. OblicaStudio

    OblicaStudio

    Joined:
    Feb 20, 2021
    Posts:
    30
    Thanks. Yes, I have already enabled Read/Write but it still throws the access exception when Crunch compression is enabled in the import setting. The moment I remove Crunch compression, texture became assessible within script.
     
  8. OblicaStudio

    OblicaStudio

    Joined:
    Feb 20, 2021
    Posts:
    30
    So I have found the solution to access Crunched compressed textures in the script. Unfortunately Unity's scripting document about GetPixels() does not provide enough information on Crunch compressed texture. It simply says that GetPixels() does not support crunch compressed texture. However it does not mention that we can use GetPixels32() to work is around. It has been mentioned in GetPixels32() scripting reference. So if you want to access crunched compressed textures in your script, you first need to convert that to another format (I don't know what it has been called). Use following code:

    public Texture2D Convert(Texture2D crunchedImage)
    {
    Texture2D image = new Texture2D(crunchedImage.width, crunchedImage.height);
    image.SetPixels32(crunchedImage.GetPixels32(0));
    image.Apply();
    Resources.UnloadAsset(crunchedImage);
    return image;
    }

    Now you can use this returning image in GetPixel() manipulations.

    I know its an overhead but at least I don't find any another way to access crunched textures. It is fine with me as I am working on a Jigsaw puzzle and need to access a single image at a time. But it could have performance implications if you have many images compressed using crunch compression and you need to accessed all of them at once (probably in one scene).