Wrapping 2D perlin noise

前端 未结 1 391
野性不改
野性不改 2021-02-04 12:06

I\'m working with Perlin Noise for a height map generation algorithm, I would like to make it wrap around edges so that it can been seen as continuous.. is there a simple way or

相关标签:
1条回答
  • 2021-02-04 12:48

    Perlin noise is obtained as the sum of waveforms. The waveforms are obtained by interpolating random values, and the higher octave waveforms have smaller scaling factors whereas the interpolated random values are nearer to each other. To make this wrap around, you just need to properly interpolate around the y- and x-axes in the usual toroidal fashion, i.e. if your X-axis spans from x_min to x_max, and the leftmost random point (which is being interpolated) is at x0 and the rightmost at x1 (x_min < x0 < x1 < x_max), the value for the interpolated pixels right to x1 and left from x0 are obtained by interpolating from x1 to x0 (wrapping around the edges).

    Here pseudocode for one of the octaves using linear interpolation. This assumes a 256 x 256 matrix where the Perlin noise grid size is a power of two pixels... just to make it readable. Imagine e.g. size==16:

     wrappable_perlin_octave(grid, size):
       for (x=0;x<256;x+=size):
         for (y=0;y<256;y+=size):
           grid[x][y] = random()
       for (x=0;x<256;x+=size):
         for (y=0;y<256;y+=size):
           if (x % size != 0 || y % size != 0): # interpolate
             ax = x - x % size
             bx = (ax + size) % 256 # wrap-around
             ay = y - y % size
             by = (ay + size) % 256 # wrap-around
             h = (x % size) / size # horizontal balance, floating-point calculation
             v = (y % size) / size # vertical balance, floating-point calculation
             grid[x][y] = grid[ax][ay] * (1-h) * (1-v) +
                          grid[bx][ay] * h * (1-v) +
                          grid[ax][by] * (1-h) * v +
                          grid[bx][by] * h * v
    
    0 讨论(0)
提交回复
热议问题