mirror of
https://github.com/maxartz15/VertexAnimation.git
synced 2025-02-20 13:19:32 +01:00
Fixed LODs and Mesh generation.
This commit is contained in:
parent
c23fcfcfa4
commit
ee0fa7a5fa
@ -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);
|
||||||
|
@ -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))
|
||||||
|
49
Runtime/Scripts/AnimatedPrefabSpawner.cs
Normal file
49
Runtime/Scripts/AnimatedPrefabSpawner.cs
Normal 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 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
3
Runtime/Scripts/AnimatedPrefabSpawner.cs.meta
Normal file
3
Runtime/Scripts/AnimatedPrefabSpawner.cs.meta
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 2ed2332b4dc14154a8d3c0c6d737564d
|
||||||
|
timeCreated: 1670127953
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
@ -10,42 +10,64 @@ 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) =>
|
||||||
{
|
{
|
||||||
// Get the animation lib data.
|
if ( vaAnimatorStateComponent.Enabled )
|
||||||
ref VA_AnimationLibraryData animationsRef = ref animator.animationLibrary.Value;
|
|
||||||
|
|
||||||
// Lerp animations.
|
|
||||||
// Set animation for lerp.
|
|
||||||
int animationIndexNext = animator.animationIndexNext;
|
|
||||||
if (animator.animationIndexNext < 0)
|
|
||||||
{
|
{
|
||||||
animationIndexNext = animator.animationIndex;
|
// Get the animation lib data.
|
||||||
//animator.animationIndexNext = animationIndexNext + 1;
|
ref VA_AnimationLibraryData animationsRef = ref animator.animationLibrary.Value;
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate next frame time for lerp.
|
//if ( animator.AnimationName != vaAnimatorStateComponent.CurrentAnimationName )
|
||||||
float animationTimeNext = animator.animationTime + (1.0f / animationsRef.animations[animationIndexNext].maxFrames);
|
//{
|
||||||
if (animationTimeNext > animationsRef.animations[animationIndexNext].duration)
|
// // Set the animation index on the AnimatorComponent to play this animation.
|
||||||
{
|
// animator.animationIndex = VA_AnimationLibraryUtils.GetAnimation(ref animationsRef, vaAnimatorStateComponent.CurrentAnimationName);
|
||||||
// Set time. Using the difference to smooth out animations when looping.
|
// animator.AnimationName = vaAnimatorStateComponent.CurrentAnimationName;
|
||||||
animationTimeNext -= animator.animationTime;
|
//}
|
||||||
}
|
|
||||||
|
|
||||||
|
// 'Play' the actual animation.
|
||||||
|
animator.animationTime += deltaTime * animationsRef.animations[animator.animationIndex].frameTime;
|
||||||
|
|
||||||
for ( int i = 0; i < bones.Length; i++ )
|
if (animator.animationTime > animationsRef.animations[animator.animationIndex].duration)
|
||||||
{
|
|
||||||
VaAnimationDataComponent vaAnimationDataComponent = new VaAnimationDataComponent();
|
|
||||||
vaAnimationDataComponent.Value = new float4
|
|
||||||
{
|
{
|
||||||
x = animator.animationTime,
|
// Set time. Using the difference to smoothen out animations when looping.
|
||||||
y = VA_AnimationLibraryUtils.GetAnimationMapIndex( ref animationsRef, animator.animationIndex ),
|
animator.animationTime -= animationsRef.animations[animator.animationIndex].duration;
|
||||||
z = animationTimeNext,
|
}
|
||||||
w = VA_AnimationLibraryUtils.GetAnimationMapIndex( ref animationsRef, animationIndexNext )
|
|
||||||
};
|
// Lerp animations.
|
||||||
SystemAPI.SetComponent<VaAnimationDataComponent>( bones[i].Value, vaAnimationDataComponent );
|
// 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();
|
}).Run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user