Different color of reflection on different objects with the same material

What I have: two objects (#1 & #2) with the same material but with different object pass indexes, and one object (#3) which is reflected on the previous two.

What I need: without using compositing to make the color of reflection of object #3 different on objects #1 and #2 (i.g., white – on #1 and yellow – on #2).

I cannot figure out what the nodes should look like for this task :spin:


Thanks!

As the glossy rays don’t carry the information from which object they’re generated, this is quite difficult to achieve just with nodes.
In OSL you can probe the incoming glossy ray with a trace, and try to get the pass info from the trace message to set how obj#3 should look like to that ray.

These four objects all have the same material, but the colors are driven by different Object Indices:


Math nodes can isolate each Object Index. Then those can input to the Fac on a Mix node to assign specific colors to different objects (such as the color of the Glossy node).

The “isolater” group. These math nodes take the Object Indices as an input, as well as a number defining the specific Object Index you’re looking for. You use a Greater-Than and Less-Than node barely below and above the number to fudge in an “Equal” Node (which I don’t think exists). Basically this allows you to choose a specific index – that object returns white (1) and the others black (0).


Finally, this group runs into mix nodes to assign different color or material aspects to various objects.


It seems a bit complicated rather than just assigning different materials, but hey, whatever works for you , you know?

@Benu For the ‘IsEqual’ I normally clamp the sum of [A greater than B] to [A less than B] and subtract the result to 1.(just a little bit simpler and faster then your setup) :wink:

About the OP, if the purpose is to change the color of the reflection, than Benu’s setup will work, and each objectIndex will reflect all the scene with the chosen color. If instead you just want one specific object to appear reflected in objects, with different colors, than you cannot do it with nodes. :frowning:

This is exactly what I wanted.

Negative result is result too. Thanks for explanations to everybody!

that doesn’t mean it can’t be done…
here’s a small OSL script to get the object index of the origin of the incoming ray.
(is not ‘waterproof’, and highly experimental:p)

shader GetIncomingIndex
(
        output float ObjectIndex = 0
)

{
    vector VectorTrace = I;
    int Hit=0;
    
    int DoTrace = trace (P, VectorTrace) ;
    if (DoTrace) {
        int HitTrace = getmessage ("trace", "hit" , Hit ) ;
        if(Hit!=0){
            HitTrace = getmessage ("trace", "object:index" ,  ObjectIndex ) ;
        }
    }   
}

there is a setup that may halp you here: http://www.pasteall.org/blend/37789
this is the setting: sphere and sphere 1 share the same material. there is also sphere 2, which surrounds sphere, and is slightly bigger than sphere (scale set to 1.02). sphere 2 material is set to transparent, so it is invisible (except for wxtreme situations).
the “magic” happens in the cube material:



the reason it works is that the transparent sphere 2 puts a “marker” on the light coming from sphere, that you can use to differentiate between sphere and sphere 1.

@oris: very interesting. Thanks for sharing.

Oris, thank you so much! A little bit more knowledge of Blender nodes and impossible becomes possible! :yes:

you’re welcome :smiley: good to know it helps!