-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
[rmodels] Fix glTF skinning when joints have non-joint parent nodes #5876
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
Merged
raysan5
merged 2 commits into
raysan5:master
from
cseelhoff:fix-gltf-skinning-nonjoint-parents
May 29, 2026
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5375,16 +5375,18 @@ static BoneInfo *LoadBoneInfoGLTF(cgltf_skin skin, int *boneCount) | |
| cgltf_node node = *skin.joints[i]; | ||
| if (node.name != NULL) strncpy(bones[i].name, node.name, sizeof(bones[i].name) - 1); | ||
|
|
||
| // Find parent bone index | ||
| // Find parent bone index by walking up the node tree past any | ||
| // non-joint ancestors (intermediate transform nodes used by some | ||
| // DCC exporters), until we hit a node that is also in skin.joints. | ||
| int parentIndex = -1; | ||
|
|
||
| for (unsigned int j = 0; j < skin.joints_count; j++) | ||
| cgltf_node *ancestor = node.parent; | ||
| while (ancestor != NULL && parentIndex == -1) | ||
| { | ||
| if (skin.joints[j] == node.parent) | ||
| for (unsigned int j = 0; j < skin.joints_count; j++) | ||
| { | ||
| parentIndex = (int)j; | ||
| break; | ||
| if (skin.joints[j] == ancestor) { parentIndex = (int)j; break; } | ||
| } | ||
| if (parentIndex == -1) ancestor = ancestor->parent; | ||
|
raysan5 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| bones[i].parent = parentIndex; | ||
|
|
@@ -6518,17 +6520,33 @@ static ModelAnimation *LoadModelAnimationsGLTF(const char *fileName, int *animCo | |
| *animCount = (int)data->animations_count; | ||
| animations = (ModelAnimation *)RL_CALLOC(data->animations_count, sizeof(ModelAnimation)); | ||
|
|
||
| Transform worldTransform = { 0 }; | ||
| cgltf_float cgltf_worldTransform[16] = { 0 }; | ||
| cgltf_node *node = skin.joints[0]; | ||
| cgltf_node_transform_world(node->parent, cgltf_worldTransform); | ||
| Matrix worldMatrix = { | ||
| cgltf_worldTransform[0], cgltf_worldTransform[4], cgltf_worldTransform[8], cgltf_worldTransform[12], | ||
| cgltf_worldTransform[1], cgltf_worldTransform[5], cgltf_worldTransform[9], cgltf_worldTransform[13], | ||
| cgltf_worldTransform[2], cgltf_worldTransform[6], cgltf_worldTransform[10], cgltf_worldTransform[14], | ||
| cgltf_worldTransform[3], cgltf_worldTransform[7], cgltf_worldTransform[11], cgltf_worldTransform[15] | ||
| }; | ||
| MatrixDecompose(worldMatrix, &worldTransform.translation, &worldTransform.rotation, &worldTransform.scale); | ||
| // Precompute, per joint, the static transform contributed by any | ||
| // intermediate non-joint nodes between the joint and its nearest | ||
| // joint ancestor. This handles exporters (e.g. wow.export) that | ||
| // store bone offsets on dummy parent nodes rather than on the | ||
| // joints themselves. Depends only on the skin, not the animation. | ||
| int jointCount = (int)skin.joints_count; | ||
| Matrix *extOffset = (Matrix *)RL_MALLOC(jointCount*sizeof(Matrix)); | ||
|
raysan5 marked this conversation as resolved.
|
||
| for (int k = 0; k < jointCount; k++) | ||
| { | ||
| extOffset[k] = MatrixIdentity(); | ||
| cgltf_node *n = skin.joints[k]->parent; | ||
| while (n != NULL) | ||
| { | ||
| bool isJoint = false; | ||
| for (int jj = 0; jj < jointCount; jj++) | ||
| { | ||
| if (skin.joints[jj] == n) { isJoint = true; break; } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Formatting |
||
| } | ||
| if (isJoint) break; | ||
| Matrix nm = MatrixMultiply(MatrixMultiply( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Formatting or try to write this more readable. |
||
| MatrixScale(n->scale[0], n->scale[1], n->scale[2]), | ||
| QuaternionToMatrix((Quaternion){ n->rotation[0], n->rotation[1], n->rotation[2], n->rotation[3] })), | ||
| MatrixTranslate(n->translation[0], n->translation[1], n->translation[2])); | ||
| extOffset[k] = MatrixMultiply(extOffset[k], nm); | ||
| n = n->parent; | ||
| } | ||
| } | ||
|
|
||
| for (unsigned int a = 0; a < data->animations_count; a++) | ||
| { | ||
|
|
@@ -6637,27 +6655,29 @@ static ModelAnimation *LoadModelAnimationsGLTF(const char *fileName, int *animCo | |
| } | ||
| } | ||
|
|
||
| animations[a].keyframePoses[j][k] = (Transform){ | ||
| .translation = translation, | ||
| .rotation = rotation, | ||
| .scale = scale | ||
| }; | ||
| // Compose joint local TRS, then prepend the static | ||
| // intermediate non-joint offsets so the final TRS is | ||
| // expressed relative to the joint's skeleton parent. | ||
| Matrix S = MatrixScale(scale.x, scale.y, scale.z); | ||
| Matrix R = QuaternionToMatrix(rotation); | ||
| Matrix T = MatrixTranslate(translation.x, translation.y, translation.z); | ||
| Matrix jointLocal = MatrixMultiply(MatrixMultiply(S, R), T); | ||
| Matrix combined = MatrixMultiply(jointLocal, extOffset[k]); | ||
|
|
||
| Transform tr; | ||
| MatrixDecompose(combined, &tr.translation, &tr.rotation, &tr.scale); | ||
| animations[a].keyframePoses[j][k] = tr; | ||
| } | ||
|
|
||
| Transform *root = &animations[a].keyframePoses[j][0]; | ||
| root->rotation = QuaternionMultiply(worldTransform.rotation, root->rotation); | ||
| root->scale = Vector3Multiply(root->scale, worldTransform.scale); | ||
| root->translation = Vector3Multiply(root->translation, worldTransform.scale); | ||
| root->translation = Vector3RotateByQuaternion(root->translation, worldTransform.rotation); | ||
| root->translation = Vector3Add(root->translation, worldTransform.translation); | ||
|
|
||
| BuildPoseFromParentJoints(bones, animations[a].boneCount, animations[a].keyframePoses[j]); | ||
| } | ||
|
|
||
| TRACELOG(LOG_INFO, "MODEL: [%s] Loaded animation: %s | Frames: %d | Duration: %fs", fileName, (animData.name != NULL)? animData.name : "NULL", animations[a].keyframeCount, animDuration); | ||
| RL_FREE(boneChannels); | ||
| RL_FREE(bones); | ||
| } | ||
|
|
||
| RL_FREE(extOffset); | ||
| } | ||
|
|
||
| if (data->skins_count > 1) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.