I use this function to linearize the depth value in my shaders:
float linearize_depth(float d, float zNear, float zFar)
{
return zNear * zFar / (zFar + d * (zNear - zFar));
}
What function would do the exact inverse transformation?
I use this function to linearize the depth value in my shaders:
float linearize_depth(float d, float zNear, float zFar)
{
return zNear * zFar / (zFar + d * (zNear - zFar));
}
What function would do the exact inverse transformation?
Copyright © 2021 Jogjafile Inc.
With logarithmic depth buffer, the mapping of scene (camera space) depth to values that ultimately end up in the depth buffer (0..1) is:
where z is the positive depth into the scene, otherwise obtainable from the w component in clip space after the projection (in your code you can use ..log(clip.w + 1.0)..).
To retrieve the camera space depth in a fragment shader, the equation needs to be inverted:
or equivalently
To get a linear mapping from 0..far into a 0..1, just divide it by the far value.
Credit goes to camenomizoratojoakizunewake. Original answer can be found here.