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.
LukasPosted at 19:40h, 12 January
Hello, it doesn’t work in unity 4.0 ?
Ana TodorPosted at 17:49h, 26 January
Hi, what error do you get?
RogPosted at 15:15h, 06 March
Thanks very much for this, the usage via a plane works brilliantly for layered render textures.
Horizontal-only, or vertical-only versions could be interesting, especially animated (via iTween or otherwise). I may play around with that. =)
And yes it’s working well for me (Unity 4 Pro).
Ana TodorPosted at 16:41h, 07 March
Hey Rog, feel free to go wild. That’s why I put it out for the masses ;).
And thanks or testing it out in Unity 4.
Ana TodorPosted at 16:42h, 07 March
Also yay, my free blur shader has been rated with 4 stars on the Unity Asset store! Good enough for my first submission :P.
HridayPosted at 11:11h, 30 July
Which one is that? I didn’t see it there.
Philippe BlanchardPosted at 22:52h, 15 March
Hi, this is super useful and interesting but for some reason when I use it the blurred image is flipped in y, I must be doing something wrong.
Ana TodorPosted at 00:10h, 18 March
Hi Phillipe, you’re not doing anything wrong. I think that relates to the rendering path configured with Unity. Try going to Player Settings > Other Settings>Rendering Path and play with that around. If that doesn’t work, I think you can modify the code to do an image flip. If I remember well, I might have done that myself and you simply need to remove the code where I invert the image.
DavidPosted at 16:40h, 10 May
the shader has an error “Program ‘vert’, incorrect number of arguments to numeric-type constructor (compiling for d3d11) at line 25. Same for d3d11_9x.
The sample scene shows the pink default shader. Using Unity 4.1.1f4
Ana TodorPosted at 20:40h, 15 May
Sorry for the late answer, I maintain this blog and shader in my free time and I haven’t had much lately :D. The error you get is actually a warning which doesn’t affect the rendering of the shader at all.
Anyway to correct that please open the shader and modify this line:
half4 sum = half4(0.0);
half4 sum = half4(0.0,0.0,0.0,0.0);
I’m going to submit this fix to the asset store as well soon, but it doesn’t hurt having it here for reference as well.
The reason for which you are seeing the default pink shader is because my shader uses the GrabPass for taking the image behind it. GrabPass internally uses RenderTextures, which are a Unity Pro feature. So unless you have Unity Pro, I’m afraid you will not be able to use this shader. I’ll try to look into a workaround for this, but I’m pretty sure there is none.
I think Pro features are poorly signalled by the editor. It would be nice if Unity threw up explicit warnings when RenderTextures aren’t supported by the current license. I wrote the shader myself when i had the trial version of unity pro running and when only observed this when my license expired.
DavidPosted at 22:25h, 15 May
thanks for the info! I had hoped this was a possibility of getting a good blur shader without the pro version but sadly that doesn’t seem to be possible.
You should make a note in the asset store saying that Unity Pro is needed.
pengbitaoPosted at 06:54h, 11 May
hi , really want to use your blur shader of unity3d. but after I tried it. I found that it cannot work on my computer. It says ““Program ‘vert’, incorrect number of arguments to numeric-type constructor (compiling for d3d11) at line 25. Same for d3d11_9x. ” hope you can help me to sovle this problem. thank you very much.
Ana TodorPosted at 20:42h, 15 May
Hello, please see the reply I posted above to a person who had the same problem as you did. And sorry for the late answer!
Eryk PajewskiPosted at 14:24h, 30 June
Great example, thank you very much for shearing.
WillPosted at 18:38h, 18 September
Im no expert when it comes to writing shaders so this question may come across a bit thick. But I would like to create a shader that adds blur to selective objects like you have without the mirroring that you have created to represent water. I really love this shader and can see it being used a lot in 2d games.
love u3dPosted at 06:03h, 30 November
Blur shader (Pro Only) version 1.1 error? in unity 4.3.0f4 ,the Blur shader (Pro Only) version 1.1 be imported,but show “Material doesn’t have a float or range property ‘_blurSizeXY’
please tell me how to solve the problem