Python pnoise返回0:为什么会这样?

时间:2012-10-04 19:41:54

标签: python floating-point perlin-noise

我正在尝试使用Python的Perlin noise模块中的pnoise2()生成2D 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有效,代码很简单:

import sys
from noise import pnoise2
import random
random.seed()
octaves = random.random()
freq = 16.0 * octaves
for y in range(30):
    for x in range(40):
        n = int(pnoise2(x/freq, y / freq, 1)*10+3)
        if n>=1:
            n=1
        else:
            n=0
        print n,
    print

因此,由于我不知道* 0.5+ 0.5* 10+3等值是什么,我尝试了自己,同时排除了其中一些值。在上面的示例中,random.random()返回一个浮动,例如然后将0.7806等乘以16.0以获得频率。 注意:那已经让我感到困惑,因为八度,我认为它应该是一个整数,因为它告诉连续的噪声函数的数量(注意它是一个整数1,作为{中的第三个值传入{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 x必须包含小数,以便函数返回y以外的值。

此后,我的问题是我不明白为什么。有人可以开导我吗?

1 个答案:

答案 0 :(得分:1)

正如我的评论中所述,您需要在float0.0之间传递x和y的函数1.0

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

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

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

这个细节存在的事实是perlin噪声所固有的。请参阅下面注释中的倒数第二点(摘自来源):

   """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. 
    """