Unity:Runtime-Terrain-Editor如何举一个圈

时间:2018-10-28 19:22:03

标签: c# unity3d terrain heightmap

我正在尝试创建一个可以在运行时运行的地形编辑器。在这一点上,我可以举一个正方形,但在举一个圆形时遇到一些问题。我看到了这个post,并认为这是解决我的问题的方法,所以我将其转移到了我的代码中。它确实在某处(我没有点击的地方)上画了一个圆圈,但效率不高,因为它会更新整个地形。

整个功能:

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

    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);

    int terX = (int)locationInTerrain.x - offset;
    int terZ = (int)locationInTerrain.z - offset;
    int terXInv = (int)locationInTerrain.x + offset;
    int terZInv = (int)locationInTerrain.z + offset;

    //This raises a square
    //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);

    //This raises a circle
    float[,] heights = targetTerrainData.GetHeights(0, 0, terrainHeightMapWidth, terrainHeightMapHeight);

    for (int xx = terX; xx < terXInv; xx++)
    {
        for (int yy = terZ; yy < terZInv; yy++)
        {
            float currentRadiusSqr = (new Vector2(locationInTerrain.x, locationInTerrain.z) - new Vector2(xx, yy)).sqrMagnitude;
            if(currentRadiusSqr < offset*offset)
            {
                heights[xx, yy] += (effectIncrement * Time.smoothDeltaTime);
            }                
        }
    }
    targetTerrainData.SetHeights(0, 0, heights);
}

以下所有行均只是上面的摘录。

我想我的问题是这一行:

float[,] heights = targetTerrainData.GetHeights(0, 0, terrainHeightMapWidth, terrainHeightMapHeight);

因为提高正方形效果很好。我只是更新了一个特定区域,所以它在不降低帧率的情况下更新了地形:

float[,] heights = targetTerrainData.GetHeights(terX, terZ, areaOfEffectSize, areaOfEffectSize);
[...]
targetTerrainData.SetHeights(terX, terZ, heights);

所以我的问题是,如何像我增加正方形一样,增加一个圆并更新更改?仅更新更改的高度?

我试图从正方形开始使用这条线:

float[,] heights = targetTerrainData.GetHeights(terX, terZ, areaOfEffectSize, areaOfEffectSize);
[...]
targetTerrainData.SetHeights(terX, terZ, heights);

与:

        for (int xx = terX; xx < terXInv; xx++)
    {
        for (int yy = terZ; yy < terZInv; yy++)
        {
            float currentRadiusSqr = (new Vector2(locationInTerrain.x, locationInTerrain.z) - new Vector2(xx, yy)).sqrMagnitude;
            if(currentRadiusSqr < offset*offset)
            {
                heights[xx, yy] += (effectIncrement * Time.smoothDeltaTime);
            }                
        }
    }

但是没有用。我收到错误消息:

IndexOutOfRangeException: Index was outside the bounds of the array.

0 个答案:

没有答案