- Home /
Is it possible to render material transparent if it's above another specific material?
If object A is between the camera and object B i want the intersecting part of object A to be rendered transparent.
Is it possible to make such a shader?
Answer by Piyush_Pandey · Aug 03, 2018 at 07:30 AM
Yes. Unity is even having a whole article about it. You can check it here.
I will summarize it :- 1) Make two material Green and Red.
2) Make two shaders shadGreen and shadRed and add them to Green and Red respectively.
3) In the shader code of of shadGreen add
Pass
{
//this stencil with compare with any other stencil and will NOT render above the stencil tagged with "5"
Stencil
{
Ref 5
Comp Notequal
ZFail decrWrap
}
******rest of the shader code******
}
4) Now in shadRed code add
Pass
{
Stencil
{
Ref 5
Comp always
Pass replace
}
*********rest of the shader code******
}
4) Now apply the materials to the green and red objects. Your green object will not be visible above the red object.
If i knew the exact type of your objects {eg. Mesh, sprite, Canvas UI}, i might have helped with the complete shader code as they need different shaders. But as it is not mentioned here, i think you might take it from here.
Wow, works like a charm :) Thanks a lot mate! Hope the performance on mobile will be ok.
Happy to help. Just use this stencil code with a mobile optimized shader and you will have no problem.