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.

Thanks for your answers!

This is my object: a cylindrical path.

enter image description here

  • 1
    $\begingroup$ By "viewpoint", do you mean camera? Also, what render engine? $\endgroup$ – cmomoney Jan 27 '17 at 2:49
  • 1
    $\begingroup$ 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. $\endgroup$ – Leander Jan 27 '17 at 11:42
  • $\begingroup$ Or use a distance driver. $\endgroup$ – cmomoney Jan 27 '17 at 13:51
  • $\begingroup$ only for blender render $\endgroup$ – Khang Cao Jan 27 '17 at 21:21
  • $\begingroup$ can you show a picture of your object for context? Is the object rotating on its own axis? $\endgroup$ – 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:

Cycles Distance

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.

Distance Based Coloring

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

Hue Based Coloring

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

Saturation Based Coloring

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


  • $\begingroup$ 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? $\endgroup$ – Antti Jun 23 '18 at 18:46
  • 1
    $\begingroup$ @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. $\endgroup$ – Omar Emara Jun 23 '18 at 19:08
  • $\begingroup$ 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? $\endgroup$ – Antti Jun 24 '18 at 20:15
  • 1
    $\begingroup$ @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. $\endgroup$ – Omar Emara Jun 24 '18 at 20:40

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

enter image description here

  • $\begingroup$ Just about what I was going to suggest . . $\endgroup$ – Robin Betts Jun 23 '18 at 19:58
  • $\begingroup$ 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. $\endgroup$ – Omar Emara Jun 23 '18 at 21:05

You will have to do this manually like this: 1



Final Result:


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

Sorry about the bad quality gif.

  • $\begingroup$ Please consider this (i.stack.imgur.com/Dyj80.png) layout/frame when creating gifs. Source: meta.blender.stackexchange.com/a/964/30849 $\endgroup$ – Leander Jan 27 '17 at 11:47
  • 1
    $\begingroup$ It doesn't "have" to be done manually. $\endgroup$ – cmomoney Jan 27 '17 at 13:52
  • $\begingroup$ thanks for your answer! Is it also possible without moving the object? The path is fixed in my object. So I just move my camera view. $\endgroup$ – Khang Cao Jan 27 '17 at 21:24
  • $\begingroup$ If your object is on a path, then press I on the colour bar at the start of the path. Then change the colour at the end of the path and press I on the new colour bar. $\endgroup$ – Grimlock Jan 27 '17 at 23:46
  • $\begingroup$ @cmomoney I don't know of any other way of doing this $\endgroup$ – Grimlock Jan 27 '17 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.