label_3d.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.h"
  33. #include "scene/resources/font.h"
  34. class Label3D : public GeometryInstance {
  35. GDCLASS(Label3D, GeometryInstance);
  36. public:
  37. enum DrawFlags {
  38. FLAG_SHADED,
  39. FLAG_DOUBLE_SIDED,
  40. FLAG_DISABLE_DEPTH_TEST,
  41. FLAG_FIXED_SIZE,
  42. FLAG_MAX
  43. };
  44. enum AlphaCutMode {
  45. ALPHA_CUT_DISABLED,
  46. ALPHA_CUT_DISCARD,
  47. ALPHA_CUT_OPAQUE_PREPASS
  48. };
  49. enum Align {
  50. ALIGN_LEFT,
  51. ALIGN_CENTER,
  52. ALIGN_RIGHT,
  53. ALIGN_FILL
  54. };
  55. enum VAlign {
  56. VALIGN_TOP,
  57. VALIGN_CENTER,
  58. VALIGN_BOTTOM,
  59. VALIGN_FILL
  60. };
  61. private:
  62. real_t pixel_size = 0.01;
  63. bool flags[FLAG_MAX] = {};
  64. AlphaCutMode alpha_cut = ALPHA_CUT_DISABLED;
  65. float alpha_scissor_threshold = 0.5;
  66. AABB aabb;
  67. mutable Ref<TriangleMesh> triangle_mesh;
  68. RID mesh;
  69. struct SurfaceData {
  70. PoolVector3Array mesh_vertices;
  71. PoolVector3Array mesh_normals;
  72. PoolRealArray mesh_tangents;
  73. PoolColorArray mesh_colors;
  74. PoolVector2Array mesh_uvs;
  75. PoolIntArray indices;
  76. int offset = 0;
  77. float z_shift = 0.0;
  78. RID material;
  79. };
  80. struct SurfaceKey {
  81. uint64_t texture_id;
  82. int32_t priority;
  83. bool operator==(const SurfaceKey &p_b) const {
  84. return (texture_id == p_b.texture_id) && (priority == p_b.priority);
  85. }
  86. SurfaceKey(uint64_t p_texture_id, int p_priority) {
  87. texture_id = p_texture_id;
  88. priority = p_priority;
  89. }
  90. };
  91. struct SurfaceKeyHasher {
  92. _FORCE_INLINE_ static uint32_t hash(const SurfaceKey &p_a) {
  93. uint32_t hash = hash_djb2_one_32(p_a.texture_id);
  94. return hash_djb2_one_32(p_a.priority, hash);
  95. }
  96. };
  97. HashMap<SurfaceKey, SurfaceData, SurfaceKeyHasher> surfaces;
  98. struct WordCache {
  99. enum {
  100. CHAR_NEWLINE = -1,
  101. CHAR_WRAPLINE = -2
  102. };
  103. int char_pos; // if -1, then newline
  104. int word_len;
  105. int pixel_width;
  106. int space_count;
  107. WordCache *next;
  108. WordCache() {
  109. char_pos = 0;
  110. word_len = 0;
  111. pixel_width = 0;
  112. next = nullptr;
  113. space_count = 0;
  114. }
  115. };
  116. bool word_cache_dirty = true;
  117. WordCache *word_cache = nullptr;
  118. int line_count = 0;
  119. Align horizontal_alignment = ALIGN_CENTER;
  120. VAlign vertical_alignment = VALIGN_CENTER;
  121. String text;
  122. String xl_text;
  123. bool uppercase = false;
  124. bool autowrap = false;
  125. float width = 500.0;
  126. Ref<Font> font_override;
  127. mutable Ref<Font> theme_font;
  128. Color modulate = Color(1, 1, 1, 1);
  129. Point2 lbl_offset;
  130. int outline_render_priority = -1;
  131. int render_priority = 0;
  132. Color outline_modulate = Color(0, 0, 0, 1);
  133. float line_spacing = 0.f;
  134. RID base_material;
  135. SpatialMaterial::BillboardMode billboard_mode = SpatialMaterial::BILLBOARD_DISABLED;
  136. bool pending_update = false;
  137. void regenerate_word_cache();
  138. int get_longest_line_width() const;
  139. float _generate_glyph_surfaces(const Ref<Font> &p_font, CharType p_char, CharType p_next, Vector2 p_offset, const Color &p_modulate, int p_priority, bool p_outline);
  140. protected:
  141. void _notification(int p_what);
  142. static void _bind_methods();
  143. void _validate_property(PropertyInfo &property) const;
  144. void _im_update();
  145. void _font_changed();
  146. void _queue_update();
  147. void _shape();
  148. public:
  149. void set_horizontal_alignment(Align p_alignment);
  150. Align get_horizontal_alignment() const;
  151. void set_vertical_alignment(VAlign p_alignment);
  152. VAlign get_vertical_alignment() const;
  153. void set_render_priority(int p_priority);
  154. int get_render_priority() const;
  155. void set_outline_render_priority(int p_priority);
  156. int get_outline_render_priority() const;
  157. void set_text(const String &p_string);
  158. String get_text() const;
  159. void set_uppercase(bool p_uppercase);
  160. bool is_uppercase() const;
  161. void set_font(const Ref<Font> &p_font);
  162. Ref<Font> get_font() const;
  163. Ref<Font> _get_font_or_default() const;
  164. void set_line_spacing(float p_size);
  165. float get_line_spacing() const;
  166. void set_modulate(const Color &p_color);
  167. Color get_modulate() const;
  168. void set_outline_modulate(const Color &p_color);
  169. Color get_outline_modulate() const;
  170. void set_autowrap(bool p_mode);
  171. bool get_autowrap() 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_billboard_mode(SpatialMaterial::BillboardMode p_mode);
  185. SpatialMaterial::BillboardMode get_billboard_mode() const;
  186. virtual AABB get_aabb() const;
  187. Ref<TriangleMesh> generate_triangle_mesh() const;
  188. virtual PoolVector<Face3> get_faces(uint32_t p_usage_flags) const;
  189. Label3D();
  190. ~Label3D();
  191. };
  192. VARIANT_ENUM_CAST(Label3D::DrawFlags);
  193. VARIANT_ENUM_CAST(Label3D::AlphaCutMode);
  194. VARIANT_ENUM_CAST(Label3D::Align);
  195. VARIANT_ENUM_CAST(Label3D::VAlign);
  196. #endif // LABEL_3D_H