Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Reverse Mask

Discussion in 'Shaders' started by raybarrera, Jun 9, 2013.

  1. raybarrera

    raybarrera

    Joined:
    Nov 20, 2010
    Posts:
    207
    Howdy,

    Here's what I'm trying to achieve:

    I want to do, effectively, what a RenderTexture does, but the problem with render textures is that the size of the mesh (and UVs) affect the image.

    I have once circular piece of geometry that will be the inverse mask, and another masked piece of geometry that sits behind it. I want to only be able to see the masked object when it's "inside" the circle from the camera's POV. However, I do not want to draw the mask itself, I only want to use it as a mask. The masked object should "disappear" as it moves out from behind the mask.

    Here is a visual example:
    $sample.png
    As indicated, the blue part of the masked object is visible, while the striped part is not, which is the part "outside" of the circular mask, which is invisible (though represented here visually as the gray circle).

    Any ideas?
     
  2. lmbarns

    lmbarns

    Joined:
    Jul 14, 2011
    Posts:
    1,628
    http://wiki.unity3d.com/index.php?title=DepthMask grab the sample scene and look at how the mesh on the boat stops the water from showing through the hull which is submerged below the water plane.

    I do the same thing with the regular mask to make a 3d hole you can look into through another mesh....like the game portal. It's awesome....

    $mask4.jpg $mask3.jpg $mask2.jpg $mask1.jpg



    Masking off under the ice plane, but from the top you can see into the hole, both the hole and the bottle are masked off below the surface plane, and a circular plane masks off the top ground plane so you can see into the hole:


    Basically you have to get creative with sandwiching meshes and setting their render queue to show behind one mesh and not behind another.
     
    Last edited: Jun 9, 2013
    domkia likes this.
  3. raybarrera

    raybarrera

    Joined:
    Nov 20, 2010
    Posts:
    207
    lmbarns, thanks for the response, this is very helpful.
    I'm going to play with this for a bit and see if I can figure it out.
     
  4. lmbarns

    lmbarns

    Joined:
    Jul 14, 2011
    Posts:
    1,628
    Not sure if it would be easiest to make a plane with a hole cut out of it, then just use it as the mask (it's shader as the mask) and the object would be visible if it moved behind the hole if you adjust it's render queue to I believe lower than 3010, play around with under 3010 and over 3010 on objects you put the script on. I was surprised with how simple the shader and script are, but they work really well, but I had to think about the meshes. These are the pieces I use to mask it all off:

    Early version before I fully masked off below the surface



    I spent all day yesterday working with it after discovering it on the wiki hehe
     
    Last edited: Jun 9, 2013
  5. Cat1201

    Cat1201

    Joined:
    Aug 31, 2013
    Posts:
    1
    I'm sorry. I didn't figure it out. How can I reverse the mask? I'm getting mess up with those layers and shaders... :confused::confused:
     
  6. Dream_Code_TM

    Dream_Code_TM

    Joined:
    Jul 19, 2013
    Posts:
    38
  7. trx88

    trx88

    Joined:
    Nov 30, 2015
    Posts:
    2