Landscape and Foliage API – Map generation using Landscape and Foliage APIs

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.

Landscape and Foliage API – Map generation using Landscape and Foliage APIs

Getting ready

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.

How to do it…

  1. Enter a loop that attempts to place N trees, where N is the number of trees to place randomly, which is specified in the UPROPERTY() of the Chapter12GameMode object.
  2. Get random XY coordinates from within a 2D box bounding the landscape object.
  3. Get the Perlin noise value @ (x, y). You may use a different Perlin noise formulation than the one used to determine landscape heights for foliage placement.
  4. Generate a random number. If the number generated is within the range of units of the Perlin noise function there, then place a tree using the SpawnFoliageInstance function. Otherwise, do not place a tree there.

Tip

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.

How it works…

Perlin noise works by generating smooth noise. For each location in an interval, (say [-1, 1]), there is a smoothly varying Perlin noise value.

How it works…

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.

Tip

Perlin noise becomes periodic, that is, tileable, with the Perlin noise functions provided by the earlier linked implementation in this recipe. By default, Perlin noise is not periodic. If you need your Perlin noise to be periodic, be careful which library function you are calling.

The base Perlin noise function is a deterministic function that returns the same value every time you call it with the same value.

There's more…

You may also set up sliders inside your Chapter12GameMode object derivative to affect the foliage and landscape generation, including parameters such as the following:

  • Amplitude of the landscape
  • Density of the foliage
  • Isocontour level for foliage
  • Variance in foliage height or scale
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.151.21