# is there a node that outputs the distance value from the camera dof in cycles?

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.

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. 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.

• The z value you get from the SeparateXYZ looks to be the same distance provided by the camera node view distance. – sambler Apr 3 '17 at 3:18
• @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. – Rich Sedman Apr 3 '17 at 6:45
• – user1853 Apr 3 '17 at 13:31
• For a deeper article on the math behind depth of field read: bhphotovideo.com/explora/photography/tips-and-solutions/… – user1853 Apr 4 '17 at 3:53
• is it possible to do the same to mix an opaque / transparent material ? – gordie Apr 17 '19 at 14:50