Depth buffer question

Can any pro guide me on these 2 functions in the shader? what is the NEAR_Z and FAR_Z stand for, is the camera far and near value? how to get that in monogame?

float LinearDepth(float depth)
{
return (depth * NEAR_Z) / (FAR_Z - depth * (FAR_Z - NEAR_Z));
}

float ViewDepth(float depth)
{
return (FAR_Z * NEAR_Z) / (FAR_Z - depth * (FAR_Z - NEAR_Z));
}

https://github.com/MrySwk/GravityEngine/blob/195fb66f731a1eb947c5bff2a56d3cade8af674e/GEngine/GDxRenderer/Shaders/LightPassPS.hlsl

float2 ProjectionConstants(float gNearZ, float gFarZ)
{
float2 projectionConstants;
projectionConstants.x = gFarZ / (gFarZ - gNearZ);
projectionConstants.y = (-gFarZ * gNearZ) / (gFarZ - gNearZ);
return projectionConstants;
}

float LinearZ(float4 outPosition)
{
float2 projectionConstants = ProjectionConstants(gNearZ, gFarZ);
float depth = outPosition.z / outPosition.w;
float linearZ = projectionConstants.y / (depth - projectionConstants.x);
return linearZ;
}

FAR_Z and NEAR_Z would be the camera clipping planes. Think of it just as how far and how close the camera will render stuff. When you create your perspective matrix NEAR_Z is nearPlaneDistance and FAR_Z is farPlaneDistance. If you need them in your shader you should pass them as a uniform.

2 Likes

And make sure near plane is not zero ;>

2 Likes

ok thanks, and wat is the different for LinearZ vs LinearDepth?

Z is sometimes used as a shorthand for depth

so are they the same?

Yes, they are the same.