Search Unity

Combining two shaders

Discussion in 'Shaders' started by TickleDucks, May 2, 2017.

  1. TickleDucks

    TickleDucks

    Joined:
    Mar 24, 2017
    Posts:
    1
    Hey guys,
    Shader noob here.
    I was wondering how it would be possible to combine two shaders.
    Code (csharp):
    1.  
    2. Shader "Custom/Jelly" {
    3.    Properties {
    4.        _MainTex ("Base (RGB)", 2D) = "white" {}
    5.    }
    6.    SubShader {
    7.        Pass {
    8.            Tags { "RenderType"="Opaque" }
    9.        
    10.            CGPROGRAM
    11.            #pragma vertex vert
    12.            #pragma fragment frag
    13.            #include "UnityCG.cginc"
    14.  
    15.            sampler2D _MainTex;
    16.            struct v2f {
    17.                float4 pos : SV_POSITION;
    18.                half2 uv : TEXCOORD0;
    19.            };
    20.  
    21.            v2f vert(appdata_base v) {
    22.                v2f o;
    23.                v.vertex.x += sign(v.vertex.x) * sin(_Time.w)/50;
    24.                v.vertex.y += sign(v.vertex.y) * cos(_Time.w)/50;
    25.                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    26.                o.uv = v.texcoord;
    27.                return o;
    28.            }
    29.  
    30.  
    31.            half4 frag(v2f i) : COLOR {
    32.                half4 c = tex2D(_MainTex, i.uv);
    33.                return c;
    34.            }
    35.  
    36.            ENDCG
    37.        }
    38.    }
    39.    FallBack "Diffuse"
    40. }
    41.  
    The second would be:
    Code (csharp):
    1.  
    2. Shader "Jelly/WithOutline" {
    3.    Properties{
    4.        _Color("Main Color", Color) = (.5,.5,.5,1)
    5.        _OutlineColor("Outline Color", Color) = (0,0,0,1)
    6.        _Outline("Outline width", Range(.002, 1)) = .005
    7.        _MainTex("Base (RGB)", 2D) = "white" { }
    8.    _ToonShade("ToonShader Cubemap(RGB)", CUBE) = "" { Texgen CubeNormal }
    9.    }
    10.  
    11.        CGINCLUDE
    12. #include "UnityCG.cginc"
    13.  
    14.    struct appdata {
    15.        float4 vertex : POSITION;
    16.        float3 normal : NORMAL;
    17.    };
    18.    struct v2f {
    19.        float4 pos : POSITION;
    20.        float4 color : COLOR;
    21.    };
    22.  
    23.    uniform float _Outline;
    24.    uniform float4 _OutlineColor;
    25.  
    26.    v2f vert(appdata v) {
    27.        v2f o;
    28.        o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    29.        float3 norm = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal);
    30.        float2 offset = TransformViewToProjection(norm.xy);
    31.        o.pos.xy += offset * o.pos.z * _Outline;
    32.        o.color = _OutlineColor;
    33.        return o;
    34.    }
    35.  
    36.  
    37.    ENDCG
    38.        SubShader{
    39.        Tags{ "RenderType" = "Opaque" "Queue" = "Transparent" }
    40.        UsePass "Toon/Basic-Alpha/BASE"
    41.        Pass{
    42.        Name "OUTLINE"
    43.        Tags{ "LightMode" = "Always" }
    44.        Cull Front
    45.        ZWrite On
    46.        ColorMask RGB
    47.        Blend SrcAlpha OneMinusSrcAlpha
    48.        CGPROGRAM
    49. #pragma vertex vert
    50. #pragma fragment frag
    51.        half4 frag(v2f i) :COLOR{ return i.color; }
    52.        ENDCG
    53.    }
    54.    }
    55.  
    56.        SubShader{
    57.        Tags{ "RenderType" = "Opaque" "Queue" = "Transparent" }
    58.        UsePass "Toon/Basic-Alpha/BASE"
    59.        Pass{
    60.        Name "OUTLINE"
    61.        Tags{ "LightMode" = "Always" }
    62.        Cull Front
    63.        ZWrite On
    64.        ColorMask RGB
    65.        Blend SrcAlpha OneMinusSrcAlpha
    66.        CGPROGRAM
    67. #pragma vertex vert
    68. #pragma exclude_renderers shaderonly
    69.        ENDCG
    70.        SetTexture[_MainTex]{ combine primary }
    71.    }
    72.    }
    73.  
    74.        Fallback "Jelly/Basic"
    75. }
    76.  
    If you could leave some comments in the code about what you put where as well then that would be really helpful.
    Thanks in advance!
     
  2. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    I have tried to combine that for you.
    The first Pass is copied from the first shader. If you named it in the first shader then you could use UsePass in the second shader - analogically to how it is done in ToonBasic and ToonBasicOutline shaders in standard assets.
    Code (CSharp):
    1. Shader "Custom/Jelly/WithOutline" {
    2.    Properties{
    3.        _Color("Main Color", Color) = (.5,.5,.5,1)
    4.        _OutlineColor("Outline Color", Color) = (0,0,0,1)
    5.        _Outline("Outline width", Range(.002, 1)) = .005
    6.        _MainTex("Base (RGB)", 2D) = "white" { }
    7.        _ToonShade("ToonShader Cubemap(RGB)", CUBE) = "" { Texgen CubeNormal }
    8.    }
    9.      
    10.        SubShader{
    11.        Pass {
    12.            Tags { "RenderType"="Opaque" }
    13.            CGPROGRAM
    14.            #pragma vertex vert
    15.            #pragma fragment frag
    16.            #include "UnityCG.cginc"
    17.            sampler2D _MainTex;
    18.            struct v2f {
    19.                float4 pos : SV_POSITION;
    20.                half2 uv : TEXCOORD0;
    21.            };
    22.            v2f vert(appdata_base v) {
    23.                v2f o;
    24.                v.vertex.x += sign(v.vertex.x) * sin(_Time.w)/50;
    25.                v.vertex.y += sign(v.vertex.y) * cos(_Time.w)/50;
    26.                o.pos = UnityObjectToClipPos(v.vertex);
    27.                o.uv = v.texcoord;
    28.                return o;
    29.            }
    30.            half4 frag(v2f i) : COLOR {
    31.                half4 c = tex2D(_MainTex, i.uv);
    32.                return c;
    33.            }
    34.             ENDCG
    35.         }
    36.  
    37.         Pass {
    38.             Name "OUTLINE"
    39.             Tags { "LightMode" = "Always" }
    40.             Cull Front
    41.             ZWrite On
    42.             ColorMask RGB
    43.             Blend SrcAlpha OneMinusSrcAlpha
    44.  
    45.             CGPROGRAM
    46.             #pragma vertex vert
    47.             #pragma fragment frag
    48.             #pragma multi_compile_fog
    49.             #include "UnityCG.cginc"
    50.  
    51.             struct appdata {
    52.                 float4 vertex : POSITION;
    53.                 float3 normal : NORMAL;
    54.             };
    55.  
    56.             struct v2f {
    57.                 float4 pos : SV_POSITION;
    58.                 UNITY_FOG_COORDS(0)
    59.                 fixed4 color : COLOR;
    60.             };
    61.    
    62.             uniform float _Outline;
    63.             uniform float4 _OutlineColor;
    64.    
    65.             v2f vert(appdata v) {
    66.                 v2f o;
    67.                 v.vertex.x += sign(v.vertex.x) * sin(_Time.w)/50;
    68.                 v.vertex.y += sign(v.vertex.y) * cos(_Time.w)/50;
    69.                 o.pos = UnityObjectToClipPos(v.vertex);
    70.  
    71.                 float3 norm   = normalize(mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal));
    72.                 float2 offset = TransformViewToProjection(norm.xy);
    73.  
    74.                 #ifdef UNITY_Z_0_FAR_FROM_CLIPSPACE //to handle recent standard asset package on older version of unity (before 5.5)
    75.                     o.pos.xy += offset * UNITY_Z_0_FAR_FROM_CLIPSPACE(o.pos.z) * _Outline;
    76.                 #else
    77.                     o.pos.xy += offset * o.pos.z * _Outline;
    78.                 #endif
    79.                 o.color = _OutlineColor;
    80.                 UNITY_TRANSFER_FOG(o,o.pos);
    81.                 return o;
    82.             }
    83.  
    84.             fixed4 frag(v2f i) : SV_Target
    85.             {
    86.                 UNITY_APPLY_FOG(i.fogCoord, i.color);
    87.                 return i.color;
    88.             }
    89.             ENDCG
    90.         }
    91.     }
    92.    Fallback "Custom/Jelly"
    93. }
     
    TickleDucks likes this.