带有Perlin噪声的计算岛误差

时间:2019-05-26 13:58:11

标签: c# unity3d perlin-noise

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

我的小岛

My Island

我需要

Im Need

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

1 个答案:

答案 0 :(得分:0)

如果您希望图像看起来更像第二张图像,最好的选择是添加一个圆形渐变,以抵消您的Perlin噪点。

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

这是一些 unested 代码。

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

希望这会有所帮助!