Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Feature Request Why is there no option to compress terrain splat maps on build?

Discussion in 'World Building' started by joeysipos, Mar 27, 2023.

  1. joeysipos

    joeysipos

    Joined:
    Jul 28, 2015
    Posts:
    41
    Is there no option to compress splat maps for a build? I understand Unity uses uncompressed splat maps on the terrain in order to paint textures correctly. But the splat maps take up way to much memory and disk space. If the terrain splatmap atlases where compressed in DX5 format it would take up 1/4 the memory and disk space plus increase performance. According to this thread:
    https://forum.unity.com/threads/terrain-splat-map-compressed-format.34578/

    Why can't there be an option to compress the splatmap atlases at build?
     
    Last edited: Mar 27, 2023
  2. AshwinMods

    AshwinMods

    Joined:
    Jul 1, 2014
    Posts:
    13
    I totally agree, there should be an option, specially for mobile builds.
    I manually compressed the terrain splatMaps to ASTC 8x8, and I was happy with the tradeoff of size and quality.

    Until unity provides such option, Here is what you can do,

    Firstly, please MAKE BACKUPS of terrain data. You may lose decent progress in these tricks,

    1. You can call Texture2D.Compress in Editor on AlphaMaps of TerrainData,
    You don't have much control what compression will be applied, but you get a smaller build.
    https://docs.unity3d.com/ScriptReference/Texture2D.Compress.html
    https://docs.unity3d.com/ScriptReference/TerrainData-alphamapTextures.html

    2. You can export the AlphaMaps as png, apply preferred compression
    Reinitialize AlphaMaps of terrain with same Texture Format, which will clear all the pixels
    but now you can use Graphics.CopyTexture to copy that data from you exported texture to AlphaMaps
    https://docs.unity3d.com/ScriptReference/ImageConversion.EncodeToPNG.html
    https://docs.unity3d.com/ScriptReference/Texture2D.Reinitialize.html
    https://docs.unity3d.com/ScriptReference/Graphics.CopyTexture.html
    Code (CSharp):
    1.  
    2.     [MenuItem("CONTEXT/Texture2D/Export as PNG")]
    3.     public static void Export_PNG()
    4.     {
    5.         Texture2D l_Tex = Selection.activeObject as Texture2D;
    6.         var l_Bytes = l_Tex.EncodeToPNG();
    7.         var l_Path = EditorUtility.SaveFilePanel("Save File", Application.dataPath, l_Tex.name, "png");
    8.         if (!string.IsNullOrEmpty(l_Path))
    9.         {
    10.             File.WriteAllBytes(l_Path, l_Bytes);
    11.         }
    12.     }
    13.  
    3. You can try this plugin, (I have not)
    This promise to compress other data as well like Normal, BaseMap, Heightmap.
    https://forum.unity.com/threads/tiny-terrain-terrain-data-compression-technology.1374558/

    In our project though, We moved from terrain to mesh upto LOD 2, as we needed a lot more flexibility,
    and we were able to get reduced build size, memory and triangle count.