Depth buffer
The depth buffer is a texture in which each on-screen pixel is assigned a greyscale value depending on its distance from the camera. This allows visual effects to easily alter with distance. It is used for soft particles.
Source's depth buffer has a very short range of just 192 units, and is also squashed down to fit into a power of two rendertarget (after having being generated in the alpha channel of the full backbuffer). It can be viewed with r_depthoverlay
.
Scaling the depth output
The available output range of the depth buffer can be adjusted in every mod that is utilizing a custom shader library. A requirement is to solely render all opaque geometry in a scene with modified shaders; this can either be achieved by manually replacing all shaders used in every VMT file (optimal solution), reloading all materials at runtime (slow) or overriding the actual shader files used by a default shader dll (probably unstable).
The depth information drawn to the backbuffer is scaled by this factor ( src\materialsystem\stdshaders\common_ps_fxc.h ):
#define OO_DESTALPHA_DEPTH_RANGE (g_LinearFogColor.w)
Changing the definition of the constant g_LinearFogColor would also result in a different range, but it seems to be inaccessible from the publicly available source code.
The value describes how the homogenous depth is transformed into normalized space, so if one was to describe the whole scene in the alpha channel, the value should be 1 / (zFar - zNear)
or 1 / projectedPosition.w
. Otherwise you should use 1 / desired_range
to calculate your constant, keep in mind that your interval starts at zNear (default 7).
To extend the range to maximum distance of 1024 units the definition should be changed to this:
#define OO_DESTALPHA_DEPTH_RANGE 0.00098 // 1 / ( 1024 - 7 )
