Add **Animation Slot** node for playing animations from code in Anim Graph

This commit is contained in:
Wojtek Figat
2021-12-16 18:57:33 +01:00
parent 6f9f2ccdc0
commit 3c3f2ae075
7 changed files with 267 additions and 0 deletions

View File

@@ -324,6 +324,89 @@ void AnimatedModel::ClearBlendShapeWeights()
_blendShapes.Clear();
}
void AnimatedModel::PlaySlotAnimation(const StringView& slotName, Animation* anim, float speed, float blendInTime, float blendOutTime)
{
CHECK(anim);
for (auto& slot : GraphInstance.Slots)
{
if (slot.Animation == anim && slot.Name == slotName)
{
slot.Pause = false;
slot.BlendInTime = blendInTime;
return;
}
}
int32 index = 0;
for (; index < GraphInstance.Slots.Count(); index++)
{
if (GraphInstance.Slots[index].Animation == nullptr)
break;
}
if (index == GraphInstance.Slots.Count())
GraphInstance.Slots.AddOne();
auto& slot = GraphInstance.Slots[index];
slot.Name = slotName;
slot.Animation = anim;
slot.Speed = speed;
slot.BlendInTime = blendInTime;
slot.BlendOutTime = blendOutTime;
}
void AnimatedModel::StopSlotAnimation()
{
GraphInstance.Slots.Clear();
}
void AnimatedModel::StopSlotAnimation(const StringView& slotName, Animation* anim)
{
for (auto& slot : GraphInstance.Slots)
{
if (slot.Animation == anim && slot.Name == slotName)
{
slot.Animation = nullptr;
break;
}
}
}
void AnimatedModel::PauseSlotAnimation()
{
for (auto& slot : GraphInstance.Slots)
slot.Pause = true;
}
void AnimatedModel::PauseSlotAnimation(const StringView& slotName, Animation* anim)
{
for (auto& slot : GraphInstance.Slots)
{
if (slot.Animation == anim && slot.Name == slotName)
{
slot.Pause = true;
break;
}
}
}
bool AnimatedModel::IsPlayingSlotAnimation()
{
for (auto& slot : GraphInstance.Slots)
{
if (slot.Animation && !slot.Pause)
return true;
}
return false;
}
bool AnimatedModel::IsPlayingSlotAnimation(const StringView& slotName, Animation* anim)
{
for (auto& slot : GraphInstance.Slots)
{
if (slot.Animation == anim && slot.Name == slotName && !slot.Pause)
return true;
}
return false;
}
void AnimatedModel::ApplyRootMotion(const RootMotionData& rootMotionDelta)
{
// Skip if no motion

View File

@@ -303,6 +303,54 @@ public:
/// </summary>
API_FUNCTION() void ClearBlendShapeWeights();
public:
/// <summary>
/// Plays the animation on the slot in Anim Graph.
/// </summary>
/// <param name="slotName">The name of the slot.</param>
/// <param name="anim">The animation to play.</param>
/// <param name="speed">The playback speed.</param>
/// <param name="blendInTime">The animation blending in time (in seconds). Cam be used to smooth the slot animation playback with the input pose when starting the animation.</param>
/// <param name="blendOutTime">The animation blending out time (in seconds). Cam be used to smooth the slot animation playback with the input pose when ending animation.</param>
API_FUNCTION() void PlaySlotAnimation(const StringView& slotName, Animation* anim, float speed = 1.0f, float blendInTime = 0.2f, float blendOutTime = 0.2f);
/// <summary>
/// Stops all the animations playback on the all slots in Anim Graph.
/// </summary>
API_FUNCTION() void StopSlotAnimation();
/// <summary>
/// Stops the animation playback on the slot in Anim Graph.
/// </summary>
/// <param name="slotName">The name of the slot.</param>
/// <param name="anim">The animation to stop.</param>
API_FUNCTION() void StopSlotAnimation(const StringView& slotName, Animation* anim);
/// <summary>
/// Pauses all the animations playback on the all slots in Anim Graph.
/// </summary>
API_FUNCTION() void PauseSlotAnimation();
/// <summary>
/// Pauses the animation playback on the slot in Anim Graph.
/// </summary>
/// <param name="slotName">The name of the slot.</param>
/// <param name="anim">The animation to pause.</param>
API_FUNCTION() void PauseSlotAnimation(const StringView& slotName, Animation* anim);
/// <summary>
/// Checks if the any animation playback is active on the any slot in Anim Graph (not paused).
/// </summary>
API_FUNCTION() bool IsPlayingSlotAnimation();
/// <summary>
/// Checks if the animation playback is active on the slot in Anim Graph (not paused).
/// </summary>
/// <param name="slotName">The name of the slot.</param>
/// <param name="anim">The animation to check.</param>
API_FUNCTION() bool IsPlayingSlotAnimation(const StringView& slotName, Animation* anim);
private:
void ApplyRootMotion(const RootMotionData& rootMotionDelta);