CurveEditUtility.cs
25.5 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditorInternal;
using UnityEngine;
using Object = UnityEngine.Object;
namespace UnityEditor.Timeline
{
// Utility class for editing animation clips from serialized properties
static class CurveEditUtility
{
static bool IsRotationKey(EditorCurveBinding binding)
{
return binding.propertyName.Contains("localEulerAnglesRaw");
}
public static void AddKey(AnimationClip clip, EditorCurveBinding sourceBinding, SerializedProperty prop, double time)
{
if (sourceBinding.isPPtrCurve)
{
AddObjectKey(clip, sourceBinding, prop, time);
}
else if (IsRotationKey(sourceBinding))
{
AddRotationKey(clip, sourceBinding, prop, time);
}
else
{
AddFloatKey(clip, sourceBinding, prop, time);
}
}
static void AddObjectKey(AnimationClip clip, EditorCurveBinding sourceBinding, SerializedProperty prop, double time)
{
if (prop.propertyType != SerializedPropertyType.ObjectReference)
return;
ObjectReferenceKeyframe[] curve = null;
var info = AnimationClipCurveCache.Instance.GetCurveInfo(clip);
var curveIndex = Array.IndexOf(info.objectBindings, sourceBinding);
if (curveIndex >= 0)
{
curve = info.objectCurves[curveIndex];
// where in the array does the evaluation land?
var evalIndex = EvaluateIndex(curve, (float)time);
if (KeyCompare(curve[evalIndex].time, (float)time, clip.frameRate) == 0)
{
curve[evalIndex].value = prop.objectReferenceValue;
}
// check the next key (always return the minimum value)
else if (evalIndex < curve.Length - 1 && KeyCompare(curve[evalIndex + 1].time, (float)time, clip.frameRate) == 0)
{
curve[evalIndex + 1].value = prop.objectReferenceValue;
}
// resize the array
else
{
if (time > curve[0].time)
evalIndex++;
var key = new ObjectReferenceKeyframe();
key.time = (float)time;
key.value = prop.objectReferenceValue;
ArrayUtility.Insert(ref curve, evalIndex, key);
}
}
else // curve doesn't exist, add it
{
curve = new ObjectReferenceKeyframe[1];
curve[0].time = (float)time;
curve[0].value = prop.objectReferenceValue;
}
AnimationUtility.SetObjectReferenceCurve(clip, sourceBinding, curve);
EditorUtility.SetDirty(clip);
}
static void AddRotationKey(AnimationClip clip, EditorCurveBinding sourceBind, SerializedProperty prop, double time)
{
if (prop.propertyType != SerializedPropertyType.Quaternion)
{
return;
}
var updateCurves = new List<AnimationCurve>();
var updateBindings = new List<EditorCurveBinding>();
var info = AnimationClipCurveCache.Instance.GetCurveInfo(clip);
for (var i = 0; i < info.bindings.Length; i++)
{
if (sourceBind.type != info.bindings[i].type)
continue;
if (info.bindings[i].propertyName.Contains("localEuler"))
{
updateBindings.Add(info.bindings[i]);
updateCurves.Add(info.curves[i]);
}
}
// use this instead of serialized properties because the editor will attempt to maintain
// correct localeulers
var eulers = ((Transform)prop.serializedObject.targetObject).localEulerAngles;
if (updateBindings.Count == 0)
{
var propName = AnimationWindowUtility.GetPropertyGroupName(sourceBind.propertyName);
updateBindings.Add(EditorCurveBinding.FloatCurve(sourceBind.path, sourceBind.type, propName + ".x"));
updateBindings.Add(EditorCurveBinding.FloatCurve(sourceBind.path, sourceBind.type, propName + ".y"));
updateBindings.Add(EditorCurveBinding.FloatCurve(sourceBind.path, sourceBind.type, propName + ".z"));
var curveX = new AnimationCurve();
var curveY = new AnimationCurve();
var curveZ = new AnimationCurve();
AddKeyFrameToCurve(curveX, (float)time, clip.frameRate, eulers.x, false);
AddKeyFrameToCurve(curveY, (float)time, clip.frameRate, eulers.y, false);
AddKeyFrameToCurve(curveZ, (float)time, clip.frameRate, eulers.z, false);
updateCurves.Add(curveX);
updateCurves.Add(curveY);
updateCurves.Add(curveZ);
}
for (var i = 0; i < updateBindings.Count; i++)
{
var c = updateBindings[i].propertyName.Last();
var value = eulers.x;
if (c == 'y') value = eulers.y;
else if (c == 'z') value = eulers.z;
AddKeyFrameToCurve(updateCurves[i], (float)time, clip.frameRate, value, false);
}
UpdateEditorCurves(clip, updateBindings, updateCurves);
}
// Add a floating point curve key
static void AddFloatKey(AnimationClip clip, EditorCurveBinding sourceBind, SerializedProperty prop, double time)
{
var updateCurves = new List<AnimationCurve>();
var updateBindings = new List<EditorCurveBinding>();
var updated = false;
var info = AnimationClipCurveCache.Instance.GetCurveInfo(clip);
for (var i = 0; i < info.bindings.Length; i++)
{
var binding = info.bindings[i];
if (binding.type != sourceBind.type)
continue;
SerializedProperty valProp = null;
var curve = info.curves[i];
// perfect match on property path, editting a float
if (prop.propertyPath.Equals(binding.propertyName))
{
valProp = prop;
}
// this is a child object
else if (binding.propertyName.Contains(prop.propertyPath))
{
valProp = prop.serializedObject.FindProperty(binding.propertyName);
}
if (valProp != null)
{
var value = GetKeyValue(valProp);
if (!float.IsNaN(value)) // Nan indicates an error retrieving the property value
{
updated = true;
AddKeyFrameToCurve(curve, (float)time, clip.frameRate, value, valProp.propertyType == SerializedPropertyType.Boolean);
updateCurves.Add(curve);
updateBindings.Add(binding);
}
}
}
// Curves don't exist, add them
if (!updated)
{
var propName = AnimationWindowUtility.GetPropertyGroupName(sourceBind.propertyName);
if (!prop.hasChildren)
{
var value = GetKeyValue(prop);
if (!float.IsNaN(value))
{
updateBindings.Add(EditorCurveBinding.FloatCurve(sourceBind.path, sourceBind.type, sourceBind.propertyName));
var curve = new AnimationCurve();
AddKeyFrameToCurve(curve, (float)time, clip.frameRate, value, prop.propertyType == SerializedPropertyType.Boolean);
updateCurves.Add(curve);
}
}
else
{
// special case because subproperties on color aren't 'visible' so you can't iterate over them
if (prop.propertyType == SerializedPropertyType.Color)
{
updateBindings.Add(EditorCurveBinding.FloatCurve(sourceBind.path, sourceBind.type, propName + ".r"));
updateBindings.Add(EditorCurveBinding.FloatCurve(sourceBind.path, sourceBind.type, propName + ".g"));
updateBindings.Add(EditorCurveBinding.FloatCurve(sourceBind.path, sourceBind.type, propName + ".b"));
updateBindings.Add(EditorCurveBinding.FloatCurve(sourceBind.path, sourceBind.type, propName + ".a"));
var c = prop.colorValue;
for (var i = 0; i < 4; i++)
{
var curve = new AnimationCurve();
AddKeyFrameToCurve(curve, (float)time, clip.frameRate, c[i], prop.propertyType == SerializedPropertyType.Boolean);
updateCurves.Add(curve);
}
}
else
{
prop = prop.Copy();
foreach (SerializedProperty cp in prop)
{
updateBindings.Add(EditorCurveBinding.FloatCurve(sourceBind.path, sourceBind.type, cp.propertyPath));
var curve = new AnimationCurve();
AddKeyFrameToCurve(curve, (float)time, clip.frameRate, GetKeyValue(cp), cp.propertyType == SerializedPropertyType.Boolean);
updateCurves.Add(curve);
}
}
}
}
UpdateEditorCurves(clip, updateBindings, updateCurves);
}
public static void RemoveKey(AnimationClip clip, EditorCurveBinding sourceBinding, SerializedProperty prop, double time)
{
if (sourceBinding.isPPtrCurve)
{
RemoveObjectKey(clip, sourceBinding, time);
}
else if (IsRotationKey(sourceBinding))
{
RemoveRotationKey(clip, sourceBinding, prop, time);
}
else
{
RemoveFloatKey(clip, sourceBinding, prop, time);
}
}
public static void RemoveObjectKey(AnimationClip clip, EditorCurveBinding sourceBinding, double time)
{
var info = AnimationClipCurveCache.Instance.GetCurveInfo(clip);
var curveIndex = Array.IndexOf(info.objectBindings, sourceBinding);
if (curveIndex >= 0)
{
var curve = info.objectCurves[curveIndex];
var evalIndex = GetKeyframeAtTime(curve, (float)time, clip.frameRate);
if (evalIndex >= 0)
{
ArrayUtility.RemoveAt(ref curve, evalIndex);
AnimationUtility.SetObjectReferenceCurve(clip, sourceBinding, curve.Length == 0 ? null : curve);
EditorUtility.SetDirty(clip);
}
}
}
public static int GetObjectKeyCount(AnimationClip clip, EditorCurveBinding sourceBinding)
{
var info = AnimationClipCurveCache.Instance.GetCurveInfo(clip);
var curveIndex = Array.IndexOf(info.objectBindings, sourceBinding);
if (curveIndex >= 0)
{
var curve = info.objectCurves[curveIndex];
return curve.Length;
}
return 0;
}
static void RemoveRotationKey(AnimationClip clip, EditorCurveBinding sourceBind, SerializedProperty prop, double time)
{
if (prop.propertyType != SerializedPropertyType.Quaternion)
{
return;
}
var updateCurves = new List<AnimationCurve>();
var updateBindings = new List<EditorCurveBinding>();
var info = AnimationClipCurveCache.Instance.GetCurveInfo(clip);
for (var i = 0; i < info.bindings.Length; i++)
{
if (sourceBind.type != info.bindings[i].type)
continue;
if (info.bindings[i].propertyName.Contains("localEuler"))
{
updateBindings.Add(info.bindings[i]);
updateCurves.Add(info.curves[i]);
}
}
foreach (var c in updateCurves)
{
RemoveKeyFrameFromCurve(c, (float)time, clip.frameRate);
}
UpdateEditorCurves(clip, updateBindings, updateCurves);
}
// Removes the float keys from curves
static void RemoveFloatKey(AnimationClip clip, EditorCurveBinding sourceBind, SerializedProperty prop, double time)
{
var updateCurves = new List<AnimationCurve>();
var updateBindings = new List<EditorCurveBinding>();
var info = AnimationClipCurveCache.Instance.GetCurveInfo(clip);
for (var i = 0; i < info.bindings.Length; i++)
{
var binding = info.bindings[i];
if (binding.type != sourceBind.type)
continue;
SerializedProperty valProp = null;
var curve = info.curves[i];
// perfect match on property path, editting a float
if (prop.propertyPath.Equals(binding.propertyName))
{
valProp = prop;
}
// this is a child object
else if (binding.propertyName.Contains(prop.propertyPath))
{
valProp = prop.serializedObject.FindProperty(binding.propertyName);
}
if (valProp != null)
{
RemoveKeyFrameFromCurve(curve, (float)time, clip.frameRate);
updateCurves.Add(curve);
updateBindings.Add(binding);
}
}
// update the curve. Do this last to not mess with the curve caches we are iterating over
UpdateEditorCurves(clip, updateBindings, updateCurves);
}
static void UpdateEditorCurve(AnimationClip clip, EditorCurveBinding binding, AnimationCurve curve)
{
if (curve.keys.Length == 0)
AnimationUtility.SetEditorCurve(clip, binding, null);
else
AnimationUtility.SetEditorCurve(clip, binding, curve);
}
static void UpdateEditorCurves(AnimationClip clip, List<EditorCurveBinding> bindings, List<AnimationCurve> curves)
{
if (curves.Count == 0)
return;
for (var i = 0; i < curves.Count; i++)
{
UpdateEditorCurve(clip, bindings[i], curves[i]);
}
EditorUtility.SetDirty(clip);
}
public static void RemoveCurves(AnimationClip clip, SerializedProperty prop)
{
if (clip == null || prop == null)
return;
var toRemove = new List<EditorCurveBinding>();
var info = AnimationClipCurveCache.Instance.GetCurveInfo(clip);
for (var i = 0; i < info.bindings.Length; i++)
{
var binding = info.bindings[i];
// check if we match directly, or with a child object
if (prop.propertyPath.Equals(binding.propertyName) || binding.propertyName.Contains(prop.propertyPath))
{
toRemove.Add(binding);
}
}
for (int i = 0; i < toRemove.Count; i++)
{
AnimationUtility.SetEditorCurve(clip, toRemove[i], null);
}
}
// adds a stepped key frame to the given curve
public static void AddKeyFrameToCurve(AnimationCurve curve, float time, float framerate, float value, bool stepped)
{
var key = new Keyframe();
bool add = true;
var keyIndex = GetKeyframeAtTime(curve, time, framerate);
if (keyIndex != -1)
{
add = false;
key = curve[keyIndex]; // retain the tangents and mode
curve.RemoveKey(keyIndex);
}
key.value = value;
key.time = GetKeyTime(time, framerate);
keyIndex = curve.AddKey(key);
if (stepped)
{
AnimationUtility.SetKeyBroken(curve, keyIndex, stepped);
AnimationUtility.SetKeyLeftTangentMode(curve, keyIndex, AnimationUtility.TangentMode.Constant);
AnimationUtility.SetKeyRightTangentMode(curve, keyIndex, AnimationUtility.TangentMode.Constant);
key.outTangent = Mathf.Infinity;
key.inTangent = Mathf.Infinity;
}
else if (add)
{
AnimationUtility.SetKeyLeftTangentMode(curve, keyIndex, AnimationUtility.TangentMode.ClampedAuto);
AnimationUtility.SetKeyRightTangentMode(curve, keyIndex, AnimationUtility.TangentMode.ClampedAuto);
}
if (keyIndex != -1 && !stepped)
{
AnimationUtility.UpdateTangentsFromModeSurrounding(curve, keyIndex);
AnimationUtility.SetKeyBroken(curve, keyIndex, false);
}
}
// Removes a keyframe at the given time from the animation curve
public static bool RemoveKeyFrameFromCurve(AnimationCurve curve, float time, float framerate)
{
var keyIndex = GetKeyframeAtTime(curve, time, framerate);
if (keyIndex == -1)
return false;
curve.RemoveKey(keyIndex);
return true;
}
// gets the value of the key
public static float GetKeyValue(SerializedProperty prop)
{
switch (prop.propertyType)
{
case SerializedPropertyType.Integer:
return prop.intValue;
case SerializedPropertyType.Boolean:
return prop.boolValue ? 1.0f : 0.0f;
case SerializedPropertyType.Float:
return prop.floatValue;
default:
Debug.LogError("Could not convert property type " + prop.propertyType.ToString() + " to float");
break;
}
return float.NaN;
}
public static void SetFromKeyValue(SerializedProperty prop, float keyValue)
{
switch (prop.propertyType)
{
case SerializedPropertyType.Float:
{
prop.floatValue = keyValue;
return;
}
case SerializedPropertyType.Integer:
{
prop.intValue = (int)keyValue;
return;
}
case SerializedPropertyType.Boolean:
{
prop.boolValue = Math.Abs(keyValue) > 0.001f;
return;
}
}
Debug.LogError("Could not convert float to property type " + prop.propertyType.ToString());
}
// gets the index of the key, -1 if not found
public static int GetKeyframeAtTime(AnimationCurve curve, float time, float frameRate)
{
var range = 0.5f / frameRate;
var keys = curve.keys;
for (var i = 0; i < keys.Length; i++)
{
var k = keys[i];
if (k.time >= time - range && k.time < time + range)
{
return i;
}
}
return -1;
}
public static int GetKeyframeAtTime(ObjectReferenceKeyframe[] curve, float time, float frameRate)
{
if (curve == null || curve.Length == 0)
return -1;
var range = 0.5f / frameRate;
for (var i = 0; i < curve.Length; i++)
{
var t = curve[i].time;
if (t >= time - range && t < time + range)
{
return i;
}
}
return -1;
}
public static float GetKeyTime(float time, float frameRate)
{
return Mathf.Round(time * frameRate) / frameRate;
}
public static int KeyCompare(float timeA, float timeB, float frameRate)
{
if (Mathf.Abs(timeA - timeB) <= 0.5f / frameRate)
return 0;
return timeA < timeB ? -1 : 1;
}
// Evaluates an object (bool curve)
public static Object Evaluate(ObjectReferenceKeyframe[] curve, float time)
{
return curve[EvaluateIndex(curve, time)].value;
}
// returns the index from evaluation
public static int EvaluateIndex(ObjectReferenceKeyframe[] curve, float time)
{
if (curve == null || curve.Length == 0)
throw new InvalidOperationException("Can not evaluate a PPtr curve with no entries");
// clamp conditions
if (time <= curve[0].time)
return 0;
if (time >= curve.Last().time)
return curve.Length - 1;
// binary search
var max = curve.Length - 1;
var min = 0;
while (max - min > 1)
{
var imid = (min + max) / 2;
if (Mathf.Approximately(curve[imid].time, time))
return imid;
if (curve[imid].time < time)
min = imid;
else if (curve[imid].time > time)
max = imid;
}
return min;
}
// Shifts the animation clip so the time start at 0
public static void ShiftBySeconds(this AnimationClip clip, float time)
{
var floatBindings = AnimationUtility.GetCurveBindings(clip);
var objectBindings = AnimationUtility.GetObjectReferenceCurveBindings(clip);
// update the float curves
foreach (var bind in floatBindings)
{
var curve = AnimationUtility.GetEditorCurve(clip, bind);
var keys = curve.keys;
for (var i = 0; i < keys.Length; i++)
keys[i].time += time;
curve.keys = keys;
AnimationUtility.SetEditorCurve(clip, bind, curve);
}
// update the PPtr curves
foreach (var bind in objectBindings)
{
var curve = AnimationUtility.GetObjectReferenceCurve(clip, bind);
for (var i = 0; i < curve.Length; i++)
curve[i].time += time;
AnimationUtility.SetObjectReferenceCurve(clip, bind, curve);
}
EditorUtility.SetDirty(clip);
}
public static void ScaleTime(this AnimationClip clip, float scale)
{
var floatBindings = AnimationUtility.GetCurveBindings(clip);
var objectBindings = AnimationUtility.GetObjectReferenceCurveBindings(clip);
// update the float curves
foreach (var bind in floatBindings)
{
var curve = AnimationUtility.GetEditorCurve(clip, bind);
var keys = curve.keys;
for (var i = 0; i < keys.Length; i++)
keys[i].time *= scale;
curve.keys = keys.OrderBy(x => x.time).ToArray();
AnimationUtility.SetEditorCurve(clip, bind, curve);
}
// update the PPtr curves
foreach (var bind in objectBindings)
{
var curve = AnimationUtility.GetObjectReferenceCurve(clip, bind);
for (var i = 0; i < curve.Length; i++)
curve[i].time *= scale;
curve = curve.OrderBy(x => x.time).ToArray();
AnimationUtility.SetObjectReferenceCurve(clip, bind, curve);
}
EditorUtility.SetDirty(clip);
}
// Creates an opposing blend curve that matches the given curve to make sure the result is normalized
public static AnimationCurve CreateMatchingCurve(AnimationCurve curve)
{
Keyframe[] keys = curve.keys;
for (var i = 0; i != keys.Length; i++)
{
if (!Single.IsPositiveInfinity(keys[i].inTangent))
keys[i].inTangent = -keys[i].inTangent;
if (!Single.IsPositiveInfinity(keys[i].outTangent))
keys[i].outTangent = -keys[i].outTangent;
keys[i].value = 1.0f - keys[i].value;
}
return new AnimationCurve(keys);
}
// Sanitizes the keys on an animation to force the property to be normalized
public static Keyframe[] SanitizeCurveKeys(Keyframe[] keys, bool easeIn)
{
if (keys.Length < 2)
{
if (easeIn)
keys = new[] { new Keyframe(0, 0), new Keyframe(1, 1) };
else
keys = new[] { new Keyframe(0, 1), new Keyframe(1, 0) };
}
else if (easeIn)
{
keys[0].time = 0;
keys[keys.Length - 1].time = 1;
keys[keys.Length - 1].value = 1;
}
else
{
keys[0].time = 0;
keys[0].value = 1;
keys[keys.Length - 1].time = 1;
}
return keys;
}
}
}