1

I'm attempting to apply a Gaussian blur to a plane, such that any object behind the plane gets blurred, and any object in front of the plane does not get blurred.

The code for my shader is as follows:

Shader "Custom/Blur" {
    Properties {
        _Color ("Main Color", Color) = (1,1,1,1)
        _BumpAmt  ("Distortion", Range (0,128)) = 10
        _MainTex ("Tint Color (RGB)", 2D) = "white" {}
        _BumpMap ("Normalmap", 2D) = "bump" {}
        _HorizontalDistort ("Horizontal Distortion", Float) = 1.0
        _VerticalDistort ("Vertical Distortion", Float) = 1.0
    }

    Category {

        // We must be transparent, so other objects are drawn before this one.
        Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Opaque" }


        SubShader {

            // Horizontal blur
            GrabPass {                    
                Tags { "LightMode" = "Always" }
            }
            Pass {
                Tags { "LightMode" = "Always" }

                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #pragma fragmentoption ARB_precision_hint_fastest
                #include "UnityCG.cginc"

                struct v2f {
                    float4 vertex : POSITION;
                    float4 uvgrab : TEXCOORD0;
                };

                v2f vert (v2f v) {
                    v2f o;
                    o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                    #if UNITY_UV_STARTS_AT_TOP // Only true in directX platforms
                    float scale = -1.0;
                    #else
                    float scale = 1.0;
                    #endif
                    o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
                    o.uvgrab.zw = o.vertex.zw;
                    return o;
                }

                sampler2D _GrabTexture;
                float4 _GrabTexture_TexelSize;
                float _HorizontalDistort;

                half4 frag( v2f i ) : COLOR {

                    half4 sum = half4(0,0,0,0);
                    #define GRABPIXEL(kernelx) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx*_HorizontalDistort, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w)))
                    sum += GRABPIXEL(-4.0) * 0.05;
                    sum += GRABPIXEL(-3.0) * 0.09;
                    sum += GRABPIXEL(-2.0) * 0.12;
                    sum += GRABPIXEL(-1.0) * 0.15;
                    sum += GRABPIXEL(0.0) * 0.18;
                    sum += GRABPIXEL(+1.0) * 0.15;
                    sum += GRABPIXEL(+2.0) * 0.12;
                    sum += GRABPIXEL(+3.0) * 0.09;
                    sum += GRABPIXEL(+4.0) * 0.05;

                    return sum;
                }
                ENDCG
            }
            // Vertical blur
            GrabPass {                        
                Tags { "LightMode" = "Always" }
            }
            Pass {
                Tags { "LightMode" = "Always" }

                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #pragma fragmentoption ARB_precision_hint_fastest
                #include "UnityCG.cginc"

                struct v2f {
                    float4 vertex : POSITION;
                    float4 uvgrab : TEXCOORD0;
                };

                v2f vert (v2f v) {
                    v2f o;
                    o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                    #if UNITY_UV_STARTS_AT_TOP
                    float scale = -1.0;
                    #else
                    float scale = 1.0;
                    #endif
                    o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
                    o.uvgrab.zw = o.vertex.zw;
                    return o;
                }

                sampler2D _GrabTexture;
                float4 _GrabTexture_TexelSize;
                float _VerticalDistort;

                half4 frag( v2f i ) : COLOR {

                    half4 sum = half4(0,0,0,0);
                    #define GRABPIXEL(kernely) tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely*_VerticalDistort, i.uvgrab.z, i.uvgrab.w)))
                    sum += GRABPIXEL(-4.0) * 0.05;
                    sum += GRABPIXEL(-3.0) * 0.09;
                    sum += GRABPIXEL(-2.0) * 0.12;
                    sum += GRABPIXEL(-1.0) * 0.15;
                    sum += GRABPIXEL(0.0) * 0.18;
                    sum += GRABPIXEL(+1.0) * 0.15;
                    sum += GRABPIXEL(+2.0) * 0.12;
                    sum += GRABPIXEL(+3.0) * 0.09;
                    sum += GRABPIXEL(+4.0) * 0.05;

                    return sum;
                }
                ENDCG
            }
        }
    }
}

However, while this successfully blurs objects behind the plane, objects in front of the plane have a weird glow effect: Weird glow effect

Blowing up so that you can see better: Zoomed in version of sphere

I've been struggling on this for a while, any help would be greatly appreciated!

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • Either weight in only pixels with depth behind the current depth, or even better, render the geometry sorted from back to front. – Yakov Galka Nov 22 '16 at 06:54
  • How does one do that? Been googling it for some time but can't figure out how to switch the geometry to render back to front. – Nihal Mirpuri Nov 22 '16 at 10:20
  • I don't know. It's straight forward with plain OpenGL, but apparently you don't use OpenGL directly, so that's why I removed that tag. – Yakov Galka Nov 22 '16 at 10:22

0 Answers0