# Modifying Terrain Splats Dynamically

My aim is to paint the terrain a run time (specifically load-time). While I do have a method for this, as shown below in its most rough form, it takes an EXTREMELY long time to paint even a small square of the terrain.

Could anyone point out a superior method for this?

Code below:

`````` public void Paint()
{
TerrainData td = Terrain.activeTerrain.terrainData;

float[,,] element = new float[1, 1, 8];

for (int y = 0; y < 100; y++)
{
for (int x = 0; x < 100; x++)
{
int rand = Random.Range(0, 4);

if (rand == 0)
{
element[0, 0, 0] = 0;
element[0, 0, 1] = 0;
element[0, 0, 2] = 0;
element[0, 0, 3] = 0;
element[0, 0, 4] = 0;
element[0, 0, 5] = 1;
element[0, 0, 6] = 0;
element[0, 0, 7] = 0;
}
else
if (rand == 1)
{
element[0, 0, 0] = 0;
element[0, 0, 1] = 0;
element[0, 0, 2] = 0;
element[0, 0, 3] = 0;
element[0, 0, 4] = 0;
element[0, 0, 5] = 0;
element[0, 0, 6] = 0;
element[0, 0, 7] = 0;
}
else
if (rand == 2)
{
element[0, 0, 0] = 0;
element[0, 0, 1] = 0;
element[0, 0, 2] = 0;
element[0, 0, 3] = 0;
element[0, 0, 4] = 0;
element[0, 0, 5] = 0;
element[0, 0, 6] = 1;
element[0, 0, 7] = 0;
}
else
if (rand == 3)
{
element[0, 0, 0] = 0;
element[0, 0, 1] = 0;
element[0, 0, 2] = 0;
element[0, 0, 3] = 0;
element[0, 0, 4] = 0;
element[0, 0, 5] = 0;
element[0, 0, 6] = 0;
element[0, 0, 7] = 1;
}

td.SetAlphamaps(x, y, element); // update only the selected
}
}
}
``````

My poor attempt is a rough amalgamation of various methods found across the internet. All it does it, within the small defined 100x100 square of terrain, apply a random splat to each “square”.

If anyone could help, I would be extremely grateful

You could certainly collapse a lot of this code with a for loop or two. That said, you are iteratinig 10000 times the SetAlphamaps call, which only needs to be done once. Move that to the very end. You may also want to put a yield inside one of those loops to prevent it ‘hanging’