python中的二维噪声生成

2024-10-01 22:32:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我用pygame创建了一个生成随机地形并显示出来的程序。但我的地形生成是随机的,因此非常糟糕,我开始寻找噪音。 噪音包对我不起作用它给出了这个错误:

Command "c:\users\intel\appdata\local\programs\python\python36-32\python.exe -u -c "import setuptool
s, tokenize;__file__='C:\\Users\\Intel\\AppData\\Local\\Temp\\pip-build-vepf8y8v\\noise\\setup.py';f
=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compil
e(code, __file__, 'exec'))" install --record C:\Users\Intel\AppData\Local\Temp\pip-yim68ua0-record\i
nstall-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\User
s\Intel\AppData\Local\Temp\pip-build-vepf8y8v\noise\

而且我完全不知道怎么做。 假设我想产生这样的结果:

^{pr2}$

0代表草,1代表水,2代表沙子。沙子必须紧挨着水的末端,水需要有一些水状的形状。在


Tags: pipbuildlocalcode代表recordusersappdata
2条回答

对我来说,地形生成是一个多步骤的过程。在

(1)从圆顶等原始形状开始,这些形状可以通过程序生成。或者,你也可以用Voronoi板做实验,在碰撞发生的地方使能级升高。还要看看柏林噪音。在

(2a)利用降雨模拟侵蚀。河流侵蚀可以用球体“滚”下地形的斜坡来模拟。当水滴流过一个节点时,它会带走一小块土地,从而降低土地的高度。在

液滴的下一个位置是由相邻节点或分片之间的最低负标高变化决定的。在每一轮侵蚀,甚至下降,你可以平滑地形,这样未来的下降将知道在哪里下降。在

当你的水滴沿着斜坡滚下时,它们最终需要找到一个停止点。最现实、最耗时的方法是找出洪水水位,形成湖泊和海洋。一种更快的方法是随着时间的推移,液滴的尺寸减小,当它们达到零时停止。在

(2b)绘制侵蚀水流图。这有利于河流的生成。其基本思想是找到一个流出的边缘点。可以有多个流出点,但必须并行处理河流生成算法。算法的基本思想是

start out by making directed graph(s) for water flow:
    push the edge points onto a stack as seeds;
    while stack is not empty:
        pop a node;
        mark node as visited;
        randomly select a number of backflow forks (1 to 3 usually works);
        select up to that number of unvisited nodes, depending on which are unvisited;
        // this can be done randomly or based on the height of primitive terrain;
        make links from popped node to selected fork nodes;
        push the selected fork nodes;

now simulate flow volume:
    initialize a flow-volume value for each node or tile to be zero;
    for each tile on the map:
        push tile on to a stack;
        while the node stack is not empty:
            pop a node;
            increment the node's flow volume by 1;
            push any backflow neighbors;

您可以使用水量图来确定侵蚀量(挖土深度和半径)。或者,您可以使用它来预先编写drop flow 2a的算法脚本。但是你必须改变有向图弧的方向。在

(3)侵蚀后,可以对地形施加轻微的噪声,使其摇摆。我发现随机圆半径的加法混合是一种有用且快速的方法。你也可以使用伯林噪声与更高的八度比你使用的基础地形。记住模糊会软化你的坡度,噪音会使事情变得更尖锐。在

这会给你一个不错的地形。如果你想走得更远的话,你可以看看湖水一代和悬崖一代。你还必须决定是否需要其他特征,比如火山口、拱门、台地和塔楼。在

分形的海岸线和河流也值得一看。我只需要得到曼德尔布洛特关于这个问题的所有书籍和论文。尽管他的研究成果可以应用于更广泛的计算机科学和数学领域,但他在地形生成方面有着丰富的信息。在

对于地形生成,大多数算法都使用某种Perlin noise。在

在Python中,实现已经存在。您可以检查caseman的实现on GitHub或{a3}。这个软件包有麻省理工学院的许可证,这意味着你可以不受任何限制地使用它。如果您需要其他类型的噪声,我已经用Python实现了一些噪声生成器,您可以找到on my own GitHub。在


编辑:下面是一个如何使用该模块的示例:

import noise

def neighbors(terrain, i, j, L, C):
    possible_neigbors = [(i+di, j+dj) for di in [-1, 0, 1] for dj in [-1, 0, 1]]
    return [terrain[i2][j2] for (i2, j2) in possible_neigbors
              if i2 >= 0 and j2 >= 0 and i2 < L and j2 < C]

def generate_terrain(L, C):
    noisegrid = [[noise.pnoise2(j/C, i/L) for j in range(C)] for i in range(L)]
    terrain = [[int(noisegrid[i][j] > 0.12) for j in range(C)] for i in range(L)]

    for i in range(L):
        for j in range(C):
            if terrain[i][j] == 0:
                nb = neighbors(terrain, i, j, L, C)
                if 1 in nb:
                    terrain[i][j] = 2

    return terrain

terrain = generate_terrain(6, 20)

当我运行这段代码时,它生成了:

^{pr2}$

对于地形形状的更多控制,或者从一次执行到另一次执行的随机性,请阅读模块代码中的注释。在

相关问题 更多 >

    热门问题