unity-game-engine - Unity 高斯模糊着色器只是使我的纹理变白 - 为什么?

标签 unity-game-engine shader blur fragment-shader

我正在尝试根据本教程制作高斯模糊着色器: https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson5

但是当我将其转换为 Unity 的着色器语言时,除了透明度(并且没有模糊以太)之外,我的纹理中除了白色什么也没有。

这是我的 Unity 模糊着色器代码:

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
            }
        }
    }

有人能告诉我为什么我的着色器只给我全白色而不是正常颜色吗?另外,如果有人能告诉我为什么它不模糊那就太好了。

最佳答案

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"    
}

获得白色纹理的原因是 mul(tex2D(_MainTex, i.uv),float4(sum.r, sum.g, sum.b, sum.a)); 返回 float1 ( float1 mul(float4 v, float4x1 M); )

关于unity-game-engine - Unity 高斯模糊着色器只是使我的纹理变白 - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32811531/

相关文章:

android - 在 eclipse 中访问 Unity 变量

Swift- SKEffectNode 需要一段时间才能出现

iphone - 未调用 UIApplicationDelegate 方法

c# - 在 Unity 中使用泛型作为变量类型

c++ - 为什么在 C++ 顶点函数中有两个括号 [?

opengl-es - 如何在WebGL中访问相邻像素的深度值?

c++ - 仅显示灰色的 OpenGL 反射着色器

ios - 当应用程序处于后台时,CIFilter 无法正常工作

css - 固定位置不适用于 CSS 模糊过滤器

c# - 如何在不减慢至 0.3 FPS 的情况下移动 500 个 Rigidbody 立方体?