label_3d.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. /**************************************************************************/
  2. /* label_3d.cpp */
  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. #include "label_3d.h"
  31. #include "scene/main/window.h"
  32. #include "scene/resources/theme.h"
  33. #include "scene/theme/theme_db.h"
  34. void Label3D::_bind_methods() {
  35. ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &Label3D::set_horizontal_alignment);
  36. ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &Label3D::get_horizontal_alignment);
  37. ClassDB::bind_method(D_METHOD("set_vertical_alignment", "alignment"), &Label3D::set_vertical_alignment);
  38. ClassDB::bind_method(D_METHOD("get_vertical_alignment"), &Label3D::get_vertical_alignment);
  39. ClassDB::bind_method(D_METHOD("set_modulate", "modulate"), &Label3D::set_modulate);
  40. ClassDB::bind_method(D_METHOD("get_modulate"), &Label3D::get_modulate);
  41. ClassDB::bind_method(D_METHOD("set_outline_modulate", "modulate"), &Label3D::set_outline_modulate);
  42. ClassDB::bind_method(D_METHOD("get_outline_modulate"), &Label3D::get_outline_modulate);
  43. ClassDB::bind_method(D_METHOD("set_text", "text"), &Label3D::set_text);
  44. ClassDB::bind_method(D_METHOD("get_text"), &Label3D::get_text);
  45. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &Label3D::set_text_direction);
  46. ClassDB::bind_method(D_METHOD("get_text_direction"), &Label3D::get_text_direction);
  47. ClassDB::bind_method(D_METHOD("set_language", "language"), &Label3D::set_language);
  48. ClassDB::bind_method(D_METHOD("get_language"), &Label3D::get_language);
  49. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &Label3D::set_structured_text_bidi_override);
  50. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &Label3D::get_structured_text_bidi_override);
  51. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &Label3D::set_structured_text_bidi_override_options);
  52. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &Label3D::get_structured_text_bidi_override_options);
  53. ClassDB::bind_method(D_METHOD("set_uppercase", "enable"), &Label3D::set_uppercase);
  54. ClassDB::bind_method(D_METHOD("is_uppercase"), &Label3D::is_uppercase);
  55. ClassDB::bind_method(D_METHOD("set_render_priority", "priority"), &Label3D::set_render_priority);
  56. ClassDB::bind_method(D_METHOD("get_render_priority"), &Label3D::get_render_priority);
  57. ClassDB::bind_method(D_METHOD("set_outline_render_priority", "priority"), &Label3D::set_outline_render_priority);
  58. ClassDB::bind_method(D_METHOD("get_outline_render_priority"), &Label3D::get_outline_render_priority);
  59. ClassDB::bind_method(D_METHOD("set_font", "font"), &Label3D::set_font);
  60. ClassDB::bind_method(D_METHOD("get_font"), &Label3D::get_font);
  61. ClassDB::bind_method(D_METHOD("set_font_size", "size"), &Label3D::set_font_size);
  62. ClassDB::bind_method(D_METHOD("get_font_size"), &Label3D::get_font_size);
  63. ClassDB::bind_method(D_METHOD("set_outline_size", "outline_size"), &Label3D::set_outline_size);
  64. ClassDB::bind_method(D_METHOD("get_outline_size"), &Label3D::get_outline_size);
  65. ClassDB::bind_method(D_METHOD("set_line_spacing", "line_spacing"), &Label3D::set_line_spacing);
  66. ClassDB::bind_method(D_METHOD("get_line_spacing"), &Label3D::get_line_spacing);
  67. ClassDB::bind_method(D_METHOD("set_autowrap_mode", "autowrap_mode"), &Label3D::set_autowrap_mode);
  68. ClassDB::bind_method(D_METHOD("get_autowrap_mode"), &Label3D::get_autowrap_mode);
  69. ClassDB::bind_method(D_METHOD("set_autowrap_trim_flags", "autowrap_trim_flags"), &Label3D::set_autowrap_trim_flags);
  70. ClassDB::bind_method(D_METHOD("get_autowrap_trim_flags"), &Label3D::get_autowrap_trim_flags);
  71. ClassDB::bind_method(D_METHOD("set_justification_flags", "justification_flags"), &Label3D::set_justification_flags);
  72. ClassDB::bind_method(D_METHOD("get_justification_flags"), &Label3D::get_justification_flags);
  73. ClassDB::bind_method(D_METHOD("set_width", "width"), &Label3D::set_width);
  74. ClassDB::bind_method(D_METHOD("get_width"), &Label3D::get_width);
  75. ClassDB::bind_method(D_METHOD("set_pixel_size", "pixel_size"), &Label3D::set_pixel_size);
  76. ClassDB::bind_method(D_METHOD("get_pixel_size"), &Label3D::get_pixel_size);
  77. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &Label3D::set_offset);
  78. ClassDB::bind_method(D_METHOD("get_offset"), &Label3D::get_offset);
  79. ClassDB::bind_method(D_METHOD("set_draw_flag", "flag", "enabled"), &Label3D::set_draw_flag);
  80. ClassDB::bind_method(D_METHOD("get_draw_flag", "flag"), &Label3D::get_draw_flag);
  81. ClassDB::bind_method(D_METHOD("set_billboard_mode", "mode"), &Label3D::set_billboard_mode);
  82. ClassDB::bind_method(D_METHOD("get_billboard_mode"), &Label3D::get_billboard_mode);
  83. ClassDB::bind_method(D_METHOD("set_alpha_cut_mode", "mode"), &Label3D::set_alpha_cut_mode);
  84. ClassDB::bind_method(D_METHOD("get_alpha_cut_mode"), &Label3D::get_alpha_cut_mode);
  85. ClassDB::bind_method(D_METHOD("set_alpha_scissor_threshold", "threshold"), &Label3D::set_alpha_scissor_threshold);
  86. ClassDB::bind_method(D_METHOD("get_alpha_scissor_threshold"), &Label3D::get_alpha_scissor_threshold);
  87. ClassDB::bind_method(D_METHOD("set_alpha_hash_scale", "threshold"), &Label3D::set_alpha_hash_scale);
  88. ClassDB::bind_method(D_METHOD("get_alpha_hash_scale"), &Label3D::get_alpha_hash_scale);
  89. ClassDB::bind_method(D_METHOD("set_alpha_antialiasing", "alpha_aa"), &Label3D::set_alpha_antialiasing);
  90. ClassDB::bind_method(D_METHOD("get_alpha_antialiasing"), &Label3D::get_alpha_antialiasing);
  91. ClassDB::bind_method(D_METHOD("set_alpha_antialiasing_edge", "edge"), &Label3D::set_alpha_antialiasing_edge);
  92. ClassDB::bind_method(D_METHOD("get_alpha_antialiasing_edge"), &Label3D::get_alpha_antialiasing_edge);
  93. ClassDB::bind_method(D_METHOD("set_texture_filter", "mode"), &Label3D::set_texture_filter);
  94. ClassDB::bind_method(D_METHOD("get_texture_filter"), &Label3D::get_texture_filter);
  95. ClassDB::bind_method(D_METHOD("generate_triangle_mesh"), &Label3D::generate_triangle_mesh);
  96. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pixel_size", PROPERTY_HINT_RANGE, "0.0001,128,0.0001,suffix:m"), "set_pixel_size", "get_pixel_size");
  97. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset", PROPERTY_HINT_NONE, "suffix:px"), "set_offset", "get_offset");
  98. ADD_GROUP("Flags", "");
  99. ADD_PROPERTY(PropertyInfo(Variant::INT, "billboard", PROPERTY_HINT_ENUM, "Disabled,Enabled,Y-Billboard"), "set_billboard_mode", "get_billboard_mode");
  100. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "shaded"), "set_draw_flag", "get_draw_flag", FLAG_SHADED);
  101. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "double_sided"), "set_draw_flag", "get_draw_flag", FLAG_DOUBLE_SIDED);
  102. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "no_depth_test"), "set_draw_flag", "get_draw_flag", FLAG_DISABLE_DEPTH_TEST);
  103. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "fixed_size"), "set_draw_flag", "get_draw_flag", FLAG_FIXED_SIZE);
  104. ADD_PROPERTY(PropertyInfo(Variant::INT, "alpha_cut", PROPERTY_HINT_ENUM, "Disabled,Discard,Opaque Pre-Pass,Alpha Hash"), "set_alpha_cut_mode", "get_alpha_cut_mode");
  105. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "alpha_scissor_threshold", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_alpha_scissor_threshold", "get_alpha_scissor_threshold");
  106. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "alpha_hash_scale", PROPERTY_HINT_RANGE, "0,2,0.01"), "set_alpha_hash_scale", "get_alpha_hash_scale");
  107. ADD_PROPERTY(PropertyInfo(Variant::INT, "alpha_antialiasing_mode", PROPERTY_HINT_ENUM, "Disabled,Alpha Edge Blend,Alpha Edge Clip"), "set_alpha_antialiasing", "get_alpha_antialiasing");
  108. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "alpha_antialiasing_edge", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_alpha_antialiasing_edge", "get_alpha_antialiasing_edge");
  109. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_filter", PROPERTY_HINT_ENUM, "Nearest,Linear,Nearest Mipmap,Linear Mipmap,Nearest Mipmap Anisotropic,Linear Mipmap Anisotropic"), "set_texture_filter", "get_texture_filter");
  110. ADD_PROPERTY(PropertyInfo(Variant::INT, "render_priority", PROPERTY_HINT_RANGE, itos(RS::MATERIAL_RENDER_PRIORITY_MIN) + "," + itos(RS::MATERIAL_RENDER_PRIORITY_MAX) + ",1"), "set_render_priority", "get_render_priority");
  111. ADD_PROPERTY(PropertyInfo(Variant::INT, "outline_render_priority", PROPERTY_HINT_RANGE, itos(RS::MATERIAL_RENDER_PRIORITY_MIN) + "," + itos(RS::MATERIAL_RENDER_PRIORITY_MAX) + ",1"), "set_outline_render_priority", "get_outline_render_priority");
  112. ADD_GROUP("Text", "");
  113. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "modulate"), "set_modulate", "get_modulate");
  114. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "outline_modulate"), "set_outline_modulate", "get_outline_modulate");
  115. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT, ""), "set_text", "get_text");
  116. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "font", PROPERTY_HINT_RESOURCE_TYPE, "Font"), "set_font", "get_font");
  117. ADD_PROPERTY(PropertyInfo(Variant::INT, "font_size", PROPERTY_HINT_RANGE, "1,256,1,or_greater,suffix:px"), "set_font_size", "get_font_size");
  118. ADD_PROPERTY(PropertyInfo(Variant::INT, "outline_size", PROPERTY_HINT_RANGE, "0,127,1,suffix:px"), "set_outline_size", "get_outline_size");
  119. ADD_PROPERTY(PropertyInfo(Variant::INT, "horizontal_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_horizontal_alignment", "get_horizontal_alignment");
  120. ADD_PROPERTY(PropertyInfo(Variant::INT, "vertical_alignment", PROPERTY_HINT_ENUM, "Top,Center,Bottom"), "set_vertical_alignment", "get_vertical_alignment");
  121. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "uppercase"), "set_uppercase", "is_uppercase");
  122. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "line_spacing", PROPERTY_HINT_NONE, "suffix:px"), "set_line_spacing", "get_line_spacing");
  123. ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_mode", PROPERTY_HINT_ENUM, "Off,Arbitrary,Word,Word (Smart)"), "set_autowrap_mode", "get_autowrap_mode");
  124. ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_trim_flags", PROPERTY_HINT_FLAGS, vformat("Trim Spaces After Break:%d,Trim Spaces Before Break:%d", TextServer::BREAK_TRIM_START_EDGE_SPACES, TextServer::BREAK_TRIM_END_EDGE_SPACES)), "set_autowrap_trim_flags", "get_autowrap_trim_flags");
  125. ADD_PROPERTY(PropertyInfo(Variant::INT, "justification_flags", PROPERTY_HINT_FLAGS, "Kashida Justification:1,Word Justification:2,Justify Only After Last Tab:8,Skip Last Line:32,Skip Last Line With Visible Characters:64,Do Not Skip Single Line:128"), "set_justification_flags", "get_justification_flags");
  126. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "width", PROPERTY_HINT_NONE, "suffix:px"), "set_width", "get_width");
  127. ADD_GROUP("BiDi", "");
  128. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left"), "set_text_direction", "get_text_direction");
  129. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  130. ADD_PROPERTY(PropertyInfo(Variant::INT, "structured_text_bidi_override", PROPERTY_HINT_ENUM, "Default,URI,File,Email,List,None,Custom"), "set_structured_text_bidi_override", "get_structured_text_bidi_override");
  131. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
  132. BIND_ENUM_CONSTANT(FLAG_SHADED);
  133. BIND_ENUM_CONSTANT(FLAG_DOUBLE_SIDED);
  134. BIND_ENUM_CONSTANT(FLAG_DISABLE_DEPTH_TEST);
  135. BIND_ENUM_CONSTANT(FLAG_FIXED_SIZE);
  136. BIND_ENUM_CONSTANT(FLAG_MAX);
  137. BIND_ENUM_CONSTANT(ALPHA_CUT_DISABLED);
  138. BIND_ENUM_CONSTANT(ALPHA_CUT_DISCARD);
  139. BIND_ENUM_CONSTANT(ALPHA_CUT_OPAQUE_PREPASS);
  140. BIND_ENUM_CONSTANT(ALPHA_CUT_HASH);
  141. }
  142. void Label3D::_validate_property(PropertyInfo &p_property) const {
  143. if (
  144. p_property.name == "material_override" ||
  145. p_property.name == "material_overlay" ||
  146. p_property.name == "lod_bias" ||
  147. p_property.name == "gi_mode" ||
  148. p_property.name == "gi_lightmap_scale") {
  149. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  150. }
  151. if (p_property.name == "cast_shadow" && alpha_cut == ALPHA_CUT_DISABLED) {
  152. // Alpha-blended materials can't cast shadows.
  153. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  154. }
  155. }
  156. void Label3D::_notification(int p_what) {
  157. switch (p_what) {
  158. case NOTIFICATION_ENTER_TREE: {
  159. if (!pending_update) {
  160. _im_update();
  161. }
  162. Window *window = get_window();
  163. ERR_FAIL_NULL(window);
  164. window->connect("size_changed", callable_mp(this, &Label3D::_font_changed));
  165. } break;
  166. case NOTIFICATION_EXIT_TREE: {
  167. Window *window = get_window();
  168. ERR_FAIL_NULL(window);
  169. window->disconnect("size_changed", callable_mp(this, &Label3D::_font_changed));
  170. } break;
  171. case NOTIFICATION_TRANSLATION_CHANGED: {
  172. String new_text = atr(text);
  173. if (new_text == xl_text) {
  174. return; // Nothing new.
  175. }
  176. xl_text = new_text;
  177. dirty_text = true;
  178. _queue_update();
  179. } break;
  180. }
  181. }
  182. void Label3D::_im_update() {
  183. _shape();
  184. triangle_mesh.unref();
  185. update_gizmos();
  186. pending_update = false;
  187. }
  188. void Label3D::_queue_update() {
  189. if (pending_update) {
  190. return;
  191. }
  192. pending_update = true;
  193. callable_mp(this, &Label3D::_im_update).call_deferred();
  194. }
  195. AABB Label3D::get_aabb() const {
  196. return aabb;
  197. }
  198. Ref<TriangleMesh> Label3D::generate_triangle_mesh() const {
  199. if (triangle_mesh.is_valid()) {
  200. return triangle_mesh;
  201. }
  202. Ref<Font> font = _get_font_or_default();
  203. if (font.is_null()) {
  204. return Ref<TriangleMesh>();
  205. }
  206. Vector<Vector3> faces;
  207. faces.resize(6);
  208. Vector3 *facesw = faces.ptrw();
  209. float total_h = 0.0;
  210. float max_line_w = 0.0;
  211. for (int i = 0; i < lines_rid.size(); i++) {
  212. total_h += TS->shaped_text_get_size(lines_rid[i]).y + line_spacing;
  213. max_line_w = MAX(max_line_w, TS->shaped_text_get_width(lines_rid[i]));
  214. }
  215. float vbegin = 0;
  216. switch (vertical_alignment) {
  217. case VERTICAL_ALIGNMENT_FILL:
  218. case VERTICAL_ALIGNMENT_TOP: {
  219. // Nothing.
  220. } break;
  221. case VERTICAL_ALIGNMENT_CENTER: {
  222. vbegin = (total_h - line_spacing) / 2.0;
  223. } break;
  224. case VERTICAL_ALIGNMENT_BOTTOM: {
  225. vbegin = (total_h - line_spacing);
  226. } break;
  227. }
  228. Vector2 offset = Vector2(0, vbegin);
  229. switch (horizontal_alignment) {
  230. case HORIZONTAL_ALIGNMENT_LEFT:
  231. break;
  232. case HORIZONTAL_ALIGNMENT_FILL:
  233. case HORIZONTAL_ALIGNMENT_CENTER: {
  234. offset.x = -max_line_w / 2.0;
  235. } break;
  236. case HORIZONTAL_ALIGNMENT_RIGHT: {
  237. offset.x = -max_line_w;
  238. } break;
  239. }
  240. Rect2 final_rect = Rect2(offset + lbl_offset, Size2(max_line_w, total_h));
  241. if (final_rect.size.x == 0 || final_rect.size.y == 0) {
  242. return Ref<TriangleMesh>();
  243. }
  244. real_t px_size = get_pixel_size();
  245. Vector2 vertices[4] = {
  246. (final_rect.position + Vector2(0, -final_rect.size.y)) * px_size,
  247. (final_rect.position + Vector2(final_rect.size.x, -final_rect.size.y)) * px_size,
  248. (final_rect.position + Vector2(final_rect.size.x, 0)) * px_size,
  249. final_rect.position * px_size,
  250. };
  251. static const int indices[6] = {
  252. 0, 1, 2,
  253. 0, 2, 3
  254. };
  255. for (int j = 0; j < 6; j++) {
  256. int i = indices[j];
  257. Vector3 vtx;
  258. vtx[0] = vertices[i][0];
  259. vtx[1] = vertices[i][1];
  260. facesw[j] = vtx;
  261. }
  262. triangle_mesh.instantiate();
  263. triangle_mesh->create(faces);
  264. return triangle_mesh;
  265. }
  266. void Label3D::_generate_glyph_surfaces(const Glyph &p_glyph, Vector2 &r_offset, const Color &p_modulate, int p_priority, int p_outline_size) {
  267. if (p_glyph.index == 0) {
  268. r_offset.x += p_glyph.advance * pixel_size * p_glyph.repeat; // Non visual character, skip.
  269. return;
  270. }
  271. Vector2 gl_of;
  272. Vector2 gl_sz;
  273. Rect2 gl_uv;
  274. Size2 texs;
  275. RID tex;
  276. if (p_glyph.font_rid.is_valid()) {
  277. tex = TS->font_get_glyph_texture_rid(p_glyph.font_rid, Vector2i(p_glyph.font_size, p_outline_size), p_glyph.index);
  278. if (tex.is_valid()) {
  279. gl_of = (TS->font_get_glyph_offset(p_glyph.font_rid, Vector2i(p_glyph.font_size, p_outline_size), p_glyph.index) + Vector2(p_glyph.x_off, p_glyph.y_off)) * pixel_size;
  280. gl_sz = TS->font_get_glyph_size(p_glyph.font_rid, Vector2i(p_glyph.font_size, p_outline_size), p_glyph.index) * pixel_size;
  281. gl_uv = TS->font_get_glyph_uv_rect(p_glyph.font_rid, Vector2i(p_glyph.font_size, p_outline_size), p_glyph.index);
  282. texs = TS->font_get_glyph_texture_size(p_glyph.font_rid, Vector2i(p_glyph.font_size, p_outline_size), p_glyph.index);
  283. }
  284. } else if (((p_glyph.flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL) && ((p_glyph.flags & TextServer::GRAPHEME_IS_EMBEDDED_OBJECT) != TextServer::GRAPHEME_IS_EMBEDDED_OBJECT)) {
  285. gl_sz = TS->get_hex_code_box_size(p_glyph.font_size, p_glyph.index) * pixel_size;
  286. gl_of = Vector2(0, -gl_sz.y);
  287. }
  288. if (gl_uv.size.x <= 2 || gl_uv.size.y <= 2) {
  289. r_offset.x += p_glyph.advance * pixel_size * p_glyph.repeat; // Nothing to draw.
  290. return;
  291. }
  292. bool msdf = TS->font_is_multichannel_signed_distance_field(p_glyph.font_rid);
  293. for (int j = 0; j < p_glyph.repeat; j++) {
  294. SurfaceKey key = SurfaceKey(tex.get_id(), p_priority, p_outline_size);
  295. if (!surfaces.has(key)) {
  296. SurfaceData surf;
  297. surf.material = RenderingServer::get_singleton()->material_create();
  298. // Set defaults for material, names need to match up those in StandardMaterial3D
  299. RS::get_singleton()->material_set_param(surf.material, "albedo", Color(1, 1, 1, 1));
  300. RS::get_singleton()->material_set_param(surf.material, "specular", 0.5);
  301. RS::get_singleton()->material_set_param(surf.material, "metallic", 0.0);
  302. RS::get_singleton()->material_set_param(surf.material, "roughness", 1.0);
  303. RS::get_singleton()->material_set_param(surf.material, "uv1_offset", Vector3(0, 0, 0));
  304. RS::get_singleton()->material_set_param(surf.material, "uv1_scale", Vector3(1, 1, 1));
  305. RS::get_singleton()->material_set_param(surf.material, "uv2_offset", Vector3(0, 0, 0));
  306. RS::get_singleton()->material_set_param(surf.material, "uv2_scale", Vector3(1, 1, 1));
  307. RS::get_singleton()->material_set_param(surf.material, "alpha_scissor_threshold", alpha_scissor_threshold);
  308. RS::get_singleton()->material_set_param(surf.material, "alpha_hash_scale", alpha_hash_scale);
  309. RS::get_singleton()->material_set_param(surf.material, "alpha_antialiasing_edge", alpha_antialiasing_edge);
  310. if (msdf) {
  311. RS::get_singleton()->material_set_param(surf.material, "msdf_pixel_range", TS->font_get_msdf_pixel_range(p_glyph.font_rid));
  312. RS::get_singleton()->material_set_param(surf.material, "msdf_outline_size", p_outline_size);
  313. }
  314. BaseMaterial3D::Transparency mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_ALPHA;
  315. if (get_alpha_cut_mode() == ALPHA_CUT_DISCARD) {
  316. mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_ALPHA_SCISSOR;
  317. } else if (get_alpha_cut_mode() == ALPHA_CUT_OPAQUE_PREPASS) {
  318. mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_ALPHA_DEPTH_PRE_PASS;
  319. } else if (get_alpha_cut_mode() == ALPHA_CUT_HASH) {
  320. mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_ALPHA_HASH;
  321. }
  322. RID shader_rid;
  323. StandardMaterial3D::get_material_for_2d(get_draw_flag(FLAG_SHADED), mat_transparency, get_draw_flag(FLAG_DOUBLE_SIDED), get_billboard_mode() == StandardMaterial3D::BILLBOARD_ENABLED, get_billboard_mode() == StandardMaterial3D::BILLBOARD_FIXED_Y, msdf, get_draw_flag(FLAG_DISABLE_DEPTH_TEST), get_draw_flag(FLAG_FIXED_SIZE), texture_filter, alpha_antialiasing_mode, &shader_rid);
  324. RS::get_singleton()->material_set_shader(surf.material, shader_rid);
  325. RS::get_singleton()->material_set_param(surf.material, "texture_albedo", tex);
  326. RS::get_singleton()->material_set_param(surf.material, "albedo_texture_size", texs);
  327. if (get_alpha_cut_mode() == ALPHA_CUT_DISABLED) {
  328. RS::get_singleton()->material_set_render_priority(surf.material, p_priority);
  329. } else {
  330. surf.z_shift = p_priority * pixel_size;
  331. }
  332. surfaces[key] = surf;
  333. }
  334. SurfaceData &s = surfaces[key];
  335. s.mesh_vertices.resize((s.offset + 1) * 4);
  336. s.mesh_normals.resize((s.offset + 1) * 4);
  337. s.mesh_tangents.resize((s.offset + 1) * 16);
  338. s.mesh_colors.resize((s.offset + 1) * 4);
  339. s.mesh_uvs.resize((s.offset + 1) * 4);
  340. s.mesh_vertices.write[(s.offset * 4) + 3] = Vector3(r_offset.x + gl_of.x, r_offset.y - gl_of.y - gl_sz.y, s.z_shift);
  341. s.mesh_vertices.write[(s.offset * 4) + 2] = Vector3(r_offset.x + gl_of.x + gl_sz.x, r_offset.y - gl_of.y - gl_sz.y, s.z_shift);
  342. s.mesh_vertices.write[(s.offset * 4) + 1] = Vector3(r_offset.x + gl_of.x + gl_sz.x, r_offset.y - gl_of.y, s.z_shift);
  343. s.mesh_vertices.write[(s.offset * 4) + 0] = Vector3(r_offset.x + gl_of.x, r_offset.y - gl_of.y, s.z_shift);
  344. for (int i = 0; i < 4; i++) {
  345. s.mesh_normals.write[(s.offset * 4) + i] = Vector3(0.0, 0.0, 1.0);
  346. s.mesh_tangents.write[(s.offset * 16) + (i * 4) + 0] = 1.0;
  347. s.mesh_tangents.write[(s.offset * 16) + (i * 4) + 1] = 0.0;
  348. s.mesh_tangents.write[(s.offset * 16) + (i * 4) + 2] = 0.0;
  349. s.mesh_tangents.write[(s.offset * 16) + (i * 4) + 3] = 1.0;
  350. s.mesh_colors.write[(s.offset * 4) + i] = p_modulate;
  351. s.mesh_uvs.write[(s.offset * 4) + i] = Vector2();
  352. }
  353. if (tex.is_valid()) {
  354. s.mesh_uvs.write[(s.offset * 4) + 3] = Vector2(gl_uv.position.x / texs.x, (gl_uv.position.y + gl_uv.size.y) / texs.y);
  355. s.mesh_uvs.write[(s.offset * 4) + 2] = Vector2((gl_uv.position.x + gl_uv.size.x) / texs.x, (gl_uv.position.y + gl_uv.size.y) / texs.y);
  356. s.mesh_uvs.write[(s.offset * 4) + 1] = Vector2((gl_uv.position.x + gl_uv.size.x) / texs.x, gl_uv.position.y / texs.y);
  357. s.mesh_uvs.write[(s.offset * 4) + 0] = Vector2(gl_uv.position.x / texs.x, gl_uv.position.y / texs.y);
  358. }
  359. s.indices.resize((s.offset + 1) * 6);
  360. s.indices.write[(s.offset * 6) + 0] = (s.offset * 4) + 0;
  361. s.indices.write[(s.offset * 6) + 1] = (s.offset * 4) + 1;
  362. s.indices.write[(s.offset * 6) + 2] = (s.offset * 4) + 2;
  363. s.indices.write[(s.offset * 6) + 3] = (s.offset * 4) + 0;
  364. s.indices.write[(s.offset * 6) + 4] = (s.offset * 4) + 2;
  365. s.indices.write[(s.offset * 6) + 5] = (s.offset * 4) + 3;
  366. s.offset++;
  367. r_offset.x += p_glyph.advance * pixel_size;
  368. }
  369. }
  370. void Label3D::_shape() {
  371. // When a shaped text is invalidated by an external source, we want to reshape it.
  372. if (!TS->shaped_text_is_ready(text_rid)) {
  373. dirty_text = true;
  374. }
  375. for (const RID &line_rid : lines_rid) {
  376. if (!TS->shaped_text_is_ready(line_rid)) {
  377. dirty_lines = true;
  378. break;
  379. }
  380. }
  381. // Clear mesh.
  382. RS::get_singleton()->mesh_clear(mesh);
  383. aabb = AABB();
  384. // Clear materials.
  385. for (const KeyValue<SurfaceKey, SurfaceData> &E : surfaces) {
  386. RenderingServer::get_singleton()->free(E.value.material);
  387. }
  388. surfaces.clear();
  389. Ref<Font> font = _get_font_or_default();
  390. ERR_FAIL_COND(font.is_null());
  391. // Update text buffer.
  392. if (dirty_text) {
  393. TS->shaped_text_clear(text_rid);
  394. TS->shaped_text_set_direction(text_rid, text_direction);
  395. String txt = (uppercase) ? TS->string_to_upper(xl_text, language) : xl_text;
  396. TS->shaped_text_add_string(text_rid, txt, font->get_rids(), font_size, font->get_opentype_features(), language);
  397. TypedArray<Vector3i> stt;
  398. if (st_parser == TextServer::STRUCTURED_TEXT_CUSTOM) {
  399. GDVIRTUAL_CALL(_structured_text_parser, st_args, txt, stt);
  400. } else {
  401. stt = TS->parse_structured_text(st_parser, st_args, txt);
  402. }
  403. TS->shaped_text_set_bidi_override(text_rid, stt);
  404. dirty_text = false;
  405. dirty_font = false;
  406. dirty_lines = true;
  407. } else if (dirty_font) {
  408. int spans = TS->shaped_get_span_count(text_rid);
  409. for (int i = 0; i < spans; i++) {
  410. TS->shaped_set_span_update_font(text_rid, i, font->get_rids(), font_size, font->get_opentype_features());
  411. }
  412. dirty_font = false;
  413. dirty_lines = true;
  414. }
  415. if (dirty_lines) {
  416. for (int i = 0; i < lines_rid.size(); i++) {
  417. TS->free_rid(lines_rid[i]);
  418. }
  419. lines_rid.clear();
  420. BitField<TextServer::LineBreakFlag> autowrap_flags = TextServer::BREAK_MANDATORY;
  421. switch (autowrap_mode) {
  422. case TextServer::AUTOWRAP_WORD_SMART:
  423. autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_ADAPTIVE | TextServer::BREAK_MANDATORY;
  424. break;
  425. case TextServer::AUTOWRAP_WORD:
  426. autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_MANDATORY;
  427. break;
  428. case TextServer::AUTOWRAP_ARBITRARY:
  429. autowrap_flags = TextServer::BREAK_GRAPHEME_BOUND | TextServer::BREAK_MANDATORY;
  430. break;
  431. case TextServer::AUTOWRAP_OFF:
  432. break;
  433. }
  434. autowrap_flags = autowrap_flags | autowrap_flags_trim;
  435. PackedInt32Array line_breaks = TS->shaped_text_get_line_breaks(text_rid, width, 0, autowrap_flags);
  436. float max_line_w = 0.0;
  437. for (int i = 0; i < line_breaks.size(); i = i + 2) {
  438. RID line = TS->shaped_text_substr(text_rid, line_breaks[i], line_breaks[i + 1] - line_breaks[i]);
  439. max_line_w = MAX(max_line_w, TS->shaped_text_get_width(line));
  440. lines_rid.push_back(line);
  441. }
  442. if (horizontal_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  443. int jst_to_line = lines_rid.size();
  444. if (lines_rid.size() == 1 && jst_flags.has_flag(TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE)) {
  445. jst_to_line = lines_rid.size();
  446. } else {
  447. if (jst_flags.has_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE)) {
  448. jst_to_line = lines_rid.size() - 1;
  449. }
  450. if (jst_flags.has_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE_WITH_VISIBLE_CHARS)) {
  451. for (int i = lines_rid.size() - 1; i >= 0; i--) {
  452. if (TS->shaped_text_has_visible_chars(lines_rid[i])) {
  453. jst_to_line = i;
  454. break;
  455. }
  456. }
  457. }
  458. }
  459. for (int i = 0; i < jst_to_line; i++) {
  460. TS->shaped_text_fit_to_width(lines_rid[i], (width > 0) ? width : max_line_w, jst_flags);
  461. }
  462. }
  463. dirty_lines = false;
  464. }
  465. // Generate surfaces and materials.
  466. float total_h = 0.0;
  467. for (int i = 0; i < lines_rid.size(); i++) {
  468. total_h += (TS->shaped_text_get_size(lines_rid[i]).y + line_spacing) * pixel_size;
  469. }
  470. float vbegin = 0.0;
  471. switch (vertical_alignment) {
  472. case VERTICAL_ALIGNMENT_FILL:
  473. case VERTICAL_ALIGNMENT_TOP: {
  474. // Nothing.
  475. } break;
  476. case VERTICAL_ALIGNMENT_CENTER: {
  477. vbegin = (total_h - line_spacing * pixel_size) / 2.0;
  478. } break;
  479. case VERTICAL_ALIGNMENT_BOTTOM: {
  480. vbegin = (total_h - line_spacing * pixel_size);
  481. } break;
  482. }
  483. Vector2 offset = Vector2(0, vbegin + lbl_offset.y * pixel_size);
  484. for (int i = 0; i < lines_rid.size(); i++) {
  485. const Glyph *glyphs = TS->shaped_text_get_glyphs(lines_rid[i]);
  486. int gl_size = TS->shaped_text_get_glyph_count(lines_rid[i]);
  487. float line_width = TS->shaped_text_get_width(lines_rid[i]) * pixel_size;
  488. switch (horizontal_alignment) {
  489. case HORIZONTAL_ALIGNMENT_LEFT:
  490. offset.x = 0.0;
  491. break;
  492. case HORIZONTAL_ALIGNMENT_FILL:
  493. case HORIZONTAL_ALIGNMENT_CENTER: {
  494. offset.x = -line_width / 2.0;
  495. } break;
  496. case HORIZONTAL_ALIGNMENT_RIGHT: {
  497. offset.x = -line_width;
  498. } break;
  499. }
  500. offset.x += lbl_offset.x * pixel_size;
  501. if (aabb == AABB()) {
  502. aabb.position = Vector3(offset.x, offset.y, 0);
  503. aabb.expand_to(Vector3(offset.x + line_width, offset.y - (TS->shaped_text_get_size(lines_rid[i]).y + line_spacing) * pixel_size, 0));
  504. } else {
  505. aabb.expand_to(Vector3(offset.x, offset.y, 0));
  506. aabb.expand_to(Vector3(offset.x + line_width, offset.y - (TS->shaped_text_get_size(lines_rid[i]).y + line_spacing) * pixel_size, 0));
  507. }
  508. offset.y -= TS->shaped_text_get_ascent(lines_rid[i]) * pixel_size;
  509. if (outline_modulate.a != 0.0 && outline_size > 0) {
  510. // Outline surfaces.
  511. Vector2 ol_offset = offset;
  512. for (int j = 0; j < gl_size; j++) {
  513. _generate_glyph_surfaces(glyphs[j], ol_offset, outline_modulate, outline_render_priority, outline_size);
  514. }
  515. }
  516. // Main text surfaces.
  517. for (int j = 0; j < gl_size; j++) {
  518. _generate_glyph_surfaces(glyphs[j], offset, modulate, render_priority);
  519. }
  520. offset.y -= (TS->shaped_text_get_descent(lines_rid[i]) + line_spacing) * pixel_size;
  521. }
  522. for (const KeyValue<SurfaceKey, SurfaceData> &E : surfaces) {
  523. Array mesh_array;
  524. mesh_array.resize(RS::ARRAY_MAX);
  525. mesh_array[RS::ARRAY_VERTEX] = E.value.mesh_vertices;
  526. mesh_array[RS::ARRAY_NORMAL] = E.value.mesh_normals;
  527. mesh_array[RS::ARRAY_TANGENT] = E.value.mesh_tangents;
  528. mesh_array[RS::ARRAY_COLOR] = E.value.mesh_colors;
  529. mesh_array[RS::ARRAY_TEX_UV] = E.value.mesh_uvs;
  530. mesh_array[RS::ARRAY_INDEX] = E.value.indices;
  531. RS::SurfaceData sd;
  532. RS::get_singleton()->mesh_create_surface_data_from_arrays(&sd, RS::PRIMITIVE_TRIANGLES, mesh_array);
  533. sd.material = E.value.material;
  534. RS::get_singleton()->mesh_add_surface(mesh, sd);
  535. }
  536. }
  537. void Label3D::set_text(const String &p_string) {
  538. if (text == p_string) {
  539. return;
  540. }
  541. text = p_string;
  542. xl_text = atr(p_string);
  543. dirty_text = true;
  544. _queue_update();
  545. }
  546. String Label3D::get_text() const {
  547. return text;
  548. }
  549. void Label3D::set_horizontal_alignment(HorizontalAlignment p_alignment) {
  550. ERR_FAIL_INDEX((int)p_alignment, 4);
  551. if (horizontal_alignment != p_alignment) {
  552. if (horizontal_alignment == HORIZONTAL_ALIGNMENT_FILL || p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  553. dirty_lines = true; // Reshape lines.
  554. }
  555. horizontal_alignment = p_alignment;
  556. _queue_update();
  557. }
  558. }
  559. HorizontalAlignment Label3D::get_horizontal_alignment() const {
  560. return horizontal_alignment;
  561. }
  562. void Label3D::set_vertical_alignment(VerticalAlignment p_alignment) {
  563. ERR_FAIL_INDEX((int)p_alignment, 4);
  564. if (vertical_alignment != p_alignment) {
  565. vertical_alignment = p_alignment;
  566. _queue_update();
  567. }
  568. }
  569. VerticalAlignment Label3D::get_vertical_alignment() const {
  570. return vertical_alignment;
  571. }
  572. void Label3D::set_text_direction(TextServer::Direction p_text_direction) {
  573. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  574. if (text_direction != p_text_direction) {
  575. text_direction = p_text_direction;
  576. dirty_text = true;
  577. _queue_update();
  578. }
  579. }
  580. TextServer::Direction Label3D::get_text_direction() const {
  581. return text_direction;
  582. }
  583. void Label3D::set_language(const String &p_language) {
  584. if (language != p_language) {
  585. language = p_language;
  586. dirty_text = true;
  587. _queue_update();
  588. }
  589. }
  590. String Label3D::get_language() const {
  591. return language;
  592. }
  593. void Label3D::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) {
  594. if (st_parser != p_parser) {
  595. st_parser = p_parser;
  596. dirty_text = true;
  597. _queue_update();
  598. }
  599. }
  600. TextServer::StructuredTextParser Label3D::get_structured_text_bidi_override() const {
  601. return st_parser;
  602. }
  603. void Label3D::set_structured_text_bidi_override_options(Array p_args) {
  604. if (st_args != p_args) {
  605. st_args = p_args;
  606. dirty_text = true;
  607. _queue_update();
  608. }
  609. }
  610. Array Label3D::get_structured_text_bidi_override_options() const {
  611. return st_args;
  612. }
  613. void Label3D::set_uppercase(bool p_uppercase) {
  614. if (uppercase != p_uppercase) {
  615. uppercase = p_uppercase;
  616. dirty_text = true;
  617. _queue_update();
  618. }
  619. }
  620. bool Label3D::is_uppercase() const {
  621. return uppercase;
  622. }
  623. void Label3D::set_render_priority(int p_priority) {
  624. ERR_FAIL_COND(p_priority < RS::MATERIAL_RENDER_PRIORITY_MIN || p_priority > RS::MATERIAL_RENDER_PRIORITY_MAX);
  625. if (render_priority != p_priority) {
  626. render_priority = p_priority;
  627. _queue_update();
  628. }
  629. }
  630. int Label3D::get_render_priority() const {
  631. return render_priority;
  632. }
  633. void Label3D::set_outline_render_priority(int p_priority) {
  634. ERR_FAIL_COND(p_priority < RS::MATERIAL_RENDER_PRIORITY_MIN || p_priority > RS::MATERIAL_RENDER_PRIORITY_MAX);
  635. if (outline_render_priority != p_priority) {
  636. outline_render_priority = p_priority;
  637. _queue_update();
  638. }
  639. }
  640. int Label3D::get_outline_render_priority() const {
  641. return outline_render_priority;
  642. }
  643. void Label3D::_font_changed() {
  644. dirty_font = true;
  645. _queue_update();
  646. }
  647. void Label3D::set_font(const Ref<Font> &p_font) {
  648. if (font_override != p_font) {
  649. if (font_override.is_valid()) {
  650. font_override->disconnect_changed(callable_mp(this, &Label3D::_font_changed));
  651. }
  652. font_override = p_font;
  653. dirty_font = true;
  654. if (font_override.is_valid()) {
  655. font_override->connect_changed(callable_mp(this, &Label3D::_font_changed));
  656. }
  657. _queue_update();
  658. }
  659. }
  660. Ref<Font> Label3D::get_font() const {
  661. return font_override;
  662. }
  663. Ref<Font> Label3D::_get_font_or_default() const {
  664. // Similar code taken from `FontVariation::_get_base_font_or_default`.
  665. if (theme_font.is_valid()) {
  666. theme_font->disconnect_changed(callable_mp(const_cast<Label3D *>(this), &Label3D::_font_changed));
  667. theme_font.unref();
  668. }
  669. if (font_override.is_valid()) {
  670. return font_override;
  671. }
  672. const StringName theme_name = SceneStringName(font);
  673. Vector<StringName> theme_types;
  674. ThemeDB::get_singleton()->get_native_type_dependencies(get_class_name(), theme_types);
  675. ThemeContext *global_context = ThemeDB::get_singleton()->get_default_theme_context();
  676. Vector<Ref<Theme>> themes = global_context->get_themes();
  677. if (Engine::get_singleton()->is_editor_hint()) {
  678. themes.insert(0, ThemeDB::get_singleton()->get_project_theme());
  679. }
  680. for (const Ref<Theme> &theme : themes) {
  681. if (theme.is_null()) {
  682. continue;
  683. }
  684. for (const StringName &E : theme_types) {
  685. if (!theme->has_font(theme_name, E)) {
  686. continue;
  687. }
  688. Ref<Font> f = theme->get_font(theme_name, E);
  689. if (f.is_valid()) {
  690. theme_font = f;
  691. theme_font->connect_changed(callable_mp(const_cast<Label3D *>(this), &Label3D::_font_changed));
  692. }
  693. return f;
  694. }
  695. }
  696. Ref<Font> f = global_context->get_fallback_theme()->get_font(theme_name, StringName());
  697. if (f.is_valid()) {
  698. theme_font = f;
  699. theme_font->connect_changed(callable_mp(const_cast<Label3D *>(this), &Label3D::_font_changed));
  700. }
  701. return f;
  702. }
  703. void Label3D::set_font_size(int p_size) {
  704. if (font_size != p_size) {
  705. font_size = p_size;
  706. dirty_font = true;
  707. _queue_update();
  708. }
  709. }
  710. int Label3D::get_font_size() const {
  711. return font_size;
  712. }
  713. void Label3D::set_outline_size(int p_size) {
  714. if (outline_size != p_size) {
  715. outline_size = p_size;
  716. _queue_update();
  717. }
  718. }
  719. int Label3D::get_outline_size() const {
  720. return outline_size;
  721. }
  722. void Label3D::set_modulate(const Color &p_color) {
  723. if (modulate != p_color) {
  724. modulate = p_color;
  725. _queue_update();
  726. }
  727. }
  728. Color Label3D::get_modulate() const {
  729. return modulate;
  730. }
  731. void Label3D::set_outline_modulate(const Color &p_color) {
  732. if (outline_modulate != p_color) {
  733. outline_modulate = p_color;
  734. _queue_update();
  735. }
  736. }
  737. Color Label3D::get_outline_modulate() const {
  738. return outline_modulate;
  739. }
  740. void Label3D::set_autowrap_mode(TextServer::AutowrapMode p_mode) {
  741. if (autowrap_mode != p_mode) {
  742. autowrap_mode = p_mode;
  743. dirty_lines = true;
  744. _queue_update();
  745. }
  746. }
  747. TextServer::AutowrapMode Label3D::get_autowrap_mode() const {
  748. return autowrap_mode;
  749. }
  750. void Label3D::set_autowrap_trim_flags(BitField<TextServer::LineBreakFlag> p_flags) {
  751. if (autowrap_flags_trim != (p_flags & TextServer::BREAK_TRIM_MASK)) {
  752. autowrap_flags_trim = (p_flags & TextServer::BREAK_TRIM_MASK);
  753. dirty_lines = true;
  754. _queue_update();
  755. }
  756. }
  757. BitField<TextServer::LineBreakFlag> Label3D::get_autowrap_trim_flags() const {
  758. return autowrap_flags_trim;
  759. }
  760. void Label3D::set_justification_flags(BitField<TextServer::JustificationFlag> p_flags) {
  761. if (jst_flags != p_flags) {
  762. jst_flags = p_flags;
  763. dirty_lines = true;
  764. _queue_update();
  765. }
  766. }
  767. BitField<TextServer::JustificationFlag> Label3D::get_justification_flags() const {
  768. return jst_flags;
  769. }
  770. void Label3D::set_width(float p_width) {
  771. if (width != p_width) {
  772. width = p_width;
  773. dirty_lines = true;
  774. _queue_update();
  775. }
  776. }
  777. float Label3D::get_width() const {
  778. return width;
  779. }
  780. void Label3D::set_pixel_size(real_t p_amount) {
  781. if (pixel_size != p_amount) {
  782. pixel_size = p_amount;
  783. _queue_update();
  784. }
  785. }
  786. real_t Label3D::get_pixel_size() const {
  787. return pixel_size;
  788. }
  789. void Label3D::set_offset(const Point2 &p_offset) {
  790. if (lbl_offset != p_offset) {
  791. lbl_offset = p_offset;
  792. _queue_update();
  793. }
  794. }
  795. Point2 Label3D::get_offset() const {
  796. return lbl_offset;
  797. }
  798. void Label3D::set_line_spacing(float p_line_spacing) {
  799. if (line_spacing != p_line_spacing) {
  800. line_spacing = p_line_spacing;
  801. _queue_update();
  802. }
  803. }
  804. float Label3D::get_line_spacing() const {
  805. return line_spacing;
  806. }
  807. void Label3D::set_draw_flag(DrawFlags p_flag, bool p_enable) {
  808. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  809. if (flags[p_flag] != p_enable) {
  810. flags[p_flag] = p_enable;
  811. _queue_update();
  812. }
  813. }
  814. bool Label3D::get_draw_flag(DrawFlags p_flag) const {
  815. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  816. return flags[p_flag];
  817. }
  818. void Label3D::set_billboard_mode(StandardMaterial3D::BillboardMode p_mode) {
  819. ERR_FAIL_INDEX(p_mode, 3);
  820. if (billboard_mode != p_mode) {
  821. billboard_mode = p_mode;
  822. _queue_update();
  823. }
  824. }
  825. StandardMaterial3D::BillboardMode Label3D::get_billboard_mode() const {
  826. return billboard_mode;
  827. }
  828. void Label3D::set_alpha_cut_mode(AlphaCutMode p_mode) {
  829. ERR_FAIL_INDEX(p_mode, ALPHA_CUT_MAX);
  830. if (alpha_cut != p_mode) {
  831. alpha_cut = p_mode;
  832. _queue_update();
  833. notify_property_list_changed();
  834. }
  835. }
  836. void Label3D::set_texture_filter(StandardMaterial3D::TextureFilter p_filter) {
  837. if (texture_filter != p_filter) {
  838. texture_filter = p_filter;
  839. _queue_update();
  840. }
  841. }
  842. StandardMaterial3D::TextureFilter Label3D::get_texture_filter() const {
  843. return texture_filter;
  844. }
  845. Label3D::AlphaCutMode Label3D::get_alpha_cut_mode() const {
  846. return alpha_cut;
  847. }
  848. void Label3D::set_alpha_hash_scale(float p_hash_scale) {
  849. if (alpha_hash_scale != p_hash_scale) {
  850. alpha_hash_scale = p_hash_scale;
  851. _queue_update();
  852. }
  853. }
  854. float Label3D::get_alpha_hash_scale() const {
  855. return alpha_hash_scale;
  856. }
  857. void Label3D::set_alpha_scissor_threshold(float p_threshold) {
  858. if (alpha_scissor_threshold != p_threshold) {
  859. alpha_scissor_threshold = p_threshold;
  860. _queue_update();
  861. }
  862. }
  863. float Label3D::get_alpha_scissor_threshold() const {
  864. return alpha_scissor_threshold;
  865. }
  866. void Label3D::set_alpha_antialiasing(BaseMaterial3D::AlphaAntiAliasing p_alpha_aa) {
  867. if (alpha_antialiasing_mode != p_alpha_aa) {
  868. alpha_antialiasing_mode = p_alpha_aa;
  869. _queue_update();
  870. }
  871. }
  872. BaseMaterial3D::AlphaAntiAliasing Label3D::get_alpha_antialiasing() const {
  873. return alpha_antialiasing_mode;
  874. }
  875. void Label3D::set_alpha_antialiasing_edge(float p_edge) {
  876. if (alpha_antialiasing_edge != p_edge) {
  877. alpha_antialiasing_edge = p_edge;
  878. _queue_update();
  879. }
  880. }
  881. float Label3D::get_alpha_antialiasing_edge() const {
  882. return alpha_antialiasing_edge;
  883. }
  884. Label3D::Label3D() {
  885. for (int i = 0; i < FLAG_MAX; i++) {
  886. flags[i] = (i == FLAG_DOUBLE_SIDED);
  887. }
  888. text_rid = TS->create_shaped_text();
  889. mesh = RenderingServer::get_singleton()->mesh_create();
  890. // Disable shadow casting by default to improve performance and avoid unintended visual artifacts.
  891. set_cast_shadows_setting(SHADOW_CASTING_SETTING_OFF);
  892. // Label3D can't contribute to GI in any way, so disable it to improve performance.
  893. set_gi_mode(GI_MODE_DISABLED);
  894. set_base(mesh);
  895. }
  896. Label3D::~Label3D() {
  897. for (int i = 0; i < lines_rid.size(); i++) {
  898. TS->free_rid(lines_rid[i]);
  899. }
  900. lines_rid.clear();
  901. TS->free_rid(text_rid);
  902. ERR_FAIL_NULL(RenderingServer::get_singleton());
  903. RenderingServer::get_singleton()->free(mesh);
  904. for (KeyValue<SurfaceKey, SurfaceData> E : surfaces) {
  905. RenderingServer::get_singleton()->free(E.value.material);
  906. }
  907. surfaces.clear();
  908. }