From 21a13649be834b76be78280e0ba9f4a6000e446f Mon Sep 17 00:00:00 2001 From: Andrea Catania Date: Sat, 24 Jul 2021 08:39:18 +0200 Subject: Fix Static & Rigid body generation on mesh import. The function `_gen_shape_list` is using the resource `Res`, but during the import phase the resource used is instead `Ref`. Note: the `Ref` is an intermediate resource, that will be used to create a `Res` at the end of the import process. `Ref` and `Ref` are not inheriting each other, so the internal cast done by `Ref<>` during the assignment, is always null: ```c++ Ref import_mesh(/* Assume it's initialized */); CRASH_NOW(import_mesh.is_null()); Ref mesh = import_mesh; CRASH_NOW(mesh.is_null()); // <--- Here we have a crash, since it's impossible perform the above cast, and the `mesh` is always null. ``` Here the full list of call to `_gen_shape_list`, where we can notice that a `Ref` is passed: - https://github.com/AndreaCatania/godot/blob/master/editor/import/resource_importer_scene.cpp#L428 - https://github.com/AndreaCatania/godot/blob/master/editor/import/resource_importer_scene.cpp#L454-L458 - https://github.com/AndreaCatania/godot/blob/master/editor/import/resource_importer_scene.cpp#L512-L516 As you can notice, we always pass the following mesh: `Ref mesh = mi->get_mesh();`. We already have the function `_pre_gen_shape_list` that executes the exact same job but using the correct type; Since there is no further usage of the function `_gen_shape_list` in the code base, I think it's just some leftover code, so I removed it entirely to use the proper function. --- editor/import/resource_importer_scene.cpp | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/editor/import/resource_importer_scene.cpp b/editor/import/resource_importer_scene.cpp index c14b948dae..1c79d4f078 100644 --- a/editor/import/resource_importer_scene.cpp +++ b/editor/import/resource_importer_scene.cpp @@ -233,21 +233,6 @@ static String _fixstr(const String &p_what, const String &p_str) { return what; } -static void _gen_shape_list(const Ref &mesh, List> &r_shape_list, bool p_convex) { - ERR_FAIL_NULL_MSG(mesh, "Cannot generate shape list with null mesh value"); - if (!p_convex) { - Ref shape = mesh->create_trimesh_shape(); - r_shape_list.push_back(shape); - } else { - Vector> cd = mesh->convex_decompose(); - if (cd.size()) { - for (int i = 0; i < cd.size(); i++) { - r_shape_list.push_back(cd[i]); - } - } - } -} - static void _pre_gen_shape_list(const Ref &mesh, List> &r_shape_list, bool p_convex) { ERR_FAIL_NULL_MSG(mesh, "Cannot generate shape list with null mesh value"); if (!p_convex) { @@ -425,7 +410,7 @@ Node *ResourceImporterScene::_pre_fix_node(Node *p_node, Node *p_root, Mapget_name(), "col")) { - _gen_shape_list(mesh, shapes, false); + _pre_gen_shape_list(mesh, shapes, false); collision_map[mesh] = shapes; mesh->set_name(_fixstr(mesh->get_name(), "col")); } else if (_teststr(mesh->get_name(), "convcol")) { - _gen_shape_list(mesh, shapes, true); + _pre_gen_shape_list(mesh, shapes, true); collision_map[mesh] = shapes; mesh->set_name(_fixstr(mesh->get_name(), "convcol")); } -- cgit v1.2.3