Example #1
0
        /// <summary>
        /// Applies any changes made to the animation curves or events to the actual animation clip. Only works for
        /// non-imported animation clips.
        /// </summary>
        /// <param name="tangents">Tangent modes for all the saved animation curves.</param>
        public void Apply(out EditorAnimClipTangents tangents)
        {
            if (isImported || clip == null)
            {
                tangents = null;
                return;
            }

            List <NamedVector3Curve>    positionCurves = new List <NamedVector3Curve>();
            List <NamedQuaternionCurve> rotationCurves = new List <NamedQuaternionCurve>();
            List <NamedVector3Curve>    scaleCurves    = new List <NamedVector3Curve>();
            List <NamedFloatCurve>      floatCurves    = new List <NamedFloatCurve>();

            List <EditorVector3CurveTangents> positionTangents = new List <EditorVector3CurveTangents>();
            List <EditorVector3CurveTangents> rotationTangents = new List <EditorVector3CurveTangents>();
            List <EditorVector3CurveTangents> scaleTangents    = new List <EditorVector3CurveTangents>();
            List <EditorFloatCurveTangents>   floatTangents    = new List <EditorFloatCurveTangents>();

            foreach (var kvp in curves)
            {
                string[] pathEntries = kvp.Key.Split('/');
                if (pathEntries.Length == 0)
                {
                    continue;
                }

                string lastEntry = pathEntries[pathEntries.Length - 1];

                if (lastEntry == "Position" || lastEntry == "Rotation" || lastEntry == "Scale")
                {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < pathEntries.Length - 2; i++)
                    {
                        sb.Append(pathEntries[i] + "/");
                    }

                    if (pathEntries.Length > 1)
                    {
                        sb.Append(pathEntries[pathEntries.Length - 2]);
                    }

                    string curvePath = sb.ToString();

                    NamedVector3Curve curve = new NamedVector3Curve();
                    curve.name  = curvePath;
                    curve.curve = AnimationUtility.CombineCurve(new[]
                    {
                        new AnimationCurve(kvp.Value.curveInfos[0].curve.KeyFrames),
                        new AnimationCurve(kvp.Value.curveInfos[1].curve.KeyFrames),
                        new AnimationCurve(kvp.Value.curveInfos[1].curve.KeyFrames)
                    });

                    EditorVector3CurveTangents curveTangents = new EditorVector3CurveTangents();
                    curveTangents.name      = curvePath;
                    curveTangents.tangentsX = kvp.Value.curveInfos[0].curve.TangentModes;
                    curveTangents.tangentsY = kvp.Value.curveInfos[1].curve.TangentModes;
                    curveTangents.tangentsZ = kvp.Value.curveInfos[2].curve.TangentModes;

                    if (lastEntry == "Position")
                    {
                        positionCurves.Add(curve);
                        positionTangents.Add(curveTangents);
                    }
                    else if (lastEntry == "Rotation")
                    {
                        NamedQuaternionCurve quatCurve = new NamedQuaternionCurve();
                        quatCurve.name  = curve.name;
                        quatCurve.curve = AnimationUtility.EulerToQuaternionCurve(curve.curve);

                        rotationCurves.Add(quatCurve);
                        rotationTangents.Add(curveTangents);
                    }
                    else if (lastEntry == "Scale")
                    {
                        scaleCurves.Add(curve);
                        scaleTangents.Add(curveTangents);
                    }
                }
                else
                {
                    Action <int, string, string, AnimationCurveFlags> addCurve = (idx, path, subPath, flags) =>
                    {
                        string fullPath = path + subPath;

                        NamedFloatCurve curve = new NamedFloatCurve(fullPath,
                                                                    new AnimationCurve(kvp.Value.curveInfos[idx].curve.KeyFrames));
                        curve.flags = flags;

                        EditorFloatCurveTangents curveTangents = new EditorFloatCurveTangents();
                        curveTangents.name     = fullPath;
                        curveTangents.tangents = kvp.Value.curveInfos[idx].curve.TangentModes;

                        floatCurves.Add(curve);
                        floatTangents.Add(curveTangents);
                    };

                    switch (kvp.Value.type)
                    {
                    case SerializableProperty.FieldType.Vector2:
                        addCurve(0, kvp.Key, ".x", 0);
                        addCurve(1, kvp.Key, ".y", 0);
                        break;

                    case SerializableProperty.FieldType.Vector3:
                        addCurve(0, kvp.Key, ".x", 0);
                        addCurve(1, kvp.Key, ".y", 0);
                        addCurve(2, kvp.Key, ".z", 0);
                        break;

                    case SerializableProperty.FieldType.Vector4:
                        addCurve(0, kvp.Key, ".x", 0);
                        addCurve(1, kvp.Key, ".y", 0);
                        addCurve(2, kvp.Key, ".z", 0);
                        addCurve(3, kvp.Key, ".w", 0);
                        break;

                    case SerializableProperty.FieldType.Color:
                        addCurve(0, kvp.Key, ".r", 0);
                        addCurve(1, kvp.Key, ".g", 0);
                        addCurve(2, kvp.Key, ".b", 0);
                        addCurve(3, kvp.Key, ".a", 0);
                        break;

                    case SerializableProperty.FieldType.Bool:
                    case SerializableProperty.FieldType.Int:
                    case SerializableProperty.FieldType.Float:
                    {
                        AnimationCurveFlags flags = 0;
                        string path = kvp.Key;

                        if (IsMorphShapeCurve(kvp.Key))
                        {
                            string   trimmedPath = path.Trim('/');
                            string[] entries     = trimmedPath.Split('/');

                            bool isWeight = entries[entries.Length - 2] == "Weight";

                            if (isWeight)
                            {
                                flags = AnimationCurveFlags.MorphWeight;
                            }
                            else
                            {
                                flags = AnimationCurveFlags.MorphFrame;
                            }

                            path = entries[entries.Length - 1];
                        }

                        addCurve(0, path, "", flags);
                    }
                    break;
                    }
                }
            }

            AnimationCurves newClipCurves = new AnimationCurves();

            newClipCurves.Position = positionCurves.ToArray();
            newClipCurves.Rotation = rotationCurves.ToArray();
            newClipCurves.Scale    = scaleCurves.ToArray();
            newClipCurves.Generic  = floatCurves.ToArray();

            clip.Curves     = newClipCurves;
            clip.Events     = events;
            clip.SampleRate = (uint)sampleRate;

            tangents = new EditorAnimClipTangents();
            tangents.positionCurves = positionTangents.ToArray();
            tangents.rotationCurves = rotationTangents.ToArray();
            tangents.scaleCurves    = scaleTangents.ToArray();
            tangents.floatCurves    = floatTangents.ToArray();
        }
Example #2
0
 /// <summary>Constructs a new named animation curve.</summary>
 /// <param name="name">Name of the curve.</param>
 /// <param name="flags">Flags that describe the animation curve.</param>
 /// <param name="curve">Curve containing the animation data.</param>
 public NamedVector3Curve(string name, AnimationCurveFlags flags, Vector3Curve curve)
 {
     this.name  = name;
     this.flags = (AnimationCurveFlags)0;
     this.curve = curve;
 }
Example #3
0
 /// <summary>Constructs a new named animation curve.</summary>
 /// <param name="name">Name of the curve.</param>
 /// <param name="flags">Flags that describe the animation curve.</param>
 /// <param name="curve">Curve containing the animation data.</param>
 public NamedIntegerCurve(string name, AnimationCurveFlags flags, IntegerCurve curve)
 {
     this.name  = name;
     this.flags = (AnimationCurveFlags)0;
     this.curve = curve;
 }
Example #4
0
 /// <summary>Constructs a new named animation curve.</summary>
 /// <param name="name">Name of the curve.</param>
 /// <param name="flags">Flags that describe the animation curve.</param>
 /// <param name="curve">Curve containing the animation data.</param>
 public NamedQuaternionCurve(string name, AnimationCurveFlags flags, QuaternionCurve curve)
 {
     this.name  = name;
     this.flags = (AnimationCurveFlags)0;
     this.curve = curve;
 }
 /// <summary>
 /// Constructor for internal runtime use only.
 /// </summary>
 /// <param name="name">Name of the curve.</param>
 /// <param name="flags">Flags that describe the animation curve, of type <see cref="AnimationCurveFlags"/>.</param>
 /// <param name="x">Curve representing the x axis of the vector.</param>
 /// <param name="y">Curve representing the y axis of the vector.</param>
 /// <param name="z">Curve representing the z axis of the vector.</param>
 private NamedVector3Curve(string name, int flags, AnimationCurve x, AnimationCurve y, AnimationCurve z)
 {
     Name = name;
     Flags = (AnimationCurveFlags) flags;
     X = x;
     Y = y;
     Z = z;
 }
 /// <summary>
 /// Constructor for internal runtime use only.
 /// </summary>
 /// <param name="name">Name of the curve.</param>
 /// <param name="flags">Flags that describe the animation curve, of type <see cref="AnimationCurveFlags"/>.</param>
 /// <param name="curve">Curve representing the floating point values.</param>
 private NamedFloatCurve(string name, int flags, AnimationCurve curve)
 {
     Name = name;
     Flags = (AnimationCurveFlags)flags;
     Curve = curve;
 }