Search code examples
c#unity-game-engineterrainheightmap

Unity/C# - Change size of brush for painting terrain


I'm coding an runtime terrain editor for unity and got stuck. At first I wanted just to paint with textrue the terrain. I found this code and it worked fine:

SCRIPT: TerrainPainter

void Paint(Vector3 point)
{
    mapX = (int)(((point.x - terrainPosition.x) / terrainData.size.x) * heightmapWidth);
    mapY = (int)(((point.z - terrainPosition.z) / terrainData.size.z) * heigtmapHeight);

    splatmapData[mapY, mapX, 0] = element[0, 0, 0] = 0;
    splatmapData[mapY, mapX, 1] = element[0, 0, 1] = 1;

    terrain.terrainData.SetAlphamaps(mapX, mapY, element);
}

But now I want to paint with different sizes/thickness. I have another script, named Terrainmodifier, which I use to raise and lower the terrain. There I have this lines for raising:

SCRIPT: Terrainmodifier

    public void RaiseTerrain(Terrain terrain, Vector3 location, float effectIncrement)
{
    int offset = areaOfEffectSize / 2;

    //--1--
    Vector3 tempCoord = (location - terrain.GetPosition());
    Vector3 coord;

    coord = new Vector3(
        (tempCoord.x / GetTerrainSize().x),
        (tempCoord.y / GetTerrainSize().y),
        (tempCoord.z / GetTerrainSize().z)
        );

    Vector3 locationInTerrain = new Vector3(coord.x * terrainHeightMapWidth, 0, coord.z * terrainHeightMapHeight);
    // End --1--

    // --2--
    int terX = (int)locationInTerrain.x - offset;
    int terZ = (int)locationInTerrain.z - offset;
    // End --2--

    // --3--
    float[,] heights = targetTerrainData.GetHeights(terX, terZ, areaOfEffectSize, areaOfEffectSize);

    for (int xx = 0; xx < areaOfEffectSize; xx++)
    {
        for (int yy = 0; yy < areaOfEffectSize; yy++)
        {
            heights[xx, yy] += (effectIncrement * Time.smoothDeltaTime);
        }
    }

    targetTerrainData.SetHeights(terX, terZ, heights);
}

So I thought I could use this as an aid and transfer it. So I took GetAlphamaps() instead of GetHeights() and added the variable areaOfEffectSize.

SCRIPT: TerrainPainter

    void Paint(Vector3 point)
{

    // --1--
    mapX = (int)(((point.x - terrainPosition.x) / terrainData.size.x) * heightmapWidth);
    mapY = (int)(((point.z - terrainPosition.z) / terrainData.size.z) * heigtmapHeight);
    // End --1--

    // --2--
    int terX = (int)mapX - (areaOfEffectSize / 2);
    int terY = (int)mapY - (areaOfEffectSize / 2);
    // End --2--

    // --3--
    splatmapData = terrainData.GetAlphamaps(terX, terY, areaOfEffectSize, areaOfEffectSize);

    for(int xx = 0; xx < areaOfEffectSize; xx++)
    {
        for (int yy = 0; yy < areaOfEffectSize; yy++)
        {
            splatmapData[yy, xx, 1] = element[0, 0, 1] = 1;
        }
    }
    terrain.terrainData.SetAlphamaps(terX, terY, element);
}

Hope sb can help me find my mistake. How can I change the size of my "brush"?

EDIT: I wrote comments into the code to see the transfered/related lines.


Solution

  • Oh guys, I did a stupid mistake. Solved this problem by passing the splatmapData into SetAlphamaps -.- So the solution is:

    [..]

            for (int xx = 0; xx < areaOfEffectSize; xx++)
        {
            for (int yy = 0; yy < areaOfEffectSize; yy++)
            {
                splatmapData[yy, xx, 0] = 0;
                splatmapData[yy, xx, 1] = 1;
            }
        }
        terrain.terrainData.SetAlphamaps(terX, terY, splatmapData);