LYGIA Shader Library

linearizeDepth (lygia/space/linearizeDepth)

linearize depth

Use:

linearizeDepth(<float> depth, <float> near, <float> far)

Check it on Github


#ifndef FNC_LINEARIZE_DEPTH
#define FNC_LINEARIZE_DEPTH

float linearizeDepth(float depth, float near, float far) {
    depth = 2.0 * depth - 1.0;
    return (2.0 * near * far) / (far + near - depth * (far - near));
}

#if defined(CAMERA_NEAR_CLIP) && defined(CAMERA_FAR_CLIP)
float linearizeDepth(float depth) {
  return linearizeDepth(depth, CAMERA_NEAR_CLIP, CAMERA_FAR_CLIP);
}
#endif

#endif

Use:

linearizeDepth(<float> depth, <float> near, <float> far)

Check it on Github


#ifndef FNC_LINEARIZE_DEPTH
#define FNC_LINEARIZE_DEPTH

float linearizeDepth(float depth, float near, float far) {
    depth = 2.0 * depth - 1.0;
    return (2.0 * near * far) / (far + near - depth * (far - near));
}

#if defined(CAMERA_NEAR_CLIP) && defined(CAMERA_FAR_CLIP)
float linearizeDepth(float depth) {
  return linearizeDepth(depth, CAMERA_NEAR_CLIP, CAMERA_FAR_CLIP);
}
#endif

#endif

Use:

linearizeDepth(<float> depth, <float> near, <float> far)

Check it on Github


#ifndef FNC_LINEARIZE_DEPTH
#define FNC_LINEARIZE_DEPTH

float linearizeDepth(float depth, float near, float far) {
    depth = 2.0 * depth - 1.0;
    return (2.0 * near * far) / (far + near - depth * (far - near));
}

#if defined(CAMERA_NEAR_CLIP) && defined(CAMERA_FAR_CLIP)
float linearizeDepth(float depth) {
  return linearizeDepth(depth, CAMERA_NEAR_CLIP, CAMERA_FAR_CLIP);
}
#endif

#endif

Licenses

LYGIA is dual-licensed under the Prosperity License and the Patron License for sponsors and contributors.

Sponsors and contributors are automatically added to the Patron License and they can ignore the any non-commercial rule of the Prosperity Licensed software (please take a look to the exception).

It's also possible to get a permanent comercial license hook to a single and specific version of LYGIA.

Get the latest news and releases

Sign up for the news letter bellow, joing the LYGIA's channel on Discord or follow the Github repository