mirror of
https://github.com/raysan5/raylib.git
synced 2026-04-14 02:59:09 -04:00
WARNING: BREAKING: REDESIGNED: **Animation System** #4606
REVIEWED: Reorganized structures for a clearer distinction between "skeleton", "skin" and "skinning" data ADDED: New structures: `ModelSkeleton`, `ModelAnimPose` (alias `Transform*`) ADDED: Runtime data `currentPose` and `boneMatrices` to `Model` structure ADDED: Support animation frames-blending, for timing control ADDED: Support animations blending, between two animations REVIEWED: All models animation loading functions ADDED: `UpdateModelAnimationEx()` for two animations blending REMOVED: `UpdateModelAnimationBones*()`, simplified API REVIEWED: Shader attributes/uniforms names for animations, for consistency REVIEWED: Multiple tweaks on animations loading for consistency between formats ADDED: example: `models_animation_timing` ADDED: example: `models_animation_blending` REVIEWED: example: `models_animation_gpu_skinning` REVIEWED: example: `models_animation_blend_custom` REVIEWED: All animated models loading examples
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
*
|
||||
* Example originally created with raylib 5.5, last time updated with raylib 5.5
|
||||
*
|
||||
* This example demonstrates per-bone animation blending, allowing smooth transitions
|
||||
* DETAILS: Example demonstrates per-bone animation blending, allowing smooth transitions
|
||||
* between two animations by interpolating bone transforms. This is useful for:
|
||||
* - Blending movement animations (walk/run) with action animations (jump/attack)
|
||||
* - Creating smooth animation transitions
|
||||
@ -27,8 +27,10 @@
|
||||
|
||||
#include "raymath.h"
|
||||
|
||||
#include <string.h> // Required for: memcpy()
|
||||
#include <stdlib.h> // Required for: NULL
|
||||
#include "rlgl.h" // Requried for: rlUpdateVertexBuffer() (CPU-skinning)
|
||||
|
||||
#include <string.h> // Required for: memcpy()
|
||||
#include <stdlib.h> // Required for: NULL
|
||||
|
||||
#if defined(PLATFORM_DESKTOP)
|
||||
#define GLSL_VERSION 330
|
||||
@ -40,8 +42,8 @@
|
||||
// Module Functions Declaration
|
||||
//------------------------------------------------------------------------------------
|
||||
static bool IsUpperBodyBone(const char *boneName);
|
||||
static void BlendModelAnimationsBones(Model *model, ModelAnimation *anim1, int frame1,
|
||||
ModelAnimation *anim2, int frame2, float blendFactor, bool upperBodyBlend);
|
||||
static void UpdateModelAnimationBones(Model *model, ModelAnimation *anim1, int frame1,
|
||||
ModelAnimation *anim2, int frame2, float blend, bool upperBodyBlend);
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
@ -57,51 +59,40 @@ int main(void)
|
||||
|
||||
// Define the camera to look into our 3d world
|
||||
Camera camera = { 0 };
|
||||
camera.position = (Vector3){ 5.0f, 5.0f, 5.0f }; // Camera position
|
||||
camera.target = (Vector3){ 0.0f, 2.0f, 0.0f }; // Camera looking at point
|
||||
camera.position = (Vector3){ 4.0f, 4.0f, 4.0f }; // Camera position
|
||||
camera.target = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera looking at point
|
||||
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
|
||||
camera.fovy = 45.0f; // Camera field-of-view Y
|
||||
camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
|
||||
|
||||
// Load gltf model
|
||||
Model characterModel = LoadModel("resources/models/gltf/greenman.glb");
|
||||
Model model = LoadModel("resources/models/gltf/greenman.glb");
|
||||
Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
|
||||
|
||||
// Load skinning shader
|
||||
// WARNING: GPU skinning must be enabled in raylib with a compilation flag,
|
||||
// if not enabled, CPU skinning will be used instead
|
||||
Shader skinningShader = LoadShader(TextFormat("resources/shaders/glsl%i/skinning.vs", GLSL_VERSION),
|
||||
TextFormat("resources/shaders/glsl%i/skinning.fs", GLSL_VERSION));
|
||||
|
||||
characterModel.materials[1].shader = skinningShader;
|
||||
model.materials[1].shader = skinningShader;
|
||||
|
||||
// Load gltf model animations
|
||||
int animsCount = 0;
|
||||
ModelAnimation *modelAnimations = LoadModelAnimations("resources/models/gltf/greenman.glb", &animsCount);
|
||||
int animCount = 0;
|
||||
ModelAnimation *anims = LoadModelAnimations("resources/models/gltf/greenman.glb", &animCount);
|
||||
|
||||
// Log all available animations for debugging
|
||||
TraceLog(LOG_INFO, "Found %d animations:", animsCount);
|
||||
for (int i = 0; i < animsCount; i++)
|
||||
{
|
||||
TraceLog(LOG_INFO, " Animation %d: %s (%d frames)", i, modelAnimations[i].name, modelAnimations[i].keyframeCount);
|
||||
}
|
||||
|
||||
// Use specific indices: walk/move = 2, attack = 3
|
||||
unsigned int animIndex1 = 2; // Walk/Move animation (index 2)
|
||||
unsigned int animIndex2 = 3; // Attack animation (index 3)
|
||||
// Use specific animation indices: 2-walk/move, 3-attack
|
||||
unsigned int animIndex0 = 2; // Walk/Move animation (index 2)
|
||||
unsigned int animIndex1 = 3; // Attack animation (index 3)
|
||||
unsigned int animCurrentFrame0 = 0;
|
||||
unsigned int animCurrentFrame1 = 0;
|
||||
unsigned int animCurrentFrame2 = 0;
|
||||
|
||||
// Validate indices
|
||||
if (animIndex1 >= animsCount) animIndex1 = 0;
|
||||
if (animIndex2 >= animsCount) animIndex2 = (animsCount > 1) ? 1 : 0;
|
||||
|
||||
TraceLog(LOG_INFO, "Using Walk (index %d): %s", animIndex1, modelAnimations[animIndex1].name);
|
||||
TraceLog(LOG_INFO, "Using Attack (index %d): %s", animIndex2, modelAnimations[animIndex2].name);
|
||||
if (animIndex0 >= animCount) animIndex0 = 0;
|
||||
if (animIndex1 >= animCount) animIndex1 = (animCount > 1) ? 1 : 0;
|
||||
|
||||
Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
|
||||
bool upperBodyBlend = true; // Toggle: true = upper/lower body blending, false = uniform blending (50/50)
|
||||
bool upperBodyBlend = true; // Toggle: true = upper/lower body blending, false = uniform blending (50/50)
|
||||
|
||||
DisableCursor(); // Limit cursor to relative movement inside the window
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
@ -109,24 +100,28 @@ int main(void)
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
UpdateCamera(&camera, CAMERA_THIRD_PERSON);
|
||||
UpdateCamera(&camera, CAMERA_ORBITAL);
|
||||
|
||||
// Toggle upper/lower body blending mode (SPACE key)
|
||||
if (IsKeyPressed(KEY_SPACE)) upperBodyBlend = !upperBodyBlend;
|
||||
|
||||
// Update animation frames
|
||||
ModelAnimation anim1 = modelAnimations[animIndex1];
|
||||
ModelAnimation anim2 = modelAnimations[animIndex2];
|
||||
ModelAnimation anim0 = anims[animIndex0];
|
||||
ModelAnimation anim1 = anims[animIndex1];
|
||||
|
||||
animCurrentFrame0 = (animCurrentFrame0 + 1)%anim0.keyframeCount;
|
||||
animCurrentFrame1 = (animCurrentFrame1 + 1)%anim1.keyframeCount;
|
||||
animCurrentFrame2 = (animCurrentFrame2 + 1)%anim2.keyframeCount;
|
||||
|
||||
// Blend the two animations
|
||||
characterModel.transform = MatrixTranslate(position.x, position.y, position.z);
|
||||
// When upperBodyBlend is ON: upper body = attack (1.0), lower body = walk (0.0)
|
||||
// When upperBodyBlend is OFF: uniform blend at 0.5 (50% walk, 50% attack)
|
||||
float blendFactor = upperBodyBlend ? 1.0f : 0.5f;
|
||||
BlendModelAnimationsBones(&characterModel, &anim1, animCurrentFrame1, &anim2, animCurrentFrame2, blendFactor, upperBodyBlend);
|
||||
float blendFactor = (upperBodyBlend? 1.0f : 0.5f);
|
||||
UpdateModelAnimationBones(&model, &anim0, animCurrentFrame0,
|
||||
&anim1, animCurrentFrame1, blendFactor, upperBodyBlend);
|
||||
|
||||
// raylib provided animation blending function
|
||||
//UpdateModelAnimationEx(model, anim0, (float)animCurrentFrame0,
|
||||
// anim1, (float)animCurrentFrame1, blendFactor);
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
@ -137,19 +132,18 @@ int main(void)
|
||||
|
||||
BeginMode3D(camera);
|
||||
|
||||
// Draw character mesh, pose calculation is done in shader (GPU skinning)
|
||||
DrawMesh(characterModel.meshes[0], characterModel.materials[1], characterModel.transform);
|
||||
DrawModel(model, position, 1.0f, WHITE);
|
||||
|
||||
DrawGrid(10, 1.0f);
|
||||
|
||||
EndMode3D();
|
||||
|
||||
// Draw UI
|
||||
DrawText("BONE BLENDING EXAMPLE", 10, 10, 20, DARKGRAY);
|
||||
DrawText(TextFormat("Walk (Animation 2): %s", anim1.name), 10, 35, 10, GRAY);
|
||||
DrawText(TextFormat("Attack (Animation 3): %s", anim2.name), 10, 50, 10, GRAY);
|
||||
DrawText(TextFormat("Mode: %s", upperBodyBlend ? "Upper/Lower Body Blending" : "Uniform Blending"), 10, 65, 10, GRAY);
|
||||
DrawText("SPACE - Toggle blending mode", 10, GetScreenHeight() - 20, 10, DARKGRAY);
|
||||
DrawText(TextFormat("ANIM 0: %s", anim0.name), 10, 10, 20, GRAY);
|
||||
DrawText(TextFormat("ANIM 1: %s", anim1.name), 10, 40, 20, GRAY);
|
||||
DrawText(TextFormat("[SPACE] Toggle blending mode: %s",
|
||||
upperBodyBlend? "Upper/Lower Body Blending" : "Uniform Blending"),
|
||||
10, GetScreenHeight() - 30, 20, DARKGRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
@ -157,8 +151,8 @@ int main(void)
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadModelAnimations(modelAnimations, animsCount); // Unload model animation
|
||||
UnloadModel(characterModel); // Unload model and meshes/material
|
||||
UnloadModelAnimations(anims, animCount); // Unload model animation
|
||||
UnloadModel(model); // Unload model and meshes/material
|
||||
UnloadShader(skinningShader); // Unload GPU skinning shader
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
@ -199,74 +193,138 @@ static bool IsUpperBodyBone(const char *boneName)
|
||||
}
|
||||
|
||||
// Blend two animations per-bone with selective upper/lower body blending
|
||||
static void BlendModelAnimationsBones(Model *model, ModelAnimation *anim1, int frame1,
|
||||
ModelAnimation *anim2, int frame2, float blendFactor, bool upperBodyBlend)
|
||||
static void UpdateModelAnimationBones(Model *model, ModelAnimation *anim0, int frame0,
|
||||
ModelAnimation *anim1, int frame1, float blend, bool upperBodyBlend)
|
||||
{
|
||||
// Validate inputs
|
||||
if (anim1->boneCount == 0 || anim1->keyframePoses == NULL ||
|
||||
anim2->boneCount == 0 || anim2->keyframePoses == NULL ||
|
||||
model->skeleton.boneCount == 0 || model->skeleton.bindPose == NULL)
|
||||
if ((anim0->boneCount != 0) && (anim0->keyframePoses != NULL) &&
|
||||
(anim1->boneCount != 0) && (anim1->keyframePoses != NULL) &&
|
||||
(model->skeleton.boneCount != 0) && (model->skeleton.bindPose != NULL))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Clamp blend factor to [0, 1]
|
||||
blendFactor = fminf(1.0f, fmaxf(0.0f, blendFactor));
|
||||
|
||||
// Ensure frame indices are valid
|
||||
if (frame1 >= anim1->keyframeCount) frame1 = anim1->keyframeCount - 1;
|
||||
if (frame2 >= anim2->keyframeCount) frame2 = anim2->keyframeCount - 1;
|
||||
if (frame1 < 0) frame1 = 0;
|
||||
if (frame2 < 0) frame2 = 0;
|
||||
|
||||
// Get bone count (use minimum of all to be safe)
|
||||
int boneCount = model->skeleton.boneCount;
|
||||
if (anim1->boneCount < boneCount) boneCount = anim1->boneCount;
|
||||
if (anim2->boneCount < boneCount) boneCount = anim2->boneCount;
|
||||
|
||||
// Blend each bone
|
||||
for (int boneId = 0; boneId < boneCount; boneId++)
|
||||
{
|
||||
// Determine blend factor for this bone
|
||||
float boneBlendFactor = blendFactor;
|
||||
// Clamp blend factor to [0, 1]
|
||||
blend = fminf(1.0f, fmaxf(0.0f, blend));
|
||||
|
||||
// If upper body blending is enabled, use different blend factors for upper vs lower body
|
||||
if (upperBodyBlend)
|
||||
// Ensure frame indices are valid
|
||||
if (frame0 >= anim0->keyframeCount) frame0 = anim0->keyframeCount - 1;
|
||||
if (frame1 >= anim1->keyframeCount) frame1 = anim1->keyframeCount - 1;
|
||||
if (frame0 < 0) frame0 = 0;
|
||||
if (frame1 < 0) frame1 = 0;
|
||||
|
||||
// Get bone count (use minimum of all to be safe)
|
||||
int boneCount = model->skeleton.boneCount;
|
||||
if (anim0->boneCount < boneCount) boneCount = anim0->boneCount;
|
||||
if (anim1->boneCount < boneCount) boneCount = anim1->boneCount;
|
||||
|
||||
// Blend each bone
|
||||
for (int boneIndex = 0; boneIndex < boneCount; boneIndex++)
|
||||
{
|
||||
const char *boneName = model->skeleton.bones[boneId].name;
|
||||
bool isUpperBody = IsUpperBodyBone(boneName);
|
||||
// Determine blend factor for this bone
|
||||
float boneBlendFactor = blend;
|
||||
|
||||
// Upper body: use anim2 (attack), Lower body: use anim1 (walk)
|
||||
// blendFactor = 0.0 means full anim1 (walk), 1.0 means full anim2 (attack)
|
||||
if (isUpperBody) boneBlendFactor = blendFactor; // Upper body: blend towards anim2 (attack)
|
||||
else boneBlendFactor = 1.0f - blendFactor; // Lower body: blend towards anim1 (walk) - invert the blend
|
||||
// If upper body blending is enabled, use different blend factors for upper vs lower body
|
||||
if (upperBodyBlend)
|
||||
{
|
||||
const char *boneName = model->skeleton.bones[boneIndex].name;
|
||||
bool isUpperBody = IsUpperBodyBone(boneName);
|
||||
|
||||
// Upper body: use anim1 (attack), Lower body: use anim0 (walk)
|
||||
// blend = 0.0 means full anim0 (walk), 1.0 means full anim1 (attack)
|
||||
if (isUpperBody) boneBlendFactor = blend; // Upper body: blend towards anim1 (attack)
|
||||
else boneBlendFactor = 1.0f - blend; // Lower body: blend towards anim0 (walk) - invert the blend
|
||||
}
|
||||
|
||||
// Get transforms from both animations
|
||||
Transform *bindTransform = &model->skeleton.bindPose[boneIndex];
|
||||
Transform *animTransform0 = &anim0->keyframePoses[frame0][boneIndex];
|
||||
Transform *animTransform1 = &anim1->keyframePoses[frame1][boneIndex];
|
||||
|
||||
// Blend the transforms
|
||||
Transform blended = { 0 };
|
||||
blended.translation = Vector3Lerp(animTransform0->translation, animTransform1->translation, boneBlendFactor);
|
||||
blended.rotation = QuaternionSlerp(animTransform0->rotation, animTransform1->rotation, boneBlendFactor);
|
||||
blended.scale = Vector3Lerp(animTransform0->scale, animTransform1->scale, boneBlendFactor);
|
||||
|
||||
// Convert bind pose to matrix
|
||||
Matrix bindMatrix = MatrixMultiply(MatrixMultiply(
|
||||
MatrixScale(bindTransform->scale.x, bindTransform->scale.y, bindTransform->scale.z),
|
||||
QuaternionToMatrix(bindTransform->rotation)),
|
||||
MatrixTranslate(bindTransform->translation.x, bindTransform->translation.y, bindTransform->translation.z));
|
||||
|
||||
// Convert blended transform to matrix
|
||||
Matrix blendedMatrix = MatrixMultiply(MatrixMultiply(
|
||||
MatrixScale(blended.scale.x, blended.scale.y, blended.scale.z),
|
||||
QuaternionToMatrix(blended.rotation)),
|
||||
MatrixTranslate(blended.translation.x, blended.translation.y, blended.translation.z));
|
||||
|
||||
// Calculate final bone matrix (similar to UpdateModelAnimationBones)
|
||||
model->boneMatrices[boneIndex] = MatrixMultiply(MatrixInvert(bindMatrix), blendedMatrix);
|
||||
}
|
||||
|
||||
// CPU skinning, updates CPU buffers and uploads them to GPU (if available)
|
||||
// NOTE: Fallback in case GPU skinning is not supported or enabled
|
||||
for (int m = 0; m < model->meshCount; m++)
|
||||
{
|
||||
Mesh mesh = model->meshes[m];
|
||||
Vector3 animVertex = { 0 };
|
||||
Vector3 animNormal = { 0 };
|
||||
const int vertexValuesCount = mesh.vertexCount*3;
|
||||
|
||||
int boneIndex = 0;
|
||||
int boneCounter = 0;
|
||||
float boneWeight = 0.0f;
|
||||
bool bufferUpdateRequired = false; // Flag to check when anim vertex information is updated
|
||||
|
||||
// Skip if missing bone data or missing anim buffers initialization
|
||||
if ((mesh.boneWeights == NULL) || (mesh.boneIndices == NULL) ||
|
||||
(mesh.animVertices == NULL) || (mesh.animNormals == NULL)) continue;
|
||||
|
||||
for (int vCounter = 0; vCounter < vertexValuesCount; vCounter += 3)
|
||||
{
|
||||
mesh.animVertices[vCounter] = 0;
|
||||
mesh.animVertices[vCounter + 1] = 0;
|
||||
mesh.animVertices[vCounter + 2] = 0;
|
||||
if (mesh.animNormals != NULL)
|
||||
{
|
||||
mesh.animNormals[vCounter] = 0;
|
||||
mesh.animNormals[vCounter + 1] = 0;
|
||||
mesh.animNormals[vCounter + 2] = 0;
|
||||
}
|
||||
|
||||
// Iterates over 4 bones per vertex
|
||||
for (int j = 0; j < 4; j++, boneCounter++)
|
||||
{
|
||||
boneWeight = mesh.boneWeights[boneCounter];
|
||||
boneIndex = mesh.boneIndices[boneCounter];
|
||||
|
||||
// Early stop when no transformation will be applied
|
||||
if (boneWeight == 0.0f) continue;
|
||||
animVertex = (Vector3){ mesh.vertices[vCounter], mesh.vertices[vCounter + 1], mesh.vertices[vCounter + 2] };
|
||||
animVertex = Vector3Transform(animVertex, model->boneMatrices[boneIndex]);
|
||||
mesh.animVertices[vCounter] += animVertex.x*boneWeight;
|
||||
mesh.animVertices[vCounter + 1] += animVertex.y*boneWeight;
|
||||
mesh.animVertices[vCounter + 2] += animVertex.z*boneWeight;
|
||||
bufferUpdateRequired = true;
|
||||
|
||||
// Normals processing
|
||||
// NOTE: We use meshes.baseNormals (default normal) to calculate meshes.normals (animated normals)
|
||||
if ((mesh.normals != NULL) && (mesh.animNormals != NULL ))
|
||||
{
|
||||
animNormal = (Vector3){ mesh.normals[vCounter], mesh.normals[vCounter + 1], mesh.normals[vCounter + 2] };
|
||||
animNormal = Vector3Transform(animNormal, MatrixTranspose(MatrixInvert(model->boneMatrices[boneIndex])));
|
||||
mesh.animNormals[vCounter] += animNormal.x*boneWeight;
|
||||
mesh.animNormals[vCounter + 1] += animNormal.y*boneWeight;
|
||||
mesh.animNormals[vCounter + 2] += animNormal.z*boneWeight;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (bufferUpdateRequired)
|
||||
{
|
||||
// Update GPU vertex buffers with updated data (position + normals)
|
||||
rlUpdateVertexBuffer(mesh.vboId[SHADER_LOC_VERTEX_POSITION], mesh.animVertices, mesh.vertexCount*3*sizeof(float), 0);
|
||||
if (mesh.normals != NULL) rlUpdateVertexBuffer(mesh.vboId[SHADER_LOC_VERTEX_NORMAL], mesh.animNormals, mesh.vertexCount*3*sizeof(float), 0);
|
||||
}
|
||||
}
|
||||
|
||||
// Get transforms from both animations
|
||||
Transform *bindTransform = &model->skeleton.bindPose[boneId];
|
||||
Transform *anim1Transform = &anim1->keyframePoses[frame1][boneId];
|
||||
Transform *anim2Transform = &anim2->keyframePoses[frame2][boneId];
|
||||
|
||||
// Blend the transforms
|
||||
Transform blended = { 0 };
|
||||
blended.translation = Vector3Lerp(anim1Transform->translation, anim2Transform->translation, boneBlendFactor);
|
||||
blended.rotation = QuaternionSlerp(anim1Transform->rotation, anim2Transform->rotation, boneBlendFactor);
|
||||
blended.scale = Vector3Lerp(anim1Transform->scale, anim2Transform->scale, boneBlendFactor);
|
||||
|
||||
// Convert bind pose to matrix
|
||||
Matrix bindMatrix = MatrixMultiply(MatrixMultiply(
|
||||
MatrixScale(bindTransform->scale.x, bindTransform->scale.y, bindTransform->scale.z),
|
||||
QuaternionToMatrix(bindTransform->rotation)),
|
||||
MatrixTranslate(bindTransform->translation.x, bindTransform->translation.y, bindTransform->translation.z));
|
||||
|
||||
// Convert blended transform to matrix
|
||||
Matrix blendedMatrix = MatrixMultiply(MatrixMultiply(
|
||||
MatrixScale(blended.scale.x, blended.scale.y, blended.scale.z),
|
||||
QuaternionToMatrix(blended.rotation)),
|
||||
MatrixTranslate(blended.translation.x, blended.translation.y, blended.translation.z));
|
||||
|
||||
// Calculate final bone matrix (similar to UpdateModelAnimationBones)
|
||||
model->boneMatrices[boneId] = MatrixMultiply(MatrixInvert(bindMatrix), blendedMatrix);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user