diff --git a/doc/classes/Font.xml b/doc/classes/Font.xml
index be03a11fe88..db7b207cfc4 100644
--- a/doc/classes/Font.xml
+++ b/doc/classes/Font.xml
@@ -43,6 +43,17 @@
Returns the font ascent (number of pixels above the baseline).
+
+
+
+
+
+ Returns outline contours of the glyph as a [code]Dictionary[/code] with the following contents:
+ [code]points[/code] - [PoolVector3Array], containing outline points. [code]x[/code] and [code]y[/code] are point coordinates. [code]z[/code] is the type of the point, using the [enum ContourPointTag] values.
+ [code]contours[/code] - [PoolIntArray], containing indices the end points of each contour.
+ [code]orientation[/code] - [bool], contour orientation. If [code]true[/code], clockwise contours must be filled.
+
+
@@ -142,5 +153,14 @@
+
+ Contour point is on the curve.
+
+
+ Contour point isn't on the curve, but serves as a control point for a conic (quadratic) Bézier arc.
+
+
+ Contour point isn't on the curve, but serves as a control point for a cubic Bézier arc.
+
diff --git a/doc/classes/TextMesh.xml b/doc/classes/TextMesh.xml
new file mode 100644
index 00000000000..1cf9b88f743
--- /dev/null
+++ b/doc/classes/TextMesh.xml
@@ -0,0 +1,49 @@
+
+
+
+ Generate an [PrimitiveMesh] from the text.
+
+
+ Generate an [PrimitiveMesh] from the text.
+ TextMesh can be generated only when using dynamic fonts with vector glyph contours. Bitmap fonts (including bitmap data in the TrueType/OpenType containers, like color emoji fonts) are not supported.
+ The UV layout is arranged in 4 horizontal strips, top to bottom: 40% of the height for the front face, 40% for the back face, 10% for the outer edges and 10% for the inner edges.
+
+
+
+
+
+
+
+ Step (in pixels) used to approximate Bézier curves.
+
+
+ Depths of the mesh, if set to [code]0.0[/code] only front surface, is generated, and UV layout is changed to use full texture for the front face only.
+
+
+ [Font] used for the [TextMesh]'s text.
+
+
+ Controls the text's horizontal alignment. Supports left, center and right. Set it to one of the [enum Align] constants.
+
+
+ The size of one pixel's width on the text to scale it in 3D.
+
+
+ The text to generate mesh from.
+
+
+ If [code]true[/code], all the text displays as UPPERCASE.
+
+
+
+
+ Align rows to the left (default).
+
+
+ Align rows centered.
+
+
+ Align rows to the right.
+
+
+
diff --git a/editor/icons/icon_text_mesh.svg b/editor/icons/icon_text_mesh.svg
new file mode 100644
index 00000000000..234ce7568e4
--- /dev/null
+++ b/editor/icons/icon_text_mesh.svg
@@ -0,0 +1 @@
+
diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp
index 8ee741939db..e9b94deffb4 100644
--- a/scene/register_scene_types.cpp
+++ b/scene/register_scene_types.cpp
@@ -660,6 +660,7 @@ void register_scene_types() {
ClassDB::register_class();
ClassDB::register_class();
ClassDB::register_class();
+ ClassDB::register_class();
ClassDB::register_class();
ClassDB::register_virtual_class();
ClassDB::register_class();
diff --git a/scene/resources/dynamic_font.cpp b/scene/resources/dynamic_font.cpp
index 826c31e531c..d88baf4c58c 100644
--- a/scene/resources/dynamic_font.cpp
+++ b/scene/resources/dynamic_font.cpp
@@ -540,6 +540,51 @@ float DynamicFontAtSize::draw_char(RID p_canvas_item, const Point2 &p_pos, CharT
return advance;
}
+Dictionary DynamicFontAtSize::get_char_contours(CharType p_char, CharType p_next, const Vector[> &p_fallbacks) const {
+ if (!valid) {
+ return Dictionary();
+ }
+
+ int32_t c = p_char;
+ if (((p_char & 0xfffffc00) == 0xd800) && (p_next & 0xfffffc00) == 0xdc00) { // decode surrogate pair.
+ c = (p_char << 10UL) + p_next - ((0xd800 << 10UL) + 0xdc00 - 0x10000);
+ }
+ if ((p_char & 0xfffffc00) == 0xdc00) { // skip trail surrogate.
+ return Dictionary();
+ }
+
+ const_cast(this)->_update_char(c);
+
+ Pair char_pair_with_font = _find_char_with_font(c, p_fallbacks);
+ const Character *ch = char_pair_with_font.first;
+ DynamicFontAtSize *font = char_pair_with_font.second;
+
+ if (ch->found) {
+ PoolVector3Array points;
+ PoolIntArray contours;
+
+ int error = FT_Load_Char(font->face, c, FT_LOAD_NO_BITMAP | (font->font->force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0));
+ ERR_FAIL_COND_V(error, Dictionary());
+
+ double scale = (1.0 / 64.0) / oversampling * scale_color_font;
+ for (short i = 0; i < font->face->glyph->outline.n_points; i++) {
+ points.push_back(Vector3(font->face->glyph->outline.points[i].x * scale, -font->face->glyph->outline.points[i].y * scale, FT_CURVE_TAG(font->face->glyph->outline.tags[i])));
+ }
+ for (short i = 0; i < font->face->glyph->outline.n_contours; i++) {
+ contours.push_back(font->face->glyph->outline.contours[i]);
+ }
+ bool orientation = (FT_Outline_Get_Orientation(&font->face->glyph->outline) == FT_ORIENTATION_FILL_RIGHT);
+
+ Dictionary out;
+ out["points"] = points;
+ out["contours"] = contours;
+ out["orientation"] = orientation;
+ return out;
+ } else {
+ return Dictionary();
+ }
+}
+
DynamicFontAtSize::Character DynamicFontAtSize::Character::not_found() {
Character ch;
ch.texture_idx = -1;
@@ -1178,6 +1223,14 @@ float DynamicFont::draw_char(RID p_canvas_item, const Point2 &p_pos, CharType p_
}
}
+Dictionary DynamicFont::get_char_contours(CharType p_char, CharType p_next) const {
+ if (!data_at_size.is_valid()) {
+ return Dictionary();
+ }
+
+ return data_at_size->get_char_contours(p_char, p_next, fallback_data_at_size);
+}
+
void DynamicFont::set_fallback(int p_idx, const Ref &p_data) {
ERR_FAIL_COND(p_data.is_null());
ERR_FAIL_INDEX(p_idx, fallbacks.size());
diff --git a/scene/resources/dynamic_font.h b/scene/resources/dynamic_font.h
index eaea7dc86f7..2e8ea0b2eed 100644
--- a/scene/resources/dynamic_font.h
+++ b/scene/resources/dynamic_font.h
@@ -205,6 +205,8 @@ public:
void set_texture_flags(uint32_t p_flags);
void update_oversampling();
+ Dictionary get_char_contours(CharType p_char, CharType p_next, const Vector][> &p_fallbacks) const;
+
DynamicFontAtSize();
~DynamicFontAtSize();
};
@@ -300,6 +302,8 @@ public:
Size2 get_char_tx_size(CharType p_char, CharType p_next, bool p_outline) const;
Rect2 get_char_tx_uv_rect(CharType p_char, CharType p_next, bool p_outline) const;
+ Dictionary get_char_contours(CharType p_char, CharType p_next) const;
+
SelfList font_list;
static Mutex dynamic_font_mutex;
diff --git a/scene/resources/font.cpp b/scene/resources/font.cpp
index d479e92f362..c6a4aec92be 100644
--- a/scene/resources/font.cpp
+++ b/scene/resources/font.cpp
@@ -106,6 +106,11 @@ void Font::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_char_tx_uv_rect", "char", "next", "outline"), &Font::get_char_tx_uv_rect, DEFVAL(0), DEFVAL(false));
ClassDB::bind_method(D_METHOD("update_changes"), &Font::update_changes);
+ ClassDB::bind_method(D_METHOD("get_char_contours", "char", "next"), &Font::get_char_contours, DEFVAL(0));
+
+ BIND_ENUM_CONSTANT(CONTOUR_CURVE_TAG_ON);
+ BIND_ENUM_CONSTANT(CONTOUR_CURVE_TAG_OFF_CONIC);
+ BIND_ENUM_CONSTANT(CONTOUR_CURVE_TAG_OFF_CUBIC);
}
Font::Font() {
diff --git a/scene/resources/font.h b/scene/resources/font.h
index 947af7b5057..4b6fdf4089e 100644
--- a/scene/resources/font.h
+++ b/scene/resources/font.h
@@ -42,6 +42,12 @@ protected:
static void _bind_methods();
public:
+ enum ContourPointTag {
+ CONTOUR_CURVE_TAG_ON = 0x01,
+ CONTOUR_CURVE_TAG_OFF_CONIC = 0x00,
+ CONTOUR_CURVE_TAG_OFF_CUBIC = 0x02
+ };
+
virtual float get_height() const = 0;
virtual float get_ascent() const = 0;
@@ -66,6 +72,8 @@ public:
virtual Size2 get_char_tx_size(CharType p_char, CharType p_next, bool p_outline) const = 0;
virtual Rect2 get_char_tx_uv_rect(CharType p_char, CharType p_next, bool p_outline) const = 0;
+ virtual Dictionary get_char_contours(CharType p_char, CharType p_next = 0) const { return Dictionary(); }
+
void update_changes();
Font();
};
@@ -216,4 +224,6 @@ public:
virtual String get_resource_type(const String &p_path) const;
};
+VARIANT_ENUM_CAST(Font::ContourPointTag);
+
#endif
diff --git a/scene/resources/primitive_meshes.cpp b/scene/resources/primitive_meshes.cpp
index eb7b331cfdd..86979e007ef 100644
--- a/scene/resources/primitive_meshes.cpp
+++ b/scene/resources/primitive_meshes.cpp
@@ -29,7 +29,13 @@
/*************************************************************************/
#include "primitive_meshes.h"
+
+#include "core/core_string_names.h"
+#include "core/os/main_loop.h"
+#include "scene/resources/theme.h"
#include "servers/visual_server.h"
+#include "thirdparty/misc/clipper.hpp"
+#include "thirdparty/misc/triangulator.h"
/**
PrimitiveMesh
@@ -1631,3 +1637,582 @@ void PointMesh::_create_mesh_array(Array &p_arr) const {
PointMesh::PointMesh() {
primitive_type = PRIMITIVE_POINTS;
}
+
+/**
+ TextMesh
+*/
+
+void TextMesh::_generate_glyph_mesh_data(uint32_t p_utf32_char, const Ref &p_font, CharType p_char, CharType p_next) const {
+ if (cache.has(p_utf32_char)) {
+ return;
+ }
+
+ GlyphMeshData &gl_data = cache[p_utf32_char];
+
+ Dictionary d = p_font->get_char_contours(p_char, p_next);
+
+ PoolVector3Array points = d["points"];
+ PoolIntArray contours = d["contours"];
+ bool orientation = d["orientation"];
+
+ if (points.size() < 3 || contours.size() < 1) {
+ return; // No full contours, only glyph control points (or nothing), ignore.
+ }
+
+ // Approximate Bezier curves as polygons.
+ // See https://freetype.org/freetype2/docs/glyphs/glyphs-6.html, for more info.
+ for (int i = 0; i < contours.size(); i++) {
+ int32_t start = (i == 0) ? 0 : (contours[i - 1] + 1);
+ int32_t end = contours[i];
+ Vector polygon;
+
+ for (int32_t j = start; j <= end; j++) {
+ if (points[j].z == Font::CONTOUR_CURVE_TAG_ON) {
+ // Point on the curve.
+ Vector2 p = Vector2(points[j].x, points[j].y) * pixel_size;
+ polygon.push_back(ContourPoint(p, true));
+ } else if (points[j].z == Font::CONTOUR_CURVE_TAG_OFF_CONIC) {
+ // Conic Bezier arc.
+ int32_t next = (j == end) ? start : (j + 1);
+ int32_t prev = (j == start) ? end : (j - 1);
+ Vector2 p0;
+ Vector2 p1 = Vector2(points[j].x, points[j].y);
+ Vector2 p2;
+
+ // For successive conic OFF points add a virtual ON point in the middle.
+ if (points[prev].z == Font::CONTOUR_CURVE_TAG_OFF_CONIC) {
+ p0 = (Vector2(points[prev].x, points[prev].y) + Vector2(points[j].x, points[j].y)) / 2.0;
+ } else if (points[prev].z == Font::CONTOUR_CURVE_TAG_ON) {
+ p0 = Vector2(points[prev].x, points[prev].y);
+ } else {
+ ERR_FAIL_MSG(vformat("Invalid conic arc point sequence at %d:%d", i, j));
+ }
+ if (points[next].z == Font::CONTOUR_CURVE_TAG_OFF_CONIC) {
+ p2 = (Vector2(points[j].x, points[j].y) + Vector2(points[next].x, points[next].y)) / 2.0;
+ } else if (points[next].z == Font::CONTOUR_CURVE_TAG_ON) {
+ p2 = Vector2(points[next].x, points[next].y);
+ } else {
+ ERR_FAIL_MSG(vformat("Invalid conic arc point sequence at %d:%d", i, j));
+ }
+
+ real_t step = CLAMP(curve_step / (p0 - p2).length(), 0.01, 0.5);
+ real_t t = step;
+ while (t < 1.0) {
+ real_t omt = (1.0 - t);
+ real_t omt2 = omt * omt;
+ real_t t2 = t * t;
+
+ Vector2 point = p1 + omt2 * (p0 - p1) + t2 * (p2 - p1);
+ Vector2 p = point * pixel_size;
+ polygon.push_back(ContourPoint(p, false));
+ t += step;
+ }
+ } else if (points[j].z == Font::CONTOUR_CURVE_TAG_OFF_CUBIC) {
+ // Cubic Bezier arc.
+ int32_t cur = j;
+ int32_t next1 = (j == end) ? start : (j + 1);
+ int32_t next2 = (next1 == end) ? start : (next1 + 1);
+ int32_t prev = (j == start) ? end : (j - 1);
+
+ // There must be exactly two OFF points and two ON points for each cubic arc.
+ if (points[prev].z != Font::CONTOUR_CURVE_TAG_ON) {
+ cur = (cur == 0) ? end : cur - 1;
+ next1 = (next1 == 0) ? end : next1 - 1;
+ next2 = (next2 == 0) ? end : next2 - 1;
+ prev = (prev == 0) ? end : prev - 1;
+ } else {
+ j++;
+ }
+ ERR_FAIL_COND_MSG(points[prev].z != Font::CONTOUR_CURVE_TAG_ON, vformat("Invalid cubic arc point sequence at %d:%d", i, prev));
+ ERR_FAIL_COND_MSG(points[cur].z != Font::CONTOUR_CURVE_TAG_OFF_CUBIC, vformat("Invalid cubic arc point sequence at %d:%d", i, cur));
+ ERR_FAIL_COND_MSG(points[next1].z != Font::CONTOUR_CURVE_TAG_OFF_CUBIC, vformat("Invalid cubic arc point sequence at %d:%d", i, next1));
+ ERR_FAIL_COND_MSG(points[next2].z != Font::CONTOUR_CURVE_TAG_ON, vformat("Invalid cubic arc point sequence at %d:%d", i, next2));
+
+ Vector2 p0 = Vector2(points[prev].x, points[prev].y);
+ Vector2 p1 = Vector2(points[cur].x, points[cur].y);
+ Vector2 p2 = Vector2(points[next1].x, points[next1].y);
+ Vector2 p3 = Vector2(points[next2].x, points[next2].y);
+
+ real_t step = CLAMP(curve_step / (p0 - p3).length(), 0.01, 0.5);
+ real_t t = step;
+ while (t < 1.0) {
+ real_t omt = (1.0 - t);
+ real_t omt2 = omt * omt;
+ real_t omt3 = omt2 * omt;
+ real_t t2 = t * t;
+ real_t t3 = t2 * t;
+
+ Vector2 point = p0 * omt3 + p1 * omt2 * t * 3.0 + p2 * omt * t2 * 3.0 + p3 * t3;
+ Vector2 p = point * pixel_size;
+ polygon.push_back(ContourPoint(p, false));
+ t += step;
+ }
+ } else {
+ ERR_FAIL_MSG(vformat("Unknown point tag at %d:%d", i, j));
+ }
+ }
+
+ if (polygon.size() < 3) {
+ continue; // Skip glyph control points.
+ }
+
+ if (!orientation) {
+ polygon.invert();
+ }
+
+ gl_data.contours.push_back(polygon);
+ }
+
+ // Calculate bounds.
+ List in_poly;
+ for (int i = 0; i < gl_data.contours.size(); i++) {
+ TriangulatorPoly inp;
+ inp.Init(gl_data.contours[i].size());
+ real_t length = 0.0;
+ for (int j = 0; j < gl_data.contours[i].size(); j++) {
+ int next = (j + 1 == gl_data.contours[i].size()) ? 0 : (j + 1);
+
+ gl_data.min_p.x = MIN(gl_data.min_p.x, gl_data.contours[i][j].point.x);
+ gl_data.min_p.y = MIN(gl_data.min_p.y, gl_data.contours[i][j].point.y);
+ gl_data.max_p.x = MAX(gl_data.max_p.x, gl_data.contours[i][j].point.x);
+ gl_data.max_p.y = MAX(gl_data.max_p.y, gl_data.contours[i][j].point.y);
+ length += (gl_data.contours[i][next].point - gl_data.contours[i][j].point).length();
+
+ inp.GetPoint(j) = gl_data.contours[i][j].point;
+ }
+ int poly_orient = inp.GetOrientation();
+ if (poly_orient == TRIANGULATOR_CW) {
+ inp.SetHole(true);
+ }
+ in_poly.push_back(inp);
+ gl_data.contours_info.push_back(ContourInfo(length, poly_orient == TRIANGULATOR_CCW));
+ }
+
+ TriangulatorPartition tpart;
+
+ //Decompose and triangulate.
+ List out_poly;
+ if (tpart.ConvexPartition_HM(&in_poly, &out_poly) == 0) {
+ ERR_FAIL_MSG("Convex decomposing failed!");
+ }
+ List out_tris;
+ for (List::Element *I = out_poly.front(); I; I = I->next()) {
+ if (tpart.Triangulate_OPT(&(I->get()), &out_tris) == 0) {
+ ERR_FAIL_MSG("Triangulation failed!");
+ }
+ }
+
+ for (List::Element *I = out_tris.front(); I; I = I->next()) {
+ TriangulatorPoly &tp = I->get();
+ ERR_FAIL_COND(tp.GetNumPoints() != 3); // Trianges only.
+
+ for (int i = 0; i < 3; i++) {
+ gl_data.triangles.push_back(Vector2(tp.GetPoint(i).x, tp.GetPoint(i).y));
+ }
+ }
+}
+
+void TextMesh::_create_mesh_array(Array &p_arr) const {
+ Ref font = _get_font_or_default();
+ ERR_FAIL_COND(font.is_null());
+
+ if (dirty_cache) {
+ cache.clear();
+ dirty_cache = false;
+ }
+
+ String t = (uppercase) ? xl_text.to_upper() : xl_text;
+
+ float line_width = font->get_string_size(t).x * pixel_size;
+
+ Vector2 offset;
+ switch (horizontal_alignment) {
+ case ALIGN_LEFT:
+ offset.x = 0.0;
+ break;
+ case ALIGN_CENTER: {
+ offset.x = -line_width / 2.0;
+ } break;
+ case ALIGN_RIGHT: {
+ offset.x = -line_width;
+ } break;
+ }
+
+ bool has_depth = !Math::is_zero_approx(depth);
+
+ // Generate glyph data, precalculate size of the arrays and mesh bounds for UV.
+ int64_t p_size = 0;
+ int64_t i_size = 0;
+
+ Vector2 min_p = Vector2(INFINITY, INFINITY);
+ Vector2 max_p = Vector2(-INFINITY, -INFINITY);
+
+ Vector2 offset_pre = offset;
+ for (int i = 0; i < t.size(); i++) {
+ CharType c = t[i];
+ CharType n = t[i + 1];
+ uint32_t utf32_char = c;
+ if (((c & 0xfffffc00) == 0xd800) && (n & 0xfffffc00) == 0xdc00) { // decode surrogate pair.
+ utf32_char = (c << 10UL) + n - ((0xd800 << 10UL) + 0xdc00 - 0x10000);
+ }
+ if ((c & 0xfffffc00) == 0xdc00) { // skip trail surrogate.
+ continue;
+ }
+
+ _generate_glyph_mesh_data(utf32_char, font, c, n);
+ GlyphMeshData &gl_data = cache[utf32_char];
+
+ p_size += gl_data.triangles.size() * ((has_depth) ? 2 : 1);
+ i_size += gl_data.triangles.size() * ((has_depth) ? 2 : 1);
+
+ if (has_depth) {
+ for (int j = 0; j < gl_data.contours.size(); j++) {
+ p_size += gl_data.contours[j].size() * 4;
+ i_size += gl_data.contours[j].size() * 6;
+ }
+ }
+
+ min_p.x = MIN(gl_data.min_p.x + offset_pre.x, min_p.x);
+ min_p.y = MIN(gl_data.min_p.y + offset_pre.y, min_p.y);
+ max_p.x = MAX(gl_data.max_p.x + offset_pre.x, max_p.x);
+ max_p.y = MAX(gl_data.max_p.y + offset_pre.y, max_p.y);
+
+ offset_pre.x += font->get_char_size(c, n).x * pixel_size;
+ }
+
+ PoolVector vertices;
+ PoolVector normals;
+ PoolVector tangents;
+ PoolVector uvs;
+ PoolVector indices;
+
+ vertices.resize(p_size);
+ normals.resize(p_size);
+ uvs.resize(p_size);
+ tangents.resize(p_size * 4);
+ indices.resize(i_size);
+
+ PoolVector::Write vertices_ptr = vertices.write();
+ PoolVector::Write normals_ptr = normals.write();
+ PoolVector::Write tangents_ptr = tangents.write();
+ PoolVector::Write uvs_ptr = uvs.write();
+ PoolVector::Write indices_ptr = indices.write();
+
+ // Generate mesh.
+ int32_t p_idx = 0;
+ int32_t i_idx = 0;
+
+ for (int i = 0; i < t.size(); i++) {
+ CharType c = t[i];
+ CharType n = t[i + 1];
+ uint32_t utf32_char = c;
+ if (((c & 0xfffffc00) == 0xd800) && (n & 0xfffffc00) == 0xdc00) { // decode surrogate pair.
+ utf32_char = (c << 10UL) + n - ((0xd800 << 10UL) + 0xdc00 - 0x10000);
+ }
+ if ((c & 0xfffffc00) == 0xdc00) { // skip trail surrogate.
+ continue;
+ }
+ _generate_glyph_mesh_data(utf32_char, font, c, n);
+ GlyphMeshData &gl_data = cache[utf32_char];
+
+ int64_t ts = gl_data.triangles.size();
+ const Vector2 *ts_ptr = gl_data.triangles.ptr();
+
+ for (int k = 0; k < ts; k += 3) {
+ // Add front face.
+ for (int l = 0; l < 3; l++) {
+ Vector3 point = Vector3(ts_ptr[k + l].x + offset.x, -ts_ptr[k + l].y + offset.y, depth / 2.0);
+ vertices_ptr[p_idx] = point;
+ normals_ptr[p_idx] = Vector3(0.0, 0.0, 1.0);
+ if (has_depth) {
+ uvs_ptr[p_idx] = Vector2(Math::range_lerp(point.x, min_p.x, max_p.x, real_t(0.0), real_t(1.0)), Math::range_lerp(point.y, -min_p.y, -max_p.y, real_t(0.0), real_t(0.4)));
+ } else {
+ uvs_ptr[p_idx] = Vector2(Math::range_lerp(point.x, min_p.x, max_p.x, real_t(0.0), real_t(1.0)), Math::range_lerp(point.y, -min_p.y, -max_p.y, real_t(0.0), real_t(1.0)));
+ }
+ tangents_ptr[p_idx * 4 + 0] = 1.0;
+ tangents_ptr[p_idx * 4 + 1] = 0.0;
+ tangents_ptr[p_idx * 4 + 2] = 0.0;
+ tangents_ptr[p_idx * 4 + 3] = 1.0;
+ indices_ptr[i_idx++] = p_idx;
+ p_idx++;
+ }
+ if (has_depth) {
+ // Add back face.
+ for (int l = 2; l >= 0; l--) {
+ Vector3 point = Vector3(ts_ptr[k + l].x + offset.x, -ts_ptr[k + l].y + offset.y, -depth / 2.0);
+ vertices_ptr[p_idx] = point;
+ normals_ptr[p_idx] = Vector3(0.0, 0.0, -1.0);
+ uvs_ptr[p_idx] = Vector2(Math::range_lerp(point.x, min_p.x, max_p.x, real_t(0.0), real_t(1.0)), Math::range_lerp(point.y, -min_p.y, -max_p.y, real_t(0.4), real_t(0.8)));
+ tangents_ptr[p_idx * 4 + 0] = -1.0;
+ tangents_ptr[p_idx * 4 + 1] = 0.0;
+ tangents_ptr[p_idx * 4 + 2] = 0.0;
+ tangents_ptr[p_idx * 4 + 3] = 1.0;
+ indices_ptr[i_idx++] = p_idx;
+ p_idx++;
+ }
+ }
+ }
+ // Add sides.
+ if (has_depth) {
+ for (int k = 0; k < gl_data.contours.size(); k++) {
+ int64_t ps = gl_data.contours[k].size();
+ const ContourPoint *ps_ptr = gl_data.contours[k].ptr();
+ const ContourInfo &ps_info = gl_data.contours_info[k];
+ real_t length = 0.0;
+ for (int l = 0; l < ps; l++) {
+ int prev = (l == 0) ? (ps - 1) : (l - 1);
+ int next = (l + 1 == ps) ? 0 : (l + 1);
+ Vector2 d1;
+ Vector2 d2 = (ps_ptr[next].point - ps_ptr[l].point).normalized();
+ if (ps_ptr[l].sharp) {
+ d1 = d2;
+ } else {
+ d1 = (ps_ptr[l].point - ps_ptr[prev].point).normalized();
+ }
+ real_t seg_len = (ps_ptr[next].point - ps_ptr[l].point).length();
+
+ Vector3 quad_faces[4] = {
+ Vector3(ps_ptr[l].point.x + offset.x, -ps_ptr[l].point.y + offset.y, -depth / 2.0),
+ Vector3(ps_ptr[next].point.x + offset.x, -ps_ptr[next].point.y + offset.y, -depth / 2.0),
+ Vector3(ps_ptr[l].point.x + offset.x, -ps_ptr[l].point.y + offset.y, depth / 2.0),
+ Vector3(ps_ptr[next].point.x + offset.x, -ps_ptr[next].point.y + offset.y, depth / 2.0),
+ };
+ for (int m = 0; m < 4; m++) {
+ const Vector2 &d = ((m % 2) == 0) ? d1 : d2;
+ real_t u_pos = ((m % 2) == 0) ? length : length + seg_len;
+ vertices_ptr[p_idx + m] = quad_faces[m];
+ normals_ptr[p_idx + m] = Vector3(d.y, d.x, 0.0);
+ if (m < 2) {
+ uvs_ptr[p_idx + m] = Vector2(Math::range_lerp(u_pos, 0, ps_info.length, real_t(0.0), real_t(1.0)), (ps_info.ccw) ? 0.8 : 0.9);
+ } else {
+ uvs_ptr[p_idx + m] = Vector2(Math::range_lerp(u_pos, 0, ps_info.length, real_t(0.0), real_t(1.0)), (ps_info.ccw) ? 0.9 : 1.0);
+ }
+ tangents_ptr[(p_idx + m) * 4 + 0] = d.x;
+ tangents_ptr[(p_idx + m) * 4 + 1] = -d.y;
+ tangents_ptr[(p_idx + m) * 4 + 2] = 0.0;
+ tangents_ptr[(p_idx + m) * 4 + 3] = 1.0;
+ }
+
+ indices_ptr[i_idx++] = p_idx;
+ indices_ptr[i_idx++] = p_idx + 1;
+ indices_ptr[i_idx++] = p_idx + 2;
+
+ indices_ptr[i_idx++] = p_idx + 1;
+ indices_ptr[i_idx++] = p_idx + 3;
+ indices_ptr[i_idx++] = p_idx + 2;
+
+ length += seg_len;
+ p_idx += 4;
+ }
+ }
+ }
+ offset.x += font->get_char_size(c, n).x * pixel_size;
+ }
+
+ if (p_size == 0) {
+ // If empty, add single trinagle to suppress errors.
+ vertices.push_back(Vector3());
+ normals.push_back(Vector3());
+ uvs.push_back(Vector2());
+ tangents.push_back(1.0);
+ tangents.push_back(0.0);
+ tangents.push_back(0.0);
+ tangents.push_back(1.0);
+ indices.push_back(0);
+ indices.push_back(0);
+ indices.push_back(0);
+ }
+
+ p_arr[VS::ARRAY_VERTEX] = vertices;
+ p_arr[VS::ARRAY_NORMAL] = normals;
+ p_arr[VS::ARRAY_TANGENT] = tangents;
+ p_arr[VS::ARRAY_TEX_UV] = uvs;
+ p_arr[VS::ARRAY_INDEX] = indices;
+}
+
+void TextMesh::_bind_methods() {
+ ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &TextMesh::set_horizontal_alignment);
+ ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &TextMesh::get_horizontal_alignment);
+
+ ClassDB::bind_method(D_METHOD("set_text", "text"), &TextMesh::set_text);
+ ClassDB::bind_method(D_METHOD("get_text"), &TextMesh::get_text);
+
+ ClassDB::bind_method(D_METHOD("set_font", "font"), &TextMesh::set_font);
+ ClassDB::bind_method(D_METHOD("get_font"), &TextMesh::get_font);
+
+ ClassDB::bind_method(D_METHOD("set_depth", "depth"), &TextMesh::set_depth);
+ ClassDB::bind_method(D_METHOD("get_depth"), &TextMesh::get_depth);
+
+ ClassDB::bind_method(D_METHOD("set_pixel_size", "pixel_size"), &TextMesh::set_pixel_size);
+ ClassDB::bind_method(D_METHOD("get_pixel_size"), &TextMesh::get_pixel_size);
+
+ ClassDB::bind_method(D_METHOD("set_curve_step", "curve_step"), &TextMesh::set_curve_step);
+ ClassDB::bind_method(D_METHOD("get_curve_step"), &TextMesh::get_curve_step);
+
+ ClassDB::bind_method(D_METHOD("set_uppercase", "enable"), &TextMesh::set_uppercase);
+ ClassDB::bind_method(D_METHOD("is_uppercase"), &TextMesh::is_uppercase);
+
+ ClassDB::bind_method(D_METHOD("_font_changed"), &TextMesh::_font_changed);
+ ClassDB::bind_method(D_METHOD("_request_update"), &TextMesh::_request_update);
+
+ ADD_GROUP("Text", "");
+ ADD_PROPERTY(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text");
+ ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "font", PROPERTY_HINT_RESOURCE_TYPE, "Font"), "set_font", "get_font");
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "horizontal_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_horizontal_alignment", "get_horizontal_alignment");
+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "uppercase"), "set_uppercase", "is_uppercase");
+
+ ADD_GROUP("Mesh", "");
+ ADD_PROPERTY(PropertyInfo(Variant::REAL, "pixel_size", PROPERTY_HINT_RANGE, "0.0001,128,0.0001"), "set_pixel_size", "get_pixel_size");
+ ADD_PROPERTY(PropertyInfo(Variant::REAL, "curve_step", PROPERTY_HINT_RANGE, "0.1,10,0.1"), "set_curve_step", "get_curve_step");
+ ADD_PROPERTY(PropertyInfo(Variant::REAL, "depth", PROPERTY_HINT_RANGE, "0.0,100.0,0.001,or_greater"), "set_depth", "get_depth");
+
+ BIND_ENUM_CONSTANT(ALIGN_LEFT);
+ BIND_ENUM_CONSTANT(ALIGN_CENTER);
+ BIND_ENUM_CONSTANT(ALIGN_RIGHT);
+}
+
+void TextMesh::_notification(int p_what) {
+ switch (p_what) {
+ case MainLoop::NOTIFICATION_TRANSLATION_CHANGED: {
+ String new_text = tr(text);
+ if (new_text == xl_text) {
+ return; // Nothing new.
+ }
+ xl_text = new_text;
+ _request_update();
+ } break;
+ }
+}
+
+TextMesh::TextMesh() {
+ primitive_type = PRIMITIVE_TRIANGLES;
+}
+
+TextMesh::~TextMesh() {
+}
+
+void TextMesh::set_horizontal_alignment(TextMesh::Align p_alignment) {
+ ERR_FAIL_INDEX((int)p_alignment, 3);
+ if (horizontal_alignment != p_alignment) {
+ horizontal_alignment = p_alignment;
+ _request_update();
+ }
+}
+
+TextMesh::Align TextMesh::get_horizontal_alignment() const {
+ return horizontal_alignment;
+}
+
+void TextMesh::set_text(const String &p_string) {
+ if (text != p_string) {
+ text = p_string;
+ xl_text = tr(text);
+ _request_update();
+ }
+}
+
+String TextMesh::get_text() const {
+ return text;
+}
+
+void TextMesh::_font_changed() {
+ dirty_cache = true;
+ call_deferred("_request_update");
+}
+
+void TextMesh::set_font(const Ref &p_font) {
+ if (font_override != p_font) {
+ if (font_override.is_valid()) {
+ font_override->disconnect(CoreStringNames::get_singleton()->changed, this, "_font_changed");
+ }
+ font_override = p_font;
+ dirty_cache = true;
+ if (font_override.is_valid()) {
+ font_override->connect(CoreStringNames::get_singleton()->changed, this, "_font_changed");
+ }
+ _request_update();
+ }
+}
+
+Ref TextMesh::get_font() const {
+ return font_override;
+}
+
+Ref TextMesh::_get_font_or_default() const {
+ if (font_override.is_valid()) {
+ return font_override;
+ }
+
+ // Check the project-defined Theme resource.
+ if (Theme::get_project_default().is_valid()) {
+ List theme_types;
+ Theme::get_project_default()->get_type_dependencies(get_class_name(), StringName(), &theme_types);
+
+ for (List::Element *E = theme_types.front(); E; E = E->next()) {
+ if (Theme::get_project_default()->has_theme_item(Theme::DATA_TYPE_FONT, "font", E->get())) {
+ return Theme::get_project_default()->get_theme_item(Theme::DATA_TYPE_FONT, "font", E->get());
+ }
+ }
+ }
+
+ // Lastly, fall back on the items defined in the default Theme, if they exist.
+ {
+ List theme_types;
+ Theme::get_default()->get_type_dependencies(get_class_name(), StringName(), &theme_types);
+
+ for (List::Element *E = theme_types.front(); E; E = E->next()) {
+ if (Theme::get_default()->has_theme_item(Theme::DATA_TYPE_FONT, "font", E->get())) {
+ return Theme::get_default()->get_theme_item(Theme::DATA_TYPE_FONT, "font", E->get());
+ }
+ }
+ }
+
+ // If they don't exist, use any type to return the default/empty value.
+ return Theme::get_default()->get_theme_item(Theme::DATA_TYPE_FONT, "font", StringName());
+}
+
+void TextMesh::set_depth(real_t p_depth) {
+ if (depth != p_depth) {
+ depth = MAX(p_depth, 0.0);
+ _request_update();
+ }
+}
+
+real_t TextMesh::get_depth() const {
+ return depth;
+}
+
+void TextMesh::set_pixel_size(real_t p_amount) {
+ if (pixel_size != p_amount) {
+ pixel_size = CLAMP(p_amount, 0.0001, 128.0);
+ dirty_cache = true;
+ _request_update();
+ }
+}
+
+real_t TextMesh::get_pixel_size() const {
+ return pixel_size;
+}
+
+void TextMesh::set_curve_step(real_t p_step) {
+ if (curve_step != p_step) {
+ curve_step = CLAMP(p_step, 0.1, 10.0);
+ dirty_cache = true;
+ _request_update();
+ }
+}
+
+real_t TextMesh::get_curve_step() const {
+ return curve_step;
+}
+
+void TextMesh::set_uppercase(bool p_uppercase) {
+ if (uppercase != p_uppercase) {
+ uppercase = p_uppercase;
+ _request_update();
+ }
+}
+
+bool TextMesh::is_uppercase() const {
+ return uppercase;
+}
diff --git a/scene/resources/primitive_meshes.h b/scene/resources/primitive_meshes.h
index 6468676ef34..410735541bd 100644
--- a/scene/resources/primitive_meshes.h
+++ b/scene/resources/primitive_meshes.h
@@ -31,6 +31,7 @@
#ifndef PRIMITIVE_MESHES_H
#define PRIMITIVE_MESHES_H
+#include "scene/resources/font.h"
#include "scene/resources/mesh.h"
///@TODO probably should change a few integers to unsigned integers...
@@ -364,4 +365,100 @@ public:
PointMesh();
};
+/**
+ Text...
+*/
+
+class TextMesh : public PrimitiveMesh {
+ GDCLASS(TextMesh, PrimitiveMesh);
+
+public:
+ enum Align {
+
+ ALIGN_LEFT,
+ ALIGN_CENTER,
+ ALIGN_RIGHT
+ };
+
+private:
+ struct ContourPoint {
+ Vector2 point;
+ bool sharp = false;
+
+ ContourPoint(){};
+ ContourPoint(const Vector2 &p_pt, bool p_sharp) {
+ point = p_pt;
+ sharp = p_sharp;
+ };
+ };
+ struct ContourInfo {
+ real_t length = 0.0;
+ bool ccw = true;
+ ContourInfo(){};
+ ContourInfo(real_t p_len, bool p_ccw) {
+ length = p_len;
+ ccw = p_ccw;
+ }
+ };
+ struct GlyphMeshData {
+ Vector triangles;
+ Vector> contours;
+ Vector contours_info;
+ Vector2 min_p = Vector2(INFINITY, INFINITY);
+ Vector2 max_p = Vector2(-INFINITY, -INFINITY);
+ };
+ mutable HashMap cache;
+
+ String text;
+ String xl_text;
+
+ Ref font_override;
+
+ Align horizontal_alignment = ALIGN_CENTER;
+ bool uppercase = false;
+
+ real_t depth = 0.05;
+ real_t pixel_size = 0.01;
+ real_t curve_step = 0.5;
+
+ mutable bool dirty_cache = true;
+
+ void _generate_glyph_mesh_data(uint32_t p_utf32_char, const Ref &p_font, CharType p_char, CharType p_next) const;
+ void _font_changed();
+
+protected:
+ static void _bind_methods();
+ void _notification(int p_what);
+
+ virtual void _create_mesh_array(Array &p_arr) const;
+
+public:
+ TextMesh();
+ ~TextMesh();
+
+ void set_horizontal_alignment(Align p_alignment);
+ Align get_horizontal_alignment() const;
+
+ void set_text(const String &p_string);
+ String get_text() const;
+
+ void set_font(const Ref &p_font);
+ Ref get_font() const;
+ Ref _get_font_or_default() const;
+
+ void set_uppercase(bool p_uppercase);
+ bool is_uppercase() const;
+
+ void set_depth(real_t p_depth);
+ real_t get_depth() const;
+
+ void set_curve_step(real_t p_step);
+ real_t get_curve_step() const;
+
+ void set_pixel_size(real_t p_amount);
+ real_t get_pixel_size() const;
+};
+
+VARIANT_ENUM_CAST(TextMesh::Align);
+
#endif
]