Search Unity

PerlinNoise - Tiles are repeating same noise

Discussion in 'Scripting' started by jchambers, Dec 23, 2020.

  1. jchambers

    jchambers

    Joined:
    Feb 4, 2013
    Posts:
    7
    Hi all.

    I am generating infinite tiles and am having trouble getting the PerlinNoise (height) to be applied to the world coords.The tiles repeat the same noise over and over.

    Not getting x/z offsets...

    The references I have gone over:
    Catlike coding
    Holistic3d
    Sebastian Lague
    https://kihontekina.dev/posts/perlin_noise_part_one/


    terrain-sample.jpg


    The tiles are 50 x 50 -- will always be increments of 10.
    worldX & worldZ are the world coords of each tile.
    noisePoints - number of vertices on one side (70 + 1)

    Code (CSharp):
    1.    
    2.      float[,] CreateNoiseMapV2(int noisePoints, int worldX, int worldZ)
    3.         {
    4.             float[,]  noiseMap     = new float[noisePoints, noisePoints];
    5.             Vector2[] octaveOffset = new Vector2[terrainSettings.octaves];
    6.  
    7.             System.Random prng           = new System.Random(Statics.gameSeed);
    8.             float         maxNoiseHeight = float.MinValue;
    9.             float         minNoiseHeight = float.MaxValue;
    10.             float         scale          = ((float) terrainSettings.perlinScale <= 0) ? 0.0001f : (float) terrainSettings.perlinScale;
    11.  
    12.             for (int i = 0; i < terrainSettings.octaves; i++)
    13.             {
    14.                 float offsetX = prng.Next(-100000, 100000) + worldX;
    15.                 float offsetY = prng.Next(-100000, 100000) - worldZ;
    16.                 octaveOffset[i] = new Vector2(offsetX, offsetY);
    17.             }
    18.  
    19.             for (int x = 0; x < noisePoints; x++)
    20.             {
    21.                 for (int z = 0; z < noisePoints; z++)
    22.                 {
    23.                     float amplitude   = 1f;
    24.                     float frequency   = 1f;
    25.                     float noiseHeight = 0f;
    26.  
    27.                     for (int i = 0; i < terrainSettings.octaves; i++)
    28.                     {
    29.                         float sampleX = (x + octaveOffset[i].x) / scale * frequency;
    30.                         float sampleZ = (z + octaveOffset[i].y) / scale * frequency;
    31.  
    32.                         float perlinValue = Mathf.PerlinNoise(sampleX, sampleZ);
    33.                         noiseHeight += perlinValue * amplitude;
    34.  
    35.                         amplitude *= terrainSettings.persistence;
    36.                         frequency *= terrainSettings.lacinarity;
    37.                     }
    38.  
    39.                     maxNoiseHeight = (noiseHeight > maxNoiseHeight) ? noiseHeight : maxNoiseHeight;
    40.                     minNoiseHeight = (noiseHeight < minNoiseHeight) ? noiseHeight : minNoiseHeight;
    41.  
    42.                     noiseMap[x, z] = noiseHeight;
    43.                 }
    44.             }
    45.            
    46.             // maxNoiseHeight -- To be used here
    47.  
    48.             return noiseMap;
    49.         }
    50.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,747
    Line 29 and 30... you need to add (for each Perlin input axis) the GameObject world offset, but scaled down to the local "Perlin coords."

    You probably need something in line 14 and 15 too since they are randomly choosing the octave offsets, which are included in lines 29 and 30 above... thinking about it, you probably would need to use the same octave settings for ALL tiles you want to line up.
     
  3. jchambers

    jchambers

    Joined:
    Feb 4, 2013
    Posts:
    7
    Thanks for the reply Kurt, that was enough to get me looking at it differently.

    The gameSeed keeps the perlin noise consistent
    Code (CSharp):
    1.  
    2. System.Random prng         = new System.Random(Statics.gameSeed);
    3.  

    The mapCenter is the offset controller for the noise.

    float offsetX = prng.Next(-100000, 100000) + mapCenter.x;
    float offsetZ = prng.Next(-100000, 100000) + mapCenter.y;


    After many more trials I am on the right track.

    terrain-sample-2.jpg

    I still need to work on mX and mZ as they do not work for if I change the vertPoints.
    But I can finally see the correlation so I will be able to find a solution that will work for all instances.
    The * 2 will need to be a variable I have not found yet.

    terrainSettings.meshConstant = 10;
    terrainSettings.meshScale = 5;
    vertPoints = 70;

    Code (CSharp):
    1.  
    2.           float meshMultiplier = terrainSettings.meshConstant * terrainSettings.meshScale;
    3.             float mX             = meshMultiplier / vertPoints * 2 * vertPoints;
    4.             mX *= meshMultiplier / terrainSettings.perlinScale ;
    5.             float mZ = meshMultiplier  / vertPoints * 2 * vertPoints;
    6.             mZ *= meshMultiplier / terrainSettings.perlinScale ;
    7.          
    8.             Vector2 mapCenter = new Vector2(x * mX, z * mZ);
    9.          
    10.             /* This calls CreateNoiseMapV2() */
    11.             CreateHeightMap(vertPoints + 1, mapCenter);
    12.  
    and a I updated the method/function.

    Code (CSharp):
    1.  
    2.         float[,] CreateNoiseMapV2(int noisePoints, Vector2 mapCenter)
    3.         {
    4.             System.Random prng         = new System.Random(Statics.gameSeed);
    5.             float[,]      noiseMap     = new float[noisePoints, noisePoints];
    6.             Vector2[]     octaveOffset = new Vector2[terrainSettings.octaves];
    7.  
    8.  
    9.             float maxNoiseHeight = float.MinValue;
    10.             float amplitude      = 1f;
    11.             float frequency      = 1f;
    12.             float scale          = ((float) terrainSettings.perlinScale <= 0) ? 0.0001f : (float) terrainSettings.perlinScale;
    13.             float width          = terrainSettings.meshConstant * terrainSettings.meshScale;
    14.             float halved         = noisePoints / 2f;
    15.  
    16.             Debug.Log(">>>>>>>>>>>>>>>>>>>>>> " + mapCenter.ToString());
    17.  
    18.             for (int i = 0; i < terrainSettings.octaves; i++)
    19.             {
    20.                 float offsetX = prng.Next(-100000, 100000) + mapCenter.x;
    21.                 float offsetZ = prng.Next(-100000, 100000) + mapCenter.y;
    22.                 octaveOffset[i] = new Vector2(offsetX / noisePoints, offsetZ / noisePoints);
    23.  
    24.                 maxNoiseHeight += amplitude;
    25.                 amplitude *= terrainSettings.persistence;
    26.             }
    27.  
    28.             for (int x = 0; x < noisePoints; x++)
    29.             {
    30.                 for (int z = 0; z < noisePoints; z++)
    31.                 {
    32.                     amplitude = 1f;
    33.                     frequency = 1f;
    34.                     float noiseHeight = 0f;
    35.  
    36.                     for (int i = 0; i < terrainSettings.octaves; i++)
    37.                     {
    38.                         float sampleX = (x - halved) / scale * frequency + octaveOffset[i].x * frequency;
    39.                         float sampleZ = (z - halved) / scale * frequency + octaveOffset[i].y * frequency;
    40.  
    41.                         float perlinValue = Mathf.PerlinNoise(sampleX, sampleZ) * 2 - 1;
    42.  
    43.                         noiseHeight += perlinValue * amplitude;
    44.                         amplitude *= terrainSettings.persistence;
    45.                         frequency *= terrainSettings.lacinarity;
    46.                     }
    47.  
    48.                     noiseMap[x, z] = noiseHeight;
    49.                 }
    50.             }
    51.  
    52.             return noiseMap;
    53.         }
    54.  
    55.  
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,747
    "I love it when a good procgen plan comes together."
     
    jchambers likes this.