Ejemplo n.º 1
0
        public Vector3 getPositionAtFrame(Transform t, int frame, int frameRate, bool forceWorld)
        {
            int keyCount = keys.Count;

            if (keyCount <= 0)
            {
                return(GetPosition(t));
            }

            TranslationKey firstKey = keys[0] as TranslationKey;

            //check if behind first key
            if (frame <= firstKey.frame && (!firstKey.canTween || firstKey.path.Length == 1))
            {
                return(convertPosition(t, firstKey.position, forceWorld));
            }

            TranslationKey lastKey = keyCount == 1 ? firstKey : keys[keyCount - 1] as TranslationKey;

            //check if past last key
            if (frame >= lastKey.endFrame && !lastKey.canTween)
            {
                return(convertPosition(t, lastKey.position, forceWorld));
            }

            //check in-between
            for (int i = 0; i < keyCount; i++)
            {
                TranslationKey key     = keys[i] as TranslationKey;
                TranslationKey keyNext = i < keyCount - 1 ? keys[i + 1] as TranslationKey : null;

                if (frame >= key.endFrame && keyNext != null && (!keyNext.canTween || keyNext.path.Length > 1))
                {
                    continue;
                }

                if (!key.canTween || key.path.Length == 1)
                {
                    return(convertPosition(t, key.position, forceWorld));
                }
                else if (key.path.Length == 0)
                {
                    continue;
                }

                float fNumFrames = (float)key.getNumberOfFrames(frameRate);

                float _value;

                float framePositionInPath = Mathf.Clamp(frame - (float)key.frame, 0f, fNumFrames);

                if (key.hasCustomEase())
                {
                    _value = Utility.EaseCustom(0.0f, 1.0f, framePositionInPath / fNumFrames, key.easeCurve);
                }
                else
                {
                    var ease = Utility.GetEasingFunction((Ease)key.easeType);
                    _value = ease(framePositionInPath, fNumFrames, key.amplitude, key.period);
                    if (float.IsNaN(_value)) //this really shouldn't happen...
                    {
                        break;
                    }
                }

                return(convertPosition(t, key.GetPoint(Mathf.Clamp01(_value)), forceWorld));
            }

            Debug.LogError("Animator: Could not get " + t.name + " position at frame '" + frame + "'");
            return(GetPosition(t));
        }
Ejemplo n.º 2
0
        public Vector3 getPositionAtFrame(Transform t, float frame, int frameRate, bool forceWorld)
        {
            int keyCount = keys.Count;

            if (keyCount <= 0)
            {
                return(GetPosition(t));
            }

            int iFrame = Mathf.RoundToInt(frame);

            TranslationKey firstKey = keys[0] as TranslationKey;

            //check if only key or behind first key
            if (keyCount == 1 || iFrame <= firstKey.frame)
            {
                return(convertPosition(t, firstKey.position, forceWorld));
            }

            //check in-between
            for (int i = 0; i < keyCount; i++)
            {
                TranslationKey key = keys[i] as TranslationKey;

                if (key.endFrame == -1) //invalid
                {
                    continue;
                }

                //end of last path in track?
                if (iFrame >= key.endFrame)
                {
                    if (key.interp == Key.Interpolation.None)
                    {
                        if (i + 1 == keyCount)
                        {
                            return(convertPosition(t, key.position, forceWorld));
                        }
                    }
                    else if (key.interp == Key.Interpolation.Linear || key.path == null)
                    {
                        if (i + 1 == keyCount - 1)
                        {
                            var pt = ((TranslationKey)keys[i + 1]).position;
                            return(convertPosition(t, pt, forceWorld));
                        }
                    }
                    else if (key.interp == Key.Interpolation.Curve)
                    {
                        if (i + key.keyCount == keyCount) //end of last path in track?
                        {
                            return(convertPosition(t, ((TranslationKey)keys[i + key.keyCount - 1]).position, forceWorld));
                        }
                    }

                    continue;
                }

                if (key.interp == Key.Interpolation.None)
                {
                    return(convertPosition(t, key.position, forceWorld));
                }
                else if (key.interp == Key.Interpolation.Linear || key.path == null)
                {
                    var keyNext = keys[i + 1] as TranslationKey;

                    float numFrames = (float)key.getNumberOfFrames(frameRate);

                    float framePositionInAction = Mathf.Clamp(frame - (float)key.frame, 0f, numFrames);

                    var start = key.position;
                    var end   = keyNext.position;

                    if (key.hasCustomEase())
                    {
                        return(convertPosition(t, Vector3.Lerp(start, end, Utility.EaseCustom(0.0f, 1.0f, framePositionInAction / numFrames, key.easeCurve)), forceWorld));
                    }
                    else
                    {
                        var ease = Utility.GetEasingFunction((Ease)key.easeType);
                        return(convertPosition(t, Vector3.Lerp(start, end, ease(framePositionInAction, numFrames, key.amplitude, key.period)), forceWorld));
                    }
                }
                else
                {
                    float _value = Mathf.Clamp01((frame - key.frame) / key.getNumberOfFrames(frameRate));

                    var pt = key.GetPoint(_value);

                    return(convertPosition(t, pt, forceWorld));
                }
            }

            return(GetPosition(t)); //last key is impartial tween
        }
Ejemplo n.º 3
0
        // preview a frame in the scene view
        public override void previewFrame(ITarget itarget, float frame, int frameRate, bool play, float playSpeed)
        {
            Transform t = GetTarget(itarget) as Transform;

            if (!t)
            {
                return;
            }

            int keyCount = keys.Count;

            if (keys == null || keyCount <= 0)
            {
                return;
            }

            int iFrame = Mathf.RoundToInt(frame);

            TranslationKey firstKey = keys[0] as TranslationKey;

            //check if behind first key
            if (iFrame <= firstKey.frame && (!firstKey.canTween || firstKey.path.Length == 1))
            {
                SetPosition(t, firstKey.position);
                return;
            }

            TranslationKey lastKey = keyCount == 1 ? firstKey : keys[keyCount - 1] as TranslationKey;

            //check if past last key
            if (iFrame >= lastKey.endFrame)
            {
                SetPosition(t, lastKey.position);
                return;
            }

            //check in-between
            for (int i = 0; i < keyCount; i++)
            {
                TranslationKey key = keys[i] as TranslationKey;

                if (key.path == null)
                {
                    continue;
                }

                if (iFrame >= key.endFrame && i < keyCount - 1)
                {
                    continue;
                }

                if ((!key.canTween || key.path.Length <= 1))
                {
                    SetPosition(t, key.position);
                    return;
                }

                float fNumFrames = (float)key.getNumberOfFrames(frameRate);

                float _value;

                float framePositionInPath = Mathf.Clamp(frame - (float)key.frame, 0f, fNumFrames);

                if (key.hasCustomEase())
                {
                    _value = Utility.EaseCustom(0.0f, 1.0f, framePositionInPath / fNumFrames, key.easeCurve);
                }
                else
                {
                    var ease = Utility.GetEasingFunction((Ease)key.easeType);
                    _value = ease(framePositionInPath, fNumFrames, key.amplitude, key.period);
                    if (float.IsNaN(_value)) //this really shouldn't happen...
                    {
                        return;
                    }
                }

                SetPosition(t, key.GetPoint(Mathf.Clamp01(_value)));

                return;
            }
        }
Ejemplo n.º 4
0
        // preview a frame in the scene view
        public override void previewFrame(ITarget itarget, float frame, int frameRate, bool play, float playSpeed)
        {
            Transform t = GetTarget(itarget) as Transform;

            if (!t)
            {
                return;
            }

            int keyCount = keys.Count;

            if (keyCount <= 0)
            {
                return;
            }

            int iFrame = Mathf.RoundToInt(frame);

            TranslationKey firstKey = keys[0] as TranslationKey;

            //check if only key or behind first key
            if (keyCount == 1 || iFrame < firstKey.frame)
            {
                t.localPosition = convertPosition(t, firstKey.position, false);
                return;
            }
            else if (iFrame == firstKey.frame)
            {
                if (firstKey.interp == Key.Interpolation.Linear || firstKey.path == null)  //apply orientation
                {
                    t.localPosition = convertPosition(t, firstKey.position, false);

                    if (firstKey.orientMode != OrientMode.None)
                    {
                        var nextPt = ((TranslationKey)keys[1]).position;
                        if (t.parent)
                        {
                            nextPt = t.parent.TransformPoint(nextPt);
                        }

                        t.rotation = firstKey.GetOrientation(t, nextPt);
                    }
                }
                else if (firstKey.interp == Key.Interpolation.Curve)  //apply orientation
                {
                    t.localPosition = convertPosition(t, firstKey.position, false);

                    if (firstKey.orientMode != OrientMode.None)
                    {
                        t.rotation = firstKey.GetOrientation(t, 0f);
                    }
                }
                else
                {
                    t.localPosition = convertPosition(t, firstKey.position, false);
                }

                return;
            }

            //check in-between
            for (int i = 0; i < keyCount; i++)
            {
                TranslationKey key = keys[i] as TranslationKey;

                if (key.endFrame == -1) //invalid
                {
                    continue;
                }

                //end of last path in track?
                if (iFrame >= key.endFrame)
                {
                    if (key.interp == Key.Interpolation.None)
                    {
                        if (i + 1 == keyCount)
                        {
                            t.localPosition = convertPosition(t, key.position, false);
                            return;
                        }
                    }
                    else if (key.interp == Key.Interpolation.Linear || key.path == null)
                    {
                        if (i + 1 == keyCount - 1)
                        {
                            var pt = ((TranslationKey)keys[i + 1]).position;

                            t.localPosition = convertPosition(t, pt, false);

                            if (key.orientMode != OrientMode.None && iFrame == key.endFrame) //only apply rotation if we are at end of frame
                            {
                                Vector3 ptW, prevPtW;
                                if (t.parent)
                                {
                                    ptW     = t.parent.TransformPoint(pt);
                                    prevPtW = t.parent.TransformPoint(key.position);
                                }
                                else
                                {
                                    ptW     = pt;
                                    prevPtW = key.position;
                                }
                                var dir = (ptW - prevPtW).normalized;

                                t.rotation = key.GetOrientation(t, ptW + dir);
                            }

                            return;
                        }
                    }
                    else if (key.interp == Key.Interpolation.Curve)
                    {
                        if (i + key.keyCount == keyCount) //end of last path in track?
                        {
                            t.localPosition = convertPosition(t, ((TranslationKey)keys[i + key.keyCount - 1]).position, false);

                            if (key.orientMode != OrientMode.None && iFrame == key.endFrame) //only apply rotation if we are at end of frame
                            {
                                t.rotation = key.GetOrientation(t, 1f);
                            }

                            return;
                        }
                    }

                    continue;
                }

                if (key.interp == Key.Interpolation.None)
                {
                    t.localPosition = convertPosition(t, key.position, false);
                }
                else if (key.interp == Key.Interpolation.Linear || key.path == null)
                {
                    var keyNext = keys[i + 1] as TranslationKey;

                    float numFrames = (float)key.getNumberOfFrames(frameRate);

                    float framePositionInAction = Mathf.Clamp(frame - (float)key.frame, 0f, numFrames);

                    var start = key.position;
                    var end   = keyNext.position;

                    if (key.hasCustomEase())
                    {
                        t.localPosition = convertPosition(t, Vector3.Lerp(start, end, Utility.EaseCustom(0.0f, 1.0f, framePositionInAction / numFrames, key.easeCurve)), false);
                    }
                    else
                    {
                        var ease = Utility.GetEasingFunction((Ease)key.easeType);
                        t.localPosition = convertPosition(t, Vector3.Lerp(start, end, ease(framePositionInAction, numFrames, key.amplitude, key.period)), false);
                    }

                    if (key.orientMode != OrientMode.None)
                    {
                        t.rotation = key.GetOrientation(t, t.parent ? t.parent.TransformPoint(end) : end);
                    }
                }
                else
                {
                    float _value = Mathf.Clamp01((frame - key.frame) / key.getNumberOfFrames(frameRate));

                    var pt = key.GetPoint(_value);
                    t.localPosition = convertPosition(t, pt, false);

                    if (key.orientMode != OrientMode.None)
                    {
                        t.rotation = key.GetOrientation(t, _value);
                    }
                }

                return;
            }

            //var lpos = getPositionAtFrame(t, frame, frameRate, false);

            //t.localPosition = lpos;
        }