Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping 2D perlin noise

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 trick to do that? I guess I need something like a spherical noise so that either horizontally and vertically it wraps around. I would be happy also with just 1 wrapping axis but two would be better.

For now I'm using the classical algorithm in which you can set up how many octaves you want to add and which are the multipliers used for changing amplitude and frequency of the waves between every successive octave.

Thanks in advance!

like image 685
Jack Avatar asked Apr 07 '11 21:04

Jack


1 Answers

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
like image 70
Antti Huima Avatar answered Sep 29 '22 10:09

Antti Huima