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#ifndef LABEL_3D_H
32#define LABEL_3D_H
33
34#include "scene/3d/visual_instance_3d.h"
35#include "scene/resources/font.h"
36
37#include "servers/text_server.h"
38
39class Label3D : public GeometryInstance3D {
40 GDCLASS(Label3D, GeometryInstance3D);
41
42public:
43 enum DrawFlags {
44 FLAG_SHADED,
45 FLAG_DOUBLE_SIDED,
46 FLAG_DISABLE_DEPTH_TEST,
47 FLAG_FIXED_SIZE,
48 FLAG_MAX
49 };
50
51 enum AlphaCutMode {
52 ALPHA_CUT_DISABLED,
53 ALPHA_CUT_DISCARD,
54 ALPHA_CUT_OPAQUE_PREPASS,
55 ALPHA_CUT_HASH,
56 ALPHA_CUT_MAX
57 };
58
59private:
60 real_t pixel_size = 0.005;
61 bool flags[FLAG_MAX] = {};
62 AlphaCutMode alpha_cut = ALPHA_CUT_DISABLED;
63 float alpha_scissor_threshold = 0.5;
64 float alpha_hash_scale = 1.0;
65 StandardMaterial3D::AlphaAntiAliasing alpha_antialiasing_mode = StandardMaterial3D::ALPHA_ANTIALIASING_OFF;
66 float alpha_antialiasing_edge = 0.0f;
67
68 AABB aabb;
69
70 mutable Ref<TriangleMesh> triangle_mesh;
71 RID mesh;
72 struct SurfaceData {
73 PackedVector3Array mesh_vertices;
74 PackedVector3Array mesh_normals;
75 PackedFloat32Array mesh_tangents;
76 PackedColorArray mesh_colors;
77 PackedVector2Array mesh_uvs;
78 PackedInt32Array indices;
79 int offset = 0;
80 float z_shift = 0.0;
81 RID material;
82 };
83
84 struct SurfaceKey {
85 uint64_t texture_id;
86 int32_t priority;
87 int32_t outline_size;
88
89 bool operator==(const SurfaceKey &p_b) const {
90 return (texture_id == p_b.texture_id) && (priority == p_b.priority) && (outline_size == p_b.outline_size);
91 }
92
93 SurfaceKey(uint64_t p_texture_id, int p_priority, int p_outline_size) {
94 texture_id = p_texture_id;
95 priority = p_priority;
96 outline_size = p_outline_size;
97 }
98 };
99
100 struct SurfaceKeyHasher {
101 _FORCE_INLINE_ static uint32_t hash(const SurfaceKey &p_a) {
102 return hash_murmur3_buffer(&p_a, sizeof(SurfaceKey));
103 }
104 };
105
106 HashMap<SurfaceKey, SurfaceData, SurfaceKeyHasher> surfaces;
107
108 HorizontalAlignment horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER;
109 VerticalAlignment vertical_alignment = VERTICAL_ALIGNMENT_CENTER;
110 String text;
111 String xl_text;
112 bool uppercase = false;
113
114 TextServer::AutowrapMode autowrap_mode = TextServer::AUTOWRAP_OFF;
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
151protected:
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
166public:
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_justification_flags(BitField<TextServer::JustificationFlag> p_flags);
220 BitField<TextServer::JustificationFlag> get_justification_flags() const;
221
222 void set_width(float p_width);
223 float get_width() const;
224
225 void set_pixel_size(real_t p_amount);
226 real_t get_pixel_size() const;
227
228 void set_offset(const Point2 &p_offset);
229 Point2 get_offset() const;
230
231 void set_draw_flag(DrawFlags p_flag, bool p_enable);
232 bool get_draw_flag(DrawFlags p_flag) const;
233
234 void set_alpha_cut_mode(AlphaCutMode p_mode);
235 AlphaCutMode get_alpha_cut_mode() const;
236
237 void set_alpha_scissor_threshold(float p_threshold);
238 float get_alpha_scissor_threshold() const;
239
240 void set_alpha_hash_scale(float p_hash_scale);
241 float get_alpha_hash_scale() const;
242
243 void set_alpha_antialiasing(BaseMaterial3D::AlphaAntiAliasing p_alpha_aa);
244 BaseMaterial3D::AlphaAntiAliasing get_alpha_antialiasing() const;
245
246 void set_alpha_antialiasing_edge(float p_edge);
247 float get_alpha_antialiasing_edge() const;
248
249 void set_billboard_mode(StandardMaterial3D::BillboardMode p_mode);
250 StandardMaterial3D::BillboardMode get_billboard_mode() const;
251
252 void set_texture_filter(StandardMaterial3D::TextureFilter p_filter);
253 StandardMaterial3D::TextureFilter get_texture_filter() const;
254
255 virtual AABB get_aabb() const override;
256 Ref<TriangleMesh> generate_triangle_mesh() const;
257
258 Label3D();
259 ~Label3D();
260};
261
262VARIANT_ENUM_CAST(Label3D::DrawFlags);
263VARIANT_ENUM_CAST(Label3D::AlphaCutMode);
264
265#endif // LABEL_3D_H
266