Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Mesh Layer Mask?

Discussion in 'Shaders' started by daleo-dorito, May 11, 2024.

  1. daleo-dorito

    daleo-dorito

    Joined:
    Nov 28, 2023
    Posts:
    3
    So... I am kinda like really new to this stuff, specially I am a total noob on shaders, and I am pretty sure this should be done with a shader and I know you can like give a material alpha 0 to make it virtually invisible if its coordinate is like higher than X number for example. But is there a way to do this dinamically?

    Is there a way to make sorta of a sprite mask but with 3d meshes? I would love to be able to tag certain meshes or colliders as "Layer Masks" and make the objects dissapear when they are inside them.

    Just a visual clarification. My question is: Is there a way to make the part of the grey cube inside that collider to have alpha 0?



    As I said, I am a total noob so... if I need some like raycast shenanigans or complex shader coding I am not going to grasp it very well so sry in advance
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,556
    You can make a shader that takes in the coordinates of your box or sphere or other simple shape, and then refuse to draw anything that is inside that volume. A few caveats: it wouldn't support a complex shape very easily, it won't fill in the hole on the side of the cutout area (e.g., your gray box would become a gray paperbag open on the cut side), and everything that you want to cut out would need to use that shader (e.g., the shader would go on the gray box).

    You can make a shader that draws an alpha of zero but all the depth information. If you put this on a custom render pass before other objects are rendered, then it would essentially block all of the other objects from drawing inside that volume. This would let you put this material on just the objects representing any cutout volume you wanted, it could be any mesh shape easily, but it would still have the hole problem where objects cut in half would show emptyness at the border.
     
  3. daleo-dorito

    daleo-dorito

    Joined:
    Nov 28, 2023
    Posts:
    3
    This is meant to do stuff like when moving a up/down door it doesn't clip through the ceiling or make certain attacks that take a wide area and I cannot just remove easily like big areas do not show outside the arena itself. So it doesn't need to be perfect since most of the time the transition is gonna be hidden by walls and stuff.

    How could I implement a shader like that? Cause what you are saying is basically my idea, but I don't know how to do that.