1
$\begingroup$

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.

$\endgroup$
5
$\begingroup$

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.

material

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

result

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.

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

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.