Search Unity

Question Transparent and Depth of Field approach?

Discussion in 'Shaders' started by SpookyCat, Sep 5, 2022.

  1. SpookyCat

    SpookyCat

    Joined:
    Jan 25, 2010
    Posts:
    3,767
    I wanted to try and do a work around for rendering a transparent sheet that would work with the Depth of Field post effect in the built in pipeline. So I did a simple depth only shader and set the render queue to happen after solid geom but before transparent and it uses the offset option in the shader to render the depth a little bit further from the camera, and then a second material to to actually render the transparent sheet using the standard transparent shader. But this is not working, has anyone else tried anything else to get a transparent object to work with depth of field?

    Below is the simple depth only shader I am using in case I have missed something that someone might spot.
    Code (CSharp):
    1. Shader "Depth Only"
    2. {
    3.     SubShader
    4.     {
    5.         // Render the depth after regular geometry, but before transparent things.
    6.         Tags { "RenderType"="Opaque" "Queue"="Geometry+10" }
    7.        
    8.         // Don't draw in the RGBA channels; just the depth buffer
    9.         ColorMask 0
    10.         ZWrite On
    11.         Offset 0.5, 1
    12.          
    13.         Pass
    14.         {}
    15.     }
    16. }
     
  2. spakment

    spakment

    Joined:
    Dec 19, 2017
    Posts:
    96
    Bgolus' answer here has the issue explained
    https://forum.unity.com/threads/depth-of-field-issues-with-transparent-render-queue.1041292/
    The DoF is using the shadow pass, so the shader needs a shadow caster pass and should be in or below AlphaTest - your example is, so should be fine.
    A simple way to copy in a shadow caster pass is to add
    Code (JavaScript):
    1. Fallback "Mobile/VertexLit"
    At the end of the shader.

    More great info from the BGolus here
    https://forum.unity.com/threads/dof-on-transparent-shader.604363/
     
    Last edited: Sep 20, 2022