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

Fixed regression of skinning with skeleton #52462

Merged
merged 1 commit into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
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
12 changes: 5 additions & 7 deletions editor/plugins/node_3d_editor_gizmos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2097,7 +2097,6 @@ void Skeleton3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
Color bonecolor = Color(1.0, 0.4, 0.4, 0.3);
Color rootcolor = Color(0.4, 1.0, 0.4, 0.1);

//LocalVector<int> bones_to_process = skel->get_parentless_bones();
LocalVector<int> bones_to_process;
bones_to_process = skel->get_parentless_bones();

Expand All @@ -2109,19 +2108,18 @@ void Skeleton3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
child_bones_vector = skel->get_bone_children(current_bone_idx);
int child_bones_size = child_bones_vector.size();

// You have children but no parent, then you must be a root/parentless bone.
if (child_bones_size >= 0 && skel->get_bone_parent(current_bone_idx) <= 0) {
grests[current_bone_idx] = skel->global_pose_to_local_pose(current_bone_idx, skel->get_bone_global_pose(current_bone_idx));
if (skel->get_bone_parent(current_bone_idx) < 0) {
grests[current_bone_idx] = skel->get_bone_rest(current_bone_idx);
}

for (int i = 0; i < child_bones_size; i++) {
int child_bone_idx = child_bones_vector[i];

grests[child_bone_idx] = skel->global_pose_to_local_pose(child_bone_idx, skel->get_bone_global_pose(child_bone_idx));
grests[child_bone_idx] = grests[current_bone_idx] * skel->get_bone_rest(child_bone_idx);
Vector3 v0 = grests[current_bone_idx].origin;
Vector3 v1 = grests[child_bone_idx].origin;
Vector3 d = skel->get_bone_rest(child_bone_idx).origin.normalized();
real_t dist = skel->get_bone_rest(child_bone_idx).origin.length();
Vector3 d = (v1 - v0).normalized();
real_t dist = v0.distance_to(v1);

// Find closest axis.
int closest = -1;
Expand Down
22 changes: 14 additions & 8 deletions scene/3d/skeleton_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -896,19 +896,25 @@ Ref<SkinReference> Skeleton3D::register_skin(const Ref<Skin> &p_skin) {
int len = bones.size();

// calculate global rests and invert them
Vector<int> bones_to_process = get_parentless_bones();
LocalVector<int> bones_to_process;
bones_to_process = get_parentless_bones();
while (bones_to_process.size() > 0) {
int current_bone_idx = bones_to_process[0];
bones_to_process.erase(current_bone_idx);
const Bone &b = bonesptr[current_bone_idx];

// Note: the code below may not work by default. May need to track an integer for the bone pose index order
// in the while loop, instead of using current_bone_idx.
if (b.parent >= 0) {
skin->set_bind_pose(current_bone_idx, skin->get_bind_pose(b.parent) * b.rest);
} else {
bones_to_process.erase(current_bone_idx);
LocalVector<int> child_bones_vector;
child_bones_vector = get_bone_children(current_bone_idx);
int child_bones_size = child_bones_vector.size();
if (b.parent < 0) {
skin->set_bind_pose(current_bone_idx, b.rest);
}
for (int i = 0; i < child_bones_size; i++) {
int child_bone_idx = child_bones_vector[i];
const Bone &cb = bonesptr[child_bone_idx];
skin->set_bind_pose(child_bone_idx, skin->get_bind_pose(current_bone_idx) * cb.rest);
// Add the bone's children to the list of bones to be processed.
bones_to_process.push_back(child_bones_vector[i]);
}
}

for (int i = 0; i < len; i++) {
Expand Down