From 4d8e6fdd2d916296ad2ec6b7e7d8391ef3bdae26 Mon Sep 17 00:00:00 2001 From: Pawel Lampe Date: Sun, 30 Jan 2022 11:30:17 +0100 Subject: [PATCH] Fix transforms involved into navmesh baking fixes #57231, fixes #57421 Within the context of parsing navigation geometry, this commit: - added missing transform of `MultiMeshInstance` - changed all transforms to global ones so that they don't need to be calculated by hand --- .../navigation/navigation_mesh_generator.cpp | 23 ++++++++----------- .../navigation/navigation_mesh_generator.h | 2 +- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/modules/navigation/navigation_mesh_generator.cpp b/modules/navigation/navigation_mesh_generator.cpp index 978d9612447..789d559c1a1 100644 --- a/modules/navigation/navigation_mesh_generator.cpp +++ b/modules/navigation/navigation_mesh_generator.cpp @@ -141,12 +141,12 @@ void NavigationMeshGenerator::_add_faces(const PoolVector3Array &p_faces, const } } -void NavigationMeshGenerator::_parse_geometry(Transform p_accumulated_transform, Node *p_node, Vector &p_vertices, Vector &p_indices, int p_generate_from, uint32_t p_collision_mask, bool p_recurse_children) { +void NavigationMeshGenerator::_parse_geometry(const Transform &p_navmesh_xform, Node *p_node, Vector &p_vertices, Vector &p_indices, int p_generate_from, uint32_t p_collision_mask, bool p_recurse_children) { if (Object::cast_to(p_node) && p_generate_from != NavigationMesh::PARSED_GEOMETRY_STATIC_COLLIDERS) { MeshInstance *mesh_instance = Object::cast_to(p_node); Ref mesh = mesh_instance->get_mesh(); if (mesh.is_valid()) { - _add_mesh(mesh, p_accumulated_transform * mesh_instance->get_transform(), p_vertices, p_indices); + _add_mesh(mesh, p_navmesh_xform * mesh_instance->get_global_transform(), p_vertices, p_indices); } } @@ -160,7 +160,7 @@ void NavigationMeshGenerator::_parse_geometry(Transform p_accumulated_transform, n = multimesh->get_instance_count(); } for (int i = 0; i < n; i++) { - _add_mesh(mesh, p_accumulated_transform * multimesh->get_instance_transform(i), p_vertices, p_indices); + _add_mesh(mesh, p_navmesh_xform * multimesh_instance->get_global_transform() * multimesh->get_instance_transform(i), p_vertices, p_indices); } } } @@ -172,7 +172,7 @@ void NavigationMeshGenerator::_parse_geometry(Transform p_accumulated_transform, if (!meshes.empty()) { Ref mesh = meshes[1]; if (mesh.is_valid()) { - _add_mesh(mesh, p_accumulated_transform * csg_shape->get_transform(), p_vertices, p_indices); + _add_mesh(mesh, p_navmesh_xform * csg_shape->get_global_transform(), p_vertices, p_indices); } } } @@ -187,7 +187,7 @@ void NavigationMeshGenerator::_parse_geometry(Transform p_accumulated_transform, if (Object::cast_to(child)) { CollisionShape *col_shape = Object::cast_to(child); - Transform transform = p_accumulated_transform * static_body->get_transform() * col_shape->get_transform(); + Transform transform = p_navmesh_xform * static_body->get_global_transform() * col_shape->get_transform(); Ref mesh; Ref s = col_shape->get_shape(); @@ -271,12 +271,12 @@ void NavigationMeshGenerator::_parse_geometry(Transform p_accumulated_transform, if (gridmap) { if (p_generate_from != NavigationMesh::PARSED_GEOMETRY_STATIC_COLLIDERS) { Array meshes = gridmap->get_meshes(); - Transform xform = gridmap->get_transform(); + Transform xform = gridmap->get_global_transform(); for (int i = 0; i < meshes.size(); i += 2) { Ref mesh = meshes[i + 1]; if (mesh.is_valid()) { Transform mesh_xform = meshes[i]; - _add_mesh(mesh, p_accumulated_transform * xform * mesh_xform, p_vertices, p_indices); + _add_mesh(mesh, p_navmesh_xform * xform * mesh_xform, p_vertices, p_indices); } } } @@ -365,14 +365,9 @@ void NavigationMeshGenerator::_parse_geometry(Transform p_accumulated_transform, } #endif - if (Object::cast_to(p_node)) { - Spatial *spatial = Object::cast_to(p_node); - p_accumulated_transform = p_accumulated_transform * spatial->get_transform(); - } - if (p_recurse_children) { for (int i = 0; i < p_node->get_child_count(); i++) { - _parse_geometry(p_accumulated_transform, p_node->get_child(i), p_vertices, p_indices, p_generate_from, p_collision_mask, p_recurse_children); + _parse_geometry(p_navmesh_xform, p_node->get_child(i), p_vertices, p_indices, p_generate_from, p_collision_mask, p_recurse_children); } } } @@ -606,7 +601,7 @@ void NavigationMeshGenerator::bake(Ref p_nav_mesh, Node *p_node) p_node->get_tree()->get_nodes_in_group(p_nav_mesh->get_source_group_name(), &parse_nodes); } - Transform navmesh_xform = Object::cast_to(p_node)->get_transform().affine_inverse(); + Transform navmesh_xform = Object::cast_to(p_node)->get_global_transform().affine_inverse(); for (const List::Element *E = parse_nodes.front(); E; E = E->next()) { NavigationMesh::ParsedGeometryType geometry_type = p_nav_mesh->get_parsed_geometry_type(); uint32_t collision_mask = p_nav_mesh->get_collision_mask(); diff --git a/modules/navigation/navigation_mesh_generator.h b/modules/navigation/navigation_mesh_generator.h index d733efa9464..fa42a7173b2 100644 --- a/modules/navigation/navigation_mesh_generator.h +++ b/modules/navigation/navigation_mesh_generator.h @@ -52,7 +52,7 @@ protected: static void _add_vertex(const Vector3 &p_vec3, Vector &p_vertices); static void _add_mesh(const Ref &p_mesh, const Transform &p_xform, Vector &p_vertices, Vector &p_indices); static void _add_faces(const PoolVector3Array &p_faces, const Transform &p_xform, Vector &p_vertices, Vector &p_indices); - static void _parse_geometry(Transform p_accumulated_transform, Node *p_node, Vector &p_vertices, Vector &p_indices, int p_generate_from, uint32_t p_collision_mask, bool p_recurse_children); + static void _parse_geometry(const Transform &p_navmesh_xform, Node *p_node, Vector &p_vertices, Vector &p_indices, int p_generate_from, uint32_t p_collision_mask, bool p_recurse_children); static void _convert_detail_mesh_to_native_navigation_mesh(const rcPolyMeshDetail *p_detail_mesh, Ref p_nav_mesh); static void _build_recast_navigation_mesh(