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

Question Backface is not getting DOF correctly. (GIF and Shader attached)

Discussion in 'Shaders' started by marchall_box, Sep 22, 2021.

  1. marchall_box

    marchall_box

    Joined:
    Mar 28, 2013
    Posts:
    139
    Hello,

    I have this problem where the backface render of the mesh is not getting DOF properly.
    The backface is almost getting faded away like a transparent object with a low value of alpha.
    ezgif-1-de86ae8f82ee.gif


    I am using Cull Off on a custom surface shader like below.
    Code (CSharp):
    1. Shader "Custom/NewSurfaceCullOff"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    8.         _Metallic ("Metallic", Range(0,1)) = 0.0
    9.     }
    10.     SubShader
    11.     {
    12.         Tags { "RenderType"="Opaque" }
    13.         LOD 200
    14.         Cull Off
    15.         CGPROGRAM
    16.         #pragma surface surf Standard fullforwardshadows
    17.         #pragma target 3.0
    18.         sampler2D _MainTex;
    19.         struct Input
    20.         {
    21.             float2 uv_MainTex;
    22.         };
    23.         half _Glossiness;
    24.         half _Metallic;
    25.         fixed4 _Color;
    26.         void surf (Input IN, inout SurfaceOutputStandard o)
    27.         {
    28.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    29.             o.Albedo = c.rgb;
    30.             o.Metallic = _Metallic;
    31.             o.Smoothness = _Glossiness;
    32.             o.Alpha = c.a;
    33.         }
    34.         ENDCG
    35.     }
    36.     FallBack "Diffuse"
    37. }
    I am using PostProcessing Stack v2 with Built-In RP.
    Unity 2019.4.29f1.
    In order to get DOF properly for the backface, what should I do?
     
    Last edited: Sep 22, 2021
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Depth of field uses the camera depth texture, which is a screen space texture that has the depth of the closest opaque object at each pixel. When using forward rendering, this is created by rendering all the visible opaque objects using their shadow caster pass. In the example you have above, the shadow caster pass is coming from the fallback, and the diffuse shader’s shadow caster is one sided.

    The solution is to use a custom shadow caster pass rather than one from a fallback. For a Surface Shader you can accomplish this by adding
    addshadow
    to the
    #pragma surface
    line.
     
    Ruchir and marchall_box like this.
  3. marchall_box

    marchall_box

    Joined:
    Mar 28, 2013
    Posts:
    139
    @bgolus Thank you. You saved my life.