diff --git a/Editor/Scripts/ModelBaker/AnimationPrefab.cs b/Editor/Scripts/ModelBaker/AnimationPrefab.cs index 68d450b..2bdeb94 100644 --- a/Editor/Scripts/ModelBaker/AnimationPrefab.cs +++ b/Editor/Scripts/ModelBaker/AnimationPrefab.cs @@ -1,3 +1,4 @@ +using System.IO; using UnityEngine; using UnityEditor; @@ -14,15 +15,15 @@ namespace TAO.VertexAnimation.Editor parent = PrefabUtility.LoadPrefabContents(path); // Check setup. - //if (!parent.TryGetComponent(out LODGroup _)) - //{ - // parent.AddComponent(); - //} + if (!parent.TryGetComponent(out LODGroup _)) + { + parent.AddComponent(); + } - //if (!parent.TryGetComponent(out VA_AnimatorComponentAuthoring _)) - //{ - // parent.AddComponent(); - //} + if (!parent.TryGetComponent(out VA_AnimationLibraryComponentAuthoring _)) + { + parent.AddComponent(); + } //if (!parent.TryGetComponent(out Unity.Entities.ConvertToEntity _)) //{ @@ -32,12 +33,25 @@ namespace TAO.VertexAnimation.Editor else { // Create parent. - parent = new GameObject(name); + parent = new GameObject(name, typeof(LODGroup), typeof(VA_AnimationLibraryComponentAuthoring)); } // Create all LODs. LOD[] lods = new LOD[meshes.Length]; + //string meshPath = "Assets/Mesh" + parent.name; + //int index = 0; + //foreach ( Mesh mesh in meshes ) + //{ + // if ( !AssetDatabaseUtils.HasAsset( meshPath + index + ".asset", typeof( Mesh ) ) ) + // { + // AssetDatabase.CreateAsset( mesh, meshPath + index + ".asset" ); + // } +// + // index++; + //} + + AssetDatabase.SaveAssets(); for (int i = 0; i < meshes.Length; i++) { string childName = string.Format("{0}_LOD{1}", name, i); @@ -51,10 +65,15 @@ namespace TAO.VertexAnimation.Editor } else { - child = new GameObject(childName, typeof(MeshFilter), typeof(MeshRenderer)); + child = new GameObject(childName, typeof(MeshFilter), typeof(MeshRenderer), typeof(VA_AnimationDataComponentAuthoring)); } } + if (!child.TryGetComponent(out VA_AnimationDataComponentAuthoring ad)) + { + child.AddComponent(); + } + if (child.TryGetComponent(out MeshFilter mf)) { mf.sharedMesh = meshes[i]; @@ -66,12 +85,12 @@ namespace TAO.VertexAnimation.Editor } child.transform.SetParent(parent.transform); - //lods[i] = new LOD(lodTransitions[i], new Renderer[1] { mr }); + lods[i] = new LOD(lodTransitions[i], new Renderer[1] { mr }); } - //var lodGroup = parent.GetComponent(); - //lodGroup.SetLODs(lods); - //lodGroup.RecalculateBounds(); + var lodGroup = parent.GetComponent(); + lodGroup.SetLODs(lods); + lodGroup.RecalculateBounds(); // Create prefab. GameObject prefab = PrefabUtility.SaveAsPrefabAssetAndConnect(parent, path, InteractionMode.AutomatedAction); diff --git a/Editor/Scripts/ModelBaker/VA_ModelBaker.cs b/Editor/Scripts/ModelBaker/VA_ModelBaker.cs index 9046439..061a897 100644 --- a/Editor/Scripts/ModelBaker/VA_ModelBaker.cs +++ b/Editor/Scripts/ModelBaker/VA_ModelBaker.cs @@ -156,8 +156,8 @@ namespace TAO.VertexAnimation.Editor // Get info. NamingConventionUtils.PositionMapInfo info = bakedData.GetPositionMap.name.GetTextureInfo(); - bakedData.mesh.SetTriangles( bakedData.mesh.triangles, 0 ); - meshes = new[] { bakedData.mesh }; + //bakedData.mesh.SetTriangles( bakedData.mesh.triangles, 0 ); + //meshes = new[] { bakedData.mesh }; // Generate Material if (!AssetDatabaseUtils.HasChildAsset(this, material)) diff --git a/Runtime/Scripts/AnimatedPrefabSpawner.cs b/Runtime/Scripts/AnimatedPrefabSpawner.cs new file mode 100644 index 0000000..8968ce8 --- /dev/null +++ b/Runtime/Scripts/AnimatedPrefabSpawner.cs @@ -0,0 +1,49 @@ +using System; +using UnityEngine; + +namespace TAO.VertexAnimation +{ + +public class AnimatedPrefabSpawner : MonoBehaviour +{ + public GameObject Prefab; + + public Transform BottomLeftCorner; + + public Transform Parent; + + public int Width; + public int Height; + + public float Distance; + + [ContextMenu("Test")] + public void SetAllSeeds() + { + + Vector3 currentPosition = BottomLeftCorner.position; + Vector3 startPosition = currentPosition; + for ( int i = 0; i < Width; i++ ) + { + for ( int j = 0; j < Height; j++ ) + { + GameObject instance = Instantiate( Prefab, Parent, true ); + instance.transform.position = currentPosition; + currentPosition = new Vector3( currentPosition.x + Distance, currentPosition.y, currentPosition.z ); + } + currentPosition = new Vector3( startPosition.x , currentPosition.y, currentPosition.z + Distance ); + } + VA_AnimationLibraryComponentAuthoring[] vaAnimationLibraryComponentAuthorings = Parent.GetComponentsInChildren < VA_AnimationLibraryComponentAuthoring >(); + + foreach ( VA_AnimationLibraryComponentAuthoring authoring in vaAnimationLibraryComponentAuthorings ) + { + var test = Guid.NewGuid().GetHashCode().ToString(); + Debug.Log( test ); + test = test.Substring( test.Length - 4 ); + Debug.Log( UInt32.Parse( test ) ); + authoring.Seed = UInt32.Parse( test ); + } + } +} + +} diff --git a/Runtime/Scripts/AnimatedPrefabSpawner.cs.meta b/Runtime/Scripts/AnimatedPrefabSpawner.cs.meta new file mode 100644 index 0000000..28a8ff1 --- /dev/null +++ b/Runtime/Scripts/AnimatedPrefabSpawner.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 2ed2332b4dc14154a8d3c0c6d737564d +timeCreated: 1670127953 \ No newline at end of file diff --git a/Runtime/Scripts/VA_AnimationLibraryComponentAuthoring.cs b/Runtime/Scripts/VA_AnimationLibraryComponentAuthoring.cs index 00c0be6..cbef7c9 100644 --- a/Runtime/Scripts/VA_AnimationLibraryComponentAuthoring.cs +++ b/Runtime/Scripts/VA_AnimationLibraryComponentAuthoring.cs @@ -1,7 +1,10 @@ -using Unity.Entities; +using System; +using Unity.Entities; using Unity.Collections; +using Unity.Mathematics; using UnityEngine; using Hash128 = Unity.Entities.Hash128; +using Random = Unity.Mathematics.Random; namespace TAO.VertexAnimation { @@ -10,6 +13,7 @@ namespace TAO.VertexAnimation { public VA_AnimationLibrary AnimationLibrary; public bool DebugMode = false; + public uint Seed; } internal struct SkinnedMeshEntity : IBufferElementData @@ -65,16 +69,32 @@ public class VA_AnimationLibraryComponentBaker : Baker < VA_AnimationLibraryComp AddComponent( animationLibrary ); BlobAssetReference animLib = animationLibrary.AnimLibAssetRef; - + // Get the animation lib data. + ref VA_AnimationLibraryData animationsRef = ref animLib.Value; + Random random = new Random( authoring.Seed ); + int index = random.NextInt( 20 ); // Add animator to 'parent'. VA_AnimatorComponent animatorComponent = new VA_AnimatorComponent { - animationIndex = 0, + AnimationName = animationsRef.animations[index].name, + animationIndex = index, animationIndexNext = -1, animationTime = 0, animationLibrary = animLib }; AddComponent(animatorComponent); + + + VA_AnimatorStateComponent animatorStateComponent = new VA_AnimatorStateComponent + { + Enabled = true, + CurrentAnimationName = animationsRef.animations[index].name, + AnimationIndex = index, + AnimationIndexNext = -1, + }; + + AddComponent( animatorStateComponent ); + var boneEntityArray = AddBuffer(); MeshRenderer[] skinnedMeshRenderers = @@ -92,9 +112,19 @@ public class VA_AnimationLibraryComponentBaker : Baker < VA_AnimationLibraryComp //[GenerateAuthoringComponent] public struct VA_AnimatorComponent : IComponentData { + public FixedString64Bytes AnimationName; public int animationIndex; public int animationIndexNext; public float animationTime; public BlobAssetReference animationLibrary; } + +public struct VA_AnimatorStateComponent : IComponentData +{ + public bool Enabled; + public FixedString64Bytes CurrentAnimationName; + public int AnimationIndex; + public int AnimationIndexNext; + public Random Rand; +} } \ No newline at end of file diff --git a/Runtime/Scripts/VA_AnimatorSystem.cs b/Runtime/Scripts/VA_AnimatorSystem.cs index 0b9a6e3..e1b2d4d 100644 --- a/Runtime/Scripts/VA_AnimatorSystem.cs +++ b/Runtime/Scripts/VA_AnimatorSystem.cs @@ -10,42 +10,64 @@ namespace TAO.VertexAnimation { protected override void OnUpdate() { + float deltaTime = SystemAPI.Time.DeltaTime; // This is only executed if we have a valid skinning setup Entities - .ForEach((VA_AnimatorComponent animator, in DynamicBuffer bones) => + .ForEach((ref VA_AnimatorComponent animator, in VA_AnimatorStateComponent vaAnimatorStateComponent, in DynamicBuffer bones) => { - // Get the animation lib data. - ref VA_AnimationLibraryData animationsRef = ref animator.animationLibrary.Value; - - // Lerp animations. - // Set animation for lerp. - int animationIndexNext = animator.animationIndexNext; - if (animator.animationIndexNext < 0) + if ( vaAnimatorStateComponent.Enabled ) { - animationIndexNext = animator.animationIndex; - //animator.animationIndexNext = animationIndexNext + 1; - } + // Get the animation lib data. + ref VA_AnimationLibraryData animationsRef = ref animator.animationLibrary.Value; - // Calculate next frame time for lerp. - float animationTimeNext = animator.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 -= animator.animationTime; - } + //if ( animator.AnimationName != vaAnimatorStateComponent.CurrentAnimationName ) + //{ + // // Set the animation index on the AnimatorComponent to play this animation. + // animator.animationIndex = VA_AnimationLibraryUtils.GetAnimation(ref animationsRef, vaAnimatorStateComponent.CurrentAnimationName); + // animator.AnimationName = vaAnimatorStateComponent.CurrentAnimationName; + //} + + + // 'Play' the actual animation. + animator.animationTime += deltaTime * animationsRef.animations[animator.animationIndex].frameTime; - for ( int i = 0; i < bones.Length; i++ ) - { - VaAnimationDataComponent vaAnimationDataComponent = new VaAnimationDataComponent(); - vaAnimationDataComponent.Value = new float4 + if (animator.animationTime > animationsRef.animations[animator.animationIndex].duration) { - x = animator.animationTime, - y = VA_AnimationLibraryUtils.GetAnimationMapIndex( ref animationsRef, animator.animationIndex ), - z = animationTimeNext, - w = VA_AnimationLibraryUtils.GetAnimationMapIndex( ref animationsRef, animationIndexNext ) - }; - SystemAPI.SetComponent( bones[i].Value, vaAnimationDataComponent ); + // Set time. Using the difference to smoothen out animations when looping. + animator.animationTime -= animationsRef.animations[animator.animationIndex].duration; + } + + // Lerp animations. + // Set animation for lerp. + int animationIndexNext = animator.animationIndexNext; + if (animator.animationIndexNext < 0) + { + animationIndexNext = animator.animationIndex; + //animator.animationIndexNext = animationIndexNext + 1; + } + + // Calculate next frame time for lerp. + float animationTimeNext = animator.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 -= animator.animationTime; + } + + for ( int i = 0; i < bones.Length; i++ ) + { + VaAnimationDataComponent vaAnimationDataComponent = new VaAnimationDataComponent(); + vaAnimationDataComponent.Value = new float4 + { + x = animator.animationTime, + y = VA_AnimationLibraryUtils.GetAnimationMapIndex( ref animationsRef, animator.animationIndex ), + z = animationTimeNext, + w = VA_AnimationLibraryUtils.GetAnimationMapIndex( ref animationsRef, animationIndexNext ) + }; + SystemAPI.SetComponent( bones[i].Value, vaAnimationDataComponent ); + } } + }).Run(); } }