Search Unity

TextMesh Pro TMP Mask Shader

Discussion in 'UGUI & TextMesh Pro' started by bradbecker, May 7, 2019.

  1. bradbecker

    bradbecker

    Joined:
    Dec 26, 2014
    Posts:
    130
    @Stephan_B I'm using the mobile mask shader for TMP and not having any luck. Is there documentation somewhere on how it works? Does the Mask Texture use alpha or white/black to determine cutoff? Does the Clip Rect affect how the Mask Texture is stretched across the paragraph that it is masking? What about Scale X and Scale Y?

    I ask because basically any mask texture is acting as if it's a single point. If Wipe Control is past a certain point, it's all on. Otherwise the whole TMP is masked off.
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Take a look at Example 19 included in the TMP Examples & Extras.
     
  3. bradbecker

    bradbecker

    Joined:
    Dec 26, 2014
    Posts:
    130
    @Stephan_B - that example is using a separate mask. I'm talking about the TextMesh Pro mobile masking shader.
     
  4. bradbecker

    bradbecker

    Joined:
    Dec 26, 2014
    Posts:
    130
    @Stephan_B To be clear, I'm just looking for info on the TextMesh Pro UGUI Mobile Masking shader (the Debug Settings). The problem is that animating the Wipe Control is just fading in and out the entire TextMesh Pro UGUI object as if it was only sampling a single point.
     
  5. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Normally, the masking region is defined by the 2D RectMask when using the Canvas System. In this case, the 2D RectMask, defines the Clip Rect values.

    Outside of the canvas, you will need to manually or via some script adjust the ClipRect values.

    So create a normal <TextMeshPro> object and switch to the Material Preset as seen in the image and then tweak the Clip Rect properties is shown below.

    upload_2019-5-8_19-58-18.png

    upload_2019-5-8_19-58-36.png

    Certainly not the most intuitive. This is another one of these things I need to revisit at some point.
     
  6. bradbecker

    bradbecker

    Joined:
    Dec 26, 2014
    Posts:
    130
    I'm using a TextMesh Pro UGUI components though. Are you saying that the Clip Rect properties in the Debug Settings are overwritten by the bounds of the Rect Transform of the gameobject? It's currently only fading the entire TMP UGUI object out as if the entire object was covered by just a few pixels of mask or something.
     
  7. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    When using the Canvas System, the Clip Rect is controlled by the 2D RectMask.

    It is fading because the region is super large and as such give the illusion of fading. Add a 2D RectMask as a parent. This results in the Clip Rect (internally) matching the RectTransform of the 2D RectMask.
     
  8. bradbecker

    bradbecker

    Joined:
    Dec 26, 2014
    Posts:
    130
    So I can't just set the Clip Rect fields in the Debug Settings area?
     
  9. bradbecker

    bradbecker

    Joined:
    Dec 26, 2014
    Posts:
    130
  10. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    You might be able to use CanvasRenderer.EnableRectClipping to set the clip rect on the specific object. I have not tested this but it could work.
     
  11. bradbecker

    bradbecker

    Joined:
    Dec 26, 2014
    Posts:
    130
    So what's the purpose the the Clip Rect fields in the debug settings then?
     
  12. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    It is the property used by the Canvas system / 2d RectMask which defines the clipping region. The 2D Rect Mask uses (and overrides these values) when using the Canvas system. The Canvas system sets that up per Canvas Renderer whereas on the individual material it is for each text object that shares this material.
     
    Last edited: May 16, 2019
  13. bradbecker

    bradbecker

    Joined:
    Dec 26, 2014
    Posts:
    130
    I've seen examples where it's set to {-10, 10, -10, 10} and others where it's in the -32k to 32k range. Does it matter what this is set to? Either way, it doesn't seem to help my problem with the whole text fading out as if it was covered by a single pixel of the mask texture...
     
  14. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The size of the RectTransform that contains a 2D RectMask defines the size of the Mask / Clipping region.




    upload_2019-5-16_22-24-29.png

    The fading part is simply an illusion / the result of the fact the Clip Rect is super large and as such the gradient in the texture that is used to fade / soften the edge is so spread that everything fades at once.

    To reproduce this, create a new scene and add a Canvas. Create a GameObject as a child of the Canvas. Add a 2D RectMask to it.

    upload_2019-5-16_22-28-3.png

    Add a child TextMeshProUGUI object as seen below.

    upload_2019-5-16_22-31-5.png

    Select the same shader as above and add the Mask Texture (Wipe Pattern Circle) which is included in the TMP Examples & Extras.

    upload_2019-5-16_22-32-18.png

    As you can see above, the size of the circle matches the size of the parent RectTransform that contains the 2D RectMask. Here I have good control over the softness on the edge and wipe, etc.

    Now if you change the size of the parent RectTransform and set to 1000 x 1000 but keep the text object size at 100 x 100, when you now move the Edge Softness and Wipe Position, you will see that the object now appears to fade. At 1000 X 1000, we can still see part of the edge / softness but at 32K X 32K using a 64 pixel x 64 pixel texture, the image simple turns on or off (almost).

    Ie. The size of the 2D RectMask RectTransform defines the Clip Rect which the Canvas system sets to infinity unless the parent has a 2D RectMask on it.

    I believe using https://docs.unity3d.com/ScriptReference/CanvasRenderer.EnableRectClipping.html will allow you to define the clipping region manually via code.
     
  15. bradbecker

    bradbecker

    Joined:
    Dec 26, 2014
    Posts:
    130
    Thanks! Will do...
     
  16. Lynxed

    Lynxed

    Joined:
    Dec 9, 2012
    Posts:
    121
    Sorry for necroposting, but if you are experiencing problems with arbitrary masking, and your project is old enough, try recreating font assets from scratch. Simple reimport and "regenerating textures" won't work.
    I had a case, where arbitrary image masking did not work, because my font assets were generated at a time when TMPro just became a part of Unity official team, so stuff was being ironed out back then.
     
    chuckyluv869 likes this.