Skip to content

Commit

Permalink
feat: add animation blending to ecs ext
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan hoffstadt authored and hoffstadt committed May 21, 2024
1 parent 5e159ce commit 88c06d1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
25 changes: 15 additions & 10 deletions extensions/pl_ecs_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,8 @@ pl_ecs_add_component(plComponentLibrary* ptLibrary, plComponentType tType, plEnt
pl_sb_add(sbComponents);
ptManager->pComponents = sbComponents;
sbComponents[uComponentIndex] = (plAnimationComponent){
.fSpeed = 1.0f
.fSpeed = 1.0f,
.fBlendAmount = 1.0f
};
return &sbComponents[uComponentIndex];
}
Expand Down Expand Up @@ -1145,16 +1146,18 @@ pl_run_animation_update_system(plComponentLibrary* ptLibrary, float fDeltaTime)
{
const plVec3 tPrev = *(plVec3*)&ptData->sbfKeyFrameData[iPrevKey * 3];
const plVec3 tNext = *(plVec3*)&ptData->sbfKeyFrameData[iNextKey * 3];
ptTransform->tTranslation = (plVec3){
const plVec3 tTranslation = (plVec3){
.x = tPrev.x * (1.0f - fTn) + tNext.x * fTn,
.y = tPrev.y * (1.0f - fTn) + tNext.y * fTn,
.z = tPrev.z * (1.0f - fTn) + tNext.z * fTn,
};
};
ptTransform->tTranslation = pl_lerp_vec3(ptTransform->tTranslation, tTranslation, ptAnimationComponent->fBlendAmount);
}

else if(ptSampler->tMode == PL_ANIMATION_MODE_STEP)
{
ptTransform->tTranslation = *(plVec3*)&ptData->sbfKeyFrameData[iPrevKey * 3];
const plVec3 tTranslation = *(plVec3*)&ptData->sbfKeyFrameData[iPrevKey * 3];
ptTransform->tTranslation = pl_lerp_vec3(ptTransform->tTranslation, tTranslation, ptAnimationComponent->fBlendAmount);
}

else if(ptSampler->tMode == PL_ANIMATION_MODE_CUBIC_SPLINE)
Expand All @@ -1164,14 +1167,16 @@ pl_run_animation_update_system(plComponentLibrary* ptLibrary, float fDeltaTime)
const int iV = 1 * 3;
const int iB = 2 * 3;

plVec3 tTranslation = {0};
for(uint32_t k = 0; k < 3; k++)
{
const float iV0 = *(float*)&ptData->sbfKeyFrameData[iPrevIndex + k + iV];
const float a = fKeyDelta * *(float*)&ptData->sbfKeyFrameData[iNextIndex + k + iA];
const float b = fKeyDelta * *(float*)&ptData->sbfKeyFrameData[iPrevIndex + k + iB];
const float v1 = *(float*)&ptData->sbfKeyFrameData[iNextIndex + k + iV];
ptTransform->tTranslation.d[k] = ((2 * fTCub - 3 * fTSq + 1) * iV0) + ((fTCub - 2 * fTSq + fTn) * b) + ((-2 * fTCub + 3 * fTSq) * v1) + ((fTCub - fTSq) * a);
tTranslation.d[k] = ((2 * fTCub - 3 * fTSq + 1) * iV0) + ((fTCub - 2 * fTSq + fTn) * b) + ((-2 * fTCub + 3 * fTSq) * v1) + ((fTCub - fTSq) * a);
}
ptTransform->tTranslation = pl_lerp_vec3(ptTransform->tTranslation, tTranslation, ptAnimationComponent->fBlendAmount);
}
break;
}
Expand Down Expand Up @@ -1221,11 +1226,13 @@ pl_run_animation_update_system(plComponentLibrary* ptLibrary, float fDeltaTime)
{
const plVec4 tQ0 = *(plVec4*)&ptData->sbfKeyFrameData[iPrevKey * 4];
const plVec4 tQ1 = *(plVec4*)&ptData->sbfKeyFrameData[iNextKey * 4];
ptTransform->tRotation = pl_quat_slerp(tQ0, tQ1, fTn);
const plVec4 tRotation = pl_quat_slerp(tQ0, tQ1, fTn);
ptTransform->tRotation = pl_quat_slerp(ptTransform->tRotation, tRotation, ptAnimationComponent->fBlendAmount);
}
else if(ptSampler->tMode == PL_ANIMATION_MODE_STEP)
{
ptTransform->tRotation = *(plVec4*)&ptData->sbfKeyFrameData[iPrevKey * 4];
const plVec4 tRotation = *(plVec4*)&ptData->sbfKeyFrameData[iPrevKey * 4];
ptTransform->tRotation = pl_quat_slerp(ptTransform->tRotation, tRotation, ptAnimationComponent->fBlendAmount);
}
else if(ptSampler->tMode == PL_ANIMATION_MODE_CUBIC_SPLINE)
{
Expand All @@ -1235,7 +1242,6 @@ pl_run_animation_update_system(plComponentLibrary* ptLibrary, float fDeltaTime)
const int iB = 2 * 4;

plVec4 tResult = {0};

for(uint32_t k = 0; k < 4; k++)
{
const float iV0 = *(float*)&ptData->sbfKeyFrameData[iPrevIndex + k + iV];
Expand All @@ -1245,12 +1251,11 @@ pl_run_animation_update_system(plComponentLibrary* ptLibrary, float fDeltaTime)

tResult.d[k] = ((2 * fTCub - 3 * fTSq + 1) * iV0) + ((fTCub - 2 * fTSq + fTn) * b) + ((-2 * fTCub + 3 * fTSq) * iV1) + ((fTCub - fTSq) * a);
}
ptTransform->tRotation = pl_quat_slerp(ptTransform->tRotation, tResult, ptAnimationComponent->fBlendAmount);
}
break;
}
}

ptTransform->tWorld = pl_rotation_translation_scale(ptTransform->tRotation, ptTransform->tTranslation, ptTransform->tScale);
}
}

Expand Down
1 change: 1 addition & 0 deletions extensions/pl_ecs_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ typedef struct _plAnimationComponent
float fEnd;
float fTimer;
float fSpeed;
float fBlendAmount;
plAnimationChannel* sbtChannels;
plAnimationSampler* sbtSamplers;
} plAnimationComponent;
Expand Down
2 changes: 1 addition & 1 deletion extensions/pl_model_loader_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ pl_load_ext(plApiRegistryI* ptApiRegistry, bool bReload)
// load required extensions (may already be loaded)
const plExtensionRegistryI* ptExtensionRegistry = ptApiRegistry->first(PL_API_EXTENSION_REGISTRY);
ptExtensionRegistry->load("pl_resource_ext", NULL, NULL, false);
ptExtensionRegistry->load("pl_ecs_ext", NULL, NULL, false);
ptExtensionRegistry->load("pl_ecs_ext", NULL, NULL, true);

// load required APIs
gptResource = ptApiRegistry->first(PL_API_RESOURCE);
Expand Down
2 changes: 1 addition & 1 deletion extensions/pl_ref_renderer_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -4875,7 +4875,7 @@ pl_load_ext(plApiRegistryI* ptApiRegistry, bool bReload)
ptExtensionRegistry->load("pl_stats_ext", NULL, NULL, false);
ptExtensionRegistry->load("pl_graphics_ext", NULL, NULL, false);
ptExtensionRegistry->load("pl_gpu_allocators_ext", NULL, NULL, false);
ptExtensionRegistry->load("pl_ecs_ext", NULL, NULL, false);
ptExtensionRegistry->load("pl_ecs_ext", NULL, NULL, true);
ptExtensionRegistry->load("pl_resource_ext", NULL, NULL, false);
ptExtensionRegistry->load("pl_draw_ext", NULL, NULL, true);
ptExtensionRegistry->load("pl_ui_ext", NULL, NULL, true);
Expand Down
4 changes: 2 additions & 2 deletions scripts/gen_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def add_plugin_to_metal_app(name, reloadable, objc = False, binary_name = None,
add_plugin_to_vulkan_app("pl_stats_ext", False)
add_plugin_to_vulkan_app("pl_job_ext", False)
add_plugin_to_vulkan_app("pl_model_loader_ext", False)
add_plugin_to_vulkan_app("pl_ecs_ext", False)
add_plugin_to_vulkan_app("pl_ecs_ext", True)
add_plugin_to_vulkan_app("pl_resource_ext", False)
add_plugin_to_vulkan_app("pl_gpu_allocators_ext", False)
add_plugin_to_vulkan_app("pl_ref_renderer_ext", True)
Expand All @@ -133,7 +133,7 @@ def add_plugin_to_metal_app(name, reloadable, objc = False, binary_name = None,
add_plugin_to_metal_app("pl_image_ext", False)
add_plugin_to_metal_app("pl_stats_ext", False)
add_plugin_to_metal_app("pl_job_ext", False)
add_plugin_to_metal_app("pl_ecs_ext", False)
add_plugin_to_metal_app("pl_ecs_ext", True)
add_plugin_to_metal_app("pl_model_loader_ext", False)
add_plugin_to_metal_app("pl_resource_ext", False)
add_plugin_to_metal_app("pl_metal_ext", False, True, "pl_graphics_ext")
Expand Down

0 comments on commit 88c06d1

Please sign in to comment.