VertexAnimation/Runtime/Scripts/AnimationLibraryComponentAuthoring.cs

130 lines
4.3 KiB
C#
Raw Normal View History

2022-12-04 15:47:07 +01:00
using System;
using System.Collections.Generic;
2022-12-04 15:47:07 +01:00
using Unity.Entities;
using Unity.Collections;
2022-12-04 15:47:07 +01:00
using Unity.Mathematics;
2022-12-04 04:18:35 +01:00
using UnityEngine;
using Hash128 = Unity.Entities.Hash128;
2022-12-04 15:47:07 +01:00
using Random = Unity.Mathematics.Random;
namespace TAO.VertexAnimation
{
2022-12-04 04:18:35 +01:00
[UnityEngine.DisallowMultipleComponent]
public class AnimationLibraryComponentAuthoring : UnityEngine.MonoBehaviour
{
public AnimationLibrary AnimationLibrary;
2022-12-04 04:18:35 +01:00
public bool DebugMode = false;
2022-12-04 15:47:07 +01:00
public uint Seed;
}
2022-12-04 04:18:35 +01:00
internal struct SkinnedMeshEntity : IBufferElementData
{
public Entity Value;
}
public struct AnimationLibraryComponent : IComponentData
2022-12-04 04:18:35 +01:00
{
public BlobAssetReference<VA_AnimationLibraryData> AnimLibAssetRef;
public BlobAssetStore BlobAssetStore;
}
public class AnimationLibraryComponentBaker : Baker < AnimationLibraryComponentAuthoring >
2022-12-04 04:18:35 +01:00
{
public override void Bake( AnimationLibraryComponentAuthoring authoring )
2022-12-04 04:18:35 +01:00
{
authoring.AnimationLibrary.Init();
AnimationLibraryComponent animationLibrary = new AnimationLibraryComponent();
2022-12-04 04:18:35 +01:00
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 15:47:07 +01:00
// Get the animation lib data.
ref VA_AnimationLibraryData animationsRef = ref animLib.Value;
Random random = new Random( authoring.Seed != 0 ? authoring.Seed : 42 );
2022-12-04 15:47:07 +01:00
int index = random.NextInt( 20 );
2022-12-04 04:18:35 +01:00
// Add animator to 'parent'.
VA_AnimatorComponent animatorComponent = new VA_AnimatorComponent
{
Enabled = true,
2022-12-04 15:47:07 +01:00
AnimationName = animationsRef.animations[index].name,
AnimationIndex = 2,
AnimationIndexNext = -1,
AnimationTime = 0,
AnimationLibrary = animLib
2022-12-04 04:18:35 +01:00
};
AddComponent(animatorComponent);
2022-12-04 15:47:07 +01:00
VA_AnimatorBlendStateComponent animatorStateComponent = new VA_AnimatorBlendStateComponent
2022-12-04 15:47:07 +01:00
{
BlendingEnabled = true,
AnimationIndex = 1,
2022-12-04 15:47:07 +01:00
AnimationIndexNext = -1,
AnimationTime = 0
2022-12-04 15:47:07 +01:00
};
AddComponent( animatorStateComponent );
2022-12-04 04:18:35 +01:00
var boneEntityArray = AddBuffer<SkinnedMeshEntity>();
MeshRenderer[] meshRenderers =
2022-12-04 04:18:35 +01:00
authoring.transform.GetComponentsInChildren < MeshRenderer >();
boneEntityArray.ResizeUninitialized(meshRenderers.Length);
for (int meshIndex = 0; meshIndex < meshRenderers.Length; ++meshIndex)
2022-12-04 04:18:35 +01:00
{
var meshEntity = GetEntity(meshRenderers[meshIndex]);
boneEntityArray[meshIndex] = new SkinnedMeshEntity {Value = meshEntity};
}
}
2022-12-04 04:18:35 +01:00
}
//[GenerateAuthoringComponent]
public struct VA_AnimatorComponent : IComponentData
{
public bool Enabled;
2022-12-04 15:47:07 +01:00
public FixedString64Bytes AnimationName;
public int AnimationIndex;
public int AnimationIndexNext;
public float AnimationTime;
public BlobAssetReference<VA_AnimationLibraryData> AnimationLibrary;
2022-12-04 04:18:35 +01:00
}
2022-12-04 15:47:07 +01:00
public struct VA_AnimatorBlendStateComponent : IComponentData
2022-12-04 15:47:07 +01:00
{
public bool BlendingEnabled;
2022-12-04 15:47:07 +01:00
public int AnimationIndex;
public int AnimationIndexNext;
public float AnimationTime;
2022-12-04 15:47:07 +01:00
}
}