3d_text.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. .. _doc_3d_text:
  2. 3D text
  3. =======
  4. Introduction
  5. ------------
  6. In a project, there may be times when text needs to be created as part of a 3D
  7. scene and not just in the HUD. Godot provides 2 methods to do this: the
  8. Label3D node and the TextMesh *resource* for a MeshInstance3D node.
  9. Additionally, Godot makes it possible to position Control nodes according to a
  10. 3D point's position on the camera. This can be used as an alternative to "true"
  11. 3D text in situations where Label3D and TextMesh aren't flexible enough.
  12. .. seealso::
  13. This page does **not** cover how to display a GUI scene within a 3D
  14. environment. For information on how to achieve that, see the
  15. `GUI in 3D <https://github.com/godotengine/godot-demo-projects/tree/master/viewport/gui_in_3d>`__
  16. demo project.
  17. Label3D
  18. -------
  19. .. image:: img/label_3d.png
  20. Label3D behaves like a Label node, but in 3D space. Unlike the Label node, this
  21. Label3D node does **not** inherit properties of a GUI theme. However, its look
  22. remains customizable and uses the same font subresource as Control nodes
  23. (including support for :abbr:`MSDF (Multi-channel Signed Distance Font)` font
  24. rendering).
  25. Advantages
  26. ^^^^^^^^^^
  27. - Label3D is faster to generate than TextMesh. While both use a caching
  28. mechanism to only render new glyphs once, Label3D will still be faster to
  29. (re)generate, especially for long text. This can avoid stuttering during
  30. gameplay on low-end CPUs or mobile.
  31. - Label3D can use bitmap fonts and dynamic fonts (with and without
  32. :abbr:`MSDF (Multi-channel Signed Distance Font)` or mipmaps). This makes it
  33. more flexible on that aspect compared to TextMesh, especially for rendering
  34. fonts with self-intersecting outlines.
  35. .. seealso::
  36. See :ref:`doc_gui_using_fonts` for guidelines on configuring font imports.
  37. Limitations
  38. ^^^^^^^^^^^
  39. By default, Label3D has limited interaction with a 3D environment. It can be
  40. occluded by geometry and lit by light sources if the **Shaded** flag is enabled.
  41. However, it will not cast shadows even if **Cast Shadow** is set to **On** in
  42. the Label3D's GeometryInstance3D properties. This is because the node internally
  43. generates a quad mesh (one glyph per quad) with transparent textures and has the
  44. same limitations as Sprite3D. Transparency sorting issues can also become apparent
  45. when several Label3Ds overlap, especially if they have outlines.
  46. This can be mitigated by setting the Label3D's transparency mode to **Alpha
  47. Cut**, at the cost of less smooth text rendering. The **Opaque Pre-Pass**
  48. transparency mode can preserve text smoothness while allowing the Label3D to
  49. cast shadows, but some transparency sorting issues will remain.
  50. See :ref:`Transparency sorting <doc_3d_rendering_limitations_transparency_sorting>`
  51. section in the 3D rendering limitations page for more information.
  52. TextMesh
  53. --------
  54. .. image:: img/text_mesh.png
  55. The TextMesh resource has similarities to Label3D. They both display text in a
  56. 3D scene, and will use the same font subresource. However, instead of generating
  57. transparent quads, TextMesh generates 3D geometry that represents the glyphs'
  58. contours and has the properties of a mesh. As a result, a TextMesh is shaded by
  59. default and automatically casts shadows onto the environment. A TextMesh can
  60. also have a material applied to it (including custom shaders).
  61. Here is an example of a texture and how it's applied to the mesh. You can use
  62. the texture below as a reference for the generated mesh's UV map:
  63. .. image:: img/text_mesh_texture.png
  64. .. image:: img/text_mesh_textured.png
  65. Advantages
  66. ^^^^^^^^^^
  67. TextMesh has a few advantages over Label3D:
  68. - TextMesh can use a texture to modify text color on a per-side basis.
  69. - TextMesh geometry can have actual depth to it, giving glyphs a 3D look.
  70. - TextMesh can use custom shaders, unlike Label3D.
  71. Limitations
  72. ^^^^^^^^^^^
  73. There are some limitations to TextMesh:
  74. - No built-in outline support, unlike Label3D. This can be simulated using custom
  75. shaders though.
  76. - Only dynamic fonts are supported (``.ttf``, ``.otf``, ``.woff``, ``.woff2``).
  77. Bitmap fonts in the ``.fnt`` or ``.font`` formats are **not** supported.
  78. - Fonts with self-intersecting outlines will not render correctly.
  79. If you notice rendering issues on fonts downloaded from websites such as
  80. Google Fonts, try downloading the font from the font author's official
  81. website instead.
  82. Projected Label node (or any other Control)
  83. -------------------------------------------
  84. There is a last solution that is more complex to set up, but provides the most
  85. flexibility: projecting a 2D node onto 3D space. This can be achieved using the
  86. return value of :ref:`unproject_position<class_Camera3D_method_unproject_position>`
  87. method on a Camera3D node in a script's ``_process()`` function. This return value
  88. should then be used to set the ``position`` property of a Control node.
  89. See the `3D waypoints <https://github.com/godotengine/godot-demo-projects/tree/master/3d/waypoints>`__
  90. demo for an example of this.
  91. Advantages
  92. ^^^^^^^^^^
  93. - Any Control node can be used, including Label, RichTextLabel or even nodes such
  94. as Button. This allows for powerful formatting and GUI interaction.
  95. - The script-based approach allows for complete freedom in positioning.
  96. For example, this makes it considerably easier to pin Controls to the screen's
  97. edges when they go off-screen (for in-game 3D markers).
  98. - Control theming is obeyed. This allows for easier customization that globally
  99. applies to the project.
  100. Limitations
  101. ^^^^^^^^^^^
  102. - Projected Controls cannot be occluded by 3D geometry in any way. You can use a
  103. RayCast to fully hide the control if its target position is occluded by a
  104. collider, but this doesn't allow for partially hiding the control behind a
  105. wall.
  106. - Changing text size depending on distance by adjusting the Control's ``scale``
  107. property is possible, but it needs to be done manually. Label3D and TextMesh
  108. automatically take care of this, at the cost of less flexibility (can't set a
  109. minimum/maximum text size in pixels).
  110. - Handling resolution and aspect ratio changes must be taken into account in the
  111. script, which can be challenging.
  112. Should I use Label3D, TextMesh or a projected Control?
  113. ------------------------------------------------------
  114. In most scenarios, Label3D is recommended as it's easier to set up and provides
  115. higher rendering quality (especially if 3D antialiasing is disabled).
  116. For advanced use cases, TextMesh is more flexible as it allows styling the text
  117. with custom shaders. Custom shaders allow for modifying the final geometry, such
  118. as curving the text along a surface. Since the text is actual 3D geometry, the
  119. text can optionally have depth to it and can also contribute to global
  120. illumination.
  121. If you need features such as BBCode or Control theming support, then using a projected
  122. RichTextLabel node is the only way to go.