如何计算噪声图上的二维高度差异

How to calculate 2D height disparities on a noise map

我正在使用六边形网格在 Unity (C#) 中创建基于图块的游戏。我正在我的游戏中制作丘陵和山脉,为了优化,我只激活了顶层的瓷砖,而让其余的瓷砖处于非活动状态。我用基于噪声的高度图渲染每个图块(请注意网格本身的颜色,如棕色、绿色和灰色与高度不对应)。

但是,当山变高时,我遇到了问题,并且由于高度差异,一个瓷砖与下一个瓷砖之间的垂直间隙在地图上显示出一个洞。

为了解决这个问题,我正在制作一个追溯函数,它循环遍历噪声图上的每个像素,如果它检测到一个像素和与其相邻的另一个像素之间存在差异,那么它会激活较高瓷砖下方的瓷砖差距。下面的蓝色图块代表它检测到的存在差异的图块,红色的图块是由于更高的对应物被检测到而被激活的图块。

然而,大多数时候,在正确的位置上没有识别出差异。您可以在上图中看到,应该被认为有差异的棕色瓷砖(因为它留下了一个空隙)有一个大洞,下面没有红色瓷砖,而其他没有间隙的瓷砖被认为有差异并且是因此蓝色。这是高度图和检测到的差异之间几乎没有相关性的表示:

上面可以看到,高度图上的白色,代表高的区域,与格子上的蓝色不对应,蓝色应该对应视差(因此主要对应白黑交汇的地方)噪声图,勾勒出高低区域之间的位置)。

这是我检测差异的代码:

//ADD BLOCKS BELOW WHERE MAGNITUDE IS HIGH SO NO OPEN SPACES
int wi = -1;
int thisChunkX = 0;
for (int w = 0; w < thisTexture.width; w++)
{
    wi++;

    if (wi == chunkSize.x) { wi = 0; thisChunkX++; }

    int hi = -1;
    int thisChunkZ = 0;

    for (int h = 0; h < thisTexture.height; h++)
    {
        hi++;

        if (hi == chunkSize.z) { hi = 0; thisChunkZ++; }

        if (w - 1 >= 0 && h - 1 >= 0 && w + 1 < thisTexture.width && h + 1 < thisTexture.height)//for all non-direct borders in the texture
        {
            List<float> surroundingHexes = new List<float>
            {
                thisTexture.GetPixel(w+1, h).maxColorComponent,//top
                thisTexture.GetPixel(w-1, h).maxColorComponent,//bottom
                thisTexture.GetPixel(w-1, h+1).maxColorComponent,//top left
                thisTexture.GetPixel(w+1, h+1).maxColorComponent,//top right
                thisTexture.GetPixel(w-1, h).maxColorComponent,//bottom left
                thisTexture.GetPixel(w+1, h).maxColorComponent//bottom right
            };

            float thisHex = thisTexture.GetPixel(w, h).maxColorComponent;

            foreach (float thisSurroundingHex in surroundingHexes)
            {
                float hexMagnitude = thisHex - thisSurroundingHex;//IMPORTANT!!!

                if (thisTexture.GetPixel(w, h) != new Color(1, 0, 0) && hexMagnitude > 0.25f)//IMPORTANT!!!
                {
                    //DISPARITY IS DETECTED
                    
                    //other stuff

                    break;
                }
            }
        }
    }

即使 hexMagnitude > 0.25f 不是最准确的,自 hexMagnitude = thisHex - thisSurroundingHex 以来,是否仍然存在表示高度差异的强相关性,其中 thisHexthisSurroundingHex 分别是与高度对应的值?任何帮助将不胜感激。谢谢!

我可以建议将 for 循环变量命名为 x 和 y 而不是 w 和 h 吗?我认为 x 和 y 更明显地分配给两个坐标。 反正。我还没有完全检查这是否是唯一的问题,但我觉得你把像素检查设置错了。 (见下文)

thisTexture.GetPixel(w+1, h).maxColorComponent,//top  << shouldn't this be (w, h+1)?
thisTexture.GetPixel(w-1, h).maxColorComponent,//bottom  << Shouldn't this be (w, h-1)?
thisTexture.GetPixel(w-1, h+1).maxColorComponent,//top left  << Looks fine
thisTexture.GetPixel(w+1, h+1).maxColorComponent,//top right  << Looks fine
thisTexture.GetPixel(w-1, h).maxColorComponent,//bottom left  << This is the same as "bottom". Shouldn't this be (w-1, h-1)?
thisTexture.GetPixel(w+1, h).maxColorComponent//bottom right  << This is the same as "top". shouldn't this be (w+1, h-1)?

此外,理论上您不需要像素周围的每个像素。当您检查右下角的像素并稍后到达同一像素时,您不再需要检查左上角的像素,因为已经进行了该检查。 所以你真正需要的是

thisTexture.GetPixel(w, h+1).maxColorComponent,//top
thisTexture.GetPixel(w+1, h+1).maxColorComponent,//top right
thisTexture.GetPixel(w+1, h-1).maxColorComponent,//bottom right

根据检查的瓷砖顺序,您可能需要更改检查方向,甚至添加第四个检查方向(之前没有使用过六边形网格)