I came across this cool shader effect (shader code inside the link). The depth buffer fading effect works well on perspective camera, but not with orthographic camera mode I’m using.
Is there an alternative way to recreate the Y-depth effect on the ortho camera? I couldn’t find anything on Google but I may be using the wrong keywords.
Any ideas or keywords to search for would help. Thanks!
Why this happens:
Perspective cameras show objects larger up close and smaller as they get farther away. Because of this, you get a closer look at objects close to the camera, so you want better depth quality on them. For this reason, you have logarithmic depth, which gives better accuracy up-close at the cost of lower depth-quality farther away from the camera. (Nobody will notice those 2 pixels z-fighting in the distance anyway)
Orthographic cameras use linear depth because you can see object just the same no matter how far away they are from the camera, so there’s no reason to have better accuracy on closer objects.
With logarithmic depth, you end up using depth values 0 through 0.7 in just the first 10% of the frustum and distribute the rest throughout the remaining 90% of the frustum. Linear depth goes from 0 to 1 linearly. (Let me know if you’d like me to explain any of this better)
“Linear01Depth” converts logarythmic depth into linear depth, but you already have linear depth on an orthographic camera, so just remove that function call. The result should be this:
Thanks for the explanation Gambit_MSplitz! I get what you mean.
I’ve tested the changes, but the result remains the same for ortho camera (the effect no longer works in perspective camera though, as expected). The ortho camera depth buffer seems to be either 0 or 1, but nothing in between?
I’ll try fiddling with it more. Thanks!
Update:
Lowering the ortho camera’s Clipping Planes to between 0.2 and 3.5 helped somewhat (no effect on the sphere though), but now the scene objects clip really badly. Any ideas? Or should I just drop it?
with LinearEyeDepth
without LinearEyeDepth
Update 2:
Getting close! Made some code changes to make it work with normal Clipping Planes value of 0.01 to 1000.
Code changes
float sceneZ = tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)).r * 40; //multiply with value
float partZ = i.projPos.y; //changed to Y-axis
There’s still an issue of the effect being more apparent when object is at the bottom of the screen. Will try to figure this out…
So in your “update 2” you say the effect is darker towards the bottom of the screen… that’s because you have your camera at an angle so the objects at the bottom of the screen are closer to the camera.
I could be wrong, but it seems the only problem you have now is the rate at which you fade from pink to transparent. Since the falloff is not linear, I think something in the calculation is still expecting you to use a perspective camera.
Well that makes sense, the depth value 0 to 1 tells you how far between the near and far plane you are where 0 is the near plane and 1 is the far plane.
It’s been a number of years, but would anyone have any ideas or insight into this? Trying to wrap my head around shaderforge and the above comments is leading me nowhere!
Sorry for the non-answer, but Shaderforge isn’t supported in Unity 2017 or later, so you may want to consider switching to Amplify shader editor or the built-in shader graph.
If you must have an answer, you may want to start your own thread to get new eyes on the question, or ask in the shader forge forums.
This 3-line code snippet seems the best start for dealing with these issues:
… it essentially replaces LinearEyeDepth with a more intelligent decision based on the camera’s current mode.
It is not perfect - UnityEditor makes some guesses when converting to/from ortho camera that aren’t always correct - but it’s a good start and is easily tweakable.