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

Shadows on displaced vert/frag shader only show up with certain camera angles

Discussion in 'Shaders' started by Skotrap7, Oct 10, 2020.

  1. Skotrap7

    Skotrap7

    Joined:
    May 24, 2018
    Posts:
    125
    Hi, I'm pretty new at understanding shaders. I've been following along with the shader examples and checking out other demos/tutorials. I'm trying to build a shader that does vertex displacement, and I have been able to get everything working how I like it in the editor, with one exception. When I rotate my camera in scene view above a certain angle, the shadows disappear and everything goes matte.

    After intense debugging and failed attempts at making any progress, and massive amounts of googling, I think I found the answer to my problem, but I can't tell for certain. Here is the post that gave me the "aha" moment and I'm hoping someone can confirm my suspicions:

    https://forum.unity.com/threads/incorrect-shadows-with-vertex-shader-displacement.500976/

    The mesh that I am using for my shader while I work on it is just a plane, and if I understand this post, the shadows are disappearing because the mesh, being a plane, has a planar bounds, which means I am displacing the verticies outside the bounds of the shadowmap.

    Does that sound right? And, if so, does that means that even if I applied this shader to a different mesh form, I could experience the same issue if the vertex gets displaced beyond the bounding box of the mesh?

    Here is are some screen shots of what I'm seeing happen:

    Shadows look nice:
    upload_2020-10-10_12-49-18.png

    Slight tilt of the camera in scene view:

    upload_2020-10-10_12-49-24.png
     
  2. Skotrap7

    Skotrap7

    Joined:
    May 24, 2018
    Posts:
    125
    Hmm, I'm thinking that this might not be the issue after all. I tried changing the bounds of the mesh to be large enough to contain the whole displaced area and it is still doing this. My shader code is just the "Lit/Diffuse With Shadows" one from the examples with vertex displacement added to the vert function and to the shadowcaster following this post.

    The vertex displacement in my vert function is done by sampling a noise texture.

    Code (CSharp):
    1.  
    2. float height = tex2Dlod(_NoiseTex, float4(v.texcoord.xy, 0, 0)) * _ScaleFactor;
    3. float3 vertexPosition = float3(0, height, 0);
    4. v.vertex.xyz += mul(unity_WorldToObject, vertexPosition);
    5.  
    And my shadowcaster vert function:

    Code (CSharp):
    1.  
    2. float height = tex2Dlod(_NoiseTex, float4(v.texcoord.xy, 0, 0)) * _ScaleFactor;
    3. float3 vertexPosition = float3(0, height, 0);
    4. v.vertex = mul(unity_WorldToObject, float4(vertexPosition, 1));
    5. TRANSFER_SHADOW_CASTER_NORMALOFFSET(o);
    6.  
    Everything else is just the same as the example shader "Lit/Diffuse With Shadows"

    I'm stuck on thinking it has to do with the plane mesh, or something. Any help appreciated.