Search Unity

Feature Request Terrain GetHeights and GetAlphamaps without generating garbages

Discussion in 'Editor & General Support' started by Lesnikus5, Jan 29, 2021.

  1. Lesnikus5

    Lesnikus5

    Joined:
    May 20, 2016
    Posts:
    131
    The GetHeights and GetAlphamaps methods each time allocate memory to create a float array and then return it. Even in the Unity Manual, the Understanding Automatic Memory Management page that this is bad memory practice:

    Why not make a version of the method that takes a float array and fills it with values, without initializing a new array? By analogy with the existing methods:

    Physics.RaycastNonAlloc instead of Physics.RaycastAll
    Physics.OverlapSphereNonAlloc instead of Physics.OverlapSphere

    It might look like this:

    public void TerrainData.GetHeightsNonAlloc (int xBase, int yBase, int width, int height, float [,] heights);
    public void TerrainData.GetAlphamapsNonAlloc (int x, int y, int width, int height, float [,,] alphamaps);


    instead:

    public float [,] TerrainData.GetHeights (int xBase, int yBase, int width, int height);
    public float [,,] TerrainData.GetAlphamaps (int x, int y, int width, int height);


    Usage might look like:

    Code (CSharp):
    1. float [,] heights = new float [sizeHeightmap, sizeHeightmap];
    2. terrain.terrainData.GetHeightsNonAlloc (0, 0, sizeHeightmap, sizeHeightmap, heights)
    3. float [,,] alphamaps = new float [sizeAlphamap, sizeAlphamap, 32];
    4. terrain.terrainData.GetAlphamapsNonAlloc (0, 0, sizeAlphamap, sizeAlphamap, alphamaps)
    Problem context:
    In my game, the player can edit the terrain in real time and then save it. The large world of the game consists of 64 terrain tiles with a resolution of each 1025x1025. To serialize the heights of all terrains to bytes and save them to files, I make a call to the terrainData.GetHeights method, which results in huge GC Alloc allocations of about 250 mb for heightmaps. But I also have to save alphamaps, which theoretically requires about 8000 mb (250 mb * 32 material slots). But I didn't even dare to check GetAlphamaps

    3.jpg

    It is clear that I will save only those tiles that were changed by the player in the current game session, but some players may have such a long game session in which they have time to change all or almost all of the 64 terrains. This is absolutely not useful, especially with GetAlphamaps.

    I tried to read heights one at a time using GetHeight, but saving 64 million heights from 2.5 seconds increased several times, which I would like to avoid. Alphamaps cannot read values without GC Alloc in any way.
     
    Last edited: Jan 29, 2021
  2. nikescar

    nikescar

    Joined:
    Nov 16, 2011
    Posts:
    165
    I second this feature request. I'm trying to check the terrain splat map texture mix under each tire of a vehicle and it seems that alphamap is the only way.
     
    noio likes this.
  3. Lesnikus5

    Lesnikus5

    Joined:
    May 20, 2016
    Posts:
    131
    I created a workaround by copying the data from TerrainData to my array so I can access the values without the GC Alloc. But you understand that this is a crutch. My array takes up memory and it would be better to have standard GetAlphamaps/GetHeightmap functions without garbage allocation. I would be grateful if someone from the developers pays attention to this request and answers whether it is worth expecting a solution to the problem.
     
  4. nikescar

    nikescar

    Joined:
    Nov 16, 2011
    Posts:
    165
    I did something similar. Since all I needed for my purposes was the dominant map, I just need to store a 2D array of bytes for each coordinate on the terrain. Since my terrains are only 1024x1024 that's only what, 1MB? Still, this feature request is still valid and needed, especially if you are doing anything more complex like the OP.
     
    noio likes this.
  5. Ponzel

    Ponzel

    Joined:
    Jun 17, 2017
    Posts:
    41
    Yup, we'd also appreciate having access to terrain height data without having to cache it ourselves to avoid GC issues!
     
    chadfranklin47 likes this.
  6. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    We still need this feature!
     
    chadfranklin47 and nikescar like this.
  7. REDvsGREEN

    REDvsGREEN

    Joined:
    Sep 8, 2019
    Posts:
    34
    Still needed
     
    chadfranklin47 likes this.
  8. Seb88250

    Seb88250

    Joined:
    Jul 27, 2020
    Posts:
    11
    I need this for my city-builder...
     
    REDvsGREEN likes this.
  9. knxrb

    knxrb

    Joined:
    Dec 16, 2018
    Posts:
    21
    This would be a great benefit to my project too!
     
  10. JanGehr

    JanGehr

    Joined:
    Feb 4, 2013
    Posts:
    18
    We still need this feature!
     
  11. noio

    noio

    Joined:
    Dec 17, 2013
    Posts:
    232
    This is smart! :cool: Thank you, I am also using this approach. If we're gonna cache stuff anyway, that saves on computing the max layer weight each time!