Esempio n. 1
0
        public void CalculateGradient(bool cycled = false)
        {
            if (_keys.Count == 0)
            {
                return;
            }

            if (cycled)
            {
                _keys.Add(_keys[0]);
            }

            if (cycled)
            {
                var tmp = SplineUtils.Sub(_keys[1].Key, _keys[_keys.Count - 2].Key);
                _keys[0] = new KeyValuePair <Vector3, Vector3>(_keys[0].Key, SplineUtils.Mul(tmp, 0.5f));
                _keys[_keys.Count - 1] = new KeyValuePair <Vector3, Vector3>(_keys[_keys.Count - 1].Key, _keys[0].Value);
            }
            else
            {
                if (_keys.Count > 1)
                {
                    var tmp1 = SplineUtils.Sub(_keys[1].Key, _keys[0].Key);
                    var tmp2 = SplineUtils.Sub(_keys[_keys.Count - 1].Key, _keys[_keys.Count - 2].Key);
                    _keys[0] = new KeyValuePair <Vector3, Vector3>(_keys[0].Key, tmp1);
                    _keys[_keys.Count - 1] = new KeyValuePair <Vector3, Vector3>(_keys[_keys.Count - 1].Key, tmp2);
                }
            }

            for (int i = 1; i < _keys.Count - 1; i++)
            {
                var tmp = SplineUtils.Sub(_keys[i + 1].Key, _keys[i - 1].Key);
                _keys[i] = new KeyValuePair <Vector3, Vector3>(_keys[i].Key, SplineUtils.Mul(tmp, 0.5f));
            }
        }
Esempio n. 2
0
        public void CalculateGradient()
        {
            _keys.Sort((KeyFrame a, KeyFrame b) =>
            {
                return(a.time.CompareTo(b.time));
            });

            int e = _keys.Count - 1;

            var tmp_key0 = _keys[0];
            var tmp_keye = _keys[e];

            var tmp1 = SplineUtils.Sub(_keys[1].value, _keys[0].value);
            var tmp2 = SplineUtils.Sub(_keys[e].value, _keys[e - 1].value);

            tmp_key0.ts = SplineUtils.Mul(tmp1, 0.5f * (_keys[1].time - _keys[0].time));
            tmp_keye.td = SplineUtils.Mul(tmp2, 0.5f * (_keys[e].time - _keys[e - 1].time));

            _keys[0] = tmp_key0;
            _keys[e] = tmp_keye;

            for (int i = 1; i < e; i++)
            {
                var tmp_keyi = _keys[i];
                var tmp3     = SplineUtils.Sub(_keys[i + 1].value, _keys[i - 1].value);
                tmp_keyi.ts = tmp_keyi.td = SplineUtils.Mul(tmp3, 0.5f);

                float inv_dd = 1.0f / (_keys[i + 1].time - _keys[i - 1].time);

                float k1 = 2.0f * (_keys[i].time - _keys[i - 1].time) * inv_dd;
                float k2 = 2.0f * (_keys[i + 1].time - _keys[i].time) * inv_dd;

                tmp_keyi.td = SplineUtils.Mul(tmp_keyi.td, k1);
                tmp_keyi.ts = SplineUtils.Mul(tmp_keyi.ts, k2);

                _keys[i] = tmp_keyi;
            }
        }