10
$\begingroup$

I have the following scenario in cycles:

enter image description here

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

$\endgroup$
3
  • $\begingroup$ Related blender.stackexchange.com/questions/79851/… $\endgroup$ Jan 26 '18 at 11:59
  • $\begingroup$ 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? $\endgroup$
    – kheetor
    Jan 26 '18 at 12:44
  • $\begingroup$ I want this to create a pixel map density and no need to be in real-time, only need the rendered final image. $\endgroup$
    – Miguel
    Jan 26 '18 at 12:57
9
$\begingroup$

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.

Gradient with distance node set up

$\endgroup$
11
$\begingroup$

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.

dynamic paint setup

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

material setup

$\endgroup$
1
  • 2
    $\begingroup$ What would this look like in the new EVEE viewport in 2.8? $\endgroup$
    – patmo141
    Dec 12 '18 at 21:00
5
$\begingroup$

Another way of doing this:

enter image description here

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.

enter image description here

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.

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

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.