Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice
  4. Join us on Thursday, September 29, for a day with Unity's SRP teams here on the forum or on Reddit, and discuss topics around URP, HDRP, and the Scriptable Render Pipeline in general.
    Dismiss Notice

Question Streaks Across My Procedurally Generated Terrain

Discussion in 'Scripting' started by PaperMouseGames, Sep 23, 2022 at 2:02 PM.

  1. PaperMouseGames

    PaperMouseGames

    Joined:
    Jul 31, 2018
    Posts:
    410
    So I'm generating a height map via code with Perlin Noise and the applying it to terrains, but I'm getting these slightly streaks across the terrain.

    terrain streaks.png

    I don't mind posting my code up if anyone wants to see it, but it's a lot and I'm just not sure where this might be taking place so I was hoping someone might have seen something like this before and have some advice on where to look.

    Like I said; I'm generating the terrain by first creating a perlin noise map into a 2D array. Then I'm applying those heights using the terrainData.SetHeights method.

    I'm also generating the noise map on a plane and the streaks seem to be missing there, so it's something that's happening somewhere when I'm setting the heights to the mesh.
     
  2. TheFunnySide

    TheFunnySide

    Joined:
    Nov 17, 2018
    Posts:
    182
    Can you write the height map into an image and check if the streaks are already present?
     
  3. PaperMouseGames

    PaperMouseGames

    Joined:
    Jul 31, 2018
    Posts:
    410
    The texture looks normal, so the streaks seem to be happening during the terrain shaping.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    28,159
    Do a simple cheesy 2x2 gaussian blur on the heightmap data before writing it... does the banding go away?
     
  5. PaperMouseGames

    PaperMouseGames

    Joined:
    Jul 31, 2018
    Posts:
    410
    How would I do that? Using a shader? Or is there a way to blur/blend the actual height map values?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    28,159
    Just loop in 2 dimensions and do it... it's just test code you're going to throw away.

    Code (csharp):
    1. for (int y = 0; y < height - 1; y++)
    2. {
    3.   for (int x = 0; x < width - 1; x++)
    4.   {
    5.      float total = getCell( x, y) +
    6.                   getCell( x + 1, y) +
    7.                   getCell( x + 1, y + 1) +
    8.                   getCell( x, y + 1);
    9.      total /= 4;
    10.      // todo: write this total to a fresh array (NOT THE SAME ARRAY!)
    11.   }
    12. }
    (Obviously the "getCell()" method is based on however you're storing this 2d float array.)
     
    PaperMouseGames likes this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    28,159
    The only point of trying this is to isolate where the regular ridged noise is coming from: is it a terrain bug or does your data actually contain this noise?
     
  8. PaperMouseGames

    PaperMouseGames

    Joined:
    Jul 31, 2018
    Posts:
    410
    So it looks like I'm still getting the streaks on the terrain. Here is a look at what I did:

    Code (CSharp):
    1. int width = 1 + terrainSO.AlphaMapResolution * 2 * worldLength;
    2.         int height = 1 + terrainSO.AlphaMapResolution * 2 * worldLength;
    3.         float[,] noiseMap = NewNoiseMap(width, height, seed, noiseScale,
    4.             octaves, persistance, lacunarity, offset);
    5.         DrawMesh(noiseMap, meshHeightMultiplier, meshHeightCurve);
    6.  
    7.         float[,] newArray = new float[noiseMap.GetLength(0) - 1, noiseMap.GetLength(1) - 1];
    8.  
    9.         for (int x = 0; x < noiseMap.GetLength(0) - 1; x++)
    10.         {
    11.             for (int y = 0; y < noiseMap.GetLength(1) - 1; y++)
    12.             {
    13.                 float total = noiseMap[x, y] +
    14.                     noiseMap[x + 1, y] +
    15.                     noiseMap[x + 1, y + 1] +
    16.                     noiseMap[x, y + 1];
    17.  
    18.                 total /= 4;
    19.                 newArray[x, y] = total;
    20.             }
    21.         }
    22.  
    23.         for (int x = 0; x < noiseMap.GetLength(0) - 1; x++)
    24.         {
    25.             for (int y = 0; y < noiseMap.GetLength(1) - 1; y++)
    26.             {
    27.                 noiseMap[x, y] = newArray[x, y];
    28.             }
    29.         }
    This is the code leading up to actually setting the heights of the terrain, where I'm creating the noiseMap array.

    And here is the code setting the heights:

    Code (CSharp):
    1. for (int x = 0; x < terrainSO.Terrains.GetLength(0); x++)
    2.         {
    3.             for (int z = 0; z < terrainSO.Terrains.GetLength(1); z++)
    4.             {
    5.                 int xStart = terrainSO.AlphaMapResolution * 2 * x;
    6.                 int xEnd = 1 + terrainSO.AlphaMapResolution * 2 * (x + 1);
    7.                 int zStart = terrainSO.AlphaMapResolution * 2 * z;
    8.                 int zEnd = 1 + terrainSO.AlphaMapResolution * 2 * (z + 1);
    9.                 terrainSO.Terrains[x, z].terrainData.SetHeights(0, 0, NoiseMapFragment(noiseMap, worldLength,
    10.                     xStart, xEnd, zStart, zEnd));
    11.             }
    12.         }
    There are a few methods there I'm not showing, let me know if it would be helpful to see any of them and I can post them. Really stumped with this!
     
  9. REDACT3D_

    REDACT3D_

    Joined:
    Nov 8, 2020
    Posts:
    111
    To my eyes it almost looks like the distortion texture is squished. As in the X Y scale values are not the same to make a square texture.

    what if you apply the same distortion to an imported subdivided grid plane?
    could rule-out anything to do with terrane
     
  10. PaperMouseGames

    PaperMouseGames

    Joined:
    Jul 31, 2018
    Posts:
    410
    Sorry not sure I'm following you. Not sure why X and Y wouldn't be the same. I'll see if I can try the grid plane idea though
     
unityunity