Search Unity

Is there a way to convert TextMeshPro text into an actual mesh?

Discussion in 'UGUI & TextMesh Pro' started by cxode, Nov 8, 2021.

  1. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    Hello!

    In my game, players can build structures. I'm working on a feature where players can export their builds to 3D modeling software. However, I'm having trouble including text in these exports.

    In-game:

    Exported:
    upload_2021-11-8_14-58-31.png

    Is there a way to convert the TMP SDF glyphs to a mesh? Ideally, I'd be able to choose the fidelity of the conversion (i.e. how many triangles are used). Alternatively, is there a better way to get the text geometry into Blender?
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The reason why the text renders as blocks in blender is because the text and texture that contains the glyphs is using Signed Distance Field which Blender isn't able to handle.

    The geometry of the text is (2) triangles per character. The shape of each character comes from the texture and is not geometry based.

    In order to export that text in Blender, you will likely have to render the text to a render texture which Blender would then be able to apply on the export objects.

    I have not personally tried to export to Blender so perhaps this is something that some other users have done and could provide some additional pointers / tips on here.
     
    cxode likes this.
  3. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    Ah, sticking the text on a render texture is a great idea, thanks!

    Is there some easy way to directly convert a TMP object to a render texture, or am I going to have to muck around with cameras and custom layers and stuff?
     
  4. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    Update, I mucked around with cameras and custom layers and stuff lol

    https://github.com/JimmyCushnie/JimmysUnityUtilities/blob/master/Scripts/TextMeshProToTexture.cs

    Usage:

    Code (CSharp):
    1. var renderer = TextMeshProToTexture.Create(layer: 30);
    2. Texture2D texture = renderer.Render(TextMeshProObject);
    3. renderer.Dispose();
    With this code, you should be able to render any TextMeshPro object to a texture of a given resolution. There is also an option to render many TMPs of different sizes with a consistent pixel density.

    Hopefully this code is useful to somebody else. If you find an issue with it let me know and I'll fix it.
     
    RobertVerdes likes this.