# 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? Jan 27, 2017 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. Jan 27, 2017 at 11:42
• Or use a distance driver. Jan 27, 2017 at 13:51
• only for blender render Jan 27, 2017 at 21:21
• can you show a picture of your object for context? Is the object rotating on its own axis? Jan 29, 2017 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? Jun 23, 2018 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. Jun 23, 2018 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? Jun 24, 2018 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. Jun 24, 2018 at 20:40

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

• Just about what I was going to suggest . . Jun 23, 2018 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. Jun 23, 2018 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.