- Implemented a much more efficient bone weight calculation.

- Added debug messages for measuring the time each step of a mesh slice takes.
This commit is contained in:
2026-08-23 22:18:47 -04:00
parent 814299e449
commit bbaa3d01d2
+92 -51
View File
@@ -11,6 +11,7 @@
#include <map>
#include "godot_cpp/classes/time.hpp"
#include "godot_cpp/variant/utility_functions.hpp"
using namespace godot;
@@ -25,11 +26,17 @@ MeshEditingLibrary::~MeshEditingLibrary()
PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original_mesh_instance, const Skeleton3D *original_skeleton, const Plane &local_plane, ArrayMesh *out_first_half, ArrayMesh *out_other_half, const MeshEditCapUV cap_option, const Ref<StandardMaterial3D> cap_material)
{
const Time *time = Time::get_singleton();
UtilityFunctions::print("Started slicing mesh at ", time->get_ticks_msec(), "ms");
const Mesh *original_mesh = original_mesh_instance->get_mesh().ptr();
const uint8_t num_surfaces = original_mesh->get_surface_count();
const SkinnedSurfaces &skinned_vertex_positions = MeshEditingLibrary::get_skinned_vertex_positions(original_mesh, original_skeleton);
UtilityFunctions::print("Got skinned vertices at ", time->get_ticks_msec(), "ms");
PackedVector3Array impact_points;
PackedVector3Array cap_section_vertices;
@@ -158,6 +165,8 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original
PackedInt32Array other_half_section_indices;
const PackedInt32Array &surface_index_array = surface_arrays[ArrayMesh::ARRAY_INDEX];
UtilityFunctions::print("Surface ", surface, " sorted by plane distance at ", time->get_ticks_msec(), "ms");
if (!(first_half_section_vertices.size() > 0 && other_half_section_vertices.size() > 0))
{
if (first_half_section_vertices.size() > 0)
@@ -453,6 +462,8 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original
out_other_half->surface_set_material(surface, original_mesh->surface_get_material(surface));
}
UtilityFunctions::print("Surface ", surface, " split edges at ", time->get_ticks_msec(), "ms");
if (clip_edges.size() > 0)
{
std::vector<MeshEditEdge2D> edges_2d;
@@ -535,6 +546,8 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original
impact_points.append(polygon_centroid);
}
}
UtilityFunctions::print("Surface ", surface, " created cap surface at ", time->get_ticks_msec(), "ms");
}
if (cap_section_vertices.size() > 0 && cap_section_indices.size() > 0)
@@ -575,6 +588,8 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original
out_other_half->add_surface_from_arrays(ArrayMesh::PRIMITIVE_TRIANGLES, cap_other_mesh_section);
out_other_half->surface_set_material(out_other_half->get_surface_count()-1, cap_material);
}
UtilityFunctions::print("Generated final cap surface at ", time->get_ticks_msec(), "ms");
return impact_points;
}
@@ -585,68 +600,94 @@ SkinnedSurfaces MeshEditingLibrary::get_skinned_vertex_positions(const Mesh *mes
SkinnedSurfaces skinned_vertex_surfaces;
skinned_vertex_surfaces.resize(num_surfaces);
for (uint8_t surface = 0; surface < num_surfaces; surface++)
TypedArray<Transform3D> bone_transforms;
if (skeleton)
{
const Array &mesh_arrays = mesh->surface_get_arrays(surface);
const PackedVector3Array &mesh_vertex_array = mesh_arrays[ArrayMesh::ARRAY_VERTEX];
const uint32_t mesh_vertex_array_size = mesh_vertex_array.size();
SkinnedVertices skinned_vertex_array;
skinned_vertex_array.resize(mesh_vertex_array_size);
if (skeleton)
const uint16_t bone_count = skeleton->get_bone_count();
bone_transforms.resize(bone_count);
for (uint8_t bone_index = 0; bone_index < bone_count; bone_index++)
{
const PackedInt32Array &mesh_bones_array = mesh_arrays[ArrayMesh::ARRAY_BONES];
const PackedFloat32Array &mesh_weights_array = mesh_arrays[ArrayMesh::ARRAY_WEIGHTS];
const uint8_t num_bones_per_vertex = mesh_bones_array.size() / mesh_vertex_array_size;
for (uint32_t vertex_index = 0; vertex_index < mesh_vertex_array_size; vertex_index++)
{
const Vector3 &vertex = mesh_vertex_array[vertex_index];
Transform3D *transforms = new Transform3D[num_bones_per_vertex];
const uint32_t bone_index_start = (vertex_index * num_bones_per_vertex);
for (uint8_t bone_index_offset = 0; bone_index_offset < num_bones_per_vertex; bone_index_offset++)
{
const uint32_t bone = mesh_bones_array[bone_index_start + bone_index_offset];
const float weight = mesh_weights_array[bone_index_start + bone_index_offset];
transforms[bone_index_offset] = skeleton->get_bone_global_pose(bone) * skeleton->get_bone_global_rest(bone).inverse() * weight;
}
Vector3 x_basis, y_basis, z_basis, origin;
for (uint8_t transform = 0; transform < num_bones_per_vertex; transform++)
{
x_basis += transforms[transform].basis.rows[0];
y_basis += transforms[transform].basis.rows[1];
z_basis += transforms[transform].basis.rows[2];
origin += transforms[transform].origin;
}
const Vector3 &transformed_vertex = Vector3(
x_basis.dot(vertex) + origin.x,
y_basis.dot(vertex) + origin.y,
z_basis.dot(vertex) + origin.z);
skinned_vertex_array[vertex_index] = transformed_vertex;
// The above is identical to the code seen below, except the below code does not work.
// This is fucking stupid.
//
// const Transform3D &final_transform = Transform3D(x_basis, y_basis, z_basis, origin);
// skinned_vertex_array[vertex_index] = final_transform.xform(vertex);
delete transforms;
}
bone_transforms[bone_index] = skeleton->get_bone_global_pose(bone_index) * skeleton->get_bone_global_rest(bone_index).inverse();
}
else
for (uint8_t surface = 0; surface < num_surfaces; surface++)
{
const Array &mesh_arrays = mesh->surface_get_arrays(surface);
const PackedVector3Array &mesh_vertex_array = mesh_arrays[ArrayMesh::ARRAY_VERTEX];
const uint32_t mesh_vertex_array_size = mesh_vertex_array.size();
SkinnedVertices skinned_vertex_array;
skinned_vertex_array.resize(mesh_vertex_array_size);
if (skeleton)
{
const PackedInt32Array &mesh_bones_array = mesh_arrays[ArrayMesh::ARRAY_BONES];
const PackedFloat32Array &mesh_weights_array = mesh_arrays[ArrayMesh::ARRAY_WEIGHTS];
const uint8_t num_bones_per_vertex = mesh_bones_array.size() / mesh_vertex_array_size;
for (uint32_t vertex_index = 0; vertex_index < mesh_vertex_array_size; vertex_index++)
{
const Vector3 &vertex = mesh_vertex_array[vertex_index];
Transform3D *transforms = new Transform3D[num_bones_per_vertex];
const uint32_t bone_index_start = (vertex_index * num_bones_per_vertex);
for (uint8_t bone_index_offset = 0; bone_index_offset < num_bones_per_vertex; bone_index_offset++)
{
const float weight = mesh_weights_array[bone_index_start + bone_index_offset];
const uint32_t bone = mesh_bones_array[bone_index_start + bone_index_offset];
transforms[bone_index_offset] = ((Transform3D)bone_transforms[bone]) * weight;
}
Vector3 x_basis, y_basis, z_basis, origin;
for (uint8_t transform = 0; transform < num_bones_per_vertex; transform++)
{
x_basis += transforms[transform].basis.rows[0];
y_basis += transforms[transform].basis.rows[1];
z_basis += transforms[transform].basis.rows[2];
origin += transforms[transform].origin;
}
const Vector3 &transformed_vertex = Vector3(
x_basis.dot(vertex) + origin.x,
y_basis.dot(vertex) + origin.y,
z_basis.dot(vertex) + origin.z);
skinned_vertex_array[vertex_index] = transformed_vertex;
// The above is identical to the code seen below, except the below code does not work.
// This is fucking stupid.
//
// const Transform3D &final_transform = Transform3D(x_basis, y_basis, z_basis, origin);
// skinned_vertex_array[vertex_index] = final_transform.xform(vertex);
delete transforms;
}
}
skinned_vertex_surfaces[surface] = skinned_vertex_array;
}
return skinned_vertex_surfaces;
}
else
{
for (uint8_t surface = 0; surface < num_surfaces; surface++)
{
const Array &mesh_arrays = mesh->surface_get_arrays(surface);
const PackedVector3Array &mesh_vertex_array = mesh_arrays[ArrayMesh::ARRAY_VERTEX];
const uint32_t mesh_vertex_array_size = mesh_vertex_array.size();
SkinnedVertices skinned_vertex_array;
skinned_vertex_array.resize(mesh_vertex_array_size);
for (uint32_t vertex_index = 0; vertex_index < mesh_vertex_array_size; vertex_index++)
{
skinned_vertex_array[vertex_index] = mesh_vertex_array[vertex_index];
}
}
skinned_vertex_surfaces[surface] = skinned_vertex_array;
return skinned_vertex_surfaces;
}
return skinned_vertex_surfaces;
return SkinnedSurfaces();
}