LYGIA Shader Library

depth2viewZ (lygia/v1.1.6/space/depth2viewZ)

Convert camera depth to view depth. based on https://github.com/mrdoob/three.js/blob/master/src/renderers/shaders/ShaderChunk/packing.glsl.js

Use:

<float> depth2viewZ( <float> depth [, <float> near, <float> far] )

Check it on Github


#ifndef FNC_DEPTH2VIEWZ
#define FNC_DEPTH2VIEWZ
float depth2viewZ( const in float depth, const in float near, const in float far ) {
    #if defined(CAMERA_ORTHOGRAPHIC_PROJECTION) 
    // ORTHOGRAPHIC
    return depth * ( near - far ) - near;
    #else 
    // PERSPECCTIVE
    return ( near * far ) / ( ( far - near ) * depth - far );
    #endif
}

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

#endif

Use:

<float> depth2viewZ( <float> depth [, <float> near, <float> far] )

Check it on Github


#ifndef FNC_DEPTH2VIEWZ
#define FNC_DEPTH2VIEWZ
float depth2viewZ( const in float depth, const in float near, const in float far ) {
    #if defined(CAMERA_ORTHOGRAPHIC_PROJECTION) 
    // ORTHOGRAPHIC
    return depth * ( near - far ) - near;
    #else 
    // PERSPECCTIVE
    return ( near * far ) / ( ( far - near ) * depth - far );
    #endif
}

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

#endif

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