08 Jan Custom Blur Shader for Unity3D
During the development of the HOTAH game under the DADIU initiative, I found myself flirting with Unity shader programming. More specifically, the art direction suggested we should have some nifty looking water which reminds of the milky water in Limbo. Limbo being a pure 2D game and us working in 2.5D, I got away with developing a custom blur shader.
While Unity 3D offers a lot of professional shaders out of the box, it only offers a blur shader as a post-processing effect, meaning that the blur is only applied to the whole scene rendered by the camera. As we needed selective blur, I developed a shader that renders what is behind the object it is applied to and then applies a Gaussian blur to the grabbed texture.
Of course, you can read all the math behind it on Wikipedia (don’t forget to check notes and references as well), especially if you need to calculate the weights for the filter. Just to get you in the mood, you can visualize how the blurring happens using the image below.
While the blurring could be done in only one pass, it is computationally safer to do it in two passes: one for the vertical blurring and one for the horizontal one. For example, since I am using a mask size of 5 , the cost for two passes is only 11+11 = 22 texture samples, while it would take 11*11=121 texture samples for a single pass.
You can see a demonstration of the shader below. It works best with values between 0 and 2. It starts showing huge offset for higher values and I’m still trying to find a suitable solution for that (suggestions are welcome) .
Where to get the blur shader
As of yesterday, I have made the shader available on the Unity store for free, and you can access it through this link. I am also open to suggestions in making my shader better, as there’s always room for improvement.