Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Best texture format for a single channel mask-type texture.

Discussion in 'Editor & General Support' started by nomadic, Oct 17, 2013.

  1. nomadic

    nomadic

    Joined:
    Mar 4, 2010
    Posts:
    44
    I am creating a series of puzzle pieces, and--intending to cut down file size--I made a single RGB texture of the completed puzzle and several black white textures (one for each puzzle piece) to serve as alpha masks. This works great because I can have infinite iterations of puzzle piece shapes for just a single RGB texture.

    However, it's not cutting down on memory as I had hoped. For example, one of the B&W masks is 13KB on my hard drive but 341KB (compressed) in Unity!

    Is there a way to cut this down? By importing the masks as single-channel images? By exporting from photoshop a certain way?

    Thanks-
     
  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,649
    Which platform/compression is this? PVRTC?
     
  3. nomadic

    nomadic

    Joined:
    Mar 4, 2010
    Posts:
    44
    @superpig:
    Checked out the TextureFormat documentation... Yes, I'm working on a Macbook right now so I'm guessing it's PVRTC by default. Is there a way to force a texture to Alpha8 to reduce its memory at build time?
     
  4. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    What size / resolution is you image?

    The problem is that the is no good way to compress an 8 bit image file.
    An 8 bit image file is 0.7Mb.
    An compressed 32 bit DXT1 file is 341,4 KB

    You can remote the Mip Maps in the importer to go down form 341,4 KB to 256,0 KB but that all i can get it down (image file is 1024x512).

    But hat amount of memory should not kill your application.
     
  5. Deleted User

    Deleted User

    Guest

    A few things to add:

    First off, it's not really good practice to compare file size on disk with size in memory. Textures on disk can use all sorts of compression techniques that don't work on a GPU. As an example, PNG files are internally zipped when saved which makes them very small on disk, but no smaller in memory when you load them. GPUs can't handle gzipping on the fly anyway.

    To your original question, the main goal is for you to find a way to store your textures into a format that has fewer than 8 bits per pixel (i.e. DXT1 or PVRTC2 or 4). As fffMalzbier says, an grayscale image is already 8 bits, and you have to use fewer than 8 bits per pixel if you want to save anything.

    Unfortunately, on many platforms (especially GL or DX based ones) having an alpha channel forces you into using a format that supports both color and alpha (i.e. DXT5), and those formats are usually 8 bits per pixel or more, which is a wash. The trick to get around this is to save the mask into one of the color channels of your image and save the file with out any alpha. Usually the best color is the green channel because many GPU codecs give extra bits to green (because of how sensitive the human eye is to green). You can also save the mask into all three channels, but that may make things more cumbersome in Photoshop. Then, you want to make sure that texture is saved into the lowest bit depth, non-alpha format for your target platform (i.e. DXT1 for PC/Mac, PVRTC2 no alpha for iOS, etc). Lastly, you'll need to make your shaders look in the green (or whatever) channel for the mask instead of the alpha channel. There are actually GPU texture swizzle tricks that you can use to have the GPU load the green channel into the alpha for your shader, but I'm not sure if unity exposes the logic necessary for you to do any of that.

    I hope that helps!

    PS - PVRTC is only used on devices that have PowerVR GPUs (i.e. all iOS devices and some rare android devices). If you're building for Mac, it's almost certainly using using DXT for texture compression.
     
    Arkade and colin299 like this.