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

In-Game drawing on terrain feels slow.

Discussion in 'Scripting' started by mark-velthuis, Mar 20, 2013.

  1. mark-velthuis

    mark-velthuis

    Joined:
    Feb 10, 2011
    Posts:
    47
    I'm trying to draw on terrain in-game (by adjusting splatmap values).
    The code I have works, but feels realy slow compared to the unity editor.

    Code (csharp):
    1.    
    2. public void DrawOnTerrain(TerrainData terrainData, Vector2 texCoord) {
    3.     //Calculate splatmap positions based on given texture coordinate
    4.     int pointx = (int)(terrainData.alphamapHeight * texCoord.x);
    5.     int pointy = (int)(terrainData.alphamapWidth * texCoord.y);
    6.     //Make new splatmap values
    7.     float[,,] maps = new float[1,1,4];
    8.     maps[0, 0, 0] = 0;      
    9.     maps[0, 0, 1] = 0;  
    10.     maps[0, 0, 2] = 0;  
    11.     maps[0, 0, 3] = 1;  
    12.     //Place them back in the terrain
    13.     terrainData.SetAlphamaps(pointx, pointy, maps);
    14. }
    15.  
    SetAlphamaps causes a huge decrease in performance (+- 35ms)
    The terrain has 4 splatmaps. The splatmap resolution is 512 x 512.
    According to the profiler, the performance is going into
    Camera.render
    >Culling
    >>Terrain.CullAllTerrains()

    If I change the camera culling mask to not include the terrain, it doesn't take nearly as much performance, but obviously you don't see the changes untill the culling mask is set to include the terrain again.
    The Unity terrain editor feels a LOT smoother, even when used in a paused game. Is it possible to edit terrain in-game with the same performance ?
     
  2. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Same exact problem here. A solution would be nice. My performance drops from about 65 fps to 25 fps when drawing on the terrain using almost the exact same script you posted above.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    There is no solution. The built in terrain in unity does not perform adequately for run time modification.

    You can roll your own terrain, use a third party terrain, or wait for the upgrade.
     
  4. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Ahhhh, okay :(

    Any suggestions for third party terrain?