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

DepthTextureMode.DepthNormals - How to flip normals for back facing triangles?

Discussion in 'Image Effects' started by BrightBit, Feb 27, 2017.

  1. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    264
    While using Unity's Screen Space Ambient Occlusion Image Effect I noticed some artifacts on my cutout materials. It turns out that the SSAO effect is based on a texture created by the camera that combines depth and normals into one 32 bit texture. However, it seems that the back facing sides of my meshes which are using the cutout materials aren't flipped before they are written into this texture. Can I change that or do you know of some kind of workaround? I know that I could create the back sides manually but I would like to avoid that.



    Here's the normal part of the "depth & normal" render texture that the camera is creating:

     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    The easiest fix is to not use a double sided shader and just double up the geometry.

    Otherwise check the shader forum for "double sided normals" and specially "VFACE". I've made a couple of posts about this specifically. This will fix the normals if you're using deferred rendering for both lighting and SSAO.

    If you're using forward rendering the depth & normal texture used by the SSAO (and other) image effect is generated using a replacement shader pass and you'll have to make a custom version of the shader with a new pass specific for your double sided cutout, and update your shader's RenderType tag to match the custom pass. In this case I'm pretty sure you're using deferred since I don't think the back faces would even show up with out adding a custom pass to begin with.
     
  3. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    264
    Thank you for your fast and detailed answer. Doubling up the geometry would have a great impact on my vertex count so I am trying to avoid that. Currently I am using forward rendering with a custom shadow collector pass where I also disable culling. That's why the back faces show up in the depth & normal texture. I am now thinking about switching to deferred rendering since I will also have a lot of dynamic lights. At first I rejected that due to issues with transparency and fog.

    In the case I want to stick to forward rendering: How would I have to create this custom version of the replacement shader pass? I found the original shader in the builtin shaders archive. It seems to be called "Hidden/Internal-DepthNormalsTexture" but how can I make Unity use my own version of that?
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Under the Graphics settings they have overrides for most of the built-in shaders.
     
  5. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    264
    I'm using Unity 5.5.1f1 and I am afraid that it is missing the Depth + Normals shader:

     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    I can't remember if they fixed that in 5.5.2 or not. :/ It's still in the settings file itself I think, just not exposed in the UI. :rolleyes:
     
    BrightBit likes this.
  7. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    264
    Okay, good to know. Thank you. :)
     
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Checked ... not fixed in 5.5.2, but is in the settings file itself, and is accessible in the editor using the debug inspector (right click on the inspector tab and select debug).
     
    brokenm and BrightBit like this.