# 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.

• – Duarte Farrajota Ramos 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? – kheetor 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. – Miguel Jan 26 '18 at 12:57

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.