[Solved]Calculating Depth Values

When it comes to the depth buffer, this website is often linked to me: https://www.sjbaker.org/steve/omniv/love_your_z_buffer.html

It contains a Z calculator, for which you can input your Far and Near plane values, the Z distance for this fragment (in between the 2), the number of bits, and it calculates a result.

I am hoping someone can confirm I’m understanding them correctly before I start using them - if I put in Z distance = 1000, with far also = 1000, I get 16777215
With 1<<24 (from the formula) = 16777216, does this mean to get correct values I just have to normalize to get them into the 0-1 range?

I ask as I want to load a texture and use that as a sort of pre-computed depth buffer.

EDIT: well it seems they are calculating them as relative to the max, so simple to normalize.

That’s not how all depth values are calculated.

With orthographic projection, the depth values are indeed linearly distributed. For a given Z-value, it’s a simple matter of calculating (Z-min)/(max-min) * 2^depthBits, where depthBits is 24 in your case. If you’re using OpenGL, you should just load the 0.0-1.0 float to the depth buffer, as OpenGL will do the quantizing to 16/24/32 bits for you.

With perspective projection, the depth precision is not linearly distributed. They’re essentially logarithmically distributed over the view frustum, giving significantly more precision near the camera and much lower precision far away.

The formula I am using from the website is

(1 << N) * (a + b / z)

a = (zFar/(zFar-zNear))
b/z = (zFar * zNear / (zNear - zFar))/z

The results are non-linear, just it gives me values between 0 and 2^24 (provided I put in 24 bit), so 16777216

The thing is I want to calculate them in Blender using math nodes

Ah, ok. Yeah, that formula looks correct.

Cool, thanks. I guess ultimately I’ll find out when everything runs - it’s usually always a disaster first time anyway :stuck_out_tongue: