Search Unity

Question Hide part of texture behind another texture

Discussion in 'Shader Graph' started by Vacummus, Apr 18, 2020.

  1. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    Working on a isometric 2D game (using sprites), and I am trying to solve a problem where I want to hide the feet of a character when they walk into a puddle. How can I go about accomplishing this with shader graph?

    Currently, when the character walks over the puddle sprite, his feet are drawn on top of it:



    But what I would like is for the feet to hide behind the puddle sprite like so:



    Also, I am aware that I can solve this problem without shaders by splitting the character sprite into two sprites (feet and rest of the body) and just layering the puddle sprite between them. But that's not an option for me as the game will have different sizes of puddles where for some of them the character can be knees deep in the water.
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi,
    Just an idea, maybe you could make a shader for that character, which allows you to hide part of the sprite by just writing to the alpha channel. This could be easily accomplished at least math-wise and how the effect itself can be made. Then detect when the character is walking into water and change that effect parameter in your shader.
    This might of course leave some edge cases, like when your character is on the edge of the water...
     
  3. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    Yeah, it would not work for the edge cases, but it would get me half of the way there at least. How would you go about applying changes to only the bottom 10% of a texture?
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Well, in a shader you could just write using some logic (either branching with if) or some intrinsic function to write to the final texel's alpha channel. Which render pipeline are you using?

    And now that I thought about it, I'm not sure if shader graph supported sprites... Hmm. I'll update my post a bit later as I can't remember atm.
     
  5. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    I am using the latest universal render pipeline. It has a 2d renderer (currently experimental) that allows you to use shader graph on sprites.
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    You honestly probably don’t want to solve this with Shader Graph, or really any custom shader or content work.

    Use a sprite mask.
     
  7. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    Tried using sprite mask, but its too limiting for my use case. When I create a mask of the puddle and the character walks into it, it masks out all of his legs:



    I just want the feet to be masked with the rest of body still being drawn over the puddle.
     
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Don't use the water sprite as a mask. There's no reason the mask itself even needs to be visible. Use a box or some wavy shape sprite texture (not sprite component) that's over the feet as the mask.
     
    Vacummus likes this.
  9. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    Ahh. That should do it. I can even programmatically move that sprite mask as the player goes into deeper areas of a puddle. Great solution. Thank you.
     
  10. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Yes well I was thinking if you want to have the "mask" texture animated, then a custom shader or shader graph would be a lot better. i.e. something like this:

    animated_mask.PNG

    And you could do pretty much anything... and it would be neatly packaged in a shader/graph.
    But like @bgolus said, mask probably works best as a mask.
     
    Vacummus likes this.