2022-12-04 04:18:35 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Unity.Entities;
|
2020-12-06 02:36:15 +01:00
|
|
|
|
using Unity.Transforms;
|
2020-12-08 17:36:20 +01:00
|
|
|
|
using Unity.Mathematics;
|
2020-12-06 02:36:15 +01:00
|
|
|
|
|
|
|
|
|
namespace TAO.VertexAnimation
|
|
|
|
|
{
|
2020-12-14 20:27:44 +01:00
|
|
|
|
// System to update all the animations.
|
2022-12-04 04:18:35 +01:00
|
|
|
|
public partial class VA_AnimatorSystem : SystemBase
|
2020-12-06 02:36:15 +01:00
|
|
|
|
{
|
|
|
|
|
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) =>
|
2020-12-08 00:33:06 +01:00
|
|
|
|
{
|
2020-12-14 20:27:44 +01:00
|
|
|
|
// Get the animation lib data.
|
2022-12-04 04:18:35 +01:00
|
|
|
|
ref VA_AnimationLibraryData animationsRef = ref animator.animationLibrary.Value;
|
2020-12-14 20:27:44 +01:00
|
|
|
|
|
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++ )
|
2020-12-14 20:27:44 +01:00
|
|
|
|
{
|
2022-12-04 04:18:35 +01:00
|
|
|
|
VaAnimationDataComponent vaAnimationDataComponent = new VaAnimationDataComponent();
|
|
|
|
|
vaAnimationDataComponent.Value = new float4
|
2020-12-14 20:27:44 +01:00
|
|
|
|
{
|
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();
|
2020-12-08 00:33:06 +01:00
|
|
|
|
}
|
2020-12-06 02:36:15 +01:00
|
|
|
|
}
|
|
|
|
|
}
|