单工噪声功能:意外结果

时间:2013-10-09 01:40:58

标签: python noise

我正在尝试将此噪音模块应用于我正在进行的项目,但我没有得到我期望的结果: https://pypi.python.org/pypi/noise/

而不是varied height-map,每行往往具有完全相同的值。如果我创建250x250的东西,它将生成250次相同的值,然后生成250次新值,直到它结束。

这是我目前正在使用的功能。我很了解这个功能,但我不确定如何获得更多“有趣”的结果。谢谢您的帮助。

class SimplexNoise(BaseNoise):

    def noise2(self, x, y):
        """2D Perlin simplex noise. 

        Return a floating point value from -1 to 1 for the given x, y coordinate. 
        The same value is always returned for a given x, y pair unless the
        permutation table changes (see randomize above). 
        """
        # Skew input space to determine which simplex (triangle) we are in
        s = (x + y) * _F2
        i = floor(x + s)
        j = floor(y + s)
        t = (i + j) * _G2
        x0 = x - (i - t) # "Unskewed" distances from cell origin
        y0 = y - (j - t)

        if x0 > y0:
            i1 = 1; j1 = 0 # Lower triangle, XY order: (0,0)->(1,0)->(1,1)
        else:
            i1 = 0; j1 = 1 # Upper triangle, YX order: (0,0)->(0,1)->(1,1)

        x1 = x0 - i1 + _G2 # Offsets for middle corner in (x,y) unskewed coords
        y1 = y0 - j1 + _G2
        x2 = x0 + _G2 * 2.0 - 1.0 # Offsets for last corner in (x,y) unskewed coords
        y2 = y0 + _G2 * 2.0 - 1.0

        # Determine hashed gradient indices of the three simplex corners
        perm = BaseNoise.permutation      
        ii = int(i) % BaseNoise.period
        jj = int(j) % BaseNoise.period
        gi0 = perm[ii + perm[jj]] % 12
        gi1 = perm[ii + i1 + perm[jj + j1]] % 12
        gi2 = perm[ii + 1 + perm[jj + 1]] % 12

        # Calculate the contribution from the three corners
        tt = 0.5 - x0**2 - y0**2
        if tt > 0:
            g = _GRAD3[gi0]
            noise = tt**4 * (g[0] * x0 + g[1] * y0)
        else:
            noise = 0.0

        tt = 0.5 - x1**2 - y1**2
        if tt > 0:
            g = _GRAD3[gi1]
            noise += tt**4 * (g[0] * x1 + g[1] * y1)

        tt = 0.5 - x2**2 - y2**2
        if tt > 0:
            g = _GRAD3[gi2]
            noise += tt**4 * (g[0] * x2 + g[1] * y2)

        return noise * 70.0 # scale noise to [-1, 1]

win = pygcurse.PygcurseWindow(85, 70, 'Generate')

octaves = 2
ysize = 150
xsize = 150
freq = 32.0 * octaves

for y in range(ysize):
    for x in range(xsize):
        tile = SimplexNoise.noise2(x / freq, y / freq, octaves)
        win.write(str(tile) + "\n")

0 个答案:

没有答案