-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
AnimationClipEditor.cs
1794 lines (1505 loc) · 78.5 KB
/
AnimationClipEditor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
using UnityEditor;
using UnityEditor.Animations;
using UnityEditor.AnimatedValues;
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using Object = UnityEngine.Object;
namespace UnityEditor
{
[CustomEditor(typeof(AnimationClip))]
internal class AnimationClipEditor : Editor
{
internal static void EditWithImporter(AnimationClip clip)
{
ModelImporter importer = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(clip)) as ModelImporter;
if (importer)
{
Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(importer.assetPath);
ModelImporterEditor inspector = Editor.CreateEditor(importer) as ModelImporterEditor;
EditorPrefs.SetInt(inspector.GetType().Name + "ActiveEditorIndex", 2);
int clipIndex = 0;
var clipAnimations = importer.clipAnimations;
for (int i = 0; i < clipAnimations.Length; i++)
{
if (clipAnimations[i].name == clip.name)
clipIndex = i;
}
EditorPrefs.SetInt("ModelImporterClipEditor.ActiveClipIndex", clipIndex);
}
}
private static class Styles
{
public static GUIContent StartFrame = EditorGUIUtility.TrTextContent("Start", "Start frame of the clip.");
public static GUIContent EndFrame = EditorGUIUtility.TrTextContent("End", "End frame of the clip.");
public static string AverageVelocity = L10n.Tr("Average Velocity: {0}\nAverage Angular Y Speed: {1} deg/s");
public static GUIContent HasAdditiveReferencePose = EditorGUIUtility.TrTextContent("Additive Reference Pose", "Enable to define the additive reference pose frame.");
public static GUIContent AdditiveReferencePoseFrame = EditorGUIUtility.TrTextContent("Pose Frame", "Pose Frame.");
public static GUIContent LoopTime = EditorGUIUtility.TrTextContent("Loop Time", "Enable to make the animation plays through and then restarts when the end is reached.");
public static GUIContent LoopPose = EditorGUIUtility.TrTextContent("Loop Pose", "Enable to make the animation loop seamlessly.");
public static GUIContent LoopCycleOffset = EditorGUIUtility.TrTextContent("Cycle Offset", "Offset to the cycle of a looping animation, if we want to start it at a different time.");
public static GUIContent RootTransformRotation = EditorGUIUtility.TrTextContent("Root Transform Rotation");
public static GUIContent RootTransformRotationY = EditorGUIUtility.TrTextContent("Root Transform Position (Y)");
public static GUIContent RootTransformPositionXZ = EditorGUIUtility.TrTextContent("Root Transform Position (XZ)");
public static GUIContent BakeIntoPoseOrientation = EditorGUIUtility.TrTextContent("Bake Into Pose", "Enable to make root rotation be baked into the movement of the bones. Disable to make root rotation be stored as root motion.");
public static GUIContent OrientationOffsetY = EditorGUIUtility.TrTextContent("Offset", "Offset to the root rotation (in degrees).");
public static GUIContent BasedUponOrientation = EditorGUIUtility.TrTextContent("Based Upon", "What the root rotation is based upon.");
public static GUIContent BasedUponStartOrientation = EditorGUIUtility.TrTextContent("Based Upon (at Start)", "What the root rotation is based upon.");
public static GUIContent[] BasedUponRotationHumanOpt =
{
EditorGUIUtility.TrTextContent("Original", "Keeps the rotation as it is authored in the source file."),
EditorGUIUtility.TrTextContent("Body Orientation", "Keeps the upper body pointing forward.")
};
public static GUIContent[] BasedUponRotationOpt =
{
EditorGUIUtility.TrTextContent("Original", "Keeps the rotation as it is authored in the source file."),
EditorGUIUtility.TrTextContent("Root Node Rotation", "Keeps the upper body pointing forward.")
};
public static GUIContent BakeIntoPosePositionY = EditorGUIUtility.TrTextContent("Bake Into Pose", "Enable to make vertical root motion be baked into the movement of the bones. Disable to make vertical root motion be stored as root motion.");
public static GUIContent PositionOffsetY = EditorGUIUtility.TrTextContent("Offset", "Offset to the vertical root position.");
public static GUIContent BasedUponPositionY = EditorGUIUtility.TrTextContent("Based Upon", "What the vertical root position is based upon.");
public static GUIContent BasedUponStartPositionY = EditorGUIUtility.TrTextContent("Based Upon (at Start)", "What the vertical root position is based upon.");
public static GUIContent[] BasedUponPositionYHumanOpt =
{
EditorGUIUtility.TrTextContent("Original", "Keeps the vertical position as it is authored in the source file."),
EditorGUIUtility.TrTextContent("Center of Mass", "Keeps the center of mass aligned with root transform position."),
EditorGUIUtility.TrTextContent("Feet", "Keeps the feet aligned with the root transform position.")
};
public static GUIContent[] BasedUponPositionYOpt =
{
EditorGUIUtility.TrTextContent("Original", "Keeps the vertical position as it is authored in the source file."),
EditorGUIUtility.TrTextContent("Root Node Position")
};
public static GUIContent BakeIntoPosePositionXZ = EditorGUIUtility.TrTextContent("Bake Into Pose", "Enable to make horizontal root motion be baked into the movement of the bones. Disable to make horizontal root motion be stored as root motion.");
public static GUIContent BasedUponPositionXZ = EditorGUIUtility.TrTextContent("Based Upon", "What the horizontal root position is based upon.");
public static GUIContent BasedUponStartPositionXZ = EditorGUIUtility.TrTextContent("Based Upon (at Start)", "What the horizontal root position is based upon.");
public static GUIContent[] BasedUponPositionXZHumanOpt =
{
EditorGUIUtility.TrTextContent("Original", "Keeps the horizontal position as it is authored in the source file."),
EditorGUIUtility.TrTextContent("Center of Mass", "Keeps the center of mass aligned with root transform position.")
};
public static GUIContent[] BasedUponPositionXZOpt =
{
EditorGUIUtility.TrTextContent("Original", "Keeps the horizontal position as it is authored in the source file."),
EditorGUIUtility.TrTextContent("Root Node Position")
};
public static GUIContent Mirror = EditorGUIUtility.TrTextContent("Mirror", "Mirror left and right in this clip.");
public static GUIContent Curves = EditorGUIUtility.TrTextContent("Curves", "Parameter-related curves.");
public static GUIContent Length = EditorGUIUtility.TrTextContent("Length");
public static GUIContent AddLoopFrame = EditorGUIUtility.TrTextContent("Add Loop Frame");
public static GUIContent WrapMode = EditorGUIUtility.TrTextContent("Wrap Mode");
public static GUIContent Events = EditorGUIUtility.TrTextContent("Events");
public static GUIContent LoopMatch = EditorGUIUtility.TrTextContent("loop match");
public static GUIContent AddEventContent = EditorGUIUtility.TrIconContent("Animation.AddEvent", "Add Event.");
public static GUIContent GreenLightIcon = EditorGUIUtility.IconContent("lightMeter/greenLight");
public static GUIContent LightRimIcon = EditorGUIUtility.IconContent("lightMeter/lightRim");
public static GUIContent OrangeLightIcon = EditorGUIUtility.IconContent("lightMeter/orangeLight");
public static GUIContent RedLightIcon = EditorGUIUtility.IconContent("lightMeter/redLight");
public static GUIContent PrevKeyContent = EditorGUIUtility.TrIconContent("Animation.PrevKey", "Go to previous key frame.");
public static GUIContent NextKeyContent = EditorGUIUtility.TrIconContent("Animation.NextKey", "Go to next key frame.");
public static GUIContent AddKeyframeContent = EditorGUIUtility.TrIconContent("Animation.AddKeyframe", "Add Keyframe.");
}
static string s_LoopMeterStr = "LoopMeter";
static int s_LoopMeterHint = s_LoopMeterStr.GetHashCode();
static string s_LoopOrientationMeterStr = "LoopOrientationMeter";
static int s_LoopOrientationMeterHint = s_LoopOrientationMeterStr.GetHashCode();
static string s_LoopPositionYMeterStr = "LoopPostionYMeter";
static int s_LoopPositionYMeterHint = s_LoopPositionYMeterStr.GetHashCode();
static string s_LoopPositionXZMeterStr = "LoopPostionXZMeter";
static int s_LoopPositionXZMeterHint = s_LoopPositionXZMeterStr.GetHashCode();
static public float s_EventTimelineMax = 1.05f;
// Update the ClipInfo if needed.
// Needed because of the dummy serialized property (m_DefaultClipsSerializedObject) TransferDefaultClipsToCustomClips
private void UpdateEventsPopupClipInfo(AnimationClipInfoProperties info)
{
if (m_EventManipulationHandler != null)
m_EventManipulationHandler.UpdateEvents(info);
}
private AvatarMask m_Mask = null;
public AvatarMask mask
{
get { return m_Mask; }
set { m_Mask = value; }
}
private AnimationClipInfoProperties m_ClipInfo = null;
public void ShowRange(AnimationClipInfoProperties info)
{
UpdateEventsPopupClipInfo(info);
m_ClipInfo = info;
info.AssignToPreviewClip(m_Clip);
}
public string[] takeNames { get; set; }
public int takeIndex { get; set; }
private AnimationClip m_Clip = null;
private AnimatorController m_Controller = null;
private AnimatorStateMachine m_StateMachine;
private AnimatorState m_State;
private AvatarPreview m_AvatarPreview = null;
private TimeArea m_TimeArea;
private TimeArea m_EventTimeArea;
private bool m_DraggingRange = false;
private bool m_DraggingRangeBegin = false;
private bool m_DraggingRangeEnd = false;
private float m_DraggingStartFrame = 0;
private float m_DraggingStopFrame = 0;
private float m_DraggingAdditivePoseFrame = 0;
private bool m_LoopTime = false;
private bool m_LoopBlend = false;
private bool m_LoopBlendOrientation = false;
private bool m_LoopBlendPositionY = false;
private bool m_LoopBlendPositionXZ = false;
private float m_StartFrame = 0;
private float m_StopFrame = 1;
private float m_AdditivePoseFrame = 0;
private float m_InitialClipLength = 0;
static private bool m_ShowCurves = false;
private EventManipulationHandler m_EventManipulationHandler;
static private bool m_ShowEvents = false;
bool m_NeedsToGenerateClipInfo = false;
public bool needsToGenerateClipInfo
{
get { return m_NeedsToGenerateClipInfo; }
set { m_NeedsToGenerateClipInfo = value; }
}
const int kSamplesPerSecond = 60;
const int kPose = 0;
const int kRotation = 1;
const int kHeight = 2;
const int kPosition = 3;
Vector2[][][] m_QualityCurves = new Vector2[4][][];
bool m_DirtyQualityCurves = false;
private void InitController()
{
if (m_Clip.legacy)
return;
if (m_AvatarPreview != null && m_AvatarPreview.Animator != null)
{
bool wasInitialized = true;
if (m_Controller == null)
{
m_Controller = new AnimatorController();
m_Controller.pushUndo = false;
m_Controller.hideFlags = HideFlags.HideAndDontSave;
m_Controller.AddLayer("preview");
m_StateMachine = m_Controller.layers[0].stateMachine;
m_StateMachine.pushUndo = false;
m_StateMachine.hideFlags = HideFlags.HideAndDontSave;
if (mask != null)
{
AnimatorControllerLayer[] layers = m_Controller.layers;
layers[0].avatarMask = mask;
m_Controller.layers = layers;
}
wasInitialized = false;
}
if (m_State == null)
{
m_State = m_StateMachine.AddState("preview");
m_State.pushUndo = false;
AnimatorControllerLayer[] layers = m_Controller.layers;
m_State.motion = m_Clip;
m_Controller.layers = layers;
m_State.iKOnFeet = m_AvatarPreview.IKOnFeet;
m_State.hideFlags = HideFlags.HideAndDontSave;
wasInitialized = false;
}
AnimatorController.SetAnimatorController(m_AvatarPreview.Animator, m_Controller);
if (AnimatorController.GetEffectiveAnimatorController(m_AvatarPreview.Animator) != m_Controller)
{
AnimatorController.SetAnimatorController(m_AvatarPreview.Animator, m_Controller);
}
if (!wasInitialized)
{
m_AvatarPreview.Animator.Play(0, 0, 0);
m_AvatarPreview.Animator.Update(0);
m_AvatarPreview.ResetPreviewFocus();
}
}
}
internal override void OnHeaderIconGUI(Rect iconRect)
{
// It doesn't make sense to try and use the preview
Texture2D icon = null;
bool isLoadingAssetPreview = AssetPreview.IsLoadingAssetPreview(target.GetInstanceID());
icon = AssetPreview.GetAssetPreview(target);
if (!icon)
{
// We have a static preview it just hasn't been loaded yet. Repaint until we have it loaded.
if (isLoadingAssetPreview)
Repaint();
icon = AssetPreview.GetMiniThumbnail(target);
}
GUI.DrawTexture(iconRect, icon);
}
internal override void OnHeaderTitleGUI(Rect titleRect, string header)
{
if (m_ClipInfo != null)
m_ClipInfo.name = EditorGUI.DelayedTextField(titleRect, m_ClipInfo.name, EditorStyles.textField);
else
base.OnHeaderTitleGUI(titleRect, header);
}
internal override void OnHeaderControlsGUI()
{
if (m_ClipInfo != null && takeNames != null && takeNames.Length > 1)
{
EditorGUIUtility.labelWidth = 80;
takeIndex = EditorGUILayout.Popup("Source Take", takeIndex, takeNames);
}
else
{
base.OnHeaderControlsGUI();
ModelImporter importer = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(target)) as ModelImporter;
if (importer != null && m_ClipInfo == null)
{
if (GUILayout.Button("Edit...", EditorStyles.miniButton, GUILayout.ExpandWidth(false)))
{
EditWithImporter(target as AnimationClip);
}
}
}
}
private void DestroyController()
{
if (m_AvatarPreview != null && m_AvatarPreview.Animator != null)
{
AnimatorController.SetAnimatorController(m_AvatarPreview.Animator, null);
}
Object.DestroyImmediate(m_Controller);
Object.DestroyImmediate(m_State);
m_Controller = null;
m_StateMachine = null;
m_State = null;
}
private void SetPreviewAvatar()
{
DestroyController();
InitController();
}
void Init()
{
if (m_AvatarPreview == null)
{
m_AvatarPreview = new AvatarPreview(null, target as Motion);
m_AvatarPreview.OnAvatarChangeFunc = SetPreviewAvatar;
m_AvatarPreview.fps = Mathf.RoundToInt((target as AnimationClip).frameRate);
m_AvatarPreview.ShowIKOnFeetButton = (target as Motion).isHumanMotion;
m_AvatarPreview.ResetPreviewFocus();
}
// force an update on timeControl if AvatarPreviewer is closed when creating/editing animation curves
// prevent from having a nomralizedTime == -inf
if (m_AvatarPreview.timeControl.currentTime == Mathf.NegativeInfinity)
m_AvatarPreview.timeControl.Update();
}
internal void OnEnable()
{
m_Clip = target as AnimationClip;
//case 790259: The length of the clip will be changed by this inspector, so we can't use it for sampling
m_InitialClipLength = m_Clip.stopTime - m_Clip.startTime;
if (m_TimeArea == null)
{
m_TimeArea = new TimeArea(true);
m_TimeArea.hRangeLocked = false;
m_TimeArea.vRangeLocked = true;
m_TimeArea.hSlider = true;
m_TimeArea.vSlider = false;
m_TimeArea.hRangeMin = m_Clip.startTime;
m_TimeArea.hRangeMax = m_Clip.stopTime;
m_TimeArea.margin = 10;
m_TimeArea.scaleWithWindow = true;
m_TimeArea.SetShownHRangeInsideMargins(m_Clip.startTime, m_Clip.stopTime);
m_TimeArea.hTicks.SetTickModulosForFrameRate(m_Clip.frameRate);
m_TimeArea.ignoreScrollWheelUntilClicked = true;
}
if (m_EventTimeArea == null)
{
m_EventTimeArea = new TimeArea(true);
m_EventTimeArea.hRangeLocked = true;
m_EventTimeArea.vRangeLocked = true;
m_EventTimeArea.hSlider = false;
m_EventTimeArea.vSlider = false;
m_EventTimeArea.hRangeMin = 0;
m_EventTimeArea.hRangeMax = s_EventTimelineMax;
m_EventTimeArea.margin = 10;
m_EventTimeArea.scaleWithWindow = true;
m_EventTimeArea.SetShownHRangeInsideMargins(0, s_EventTimelineMax);
m_EventTimeArea.hTicks.SetTickModulosForFrameRate(60);
m_EventTimeArea.ignoreScrollWheelUntilClicked = true;
}
if (m_EventManipulationHandler == null)
m_EventManipulationHandler = new EventManipulationHandler(m_EventTimeArea);
}
void OnDisable()
{
DestroyController();
if (m_AvatarPreview != null)
{
m_AvatarPreview.OnDisable();
m_AvatarPreview = null;
}
}
public override bool HasPreviewGUI()
{
if (Event.current.type == EventType.Layout)
{
Init();
}
return m_AvatarPreview != null;
}
public override void OnPreviewSettings()
{
m_AvatarPreview.DoPreviewSettings();
}
void CalculateQualityCurves()
{
for (int i = 0; i < 4; i++)
m_QualityCurves[i] = new Vector2[2][];
for (int q = 0; q < 2; q++)
{
// [case 491172]
// There is no need to sample the quality curve outside of the animation range [m_Clip.startTime, m_Clip.stopTime] because the Time area show only the animation range anyway
// so it not possible for the user to see curve outside of this range.
float clipStartTime = Mathf.Clamp(m_ClipInfo.firstFrame / m_Clip.frameRate, m_Clip.startTime, m_Clip.stopTime);
float clipStopTime = Mathf.Clamp(m_ClipInfo.lastFrame / m_Clip.frameRate, m_Clip.startTime, m_Clip.stopTime);
float fixedTime = (q == 0 ? clipStopTime : clipStartTime);
float startTime = (q == 0 ? 0 : clipStartTime);
float stopTime = (q == 0 ? clipStopTime : m_Clip.stopTime);
// Start sample may be a bit before start time; stop sample may be a bit after stop time
int startSample = Mathf.FloorToInt(startTime * kSamplesPerSecond);
int stopSample = Mathf.CeilToInt(stopTime * kSamplesPerSecond);
m_QualityCurves[kPose][q] = new Vector2[stopSample - startSample + 1];
m_QualityCurves[kRotation][q] = new Vector2[stopSample - startSample + 1];
m_QualityCurves[kHeight][q] = new Vector2[stopSample - startSample + 1];
m_QualityCurves[kPosition][q] = new Vector2[stopSample - startSample + 1];
QualityCurvesTime qualityCurvesTime = new QualityCurvesTime();
qualityCurvesTime.fixedTime = fixedTime;
qualityCurvesTime.variableEndStart = startTime;
qualityCurvesTime.variableEndEnd = stopTime;
qualityCurvesTime.q = q;
MuscleClipUtility.CalculateQualityCurves(m_Clip, qualityCurvesTime,
m_QualityCurves[kPose][q], m_QualityCurves[kRotation][q],
m_QualityCurves[kHeight][q], m_QualityCurves[kPosition][q]);
}
m_DirtyQualityCurves = false;
}
public override void OnInteractivePreviewGUI(Rect r, GUIStyle background)
{
bool isRepaint = (Event.current.type == EventType.Repaint);
InitController();
if (isRepaint)
m_AvatarPreview.timeControl.Update();
// Set to full take range
AnimationClip clip = target as AnimationClip;
AnimationClipSettings previewInfo = AnimationUtility.GetAnimationClipSettings(clip);
// Set settings
m_AvatarPreview.timeControl.loop = true; // always looping, waiting for UI ctrl...
// Sample Animation
if (isRepaint && m_AvatarPreview.PreviewObject != null)
{
if (clip.legacy == false && m_AvatarPreview.Animator != null)
{
if (m_State != null)
m_State.iKOnFeet = m_AvatarPreview.IKOnFeet;
float normalizedTime = previewInfo.stopTime - previewInfo.startTime != 0 ? (m_AvatarPreview.timeControl.currentTime - previewInfo.startTime) / (previewInfo.stopTime - previewInfo.startTime) : 0.0f;
m_AvatarPreview.Animator.Play(0, 0, normalizedTime);
m_AvatarPreview.Animator.Update(m_AvatarPreview.timeControl.deltaTime);
}
else
{
clip.SampleAnimation(m_AvatarPreview.PreviewObject, m_AvatarPreview.timeControl.currentTime);
}
}
m_AvatarPreview.DoAvatarPreview(r, background);
}
public void ClipRangeGUI(ref float startFrame, ref float stopFrame, out bool changedStart, out bool changedStop, bool showAdditivePoseFrame, ref float additivePoseframe, out bool changedAdditivePoseframe)
{
changedStart = false;
changedStop = false;
changedAdditivePoseframe = false;
m_DraggingRangeBegin = false;
m_DraggingRangeEnd = false;
bool invalidRange = (
startFrame + 0.01f < m_Clip.startTime * m_Clip.frameRate ||
startFrame - 0.01f > m_Clip.stopTime * m_Clip.frameRate ||
stopFrame + 0.01f < m_Clip.startTime * m_Clip.frameRate ||
stopFrame - 0.01f > m_Clip.stopTime * m_Clip.frameRate);
bool fixRange = false;
if (invalidRange)
{
GUILayout.BeginHorizontal(EditorStyles.helpBox);
GUILayout.Label("The clip range is outside of the range of the source take.", EditorStyles.wordWrappedMiniLabel);
GUILayout.FlexibleSpace();
GUILayout.BeginVertical();
GUILayout.Space(5);
if (GUILayout.Button("Clamp Range"))
fixRange = true;
GUILayout.EndVertical();
GUILayout.EndHorizontal();
}
// Time line
Rect timeRect = GUILayoutUtility.GetRect(10, 18 + 15);
GUI.Label(timeRect, "", "TE Toolbar");
if (Event.current.type == EventType.Repaint)
m_TimeArea.rect = timeRect;
m_TimeArea.BeginViewGUI();
m_TimeArea.EndViewGUI();
timeRect.height -= 15;
// Start and stop markers
int startHandleId = GUIUtility.GetControlID(3126789, FocusType.Passive);
int stopHandleId = GUIUtility.GetControlID(3126789, FocusType.Passive);
int additiveHandleId = GUIUtility.GetControlID(3126789, FocusType.Passive);
GUI.BeginGroup(new Rect(timeRect.x + 1, timeRect.y + 1, timeRect.width - 2, timeRect.height - 2));
{
timeRect.x = timeRect.y = -1;
// Draw selected range as blue tint
float startPixel = m_TimeArea.FrameToPixel(startFrame, m_Clip.frameRate, timeRect);
float stopPixel = m_TimeArea.FrameToPixel(stopFrame, m_Clip.frameRate, timeRect);
GUI.Label(new Rect(startPixel, timeRect.y, stopPixel - startPixel, timeRect.height), "", EditorStyles.selectionRect);
// Draw time ruler
m_TimeArea.TimeRuler(timeRect, m_Clip.frameRate);
// Current time indicator
TimeArea.DrawPlayhead(m_TimeArea.TimeToPixel(m_AvatarPreview.timeControl.currentTime, timeRect), timeRect.yMin, timeRect.yMax, 2f, 1f);
using (new EditorGUI.DisabledScope(invalidRange))
{
// Range handles
float startTime = startFrame / m_Clip.frameRate;
TimeArea.TimeRulerDragMode inPoint = m_TimeArea.BrowseRuler(timeRect, startHandleId, ref startTime, 0, false, "TL InPoint");
if (inPoint == TimeArea.TimeRulerDragMode.Cancel)
{
startFrame = m_DraggingStartFrame;
}
else if (inPoint != TimeArea.TimeRulerDragMode.None)
{
startFrame = startTime * m_Clip.frameRate;
// Snapping bias. Snap to whole frames when zoomed out.
startFrame = MathUtils.RoundBasedOnMinimumDifference(startFrame, m_TimeArea.PixelDeltaToTime(timeRect) * m_Clip.frameRate * 10);
changedStart = true;
}
float stopTime = stopFrame / m_Clip.frameRate;
TimeArea.TimeRulerDragMode outPoint = m_TimeArea.BrowseRuler(timeRect, stopHandleId, ref stopTime, 0, false, "TL OutPoint");
if (outPoint == TimeArea.TimeRulerDragMode.Cancel)
{
stopFrame = m_DraggingStopFrame;
}
else if (outPoint != TimeArea.TimeRulerDragMode.None)
{
stopFrame = stopTime * m_Clip.frameRate;
// Snapping bias. Snap to whole frames when zoomed out.
stopFrame = MathUtils.RoundBasedOnMinimumDifference(stopFrame, m_TimeArea.PixelDeltaToTime(timeRect) * m_Clip.frameRate * 10);
changedStop = true;
}
// Additive pose frame Handle
if (showAdditivePoseFrame)
{
float additivePoseTime = additivePoseframe / m_Clip.frameRate;
TimeArea.TimeRulerDragMode additivePoint = m_TimeArea.BrowseRuler(timeRect, additiveHandleId, ref additivePoseTime, 0, false, "TL playhead");
if (additivePoint == TimeArea.TimeRulerDragMode.Cancel)
{
additivePoseframe = m_DraggingAdditivePoseFrame;
}
else if (additivePoint != TimeArea.TimeRulerDragMode.None)
{
additivePoseframe = additivePoseTime * m_Clip.frameRate;
// Snapping bias. Snap to whole frames when zoomed out.
additivePoseframe = MathUtils.RoundBasedOnMinimumDifference(additivePoseframe, m_TimeArea.PixelDeltaToTime(timeRect) * m_Clip.frameRate * 10);
changedAdditivePoseframe = true;
}
}
}
if (EditorGUIUtility.hotControl == startHandleId)
changedStart = true;
if (EditorGUIUtility.hotControl == stopHandleId)
changedStop = true;
if (EditorGUIUtility.hotControl == additiveHandleId)
changedAdditivePoseframe = true;
}
GUI.EndGroup();
// Start and stop time float fields
using (new EditorGUI.DisabledScope(invalidRange))
{
EditorGUILayout.BeginHorizontal();
{
EditorGUI.BeginChangeCheck();
startFrame = EditorGUILayout.FloatField(Styles.StartFrame, Mathf.Round(startFrame * 1000) / 1000);
if (EditorGUI.EndChangeCheck())
changedStart = true;
GUILayout.FlexibleSpace();
EditorGUI.BeginChangeCheck();
stopFrame = EditorGUILayout.FloatField(Styles.EndFrame, Mathf.Round(stopFrame * 1000) / 1000);
if (EditorGUI.EndChangeCheck())
changedStop = true;
}
EditorGUILayout.EndHorizontal();
}
changedStart |= fixRange;
changedStop |= fixRange;
// Start and stop time value clamping
if (changedStart)
startFrame = Mathf.Clamp(startFrame, m_Clip.startTime * m_Clip.frameRate, Mathf.Clamp(stopFrame, m_Clip.startTime * m_Clip.frameRate, stopFrame));
if (changedStop)
stopFrame = Mathf.Clamp(stopFrame, startFrame, m_Clip.stopTime * m_Clip.frameRate);
if (changedAdditivePoseframe)
additivePoseframe = Mathf.Clamp(additivePoseframe, m_Clip.startTime * m_Clip.frameRate, m_Clip.stopTime * m_Clip.frameRate);
// Keep track of whether we're currently dragging the range or not
if (changedStart || changedStop || changedAdditivePoseframe)
{
if (!m_DraggingRange)
m_DraggingRangeBegin = true;
m_DraggingRange = true;
}
else if (m_DraggingRange && EditorGUIUtility.hotControl == 0 && Event.current.type == EventType.Repaint)
{
m_DraggingRangeEnd = true;
m_DraggingRange = false;
m_DirtyQualityCurves = true;
Repaint();
}
GUILayout.Space(10);
}
string GetStatsText()
{
string statsText = "";
bool IsHumanClip = targets.Length == 1 ? (target as Motion).isHumanMotion : false;
// Muscle clip info is currently only available for humanoid
if (IsHumanClip)
{
statsText = string.Format(Styles.AverageVelocity, m_Clip.averageSpeed.ToString("0.000"), (m_Clip.averageAngularSpeed * 180.0f / Mathf.PI).ToString("0.0"));
}
// Only show stats in final clip not for the preview clip
if (m_ClipInfo == null)
{
AnimationClipStats stats = new AnimationClipStats();
stats.Reset();
for (int clipIter = 0; clipIter < targets.Length; clipIter++)
{
AnimationClip clip = targets[clipIter] as AnimationClip;
if (clip != null)
{
AnimationClipStats clipStats = AnimationUtility.GetAnimationClipStats(clip);
stats.Combine(clipStats);
}
}
if (statsText.Length != 0)
statsText += '\n';
float constantRatio = (float)stats.constantCurves / (float)stats.totalCurves * 100.0f;
float denseRatio = (float)stats.denseCurves / (float)stats.totalCurves * 100.0f;
float streamRatio = (float)stats.streamCurves / (float)stats.totalCurves * 100.0f;
statsText += string.Format("Curves Pos: {0} Quaternion: {1} Euler: {2} Scale: {3} Muscles: {4} Generic: {5} PPtr: {6}\n", stats.positionCurves, stats.quaternionCurves, stats.eulerCurves, stats.scaleCurves, stats.muscleCurves, stats.genericCurves, stats.pptrCurves);
statsText += string.Format("Curves Total: {0}, Constant: {1} ({2}%) Dense: {3} ({4}%) Stream: {5} ({6}%)\n", stats.totalCurves, stats.constantCurves, constantRatio.ToString("0.0"), stats.denseCurves, denseRatio.ToString("0.0"), stats.streamCurves, streamRatio.ToString("0.0"));
statsText += EditorUtility.FormatBytes(stats.size);
}
return statsText;
}
private float GetClipLength()
{
if (m_ClipInfo == null)
return m_Clip.length;
else
return (m_ClipInfo.lastFrame - m_ClipInfo.firstFrame) / m_Clip.frameRate;
}
// A minimal list of settings to be shown in the Asset Store preview inspector
internal override void OnAssetStoreInspectorGUI()
{
OnInspectorGUI();
}
public override void OnInspectorGUI()
{
Init();
EditorGUIUtility.labelWidth = 50;
EditorGUIUtility.fieldWidth = 30;
EditorGUILayout.BeginHorizontal();
{
using (new EditorGUI.DisabledScope(true))
{
GUILayout.Label(Styles.Length, EditorStyles.miniLabel, GUILayout.Width(50 - 4));
GUILayout.Label(GetClipLength().ToString("0.000"), EditorStyles.miniLabel);
GUILayout.FlexibleSpace();
GUILayout.Label(m_Clip.frameRate + " FPS", EditorStyles.miniLabel);
}
}
EditorGUILayout.EndHorizontal();
if (!m_Clip.legacy)
MuscleClipGUI();
else
AnimationClipGUI();
}
private void AnimationClipGUI()
{
if (m_ClipInfo != null)
{
float startFrame = m_ClipInfo.firstFrame;
float stopFrame = m_ClipInfo.lastFrame;
float additivePoseFrame = 0;
bool changedStart = false;
bool changedStop = false;
bool changedAdditivePoseFrame = false;
ClipRangeGUI(ref startFrame, ref stopFrame, out changedStart, out changedStop, false, ref additivePoseFrame, out changedAdditivePoseFrame);
if (changedStart)
m_ClipInfo.firstFrame = startFrame;
if (changedStop)
m_ClipInfo.lastFrame = stopFrame;
m_AvatarPreview.timeControl.startTime = startFrame / m_Clip.frameRate;
m_AvatarPreview.timeControl.stopTime = stopFrame / m_Clip.frameRate;
}
else
{
m_AvatarPreview.timeControl.startTime = 0;
m_AvatarPreview.timeControl.stopTime = m_Clip.length;
}
EditorGUIUtility.labelWidth = 0;
EditorGUIUtility.fieldWidth = 0;
if (m_ClipInfo != null)
m_ClipInfo.loop = EditorGUILayout.Toggle(Styles.AddLoopFrame, m_ClipInfo.loop);
EditorGUI.BeginChangeCheck();
int wrap = m_ClipInfo != null ? m_ClipInfo.wrapMode : (int)m_Clip.wrapMode;
wrap = (int)(WrapModeFixed)EditorGUILayout.EnumPopup(Styles.WrapMode, (WrapModeFixed)wrap);
if (EditorGUI.EndChangeCheck())
{
if (m_ClipInfo != null)
m_ClipInfo.wrapMode = wrap;
else
m_Clip.wrapMode = (WrapMode)wrap;
}
}
void CurveGUI()
{
if (m_ClipInfo == null)
return;
float time = m_AvatarPreview.timeControl.normalizedTime;
for (int i = 0; i < m_ClipInfo.GetCurveCount(); i++)
{
GUILayout.Space(5);
GUILayout.BeginHorizontal();
{
if (GUILayout.Button(GUIContent.none, "OL Minus", GUILayout.Width(17))) m_ClipInfo.RemoveCurve(i);
else
{
GUILayout.BeginVertical(GUILayout.Width(125));
string prevName = m_ClipInfo.GetCurveName(i);
string newName = EditorGUILayout.DelayedTextField(prevName, EditorStyles.textField);
if (prevName != newName)
m_ClipInfo.SetCurveName(i, newName);
SerializedProperty curveProperty = m_ClipInfo.GetCurveProperty(i);
AnimationCurve curve = curveProperty.animationCurveValue;
int keyCount = curve.length;
bool isKey = false;
int keyIndex = keyCount - 1;
for (int keyIter = 0; keyIter < keyCount; keyIter++)
{
if (Mathf.Abs(curve.keys[keyIter].time - time) < 0.0001f)
{
isKey = true;
keyIndex = keyIter;
break;
}
else if (curve.keys[keyIter].time > time)
{
keyIndex = keyIter;
break;
}
}
GUILayout.BeginHorizontal();
if (GUILayout.Button(Styles.PrevKeyContent))
{
if (keyIndex > 0)
{
keyIndex--;
m_AvatarPreview.timeControl.normalizedTime = curve.keys[keyIndex].time;
}
}
if (GUILayout.Button(Styles.NextKeyContent))
{
if (isKey && keyIndex < keyCount - 1) keyIndex++;
m_AvatarPreview.timeControl.normalizedTime = curve.keys[keyIndex].time;
}
float val, newVal;
using (new EditorGUI.DisabledScope(!isKey))
{
string orgFormat = EditorGUI.kFloatFieldFormatString;
EditorGUI.kFloatFieldFormatString = "n3";
val = curve.Evaluate(time);
newVal = EditorGUILayout.FloatField(val, GUILayout.Width(60));
EditorGUI.kFloatFieldFormatString = orgFormat;
}
bool addKey = false;
if (val != newVal)
{
if (isKey) curve.RemoveKey(keyIndex);
addKey = true;
}
using (new EditorGUI.DisabledScope(isKey))
{
if (GUILayout.Button(Styles.AddKeyframeContent))
{
addKey = true;
}
}
if (addKey)
{
Keyframe key = new Keyframe();
key.time = time;
key.value = newVal;
key.inTangent = 0;
key.outTangent = 0;
curve.AddKey(key);
m_ClipInfo.SetCurve(i, curve);
UnityEditorInternal.AnimationCurvePreviewCache.ClearCache();
}
GUILayout.EndHorizontal();
GUILayout.EndVertical();
EditorGUILayout.CurveField(curveProperty, EditorGUI.kCurveColor, new Rect(), GUIContent.none, GUILayout.Height(40));
Rect curveRect = GUILayoutUtility.GetLastRect();
keyCount = curve.length;
TimeArea.DrawPlayhead(curveRect.x + time * curveRect.width, curveRect.yMin, curveRect.yMax, 1f, 1f);
for (int keyIter = 0; keyIter < keyCount; keyIter++)
{
float keyTime = curve.keys[keyIter].time;
Handles.color = Color.white;
Handles.DrawLine(new Vector3(curveRect.x + keyTime * curveRect.width, curveRect.y + curveRect.height - 10, 0), new Vector3(curveRect.x + keyTime * curveRect.width, curveRect.y + curveRect.height, 0));
}
}
GUILayout.EndHorizontal();
}
}
GUILayout.BeginHorizontal();
if (GUILayout.Button(GUIContent.none, "OL Plus", GUILayout.Width(17))) m_ClipInfo.AddCurve();
GUILayout.EndHorizontal();
}
private void EventsGUI()
{
if (m_ClipInfo == null)
return;
GUILayout.BeginHorizontal();
if (GUILayout.Button(Styles.AddEventContent, GUILayout.Width(25)))
{
m_ClipInfo.AddEvent(Mathf.Clamp01(m_AvatarPreview.timeControl.normalizedTime));
m_EventManipulationHandler.SelectEvent(m_ClipInfo.GetEvents(), m_ClipInfo.GetEventCount() - 1, m_ClipInfo);
needsToGenerateClipInfo = true;
}
Rect timeRect = GUILayoutUtility.GetRect(10, 18 + 15);
timeRect.xMin += 5;
timeRect.xMax -= 4;
GUI.Label(timeRect, "", "TE Toolbar");
if (Event.current.type == EventType.Repaint)
m_EventTimeArea.rect = timeRect;
timeRect.height -= 15;
m_EventTimeArea.TimeRuler(timeRect, 100.0f);
GUI.BeginGroup(new Rect(timeRect.x + 1, timeRect.y + 1, timeRect.width - 2, timeRect.height - 2));
{
Rect localTimeRect = new Rect(-1, -1, timeRect.width, timeRect.height);
AnimationEvent[] events = m_ClipInfo.GetEvents();
if (m_EventManipulationHandler.HandleEventManipulation(localTimeRect, ref events, m_ClipInfo)) // had changed
{
m_ClipInfo.SetEvents(events);
}
// Current time indicator
TimeArea.DrawPlayhead(m_EventTimeArea.TimeToPixel(m_AvatarPreview.timeControl.normalizedTime, localTimeRect), localTimeRect.yMin, localTimeRect.yMax, 2f, 1f);
}
GUI.EndGroup();
GUILayout.EndHorizontal();
m_EventManipulationHandler.Draw(timeRect);
}
private void MuscleClipGUI()
{
EditorGUI.BeginChangeCheck();
InitController();
AnimationClipSettings animationClipSettings = AnimationUtility.GetAnimationClipSettings(m_Clip);
m_StartFrame = m_DraggingRange ? m_StartFrame : animationClipSettings.startTime * m_Clip.frameRate;
m_StopFrame = m_DraggingRange ? m_StopFrame : animationClipSettings.stopTime * m_Clip.frameRate;
m_AdditivePoseFrame = m_DraggingRange ? m_AdditivePoseFrame : animationClipSettings.additiveReferencePoseTime * m_Clip.frameRate;
float startTime = m_StartFrame / m_Clip.frameRate;
float stopTime = m_StopFrame / m_Clip.frameRate;
float additivePoseTime = m_AdditivePoseFrame / m_Clip.frameRate;
MuscleClipQualityInfo clipQualityInfo = MuscleClipUtility.GetMuscleClipQualityInfo(m_Clip, startTime,
stopTime);
bool IsHumanClip = (target as Motion).isHumanMotion;
bool hasMotionCurves = AnimationUtility.HasMotionCurves(m_Clip);
bool hasRootCurves = AnimationUtility.HasRootCurves(m_Clip);
bool hasGenericRootTransform = AnimationUtility.HasGenericRootTransform(m_Clip);
bool hasMotionFloatCurves = AnimationUtility.HasMotionFloatCurves(m_Clip);
bool hasAnyRootCurves = hasRootCurves || hasMotionCurves;
bool changedStart = false;
bool changedStop = false;
bool changedAdditivePoseFrame = false;