label_3d.cpp 41 KB

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