Grass placed with script is less dense then grass placed with brush.

I’m using Unity Editor version 2021.3.21f1, HDRP with Unity’s Terrain.

So I stumbled upon an issue I don’t know how to resolve. I’ve been trying to create a world-generator and placing grass through code seems to give me worse results as using unity’s paint brush tools.


The results above are consistent along all detail resolutions, the brush (left side) always places a tad more grass then my script (right side) which is weird to me.

Here’s the code to place grass on every single position for two grass types:

int detailRes = terrain.terrainData.detailResolution;
int[,] detailMap = new int[detailRes, detailRes];
int[,] detailMap2 = new int[detailRes, detailRes];

for(int i = 0; i < detailRes; i++)
{
    for(int j = 0; j < detailRes; j++)
    {
        detailMap[i, j] = 1;
        detailMap2[i, j] = 1;
    }
}

terrain.terrainData.SetDetailLayer(0, 0, 0, detailMap);
terrain.terrainData.SetDetailLayer(0, 0, 1, detailMap2);

I don’t know whether this is on me or the brush just works better, I couldn’t really find much documentation on this nor people talking about it through a quick google and forum search. Does anybody have experience with this?

edit: I realized that you can write code snippets instead of showing images of the code.

Unlike the terrainlayers (splat textures), the detail layers do not contain weights. Assigning a 1 means 1 detail is placed in that part of the terrain.

You could check the output of the brush by getting the detail layer from the terrain after brushing it instead of writing to it.

Hmm yeah you are right, I was wondering why the detailMap was an int-array instead of float. Quick tests show that increasing it to

detailMap[i, j] = 5;

gives the same result as the terrain brush. Surprisingly I could go way beyond it too but at some point I didn’t see much difference. But setting it to 5 already gives me a really good result.

As I now understand it, the layers correspond to the defined detailPrototypes and the detailMap[i, j] values correspond to the amount of prototypes (e.g. grass here) for a single position. I don’t quite get how it spaces them at that single position but the results are good.

Thanks for the help!