Blog

Masking in Unity

Masking is a fundamental technique that has hundreds of usages. Just take a look through some of the Flash work in my portfolio and you’ll see where masking really shines with respect to animating the introducing of visual elements. Coming from a Flash background, I was dumbfounded when I realized Unity didn’t have any technique for masking geometry and images. Luckily I finally found the answer in the “Depth Mask” shader.

Though the “Depth Mask” shader is Unity’s answer to masking its not without it quirks if you are looking for a 1:1 comparison with the traditional 2D mask. The way this shader works is whatever geometry is utilizing it will essentially hide whatever is behind it. This means EVERYTHING behind it. The best way to constrain the effects of this is by layering cameras that specifically render masked “scenarios”.

The actual code for the a depth mask shader is short and sweet:

Shader "Depth Mask" {
    SubShader {
        Tags {"Queue" = "Geometry-10" }       
        Lighting Off
        ZTest LEqual
        ZWrite On
        ColorMask 0
        Pass {}
    }
}

If you change any aspects of the material of an object you are trying to mask (texture, colors, etc…) the “Depth Mask” will fail unless you run the SetPass() of the object that is carrying the actual “Depth Mask” shader. This method is documented here.

Download

12 Comments

  1. '_"
    February 15, 2011

    oh so sweet.

    what would we do without you.

  2. February 15, 2011

    […] This post was mentioned on Twitter by Will Goldstone and Frederic RP, Bob Berkebile. Bob Berkebile said: Masking in #Unity3D? SOLVED! Bring on the old school #Flash transitions and animation techniques! http://bit.ly/eKXFPm […]

  3. February 15, 2011

    Awesome!

  4. February 16, 2011

    Update: added information for any problems you may have if the “Depth Mask” fails after you change properties of the masked object’s material.

  5. John
    June 27, 2011

    Hey, works quite well, but if i move all objects in the scene in any direction, the texture never shows up again (which doesnt make any sense in my opinion). Is there a fix for this?
    thanks in advance!

    • June 27, 2011

      That doesn’t make sense. Can you provide more details? Also, do a search on my site for mask and see if there’s any details in the other post I did about masking in Unity that helps.

  6. September 25, 2011

    Hi this is such a great shader and example.
    I’ve made a video tutorial based on this technique to achieve a spinning mask effect with ex2D:
    http://abitgames.com/2011/blog/tutorial-skill-cooldown-effect-ex2d/

  7. Erik
    December 10, 2011

    Thank you sir. I’m surprised how simple the solution was, but I would have never thought of it myself.

  8. June 1, 2013

    Thank you SO much. This is so fucking useful. I’ve been trying to figure it out for so long. I’ll admit I was looking for the opposite effect – where it only shows through the shape of a texture – but this is even better because you can instead use pieces of geometry to create a mask. This is perfect for using many different cameras at one time. Thank you again sir. Fantastic job.

  9. Allan
    September 14, 2013

    I know this is an old post but I must say this is indeed awesome man, extremely useful!

    As Dela mentioned, I got here looking for the opposite effect (the mesh defines where the layer IS visible), but I suppose thats not possible right? Anyway, that is awesome, certainly will help a lot!

    Thanks again!

  10. Warren
    June 19, 2014

    Thanks for this post! There have been tons of post online which have not directly addressed this problem. I hope that with Unity (post 4.3) they will start to add this feature in a more accessible way. After looking at your sample project I managed to get masking set up nicely in my own project. The only pontential “problem” is that you need to create a mesh to provide the occlusion for masking. In most cases, a quad will do. But with more complex shapes, addition work needs to be done to create the odd shapes.

    I hope this project helps others too!