How to use Procedural Content Generation (PCG) in Unreal Engine

How to use Procedural Content Generation (PCG) in Unreal Engine

Set up parameters for how you want content generated and placed for any kind of content such as foliage, rocks, buildings, etc.

Plugins

  • Procedural Content Generation Framework (PCG)

Overview of Steps

  1. Add a PCG Volume to your level (this is where you allow PCG content to go)

  2. Create a PCG Graph (a special blueprint)

  3. Define the content to be generated.

Add PCG Volume

  1. Add "PCGVolume" actor from the Place Actors pane (search for it) or click the Quick Add button on the toolbar and go to All Classes > and search.

  2. The PCG Volume is a wireframe box where procedural content will go. It will be very small so resize it and place it on your level where you want the content to go.

Create the PCG Graph

  1. In your Content Browser, right-click > PCG > PCG Graph to add the PCG Graph file and rename it.

  2. Connect this new PCG Graph to your PCG Volume:

    1. Select PCGVolume in Outliner

    2. Go to Details Pane > PCGComponent > PCG > Graph Instance > Instance > Graph property

    3. Click the dropdown and search for your new PCG Graph.

  3. Open your PCG Graph and add a "Get Landscape Data" node to the graph.

    1. Note: This node gets the landscape surface data from within your PCG Volume.
  4. Drag the Out parameter and create a Surface Sampler node. (Get Landscape Data.Out should be connected to the Surface Sampler.Surface.

    1. Note: The Surface Sampler uses the landscape surface data to generate points where content can be spawned.

  5. Debug the Surface Sampler (press D while the node is selected or right-click the node and check the Debug option).

  6. Outliner > Select PCGVolume

  7. Details pane > PCG > Click the Generate button.

  8. In your viewport where your PCG Volume is, you should see a lot of little boxes spawned. These are the points that the Surface Sampler generated.

    1. Note: The color of the boxes (black, grays, white) represents the probability of density where black is least likely to be populated and white is most likely to be populated.
  9. From Surface Sampler > Out, drag and add a Static Mesh Spawner. This is where you tell it what content you want procedurally generated.

Define the content to be generated

  1. In the PCG Graph, select the Static Mesh Spawner.

  2. In the Details pane > Settings > Mesh Entities > click the + button. This is where you will add all the meshes you want placed.

  3. Expand Index [0] > Descriptor > and select the dropdown on Static Mesh to select the content you want to have generated.

  4. Repeat these steps to add more items.

  5. Tip: If you look at your viewport now, you should see your content being generated (coming out of the squares). To hide the squares, go back into your PCG Graph and stop debugging the Surface Sampler node.

Tips

You may want to adjust the density, scales (size), and rotation of your content to make it look more random or natural. Here are ways to do that.

Density

  • All Content: Go to your PCG Graph > Surface Sampler node > Details pane > Settings > Points Per Squared Meter and increase or decrease.

  • Single Item: Although not really "density", more like "occurrence rate" within the predetermined density, you can make some items appear more than other items:

    • Go to your PCG Graph > Static Mesh Spawner > Details pane > Mesh Entities > Expand the Index of the mesh you want to adjust the Weight property. Increasing will spawn more of this item than other items with lower numbers.

Rotation and Scaling

In your PCG Graph, you need to add a new "Transform Points" node between the Surface Sampler node and the Static Mesh Spawner:

  • Rotation: Transform Points node > Details Pane > expand the Rotation Max property > Add a Z value between 1 and 360.

  • Scaling: Transform Points node > Details Pane > Scale Min and Scale Max properties > Adjust the values to make the objects bigger or smaller.

    • Note: These properties have a lock on them so the scale uniformly.

Exclusion

  • There is a way to exclude spawn points on certain other objects, such as in water but I won't bet getting into that in this blog. If you would like to know more about how that works, you can find a YouTube Video here.

Unanswered Questions

There are still some things I don't know how to control, such as adjusting the tilt of a mesh on a mountainside. For example, a tree will stick out at a 90-degree angle from the mountainside. I don't know if there is an easy way to fix this.