How to implement particles using ECS

I just have a simple question about implementing particles using ECS. For example, I want to have a fire effect when an entity catches fire, but my current understanding is that I would Instantiate a non-entity GameObject prefab of the particles for each entity on fire. In addition to that I would need to constantly sync positions between each entity and their fire particle GameObject. Then when the fire effect has ended on the entity, I need to stop the particle system on the GameObject and Destroy() it after a few seconds after all particles fade out.

The issue I am facing is with syncing positions. Instantiating VFX for each entity is easy but how should I keep track of its associated entity to sync positions? I was thinking I could store a dictionary of the entity and VFX transform, but it hardly seems efficient if I had hundreds of entities needing to catch fire. I could optimize the Instantiation using an object pooler but having to store a dictionary to lookup each position for each entity every frame doesn’t seem performant.

I guess what I am asking is if there is a smarter way to sync positions from entities to GameObjects? Alternatively if there is a pure ECS way to do particle effects I am all ears.

1 Like

This is how Unity syncs companion game objects. It uses TransformAccessArray to jobify transform access. Pretty efficient, not very convenient.

Also note that the PaticleSystem/VisualEffect components technically support baking using this whole companion object system thing, so maybe you can use it out of the box.

1 Like

Purest way with entities is either use your pure entities data with custom shader simulating some logic (like fire, trails, water splash etc.) most efficient one but obviously has multiple limitations for complex VFX, require to write shader for most custom and complex effects based on vertex colors or additional data.

Another way (well techincally very similar to first one in sense of full GPU simulation but much more flexible and also very performant) is to use Unity VFX Graph and populate graphic buffers with data and/or trigger VFX Graph events.
You can look at

Specifically at ECSGalaxySample/Assets/Scripts/VFXSystem.cs at main · Unity-Technologies/ECSGalaxySample · GitHub
In this file you’ll see whole basic setup and how to work with that. In this Unity example they use 1 graph per effect, they’re using straight Set approaches to buffers. But after you get basics you can expang that approach to have just one graph for all effects and fully control it’s behaviour from buffers with bursted multithreaded population with lock/unlock approach.

Wow! Very cool sample! I’ll check it out thanks!