VertexAnimation/Runtime/Scripts/VA_AnimationLibraryComponentAuthoring.cs

100 lines
3.4 KiB
C#
Raw Normal View History

using Unity.Entities;
using Unity.Collections;
2022-12-04 04:18:35 +01:00
using UnityEngine;
using Hash128 = Unity.Entities.Hash128;
namespace TAO.VertexAnimation
{
2022-12-04 04:18:35 +01:00
[UnityEngine.DisallowMultipleComponent]
public class VA_AnimationLibraryComponentAuthoring : UnityEngine.MonoBehaviour
{
2022-12-04 04:18:35 +01:00
public VA_AnimationLibrary AnimationLibrary;
public bool DebugMode = false;
}
2022-12-04 04:18:35 +01:00
internal struct SkinnedMeshEntity : IBufferElementData
{
public Entity Value;
}
2022-12-04 04:18:35 +01:00
public struct VA_AnimationLibraryComponent : IComponentData
{
public BlobAssetReference<VA_AnimationLibraryData> AnimLibAssetRef;
public BlobAssetStore BlobAssetStore;
}
public class VA_AnimationLibraryComponentBaker : Baker < VA_AnimationLibraryComponentAuthoring >
{
public override void Bake( VA_AnimationLibraryComponentAuthoring authoring )
{
authoring.AnimationLibrary.Init();
VA_AnimationLibraryComponent animationLibrary = new VA_AnimationLibraryComponent();
using (BlobBuilder blobBuilder = new BlobBuilder(Allocator.Temp))
{
2022-12-04 04:18:35 +01:00
// Construct the root.
ref VA_AnimationLibraryData animationDataBlobAsset = ref blobBuilder.ConstructRoot<VA_AnimationLibraryData>();
2022-12-04 04:18:35 +01:00
// Set all the data.
BlobBuilderArray<VA_AnimationData> animationDataArray = blobBuilder.Allocate(ref animationDataBlobAsset.animations, authoring.AnimationLibrary.animationData.Count);
2022-12-04 04:18:35 +01:00
for (int i = 0; i < animationDataArray.Length; i++)
{
// Copy data.
animationDataArray[i] = authoring.AnimationLibrary.animationData[i];
2022-12-04 04:18:35 +01:00
if (authoring.DebugMode)
{
UnityEngine.Debug.Log("VA_AnimationLibrary added " + animationDataArray[i].name.ToString());
}
}
2022-12-04 04:18:35 +01:00
// Construct blob asset reference.
//BlobAssetReference<VA_AnimationLibraryData> animLibAssetRef = blobBuilder.CreateBlobAssetReference<VA_AnimationLibraryData>(Allocator.Persistent);
// Static because of multi scene setup.
animationLibrary.AnimLibAssetRef = blobBuilder.CreateBlobAssetReference<VA_AnimationLibraryData>(Allocator.Persistent);
Hash128 hash128 = new Hash128( VA_AnimationLibraryUtils.AnimationLibraryAssetStoreName );
// Add it to the asset store.
animationLibrary.BlobAssetStore = new BlobAssetStore( 50);
animationLibrary.BlobAssetStore.TryAdd(hash128, ref animationLibrary.AnimLibAssetRef);
2022-12-04 04:18:35 +01:00
if (authoring.DebugMode)
{
UnityEngine.Debug.Log("VA_AnimationLibrary has " + animationLibrary.AnimLibAssetRef.Value.animations.Length.ToString() + " animations.");
}
}
AddComponent( animationLibrary );
BlobAssetReference<VA_AnimationLibraryData> animLib = animationLibrary.AnimLibAssetRef;
2022-12-04 04:18:35 +01:00
// Add animator to 'parent'.
VA_AnimatorComponent animatorComponent = new VA_AnimatorComponent
{
animationIndex = 0,
animationIndexNext = -1,
animationTime = 0,
animationLibrary = animLib
};
AddComponent(animatorComponent);
var boneEntityArray = AddBuffer<SkinnedMeshEntity>();
2022-12-04 04:18:35 +01:00
MeshRenderer[] skinnedMeshRenderers =
authoring.transform.GetComponentsInChildren < MeshRenderer >();
boneEntityArray.ResizeUninitialized(skinnedMeshRenderers.Length);
2022-12-04 04:18:35 +01:00
for (int boneIndex = 0; boneIndex < skinnedMeshRenderers.Length; ++boneIndex)
{
var boneEntity = GetEntity(skinnedMeshRenderers[boneIndex]);
boneEntityArray[boneIndex] = new SkinnedMeshEntity {Value = boneEntity};
}
}
2022-12-04 04:18:35 +01:00
}
//[GenerateAuthoringComponent]
public struct VA_AnimatorComponent : IComponentData
{
public int animationIndex;
public int animationIndexNext;
public float animationTime;
public BlobAssetReference<VA_AnimationLibraryData> animationLibrary;
}
}