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

@ -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<LODGroup>();
//}
if (!parent.TryGetComponent(out LODGroup _))
{
parent.AddComponent<LODGroup>();
}
//if (!parent.TryGetComponent(out VA_AnimatorComponentAuthoring _))
//{
// parent.AddComponent<VA_AnimatorComponentAuthoring>();
//}
if (!parent.TryGetComponent(out VA_AnimationLibraryComponentAuthoring _))
{
parent.AddComponent<VA_AnimationLibraryComponentAuthoring>();
}
//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<VA_AnimationDataComponentAuthoring>();
}
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>();
//lodGroup.SetLODs(lods);
//lodGroup.RecalculateBounds();
var lodGroup = parent.GetComponent<LODGroup>();
lodGroup.SetLODs(lods);
lodGroup.RecalculateBounds();
// Create prefab.
GameObject prefab = PrefabUtility.SaveAsPrefabAssetAndConnect(parent, path, InteractionMode.AutomatedAction);

View File

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

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