From a32a2613e9d866d1958544246dca6fbfd00179d9 Mon Sep 17 00:00:00 2001 From: lawnjelly Date: Sun, 11 Feb 2024 09:30:26 +0000 Subject: [PATCH] Fix `Mesh::get_face_count()` This fixes a minor bug whereby facecount was actually returning the facecount * 3. There were no major problems from this, but it did mean the optional threshold poly count used when merging was out by a factor of 3. --- editor/plugins/merge_group_editor_plugin.cpp | 2 +- scene/resources/mesh.cpp | 20 ++++++++++---------- scene/resources/mesh.h | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/editor/plugins/merge_group_editor_plugin.cpp b/editor/plugins/merge_group_editor_plugin.cpp index fd9ab62c46c..10f74959cc5 100644 --- a/editor/plugins/merge_group_editor_plugin.cpp +++ b/editor/plugins/merge_group_editor_plugin.cpp @@ -324,7 +324,7 @@ void MergeGroupEditorPlugin::_remove_queue_deleted_nodes_recursive(Node *p_node) uint32_t MergeGroupEditorPlugin::_get_mesh_poly_count(const MeshInstance &p_mi) const { Ref rmesh = p_mi.get_mesh(); if (rmesh.is_valid()) { - return rmesh->get_face_count(); + return rmesh->get_triangle_count(); } return 0; diff --git a/scene/resources/mesh.cpp b/scene/resources/mesh.cpp index e51c6df397a..729e8d10569 100644 --- a/scene/resources/mesh.cpp +++ b/scene/resources/mesh.cpp @@ -42,7 +42,7 @@ Mesh::ConvexDecompositionFunc Mesh::convex_decomposition_function = nullptr; -int Mesh::surface_get_face_count(int p_idx) const { +int Mesh::surface_get_triangle_count(int p_idx) const { ERR_FAIL_INDEX_V(p_idx, get_surface_count(), 0); switch (surface_get_primitive_type(p_idx)) { @@ -50,14 +50,14 @@ int Mesh::surface_get_face_count(int p_idx) const { int len = (surface_get_format(p_idx) & ARRAY_FORMAT_INDEX) ? surface_get_array_index_len(p_idx) : surface_get_array_len(p_idx); // Don't error if zero, it's valid (we'll just skip it later). ERR_FAIL_COND_V_MSG((len % 3) != 0, 0, vformat("Ignoring surface %d, incorrect %s count: %d (for PRIMITIVE_TRIANGLES).", p_idx, (surface_get_format(p_idx) & ARRAY_FORMAT_INDEX) ? "index" : "vertex", len)); - return len; + return len / 3; } break; case PRIMITIVE_TRIANGLE_FAN: case PRIMITIVE_TRIANGLE_STRIP: { int len = (surface_get_format(p_idx) & ARRAY_FORMAT_INDEX) ? surface_get_array_index_len(p_idx) : surface_get_array_len(p_idx); // Don't error if zero, it's valid (we'll just skip it later). ERR_FAIL_COND_V_MSG(len != 0 && len < 3, 0, vformat("Ignoring surface %d, incorrect %s count: %d (for %s).", p_idx, (surface_get_format(p_idx) & ARRAY_FORMAT_INDEX) ? "index" : "vertex", len, (surface_get_primitive_type(p_idx) == PRIMITIVE_TRIANGLE_FAN) ? "PRIMITIVE_TRIANGLE_FAN" : "PRIMITIVE_TRIANGLE_STRIP")); - return (len == 0) ? 0 : (len - 2) * 3; + return (len == 0) ? 0 : (len - 2); } break; default: { } break; @@ -66,14 +66,14 @@ int Mesh::surface_get_face_count(int p_idx) const { return 0; } -int Mesh::get_face_count() const { - int faces_size = 0; +int Mesh::get_triangle_count() const { + int triangle_count = 0; for (int i = 0; i < get_surface_count(); i++) { - faces_size += surface_get_face_count(i); + triangle_count += surface_get_triangle_count(i); } - return faces_size; + return triangle_count; } Ref Mesh::generate_triangle_mesh_from_aabb() const { @@ -151,14 +151,14 @@ Ref Mesh::generate_triangle_mesh() const { return triangle_mesh; } - int faces_size = get_face_count(); + int faces_vertex_count = get_triangle_count() * 3; - if (faces_size == 0) { + if (faces_vertex_count == 0) { return triangle_mesh; } PoolVector faces; - faces.resize(faces_size); + faces.resize(faces_vertex_count); PoolVector::Write facesw = faces.write(); int widx = 0; diff --git a/scene/resources/mesh.h b/scene/resources/mesh.h index dab429c538c..d9c973eb5d6 100644 --- a/scene/resources/mesh.h +++ b/scene/resources/mesh.h @@ -137,11 +137,11 @@ public: virtual void surface_set_material(int p_idx, const Ref &p_material) = 0; virtual Ref surface_get_material(int p_idx) const = 0; virtual int get_blend_shape_count() const = 0; - int surface_get_face_count(int p_idx) const; + int surface_get_triangle_count(int p_idx) const; virtual StringName get_blend_shape_name(int p_index) const = 0; virtual void set_blend_shape_name(int p_index, const StringName &p_name) = 0; - int get_face_count() const; + int get_triangle_count() const; PoolVector get_faces() const; Ref generate_triangle_mesh() const; Ref generate_triangle_mesh_from_aabb() const;