Z buffer: how can i find distance between render pixel and scene object?

Hello Everyone,

I’m working on blender for a university assay and i’m looking for a way to calcuate the distance between each sampling pixel and the object falling in its frustum, something like a pixel-wise Z buffer.
Can anybody help me find the info I need in source code?
Thank you

You want the depth pass, but you need to enable it on the render layers. IIRC, mist pass kinda normalizes the info between min and max, depth assigns a value per pixel which is the distance.

Don’t you have the source code of it? I need the source code that generate those passes.

I found this script:


Is there anybody that can help me understand where can i retrieve the single Z buffer value?
Comment on line 179 says the z value is given but doesn’t explain where.
Thx

That’s code for UV map. You should learn to navigate code and look for what you need. Depth is a render pass, so it’s in the kernel passes file.

Specifically the line 195 returns the depth pass, you can then go backwards from there looking for the function.

Moving back to the Cycles section, assuming this is a question about Cycles code and not the Python API.

Thank you so much for the help! <3
If you don’t mind would you explain me how camera_z_depth() works? if it is pixel- wise or smth else?
Also any advice or reference on how to better navigate code?
Thank you

My bad, thank you for correction :wink:

Looking around i found in kernel_bake several values assigned in kernel_camera.h such ad ray.p ray.d and ray.t.
Watching at some other QnA here on the site i found that ray.t should be the distance between each sampling pixel and the object in front of it, am I right?
Thanks

You should probably provide more context to explain what you are trying to achieve, otherwise it’s hard to help.

Some points:

  • camera_z_depth() is not by itself pixel-wise, it’s just a function that could be called per pixel.
  • ray.t is the distance from the ray origin to the hit. For camera rays this is a distance from the camera origin, but there are many other types of rays.
  • Note that distance from the camera origin is not the same as values found in a standard Z buffer.

I’m trying to give the distance between each sampling pixel and the object in front of it in cmj_sample_2D.
I thought, since i found ray.t in kernel_bake to take that info there, but now that you say that ray could be any other ray i guess i’m wrong.
How can i find said distance?
thx

eidt:
I also tried kernel_data.cam.perspective.z but it didn’t work.

I’m not sure why you would want the Z depth in cmj_sample_2D, what kind of algorithm is that for?

That’s a generic random number sampling function used for many purposes, and it does not get the object or ray passed in. It’s also used for sampling the camera ray for example, before there even is an object hit.

That’s for a university assay: I’m trying to change the number of samples based on distance and see if there may or may not be an improvement in performances. The problem is in this class programming in blender is not teached and I’m trying to do the extra mile.

The function to generate the random number is not where you would change the number of samples.

Easiest would be to do this experiment with the branched path tracer in kernel_path_branched.h. For regular path tracing this is hard as it is based on pure path tracing without any ray splitting. Search for num_samples_adjust in the kernel code to see how the number of samples may be dynamically adjusted in branched path tracing.

1 Like

Thank you so much, I’ll surely look for it!
I just noticed you actively took part in that area of developement, so cool!
May I ask you if you still can indicate me where to find that damn distance? I’ve been looking for it so long I really want to knwo where it is at this point XD
Thank you again!

Calling camera_distance(kg, sd->P) should get you the distance.

1 Like

thank you so much :wink:

just for curiosity: what should be in sd inside camera_distance(kg, sd->P) and where may i find it, if you now?