mirror of
				https://github.com/maxartz15/VertexAnimation.git
				synced 2025-11-04 10:25:44 +01:00 
			
		
		
		
	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:
		
							
								
								
									
										51
									
								
								Runtime/Scripts/VA_AnimationLibrary.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								Runtime/Scripts/VA_AnimationLibrary.cs
									
									
									
									
									
										Normal 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);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										11
									
								
								Runtime/Scripts/VA_AnimationLibrary.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Runtime/Scripts/VA_AnimationLibrary.cs.meta
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
fileFormatVersion: 2
 | 
			
		||||
guid: 79884e6263d984c44af76267d129d76b
 | 
			
		||||
MonoImporter:
 | 
			
		||||
  externalObjects: {}
 | 
			
		||||
  serializedVersion: 2
 | 
			
		||||
  defaultReferences: []
 | 
			
		||||
  executionOrder: 0
 | 
			
		||||
  icon: {instanceID: 0}
 | 
			
		||||
  userData: 
 | 
			
		||||
  assetBundleName: 
 | 
			
		||||
  assetBundleVariant: 
 | 
			
		||||
@@ -17,12 +17,16 @@ 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);
 | 
			
		||||
@@ -33,6 +37,7 @@ namespace TAO.VertexAnimation
 | 
			
		||||
					animationIndex = 0,
 | 
			
		||||
					animationIndexSchedule = -1,
 | 
			
		||||
					animationTime = 0,
 | 
			
		||||
					animationsRef = assetReference
 | 
			
		||||
				};
 | 
			
		||||
				DstEntityManager.AddComponentData(entity, animatorComponent);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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();
 | 
			
		||||
	
 | 
			
		||||
			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;
 | 
			
		||||
					
 | 
			
		||||
					//if(HasComponent<VA_AnimationTimeComponent>(child))
 | 
			
		||||
	//public class VA_AnimatorSystem2 : SystemBase
 | 
			
		||||
	//{
 | 
			
		||||
	                    var atc = GetComponent<VA_AnimationTimeComponent>(child);
 | 
			
		||||
	                    atc.Value = ac.animationTime;
 | 
			
		||||
						SetComponent(child, atc);
 | 
			
		||||
	//    protected override void OnCreate()
 | 
			
		||||
	//    {
 | 
			
		||||
	//        base.OnCreate();
 | 
			
		||||
	
 | 
			
		||||
	//		Enabled = false;
 | 
			
		||||
	//    }
 | 
			
		||||
	
 | 
			
		||||
					//if(HasComponent<VA_AnimationIndexComponent>(child))
 | 
			
		||||
	//    protected override void OnUpdate()
 | 
			
		||||
	//	{
 | 
			
		||||
						var aic = GetComponent<VA_AnimationIndexComponent>(child);
 | 
			
		||||
						aic.Value = ac.animationIndex;
 | 
			
		||||
						SetComponent(child, aic);
 | 
			
		||||
	//		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_AnimationIndexComponent>(child))
 | 
			
		||||
	//				//{
 | 
			
		||||
	//					var aic = GetComponent<VA_AnimationIndexComponent>(child);
 | 
			
		||||
	//					aic.Value = ac.animationIndex;
 | 
			
		||||
	//					SetComponent(child, aic);
 | 
			
		||||
	//				//}
 | 
			
		||||
	//			}
 | 
			
		||||
	//		})
 | 
			
		||||
	//		.Run();
 | 
			
		||||
	//	}
 | 
			
		||||
	//}
 | 
			
		||||
				}
 | 
			
		||||
			})
 | 
			
		||||
			.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();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user