Search code examples
godotgdscript

Godot: Text on mesh surface


I want to add text on the surface of a mesh. Is there a simple way to do that through GDScript? The mesh could be a built in mesh object or an imported one (from blender for example).


Solution

  • If you mean text on a 3d mesh, here's how to do it:

    1. Add a Sprite3D

    2. Add a Viewport (plain) as a child of your Sprite3D

    3. Add a Label as a child of your Viewport

    4. Type the text you want to display into text, in the label

    5. Attach a script to the Viewport

    6. Put this inside:

      tool

      extends Viewport

      func _process(_delta):

      size = $Label.rect_size
      

    (Sorry about this code, it's not all fitting into one code, so just type it out)

    1. Go into the properties of the Viewport, and go under the section: Render Target
    2. Turn V Flip on
    3. (OPTIONAL) If you want to make the background transparent, turn Transparent on (near the top of Viewport)
    4. Now, go into Sprite3D, and add a texture (New ViewportTexture)
    5. Select the viewport which is a child of Sprite3D
    6. Now save and reload your scene

    If you want to increase the size, you have to follow these steps:

    1. Go to the Label -> Theme Overrides -> Fonts -> New DynamicFont -> Settings -> Size.
    2. If you change the size, it changes the font size.
    3. To actually display a font, you need to go to Font -> Load
    4. Now select a ttf file (font file)
    5. This will give you any font which you put in, as a ttf file. (Google fonts is a great source to find ttf files)

    Hopefully this works for you, if it doesn't tell me, and I will try fixing it!