Created
June 22, 2023 03:23
-
-
Save gyosit/1616e319e381a2b33069ea8c8f4c3512 to your computer and use it in GitHub Desktop.
Recording humanoid model animation with skkinnedmeshrenderer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor.Animations; | |
using UnityEditor; | |
public class RealTimeRecorder : MonoBehaviour { | |
public AnimationClip clip; | |
private AnimationClip clip_transform; | |
private AnimationClip clip_blendshape; | |
private GameObjectRecorder m_Recorder_transform; | |
private GameObjectRecorder m_Recorder_blendshape; | |
void Start() | |
{ | |
// Create temporary clip to record into. | |
clip_transform = new AnimationClip(); | |
clip_blendshape = new AnimationClip(); | |
// Create recorder and record the script GameObject. | |
// GameObject root = gameObject.transform.Find("Root").gameObject; | |
GameObject face = gameObject.transform.Find("Face").gameObject; | |
m_Recorder_transform = new GameObjectRecorder(gameObject); | |
m_Recorder_blendshape = new GameObjectRecorder(face); | |
// Bind all the Animators on the GameObject and all its children. | |
m_Recorder_transform.BindComponentsOfType<Transform>(gameObject, true); | |
m_Recorder_blendshape.BindComponentsOfType<SkinnedMeshRenderer>(face, true); | |
Debug.Log("Start recording"); | |
} | |
void LateUpdate() | |
{ | |
if (clip == null || clip_transform == null || clip_blendshape == null) | |
return; | |
// Take a snapshot and record all the bindings values for this frame. | |
m_Recorder_transform.TakeSnapshot(Time.deltaTime); | |
m_Recorder_blendshape.TakeSnapshot(Time.deltaTime); | |
} | |
void OnDisable() | |
{ | |
if (clip == null || clip_transform == null || clip_blendshape == null) | |
return; | |
if (m_Recorder_transform.isRecording) | |
{ | |
// Save the recorded session to the clip. | |
m_Recorder_transform.SaveToClip(clip_transform); | |
} | |
if (m_Recorder_blendshape.isRecording) | |
{ | |
// Save the recorded session to the clip. | |
m_Recorder_blendshape.SaveToClip(clip_blendshape); | |
} | |
// Convert Skinned Mesh Renderers for Face animation | |
EditorCurveBinding[] bindings_transform = AnimationUtility.GetCurveBindings(clip_transform); | |
EditorCurveBinding[] bindings_blendshape = AnimationUtility.GetCurveBindings(clip_blendshape); | |
foreach (EditorCurveBinding binding in bindings_transform) | |
{ | |
if(!binding.propertyName.Contains("Position")) | |
{ | |
AnimationCurve curve = AnimationUtility.GetEditorCurve(clip_transform, binding); | |
clip.SetCurve(binding.path, binding.type, binding.propertyName, curve); | |
} | |
} | |
foreach (EditorCurveBinding binding in bindings_blendshape) | |
{ | |
if (binding.propertyName.Contains("blendShape") && !binding.propertyName.Contains("Fcl_HA")) | |
{ | |
AnimationCurve curve = AnimationUtility.GetEditorCurve(clip_blendshape, binding); | |
clip.SetCurve("Face", typeof(SkinnedMeshRenderer), binding.propertyName, curve); | |
} | |
} | |
Debug.Log("Animation clip created"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment