label_3d.cpp 35 KB

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