Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exporting MeshInstances without a Skeleton in the GLTF module #77545

Merged
merged 1 commit into from
Jun 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 10 additions & 18 deletions modules/gltf/gltf_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6308,29 +6308,21 @@ void GLTFDocument::_convert_mesh_instances(Ref<GLTFState> p_state) {
node->rotation = mi_xform.basis.get_rotation_quaternion();
node->position = mi_xform.origin;

Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(mi->get_node(mi->get_skeleton_path()));
if (!skeleton) {
continue;
}
if (!skeleton->get_bone_count()) {
Node *skel_node = mi->get_node_or_null(mi->get_skeleton_path());
Skeleton3D *godot_skeleton = Object::cast_to<Skeleton3D>(skel_node);
if (!godot_skeleton || godot_skeleton->get_bone_count() == 0) {
continue;
}
// At this point in the code, we know we have a Skeleton3D with at least one bone.
Ref<Skin> skin = mi->get_skin();
Ref<GLTFSkin> gltf_skin;
gltf_skin.instantiate();
Array json_joints;

NodePath skeleton_path = mi->get_skeleton_path();
Node *skel_node = mi->get_node_or_null(skeleton_path);
Skeleton3D *godot_skeleton = nullptr;
if (skel_node != nullptr) {
godot_skeleton = cast_to<Skeleton3D>(skel_node);
}
if (godot_skeleton != nullptr && p_state->skeleton3d_to_gltf_skeleton.has(godot_skeleton->get_instance_id())) {
if (p_state->skeleton3d_to_gltf_skeleton.has(godot_skeleton->get_instance_id())) {
// This is a skinned mesh. If the mesh has no ARRAY_WEIGHTS or ARRAY_BONES, it will be invisible.
const GLTFSkeletonIndex skeleton_gltf_i = p_state->skeleton3d_to_gltf_skeleton[godot_skeleton->get_instance_id()];
Ref<GLTFSkeleton> gltf_skeleton = p_state->skeletons[skeleton_gltf_i];
int bone_cnt = skeleton->get_bone_count();
int bone_cnt = godot_skeleton->get_bone_count();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the old code had both skeleton and godot_skeleton which were guaranteed to point to the same object. We have to pick one name, so I figured godot_skeleton is better since more it's distinguished from gltf_skeleton.

ERR_FAIL_COND(bone_cnt != gltf_skeleton->joints.size());

ObjectID gltf_skin_key;
Expand All @@ -6348,7 +6340,7 @@ void GLTFDocument::_convert_mesh_instances(Ref<GLTFState> p_state) {
} else {
if (skin.is_null()) {
// Note that gltf_skin_key should remain null, so these can share a reference.
skin = skeleton->create_skin_from_rest_transforms();
skin = godot_skeleton->create_skin_from_rest_transforms();
}
gltf_skin.instantiate();
gltf_skin->godot_skin = skin;
Expand All @@ -6358,7 +6350,7 @@ void GLTFDocument::_convert_mesh_instances(Ref<GLTFState> p_state) {
//gltf_state->godot_to_gltf_node[skel_node]
HashMap<StringName, int> bone_name_to_idx;
for (int bone_i = 0; bone_i < bone_cnt; bone_i++) {
bone_name_to_idx[skeleton->get_bone_name(bone_i)] = bone_i;
bone_name_to_idx[godot_skeleton->get_bone_name(bone_i)] = bone_i;
}
for (int bind_i = 0, cnt = skin->get_bind_count(); bind_i < cnt; bind_i++) {
int bone_i = skin->get_bind_bone(bind_i);
Expand All @@ -6369,13 +6361,13 @@ void GLTFDocument::_convert_mesh_instances(Ref<GLTFState> p_state) {
}
ERR_CONTINUE(bone_i < 0 || bone_i >= bone_cnt);
if (bind_name == StringName()) {
bind_name = skeleton->get_bone_name(bone_i);
bind_name = godot_skeleton->get_bone_name(bone_i);
}
GLTFNodeIndex skeleton_bone_i = gltf_skeleton->joints[bone_i];
gltf_skin->joints_original.push_back(skeleton_bone_i);
gltf_skin->joints.push_back(skeleton_bone_i);
gltf_skin->inverse_binds.push_back(bind_pose);
if (skeleton->get_bone_parent(bone_i) == -1) {
if (godot_skeleton->get_bone_parent(bone_i) == -1) {
gltf_skin->roots.push_back(skeleton_bone_i);
}
gltf_skin->joint_i_to_bone_i[bind_i] = bone_i;
Expand Down