UsePass使我的CPU无法运行着色器,但我可以单独运行它们

时间:2015-09-28 15:05:00

标签: unity3d shader gpu fragment-shader

好吧,所以我正在制作一个需要UsePass的着色器。我创建了Shader,我将使用UsePassing:

Shader "Unlit/GrabGlow"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        threshold ("threshold", Range(0,1)) = 0

    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            Name "MAIN"
            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 threshold = 0.5;

            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
            {
                float3 th = float3(threshold,threshold,threshold);
                float2 tc = i.uv;

                float4 texColor = tex2D(_MainTex, float2(tc.x, tc.y));

                if(texColor.r >= th.r && texColor.g >= th.g && texColor.b >= th.b){ 
                     return texColor;      
                }
                return float4(0.0, 0.0, 0.0, 0.0);
            }
            ENDCG
        }
    }
}

如果我将它放在所需的纹理上,这将完美地运作。

接下来是实际的完整着色器:

Shader "Unlit/CrossFlares"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        threshold ("threshold", Range(0,1)) = 0
        radius ("radius", Range(0,30)) = 0
        resolution ("resolution", float) = 800
    }
    SubShader
    {
        Tags { "RenderType" = "Opaque"  }
        LOD 100

        UsePass "Unlit/GRABGLOW/MAIN"

        GrabPass
        {
            "_ScreenTex"
        }

        //SetTexture [_ScreenTex]

        Pass
        {
            Name "hBlur"     

            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 = 200;
            uniform  float threshold = 0.5;
            uniform  float2 dir = float2(1,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
            {    
                float3 th = float3(threshold,threshold,threshold);
                float2 tc = i.uv;

                float4 texColor = tex2D(_MainTex, float2(tc.x, tc.y));
                float4 sum = float4(0.0, 0.0, 0.0, 0.0);

                if(texColor.r >= th.r && texColor.g >= th.g && texColor.b >= th.b){            

                    //blur radius in pixels
                    float blur = radius/resolution/4; 

                    //the direction of our blur
                    //(1.0, 0.0) -> x-axis blur
                    //(0.0, 1.0) -> y-axis blur

                    float hstep = 1;
                    float vstep = 1;

                    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 sum;
            }
            ENDCG
        }

        Pass
        {


            Name "vBlur"
            Blend  OneMinusDstColor One


            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 _GrabTexture;

            sampler2D _MainTex;
            float4 _MainTex_ST;
            uniform  float resolution = 800;
            uniform  float radius = 200;
            uniform  float threshold = 0.5;
            uniform  float2 dir = float2(1,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
            {                   
                float3 th = float3(threshold,threshold,threshold);
                float2 tc = i.uv;

                float4 texColor = tex2D(_MainTex, float2(tc.x, tc.y));
                float4 sum = float4(0.0, 0.0, 0.0, 0.0);

                if(texColor.r >= th.r && texColor.g >= th.g && texColor.b >= th.b){            

                    //blur radius in pixels
                    float blur = radius/resolution/4; 

                    //the direction of our blur
                    //(1.0, 0.0) -> x-axis blur
                    //(0.0, 1.0) -> y-axis blur

                    float hstep = -1;
                    float vstep = 1;

                    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 sum;
            }
            ENDCG
        }
    }
}

只要我将UsePass行注释掉,它也可以正常工作。

但是一旦我添加了UsePass,我就会收到错误: 'Unlit / CrossFlares'中的着色器警告:此GPU不支持着色器(没有子载体/后备适合)

那么为什么我可以自己使用着色器,但是当我尝试UsePass时,它会抛出这个错误?

0 个答案:

没有答案