You can use the scalar to vector node to extract the position of an object and make it available in the Render Tree
There are many instances where you want to be able to control an attribute of a shader based on its distance to the camera or another object in the scene. Regardless of the end effect you’re trying to achieve, the procedure of getting there is about the same.
Select any of the spheres (they all share the same material) and press [7] to open a Render Tree. From the Nodes > Conversions menu, get a Scalar to Vector node. This node enables you to enter any XYZ coordinates and by linking their separate values to the corresponding value of the Ctrl_Null, you’ve efficiently extracted the nulls position and made it available in the render tree. Open its PPG and click the Lock icon to pin it to the screen. Select the Ctrl_Null and press [Ctrl] + [K] to open its transform PPG. Drag the animation icon (the green divot) for the X Position (in the transform PPG) and drop it onto the animation icon of the Input X in the Scalar to Vector PPG. Repeat for the Y and Z position.
Rather than extracting the position of the object holding the material, which you can’t do anyway in this scenario since there are multiple objects, you’ll use its intersection point. By comparing the intersection point with the position of the Ctrl_Null you can extract the distance between them. From the Nodes > State menu get a Vector state. Open its PPG and change the State Parameter to Intersection point. From the Nodes > Math menu get a Vector-Scalar node. Open its PPG and change the operation to Distance between V1 and V2. Connect the Scalar to Vector node to the vector_input1 of the Vector_math_scalar node and the Vector state to the vector_input2. Depending on the scale of your scene the distance value can be extremely small or large so you’ll have to change its range to something more useful. From the Nodes > Mixers menu get a Gradient node and a Mix 2 Colors node. Connect the Vector_math_scalar node to the input of the Gradient node and the Gradient node to the Weight input of the Mix_2colors node. Connect the Mix_2colors node to the ambient and diffuse input of the Blinn node.
Open the Gradient PPG and switch to the Input tab. In the Active Range section, change the Maximum to 20. Switch back to the RGBA Gradient tab and click the Black/White preset. The minimum and Maximum values in the Active Range controls at which distance the Ctrl_Null start effecting the material and the gradient controls the falloff of the effect. So for a sharper falloff, simply move the markers closer together.
The project files used in this tutorial can be found at:
http://www.redi-vivus.com/Caffeineabuse/Modify_Color_By_Distance.zip
You can then compare the position of the object with the intersection point on surface to extract the distance between them.
Quick tip
To further enhance the effects falloff you can add a third marker in the middle of the gradient and connect a greyscale texture, such as a fractal, to it. This way you can breakup the regularity of the transition from black to white.
Read the full post>>