How to render an object’s x,y,z position

A user asked how to render an object’s world-space position into an image.  To address this question, we’ve included a new ShowPosition surface shader in Air 11.04 and later.  Here are the shader parameters:

Surface "ShowPosition"
 "float UnitSize" [1]
 "float[3] Origin" [0 0 0]
 "float Clamp" [0]
 "float Cyclic" [0]
 "string ShadingSpace" ["world"]

Here’s a rendering with the default parameter values.  The output color is the world-space x,y,z position of the current shading location:

ShowPosition

It doesn’t normally make sense to filter position values, so this image was rendered with only 1 pixel sample (PixelSamples 1 1 in the RIB file) and no filtering.

The position values may well be greater than 1 or less than 0, so a full-precision position image should be saved in a file format such as HDR, EXR, or TIFF that supports floating-point data.

The ShowPosition shader also allows the position values to be scaled to lie in the unit interval, which allows the data to be recorded in an 8-bit or 16-bit image.  The scaling is performed by mapping the position into a cube with one corner defined by the Origin parameter and each side of length UnitSize.  In this image the UnitSize is set to 10:

show scaled position

Position values that lie outside of the box defined by the Origin and UnitSize value will be clamped to the unit interval if the Clamp parameter is set to 1.  If the Cyclic parameter is set to 1, out of range values will be wrapped to form a periodic pattern.  Here’s the same model with UnitSize set to 1 and Cyclic set to 1:

show cyclic position

This entry was posted in Passes, Shaders and tagged , . Bookmark the permalink.