コード例 #1
0
    public void PerlinNoise1DTerrainGeneration(int nOctaves, int perlinSeed, float fBias)
    {
        if (_map == null)
        {
            return;
        }

        int maxOctavesCount = 0;
        int worldWidth      = _map.GetMapSize().x;

        while (worldWidth != 0)
        {
            worldWidth /= 2;
            maxOctavesCount++;
        }

        if (nOctaves > maxOctavesCount)
        {
            nOctaves = maxOctavesCount;
        }

        ClearWorld();

        float defaultChange = 1.0f / _map.GetMapSize().y;

        PerlinNoise pn = new PerlinNoise(perlinSeed);

        float[] perlinArray = new float[_map.GetMapSize().x];

        pn.Get1DPerlinNoise(_map.GetMapSize().x, nOctaves, fBias, ref perlinArray);

        for (int x = 0; x < _map.GetMapSize().x; x++)
        {
            int height = (int)(perlinArray[x] * _map.GetMapSize().y);

            for (int y = 0; y < height; y++)
            {
                GameObject cell = _map.GetCellAt(new Vector2Int(x, y)).GetGameObject();

                float f           = defaultChange * y;
                Color heightColor = new Color(f, f, f, 1);
                cell.GetComponent <SpriteRenderer>().color = heightColor;
            }
        }
    }