Improve Path2D debug performance

Improves Path2D node debug performance by changing the debug to use mesh and multimesh instead of slower canvas draw functions.
This commit is contained in:
smix8
2025-06-07 11:43:50 +02:00
parent 8f87e60307
commit e64fe63f17
4 changed files with 379 additions and 80 deletions

View File

@@ -87,81 +87,169 @@ bool Path2D::_edit_is_selected_on_click(const Point2 &p_point, double p_toleranc
void Path2D::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
#ifdef DEBUG_ENABLED
_debug_create();
#endif
} break;
case NOTIFICATION_EXIT_TREE: {
#ifdef DEBUG_ENABLED
_debug_free();
#endif
} break;
// Draw the curve if path debugging is enabled.
case NOTIFICATION_DRAW: {
if (curve.is_null()) {
break;
}
if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_paths_hint()) {
return;
}
if (curve->get_point_count() < 2) {
return;
}
#ifdef TOOLS_ENABLED
const real_t line_width = get_tree()->get_debug_paths_width() * EDSCALE;
#else
const real_t line_width = get_tree()->get_debug_paths_width();
#ifdef DEBUG_ENABLED
_debug_update();
#endif
real_t interval = 10;
const real_t length = curve->get_baked_length();
if (length > CMP_EPSILON) {
const int sample_count = int(length / interval) + 2;
interval = length / (sample_count - 1); // Recalculate real interval length.
Vector<Transform2D> frames;
frames.resize(sample_count);
{
Transform2D *w = frames.ptrw();
for (int i = 0; i < sample_count; i++) {
w[i] = curve->sample_baked_with_rotation(i * interval, false);
}
}
const Transform2D *r = frames.ptr();
// Draw curve segments
{
PackedVector2Array v2p;
v2p.resize(sample_count);
Vector2 *w = v2p.ptrw();
for (int i = 0; i < sample_count; i++) {
w[i] = r[i].get_origin();
}
draw_polyline(v2p, get_tree()->get_debug_paths_color(), line_width, false);
}
// Draw fish bone every 4 points to reduce visual noise and performance impact
// (compared to drawing it for every point).
{
PackedVector2Array v2p;
v2p.resize(3);
Vector2 *w = v2p.ptrw();
for (int i = 0; i < sample_count; i += 4) {
const Vector2 p = r[i].get_origin();
const Vector2 side = r[i].columns[1];
const Vector2 forward = r[i].columns[0];
// Fish Bone.
w[0] = p + (side - forward) * 5;
w[1] = p;
w[2] = p + (-side - forward) * 5;
draw_polyline(v2p, get_tree()->get_debug_paths_color(), line_width * 0.5, false);
}
}
}
} break;
}
}
#ifdef DEBUG_ENABLED
void Path2D::_debug_create() {
ERR_FAIL_NULL(RS::get_singleton());
if (debug_mesh_rid.is_null()) {
debug_mesh_rid = RS::get_singleton()->mesh_create();
}
if (debug_instance.is_null()) {
debug_instance = RS::get_singleton()->instance_create();
}
RS::get_singleton()->instance_set_base(debug_instance, debug_mesh_rid);
RS::get_singleton()->instance_geometry_set_cast_shadows_setting(debug_instance, RS::SHADOW_CASTING_SETTING_OFF);
}
void Path2D::_debug_free() {
ERR_FAIL_NULL(RS::get_singleton());
if (debug_instance.is_valid()) {
RS::get_singleton()->free(debug_instance);
debug_instance = RID();
}
if (debug_mesh_rid.is_valid()) {
RS::get_singleton()->free(debug_mesh_rid);
debug_mesh_rid = RID();
}
}
void Path2D::_debug_update() {
ERR_FAIL_NULL(RS::get_singleton());
RenderingServer *rs = RS::get_singleton();
ERR_FAIL_NULL(SceneTree::get_singleton());
ERR_FAIL_NULL(RenderingServer::get_singleton());
const bool path_debug_enabled = (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_paths_hint());
if (!path_debug_enabled) {
_debug_free();
return;
}
if (debug_mesh_rid.is_null() || debug_instance.is_null()) {
_debug_create();
}
rs->mesh_clear(debug_mesh_rid);
if (curve.is_null()) {
return;
}
if (curve->get_point_count() < 2) {
return;
}
const real_t baked_length = curve->get_baked_length();
if (baked_length <= CMP_EPSILON) {
return;
}
const Color debug_color = get_tree()->get_debug_paths_color();
bool debug_paths_show_fish_bones = true;
real_t sample_interval = 10.0;
const int sample_count = int(baked_length / sample_interval) + 2;
sample_interval = baked_length / (sample_count - 1); // Recalculate real interval length.
Vector<Transform2D> samples;
samples.resize(sample_count);
Transform2D *samples_ptrw = samples.ptrw();
for (int i = 0; i < sample_count; i++) {
samples_ptrw[i] = curve->sample_baked_with_rotation(i * sample_interval, false);
}
const Transform2D *samples_ptr = samples.ptr();
// Draw curve segments
{
Vector<Vector2> ribbon;
ribbon.resize(sample_count);
Vector2 *ribbon_ptrw = ribbon.ptrw();
for (int i = 0; i < sample_count; i++) {
ribbon_ptrw[i] = samples_ptr[i].get_origin();
}
Array ribbon_array;
ribbon_array.resize(Mesh::ARRAY_MAX);
ribbon_array[Mesh::ARRAY_VERTEX] = ribbon;
Vector<Color> ribbon_color;
ribbon_color.resize(ribbon.size());
ribbon_color.fill(debug_color);
ribbon_array[Mesh::ARRAY_COLOR] = ribbon_color;
rs->mesh_add_surface_from_arrays(debug_mesh_rid, RS::PRIMITIVE_LINE_STRIP, ribbon_array, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
}
// Render path fish bones.
if (debug_paths_show_fish_bones) {
int fish_bones_interval = 4;
const int vertex_per_bone = 4;
Vector<Vector2> bones;
bones.resize(sample_count * vertex_per_bone);
Vector2 *bones_ptrw = bones.ptrw();
for (int i = 0; i < sample_count; i += fish_bones_interval) {
const Transform2D &sample_transform = samples_ptr[i];
const Vector2 point = sample_transform.get_origin();
const Vector2 &side = sample_transform.columns[1];
const Vector2 &forward = sample_transform.columns[0];
const int bone_idx = i * vertex_per_bone;
bones_ptrw[bone_idx] = point;
bones_ptrw[bone_idx + 1] = point + (side - forward) * 5;
bones_ptrw[bone_idx + 2] = point;
bones_ptrw[bone_idx + 3] = point + (-side - forward) * 5;
}
Array bone_array;
bone_array.resize(Mesh::ARRAY_MAX);
bone_array[Mesh::ARRAY_VERTEX] = bones;
Vector<Color> bones_color;
bones_color.resize(bones.size());
bones_color.fill(debug_color);
bone_array[Mesh::ARRAY_COLOR] = bones_color;
rs->mesh_add_surface_from_arrays(debug_mesh_rid, RS::PRIMITIVE_LINES, bone_array, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
}
rs->canvas_item_clear(get_canvas_item());
rs->canvas_item_add_mesh(get_canvas_item(), debug_mesh_rid, Transform2D());
}
#endif // DEBUG_ENABLED
void Path2D::_curve_changed() {
if (!is_inside_tree()) {
return;