Started adding mesh editing functions to liborng.

This commit is contained in:
2026-08-02 05:12:03 -04:00
parent 8ab4bde4a1
commit a8bd6bd4da
6 changed files with 1622 additions and 1 deletions
+1 -1
View File
@@ -24,6 +24,7 @@
*.la
*.a
*.lib
*.ilk
# Executables
*.exe
@@ -34,4 +35,3 @@
# ---> Platform Files
*.sh
*.*.dblite
Binary file not shown.
File diff suppressed because it is too large Load Diff
+352
View File
@@ -0,0 +1,352 @@
/*
* ©2023 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 "earcut.hpp"
#include <map>
#include "godot_cpp/variant/utility_functions.hpp"
using namespace godot;
MeshEditingLibrary::MeshEditingLibrary()
{
}
MeshEditingLibrary::~MeshEditingLibrary()
{
}
bool MeshEditingLibrary::slice_mesh(const Mesh *original_mesh, const Plane &local_plane, ArrayMesh *out_first_half, ArrayMesh *out_other_half, const MeshSliceCapUVOption cap_option, const StandardMaterial3D *cap_material, Vector3 impact_point)
{
const uint8_t num_surfaces = original_mesh->get_surface_count();
std::vector<MeshEditEdge3D> clip_edges;
for (uint8_t surface = 0; surface < num_surfaces; surface++)
{
const Array &surface_arrays = original_mesh->surface_get_arrays(0);
const PackedVector3Array &surface_vertex_array = surface_arrays[Mesh::ARRAY_VERTEX];
const PackedVector3Array &surface_normal_array = surface_arrays[Mesh::ARRAY_NORMAL];
const PackedFloat32Array &surface_tangent_array = surface_arrays[Mesh::ARRAY_TANGENT];
const PackedVector2Array &surface_uv_array = surface_arrays[Mesh::ARRAY_TEX_UV];
const PackedVector2Array &surface_uv2_array = surface_arrays[Mesh::ARRAY_TEX_UV2];
const PackedColorArray &surface_colour_array = surface_arrays[Mesh::ARRAY_COLOR];
const PackedInt32Array &surface_bone_array = surface_arrays[Mesh::ARRAY_BONES];
const PackedFloat32Array &surface_weight_array = surface_arrays[Mesh::ARRAY_WEIGHTS];
const Vector3 *surface_vertex_array_ptr = surface_vertex_array.ptr();
const uint8_t num_vertices = surface_vertex_array.size();
std::vector<float> vertex_distance;
vertex_distance.reserve(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;
uint32_t num_first_half_verts = 0;
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;
uint32_t num_other_half_verts = 0;
for (uint8_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] = num_first_half_verts;
num_first_half_verts++;
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] = num_other_half_verts;
num_other_half_verts++;
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]);
}
}
}
}
// std::vector<> clip_edges
PackedInt32Array first_half_section_indices;
PackedInt32Array other_half_section_indices;
const PackedInt32Array &surface_index_array = surface_arrays[Mesh::ARRAY_INDEX];
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]);
// There should never be a point where an edge is marked for splitting but is entirely on one side of the plane.
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);
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]].lerp(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_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 *bone_pointer = surface_weight_array.ptr();
int32_t *interp_bones = new int32_t[num_bones_per_vertex];
float *interp_weights = new float[num_bones_per_vertex];
uint32_t i = 0;
for (i; i < num_bones_per_vertex; i++)
{
const int32_t bone = *(bone_pointer + (base_v[this_vert] * num_bones_per_vertex) + i);
}
}
final_verts[num_final_verts++] = num_first_half_verts;
other_final_verts[num_other_final_verts++] = num_other_half_verts;
// Lerp the skinned vertex position here; it's necessary for projecting verts to a 2D plane correctly.
const Vector3 skinned_lerp = interp_vert;
assert(clipped_edges < 2);
MeshEditVert3D edge_vertex;
edge_vertex.index = num_first_half_verts;
edge_vertex.position = skinned_lerp;
if (clipped_edges == 0)
{
new_clip_edge.v0 = edge_vertex;
}
else
{
new_clip_edge.v1 = edge_vertex;
}
clipped_edges++;
num_first_half_verts++;
num_other_half_verts++;
}
}
// 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++)
{
first_half_section_indices.append(other_final_verts[0]);
first_half_section_indices.append(other_final_verts[vertex_index - 1]);
first_half_section_indices.append(other_final_verts[vertex_index]);
}
}
}
Array first_half_section;
first_half_section.resize(Mesh::ARRAY_MAX);
first_half_section[Mesh::ARRAY_VERTEX] = first_half_section_vertices;
first_half_section[Mesh::ARRAY_NORMAL] = first_half_section_normals;
// first_half_section[Mesh::ARRAY_TANGENT] = first_half_section_tangents;
first_half_section[Mesh::ARRAY_TEX_UV] = first_half_section_uvs;
first_half_section[Mesh::ARRAY_TEX_UV2] = first_half_section_uv2s;
first_half_section[Mesh::ARRAY_COLOR] = first_half_section_colours;
first_half_section[Mesh::ARRAY_BONES] = first_half_section_bones;
first_half_section[Mesh::ARRAY_WEIGHTS] = first_half_section_weights;
Array other_half_section;
other_half_section.resize(Mesh::ARRAY_MAX);
other_half_section[Mesh::ARRAY_VERTEX] = other_half_section_vertices;
other_half_section[Mesh::ARRAY_NORMAL] = other_half_section_normals;
// other_half_section[Mesh::ARRAY_TANGENT] = other_half_section_tangents;
other_half_section[Mesh::ARRAY_TEX_UV] = other_half_section_uvs;
other_half_section[Mesh::ARRAY_TEX_UV2] = other_half_section_uv2s;
other_half_section[Mesh::ARRAY_COLOR] = other_half_section_colours;
other_half_section[Mesh::ARRAY_BONES] = other_half_section_bones;
other_half_section[Mesh::ARRAY_WEIGHTS] = other_half_section_weights;
out_first_half->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, first_half_section);
out_other_half->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, other_half_section);
}
if (clip_edges.size() > 0)
{
PackedVector3Array slice_section_vertices;
PackedVector3Array slice_section_normals;
PackedFloat32Array slice_section_tangents;
PackedVector2Array slice_section_uvs;
PackedVector2Array slice_section_uv2s;
PackedColorArray slice_section_colours;
PackedInt32Array slice_section_bones;
PackedFloat32Array slice_section_weights;
uint32_t num_slice_section_verts = 0;
}
return false;
}
+58
View File
@@ -0,0 +1,58 @@
/*
* ©2023 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/node.hpp>
#include <godot_cpp/classes/standard_material3d.hpp>
#include <godot_cpp/variant/vector3.hpp>
#include <godot_cpp/variant/plane.hpp>
using namespace godot;
struct MeshEditVert3D
{
uint32_t index; // Index into the original vertex array
Vector3 position; // Position used for generating geometry
};
struct MeshEditEdge3D
{
MeshEditVert3D v0; // Start vertex
MeshEditVert3D v1; // End vertex
};
enum MeshSliceCapUVOption
{
FILL_MESH_BOUNDS,
FILL_CAP_BOUNDS
};
class MeshEditingLibrary : public Node
{
GDCLASS(MeshEditingLibrary, Node);
public:
MeshEditingLibrary();
~MeshEditingLibrary();
static bool slice_mesh(const Mesh *original_mesh, const Plane &local_plane, ArrayMesh *out_first_half, ArrayMesh *out_other_half, const MeshSliceCapUVOption cap_option, const StandardMaterial3D *cap_material, Vector3 impact_point);
private:
// Godot boilerplate below
protected:
static void _bind_methods()
{
ClassDB::bind_static_method("MeshEditingLibrary", D_METHOD("slice_mesh", "original_mesh", "local_plane", "out_first_half", "out_other_half", "cap_option", "cap_material", "impact_point"), &MeshEditingLibrary::slice_mesh);
}
};
VARIANT_ENUM_CAST(MeshSliceCapUVOption);
+4
View File
@@ -25,6 +25,8 @@
#include "resources/level_metadata_map.h"
#include "resources/level_metadata_resource.h"
#include "nodes/mesh_editing_library.h"
#include "singletons/input_handler.h"
#include "singletons/save_manager.h"
#include "singletons/scene_loader.h"
@@ -67,6 +69,8 @@ void initialize_orng_module(ModuleInitializationLevel p_level) {
ClassDB::register_class<SaveFileData>();
ClassDB::register_class<MeshEditingLibrary>();
GDSINGLETON_REGISTER_CLASS(InputHandler, _input_handler_singleton);
GDSINGLETON_REGISTER_CLASS(SaveManager, _save_manager_singleton);
GDSINGLETON_REGISTER_CLASS(SceneLoader, _scene_loader_singleton);