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 UnityEngine;
using UnityEditor; using UnityEditor;
@ -14,15 +15,15 @@ namespace TAO.VertexAnimation.Editor
parent = PrefabUtility.LoadPrefabContents(path); parent = PrefabUtility.LoadPrefabContents(path);
// Check setup. // Check setup.
//if (!parent.TryGetComponent(out LODGroup _)) if (!parent.TryGetComponent(out LODGroup _))
//{ {
// parent.AddComponent<LODGroup>(); parent.AddComponent<LODGroup>();
//} }
//if (!parent.TryGetComponent(out VA_AnimatorComponentAuthoring _)) if (!parent.TryGetComponent(out VA_AnimationLibraryComponentAuthoring _))
//{ {
// parent.AddComponent<VA_AnimatorComponentAuthoring>(); parent.AddComponent<VA_AnimationLibraryComponentAuthoring>();
//} }
//if (!parent.TryGetComponent(out Unity.Entities.ConvertToEntity _)) //if (!parent.TryGetComponent(out Unity.Entities.ConvertToEntity _))
//{ //{
@ -32,12 +33,25 @@ namespace TAO.VertexAnimation.Editor
else else
{ {
// Create parent. // Create parent.
parent = new GameObject(name); parent = new GameObject(name, typeof(LODGroup), typeof(VA_AnimationLibraryComponentAuthoring));
} }
// Create all LODs. // Create all LODs.
LOD[] lods = new LOD[meshes.Length]; 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++) for (int i = 0; i < meshes.Length; i++)
{ {
string childName = string.Format("{0}_LOD{1}", name, i); string childName = string.Format("{0}_LOD{1}", name, i);
@ -51,10 +65,15 @@ namespace TAO.VertexAnimation.Editor
} }
else 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)) if (child.TryGetComponent(out MeshFilter mf))
{ {
mf.sharedMesh = meshes[i]; mf.sharedMesh = meshes[i];
@ -66,12 +85,12 @@ namespace TAO.VertexAnimation.Editor
} }
child.transform.SetParent(parent.transform); 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>(); var lodGroup = parent.GetComponent<LODGroup>();
//lodGroup.SetLODs(lods); lodGroup.SetLODs(lods);
//lodGroup.RecalculateBounds(); lodGroup.RecalculateBounds();
// Create prefab. // Create prefab.
GameObject prefab = PrefabUtility.SaveAsPrefabAssetAndConnect(parent, path, InteractionMode.AutomatedAction); GameObject prefab = PrefabUtility.SaveAsPrefabAssetAndConnect(parent, path, InteractionMode.AutomatedAction);

View File

@ -156,8 +156,8 @@ namespace TAO.VertexAnimation.Editor
// Get info. // Get info.
NamingConventionUtils.PositionMapInfo info = bakedData.GetPositionMap.name.GetTextureInfo(); NamingConventionUtils.PositionMapInfo info = bakedData.GetPositionMap.name.GetTextureInfo();
bakedData.mesh.SetTriangles( bakedData.mesh.triangles, 0 ); //bakedData.mesh.SetTriangles( bakedData.mesh.triangles, 0 );
meshes = new[] { bakedData.mesh }; //meshes = new[] { bakedData.mesh };
// Generate Material // Generate Material
if (!AssetDatabaseUtils.HasChildAsset(this, 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.Collections;
using Unity.Mathematics;
using UnityEngine; using UnityEngine;
using Hash128 = Unity.Entities.Hash128; using Hash128 = Unity.Entities.Hash128;
using Random = Unity.Mathematics.Random;
namespace TAO.VertexAnimation namespace TAO.VertexAnimation
{ {
@ -10,6 +13,7 @@ namespace TAO.VertexAnimation
{ {
public VA_AnimationLibrary AnimationLibrary; public VA_AnimationLibrary AnimationLibrary;
public bool DebugMode = false; public bool DebugMode = false;
public uint Seed;
} }
internal struct SkinnedMeshEntity : IBufferElementData internal struct SkinnedMeshEntity : IBufferElementData
@ -65,16 +69,32 @@ public class VA_AnimationLibraryComponentBaker : Baker < VA_AnimationLibraryComp
AddComponent( animationLibrary ); AddComponent( animationLibrary );
BlobAssetReference<VA_AnimationLibraryData> animLib = animationLibrary.AnimLibAssetRef; 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'. // Add animator to 'parent'.
VA_AnimatorComponent animatorComponent = new VA_AnimatorComponent VA_AnimatorComponent animatorComponent = new VA_AnimatorComponent
{ {
animationIndex = 0, AnimationName = animationsRef.animations[index].name,
animationIndex = index,
animationIndexNext = -1, animationIndexNext = -1,
animationTime = 0, animationTime = 0,
animationLibrary = animLib animationLibrary = animLib
}; };
AddComponent(animatorComponent); 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>(); var boneEntityArray = AddBuffer<SkinnedMeshEntity>();
MeshRenderer[] skinnedMeshRenderers = MeshRenderer[] skinnedMeshRenderers =
@ -92,9 +112,19 @@ public class VA_AnimationLibraryComponentBaker : Baker < VA_AnimationLibraryComp
//[GenerateAuthoringComponent] //[GenerateAuthoringComponent]
public struct VA_AnimatorComponent : IComponentData public struct VA_AnimatorComponent : IComponentData
{ {
public FixedString64Bytes AnimationName;
public int animationIndex; public int animationIndex;
public int animationIndexNext; public int animationIndexNext;
public float animationTime; public float animationTime;
public BlobAssetReference<VA_AnimationLibraryData> animationLibrary; 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,13 +10,33 @@ namespace TAO.VertexAnimation
{ {
protected override void OnUpdate() protected override void OnUpdate()
{ {
float deltaTime = SystemAPI.Time.DeltaTime;
// This is only executed if we have a valid skinning setup // This is only executed if we have a valid skinning setup
Entities Entities
.ForEach((VA_AnimatorComponent animator, in DynamicBuffer<SkinnedMeshEntity> bones) => .ForEach((ref VA_AnimatorComponent animator, in VA_AnimatorStateComponent vaAnimatorStateComponent, in DynamicBuffer<SkinnedMeshEntity> bones) =>
{
if ( vaAnimatorStateComponent.Enabled )
{ {
// Get the animation lib data. // Get the animation lib data.
ref VA_AnimationLibraryData animationsRef = ref animator.animationLibrary.Value; ref VA_AnimationLibraryData animationsRef = ref animator.animationLibrary.Value;
//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;
if (animator.animationTime > animationsRef.animations[animator.animationIndex].duration)
{
// Set time. Using the difference to smoothen out animations when looping.
animator.animationTime -= animationsRef.animations[animator.animationIndex].duration;
}
// Lerp animations. // Lerp animations.
// Set animation for lerp. // Set animation for lerp.
int animationIndexNext = animator.animationIndexNext; int animationIndexNext = animator.animationIndexNext;
@ -46,6 +66,8 @@ namespace TAO.VertexAnimation
}; };
SystemAPI.SetComponent<VaAnimationDataComponent>( bones[i].Value, vaAnimationDataComponent ); SystemAPI.SetComponent<VaAnimationDataComponent>( bones[i].Value, vaAnimationDataComponent );
} }
}
}).Run(); }).Run();
} }
} }