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.
public void DrawOnTerrain(TerrainData terrainData, Vector2 texCoord) {
//Calculate splatmap positions based on given texture coordinate
int pointx = (int)(terrainData.alphamapHeight * texCoord.x);
int pointy = (int)(terrainData.alphamapWidth * texCoord.y);
//Make new splatmap values
float[,,] maps = new float[1,1,4];
maps[0, 0, 0] = 0;
maps[0, 0, 1] = 0;
maps[0, 0, 2] = 0;
maps[0, 0, 3] = 1;
//Place them back in the terrain
terrainData.SetAlphamaps(pointx, pointy, maps);
}
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 ?