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

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:

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.

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?

Under the Graphics settings they have overrides for most of the built-in shaders.

I’m using Unity 5.5.1f1 and I am afraid that it is missing the Depth + Normals shader:

I can’t remember if they fixed that in 5.5.2 or not. :confused: It’s still in the settings file itself I think, just not exposed in the UI. :roll_eyes:

1 Like

Okay, good to know. Thank you. :slight_smile:

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).

2 Likes