Search Unity

Custom shader doesn't receive shadow

Discussion in 'Shaders' started by leitz, Apr 13, 2017.

  1. leitz

    leitz

    Joined:
    Oct 25, 2016
    Posts:
    1
    hi !

    i have got a problem receiving shadows with this shader.


    Code (CSharp):
    1. Shader "Custom/TerrainSand"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Main Color", Color) = (1,1,1,0.5)
    6.         _MainTex("Base (RGB)", 2D) = "white" {}
    7.         _MainTex2("Base 2 (RGB)", 2D) = "white" {}
    8.         _Mask("Mix Mask (A)", 2D) = "gray" {}
    9.         _BumpMap("Bumpmap (RGB)", 2D) = "bump" {}
    10.     }
    11.  
    12.     Category
    13.     {
    14.         /* Upgrade NOTE: commented out, possibly part of old style per-pixel lighting: Blend AppSrcAdd AppDstAdd */
    15.         Fog{ Color[_AddFog] }
    16.  
    17.         // ------------------------------------------------------------------
    18.         // ARB fragment program
    19.  
    20.         #warning Upgrade NOTE: SubShader commented out; uses Unity 2.x per-pixel lighting. You should rewrite shader into a Surface Shader.
    21.         SubShader
    22.         {
    23.         // Ambient pass
    24.         Pass{
    25.             Tags{ "LightMode" = "Always" /* Upgrade NOTE: changed from PixelOrNone to Always */ }
    26.             Color[_PPLAmbient]
    27.             SetTexture[_MainTex]{ combine texture }
    28.             SetTexture[_Mask]{ combine previous, texture }
    29.             SetTexture[_MainTex2]{ combine texture lerp(previous) previous }
    30.             SetTexture[_MainTex2]{ constantColor[_Color] combine previous * primary DOUBLE, previous * constant }
    31.         }
    32.  
    33.     // Vertex lights
    34.         Pass
    35.         {
    36.             Tags{ "LightMode" = "ForwardBase" }
    37.             Lighting On
    38.             Material{
    39.             Diffuse[_Color]
    40.             Emission[_PPLAmbient]
    41.         }
    42.  
    43.         SetTexture[_MainTex]{ combine texture }
    44.         SetTexture[_Mask]{ combine previous, texture }
    45.         SetTexture[_MainTex2]{ combine texture lerp(previous) previous }
    46.         SetTexture[_MainTex2]{ constantColor[_Color] combine previous * primary DOUBLE, previous * constant }
    47.     }
    48.  
    49.     // Pixel lights
    50.     Pass
    51.     {
    52.         Name "PPL"
    53.         Tags{ "LightMode" = "Pixel" }
    54.  
    55.         CGPROGRAM
    56.         // Upgrade NOTE: excluded shader from DX11; has structs without semantics (struct v2f members uv,lightDirT)
    57.         #pragma exclude_renderers d3d11
    58.         #pragma vertex vert
    59.         #pragma fragment frag
    60.         #pragma fragmentoption ARB_fog_exp2
    61.         #pragma fragmentoption ARB_precision_hint_fastest
    62.         #pragma multi_compile_builtin
    63.  
    64.         #include "UnityCG.cginc"
    65.         #include "AutoLight.cginc"
    66.  
    67.         struct v2f
    68.         {
    69.             float4 pos : SV_POSITION;
    70.             LIGHTING_COORDS()
    71.             float2  uv[4];
    72.             float3  lightDirT;
    73.  
    74.         };
    75.  
    76.         float4 _MainTex_ST, _MainTex2_ST, _Mask_ST, _BumpMap_ST;
    77.  
    78.         v2f vert(appdata_tan v)
    79.         {
    80.             v2f o;
    81.             o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    82.             o.uv[0] = TRANSFORM_TEX(v.texcoord, _MainTex);
    83.             o.uv[1] = TRANSFORM_TEX(v.texcoord, _MainTex2);
    84.             o.uv[2] = TRANSFORM_TEX(v.texcoord, _Mask);
    85.             o.uv[3] = TRANSFORM_TEX(v.texcoord, _BumpMap);
    86.  
    87.             TANGENT_SPACE_ROTATION;
    88.             o.lightDirT = mul(rotation, ObjSpaceLightDir(v.vertex));
    89.  
    90.             TRANSFER_VERTEX_TO_FRAGMENT(o);
    91.             return o;
    92.         }
    93.  
    94.         uniform sampler2D _MainTex;
    95.         uniform sampler2D _MainTex2;
    96.         uniform sampler2D _Mask;
    97.         uniform sampler2D _BumpMap;
    98.  
    99.         float4 frag(v2f i) : COLOR
    100.         {
    101.             half4 col1 = tex2D(_MainTex,i.uv[0]);
    102.             half4 col2 = tex2D(_MainTex2,i.uv[1]);
    103.             half mix = tex2D(_Mask,i.uv[2]).a;
    104.             half4 texcol = lerp(col1,col2,mix);
    105.  
    106.             // get normal from the normal map
    107.             float3 normal = tex2D(_BumpMap, i.uv[3]).xyz * 2 - 1;
    108.             float atten = LIGHT_ATTENUATION(i);
    109.             return DiffuseLight(i.lightDirT, normal, texcol, atten);
    110.         }
    111.  
    112.         ENDCG
    113.         }
    114.     }
    115.  
    116.     // ------------------------------------------------------------------
    117.     // Four texture cards
    118.  
    119.     SubShader
    120.     {
    121.         // Vertex lit
    122.         Pass
    123.         {
    124.             Lighting On
    125.             Material
    126.             {
    127.                 Diffuse[_Color]
    128.                 Ambient[_Color]
    129.             }
    130.  
    131.             SetTexture[_MainTex]{ combine texture }
    132.             SetTexture[_Mask]{ combine previous, texture }
    133.             SetTexture[_MainTex2]{ combine texture lerp(previous) previous }
    134.             SetTexture[_MainTex2]{ constantColor[_Color] combine previous * primary DOUBLE, previous * constant }
    135.             }
    136.         }
    137.     }
    138.     FallBack "Diffuse"
    139. }
    It does what i need (2 texture on terrain with a mask) but i can't figure out why there is no shadows...

    Can anyone point me to the right direction to solve this problem?

    Thanks for your help and sorry for the "n+1" post about this
     
    Last edited: Apr 13, 2017