From 2a055dd88161c937a0a90f4771baee170dd17c38 Mon Sep 17 00:00:00 2001 From: Jamie Greunbaum Date: Sat, 12 Sep 2026 11:56:10 -0400 Subject: [PATCH] Attempting to fix mesh slicing across multiple surfaces. --- src/liborng/nodes/mesh_editing_library.cpp | 471 ++++++++++++--------- src/liborng/nodes/mesh_editing_library.h | 6 +- 2 files changed, 274 insertions(+), 203 deletions(-) diff --git a/src/liborng/nodes/mesh_editing_library.cpp b/src/liborng/nodes/mesh_editing_library.cpp index 29a91a2..c40d1f1 100644 --- a/src/liborng/nodes/mesh_editing_library.cpp +++ b/src/liborng/nodes/mesh_editing_library.cpp @@ -11,6 +11,7 @@ #include +#include "godot_cpp/classes/immediate_mesh.hpp" #include "godot_cpp/classes/time.hpp" #include "godot_cpp/variant/utility_functions.hpp" using namespace godot; @@ -40,6 +41,38 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original PackedVector3Array impact_points; + std::vector surface_has_normal; + std::vector surface_has_tangent; + std::vector surface_has_uv; + std::vector surface_has_uv2; + std::vector surface_has_colour; + std::vector surface_has_bone; + std::vector surface_has_weight; + + std::vector surface_bone_array_size; + std::vector surface_num_bones_per_vertex; + + std::vector first_half_vertices; + std::vector first_half_normals; + std::vector first_half_tangents; + std::vector first_half_uvs; + std::vector first_half_uv2s; + std::vector first_half_colours; + std::vector first_half_bones; + std::vector first_half_weights; + + std::vector other_half_vertices; + std::vector other_half_normals; + std::vector other_half_tangents; + std::vector other_half_uvs; + std::vector other_half_uv2s; + std::vector other_half_colours; + std::vector other_half_bones; + std::vector other_half_weights; + + std::vector first_half_indices; + std::vector other_half_indices; + PackedVector3Array cap_section_vertices; PackedVector3Array cap_section_normals; PackedVector3Array cap_section_flipped_normals; @@ -52,6 +85,8 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original PackedFloat32Array cap_section_weights; PackedInt32Array cap_section_indices; + std::vector clip_edges; + for (uint8_t surface = 0; surface < num_surfaces; surface++) { const SkinnedVertices &surface_skinned_vertex_positions = skinned_vertex_positions[surface]; @@ -75,36 +110,34 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original std::map base_to_sliced_vert_index; std::map base_to_other_sliced_vert_index; - const bool has_normal = surface_normal_array.size() >= num_vertices; - const bool has_tangent = surface_tangent_array.size() >= num_vertices * 4; - const bool has_uv = surface_uv_array.size() >= num_vertices; - const bool has_uv2 = surface_uv2_array.size() >= num_vertices; - const bool has_colour = surface_colour_array.size() >= num_vertices; - const bool has_bone = surface_bone_array.size() >= num_vertices * 4; - const bool has_weight = surface_weight_array.size() >= num_vertices * 4; + const bool has_normal = surface_normal_array.size() >= num_vertices; surface_has_normal.push_back(has_normal); + const bool has_tangent = surface_tangent_array.size() >= num_vertices * 4; surface_has_tangent.push_back(has_tangent); + const bool has_uv = surface_uv_array.size() >= num_vertices; surface_has_uv.push_back(has_uv); + const bool has_uv2 = surface_uv2_array.size() >= num_vertices; surface_has_uv2.push_back(has_uv2); + const bool has_colour = surface_colour_array.size() >= num_vertices; surface_has_colour.push_back(has_colour); + const bool has_bone = surface_bone_array.size() >= num_vertices * 4; surface_has_bone.push_back(has_bone); + const bool has_weight = surface_weight_array.size() >= num_vertices * 4; surface_has_weight.push_back(has_weight); - const uint32_t bone_array_size = surface_bone_array.size(); - const uint8_t num_bones_per_vertex = bone_array_size / num_vertices; + const uint32_t bone_array_size = surface_bone_array.size(); surface_bone_array_size.push_back(bone_array_size); + const uint8_t num_bones_per_vertex = surface_bone_array_size[surface] / num_vertices; surface_num_bones_per_vertex.push_back(num_bones_per_vertex); - PackedVector3Array first_half_section_vertices; - PackedVector3Array first_half_section_normals; - PackedFloat32Array first_half_section_tangents; - PackedVector2Array first_half_section_uvs; - PackedVector2Array first_half_section_uv2s; - PackedColorArray first_half_section_colours; - PackedInt32Array first_half_section_bones; - PackedFloat32Array first_half_section_weights; + first_half_vertices.push_back(PackedVector3Array()); + first_half_normals.push_back(PackedVector3Array()); + first_half_tangents.push_back(PackedFloat32Array()); + first_half_uvs.push_back(PackedVector2Array()); + first_half_uv2s.push_back(PackedVector2Array()); + first_half_colours.push_back(PackedColorArray()); + first_half_bones.push_back(PackedInt32Array()); + first_half_weights.push_back(PackedFloat32Array()); - PackedVector3Array other_half_section_vertices; - PackedVector3Array other_half_section_normals; - PackedFloat32Array other_half_section_tangents; - PackedVector2Array other_half_section_uvs; - PackedVector2Array other_half_section_uv2s; - PackedColorArray other_half_section_colours; - PackedInt32Array other_half_section_bones; - PackedFloat32Array other_half_section_weights; - - std::vector clip_edges; + other_half_vertices.push_back(PackedVector3Array()); + other_half_normals.push_back(PackedVector3Array()); + other_half_tangents.push_back(PackedFloat32Array()); + other_half_uvs.push_back(PackedVector2Array()); + other_half_uv2s.push_back(PackedVector2Array()); + other_half_colours.push_back(PackedColorArray()); + other_half_bones.push_back(PackedInt32Array()); + other_half_weights.push_back(PackedFloat32Array()); for (uint32_t vertex = 0; vertex < num_vertices; vertex++) { @@ -112,71 +145,71 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original if (vertex_distance[vertex] >= 0.0f) { - base_to_sliced_vert_index[vertex] = first_half_section_vertices.size(); + base_to_sliced_vert_index[vertex] = first_half_vertices[surface].size(); - first_half_section_vertices.append(surface_vertex_array_ptr[vertex]); - if (has_normal) { first_half_section_normals.append(surface_normal_array.ptr()[vertex]); } - if (has_uv) { first_half_section_uvs.append(surface_uv_array.ptr()[vertex]); } - if (has_uv2) { first_half_section_uv2s.append(surface_uv2_array.ptr()[vertex]); } - if (has_colour) { first_half_section_colours.append(surface_colour_array.ptr()[vertex]); } + first_half_vertices[surface].append(surface_vertex_array_ptr[vertex]); + if (has_normal) { first_half_normals[surface].append(surface_normal_array.ptr()[vertex]); } + if (has_uv) { first_half_uvs[surface].append(surface_uv_array.ptr()[vertex]); } + if (has_uv2) { first_half_uv2s[surface].append(surface_uv2_array.ptr()[vertex]); } + if (has_colour) { first_half_colours[surface].append(surface_colour_array.ptr()[vertex]); } if (has_tangent) { - first_half_section_tangents.append(surface_tangent_array.ptr()[vertex * 4]); - first_half_section_tangents.append(surface_tangent_array.ptr()[(vertex * 4) + 1]); - first_half_section_tangents.append(surface_tangent_array.ptr()[(vertex * 4) + 2]); - first_half_section_tangents.append(surface_tangent_array.ptr()[(vertex * 4) + 3]); + first_half_tangents[surface].append(surface_tangent_array.ptr()[vertex * 4]); + first_half_tangents[surface].append(surface_tangent_array.ptr()[(vertex * 4) + 1]); + first_half_tangents[surface].append(surface_tangent_array.ptr()[(vertex * 4) + 2]); + first_half_tangents[surface].append(surface_tangent_array.ptr()[(vertex * 4) + 3]); } if (has_bone) { for (uint8_t i = 0; i < num_bones_per_vertex; i++) { - first_half_section_bones.append(surface_bone_array.ptr()[(vertex * num_bones_per_vertex) + i]); - first_half_section_weights.append(surface_weight_array.ptr()[(vertex * num_bones_per_vertex) + i]); + first_half_bones[surface].append(surface_bone_array.ptr()[(vertex * num_bones_per_vertex) + i]); + first_half_weights[surface].append(surface_weight_array.ptr()[(vertex * num_bones_per_vertex) + i]); } } } else { - base_to_other_sliced_vert_index[vertex] = other_half_section_vertices.size(); + base_to_other_sliced_vert_index[vertex] = other_half_vertices[surface].size(); - other_half_section_vertices.append(surface_vertex_array_ptr[vertex]); - if (has_normal) { other_half_section_normals.append(surface_normal_array.ptr()[vertex]); } - if (has_uv) { other_half_section_uvs.append(surface_uv_array.ptr()[vertex]); } - if (has_uv2) { other_half_section_uv2s.append(surface_uv2_array.ptr()[vertex]); } - if (has_colour) { other_half_section_colours.append(surface_colour_array.ptr()[vertex]); } + other_half_vertices[surface].append(surface_vertex_array_ptr[vertex]); + if (has_normal) { other_half_normals[surface].append(surface_normal_array.ptr()[vertex]); } + if (has_uv) { other_half_uvs[surface].append(surface_uv_array.ptr()[vertex]); } + if (has_uv2) { other_half_uv2s[surface].append(surface_uv2_array.ptr()[vertex]); } + if (has_colour) { other_half_colours[surface].append(surface_colour_array.ptr()[vertex]); } if (has_tangent) { - other_half_section_tangents.append(surface_tangent_array.ptr()[vertex * 4]); - other_half_section_tangents.append(surface_tangent_array.ptr()[(vertex * 4) + 1]); - other_half_section_tangents.append(surface_tangent_array.ptr()[(vertex * 4) + 2]); - other_half_section_tangents.append(surface_tangent_array.ptr()[(vertex * 4) + 3]); + other_half_tangents[surface].append(surface_tangent_array.ptr()[vertex * 4]); + other_half_tangents[surface].append(surface_tangent_array.ptr()[(vertex * 4) + 1]); + other_half_tangents[surface].append(surface_tangent_array.ptr()[(vertex * 4) + 2]); + other_half_tangents[surface].append(surface_tangent_array.ptr()[(vertex * 4) + 3]); } if (has_bone) { for (uint8_t i = 0; i < num_bones_per_vertex; i++) { - other_half_section_bones.append(surface_bone_array.ptr()[(vertex * num_bones_per_vertex) + i]); - other_half_section_weights.append(surface_weight_array.ptr()[(vertex * num_bones_per_vertex) + i]); + other_half_bones[surface].append(surface_bone_array.ptr()[(vertex * num_bones_per_vertex) + i]); + other_half_weights[surface].append(surface_weight_array.ptr()[(vertex * num_bones_per_vertex) + i]); } } } } - PackedInt32Array first_half_section_indices; - PackedInt32Array other_half_section_indices; + first_half_indices.push_back(PackedInt32Array()); + other_half_indices.push_back(PackedInt32Array()); const PackedInt32Array &surface_index_array = surface_arrays[ArrayMesh::ARRAY_INDEX]; PRINT_LOG(MESH_EDITING_LIBRARY_TAG, "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_vertices[surface].size() > 0 && other_half_vertices[surface].size() > 0)) { - if (first_half_section_vertices.size() > 0) + if (first_half_vertices[surface].size() > 0) { - first_half_section_indices = surface_index_array; + first_half_indices[surface] = surface_index_array; } - else if (other_half_section_vertices.size() > 0) + else if (other_half_vertices[surface].size() > 0) { - other_half_section_indices = surface_index_array; + other_half_indices[surface] = surface_index_array; } } else @@ -202,15 +235,15 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original if (sliced_v[0] != base_to_sliced_end && sliced_v[1] != base_to_sliced_end && sliced_v[2] != base_to_sliced_end) { // If the triangle is entirely in the first slice, send all the vertices to the first slice. - first_half_section_indices.append(sliced_v[0]->second); - first_half_section_indices.append(sliced_v[1]->second); - first_half_section_indices.append(sliced_v[2]->second); + first_half_indices[surface].append(sliced_v[0]->second); + first_half_indices[surface].append(sliced_v[1]->second); + first_half_indices[surface].append(sliced_v[2]->second); } else if (sliced_other_v[0] != base_to_other_sliced_end && sliced_other_v[1] != base_to_other_sliced_end && sliced_other_v[2] != base_to_other_sliced_end) { // If the triangle is entirely in the second slice, send all the vertices to the second slice. - other_half_section_indices.append(sliced_other_v[0]->second); - other_half_section_indices.append(sliced_other_v[1]->second); - other_half_section_indices.append(sliced_other_v[2]->second); + other_half_indices[surface].append(sliced_other_v[0]->second); + other_half_indices[surface].append(sliced_other_v[1]->second); + other_half_indices[surface].append(sliced_other_v[2]->second); } else { // If the triangle is split by the slice plane, then slice the overlapping edges. @@ -250,14 +283,15 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original const Vector3 &interp_vert = surface_vertex_array[base_v[this_vert]].lerp( surface_vertex_array[base_v[next_vert]], alpha); - final_verts[num_final_verts++] = first_half_section_vertices.size(); - other_final_verts[num_other_final_verts++] = other_half_section_vertices.size(); + final_verts[num_final_verts++] = first_half_vertices[surface].size(); + other_final_verts[num_other_final_verts++] = other_half_vertices[surface].size(); const Vector3 &skinned_interp_vert = surface_skinned_vertex_positions[base_v[this_vert]].lerp( surface_skinned_vertex_positions[base_v[next_vert]], alpha); MeshEditVert3D edge_vertex; - edge_vertex.index = first_half_section_vertices.size(); + edge_vertex.surface = surface; + edge_vertex.index = first_half_vertices[surface].size(); edge_vertex.position = skinned_interp_vert; if (clipped_edges == 0) { @@ -270,14 +304,14 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original clipped_edges++; assert(clipped_edges <= 2); - first_half_section_vertices.append(interp_vert); - other_half_section_vertices.append(interp_vert); + first_half_vertices[surface].append(interp_vert); + other_half_vertices[surface].append(interp_vert); if (has_normal) { const Vector3 interp_normal = surface_normal_array.ptr()[base_v[this_vert]].slerp(surface_normal_array.ptr()[base_v[next_vert]], alpha); - first_half_section_normals.append(interp_normal); - other_half_section_normals.append(interp_normal); + first_half_normals[surface].append(interp_normal); + other_half_normals[surface].append(interp_normal); } if (has_tangent) @@ -292,36 +326,36 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original const Vector3 &interp_tangent = this_tangent.slerp(next_tangent, alpha); const uint8_t &interp_binormal = UtilityFunctions::roundi(UtilityFunctions::lerpf(this_binormal, next_binormal, alpha)); - first_half_section_tangents.append(interp_tangent.x); - first_half_section_tangents.append(interp_tangent.y); - first_half_section_tangents.append(interp_tangent.z); - first_half_section_tangents.append(interp_binormal); + first_half_tangents[surface].append(interp_tangent.x); + first_half_tangents[surface].append(interp_tangent.y); + first_half_tangents[surface].append(interp_tangent.z); + first_half_tangents[surface].append(interp_binormal); - other_half_section_tangents.append(interp_tangent.x); - other_half_section_tangents.append(interp_tangent.y); - other_half_section_tangents.append(interp_tangent.z); - other_half_section_tangents.append(interp_binormal); + other_half_tangents[surface].append(interp_tangent.x); + other_half_tangents[surface].append(interp_tangent.y); + other_half_tangents[surface].append(interp_tangent.z); + other_half_tangents[surface].append(interp_binormal); } if (has_uv) { const Vector2 &interp_uv = surface_uv_array.ptr()[base_v[this_vert]].lerp(surface_uv_array.ptr()[base_v[next_vert]], alpha); - first_half_section_uvs.append(interp_uv); - other_half_section_uvs.append(interp_uv); + first_half_uvs[surface].append(interp_uv); + other_half_uvs[surface].append(interp_uv); } if (has_uv2) { const Vector2 &interp_uv2 = surface_uv2_array.ptr()[base_v[this_vert]].lerp(surface_uv2_array.ptr()[base_v[next_vert]], alpha); - first_half_section_uv2s.append(interp_uv2); - other_half_section_uv2s.append(interp_uv2); + first_half_uv2s[surface].append(interp_uv2); + other_half_uv2s[surface].append(interp_uv2); } if (has_colour) { const Color &interp_colour = surface_colour_array.ptr()[base_v[this_vert]].lerp(surface_colour_array.ptr()[base_v[next_vert]], alpha); - first_half_section_colours.append(interp_colour); - other_half_section_colours.append(interp_colour); + first_half_colours[surface].append(interp_colour); + other_half_colours[surface].append(interp_colour); } if (has_bone && has_weight) @@ -389,17 +423,17 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original if (vertex_bone < bone_weight_pairs.size()) { const float normalised_weight = bone_weight_pairs[vertex_bone].second / normalisation_value; - first_half_section_bones.append(bone_weight_pairs[vertex_bone].first); - first_half_section_weights.append(normalised_weight); - other_half_section_bones.append(bone_weight_pairs[vertex_bone].first); - other_half_section_weights.append(normalised_weight); + first_half_bones[surface].append(bone_weight_pairs[vertex_bone].first); + first_half_weights[surface].append(normalised_weight); + other_half_bones[surface].append(bone_weight_pairs[vertex_bone].first); + other_half_weights[surface].append(normalised_weight); } else { - first_half_section_bones.append(0); - first_half_section_weights.append(0.0f); - other_half_section_bones.append(0); - other_half_section_weights.append(0.0f); + first_half_bones[surface].append(0); + first_half_weights[surface].append(0.0f); + other_half_bones[surface].append(0); + other_half_weights[surface].append(0.0f); } } } @@ -412,146 +446,148 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original for (uint32_t vertex_index = 2; vertex_index < num_final_verts; vertex_index++) { - first_half_section_indices.append(final_verts[0]); - first_half_section_indices.append(final_verts[vertex_index - 1]); - first_half_section_indices.append(final_verts[vertex_index]); + first_half_indices[surface].append(final_verts[0]); + first_half_indices[surface].append(final_verts[vertex_index - 1]); + first_half_indices[surface].append(final_verts[vertex_index]); } for (uint32_t vertex_index = 2; vertex_index < num_other_final_verts; vertex_index++) { - other_half_section_indices.append(other_final_verts[0]); - other_half_section_indices.append(other_final_verts[vertex_index - 1]); - other_half_section_indices.append(other_final_verts[vertex_index]); + other_half_indices[surface].append(other_final_verts[0]); + other_half_indices[surface].append(other_final_verts[vertex_index - 1]); + other_half_indices[surface].append(other_final_verts[vertex_index]); } } } } - if (first_half_section_vertices.size() > 0 && first_half_section_indices.size() > 0) + if (first_half_vertices[surface].size() > 0 && first_half_indices[surface].size() > 0) { Array first_half_section; first_half_section.resize(ArrayMesh::ARRAY_MAX); - first_half_section[ArrayMesh::ARRAY_VERTEX] = first_half_section_vertices; - if (has_normal) first_half_section[ArrayMesh::ARRAY_NORMAL] = first_half_section_normals; - if (has_tangent) first_half_section[ArrayMesh::ARRAY_TANGENT] = first_half_section_tangents; - if (has_uv) first_half_section[ArrayMesh::ARRAY_TEX_UV] = first_half_section_uvs; - if (has_uv2) first_half_section[ArrayMesh::ARRAY_TEX_UV2] = first_half_section_uv2s; - if (has_colour) first_half_section[ArrayMesh::ARRAY_COLOR] = first_half_section_colours; - if (has_bone) first_half_section[ArrayMesh::ARRAY_BONES] = first_half_section_bones; - if (has_weight) first_half_section[ArrayMesh::ARRAY_WEIGHTS] = first_half_section_weights; - first_half_section[ArrayMesh::ARRAY_INDEX] = first_half_section_indices; + first_half_section[ArrayMesh::ARRAY_VERTEX] = first_half_vertices[surface]; + if (has_normal) first_half_section[ArrayMesh::ARRAY_NORMAL] = first_half_normals[surface]; + if (has_tangent) first_half_section[ArrayMesh::ARRAY_TANGENT] = first_half_tangents[surface]; + if (has_uv) first_half_section[ArrayMesh::ARRAY_TEX_UV] = first_half_uvs[surface]; + if (has_uv2) first_half_section[ArrayMesh::ARRAY_TEX_UV2] = first_half_uv2s[surface]; + if (has_colour) first_half_section[ArrayMesh::ARRAY_COLOR] = first_half_colours[surface]; + if (has_bone) first_half_section[ArrayMesh::ARRAY_BONES] = first_half_bones[surface]; + if (has_weight) first_half_section[ArrayMesh::ARRAY_WEIGHTS] = first_half_weights[surface]; + first_half_section[ArrayMesh::ARRAY_INDEX] = first_half_indices[surface]; out_first_half->add_surface_from_arrays(ArrayMesh::PRIMITIVE_TRIANGLES, first_half_section); out_first_half->surface_set_material(surface, original_mesh->surface_get_material(surface)); } - if (other_half_section_vertices.size() > 0 && other_half_section_indices.size() > 0) + if (other_half_vertices[surface].size() > 0 && other_half_indices[surface].size() > 0) { Array other_half_section; other_half_section.resize(ArrayMesh::ARRAY_MAX); - other_half_section[ArrayMesh::ARRAY_VERTEX] = other_half_section_vertices; - if (has_normal) other_half_section[ArrayMesh::ARRAY_NORMAL] = other_half_section_normals; - if (has_tangent) other_half_section[ArrayMesh::ARRAY_TANGENT] = other_half_section_tangents; - if (has_uv) other_half_section[ArrayMesh::ARRAY_TEX_UV] = other_half_section_uvs; - if (has_uv2) other_half_section[ArrayMesh::ARRAY_TEX_UV2] = other_half_section_uv2s; - if (has_colour) other_half_section[ArrayMesh::ARRAY_COLOR] = other_half_section_colours; - if (has_bone) other_half_section[ArrayMesh::ARRAY_BONES] = other_half_section_bones; - if (has_weight) other_half_section[ArrayMesh::ARRAY_WEIGHTS] = other_half_section_weights; - other_half_section[ArrayMesh::ARRAY_INDEX] = other_half_section_indices; + other_half_section[ArrayMesh::ARRAY_VERTEX] = other_half_vertices[surface]; + if (has_normal) other_half_section[ArrayMesh::ARRAY_NORMAL] = other_half_normals[surface]; + if (has_tangent) other_half_section[ArrayMesh::ARRAY_TANGENT] = other_half_tangents[surface]; + if (has_uv) other_half_section[ArrayMesh::ARRAY_TEX_UV] = other_half_uvs[surface]; + if (has_uv2) other_half_section[ArrayMesh::ARRAY_TEX_UV2] = other_half_uv2s[surface]; + if (has_colour) other_half_section[ArrayMesh::ARRAY_COLOR] = other_half_colours[surface]; + if (has_bone) other_half_section[ArrayMesh::ARRAY_BONES] = other_half_bones[surface]; + if (has_weight) other_half_section[ArrayMesh::ARRAY_WEIGHTS] = other_half_weights[surface]; + other_half_section[ArrayMesh::ARRAY_INDEX] = other_half_indices[surface]; out_other_half->add_surface_from_arrays(ArrayMesh::PRIMITIVE_TRIANGLES, other_half_section); out_other_half->surface_set_material(surface, original_mesh->surface_get_material(surface)); } PRINT_LOG(MESH_EDITING_LIBRARY_TAG, "Surface ", surface, " split edges at ", time->get_ticks_msec(), "ms"); + } + + if (clip_edges.size() > 0) + { + std::vector edges_2d; + std::vector polygon_set; + std::vector error_polygons; + const Transform3D &plane_inverse_transform = MeshEditingLibrary::project_edges(edges_2d, original_mesh_instance->get_transform(), clip_edges, local_plane); + MeshEditingLibrary::build_2d_polygons_from_edges(polygon_set, edges_2d, error_polygons); - if (clip_edges.size() > 0) + MeshEditingLibrary::draw_debug_error_polygons(error_polygons, plane_inverse_transform); + + MeshSlicePlaneOrientation uv_plane = MeshSlicePlaneOrientation::Z; + const Basis &mesh_instance_basis = original_mesh_instance->get_basis(); + const Vector3 &local_plane_normal = local_plane.get_normal(); + if (UtilityFunctions::absf(local_plane_normal.dot(mesh_instance_basis.xform(Vector3(0.0f, 1.0f, 0.0f)))) > 0.5f) { - std::vector edges_2d; - std::vector polygon_set; - std::vector error_polygons; - MeshEditingLibrary::project_edges(edges_2d, original_mesh_instance->get_transform(), clip_edges, local_plane); - MeshEditingLibrary::build_2d_polygons_from_edges(polygon_set, edges_2d, error_polygons); - - MeshSlicePlaneOrientation uv_plane = MeshSlicePlaneOrientation::Z; - const Basis &mesh_instance_basis = original_mesh_instance->get_basis(); - const Vector3 &local_plane_normal = local_plane.get_normal(); - if (UtilityFunctions::absf(local_plane_normal.dot(mesh_instance_basis.xform(Vector3(0.0f, 1.0f, 0.0f)))) > 0.5f) - { - uv_plane = MeshSlicePlaneOrientation::Y; - } - else if (UtilityFunctions::absf(local_plane_normal.dot(mesh_instance_basis.xform(Vector3(1.0f, 0.0f, 0.0f)))) > 0.5f) - { - uv_plane = MeshSlicePlaneOrientation::X; - } - - const Vector3 &mesh_bounds = original_mesh->get_aabb().get_size(); - const uint32_t num_polygons = polygon_set.size(); - for (uint32_t polygon_index = 0; polygon_index < num_polygons; polygon_index++) - { - using Point = std::array; - using Polygon = std::vector>; - - Polygon earcut_polygon; - std::vector sub_polygon; - - Vector3 polygon_centroid; - - const uint32_t polygon_vertex_base = cap_section_vertices.size(); - for (const MeshEditVert2D &vertex : polygon_set[polygon_index].vertices) - { - const Vector3 &position = first_half_section_vertices[vertex.index]; - const Vector3 &local_plane_transformed_normal = skinned_vertex_transforms[surface][vertex.index].xform(local_plane_normal).normalized(); - - cap_section_vertices.append(position); - if (has_normal) { cap_section_normals.append(local_plane_transformed_normal); cap_section_flipped_normals.append(local_plane_transformed_normal * -1.0f); } - if (has_colour) { cap_section_colours.append(first_half_section_colours[vertex.index]); } - if (has_uv) { cap_section_uvs.append(MeshEditingLibrary::calculate_planar_uv(position, mesh_bounds, uv_plane)); } - if (has_uv2) { cap_section_uv2s.append(first_half_section_uv2s[vertex.index]); } - if (has_tangent) - { - for (uint8_t i = 0; i < 4; i++) - { - cap_section_tangents.append(first_half_section_tangents[(vertex.index * 4) + i]); - cap_section_flipped_tangents.append(first_half_section_tangents[(vertex.index * 4) + i] * -1.0f); - } - } - if (has_bone) - { - for (uint8_t i = 0; i < num_bones_per_vertex; i++) - { - cap_section_bones.append(first_half_section_bones[(vertex.index * num_bones_per_vertex) + i]); - } - } - if (has_weight) - { - for (uint8_t i = 0; i < num_bones_per_vertex; i++) - { - cap_section_weights.append(first_half_section_weights[(vertex.index * num_bones_per_vertex) + i]); - } - } - - sub_polygon.push_back({vertex.position.x, vertex.position.y}); - polygon_centroid += position; - } - - polygon_centroid /= polygon_set[polygon_index].vertices.size(); - impact_points.append(polygon_centroid); - - earcut_polygon.push_back(sub_polygon); - std::vector indices = mapbox::earcut(earcut_polygon); - for (uint32_t i = 0; (i+2) < indices.size(); i += 3) - { - cap_section_indices.append(indices[i+1] + polygon_vertex_base); - cap_section_indices.append(indices[i] + polygon_vertex_base); - cap_section_indices.append(indices[i+2] + polygon_vertex_base); - } - } + uv_plane = MeshSlicePlaneOrientation::Y; + } + else if (UtilityFunctions::absf(local_plane_normal.dot(mesh_instance_basis.xform(Vector3(1.0f, 0.0f, 0.0f)))) > 0.5f) + { + uv_plane = MeshSlicePlaneOrientation::X; } - PRINT_LOG(MESH_EDITING_LIBRARY_TAG, "Surface ", surface, " created cap surface at ", time->get_ticks_msec(), "ms"); + const Vector3 &mesh_bounds = original_mesh->get_aabb().get_size(); + const uint32_t num_polygons = polygon_set.size(); + for (uint32_t polygon_index = 0; polygon_index < num_polygons; polygon_index++) + { + using Point = std::array; + using Polygon = std::vector>; + + Polygon earcut_polygon; + std::vector sub_polygon; + + Vector3 polygon_centroid; + + const uint32_t polygon_vertex_base = cap_section_vertices.size(); + for (const MeshEditVert2D &vertex : polygon_set[polygon_index].vertices) + { + const Vector3 &position = first_half_vertices[vertex.surface][vertex.index]; + const Vector3 &local_plane_transformed_normal = skinned_vertex_transforms[vertex.surface][vertex.index].xform(local_plane_normal).normalized(); + + cap_section_vertices.append(position); + if (surface_has_normal[vertex.surface]) { cap_section_normals.append(local_plane_transformed_normal); cap_section_flipped_normals.append(local_plane_transformed_normal * -1.0f); } + if (surface_has_colour[vertex.surface]) { cap_section_colours.append(first_half_colours[vertex.surface][vertex.index]); } + if (surface_has_uv[vertex.surface]) { cap_section_uvs.append(MeshEditingLibrary::calculate_planar_uv(position, mesh_bounds, uv_plane)); } + if (surface_has_uv2[vertex.surface]) { cap_section_uv2s.append(first_half_uv2s[vertex.surface][vertex.index]); } + if (surface_has_tangent[vertex.surface]) + { + for (uint8_t i = 0; i < 4; i++) + { + cap_section_tangents.append(first_half_tangents[vertex.surface][(vertex.index * 4) + i]); + cap_section_flipped_tangents.append(first_half_tangents[vertex.surface][(vertex.index * 4) + i] * -1.0f); + } + } + if (surface_has_bone[vertex.surface]) + { + for (uint8_t i = 0; i < surface_num_bones_per_vertex[vertex.surface]; i++) + { + cap_section_bones.append(first_half_bones[vertex.surface][(vertex.index * surface_num_bones_per_vertex[vertex.surface]) + i]); + } + } + if (surface_has_weight[vertex.surface]) + { + for (uint8_t i = 0; i < surface_num_bones_per_vertex[vertex.surface]; i++) + { + cap_section_weights.append(first_half_weights[vertex.surface][(vertex.index * surface_num_bones_per_vertex[vertex.surface]) + i]); + } + } + + sub_polygon.push_back({vertex.position.x, vertex.position.y}); + polygon_centroid += position; + } + + polygon_centroid /= polygon_set[polygon_index].vertices.size(); + impact_points.append(polygon_centroid); + + earcut_polygon.push_back(sub_polygon); + std::vector indices = mapbox::earcut(earcut_polygon); + for (uint32_t i = 0; (i+2) < indices.size(); i += 3) + { + cap_section_indices.append(indices[i+1] + polygon_vertex_base); + cap_section_indices.append(indices[i] + polygon_vertex_base); + cap_section_indices.append(indices[i+2] + polygon_vertex_base); + } + } } + PRINT_LOG(MESH_EDITING_LIBRARY_TAG, "Created cap surfaces at ", time->get_ticks_msec(), "ms"); + if (cap_section_vertices.size() > 0 && cap_section_indices.size() > 0) { Array cap_mesh_section; @@ -695,7 +731,7 @@ SkinnedSurfaces MeshEditingLibrary::get_skinned_vertex_positions(const Mesh *mes } -void MeshEditingLibrary::project_edges(std::vector &out_2d_edges, const Transform3D &to_node_space, const std::vector &in_3d_edges, const Plane &plane) +const Transform3D MeshEditingLibrary::project_edges(std::vector &out_2d_edges, const Transform3D &to_node_space, const std::vector &in_3d_edges, const Plane &plane) { out_2d_edges.resize(in_3d_edges.size()); @@ -718,6 +754,8 @@ void MeshEditingLibrary::project_edges(std::vector &out_2d_edges out_2d_edges[i].v0 = v0; out_2d_edges[i].v1 = v1; } + + return plane_inverse_transform; } void MeshEditingLibrary::build_2d_polygons_from_edges(std::vector &out_polygons, const std::vector &in_edges, std::vector &error_polygons) @@ -844,3 +882,32 @@ const Vector2 MeshEditingLibrary::calculate_planar_uv(const Vector3 &vertex, con return Vector2(vertex.x / mesh_bounds.x, vertex.y / mesh_bounds.y); } } + + +void MeshEditingLibrary::draw_debug_error_polygons(const std::vector &error_polygons, const Transform3D &plane_transform) +{ +#ifdef DEBUG_ENABLED + Ref material; + material.instantiate(); + material->set_flag(BaseMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true); + material->set_shading_mode(BaseMaterial3D::SHADING_MODE_UNSHADED); + + ImmediateMesh *im = memnew(ImmediateMesh); + im->surface_begin(Mesh::PRIMITIVE_LINE_STRIP, material); + const uint32_t num_polygons = error_polygons.size(); + for (uint32_t i = 0; i < num_polygons; i++) + { + const MeshEditPolygon2D &polygon = error_polygons[i]; + const uint32_t num_points = error_polygons[i].vertices.size(); + for (uint32_t j = 0; j < num_points; j++) + { + const MeshEditVert2D &vertex = polygon.vertices[j]; + const Vector3 &transformed_vertex = plane_transform.inverse().xform(Vector3(vertex.position.x, vertex.position.y, 0.0f)); + + im->surface_set_color(Color(1.0f, 0.0f, 0.0f, 1.0f)); + im->surface_add_vertex(transformed_vertex); + } + } + im->surface_end(); +#endif // DEBUG_ENABLED +} diff --git a/src/liborng/nodes/mesh_editing_library.h b/src/liborng/nodes/mesh_editing_library.h index 270628c..1a3aab3 100644 --- a/src/liborng/nodes/mesh_editing_library.h +++ b/src/liborng/nodes/mesh_editing_library.h @@ -25,12 +25,14 @@ typedef std::vector SkinnedSurfaces; struct MeshEditVert3D { + uint8_t surface; // Surface index of the original vertex array uint32_t index; // Index into the original vertex array Vector3 position; // Position used for generating geometry }; struct MeshEditVert2D { + uint8_t surface; // Surface index of the original vertex array uint32_t index; // Index into the original vertex array Vector2 position; // Position used for generating geometry }; @@ -83,11 +85,13 @@ private: static SkinnedSurfaces get_skinned_vertex_positions(const Mesh *mesh, const Skeleton3D *skeleton = nullptr, std::vector> &bone_transforms = std::vector>()); - static void project_edges(std::vector &out_2d_edges, const Transform3D &to_node_space, const std::vector &in_3d_edges, const Plane &plane); + static const Transform3D project_edges(std::vector &out_2d_edges, const Transform3D &to_node_space, const std::vector &in_3d_edges, const Plane &plane); static void build_2d_polygons_from_edges(std::vector &out_polygons, const std::vector &in_edges, std::vector &error_polygons); static bool find_next_edge(MeshEditEdge2D &out_next_edge, std::vector &in_edge_set, const MeshEditVert2D &start); static void fix_polygon_winding(MeshEditPolygon2D &polygon); + static void draw_debug_error_polygons(const std::vector &error_polygons, const Transform3D &plane_transform); + // Godot boilerplate below protected: static void _bind_methods()