1
$\begingroup$

I am trying to get the depth of every pixel to the center of the projection of the camera in blender. I am trying to get the actual depth and not a normalized value. Hence have I tried the following in the composite window:

enter image description here

I have placed the camera at a distance of 6 meters from the cube as in below images.

enter image description here

enter image description here

Now when I render my image and the .exr file gets stored all the values in it show the value as 5.0. (I have used Python/open cv to cross-check this. Also, I have verified this in the rendered image by right-clicking and checking the z value) In my understanding, the depth of every pixel should be different. For example, if I try to check for the value at one of the pixel corresponding to the vertex of the cube then the value of that pixel should be different from the value at the center of the cube.

The requirement is somewhat similar to what the Camera Data Node provides with the view z- depth option (https://docs.blender.org/manual/en/dev/render/shader_nodes/input/camera_data.html?highlight=camera%20data%20node).

However, I don't think that would generalize well to a whole scene containing many different objects. Is there a way to achieve the above? Can someone guide me with it?

$\endgroup$

1 Answer 1

1
$\begingroup$

As you have discovered, the 'Depth' pass only returns the 'depth' from the plane of the camera, not the distance travelled to the surface - so it ignores any diagonal element. The Camera Data node does provide this information but that is at shader-level only.

A similar effect can be achieved in the compositor by using the Mist Pass. The Mist Pass normally returns a value between zero (at the start of the mist) and 1 (at the limit of the mist) and represents the total distance travelled rather than the distance from the plane of the camera. By setting the Mist to be Linear we can use simple maths operations to convert from the 0-1 range into the actual distance to that point in the scene.

Start by enabling the Mist Pass and set the Falloff to Linear :

enable mist pass

setup mist

The 'Start' should be the lowest distance you're interested to measure while the 'Depth' should be the range. Note that a narrower 'band' of mist will give better precision - although you could use 0 for the start and a large value for the range to cover the whole scene if desired. Ensure the Falloff is set to Linear.

All that is now required is to setup the compositor to Multiply by the Mist Depth and Add the Mist Start. This will convert the 0.0 - 1.0 mist value to the 'Start' - 'Depth+Start' range; ie, the actual distance to that surface. This can be achieved either with the single Multiply Add operation or using separate Multiply and Add nodes. enter image description here

$\endgroup$
4
  • $\begingroup$ I tried the above. However, I still receive the same issue. When I place the camera at 5m, I still get all the values of the pixel corresponding to the point on the surface of the cube as 4.348. I have used the start and depth as 2m and 15m respectively for the Mist Pass. I even tried to get the output into an OpenEXR file using the File Output node and connecting the value of MulitplyAdd Node in addition to what you suggested and got the same results in the file, which is natural. Anything that I might be missing? $\endgroup$
    – Sourabh
    Sep 19, 2021 at 17:53
  • $\begingroup$ @Sourabh That's strange... did you definitely use the Mist socket as shown above - and re-render once you've enabled Mist? The results I'm seeing is varying distance (so the centre of the cube surface is showing as 5.000 with increasing values as I move out to the edges and corners) - so it's working on my setup. If you can't get to the bottom of it, please share your .blend file and I can take a look. $\endgroup$ Sep 19, 2021 at 19:35
  • 1
    $\begingroup$ Sorry, there was some mistake from my side. Thank you so much for the solution :). Works well !! $\endgroup$
    – Sourabh
    Sep 19, 2021 at 21:25
  • $\begingroup$ No worries - glad to be able to help :-) $\endgroup$ Sep 19, 2021 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.