Started adding mesh editing functions to liborng.

This commit is contained in:
2026-08-07 14:28:25 -04:00
parent 8ab4bde4a1
commit 882e5f694d
7 changed files with 2058 additions and 2 deletions
+2 -1
View File
@@ -24,6 +24,7 @@
*.la *.la
*.a *.a
*.lib *.lib
*.ilk
# Executables # Executables
*.exe *.exe
@@ -34,4 +35,4 @@
# ---> Platform Files # ---> Platform Files
*.sh *.sh
*.*.dblite *.dblite
Binary file not shown.
File diff suppressed because it is too large Load Diff
+738
View File
@@ -0,0 +1,738 @@
/*
* ©2026 Batty Bovine Productions, LLC. All Rights Reserved.
*
* If this source code makes it to the public internet, this software can be
* considered to be protected by the MIT licence. Have fun with it.
*/
#include "mesh_editing_library.h"
#include "orng_macros.h"
#include "earcut.hpp"
#include <map>
#include "godot_cpp/variant/utility_functions.hpp"
using namespace godot;
MeshEditingLibrary::MeshEditingLibrary()
{
}
MeshEditingLibrary::~MeshEditingLibrary()
{
}
TypedArray<PackedVector3Array> MeshEditingLibrary::get_skinned_vertex_positions(const Skeleton3D *skeleton, const Mesh *mesh, const Transform3D &component_to_world)
{
TypedArray<PackedVector3Array> skinned_positions;
std::vector<Transform3D> rest_bone_transforms;
std::vector<Transform3D> pose_bone_transforms;
const uint32_t num_bones = skeleton->get_bone_count();
rest_bone_transforms.resize(num_bones);
pose_bone_transforms.resize(num_bones);
for (uint32_t i = 0; i < num_bones; i++)
{
rest_bone_transforms[i] = skeleton->get_bone_rest(i).inverse();
pose_bone_transforms[i] = skeleton->get_bone_pose(i);
}
for (uint32_t surface = 0; surface < mesh->get_surface_count(); surface++)
{
Array mesh_arrays = mesh->surface_get_arrays(surface);
const PackedVector3Array &mesh_vertex_array = mesh_arrays[ArrayMesh::ARRAY_VERTEX];
const PackedInt32Array &mesh_bones_array = mesh_arrays[ArrayMesh::ARRAY_BONES];
const PackedFloat32Array &mesh_weights_array = mesh_arrays[ArrayMesh::ARRAY_WEIGHTS];
const uint32_t num_vertices = mesh_vertex_array.size();
const uint8_t num_bones_per_vertex = mesh_bones_array.size() / num_vertices;
PackedVector3Array surface_skinned_positions;
surface_skinned_positions.resize(num_vertices);
for (uint32_t vertex = 0; vertex < num_vertices; vertex++)
{
const Vector3 &rest_vertex_position = mesh_vertex_array[vertex];
Vector3 skinned_vertex_position;
for (uint8_t bone_index = 0; bone_index < num_bones_per_vertex; bone_index++)
{
const uint16_t bone = mesh_bones_array[(vertex * num_bones_per_vertex) + bone_index];
const float weight = mesh_weights_array[(vertex * num_bones_per_vertex) + bone_index];
const Transform3D &bone_transform = (pose_bone_transforms[bone] * rest_bone_transforms[bone]) * weight;
skinned_vertex_position += bone_transform.xform(rest_vertex_position);
}
surface_skinned_positions[vertex] = skinned_vertex_position;
}
skinned_positions.append(surface_skinned_positions);
}
return skinned_positions;
}
PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original_mesh_instance, const Plane &local_plane, ArrayMesh *out_first_half, ArrayMesh *out_other_half, const MeshEditCapUV cap_option, const Ref<StandardMaterial3D> cap_material)
{
const Mesh *original_mesh = original_mesh_instance->get_mesh().ptr();
const uint8_t num_surfaces = original_mesh->get_surface_count();
PackedVector3Array impact_points;
PackedVector3Array cap_section_vertices;
PackedVector3Array cap_section_normals;
PackedVector3Array cap_section_flipped_normals;
PackedFloat32Array cap_section_tangents;
PackedVector2Array cap_section_uvs;
PackedVector2Array cap_section_uv2s;
PackedColorArray cap_section_colours;
PackedInt32Array cap_section_bones;
PackedFloat32Array cap_section_weights;
PackedInt32Array cap_section_indices;
for (uint8_t surface = 0; surface < num_surfaces; surface++)
{
const Array &surface_arrays = original_mesh->surface_get_arrays(surface);
const PackedVector3Array &surface_vertex_array = surface_arrays[ArrayMesh::ARRAY_VERTEX];
const PackedVector3Array &surface_normal_array = surface_arrays[ArrayMesh::ARRAY_NORMAL];
const PackedFloat32Array &surface_tangent_array = surface_arrays[ArrayMesh::ARRAY_TANGENT];
const PackedVector2Array &surface_uv_array = surface_arrays[ArrayMesh::ARRAY_TEX_UV];
const PackedVector2Array &surface_uv2_array = surface_arrays[ArrayMesh::ARRAY_TEX_UV2];
const PackedColorArray &surface_colour_array = surface_arrays[ArrayMesh::ARRAY_COLOR];
const PackedInt32Array &surface_bone_array = surface_arrays[ArrayMesh::ARRAY_BONES];
const PackedFloat32Array &surface_weight_array = surface_arrays[ArrayMesh::ARRAY_WEIGHTS];
const Vector3 *surface_vertex_array_ptr = surface_vertex_array.ptr();
const uint32_t num_vertices = surface_vertex_array.size();
std::vector<float> vertex_distance;
vertex_distance.resize(num_vertices);
std::map<uint32_t, uint32_t> base_to_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_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 uint32_t bone_array_size = surface_bone_array.size();
const uint8_t num_bones_per_vertex = bone_array_size / num_vertices;
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;
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++)
{
vertex_distance[vertex] = local_plane.distance_to(surface_vertex_array_ptr[vertex]);
if (vertex_distance[vertex] >= 0.0f)
{
base_to_sliced_vert_index[vertex] = first_half_section_vertices.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]); }
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]);
}
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]);
}
}
}
else
{
base_to_other_sliced_vert_index[vertex] = other_half_section_vertices.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]); }
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]);
}
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]);
}
}
}
}
PackedInt32Array first_half_section_indices;
PackedInt32Array other_half_section_indices;
const PackedInt32Array &surface_index_array = surface_arrays[ArrayMesh::ARRAY_INDEX];
if (!(first_half_section_vertices.size() > 0 && other_half_section_vertices.size() > 0))
{
if (first_half_section_vertices.size() > 0)
{
first_half_section_indices = surface_index_array;
}
else if (other_half_section_vertices.size() > 0)
{
other_half_section_indices = surface_index_array;
}
}
else
{
const uint32_t num_triangles = surface_index_array.size();
for (uint32_t triangle_index = 0; triangle_index < num_triangles; triangle_index += 3)
{
uint32_t base_v[3];
std::map<uint32_t, uint32_t>::iterator sliced_v[3];
std::map<uint32_t, uint32_t>::iterator sliced_other_v[3];
const std::map<uint32_t, uint32_t>::iterator base_to_sliced_end = base_to_sliced_vert_index.end();
const std::map<uint32_t, uint32_t>::iterator base_to_other_sliced_end = base_to_other_sliced_vert_index.end();
for (uint32_t i = 0; i < 3; i++)
{
base_v[i] = surface_index_array.ptr()[triangle_index + i];
sliced_v[i] = base_to_sliced_vert_index.find(base_v[i]);
sliced_other_v[i] = base_to_other_sliced_vert_index.find(base_v[i]);
// All vertex indices must be represented by one of the two slice index maps.
assert((sliced_v[i] != base_to_sliced_vert_index.end()) != (sliced_other_v[i] != base_to_other_sliced_vert_index.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.
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);
}
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);
}
else
{ // If the triangle is split by the slice plane, then slice the overlapping edges.
uint32_t final_verts[4] = { 0, 0, 0, 0 };
uint8_t num_final_verts = 0;
uint32_t other_final_verts[4] = { 0, 0, 0, 0 };
uint8_t num_other_final_verts = 0;
MeshEditEdge3D new_clip_edge;
uint8_t clipped_edges = 0;
float plane_distance[3] = {
vertex_distance[base_v[0]],
vertex_distance[base_v[1]],
vertex_distance[base_v[2]]
};
for (uint32_t this_vert = 0; this_vert < 3; this_vert++)
{
if (sliced_v[this_vert] != base_to_sliced_end)
{
final_verts[num_final_verts] = sliced_v[this_vert]->second;
num_final_verts++;
}
else
{
other_final_verts[num_other_final_verts] = sliced_other_v[this_vert]->second;
num_other_final_verts++;
}
uint32_t next_vert = (this_vert + 1) % 3;
if ((sliced_v[this_vert] == base_to_sliced_end) != (sliced_v[next_vert] == base_to_sliced_end))
{
float alpha = UtilityFunctions::clampf(-plane_distance[this_vert] / (plane_distance[next_vert] - plane_distance[this_vert]), 0.0f, 1.0f);
const Vector3 interp_vert = surface_vertex_array_ptr[base_v[this_vert]].lerp(
surface_vertex_array_ptr[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();
// Lerp the skinned vertex position here; it's necessary for projecting vertices to a 2D plane correctly.
// This part needs to be finished once we can get the skinned vertex positions.
// DON'T FORGET TO DO THAT
const Vector3 skinned_lerp = interp_vert;
MeshEditVert3D edge_vertex;
edge_vertex.index = first_half_section_vertices.size();
edge_vertex.position = skinned_lerp;
if (clipped_edges == 0)
{
new_clip_edge.v0 = edge_vertex;
}
else
{
new_clip_edge.v1 = edge_vertex;
}
clipped_edges++;
assert(clipped_edges <= 2);
first_half_section_vertices.append(interp_vert);
other_half_section_vertices.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);
}
if (has_tangent)
{
const float *this_tangent_start = (surface_tangent_array.ptr() + (base_v[this_vert] * 4));
const float *next_tangent_start = (surface_tangent_array.ptr() + (base_v[next_vert] * 4));
const Vector3 &this_tangent = Vector3(*this_tangent_start, *(this_tangent_start + 1), *(this_tangent_start + 2));
const Vector3 &next_tangent = Vector3(*next_tangent_start, *(next_tangent_start + 1), *(next_tangent_start + 2));
const float this_binormal = *(this_tangent_start + 3);
const float next_binormal = *(next_tangent_start + 3);
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);
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);
}
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);
}
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);
}
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);
}
if (has_bone && has_weight)
{
const int32_t *bone_pointer = surface_bone_array.ptr();
const float *weight_pointer = surface_weight_array.ptr();
std::vector<std::pair<int32_t, float>> interp_bones_and_weights;
// First pack all bones and weights into a list of pairs
uint8_t i = 0;
for (i = 0; i < num_bones_per_vertex; i++)
{
interp_bones_and_weights.emplace_back(std::pair<int32_t,float>(
*(bone_pointer + (base_v[this_vert] * num_bones_per_vertex) + i),
*(weight_pointer + (base_v[this_vert] * num_bones_per_vertex) + i)
));
}
// Next, find each first half bone that matches one in the second half, and interpolate between them.
// If the second half has a unique bone, interpolate a new value from 0 for it.
for (uint32_t other_bone_index = 0; other_bone_index < num_bones_per_vertex; other_bone_index++)
{
const uint32_t other_bone = *(bone_pointer + (base_v[next_vert] * num_bones_per_vertex) + other_bone_index);
const float other_weight = *(weight_pointer + (base_v[next_vert] * num_bones_per_vertex) + other_bone_index);
int8_t matching_bone_index = -1;
for (uint8_t i = 0; i < interp_bones_and_weights.size(); i++)
{
if (interp_bones_and_weights[i].first == other_bone)
{
matching_bone_index = i;
break;
}
}
if (matching_bone_index != -1)
{
interp_bones_and_weights[matching_bone_index].second = UtilityFunctions::lerpf(
interp_bones_and_weights[matching_bone_index].second, other_weight, alpha);
}
else
{
interp_bones_and_weights.emplace_back(std::pair<int32_t,float>(
other_bone, (float)UtilityFunctions::lerpf(0.0f, other_weight, alpha)));
}
}
// Sort the list of bones and weights from highest weight to lowest weight
std::sort(interp_bones_and_weights.begin(), interp_bones_and_weights.end(),
[=](std::pair<int32_t, float>&a, std::pair<int32_t, float>&b)
{ return a.second > b.second; });
// Finally, add all our new interpolated bones and weights to the arrays
for (i = 0; i < num_bones_per_vertex; i++)
{
first_half_section_bones.append(interp_bones_and_weights[i].first);
first_half_section_weights.append(interp_bones_and_weights[i].second);
other_half_section_bones.append(interp_bones_and_weights[i].first);
other_half_section_weights.append(interp_bones_and_weights[i].second);
}
}
}
}
// There should always be exactly two sliced edges per triangle
assert(clipped_edges == 2);
clip_edges.emplace_back(new_clip_edge);
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]);
}
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]);
}
}
}
}
if (first_half_section_vertices.size() > 0 && first_half_section_indices.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;
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)
{
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;
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));
}
if (clip_edges.size() > 0)
{
std::vector<MeshEditEdge2D> edges_2d;
std::vector<MeshEditPolygon2D> polygon_set;
std::vector<MeshEditPolygon2D> 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<float,2>;
using Polygon = std::vector<std::vector<Point>>;
Polygon earcut_polygon;
std::vector<Point> 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];
cap_section_vertices.append(position);
if (has_normal) { cap_section_normals.append(local_plane_normal * -1.0f); cap_section_flipped_normals.append(local_plane_normal); }
// if (has_tangent) { for (uint8_t i = 0; i < 4; i++) { cap_section_tangents.append(first_half_section_tangents[(vertex.index * 4) + i]); } }
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_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_bones.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;
}
earcut_polygon.push_back(sub_polygon);
std::vector<uint32_t> indices = mapbox::earcut<uint32_t>(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);
}
polygon_centroid /= polygon_set[polygon_index].vertices.size();
impact_points.append(polygon_centroid);
}
}
}
if (cap_section_vertices.size() > 0 && cap_section_indices.size() > 0)
{
Array cap_mesh_section;
cap_mesh_section.resize(ArrayMesh::ARRAY_MAX);
cap_mesh_section[ArrayMesh::ARRAY_VERTEX] = cap_section_vertices;
cap_mesh_section[ArrayMesh::ARRAY_INDEX] = cap_section_indices;
if (cap_section_normals.size()) cap_mesh_section[ArrayMesh::ARRAY_NORMAL] = cap_section_normals;
// if (cap_section_tangents.size()) cap_mesh_section[ArrayMesh::ARRAY_TANGENT] = cap_section_tangents;
if (cap_section_colours.size()) cap_mesh_section[ArrayMesh::ARRAY_COLOR] = cap_section_colours;
if (cap_section_uvs.size()) cap_mesh_section[ArrayMesh::ARRAY_TEX_UV] = cap_section_uvs;
if (cap_section_uv2s.size()) cap_mesh_section[ArrayMesh::ARRAY_TEX_UV2] = cap_section_uv2s;
if (cap_section_bones.size()) cap_mesh_section[ArrayMesh::ARRAY_BONES] = cap_section_bones;
if (cap_section_weights.size()) cap_mesh_section[ArrayMesh::ARRAY_WEIGHTS] = cap_section_weights;
out_first_half->add_surface_from_arrays(ArrayMesh::PRIMITIVE_TRIANGLES, cap_mesh_section);
out_first_half->surface_set_material(out_first_half->get_surface_count()-1, cap_material);
const uint32_t num_cap_triangles = cap_section_indices.size();
for (uint32_t i = 0; i < num_cap_triangles; i += 3)
{
const int32_t old_index = cap_section_indices[i];
cap_section_indices[i] = cap_section_indices[i+1];
cap_section_indices[i+1] = old_index;
}
Array cap_other_mesh_section;
cap_other_mesh_section.resize(ArrayMesh::ARRAY_MAX);
cap_other_mesh_section[ArrayMesh::ARRAY_VERTEX] = cap_section_vertices;
cap_other_mesh_section[ArrayMesh::ARRAY_INDEX] = cap_section_indices;
if (cap_section_normals.size()) cap_other_mesh_section[ArrayMesh::ARRAY_NORMAL] = cap_section_flipped_normals;
// if (cap_section_tangents.size()) cap_other_mesh_section[ArrayMesh::ARRAY_TANGENT] = cap_section_flipped_tangents;
if (cap_section_colours.size()) cap_other_mesh_section[ArrayMesh::ARRAY_COLOR] = cap_section_colours;
if (cap_section_uvs.size()) cap_other_mesh_section[ArrayMesh::ARRAY_TEX_UV] = cap_section_uvs;
if (cap_section_uv2s.size()) cap_other_mesh_section[ArrayMesh::ARRAY_TEX_UV2] = cap_section_uv2s;
if (cap_section_bones.size()) cap_other_mesh_section[ArrayMesh::ARRAY_BONES] = cap_section_bones;
if (cap_section_weights.size()) cap_other_mesh_section[ArrayMesh::ARRAY_WEIGHTS] = cap_section_weights;
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);
}
return impact_points;
}
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)
{
out_2d_edges.resize(in_3d_edges.size());
const Transform3D &plane_inverse_transform = Transform3D(Basis::looking_at(plane.get_normal()), plane.center()).inverse();
for (uint32_t i = 0; i < in_3d_edges.size(); i++)
{
MeshEditVert2D v0;
Vector3 p = plane_inverse_transform.xform(in_3d_edges[i].v0.position);
v0.index = in_3d_edges[i].v0.index;
v0.position.x = p.x;
v0.position.y = p.y;
MeshEditVert2D v1;
p = plane_inverse_transform.xform(in_3d_edges[i].v1.position);
v1.index = in_3d_edges[i].v1.index;
v1.position.x = p.x;
v1.position.y = p.y;
out_2d_edges[i].v0 = v0;
out_2d_edges[i].v1 = v1;
}
}
void MeshEditingLibrary::build_2d_polygons_from_edges(std::vector<MeshEditPolygon2D> &out_polygons, const std::vector<MeshEditEdge2D> &in_edges, std::vector<MeshEditPolygon2D> &error_polygons)
{
std::vector<MeshEditEdge2D> edge_set = in_edges;
while (edge_set.size() > 0)
{
MeshEditPolygon2D new_polygon;
const MeshEditEdge2D &first_edge = edge_set.back();
edge_set.pop_back();
new_polygon.vertices.emplace_back(first_edge.v0);
new_polygon.vertices.emplace_back(first_edge.v1);
MeshEditVert2D &polygon_end = new_polygon.vertices.back();
MeshEditEdge2D next_edge;
while (MeshEditingLibrary::find_next_edge(next_edge, edge_set, polygon_end))
{
new_polygon.vertices.emplace_back(next_edge.v1);
polygon_end = new_polygon.vertices.back();
}
if (new_polygon.vertices.size() >= 4 && (new_polygon.vertices.front().position - new_polygon.vertices.back().position).length_squared() < PRETTY_SMALL_NUMBER)
{
new_polygon.vertices.pop_back();
MeshEditingLibrary::fix_polygon_winding(new_polygon);
out_polygons.emplace_back(new_polygon);
}
else
{
error_polygons.emplace_back(new_polygon);
}
}
}
bool MeshEditingLibrary::find_next_edge(MeshEditEdge2D &out_next_edge, std::vector<MeshEditEdge2D> &in_edge_set, const MeshEditVert2D &start)
{
float closest_squared_distance = FLT_MAX;
int32_t out_edge_index = -1;
// Search the edges for one that starts closest to the starting point
uint32_t num_in_edges = in_edge_set.size();
for (uint32_t i = 0; i < num_in_edges; i++)
{
float distance_squared = (in_edge_set[i].v0.position - start.position).length_squared();
if (distance_squared < closest_squared_distance)
{
closest_squared_distance = distance_squared;
out_next_edge = in_edge_set[i];
out_edge_index = i;
}
distance_squared = (in_edge_set[i].v1.position - start.position).length_squared();
if (distance_squared < closest_squared_distance)
{
closest_squared_distance = distance_squared;
out_next_edge = in_edge_set[i];
std::swap(out_next_edge.v0, out_next_edge.v1);
out_edge_index = i;
}
}
// If the next edge starts close enough, return it
if (closest_squared_distance < TINY_NUMBER)
{
assert(out_edge_index >= 0);
in_edge_set.erase(in_edge_set.begin() + out_edge_index);
return true;
}
return false;
}
void MeshEditingLibrary::fix_polygon_winding(MeshEditPolygon2D &polygon)
{
float total_angle = 0.0f;
for (int32_t i = polygon.vertices.size() - 1; i >= 0; i--)
{
const int32_t a_index = (i - 1) % polygon.vertices.size();
const int32_t b_index = i;
const int32_t c_index = (i + 1) % polygon.vertices.size();
const float ab_dist_squared = (polygon.vertices[b_index].position - polygon.vertices[a_index].position).length_squared();
const Vector2 ab_edge = (polygon.vertices[b_index].position - polygon.vertices[a_index].position).normalized();
const float bc_dist_squared = (polygon.vertices[c_index].position - polygon.vertices[b_index].position).length_squared();
const Vector2 bc_edge = (polygon.vertices[c_index].position - polygon.vertices[b_index].position).normalized();
if (ab_dist_squared < TINY_NUMBER || bc_dist_squared < TINY_NUMBER || (ab_edge - bc_edge).length_squared() < TEENY_TINY_NUMBER)
{
polygon.vertices.erase(polygon.vertices.begin() + i);
}
else
{
total_angle += UtilityFunctions::asin(ab_edge.x * bc_edge.y - ab_edge.y * bc_edge.x);
}
}
if (total_angle < 0.0f)
{
const uint32_t num_vertices = polygon.vertices.size();
std::vector<MeshEditVert2D> new_vertices;
new_vertices.resize(num_vertices);
for (uint32_t i = 0; i < num_vertices; i++)
{
new_vertices[i] = polygon.vertices[num_vertices - (i + 1)];
}
polygon.vertices = new_vertices;
}
}
const Vector2 MeshEditingLibrary::calculate_planar_uv(const Vector3 &vertex, const Vector3 &mesh_bounds, const MeshSlicePlaneOrientation &axis)
{
switch(axis)
{
case MeshSlicePlaneOrientation::X:
return Vector2(vertex.y / mesh_bounds.y, vertex.z / mesh_bounds.z);
case MeshSlicePlaneOrientation::Y:
return Vector2(vertex.x / mesh_bounds.x, vertex.z / mesh_bounds.z);
case MeshSlicePlaneOrientation::Z: default:
return Vector2(vertex.x / mesh_bounds.x, vertex.y / mesh_bounds.y);
}
}
+98
View File
@@ -0,0 +1,98 @@
/*
* ©2026 Batty Bovine Productions, LLC. All Rights Reserved.
*
* If this source code makes it to the public internet, this software can be
* considered to be protected by the MIT licence. Have fun with it.
*/
#pragma once
#include <godot_cpp/classes/array_mesh.hpp>
#include <godot_cpp/classes/mesh_instance3d.hpp>
#include <godot_cpp/classes/skeleton3d.hpp>
#include <godot_cpp/classes/standard_material3d.hpp>
#include <godot_cpp/variant/plane.hpp>
#include <godot_cpp/variant/vector3.hpp>
using namespace godot;
#include <vector>
struct MeshEditVert3D
{
uint32_t index; // Index into the original vertex array
Vector3 position; // Position used for generating geometry
};
struct MeshEditVert2D
{
uint32_t index; // Index into the original vertex array
Vector2 position; // Position used for generating geometry
};
struct MeshEditEdge2D
{
MeshEditVert2D v0; // Start vertex
MeshEditVert2D v1; // End vertex
};
struct MeshEditEdge3D
{
MeshEditVert3D v0; // Start vertex
MeshEditVert3D v1; // End vertex
};
struct MeshEditPolygon2D
{
std::vector<MeshEditVert2D> vertices; // List of vertices representing a closed 2D polygon
};
class MeshEditingLibrary : public Node
{
GDCLASS(MeshEditingLibrary, Node);
public:
MeshEditingLibrary();
~MeshEditingLibrary();
enum MeshEditCapUV
{
CAP_UV_FILL_MESH_BOUNDS,
CAP_UV_FILL_CAP_BOUNDS,
CAP_UV_TILED
};
static TypedArray<PackedVector3Array> get_skinned_vertex_positions(const Skeleton3D *skeleton, const Mesh *mesh, const Transform3D &component_to_world);
static PackedVector3Array slice_mesh(const MeshInstance3D *original_mesh_instance, const Plane &local_plane, ArrayMesh *out_first_half, ArrayMesh *out_other_half, const MeshEditCapUV cap_option, const Ref<StandardMaterial3D> cap_material);
protected:
enum MeshSlicePlaneOrientation
{
X,
Y,
Z
};
private:
static const Vector2 calculate_planar_uv(const Vector3 &vertex, const Vector3 &mesh_bounds, const MeshSlicePlaneOrientation &axis);
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 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 void fix_polygon_winding(MeshEditPolygon2D &polygon);
// Godot boilerplate below
protected:
static void _bind_methods()
{
ClassDB::bind_static_method("MeshEditingLibrary", D_METHOD("get_skinned_vertex_positions", "skeleton", "mesh"), &MeshEditingLibrary::get_skinned_vertex_positions);
ClassDB::bind_static_method("MeshEditingLibrary", D_METHOD("slice_mesh", "original_mesh_instance", "local_plane", "out_first_half", "out_other_half", "cap_option", "cap_material"), &MeshEditingLibrary::slice_mesh);
BIND_ENUM_CONSTANT(CAP_UV_FILL_MESH_BOUNDS);
BIND_ENUM_CONSTANT(CAP_UV_FILL_CAP_BOUNDS);
BIND_ENUM_CONSTANT(CAP_UV_TILED);
}
};
VARIANT_ENUM_CAST(MeshEditingLibrary::MeshEditCapUV);
+9 -1
View File
@@ -1,5 +1,13 @@
#pragma once #pragma once
/**
* Helper constants
*/
#define TEENY_TINY_NUMBER 0.000000000001f
#define TINY_NUMBER 0.0000000001f
#define PRETTY_SMALL_NUMBER 0.00000001f
#define SMALL_NUMBER 0.000001f
/** /**
* Method and property binding helpers * Method and property binding helpers
*/ */
@@ -27,4 +35,4 @@
* Deferred function helpers * Deferred function helpers
*/ */
#define CALL_NEXT_FRAME(C) \ #define CALL_NEXT_FRAME(C) \
this->get_tree()->create_timer(0.001)->connect("timeout", C) this->get_tree()->create_timer(SMALL_NUMBER)->connect("timeout", C)
+4
View File
@@ -25,6 +25,8 @@
#include "resources/level_metadata_map.h" #include "resources/level_metadata_map.h"
#include "resources/level_metadata_resource.h" #include "resources/level_metadata_resource.h"
#include "nodes/mesh_editing_library.h"
#include "singletons/input_handler.h" #include "singletons/input_handler.h"
#include "singletons/save_manager.h" #include "singletons/save_manager.h"
#include "singletons/scene_loader.h" #include "singletons/scene_loader.h"
@@ -67,6 +69,8 @@ void initialize_orng_module(ModuleInitializationLevel p_level) {
ClassDB::register_class<SaveFileData>(); ClassDB::register_class<SaveFileData>();
ClassDB::register_class<MeshEditingLibrary>();
GDSINGLETON_REGISTER_CLASS(InputHandler, _input_handler_singleton); GDSINGLETON_REGISTER_CLASS(InputHandler, _input_handler_singleton);
GDSINGLETON_REGISTER_CLASS(SaveManager, _save_manager_singleton); GDSINGLETON_REGISTER_CLASS(SaveManager, _save_manager_singleton);
GDSINGLETON_REGISTER_CLASS(SceneLoader, _scene_loader_singleton); GDSINGLETON_REGISTER_CLASS(SceneLoader, _scene_loader_singleton);