Writing Particle Shaders in Unity is quite simple. Some of the key elements to keep in mind are Transparency and Depth. A particle effect can be an expensive part of your scene, so it is important to optimize the shader and keep the effect as simple as possible. Below we will build a particle effect and explain the key pieces necessary to building that effect.
Writing Particle Shaders
It is essential that you allow Unity to sort your particles into the correct render queue and that it knows that the particle is a transparent effect. This will allow Unity to use Alpha Blending, which is an integral part of Transparency.
This Blend mode is considered the Additive Blend. It takes your geometry and adds it to the pixel value beneath it, based on the Alpha value of the Source Pixel.
This setting disables Backface Culling. This means that even if you are behind the object, it will still be visible. For performance reasons, many shaders default to "Culling" backfaces. We set this value to inform the GPU that it should always render this object.
A particle effect should not contribute to the depth of the scene if it is a transparent object. Because it is sorted in a later queue than regular Geometry, we do not want it to contribute to the scene depth, else it would draw over objects even though they are in front of it.
This allows us to disable writing to the depth buffer.
Particles effects are commonly projected onto the screen, based on the depth of the scene. In order to control the clipping of a particle when it hits an object, we use a concept called soft particle projection. This calls for a particle to sample the depth of the scene via the camera's depth texture. With this depth value, we can control the alpha value of the particle as it clips through a surface. This can provide a very nice effect, but can be very expensive. One would typically control this effect via a Unity Shader Keyword:
When this Keyword is enabled, the vertex and fragment programs calculate the particle alpha based on scene depth.