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