Search Unity

TextMesh Pro TMP and vertex changes

Discussion in 'UGUI & TextMesh Pro' started by dadude123, Jan 28, 2018.

  1. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    789
    Hello,
    I'm currently creating a variety of vertex modifiers for text mesh pro and I have a few questions.

    1. When exactly is `TMPro_EventManager.TEXT_CHANGED_EVENT` called? Everytime the mesh is "regenerated"? When is that? It seems to also get regenerated when the maxVisibleCharacters property is changed. Is it called when ANYTHING gets changed? Does maxVisibleCharacters cause a mesh rebuild?

    2. Why is the parameter of TMPro_EventManager.TEXT_CHANGED_EVENT "object", why not TMP_Text? What else can it contain?

    3. When exactly do I have to call textComponent.ForceMeshUpdate(); ? Only if I want changes (for example .text, or .fontSize or so) to get applied to the mesh instantly? Or do I also have to call it at least once?
    Will it trigger TEXT_CHANGED_EVENT ?

    4. Can I emulate maxVisibleCharacters myself by simply modifying the vertex color alpha? Are there any gotchas I should be aware of if I'd do that? Does maxVisibleCharacters just set isVisible to false and then it won't generate any vertices for characters beyond? Or does it work in a different way?

    5. textInfo.CopyMeshInfoVertexData(); sounds like it creates an actual copy, as in it allocates it and returns it to me, is that right? Is there a way to get a shared buffer filled maybe?
    What does it do internally? Is it (as I expect) just a helper function that creates a copy of textInfo.meshInfo for me?

    6. In the example (VertexJitter) vertices are copied back in some additional step (// Push changes into meshes). When is that needed? And when can we just modify the original data and then use .UpdateVertexData();??

    7. Changes to the vertex data are not reset by TMP unless someone calls ForceMeshUpdate(), but ForceMeshUpdate() is called indirectly when some property has changed; but it is also delayed until the next time the mesh should get rendered (probably to prevent regenerating the mesh for every change when multiple things are changed in one frame) , right?


    All in all I'm trying to get a solid understanding of what exactly is happening when, and how that affects me because for some effects I have to cache quite a bit of data which is somewhat expensive to generate in some cases/for some effects.
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    Whenever a property of the text object is changed, it will schedule a regeneration of the text object which will happen onPreCull of the camera.

    Because multiple text objects will fire the event so you have to make sure you are handling the correct object.

    Whenever you want the change to a given property or properties to take affect right away instead of waiting for those changes to be processed when onPreCull happens.
    maxVisibleCharacters simply marks the vertices of the characters that exceed it as degenerates.

    This is simply used to retain a copy of the unmodified vertex attributes. Some visual effects you want to be cumulative where you keep adjusting some values over time whereas other effects you want to always modify based on the original layout / vertex attributes. It all really depends on the visual effect / modifications you are trying to do.

    The mesh data is contained in the meshInfo and eventually pushed into the mesh class which is set on the renderer. So whenever a property is changed, that is unless the renderMode is set TextRenderFlags.DontRender the mesh will get updated and those changes pushed to the renderer. These steps and again depending on the visual FX, require that you know if you did let TMP update the mesh or if you are stopping it from doing it via the DontRender flag.

    Whenever a property of the text object is changed, TMP will regenerate it. You have to decide if you want TMP to make those changes or if you want to use the DontRender flag to control all of that. Most of the examples like Jitter, grab the resulting mesh data from the mesh Info and then override / push a new mesh to the renderer.

    Again, the mesh is regenerated late in the update cycle which is just before culling takes place in onPreCull. If 10 properties are changes, the text will only get regenerated once to account for all the changes. When calling ForceMeshUpdate() is will simply process any of the changed properties right at that time. Then unless other properties were changed before onPreCull, the text would not get generated again that frame. If properties were changed after ForceMeshUpdate() and before onPreCull then the text would get regenerated again that frame.
     
    dadude123 likes this.
  3. bourriquet

    bourriquet

    Joined:
    Jul 17, 2012
    Posts:
    181
    How does that explain why it is an Object rather than a TMP_Text? Is this object always a text object or can it be something else?