Skip to content

Commit 8808a39

Browse files
[rmodels] Added implementation of UpdateModelAnimationBonesWithBlending() function
Signed-off-by: Kirandeep-Singh-Khehra <[email protected]>
1 parent 602583b commit 8808a39

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/raylib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,7 @@ RLAPI void SetModelMeshMaterial(Model *model, int meshId, int materialId);
16051605
RLAPI ModelAnimation *LoadModelAnimations(const char *fileName, int *animCount); // Load model animations from file
16061606
RLAPI void UpdateModelAnimation(Model model, ModelAnimation anim, int frame); // Update model animation pose (CPU)
16071607
RLAPI void UpdateModelAnimationBones(Model model, ModelAnimation anim, int frame); // Update model animation mesh bone matrices (GPU skinning)
1608+
RLAPI void UpdateModelAnimationBonesWithBlending(Model model, ModelAnimation animA, int frameA, ModelAnimation animB, int frameB, float blendFactor); // Update model animation mesh bone matrices with blending between two poses(GPU skinning)
16081609
RLAPI void UnloadModelAnimation(ModelAnimation anim); // Unload animation data
16091610
RLAPI void UnloadModelAnimations(ModelAnimation *animations, int animCount); // Unload animation array data
16101611
RLAPI bool IsModelAnimationValid(Model model, ModelAnimation anim); // Check model animation skeleton match

src/rmodels.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,6 +2308,65 @@ void UpdateModelAnimationBones(Model model, ModelAnimation anim, int frame)
23082308
}
23092309
}
23102310

2311+
// Update model animated bones transform matrices by blendin between two different given frames of different ModelAnimation(could be same too)
2312+
// NOTE: Updated data is not uploaded to GPU but kept at model.meshes[i].boneMatrices[boneId],
2313+
// to be uploaded to shader at drawing, in case GPU skinning is enabled
2314+
void UpdateModelAnimationBonesWithBlending(Model model, ModelAnimation animA, int frameA, ModelAnimation animB, int frameB, float blendFactor)
2315+
{
2316+
if ((animA.frameCount > 0) && (animA.bones != NULL) && (animA.framePoses != NULL) &&
2317+
(animB.frameCount > 0) && (animB.bones != NULL) && (animB.framePoses != NULL) &&
2318+
(blendFactor >= 0.0f) && (blendFactor <= 1.0))
2319+
{
2320+
frameA = frameA % animA.frameCount;
2321+
frameB = frameB % animB.frameCount;
2322+
2323+
for (int i = 0; i < model.meshCount; i++)
2324+
{
2325+
if (model.meshes[i].boneMatrices)
2326+
{
2327+
assert(model.meshes[i].boneCount == animA.boneCount);
2328+
assert(model.meshes[i].boneCount == animB.boneCount);
2329+
2330+
for (int boneId = 0; boneId < model.meshes[i].boneCount; boneId++)
2331+
{
2332+
Vector3 inTranslation = model.bindPose[boneId].translation;
2333+
Quaternion inRotation = model.bindPose[boneId].rotation;
2334+
Vector3 inScale = model.bindPose[boneId].scale;
2335+
2336+
Vector3 outATranslation = animA.framePoses[frameA][boneId].translation;
2337+
Quaternion outARotation = animA.framePoses[frameA][boneId].rotation;
2338+
Vector3 outAScale = animA.framePoses[frameA][boneId].scale;
2339+
2340+
Vector3 outBTranslation = animB.framePoses[frameB][boneId].translation;
2341+
Quaternion outBRotation = animB.framePoses[frameB][boneId].rotation;
2342+
Vector3 outBScale = animB.framePoses[frameB][boneId].scale;
2343+
2344+
Vector3 outTranslation = Vector3Lerp(outATranslation, outBTranslation, blendFactor);
2345+
Quaternion outRotation = QuaternionSlerp(outARotation, outBRotation, blendFactor);
2346+
Vector3 outScale = Vector3Lerp(outAScale, outBScale, blendFactor);
2347+
2348+
Vector3 invTranslation = Vector3RotateByQuaternion(Vector3Negate(inTranslation), QuaternionInvert(inRotation));
2349+
Quaternion invRotation = QuaternionInvert(inRotation);
2350+
Vector3 invScale = Vector3Divide((Vector3){ 1.0f, 1.0f, 1.0f }, inScale);
2351+
2352+
Vector3 boneTranslation = Vector3Add(
2353+
Vector3RotateByQuaternion(Vector3Multiply(outScale, invTranslation),
2354+
outRotation), outTranslation);
2355+
Quaternion boneRotation = QuaternionMultiply(outRotation, invRotation);
2356+
Vector3 boneScale = Vector3Multiply(outScale, invScale);
2357+
2358+
Matrix boneMatrix = MatrixMultiply(MatrixMultiply(
2359+
QuaternionToMatrix(boneRotation),
2360+
MatrixTranslate(boneTranslation.x, boneTranslation.y, boneTranslation.z)),
2361+
MatrixScale(boneScale.x, boneScale.y, boneScale.z));
2362+
2363+
model.meshes[i].boneMatrices[boneId] = boneMatrix;
2364+
}
2365+
}
2366+
}
2367+
}
2368+
}
2369+
23112370
// at least 2x speed up vs the old method
23122371
// Update model animated vertex data (positions and normals) for a given frame
23132372
// NOTE: Updated data is uploaded to GPU

0 commit comments

Comments
 (0)