The material (and object) index works only when the object is uncovered by other objects. But the thing is the object can be visible behind a transparent or semi-transparent objects. In cycles it seems that if I use refractive material like the glass shader the material/object mask no longer appears in the render behind the transparent object.

The mask result is below:

Let's say I want to color correct the blue torus in composition so I need the refracted part of the object as well. Is there a way to obtain the correct mask for the torus?

2 Answers
2

Blender is correctly showing the mask of the object that form the camera point of view truly has the material assigned. The squared piece is made of glass, the fact that is somehow showing what's behind doesn't change the material which is made of.

We can try to mask the "perception" of the material, but we'll have to use the rendered image itself, because only there the optic calculation occurs. Think about a deforming glass as the one show below:

The refraction, the dimming of the bounced light, the bending of the geometry, the fact that on the short side of the box you cannot see the torus... are all effects that strictly depends on the characteristic of the material (ior for example) and of the geometry.

What I'm trying to say is that you can't get the mask without actually render the image, beacause is in the simulation that all effects take place.

Maybe one day someone will implement a object id pass capable of taking optics into account, but for the moment I would switch all the lights off, add an emission shader to the interested object and use the rendered image as a mask.

It's certenly not an optimal solution as you'll probably have to set up a parallel blendfiles just for this task.

Similar to @Carlo's answer you can set this up within a single blendfile (and within the same scene) by making use of the Render Layers 'material override' setting. Set up each object with a pass index relating to the type of material in use. For example, we could have :

0 = Black diffuse (obscures mask)

1 = Emissive (the thing we want the mask for)

2 = Glass

3 = Glossy

You can potentially extend this to include any type of distinct material you desire (anything transmissive that you want the mask to take account of - eg, different indexes for different IORs, one for transmission, another for 'rough' glossy, etc - each assigned a suitable ID).

Then set up a material (named 'Mask' or similar) to determine the shader to use based on the object ID for each object (see nodes in the image below). Obviously this would need to be extended if you have extended the list of materials defined above.

Set up your render layer to disable Use Environment (so there is no environment lighting), and to set the 'override' Material to our new 'Mask' material - this will result in all objects in the scene being rendered with our 'Mask' material which will assign the shading based on the Object ID of each object (eg, 1 = Emissive, 2 = Refractive, etc.).

Rendering the layer will now produce something like the following - which can hopefully be used as your mask :

In the above example I have the torus emissive (index 1), the block as glass (index 2) and the floor glossy (index 3). Anything with an index of zero will act to block the mask. The Greater Than nodes are set between each of the levels - ie, 0.5, 1.5, 2.5 to be between the IDs 0, 1, 2, 3. Add as many Greater Than/Mix sets as unique IDs you require, along with the relevant Shader to mimic the effect you require that object to have on the mask.

Note that you can move lamps to specific layers and exclude them from the 'Mask' render layer if you do not want them to be included.

To still render the 'normal' image you can simply set up another render layer without the 'Mask' material override.

To demonstrate using the mask, consider the following Compositor nodes :

The above nodes simply use the Mask to mix between the original rendered image and the image multiplied by a color (set via an RGB input node). By varying the RGB color you can easily change the color of the torus purely within the compositor (including it's reflection and refraction) without having to re-render the original :

$\begingroup$Sure - I’ll try and dig it out for you (I think it’s on my broken old laptop so might take a little while).$\endgroup$
– Rich SedmanSep 3 '18 at 22:21

$\begingroup$@Prag I couldn't recover the original Blend file so have put together a new one and updated the answer to also include an example of using the generated mask to vary the color of the torus post-rendering.$\endgroup$
– Rich SedmanSep 26 '18 at 22:07