mirror of
https://github.com/raysan5/raylib.git
synced 2026-02-21 04:59:17 -05:00
[rmodels] Added implementation of UpdateModelAnimationBonesWithBlending() function (#4578)
* [rmodels] Added implementation of `UpdateModelAnimationBonesWithBlending()` function Signed-off-by: Kirandeep-Singh-Khehra <kirandeepsinghkhehra@gmail.com> * [rmodels] Added example for animation blending and fixed wrap issue for blend factor Signed-off-by: Kirandeep-Singh-Khehra <kirandeepsinghkhehra@gmail.com> * [rmodels] Updated build information for animation blending example Signed-off-by: Kirandeep-Singh-Khehra <kirandeepsinghkhehra@gmail.com> * [rmodels] Fixed typos in anmation blending example Signed-off-by: Kirandeep-Singh-Khehra <kirandeepsinghkhehra@gmail.com> * [rmodels] Updated blend function signature and added function to update verts from bones Signed-off-by: Kirandeep-Singh-Khehra <kirandeepsinghkhehra@gmail.com> * [rmodels] Updated documentation Signed-off-by: Kirandeep-Singh-Khehra <kirandeepsinghkhehra@gmail.com> * rlparser: update raylib_api.* by CI * rlparser: update raylib_api.* by CI --------- Signed-off-by: Kirandeep-Singh-Khehra <kirandeepsinghkhehra@gmail.com> Co-authored-by: Ray <raysan5@gmail.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
97023def48
commit
0e6cb0993d
@ -2346,12 +2346,70 @@ void UpdateModelAnimationBones(Model model, ModelAnimation anim, int frame)
|
||||
}
|
||||
}
|
||||
|
||||
// at least 2x speed up vs the old method
|
||||
// Update model animated vertex data (positions and normals) for a given frame
|
||||
// NOTE: Updated data is uploaded to GPU
|
||||
void UpdateModelAnimation(Model model, ModelAnimation anim, int frame)
|
||||
// Update model animated bones transform matrices by interpolating between two different given frames of different ModelAnimation(could be same too)
|
||||
// NOTE: Updated data is not uploaded to GPU but kept at model.meshes[i].boneMatrices[boneId],
|
||||
// to be uploaded to shader at drawing, in case GPU skinning is enabled
|
||||
void UpdateModelAnimationBonesLerp(Model model, ModelAnimation animA, int frameA, ModelAnimation animB, int frameB, float value)
|
||||
{
|
||||
UpdateModelAnimationBones(model,anim,frame);
|
||||
if ((animA.frameCount > 0) && (animA.bones != NULL) && (animA.framePoses != NULL) &&
|
||||
(animB.frameCount > 0) && (animB.bones != NULL) && (animB.framePoses != NULL) &&
|
||||
(value >= 0.0f) && (value <= 1.0f))
|
||||
{
|
||||
frameA = frameA % animA.frameCount;
|
||||
frameB = frameB % animB.frameCount;
|
||||
|
||||
for (int i = 0; i < model.meshCount; i++)
|
||||
{
|
||||
if (model.meshes[i].boneMatrices)
|
||||
{
|
||||
assert(model.meshes[i].boneCount == animA.boneCount);
|
||||
assert(model.meshes[i].boneCount == animB.boneCount);
|
||||
|
||||
for (int boneId = 0; boneId < model.meshes[i].boneCount; boneId++)
|
||||
{
|
||||
Vector3 inTranslation = model.bindPose[boneId].translation;
|
||||
Quaternion inRotation = model.bindPose[boneId].rotation;
|
||||
Vector3 inScale = model.bindPose[boneId].scale;
|
||||
|
||||
Vector3 outATranslation = animA.framePoses[frameA][boneId].translation;
|
||||
Quaternion outARotation = animA.framePoses[frameA][boneId].rotation;
|
||||
Vector3 outAScale = animA.framePoses[frameA][boneId].scale;
|
||||
|
||||
Vector3 outBTranslation = animB.framePoses[frameB][boneId].translation;
|
||||
Quaternion outBRotation = animB.framePoses[frameB][boneId].rotation;
|
||||
Vector3 outBScale = animB.framePoses[frameB][boneId].scale;
|
||||
|
||||
Vector3 outTranslation = Vector3Lerp(outATranslation, outBTranslation, value);
|
||||
Quaternion outRotation = QuaternionSlerp(outARotation, outBRotation, value);
|
||||
Vector3 outScale = Vector3Lerp(outAScale, outBScale, value);
|
||||
|
||||
Vector3 invTranslation = Vector3RotateByQuaternion(Vector3Negate(inTranslation), QuaternionInvert(inRotation));
|
||||
Quaternion invRotation = QuaternionInvert(inRotation);
|
||||
Vector3 invScale = Vector3Divide((Vector3){ 1.0f, 1.0f, 1.0f }, inScale);
|
||||
|
||||
Vector3 boneTranslation = Vector3Add(
|
||||
Vector3RotateByQuaternion(Vector3Multiply(outScale, invTranslation),
|
||||
outRotation), outTranslation);
|
||||
Quaternion boneRotation = QuaternionMultiply(outRotation, invRotation);
|
||||
Vector3 boneScale = Vector3Multiply(outScale, invScale);
|
||||
|
||||
Matrix boneMatrix = MatrixMultiply(MatrixMultiply(
|
||||
QuaternionToMatrix(boneRotation),
|
||||
MatrixTranslate(boneTranslation.x, boneTranslation.y, boneTranslation.z)),
|
||||
MatrixScale(boneScale.x, boneScale.y, boneScale.z));
|
||||
|
||||
model.meshes[i].boneMatrices[boneId] = boneMatrix;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update model vertex data (positions and normals) from mesh bone data
|
||||
// NOTE: Updated data is uploaded to GPU
|
||||
void UpdateModelVertsToCurrentBones(Model model)
|
||||
{
|
||||
//UpdateModelAnimationBones(model, anim, frame); // TODO: Review
|
||||
|
||||
for (int m = 0; m < model.meshCount; m++)
|
||||
{
|
||||
@ -2416,6 +2474,15 @@ void UpdateModelAnimation(Model model, ModelAnimation anim, int frame)
|
||||
}
|
||||
}
|
||||
|
||||
// at least 2x speed up vs the old method
|
||||
// Update model animated vertex data (positions and normals) for a given frame
|
||||
// NOTE: Updated data is uploaded to GPU
|
||||
void UpdateModelAnimation(Model model, ModelAnimation anim, int frame)
|
||||
{
|
||||
UpdateModelAnimationBones(model,anim,frame);
|
||||
UpdateModelVertsToCurrentBones(model);
|
||||
}
|
||||
|
||||
// Unload animation array data
|
||||
void UnloadModelAnimations(ModelAnimation *animations, int animCount)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user