# Create a gradient dependant of distance to an object

I have the following scenario in cycles:

I would like to know if it is possible to create a gradient on a plane like the one on the image above where the colours depend on the distance to an object (sphere in this case). In this case, the surface right below the ball would be the nearest one and hence would be of one colour. As we move away from the plane's center the colour should change accordingly to the distance to the sphere. I would like to automatize the process so that blender takes the coordinates of the sphere and automatically draws a colour gradient on the plane's surface. I have managed to create the gradient but I cannot make it dependant of the distance to an object.

Thanks in advance

• Jan 26 '18 at 11:59
• With cycles / surface nodes you could make a setup that measures distance to a single point that happens in shader pipeline. You can write image data via python script so you can sample the areas and calculate the distance and "bake" the data but it is relatively slow since python is run on CPU. Are you looking to use this real-time, in rendering or export some custom data format? Jan 26 '18 at 12:44
• I want this to create a pixel map density and no need to be in real-time, only need the rendered final image. Jan 26 '18 at 12:57

## 3 Answers

Here is a simple node setup for Cycles. Note that this will use the sphere object origin, not its surfaces. This material will not consider the shape of your object.

Use the Mapping node to tweak the effect of the gradient and the ColorRamp node to change the gradient colors.

Results can be viewed in render mode.

You can do something like this with dynamic paint. This solution does calculate proximity from the mesh, not just the center point.

The plane should be your dynamic paint canvas, the sphere (or whatever object(s)) the brush. I suggest painting to vertices as that's easy to use in the material node setup. On the brush object, set your paint source to "Proximity" and set a Paint Distance that gives you the gradient values you're looking for.

Then set up your material to translate the gradient into a color gradient input to a shader. Here I've used the wetmap.

• What would this look like in the new EVEE viewport in 2.8? Dec 12 '18 at 21:00

Another way of doing this:

Add a Value node to your shader, then...

1. Right click on the number field and choose edit driver.
2. Under Driver Settings, set Type to "Sum Values".
3. In the Object field, select your sphere.
4. For Type set "X Location".
5. For Space, set World Space.

Repeat with Y and Z, then wire the results into a Combine XYZ node. Now you're passing your sphere's position to the shader.

To get the distance from the sphere to your surface:

1. Add a Geometry Node
2. Add Vector Math Node, set it to Subtract
3. Subtract the Combine XYZ node from the Geometry Position node.
4. Add another Vector Math node. Set it to Dot Product
5. Wire the output of the Vector Subtract node into both vectors of the Dot Product node.

Now you've got distance going from 0 to a big number. Scale it down, clamp it, and feed it to a color ramp.

• So this is just distance to the center of the object, not distance to the surface of the object, right? May 14 at 1:38
• Yep, that's correct May 15 at 20:06