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

Question Problem with billboard shadow caster

Discussion in 'Shaders' started by ununion, May 12, 2021.

  1. ununion

    ununion

    Joined:
    Dec 2, 2018
    Posts:
    275
    I want to achieve a shader that has billboard shadow caster"means shadow always face to light include any light which cast shadow"

    here is my code
    Code (CSharp):
    1. Pass{
    2.             Tags {"LightMode" = "ShadowCaster"}
    3.             LOD 100
    4.        
    5.             ztest off
    6.            
    7.             CGPROGRAM
    8.             #pragma vertex vert
    9.             #pragma fragment frag
    10.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
    11.                 #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
    12.  
    13.             struct appdata
    14.             {
    15.                 float4 vertex:POSITION;
    16.                 float2 texcoord:TEXCOORD0;
    17.             };
    18.            
    19.             sampler2D _MainTex;
    20.             sampler2D _BumpMap;
    21.             fixed4 _Color;
    22.             float _MaxHeight;
    23.             float _XScale;
    24.  
    25.             struct v2f {
    26.                 float4 vertex:SV_POSITION;
    27.                 float2 texcoord:TEXCOORD0;
    28.             };
    29.            
    30.             v2f vert(appdata v){
    31.                 v2f o;
    32.                 float3 forward=mul(unity_WorldToObject,_WorldSpaceLightPos0.xyz).xyz;
    33.                 forward.y=0;
    34.                 forward=normalize(forward);
    35.                 float3 up=float3(0,1,0);
    36.                 float3 right=normalize(cross(forward,up));
    37.                 up=normalize(cross(right,forward));
    38.  
    39.                 float sc=lerp(0,_XScale,v.vertex.y/_MaxHeight);
    40.  
    41.                 float3 vertex= v.vertex.x*right+v.vertex.y*up;
    42.  
    43.                 o.vertex=UnityObjectToClipPos(vertex);
    44.                 o.texcoord=v.texcoord;
    45.                 return o;
    46.  
    47.             }
    48.             float4 frag(v2f i):SV_Target
    49.             {
    50.                 float4 c=tex2D(_MainTex,i.texcoord);
    51.                 clip(c.a-0.8);
    52.                 return c;
    53.             }
    54.            
    55.             ENDCG
    56.         }
    in standard render pipeline the effect is correct like this img
    upload_2021-5-12_9-17-8.png
    both of the directional light and spot light work very well with correct billboard shadow.


    but if in urp,the spot light shadow will be wrong like this
    upload_2021-5-12_9-18-46.png
    as you can see,the spot light shadow also cast by sunlight billboard.

    so how can i fix it.thanks
     
  2. ununion

    ununion

    Joined:
    Dec 2, 2018
    Posts:
    275
    hope my idol @bgolus can help me.
     
  3. ununion

    ununion

    Joined:
    Dec 2, 2018
    Posts:
    275
    Code (CSharp):
    1. float3 forward=float3(1,1,1);
    2. #if _DIRECTIONAL
    3.                 forward= mul(unity_WorldToObject,_LightDirection).xyz;
    4. #else
    5.                 Light l=GetAdditionalLight(_ShadowLightIndex,mul(unity_WorldToObject,v.vertex));
    6.                 forward=mul(unity_WorldToObject,l.direction).xyz;
    7. #endif
    i try this,but still not work...