Search Unity

Question Depth Based Shader

Discussion in 'Shader Graph' started by Digital_Seven, Apr 1, 2020.

  1. Digital_Seven

    Digital_Seven

    Joined:
    Aug 20, 2015
    Posts:
    15
    I'm trying to make a shader that will cut away at obstacles that are obscuring the player from the camera.

    I have a kind of solution, but it doesn't take depth into account (nor does it the actual player's position). I've been banging my head against this for almost 2 days now. There just aren't any tutorials on how to make this work. I swear that once I get this working I will make one.

    Could anyone help me, or point me in the right direction?

    My goal (approximate)


    What I've got:
    Graph 2.png

    It doesn't look like much, but I swear getting that circle was a while... I had other solutions that were just bad. I've only started working with shaders yesterday, I'm fairly competent in coding.
     
  2. Elizabeth_LeGros

    Elizabeth_LeGros

    Unity Technologies

    Joined:
    Jan 7, 2020
    Posts:
    50
    Does it need to take depth into account? What do you hope to accomplish with the depth?
    As for player position I would just calculate that in C# land and pass that as a parameter to the shader.
     
  3. Digital_Seven

    Digital_Seven

    Joined:
    Aug 20, 2015
    Posts:
    15
    I figured it would be depth-based because it would be only affecting objects between the camera and the player, or are you suggesting I do a material swap based on the distance between the objects I'd like to have this effect on?
     
  4. Elizabeth_LeGros

    Elizabeth_LeGros

    Unity Technologies

    Joined:
    Jan 7, 2020
    Posts:
    50
    Ah I see what you are saying. Youll only be able to check depth if you mark the object as transparent, and having a lot of objects then marked as transparent isn't ideal. You could control that swap from c# to at least be better (ie only setting the material to a transparent version if it gets close enough to obstructing the player in screen space).
     
  5. Digital_Seven

    Digital_Seven

    Joined:
    Aug 20, 2015
    Posts:
    15
    edit: I think I understand what you were saying about checking depth if it's transparent. Yea, that overhead is less than ideal. Did I mention I'm using Universal RP? Seems like a C# solution might be the only way

    I'm understanding that you're suggesting an alternative material with reduced opacity, having the whole object isn't my goal. I'd like to just add, if it's possible, a way to only apply the "Circle" part of my shader IF it's obstructing the player. But I could control adding this material to my objects in C# based on some control code. I'd really like to know if it's possible through the shader alone though.
     
    Last edited: Apr 1, 2020
  6. Elizabeth_LeGros

    Elizabeth_LeGros

    Unity Technologies

    Joined:
    Jan 7, 2020
    Posts:
    50
    Ill think about it a bit, but I dont _think_ there is a way to do this purely in shadergraph or even in straight shader code.
     
  7. Mauri

    Mauri

    Joined:
    Dec 9, 2010
    Posts:
    2,665
  8. Digital_Seven

    Digital_Seven

    Joined:
    Aug 20, 2015
    Posts:
    15
    I'd actually seen that first one a few times. I've got what I have currently because of it, but the main issue I have with it is that it uses "Stencil" and as far as I can tell this isn't a feature in URP.

    I'll continue to tinker with it today to see how far I get. So far it seems that there will need to be some lifting on the C# side of things. I have some ideas
     
    Last edited: Apr 2, 2020