unity-game-engineshaderblurfragment-shader

Unity Gaussian blur Shader just makes my texture white - Why?


I'm trying to make a Gaussian Blur Shader work from this tutorial: https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson5

But when I convert it to unity's shader language I'm getting nothing but white in my texture, except where transparency is (and no bluring ether.)

Here is my Unity Blur Shader code:

Shader "Unlit/UnlitShaderTest"  
  {
        Properties
        {
            _MainTex ("Texture", 2D) = "white" {}
        }
        SubShader
        {
            Tags { "RenderType"="Opaque" }
            LOD 100

            Pass
            {
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag       
                #include "UnityCG.cginc"

                struct appdata
                {
                    float4 vertex : POSITION;
                    float2 uv : TEXCOORD0;
                };

                struct v2f
                {
                    float2 uv : TEXCOORD0;
                    float4 vertex : SV_POSITION;
                };

                sampler2D _MainTex;
                float4 _MainTex_ST;

                uniform  float resolution = 800;
                uniform  float radius = 400;
                uniform  float2 dir = float2(0,1);

                v2f vert (appdata v)
                {
                    v2f o;
                    o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                    o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                    return o;
                }

                fixed4 frag (v2f i) : SV_Target
                {
                    float4 sum = float4(0.0, 0.0, 0.0, 0.0);
                    float2 tc = i.uv;

                    float blur = 0;// radius/resolution; 

                    //the direction of our blur
                    //(1.0, 0.0) -> x-axis blur
                    //(0.0, 1.0) -> y-axis blur
                    float hstep = dir.x;
                    float vstep = dir.y;

                    sum += mul(tex2D(_MainTex, float2(tc.x - mul(mul(4.0,blur),hstep), tc.y - mul(mul(4.0,blur),vstep))),0.0162162162);
                    sum += mul(tex2D(_MainTex, float2(tc.x - mul(mul(3.0,blur),hstep), tc.y - mul(mul(3.0,blur),vstep))),0.0540540541);
                    sum += mul(tex2D(_MainTex, float2(tc.x - mul(mul(2.0,blur),hstep), tc.y - mul(mul(2.0,blur),vstep))),0.1216216216);
                    sum += mul(tex2D(_MainTex, float2(tc.x - mul(mul(1.0,blur),hstep), tc.y - mul(mul(1.0,blur),vstep))),0.1945945946);

                    sum += mul(tex2D(_MainTex, float2(tc.x, tc.y)),0.2270270270);

                    sum += mul(tex2D(_MainTex, float2(tc.x + mul(mul(1.0,blur),hstep), tc.y + mul(mul(1.0,blur),vstep))),0.1945945946);
                    sum += mul(tex2D(_MainTex, float2(tc.x + mul(mul(2.0,blur),hstep), tc.y + mul(mul(2.0,blur),vstep))),0.1216216216);
                    sum += mul(tex2D(_MainTex, float2(tc.x + mul(mul(3.0,blur),hstep), tc.y + mul(mul(3.0,blur),vstep))),0.0540540541);
                    sum += mul(tex2D(_MainTex, float2(tc.x + mul(mul(4.0,blur),hstep), tc.y + mul(mul(4.0,blur),vstep))),0.0162162162);

                    // sample the texture
                    fixed4 col = mul(tex2D(_MainTex, i.uv),float4(sum.r, sum.g, sum.b, sum.a));             
                    return col;
                }
                ENDCG
            }
        }
    }

Can someone tell me why my shader is just giving me all white instead of the normal colors? Also if someone could tell me why it's not bluring that'd be great too.


Solution

  • Shader "Custom/GaussianBlur"
    { 
        Properties
        {
            _MainTex ("Texture", 2D) = "white" {}
            radius ("Radius", Range(0,30)) = 15
            resolution ("Resolution", float) = 800  
            hstep("HorizontalStep", Range(0,1)) = 0.5
            vstep("VerticalStep", Range(0,1)) = 0.5  
        }
    
        SubShader
        {
            Tags {"Queue"="Transparent" "IgnoreProjector"="true" "RenderType"="Transparent"}
            ZWrite Off Blend SrcAlpha OneMinusSrcAlpha Cull Off
            Pass
            {    
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #pragma fragmentoption ARB_precision_hint_fastest
                #include "UnityCG.cginc"
    
                struct appdata_t
                {
                    float4 vertex   : POSITION;
                    float4 color    : COLOR;
                    float2 texcoord : TEXCOORD0;
                };    
                struct v2f
                {
                    half2 texcoord  : TEXCOORD0;
                    float4 vertex   : SV_POSITION;
                    fixed4 color    : COLOR;
                };
    
                sampler2D _MainTex;
                float radius;
                float resolution;
    
                //the direction of our blur
                //hstep (1.0, 0.0) -> x-axis blur
                //vstep(0.0, 1.0) -> y-axis blur
                //for example horizontaly blur equal:
                //float hstep = 1;
                //float vstep = 0;
                float hstep;
                float vstep;
    
                v2f vert(appdata_t IN)
                {
                    v2f OUT;
                    OUT.vertex = UnityObjectToClipPos(IN.vertex);
                    OUT.texcoord = IN.texcoord;
                    OUT.color = IN.color;
                    return OUT;
                }
    
                float4 frag(v2f i) : COLOR
                {    
                    float2 uv = i.texcoord.xy;
                    float4 sum = float4(0.0, 0.0, 0.0, 0.0);
                    float2 tc = uv;
    
                    //blur radius in pixels
                    float blur = radius/resolution/4;     
    
                    sum += tex2D(_MainTex, float2(tc.x - 4.0*blur*hstep, tc.y - 4.0*blur*vstep)) * 0.0162162162;
                    sum += tex2D(_MainTex, float2(tc.x - 3.0*blur*hstep, tc.y - 3.0*blur*vstep)) * 0.0540540541;
                    sum += tex2D(_MainTex, float2(tc.x - 2.0*blur*hstep, tc.y - 2.0*blur*vstep)) * 0.1216216216;
                    sum += tex2D(_MainTex, float2(tc.x - 1.0*blur*hstep, tc.y - 1.0*blur*vstep)) * 0.1945945946;
    
                    sum += tex2D(_MainTex, float2(tc.x, tc.y)) * 0.2270270270;
    
                    sum += tex2D(_MainTex, float2(tc.x + 1.0*blur*hstep, tc.y + 1.0*blur*vstep)) * 0.1945945946;
                    sum += tex2D(_MainTex, float2(tc.x + 2.0*blur*hstep, tc.y + 2.0*blur*vstep)) * 0.1216216216;
                    sum += tex2D(_MainTex, float2(tc.x + 3.0*blur*hstep, tc.y + 3.0*blur*vstep)) * 0.0540540541;
                    sum += tex2D(_MainTex, float2(tc.x + 4.0*blur*hstep, tc.y + 4.0*blur*vstep)) * 0.0162162162;
                    return float4(sum.rgb, 1);
                }    
                ENDCG
            }
        }
        Fallback "Sprites/Default"    
    }
    

    The reason you are getting a white texture is because mul(tex2D(_MainTex, i.uv),float4(sum.r, sum.g, sum.b, sum.a)); returns a float1 ( float1 mul(float4 v, float4x1 M); )