Search Unity

Question Tree generation with noise

Discussion in 'Scripting' started by r31o, Nov 11, 2021.

  1. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    Hi,
    I recently finished the tutorial series of Sebastian lague about creating procedural terrains. Now I want to do a tree generator script that, loops for all the vertices, cheks if the height is correct, if it is, determine if should be a tree over there using perlin noise for randomness (That is a requirment) and if should be, place a tree gameobject.
    The problem is that, even if I know how to do it, I dont know how to modify the noise so there are not ultra dense forests and then, nothing, I want randomly spread trees in groups of 1-4.
    Can anyone help me please?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    One handy way to do this sort of thing is to produce your own initial "maximum density forest."

    Lets say for argument sake that is one tree per square world unit (meter).

    A pair of for loops nested, one iterating X and one iterating along Z, would give you a perfect grid of highest-density trees at a given spacing.

    Adding random noise to laterally offset their positions within each square makes it look more like a real forest.

    Finally, to get smoothly-changing areas of density, instead of blindly putting down each tree, you would use its position in the maximum density grid to look up a noise (such as Perlin noise) and decide if the tree should be there.

    You can also layer Perlin noise together of different frequencies to get interesting results.
     
  3. ensiferum888

    ensiferum888

    Joined:
    May 11, 2013
    Posts:
    317
    You should look into Poisson Disc Sampling to generate a list of points that seem random but still follow a similar distribution pattern. The end result looks really natural. I followed this tutorial to implement: https://thecodingtrain.com/CodingChallenges/033-poisson-disc.html

    You could generate a black and white mask using Perlin noise, then overlay this on your terrain. Only allow points from your PDS process if they fall on a white area on your noise texture.

    Here you can see the trees generated in my game:



    Here it is with the perlin noise control map:
     
  4. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    Thanks for the reply!
    I currently working on a script to do it.
     
  5. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    I watched the page, but it is in Javascript.
     
  6. ensiferum888

    ensiferum888

    Joined:
    May 11, 2013
    Posts:
    317
    Yes but it's using primitive data types, nothing is JS specific. I used that page to write the equivalent C# you should try it it's a great exercise.