Search Unity

TextMesh Pro Textmesh Pro Text clipping with custom 3D material on Android

Discussion in 'UGUI & TextMesh Pro' started by dkilford, Jun 5, 2018.

  1. dkilford

    dkilford

    Joined:
    Jan 24, 2014
    Posts:
    5
    Hi,

    I'm currently trying to resolve an issue where UGUI Text Mesh Pro text is incorrectly clipping with a material using a custom shader used in the world space when displaying on android using OpenGL ES 2.0.

    I'm guessing I'm missing a keyword in my shader that is causing a weird interaction with TMPro's UGUI Text. It seems to work fine in the editor, and I can see the problem when using both the regular SDF shader and the mobile SDF shader.

    I have seen the problem before and managed to fix it by increasing the Material Render Queue properties on the font material, but it's stopped working today and I have absolutely no idea why.

    Using Unity 2018.1.0f2

    Example: Screenshot_2018-06-05-16-46-54.png

    The parts of the text are disappearing when drawn in a UGUI canvas infront of 3d objects using this custom shader:
    Code (CSharp):
    1. Shader "Custom/Diffuse"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "RenderType"="Opaque" }
    10.  
    11.         Pass
    12.         {
    13.             Tags{"LightMode" = "ForwardBase"}
    14.  
    15.             CGPROGRAM
    16.             #pragma vertex vert
    17.             #pragma fragment frag
    18.             #pragma multi_compile_fog
    19.             #pragma multi_compile_fwdbase
    20.          
    21.             #pragma fragmentoption ARB_precision_hint_fastest
    22.             #pragma target 2.0
    23.             #pragma hardware_tier_variants gles
    24.          
    25.             #include "UnityCG.cginc"
    26.             #include "AutoLight.cginc"
    27.             #include "Lighting.cginc"
    28.  
    29.             struct appdata
    30.             {
    31.                 float4 vertex : POSITION;
    32.                 float2 uv : TEXCOORD0;
    33.                 half3 normal : NORMAL;
    34.             };
    35.  
    36.             struct v2f
    37.             {
    38.                 float2 uv : TEXCOORD0;
    39.                 LIGHTING_COORDS(1, 2) // Store the lighting coords in texcoords 0 and 1.
    40.                 UNITY_FOG_COORDS(3)
    41.                 float4 pos : SV_POSITION;
    42.                 fixed4 vertexColour : COLOR;
    43.             };
    44.  
    45.             sampler2D _MainTex;
    46.             float4 _MainTex_ST;
    47.          
    48.             v2f vert (appdata v)
    49.             {
    50.                 v2f o;
    51.                 o.pos = UnityObjectToClipPos(v.vertex);
    52.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    53.  
    54.                 UNITY_TRANSFER_FOG(o,o.pos);
    55.                 TRANSFER_VERTEX_TO_FRAGMENT(o);
    56.  
    57.                 float3 n = UnityObjectToWorldNormal(v.normal) * 0.3f;
    58.                 o.vertexColour = _LightColor0 * saturate(4 * dot(n, _WorldSpaceLightPos0.xyz) + 0.1f);
    59.                 o.vertexColour.a = 1;
    60.              
    61.                 return o;
    62.             }
    63.          
    64.             fixed4 frag (v2f i) : SV_Target
    65.             {
    66.             #if !UNITY_HARDWARE_TIER1
    67.                 fixed atten = LIGHT_ATTENUATION(i);
    68.                 fixed4 col = tex2D(_MainTex, i.uv) * i.vertexColour * saturate(unity_AmbientSky + fixed4(atten, atten, atten, 1));
    69.                 // apply fog
    70.                 UNITY_APPLY_FOG(i.fogCoord, col);
    71.             #else
    72.                 fixed4 col = tex2D(_MainTex, i.uv) * i.vertexColour;
    73.             #endif
    74.                 return col;
    75.             }
    76.             ENDCG
    77.         }
    78.  
    79.         UsePass "Custom/ShadowShader/SHADOWCASTER"
    80.     }
    81.  
    82.     FallBack "Diffuse"
    83. }
    84.  
    Thanks
     
  2. dkilford

    dkilford

    Joined:
    Jan 24, 2014
    Posts:
    5
    Solved the problem through a bunch of digging around.

    In my TMP_SDF-Mobile.shader file I made these changes:

    At the end of the Shader Properties I added:
    [Enum(UnityEngine.Rendering.CompareFunction)] unity_GUIZTestMode("ZTest", Float) = 4

    In the subshader I changed:
    ZTest [unity_GUIZTestMode]

    To
    ZTest [Always]

    The bug is also reproducible on iOS.

    EDIT
    Making these changes causes another problem alone doesn't fix the problem. I had to change my render queue on my font material. Changing it in the editor caused text to draw over UI elements.

    To fix this I made a script that when running on mobile edits render queue of the font meterial to a higher number.
     
    Last edited: Jun 6, 2018