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

Question Masking mesh of all child objects based on parent object mesh

Discussion in 'Shaders' started by stark89, Jan 14, 2022.

  1. stark89

    stark89

    Joined:
    Jul 5, 2012
    Posts:
    3
    Hello Everyone,

    I am not sure if this is possible to execute but i want to mask all the mesh in the child object based on the Parent objects mesh.

    Here is a better picture for what i am trying to achieve.
    image (1).png

    The two red cubes are set as the child of the big Green rectangle cube.
    Is there a way in which i can mask the portion lying outside the green rectangle making only the portion within the green rectangle visible?

    Thanks in advance
     
  2. fleity

    fleity

    Joined:
    Oct 13, 2015
    Posts:
    338
    search for stencil buffer
    using that you could achieve this. in URP I believe you have to set this up using custom renderer features (probably one for the green mask and one for the actual objects)
     
    stark89 likes this.
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Stencils won’t do exactly what you seem to want here. Stencils are a purely 2D screen space thing, which are very powerful and useful, but it won’t let you handle complex 3D intersections like this.

    If all you want is for the red objects to appear anywhere the green block is visible in 2D, then this will be perfect for you. If you want to handle 3D intersections you can get part of the way with a combination of ZWrite depth rendering of back faces of the green object and stencils. But it can’t handle hiding of red objects that are closer to the camera than the green object.

    Realistically the only 100% solution is to use a shader on the red objects that have a mathematical or 3D SDF representation of the parent object.
     
    fleity likes this.
  4. Annopolus

    Annopolus

    Joined:
    Apr 15, 2015
    Posts:
    18
    Hi!

    I have the same problem and looking exactly what you are looking for.
    Till now I found a way how to do the inverse mask:
    https://forum.unity.com/threads/solved-mask-shader-with-ztest-always.519094/
    https://answers.unity.com/questions/316064/can-i-obscure-an-object-using-an-invisible-object.html

    but completely dont know how (if possible) to provide changes in the shaders (render queue) to get the inverse effect (object inside the mask object should be visible, object outside the mask object will be hidden).
     
    stark89 likes this.