I'm trying to work out a node group that will take the distance value from the camera to color objects from that point forwards / backwards and i've been triying to find an output that will give me this value so i can use it.


1 Answer 1


You can use the Vector Transform node to convert from Object or World coordinates (depending on whether you're using 'Object'coordinates from the Texture Coordinates node or the Position from the Geometry node) and then Separate XYZ to get the distance from the Z coordinate. With some simple maths you can then shade based on distance based on the depth of field.


In the above material, changing the value of the Subtract node will adjust the distance to the centre of the 'range' affected by the Color Ramp (larger values are more distant). Changing the value of the Divide node will affect the span of the 'range' (larger values spread the colors over a deeper range).


Note that you must have the Vector Transform set to 'Point' for this to work with Object coordinates.

EDIT : As pointed out by @sambler, the first 3 nodes can actually be replaced with a Camera Data node.

camera data

The View Z Depth socket will produce the same result as that shown above (the distance from the plane of the camera) whereas the View Distance will be a true distance from the point location of the camera.

Note, however, that the Vector Transform method is still useful if you require additional effects - such as applying a transform to the Camera or Object coordinates using a Mapping Node (to, say, rotate or shift along an axis) or similar.

  • $\begingroup$ The z value you get from the SeparateXYZ looks to be the same distance provided by the camera node view distance. $\endgroup$
    – sambler
    Commented Apr 3, 2017 at 3:18
  • $\begingroup$ @sambler Quite right - I often think in terms of the raw vectors and forget about those convenience nodes. Yes, that provides the same result as the Camera node - although it would be te Z Depth rather than Distance in this case (the difference being that the Z Depth is the distance to the plane parallel to the plane of the camera and the Distance being the true distance to the surface). The Camara Data node should be more efficient (although I haven't tested that) but it's worth being aware of what it does in terms of the raw vectors. $\endgroup$ Commented Apr 3, 2017 at 6:45
  • $\begingroup$ related: blender.stackexchange.com/questions/27764/… $\endgroup$
    – user1853
    Commented Apr 3, 2017 at 13:31
  • $\begingroup$ For a deeper article on the math behind depth of field read: bhphotovideo.com/explora/photography/tips-and-solutions/… $\endgroup$
    – user1853
    Commented Apr 4, 2017 at 3:53
  • $\begingroup$ is it possible to do the same to mix an opaque / transparent material ? $\endgroup$
    – gordie
    Commented Apr 17, 2019 at 14:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .