# How to color object based on distance from camera with geometry nodes?

I'm trying to recreate this effect Is it possible to change an object's color depending on its distance? In my project, I have 2*2 KM plane which need to be painted gradually in different colors based on distance from camera. I've tried this: Is it possible to calculate the distance between the vertices of two objects in Geometry Nodes? but it didn't help me. Thank you for response.

• In which way it didn't help you? What Geometry Nodes setup do you have so far? Commented Aug 22, 2022 at 16:19
• Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
– Community Bot
Commented Aug 22, 2022 at 16:52

A camera has no geometry, so you would have to convert the instance of the camera into a point with Instances to Points.

You can then use this point as a reference with Geometry Proximity and thus assign values to the points of your mesh.

The values resulting from the distance to the camera you would have to adjust to your needs with Map Range.

You store these values with Store Named Attribute in the geometry, which you can read again in the shader with Attribute and convert into a color.

(Blender 3.2+)

Update

Of course you can solve this even easier (thanks to @Gordon Brinkmann for the attentive reading):

Instead of solving the distance via Geometry Proximity, you can directly do the distance to the camera with Vector Math:

• No need for the Store Named Attribute, just plug the Result from the Map Range node into the empty socket of the Group Output and give it the name in the modifier. Commented Aug 22, 2022 at 17:59
• Oh, and you could calculate the distance between the camera's location and the position of the plane's vertices by using a Vector Math node. No need for geometry proximity and hence no converting of the camera into a point. And you can leave the camera's Object Info node at default values, no switching to Relative or enabling As Instance. Commented Aug 22, 2022 at 18:04
• @GordonBrinkmann Thank you, that is completely correct! I have once again thought a little more complicated than necessary :D ...but with the example can still do a lot. Commented Aug 22, 2022 at 18:15
• I actually thought the OP was asking for what the link directly showed me at first, cubes that get an overall uniform color depending on the distance instead of a gradient. In this case your Store Named Attribute (or Capture Attribute linked to the Group Output) with domain set to Instance would be the better solution, since you can capture the location of the instances. Coloring the cubes differently only works if you realize the instances, but then you'll get the gradient with my simpler solution. The Store Named/Capture Attribute gets the location before they are realized. Commented Aug 22, 2022 at 18:28
• @GordonBrinkmann: Looks like you are the only one here who reads ALL the answers :D (and understands them) :D :D Commented Aug 23, 2022 at 5:12

If you want the color-change at render-time and/or at render, (rather than the geometry's,) resolution, it might be better to do this with a shader?

You always have the option of baking the color to an image-texture..