I've been trying to add shadow to my flag that animated In my vertex shader but after adding shadow , my shadow is fixed and does not follow the animation of the flag.
for adding shadow I use below link:
Diffuse has a fallback of vertex lit, which contains within it two passes; Shadow Caster and Shadow Receiver. These are the two passes that allow the shader to cast and receive shadows. http://polycount.com/discussion/131260/how-to-get-custom-shaders-to-recieve-shadows
my shader is here:
Shader "Custom/Flag" {
Properties {
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Speed ("Speed", Range(0, 5.0)) = 1
_Frequency ("Frequency", Range(0, 1.3)) = 1
_Amplitude ("Amplitude", Range(0, 5.0)) = 1
}
SubShader {
Tags { "RenderType"="Opaque" "LightMode"="ForwardBase" }
Cull off
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase
#include "UnityCG.cginc"
#include "AutoLight.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
LIGHTING_COORDS(1, 2)
};
float _Speed;
float _Frequency;
float _Amplitude;
v2f vert(appdata_base v)
{
v2f o;
v.vertex.y += cos((v.vertex.x + _Time.y * _Speed) * _Frequency) * _Amplitude * (v.vertex.x - 5);
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
TRANSFER_VERTEX_TO_FRAGMENT(o);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
return tex2D(_MainTex, i.uv);
}
ENDCG
}
}
FallBack "VertexLit"
}
Finally I use "LightMode"="ShadowCaster"
but this Tag prevents rendering my flag like this problem.A solution came to my mind that works correctly "using two pass for implementing shadows: 1.normal pass 2.ShadowCaster pass" but I think It's not optimize.
this is my solution as Pseudo Code:
SubShader {
Tags { "RenderType"="Opaque" }
Cull off
Pass {
CGPROGRAM
Shader "Custom/Flag" // normal Pass for animating flag
ENDCG
}
Pass {
Tags { "LightMode"="ShadowCaster" }
CGPROGRAM
Shader "Custom/Flag" //ShadowCaster Pass for recieving shadow
ENDCG
}
}
FallBack "VertexLit"
}
In short I finding a way for adding shadows for vertex animated objects In optimized way.