Hi. I’m trying to use Unity’s bluring script to make a variable bluring effect. By that I mean for example the upper half of the screen should be less blured that the bottom half. Is such thing possible? Thanks
It is possible. In the BlurEffect script, the following two lines write the blurred image to the screen:
if( oddEven )
Graphics.Blit(buffer, destination);
else
Graphics.Blit(buffer2, destination);
This Blit simply copies the contents of buffer or buffer2 to the destination texture.
Another version of Blit (Unity - Scripting API: Graphics.Blit) uses a 3rd Material parameter. This material will be used when writing to destination. In this Material, you can put any shader with any number of extra data besides the blurred image: like the source image and a mask texture to identify how much blur each section of the screen needs. Then have the shader in that new material blend between source and blurred texture based on the mask. You don’t even need Cg for that, it can be fixed pipeline if you want.
Thanks for the quick answer. I’m sorry if I’m annoying but how would I make such a mask?
That depends on how you want the mask to work. In your shader you can choose to mask based on the color, in which case you would make a black and white image, where black means no blur and white max blur. Or you can blend based on alpha, in which case you’ll probably use the same image but imported with ‘generate alpha from grayscale’.
Hi,
Perhaps I am stating the obvious but anyways, I think that a color mask is less expensive memory - wise since it can be saved in a rgb texture format opposed to the rgba needed for the alpha mask. I really wish I could play with these image filters myself, but I have to wait for a while.
If you go the alpha route, you can set the format of the mask texture to Alpha8. In that case, the alpha approach takes less memory than RGB
Hi tomvds,
Thanks for the clarification, it sounds great !
-Ippokratis
Thank you both for the awsome answered. I’m leaning towards using the black white image for the mask with a gradient from top to bottom. This question might seem stupid but please bear with me. How would the shader make the blurring stronger in the areas of the mask which are closer to white? A small example would help me a lot, I’m far from being good at shaders. Thank you again
This is the shader I use for blending blur with the source image (note that all the " characters are duplicated as i defined this shader inside a script):
Shader ""BlurBlend""
{
Properties
{
_MainTex ("""", any) = """" {}
_Src ("""", any) = """" {}
_Blend ("""", Range(0,1)) = .5
}
SubShader
{
Pass
{
ZTest Always
Cull Off
ZWrite Off
Fog { Mode Off }
SetTexture [_MainTex]
{
constantColor (0,0,0,[_Blend])
combine texture, constant alpha
}
SetTexture [_Src]
{
combine previous lerp(previous alpha) texture
}
}
}
Fallback off
}
_MainTex should be set to the blurred image. _Src should be assigned the original image. In my case, I blend based on a single value (_Blend) just to be able to gradually fade in the blur. In your case, you should get this value from your mask texture by adding an extra SetTexture stage before the _MainTex one.
A little too late maybe… I’m new in shaders and I’m trying to do the same thing: blur just a specific section of my texture (gradually), I was excited when I saw your code but declaring it inside another shader is… beyond me, not sure what you mean… would you mind explaining further and how to apply this shader to some mask?