From 1c9ab36bd89d5940c9b8e0942580280886d8bd51 Mon Sep 17 00:00:00 2001 From: max Date: Thu, 21 Jan 2021 10:13:29 +0100 Subject: [PATCH] Apply root motion option. Added option to apply root motion. --- .../ModelBaker/Editor/VA_ModelBakerEditor.cs | 1 + Editor/Scripts/ModelBaker/VA_ModelBaker.cs | 3 ++- Runtime/Scripts/ModelBaker/AnimationBaker.cs | 14 +++++++++++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Editor/Scripts/ModelBaker/Editor/VA_ModelBakerEditor.cs b/Editor/Scripts/ModelBaker/Editor/VA_ModelBakerEditor.cs index 7175127..bc00536 100644 --- a/Editor/Scripts/ModelBaker/Editor/VA_ModelBakerEditor.cs +++ b/Editor/Scripts/ModelBaker/Editor/VA_ModelBakerEditor.cs @@ -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")); } diff --git a/Editor/Scripts/ModelBaker/VA_ModelBaker.cs b/Editor/Scripts/ModelBaker/VA_ModelBaker.cs index 84fcc14..f7acae6 100644 --- a/Editor/Scripts/ModelBaker/VA_ModelBaker.cs +++ b/Editor/Scripts/ModelBaker/VA_ModelBaker.cs @@ -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()); diff --git a/Runtime/Scripts/ModelBaker/AnimationBaker.cs b/Runtime/Scripts/ModelBaker/AnimationBaker.cs index 8d1118e..86a98fb 100644 --- a/Runtime/Scripts/ModelBaker/AnimationBaker.cs +++ b/Runtime/Scripts/ModelBaker/AnimationBaker.cs @@ -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().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.BakeMesh(mesh);