Hi everyone! I’m trying to implement a static shadow in my custom shader. It works, but when i’m moving/rotating a camera, shadows flickering sometimes. If I turn off/delete light source shadow working propertly.
What I’m doing wrong?
Shader "Custom/Alt_Standart_V3"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
#pragma multi_compile SHADOWS_SHADOWMASK
#include "UnityCG.cginc"
#include "UnityShadowLibrary.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float2 uv_1 : TEXCOORD1;
};
struct v2f
{
float2 uv : TEXCOORD0;
float2 uv_1 : TEXCOORD1;
UNITY_FOG_COORDS(2)
float4 vertex : SV_POSITION;
float4 wldPos : TEXCOORD3;
//UNITY_SHADOW_COORDS(4)
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
UNITY_INITIALIZE_OUTPUT(v2f, o);
o.vertex = UnityObjectToClipPos(v.vertex);
o.wldPos = mul(unity_ObjectToWorld, v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
#ifndef LIGHTMAP_OFF
o.uv_1 = v.uv_1 * unity_LightmapST.xy + unity_LightmapST.zw;
#endif
UNITY_TRANSFER_FOG(o,o.vertex);
//UNITY_TRANSFER_SHADOW(o);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
#if defined (SHADOWS_SHADOWMASK)
#ifndef LIGHTMAP_OFF
half4 bakedColorTex = UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv_1);
//half shadowMaskAttenuation = UNITY_SAMPLE_TEX2D(unity_ShadowMask, i.uv_1);
half shadowMaskAttenuation = UnitySampleBakedOcclusion(i.uv_1, i.vertex);
half3 bakedColor = DecodeLightmap(bakedColorTex);
col.rgb *= bakedColor;
col.rgb *= shadowMaskAttenuation;
#endif
#endif
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}