Mesh slicing now works properly when a cut crosses through multiple surfaces.

This commit is contained in:
2026-09-12 23:43:59 -04:00
parent 004c845af7
commit e08082d6e1
2 changed files with 277 additions and 205 deletions
+207 -139
View File
@@ -11,6 +11,7 @@
#include <map> #include <map>
#include "godot_cpp/classes/immediate_mesh.hpp"
#include "godot_cpp/classes/time.hpp" #include "godot_cpp/classes/time.hpp"
#include "godot_cpp/variant/utility_functions.hpp" #include "godot_cpp/variant/utility_functions.hpp"
using namespace godot; using namespace godot;
@@ -24,7 +25,7 @@ MeshEditingLibrary::~MeshEditingLibrary()
{ {
} }
PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original_mesh_instance, const Skeleton3D *original_skeleton, const Plane &local_plane, Ref<ArrayMesh> out_first_half, Ref<ArrayMesh> out_other_half, const MeshEditCapUV cap_option, const Ref<StandardMaterial3D> cap_material) PackedVector3Array MeshEditingLibrary::slice_mesh(MeshInstance3D *original_mesh_instance, const Skeleton3D *original_skeleton, const Plane &local_plane, Ref<ArrayMesh> out_first_half, Ref<ArrayMesh> out_other_half, const MeshEditCapUV cap_option, const Ref<StandardMaterial3D> cap_material)
{ {
const Time *time = Time::get_singleton(); const Time *time = Time::get_singleton();
@@ -40,6 +41,38 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original
PackedVector3Array impact_points; PackedVector3Array impact_points;
std::vector<bool> surface_has_normal; surface_has_normal.resize(num_surfaces);
std::vector<bool> surface_has_tangent; surface_has_tangent.resize(num_surfaces);
std::vector<bool> surface_has_uv; surface_has_uv.resize(num_surfaces);
std::vector<bool> surface_has_uv2; surface_has_uv2.resize(num_surfaces);
std::vector<bool> surface_has_colour; surface_has_colour.resize(num_surfaces);
std::vector<bool> surface_has_bone; surface_has_bone.resize(num_surfaces);
std::vector<bool> surface_has_weight; surface_has_weight.resize(num_surfaces);
std::vector<uint32_t> surface_bone_array_size; surface_bone_array_size.resize(num_surfaces);
std::vector<uint8_t> surface_num_bones_per_vertex; surface_num_bones_per_vertex.resize(num_surfaces);
std::vector<PackedVector3Array> first_half_vertices; first_half_vertices.resize(num_surfaces);
std::vector<PackedVector3Array> first_half_normals; first_half_normals.resize(num_surfaces);
std::vector<PackedFloat32Array> first_half_tangents; first_half_tangents.resize(num_surfaces);
std::vector<PackedVector2Array> first_half_uvs; first_half_uvs.resize(num_surfaces);
std::vector<PackedVector2Array> first_half_uv2s; first_half_uv2s.resize(num_surfaces);
std::vector<PackedColorArray> first_half_colours; first_half_colours.resize(num_surfaces);
std::vector<PackedInt32Array> first_half_bones; first_half_bones.resize(num_surfaces);
std::vector<PackedFloat32Array> first_half_weights; first_half_weights.resize(num_surfaces);
std::vector<PackedVector3Array> other_half_vertices; other_half_vertices.resize(num_surfaces);
std::vector<PackedVector3Array> other_half_normals; other_half_normals.resize(num_surfaces);
std::vector<PackedFloat32Array> other_half_tangents; other_half_tangents.resize(num_surfaces);
std::vector<PackedVector2Array> other_half_uvs; other_half_uvs.resize(num_surfaces);
std::vector<PackedVector2Array> other_half_uv2s; other_half_uv2s.resize(num_surfaces);
std::vector<PackedColorArray> other_half_colours; other_half_colours.resize(num_surfaces);
std::vector<PackedInt32Array> other_half_bones; other_half_bones.resize(num_surfaces);
std::vector<PackedFloat32Array> other_half_weights; other_half_weights.resize(num_surfaces);
std::vector<PackedInt32Array> first_half_indices; first_half_indices.resize(num_surfaces);
std::vector<PackedInt32Array> other_half_indices; other_half_indices.resize(num_surfaces);
PackedVector3Array cap_section_vertices; PackedVector3Array cap_section_vertices;
PackedVector3Array cap_section_normals; PackedVector3Array cap_section_normals;
PackedVector3Array cap_section_flipped_normals; PackedVector3Array cap_section_flipped_normals;
@@ -52,6 +85,8 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original
PackedFloat32Array cap_section_weights; PackedFloat32Array cap_section_weights;
PackedInt32Array cap_section_indices; PackedInt32Array cap_section_indices;
std::vector<MeshEditEdge3D> clip_edges;
for (uint8_t surface = 0; surface < num_surfaces; surface++) for (uint8_t surface = 0; surface < num_surfaces; surface++)
{ {
const SkinnedVertices &surface_skinned_vertex_positions = skinned_vertex_positions[surface]; const SkinnedVertices &surface_skinned_vertex_positions = skinned_vertex_positions[surface];
@@ -75,36 +110,19 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original
std::map<uint32_t, uint32_t> base_to_sliced_vert_index; std::map<uint32_t, uint32_t> base_to_sliced_vert_index;
std::map<uint32_t, uint32_t> base_to_other_sliced_vert_index; std::map<uint32_t, uint32_t> base_to_other_sliced_vert_index;
const bool has_normal = surface_normal_array.size() >= num_vertices; const bool has_normal = surface_normal_array.size() >= num_vertices; surface_has_normal[surface] = has_normal;
const bool has_tangent = surface_tangent_array.size() >= num_vertices * 4; const bool has_tangent = surface_tangent_array.size() >= num_vertices * 4; surface_has_tangent[surface] = has_tangent;
const bool has_uv = surface_uv_array.size() >= num_vertices; const bool has_uv = surface_uv_array.size() >= num_vertices; surface_has_uv[surface] = has_uv;
const bool has_uv2 = surface_uv2_array.size() >= num_vertices; const bool has_uv2 = surface_uv2_array.size() >= num_vertices; surface_has_uv2[surface] = has_uv2;
const bool has_colour = surface_colour_array.size() >= num_vertices; const bool has_colour = surface_colour_array.size() >= num_vertices; surface_has_colour[surface] = has_colour;
const bool has_bone = surface_bone_array.size() >= num_vertices * 4; const bool has_bone = surface_bone_array.size() >= num_vertices * 4; surface_has_bone[surface] = has_bone;
const bool has_weight = surface_weight_array.size() >= num_vertices * 4; const bool has_weight = surface_weight_array.size() >= num_vertices * 4; surface_has_weight[surface] = has_weight;
const uint32_t bone_array_size = surface_bone_array.size(); const uint32_t bone_array_size = surface_bone_array.size();
const uint8_t num_bones_per_vertex = bone_array_size / num_vertices; surface_bone_array_size[surface] = bone_array_size;
PackedVector3Array first_half_section_vertices; const uint8_t num_bones_per_vertex = surface_bone_array_size[surface] / num_vertices;
PackedVector3Array first_half_section_normals; surface_num_bones_per_vertex[surface] = num_bones_per_vertex;
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;
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<MeshEditEdge3D> clip_edges;
for (uint32_t vertex = 0; vertex < num_vertices; vertex++) for (uint32_t vertex = 0; vertex < num_vertices; vertex++)
{ {
@@ -112,71 +130,69 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original
if (vertex_distance[vertex] >= 0.0f) 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]); first_half_vertices[surface].append(surface_vertex_array_ptr[vertex]);
if (has_normal) { first_half_section_normals.append(surface_normal_array.ptr()[vertex]); } if (has_normal) { first_half_normals[surface].append(surface_normal_array.ptr()[vertex]); }
if (has_uv) { first_half_section_uvs.append(surface_uv_array.ptr()[vertex]); } if (has_uv) { first_half_uvs[surface].append(surface_uv_array.ptr()[vertex]); }
if (has_uv2) { first_half_section_uv2s.append(surface_uv2_array.ptr()[vertex]); } if (has_uv2) { first_half_uv2s[surface].append(surface_uv2_array.ptr()[vertex]); }
if (has_colour) { first_half_section_colours.append(surface_colour_array.ptr()[vertex]); } if (has_colour) { first_half_colours[surface].append(surface_colour_array.ptr()[vertex]); }
if (has_tangent) if (has_tangent)
{ {
first_half_section_tangents.append(surface_tangent_array.ptr()[vertex * 4]); first_half_tangents[surface].append(surface_tangent_array.ptr()[vertex * 4]);
first_half_section_tangents.append(surface_tangent_array.ptr()[(vertex * 4) + 1]); first_half_tangents[surface].append(surface_tangent_array.ptr()[(vertex * 4) + 1]);
first_half_section_tangents.append(surface_tangent_array.ptr()[(vertex * 4) + 2]); first_half_tangents[surface].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) + 3]);
} }
if (has_bone) if (has_bone)
{ {
for (uint8_t i = 0; i < num_bones_per_vertex; i++) 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_bones[surface].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_weights[surface].append(surface_weight_array.ptr()[(vertex * num_bones_per_vertex) + i]);
} }
} }
} }
else 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]); other_half_vertices[surface].append(surface_vertex_array_ptr[vertex]);
if (has_normal) { other_half_section_normals.append(surface_normal_array.ptr()[vertex]); } if (has_normal) { other_half_normals[surface].append(surface_normal_array.ptr()[vertex]); }
if (has_uv) { other_half_section_uvs.append(surface_uv_array.ptr()[vertex]); } if (has_uv) { other_half_uvs[surface].append(surface_uv_array.ptr()[vertex]); }
if (has_uv2) { other_half_section_uv2s.append(surface_uv2_array.ptr()[vertex]); } if (has_uv2) { other_half_uv2s[surface].append(surface_uv2_array.ptr()[vertex]); }
if (has_colour) { other_half_section_colours.append(surface_colour_array.ptr()[vertex]); } if (has_colour) { other_half_colours[surface].append(surface_colour_array.ptr()[vertex]); }
if (has_tangent) if (has_tangent)
{ {
other_half_section_tangents.append(surface_tangent_array.ptr()[vertex * 4]); other_half_tangents[surface].append(surface_tangent_array.ptr()[vertex * 4]);
other_half_section_tangents.append(surface_tangent_array.ptr()[(vertex * 4) + 1]); other_half_tangents[surface].append(surface_tangent_array.ptr()[(vertex * 4) + 1]);
other_half_section_tangents.append(surface_tangent_array.ptr()[(vertex * 4) + 2]); other_half_tangents[surface].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) + 3]);
} }
if (has_bone) if (has_bone)
{ {
for (uint8_t i = 0; i < num_bones_per_vertex; i++) 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_bones[surface].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_weights[surface].append(surface_weight_array.ptr()[(vertex * num_bones_per_vertex) + i]);
} }
} }
} }
} }
PackedInt32Array first_half_section_indices;
PackedInt32Array other_half_section_indices;
const PackedInt32Array &surface_index_array = surface_arrays[ArrayMesh::ARRAY_INDEX]; 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"); 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 else
@@ -202,15 +218,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 (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. { // 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_indices[surface].append(sliced_v[0]->second);
first_half_section_indices.append(sliced_v[1]->second); first_half_indices[surface].append(sliced_v[1]->second);
first_half_section_indices.append(sliced_v[2]->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) 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. { // 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_indices[surface].append(sliced_other_v[0]->second);
other_half_section_indices.append(sliced_other_v[1]->second); other_half_indices[surface].append(sliced_other_v[1]->second);
other_half_section_indices.append(sliced_other_v[2]->second); other_half_indices[surface].append(sliced_other_v[2]->second);
} }
else else
{ // If the triangle is split by the slice plane, then slice the overlapping edges. { // If the triangle is split by the slice plane, then slice the overlapping edges.
@@ -250,14 +266,15 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original
const Vector3 &interp_vert = surface_vertex_array[base_v[this_vert]].lerp( const Vector3 &interp_vert = surface_vertex_array[base_v[this_vert]].lerp(
surface_vertex_array[base_v[next_vert]], alpha); surface_vertex_array[base_v[next_vert]], alpha);
final_verts[num_final_verts++] = first_half_section_vertices.size(); final_verts[num_final_verts++] = first_half_vertices[surface].size();
other_final_verts[num_other_final_verts++] = other_half_section_vertices.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( const Vector3 &skinned_interp_vert = surface_skinned_vertex_positions[base_v[this_vert]].lerp(
surface_skinned_vertex_positions[base_v[next_vert]], alpha); surface_skinned_vertex_positions[base_v[next_vert]], alpha);
MeshEditVert3D edge_vertex; 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; edge_vertex.position = skinned_interp_vert;
if (clipped_edges == 0) if (clipped_edges == 0)
{ {
@@ -270,14 +287,14 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original
clipped_edges++; clipped_edges++;
assert(clipped_edges <= 2); assert(clipped_edges <= 2);
first_half_section_vertices.append(interp_vert); first_half_vertices[surface].append(interp_vert);
other_half_section_vertices.append(interp_vert); other_half_vertices[surface].append(interp_vert);
if (has_normal) 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); 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); first_half_normals[surface].append(interp_normal);
other_half_section_normals.append(interp_normal); other_half_normals[surface].append(interp_normal);
} }
if (has_tangent) if (has_tangent)
@@ -292,36 +309,36 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original
const Vector3 &interp_tangent = this_tangent.slerp(next_tangent, alpha); const Vector3 &interp_tangent = this_tangent.slerp(next_tangent, alpha);
const uint8_t &interp_binormal = UtilityFunctions::roundi(UtilityFunctions::lerpf(this_binormal, next_binormal, 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_tangents[surface].append(interp_tangent.x);
first_half_section_tangents.append(interp_tangent.y); first_half_tangents[surface].append(interp_tangent.y);
first_half_section_tangents.append(interp_tangent.z); first_half_tangents[surface].append(interp_tangent.z);
first_half_section_tangents.append(interp_binormal); first_half_tangents[surface].append(interp_binormal);
other_half_section_tangents.append(interp_tangent.x); other_half_tangents[surface].append(interp_tangent.x);
other_half_section_tangents.append(interp_tangent.y); other_half_tangents[surface].append(interp_tangent.y);
other_half_section_tangents.append(interp_tangent.z); other_half_tangents[surface].append(interp_tangent.z);
other_half_section_tangents.append(interp_binormal); other_half_tangents[surface].append(interp_binormal);
} }
if (has_uv) 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); 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); first_half_uvs[surface].append(interp_uv);
other_half_section_uvs.append(interp_uv); other_half_uvs[surface].append(interp_uv);
} }
if (has_uv2) 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); 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); first_half_uv2s[surface].append(interp_uv2);
other_half_section_uv2s.append(interp_uv2); other_half_uv2s[surface].append(interp_uv2);
} }
if (has_colour) 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); 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); first_half_colours[surface].append(interp_colour);
other_half_section_colours.append(interp_colour); other_half_colours[surface].append(interp_colour);
} }
if (has_bone && has_weight) if (has_bone && has_weight)
@@ -389,17 +406,17 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original
if (vertex_bone < bone_weight_pairs.size()) if (vertex_bone < bone_weight_pairs.size())
{ {
const float normalised_weight = bone_weight_pairs[vertex_bone].second / normalisation_value; 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_bones[surface].append(bone_weight_pairs[vertex_bone].first);
first_half_section_weights.append(normalised_weight); first_half_weights[surface].append(normalised_weight);
other_half_section_bones.append(bone_weight_pairs[vertex_bone].first); other_half_bones[surface].append(bone_weight_pairs[vertex_bone].first);
other_half_section_weights.append(normalised_weight); other_half_weights[surface].append(normalised_weight);
} }
else else
{ {
first_half_section_bones.append(0); first_half_bones[surface].append(0);
first_half_section_weights.append(0.0f); first_half_weights[surface].append(0.0f);
other_half_section_bones.append(0); other_half_bones[surface].append(0);
other_half_section_weights.append(0.0f); other_half_weights[surface].append(0.0f);
} }
} }
} }
@@ -412,67 +429,74 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original
for (uint32_t vertex_index = 2; vertex_index < num_final_verts; vertex_index++) for (uint32_t vertex_index = 2; vertex_index < num_final_verts; vertex_index++)
{ {
first_half_section_indices.append(final_verts[0]); first_half_indices[surface].append(final_verts[0]);
first_half_section_indices.append(final_verts[vertex_index - 1]); first_half_indices[surface].append(final_verts[vertex_index - 1]);
first_half_section_indices.append(final_verts[vertex_index]); first_half_indices[surface].append(final_verts[vertex_index]);
} }
for (uint32_t vertex_index = 2; vertex_index < num_other_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_indices[surface].append(other_final_verts[0]);
other_half_section_indices.append(other_final_verts[vertex_index - 1]); other_half_indices[surface].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[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; Array first_half_section;
first_half_section.resize(ArrayMesh::ARRAY_MAX); first_half_section.resize(ArrayMesh::ARRAY_MAX);
first_half_section[ArrayMesh::ARRAY_VERTEX] = first_half_section_vertices; first_half_section[ArrayMesh::ARRAY_VERTEX] = first_half_vertices[surface];
if (has_normal) first_half_section[ArrayMesh::ARRAY_NORMAL] = first_half_section_normals; if (has_normal) first_half_section[ArrayMesh::ARRAY_NORMAL] = first_half_normals[surface];
if (has_tangent) first_half_section[ArrayMesh::ARRAY_TANGENT] = first_half_section_tangents; 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_section_uvs; 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_section_uv2s; 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_section_colours; if (has_colour) first_half_section[ArrayMesh::ARRAY_COLOR] = first_half_colours[surface];
if (has_bone) first_half_section[ArrayMesh::ARRAY_BONES] = first_half_section_bones; if (has_bone) first_half_section[ArrayMesh::ARRAY_BONES] = first_half_bones[surface];
if (has_weight) first_half_section[ArrayMesh::ARRAY_WEIGHTS] = first_half_section_weights; if (has_weight) first_half_section[ArrayMesh::ARRAY_WEIGHTS] = first_half_weights[surface];
first_half_section[ArrayMesh::ARRAY_INDEX] = first_half_section_indices; 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->add_surface_from_arrays(ArrayMesh::PRIMITIVE_TRIANGLES, first_half_section);
out_first_half->surface_set_material(surface, original_mesh->surface_get_material(surface)); 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; Array other_half_section;
other_half_section.resize(ArrayMesh::ARRAY_MAX); other_half_section.resize(ArrayMesh::ARRAY_MAX);
other_half_section[ArrayMesh::ARRAY_VERTEX] = other_half_section_vertices; other_half_section[ArrayMesh::ARRAY_VERTEX] = other_half_vertices[surface];
if (has_normal) other_half_section[ArrayMesh::ARRAY_NORMAL] = other_half_section_normals; if (has_normal) other_half_section[ArrayMesh::ARRAY_NORMAL] = other_half_normals[surface];
if (has_tangent) other_half_section[ArrayMesh::ARRAY_TANGENT] = other_half_section_tangents; 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_section_uvs; 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_section_uv2s; 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_section_colours; if (has_colour) other_half_section[ArrayMesh::ARRAY_COLOR] = other_half_colours[surface];
if (has_bone) other_half_section[ArrayMesh::ARRAY_BONES] = other_half_section_bones; if (has_bone) other_half_section[ArrayMesh::ARRAY_BONES] = other_half_bones[surface];
if (has_weight) other_half_section[ArrayMesh::ARRAY_WEIGHTS] = other_half_section_weights; if (has_weight) other_half_section[ArrayMesh::ARRAY_WEIGHTS] = other_half_weights[surface];
other_half_section[ArrayMesh::ARRAY_INDEX] = other_half_section_indices; 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->add_surface_from_arrays(ArrayMesh::PRIMITIVE_TRIANGLES, other_half_section);
out_other_half->surface_set_material(surface, original_mesh->surface_get_material(surface)); 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"); PRINT_LOG(MESH_EDITING_LIBRARY_TAG, "Surface ", surface, " split edges at ", time->get_ticks_msec(), "ms");
}
if (clip_edges.size() > 0) if (clip_edges.size() > 0)
{ {
std::vector<MeshEditEdge2D> edges_2d; std::vector<MeshEditEdge2D> edges_2d;
std::vector<MeshEditPolygon2D> polygon_set; std::vector<MeshEditPolygon2D> polygon_set;
std::vector<MeshEditPolygon2D> error_polygons; std::vector<MeshEditPolygon2D> error_polygons;
MeshEditingLibrary::project_edges(edges_2d, original_mesh_instance->get_transform(), clip_edges, local_plane); 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); MeshEditingLibrary::build_2d_polygons_from_edges(polygon_set, edges_2d, error_polygons);
Ref<Mesh> debug_mesh = MeshEditingLibrary::draw_debug_polygons(polygon_set, plane_inverse_transform, Color(0.0, 1.0, 0.0, 1.0));
MeshInstance3D *mi = memnew(MeshInstance3D);
original_mesh_instance->add_child(mi);
mi->set_transform(Transform3D());
mi->set_mesh(debug_mesh);
MeshSlicePlaneOrientation uv_plane = MeshSlicePlaneOrientation::Z; MeshSlicePlaneOrientation uv_plane = MeshSlicePlaneOrientation::Z;
const Basis &mesh_instance_basis = original_mesh_instance->get_basis(); const Basis &mesh_instance_basis = original_mesh_instance->get_basis();
const Vector3 &local_plane_normal = local_plane.get_normal(); const Vector3 &local_plane_normal = local_plane.get_normal();
@@ -500,34 +524,38 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original
const uint32_t polygon_vertex_base = cap_section_vertices.size(); const uint32_t polygon_vertex_base = cap_section_vertices.size();
for (const MeshEditVert2D &vertex : polygon_set[polygon_index].vertices) for (const MeshEditVert2D &vertex : polygon_set[polygon_index].vertices)
{ {
const Vector3 &position = first_half_section_vertices[vertex.index]; const Vector3 &position = first_half_vertices[vertex.surface][vertex.index];
const Vector3 &local_plane_transformed_normal = skinned_vertex_transforms[surface][vertex.index].xform(local_plane_normal).normalized(); const Vector3 &local_plane_transformed_normal = skinned_vertex_transforms[vertex.surface][vertex.index].xform(local_plane_normal).normalized();
cap_section_vertices.append(position); 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 (surface_has_normal[vertex.surface])
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)); } cap_section_normals.append(local_plane_transformed_normal);
if (has_uv2) { cap_section_uv2s.append(first_half_section_uv2s[vertex.index]); } cap_section_flipped_normals.append(local_plane_transformed_normal * -1.0f);
if (has_tangent) }
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++) for (uint8_t i = 0; i < 4; i++)
{ {
cap_section_tangents.append(first_half_section_tangents[(vertex.index * 4) + i]); cap_section_tangents.append(first_half_tangents[vertex.surface][(vertex.index * 4) + i]);
cap_section_flipped_tangents.append(first_half_section_tangents[(vertex.index * 4) + i] * -1.0f); cap_section_flipped_tangents.append(first_half_tangents[vertex.surface][(vertex.index * 4) + i] * -1.0f);
} }
} }
if (has_bone) if (surface_has_bone[vertex.surface])
{ {
for (uint8_t i = 0; i < num_bones_per_vertex; i++) for (uint8_t i = 0; i < surface_num_bones_per_vertex[vertex.surface]; i++)
{ {
cap_section_bones.append(first_half_section_bones[(vertex.index * num_bones_per_vertex) + i]); cap_section_bones.append(first_half_bones[vertex.surface][(vertex.index * surface_num_bones_per_vertex[vertex.surface]) + i]);
} }
} }
if (has_weight) if (surface_has_weight[vertex.surface])
{ {
for (uint8_t i = 0; i < num_bones_per_vertex; i++) for (uint8_t i = 0; i < surface_num_bones_per_vertex[vertex.surface]; i++)
{ {
cap_section_weights.append(first_half_section_weights[(vertex.index * num_bones_per_vertex) + i]); cap_section_weights.append(first_half_weights[vertex.surface][(vertex.index * surface_num_bones_per_vertex[vertex.surface]) + i]);
} }
} }
@@ -549,8 +577,7 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original
} }
} }
PRINT_LOG(MESH_EDITING_LIBRARY_TAG, "Surface ", surface, " created cap surface at ", time->get_ticks_msec(), "ms"); 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) if (cap_section_vertices.size() > 0 && cap_section_indices.size() > 0)
{ {
@@ -695,7 +722,7 @@ SkinnedSurfaces MeshEditingLibrary::get_skinned_vertex_positions(const Mesh *mes
} }
void MeshEditingLibrary::project_edges(std::vector<MeshEditEdge2D> &out_2d_edges, const Transform3D &to_node_space, const std::vector<MeshEditEdge3D> &in_3d_edges, const Plane &plane) const Transform3D MeshEditingLibrary::project_edges(std::vector<MeshEditEdge2D> &out_2d_edges, const Transform3D &to_node_space, const std::vector<MeshEditEdge3D> &in_3d_edges, const Plane &plane)
{ {
out_2d_edges.resize(in_3d_edges.size()); out_2d_edges.resize(in_3d_edges.size());
@@ -705,6 +732,7 @@ void MeshEditingLibrary::project_edges(std::vector<MeshEditEdge2D> &out_2d_edges
{ {
MeshEditVert2D v0; MeshEditVert2D v0;
Vector3 p = plane_inverse_transform.xform(in_3d_edges[i].v0.position); Vector3 p = plane_inverse_transform.xform(in_3d_edges[i].v0.position);
v0.surface = in_3d_edges[i].v0.surface;
v0.index = in_3d_edges[i].v0.index; v0.index = in_3d_edges[i].v0.index;
v0.position.x = p.x; v0.position.x = p.x;
v0.position.y = p.y; v0.position.y = p.y;
@@ -712,12 +740,15 @@ void MeshEditingLibrary::project_edges(std::vector<MeshEditEdge2D> &out_2d_edges
MeshEditVert2D v1; MeshEditVert2D v1;
p = plane_inverse_transform.xform(in_3d_edges[i].v1.position); p = plane_inverse_transform.xform(in_3d_edges[i].v1.position);
v1.index = in_3d_edges[i].v1.index; v1.index = in_3d_edges[i].v1.index;
v1.surface = in_3d_edges[i].v1.surface;
v1.position.x = p.x; v1.position.x = p.x;
v1.position.y = p.y; v1.position.y = p.y;
out_2d_edges[i].v0 = v0; out_2d_edges[i].v0 = v0;
out_2d_edges[i].v1 = v1; out_2d_edges[i].v1 = v1;
} }
return plane_inverse_transform;
} }
void MeshEditingLibrary::build_2d_polygons_from_edges(std::vector<MeshEditPolygon2D> &out_polygons, const std::vector<MeshEditEdge2D> &in_edges, std::vector<MeshEditPolygon2D> &error_polygons) void MeshEditingLibrary::build_2d_polygons_from_edges(std::vector<MeshEditPolygon2D> &out_polygons, const std::vector<MeshEditEdge2D> &in_edges, std::vector<MeshEditPolygon2D> &error_polygons)
@@ -844,3 +875,40 @@ const Vector2 MeshEditingLibrary::calculate_planar_uv(const Vector3 &vertex, con
return Vector2(vertex.x / mesh_bounds.x, vertex.y / mesh_bounds.y); return Vector2(vertex.x / mesh_bounds.x, vertex.y / mesh_bounds.y);
} }
} }
Ref<Mesh> MeshEditingLibrary::draw_debug_polygons(const std::vector<MeshEditPolygon2D> &polygons, const Transform3D &plane_transform, const Color &colour)
{
#ifdef DEBUG_ENABLED
Ref<StandardMaterial3D> 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 = polygons.size();
for (uint32_t i = 0; i < num_polygons; i++)
{
const MeshEditPolygon2D &polygon = polygons[i];
const uint32_t num_points = 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(colour);
im->surface_add_vertex(transformed_vertex);
}
const MeshEditVert2D &vertex = polygon.vertices.front();
const Vector3 &transformed_vertex = plane_transform.inverse().xform(Vector3(vertex.position.x, vertex.position.y, 0.0f));
im->surface_set_color(colour);
im->surface_add_vertex(transformed_vertex);
}
im->surface_end();
return im;
#endif // DEBUG_ENABLED
}
+6 -2
View File
@@ -25,12 +25,14 @@ typedef std::vector<SkinnedVertices> SkinnedSurfaces;
struct MeshEditVert3D struct MeshEditVert3D
{ {
uint8_t surface; // Surface index of the original vertex array
uint32_t index; // Index into the original vertex array uint32_t index; // Index into the original vertex array
Vector3 position; // Position used for generating geometry Vector3 position; // Position used for generating geometry
}; };
struct MeshEditVert2D struct MeshEditVert2D
{ {
uint8_t surface; // Surface index of the original vertex array
uint32_t index; // Index into the original vertex array uint32_t index; // Index into the original vertex array
Vector2 position; // Position used for generating geometry Vector2 position; // Position used for generating geometry
}; };
@@ -68,7 +70,7 @@ public:
CAP_UV_TILED CAP_UV_TILED
}; };
static PackedVector3Array slice_mesh(const MeshInstance3D *original_mesh_instance, const Skeleton3D *original_skeleton, const Plane &local_plane, Ref<ArrayMesh> out_first_half, Ref<ArrayMesh> out_other_half, const MeshEditCapUV cap_option, const Ref<StandardMaterial3D> cap_material); static PackedVector3Array slice_mesh(MeshInstance3D *original_mesh_instance, const Skeleton3D *original_skeleton, const Plane &local_plane, Ref<ArrayMesh> out_first_half, Ref<ArrayMesh> out_other_half, const MeshEditCapUV cap_option, const Ref<StandardMaterial3D> cap_material);
protected: protected:
enum MeshSlicePlaneOrientation enum MeshSlicePlaneOrientation
@@ -83,11 +85,13 @@ private:
static SkinnedSurfaces get_skinned_vertex_positions(const Mesh *mesh, const Skeleton3D *skeleton = nullptr, std::vector<std::vector<Transform3D>> &bone_transforms = std::vector<std::vector<Transform3D>>()); static SkinnedSurfaces get_skinned_vertex_positions(const Mesh *mesh, const Skeleton3D *skeleton = nullptr, std::vector<std::vector<Transform3D>> &bone_transforms = std::vector<std::vector<Transform3D>>());
static void project_edges(std::vector<MeshEditEdge2D> &out_2d_edges, const Transform3D &to_node_space, const std::vector<MeshEditEdge3D> &in_3d_edges, const Plane &plane); static const Transform3D project_edges(std::vector<MeshEditEdge2D> &out_2d_edges, const Transform3D &to_node_space, const std::vector<MeshEditEdge3D> &in_3d_edges, const Plane &plane);
static void build_2d_polygons_from_edges(std::vector<MeshEditPolygon2D> &out_polygons, const std::vector<MeshEditEdge2D> &in_edges, std::vector<MeshEditPolygon2D> &error_polygons); static void build_2d_polygons_from_edges(std::vector<MeshEditPolygon2D> &out_polygons, const std::vector<MeshEditEdge2D> &in_edges, std::vector<MeshEditPolygon2D> &error_polygons);
static bool find_next_edge(MeshEditEdge2D &out_next_edge, std::vector<MeshEditEdge2D> &in_edge_set, const MeshEditVert2D &start); static bool find_next_edge(MeshEditEdge2D &out_next_edge, std::vector<MeshEditEdge2D> &in_edge_set, const MeshEditVert2D &start);
static void fix_polygon_winding(MeshEditPolygon2D &polygon); static void fix_polygon_winding(MeshEditPolygon2D &polygon);
static Ref<Mesh> draw_debug_polygons(const std::vector<MeshEditPolygon2D> &polygons, const Transform3D &plane_transform, const Color &colour);
// Godot boilerplate below // Godot boilerplate below
protected: protected:
static void _bind_methods() static void _bind_methods()