- Home /
Unity Shader, masking a specific layer?
Hi all,
I have some experience and am comfortable with Unity's JavaScript and Unity environment, however I am a complete novice when it comes to shader programming, so I need some pointers or help, here's the situation:
I have found this great simple shader that masks out everything from pixelplacement.com. However, I would need to expand it so that I have control over which layer I want to mask out. Here is the code:
Shader "Depth Mask" {
SubShader {
Tags {"Queue" = "Geometry-10" }
Lighting Off
ZTest LEqual
ZWrite On
ColorMask 0
Pass {}
}
}
Is there a way to do this? Can anyone help me out with this, please? Many thanks in advance! :)
Voja
Answer by Bunny83 · Aug 08, 2012 at 12:00 PM
Layers do not exist in shaders. It's a Unity runtime feature. The best way is to use multiple cameras for the different layers. That way you can render the layers seperately.
Take a look at the cullingMask and of course depth.
Hello,
Thanks for your reply. I do understand and know culling and depth and already do use layered rendering for various purposes, but the problem I need a solution for is to mask out parts of the object on one layer, while seeing rendering layers underneath, for example:
camera depth 2: contains CUBE camera depth 1: contains PLANE
I want to be able to mask out parts of the CUBE (on depth 2) and to be able to see PLANE (on depth 1) trough the masked parts of the CUB$$anonymous$$ I can achieve masking with the shown shader, but it masks out everything.
I hope this makes it clearer, sorry if my original post was unclear :) Thanks!
Hello again,
Your reply was essentially correct, my setup idea was correct but I have overlooked that my masking object was not in the correct layer. Once I have corrected that problem, I am able to achieve full control over masking :) Thanks Bunny! :)
Yes, so you render all normal layers with camera 1 which is rendered first. Then you render your other layer which you want to mask. On this layer you have your masking geometry which is rendered in a renderqueue before the normal geometry like your above shader does. That's all. All stuff from the other layers are rendered beforehand.
You might need to use a different skybox if you even use one. We've also used a mask shader to mask out parts of the terrain. However the built-in skybox is rendered quite late to reduce overdraw. We used a real skybox which we moved manually with the player ;)