Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/3d/label_3d.h
9896 views
1
/**************************************************************************/
2
/* label_3d.h */
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
#pragma once
32
33
#include "scene/3d/visual_instance_3d.h"
34
#include "scene/resources/font.h"
35
36
#include "servers/text_server.h"
37
38
class Label3D : public GeometryInstance3D {
39
GDCLASS(Label3D, GeometryInstance3D);
40
41
public:
42
enum DrawFlags {
43
FLAG_SHADED,
44
FLAG_DOUBLE_SIDED,
45
FLAG_DISABLE_DEPTH_TEST,
46
FLAG_FIXED_SIZE,
47
FLAG_MAX
48
};
49
50
enum AlphaCutMode {
51
ALPHA_CUT_DISABLED,
52
ALPHA_CUT_DISCARD,
53
ALPHA_CUT_OPAQUE_PREPASS,
54
ALPHA_CUT_HASH,
55
ALPHA_CUT_MAX
56
};
57
58
private:
59
real_t pixel_size = 0.005;
60
bool flags[FLAG_MAX] = {};
61
AlphaCutMode alpha_cut = ALPHA_CUT_DISABLED;
62
float alpha_scissor_threshold = 0.5;
63
float alpha_hash_scale = 1.0;
64
StandardMaterial3D::AlphaAntiAliasing alpha_antialiasing_mode = StandardMaterial3D::ALPHA_ANTIALIASING_OFF;
65
float alpha_antialiasing_edge = 0.0f;
66
67
AABB aabb;
68
69
mutable Ref<TriangleMesh> triangle_mesh;
70
RID mesh;
71
struct SurfaceData {
72
PackedVector3Array mesh_vertices;
73
PackedVector3Array mesh_normals;
74
PackedFloat32Array mesh_tangents;
75
PackedColorArray mesh_colors;
76
PackedVector2Array mesh_uvs;
77
PackedInt32Array indices;
78
int offset = 0;
79
float z_shift = 0.0;
80
RID material;
81
};
82
83
struct SurfaceKey {
84
uint64_t texture_id;
85
int32_t priority;
86
int32_t outline_size;
87
88
bool operator==(const SurfaceKey &p_b) const {
89
return (texture_id == p_b.texture_id) && (priority == p_b.priority) && (outline_size == p_b.outline_size);
90
}
91
92
SurfaceKey(uint64_t p_texture_id, int p_priority, int p_outline_size) {
93
texture_id = p_texture_id;
94
priority = p_priority;
95
outline_size = p_outline_size;
96
}
97
};
98
99
struct SurfaceKeyHasher {
100
_FORCE_INLINE_ static uint32_t hash(const SurfaceKey &p_a) {
101
return hash_murmur3_buffer(&p_a, sizeof(SurfaceKey));
102
}
103
};
104
105
HashMap<SurfaceKey, SurfaceData, SurfaceKeyHasher> surfaces;
106
107
HorizontalAlignment horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER;
108
VerticalAlignment vertical_alignment = VERTICAL_ALIGNMENT_CENTER;
109
String text;
110
String xl_text;
111
bool uppercase = false;
112
113
TextServer::AutowrapMode autowrap_mode = TextServer::AUTOWRAP_OFF;
114
BitField<TextServer::LineBreakFlag> autowrap_flags_trim = TextServer::BREAK_TRIM_START_EDGE_SPACES | TextServer::BREAK_TRIM_END_EDGE_SPACES;
115
BitField<TextServer::JustificationFlag> jst_flags = TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_SKIP_LAST_LINE | TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE;
116
float width = 500.0;
117
118
int font_size = 32;
119
Ref<Font> font_override;
120
mutable Ref<Font> theme_font;
121
Color modulate = Color(1, 1, 1, 1);
122
Point2 lbl_offset;
123
int outline_render_priority = -1;
124
int render_priority = 0;
125
126
int outline_size = 12;
127
Color outline_modulate = Color(0, 0, 0, 1);
128
129
float line_spacing = 0.f;
130
131
String language;
132
TextServer::Direction text_direction = TextServer::DIRECTION_AUTO;
133
TextServer::StructuredTextParser st_parser = TextServer::STRUCTURED_TEXT_DEFAULT;
134
Array st_args;
135
136
RID text_rid;
137
Vector<RID> lines_rid;
138
139
RID base_material;
140
StandardMaterial3D::BillboardMode billboard_mode = StandardMaterial3D::BILLBOARD_DISABLED;
141
StandardMaterial3D::TextureFilter texture_filter = StandardMaterial3D::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS;
142
143
bool pending_update = false;
144
145
bool dirty_lines = true;
146
bool dirty_font = true;
147
bool dirty_text = true;
148
149
void _generate_glyph_surfaces(const Glyph &p_glyph, Vector2 &r_offset, const Color &p_modulate, int p_priority = 0, int p_outline_size = 0);
150
151
protected:
152
GDVIRTUAL2RC(TypedArray<Vector3i>, _structured_text_parser, Array, String)
153
154
void _notification(int p_what);
155
156
static void _bind_methods();
157
158
void _validate_property(PropertyInfo &p_property) const;
159
160
void _im_update();
161
void _font_changed();
162
void _queue_update();
163
164
void _shape();
165
166
public:
167
void set_horizontal_alignment(HorizontalAlignment p_alignment);
168
HorizontalAlignment get_horizontal_alignment() const;
169
170
void set_vertical_alignment(VerticalAlignment p_alignment);
171
VerticalAlignment get_vertical_alignment() const;
172
173
void set_render_priority(int p_priority);
174
int get_render_priority() const;
175
176
void set_outline_render_priority(int p_priority);
177
int get_outline_render_priority() const;
178
179
void set_text(const String &p_string);
180
String get_text() const;
181
182
void set_text_direction(TextServer::Direction p_text_direction);
183
TextServer::Direction get_text_direction() const;
184
185
void set_language(const String &p_language);
186
String get_language() const;
187
188
void set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser);
189
TextServer::StructuredTextParser get_structured_text_bidi_override() const;
190
191
void set_structured_text_bidi_override_options(Array p_args);
192
Array get_structured_text_bidi_override_options() const;
193
194
void set_uppercase(bool p_uppercase);
195
bool is_uppercase() const;
196
197
void set_font(const Ref<Font> &p_font);
198
Ref<Font> get_font() const;
199
Ref<Font> _get_font_or_default() const;
200
201
void set_font_size(int p_size);
202
int get_font_size() const;
203
204
void set_outline_size(int p_size);
205
int get_outline_size() const;
206
207
void set_line_spacing(float p_size);
208
float get_line_spacing() const;
209
210
void set_modulate(const Color &p_color);
211
Color get_modulate() const;
212
213
void set_outline_modulate(const Color &p_color);
214
Color get_outline_modulate() const;
215
216
void set_autowrap_mode(TextServer::AutowrapMode p_mode);
217
TextServer::AutowrapMode get_autowrap_mode() const;
218
219
void set_autowrap_trim_flags(BitField<TextServer::LineBreakFlag> p_flags);
220
BitField<TextServer::LineBreakFlag> get_autowrap_trim_flags() const;
221
222
void set_justification_flags(BitField<TextServer::JustificationFlag> p_flags);
223
BitField<TextServer::JustificationFlag> get_justification_flags() const;
224
225
void set_width(float p_width);
226
float get_width() const;
227
228
void set_pixel_size(real_t p_amount);
229
real_t get_pixel_size() const;
230
231
void set_offset(const Point2 &p_offset);
232
Point2 get_offset() const;
233
234
void set_draw_flag(DrawFlags p_flag, bool p_enable);
235
bool get_draw_flag(DrawFlags p_flag) const;
236
237
void set_alpha_cut_mode(AlphaCutMode p_mode);
238
AlphaCutMode get_alpha_cut_mode() const;
239
240
void set_alpha_scissor_threshold(float p_threshold);
241
float get_alpha_scissor_threshold() const;
242
243
void set_alpha_hash_scale(float p_hash_scale);
244
float get_alpha_hash_scale() const;
245
246
void set_alpha_antialiasing(BaseMaterial3D::AlphaAntiAliasing p_alpha_aa);
247
BaseMaterial3D::AlphaAntiAliasing get_alpha_antialiasing() const;
248
249
void set_alpha_antialiasing_edge(float p_edge);
250
float get_alpha_antialiasing_edge() const;
251
252
void set_billboard_mode(StandardMaterial3D::BillboardMode p_mode);
253
StandardMaterial3D::BillboardMode get_billboard_mode() const;
254
255
void set_texture_filter(StandardMaterial3D::TextureFilter p_filter);
256
StandardMaterial3D::TextureFilter get_texture_filter() const;
257
258
virtual AABB get_aabb() const override;
259
virtual Ref<TriangleMesh> generate_triangle_mesh() const override;
260
261
Label3D();
262
~Label3D();
263
};
264
265
VARIANT_ENUM_CAST(Label3D::DrawFlags);
266
VARIANT_ENUM_CAST(Label3D::AlphaCutMode);
267
268