# Is it possible to change an object's color depending on its distance?

For example I have a semitransparent sphere which contains another object which is a cylindrical line. Let's say this cylindrical path is blue. Is it possible to change the color when I rotate my object? For example the color of the line is darker when it's closer to my viewpoint and lighter when it's further away. So the line would be a color spectrum of blue.

This is my object: a cylindrical path.

• By "viewpoint", do you mean camera? Also, what render engine? – cmomoney Jan 27 '17 at 2:49
• Use the light path > ray length node in cycles. Or subtract the object position from the camera position and calculate the vector length in the cycles material. Or use the freestyle properties. Tag me if you can't find any resources on these topics. – Leander Jan 27 '17 at 11:42
• Or use a distance driver. – cmomoney Jan 27 '17 at 13:51
• only for blender render – Khang Cao Jan 27 '17 at 21:21
• can you show a picture of your object for context? Is the object rotating on its own axis? – cmomoney Jan 29 '17 at 22:14

Animation Nodes can be used here. I shall show two methods for computing the distance between the objects and the camera.

# Passing Camera Location To Cycles

The easiest and most efficient method is to use Animation Nodes to pass the camera location to cycles and compute the distance there through the Object Info Node. This is done as follows:

Notice that the distance between two points $A, B$ is equal to $\vert\vert\vec{AB}\vert\vert = \vert\vert\vec{B}-\vec{A}\vert\vert$. But for some vector $\vec{V}$, $\vec{V}\cdot \vec{V} = \vert\vert\vec{V}\vert\vert^2$ and it follows that $\vert\vert\vec{V}\vert\vert = \sqrt{\vec{V}\cdot \vec{V}}$ which is what we used to compute the distance above.

# Using Vertex Colors

If more advanced computations are required, then you can compute the distance in Animation Nodes and pass it to cycles in the form of Vertex Colors. I compute the distance between the object and active camera, remap it by subtraction and division, though you can use the Remap Value Node if you are more comfortable with it, compute a color in whatever way you want, set the vertex color of the object to that color.

Why don't we use the distance a a Hue for the color?

Why don't we use the scale of the object as the saturation for the color?

I showed how to do that for a single object, however, you can do that for any number of objects using a simple loop:

• I like this solution but can't really make it work for some reason. Are those cubes in your picture all part of a single object or are they multiple independent cubes? – Antti Jun 23 '18 at 18:46
• @Antti They are multiple independent objects. I only showed how to do it for a single object for simplicity. If you want to do it for multiple objects simply loop over them and do the same. Will add an example in a moment. – Omar Emara Jun 23 '18 at 19:08
• Thank you so much! There is just one thing I cant figure out from your new example: what is the parameter called "distance" in your loop input, I can't find anything with that name in the available options for new iterators, new parameteres or new generator outputs? – Antti Jun 24 '18 at 20:15
• @Antti The Distance is the name of the parameter not the type, when you add a parameter, a generator or an iterator, you search for the required type, in our case, the type is Float because distance is a scalar quantity. – Omar Emara Jun 24 '18 at 20:40

Okay, thanks for the answers guys! I figured it out!

• Just about what I was going to suggest . . – Robin Betts Jun 23 '18 at 19:58
• It should be noted here that the view distance is, in fact, a fragment shader, meaning it is computed for each pixel, not per object. – Omar Emara Jun 23 '18 at 21:05

You will have to do this manually like this:

then:

Final Result:

NOTE: Same works for cycles. Just click I on the colour at each different position.