Search Unity

[SOLVED] URP Depth Of Field - Transparency / Render Queue Problem

Discussion in 'Universal Render Pipeline' started by Neohun, Apr 3, 2020.

  1. Neohun

    Neohun

    Joined:
    Oct 30, 2018
    Posts:
    77
    Hello,
    I have a problem with post-processing render queue and I'm not sure if it's a bug or not but I made a bug report one week ago and there was no reply yet I re-write them with an update but nope nothing again so I try my chance here.. The problem is that If I set the materials render queue higher than 2500-2550 then the DOF (depth of field) fx doesn't work properly as you can see in the attached screen-shots.. I can set separate material for these cutscene objects yeah but there're a lot of them and I want to try my chance here firstly.. So I hope I can find some help here..

    SS_Blurry.PNG

    SS_Normal.PNG
     
    Last edited: Apr 18, 2020
    luizero00 likes this.
  2. slumberface

    slumberface

    Joined:
    May 22, 2016
    Posts:
    15
    As far as I've come to understand, this is an inherent shortcoming with how transparency is dealt with. Transparent shaders don't write to the depth texture (forgive me if this is the wrong term), so when the post-processing pass is sampling vertex locations to apply DoF, it is actually reading what is behind the transparent object and applying DoF based on that distance to that part of the screen.

    This is not a bug with URP, and unfortunately is a really complex topic that currently doesn't seem to have easy or performance-friendly solutions that I have been able to find. Been struggling with this issue for a long time, and the best I could manage to do is to render the transparent object twice. Once with a camera that renders every layer with post processing, and then a camera that only renders the transparent objects on top of the first camera. This was in standard pipeline, and running 2 cameras would pull the framerate down from ~125fps to ~100fps.

    I know that URP now supports multi-camera, but I don't know if the implementation is viable in URP or not. In standard pipeline, the "transparent camera" would have it's clear flags set to "Don't Clear" and it would be ordered before the main camera, with everything culled except your transparent objects.

    My 2 cents: ditch DoF if you can OR ditch transparency. Trying to solve that problem was distracting me from spending time improving the game itself. As a side note, alpha cutout shaders respect the DoF because they render to geometry queue (and as such don't support semi-visibility/translucency)
     
    Last edited: Apr 5, 2020
    luizero00 and Neohun like this.
  3. Neohun

    Neohun

    Joined:
    Oct 30, 2018
    Posts:
    77
    Hello slumbarface, thanks for detailed reply and your interest,
    It doesn't seem to be a bug for me either but I wanted to be sure before changing every cutscene character's material as a geometry render queue material.. the fact that I don't need transparency but I use decal for bullet hole fx so I had to make my character materials render queue higher than decal's render queue which should be above "Alpha Test" (2450) queue to be rendered properly and by setting the char material render queue higher decal's I don't allow the decal to override my char materials and render properly so it seems that I have to go this way for now but if I find a valid fix I'll share here..
    Best Regards,
     
  4. Neohun

    Neohun

    Joined:
    Oct 30, 2018
    Posts:
    77
    Alright, after a further investigation I have found a great solution the key is the "stencil" and the new "Renderer Features". So I'll share the solution here for those who's struggling with the same problem..
    First of all as I mentioned before my problem was that I was setting my character materials render queue higher above the decal material render queue to don't let my bullet decals to override my characters but when I set the render queue above 2501 they're being rendered as transparent and with the DOF (depth of field) fx they were being rendered blurry..
    So I have searched to find a valid fix then I hit the stencil buffers which I didn't know much about it because I use only shader graph for create my shaders.. When I realized they're used to mask the rendering for the shaders I have checked the shader graph for stencil buffer feature but there was no support yet so I use the renderer features instead to write the stencil buffer for my character materials so I have masked my character layers and overridden the stencil value as "1" because decal material's stencil reference value was 0 and that means show the decal only if the other material's stencil value is 0.. Thus I don't have to set my character materials render queue higher so I set it as "Geometry" again.. This is the fix for my problem and here's the results You can see my settings and the results on the screenshots below..

    ss_settiings_stencilOverride_opaque.PNG ss_no stencil.PNG ss_stencil overridden.PNG

    Also I have checked if this fix works even when we set the material as transparent queue and it turned out actually stencil override fix the problem with DOF for the materials which is "opaque" but rendering in "transparent queue".. But It doesn't work for "actual transparent" materials because they don't write to depth buffer so if you want to fix the problem for actual transparent materials you have to override and write the depth buffer from the "renderer features" for layers which you want thus you'll have a solution for blurry transparent materials with DOF.. So here's the results the screenshot below.. Good luck for all..

    result_stencil  override for transparent queue materials.PNG

    transparent_depthWrite_results.png
     
    Last edited: Apr 19, 2020
    StenCG likes this.
  5. Waz

    Waz

    Joined:
    May 1, 2010
    Posts:
    287
    Should this work for any transparent material? I've tried with a plain URP "Lit" material set to Transparent, but the Depth Of Field still blurs those areas after making the above Renderer Features changes, exactly as shown. (Unity 2020.1.1f1). Have I missed some step?
     
  6. nhien_unity717

    nhien_unity717

    Joined:
    Aug 7, 2020
    Posts:
    1
    Hello, I've also been attempting something similar and still can't get it to work. Wondering if anyone else got any more information on this?
     
  7. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    13,363
    Same here, maybe is Unity version related ?
     
    Armegalo likes this.
  8. Sabrino

    Sabrino

    Joined:
    Aug 8, 2015
    Posts:
    36
    Same here, Unity 2020 LTS. DOF still blurs after overriding the depth settings in renderer features. Why?
     
  9. Neohun

    Neohun

    Joined:
    Oct 30, 2018
    Posts:
    77
    Hi, it should work properly for any materials if you set the renderer feature properly maybe you miss something.
    You should put the transparent object on the layer that you want to override the depth from renderer features.. then remove it from "Default Layer Mask" and select the layer you want to override under the "Filters" section (in my case in the images above it's "Enemy" layer).. the problem between the transparent objects and DOF is that transparent materials don't write to depth buffer so via renderer features you override to write to depth buffer to avoid this problem.. as the images shown above it works without any problem, I'm not sure whether it's related with unity version..
     
    Last edited: Sep 3, 2021
  10. Sabrino

    Sabrino

    Joined:
    Aug 8, 2015
    Posts:
    36
    Yeah, I made sure to set everything as you said, and as your screenshot above shows. Your explanation is very clear. I also tried setting an override material in the renderer feature like mentioned in another thread, but it does not make a difference. Really not sure what the problem is, since as you say, the problem with DOF should be that transparent objects don't write to depth.

    Tried changing shader, playing with the renderer feature settings, but nothing worked. Since the other posters in the thread weren't able to make it work, there must be something we're missing..
     
  11. Neohun

    Neohun

    Joined:
    Oct 30, 2018
    Posts:
    77
    If you did everything correctly, Maybe it's a bug in the new releases because I still use "2019.4 LTS" and cannot confirm with the new releases.. DOF works properly with opaque materials right? the only problem is about transparent materials? And I don't remember whether renderer feature use depth texture, maybe enable "Depth Texture" of the URP Asset settings and check the results..
     
    Last edited: Sep 4, 2021
  12. Sabrino

    Sabrino

    Joined:
    Aug 8, 2015
    Posts:
    36
    I finally found the problem! This fix only worked for me after I disabled MSAA in the URP quality settings asset.

    Thank you for the help Neohun.
     
  13. Neohun

    Neohun

    Joined:
    Oct 30, 2018
    Posts:
    77
    I'm glad you made it work. But it's strange though, why it's related with MSAA? it shouldn't be a problem at all.. It's more likely to be a bug or a problem about the rendering order or something..