VertexAnimation/Runtime/Scripts/VA_AnimatorSystem.cs

74 lines
2.8 KiB
C#
Raw Normal View History

2022-12-04 04:18:35 +01:00
using System.Collections.Generic;
using Unity.Entities;
using Unity.Transforms;
using Unity.Mathematics;
namespace TAO.VertexAnimation
{
// System to update all the animations.
2022-12-04 04:18:35 +01:00
public partial class VA_AnimatorSystem : SystemBase
{
protected override void OnUpdate()
{
2022-12-04 15:47:07 +01:00
float deltaTime = SystemAPI.Time.DeltaTime;
2022-12-04 04:18:35 +01:00
// This is only executed if we have a valid skinning setup
Entities
2022-12-04 15:47:07 +01:00
.ForEach((ref VA_AnimatorComponent animator, in VA_AnimatorStateComponent vaAnimatorStateComponent, in DynamicBuffer<SkinnedMeshEntity> bones) =>
{
2022-12-04 15:47:07 +01:00
if ( vaAnimatorStateComponent.Enabled )
2021-02-09 17:21:42 +01:00
{
2022-12-04 15:47:07 +01:00
// Get the animation lib data.
ref VA_AnimationLibraryData animationsRef = ref animator.animationLibrary.Value;
2021-02-09 17:21:42 +01:00
2022-12-04 15:47:07 +01:00
//if ( animator.AnimationName != vaAnimatorStateComponent.CurrentAnimationName )
//{
// // Set the animation index on the AnimatorComponent to play this animation.
// animator.animationIndex = VA_AnimationLibraryUtils.GetAnimation(ref animationsRef, vaAnimatorStateComponent.CurrentAnimationName);
// animator.AnimationName = vaAnimatorStateComponent.CurrentAnimationName;
//}
// 'Play' the actual animation.
animator.animationTime += deltaTime * animationsRef.animations[animator.animationIndex].frameTime;
2021-02-09 17:21:42 +01:00
2022-12-04 15:47:07 +01:00
if (animator.animationTime > animationsRef.animations[animator.animationIndex].duration)
{
// Set time. Using the difference to smoothen out animations when looping.
animator.animationTime -= animationsRef.animations[animator.animationIndex].duration;
}
// Lerp animations.
// Set animation for lerp.
int animationIndexNext = animator.animationIndexNext;
if (animator.animationIndexNext < 0)
{
animationIndexNext = animator.animationIndex;
//animator.animationIndexNext = animationIndexNext + 1;
}
// Calculate next frame time for lerp.
float animationTimeNext = animator.animationTime + (1.0f / animationsRef.animations[animationIndexNext].maxFrames);
if (animationTimeNext > animationsRef.animations[animationIndexNext].duration)
{
// Set time. Using the difference to smooth out animations when looping.
animationTimeNext -= animator.animationTime;
}
for ( int i = 0; i < bones.Length; i++ )
{
2022-12-04 15:47:07 +01:00
VaAnimationDataComponent vaAnimationDataComponent = new VaAnimationDataComponent();
vaAnimationDataComponent.Value = new float4
{
x = animator.animationTime,
y = VA_AnimationLibraryUtils.GetAnimationMapIndex( ref animationsRef, animator.animationIndex ),
z = animationTimeNext,
w = VA_AnimationLibraryUtils.GetAnimationMapIndex( ref animationsRef, animationIndexNext )
};
SystemAPI.SetComponent<VaAnimationDataComponent>( bones[i].Value, vaAnimationDataComponent );
}
2022-12-04 04:18:35 +01:00
}
2022-12-04 15:47:07 +01:00
2022-12-04 04:18:35 +01:00
}).Run();
}
}
}