Search Unity

Static shadow flickering in custom v2f shader.

Discussion in 'Shaders' started by RZR_SRT, Nov 4, 2019.

  1. RZR_SRT

    RZR_SRT

    Joined:
    Dec 18, 2012
    Posts:
    18
    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?

    Code (CSharp):
    1. Shader "Custom/Alt_Standart_V3"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 100
    11.  
    12.         Pass
    13.         {
    14.             CGPROGRAM
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.             // make fog work
    18.             #pragma multi_compile_fog
    19.             #pragma fragmentoption ARB_precision_hint_fastest
    20.             #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
    21.             #pragma multi_compile SHADOWS_SHADOWMASK
    22.  
    23.             #include "UnityCG.cginc"
    24.         #include "UnityShadowLibrary.cginc"
    25.          
    26.          
    27.  
    28.             struct appdata
    29.             {
    30.                 float4 vertex : POSITION;
    31.                 float2 uv : TEXCOORD0;
    32.                 float2 uv_1 : TEXCOORD1;
    33.             };
    34.  
    35.             struct v2f
    36.             {
    37.                 float2 uv : TEXCOORD0;
    38.                 float2 uv_1 : TEXCOORD1;
    39.                 UNITY_FOG_COORDS(2)
    40.                 float4 vertex : SV_POSITION;
    41.                 float4 wldPos : TEXCOORD3;
    42.                 //UNITY_SHADOW_COORDS(4)
    43.              
    44.             };
    45.  
    46.             sampler2D _MainTex;
    47.             float4 _MainTex_ST;
    48.  
    49.             v2f vert (appdata v)
    50.             {
    51.                 v2f o;
    52.                 UNITY_INITIALIZE_OUTPUT(v2f, o);
    53.                 o.vertex = UnityObjectToClipPos(v.vertex);
    54.                 o.wldPos = mul(unity_ObjectToWorld, v.vertex);
    55.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    56.  
    57.                 #ifndef LIGHTMAP_OFF
    58.                 o.uv_1 = v.uv_1 * unity_LightmapST.xy + unity_LightmapST.zw;
    59.                 #endif
    60.  
    61.                 UNITY_TRANSFER_FOG(o,o.vertex);
    62.                 //UNITY_TRANSFER_SHADOW(o);
    63.                
    64.                 return o;
    65.             }
    66.  
    67.             fixed4 frag (v2f i) : SV_Target
    68.             {
    69.                 // sample the texture
    70.                 fixed4 col = tex2D(_MainTex, i.uv);
    71.                 #if defined (SHADOWS_SHADOWMASK)
    72.                 #ifndef LIGHTMAP_OFF
    73.                 half4 bakedColorTex = UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv_1);
    74.                 //half shadowMaskAttenuation = UNITY_SAMPLE_TEX2D(unity_ShadowMask, i.uv_1);
    75.                 half shadowMaskAttenuation = UnitySampleBakedOcclusion(i.uv_1, i.vertex);
    76.                 half3 bakedColor = DecodeLightmap(bakedColorTex);
    77.                 col.rgb *= bakedColor;
    78.                 col.rgb *=  shadowMaskAttenuation;
    79.              
    80.                 #endif
    81.                 #endif
    82.              
    83.                // apply fog
    84.                 UNITY_APPLY_FOG(i.fogCoord, col);
    85.                 return col;
    86.             }
    87.             ENDCG
    88.         }
    89.     }
    90. }
     
    Last edited: Nov 4, 2019
  2. RZR_SRT

    RZR_SRT

    Joined:
    Dec 18, 2012
    Posts:
    18
    Ok. Maybe exist another way to get baked shadows in custom shader?