首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在无限映射增长时连续生成Perlin噪声?

如何在无限映射增长时连续生成Perlin噪声?
EN

Stack Overflow用户
提问于 2019-10-05 02:07:33
回答 1查看 1.4K关注 0票数 0

编辑:我最终使用了在这里找到的FastNoise库:https://github.com/Auburns/FastNoise,它包含了人们可能需要生成许多不同类型噪声的所有内容。正如标题可能暗示的那样,它也相当快!

我正在创建一个二维的,程序生成的世界。随着播放器的移动,我从光盘加载和卸载数据块。我使用元胞自动机函数来定义每个块中的本地瓦片是如何设计的,但我需要噪声(在本例中为Perlin)来定义在创建新块时每个块将是什么生物群类型。我理解如何在0和1之间转换小数来表示这一点,我唯一的问题是,我遵循的创建Perlin noise的教程要求您向其传递一个预定义的2d数组,并返回一个相同大小的noise数组。因为我的世界是动态增长的,所以我对如何使用固定大小的数组来指定新的块类型感到有点困惑。

我看到的其他答案并没有确切地涵盖如何处理噪声生成的无限部分。我最好的猜测是,我需要以某种方式生成或扩展每个新创建的块的噪声,尽管我如何做到这一点让我感到困惑。

下面是我从这里翻译成C#的一些代码:http://devmag.org.za/2009/04/25/perlin-noise/

诚然,这里的一些数学我还不完全理解,特别是位函数!

代码语言:javascript
复制
public class PerlinNoiseGenerator
    {
        public int OctaveCount { get; set; }
        public float Persistence { get; set; }


        public PerlinNoiseGenerator(int octaveCount, float persistence)
        {
            this.OctaveCount = octaveCount;
            this.Persistence = persistence;
        }

        public float[,] GenerateWhiteNoise(int width, int height)
        {
            float[,] noiseFieldToReturn = new float[width, height];
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    noiseFieldToReturn[i, j] = (float)Game1.Utility.RGenerator.NextDouble() % 1;
                }
            }
            return noiseFieldToReturn;
        }

        public float[,] SmoothNoiseField(float[,] whiteNoise, int octave)
        {
            int width = whiteNoise.GetLength(0);
            int height = whiteNoise.GetLength(1);
            float[,] smoothField = new float[width, height];

            int samplePeriod = 1 << octave;

            float sampleFrequency = 1.0f / samplePeriod;

            for(int i =0; i < width; i++)
            {
                int samplei0 = (i / samplePeriod) * samplePeriod;
                int samplei1 = (samplei0 + samplePeriod) % width;

                float horizontalBlend = (i - samplei0) * sampleFrequency;
                for(int j =0; j < height; j++)
                {
                    int samplej0 = (j/samplePeriod) * samplePeriod;
                    int samplej1 = (samplej0 + samplePeriod) % height;
                    float verticalBlend = (j - samplej0) * sampleFrequency;

                    float top = LinearInterpolate(whiteNoise[samplei0, samplej0],
                        whiteNoise[samplei1, samplej0], horizontalBlend);

                    float bottom = LinearInterpolate(whiteNoise[samplei0, samplej1],
                        whiteNoise[samplei1, samplej1], horizontalBlend);

                    smoothField[i, j] = LinearInterpolate(top, bottom, verticalBlend);
                }
            }

            return smoothField;
        }

        public float[,] GeneratePerlinNoise(float[,] baseNoise, int octaveCount)
        {
            int width = baseNoise.GetLength(0);
            int height = baseNoise.GetLength(1);

            float[][,] smoothNoise = new float[octaveCount][,];

            float persistance = .5f;

            for(int i =0; i < octaveCount;i++)
            {
                smoothNoise[i] = SmoothNoiseField(baseNoise, i);
            }
            float[,] perlinNoise = new float[width, height];
            float amplitude = 1f;
            float totalAmplitude = 0.0f;

            for(int octave = octaveCount - 1; octave > 0; octave-- )
            {
                amplitude *= persistance;
                totalAmplitude += amplitude;

                for(int i =0; i < width;i++)
                {
                    for(int j =0; j < height; j++)
                    {
                        perlinNoise[i, j] += smoothNoise[octave][i, j] * amplitude;
                    }
                }
            }

            for(int i =0; i < width; i++)
            {
                for(int j =0; j < height; j++)
                {
                    perlinNoise[i, j] /= totalAmplitude;
                }
            }
            return perlinNoise;
        }

        public float LinearInterpolate(float a, float b, float alpha)
        {
            return a * (1 - alpha) + alpha * b;
        }
    } 

这段代码应该编译并产生一个固定大小的噪声数组

EN

回答 1

Stack Overflow用户

发布于 2019-10-15 14:47:00

您要确保的主要内容是起始随机噪声是伪随机的,因此您始终有一个坐标的“固定随机值”。

这可能是你不得不重写你的随机噪声发生器,使用坐标作为输入。我假设你的地图有一个随机的种子编号,所以你可以以这篇文章为起点,添加一个因子:A pseudo-random number generator based on 2 inputs

为了给您的地图制作提供一点灵感,我不久前写了这篇文章:https://steemit.com/map/@beeheap/create-a-fantasy-grid-map-in-excel

在您的评论之后添加:您需要更改的唯一函数是GenerateWhiteNoise函数。我不会说C#,但大体是这样的:

代码语言:javascript
复制
GenerateWhiteNoise(int x_start_coord, int y_start_coord, int random_seed) {
    int default_x_width = 100;
    int default_y_heigth = 50;
    float[,] noiseFieldToReturn = new float[width, height];

    for (in x = x_start_coord; i < default_x_width + x_start_coord; x++)
    {
        for (in y = y_start_coord; i < default_y_width + y_start_coord; y++)
        {
            noiseFieldToReturn[i, j] = (float)pseudo_rnd_value(x, y, random_seed);
        }
    }
    return noiseFieldToReturn;
}

这将为您提供构建地图瓦片所需的伪随机值,您唯一需要的就是玩家的坐标(x和y)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58241309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档