2020-12-14 20:27:44 +01:00
|
|
|
|
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;
|
2021-02-09 17:21:42 +01:00
|
|
|
|
using UnityEngine.Animations;
|
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.
|
2020-12-06 02:36:15 +01:00
|
|
|
|
public class VA_AnimatorSystem : SystemBase
|
|
|
|
|
{
|
|
|
|
|
protected override void OnUpdate()
|
|
|
|
|
{
|
2020-12-14 20:27:44 +01:00
|
|
|
|
var animationData = GetComponentDataFromEntity<VA_AnimationDataComponent>(false);
|
2020-12-08 00:33:06 +01:00
|
|
|
|
|
2020-12-06 02:36:15 +01:00
|
|
|
|
Entities.ForEach((ref VA_AnimatorComponent ac, in DynamicBuffer<Child> children) =>
|
2020-12-08 00:33:06 +01:00
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < children.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
// Get child.
|
|
|
|
|
Entity child = children[i].Value;
|
|
|
|
|
|
2020-12-14 20:27:44 +01:00
|
|
|
|
// Get the animation lib data.
|
|
|
|
|
ref VA_AnimationLibraryData animationsRef = ref ac.animationLibrary.Value;
|
|
|
|
|
|
2021-02-09 17:21:42 +01:00
|
|
|
|
// Lerp animations.
|
|
|
|
|
// Set animation for lerp.
|
|
|
|
|
int animationIndexNext = ac.animationIndexNext;
|
|
|
|
|
if (ac.animationIndexNext < 0)
|
|
|
|
|
{
|
|
|
|
|
animationIndexNext = ac.animationIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculate next frame time for lerp.
|
|
|
|
|
float animationTimeNext = ac.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 -= ac.animationTime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set material data.
|
2020-12-14 20:27:44 +01:00
|
|
|
|
animationData[child] = new VA_AnimationDataComponent
|
|
|
|
|
{
|
|
|
|
|
Value = new float4
|
|
|
|
|
{
|
|
|
|
|
x = ac.animationTime,
|
|
|
|
|
y = VA_AnimationLibraryUtils.GetAnimationMapIndex(ref animationsRef, ac.animationIndex),
|
2021-02-09 17:21:42 +01:00
|
|
|
|
z = animationTimeNext,
|
|
|
|
|
w = VA_AnimationLibraryUtils.GetAnimationMapIndex(ref animationsRef, animationIndexNext)
|
2020-12-14 20:27:44 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
2020-12-08 00:33:06 +01:00
|
|
|
|
}
|
|
|
|
|
})
|
2020-12-14 20:27:44 +01:00
|
|
|
|
.WithNativeDisableContainerSafetyRestriction(animationData)
|
2021-01-25 21:42:08 +01:00
|
|
|
|
.WithName("VA_AnimatorSystem")
|
2020-12-09 18:36:32 +01:00
|
|
|
|
.ScheduleParallel();
|
2020-12-08 00:33:06 +01:00
|
|
|
|
}
|
2020-12-06 02:36:15 +01:00
|
|
|
|
}
|
|
|
|
|
}
|