Apply root motion option.

Added option to apply root motion.
This commit is contained in:
max 2021-01-21 10:13:29 +01:00
parent 468d1472c2
commit 1c9ab36bd8
3 changed files with 14 additions and 4 deletions

View File

@ -28,6 +28,7 @@ namespace TAO.VertexAnimation.Editor
{
EditorGUILayout.PropertyField(serializedObject.FindProperty("model"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("animationClips"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("applyRootMotion"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("fps"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("textureWidth"));
}

View File

@ -12,6 +12,7 @@ namespace TAO.VertexAnimation.Editor
// Input.
public GameObject model;
public AnimationClip[] animationClips;
public bool applyRootMotion = false;
[Range(1, 60)]
public int fps = 24;
public int textureWidth = 512;
@ -97,7 +98,7 @@ namespace TAO.VertexAnimation.Editor
target.name = model.name;
target.ConbineAndConvertGameObject();
AnimationBaker.BakedData bakedData = target.Bake(animationClips, fps, textureWidth);
AnimationBaker.BakedData bakedData = target.Bake(animationClips, applyRootMotion, fps, textureWidth);
positionMap = VA_Texture2DArrayUtils.CreateTextureArray(bakedData.positionMaps.ToArray(), false, true, TextureWrapMode.Repeat, FilterMode.Point, 1, string.Format("{0}_PositionMap", name), true);
meshes = bakedData.mesh.GenerateLOD(lodSettings.LODCount(), lodSettings.GetQualitySettings());

View File

@ -25,6 +25,7 @@ namespace TAO.VertexAnimation
[System.Serializable]
public struct AnimationInfo
{
public bool applyRootMotion;
public int rawFrameHeight;
public int frameHeight;
public int frameSpacing;
@ -35,8 +36,9 @@ namespace TAO.VertexAnimation
public int fps;
// Create animation info and calculate values.
public AnimationInfo(Mesh mesh, int frames, int textureWidth, int fps)
public AnimationInfo(Mesh mesh, bool applyRootMotion, int frames, int textureWidth, int fps)
{
this.applyRootMotion = applyRootMotion;
this.frames = frames;
this.textureWidth = textureWidth;
this.fps = fps;
@ -51,7 +53,7 @@ namespace TAO.VertexAnimation
}
}
public static BakedData Bake(this GameObject model, AnimationClip[] animationClips, int fps, int textureWidth)
public static BakedData Bake(this GameObject model, AnimationClip[] animationClips, bool applyRootMotion, int fps, int textureWidth)
{
BakedData bakedData = new BakedData()
{
@ -75,7 +77,7 @@ namespace TAO.VertexAnimation
Mesh mesh = model.GetComponent<SkinnedMeshRenderer>().sharedMesh;
// Get the info for the biggest animation.
AnimationInfo animationInfo = new AnimationInfo(mesh, maxFrames, textureWidth, fps);
AnimationInfo animationInfo = new AnimationInfo(mesh, applyRootMotion, maxFrames, textureWidth, fps);
foreach (AnimationClip ac in animationClips)
{
@ -98,6 +100,12 @@ namespace TAO.VertexAnimation
name = string.Format("{0}", model.name)
};
// Set root motion options.
if (model.TryGetComponent(out Animator animator))
{
animator.applyRootMotion = animationInfo.applyRootMotion;
}
// Bake mesh for a copy and to apply the new UV's to.
SkinnedMeshRenderer skinnedMeshRenderer = model.GetComponent<SkinnedMeshRenderer>();
skinnedMeshRenderer.BakeMesh(mesh);