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;
            }

            //TODO: figure out how to preview frame during edit
            if (Application.isPlaying)
            {
                if (!mIsInit)
                {
                    Init(target);
                }

                // if before or equal to first frame, or is the only frame
                MaterialKey firstKey = keys[0] as MaterialKey;
                if (firstKey.endFrame == -1 || (frame <= (float)firstKey.frame && !firstKey.canTween))
                {
                    firstKey.ApplyValue(_propertyType, _property, mPropId, materialInstance);
                    return;
                }

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

                    if (frame >= (float)key.endFrame && keyNext != null && (!keyNext.canTween || keyNext.endFrame != -1))
                    {
                        continue;
                    }
                    // if no ease
                    if (!key.canTween || keyNext == null)
                    {
                        key.ApplyValue(_propertyType, _property, mPropId, materialInstance);
                        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((Ease)key.easeType);
                        t = ease(framePositionInAction, key.getNumberOfFrames(frameRate), key.amplitude, key.period);
                    }

                    MaterialKey.ApplyValueLerp(_propertyType, _property, mPropId, materialInstance, key, keyNext, t);
                    return;
                }
            }
        }
Exemplo n.º 2
0
        public static void ApplyValueLerp(MaterialTrack.ValueType valueType, string prop, int propId, Material mat, MaterialKey fromKey, MaterialKey toKey, float t)
        {
            switch (valueType)
            {
            case MaterialTrack.ValueType.Float:
            case MaterialTrack.ValueType.Range:
                mat.SetFloat(propId, Mathf.Lerp(fromKey.val, toKey.val, t));
                break;

            case MaterialTrack.ValueType.Vector:
                mat.SetVector(propId, Vector4.Lerp(fromKey.vector, toKey.vector, t));
                break;

            case MaterialTrack.ValueType.Color:
                mat.SetColor(propId, Color.Lerp(fromKey.color, toKey.color, t));
                break;

            case MaterialTrack.ValueType.TexOfs:
                mat.SetTextureOffset(prop, Vector2.Lerp(fromKey.texOfs, toKey.texOfs, t));
                break;

            case MaterialTrack.ValueType.TexScale:
                mat.SetTextureScale(prop, Vector2.Lerp(fromKey.texScale, toKey.texScale, t));
                break;
            }
        }
Exemplo n.º 3
0
        public MaterialKey addKey(ITarget target, int _frame)
        {
            Material mat = GetMaterial(target);
            //mat.get
            //

            MaterialKey k = null, prevKey = null;

            foreach (MaterialKey key in keys)
            {
                // if key exists on frame, update key
                if (key.frame == _frame)
                {
                    k = key;
                }
                else if (key.frame < _frame)
                {
                    prevKey = key;
                }
            }

            if (k == null)
            {
                k       = new MaterialKey();
                k.frame = _frame;

                //copy previous frame tween settings
                if (prevKey != null)
                {
                    k.interp    = prevKey.interp;
                    k.easeType  = prevKey.easeType;
                    k.easeCurve = prevKey.easeCurve;
                }
                else
                {
                    k.interp = canTween ? Key.Interpolation.Linear : Key.Interpolation.None; //default
                }
                // add a new key
                keys.Add(k);
            }

            //set value
            if (!string.IsNullOrEmpty(property))
            {
                switch (_propertyType)
                {
                case ValueType.Float:
                case ValueType.Range:
                    k.val = mat.GetFloat(property);
                    break;

                case ValueType.Vector:
                    k.vector = mat.GetVector(property);
                    break;

                case ValueType.Color:
                    k.color = mat.GetColor(property);
                    break;

                case ValueType.TexEnv:
                    k.texture = mat.GetTexture(property);
                    break;

                case ValueType.TexOfs:
                    k.texOfs = mat.GetTextureOffset(property);
                    break;

                case ValueType.TexScale:
                    k.texScale = mat.GetTextureScale(property);
                    break;
                }
            }
            else
            {
                Debug.LogWarning("Property is not set in: " + name);
            }

            updateCache(target);

            return(k);
        }