6

I have a blender file where a sphere is orbiting a force field. I want it to be more red when it's closer to the center, and more blue when it's further. I already have the math figured out, I just need to know how to get x/y coordinates in nodes.

Here's the file:

arduinoob
  • 87
  • 1
  • 5

2 Answers2

8

Use the object info node connected to a separate xyz node:

enter image description here

JakeD
  • 8,467
  • 2
  • 30
  • 73
6

Here's a node setup, which sets the colour based on the distance from a specified point in the XY plane. enter image description here
The inputs to the node group1 are as follows:
X Origin - The X coordinate of the point from which to calculate the distance
Y Origin - The Y coordinate of the point from which to calculate the distance
Inner perimeter - The threshold at which distance is set to 0 (i.e. closer than this will be all red).
Outer perimeter - The distance at which distance is set to 1 (i.e. farther than this will be all blue)

Here's the inside of the node group:
enter image description here
The Location output of the Object info node, gives the Object's location in World coordinates.
The Separate XYZ separates a vector or position in three values, one for each of X, Y and Z components of the vector/position.
The following math nodes up to and including the last Power node calculate the distance from the specified point according to the common distance formula.
The Subtract node following the Power node subtracts the Inner perimeter from the calculated distance.
The Subtract node below that calculates the distance between the Inner and Outer perimeters.
Finally, the Divide node divides the distance between the object and the centre point by the distance between the Inner and Outer perimeters.
The output is a value which at the Inner perimeter will be 0 (closer than that, will yield a negative value) and at the Outer perimeter will be 1 (farther away will yield a value that is greater than 1). The Fac input of the ColorRamp clamps the values that are outside 0-1, so the fact that we get values greater than 1 and less than 0 won't be a problem.

Here's an animated GIF of the result:
enter image description here

And here's the blend (it's basically the same blend you provided, with the material for the "planet" modified).

1When using node groups, you can edit the contents of a node group by selecting it and pressing Tab to get inside the group. When inside a node group, you can get back to the overview by pressing Tab again.

  • "The Location output of the Object info node, gives the Object's location in World coordinates." if you input Location into Material Output Surface you'll clearly see Location is a value in the range [0,1] and not world coordinates At All. Or... it gets converted into that somehow. Through https://blender.stackexchange.com/questions/10673 you can see it's indeed world coordinates. You must use a map range to stop it from clipping the value. – Unknow0059 Jan 12 '24 at 08:24