合并两个Unity着色器

时间:2018-10-08 19:59:39

标签: unity3d shader

我正在寻找一种卡通着色器,它既要具有一个着色器的单元着色效果,又要具有另一个着色器的轮廓。

是否有相对简单的方法将两者结合起来?

大纲使用“ UsePass”引用了一个简单的平面着色器,有没有办法我可以引用单元格着色器?

这是轮廓着色器:

Shader "TSF/BaseOutline1"
{
    Properties 
    {

        [MaterialToggle(_TEX_ON)] _DetailTex ("Enable Detail texture", Float) = 0   //1
        _MainTex ("Detail", 2D) = "white" {}                                        //2
        _ToonShade ("Shade", 2D) = "white" {}                                       //3
        [MaterialToggle(_COLOR_ON)] _TintColor ("Enable Color Tint", Float) = 0     //4
        _Color ("Base Color", Color) = (1,1,1,1)                                    //5 
        [MaterialToggle(_VCOLOR_ON)] _VertexColor ("Enable Vertex Color", Float) = 0//6        
        _Brightness ("Brightness 1 = neutral", Float) = 1.0                         //7 
        _OutlineColor ("Outline Color", Color) = (0.5,0.5,0.5,1.0)                  //10
        _Outline ("Outline width", Float) = 0.01                                    //11

    }

    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 250 
        Lighting Off
        Fog { Mode Off }

        UsePass "TSF/Base1/BASE"

        Pass
        {
            Cull Front
            ZWrite On
            CGPROGRAM
            #include "UnityCG.cginc"
            #pragma fragmentoption ARB_precision_hint_fastest
            #pragma glsl_no_auto_normalization
            #pragma vertex vert
            #pragma fragment frag

            struct appdata_t 
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
            };

            struct v2f 
            {
                float4 pos : SV_POSITION;
            };

            fixed _Outline;


            v2f vert (appdata_t v) 
            {
                v2f o;
                o.pos = v.vertex;
                o.pos.xyz += normalize(v.normal.xyz) *_Outline*0.01;
                o.pos = UnityObjectToClipPos(o.pos);
                return o;
            }

            fixed4 _OutlineColor;

            fixed4 frag(v2f i) :COLOR 
            {
                return _OutlineColor;
            }

            ENDCG
        }
    }
Fallback "Legacy Shaders/Diffuse"
}

单元阴影着色器在这里:

Shader "Toon/Cutoff"
{
    Properties
    {
        _Color("Color(RGBA)", Color) = (1,1,1,1)
        _MainTex("Texture", 2D) = "white" {}
        _Shininess ("Shininess(0.0:)", Float) = 1.0

        _ShadowThreshold ("Shadow Threshold(0.0:1.0)", Float) = 0.5
        _ShadowColor ("Shadow Color(RGBA)", Color) = (0,0,0,0.5)
        _ShadowSharpness ("Shadow Sharpness(0.0:)", Float) = 100
        _Cutoff ("Alpha cutoff", Range(0,1)) = 0.9
    }

    SubShader
    {
        // Settings
        Tags {"Queue" = "Transparent" "IgnoreProjector"="False" "RenderType" = "TransparentCutout"}



        // Surface Shader Pass ( Front )
        Cull Back
        ZWrite On
        AlphaTest Greater 0.9
        //AlphaTest Equal 1
        Blend SrcAlpha OneMinusSrcAlpha
        CGPROGRAM
        #pragma surface surf Toon alphatest:_Cutoff vertex:vert
        //  Toon Surface Shader for Unity
        //  File        :   ToonSurface.cginc
        //  Title       :   Toon Surface Shader with Alpha
        //  Language    :   Cg Language
        //  Include     :   ToonSurface.cginc
        /*
            How To Use  :

            CGPROGRAM
            #pragma surface surf Toon
            #include "ToonSurface.cginc"
            ENDCG
        */

        float4 _Color;
        sampler2D _MainTex;
        float   _ShadowThreshold;
        float4  _ShadowColor;
        float   _ShadowSharpness;
        float   _Shininess;


        struct ToonSurfaceOutput
        {
            half3 Albedo;
            half3 Normal;
            half3 Emission;
            half3 Gloss;
            half Specular;
            half Alpha;
            half4 Color;
        };

        inline half4 LightingToon (ToonSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
        {
            //Lighting paramater
            float4  lightColor = _LightColor0 * atten;
            float   lightStrength = dot(lightDir, s.Normal) * 0.5 + 0.5;

            //ToonMapping
            half shadowRate = abs( max( -1, ( min( lightStrength, _ShadowThreshold ) -_ShadowThreshold)*_ShadowSharpness ) )*_ShadowColor.a;
            float4 toon = float4(1,1,1,1) * (1-shadowRate) +  _ShadowColor *shadowRate;

            //Output
            //float4 color = saturate( _Color * (lightColor*2) ) * s.Color;
            float4 color = _Color * (lightColor) * s.Color * (atten*2) * _Shininess;

            color *= toon;
            color.a = s.Alpha;
            return color;
        }

        struct Input
        {
            float2 uv_MainTex;
        };


        void vert (inout appdata_full v, out Input o) {
            UNITY_INITIALIZE_OUTPUT(Input,o);
        }

        void surf (Input IN, inout ToonSurfaceOutput o)
        {

            // Defaults
            o.Albedo = 0.0;
            o.Emission = 0.0;
            o.Gloss = 0.0;
            o.Specular = 0.0;

            half4 c     = tex2D(_MainTex, IN.uv_MainTex) * _Color;
            //o.Albedo  = c;
            o.Color     = c;
            o.Alpha     = c.a;
        }

        ENDCG

//      // Outline Pass
//      Pass
//      {
//          Cull Off
//          ZWrite On
//          Lighting Off
//          CGPROGRAM
//          #pragma vertex vert
//          #pragma fragment frag
//          #include "UnityCG.cginc"
//          #include "ToonOutlineVertFrag.cginc"
//          ENDCG
//      }


        // Surface Shader Pass ( Back )
        Cull Front
        ZWrite On
        //AlphaTest Equal 1
        AlphaTest Greater 0.9
        Blend SrcAlpha OneMinusSrcAlpha
        CGPROGRAM
        #pragma surface surf Toon alphatest:_Cutoff  vertex:vert
        //  Toon Surface Shader for Unity
        //  File        :   ToonSurface.cginc
        //  Title       :   Toon Surface Shader with Alpha
        //  Language    :   Cg Language
        //  Include     :   ToonSurface.cginc
        /*
            How To Use  :

            CGPROGRAM
            #pragma surface surf Toon
            #include "ToonSurface.cginc"
            ENDCG
        */

        float4 _Color;
        sampler2D _MainTex;
        float   _ShadowThreshold;
        float4  _ShadowColor;
        float   _ShadowSharpness;
        float   _Shininess;


        struct ToonSurfaceOutput
        {
            half3 Albedo;
            half3 Normal;
            half3 Emission;
            half3 Gloss;
            half Specular;
            half Alpha;
            half4 Color;
        };

        inline half4 LightingToon (ToonSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
        {
            //Lighting paramater
            float4  lightColor = _LightColor0 * atten;
            float   lightStrength = dot(lightDir, s.Normal) * 0.5 + 0.5;

            //ToonMapping
            half shadowRate = abs( max( -1, ( min( lightStrength, _ShadowThreshold ) -_ShadowThreshold)*_ShadowSharpness ) )*_ShadowColor.a;
            float4 toon = float4(1,1,1,1) * (1-shadowRate) +  _ShadowColor *shadowRate;

            //Output
            //float4 color = saturate( _Color * (lightColor*2) ) * s.Color;
            float4 color = _Color * (lightColor) * s.Color * (atten*2) * _Shininess;

            color *= toon;
            color.a = s.Alpha;
            return color;
        }

        struct Input
        {
            float2 uv_MainTex;
        };


        void vert (inout appdata_full v, out Input o) {
            UNITY_INITIALIZE_OUTPUT(Input,o);
        }

        void surf (Input IN, inout ToonSurfaceOutput o)
        {

            // Defaults
            o.Albedo = 0.0;
            o.Emission = 0.0;
            o.Gloss = 0.0;
            o.Specular = 0.0;

            half4 c     = tex2D(_MainTex, IN.uv_MainTex) * _Color;
            //o.Albedo  = c;
            o.Color     = c;
            o.Alpha     = c.a;
        }

        ENDCG

    }

    // Other Environment
    Fallback "Diffuse"
}

0 个答案:

没有答案