Skip to content

Commit 15d234b

Browse files
authored
GLTF anim correctly inherits world transform (#5206)
1 parent 3f6d67c commit 15d234b

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/rmodels.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4261,9 +4261,10 @@ static void BuildPoseFromParentJoints(BoneInfo *bones, int boneCount, Transform
42614261
continue;
42624262
}
42634263
transforms[i].rotation = QuaternionMultiply(transforms[bones[i].parent].rotation, transforms[i].rotation);
4264+
transforms[i].scale = Vector3Multiply(transforms[i].scale, transforms[bones[i].parent].scale);
4265+
transforms[i].translation = Vector3Multiply(transforms[i].translation, transforms[bones[i].parent].scale);
42644266
transforms[i].translation = Vector3RotateByQuaternion(transforms[i].translation, transforms[bones[i].parent].rotation);
42654267
transforms[i].translation = Vector3Add(transforms[i].translation, transforms[bones[i].parent].translation);
4266-
transforms[i].scale = Vector3Multiply(transforms[i].scale, transforms[bones[i].parent].scale);
42674268
}
42684269
}
42694270
}
@@ -6238,6 +6239,20 @@ static ModelAnimation *LoadModelAnimationsGLTF(const char *fileName, int *animCo
62386239
*animCount = (int)data->animations_count;
62396240
animations = (ModelAnimation *)RL_CALLOC(data->animations_count, sizeof(ModelAnimation));
62406241

6242+
Transform worldTransform;
6243+
{
6244+
cgltf_float cgltf_worldTransform[16];
6245+
cgltf_node* node = skin.joints[0];
6246+
cgltf_node_transform_world(node->parent, cgltf_worldTransform);
6247+
Matrix worldMatrix = {
6248+
cgltf_worldTransform[0], cgltf_worldTransform[4], cgltf_worldTransform[8], cgltf_worldTransform[12],
6249+
cgltf_worldTransform[1], cgltf_worldTransform[5], cgltf_worldTransform[9], cgltf_worldTransform[13],
6250+
cgltf_worldTransform[2], cgltf_worldTransform[6], cgltf_worldTransform[10], cgltf_worldTransform[14],
6251+
cgltf_worldTransform[3], cgltf_worldTransform[7], cgltf_worldTransform[11], cgltf_worldTransform[15]
6252+
};
6253+
MatrixDecompose(worldMatrix, &(worldTransform.translation), &(worldTransform.rotation), &(worldTransform.scale));
6254+
}
6255+
62416256
for (unsigned int i = 0; i < data->animations_count; i++)
62426257
{
62436258
animations[i].bones = LoadBoneInfoGLTF(skin, &animations[i].boneCount);
@@ -6356,6 +6371,13 @@ static ModelAnimation *LoadModelAnimationsGLTF(const char *fileName, int *animCo
63566371
};
63576372
}
63586373

6374+
Transform* root = &animations[i].framePoses[j][0];
6375+
root->rotation = QuaternionMultiply(worldTransform.rotation, root->rotation);
6376+
root->scale = Vector3Multiply(root->scale, worldTransform.scale);
6377+
root->translation = Vector3Multiply(root->translation, worldTransform.scale);
6378+
root->translation = Vector3RotateByQuaternion(root->translation, worldTransform.rotation);
6379+
root->translation = Vector3Add(root->translation, worldTransform.translation);
6380+
63596381
BuildPoseFromParentJoints(animations[i].bones, animations[i].boneCount, animations[i].framePoses[j]);
63606382
}
63616383

0 commit comments

Comments
 (0)