Visual effect graph introduction Unity

Hello everyone, welcome back.

Today we will talk about Visual effects graph or VFX graph.

For this, I am using Unity version 2023.1 and I have a project setup in URP.

Now with that out of the way, let's get down to the business.

Particles?

Let’s start with what are particles?

“Particles are many small images or meshes, to represent visual effects that are typically difficult to portray with only single mesh or sprite”

Examples of visual effects where we can use particles are environmental effects like snowfall, rain, mist, fog, fire, explosion or gameplay effects like puff of confetti when user crosses the finish line.

And to work with particles Unity has 2 Methods, Particle system and Visual effects graph.

Particle system is older and is Unity’s default system, VFX graph is newer node base system.

Particle system vs VFX graph

So how do we know what system to use? General conception can be that VFX graph is newer so we should use VFX graph.

Not quite is this case! It is not like VFX graph has superseded the Particle system, they has some distinct differences, and we should choose one that fits our requirement the best.



Let’s talk about those differences.

  • 1st is the render pipeline support. Particle system is supported on every render pipeline. VFX graph is only supported on HDRP and URP, and can work with your custom scriptable render pipeline as well. Also keep in mind that, it is only production ready on HDRP, in URP it is still in preview phase.
  • 2nd difference is, Particle system is CPU bound, while VFX graph is GPU bound. Meaning Particle system will run on your processor and VFX graph will run on your graphics card.
  • That will bring us to our 3rd difference that, Particle system can support only hundred thousands of particles, while VFX graph can support millions of particles. Now of course these are arbitrary numbers and actually depends on your hardware, for example if you have AMD threadripper, you could get away with millions of particles even with particle system.
  • The 4th difference is, Particles can interact with physics of rest of the world or scene in Particle system, while VFX graph cannot. Again this is because Physics calculations are done on CPU and Particle system also run on CPU so particles can interact with underlying physics. Meanwhile VFX graph is pretty much GPU bound and our graphics card does not know what our Processor is cooking. Now that does not mean we can’t use our own magic trickeries to make it work but there is no built in solutions to do so.
  • 5th is, now take this one with a grain of salt because this is my own opinion. Particle system can be used for simple effects, while VFX graph can be used for more complex effects because, personally I could not replicate some of the effects with particle system but can do so with VFX graph.

Keep in mind though that I am not the VFX God so my opinion is pretty much biased and based on my own skill set. I am sure there will be someone out there who can make jaw dropping effects with particle system while I could not match them even with VFX graph.

So its not what tool you use, its about how you use it.

Also I said that VFX graph is for complex effects that does not mean you could not use it for simple effects.

So that’s the different points to help you identify what should you use.

My recommendation will be, if you are not working with built in render pipeline, and does not require physics collision, go for VFX graph, because it is good to off load as much work as we can on GPU for visual fidelity.

How to use VFX graph?

Okay now how do we use VFX graph. For that you must have project with HDRP or URP, then you go to Window > Package manager.

In Package manager, make sure you have selected “Unity registry” from the dropdown. Then scroll all the way down, find “Visual effect graph” and hit the Install button.



Now you can create VFX graph so,

Right click > create > Visual effects > and Visual effect graph. We will create rain effect for the demo so let’s name it RainVfx.

To use this asset, in the scene let’s add visual effect gameobject, so in hierarchy,

Right click > Visual effects > Visual effect. We will call it Rain.

It has a Visual effect component, we will drag our VFX graph in the Asset Template slot and we can see the default particles. To edit our effect, double click on our asset and we will be presented with VFX graph window.

Here we have our black board, our default particle system, our particle controls and all that cool stuff.

We can also name our system to stay organized, and these 4 nodes you are seeing are called “Context nodes” or “Contexts” and the blocks inside of them are called “Attributes blocks” or “blocks”.

We can also add various nodes outside of our system for example, Add node this node is called “Operator block” or “Operator”.



Context nodes

Now these 4 context nodes are the foundation of any visual effect.

  • Spawn context determines how many particles to spawn and how to spawn them, like spawn them continuously or in bursts.
  • Initialize context, think of it as the Start() of our monobehaviour where we initialize our particles, like setting the lifetime, set the initial velocity etc.
  • This Update context is similar to our Update() of the monobehaviour where we update our particles during their lifetime, like adding some turbulence, or adding other forces.
  • And the last Output context will determine how to display or render our particles, like we can set the texture, render them as sprite or meshes, set their orientation and so on.

Create rain effect

To create rain effect, first I am going to delete everything so you can very easily understand without getting overwhelmed.

Now we want to spawn the particles so let’s create a Spawn context, and we want to spawn them continuously so lets add Constant spawn rate block.

Now we can define how many particles we want to spawn each second, we would like to do that from inspector so let’s create a Float property.

Call it RainIntenstiy, give default value of 300, drag it in and feed in into the rate.



Now we want to initialize our particles, for that let’s create an Initialize particle context, then we will take spawn event output and feed it into Initialize particle context.

Here, this Capacity determines the maximum particles to render, set it to 5000.

Bound modes determines how do we like to setup our bound, Recorded will automatically take bounds from the attached target object, manual and automatic are pretty self explanatory.

These bounds are the culling area for our particles, any particles outside of this area will not be rendered to our camera also if our bounding box is not visible to camera, particles won’t be rendered.

Now I am going to set its size so that it covers my entire environment and we don’t want any padding.

Now to actually render the particles we need an Output context and we want to output them as quads so let’s create Output particle quad context.

We also need Update context, otherwise particles will not update.

Then we will take Initialize particle context output and feed it to Update context, then take its output and feed it to Output context. I will also give proper name to our system.



In Output context, for the main texture, I am going to use the default sparkle texture and set the blend mode to additive.



Now let’s add initial velocity to our particles, for that in the Initialize particle context let’s add Set velocity random block.

Here this per-component means different random velocities for each particles, while uniform means same random velocity for all particles. I will add some random velocity.

We need to set the lifetime of our particles for that let’s add Set lifetime block. Lifetime is the duration of our particle to stay alive in seconds, after that it will be destroyed. I will set it to 3 seconds so that our particles can fall below our plane before getting destroyed.

Now our particles are spawning from single point, let’s spread them out a bit, for that we will use Set posion block with Shape AABox, we will set the Position mode to Volume so our particle will spawn from any point within the box. I will set the size to cover the entire environment.



Now notice that our texture looks like droplet going upwords, so we will rotate them using Set angle block, let’s rotate them 180 degrees on Z axis.

Our droplets look too thick like snowflake so let’s set their scale using, you guessed it, Set Scale block, let’s squeeze them from X and Z axis.



Let’s add color to our particles, for that we will use Output context, let’s add Set color block. We will control it from the inspector so let’s create a color property, call it dropletColor, give default blueish color, drag it in and feed it into Set color block.

Lastly, if I view our particles from the sides, you realize that they are just 2D quads, so we will make them always face towards the camera. For that let’s add Orient block with mode Face camera position.



That's it, save our graph and we have simple rain effect.

And just like shader graphs, we can override properties in the inspector for customized look.

Trouble following along? Check out this video!



Thank you so much for reading!

Comments