Unity:是否可以连续使用2个通行证?

时间:2018-10-05 02:36:27

标签: unity3d graphics shader multipass

我有一个着色器,其中有多个遍,其中2个是轮廓遍和模糊遍。我正在尝试创建轮廓,然后使其模糊。如何使模糊过程进入轮廓过程的输出以进行模糊处理?

概述通行证:

Pass{
        Name "BLUR"
        Cull Off
        ZWrite Off
        ZTest Always
        Blend SrcAlpha OneMinusSrcAlpha
        CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#pragma multi_compile_fog

        fixed4 frag(v2f IN) : SV_Target
    {
        fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color;

        if (c.a > _Cutoff) {
            fixed4 pixelUp = tex2D(_MainTex, IN.texcoord + fixed2(0, _MainTex_TexelSize.y));
            fixed4 pixelDown = tex2D(_MainTex, IN.texcoord - fixed2(0,  _MainTex_TexelSize.y));
            fixed4 pixelRight = tex2D(_MainTex, IN.texcoord + fixed2(_MainTex_TexelSize.x, 0));
            fixed4 pixelLeft = tex2D(_MainTex, IN.texcoord - fixed2(_MainTex_TexelSize.x, 0));
            fixed4 pixelUp2 = tex2D(_MainTex, IN.texcoord + fixed2(0, 2 * _MainTex_TexelSize.y));
            fixed4 pixelDown2 = tex2D(_MainTex, IN.texcoord - fixed2(0, 2 * _MainTex_TexelSize.y));
            fixed4 pixelRight2 = tex2D(_MainTex, IN.texcoord + fixed2(2 * _MainTex_TexelSize.x, 0));
            fixed4 pixelLeft2 = tex2D(_MainTex, IN.texcoord - fixed2(2 * _MainTex_TexelSize.x, 0));
            fixed4 pixelUp3 = tex2D(_MainTex, IN.texcoord + fixed2(0, 3 * _MainTex_TexelSize.y));
            fixed4 pixelDown3 = tex2D(_MainTex, IN.texcoord - fixed2(0, 3 * _MainTex_TexelSize.y));
            fixed4 pixelRight3 = tex2D(_MainTex, IN.texcoord + fixed2(3 * _MainTex_TexelSize.x, 0));
            fixed4 pixelLeft3 = tex2D(_MainTex, IN.texcoord - fixed2(3 * _MainTex_TexelSize.x, 0));

            c = c * 0.18
                + 0.15 * (pixelUp + pixelDown + pixelRight + pixelLeft)
                + 0.12 * (pixelUp2 + pixelDown2 + pixelRight2 + pixelLeft2);
                + 0.09 * (pixelUp3 + pixelDown3 + pixelRight3 + pixelLeft3);
        }

        c.rgb *= c.a;
        return c;
    }

        ENDCG
    }

模糊通行证:

Position                    Name...ID              Name       ID Roster.Position Salary                     Game.Info TeamAbbrev AvgPointsPerGame
1        G        Ben Bishop (11402606)        ben bishop 11402606               G   8400 ARI@DAL 10/04/2018 08:30PM ET        DAL             4.40
2        G    Anton Khudobin (11402601)    anton khudobin 11402601               G   8200 ARI@DAL 10/04/2018 08:30PM ET        DAL             4.36
3        G Connor Hellebuyck (11402708) connor hellebuyck 11402708               G   7700 WPG@STL 10/04/2018 08:00PM ET        WPG             5.42
4        C      Tyler Seguin (11402648)      tyler seguin 11402648          C/UTIL   7700 ARI@DAL 10/04/2018 08:30PM ET        DAL             4.75
5        G  Laurent Brossoit (11402681)  laurent brossoit 11402681               G   7500 WPG@STL 10/04/2018 08:00PM ET        WPG             2.26
6        G       Eric Comrie (11402730)       eric comrie 11402730               G   7500 WPG@STL 10/04/2018 08:00PM ET        WPG             2.47

我是着色器的新手,因此,如果有任何建议,将不胜感激。

1 个答案:

答案 0 :(得分:3)

使用Grab pass,您可以将到目前为止生成的屏幕移到另一遍,并从中进行采样。

在您的情况下,将其放在轮廓通过之后,并以模糊过程而不是原始纹理从生成的纹理中采样。

Pass{
    // ...
    // no change
    // ...
}

GrabPass { "_GrabTexture" }        

Pass{ 
    // Note the changes from calling tex2D with _MainTex to _GrabTexture, 
    // matching the string in the GrabPass directive above 

    // ...
    // use a vertex shader to get the uvs of the screen grab

    struct v2f {
        float4 grabPos : TEXCOORD0;
        float4 pos : SV_POSITION;
    };

    v2f vert(appdata_base v) {
        v2f o;
        // use UnityObjectToClipPos from UnityCG.cginc to calculate 
        // the clip-space of the vertex
        o.pos = UnityObjectToClipPos(v.vertex);
        // use ComputeGrabScreenPos function from UnityCG.cginc
        // to get the correct texture coordinate
        o.grabPos = ComputeGrabScreenPos(o.pos);
        return o;
    }

    // Note the changes from calling tex2D with _MainTex to _GrabTexture, 
    // matching the string in the GrabPass directive above 
    // as well as IN.grabPos instead of IN.texcoord
    // and tex2Dproj instead of tex2D

    fixed4 frag(v2f IN) : SV_Target {
        fixed4 c = tex2Dproj(_GrabTexture, IN.grabPos) * IN.color; 

        if (c.a > _Cutoff) {
            fixed4 pixelUp = tex2Dproj(_GrabTexture, IN.grabPos+ fixed2(0, _MainTex_TexelSize.y));
            fixed4 pixelDown = tex2Dproj(_GrabTexture, IN.grabPos - fixed2(0,  _MainTex_TexelSize.y));
            fixed4 pixelRight = tex2Dproj(_GrabTexture, IN.texcoord + fixed2(_MainTex_TexelSize.x, 0));
            fixed4 pixelLeft = tex2Dproj(_GrabTexture, IN.texcoord - fixed2(_MainTex_TexelSize.x, 0));
            fixed4 pixelUp2 = tex2Dproj(_GrabTexture, IN.texcoord + fixed2(0, 2 * _MainTex_TexelSize.y));
            fixed4 pixelDown2 = tex2Dproj(_GrabTexture, IN.texcoord - fixed2(0, 2 * _MainTex_TexelSize.y));
            fixed4 pixelRight2 = tex2Dproj(_GrabTexture, IN.texcoord + fixed2(2 * _MainTex_TexelSize.x, 0));
            fixed4 pixelLeft2 = tex2Dproj(_GrabTexture, IN.texcoord - fixed2(2 * _MainTex_TexelSize.x, 0));
            fixed4 pixelUp3 = tex2Dproj(_GrabTexture, IN.texcoord + fixed2(0, 3 * _MainTex_TexelSize.y));
            fixed4 pixelDown3 = tex2Dproj(_GrabTexture, IN.texcoord - fixed2(0, 3 * _MainTex_TexelSize.y));
            fixed4 pixelRight3 = tex2Dproj(_GrabTexture, IN.texcoord + fixed2(3 * _MainTex_TexelSize.x, 0));
            fixed4 pixelLeft3 = tex2Dproj(_GrabTexture, IN.texcoord - fixed2(3 * _MainTex_TexelSize.x, 0));

            c = c * 0.18
                + 0.15 * (pixelUp + pixelDown + pixelRight + pixelLeft)
                + 0.12 * (pixelUp2 + pixelDown2 + pixelRight2 + pixelLeft2);
                + 0.09 * (pixelUp3 + pixelDown3 + pixelRight3 + pixelLeft3);
        }

        c.rgb *= c.a;
        return c;
    }

        ENDCG
    }