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

HDRP DepthMask Possible?

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

  1. Toxijuice

    Toxijuice

    Joined:
    May 20, 2015
    Posts:
    5
    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:
    12,329
    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:
    5
    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:
    12,329
    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:
    5
    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.
     
  6. DonHaul

    DonHaul

    Joined:
    Mar 3, 2014
    Posts:
    3
    Hey I just found a way to make this work with HDRP! Besides adding the depth mask to occluder object, you do not add the SetRenderQueue script, instead, just add a transparent material to the object you want to oclude this way, its render queue is automatically set to 3000 (equivalent to transparency queue) Screenshot_15.png
     
  7. Levrden

    Levrden

    Joined:
    Jan 26, 2016
    Posts:
    16
    Hello!
    Since a lot of people is trying the same, is it possible to share your code please?
    So noobies like me could use it without understanding everything?

    Thanks!
     
    Sheynes and VirtualDawn like this.
  8. Sheynes

    Sheynes

    Joined:
    Mar 27, 2017
    Posts:
    66
    Last edited: Oct 14, 2020
    VirtualDawn likes this.
  9. VincentAbert

    VincentAbert

    Joined:
    May 2, 2020
    Posts:
    123
  10. adamgffrd

    adamgffrd

    Joined:
    Sep 26, 2018
    Posts:
    35
    @Sheynes & @DonHaul &@bgolus Just wanted to thank you all for providing the steps and information getting this up and running. I had it working in URP and just upgraded to HDRP. This saved me a TON of time. Thanks!
     
  11. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,068
    Cannot it be done without Custom Pass Volume ?
    This is quite complicated for something this common.
     
  12. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Not really that common.

    But that comment does pretty accurately describe doing anything in the HDRP or URP that's not already supported.
     
    hippocoder likes this.
  13. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,068
    As I know it is quite often used for making non glichy complex transparent objects.
    First render to depth buffer than render transparency, this prevents unsorted triangles bug.

    But you are right this is some kind of problem with HDRP, still it looks very good IMHO.
    As for my experience with it pros wins over cons it just that some important missing features are not limited by technology but by decision of not implementing them.
     
  14. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    That is built into the HDRP though. That's a check box on the lit material.
     
    koirat likes this.
  15. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,068
    I guess you got Depth Write checkbox in mind.
    Pity I can use Transparent surface only in transparent pass.