Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Can worldspace UI be made to write to the depth buffer?

Discussion in 'Unity UI & TextMesh Pro' started by Dreamback, Apr 16, 2018.

  1. Dreamback

    Dreamback

    Joined:
    Jul 29, 2016
    Posts:
    115
    I’m doing some post processing that requires every visible object write to the depth buffer, or that object’s pixels don’t get rendered. But I’ve got a worldspace canvas with a non-translucent panel and text(mesh pro) in the scene, and they don’t appear to be writing to the depth buffer.

    Is there a way to force UI elements to write to the depth buffer? Is there some hidden shader we can edit or something?
     
  2. Kerrjgan

    Kerrjgan

    Joined:
    May 30, 2016
    Posts:
    4
    Did you ever solve this?
     
  3. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    512
    For UI in general: No, this is not possible, since the UI is always rendered in the transparent pass, in which you can't write to the Z-Buffer.

    For TextMeshPro-Objects: The non UI Version can write to the depth buffer, but you will need to modify the shader a bit to do so. When you want to write to the depth buffer, you of course can't use the transparent pass as well, so something like glow won't work either (or only with a second render pass).
    Here is a simple example shader that writes to the depth buffer with TMP:

    Code (CSharp):
    1. // Simplified version of the SDF Surface shader :
    2. // - No support for Bevel, Bump or envmap
    3. // - Diffuse only lighting
    4. // - Fully supports only 1 directional light. Other lights can affect it, but it will be per-vertex/SH.
    5. // Modified by Johannes Deml
    6.  
    7. Shader "TextMeshPro/Mobile/Distance Field (Cutout)" {
    8.  
    9. Properties {
    10.     _FaceTex            ("Fill Texture", 2D) = "white" {}
    11.     _FaceColor            ("Fill Color", Color) = (1,1,1,1)
    12.     _FaceDilate            ("Face Dilate", Range(-1,1)) = 0
    13.  
    14.     _OutlineColor        ("Outline Color", Color) = (0,0,0,1)
    15.     _OutlineTex            ("Outline Texture", 2D) = "white" {}
    16.     _OutlineWidth        ("Outline Thickness", Range(0, 1)) = 0
    17.     _OutlineSoftness        ("Outline Softness", Range(0,1)) = 0
    18.  
    19.     _GlowColor            ("Color", Color) = (0, 1, 0, 0.5)
    20.     _GlowOffset            ("Offset", Range(-1,1)) = 0
    21.     _GlowInner            ("Inner", Range(0,1)) = 0.05
    22.     _GlowOuter            ("Outer", Range(0,1)) = 0.05
    23.     _GlowPower            ("Falloff", Range(1, 0)) = 0.75
    24.  
    25.     _WeightNormal        ("Weight Normal", float) = 0
    26.     _WeightBold            ("Weight Bold", float) = 0.5
    27.  
    28.     // Should not be directly exposed to the user
    29.     _ShaderFlags        ("Flags", float) = 0
    30.     _ScaleRatioA        ("Scale RatioA", float) = 1
    31.     _ScaleRatioB        ("Scale RatioB", float) = 1
    32.     _ScaleRatioC        ("Scale RatioC", float) = 1
    33.  
    34.     _MainTex            ("Font Atlas", 2D) = "white" {}
    35.     _TextureWidth        ("Texture Width", float) = 512
    36.     _TextureHeight        ("Texture Height", float) = 512
    37.     _GradientScale        ("Gradient Scale", float) = 5.0
    38.     _ScaleX                ("Scale X", float) = 1.0
    39.     _ScaleY                ("Scale Y", float) = 1.0
    40.     _PerspectiveFilter    ("Perspective Correction", Range(0, 1)) = 0.875
    41.  
    42.     _VertexOffsetX        ("Vertex OffsetX", float) = 0
    43.     _VertexOffsetY        ("Vertex OffsetY", float) = 0
    44.    
    45.     _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    46.    
    47.     //_MaskCoord        ("Mask Coords", vector) = (0,0,0,0)
    48.     //_MaskSoftness        ("Mask Softness", float) = 0
    49. }
    50.  
    51. SubShader {
    52.  
    53.     Tags {
    54.         "Queue"="AlphaTest"
    55.         "IgnoreProjector"="True"
    56.         "RenderType"="TransparentCutout"
    57.     }
    58.  
    59.     LOD 300
    60.     Cull [_CullMode]
    61.  
    62.     CGPROGRAM
    63.     #pragma surface PixShader Lambert vertex:VertShader noforwardadd nolightmap nodirlightmap fullforwardshadows addshadow alphatest:_Cutoff
    64.     #pragma target 3.0
    65.     #pragma shader_feature __ GLOW_ON
    66.  
    67.     #include "TMPro_Properties.cginc"
    68.     #include "TMPro.cginc"
    69.  
    70.     half _FaceShininess;
    71.     half _OutlineShininess;
    72.  
    73.     struct Input
    74.     {
    75.         fixed4    color        : COLOR;
    76.         float2    uv_MainTex;
    77.         float2    uv2_FaceTex;
    78.         float2  uv2_OutlineTex;
    79.         float2    param;                    // Weight, Scale
    80.         float3    viewDirEnv;      
    81.     };
    82.  
    83.     #include "TMPro_Surface.cginc"
    84.  
    85.     ENDCG
    86.  
    87.     // Pass to render object as a shadow caster
    88.     Pass
    89.     {
    90.         Name "Caster"
    91.         Tags { "LightMode" = "ShadowCaster" "RenderType"="TransparentCutout"}
    92.         Offset 1, 1
    93.  
    94.         Fog {Mode Off}
    95.         ZWrite On ZTest LEqual Cull Off
    96.  
    97.         CGPROGRAM
    98.         #pragma vertex vert
    99.         #pragma fragment frag
    100.         #pragma multi_compile_shadowcaster
    101.         #include "UnityCG.cginc"
    102.  
    103.         struct v2f {
    104.             V2F_SHADOW_CASTER;
    105.             float2    uv            : TEXCOORD1;
    106.             float2    uv2            : TEXCOORD3;
    107.             float    alphaClip    : TEXCOORD2;
    108.         };
    109.  
    110.         uniform float4 _MainTex_ST;
    111.         uniform float4 _OutlineTex_ST;
    112.         float _OutlineWidth;
    113.         float _FaceDilate;
    114.         float _ScaleRatioA;
    115.  
    116.         v2f vert( appdata_base v )
    117.         {
    118.             v2f o;
    119.             TRANSFER_SHADOW_CASTER(o)
    120.             o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    121.             o.uv2 = TRANSFORM_TEX(v.texcoord, _OutlineTex);
    122.             o.alphaClip = (1.0 - _OutlineWidth * _ScaleRatioA - _FaceDilate * _ScaleRatioA) / 2.0;
    123.             return o;
    124.         }
    125.  
    126.         uniform sampler2D _MainTex;
    127.  
    128.         float4 frag(v2f i) : COLOR
    129.         {
    130.             fixed4 texcol = tex2D(_MainTex, i.uv).a;
    131.             clip(texcol.a - i.alphaClip);
    132.             SHADOW_CASTER_FRAGMENT(i)
    133.         }
    134.         ENDCG
    135.     }
    136. }
    137.  
    138. CustomEditor "TMPro.EditorUtilities.TMP_SDFShaderGUI"
    139. }
    140.