Monday, 14 March 2016

Particles Tutorial Unity



Note: Advanced tutorial

Hello again. Today we are going to make this:


When I wanted to add nuclear explosion into Skyship Aurora i was thinking that i could find some really good examples on market or even free. I was surprised that i have not found anything that satisfied me. So I started experiments. First I tried to learn existent prefabs on market. It was fun because all existent nuclear explosion was not real world scaled, so I need to manually scale them for real world ( yes i know that there is available particle scaler script :) ). Second problem - they was extremely complicated, used a lot of sub particle emitters and invisible psys. So I just leave it and start from 0.

Ok by steps:
1. Separate complicated effect into smaller parts ( dust at the ground, stipe,  mushroom head )
2. Setup particles for each individual part ( circle psys for ground dust, simple up emitter for stipe and sphere emitter for head )
3. Have done effect when explosion is ready and mushroom head on the top.

 Tada.

 But this was the simplest part. The hardest part was explosion behaviour. I mean:

1. Big light
2. Dust on the ground
3. Fire ball rising up with a stipe
4. Fire ball turns into dust mushroom head

So I watched all the videos on you tube with nuclear explosions and also look in-game one like Call Of Duty Modern Warfare 4 ( my favourite ).

I start experiments and found out the the most simple way to make it - use animation and scripts, instead of hardcore Shuriken functionality.

1. Big light
It was easy - point light with well setuped lens flare that I took from Unity Free Lens Flares. And control it by animation, It is about 5 second duration of big light

2. Dust on the ground
 
Sounds easy but it was not in fact. I haven't found any way to make it with one psys so i made it with 2 Circle Edge emmiters with different start particle size. Also I setuped order layers to make it correct appearence and changed sorting mode to youngest first or oldest etc. Spawn most of them before big ligt over.

3. Fire ball rising up with a stipe.
Stipe - simple emmiter with up rising dust. Tune lifetime to contol height of the stipe.

Fireball is a sphere particle emmitter with random start color generation from red to yellow and small life time to make burning effect.

How to make it move together - make it via animation. Simply move fireball up according to stipe evolution

4. Fire ball turns into dust mushroom head.

The hardest thing of the effect.

Fireball was easy - sphere emmiter with random between two color generations adds burning effect well.

Dust of the mushroom head was a little bit harder -

 

As you can see moving of the head dust is quite interesting - starting at the top of the head move down via gravity and turn up later for different pressure reason. How to do it:

1. Make sphere emmitter and emmit from the edge with some start speed
2. Add gravity multiplier,
3. Add force over lifetime curve : adding positive Y( in the world ) force to the particle later

And of course I thought it be easy - I was wrong. What I think I will do - control start fireball random colors into sorts of grey. And what have I've got - I can't control random between two colors property with animation. 
Ok I thought lets do it via code, not again this properties is kinda private. Not a problem let's do it via reflection - no way. Common how you did it ? Editor have access, code no. The only way is to use serialized properties that part of the UnityEditor namespace that you can't use in stand alone builds

OK lets make some chunk.
Create separatly fireball and dust ball and somehow blend one into other. Use separate material and control alpha material colour through the script. Just increase dust alpha, and decrease fireball material alpha. When you will see only dest destroy fireball object.

Of course tuning of the effect took some time, 4 days in fact - blending time and colour transition.

5, Lighting of the particle

Lighting is our everything. When I made first prototype of the explosion I found lack of lighting and it was hard to fix it via standard resources i mean billboards can't be realistically lighted at all. So having experience in afterburn and FumeFx I tried to make everything with small spheres that have standard Unity 5 skyboxed lighted shader and fade out mode. Of course it give some 3D look and correct light but spheres looks to much like spheres and I make about 200 spheres and it was not enough, and by the way even with small spheres the looks not so good because just of local ligthing, no global. For global I need shadow casting that can be little bit too much for 200 spheres and such distance. So I leave that idea for a moment, but later I think I will back to it.

So what do we have - colour control to simulate lighting.
I mean after explosion rising up fire sphere create a lot of light that we need to simulate for making effect more real. It was easy I just control particle start colour of inner-outer dust circle to make them brighter or darker during life cycle.

Summary
This effect is kind of not physically but artist based controlled via animation and script. I thinks that it is possible to simulate such effect in much more physically base soft and use simulation data in game or render. With such realisation it can be even more realistic with couple of blast waves, that you can do with distortion shader here:
http://kostiantyn-dvornik.blogspot.com/2014/06/interesting-advanced-particle-tutorial.html

And add shock wave of dust after it. That's not so complicated so I leave it to you :)

Thanks for reading.