Interpolation

Smooth animations :)
This commit is contained in:
max
2021-02-09 17:21:42 +01:00
parent c928f6daae
commit f7fc4434d5
6 changed files with 2898 additions and 599 deletions

View File

@ -15,8 +15,7 @@ namespace TAO.VertexAnimation
public struct VA_AnimatorComponent : IComponentData
{
public int animationIndex;
// TODO: Animation blending.
//public int animationIndexSchedule;
public int animationIndexNext;
public float animationTime;
public BlobAssetReference<VA_AnimationLibraryData> animationLibrary;
}
@ -38,7 +37,7 @@ namespace TAO.VertexAnimation
VA_AnimatorComponent animatorComponent = new VA_AnimatorComponent
{
animationIndex = 0,
//animationIndexSchedule = -1,
animationIndexNext = -1,
animationTime = 0,
animationLibrary = animLib
};

View File

@ -1,6 +1,7 @@
using Unity.Entities;
using Unity.Transforms;
using Unity.Mathematics;
using UnityEngine.Animations;
namespace TAO.VertexAnimation
{
@ -21,14 +22,31 @@ namespace TAO.VertexAnimation
// Get the animation lib data.
ref VA_AnimationLibraryData animationsRef = ref ac.animationLibrary.Value;
// 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.
animationData[child] = new VA_AnimationDataComponent
{
Value = new float4
{
x = ac.animationTime,
y = VA_AnimationLibraryUtils.GetAnimationMapIndex(ref animationsRef, ac.animationIndex),
z = VA_AnimationLibraryUtils.GetColorMapIndex(ref animationsRef, ac.animationIndex),
w = 0
z = animationTimeNext,
w = VA_AnimationLibraryUtils.GetAnimationMapIndex(ref animationsRef, animationIndexNext)
}
};
}