Questions about depth calculation of depth renderer

I saw the vertex shader calculation of the depth renderer:

#ifdef STORE_CAMERASPACE_Z
		vViewPos = view * worldPos;
#else
	#ifdef USE_REVERSE_DEPTHBUFFER
		vDepthMetric = ((-gl_Position.z + depthValues.x) / (depthValues.y));
	#else
		vDepthMetric = ((gl_Position.z + depthValues.x) / (depthValues.y));
	#endif
#endif

In vDepthMetric, the gl_Position. z value is in the ndc space, and the depthValues value is in the camera view space. Why can these two be calculated together?

I think this doc page should help understand these formula:

https://doc.babylonjs.com/features/featuresDeepDive/lights/mathShadows/

2 Likes

Thank you, I will read it carefully