Search Unity

Is it easy to add shadows (casting and receiving) to a shader?

Discussion in 'Shaders' started by Divinitize1, Aug 7, 2019.

  1. Divinitize1

    Divinitize1

    Joined:
    May 27, 2019
    Posts:
    101
    Basically, I have a project which is quite far in development.
    I'm using the free toon shader on the asset store and would love to try and see how shadows would look along with it.
    I know there are other toon shaders out there with shadows already but every time I try a new one the original colours etc I had chosen are changed and would take me a day to go through and set them all backup.

    Not sure if it's easy or not so excuse my ignorance.

    Code (CSharp):
    1. Shader "TSF/Base1"
    2. {
    3.     Properties
    4.     {
    5.         [MaterialToggle(_TEX_ON)] _DetailTex ("Enable Detail texture", Float) = 0     //1
    6.         _MainTex ("Detail", 2D) = "white" {}                                        //2
    7.         _ToonShade ("Shade", 2D) = "white" {}                                          //3
    8.         [MaterialToggle(_COLOR_ON)] _TintColor ("Enable Color Tint", Float) = 0     //4
    9.         _Color ("Base Color", Color) = (1,1,1,1)                                    //5  
    10.         [MaterialToggle(_VCOLOR_ON)] _VertexColor ("Enable Vertex Color", Float) = 0//6      
    11.         _Brightness ("Brightness 1 = neutral", Float) = 1.0                            //7  
    12.     }
    13.  
    14.     Subshader
    15.     {
    16.         Tags { "RenderType"="Opaque" }
    17.         LOD 250
    18.         ZWrite On
    19.            Cull Back
    20.         Lighting Off
    21.         Fog { Mode Off }
    22.        
    23.         Pass
    24.         {
    25.             Name "BASE"
    26.             CGPROGRAM
    27.                 #pragma vertex vert
    28.                 #pragma fragment frag
    29.                 #pragma fragmentoption ARB_precision_hint_fastest
    30.                 #include "UnityCG.cginc"
    31.                 #pragma glsl_no_auto_normalization
    32.                 #pragma multi_compile _TEX_OFF _TEX_ON
    33.                 #pragma multi_compile _COLOR_OFF _COLOR_ON
    34.  
    35.                
    36.                 #if _TEX_ON
    37.                 sampler2D _MainTex;
    38.                 half4 _MainTex_ST;
    39.                 #endif
    40.                
    41.                 struct appdata_base0
    42.                 {
    43.                     float4 vertex : POSITION;
    44.                     float3 normal : NORMAL;
    45.                     float4 texcoord : TEXCOORD0;
    46.                 };
    47.                
    48.                  struct v2f
    49.                  {
    50.                     float4 pos : SV_POSITION;
    51.                     #if _TEX_ON
    52.                     half2 uv : TEXCOORD0;
    53.                     #endif
    54.                     half2 uvn : TEXCOORD1;
    55.                  };
    56.              
    57.                 v2f vert (appdata_base0 v)
    58.                 {
    59.                     v2f o;
    60.                     o.pos = UnityObjectToClipPos ( v.vertex );
    61.                     float3 normalP = normalize(v.normal);
    62.                     float3 n = UnityObjectToWorldNormal(normalP);
    63.                     float2 uvM = mul((float3x3)UNITY_MATRIX_V, n).xy;
    64.                     uvM = ( uvM * float2(0.5, 0.5) ) + float2(0.5, 0.5);
    65.                     o.uvn = uvM;
    66.                      #if _TEX_ON
    67.                     o.uv = TRANSFORM_TEX ( v.texcoord, _MainTex );
    68.                     #endif
    69.                     return o;
    70.                 }
    71.  
    72.                   sampler2D _ToonShade;
    73.                 fixed _Brightness;
    74.                
    75.                 #if _COLOR_ON
    76.                 fixed4 _Color;
    77.                 #endif
    78.                
    79.                 fixed4 frag (v2f i) : COLOR
    80.                 {
    81.                     #if _COLOR_ON
    82.                     fixed4 toonShade = tex2D( _ToonShade, i.uvn )*_Color;
    83.                     #else
    84.                     fixed4 toonShade = tex2D( _ToonShade, i.uvn );
    85.                     #endif
    86.                    
    87.                     #if _TEX_ON
    88.                     fixed4 detail = tex2D ( _MainTex, i.uv );
    89.                     return  toonShade * detail*_Brightness;
    90.                     #else
    91.                     return  toonShade * _Brightness;
    92.                     #endif
    93.                 }
    94.             ENDCG
    95.         }
    96.     }
    97.     Fallback "Legacy Shaders/Diffuse"
    98. }
     

    Attached Files: