Text3D.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Graphics/Drawable.h"
  5. #include "../GraphicsAPI/VertexBuffer.h"
  6. #include "../Math/Matrix3x4.h"
  7. #include "../UI/Text.h"
  8. namespace Urho3D
  9. {
  10. class Text;
  11. /// 3D text component.
  12. class URHO3D_API Text3D : public Drawable
  13. {
  14. URHO3D_OBJECT(Text3D, Drawable);
  15. public:
  16. /// Construct.
  17. explicit Text3D(Context* context);
  18. /// Destruct.
  19. ~Text3D() override;
  20. /// Register object factory. Drawable must be registered first.
  21. /// @nobind
  22. static void RegisterObject(Context* context);
  23. /// Apply attribute changes that can not be applied immediately.
  24. void ApplyAttributes() override;
  25. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  26. void UpdateBatches(const FrameInfo& frame) override;
  27. /// Prepare geometry for rendering. Called from a worker thread if possible (no GPU update).
  28. void UpdateGeometry(const FrameInfo& frame) override;
  29. /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
  30. UpdateGeometryType GetUpdateGeometryType() override;
  31. /// Set font by looking from resource cache by name and font size. Return true if successful.
  32. bool SetFont(const String& fontName, float size = DEFAULT_FONT_SIZE);
  33. /// Set font and font size. Return true if successful.
  34. bool SetFont(Font* font, float size = DEFAULT_FONT_SIZE);
  35. /// Set font size only while retaining the existing font. Return true if successful.
  36. /// @property
  37. bool SetFontSize(float size);
  38. /// Set material.
  39. /// @property
  40. void SetMaterial(Material* material);
  41. /// Set text. Text is assumed to be either ASCII or UTF8-encoded.
  42. /// @property
  43. void SetText(const String& text);
  44. /// Set horizontal and vertical alignment.
  45. void SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign);
  46. /// Set horizontal alignment.
  47. /// @property
  48. void SetHorizontalAlignment(HorizontalAlignment align);
  49. /// Set vertical alignment.
  50. /// @property
  51. void SetVerticalAlignment(VerticalAlignment align);
  52. /// Set row alignment.
  53. /// @property
  54. void SetTextAlignment(HorizontalAlignment align);
  55. /// Set row spacing, 1.0 for original font spacing.
  56. /// @property
  57. void SetRowSpacing(float spacing);
  58. /// Set wordwrap. In wordwrap mode the text element will respect its current width. Otherwise it resizes itself freely.
  59. /// @property
  60. void SetWordwrap(bool enable);
  61. /// Set text effect.
  62. /// @property
  63. void SetTextEffect(TextEffect textEffect);
  64. /// Set shadow offset.
  65. /// @property
  66. void SetEffectShadowOffset(const IntVector2& offset);
  67. /// Set stroke thickness.
  68. /// @property
  69. void SetEffectStrokeThickness(int thickness);
  70. /// Set stroke rounding. Corners of the font will be rounded off in the stroke so the stroke won't have corners.
  71. /// @property
  72. void SetEffectRoundStroke(bool roundStroke);
  73. /// Set effect color.
  74. /// @property
  75. void SetEffectColor(const Color& effectColor);
  76. /// Set effect Z bias.
  77. /// @property
  78. void SetEffectDepthBias(float bias);
  79. /// Set text width. Only has effect in word wrap mode.
  80. /// @property
  81. void SetWidth(int width);
  82. /// Set color on all corners.
  83. /// @property
  84. void SetColor(const Color& color);
  85. /// Set color on one corner.
  86. /// @property{set_colors}
  87. void SetColor(Corner corner, const Color& color);
  88. /// Set opacity.
  89. /// @property
  90. void SetOpacity(float opacity);
  91. /// Set whether text has fixed size on screen (pixel-perfect) regardless of distance to camera. Works best when combined with face camera rotation. Default false.
  92. /// @property
  93. void SetFixedScreenSize(bool enable);
  94. /// Set how the text should rotate in relation to the camera. Default is to not rotate (FC_NONE).
  95. /// @property
  96. void SetFaceCameraMode(FaceCameraMode mode);
  97. /// Return font.
  98. /// @property
  99. Font* GetFont() const;
  100. /// Return font size.
  101. /// @property
  102. float GetFontSize() const;
  103. /// Return material.
  104. /// @property
  105. Material* GetMaterial() const;
  106. /// Return text.
  107. /// @property
  108. const String& GetText() const;
  109. /// Return row alignment.
  110. /// @property
  111. HorizontalAlignment GetTextAlignment() const;
  112. /// Return horizontal alignment.
  113. /// @property
  114. HorizontalAlignment GetHorizontalAlignment() const;
  115. /// Return vertical alignment.
  116. /// @property
  117. VerticalAlignment GetVerticalAlignment() const;
  118. /// Return row spacing.
  119. /// @property
  120. float GetRowSpacing() const;
  121. /// Return wordwrap mode.
  122. /// @property
  123. bool GetWordwrap() const;
  124. /// Return text effect.
  125. /// @property
  126. TextEffect GetTextEffect() const;
  127. /// Return effect shadow offset.
  128. /// @property
  129. const IntVector2& GetEffectShadowOffset() const;
  130. /// Return effect stroke thickness.
  131. /// @property
  132. int GetEffectStrokeThickness() const;
  133. /// Return effect round stroke.
  134. /// @property
  135. bool GetEffectRoundStroke() const;
  136. /// Return effect color.
  137. /// @property
  138. const Color& GetEffectColor() const;
  139. /// Return effect depth bias.
  140. /// @property
  141. float GetEffectDepthBias() const;
  142. /// Return text width.
  143. /// @property
  144. int GetWidth() const;
  145. /// Return text height.
  146. /// @property
  147. int GetHeight() const;
  148. /// Return row height.
  149. /// @property
  150. int GetRowHeight() const;
  151. /// Return number of rows.
  152. /// @property
  153. i32 GetNumRows() const;
  154. /// Return number of characters.
  155. /// @property
  156. i32 GetNumChars() const;
  157. /// Return width of row by index.
  158. /// @property{get_rowWidths}
  159. int GetRowWidth(i32 index) const;
  160. /// Return position of character by index relative to the text element origin.
  161. /// @property{get_charPositions}
  162. Vector2 GetCharPosition(i32 index);
  163. /// Return size of character by index.
  164. /// @property{get_charSizes}
  165. Vector2 GetCharSize(i32 index);
  166. /// Return corner color.
  167. /// @property{get_colors}
  168. const Color& GetColor(Corner corner) const;
  169. /// Return opacity.
  170. /// @property
  171. float GetOpacity() const;
  172. /// Return whether text has fixed screen size.
  173. /// @property
  174. bool IsFixedScreenSize() const { return fixedScreenSize_; }
  175. /// Return how the text rotates in relation to the camera.
  176. /// @property
  177. FaceCameraMode GetFaceCameraMode() const { return faceCameraMode_; }
  178. /// Set font attribute.
  179. void SetFontAttr(const ResourceRef& value);
  180. /// Return font attribute.
  181. ResourceRef GetFontAttr() const;
  182. /// Set material attribute.
  183. void SetMaterialAttr(const ResourceRef& value);
  184. /// Return material attribute.
  185. ResourceRef GetMaterialAttr() const;
  186. /// Set text attribute.
  187. void SetTextAttr(const String& value);
  188. /// Return text attribute.
  189. String GetTextAttr() const;
  190. /// Get color attribute. Uses just the top-left color.
  191. const Color& GetColorAttr() const { return text_.colors_[0]; }
  192. protected:
  193. /// Handle node being assigned.
  194. void OnNodeSet(Node* node) override;
  195. /// Recalculate the world-space bounding box.
  196. void OnWorldBoundingBoxUpdate() override;
  197. /// Mark text & geometry dirty.
  198. void MarkTextDirty();
  199. /// Update text %UI batches.
  200. void UpdateTextBatches();
  201. /// Create materials for text rendering. May only be called from the main thread. Text %UI batches must be up-to-date.
  202. void UpdateTextMaterials(bool forceUpdate = false);
  203. /// Recalculate camera facing and fixed screen size.
  204. void CalculateFixedScreenSize(const FrameInfo& frame);
  205. /// Internally used text element.
  206. Text text_;
  207. /// Geometries.
  208. Vector<SharedPtr<Geometry>> geometries_;
  209. /// Vertex buffer.
  210. SharedPtr<VertexBuffer> vertexBuffer_;
  211. /// Material to use as a base for the text material(s).
  212. SharedPtr<Material> material_;
  213. /// Text UI batches.
  214. Vector<UIBatch> uiBatches_;
  215. /// Text vertex data.
  216. Vector<float> uiVertexData_;
  217. /// Custom world transform for facing the camera automatically.
  218. Matrix3x4 customWorldTransform_;
  219. /// Text rotation mode in relation to the camera.
  220. FaceCameraMode faceCameraMode_;
  221. /// Minimal angle between text normal and look-at direction.
  222. float minAngle_;
  223. /// Fixed screen size flag.
  224. bool fixedScreenSize_;
  225. /// Text needs update flag.
  226. bool textDirty_;
  227. /// Geometry dirty flag.
  228. bool geometryDirty_;
  229. /// Flag for whether currently using SDF shader defines in the generated material.
  230. bool usingSDFShader_;
  231. /// Font texture data lost flag.
  232. bool fontDataLost_;
  233. };
  234. }