Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Combining 2 shaders into 1.

Discussion in 'Shaders' started by zreek17, Nov 20, 2014.

  1. zreek17

    zreek17

    Joined:
    Jun 2, 2013
    Posts:
    21
    Hello, my friends!
    I've got a necessity to combine a Nature Tree SoftOcclusionLeaves shader and Curve shader, in order to have bending platforms in my runner game. Here they are:

    Leaves shader
    Code (CSharp):
    1. Shader "Nature/Tree Soft Occlusion Leaves" {
    2.          Properties {
    3.                  _Color ("Main Color", Color) = (1,1,1,1)
    4.                  _MainTex ("Main Texture", 2D) = "white" {  }
    5.                  _Cutoff ("Alpha cutoff", Range(0.25,0.9)) = 0.5
    6.                  _BaseLight ("Base Light", Range(0, 1)) = 0.35
    7.                  _AO ("Amb. Occlusion", Range(0, 10)) = 2.4
    8.                  _Occlusion ("Dir Occlusion", Range(0, 20)) = 7.5
    9.                
    10.                  // These are here only to provide default values
    11.                  _Scale ("Scale", Vector) = (1,1,1,1)
    12.                  _SquashAmount ("Squash", Float) = 1
    13.          }
    14.        
    15.          SubShader {
    16.                  Tags {
    17.                          "Queue" = "Transparent-99"
    18.                          "IgnoreProjector"="True"
    19.                          "RenderType" = "TreeTransparentCutout"
    20.                  }
    21.                  Cull Off
    22.                  ColorMask RGB
    23.                
    24.                  Pass {
    25.                          Lighting On
    26.                
    27.                          CGPROGRAM
    28.                          #pragma vertex leaves
    29.                          #pragma fragment frag
    30.                          #include "SH_Vertex.cginc"
    31.                        
    32.                          sampler2D _MainTex;
    33.                          fixed _Cutoff;
    34.                        
    35.                          fixed4 frag(v2f input) : COLOR
    36.                          {
    37.                                  fixed4 c = tex2D( _MainTex, input.uv.xy);
    38.                                  c.rgb *= 2.0f * input.color.rgb;
    39.                                
    40.                                  clip (c.a - _Cutoff);
    41.                                
    42.                                  return c;
    43.                          }
    44.                          ENDCG
    45.                  }
    46.                
    47.                  Pass {
    48.                          Name "ShadowCaster"
    49.                          Tags { "LightMode" = "ShadowCaster" }
    50.                        
    51.                          Fog {Mode Off}
    52.                          ZWrite On ZTest Less Cull Off
    53.                          Offset 1, 1
    54.        
    55.                          CGPROGRAM
    56.                          #pragma vertex vert
    57.                          #pragma fragment frag
    58.                          #pragma fragmentoption ARB_precision_hint_fastest
    59.                          #pragma multi_compile_shadowcaster
    60.                          #include "UnityCG.cginc"
    61.                          #include "TerrainEngine.cginc"
    62.                        
    63.                          struct v2f {
    64.                                  V2F_SHADOW_CASTER;
    65.                                  float2 uv : TEXCOORD1;
    66.                          };
    67.                        
    68.                          struct appdata {
    69.                              float4 vertex : POSITION;
    70.                              fixed4 color : COLOR;
    71.                              float4 texcoord : TEXCOORD0;
    72.                          };
    73.                          v2f vert( appdata v )
    74.                          {
    75.                                  v2f o;
    76.                                  TerrainAnimateTree(v.vertex, v.color.w);
    77.                                  TRANSFER_SHADOW_CASTER(o)
    78.                                  o.uv = v.texcoord;
    79.                                  return o;
    80.                          }
    81.                        
    82.                          sampler2D _MainTex;
    83.                          fixed _Cutoff;
    84.                                        
    85.                          float4 frag( v2f i ) : COLOR
    86.                          {
    87.                                  fixed4 texcol = tex2D( _MainTex, i.uv );
    88.                                  clip( texcol.a - _Cutoff );
    89.                                  SHADOW_CASTER_FRAGMENT(i)
    90.                          }
    91.                          ENDCG  
    92.                  }
    93.          }
    94.        
    95.          SubShader {
    96.                  Tags {
    97.                          "Queue" = "Transparent-99"
    98.                          "IgnoreProjector"="True"
    99.                          "RenderType" = "TreeTransparentCutout"
    100.                  }
    101.                  Cull Off
    102.                  ColorMask RGB
    103.                
    104.                  Pass {
    105.                          CGPROGRAM
    106.                          #pragma exclude_renderers gles xbox360 ps3
    107.                          #pragma vertex leaves
    108.                          #include "SH_Vertex.cginc"
    109.                          ENDCG
    110.  
    111.                          Lighting On
    112.                          AlphaTest GEqual [_Cutoff]
    113.                          ZWrite On
    114.                        
    115.                          SetTexture [_MainTex] { combine primary * texture DOUBLE, texture }
    116.                  }
    117.          }
    118.        
    119.        
    120.          SubShader {
    121.                  Tags {
    122.                          "Queue" = "Transparent-99"
    123.                          "IgnoreProjector"="True"
    124.                          "RenderType" = "TransparentCutout"
    125.                  }
    126.                  Cull Off
    127.                  ColorMask RGB
    128.                  Pass {
    129.                          Tags { "LightMode" = "Vertex" }
    130.                          AlphaTest GEqual [_Cutoff]
    131.                          Lighting On
    132.                          Material {
    133.                                  Diffuse [_Color]
    134.                                  Ambient [_Color]
    135.                          }
    136.                          SetTexture [_MainTex] { combine primary * texture DOUBLE, texture }
    137.                  }              
    138.          }
    139.  
    140.  
    141.  
    142.          Dependency "BillboardShader" = "Hidden/Nature/Tree Soft Occlusion Leaves Rendertex"
    143.          Fallback Off
    144. }
    and Curve shader:

    Code (CSharp):
    1.  
    2.  
    3. SubShader {
    4.       Tags { "Queue" = "Transparent"}
    5.       Pass
    6.       {
    7. 5.        
    8.           Blend SrcAlpha OneMinusSrcAlpha
    9.           CGPROGRAM
    10.           #pragma vertex vert
    11.           #pragma fragment frag
    12. 10.         #include "UnityCG.cginc"
    13.          
    14.          
    15.  
    16.           sampler2D _MainTex;
    17. 15.         float4 _QOffset;
    18.           float _Dist;
    19.           float _Brightness;
    20.          
    21.           struct v2f {
    22. 20.             float4 pos : SV_POSITION;
    23.               float4 uv : TEXCOORD0;
    24.               float3 viewDir : TEXCOORD1;
    25.               fixed4 color : COLOR;
    26.           };
    27. 25.
    28.           v2f vert (appdata_full v)
    29.           {
    30.              v2f o;
    31.              float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
    32. 30.            float zOff = vPos.z/_Dist;
    33.              vPos += _QOffset*zOff*zOff;
    34.              o.pos = mul (UNITY_MATRIX_P, vPos);
    35.              o.uv = v.texcoord;
    36.              return o;
    37. 35.         }
    38.           half4 frag (v2f i) : COLOR0
    39.           {
    40.                 half4 col = tex2D(_MainTex, i.uv.xy);
    41.                 col *= UNITY_LIGHTMODEL_AMBIENT*_Brightness;
    42. 40.             return col;
    43.           }
    44.           ENDCG
    45.       }
    46.   }
    47. 45.
    48.   FallBack "Diffuse"
    49.  
    50.  
    51. }
    52.  
    53.  
    54.  
    But the problem is, that when I copied one to another, that didn't work. Can you tell me please, how couldI combine them?
     
  2. zreek17

    zreek17

    Joined:
    Jun 2, 2013
    Posts:
    21
    Guys, do U know, is it possible? And how could I do that correctly?