label_3d.cpp 39 KB

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