Search Unity

TextMesh Pro How to draw TextMeshProUGUI always on top?

Discussion in 'UGUI & TextMesh Pro' started by JoeStrout, Jul 16, 2021.

  1. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I'm making some world-space canvases always draw on top (these are quest/POI markers) with this trick:

    Code (CSharp):
    1.     private const string shaderTestMode = "unity_GUIZTestMode"; //The magic property we need to set
    2.     UnityEngine.Rendering.CompareFunction desiredUIComparison = UnityEngine.Rendering.CompareFunction.Always;
    3. ...
    4.             materialCopy.SetInt(shaderTestMode, (int)desiredUIComparison);
    5.             materialCopy.renderQueue = 4000;    // (overlay)
    6.             graphic.material = materialCopy;
    This works great with most UI elements, such as Image. But it does not work for TMPro text. So my canvases show up just fine except for the text.

    Is there any easy way to make TMPro honor this variable, or otherwise get it to draw on top?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Curiously, when I look at TMP_SDK-Mobile.shader, I see

    Code (CSharp):
    1.     ZTest [unity_GUIZTestMode]
    which suggests that @Stephan_B already considered this, and it ought to work. But it's not working for me. So probably I'm not doing it properly, or at the right time, or something.

    Anyone have any ideas?
     
  3. Josiah_Ironclad

    Josiah_Ironclad

    Joined:
    Sep 24, 2019
    Posts:
    156
    I found this a while back which I'm currently using for one of my projects.
    Though from implementing it, it seems the second Custom Pass is completely unnecessary, as disabling it changes nothing. I have it disabled but haven't removed it just in case.

    Though I think this requires HDRP (maybe also works for URP).
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yikes! That seems like a very heavy-handed solution. I appreciate it, though; if nothing else can work then we'll decide between this and just using the old UI.Text components for the couple of places where we need this.

    (Incidentally, in case it matters: we're using the standard rendering pipeline.)
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Still stuck on this. That ZTest [unity_GUIZTestMode] in the shader source indicates that this ought to work. It's got to be that somehow we're not updating the material that is actually in use, but I can't see what I'm missing.

    @Stephan_B, any ideas? Or anyone else?
     
  6. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Are you using <TextMeshPro> components or the <TextMeshProUGUI> components? If <TextMeshProUGUI> components, they should behave as expected and the same way as UI.Text components.

    When using the Canvas system, sorting order is determined by the order of objects in the scene hierarchy. You can nest Canvases and set a different sort order on the child canvas but I believe within those canvases child, again the sorting order is determined by the order of the objects in the scene hierarchy.

    Since the Canvas system controls / sets the ZTest [unity_GUIZTestMode] based on the Canvas Render Mode, it is likely overriding your changes.

    Any reason you didn't try the Mobile Distance Field Overlay Shader included in the TMP Essential Resources?
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I am using TextMeshProUGUI, but it's not working. All other UI elements come out on top with code in the OP, but the TextMeshProUGUIs are stubbornly occluded by any geometry in front of them.

    Hmm. This is a World Space canvas (as it must be for our needs). Would you expect that to work?

    Only that I've never heard of it. Some googling turns up a mention of it on the Shaders page of the docs, but no description of what it does.

    I'll give it a try as soon as I get back to my desk.
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    To follow up: Using the Distance Field Overlay shader seems to work for TextMeshProUGUI objects. That's good enough for my purposes. Thank you for pointing it out!
     
  9. april_4_short

    april_4_short

    Joined:
    Jul 19, 2021
    Posts:
    489
    Nothing weird there... I was using it up until about two months ago, for a year, not realising what the "Overlay" part meant, and thinking it was just the most performant TMP renderer because of ... I dunno why... when one day I saw it poking through the floor, and asked WHY is it coming through the floor???