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 have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Biomes not generating properly

Discussion in 'Scripting' started by Fluffysnails46, Dec 29, 2022.

  1. Fluffysnails46

    Fluffysnails46

    Joined:
    Jan 15, 2021
    Posts:
    52
    Recently, i was making biome generation for my game, and i encontered a problem: the code was generating multiple biomes as expected, but for some reason, the third biome wouldnt appear, until i adjusted the frequency, which is supposted to change the size of the biomes. When i adjusted it, the a different biome began to vanish. how would i go about fixing it?

    noise function:
    Code (CSharp):
    1.  public float PerlinNoise3DNotSymmetrical(float x, float y, float z)
    2. {
    3.      y += 1;
    4.      z += 2;
    5.     xy = _perlin3DFixed(x, y);
    6.     xz = _perlin3DFixed(x, z);
    7.     yz = _perlin3DFixed(y, z);
    8.     yx = _perlin3DFixed(y, x);
    9.     zx = _perlin3DFixed(z, x);
    10.     zy = _perlin3DFixed(z, y);
    11.      return xy * xz * yz * yx * zx * zy;
    12. }
    13. //stolen from source above
    14. static float _perlin3DFixed(float a, float b)
    15. {
    16.      return Mathf.Sin(Mathf.PI * Mathf.PerlinNoise(a, b));
    17. }
    biome function:
    Code (CSharp):
    1.  
    2.     int GetBiome(int x, int y, int z)
    3.     {
    4.         if(biomes.Length < 1)
    5.         {
    6.             return 0;
    7.         }
    8.         else
    9.         {
    10.             normal = (new Vector3(x-worldSizeX*.5f,y-worldSizeY*.5f,z-worldSizeZ*.5f)).normalized;
    11.             Vector2 biomeCoords = new Vector2(PerlinNoise3DNotSymmetrical((seed+832+normal.x)*biomeNoiseThreshold,(seed+832+normal.y)*biomeNoiseThreshold,(seed+832+normal.z)*biomeNoiseThreshold),PerlinNoise3DNotSymmetrical((seed+763)+normal.x*noiseThreshold,(seed+392)+normal.y*noiseThreshold,(seed+182)+normal.z*noiseThreshold));
    12.             float biomeDist = 10;
    13.             int biomeID = 0;
    14.             for(int i = 0; i < biomes.Length; i ++)
    15.             {
    16.                 if(Vector2.Distance(biomeCoords,biomes[i]) < biomeDist)
    17.                 {
    18.                     biomeDist = Vector2.Distance(biomeCoords,biomes[i]);
    19.                     biomeID = i;
    20.                 }
    21.             }
    22.             return biomeID;
    23.         }
    24.     }
     
  2. frenchynyc

    frenchynyc

    Joined:
    May 14, 2015
    Posts:
    28
    Hello,

    It is difficult to say what the issue is without more context and information about how you are using the GetBiome function and how the biomes array is set up.

    One possible issue could be that the biomeCoords vector that you are using to determine which biome is closest is not correctly calculated. Make sure that the values being passed to the PerlinNoise3DNotSymmetrical function are within the expected range (for example, if you are using values between 0 and 1 for the biomeNoiseThreshold and noiseThreshold variables, make sure that the normal vector values are also within that range).

    Additionally, you may want to consider adding some debug statements to see the values of the variables being used in the GetBiome function, such as the normal vector and the biomeCoords vector, to help you understand why certain biomes are not being selected as expected.

    Hope this helps :)
     
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    This doesn't make much sense:

    Code (CSharp):
    1. .normalized;
    The point of 3d perlin noise is to have a continuous output of a value based on an actual 3d position in space. When you normalize your position it means it's clamped onto the shell of a 3d sphere. So any position along a certain direction would always yield the same output. So you can only get values for the surface of that unitsphere and every position in space is mapped onto that unit sphere.
     
  4. Fluffysnails46

    Fluffysnails46

    Joined:
    Jan 15, 2021
    Posts:
    52
    it is normalized because i am generating procedural planets, and to add noise values i use
    Code (CSharp):
    1. .normalized
    i probably dont need it there though
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Oh... my... goodness...

    NOBODY should be forced to deal with code like that.

    If you have more than one or two dots (.) in a single statement, you're just being mean to yourself.

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    Break it up, practice social distancing in your code, one thing per line please.

    "Programming is hard enough without making it harder for ourselves." - angrypenguin on Unity3D forums

    "Combining a bunch of stuff into one line always feels satisfying, but it's always a PITA to debug." - StarManta on the Unity3D forums