How to Create a Foliage System in Unreal Engine 4
Some time ago, I realized that in most games, foliage does not react to player actions. I personally think this breaks you out of the emersion when you see foliage clipping through your body. I wanted to try to create some foliage system in Unreal Engine 4 [https://www.unrealengine.
Some time ago, I realized that in most games, foliage does not react to player actions. I personally think this breaks you out of the emersion when you see foliage clipping through your body. I wanted to try to create some foliage system in Unreal Engine 4 that fixes this issue.
When I released it, a lot of people asked how I did this/ if I wanted to create a tutorial about it. This will not be a node by node tutorial as this would take a long time to make. But more like a thought process how I went over this solution.
This is however by now means a fully-fledged solution. It could still need some more optimization and improvements on certain fields.
I started off by researching if this was already done in unreal. Most of the solutions were just a vertex shader based on distance fields. This does look good on small foliage types like grass, but it looks terrible on large foliage types.
The foliage painting tool in UE works with foliage instanced static meshes.
I scrolled further down on an unreal forum on foliage bending, and I found an answer by “Crocopede” that states that he knows how it’s done in the cry engine and that it could be replicated using physics using the Physics asset tool (PHAT).
I started off by rigging and skinning My foliage types that need to react. And put it into a blueprint in which simulate physics is turned on. I also needed to tweak the PHAT settings until it looked right.
Link to the Unreal forum:[LINK](https://forums.unrealengine.com/showthread.php?1795-Foliage-collision-foliage-bendon- collision-In-engine-feature)
There is one problem though, using this method, you are not able to use the foliage painting tool. Which means, updating a level is going to be a serious hassle. I looked further into how I could despawn and spawn these actors using the world transform date form each foliage component.
I stumbled upon another problem. The foliage painting tool in UE works with foliage instanced static meshes. If you want to get the data of individual instance, you’re in for a real hassle. I looked into overlap detection, but it only returns the actor, not a single component. Based on Jonas Molgaards tutorials I found out I could get the component out of a “break hit result” node after I raycast.
Spawning the Physics Actors
I used this info to get started, every tick, I do a “multi-sphere trace by channel” and break the hit result. From which I get the index number and hit component then I check if it is a instanced static mesh and get it’s transform to spawn the physics blueprint actor instead. Using the remove instance node the “the unrigged foliage” despawns.
At the moment I use a dirty way to check on different foliage types to replace them with the right bp actor. From a hit result, I get the name of the hit component and compare it to a string. the name within the foliage actor components are very unpractical as they always consist of “InstancedFoliageStaticMeshComponent_” + an index number corresponding the order they are in the Foliage painting tool. Eg. The first Component’s name is: “InstancedFoliageStaticMeshComponent_0”.
Replacing the actors back to static instances
Once the player gets further away from the foliage it needs to be put back into static meshes since no interaction is needed anymore. I decided to use the knowledge I got about instanced static meshes to solve this in a performant way.
Making a foliage manager was required if I wanted to respawn instanced static meshes. This is because the instance are child of one component thus generates just on draw call. In this manager, I added hierarchical instanced static meshes for each type of foliage I had. It also contained some functions to replace the foliage which then is called by each object that interacts with the foliage.
In each object, I keep an array that checks the distance between the foliage actors it spawns and itself. When it is further than a certain amount of units, I call the replace method from the foliage manager which spawns an instance on the right transform.
Based on the previously mentioned Unreal forum I added the vertex shader on top of the physically intractable foliage so it feels more like a leaf that actually bends under pressure. The distance fields do not generate from skeletal meshes so I added a cylinder to the player with a “invisible”
Explosions and trampling
Since the foliage that interacts with the player is a physics actor, I can easily add forces and impulses so if I want to add a grenade I need to sphere cast on the hit location, switch every hit component, add radial impulse and replace the actors again. This makes for a realistic behaviour on explosions.
The fire propagation system I implemented, is based on the paper of Jean-Francois Levesque. He explains how he did the fire propagation system from far cry 2.
Based on this knowledge I created a similar, slightly simplified system. I started off by creating a blueprint actor which contains a “Voxel” which contains parameters like “I’m on fire” which sets the voxel ablaze. Nex, I needed to find a way how to spread it, so I created another actor with an area of affect, which contains an array of the surrounding voxels. The latter actor is only spawned when the voxel is on fire.
When a voxel is on fire I do a raycast if there is a foliage actor overlapping in the voxel. If so, I replace it with a corresponding blueprint containing a destructible version and blend material, to simulate the foliage being destroyed by the flames. After a 10 sec delay the broken foliage despawns.