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 not working properly on geometry shader

Discussion in 'Shaders' started by KorallTheCoral, Jan 30, 2021.

  1. KorallTheCoral

    KorallTheCoral

    Joined:
    Jan 30, 2021
    Posts:
    2
    Hey, so I'm kind of new to geometry shaders. I've been experimenting with this grass shader, and I've gotten shadows to work, sort of.
    upload_2021-1-30_20-51-27.png
    As you can see, shadows go "through" the geometry.
    upload_2021-1-30_20-52-32.png

    In my geometry shader I do this.
    Code (CSharp):
    1. TRANSFER_SHADOW(o);
    Where o is a custom struct geometryOutput
    Code (CSharp):
    1.         struct geometryOutput {
    2.             float4 pos : SV_POSITION;
    3.             float3 uv : TEXCOORD0;
    4.             float3 pos_ws : TEXCOORD1;
    5.             unityShadowCoord4 _ShadowCoord : TEXCOORD2;
    6.         };
    And then in my fragment shader I sample it like so
    Code (CSharp):
    1.             float atten = SHADOW_ATTENUATION(i) + 1;
    2.             atten /= 2;
    I have run into this issue before when working with unlit shaders, but that was solved by using a fallback to vertexLit (So I'm assuming something is missing here that "writes" to the shadows, so to speak)

    I have tried implementing a ShadowCollector pass, but that hasn't given any effect. Altough that might be because I don't really understand ShadowCollector passes and haven't found any resources regarding them.

    So.. is the ShadowCollector pass what's needed here, or is there something else?
     
  2. KorallTheCoral

    KorallTheCoral

    Joined:
    Jan 30, 2021
    Posts:
    2
    One thing to add. I did sort of solve this issue with another geometry shader I was working on, by calculating the shadow position with the world space position of the base vertex instead of the object space, altough it was an imperfect soloution, as the shading was then done per vertex with leads to some artifacting if you have large triangles.
    So maybe there's something about unity "compressing" object space positions. I honestly have no clue.
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    The explicit “shadow collector” pass got phased out of Unity with Unity 5.0, some 6 years ago. However that’s because the shadow caster pass pulls double duty now as both the shadow caster and effectively the shadow collector pass by way of being used to render the camera depth texture.