Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

smoothing animation curves and recording less keyframes #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ public class UnityAnimationRecorderEditor : Editor {
SerializedProperty changeTimeScale;
SerializedProperty timeScaleOnStart;
SerializedProperty timeScaleOnRecord;
SerializedProperty smoothTangents;
SerializedProperty recordEachFrame;
SerializedProperty howOftenFrame;


void OnEnable () {
void OnEnable () {

savePath = serializedObject.FindProperty ("savePath");
fileName = serializedObject.FindProperty ("fileName");
Expand All @@ -37,8 +40,12 @@ void OnEnable () {
changeTimeScale = serializedObject.FindProperty ("changeTimeScale");
timeScaleOnStart = serializedObject.FindProperty ("timeScaleOnStart");
timeScaleOnRecord = serializedObject.FindProperty ("timeScaleOnRecord");

}
smoothTangents = serializedObject.FindProperty("smoothTangents");

recordEachFrame = serializedObject.FindProperty("recordEachFrame");
howOftenFrame = serializedObject.FindProperty("howOftenFrame");

}

public override void OnInspectorGUI () {
serializedObject.Update ();
Expand Down Expand Up @@ -87,7 +94,16 @@ public override void OnInspectorGUI () {
// recording frames setting
recordLimitedFrames.boolValue = EditorGUILayout.Toggle( "Record Limited Frames", recordLimitedFrames.boolValue );

if (recordLimitedFrames.boolValue)
smoothTangents.boolValue = EditorGUILayout.Toggle("Smooth tangents of animation curves", smoothTangents.boolValue);

recordEachFrame.boolValue = EditorGUILayout.Toggle("Record animation in every frame", recordEachFrame.boolValue);
if (!recordEachFrame.boolValue)
{
howOftenFrame.floatValue = EditorGUILayout.FloatField("Distance betwen recorded frames", howOftenFrame.floatValue);

}

if (recordLimitedFrames.boolValue)
EditorGUILayout.PropertyField (recordFrames);

serializedObject.ApplyModifiedProperties ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class UnityAnimationRecorder : MonoBehaviour {
public bool changeTimeScale = false;
public float timeScaleOnStart = 0.0f;
public float timeScaleOnRecord = 1.0f;
public bool smoothTangents = true;

Transform[] recordObjs;
UnityObjectAnimation[] objRecorders;
Expand Down Expand Up @@ -54,13 +55,7 @@ void Update () {
StopRecording ();
}

if (isStart) {
nowTime += Time.deltaTime;

for (int i = 0; i < objRecorders.Length; i++) {
objRecorders [i].AddFrame (nowTime);
}
}


}

Expand All @@ -78,19 +73,30 @@ public void StopRecording () {
ExportAnimationClip ();
}




void FixedUpdate () {

public bool recordEachFrame = true;

public float howOftenFrame = 0.2f;

float lastTime = 0f;
void FixedUpdate () {

if (isStart) {

if (frameIndex < recordFrames) {
for (int i = 0; i < objRecorders.Length; i++) {
objRecorders [i].AddFrame (nowTime);
}

++frameIndex;

nowTime += Time.fixedDeltaTime;
if (lastTime==0|| nowTime > lastTime + howOftenFrame||recordEachFrame)
{
lastTime = nowTime;

for (int i = 0; i < objRecorders.Length; i++)
{
objRecorders[i].AddFrame(nowTime);
}

++frameIndex;
}
} else {
isStart = false;
ExportAnimationClip ();
Expand All @@ -107,7 +113,14 @@ void OnGUI () {
void ExportAnimationClip () {

string exportFilePath = savePath + fileName;

if(smoothTangents)
{
for (int i = 0; i < objRecorders.Length; i++)
{
objRecorders[i].smoothCurves();

}
}
AnimationClip clip = new AnimationClip ();
clip.name = fileName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ public void AddValue( float animTime, float animValue )
Keyframe key = new Keyframe (animTime, animValue, 0.0f, 0.0f);
animCurve.AddKey (key);
}
public void SmoothTangents()
{
for (int i = 0; i < animCurve.keys.Length; ++i)
{
animCurve.SmoothTangents(i, 0); //zero weight means average
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@ public void AddFrame ( float time ) {
curves [9].AddValue (time, observeGameObject.localScale.z);

}
public void smoothCurves()
{
for (int i = 0; i < curves.Length; i++)
{
curves[i].SmoothTangents();
}
}
}