Python pnoise返回0:为什么?

2024-05-19 15:20:55 发布

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

我试图使用Python的noise模块中的pnoise2()生成2DPerlin noise。我试着跟随examples
该函数只需要xy输入:

noise2(x, y, octaves=1, persistence=0.5, repeatx=1024, repeaty=1024, base=0.0)

但是不管我传入什么,函数总是返回0.0。怎么会?我错过什么了吗?在

[编辑:05/10/12]

一些other example工作,代码很简单:

^{pr2}$

所以,由于我不知道* 0.5或{}或{}等值是为了什么,我自己尝试了,同时排除了其中一些值。在上面的例子中,random.random()返回一个浮点值,例如0.7806等,然后乘以16.0得到频率。注:这已经让我困惑了,因为八度音阶,我认为应该是一个整数,因为它告诉了连续的噪声函数的数量(另外请注意,它是一个整数1,在pnoise2()函数中作为第三个值传入)。无论如何,在函数中,range()中的一些整数被frequency除,现在大约是13.88

>>> pnoise2(1/13.88, 1/13.88, 1)
0.06868855153353493
>>> pnoise2(0.07, 0.06, 1)
0.06691436186932441
>>> pnoise2(3.07, 3.04, 1)
0.06642476158741428 
>>> pnoise2(3.00, 2.03, 1)                                                   
0.02999223154495647

但话说回来:

>>> pnoise2(3.0, 2.0, 1)                                                   
0.0
>>> pnoise2(1.0, 2.0, 1)                                                     
0.0
>>> pnoise2(4.0, 5.0, 1)
0.0

因此,我的结论是xy必须有小数,这样函数才能返回0.0以外的值。在

在这之后,我的问题是我不明白为什么。有人能开导我吗?在


Tags: 模块函数编辑base整数randomexamplesother
1条回答
网友
1楼 · 发布于 2024-05-19 15:20:55

正如我在评论中提到的,您需要在0.01.0之间传递这个函数float值。在

这可能是一个错误-如果x或y大于1.0,则可能会引发一个适当的ValueError。这可能会阻止你问这个问题!在

这是特定于实现的,但它只是一种方法,允许您以您想要/需要的任何解决方案获得结果。在

考虑这个库的编写者是否强制将max x和y值设置为100,并要求您使用int。突然间,噪声实际上是100x100网格上的单元噪声,因为您永远无法读取任何中间值。使用浮动允许用户在他们需要的任何细节级别上获得结果。在

这个细节的存在是柏林噪声固有的事实。请参阅以下注释中的最后一点(摘自来源):

   """Perlin simplex noise generator

    Adapted from Stefan Gustavson's Java implementation described here:

    http://staffwww.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf

    To summarize:

    In 2001, Ken Perlin presented 'simplex noise', a replacement for his classic
    noise algorithm.  Classic 'Perlin noise' won him an academy award and has
    become an ubiquitous procedural primitive for computer graphics over the
    years, but in hindsight it has quite a few limitations.  Ken Perlin himself
    designed simplex noise specifically to overcome those limitations, and he
    spent a lot of good thinking on it. Therefore, it is a better idea than his
    original algorithm. A few of the more prominent advantages are: 

    * Simplex noise has a lower computational complexity and requires fewer
      multiplications. 
    * Simplex noise scales to higher dimensions (4D, 5D and up) with much less
      computational cost, the complexity is O(N) for N dimensions instead of 
      the O(2^N) of classic Noise. 
    * Simplex noise has no noticeable directional artifacts.  Simplex noise has 
      a well-defined and continuous gradient everywhere that can be computed 
      quite cheaply. 
    * Simplex noise is easy to implement in hardware. 
    """

相关问题 更多 >