うにてぃブログ

主にUnityとC#に関する記事を書いていきます

【Unity】Shader コードの複数パスでパラメータを共有する

複数パスの Shader で各パスごとに全部記述していくと以下のようになる

Shader "Custom/Outline"
{
    Properties
    {
        _Stencil ("Stencil", int) = 10
    }
    SubShader
    {
        Tags {
             "RenderType"="Opaque"
        }


        Pass
        {
            Stencil
            {
                Ref  [_Stencil]
                Comp notequal
            }

            ZWrite Off

            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
            };

            float _Stencil;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                return o;
            }


            fixed4 frag (v2f i) : SV_Target
            {
                return fixed4(1, 0, 0, 1);
            }

            ENDCG
        }

        Pass
        {
            Stencil
            {
                Ref  [_Stencil]
                Comp Equal
            }

            zwrite off

            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
            };

            float _Stencil;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                return fixed4(0, 1, 0, 1);
            }

            ENDCG
        }
    }
}

各パスで同じ定義やパラメータを使っているところがあるので共通化したい。
そのためには、共通箇所を CGINCLUDE ~ ENDCG の間に記述してやることで共通化できる

さきほどの Shader を共通部分を抜き出すと以下のように簡約化できる

Shader "Custom/Outline"
{
    Properties
    {
        _Stencil ("Stencil", int) = 10
    }
    SubShader
    {
        Tags {
             "RenderType"="Opaque"
        }

        CGINCLUDE

        #pragma vertex vert
        #pragma fragment frag

        #include "UnityCG.cginc"

        struct appdata
        {
            float4 vertex : POSITION;
        };

        struct v2f
        {
            float4 vertex : SV_POSITION;
        };

        float _Stencil;

        v2f vert (appdata v)
        {
            v2f o;
            o.vertex = UnityObjectToClipPos(v.vertex);
            return o;
        }

        ENDCG

        Pass
        {
            Stencil
            {
                Ref  [_Stencil]
                Comp notequal
            }

            ZWrite Off

            CGPROGRAM

            fixed4 frag (v2f i) : SV_Target
            {
                return fixed4(1, 0, 0, 1);
            }

            ENDCG
        }

        Pass
        {
            Stencil
            {
                Ref  [_Stencil]
                Comp Equal
            }

            zwrite off
            CGPROGRAM

            fixed4 frag (v2f i) : SV_Target
            {
                return fixed4(0, 1, 0, 1);
            }

            ENDCG
        }
    }
}