Skip to content

Instantly share code, notes, and snippets.

@supahfunk
Forked from yiwenl/linearDepth.glsl
Created January 14, 2020 17:45
Show Gist options
  • Save supahfunk/53b3dfb28cecc314362e368d21632c4d to your computer and use it in GitHub Desktop.
Save supahfunk/53b3dfb28cecc314362e368d21632c4d to your computer and use it in GitHub Desktop.
uniform float zNear = 0.1;
uniform float zFar = 500.0;
// depthSample from depthTexture.r, for instance
float linearDepth(float depthSample)
{
depthSample = 2.0 * depthSample - 1.0;
float zLinear = 2.0 * zNear * zFar / (zFar + zNear - depthSample * (zFar - zNear));
return zLinear;
}
// result suitable for assigning to gl_FragDepth
float depthSample(float linearDepth)
{
float nonLinearDepth = (zFar + zNear - 2.0 * zNear * zFar / linearDepth) / (zFar - zNear);
nonLinearDepth = (nonLinearDepth + 1.0) / 2.0;
return nonLinearDepth;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment