Example #1
0
        void UpdateCurrentObjectAnimation(GameObject gObject)
        {
            if (null == currentObject || currentObject != gObject)
            {
                return;
            }

            Clear();

            AnimationSet animationSet = GlobalState.Animation.GetObjectAnimation(gObject);

            if (null == animationSet)
            {
                UpdateTrackName();
                return;
            }

            // Take only one curve (the first one) to add keys
            foreach (AnimationKey key in animationSet.curves[0].keys)
            {
                if (!keys.TryGetValue(key.frame, out List <AnimKey> keyList))
                {
                    keyList         = new List <AnimKey>();
                    keys[key.frame] = keyList;
                }

                keyList.Add(new AnimKey(key.value, key.interpolation));
            }

            UpdateTrackName();

            Transform keyframes = transform.Find("MainPanel/Tracks/Summary/Keyframes");

            foreach (var key in keys)
            {
                GameObject     keyframe = GameObject.Instantiate(keyframePrefab, keyframes);
                List <AnimKey> animKeys = key.Value;
                AnimKey        firstKey = animKeys[0];
                switch (firstKey.interpolation)
                {
                case Interpolation.Constant:
                    keyframe.GetComponent <MeshRenderer>().material.SetColor("_UnlitColor", constantInterpolationColor);
                    break;

                case Interpolation.Linear:
                    keyframe.GetComponent <MeshRenderer>().material.SetColor("_UnlitColor", linearInterpolationColor);
                    break;

                case Interpolation.Bezier:
                    keyframe.GetComponent <MeshRenderer>().material.SetColor("_UnlitColor", bezierInterpolationColor);
                    break;
                }
            }

            UpdateKeyframes();
        }
Example #2
0
        private void ImportAnimation(Assimp.Node node, GameObject go, Quaternion cumulRotation)
        {
            Assimp.Animation animation = scene.Animations[0];
            if ((GlobalState.Animation.fps / (float)animation.TicksPerSecond) * animation.DurationInTicks > GlobalState.Animation.EndFrame)
            {
                GlobalState.Animation.EndFrame = Mathf.CeilToInt(GlobalState.Animation.fps / (float)animation.TicksPerSecond * (float)animation.DurationInTicks) + 1;
            }
            Assimp.NodeAnimationChannel nodeChannel = animation.NodeAnimationChannels.Find(x => x.NodeName == node.Name);
            if (nodeChannel == null)
            {
                nodeChannel = animation.NodeAnimationChannels.Find(x => x.NodeName.Split('_')[0] == node.Name);
            }
            if (null != nodeChannel)
            {
                if (nodeChannel.PositionKeyCount < 2 && nodeChannel.RotationKeyCount < 2)
                {
                    return;
                }
                AnimationSet animationSet = new AnimationSet(go);
                animationSet.ComputeCache();
                if (nodeChannel.PositionKeyCount > 1 || nodeChannel.RotationKeyCount == nodeChannel.PositionKeyCount)
                {
                    foreach (Assimp.VectorKey vectorKey in nodeChannel.PositionKeys)
                    {
                        int frame = Mathf.CeilToInt((float)vectorKey.Time * GlobalState.Animation.fps / (float)animation.TicksPerSecond) + 1;
                        animationSet.curves[AnimatableProperty.PositionX].AddKey(new AnimationKey(frame, vectorKey.Value.X, Interpolation.Bezier));
                        animationSet.curves[AnimatableProperty.PositionY].AddKey(new AnimationKey(frame, vectorKey.Value.Y, Interpolation.Bezier));
                        animationSet.curves[AnimatableProperty.PositionZ].AddKey(new AnimationKey(frame, vectorKey.Value.Z, Interpolation.Bezier));
                    }
                }
                foreach (Assimp.VectorKey vectorKey in nodeChannel.ScalingKeys)
                {
                    int frame = Mathf.CeilToInt((float)vectorKey.Time * GlobalState.Animation.fps / (float)animation.TicksPerSecond) + 1;
                    animationSet.curves[AnimatableProperty.ScaleX].AddKey(new AnimationKey(frame, vectorKey.Value.X, Interpolation.Bezier));
                    animationSet.curves[AnimatableProperty.ScaleY].AddKey(new AnimationKey(frame, vectorKey.Value.Y, Interpolation.Bezier));
                    animationSet.curves[AnimatableProperty.ScaleZ].AddKey(new AnimationKey(frame, vectorKey.Value.Z, Interpolation.Bezier));
                }
                Vector3 previousRotation = Vector3.zero;
                foreach (Assimp.QuaternionKey quaternionKey in nodeChannel.RotationKeys)
                {
                    int        frame       = Mathf.RoundToInt((float)quaternionKey.Time * GlobalState.Animation.fps / (float)animation.TicksPerSecond) + 1;
                    Quaternion uQuaternion = new Quaternion(quaternionKey.Value.X, quaternionKey.Value.Y, quaternionKey.Value.Z, quaternionKey.Value.W);
                    uQuaternion = cumulRotation * uQuaternion * Quaternion.Inverse(cumulRotation);

                    Vector3 eulerValue = uQuaternion.eulerAngles;
                    eulerValue.x = previousRotation.x + Mathf.DeltaAngle(previousRotation.x, eulerValue.x);
                    eulerValue.y = previousRotation.y + Mathf.DeltaAngle(previousRotation.y, eulerValue.y);
                    eulerValue.z = previousRotation.z + Mathf.DeltaAngle(previousRotation.z, eulerValue.z);
                    animationSet.curves[AnimatableProperty.RotationX].AddKey(new AnimationKey(frame, eulerValue.x, Interpolation.Bezier));
                    animationSet.curves[AnimatableProperty.RotationY].AddKey(new AnimationKey(frame, eulerValue.y, Interpolation.Bezier));
                    animationSet.curves[AnimatableProperty.RotationZ].AddKey(new AnimationKey(frame, eulerValue.z, Interpolation.Bezier));
                    previousRotation = eulerValue;
                }
                GlobalState.Animation.SetObjectAnimations(go, animationSet);
            }
        }
Example #3
0
 public AnimationSet(AnimationSet set)
 {
     transform = set.transform;
     foreach (KeyValuePair <AnimatableProperty, Curve> curve in set.curves)
     {
         curves.Add(curve.Key, new Curve(curve.Key));
         SetCurve(curve.Key, curve.Value.keys);
         curves[curve.Key].ComputeCache();
     }
 }
Example #4
0
 public TangentSimpleSolver(Vector3 targetPosition, Quaternion targetRotation, AnimationSet animation, int frame, int start, int end, double tanEnergy)
 {
     positionTarget  = targetPosition;
     rotationTarget  = targetRotation;
     ObjectAnimation = animation;
     currentFrame    = frame;
     startFrame      = start;
     endFrame        = end;
     tangentEnergy   = tanEnergy;
 }
Example #5
0
 private void AddSegmentHierarchy(RigGoalController controller, int frame)
 {
     for (int i = 0; i < controller.AnimToRoot.Count; i++)
     {
         AnimationSet anim = controller.AnimToRoot[i];
         if (null != anim)
         {
             AddSegmentKeyframes(frame, anim);
         }
     }
 }
Example #6
0
        public AnimationSet GetOrCreateObjectAnimation(GameObject gobject)
        {
            AnimationSet animationSet = GetObjectAnimation(gobject);

            if (null == animationSet)
            {
                animationSet = new AnimationSet(gobject);
                animations.Add(gobject, animationSet);
            }
            return(animationSet);
        }
Example #7
0
 public void SetObjectAnimations(GameObject gobject, AnimationSet animationSet, bool callEvent = true)
 {
     animations[gobject] = animationSet;
     foreach (Curve curve in animationSet.curves.Values)
     {
         curve.ComputeCache();
     }
     if (callEvent)
     {
         onAddAnimation.Invoke(gobject);
     }
 }
Example #8
0
        private void GetAllAnimations(Transform target, List <AnimationSet> animations)
        {
            AnimationSet animationSet = GlobalState.Animation.GetObjectAnimation(target.gameObject);

            if (null != animationSet)
            {
                animations.Add(animationSet);
            }
            foreach (Transform child in target)
            {
                GetAllAnimations(child, animations);
            }
        }
Example #9
0
        private void RecursiveAnimation(List <Curve> curves, Transform target)
        {
            AnimationSet anim = GlobalState.Animation.GetObjectAnimation(target.gameObject);

            if (null != anim)
            {
                curves.Add(anim.GetCurve(AnimatableProperty.PositionX));
            }
            foreach (Transform child in target)
            {
                RecursiveAnimation(curves, child);
            }
        }
        private void RecursiveClear(Transform target)
        {
            AnimationSet anim = GlobalState.Animation.GetObjectAnimation(target.gameObject);

            if (null != anim)
            {
                animationSets.Add(target.gameObject, anim);
            }

            foreach (Transform child in target)
            {
                RecursiveClear(child);
            }
        }
Example #11
0
        public CommandMoveKeyframes(GameObject obj, int frame, int newFrame) : base("Move Keyframes")
        {
            gObject = obj;
            AnimationSet animationSet = GlobalState.Animation.GetObjectAnimation(obj);

            if (null == animationSet)
            {
                return;
            }
            foreach (Curve curve in animationSet.curves.Values)
            {
                new CommandMoveKeyframe(gObject, curve.property, frame, newFrame).Submit();
            }
        }
Example #12
0
        private void AddSegmentKeyframes(int frame, AnimationSet animation)
        {
            if (!animation.GetCurve(AnimatableProperty.PositionX).Evaluate(frame, out float posx))
            {
                posx = animation.transform.localPosition.x;
            }
            if (!animation.GetCurve(AnimatableProperty.PositionY).Evaluate(frame, out float posy))
            {
                posy = animation.transform.localPosition.y;
            }
            if (!animation.GetCurve(AnimatableProperty.PositionZ).Evaluate(frame, out float posz))
            {
                posz = animation.transform.localPosition.z;
            }
            if (!animation.GetCurve(AnimatableProperty.RotationX).Evaluate(frame, out float rotx))
            {
                rotx = animation.transform.localEulerAngles.x;
            }
            if (!animation.GetCurve(AnimatableProperty.RotationY).Evaluate(frame, out float roty))
            {
                roty = animation.transform.localEulerAngles.y;
            }
            if (!animation.GetCurve(AnimatableProperty.RotationZ).Evaluate(frame, out float rotz))
            {
                rotz = animation.transform.localEulerAngles.z;
            }
            if (!animation.GetCurve(AnimatableProperty.ScaleX).Evaluate(frame, out float scax))
            {
                scax = animation.transform.localScale.x;
            }
            if (!animation.GetCurve(AnimatableProperty.ScaleY).Evaluate(frame, out float scay))
            {
                scay = animation.transform.localScale.y;
            }
            if (!animation.GetCurve(AnimatableProperty.ScaleZ).Evaluate(frame, out float scaz))
            {
                scaz = animation.transform.localScale.z;
            }

            AddFilteredKeyframeTangent(animation.transform.gameObject,
                                       new AnimationKey(frame, posx),
                                       new AnimationKey(frame, posy),
                                       new AnimationKey(frame, posz),
                                       new AnimationKey(frame, rotx),
                                       new AnimationKey(frame, roty),
                                       new AnimationKey(frame, rotz),
                                       new AnimationKey(frame, scax),
                                       new AnimationKey(frame, scay),
                                       new AnimationKey(frame, scaz));
        }
Example #13
0
        public void AddKeyframe(GameObject gobject, AnimatableProperty property, AnimationKey key)
        {
            AnimationSet animationSet = GetObjectAnimation(gobject);

            if (null == animationSet)
            {
                animationSet = new AnimationSet(gobject);
                animations.Add(gobject, animationSet);
            }
            Curve curve = animationSet.GetCurve(property);

            curve.AddKey(key);
            onChangeCurve.Invoke(gobject, property);
        }
Example #14
0
 public TangentRigSolver(Vector3 targetPosition, Quaternion targetRotation, AnimationSet objectAnim, List <AnimationSet> animation, int frame, int startFrame, int endFrame, double continuity)
 {
     this.targetPosition = targetPosition;
     this.targetRotation = targetRotation;
     animationList       = new List <AnimationSet>();
     controllers         = new List <RigGoalController>();
     curves = new List <Curve>();
     GetCurves(objectAnim, animation);
     objectAnimation   = objectAnim;
     animationCount    = animationList.Count;
     tangentContinuity = continuity;
     currentFrame      = frame;
     firstFrame        = startFrame;
     lastFrame         = endFrame;
 }
Example #15
0
        public void MoveKeyframe(GameObject gobject, AnimatableProperty property, int frame, int newFrame)
        {
            AnimationSet animationSet = GetObjectAnimation(gobject);

            if (null == animationSet)
            {
                return;
            }

            animationSet.GetCurve(property).MoveKey(frame, newFrame);
            if (!IsAnimating())
            {
                EvaluateAnimations();
            }
        }
Example #16
0
        public void SetPathToRoot(RigController controller, List <Transform> path)
        {
            path.ForEach(x =>
            {
                AnimationSet anim = GlobalState.Animation.GetObjectAnimation(x.gameObject);
                PathToRoot.Add(x);

                AnimToRoot.Add(anim);
            });
            if (PathToRoot.Count == 0)
            {
                PathToRoot.Add(transform);
            }
            Animation      = GlobalState.Animation.GetObjectAnimation(this.gameObject);
            RootController = controller;
        }
Example #17
0
 public void CopyAnimation(GameObject source, GameObject target)
 {
     if (animations.TryGetValue(source, out AnimationSet sourceAnim))
     {
         AnimationSet targetAnim = new AnimationSet(target);
         foreach (KeyValuePair <AnimatableProperty, Curve> curve in sourceAnim.curves)
         {
             targetAnim.SetCurve(curve.Key, curve.Value.keys);
         }
         SetObjectAnimations(target, targetAnim);
     }
     for (int i = 0; i < source.transform.childCount; i++)
     {
         CopyAnimation(source.transform.GetChild(i).gameObject, target.transform.GetChild(i).gameObject);
     }
 }
Example #18
0
        private void RecursiveMoveKeyframe(Transform target, int frame, int newFrame)
        {
            AnimationSet animationSet = GlobalState.Animation.GetObjectAnimation(target.gameObject);

            if (animationSet != null)
            {
                foreach (Curve curve in animationSet.curves.Values)
                {
                    new CommandMoveKeyframe(target.gameObject, curve.property, frame, newFrame).Submit();
                }
            }
            foreach (Transform child in target)
            {
                RecursiveMoveKeyframe(child, frame, newFrame);
            }
        }
Example #19
0
        public CommandAddKeyframeTangent(GameObject obj, AnimatableProperty property, int frame, int startFrame, int endFrame, List <AnimationKey> keysChanged)
        {
            gObject       = obj;
            this.property = property;
            oldKeys       = new List <AnimationKey>();
            newKeys       = keysChanged;

            AnimationSet animationSet = GlobalState.Animation.GetObjectAnimation(gObject);

            if (null == animationSet)
            {
                return;
            }
            Curve curve = animationSet.GetCurve(property);

            curve.GetTangentKeys(startFrame, endFrame, ref oldKeys);
        }
Example #20
0
        private void RemoveEmptyAnimationSet(GameObject gobject)
        {
            AnimationSet animationSet = GetObjectAnimation(gobject);

            if (null == animationSet)
            {
                return;
            }
            foreach (Curve curve in animationSet.curves.Values)
            {
                if (curve.keys.Count != 0)
                {
                    return;
                }
            }

            animations.Remove(gobject);
        }
Example #21
0
        public bool ObjectHasKeyframeAt(GameObject gobject, int frame)
        {
            AnimationSet animationSet = GetObjectAnimation(gobject);

            if (null == animationSet)
            {
                return(false);
            }

            foreach (var curve in animationSet.curves.Values)
            {
                if (curve.HasKeyAt(frame))
                {
                    return(true);
                }
            }
            return(false);
        }
Example #22
0
        private Matrix4x4 GetBoneMatrix(AnimationSet anim, int frame)
        {
            if (null == anim)
            {
                return(Matrix4x4.identity);
            }
            Vector3 position = Vector3.zero;
            Curve   posx     = anim.GetCurve(AnimatableProperty.PositionX);
            Curve   posy     = anim.GetCurve(AnimatableProperty.PositionY);
            Curve   posz     = anim.GetCurve(AnimatableProperty.PositionZ);

            if (null != posx && null != posy && null != posz)
            {
                if (posx.Evaluate(frame, out float px) && posy.Evaluate(frame, out float py) && posz.Evaluate(frame, out float pz))
                {
                    position = new Vector3(px, py, pz);
                }
            }
            Quaternion rotation = Quaternion.identity;
            Curve      rotx     = anim.GetCurve(AnimatableProperty.RotationX);
            Curve      roty     = anim.GetCurve(AnimatableProperty.RotationY);
            Curve      rotz     = anim.GetCurve(AnimatableProperty.RotationZ);

            if (null != posx && null != roty && null != rotz)
            {
                if (rotx.Evaluate(frame, out float rx) && roty.Evaluate(frame, out float ry) && rotz.Evaluate(frame, out float rz))
                {
                    rotation = Quaternion.Euler(rx, ry, rz);
                }
            }
            Vector3 scale  = Vector3.one;
            Curve   scalex = anim.GetCurve(AnimatableProperty.ScaleX);
            Curve   scaley = anim.GetCurve(AnimatableProperty.ScaleY);
            Curve   scalez = anim.GetCurve(AnimatableProperty.ScaleZ);

            if (null != scalex && null != scaley && null != scalez)
            {
                if (scalex.Evaluate(frame, out float sx) && scaley.Evaluate(frame, out float sy) && scalez.Evaluate(frame, out float sz))
                {
                    scale = new Vector3(sx, sy, sz);
                }
            }
            return(Matrix4x4.TRS(position, rotation, scale));
        }
Example #23
0
        public void RecursiveRemove(Transform target, int frame)
        {
            AnimationSet animationSet = GlobalState.Animation.GetObjectAnimation(target.gameObject);

            if (null != animationSet)
            {
                foreach (Curve curve in animationSet.curves.Values)
                {
                    if (curve.HasKeyAt(frame))
                    {
                        new CommandRemoveKeyframe(target.gameObject, curve.property, frame, false).Submit();
                    }
                }
            }
            foreach (Transform child in target)
            {
                RecursiveRemove(child, frame);
            }
        }
Example #24
0
            public Node(GameObject targetObject, int frame, Transform parentNode, Matrix4x4 parentMatrix, float scale = 1f)
            {
                Target          = targetObject;
                Frame           = frame;
                ObjectAnimation = GlobalState.Animation.GetObjectAnimation(Target);
                Childrens       = new List <Node>();
                if (null == ObjectAnimation)
                {
                    return;
                }
                Matrix4x4 objectMatrix = parentMatrix * ObjectAnimation.GetTRSMatrix(frame);

                Maths.DecomposeMatrix(objectMatrix, out worldPosition, out Quaternion objectRotation, out Vector3 objectScale);

                if (Target.TryGetComponent <RigGoalController>(out RigGoalController controller) && controller.IsGoal)
                {
                    Sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                    controller.CheckAnimations();
                }
Example #25
0
        public static KeyFrame GetKey(AnimationSet set, int frame)
        {
            KeyProperty px = GetProperty(set, AnimatableProperty.PositionX, frame);
            KeyProperty py = GetProperty(set, AnimatableProperty.PositionY, frame);
            KeyProperty pz = GetProperty(set, AnimatableProperty.PositionZ, frame);
            KeyProperty rx = GetProperty(set, AnimatableProperty.RotationX, frame);
            KeyProperty ry = GetProperty(set, AnimatableProperty.RotationY, frame);
            KeyProperty rz = GetProperty(set, AnimatableProperty.RotationZ, frame);

            return(new KeyFrame()
            {
                frame = frame,
                posX = px,
                posY = py,
                posZ = pz,
                rotX = rx,
                rotY = ry,
                rotZ = rz
            });
        }
Example #26
0
        public void RemoveKeyframe(GameObject gobject, AnimatableProperty property, int frame)
        {
            AnimationSet animationSet = GetObjectAnimation(gobject);

            if (null == animationSet)
            {
                return;
            }
            Curve curve = animationSet.GetCurve(property);

            curve.RemoveKey(frame);

            RemoveEmptyAnimationSet(gobject);

            if (!IsAnimating())
            {
                EvaluateAnimations();
            }
            onChangeCurve.Invoke(gobject, property);
        }
Example #27
0
        public CommandRemoveKeyframe(GameObject obj, AnimatableProperty property, int frame)
        {
            gObject       = obj;
            this.property = property;

            AnimationSet animationSet = GlobalState.Animation.GetObjectAnimation(obj);

            if (null == animationSet)
            {
                return;
            }

            Curve curve = animationSet.GetCurve(property);

            if (null == curve)
            {
                return;
            }

            curve.TryFindKey(frame, out oldAnimationKey);
        }
Example #28
0
        public void HoverLine(GameObject curveObject, Vector3 point)
        {
            LineRenderer line         = curveObject.GetComponent <LineRenderer>();
            GameObject   gobject      = CurveManager.GetObjectFromCurve(curveObject);
            AnimationSet animationSet = GlobalState.Animation.GetObjectAnimation(gobject);

            int frame = GetFrameFromPoint(line, point, animationSet);

            DrawCurveGhost(gobject, frame);
            if (CurveMode == CurveEditMode.Zone || CurveMode == CurveEditMode.Segment)
            {
                DrawZone(line, frame - zoneSize, frame + zoneSize);
            }
            if (CurveMode == CurveEditMode.Tangents)
            {
                Curve curve = animationSet.GetCurve(AnimatableProperty.PositionX);
                int   prev  = curve.GetPreviousKeyFrame(frame);
                int   next  = curve.GetNextKeyFrame(frame);
                DrawZone(line, prev, next);
            }
        }
Example #29
0
        public bool Apply()
        {
            double[] new_theta = new double[paramCount];
            for (int i = 0; i < paramCount; i++)
            {
                new_theta[i] = delta_theta[i] + theta[i];
            }
            for (int l = 0; l < animationCount; l++)
            {
                AnimationSet currentAnim = animationList[l];
                for (int i = 0; i < 3; i++)
                {
                    AnimatableProperty property = (AnimatableProperty)i + 3;
                    Curve curve = currentAnim.GetCurve(property);

                    for (int k = 0; k < keyCount; k++)
                    {
                        curve.GetKeyIndex(requiredKeyframe[k], out int index);
                        Vector2 inTangent  = new Vector2((float)new_theta[12 * keyCount * l + 4 * (i * keyCount + k) + 0], (float)new_theta[12 * keyCount * l + 4 * (i * keyCount + k) + 1]);
                        Vector2 outTangent = new Vector2((float)new_theta[12 * keyCount * l + 4 * (i * keyCount + k) + 2], (float)new_theta[12 * keyCount * l + 4 * (i * keyCount + k) + 3]);
                        ModifyTangents(curve, index, inTangent, outTangent);
                    }
                }
            }
            for (int i = 3; i < 6; i++)
            {
                Curve curve = animationList[0].GetCurve((AnimatableProperty)i - 3);

                for (int k = 0; k < keyCount; k++)
                {
                    curve.GetKeyIndex(requiredKeyframe[k], out int index);
                    Vector2 inTangent  = new Vector2((float)new_theta[12 * keyCount * animationCount + 4 * ((i - 3) * keyCount + k) + 0], (float)new_theta[12 * keyCount * animationCount + 4 * ((i - 3) * keyCount + k) + 1]);
                    Vector2 outTangent = new Vector2((float)new_theta[12 * keyCount * animationCount + 4 * ((i - 3) * keyCount + k) + 2], (float)new_theta[12 * keyCount * animationCount + 4 * ((i - 3) * keyCount + k) + 3]);
                    ModifyTangents(curve, index, inTangent, outTangent);
                }
            }

            return(true);
        }
Example #30
0
        private static KeyProperty GetProperty(AnimationSet set, AnimatableProperty property, int frame)
        {
            Curve curve = set.GetCurve(property);

            if (curve.GetKeyIndex(frame, out int index))
            {
                KeyProperty key = new KeyProperty()
                {
                    value   = curve.keys[index].value,
                    inTanX  = curve.keys[index].inTangent.x,
                    inTanY  = curve.keys[index].inTangent.y,
                    outTanX = curve.keys[index].outTangent.x,
                    outTanY = curve.keys[index].outTangent.y
                };
                return(key);
            }
            else
            {
                KeyProperty key = new KeyProperty();
                return(key);
            }
        }