Added back GridMap support for the NavigationMeshGenerator.

This commit is contained in:
Relintai 2022-10-09 00:01:13 +02:00
parent cd944e8bbe
commit 575a19eafb

View File

@ -58,6 +58,10 @@
#include "editor/editor_settings.h"
#endif
#ifdef MODULE_GRIDMAP_ENABLED
#include "modules/gridmap/grid_map.h"
#endif
NavigationMeshGenerator *NavigationMeshGenerator::singleton = NULL;
void NavigationMeshGenerator::_add_vertex(const Vector3 &p_vec3, Vector<float> &p_vertices) {
@ -269,6 +273,141 @@ void NavigationMeshGenerator::_parse_geometry(const Transform &p_navmesh_xform,
}
}
#ifdef MODULE_GRIDMAP_ENABLED
GridMap *gridmap = Object::cast_to<GridMap>(p_node);
if (gridmap) {
if (p_generate_from != NavigationMesh::PARSED_GEOMETRY_STATIC_COLLIDERS) {
Array meshes = gridmap->get_meshes();
Transform xform = gridmap->get_global_transform();
for (int i = 0; i < meshes.size(); i += 2) {
Ref<Mesh> mesh = meshes[i + 1];
if (mesh.is_valid()) {
Transform mesh_xform = meshes[i];
_add_mesh(mesh, p_navmesh_xform * xform * mesh_xform, p_vertices, p_indices);
}
}
}
if (p_generate_from != NavigationMesh::PARSED_GEOMETRY_MESH_INSTANCES && (gridmap->get_collision_layer() & p_collision_mask)) {
Array shapes = gridmap->get_collision_shapes();
for (int i = 0; i < shapes.size(); i += 2) {
RID shape = shapes[i + 1];
PhysicsServer::ShapeType type = PhysicsServer::get_singleton()->shape_get_type(shape);
Variant data = PhysicsServer::get_singleton()->shape_get_data(shape);
switch (type) {
case PhysicsServer::SHAPE_SPHERE: {
real_t radius = data;
Array arr;
arr.resize(RS::ARRAY_MAX);
SphereMesh::create_mesh_array(arr, radius, radius * 2.0);
_add_mesh_array(arr, shapes[i], p_vertices, p_indices);
} break;
case PhysicsServer::SHAPE_BOX: {
Vector3 extents = data;
Array arr;
arr.resize(RS::ARRAY_MAX);
CubeMesh::create_mesh_array(arr, extents * 2.0);
_add_mesh_array(arr, shapes[i], p_vertices, p_indices);
} break;
case PhysicsServer::SHAPE_CAPSULE: {
Dictionary dict = data;
real_t radius = dict["radius"];
real_t height = dict["height"];
Array arr;
arr.resize(RS::ARRAY_MAX);
CapsuleMesh::create_mesh_array(arr, radius, height * 0.5);
_add_mesh_array(arr, shapes[i], p_vertices, p_indices);
} break;
case PhysicsServer::SHAPE_CYLINDER: {
Dictionary dict = data;
real_t radius = dict["radius"];
real_t height = dict["height"];
Array arr;
arr.resize(RS::ARRAY_MAX);
CylinderMesh::create_mesh_array(arr, radius, radius, height);
_add_mesh_array(arr, shapes[i], p_vertices, p_indices);
} break;
case PhysicsServer::SHAPE_CONVEX_POLYGON: {
PoolVector3Array vertices = data;
Geometry::MeshData md;
Error err = ConvexHullComputer::convex_hull(vertices, md);
if (err == OK) {
PoolVector3Array faces;
for (int j = 0; j < md.faces.size(); ++j) {
Geometry::MeshData::Face face = md.faces[j];
for (int k = 2; k < face.indices.size(); ++k) {
faces.push_back(md.vertices[face.indices[0]]);
faces.push_back(md.vertices[face.indices[k - 1]]);
faces.push_back(md.vertices[face.indices[k]]);
}
}
_add_faces(faces, shapes[i], p_vertices, p_indices);
}
} break;
case PhysicsServer::SHAPE_CONCAVE_POLYGON: {
PoolVector3Array faces = data;
_add_faces(faces, shapes[i], p_vertices, p_indices);
} break;
case PhysicsServer::SHAPE_HEIGHTMAP: {
Dictionary dict = data;
///< dict( int:"width", int:"depth",float:"cell_size", float_array:"heights"
int heightmap_depth = dict["depth"];
int heightmap_width = dict["width"];
if (heightmap_depth >= 2 && heightmap_width >= 2) {
const PoolRealArray &map_data = dict["heights"];
Vector2 heightmap_gridsize(heightmap_width - 1, heightmap_depth - 1);
Vector2 start = heightmap_gridsize * -0.5;
PoolVector3Array vertex_array;
vertex_array.resize((heightmap_depth - 1) * (heightmap_width - 1) * 6);
int map_data_current_index = 0;
for (int d = 0; d < heightmap_depth - 1; d++) {
for (int w = 0; w < heightmap_width - 1; w++) {
if (map_data_current_index + 1 + heightmap_depth < map_data.size()) {
float top_left_height = map_data[map_data_current_index];
float top_right_height = map_data[map_data_current_index + 1];
float bottom_left_height = map_data[map_data_current_index + heightmap_depth];
float bottom_right_height = map_data[map_data_current_index + 1 + heightmap_depth];
Vector3 top_left = Vector3(start.x + w, top_left_height, start.y + d);
Vector3 top_right = Vector3(start.x + w + 1.0, top_right_height, start.y + d);
Vector3 bottom_left = Vector3(start.x + w, bottom_left_height, start.y + d + 1.0);
Vector3 bottom_right = Vector3(start.x + w + 1.0, bottom_right_height, start.y + d + 1.0);
vertex_array.push_back(top_right);
vertex_array.push_back(bottom_left);
vertex_array.push_back(top_left);
vertex_array.push_back(top_right);
vertex_array.push_back(bottom_right);
vertex_array.push_back(bottom_left);
}
map_data_current_index += 1;
}
}
if (vertex_array.size() > 0) {
_add_faces(vertex_array, shapes[i], p_vertices, p_indices);
}
}
} break;
default: {
WARN_PRINT("Unsupported collision shape type.");
} break;
}
}
}
}
#endif
if (p_recurse_children) {
for (int i = 0; i < p_node->get_child_count(); i++) {
_parse_geometry(p_navmesh_xform, p_node->get_child(i), p_vertices, p_indices, p_generate_from, p_collision_mask, p_recurse_children);