diff --git a/scene/2d/polygon_2d.cpp b/scene/2d/polygon_2d.cpp index 1dae69844f..ebfd9c3322 100644 --- a/scene/2d/polygon_2d.cpp +++ b/scene/2d/polygon_2d.cpp @@ -354,7 +354,19 @@ void Polygon2D::_notification(int p_what) { } } - RS::get_singleton()->mesh_clear(mesh); + bool has_uv = uvs.size() == points.size(); + bool has_color = colors.size() == points.size(); + bool has_bones = skeleton_node != nullptr; + + bool needs_clear = len != last_len; + needs_clear |= index_array.size() != last_index_count; + needs_clear |= has_uv != last_has_uv; + needs_clear |= has_color != last_has_color; + needs_clear |= has_bones || has_bones != last_has_bones; + + if (needs_clear) { + RS::get_singleton()->mesh_clear(mesh); + } if (index_array.size()) { Array arr; @@ -397,10 +409,24 @@ void Polygon2D::_notification(int p_what) { return; } - RS::get_singleton()->mesh_add_surface(mesh, sd); - RS::get_singleton()->canvas_item_add_mesh(get_canvas_item(), mesh, Transform2D(), Color(1, 1, 1), texture.is_valid() ? texture->get_scaled_rid() : RID()); + if (needs_clear) { + RS::get_singleton()->mesh_add_surface(mesh, sd); + } else { + RS::get_singleton()->mesh_surface_update_vertex_region(mesh, 0, 0, sd.vertex_data); + if (has_uv || has_color) { + RS::get_singleton()->mesh_surface_update_attribute_region(mesh, 0, 0, sd.attribute_data); + } + RS::get_singleton()->mesh_surface_update_index_region(mesh, 0, 0, sd.index_data); + } } + last_len = len; + last_index_count = index_array.size(); + last_has_uv = has_uv; + last_has_color = has_color; + last_has_bones = has_bones; + + RS::get_singleton()->canvas_item_add_mesh(get_canvas_item(), mesh, Transform2D(), Color(1, 1, 1), texture.is_valid() ? texture->get_scaled_rid() : RID()); } break; } } diff --git a/scene/2d/polygon_2d.h b/scene/2d/polygon_2d.h index 57c6596603..8250f59b56 100644 --- a/scene/2d/polygon_2d.h +++ b/scene/2d/polygon_2d.h @@ -74,6 +74,11 @@ class Polygon2D : public Node2D { void _skeleton_bone_setup_changed(); RID mesh; + int last_len = 0; + int last_index_count = 0; + bool last_has_uv = false; + bool last_has_color = false; + bool last_has_bones = false; protected: void _notification(int p_what);