Text

April 25, 2020 ยท View on GitHub

Currently only TextMeshPro is supported, because they have a generated font atlas and it is easier to figure out how to scale the text to match its point size. Default UGUI Text components are not supported due to its reliance of the FontEngine and needing to sample on runtime the actual point sizes.

Conversion Pipeline

TextMeshProUGUIs are converted in several stages - as there are external components needed to render text.

  • FontAssetDelcarationSystem
  • FontAssetConversionSystem
  • TMPTextConverionSystem

FontAssetDeclarationSystem

The FontAssetDeclarationSystem declares that the TMP FontAsset's ScriptableObject dependency that all TextMeshProUGUI components needs to be represented as an entity.

FontAssetConversionSystem

This system, grabs all of the embedded FontAssets from TextMeshProUGUI and adds the following components to the FontAsset entity.

ComponentDescription
FontIDStores the unique instance ID for font asset - this is used for look up
GlyphElementStores the character's associated unicode, glyph metrics, raw uvs needed to display the font
FontFaceInfoStores Unity's FaceInfo data from the FontAsset

TMPTextConversionSystem

Grabs all TextMeshProUGUIs and adds the following components to the linked entity:

ComponentDescription
DimensionsThe rect size for displaying the text
TextFontIDThe mapping required to link to the FontID
TextOptionsThe font style, size, and alignment
CharElementStores all characters of a string
LocalVertexDataElementVertex information needed to make the mesh
LocalTriangleIndexElementMesh index information needed to store
LinkedMaterialEntityStores the entity that has the Material component object and is linked to the text entity.
MeshDataSpanStores the slice of vertex and index spans of the submesh that the text belongs to
AppliedColorThe general color of the text
BuildUIElementTagOptionally added if you want to reconstruct the root mesh
MaterialPropertyIndexStores the index used to access the Material Property

Storing Mesh Data

All letters of a mesh are built to the same vertex buffer, this batches all potential meshes such that there is only 1 issued draw calls. Generally, each letter's glyph is retrieved from the FontAsset entity, and the glyph metrics are applied to build the quad that will display. This takes into account font scaling so that larger point sizes match the editor time representation.

For a more detailed outlook of building text individually and rendering, take a look at the OpenGL tutorial, which affects the calculation to explains it quite nicely.