Optimize performance when replacing Foliage Instanced Meshes during runtime?

I recently implemented a blueprint system for replace instanced foliage with a physical skeletal mesh when the player is near, and then swaps back to the instanced foliage once the player moves away. It was thanks to a series from Jonas Molgaard.

Basically, there’s a collision sphere that moves with the player and whenever the foliage is overlapped by the sphere, it is replaced with a physics asset so it interacts with the player, and then it reverts back after the player moves away.

However, I’m running into problems with performance. Everything behaves and works well up until the foliage surpasses a certain amount of instances. It seems to be getting CPU bottle necked when it is replacing even just 1 instance of foliage. I’m assuming this is because its querying the entire foliage actor and checking all of them, so the larger the number of instances the more it queries. This recording shows the drop in performance when the instance count gets higher. (keep in mind the number of physical skeletal meshes is the same, just more foliage instances)

I’ve also got a pastebin to show the blueprint that is running the replacement code:

Does anyone know of a more optimized method for looking up and replacing foliage instanced meshes? Or any other suggestions for how to optimize this.

Hello IamBramer,

I don’t know of a way to optimize your current situation with skeletal mesh swapping, but there are several tutorials for interactive foliage (for example this one: UE4 - Tutorial - Interactive Foliage! (Update in Description) - YouTube) that use only materials.

It depends on how easy it will be for you to change your current system (while keeping the same effect), but it would definitely be far better for performance.
Even if you didn’t have to worry about replacing meshes, it is often better to use materials and world position offset than skeletal animations when you can.

Again, it may not work perfectly, but if you get the chance you should definitely try out something like that tutorial (or just search for UE4 Interactive Foliage).
Even if the materials path doesn’t work, hopefully you’ll find some way to optimize it.

Thanks for the suggestion AwesomeCadecraft!

I’ve actually got a PivotPainter material already setup that can bend the stalks when the player or creature walks by them. However, this is for a horror game and the cornstalks are meant to have a HUGE presence on the way the player interacts with the creature chasing them, and it doesn’t have the same effect without having the trailing physics after a body passes through them, as the “push everything in an outward circle only when the body is here” material approach has doesn’t really lend to have as much impact as desired.

Thanks again though!