Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Edge detection using normals & depth issue

Discussion in 'Shaders' started by ZerinLabs, Nov 13, 2020.

  1. ZerinLabs

    ZerinLabs

    Joined:
    Mar 8, 2019
    Posts:
    32
    Hi there!

    I'm creating and edge detetcion post process fx using shader-graphs custom node (so, I'm using mainly code instead of nodes...)

    My main issue is that, the shader works well on the editor, however, on the game view it displays some strange "horizontal lines".
    Due their disposition and distribution, I pressume they are related to the mipmap of one of the two main textures I'm using on this technique to detect the edges (The scene depth buffer AND the scene screen-normal buffer)

    Here you have some screenshots to clarify what I mean...

    Editor View (all good so far...)
    upload_2020-11-13_11-7-19.png

    Scene view... (note the "horizontal" lines)
    upload_2020-11-13_11-8-58.png

    Does anyone have an idea about how can I get rid of thoselines?

    At fist I was thinking about using texture LOD to set manually the mipmapping.
    However, changing these:
    float4 NM1 = SAMPLE_TEXTURE2D( ScreenNormals, SS, UV1);​
    by these
    float4 NM1 = SAMPLE_TEXTURE2D_LOD( ScreenNormals, SS, UV1, 4);​

    didn't have any noticeable effect on the result...

    thnx in advance!



    EDIT: I isolated some parts of the shader and I know for sure that the lines are causd by the screen-normal texture... but still don't know how to solve it :p
     

    Attached Files:

  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,236
    Neither the camera depth texture or the camera depth normal texture have mip maps. And even if they did, since they’re a full screen texture being sampled in screen space the mip maps would never be used as the texture dimensions match the screen resolution.

    The issue is the “screen normals” texture is the “camera depth normal texture”, note “depth” and “normal” are mentioned there, because that texture is storing both. The RG BA channels are storing the linear 01 depth (0.0 near plane, 1.0 far plane) in a low precision encoding, and the BA RG channels are storing the view space normal with stereographic projection. The actual encoding doesn’t actually matter too much. But what does matter is usually you never want to use the depth from the depth normal texture, especially not in it’s raw form. Try outputting the raw color value you get in NM1 and you’ll see those same “stripes” in the depth color. So when doing the normal comparison for outlines you should only be using the BA RG channels and ignoring RG BA.
     
    Last edited: Nov 13, 2020
    bb8_1 likes this.
  3. ZerinLabs

    ZerinLabs

    Joined:
    Mar 8, 2019
    Posts:
    32
    Hi Bgolus, and thanks for the tip!
    I will try that :)
     
  4. ZerinLabs

    ZerinLabs

    Joined:
    Mar 8, 2019
    Posts:
    32
    Excellent Bgolus it worked like a charm!

    BTW, the Normal is encoded on the RG and the depth on the BA

    anyway, thanks a lot!!
     
    bb8_1 and bgolus like this.
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,236
    Whoops! Glad you caught that. :D