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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Why is setAlphamaps on a terrain randomly slow?

Discussion in 'Scripting' started by mario-132, Aug 2, 2022.

  1. mario-132

    mario-132

    Joined:
    Feb 26, 2017
    Posts:
    3
    Hi,

    I am trying to update the alphamaps on several terrains using c# in a performant way. Updating the entire 1024x1024 map of a terrain takes 15ms which makes it difficult to keep a steady 60fps, so I decided to split up the updating of the terrain by making smaller chunks(4x4) of data and updating only one of those chunks each update. This does yield an improvement, often the chunk can be updated in 3-4ms now, which is great. But sadly this isn't consistent, there are for example times where it takes more than 15ms to update one chunk of data on a terrain. Because there are several terrains, it is possible for multiple chunks to be updated in one frame. If each of them were to actually take about 3ms to do then this wouldn't be an issue, but these random very long update times make it impossible to do.
    Here is an example of such a scenario:
    upload_2022-8-2_17-1-47.png
    The function TSLandscape.updateApplyAlphamaps has a single line in it which is the terrain.setAlphamaps.
    As you can see, the first chunk update is only about 4 ms, but the second one is 18ms. Both chunks are the same size. Since I divide the terrains into 4x4 update chunks and the terrains are 1024x1024, that means each chunk is 256x256. So my question is, why is updating the alphamaps sometimes so slow?
    And a second question, what does setAlphamaps do since it seems to be more than just updating the texture as you can see in the profiler? My assumption was that it just updates a texture and that's it but the texture updating part is only 1.5ms, so what does it do the rest of the time?

    I hope someone who knows more about this could help me ;)
    Thanks in advance
     
  2. mario-132

    mario-132

    Joined:
    Feb 26, 2017
    Posts:
    3
    Anyone?