# Control Cycles material based on the distance to an empty using Animation Nodes

Target: Emissive Intensity increases as an empty comes closer, the object is a mesh that is being instanced on a grid ( following Zach Hixson's tutorial). I also have a Rotation transformation based on vector distance of the empty and vector list of the vertices of the grid(also following the same tutorial series).

My Animation Node Experience: beginner

What I Did: I Used The rotation value ( 0-180 ) and mapped that to like 0-5 and plugged it to the cycles material output to the emissive strength.

Problem: Does not work as intended, instead of it increasing the emissive by rotation, the material is universally changed and seems to follow a single "instance".

Solution: Probably need to create a material list?Preferably instead of just showing what it is , try to explain how and why it is. It will help me and others who browse this domain.

Thank You ( also what ritual do I need to complete to summon Omar Ahmed )

The problem is that all objects share the same material, consequently, editing the material affects all objects equally. Your solution of creating a number of independent materials could work, whoever, it would be very inefficient and hard to control. Instead, we could use one of numerous methods that vary in efficiency and difficulty.

The easiest method but the least efficient is to use vertex colors. You just use the Set Vertex Color node to set a color to an object, this color could carry your emission strength. The color can then be retrieved using the Attribute node in Cycles. It should be noted, however, that your objects have to be Deep Copies. My answer here explains this in more details. Also see my answer here.

The most efficient method is to do the computations in Cycles itself. Since your emission strength depend on the distance to an empty, we can simply pass the location of the empty and compute the distance in the shader as follows:

Finally there is the method of using an image sampler based on the object indices, which you can use when your relation is more complicated that a mere distance. I explained this in details in my answer here.

• could you possibly explain a bit more in detail how the third option with cycles version works. I have tried reconstructing your gif example with no success. Also when I created node "Distribute Matrices" I did not have the option of Vectors output. Is there a chance to get a copy of the Blender file for that demo? Thxs Jul 4, 2019 at 12:28
• @GarryT You mean the third method I explained in this answer? You can just use a Decompose Matrix node to get the vectors from the matrices output of the Distribute Matrices node. Jul 4, 2019 at 16:40