Unreal Engine*: Blueprint CPU Optimizations for Cloth Simulations

ID 672935
Updated 3/5/2018
Version Latest



The contents of this guide work for version 4.19 of the Unreal Engine.

Cloth Simulations

Realistic cloth movement can bring a great amount of visual immersion into a game. Using PhysX Clothing is one way to do this without the need of hand animating. Incorporating these simulations into Unreal Engine* 4 is easy, but, as it is a taxing process on the CPU, it’s good to understand their performance characteristics and how to optimize them.

Disabling cloth simulation

Cloth simulations in Unreal* are in the level they will be simulated, whether they can be seen or not. Optimization can prevent this risk. Do not rely on the Disable Cloth setting for optimizing simulated cloth, as this only works in the construction, and has no effect while the game is in play.

Unreal* Physics Stats

To get a better understanding of cloth simulation and its effect on a game and system, we can use a console command, Stat PHYSICS, in Unreal.

After entering Stat PHYSICS at the command line, the physics table overlay appears (Figure 1). To remove it, just enter the same command into the console.

Physics overlay table
Figure 1. Physics overlay table.

While there is a lot of information available, we need only worry about the first two (Cloth Total and Cloth Sim) for the purposes of this paper.

Cloth Total represents the total number of cloth draws within the scene, and Cloth Sim (simulation) represents the number of active cloth meshes currently simulated. Keeping these two numbers within a reasonable level to your target platform helps prevent a loss of frame rate due to the CPU being loaded down with processing cloth. By adding an increasing number of cloth meshes to the level, the number of simulations the CPU can handle at once becomes apparent.

Suspending Cloth Simulation

Added to the Unreal Engine Blueprint system is the ability to suspend and resume the cloth simulations on a skeletal mesh. These added nodes solve the previous issue of the cloth simulation being reset every time with the level of detail method of cloth optimization.

Rendered function switch
Figure 2. Resume and suspend on a was recently rendered function switch.

For the purpose of this document all of the methods discussed below in the Level of Detail section still apply, but now you can exchange the Set Min LOD nodes with the Resume and Suspend Clothing Simulation nodes.

Time delay switch

With cloth simulation suspension, we are able to be more dynamic with cloth while still being able to optimize performance. However, using only an occlusion switch can lead to a dropping banner problem; wherein a cloth simulation has dynamic movement, and the player turns away (which pauses the cloth simulation), and then after some time turns back to see the cloth simulation hovering in mid-air before continuing its movement.

To solve this issue, we can use an occlusion switch and add a Boolean check to our suspension; in this way a delay before suspending the simulation can be used, giving the cloth enough time to finish its movement before coming to a final rest and remaining suspended.

Time delay switch
Figure 3. Time delay switch.

Level of Detail

When creating a skeletal mesh and attaching an apex cloth file to it, that cloth simulation will always be tied to the zero value of the level of detail (LOD) of that mesh. If the mesh is ever switched off of LOD 0, the cloth simulation will no longer take place. Using this to our advantage, we can create an LOD 1 that is the same in every way as our LOD 0 (minus the cloth apex file), and use it as a switch whenever we want to use the cloth simulation (Figure 4).

Level of detail information
Figure 4. Level of detail information.

Boolean switch

Now that we have a switch we can set up a simple blueprint to control it. By creating an event (or function), we can branch using a Boolean switch between simulating the cloth (LOD 0) and not simulating the cloth (LOD 1). This event could be called on a trigger entered to begin simulating the cloth meshes in the next area, and again when the player leaves that area to stop those simulations or any number of methods, depending on the game level.

Occlusion Culling Switch
Figure 5. Switch blueprint.

Occlusion culling switch

If a more automated approach is desired, occlusion culling can be used as the switching variable. To do this, call the Was Recently Rendered function, and attach its return to the switch branch (Figure 6). This will stop the cloth simulation when the actor is no longer rendered.

Recently Rendered function in the switch blueprint.
Figure 6. The was recently rendered function in the switch blueprint.

The problem with this method comes from the simulation reset that occurs when the simulation is switched back on. If the cloth mesh is drastically different when it is simulated, the player will always see this transition. To mitigate the chance of this happening, the bounds of the mesh can be increased with import settings. However, this also means intentionally rendering objects that cannot be seen by the player, so make sure it is worthwhile in terms of the game’s rendering demands.

A level design approach to solving this issue would include making sure all dynamically capable cloth meshes (such as flags) are placed in the same direction as the wind.

It may be possible to program a method in C++ that will save the position data of every vertex of the cloth simulation and translate the mesh back into that position when the simulation in turned back on. That could be a very taxing method, depending on the data structure used and the amount of cloth simulations in the level.

Cloth simulations without occlusion culling switch
Figure 7. Cloth simulations without occlusion culling switch.

Cloth simulations with occlusion culling switch.
Figure 8. Cloth simulations with occlusion culling switch.

Combination/Set Piece Switch

If the level happens to have a very dynamic set piece that is important enough to always look its best, an additional branch that uses a Boolean switch can be attached to the actor; in figure 9 we call it Optimize Cloth?

Set piece switch.
Figure 9. Set piece switch.

With this new switch, importance can be given to certain cloth meshes that should always be simulated by switching their Optimize Cloth? value to false.

Using a set piece switch

In figure 10 below, three cloth meshes are flags that turn away and point backwards, relative to their starting position. It takes a few seconds for this to look natural, but because they really sell the fact that they are not hand animated, I set them to be set pieces (Optimize Cloth? false), so they are always being simulated.

Complex flags used with set piece switches.
Figure 10. Complex flags used with set piece switches.