首页 > 解决方案 > 使用 perlin 噪声计算孤岛误差

问题描述

我正在开发一个小项目,它是一个 100 x 100 六边形的网格。在下面的脚本中,我用柏林噪声绘制了我的六边形,但我想要孤岛的格式并没有消失。我会留下我的代码和 2 个示例,因为我的地图会保留以及我希望它如何保留。

我的岛

我的岛

根据我的需要

我需要

int getColor(float x, float z)
{
    xTO = (int)x / terrainWidth - 30;
    zTO = (int)z / terrainHeight - 30;

    float v = Mathf.PerlinNoise((xTO + x + seed) * freq, (zTO + z) * freq);
    //  v += 0.001f;

    float form = formWorld(x, z);


    if (v < 0.25f)
    {
        //water
        return 0;
    }
    else if (v < 0.5f)
    {
        //sand
        return 1;
    }

    else if (v < 0.75f)
    {
        //grass
        return 2;
    }
    else
    {
        //Trees / Forest
        MakeNewTree(new Vector3(xx, 0, z * 7.5f));
        return 2;
    }
}

标签: c#unity3dperlin-noise

解决方案


如果你想让你的图像看起来更像第二个,最好的选择是添加一个圆形渐变来抵消你的 Perlin Noise。

最简单的方法是测量到中心的距离并将其与柏林噪声相结合。

这是一些未经测试的代码。

    int getColor(float x, float z)
    {
        xTO = (int)x / terrainWidth - 30;
        zTO = (int)z / terrainHeight - 30;

        float v = Mathf.PerlinNoise((xTO + x + seed) * freq, (zTO + z) * freq);
        //  v += 0.001f;
        v -= CircleOffset(x,z)/2; //Change the two to make the island bigger.

        float form = formWorld(x, z);


        if (v < 0.25f)
        {
            //water
            return 0;
        }
        else if (v < 0.5f)
        {
            //sand
            return 1;
        }

        else if (v < 0.75f)
        {
            //grass
            return 2;
        }
        else
        {
            //Trees / Forest
            MakeNewTree(new Vector3(xx, 0, z * 7.5f));
            return 2;
        }
    }

    float CircleOffset(float x, float y)
    {
        Vector2 center = new Vector2(terrainWidth/2,terrainHeight/2);
        float distance = Mathf.Sqrt((center.x - x)*(center.x - x) + (center.y - y) * (center.y - y));
        return distance/terrainWidth;
    }

希望这可以帮助!


推荐阅读