label_3d.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /**************************************************************************/
  2. /* label_3d.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef LABEL_3D_H
  31. #define LABEL_3D_H
  32. #include "scene/3d/visual_instance_3d.h"
  33. #include "scene/resources/font.h"
  34. #include "servers/text_server.h"
  35. class Label3D : public GeometryInstance3D {
  36. GDCLASS(Label3D, GeometryInstance3D);
  37. public:
  38. enum DrawFlags {
  39. FLAG_SHADED,
  40. FLAG_DOUBLE_SIDED,
  41. FLAG_DISABLE_DEPTH_TEST,
  42. FLAG_FIXED_SIZE,
  43. FLAG_MAX
  44. };
  45. enum AlphaCutMode {
  46. ALPHA_CUT_DISABLED,
  47. ALPHA_CUT_DISCARD,
  48. ALPHA_CUT_OPAQUE_PREPASS,
  49. ALPHA_CUT_HASH,
  50. ALPHA_CUT_MAX
  51. };
  52. private:
  53. real_t pixel_size = 0.005;
  54. bool flags[FLAG_MAX] = {};
  55. AlphaCutMode alpha_cut = ALPHA_CUT_DISABLED;
  56. float alpha_scissor_threshold = 0.5;
  57. float alpha_hash_scale = 1.0;
  58. StandardMaterial3D::AlphaAntiAliasing alpha_antialiasing_mode = StandardMaterial3D::ALPHA_ANTIALIASING_OFF;
  59. float alpha_antialiasing_edge = 0.0f;
  60. AABB aabb;
  61. mutable Ref<TriangleMesh> triangle_mesh;
  62. RID mesh;
  63. struct SurfaceData {
  64. PackedVector3Array mesh_vertices;
  65. PackedVector3Array mesh_normals;
  66. PackedFloat32Array mesh_tangents;
  67. PackedColorArray mesh_colors;
  68. PackedVector2Array mesh_uvs;
  69. PackedInt32Array indices;
  70. int offset = 0;
  71. float z_shift = 0.0;
  72. RID material;
  73. };
  74. struct SurfaceKey {
  75. uint64_t texture_id;
  76. int32_t priority;
  77. int32_t outline_size;
  78. bool operator==(const SurfaceKey &p_b) const {
  79. return (texture_id == p_b.texture_id) && (priority == p_b.priority) && (outline_size == p_b.outline_size);
  80. }
  81. SurfaceKey(uint64_t p_texture_id, int p_priority, int p_outline_size) {
  82. texture_id = p_texture_id;
  83. priority = p_priority;
  84. outline_size = p_outline_size;
  85. }
  86. };
  87. struct SurfaceKeyHasher {
  88. _FORCE_INLINE_ static uint32_t hash(const SurfaceKey &p_a) {
  89. return hash_murmur3_buffer(&p_a, sizeof(SurfaceKey));
  90. }
  91. };
  92. HashMap<SurfaceKey, SurfaceData, SurfaceKeyHasher> surfaces;
  93. HorizontalAlignment horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER;
  94. VerticalAlignment vertical_alignment = VERTICAL_ALIGNMENT_CENTER;
  95. String text;
  96. String xl_text;
  97. bool uppercase = false;
  98. TextServer::AutowrapMode autowrap_mode = TextServer::AUTOWRAP_OFF;
  99. BitField<TextServer::JustificationFlag> jst_flags = TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_SKIP_LAST_LINE | TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE;
  100. float width = 500.0;
  101. int font_size = 32;
  102. Ref<Font> font_override;
  103. mutable Ref<Font> theme_font;
  104. Color modulate = Color(1, 1, 1, 1);
  105. Point2 lbl_offset;
  106. int outline_render_priority = -1;
  107. int render_priority = 0;
  108. int outline_size = 12;
  109. Color outline_modulate = Color(0, 0, 0, 1);
  110. float line_spacing = 0.f;
  111. String language;
  112. TextServer::Direction text_direction = TextServer::DIRECTION_AUTO;
  113. TextServer::StructuredTextParser st_parser = TextServer::STRUCTURED_TEXT_DEFAULT;
  114. Array st_args;
  115. RID text_rid;
  116. Vector<RID> lines_rid;
  117. RID base_material;
  118. StandardMaterial3D::BillboardMode billboard_mode = StandardMaterial3D::BILLBOARD_DISABLED;
  119. StandardMaterial3D::TextureFilter texture_filter = StandardMaterial3D::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS;
  120. bool pending_update = false;
  121. bool dirty_lines = true;
  122. bool dirty_font = true;
  123. bool dirty_text = true;
  124. void _generate_glyph_surfaces(const Glyph &p_glyph, Vector2 &r_offset, const Color &p_modulate, int p_priority = 0, int p_outline_size = 0);
  125. protected:
  126. GDVIRTUAL2RC(TypedArray<Vector3i>, _structured_text_parser, Array, String)
  127. void _notification(int p_what);
  128. static void _bind_methods();
  129. void _validate_property(PropertyInfo &p_property) const;
  130. void _im_update();
  131. void _font_changed();
  132. void _queue_update();
  133. void _shape();
  134. public:
  135. void set_horizontal_alignment(HorizontalAlignment p_alignment);
  136. HorizontalAlignment get_horizontal_alignment() const;
  137. void set_vertical_alignment(VerticalAlignment p_alignment);
  138. VerticalAlignment get_vertical_alignment() const;
  139. void set_render_priority(int p_priority);
  140. int get_render_priority() const;
  141. void set_outline_render_priority(int p_priority);
  142. int get_outline_render_priority() const;
  143. void set_text(const String &p_string);
  144. String get_text() const;
  145. void set_text_direction(TextServer::Direction p_text_direction);
  146. TextServer::Direction get_text_direction() const;
  147. void set_language(const String &p_language);
  148. String get_language() const;
  149. void set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser);
  150. TextServer::StructuredTextParser get_structured_text_bidi_override() const;
  151. void set_structured_text_bidi_override_options(Array p_args);
  152. Array get_structured_text_bidi_override_options() const;
  153. void set_uppercase(bool p_uppercase);
  154. bool is_uppercase() const;
  155. void set_font(const Ref<Font> &p_font);
  156. Ref<Font> get_font() const;
  157. Ref<Font> _get_font_or_default() const;
  158. void set_font_size(int p_size);
  159. int get_font_size() const;
  160. void set_outline_size(int p_size);
  161. int get_outline_size() const;
  162. void set_line_spacing(float p_size);
  163. float get_line_spacing() const;
  164. void set_modulate(const Color &p_color);
  165. Color get_modulate() const;
  166. void set_outline_modulate(const Color &p_color);
  167. Color get_outline_modulate() const;
  168. void set_autowrap_mode(TextServer::AutowrapMode p_mode);
  169. TextServer::AutowrapMode get_autowrap_mode() const;
  170. void set_justification_flags(BitField<TextServer::JustificationFlag> p_flags);
  171. BitField<TextServer::JustificationFlag> get_justification_flags() const;
  172. void set_width(float p_width);
  173. float get_width() const;
  174. void set_pixel_size(real_t p_amount);
  175. real_t get_pixel_size() const;
  176. void set_offset(const Point2 &p_offset);
  177. Point2 get_offset() const;
  178. void set_draw_flag(DrawFlags p_flag, bool p_enable);
  179. bool get_draw_flag(DrawFlags p_flag) const;
  180. void set_alpha_cut_mode(AlphaCutMode p_mode);
  181. AlphaCutMode get_alpha_cut_mode() const;
  182. void set_alpha_scissor_threshold(float p_threshold);
  183. float get_alpha_scissor_threshold() const;
  184. void set_alpha_hash_scale(float p_hash_scale);
  185. float get_alpha_hash_scale() const;
  186. void set_alpha_antialiasing(BaseMaterial3D::AlphaAntiAliasing p_alpha_aa);
  187. BaseMaterial3D::AlphaAntiAliasing get_alpha_antialiasing() const;
  188. void set_alpha_antialiasing_edge(float p_edge);
  189. float get_alpha_antialiasing_edge() const;
  190. void set_billboard_mode(StandardMaterial3D::BillboardMode p_mode);
  191. StandardMaterial3D::BillboardMode get_billboard_mode() const;
  192. void set_texture_filter(StandardMaterial3D::TextureFilter p_filter);
  193. StandardMaterial3D::TextureFilter get_texture_filter() const;
  194. virtual AABB get_aabb() const override;
  195. Ref<TriangleMesh> generate_triangle_mesh() const;
  196. Label3D();
  197. ~Label3D();
  198. };
  199. VARIANT_ENUM_CAST(Label3D::DrawFlags);
  200. VARIANT_ENUM_CAST(Label3D::AlphaCutMode);
  201. #endif // LABEL_3D_H