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

Terrain heightmap access

Discussion in 'Scripting' started by touming, Apr 20, 2008.

  1. touming

    touming

    Joined:
    Apr 19, 2008
    Posts:
    14
    Does anyone know how to get fast and direct access to the heightmap of the terrain structure? The only functions to access the heightarray are SetHeights and GetHeights, wich seem to be dependent on the total resolution of the terrain.
    That means, if I just want to change a small part of the terrain (like a small rectangle) the functions seem to work on the whole data array, wich reuslts in very slow respond times, not suitable for interactive handling.

    The interactive terrain modifer doesn't seem to care about the grid size, as it works totally smooth on a 2000x2000 grid. I think their must be another way of modifing the heightmap... any ideas?

    Thanks for help!
    Tommy
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Look at those functions in the docs again: "function GetHeights (xBase : int, yBase : int, width : int, height : int) : float[,]". Just supply the parameters for the part of the terrain you want to work with.

    --Eric
     
  3. touming

    touming

    Joined:
    Apr 19, 2008
    Posts:
    14
    thanks for the reply, but thats actually what i already did. with setHeights and GetHeights I only read and write a small and fix square region of 25x25 elements.
    but it makes a huge difference in response time, if i use a 50x50 terrain or a 2000x2000 terrain - wich should be on no interest, if i would have direct access to the array.

    tommy
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Oh, sorry, at first it sounded like you were reading the entire array.

    50x50 or 2000x2000 won't make any difference with Get/SetHeights, since it has nothing to do with the size of the heightmap array; that's just the dimensions of the mesh. What matters is the resolution, not the mesh size, and even then it only matters for SetHeights.

    With this code:

    Code (csharp):
    1. var terrain : TerrainData;
    2.  
    3. function Update () {
    4.     var t = terrain.GetHeights(0, 0, 25, 25);
    5.     for (x = 0; x < 25; x++) {
    6.         for (y = 0; y < 25; y++) {
    7.             t[x,y] = Random.Range(0.0, 1.0);
    8.         }
    9.     }
    10.     //terrain.SetHeights(0, 0, t);
    11. }
    if I have no terrain visible (important to make sure you're not measuring how fast the GPU is displaying the mesh), whether the resolution is 33 or 513, I get 480 fps in both cases. So GetHeights isn't affected by the resolution of the terrain.

    If I uncomment the SetHeights line, then a resolution of 33 gets 460 fps and a resolution of 513 gets 22 fps. So SetHeights is affected by the resolution. Possibly that has something to do with PhysX maybe having to compute a new collider for the whole terrain even though just part of it changes?

    --Eric
     
  5. touming

    touming

    Joined:
    Apr 19, 2008
    Posts:
    14
    thanks very much for your testing!
    yeah, i can just confirm what you already said, setHeight is the bottleneck of the whole process. though you can decrease the resolution of the heightmap to get resonable framerates, its still a pity that a direct access to the heightmap isn't possible... :(