Started adding mesh editing functions to liborng.
This commit is contained in:
+1
-1
@@ -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
@@ -0,0 +1,440 @@
|
||||
/*
|
||||
* ©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(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.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;
|
||||
|
||||
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;
|
||||
|
||||
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];
|
||||
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;
|
||||
|
||||
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, UtilityFunctions::lerpf(0.0f, other_weight, alpha)));
|
||||
}
|
||||
}
|
||||
|
||||
// Sort the list of bones and weights from highest to lowest bone influence
|
||||
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)
|
||||
{
|
||||
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;
|
||||
|
||||
std::vector<MeshEditEdge2D> edges_2d;
|
||||
std::vector<MeshEditPolygon2D> polygon_set;
|
||||
std::vector<MeshEditPolygon2D> error_polygons;
|
||||
|
||||
UtilityFunctions::print("Plane normal: ", local_plane.get_normal());
|
||||
UtilityFunctions::print("Plane centre: ", local_plane.center());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<MeshEditEdge2D> MeshEditingLibrary::project_edges(const Transform3D &to_node_space, const std::vector<MeshEditEdge3D> &in_3d_edges, const Plane &plane)
|
||||
{
|
||||
std::vector<MeshEditEdge2D> out_2d_edges;
|
||||
out_2d_edges.reserve(in_3d_edges.size());
|
||||
|
||||
for (uint32_t i = 0; i < in_3d_edges.size(); i++)
|
||||
{
|
||||
MeshEditVert2D v0;
|
||||
v0.index = in_3d_edges[i].v0.index;
|
||||
}
|
||||
|
||||
return out_2d_edges;
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* ©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 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
|
||||
};
|
||||
|
||||
|
||||
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:
|
||||
std::vector<MeshEditEdge2D> MeshEditingLibrary::project_edges(const Transform3D &to_node_space, const std::vector<MeshEditEdge3D> &in_3d_edges, const Plane &plane);
|
||||
|
||||
// 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);
|
||||
@@ -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"
|
||||
@@ -66,6 +68,8 @@ void initialize_orng_module(ModuleInitializationLevel p_level) {
|
||||
ClassDB::register_class<PlayerSpawn>();
|
||||
|
||||
ClassDB::register_class<SaveFileData>();
|
||||
|
||||
ClassDB::register_class<MeshEditingLibrary>();
|
||||
|
||||
GDSINGLETON_REGISTER_CLASS(InputHandler, _input_handler_singleton);
|
||||
GDSINGLETON_REGISTER_CLASS(SaveManager, _save_manager_singleton);
|
||||
|
||||
Reference in New Issue
Block a user