diff --git a/Runtime/Scripts/VA_AnimationLibrary.cs b/Runtime/Scripts/VA_AnimationLibrary.cs new file mode 100644 index 0000000..5c791f2 --- /dev/null +++ b/Runtime/Scripts/VA_AnimationLibrary.cs @@ -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 animations; + } + + public class VA_AnimationDataBlobAssetConversionSystem : GameObjectConversionSystem + { + protected override void OnUpdate() + { + BlobAssetReference animationDataBlobAssetRef; + + // Blob builder to build. + using (BlobBuilder blobBuilder = new BlobBuilder(Allocator.Temp)) + { + // Construct the root. + ref VA_AnimationDataBlobAsset animationDataBlobAsset = ref blobBuilder.ConstructRoot(); + + // Set all the data. + BlobBuilderArray 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(Allocator.Persistent); + + UnityEngine.Debug.Log("Created: " + animationDataBlobAssetRef.Value.animations.Length.ToString()); + } + + // TODO: Generate Hash based on Guid. + BlobAssetStore.TryAdd(new Hash128("AnimationLib"), animationDataBlobAssetRef); + } + } +} \ No newline at end of file diff --git a/Runtime/Scripts/VA_AnimationLibrary.cs.meta b/Runtime/Scripts/VA_AnimationLibrary.cs.meta new file mode 100644 index 0000000..39fd402 --- /dev/null +++ b/Runtime/Scripts/VA_AnimationLibrary.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 79884e6263d984c44af76267d129d76b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Scripts/VA_AnimatorComponentAuthoring.cs b/Runtime/Scripts/VA_AnimatorComponentAuthoring.cs index 397d41f..862e45c 100644 --- a/Runtime/Scripts/VA_AnimatorComponentAuthoring.cs +++ b/Runtime/Scripts/VA_AnimatorComponentAuthoring.cs @@ -17,23 +17,28 @@ namespace TAO.VertexAnimation public int animationIndex; public int animationIndexSchedule; public float animationTime; + public BlobAssetReference animationsRef; } + [UpdateAfter(typeof(VA_AnimationDataBlobAssetConversionSystem))] public class VA_AnimatorConversionSystem : GameObjectConversionSystem { protected override void OnUpdate() { + BlobAssetStore.TryGet(new Unity.Entities.Hash128("AnimationLib"), out BlobAssetReference 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. diff --git a/Runtime/Scripts/VA_AnimatorSystem.cs b/Runtime/Scripts/VA_AnimatorSystem.cs index 80b4417..9b8897f 100644 --- a/Runtime/Scripts/VA_AnimatorSystem.cs +++ b/Runtime/Scripts/VA_AnimatorSystem.cs @@ -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 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 children) => + // { + // for (int i = 0; i < children.Length; i++) + // { + // // Get child. + // Entity child = children[i].Value; - //if(HasComponent(child)) - //{ - var atc = GetComponent(child); - atc.Value = ac.animationTime; - SetComponent(child, atc); - //} + // //if(HasComponent(child)) + // //{ + // var atc = GetComponent(child); + // atc.Value = ac.animationTime; + // SetComponent(child, atc); + // //} - //if(HasComponent(child)) - //{ - var aic = GetComponent(child); - aic.Value = ac.animationIndex; - SetComponent(child, aic); - //} - } - }) - .Run(); - } - } + // //if(HasComponent(child)) + // //{ + // var aic = GetComponent(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(); } }