Search Unity

Resolved Correctly calculating the depth offset for a full screen quad

Discussion in 'Shaders' started by b1gry4n, Sep 9, 2020.

  1. b1gry4n

    b1gry4n

    Joined:
    Sep 11, 2013
    Posts:
    146
    I am drawing a full screen quad to render a full screen effect to. In order for it to be affected by lighting I need to offset its depth into the scene. I can do this using a depth sample and depth offset. The green plant looking stuff is the thing I am projecting into the scene from the quad. This looks great if you are looking directly at the object being affected by shadows, but as soon as the object reaches the edges of the screen the shadows start to warp.



    I suspect I know why...first let me explain the setup in a bit more detail with the image below. the last depth offset parameter in the shader editor is so that I can slightly lower the depth so the pixel sits just above the surface. This is because it is a forward pass, so the lighting has already been calculated. If the pixel is pushed too far it appears under the geometry its being projected towards and receives shadows.




    Heres what I think is happening. The depth value obtained is in eyespace, so when that value is applied as an offset it is pushing the pixels at the borders of the screen much less than the center. That makes sense because I am subtracting the near plane. The depth value for the pixels at the border of the screen are higher than the pixels in the center due to perspective.



    I am confused about how to solve this issue. Would I try to find the value marked orange and subtract it from the depth value? If so how would I go about figuring out what that value is? Would projecting the depth as orthographic solve this? What parameters would I use in place of the orthographic projection parameters to cast from the quads space rather than camera space? Any ideas or suggestions would be helpful.
     
    Last edited: Sep 9, 2020
  2. b1gry4n

    b1gry4n

    Joined:
    Sep 11, 2013
    Posts:
    146
    I posted too soon. After looking around online I came across this great video and was able to solve for the orange marked area, which I subtracted from the depth, and use that as the offset.

     
    Last edited: Sep 9, 2020
  3. KjamesUnity

    KjamesUnity

    Joined:
    May 17, 2017
    Posts:
    1
    How did you end up resolving this? I have a similar issue