Now delete the 3 connections you've just created by selecting them and hitting del on your keyboard (remember this trick was done only to make the input[3D] parameters of the plusMinusAverage node appear in the connection editor). Now select them both (the plusMinusAverage and the multiplyDivide), and go to Window -> Connect selected. Connect the OutputX, OutputY and OutputZ respectively to Input3D.Input3Dx and Input3D. Input3Dx, Input3D. Input3Dx.
To calculate the square root of the sum, create another multiplyDivide node, and connect the Output 3Dx of the plusMinus Average node, to the Input 1 of the multiplyDivide node. In its Attributes, choose Power as operation, and set the value of x in 'Input 2' to 0.5.
The output of the last multiplyDivide node we've created is the distance from the sampled point to the camera. And since the u or v values of the ramp can only take values between 0 and 1. So, create a Set Range node, select the second multiplyDivide node, shift select the Set Range node, and go to Window -> Connect selected. Connect the OutputX of the former, to ValueX of the latter. In the Set Range node Attributes, set the Min to 0, the Max to 1, the Old min to 0 and let the Old max represent the distance beyond which objects become invisible, so in our case, the distance from our camera to the farthest sphere.
Now you can use the output of the setRange node to choose between points of a ramp. Map the color of your objects material with a ramp. In the Create render node, make sure that Normal is selected, and choose ramp. Choose black and white for the colors of the ramp. Connect the Output X of the set range node to the UCoord of the ramp node. Make sure that your ramp is a U ramp. (You might need to invert its white and black colors, depends on your case.)
This method gets over the problems that we faced with the first one.
You can also use scripting to obtain the same result of the second method. For this, you need a sampler info node, and a material. Map the color of the material with a ramp, choose normal, and choose a grayscale ramp as before.
In the Expression Editor, write the expression that calculates the RMS, which is the following code line (in my case my sampler info node is samplerInfo1):
$distance = sqrt ( pow (samplerInfo1.pointCameraX,2)
Set a maximum value which is equivalent to the old max used in the second method, and divide the distance by this max value, for example let it be 56. Then you will have to link the obtained value the ramp's V coordinate (make sure your ramp is a v ramp in this case).
ramp4.vCoord = $distance / $MaxValue;
Click on create to create your expression, and render the scene. You should obtain the same result as before.
The above methods can serve for many other uses. For example to create a height field, you have to choose the Point world Y attribute instead of the Point camera attribute, inside the sampler info node attributes.
For a video version of this tutorial, here is a direct download link (i.e. you don't have to pay or to wait to download the file): http://rapidshare.com/files/84378918/ZDepth_n_Height_GrayScale_represention.zip
For any questions or suggestions don't hesitate to contact me.