Fixed for Entities 1.0

This commit is contained in:
Maximilian Winter
2022-12-04 04:18:35 +01:00
parent 4499f67f31
commit 6fde2095aa
17 changed files with 1059 additions and 331 deletions

View File

@ -5,9 +5,103 @@
using UnityEngine;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine.Rendering;
namespace TAO.VertexAnimation
{
public static class SkinnedMeshCombiner
{
public static GameObject Combine(this SkinnedMeshRenderer target, List<SkinnedMeshRenderer> skinnedMeshRenderers, string name, Vector3 position, Quaternion rotation, Vector3 scale)
{
List<BoneWeight> boneWeights = new List<BoneWeight>();
List<Transform> bones = new List<Transform>();
List<CombineInstance> combineInstances = new List<CombineInstance>();
Material sharedMaterial = skinnedMeshRenderers[0].sharedMaterial;
Bounds newBounds = skinnedMeshRenderers[0].bounds;
int num = 0;
for( int i = 0; i < skinnedMeshRenderers.Count; ++i )
{
SkinnedMeshRenderer skinnedMeshRenderer = skinnedMeshRenderers[i];
BoneWeight[] bws = skinnedMeshRenderer.sharedMesh.boneWeights;
Transform[] bs = skinnedMeshRenderer.bones;
for( int bwIndex = 0; bwIndex < bws.Length; ++bwIndex )
{
BoneWeight boneWeight = bws[bwIndex];
boneWeight.boneIndex0 += num;
boneWeight.boneIndex1 += num;
boneWeight.boneIndex2 += num;
boneWeight.boneIndex3 += num;
boneWeights.Add( boneWeight );
}
num += bs.Length;
for( int boneIndex = 0; boneIndex < bs.Length; ++boneIndex )
{
bones.Add( bs[boneIndex] );
}
CombineInstance combineInstance = new CombineInstance()
{
mesh = skinnedMeshRenderer.sharedMesh,
transform = skinnedMeshRenderer.transform.localToWorldMatrix
};
combineInstances.Add( combineInstance );
if ( i > 0 )
{
newBounds.Encapsulate( skinnedMeshRenderers[i].bounds );
}
//skinnedMeshRenderer.enabled = false;
}
List<Matrix4x4> bindposes = new List<Matrix4x4>();
for( int i = 0; i < bones.Count; ++i )
{
Transform bone = bones[i];
bindposes.Add( bone.worldToLocalMatrix * target.transform.worldToLocalMatrix );
}
SkinnedMeshRenderer combinedSkinnedMeshRenderer = target;
combinedSkinnedMeshRenderer.updateWhenOffscreen = false;
combinedSkinnedMeshRenderer.sharedMesh = new Mesh();
combinedSkinnedMeshRenderer.sharedMesh.indexFormat = IndexFormat.UInt32;
if ( combineInstances.Count == 1 )
{
combinedSkinnedMeshRenderer.sharedMesh = combineInstances[0].mesh;
}
else
{
combinedSkinnedMeshRenderer.sharedMesh.CombineMeshes( combineInstances.ToArray(), true, true );
}
foreach ( CombineInstance combineInstance in combineInstances )
{
combinedSkinnedMeshRenderer.sharedMesh.subMeshCount += combineInstance.mesh.subMeshCount;
}
combinedSkinnedMeshRenderer.sharedMaterials = new Material[combinedSkinnedMeshRenderer.sharedMesh.subMeshCount];
for ( int i = 0; i < combinedSkinnedMeshRenderer.sharedMesh.subMeshCount; i++ )
{
combinedSkinnedMeshRenderer.sharedMaterials[i] = sharedMaterial;
}
//combinedSkinnedMeshRenderer.sharedMaterial = sharedMaterial;
combinedSkinnedMeshRenderer.bones = bones.ToArray();
combinedSkinnedMeshRenderer.sharedMesh.boneWeights = boneWeights.ToArray();
combinedSkinnedMeshRenderer.sharedMesh.bindposes = bindposes.ToArray();
combinedSkinnedMeshRenderer.sharedMesh.RecalculateBounds();
//combinedSkinnedMeshRenderer.localBounds = new Bounds( new Vector3( 0.0f, 1.0f, 0.0f ), new Vector3( 0.5f, 1.0f, 0.5f ) );
//AssetDatabase.CreateAsset( combinedSkinnedMeshRenderer.sharedMesh, $"Assets/CombinedSkinnedMeshRendererPrefabs/{name}(Mesh{System.DateTime.Now:MM_dd_yyyy-H_mm}).asset" );
//AssetDatabase.SaveAssets();
return target.gameObject;
}
}
public static class MeshCombiner
{
private struct MaterialMeshGroup
@ -262,7 +356,7 @@ namespace TAO.VertexAnimation
// Add target mesh.
SkinnedMeshRenderer target = gameObject.AddComponent<SkinnedMeshRenderer>();
target.Combine(skinnedMeshes, meshes);
target.Combine(skinnedMeshes, gameObject.name, gameObject.transform.position, gameObject.transform.rotation, gameObject.transform.localScale);
}
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: f43224daff50a5042a182c6fb12440a8
guid: e9d4c3f791214a29ac7c1238c9381900
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -16,13 +16,13 @@ namespace TAO.VertexAnimation
public VA_AnimationData GetData()
{
// TODO: Fix data name, FixedString32 doesn't transfer from editor?
Data.name = new FixedString64(name);
Data.name = new FixedString64Bytes(name);
return Data;
}
public FixedString64 GetName()
public FixedString64Bytes GetName()
{
return new FixedString64(this.name);
return new FixedString64Bytes(this.name);
}
}
}

View File

@ -0,0 +1,21 @@
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;
namespace TAO.VertexAnimation
{
public class VA_AnimationDataComponentAuthoring : MonoBehaviour
{
public float4 Color;
}
public class VA_AnimationDataBaker : Baker < VA_AnimationDataComponentAuthoring >
{
public override void Bake( VA_AnimationDataComponentAuthoring authoring )
{
//Entity parent = GetEntity( authoring.RootParent );
AddComponent( new VaAnimationDataComponent{ Value = authoring.Color} );
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4fed86d3fa104b829db2922ca534f704
timeCreated: 1670034530

View File

@ -1,64 +1,100 @@
using Unity.Entities;
using Unity.Collections;
using UnityEngine;
using Hash128 = Unity.Entities.Hash128;
namespace TAO.VertexAnimation
{
[UnityEngine.RequireComponent(typeof(ConvertToEntity))]
[UnityEngine.DisallowMultipleComponent]
[UnityEngine.DisallowMultipleComponent]
public class VA_AnimationLibraryComponentAuthoring : UnityEngine.MonoBehaviour
{
public VA_AnimationLibrary animationLibrary;
public bool debugMode = false;
public VA_AnimationLibrary AnimationLibrary;
public bool DebugMode = false;
}
public class VA_AnimationLibraryConversionSystem : GameObjectConversionSystem
internal struct SkinnedMeshEntity : IBufferElementData
{
public Entity Value;
}
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 )
{
// Static because of multi scene setup.
public static BlobAssetReference<VA_AnimationLibraryData> animLibAssetRef;
protected override void OnUpdate()
authoring.AnimationLibrary.Init();
VA_AnimationLibraryComponent animationLibrary = new VA_AnimationLibraryComponent();
using (BlobBuilder blobBuilder = new BlobBuilder(Allocator.Temp))
{
Entities.ForEach((VA_AnimationLibraryComponentAuthoring animationLib) =>
// Construct the root.
ref VA_AnimationLibraryData animationDataBlobAsset = ref blobBuilder.ConstructRoot<VA_AnimationLibraryData>();
// Set all the data.
BlobBuilderArray<VA_AnimationData> animationDataArray = blobBuilder.Allocate(ref animationDataBlobAsset.animations, authoring.AnimationLibrary.animationData.Count);
for (int i = 0; i < animationDataArray.Length; i++)
{
animationLib.animationLibrary.Init();
// Copy data.
animationDataArray[i] = authoring.AnimationLibrary.animationData[i];
// Blob builder to build.
using (BlobBuilder blobBuilder = new BlobBuilder(Allocator.Temp))
if (authoring.DebugMode)
{
// Construct the root.
ref VA_AnimationLibraryData animationDataBlobAsset = ref blobBuilder.ConstructRoot<VA_AnimationLibraryData>();
// Set all the data.
BlobBuilderArray<VA_AnimationData> animationDataArray = blobBuilder.Allocate(ref animationDataBlobAsset.animations, animationLib.animationLibrary.animationData.Count);
for (int i = 0; i < animationDataArray.Length; i++)
{
// Copy data.
animationDataArray[i] = animationLib.animationLibrary.animationData[i];
if (animationLib.debugMode)
{
UnityEngine.Debug.Log("VA_AnimationLibrary added " + animationDataArray[i].name.ToString());
}
}
// Construct blob asset reference.
//BlobAssetReference<VA_AnimationLibraryData> animLibAssetRef = blobBuilder.CreateBlobAssetReference<VA_AnimationLibraryData>(Allocator.Persistent);
// Static because of multi scene setup.
animLibAssetRef = blobBuilder.CreateBlobAssetReference<VA_AnimationLibraryData>(Allocator.Persistent);
// Add it to the asset store.
BlobAssetStore.TryAdd(new Hash128(VA_AnimationLibraryUtils.AnimationLibraryAssetStoreName), animLibAssetRef);
if (animationLib.debugMode)
{
UnityEngine.Debug.Log("VA_AnimationLibrary has " + animLibAssetRef.Value.animations.Length.ToString() + " animations.");
}
UnityEngine.Debug.Log("VA_AnimationLibrary added " + animationDataArray[i].name.ToString());
}
}
// Remove the entity since we don't need it anymore.
DstEntityManager.DestroyEntity(GetPrimaryEntity(animationLib));
});
// 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);
if (authoring.DebugMode)
{
UnityEngine.Debug.Log("VA_AnimationLibrary has " + animationLibrary.AnimLibAssetRef.Value.animations.Length.ToString() + " animations.");
}
}
AddComponent( animationLibrary );
BlobAssetReference<VA_AnimationLibraryData> animLib = animationLibrary.AnimLibAssetRef;
// Add animator to 'parent'.
VA_AnimatorComponent animatorComponent = new VA_AnimatorComponent
{
animationIndex = 0,
animationIndexNext = -1,
animationTime = 0,
animationLibrary = animLib
};
AddComponent(animatorComponent);
var boneEntityArray = AddBuffer<SkinnedMeshEntity>();
MeshRenderer[] skinnedMeshRenderers =
authoring.transform.GetComponentsInChildren < MeshRenderer >();
boneEntityArray.ResizeUninitialized(skinnedMeshRenderers.Length);
for (int boneIndex = 0; boneIndex < skinnedMeshRenderers.Length; ++boneIndex)
{
var boneEntity = GetEntity(skinnedMeshRenderers[boneIndex]);
boneEntityArray[boneIndex] = new SkinnedMeshEntity {Value = boneEntity};
}
}
}
//[GenerateAuthoringComponent]
public struct VA_AnimatorComponent : IComponentData
{
public int animationIndex;
public int animationIndexNext;
public float animationTime;
public BlobAssetReference<VA_AnimationLibraryData> animationLibrary;
}
}

View File

@ -6,7 +6,7 @@ namespace TAO.VertexAnimation
[System.Serializable]
public struct VA_AnimationData
{
public VA_AnimationData(FixedString64 a_name, int a_frames, int a_maxFrames, int a_fps, int a_positionMapIndex, int a_colorMapIndex = -1)
public VA_AnimationData(FixedString64Bytes a_name, int a_frames, int a_maxFrames, int a_fps, int a_positionMapIndex, int a_colorMapIndex = -1)
{
name = a_name;
frames = a_frames;
@ -18,7 +18,7 @@ namespace TAO.VertexAnimation
}
// The name of the animation.
public FixedString64 name;
public FixedString64Bytes name;
// The frames in this animation.
public int frames;
// The maximum of frames the texture holds.
@ -42,7 +42,7 @@ namespace TAO.VertexAnimation
{
public const string AnimationLibraryAssetStoreName = "VA_AnimationLibrary";
public static int GetAnimation(ref VA_AnimationLibraryData animationsRef, FixedString64 animationName)
public static int GetAnimation(ref VA_AnimationLibraryData animationsRef, FixedString64Bytes animationName)
{
for (int i = 0; i < animationsRef.animations.Length; i++)
{

View File

@ -5,54 +5,5 @@ using UnityEngine;
namespace TAO.VertexAnimation
{
[DisallowMultipleComponent]
public class VA_AnimatorComponentAuthoring : MonoBehaviour
{
}
//[GenerateAuthoringComponent]
public struct VA_AnimatorComponent : IComponentData
{
public int animationIndex;
public int animationIndexNext;
public float animationTime;
public BlobAssetReference<VA_AnimationLibraryData> animationLibrary;
}
[UpdateAfter(typeof(VA_AnimationLibraryConversionSystem))]
public class VA_AnimatorConversionSystem : GameObjectConversionSystem
{
protected override void OnUpdate()
{
//BlobAssetStore.TryGet(new Unity.Entities.Hash128(VA_AnimationLibraryUtils.AnimationLibraryAssetStoreName), out BlobAssetReference<VA_AnimationLibraryData> animLib);
// Static because of multi scene setup.
BlobAssetReference<VA_AnimationLibraryData> animLib = VA_AnimationLibraryConversionSystem.animLibAssetRef;
Entities.ForEach((VA_AnimatorComponentAuthoring animator) =>
{
Entity entity = GetPrimaryEntity(animator);
// Add animator to 'parent'.
VA_AnimatorComponent animatorComponent = new VA_AnimatorComponent
{
animationIndex = 0,
animationIndexNext = -1,
animationTime = 0,
animationLibrary = animLib
};
DstEntityManager.AddComponentData(entity, animatorComponent);
// Add the Material data to the children.
var children = animator.GetComponentsInChildren<MeshRenderer>();
for (int i = 0; i < children.Length; i++)
{
Entity ent = GetPrimaryEntity(children[i]);
VA_AnimationDataComponent animationData = new VA_AnimationDataComponent();
DstEntityManager.AddComponentData(ent, animationData);
}
});
}
}
}

View File

@ -1,58 +1,52 @@
using Unity.Entities;
using System.Collections.Generic;
using Unity.Entities;
using Unity.Transforms;
using Unity.Mathematics;
namespace TAO.VertexAnimation
{
// System to update all the animations.
public class VA_AnimatorSystem : SystemBase
public partial class VA_AnimatorSystem : SystemBase
{
protected override void OnUpdate()
{
var animationData = GetComponentDataFromEntity<VA_AnimationDataComponent>(false);
Entities.ForEach((ref VA_AnimatorComponent ac, in DynamicBuffer<Child> children) =>
{
for (int i = 0; i < children.Length; i++)
// This is only executed if we have a valid skinning setup
Entities
.ForEach((VA_AnimatorComponent animator, in DynamicBuffer<SkinnedMeshEntity> bones) =>
{
// Get child.
Entity child = children[i].Value;
// Get the animation lib data.
ref VA_AnimationLibraryData animationsRef = ref ac.animationLibrary.Value;
ref VA_AnimationLibraryData animationsRef = ref animator.animationLibrary.Value;
// Lerp animations.
// Set animation for lerp.
int animationIndexNext = ac.animationIndexNext;
if (ac.animationIndexNext < 0)
int animationIndexNext = animator.animationIndexNext;
if (animator.animationIndexNext < 0)
{
animationIndexNext = ac.animationIndex;
animationIndexNext = animator.animationIndex;
//animator.animationIndexNext = animationIndexNext + 1;
}
// Calculate next frame time for lerp.
float animationTimeNext = ac.animationTime + (1.0f / animationsRef.animations[animationIndexNext].maxFrames);
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 -= ac.animationTime;
animationTimeNext -= animator.animationTime;
}
// Set material data.
animationData[child] = new VA_AnimationDataComponent
for ( int i = 0; i < bones.Length; i++ )
{
Value = new float4
VaAnimationDataComponent vaAnimationDataComponent = new VaAnimationDataComponent();
vaAnimationDataComponent.Value = new float4
{
x = ac.animationTime,
y = VA_AnimationLibraryUtils.GetAnimationMapIndex(ref animationsRef, ac.animationIndex),
x = animator.animationTime,
y = VA_AnimationLibraryUtils.GetAnimationMapIndex( ref animationsRef, animator.animationIndex ),
z = animationTimeNext,
w = VA_AnimationLibraryUtils.GetAnimationMapIndex(ref animationsRef, animationIndexNext)
}
};
}
})
.WithNativeDisableContainerSafetyRestriction(animationData)
.WithName("VA_AnimatorSystem")
.ScheduleParallel();
w = VA_AnimationLibraryUtils.GetAnimationMapIndex( ref animationsRef, animationIndexNext )
};
SystemAPI.SetComponent<VaAnimationDataComponent>( bones[i].Value, vaAnimationDataComponent );
}
}).Run();
}
}
}

View File

@ -4,10 +4,12 @@ using Unity.Rendering;
namespace TAO.VertexAnimation
{
[MaterialProperty("_AnimationData", MaterialPropertyFormat.Float4)]
public struct VA_AnimationDataComponent : IComponentData
{
// animationTime, animationIndex, colorIndex, nan.
public float4 Value;
}
[MaterialProperty("_AnimationData")] //, MaterialPropertyFormat.Float4
public struct VaAnimationDataComponent : IComponentData
{
// animationTime, animationIndex, colorIndex, nan.
public float4 Value;
}
}