BlobAsset test and animation looping.

BlobAsset creaton and testing BlobAssetStore.
Start working on AnimationLibrary.
Added animation looping based upon BlobAsset data.
This commit is contained in:
max 2020-12-08 17:36:20 +01:00
parent 13f6da9437
commit 5ac8db608e
4 changed files with 120 additions and 37 deletions

View File

@ -0,0 +1,51 @@
using Unity.Entities;
using Unity.Collections;
namespace TAO.VertexAnimation
{
[System.Serializable]
public struct VA_AnimationData
{
public int frames;
public int maxFrames;
}
public struct VA_AnimationDataBlobAsset
{
public BlobArray<VA_AnimationData> animations;
}
public class VA_AnimationDataBlobAssetConversionSystem : GameObjectConversionSystem
{
protected override void OnUpdate()
{
BlobAssetReference<VA_AnimationDataBlobAsset> animationDataBlobAssetRef;
// Blob builder to build.
using (BlobBuilder blobBuilder = new BlobBuilder(Allocator.Temp))
{
// Construct the root.
ref VA_AnimationDataBlobAsset animationDataBlobAsset = ref blobBuilder.ConstructRoot<VA_AnimationDataBlobAsset>();
// Set all the data.
BlobBuilderArray<VA_AnimationData> animationDataArray = blobBuilder.Allocate(ref animationDataBlobAsset.animations, 2);
for (int i = 0; i < animationDataArray.Length; i++)
{
animationDataArray[i] = new VA_AnimationData
{
frames = 36,
maxFrames = 43
};
}
// Construct blob asset reference.
animationDataBlobAssetRef = blobBuilder.CreateBlobAssetReference<VA_AnimationDataBlobAsset>(Allocator.Persistent);
UnityEngine.Debug.Log("Created: " + animationDataBlobAssetRef.Value.animations.Length.ToString());
}
// TODO: Generate Hash based on Guid.
BlobAssetStore.TryAdd(new Hash128("AnimationLib"), animationDataBlobAssetRef);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 79884e6263d984c44af76267d129d76b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -17,23 +17,28 @@ namespace TAO.VertexAnimation
public int animationIndex;
public int animationIndexSchedule;
public float animationTime;
public BlobAssetReference<VA_AnimationDataBlobAsset> animationsRef;
}
[UpdateAfter(typeof(VA_AnimationDataBlobAssetConversionSystem))]
public class VA_AnimatorConversionSystem : GameObjectConversionSystem
{
protected override void OnUpdate()
{
BlobAssetStore.TryGet(new Unity.Entities.Hash128("AnimationLib"), out BlobAssetReference<VA_AnimationDataBlobAsset> assetReference);
Entities.ForEach((VA_AnimatorComponentAuthoring animator) =>
{
Entity entity = GetPrimaryEntity(animator);
// Add animator to 'parent'.
VA_AnimatorComponent animatorComponent = new VA_AnimatorComponent
{
animationIndex = 0,
animationIndexSchedule = -1,
animationTime = 0,
};
animationsRef = assetReference
};
DstEntityManager.AddComponentData(entity, animatorComponent);
// Add the Material data to the children.

View File

@ -2,7 +2,7 @@
using Unity.Entities;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine;
using Unity.Mathematics;
namespace TAO.VertexAnimation
{
@ -75,53 +75,69 @@ namespace TAO.VertexAnimation
}
}
public class VA_AnimatorSystem2 : SystemBase
{
protected override void OnCreate()
{
base.OnCreate();
//public class VA_AnimatorSystem2 : SystemBase
//{
// protected override void OnCreate()
// {
// base.OnCreate();
Enabled = false;
}
// Enabled = false;
// }
protected override void OnUpdate()
{
Entities.ForEach((ref VA_AnimatorComponent ac, in DynamicBuffer<Child> children) =>
{
for (int i = 0; i < children.Length; i++)
{
// Get child.
Entity child = children[i].Value;
// protected override void OnUpdate()
// {
// Entities.ForEach((ref VA_AnimatorComponent ac, in DynamicBuffer<Child> children) =>
// {
// for (int i = 0; i < children.Length; i++)
// {
// // Get child.
// Entity child = children[i].Value;
//if(HasComponent<VA_AnimationTimeComponent>(child))
//{
var atc = GetComponent<VA_AnimationTimeComponent>(child);
atc.Value = ac.animationTime;
SetComponent(child, atc);
//}
// //if(HasComponent<VA_AnimationTimeComponent>(child))
// //{
// var atc = GetComponent<VA_AnimationTimeComponent>(child);
// atc.Value = ac.animationTime;
// SetComponent(child, atc);
// //}
//if(HasComponent<VA_AnimationIndexComponent>(child))
//{
var aic = GetComponent<VA_AnimationIndexComponent>(child);
aic.Value = ac.animationIndex;
SetComponent(child, aic);
//}
}
})
.Run();
}
}
// //if(HasComponent<VA_AnimationIndexComponent>(child))
// //{
// var aic = GetComponent<VA_AnimationIndexComponent>(child);
// aic.Value = ac.animationIndex;
// SetComponent(child, aic);
// //}
// }
// })
// .Run();
// }
//}
[UpdateBefore(typeof(VA_AnimatorSystem))]
public class VA_AnimationTimeSystem : SystemBase
{
protected override void OnUpdate()
{
float time = UnityEngine.Time.deltaTime;
float deltaTime = UnityEngine.Time.deltaTime;
Entities.ForEach((ref VA_AnimatorComponent ac) =>
{
ac.animationTime += time;
// Get the animation lib data.
ref VA_AnimationDataBlobAsset animationsRef = ref ac.animationsRef.Value;
int aFrames = animationsRef.animations[ac.animationIndex].frames;
int aMaxFrames = animationsRef.animations[ac.animationIndex].maxFrames;
ac.animationTime += deltaTime;
// Time per frame.
float fTime = 1.0f / aMaxFrames;
// Animation time.
float cTime = fTime * (aFrames);
if (ac.animationTime > cTime)
{
ac.animationTime = ac.animationTime - cTime;
}
}).ScheduleParallel();
}
}