Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

HDRP DepthMask Possible?

Discussion in 'Shaders' started by Toxijuice, Jul 15, 2019.

  1. Toxijuice

    Toxijuice

    Joined:
    May 20, 2015
    Posts:
    3
    Hey there!

    I've been banging my head against this for a while but I cannot figure out if it is possible to create a DepthMask shader for HDRP (as descibed here).

    For my exact use, I'm trying to create a "hole" in the shape of whatever I have the material applied to, that shows the contents of a different camera rendered behind everything.

    I tried messing around with the render queue within the shader, different ZTest and ZWrite combinations, as well as some variations of the shader I found. On top of that, I tried messing around with every configuration of the base HDRP shaders I could think of that might do something.

    The closest I could get was making transparent materials behind the object vanish. This does mean that if I set all my Opaque materials to Transparent I get the effect I desire, however this isn't ideal.

    I'm not quite sure what to try next, any assistance would be greatly appreciated.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    6,972
    That technique works great for allowing boat interiors to sit below the water line, or to add holes into otherwise solid surfaces, but they can't be holes through a surface, only into it. For example: a golf game where you want to make the whole appear to go into the ground, or the example above of a boat's interior being below the water line. These work great. If you want a window or a doorway you can see beyond, it's still possible, but less ideal.

    Really it all comes down to the order you render all the objects in.
    1. First you need to render everything you don't want hidden.
    2. Then you need to render the depth mask.
    3. Then you need to render the objects you want to get masked.
    You should be able to do this with the HDRP, but you need to control more than just the queue of the depth mask shader / material on it's own.

    However if you're already rendering using another camera, why not render that to a render texture and then use an unlit object using screen space UVs to display that render texture? Much more straightforward.
     
    Toxijuice likes this.
  3. Toxijuice

    Toxijuice

    Joined:
    May 20, 2015
    Posts:
    3
    Hey thanks for the reply.

    I appreciate you confirming the steps of using the depth mask, though I do already understand that. In fact, it's pretty simple to set up the effect I want in the default pipeline.

    You hit the nail on the head, though, it would seem that the queue of the mask isn't enough. That's where my problem is, I can make the effect work with all transparent materials (even with alpha set all the way up so it appears opaque) because HDRP has a priority system exposed for transparency. The solution would be to find how to render the mask before all of the opaque geometry, and I can't find anything on that.

    As for why I don't use a Render Texture, that's actually my temporary solution right now. Like you said, it is much more straightforward, however not quite what I want. For whatever reason, it seems far more expensive to render to a texture than to just show the other camera's render. I also seem to have an issue with it lagging behind the main camera's movement, but that's likely just due to how quickly/when I'm updating it.

    I am definitely not opposed to other solutions to the problem, though. I am trying to create a Portal-like effect, where there's a portal or doorway that shows an area that is not physically there. A nice benefit to the Depth Mask is that it's not just rendered on a mesh, so if, for example, someone in VR sticks their head through they don't just see the void as you would with a Render Texture, but instead the actual geometry that they will see if they were to physically walk through.

    Another potential solution I will be looking into is using the Stencil Buffer. I know little about Stencil shaders, but have always assumed that they can mask geometry that *is* there, but is hidden away. I purposefully haven't put much effort into finding if it was a viable solution, though, because while looking around I saw reports of Stencil not working correctly in HDRP either.

    But yeah, like I said, I'm currently using Render Textures for now while working on other aspects of the project, but would love to find a better solution.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    6,972
    The HDRP is a deferred renderer. For the built-in deferred rendering path I know Unity ignores material queue for all deferred objects. This makes using depth masking (or stencils, which also require explicit ordering) to not work since you're left to the whim of Unity's sorting. It's possible the HDRP they've chosen to do the same thing and simply ignore the material queue for opaque deferred objects. That would certainly explain the behavior you're seeing. Not sure if there's a good way around that unfortunately beyond modifying the HDRP.

    Mutli-camera setups in general seem to be a huge pain for both SRPs as Unity designed both the LRWP and HDRP with the mindset you would never need to use a multi-camera setup. Plus multiple cameras seem to have gotten a lot more expensive in the last few years; we were doing multiple cameras in VR in the early days of PSVR without a significant performance penalty at 90 hz, and now just having a second camera with culling set to render basically nothing makes it difficult to keep the frame rate above 60 hz.
     
    Toxijuice likes this.
  5. Toxijuice

    Toxijuice

    Joined:
    May 20, 2015
    Posts:
    3
    Hm, I see. I had not realized that about deferred rendering, that certainly would explain what I'm seeing. I appreciate the insight. I suppose I'll just have to keep using my RT method and do my best to work around the limitations until something changes.