Exemplo n.º 1
0
        public override void previewFrame(ITarget target, float frame, int frameRate, bool play, float playSpeed)
        {
            if (keys == null || keys.Count <= 0)
            {
                return;
            }

            GameObject go   = GetTarget(target) as GameObject;
            Component  comp = GetTargetComp(target);

            if (!comp || !go)
            {
                return;
            }

            if (!isCached)
            {
                RefreshData(comp);
            }

            int keyCount = keys.Count;

            if (keyCount <= 0)
            {
                return;
            }

            int iFrame = Mathf.RoundToInt(frame);

            var firstKey = keys[0] as PropertyKey;

            //check if only key or behind first key
            if (keyCount == 1 || iFrame <= firstKey.frame)
            {
                //go.rotation = (cache[0] as AMPropertyAction).getStartQuaternion();
                setComponentValueFromCachedInfo(comp, firstKey.getValue(valueType));
                if (comp is Transform)
                {
                    refreshTransform(go);
                }
                return;
            }

            for (int i = 0; i < keys.Count; i++)
            {
                var key = keys[i] as PropertyKey;

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

                //end of last path in track?
                if (iFrame >= key.endFrame)
                {
                    PropertyKey toKey = null;

                    if (key.interp == Key.Interpolation.None)
                    {
                        if (i + 1 == keyCount)
                        {
                            toKey = key;
                        }
                    }
                    else if (key.interp == Key.Interpolation.Linear || key.path == null)
                    {
                        if (i + 1 == keyCount - 1)
                        {
                            toKey = (PropertyKey)keys[i + 1];
                        }
                    }
                    else if (key.interp == Key.Interpolation.Curve)
                    {
                        if (i + key.keyCount == keyCount) //end of last path in track?
                        {
                            toKey = (PropertyKey)keys[i + key.keyCount - 1];
                        }
                    }

                    if (toKey != null)
                    {
                        setComponentValueFromCachedInfo(comp, toKey.getValue(valueType));
                        if (comp is Transform)
                        {
                            refreshTransform(go);
                        }

                        return;
                    }

                    continue;
                }

                if (key.interp == Key.Interpolation.None)
                {
                    setComponentValueFromCachedInfo(comp, key.getValue(valueType));
                    if (comp is Transform)
                    {
                        refreshTransform(go);
                    }
                }
                else if (key.interp == Key.Interpolation.Linear || key.path == null)
                {
                    PropertyKey keyNext = i + 1 < keys.Count ? keys[i + 1] as PropertyKey : null;

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

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

                    float t;

                    if (key.hasCustomEase())
                    {
                        t = Utility.EaseCustom(0.0f, 1.0f, framePositionInAction / key.getNumberOfFrames(frameRate), key.easeCurve);
                    }
                    else
                    {
                        var ease = Utility.GetEasingFunction(key.easeType);
                        t = ease(framePositionInAction, key.getNumberOfFrames(frameRate), key.amplitude, key.period);
                    }

                    //qCurrent.x = ease(qStart.x,qEnd.x,percentage);
                    switch ((ValueType)valueType)
                    {
                    case ValueType.Integer:
                        setComponentValueFromCachedInfo(comp, keyNext != null ? Mathf.RoundToInt(Mathf.Lerp(Convert.ToSingle(key.val), Convert.ToSingle(keyNext.val), t)) : Convert.ToInt32(key.val));
                        break;

                    case ValueType.Long:
                        setComponentValueFromCachedInfo(comp, keyNext != null ? (long)Mathf.RoundToInt(Mathf.Lerp(Convert.ToSingle(key.val), Convert.ToSingle(keyNext.val), t)) : Convert.ToInt64(key.val));
                        break;

                    case ValueType.Float:
                        setComponentValueFromCachedInfo(comp, keyNext != null ? Mathf.Lerp(Convert.ToSingle(key.val), Convert.ToSingle(keyNext.val), t) : Convert.ToSingle(key.val));
                        break;

                    case ValueType.Double:
                        setComponentValueFromCachedInfo(comp, keyNext != null ? key.val + ((double)t) * (keyNext.val - key.val) : key.val);
                        break;

                    case ValueType.Vector2:
                        setComponentValueFromCachedInfo(comp, keyNext != null ? Vector2.Lerp(key.vect2, keyNext.vect2, t) : key.vect2);
                        break;

                    case ValueType.Vector3:
                        setComponentValueFromCachedInfo(comp, keyNext != null ? Vector3.Lerp(key.vect3, keyNext.vect3, t) : key.vect3);
                        break;

                    case ValueType.Color:
                        setComponentValueFromCachedInfo(comp, keyNext != null ? Color.Lerp(key.color, keyNext.color, t) : key.color);
                        break;

                    case ValueType.Rect:
                        if (keyNext != null)
                        {
                            Rect vStartRect   = key.rect;
                            Rect vEndRect     = keyNext.rect;
                            Rect vCurrentRect = new Rect();
                            vCurrentRect.x      = Mathf.Lerp(vStartRect.x, vEndRect.x, t);
                            vCurrentRect.y      = Mathf.Lerp(vStartRect.y, vEndRect.y, t);
                            vCurrentRect.width  = Mathf.Lerp(vStartRect.width, vEndRect.width, t);
                            vCurrentRect.height = Mathf.Lerp(vStartRect.height, vEndRect.height, t);
                            setComponentValueFromCachedInfo(comp, vCurrentRect);
                        }
                        else
                        {
                            setComponentValueFromCachedInfo(comp, key.rect);
                        }
                        break;

                    case ValueType.Vector4:
                        setComponentValueFromCachedInfo(comp, keyNext != null ? Vector4.Lerp(key.vect4, keyNext.vect4, t) : key.vect4);
                        break;

                    case ValueType.Quaternion:
                        setComponentValueFromCachedInfo(comp, keyNext != null ? Quaternion.Slerp(key.quat, keyNext.quat, t) : key.quat);
                        break;

                    default:
                        Debug.LogError("Animator: Invalid ValueType " + valueType.ToString());
                        break;
                    }
                    if (comp is Transform)
                    {
                        refreshTransform(go);
                    }
                }
                else if (key.interp == Key.Interpolation.Curve)
                {
                    float t = Mathf.Clamp01((frame - key.frame) / key.getNumberOfFrames(frameRate));

                    var val = key.GetValueFromPath(valueType, t);
                    if (val == null)
                    {
                        Debug.LogError("Animator: Invalid ValueType " + valueType.ToString());
                        return;
                    }

                    setComponentValueFromCachedInfo(comp, val);

                    if (comp is Transform)
                    {
                        refreshTransform(go);
                    }
                }

                return;
            }
        }
Exemplo n.º 2
0
        public override void previewFrame(ITarget target, float frame, int frameRate, bool play, float playSpeed)
        {
            if (keys == null || keys.Count <= 0)
            {
                return;
            }

            GameObject go   = GetTarget(target) as GameObject;
            Component  comp = GetTargetComp(target);

            if (!comp || !go)
            {
                return;
            }

            if (!isCached)
            {
                RefreshData(comp);
            }

            // if before or equal to first frame, or is the only frame
            PropertyKey firstKey = keys[0] as PropertyKey;

            if (firstKey.endFrame == -1 || (frame <= (float)firstKey.frame && !firstKey.canTween))
            {
                //go.rotation = (cache[0] as AMPropertyAction).getStartQuaternion();
                setComponentValueFromCachedInfo(comp, firstKey.getValue(valueType));
                refreshTransform(go);
                return;
            }

            // if lies on property action
            for (int i = 0; i < keys.Count; i++)
            {
                PropertyKey key     = keys[i] as PropertyKey;
                PropertyKey keyNext = i + 1 < keys.Count ? keys[i + 1] as PropertyKey : null;

                if (frame >= (float)key.endFrame && keyNext != null && (!keyNext.canTween || keyNext.endFrame != -1))
                {
                    continue;
                }
                // if no ease
                if (!key.canTween || keyNext == null)
                {
                    setComponentValueFromCachedInfo(comp, key.getValue(valueType));
                    refreshTransform(go);
                    return;
                }
                // else find value using easing function

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

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

                float t;

                if (key.hasCustomEase())
                {
                    t = Utility.EaseCustom(0.0f, 1.0f, framePositionInAction / key.getNumberOfFrames(frameRate), key.easeCurve);
                }
                else
                {
                    var ease = Utility.GetEasingFunction(key.easeType);
                    t = ease(framePositionInAction, key.getNumberOfFrames(frameRate), key.amplitude, key.period);
                }

                //qCurrent.x = ease(qStart.x,qEnd.x,percentage);
                switch ((ValueType)valueType)
                {
                case ValueType.Integer:
                    setComponentValueFromCachedInfo(comp, keyNext != null ? Mathf.RoundToInt(Mathf.Lerp(Convert.ToSingle(key.val), Convert.ToSingle(keyNext.val), t)) : Convert.ToInt32(key.val));
                    break;

                case ValueType.Long:
                    setComponentValueFromCachedInfo(comp, keyNext != null ? (long)Mathf.RoundToInt(Mathf.Lerp(Convert.ToSingle(key.val), Convert.ToSingle(keyNext.val), t)) : Convert.ToInt64(key.val));
                    break;

                case ValueType.Float:
                    setComponentValueFromCachedInfo(comp, keyNext != null ? Mathf.Lerp(Convert.ToSingle(key.val), Convert.ToSingle(keyNext.val), t) : Convert.ToSingle(key.val));
                    break;

                case ValueType.Double:
                    setComponentValueFromCachedInfo(comp, keyNext != null ? key.val + ((double)t) * (keyNext.val - key.val) : key.val);
                    break;

                case ValueType.Vector2:
                    setComponentValueFromCachedInfo(comp, keyNext != null ? Vector2.Lerp(key.vect2, keyNext.vect2, t) : key.vect2);
                    break;

                case ValueType.Vector3:
                    setComponentValueFromCachedInfo(comp, keyNext != null ? Vector3.Lerp(key.vect3, keyNext.vect3, t) : key.vect3);
                    break;

                case ValueType.Color:
                    setComponentValueFromCachedInfo(comp, keyNext != null ? Color.Lerp(key.color, keyNext.color, t) : key.color);
                    break;

                case ValueType.Rect:
                    if (keyNext != null)
                    {
                        Rect vStartRect   = key.rect;
                        Rect vEndRect     = keyNext.rect;
                        Rect vCurrentRect = new Rect();
                        vCurrentRect.x      = Mathf.Lerp(vStartRect.x, vEndRect.x, t);
                        vCurrentRect.y      = Mathf.Lerp(vStartRect.y, vEndRect.y, t);
                        vCurrentRect.width  = Mathf.Lerp(vStartRect.width, vEndRect.width, t);
                        vCurrentRect.height = Mathf.Lerp(vStartRect.height, vEndRect.height, t);
                        setComponentValueFromCachedInfo(comp, vCurrentRect);
                    }
                    else
                    {
                        setComponentValueFromCachedInfo(comp, key.rect);
                    }
                    break;

                case ValueType.Vector4:
                    setComponentValueFromCachedInfo(comp, keyNext != null ? Vector4.Lerp(key.vect4, keyNext.vect4, t) : key.vect4);
                    break;

                case ValueType.Quaternion:
                    setComponentValueFromCachedInfo(comp, keyNext != null ? Quaternion.Slerp(key.quat, keyNext.quat, t) : key.quat);
                    break;

                default:
                    Debug.LogError("Animator: Invalid ValueType " + valueType.ToString());
                    break;
                }
                refreshTransform(go);
                return;
            }
        }