VertexAnimation/Runtime/Scripts/VA_AnimatorSystem.cs

52 lines
1.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 04:18:35 +01:00
// This is only executed if we have a valid skinning setup
Entities
.ForEach((VA_AnimatorComponent animator, in DynamicBuffer<SkinnedMeshEntity> bones) =>
{
// Get the animation lib data.
2022-12-04 04:18:35 +01:00
ref VA_AnimationLibraryData animationsRef = ref animator.animationLibrary.Value;
2021-02-09 17:21:42 +01:00
// Lerp animations.
// Set animation for lerp.
2022-12-04 04:18:35 +01:00
int animationIndexNext = animator.animationIndexNext;
if (animator.animationIndexNext < 0)
2021-02-09 17:21:42 +01:00
{
2022-12-04 04:18:35 +01:00
animationIndexNext = animator.animationIndex;
//animator.animationIndexNext = animationIndexNext + 1;
2021-02-09 17:21:42 +01:00
}
// Calculate next frame time for lerp.
2022-12-04 04:18:35 +01:00
float animationTimeNext = animator.animationTime + (1.0f / animationsRef.animations[animationIndexNext].maxFrames);
2021-02-09 17:21:42 +01:00
if (animationTimeNext > animationsRef.animations[animationIndexNext].duration)
{
// Set time. Using the difference to smooth out animations when looping.
2022-12-04 04:18:35 +01:00
animationTimeNext -= animator.animationTime;
2021-02-09 17:21:42 +01:00
}
2022-12-04 04:18:35 +01:00
for ( int i = 0; i < bones.Length; i++ )
{
2022-12-04 04:18:35 +01:00
VaAnimationDataComponent vaAnimationDataComponent = new VaAnimationDataComponent();
vaAnimationDataComponent.Value = new float4
{
2022-12-04 04:18:35 +01:00
x = animator.animationTime,
y = VA_AnimationLibraryUtils.GetAnimationMapIndex( ref animationsRef, animator.animationIndex ),
2021-02-09 17:21:42 +01:00
z = animationTimeNext,
2022-12-04 04:18:35 +01:00
w = VA_AnimationLibraryUtils.GetAnimationMapIndex( ref animationsRef, animationIndexNext )
};
SystemAPI.SetComponent<VaAnimationDataComponent>( bones[i].Value, vaAnimationDataComponent );
}
}).Run();
}
}
}