Switching materials mid rendering


I have a scene with multiple cameras.

I need camera A to render the scene as it is, while camera B needs to render the scene with different materials.

The idea is that camera B will render the material ID of the scene (each pixel will be colored by the material ID).

I’ve added a scalar parameter in all of the materials ( state 1 returns the original color as basecolor, state 2 returns some ID as basecolor ).

Is it possible to switch the scalar parameter while in the render loop? So that camera A will render the original basecolors, and camera B will render the material ID?

I’ve seen this question asked before in various forms, and I’ve seen recommendations to use the custom depth buffer. However, the custom depth solution is only applicable for

whole meshes, and I need the classification for materials. For example, If I have a mesh of a tree I need to differentiate between the trunk and the leaves and so on…

Thank you.