x


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 http://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

more ▼

asked Aug 08 '12 at 11:58 AM

Dwarf Cavern gravatar image

Dwarf Cavern
3 1 1 2

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Aug 08 '12 at 12:00 PM

Bunny83 gravatar image

Bunny83
45.1k 11 48 206

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 CUBE. 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!

Aug 08 '12 at 01:59 PM Dwarf Cavern

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! :)

Aug 08 '12 at 02:41 PM Dwarf Cavern

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 ;)

Aug 08 '12 at 02:45 PM Bunny83
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3721
x1650
x106
x13

asked: Aug 08 '12 at 11:58 AM

Seen: 1349 times

Last Updated: Aug 08 '12 at 02:45 PM