diff --git a/modules/paint/nodes/curve_2d/paint_curve_2d.cpp b/modules/paint/nodes/curve_2d/paint_curve_2d.cpp index 28a0886e0..7a5f75b0d 100644 --- a/modules/paint/nodes/curve_2d/paint_curve_2d.cpp +++ b/modules/paint/nodes/curve_2d/paint_curve_2d.cpp @@ -117,15 +117,20 @@ void PaintCurve2D::_notification(int p_what) { if (_outline_enabled) { _cached_draw_pts = Variant(curve->tessellate()); - { - int len = _cached_draw_pts.size(); - Vector2 *ppw = _cached_draw_pts.ptrw(); - for (int i = 0; i < len; i++) { - ppw[i] = ppw[i]; - } - } + if (_outline_texture.is_valid()) { + Vector points; + Vector uvs; + Vector colors; + Vector indices; - draw_polyline(_cached_draw_pts, _outline_color, _outline_width, _outline_antialiased); + _prepare_render_data_outline(points, uvs, colors, indices); + + if (indices.size()) { + RS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), indices, points, colors, uvs, Vector(), Vector(), _outline_texture.is_valid() ? _outline_texture->get_rid() : RID(), -1, RID(), _outline_antialiased); + } + } else { + draw_polyline(_cached_draw_pts, _outline_color, _outline_width, _outline_antialiased); + } } } } @@ -625,6 +630,45 @@ PoolVector2Array PaintCurve2D::generate_uvs(const Vector &p_points, con return uvs; } +void PaintCurve2D::generate_polyline_mesh(const Vector &p_points, float p_width, Vector &r_triangles, Vector &r_indices) { + Vector2 prev_t; + + r_triangles.resize(p_points.size() * 2); + r_indices.resize(p_points.size() * 6); + + for (int i = 0; i < p_points.size(); i++) { + Vector2 t; + if (i == p_points.size() - 1) { + t = prev_t; + } else { + t = (p_points[i + 1] - p_points[i]).normalized().tangent(); + if (i == 0) { + prev_t = t; + } + } + + Vector2 tangent = ((t + prev_t).normalized()) * p_width * 0.5; + + r_triangles.write[i * 2 + 0] = p_points[i] + tangent; + r_triangles.write[i * 2 + 1] = p_points[i] - tangent; + + if (i != 0) { + int indx = (i - 1) * 2; + int iindx = (i - 1) * 6; + + r_indices.write[iindx] = indx; + r_indices.write[iindx + 1] = indx + 1; + r_indices.write[iindx + 2] = indx + 3; + + r_indices.write[iindx + 3] = indx + 0; + r_indices.write[iindx + 4] = indx + 2; + r_indices.write[iindx + 5] = indx + 3; + } + + prev_t = t; + } +} + void PaintCurve2D::_prepare_render_data_fill(Vector &r_points, Vector &r_uvs, Vector &r_colors, Vector &r_indices) { if (!curve.is_valid()) { return; @@ -720,6 +764,57 @@ void PaintCurve2D::_prepare_render_data_fill(Vector &r_points, Vector &r_points, Vector &r_uvs, Vector &r_colors, Vector &r_indices) { + if (!curve.is_valid()) { + return; + } + + if (_outline_width <= 0) { + return; + } + + int curve_len = curve->get_point_count(); + + if (curve_len < 2) { + return; + } + + PoolVector2Array polygon = curve->tessellate(); + int len = polygon.size(); + + Vector curve_points; + curve_points.resize(len); + + { + PoolVector::Read polyr = polygon.read(); + for (int i = 0; i < len; i++) { + curve_points.write[i] = polyr[i]; + } + } + + generate_polyline_mesh(curve_points, _outline_width, r_points, r_indices); + + len = r_points.size(); + + if (_outline_texture.is_valid()) { + Transform2D texmat(_outline_tex_rot, _outline_tex_ofs); + texmat.scale(_outline_tex_scale); + //Size2 tex_size = _outline_texture->get_size(); + + PoolVector2Array uv = generate_uvs(r_points); + + r_uvs.resize(len); + + PoolVector::Read uvr = uv.read(); + + for (int i = 0; i < len; i++) { + r_uvs.write[i] = texmat.xform(uvr[i]); + } + } + + r_colors.push_back(_outline_color); +} + void PaintCurve2D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_curve", "curve"), &PaintCurve2D::set_curve); ClassDB::bind_method(D_METHOD("get_curve"), &PaintCurve2D::get_curve); @@ -825,7 +920,7 @@ PaintCurve2D::PaintCurve2D() { _outline_enabled = true; //_outline_color = Color(0.5, 0.6, 1.0, 0.7); _outline_width = 2; - _fill_tex_scale = Vector2(1, 1); + _outline_tex_scale = Vector2(1, 1); _outline_tex_tile = false; _outline_tex_rot = 0; _outline_antialiased = true; diff --git a/modules/paint/nodes/curve_2d/paint_curve_2d.h b/modules/paint/nodes/curve_2d/paint_curve_2d.h index b71a67459..d5e3499b9 100644 --- a/modules/paint/nodes/curve_2d/paint_curve_2d.h +++ b/modules/paint/nodes/curve_2d/paint_curve_2d.h @@ -113,7 +113,11 @@ protected: //Todo this should probably be moved to Geometry, or maybe MeshUtils PoolVector2Array generate_uvs(const Vector &p_points); PoolVector2Array generate_uvs(const Vector &p_points, const Rect2 &p_uv_rect); + + void generate_polyline_mesh(const Vector &p_points, float p_width, Vector &r_triangless, Vector &r_indices); + void _prepare_render_data_fill(Vector &r_points, Vector &r_uvs, Vector &r_colors, Vector &r_indices); + void _prepare_render_data_outline(Vector &r_points, Vector &r_uvs, Vector &r_colors, Vector &r_indices); void _notification(int p_what); static void _bind_methods();