地形刷新滞后统一 如何有效地刷新地形

本文关键字:刷新 有效地 滞后 | 更新日期: 2023-09-27 18:32:52

在游戏中,玩家可以砍树。然后我实例化一棵倒下的树在它的位置。

我从地形列表中删除树并刷新地形,如下所示:

        var treeInstancesToRemove = new List<TreeInstance>(terrain.treeInstances);
        treeInstancesToRemove.RemoveAt(closestTreeIndex);
        terrain.treeInstances = treeInstancesToRemove.ToArray();
        // I refresh the terrain so the collider gets removed...
        float[,] heights = terrain.GetHeights(0, 0, 0, 0);
        terrain.SetHeights(0, 0, heights);

地形非常大...这意味着每当一棵树被砍伐时,游戏就会冻结几秒钟,然后恢复(刷新时)。有没有更快或更有效的方法可以让我看看?每砍一棵树就冻住不是很理想吗?

提前非常感谢!

地形刷新滞后统一 如何有效地刷新地形

我能建议的最好的事情是将世界分成可以单独更新的块。要么这样,要么让碰撞体在与主线程不同的线程中更新。

    float hmWidth = grav.currentTerrain.terrainData.heightmapWidth;
    float hmHeight = grav.currentTerrain.terrainData.heightmapHeight;
    // get the normalized position of this game object relative to the terrain
    Vector3 tempCoord = (transform.position - grav.currentTerrain.gameObject.transform.position);
    Vector3 coord;
    coord.x = tempCoord.x / grav.currentTerrain.terrainData.size.x;
    coord.y = tempCoord.y / grav.currentTerrain.terrainData.size.y;
    coord.z = tempCoord.z / grav.currentTerrain.terrainData.size.z;
    // get the position of the terrain heightmap where this game object is
    int posXInTerrain = (int)(coord.x * hmWidth);
    int posYInTerrain = (int)(coord.z * hmHeight);
    // we set an offset so that all the raising terrain is under this game object
    //int offset = size / 2;
    // get the heights of the terrain under this game object
    float[,] heights = grav.currentTerrain.terrainData.GetHeights(posXInTerrain, posYInTerrain, 1, 1);
    grav.currentTerrain.terrainData.SetHeights(posXInTerrain, posYInTerrain, heights);  //THIS CHANGES TERRAIN FOR GOOD