Question

I'm trying to generate 2D Perlin noise using pnoise2() from Python's noise module. I have tried to follow examples.
The function merely requires an x and y input:

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

Yet no matter what I pass in, the function always returns 0.0. How come? Did I miss something?

[edit: 05/10/12]

Some other example works, the code is simple:

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

So, since I don't know what the * 0.5 or + 0.5 or * 10+3 etc. values are for, I tried myself, whilst excluding some of these values. In the example above random.random() returns a float e.g. 0.7806 etc. which is then multiplied by 16.0 to get the frequency. NB: That already puzzles me because octaves, I thought ought to be an integer since it tells the number of successive noise functions (note also it is an integer 1 that is passed in as 3rd value in the pnoise2() function). Anyways, in the function, some integers from the range() are then divided by the frequency, now about 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

But then again:

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

So I conclude that x or y must have decimals in order for the function to return a value other than 0.0.

Now after this, my problem is I don't understand why. Can someone enlighten me please?

Était-ce utile?

La solution

As noted in my comment, you need to pass this functions float values for x and y between 0.0 and 1.0.

This could probably be filed as a bug - if x or y are greater than 1.0, an appropriate ValueError could be raised. That would probably have prevented you from having to ask this question!

This is implementation specific, but it's just a way of allowing you to get results at whatever resolution you want/need.

Consider if the writer of this library forced the max x and y values to be 100, and required that you used ints. All of a sudden the noise is really cell noise on a 100x100 grid, since you could never read any intermediate values. Using floats allows the user to get results at whatever level of detail they require.

The fact that that this detail exists is inherent to perlin noise. See the second last point in the notes below (taken from the source):

   """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. 
    """
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top