title: Tiling perlin noise seamlessly
body: <p>I have been trying to figure out how to create infinite terrain in 3D using perlin noise. I have so far got a terrain that is made of chunks, and when the player moves out of a chunk, new chunks are generated. Chunks out of range are unloaded. So I have the illusion of an infinite world.</p>
  
  <p>I am using perlin noise to create height maps for the chunks (each chunk has its own height map)</p>
  
  <p>My question is how do I seamlessly tile the height map of each chunk so that there aren't horrible gaps in the world between chunks, and without having the same height map for every chunk.</p>
  
  <p>By "gaps in the world" I mean <a href="https://i.imgur.com/UtpaD9H.png" rel="nofollow noreferrer">this</a>.</p>
  
  <p>And I followed <a href="http://devmag.org.za/2009/04/25/perlin-noise/" rel="nofollow noreferrer">this</a> to implement perlin noise</p>
  
  <p>Here is my perlin noise code:</p>
  
  <pre><code>private float[][] perlinNoise(int width, int height, int octave, float[][] whiteNoise)
  {
      float[][] result = new float[width][height];
  
      int samplePeriod = 1 &lt;&lt; octave;
      float sampleFrequency = 1.0f / samplePeriod;
  
      for (int i = 0; i &lt; width; i++)
      {
          int x1 = (i / samplePeriod) * samplePeriod;
          int x2 = (x1 + samplePeriod) % width;
          float xBlend = (i - x1) * sampleFrequency;
  
          for (int j = 0; j &lt; height; j++)
          {
              int y1 = (j / samplePeriod) * samplePeriod;
              int y2 = (y1 + samplePeriod) % height;
              float yBlend = (j - y1) * sampleFrequency;
  
              float top = (float) MathHelper.interpolateLinear(whiteNoise[x1][y1], whiteNoise[x2][y1], xBlend);
  
              float bottom = (float) MathHelper.interpolateLinear(whiteNoise[x1][y2], whiteNoise[x2][y2], xBlend);
  
              result[i][j] = (float) MathHelper.interpolateLinear(top, bottom, yBlend);
          }
      }
      return result;
  }
  
  public float[][] generatePerlinNoise(int width, int height, Random random, int octaveCount)
  {
      float[][] whiteNoise = new float[width][height];
      float[][][] totalNoise = new float[octaveCount][][];
      float[][] perlinNoise = new float[width][height];
      float amplitude = 1.0f;
      float totalAmplitude = 0.0f;
      float persistance = 0.5f;
  
      for (int i = 0; i &lt; width; i++)
      {
          for (int j = 0; j &lt; height; j++)
          {
              whiteNoise[i][j] = random.nextFloat() % 1;
          }
      }
      for (int i = 0; i &lt; octaveCount; i++)
      {
          totalNoise[i] = perlinNoise(width, height, i, whiteNoise);
      }
      for (int o = octaveCount - 1; o &gt;= 0; o--)
      {
          amplitude *= persistance;
          totalAmplitude += amplitude;
  
          for (int i = 0; i &lt; width; i++)
          {
              for (int j = 0; j &lt; height; j++)
              {
                  perlinNoise[i][j] += totalNoise[o][i][j] * amplitude;
              }
          }
      }
      for (int i = 0; i &lt; width; i++)
      {
          for (int j = 0; j &lt; height; j++)
          {
              perlinNoise[i][j] /= totalAmplitude;
          }
      }
      return perlinNoise;
  }
  </code></pre>
  
  <p>Edit: I have just asked about <a href="https://gamedev.stackexchange.com/questions/104342/tiling-perlin-noise-seamlessly-but-without-repeating-to-generate-an-infinite-3d">this on GameDev</a> my question there might be more detailed and helpful. <strong>Please try and answer there if possible</strong>, because I will be looking at this post less and that one more.</p>
  
  <p>Edit: I have realised that my noise code is <strong>NOT PERLIN NOISE</strong>. It is actually something called <strong>VALUE NOISE</strong>, which actually doesnt look as good, and wont work for what I need anyway. I cant find any good java implementation of perlin noise. And I dont just want a link to code I can use, I would like to have a tutorial, where I can understand how the algorithm actually works.</p>