Search Unity

Mesh Topology Lines not rendered to Depth Texture?

Discussion in 'Shaders' started by look001, Jun 2, 2019.

  1. look001

    look001

    Joined:
    Mar 23, 2017
    Posts:
    111
    Hi everyone,
    i experimented a bit with the depth texture. I noticed that if a mesh has Lines Topology then the lines won't be rendered to the depth texture.

    This is what the scene looks like:
    scene.PNG

    And this is what the depth texture looks like:
    render.PNG

    In the wireframe shader i set ZTest to Always and ZWrite to On:
    Code (CSharp):
    1. Shader "Custom/Lines"
    2. {
    3.    SubShader
    4.    {
    5.        Pass
    6.        {
    7.            ZTest Always
    8.            ZWrite On
    9.  
    10.            CGPROGRAM
    11.            #pragma vertex vert
    12.            #pragma fragment frag
    13.            #include "UnityCG.cginc"
    14.  
    15.            struct vertexInput
    16.            {
    17.                float4 vertex : POSITION;
    18.                float3 texCoord : TEXCOORD0;
    19.            };
    20.  
    21.            struct vertexOutput
    22.            {
    23.                float4 pos : SV_POSITION;
    24.                float3 texCoord : TEXCOORD0;
    25.            };
    26.  
    27.            vertexOutput vert(vertexInput input)
    28.            {
    29.                vertexOutput output;
    30.                output.pos = UnityObjectToClipPos(input.vertex);
    31.                output.texCoord = input.texCoord;
    32.  
    33.                return output;
    34.            }
    35.  
    36.            float4 frag(vertexOutput input) : COLOR
    37.            {
    38.                return float4(1,1,1,1);
    39.            }
    40.  
    41.            ENDCG
    42.        }
    43.    }
    44. }
    I simply render only the depth with post processing. This is the Post Processing shader:
    Code (CSharp):
    1. Shader "Post/DepthTexture"
    2. {
    3.     SubShader
    4.     {
    5.         Pass
    6.         {
    7.             CGPROGRAM
    8.             #pragma vertex vert
    9.             #pragma fragment frag
    10.  
    11.             #include "UnityCG.cginc"
    12.  
    13.        
    14.  
    15.             struct appdata
    16.             {
    17.                 float4 vertex : POSITION;
    18.                 float2 uv : TEXCOORD0;
    19.             };
    20.  
    21.             struct v2f
    22.             {
    23.                 float2 uv : TEXCOORD0;
    24.                 float4 vertex : SV_POSITION;
    25.             };
    26.  
    27.             v2f vert(appdata v)
    28.             {
    29.                 v2f o;
    30.                 o.vertex = UnityObjectToClipPos(v.vertex);
    31.                 o.uv = v.uv;
    32.                 return o;
    33.             }
    34.  
    35.             sampler2D _CameraDepthTexture;
    36.  
    37.             float4 frag(v2f input) : SV_Target
    38.             {
    39.                 float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, input.uv);
    40.                 depth = Linear01Depth(depth);
    41.  
    42.                 return depth;
    43.             }
    44.  
    45.             ENDCG
    46.         }
    47.     }
    48. }
    I enabled the depth texture for the camera and adjusted the far clipping plane to get this result.
    If the lines intersect with the cube the z culling works fine. Only the depth texture is missing the lines...
    So is it on purpose that the lines are not rendered to the depth texture? Can i somehow enable it?
    Thank you very much!
     
    Last edited: Jun 2, 2019
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Unity's camera depth texture is generated with a separate pass of the scene using each shader's shadow caster pass.

    Your shader doesn't have a shadow caster pass, or a fallback which has one, so it doesn't get rendered into the depth texture.
     
    Thomas-Mountainborn likes this.
  3. look001

    look001

    Joined:
    Mar 23, 2017
    Posts:
    111
    Amazing, it works! Thank you so much