We can use the earlier mentioned landscape generation code to create a landscape, and the procedural foliage functionality to randomly distribute some foliage on it.
Combining the capabilities of the Landscape API and Foliage API will allow you to procedurally generate complete maps. In this recipe, we will outline how this is done.
We will programmatically create a landscape and populate it with foliage using code.
To prepare to perform this recipe, we will need a UE4 project with a Generate
button to kick off generation. You can see the Landscape API – Landscape generation with Perlin noise recipe for an example of how to do this. You simply need to create a small UMG UI widget that has a Generate
button. Connect the OnClick
event of your Generate
button to a C++ UFUNCTION()
inside any C++ global object, such as your Chapter12GameMode
object, that will be used to generate your terrain.
UPROPERTY()
of the Chapter12GameMode
object.@ (x, y)
. You may use a different Perlin noise formulation than the one used to determine landscape heights for foliage placement.SpawnFoliageInstance
function. Otherwise, do not place a tree there.You should note that we are covering randomness in location using the underlying randomness in the spot we choose to test for tree placement. The actual chance to place a tree there depends on the Perlin noise value there, and whether it is within the range of units of PerlinTreeValue
.
Very dense tree distributions will look like isocontours on the map then. The width of the isocontours is the range of units.
Perlin noise works by generating smooth noise. For each location in an interval, (say [-1, 1]), there is a smoothly varying Perlin noise value.
Perlin noise values are sampled on a 2D texture. At each pixel (and even in between), we can get a very smoothly varying noise value.
Adding octaves (or integer multiples) to some variable that travels in distance across the Perlin noise function allows us to get jaggy-looking effects; for example, the tufts in clouds or crags in mountains are gotten by wider-spaced samples, which give faster varying noise.
To get cool-looking Perlin noise outputs, we will simply apply math functions to sampled Perlin noise values; for example, the sin and cos functions can generate some cool looking marble effects for you.
The base Perlin noise function is a deterministic function that returns the same value every time you call it with the same value.
3.15.151.21