Blur Shader

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.

Some Theory

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.

blur shader

Visualizing the Blur Shader

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.

Demonstration

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.

References:

Gaussian Blur Filter Shader on GameRendering.com

Ana Todor is a Computer Scientist with a playful and literary twist. She has a Bachelor of Science degree in Engineering and a Bachelor of Arts degree in Cultural Studies. She is currently studying towards obtaining a Master of Science degree in Computer Science, Digital Interactive Entertainment. Areas of specialty: programming, web design and development, video game development, literary criticism, and creative writing. Notable hobbies: photography.

Leave a Reply

15 Comments

  1. 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).

    • 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.

  2. Also yay, my free blur shader has been rated with 4 stars on the Unity Asset store! Good enough for my first submission :P.

  3. 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.

    • 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.

  4. Hi Ana,

    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

    • Hi David,
      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);
      to
      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.

      • Hi Ana,
        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.

        Cheers,
        David

  5. 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.

    • 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!

  6. Hi There,

    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.

  7. 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’
    UnityEditor.ContainerWindow:InternalCloseWindow()

    please tell me how to solve the problem

Next ArticleGabriel Knight: Sins of the Fathers Review