c# - Perlin 地形页面不匹配 : elusive bug

标签 c# debugging unity3d terrain perlin-noise

我一直在使用 Unity 游戏引擎开发一款游戏,我希望它能够将分页地形用于无限世界(如今的常见主题)。我的地形生成器专门使用 perlin 噪声。但此时,开发已被一个错误严重拖慢:页面边缘不匹配,甚至没有关闭。问题出在我生成地形的代码中,或者可能出在每次我完成页面生成时运行的另一段代码中。但问题肯定不在 u​​nity 函数调用中,所以即使你不熟悉 unity 也可以帮助我。

可能的原因... 1) 为每页的错误位置采样的 Perlin 噪音,数学失败。 2) 在页面生成之间重新播种随机数生成器。 3) Perlin 噪声在页面生成之间重新播种。 4)?我不知道。

注意:查看所有其他代码,我几乎排除了数字 2 和 3。我只为每个生成器播种一次,Unity 不会恰好在生成器运行之间重新播种随机数。

实际上,如果您能描述一种调试方法,使这更容易,请指教。从长远来看,这对我的帮助会大得多。

这是我为每个页面运行一次的代码,带有完整注释,因此您不必了解 Unity3D...

 /* x and y are the indices of the tile being loaded. The game maintains a
    square of pages loaded, where the number of pages per side is equal to
    loadSquares (see below). x and y are the indices of the page to generate
    within that square. */
    public void generate(int x, int y)
    {
         /* pagePos represents the world x and y coordinates of the bottom left
            corner of the page being generated. This is given by...
              new Vector2((-(float)loadSquares / 2.0f + x + xCoord) * tileSize, 
                          (-(float)loadSquares / 2.0f + y + zCoord) * tileSize);
            This is because the origin tile's center is at 0, 0. xCoord represents
            the tile that the target object is on, which is what the loaded square
            is centered around. tileSize is the length of each side of each tile.
         */
        Vector2 pagePos = getPagePos(x, y);
        //Here I get the number of samples x and y in the heightmap.
        int xlim = td[x,y].heightmapWidth;
        int ylim = td[x,y].heightmapHeight;
            //The actual data
        float[,] array = new float[xlim, ylim];
            //These will represent the minimum and maximum values in this tile.
            //I will need them to convert the data to something unity can use.
        float min = 0.0f;
        float max = 0.0f;
        for(int cx = 0; cx < xlim; cx++)
        {
            for(int cy = 0; cy < ylim; cy++)
            {
                //Here I actually sample the perlin function.
                //Right now it doesn't look like terrain (intentionally, testing).
                array[cx,cy] = sample(
                    new Vector3((float)cx / (float)(xlim - 1) * tileSize + pagePos.x, 
                                (float)cy / (float)(ylim - 1) * tileSize + pagePos.y, 
                    122.79f));
                //On the first iteration, set min and max
                if(cx == 0 && cy == 0)
                {
                    min = array[cx,cy];
                    max = min;
                }
                else
                {
                    //update min and max
                    min = Mathf.Min(min, array[cx,cy]);
                    max = Mathf.Max(max, array[cx,cy]);
                }
            }
        }
        //Set up the Terrain object to receive the data
        float diff = max != min ? max - min : 10.0f;
        tr[x,y].position = new Vector3(pagePos.x, min, pagePos.y);
        td[x,y].size = new Vector3(tileSize, diff, tileSize);
        //Convert the data to fit in the Terrain object
     /* Unity's terrain only accepts values between 0.0f and 1.0f. Therefore,
        I shift the terrain vertically in the code above, and I
        squish the data to fit below.
     */
        for(int cx = 0; cx < xlim; cx++)
        {
            for(int cy = 0; cy < ylim; cy++)
            {
                array[cx,cy] -= min;
                array[cx,cy] /= diff;
            }
        }
        //Set the data in the Terrain object
        td[x,y].SetHeights(0, 0, array);
    }
}

有趣且可能很重要的细节:瓷砖与 x/z 方向上与其角相邻的瓷砖正确连接。每当图 block 不具有相同的 x-to-z 增量时,就会发生采样偏移。在下图中,右侧图 block 是另一个图 block 的 +x 和 +z。具有这种关系的所有图 block 都已正确连接。

enter image description here

这是以 zip 格式上传的项目文件。告诉我它是否不工作或什么... http://www.filefactory.com/file/4fc75xtd3yzl/n/FPS_zip

要查看地形,请在切换到 testScene 后按“播放”(如果它没有从那里开始)。 GameObject 生成数据和地形对象(它具有来自 scripts/General/附件的 RandomTerrain 脚本)。您可以从那里将参数修改为 perlin 噪声。请注意,目前只有第一个 perlin octave,o_elevator 在地形生成中处于事件状态。为了解决这个故障,所有其他公共(public) perlin octave 变量都没有影响。

最佳答案

TL;DR:您需要删除 getTargetCoords 并使用 tr[x,y].position = new Vector3(pagePos.y, 0.0f, pagePos.x)

如何实现上述目标:一次在一维中工作。这意味着转动这个(从你附加的统一代码):

            array[cx,cy] = sample(
                new Vector3((float)cx * tileSize / (float)(xlim - 1) + pagePos.x, 0.0f,
                            (float)cy * tileSize / (float)(ylim - 1) + pagePos.y));

进入这个:

            array[cx,cy] = sample(
                new Vector3(
                    (float)cx * tileSize / (float)(xlim - 1) + pagePos.x,
                    0.0f,
                    0.0f
                ) 
            ); 

我很快意识到您的 getPagePos 有问题,因为它从 getTargetCoords 添加了一个值,而该函数使用了由 设置的位置getPagePos 函数!圈子里的圈子,伙计们。

protected void getTargetCoords()
{
    xCoord = Mathf.RoundToInt(target.position.x / tileSize);
    zCoord = Mathf.RoundToInt(target.position.z / tileSize);
}

所以让我们丢掉那个函数和对它的引用。事实上,让我们暂时简化 getPagePos:

protected Vector2 getPagePos(int x, int y)
{
    return new Vector2( 0.0f,0.0f);
}

天哪,TAWNOS 疯了?!嗯,是的,但这不是重点。假设您的样本函数是正确的(我根据您的断言授予您一个假设),那么我们应该从获取简单值(零!)开始并验证添加的每个额外假设。

简化了getPagePos,让我们再看一下(float)cx * tileSize/(float)(xlim - 1) + pagePos.x。它想做什么?好吧,它似乎试图通过从图 block 的原点偏移来找到当前样本的世界坐标点。让我们验证这是一个真实的假设:xLim 样本分布在 tileSize 单位上,因此世界坐标中的每个步长大小都是 tileSize/xLim。将其乘以当前值将得到我们的偏移量,因此这段代码看起来是正确的。

现在,让我们获取一个简单的图 block 偏移量并查看所有内容是否匹配。我不会尝试将水滴居中放置在所有图 block 的中间,而是进行一个简单的网格偏移(稍后可以对其进行调整以恢复居中)。

    return new Vector2( 
        tileSize * (float)x,
        0.0f
    );

运行此程序时,您可能很快就会发现问题所在。底部边缘映射旁边的图 block 的顶部边缘,而不是右边缘与左边缘匹配。无需深入研究,这可能是由您的采样方法的工作方式引起的。我不确定。这是更新后的功能:

protected Vector2 getPagePos(int x, int y)
{
    float halfTile = loadSquares / 2.0f;
    return new Vector2( 
        (-halfTile * tileSize) + (x * tileSize),
        (-halfTile * tileSize) + (y * tileSize)
    );
}

public void generate(int x, int y)
{
    if(x >= loadSquares || x < 0 || y >= loadSquares || y < 0) return;
    t[x,y] = Terrain.CreateTerrainGameObject(new TerrainData()).GetComponent<Terrain>();
    t[x,y].name = "Terrain [" + x + "," + y + "]";
    td[x,y] = t[x,y].terrainData;
    td[x,y].heightmapResolution = resolution;

    tr[x,y] = t[x,y].transform;
    Vector2 pagePos = getPagePos(x, y);
    //Actual data generation happens here.
    int xLim = td[x,y].heightmapWidth;
    int yLim = td[x,y].heightmapHeight;

    float[,] array = new float[xLim, yLim];
    float min = int.MaxValue;
    float max = int.MinValue;
    for(int cx = 0; cx < xLim; cx++)
    {
        for(int cy = 0; cy < yLim; cy++)
        {
            array[cx,cy] = sample(
                new Vector3(
                    (float)cx * (tileSize / (float)(xLim - 1)) + pagePos.x, 
                    0.0f,
                    (float)cy * (tileSize / (float)(yLim - 1)) + pagePos.y
                ) 
            ); 

            if(min > array[cx,cy]) 
                min = array[cx,cy];
            if(max < array[cx,cy]) 
                max = array[cx,cy];

        }
    }

    //Set up the Terrain object to receive the data
    float diff = max != min ? max - min : 10.0f;
    tr[x,y].position = new Vector3(pagePos.y, min, pagePos.x);
    td[x,y].size = new Vector3(tileSize, diff, tileSize);

    //Convert the data to fit in the Terrain object
    for(int cx = 0; cx < xLim; cx++)
    {
        for(int cy = 0; cy < yLim; cy++)
        {
            array[cx,cy] -= min;
            array[cx,cy] /= diff;
        }
    }
    //Set the data in the Terrain object
    td[x,y].SetHeights(0, 0, array);
}

关于c# - Perlin 地形页面不匹配 : elusive bug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11453809/

相关文章:

javascript - 从 Chrome 开发工具回溯中排除 jQuery

c# - 弹丸统一

unity3d - #pragma vertex vert_img 在 Unity 着色器中有什么作用?

ios - 是什么导致 iOS 游戏被添加到用户的游戏连接中?

c# - 如何对小数值列表进行四舍五入?

c# - 如何使用 System.Linq.Dynamic.Core 将复杂的查询字符串转换为 lambda 表达式

c++ - Visual Studio - 如何在显示消息框后捕获调试(中断)

c# - Winform 应用程序第一个 Web 请求很慢

c# - ProgressBar 从另一个线程更新

c++ - 如何调试 "Invalid parameter passed to C runtime function"?