Make MeshInstance2DNavigationGeometryParser2D use the Geometry singleton instead of the clipper2 module.

This commit is contained in:
Relintai 2023-06-07 15:32:30 +02:00
parent 2e1e153bc3
commit 443482ee47

View File

@ -36,18 +36,11 @@
#include "scene/resources/navigation_mesh_source_geometry_data_2d.h" #include "scene/resources/navigation_mesh_source_geometry_data_2d.h"
#include "scene/resources/navigation_polygon.h" #include "scene/resources/navigation_polygon.h"
#include "modules/modules_enabled.gen.h"
#ifdef MODULE_CLIPPER2_ENABLED
#include "modules/clipper2/lib/include/clipper2/clipper.h"
#endif // MODULE_CLIPPER2_ENABLED
bool MeshInstance2DNavigationGeometryParser2D::parses_node(Node *p_node) { bool MeshInstance2DNavigationGeometryParser2D::parses_node(Node *p_node) {
return (Object::cast_to<MeshInstance2D>(p_node) != nullptr); return (Object::cast_to<MeshInstance2D>(p_node) != nullptr);
} }
void MeshInstance2DNavigationGeometryParser2D::parse_geometry(Node *p_node, Ref<NavigationPolygon> p_navigation_polygon, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry) { void MeshInstance2DNavigationGeometryParser2D::parse_geometry(Node *p_node, Ref<NavigationPolygon> p_navigation_polygon, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry) {
#ifdef MODULE_CLIPPER2_ENABLED
NavigationPolygon::ParsedGeometryType parsed_geometry_type = p_navigation_polygon->get_parsed_geometry_type(); NavigationPolygon::ParsedGeometryType parsed_geometry_type = p_navigation_polygon->get_parsed_geometry_type();
if (Object::cast_to<MeshInstance2D>(p_node) && parsed_geometry_type != NavigationPolygon::PARSED_GEOMETRY_STATIC_COLLIDERS) { if (Object::cast_to<MeshInstance2D>(p_node) && parsed_geometry_type != NavigationPolygon::PARSED_GEOMETRY_STATIC_COLLIDERS) {
@ -59,9 +52,7 @@ void MeshInstance2DNavigationGeometryParser2D::parse_geometry(Node *p_node, Ref<
const Transform2D transform = mesh_instance->get_transform(); const Transform2D transform = mesh_instance->get_transform();
using namespace Clipper2Lib; Vector<Vector<Point2>> mesh_subject_paths, dummy_clip_paths;
Paths64 subject_paths, dummy_clip_paths;
for (int i = 0; i < mesh->get_surface_count(); i++) { for (int i = 0; i < mesh->get_surface_count(); i++) {
if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) { if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
@ -72,7 +63,7 @@ void MeshInstance2DNavigationGeometryParser2D::parse_geometry(Node *p_node, Ref<
continue; continue;
} }
Path64 subject_path; Vector<Point2> subject_path;
int index_count = 0; int index_count = 0;
if (mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_INDEX) { if (mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_INDEX) {
@ -93,43 +84,32 @@ void MeshInstance2DNavigationGeometryParser2D::parse_geometry(Node *p_node, Ref<
int vertex_index = mesh_indices[j]; int vertex_index = mesh_indices[j];
const Vector2 &vertex = mesh_vertices[vertex_index]; const Vector2 &vertex = mesh_vertices[vertex_index];
const Point64 &point = Point64(vertex.x, vertex.y); subject_path.push_back(vertex);
subject_path.push_back(point);
} }
} else { } else {
for (int j = 0; j < mesh_vertices.size(); ++j) { for (int j = 0; j < mesh_vertices.size(); ++j) {
const Vector2 &vertex = mesh_vertices[j]; const Vector2 &vertex = mesh_vertices[j];
subject_path.push_back(vertex);
const Point64 &point = Point64(vertex.x, vertex.y);
subject_path.push_back(point);
} }
} }
subject_paths.push_back(subject_path); mesh_subject_paths.push_back(subject_path);
} }
Paths64 path_solution; Vector<Vector<Point2>> mesh_path_solution = Geometry::merge_all2_polygons_2d(mesh_subject_paths, dummy_clip_paths, Geometry::POLYGON_FILL_TYPE_NON_ZERO);
path_solution = Union(subject_paths, dummy_clip_paths, FillRule::NonZero);
//path_solution = RamerDouglasPeucker(path_solution, 0.025);
Vector<Vector<Vector2>> polypaths; Vector<Vector<Vector2>> polypaths;
for (uint32_t i = 0; i < path_solution.size(); i++) { for (int k = 0; k < mesh_path_solution.size(); ++k) {
const Path64 &scaled_path = path_solution[i]; const Vector<Point2> &mesh_path = mesh_path_solution[k];
PoolVector<Vector2> shape_outline; PoolVector<Vector2> shape_outline;
for (const Point64 &scaled_point : scaled_path) { for (int j = 0; j < mesh_path.size(); j++) {
shape_outline.push_back(Point2(static_cast<real_t>(scaled_point.x), static_cast<real_t>(scaled_point.y))); const Vector2 &mesh_path_point = mesh_path[j];
} shape_outline.push_back(transform.xform(mesh_path_point));
for (int j = 0; j < shape_outline.size(); j++) {
shape_outline.set(j, transform.xform(shape_outline[j]));
} }
p_source_geometry->add_obstruction_outline(shape_outline); p_source_geometry->add_obstruction_outline(shape_outline);
} }
} }
#endif // MODULE_CLIPPER2_ENABLED
} }