Monday, March 07, 2011

I spent the morning toying with the python perlin noise library. It turns out that the perlin noise function doesn't produce random noise unless you instantiate a the SimplexNoise() class and call randomize.


Originally, I was using this:

def pn(m, octaves=1):
    from noise import pnoise2
    freq = 16.0 * octaves
    for y in range(len(m)):
        for x in range(len(m[0])):
            m[y][x] = int(pnoise2(x/freq, y/freq, octaves) * 127.0)
which constantly give us:

Now, I import the SimplexNoise class and randomize the permutation table

def pn3(m, octaves=1):
    from noise.perlin import SimplexNoise
    sn = SimplexNoise()
    sn.randomize()
    freq = 16.0 * octaves
    for y in range(len(m)):
        for x in range(len(m[0])):
            m[y][x] = int(sn.noise2(x/freq, y/freq) * 127.0)

and now I get random perlin noise:

I hope that helps anyone else struggling with the library.

No comments: