Skip to content

Instantly share code, notes, and snippets.

@yiwenl
Created November 21, 2019 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yiwenl/113d3fd56252032e5bdda771453396a4 to your computer and use it in GitHub Desktop.
Save yiwenl/113d3fd56252032e5bdda771453396a4 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