Robinseibold / Unity-URP-Outlines

A custom renderer feature for screen space outlines
MIT License
590 stars 51 forks source link

I implemented fog-affected outlines, and here's my advice for you #11

Open Kermalis opened 2 years ago

Kermalis commented 2 years ago

Hello, I wasn't sure where/how else to suggest this technique, but it worked for me.

My outline implementation originally used yours as a base, but mine has evolved with a different algorithm and lots is different from yours. So I'm not using the sobel algorithm, but that doesn't matter for the fog (it would only matter for depth-based outline width)

With the code below, you can add fog to any screen blit. You just need to convert the linear scene depth value to clip space. Then you can use the regular URP fog shader .hlsl functions with a Custom Function Node in your shader graph.

I wrote about it here: https://forum.unity.com/threads/get-clip-space-z-value-from-depth-sample-applying-fog-using-only-a-scene-depth-sample-urp.1344740/

TL;DR: You can use the functions in that link above and apply fog during any screen blit that can access the depth texture.

You will get this artifact: image

This is because the outlines are still using the depth of the object that was originally at that pixel (which in the above case, is the floor). To fix it, you need to instead grab the closest (min()) depth of the center pixel and the neighbor pixel you're checking in the algorithm. Then your outlines use proper fog colors.

image

EDIT: And I forgot to add, feel free to reach out to me about this if you're interested. Your videos are really amazing and I wanted to give back. And I also forgot to add that the depth sampling to fix the fog colors will differ for ortho vs perspective, if you're using eye depth only. You can steal the shader code from the Scene Depth Node source code to fix orthographic if you use eye depth. If you are using linear01 or raw, you don't need to worry about ortho :)

EDIT2: Actually Ortho isn't working with the GetClipZ_01() method, so I'll be looking into that now

Robinseibold commented 2 years ago

Nice work! Will definitely have use of this when it's time to add fog to the outlines! ❤

Kermalis commented 2 years ago

I'm doing it with a different method now, since my implementation needed ortho and perspective, but the concept is the same.

There is a method called Linear01DepthFromNear() which does a similar functionality to that GetClipZ_01() method I found, but it's bugged with DX11/Vulkan etc (any reversed Z graphics api), so I filed a bug report for now. I can share my new method for ortho if you're interested, but you can use the one in my original post still for perspective. Hopefully it helps you or anyone reading this