diff options
author | SaracenOne <SaracenOne@gmail.com> | 2022-09-07 20:13:10 +0100 |
---|---|---|
committer | SaracenOne <SaracenOne@gmail.com> | 2022-09-14 02:46:10 +0100 |
commit | 13f5c6212432bcc611364ca5df7fd67da37e3929 (patch) | |
tree | b4d48fe8bfdfa2670bac5446f8d0a7bd3725d156 /editor/import | |
parent | 200b9cde88da485659a6376266af8e9bf7be186f (diff) |
Fix LOD-generation on skinned meshes.
Diffstat (limited to 'editor/import')
-rw-r--r-- | editor/import/resource_importer_scene.cpp | 36 | ||||
-rw-r--r-- | editor/import/resource_importer_scene.h | 2 |
2 files changed, 37 insertions, 1 deletions
diff --git a/editor/import/resource_importer_scene.cpp b/editor/import/resource_importer_scene.cpp index 41061c3fc3..b5798a5784 100644 --- a/editor/import/resource_importer_scene.cpp +++ b/editor/import/resource_importer_scene.cpp @@ -1891,6 +1891,39 @@ void ResourceImporterScene::_replace_owner(Node *p_node, Node *p_scene, Node *p_ } } +Array ResourceImporterScene::_get_skinned_pose_transforms(ImporterMeshInstance3D *p_src_mesh_node) { + Array skin_pose_transform_array; + + const Ref<Skin> skin = p_src_mesh_node->get_skin(); + if (skin.is_valid()) { + NodePath skeleton_path = p_src_mesh_node->get_skeleton_path(); + const Node *node = p_src_mesh_node->get_node_or_null(skeleton_path); + const Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(node); + if (skeleton) { + int bind_count = skin->get_bind_count(); + + for (int i = 0; i < bind_count; i++) { + Transform3D bind_pose = skin->get_bind_pose(i); + String bind_name = skin->get_bind_name(i); + + int bone_idx = bind_name.is_empty() ? skin->get_bind_bone(i) : skeleton->find_bone(bind_name); + ERR_FAIL_COND_V(bone_idx >= skeleton->get_bone_count(), Array()); + + Transform3D bp_global_rest; + if (bone_idx >= 0) { + bp_global_rest = skeleton->get_bone_global_pose(bone_idx); + } else { + bp_global_rest = skeleton->get_bone_global_pose(i); + } + + skin_pose_transform_array.push_back(bp_global_rest * bind_pose); + } + } + } + + return skin_pose_transform_array; +} + void ResourceImporterScene::_generate_meshes(Node *p_node, const Dictionary &p_mesh_data, bool p_generate_lods, bool p_create_shadow_meshes, LightBakeMode p_light_bake_mode, float p_lightmap_texel_size, const Vector<uint8_t> &p_src_lightmap_cache, Vector<Vector<uint8_t>> &r_lightmap_caches) { ImporterMeshInstance3D *src_mesh_node = Object::cast_to<ImporterMeshInstance3D>(p_node); if (src_mesh_node) { @@ -2007,7 +2040,8 @@ void ResourceImporterScene::_generate_meshes(Node *p_node, const Dictionary &p_m } if (generate_lods) { - src_mesh_node->get_mesh()->generate_lods(merge_angle, split_angle); + Array skin_pose_transform_array = _get_skinned_pose_transforms(src_mesh_node); + src_mesh_node->get_mesh()->generate_lods(merge_angle, split_angle, skin_pose_transform_array); } if (create_shadow_meshes) { diff --git a/editor/import/resource_importer_scene.h b/editor/import/resource_importer_scene.h index da37893cc5..77bc06533c 100644 --- a/editor/import/resource_importer_scene.h +++ b/editor/import/resource_importer_scene.h @@ -34,6 +34,7 @@ #include "core/error/error_macros.h" #include "core/io/resource_importer.h" #include "core/variant/dictionary.h" +#include "scene/3d/importer_mesh_instance_3d.h" #include "scene/3d/node_3d.h" #include "scene/resources/animation.h" #include "scene/resources/mesh.h" @@ -208,6 +209,7 @@ class ResourceImporterScene : public ResourceImporter { SHAPE_TYPE_CAPSULE, }; + Array _get_skinned_pose_transforms(ImporterMeshInstance3D *p_src_mesh_node); void _replace_owner(Node *p_node, Node *p_scene, Node *p_new_owner); void _generate_meshes(Node *p_node, const Dictionary &p_mesh_data, bool p_generate_lods, bool p_create_shadow_meshes, LightBakeMode p_light_bake_mode, float p_lightmap_texel_size, const Vector<uint8_t> &p_src_lightmap_cache, Vector<Vector<uint8_t>> &r_lightmap_caches); void _add_shapes(Node *p_node, const Vector<Ref<Shape3D>> &p_shapes); |