【问题标题】:Python pnoise returns 0: why so?Python pnoise 返回 0:为什么会这样?
【发布时间】:2012-09-25 22:13:14
【问题描述】:

我正在尝试使用 Python 的 noise 模块中的 pnoise2() 生成 2D Perlin 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 得到频率。 NB:这已经让我感到困惑,因为八度音阶,我认为应该是一个整数,因为它告诉了连续噪声函数的数量(还要注意它是一个整数 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 必须有小数才能使函数返回0.0 以外的值。

在这之后,我的问题是我不明白为什么。有人可以请教我吗?

【问题讨论】:

标签: python floating-point perlin-noise


【解决方案1】:

如我的评论中所述,您需要在 0.01.0 之间传递 x 和 y 的函数 float 值。

这可能会作为错误提交 - 如果 x 或 y 大于 1.0,则可以提出适当的 ValueError。那可能会让你不必问这个问题!

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

考虑一下这个库的作者是否强制 x 和 y 的最大值为 100,并要求您使用整数。突然之间,噪声实际上是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. 
    """

【讨论】:

    猜你喜欢
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 2023-02-24
    • 2014-12-21
    • 1970-01-01
    • 2013-08-09
    • 2015-08-18
    • 2020-01-07
    相关资源
    最近更新 更多