Fixed LODs and Mesh generation.

This commit is contained in:
Maximilian Winter
2022-12-04 15:47:07 +01:00
parent c23fcfcfa4
commit ee0fa7a5fa
6 changed files with 170 additions and 47 deletions

View File

@ -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 );
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2ed2332b4dc14154a8d3c0c6d737564d
timeCreated: 1670127953

View File

@ -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<VA_AnimationLibraryData> 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<SkinnedMeshEntity>();
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<VA_AnimationLibraryData> animationLibrary;
}
public struct VA_AnimatorStateComponent : IComponentData
{
public bool Enabled;
public FixedString64Bytes CurrentAnimationName;
public int AnimationIndex;
public int AnimationIndexNext;
public Random Rand;
}
}

View File

@ -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<SkinnedMeshEntity> bones) =>
.ForEach((ref VA_AnimatorComponent animator, in VA_AnimatorStateComponent vaAnimatorStateComponent, in DynamicBuffer<SkinnedMeshEntity> 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<VaAnimationDataComponent>( 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<VaAnimationDataComponent>( bones[i].Value, vaAnimationDataComponent );
}
}
}).Run();
}
}