AnimationClipCurveCache.cs 13.9 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
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;

namespace UnityEditor.Timeline
{
    struct CurveBindingPair
    {
        public EditorCurveBinding binding;
        public AnimationCurve curve;
        public ObjectReferenceKeyframe[] objectCurve;
    }

    class CurveBindingGroup
    {
        public CurveBindingPair[] curveBindingPairs { get; set; }
        public Vector2 timeRange { get; set; }
        public Vector2 valueRange { get; set; }

        public bool isFloatCurve
        {
            get
            {
                return curveBindingPairs != null && curveBindingPairs.Length > 0 &&
                    curveBindingPairs[0].curve != null;
            }
        }

        public bool isObjectCurve
        {
            get
            {
                return curveBindingPairs != null && curveBindingPairs.Length > 0 &&
                    curveBindingPairs[0].objectCurve != null;
            }
        }

        public int count
        {
            get
            {
                if (curveBindingPairs == null)
                    return 0;
                return curveBindingPairs.Length;
            }
        }
    }

    class AnimationClipCurveInfo
    {
        bool m_CurveDirty = true;
        bool m_KeysDirty = true;

        public bool dirty
        {
            get { return m_CurveDirty; }
            set
            {
                m_CurveDirty = value;
                if (m_CurveDirty)
                {
                    m_KeysDirty = true;
                    if (m_groupings != null)
                        m_groupings.Clear();
                }
            }
        }

        public AnimationCurve[] curves;
        public EditorCurveBinding[] bindings;

        public EditorCurveBinding[] objectBindings;
        public List<ObjectReferenceKeyframe[]> objectCurves;

        Dictionary<string, CurveBindingGroup> m_groupings;

        // to tell whether the cache has changed
        public int version { get; private set; }

        float[] m_KeyTimes;

        Dictionary<EditorCurveBinding, float[]> m_individualBindinsKey;

        public float[] keyTimes
        {
            get
            {
                if (m_KeysDirty || m_KeyTimes == null)
                {
                    RebuildKeyCache();
                }
                return m_KeyTimes;
            }
        }

        public float[] GetCurveTimes(EditorCurveBinding curve)
        {
            return GetCurveTimes(new[] { curve });
        }

        public float[] GetCurveTimes(EditorCurveBinding[] curves)
        {
            if (m_KeysDirty || m_KeyTimes == null)
            {
                RebuildKeyCache();
            }

            var keyTimes = new List<float>();
            for (int i = 0; i < curves.Length; i++)
            {
                var c = curves[i];
                if (m_individualBindinsKey.ContainsKey(c))
                {
                    keyTimes.AddRange(m_individualBindinsKey[c]);
                }
            }
            return keyTimes.ToArray();
        }

        void RebuildKeyCache()
        {
            m_individualBindinsKey = new Dictionary<EditorCurveBinding, float[]>();

            List<float> keys = curves.SelectMany(y => y.keys).Select(z => z.time).ToList();
            for (int i = 0; i < objectCurves.Count; i++)
            {
                var kf = objectCurves[i];
                keys.AddRange(kf.Select(x => x.time));
            }

            for (int b = 0; b < bindings.Count(); b++)
            {
                m_individualBindinsKey.Add(bindings[b], curves[b].keys.Select(k => k.time).Distinct().ToArray());
            }

            m_KeyTimes = keys.OrderBy(x => x).Distinct().ToArray();
            m_KeysDirty = false;
        }

        public void Update(AnimationClip clip)
        {
            List<EditorCurveBinding> postfilter = new List<EditorCurveBinding>();
            var clipBindings = AnimationUtility.GetCurveBindings(clip);
            for (int i = 0; i < clipBindings.Length; i++)
            {
                var bind = clipBindings[i];
                if (!bind.propertyName.Contains("LocalRotation.w"))
                    postfilter.Add(RotationCurveInterpolation.RemapAnimationBindingForRotationCurves(bind, clip));
            }
            bindings = postfilter.ToArray();

            curves = new AnimationCurve[bindings.Length];
            for (int i = 0; i < bindings.Length; i++)
            {
                curves[i] = AnimationUtility.GetEditorCurve(clip, bindings[i]);
            }

            objectBindings = AnimationUtility.GetObjectReferenceCurveBindings(clip);
            objectCurves = new List<ObjectReferenceKeyframe[]>(objectBindings.Length);
            for (int i = 0; i < objectBindings.Length; i++)
            {
                objectCurves.Add(AnimationUtility.GetObjectReferenceCurve(clip, objectBindings[i]));
            }

            m_CurveDirty = false;
            m_KeysDirty = true;

            version = version + 1;
        }

        public bool GetBindingForCurve(AnimationCurve curve, ref EditorCurveBinding binding)
        {
            for (int i = 0; i < curves.Length; i++)
            {
                if (curve == curves[i])
                {
                    binding = bindings[i];
                    return true;
                }
            }
            return false;
        }

        public AnimationCurve GetCurveForBinding(EditorCurveBinding binding)
        {
            for (int i = 0; i < curves.Length; i++)
            {
                if (binding.Equals(bindings[i]))
                {
                    return curves[i];
                }
            }
            return null;
        }

        public ObjectReferenceKeyframe[] GetObjectCurveForBinding(EditorCurveBinding binding)
        {
            if (objectCurves == null)
                return null;

            for (int i = 0; i < objectCurves.Count; i++)
            {
                if (binding.Equals(objectBindings[i]))
                {
                    return objectCurves[i];
                }
            }
            return null;
        }

        // given a groupID, get the list of curve bindings
        public CurveBindingGroup GetGroupBinding(string groupID)
        {
            if (m_groupings == null)
                m_groupings = new Dictionary<string, CurveBindingGroup>();

            CurveBindingGroup result = null;
            if (!m_groupings.TryGetValue(groupID, out result))
            {
                result = new CurveBindingGroup();
                result.timeRange = new Vector2(float.MaxValue, float.MinValue);
                result.valueRange = new Vector2(float.MaxValue, float.MinValue);
                List<CurveBindingPair> found = new List<CurveBindingPair>();
                for (int i = 0; i < bindings.Length; i++)
                {
                    if (bindings[i].GetGroupID() == groupID)
                    {
                        CurveBindingPair pair = new CurveBindingPair();
                        pair.binding = bindings[i];
                        pair.curve = curves[i];
                        found.Add(pair);

                        for (int k = 0; k < curves[i].keys.Length; k++)
                        {
                            var key = curves[i].keys[k];
                            result.timeRange = new Vector2(Mathf.Min(key.time, result.timeRange.x), Mathf.Max(key.time, result.timeRange.y));
                            result.valueRange = new Vector2(Mathf.Min(key.value, result.valueRange.x), Mathf.Max(key.value, result.valueRange.y));
                        }
                    }
                }
                for (int i = 0; i < objectBindings.Length; i++)
                {
                    if (objectBindings[i].GetGroupID() == groupID)
                    {
                        CurveBindingPair pair = new CurveBindingPair();
                        pair.binding = objectBindings[i];
                        pair.objectCurve = objectCurves[i];
                        found.Add(pair);

                        for (int k = 0; k < objectCurves[i].Length; k++)
                        {
                            var key = objectCurves[i][k];
                            result.timeRange = new Vector2(Mathf.Min(key.time, result.timeRange.x), Mathf.Max(key.time, result.timeRange.y));
                        }
                    }
                }

                result.curveBindingPairs = found.OrderBy(x => AnimationWindowUtility.GetComponentIndex(x.binding.propertyName)).ToArray();

                m_groupings.Add(groupID, result);
            }
            return result;
        }
    }

    // Cache for storing the animation clip data
    class AnimationClipCurveCache
    {
        static AnimationClipCurveCache s_Instance;
        Dictionary<AnimationClip, AnimationClipCurveInfo> m_ClipCache = new Dictionary<AnimationClip, AnimationClipCurveInfo>();
        bool m_IsEnabled;


        public static AnimationClipCurveCache Instance
        {
            get
            {
                if (s_Instance == null)
                {
                    s_Instance = new AnimationClipCurveCache();
                }

                return s_Instance;
            }
        }

        public void OnEnable()
        {
            if (!m_IsEnabled)
            {
                AnimationUtility.onCurveWasModified += OnCurveWasModified;
                m_IsEnabled = true;
            }
        }

        public void OnDisable()
        {
            if (m_IsEnabled)
            {
                AnimationUtility.onCurveWasModified -= OnCurveWasModified;
                m_IsEnabled = false;
            }
        }

        // callback when a curve is edited. Force the cache to update next time it's accessed
        void OnCurveWasModified(AnimationClip clip, EditorCurveBinding binding, AnimationUtility.CurveModifiedType modification)
        {
            if (modification == AnimationUtility.CurveModifiedType.CurveDeleted)
            {
                m_ClipCache.Remove(clip);
            }
            else
            {
                AnimationClipCurveInfo data;
                if (m_ClipCache.TryGetValue(clip, out data))
                {
                    data.dirty = true;
                }
            }
        }

        public AnimationClipCurveInfo GetCurveInfo(AnimationClip clip)
        {
            AnimationClipCurveInfo data;
            if (clip == null)
                return null;
            if (!m_ClipCache.TryGetValue(clip, out data))
            {
                data = new AnimationClipCurveInfo();
                data.dirty = true;
                m_ClipCache[clip] = data;
            }
            if (data.dirty)
            {
                data.Update(clip);
            }
            return data;
        }

        public void ClearCachedProxyClips()
        {
            var toRemove = new List<AnimationClip>();
            foreach (var entry in m_ClipCache)
            {
                var clip = entry.Key;
                if (clip != null && (clip.hideFlags & HideFlags.HideAndDontSave) == HideFlags.HideAndDontSave)
                    toRemove.Add(clip);
            }

            foreach (var clip in toRemove)
            {
                m_ClipCache.Remove(clip);
                Object.DestroyImmediate(clip, true);
            }
        }


        public void Clear()
        {
            ClearCachedProxyClips();
            m_ClipCache.Clear();
        }

    }

    static class EditorCurveBindingExtension
    {
        // identifier to generate an id thats the same for all curves in the same group
        public static string GetGroupID(this EditorCurveBinding binding)
        {
            return binding.type + AnimationWindowUtility.GetPropertyGroupName(binding.propertyName);
        }
    }


    static class CurveBindingGroupExtensions
    {
        // Extentions to determine curve types
        public static bool IsEnableGroup(this CurveBindingGroup curves)
        {
            return curves.isFloatCurve && curves.count == 1 && curves.curveBindingPairs[0].binding.propertyName == "m_Enabled";
        }

        public static bool IsVectorGroup(this CurveBindingGroup curves)
        {
            if (!curves.isFloatCurve)
                return false;
            if (curves.count <= 1 || curves.count > 4)
                return false;
            char l = curves.curveBindingPairs[0].binding.propertyName.Last();
            return l == 'x' || l == 'y' || l == 'z' || l == 'w';
        }

        public static bool IsColorGroup(this CurveBindingGroup curves)
        {
            if (!curves.isFloatCurve)
                return false;
            if (curves.count != 3 && curves.count != 4)
                return false;
            char l = curves.curveBindingPairs[0].binding.propertyName.Last();
            return l == 'r' || l == 'g' || l == 'b' || l == 'a';
        }

        public static string GetDescription(this CurveBindingGroup group, float t)
        {
            string result = string.Empty;
            if (group.isFloatCurve)
            {
                if (group.count > 1)
                {
                    result += "(" + group.curveBindingPairs[0].curve.Evaluate(t).ToString("0.##");
                    for (int j = 1; j < group.curveBindingPairs.Length; j++)
                    {
                        result += "," + group.curveBindingPairs[j].curve.Evaluate(t).ToString("0.##");
                    }
                    result += ")";
                }
                else
                {
                    result = group.curveBindingPairs[0].curve.Evaluate(t).ToString("0.##");
                }
            }
            else if (group.isObjectCurve)
            {
                Object obj = null;
                if (group.curveBindingPairs[0].objectCurve.Length > 0)
                    obj = CurveEditUtility.Evaluate(group.curveBindingPairs[0].objectCurve, t);
                result = (obj == null ? "None" : obj.name);
            }

            return result;
        }
    }
}