Search Unity

Add shadows for lighting only shader

Discussion in 'Shaders' started by Muke24, Nov 21, 2020.

  1. Muke24

    Muke24

    Joined:
    Nov 6, 2019
    Posts:
    7
    How could I combine these two shaders together. So pretty much what the first shader is doing is making the part of an object that is lit by a light visible, and the rest not lit by a light invisible which is what I want. Now, the problem is that when I place another object in front of the light that there is no shadow received from the object in front of the light (the shadow technically just makes that part of the object that is covered by the shadow invisible). I will have the shader code below and pictures for an example (I'm quite new to shaders, and am more used to using amplify or shader graph, but even with those I am not hugely experienced). LightExampleNoShadow.PNG LightExampleNoShadow1.PNG

    Lighting only:

    Code (CSharp):
    1. Shader "Custom/LightingOnlyShader" {
    2.     Properties{
    3.         _Color("Main Color", Color) = (1,1,1,1)
    4.         _MainTex("Base (RGB)", 2D) = "white" {}
    5.     }
    6.  
    7.     SubShader
    8.     {
    9.         Blend SrcAlpha One
    10.         ZWrite Off
    11.         Tags {Queue = Transparent}
    12.         ColorMask RGB
    13.         // Vertex lights
    14.         Pass
    15.         {
    16.             Tags
    17.             {
    18.                 "LightMode" = "Vertex"
    19.             }
    20.             Lighting On
    21.             Material {
    22.             Diffuse[_Color]
    23.         }
    24.  
    25.         SetTexture[_MainTex]
    26.         {
    27.             constantColor[_Color]
    28.             Combine texture * primary DOUBLE, texture * constant
    29.         }
    30.     }
    31. }
    32.  
    33.         Fallback "VertexLit", 2
    34.  
    35. }

    Shadow drawer:

    Code (CSharp):
    1. Shader "Custom/ShadowDrawer"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Shadow Color", Color) = (0, 0, 0, 0.6)
    6.     }
    7.  
    8.         CGINCLUDE
    9.  
    10. #include "UnityCG.cginc"
    11. #include "AutoLight.cginc"
    12.  
    13.         struct v2f_shadow {
    14.         float4 pos : SV_POSITION;
    15.         LIGHTING_COORDS(0, 1)
    16.     };
    17.  
    18.     half4 _Color;
    19.  
    20.     v2f_shadow vert_shadow(appdata_full v)
    21.     {
    22.         v2f_shadow o;
    23.         o.pos = UnityObjectToClipPos(v.vertex);
    24.         TRANSFER_VERTEX_TO_FRAGMENT(o);
    25.         return o;
    26.     }
    27.  
    28.     half4 frag_shadow(v2f_shadow IN) : SV_Target
    29.     {
    30.         half atten = LIGHT_ATTENUATION(IN);
    31.         return half4(_Color.rgb, lerp(_Color.a, 0, atten));
    32.     }
    33.  
    34.         ENDCG
    35.  
    36.         SubShader
    37.     {
    38.         Tags{ "Queue" = "AlphaTest+49" }
    39.  
    40.             // Depth fill pass
    41.             Pass
    42.         {
    43.             ColorMask 0
    44.  
    45.             CGPROGRAM
    46.  
    47.             #pragma vertex vert
    48.             #pragma fragment frag
    49.  
    50.             struct v2f {
    51.                 float4 pos : SV_POSITION;
    52.             };
    53.  
    54.             v2f vert(appdata_full v)
    55.             {
    56.                 v2f o;
    57.                 o.pos = UnityObjectToClipPos(v.vertex);
    58.                 return o;
    59.             }
    60.  
    61.             half4 frag(v2f IN) : SV_Target
    62.             {
    63.                 return (half4)0;
    64.             }
    65.  
    66.             ENDCG
    67.         }
    68.  
    69.             // Forward base pass
    70.                 Pass
    71.             {
    72.                 Tags { "LightMode" = "ForwardBase" }
    73.                 Blend SrcAlpha OneMinusSrcAlpha
    74.                 CGPROGRAM
    75.                 #pragma vertex vert_shadow
    76.                 #pragma fragment frag_shadow
    77.                 #pragma multi_compile_fwdbase
    78.                 ENDCG
    79.             }
    80.  
    81.                 // Forward add pass
    82.                 Pass
    83.             {
    84.                 Tags { "LightMode" = "ForwardAdd" }
    85.                 Blend SrcAlpha OneMinusSrcAlpha
    86.                 CGPROGRAM
    87.                 #pragma vertex vert_shadow
    88.                 #pragma fragment frag_shadow
    89.                 #pragma multi_compile_fwdadd_fullshadows
    90.                 ENDCG
    91.             }
    92.     }
    93.     FallBack "VertexLit"
    94. }
     

    Attached Files: