2012-10-04 58 views
2

我正尝试使用Python的noise模块中的​​生成2D Perlin noise。我试图按照examples
功能仅仅需要一个xy输入:Python pnoise返回0:为什么呢?

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,作为​​函数中的第三个值传入)。不管怎么说,在功能,从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以外的值。

现在在此之后,我的问题是我不明白为什么。请有人赐教我吗?

+0

Python没有'noise'模块。你使用了什么'noise'模块? – kindall

+0

使用我用pip安装的http://pypi.python.org/pypi/noise/。 – Benjamin

+0

请参阅http://stackoverflow.com/questions/4467638/generating-3d-noise-quickly-in-python以供参考。 – Benjamin

回答

1

正如我在评论中指出的那样,您需要在0.01.0之间传递x和y的这个函数float值。

这可能会作为一个错误提交 - 如果x或y大于1.0,可以引发合适的ValueError。这可能会阻止你不必问这个问题!

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

请考虑该库的编写者是否强制max x和y值为100,并要求您使用整数。突然间,网格上的噪声真的是细胞噪声,因为你永远无法读取任何中间值。使用浮点数可以让用户得到他们需要的任何细节的结果。

这个细节存在的事实是珀林噪声所固有的。请参阅以下注释中的第二个最后一点(取自源代码):

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

明白了,谢谢:) – Benjamin