skip to Main Content

Creating Procedural Meshes

In this post we’re going to see how to create a simple procedural mesh during runtime in Unreal Engine 4.

While there are several components and plugins (most notably RuntimeMeshComponent by Koderz) that you can use in order to create procedural meshes in UE4, I’m going to use the official Procedural Component that is provided with the engine.

Adding the required Dependencies

For this post I have created a C++ project using the 4.18.3 version of the engine. Then, I added the ProceduralMeshComponent as a dependency in the public module names in the <MyProject>.Build.cs file:

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "ProceduralMeshComponent" });

Note that when you’ve added the dependency, you have to regenerate your project’s visual studio files via the .uproject file, otherwise when you attempt to access the necessary header files your project won’t be able to compile.

Creating the procedural mesh

The procedural mesh we’re going to create in this post is a simple cube. Create a new C++ class that inherits the actor class and declare the following properties on its header file:

Moreover, make sure to include the “ProceduralMeshComponent.h” file in your includes.

In order to create a mesh we need to provide a list of vertices and triangles to the Procedural Mesh Component so the engine can draw our geometry in the Viewport. Since we’re going to create a cube we need 8 vertices (one for each corner of the cube) and 12 triangles (2 for each face of the cube). Moreover, we’re going to provide a color for each vertex and colorize the whole cube later on. So here’s the whole code needed to draw a 100cm sized cube:

The AddTriangle function creates a triangle between the given vertices. So for example, when I add the vertices 0,2 and 3 the component will create a triangle connecting these three vertices (in this case the lower left, lower right and upper right corners of the cube). At this point, it is important to note that the way you’re adding your vertices in the triangles array matters. When you add your vertices in a counter-clockwise manner the engine will make sure that your triangle will face outwards instead of inwards. To display this, take a look at what happens if on the above code segment in line 45 we add the vertices 3,2,0 instead of 0,2,3:

(Click on image to enlarge in a new tab)

For more information regarding the facing of your triangles check out this post on UE4 answerhub.

Adding colors on the generated mesh

At this point, you’re going to have a default colored mesh instead of the colors shown above. To colorize your mesh, create the following material and assign it in your CustomMesh component in the begin play function:

Note that the BP_ProcMeshActor is a Blueprint based on the C++ class we’ve created above.

Animating our mesh

Let’s say that we want to move our mesh forward. To do that we have to modify the location of each vertex and then tell our component to update the section of the mesh we modified. To move our cube 1 unit each frame, type in the following code on your Tick function:

Here’s a video displaying the final result of the post:

Avatar photo

This Post Has 6 Comments

  1. i m using 4.7.6 n got compile err could not find ProceduralMeshComponent in the dependency cs please advise

  2. When I create the BP_ProcMeshActor I do not have the inherited CustomMesh component? Should it appear there by default?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top