Removing grass doesn t really work

Hello people

I have a map with terrain and grass
When i add a building on it, i would love the grass to disappear under (because else, the grass appears in the building :p)

So i implemented this

private void CutGrass(Vector3 buildingCenter, int size)
    {
        Vector3 terrainPos = terrainManager.ConvertToSplatMapCoordinate(buildingCenter);

        int[,] map = terrain.terrainData.GetDetailLayer((int)terrainPos.x - size / 2, (int)terrainPos.z - size / 2,
          size, size, 0);

        // For each pixel in the detail map...
        for (int z = 0; z < size; z++)
        {
            for (int x = 0; x < size; x++)
            {
                map[x, z] = 0;
            }
        }

        // Get all of layer zero.
        for (int i = 0; i < terrain.terrainData.terrainLayers.Length; i++)
        {
            // Assign the modified map back.
            terrain.terrainData.SetDetailLayer((int)terrainPos.x - size / 2, (int)terrainPos.z - size / 2, i, map);
        }
    }

This code works quite well
Like this, whatever are the layers under the building, the grass disappears… until…
until the grass index is too high

When i m creating my grass at the beginning, it seems that if i have more grass initial objects than the number of layers, the other ones are not disappearing

For example
If i have 3 layers of ground (rock, dirt and green)
and i have 5 type of grass (generally these are png that i add as billboard a bit everywhere)
and then i put a building somewhere
Only the 3 first type of grass under that building would be removed (it means, every instance of these 3 types would be removed which is good, but not the 2 others)
the 2 next, they stay

AND THAT IS ANNOYING :stuck_out_tongue:

So if you have any idea how to remove ALL the type of grass, i would be really pleased :slight_smile:

I hope i dont sound rude…why are you wasting your time writing a script…delete the grass thats under the buiilding?

because how could i do that during runtime ???
I can t go to every people playing the game and say, hey hey i ll cut the grass, wait a minute

Sorry, now i understand what you are saying, you have set up layers for certain objects, and your trying to exclude all 8 of them. Ill replicate this with your code when i get a chance…

Did you happen to solve this?
And how does this work: Vector3 terrainPos = terrainManager.ConvertToSplatMapCoordinate(buildingCenter);?

edit:
if anyone later in life happens to find this, this is how I got this working:

public void RemoveGrassFromArea(Vector3 point, int size)
{
    int newsize = size * 10;
    Vector2 spot = WorldToTerrain(new Vector3(point.x - size, point.y, point.z - size));
    var map = terrainData.GetDetailLayer((int)spot.x, (int)spot.y, newsize, newsize, 0);


    for (int y = 0; y < newsize; y++)
    {
        for (int x = 0; x < newsize; x++)
        {
            map[x, y] = 0;
        }
    }
    terrainData.SetDetailLayer((int)spot.x, (int)spot.y, 0, map);
}
private Vector2 WorldToTerrain(Vector3 wordCor)
{
    Vector2 vecRet = new();
    Vector3 terPosition = terrain.transform.position;
    vecRet.x = ((wordCor.x - terPosition.x) / terrain.terrainData.size.x) * terrain.terrainData.detailWidth;
    vecRet.y = ((wordCor.z - terPosition.z) / terrain.terrainData.size.z) * terrain.terrainData.detailHeight;
    return vecRet;     
}
2 Likes

Could you please post a full c+ scipt of it? Sorry to ask but im autistic and have a hard time with scripts. I have a buildable asset I use, but it never removes the grass from under the placables. ive been searching for a way to do this for months now :frowning: