Search Unity

Shadow Artifacts on Vertex Animation Shader [solved]

Discussion in 'Shaders' started by Friedemann_A, Jul 19, 2018.

  1. Friedemann_A

    Friedemann_A

    Joined:
    May 11, 2017
    Posts:
    11
    Hi everyone,

    I'm trying to write a simple vertex-animation-surface shader. The animation works fine and I managed to add animated shadows by using pragma: addshadow.

    However, the shader still produces strange artifacts:

    shader_artifacts_gif.gif

    I can get rid of them by changing the shader's render queue to "Transparent" but that seems like a hack and also disables shadow-receiving. Any ideas how to fix this?

    Here's my shader code:
    Code (CSharp):
    1. Shader "Custom/AnimateTest" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    6.         _Metallic ("Metallic", Range(0,1)) = 0.0
    7.     }
    8.     SubShader {
    9.         Tags { "RenderType"="Opaque" "DisableBatching" = "True" }
    10.         LOD 200
    11.  
    12.         CGPROGRAM
    13.         // Physically based Standard lighting model, and enable shadows on all light types
    14.         #pragma surface surf Standard vertex:myvert addshadow
    15.  
    16.         // Use shader model 3.0 target, to get nicer looking lighting
    17.         #pragma target 3.0
    18.  
    19.         sampler2D _MainTex;
    20.  
    21.         struct Input {
    22.             float2 uv_MainTex;
    23.         };
    24.  
    25.         half _Glossiness;
    26.         half _Metallic;
    27.         fixed4 _Color;
    28.  
    29.  
    30.  
    31.         void myvert(inout appdata_full v) {
    32.             v.vertex = v.vertex + ((sin(_Time.x * 50.0) + 1.0) * 0.5) * 0.50;
    33.         }
    34.  
    35.         void surf (Input IN, inout SurfaceOutputStandard o) {
    36.             // Albedo comes from a texture tinted by color
    37.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    38.             o.Albedo = c.rgb;
    39.             // Metallic and smoothness come from slider variables
    40.             o.Metallic = _Metallic;
    41.             o.Smoothness = _Glossiness;
    42.             o.Alpha = c.a;
    43.         }
    44.         ENDCG
    45.     }
    46.     FallBack "Diffuse"
    47. }
    Thanks in advance!
     
  2. Deleted User

    Deleted User

    Guest

    You should write a ShadowCaster LightMode.
     
    Friedemann_A likes this.
  3. Friedemann_A

    Friedemann_A

    Joined:
    May 11, 2017
    Posts:
    11
    Thanks for your answer! Would that really help? I'm kind of hesitant to try that since I've never done it before and I thought that pragma:addshadow tells the surface shader to create a ShadowCaster Pass.

    Also, the drop-shadow of the object seems to be cast correctly already.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,336
    The v.vertex is a float4 value, the xyz values of which are the position, but you do not want to modify the w component unless you know exactly what you're doing. For the main forward passes modifying the w component doesn't do anything as the shaders forceably override it to a value of 1.0 (the value it defaults to), but the shadow caster pass used for the camera depth and shadows maps do not, and the side effect of modifying the w component is it scales the object in projection space, which moves it away from the camera on PC and desktop due to the use of a reversed depth buffer. The solution is to only modify the xyz components.

    v.vertex.xyz = v.vertex.xyz + (sin(_Time.y * 2.5) - 1.0) * 0.25;

    (Also _Time.x is game time / 20, use _Time.y instead, which is just straight game time.)
     
    shegway and Friedemann_A like this.
  5. Deleted User

    Deleted User

    Guest

    @bgolus is right
     
    Friedemann_A likes this.
  6. Friedemann_A

    Friedemann_A

    Joined:
    May 11, 2017
    Posts:
    11
    Oh god that’s it, thank you so much! I had figured out that it had something to do with the depth buffer but I was thinking of way to complex stuff. Again, thanks, made my day :)