SafeDeltaDivide() public static method

public static SafeDeltaDivide ( float dy, float dx ) : float
dy float
dx float
return float
        public float CalculateSmoothTangent(int index, int component)
        {
            AnimationCurve curve = this.GetCurve(component);
            float          result;

            if (curve.length < 2)
            {
                result = 0f;
            }
            else if (index <= 0)
            {
                result = this.CalculateLinearTangent(curve[0], curve[1], component);
            }
            else if (index >= curve.length - 1)
            {
                result = this.CalculateLinearTangent(curve[curve.length - 1], curve[curve.length - 2], component);
            }
            else
            {
                float      time        = curve[index - 1].time;
                float      time2       = curve[index].time;
                float      time3       = curve[index + 1].time;
                Vector3    euler       = this.EvaluateEulerCurvesDirectly(time);
                Vector3    vector      = this.EvaluateEulerCurvesDirectly(time2);
                Vector3    euler2      = this.EvaluateEulerCurvesDirectly(time3);
                Quaternion quaternion  = Quaternion.Euler(euler);
                Quaternion quaternion2 = Quaternion.Euler(vector);
                Quaternion quaternion3 = Quaternion.Euler(euler2);
                if (quaternion.x * quaternion2.x + quaternion.y * quaternion2.y + quaternion.z * quaternion2.z + quaternion.w * quaternion2.w < 0f)
                {
                    quaternion = new Quaternion(-quaternion.x, -quaternion.y, -quaternion.z, -quaternion.w);
                }
                if (quaternion3.x * quaternion2.x + quaternion3.y * quaternion2.y + quaternion3.z * quaternion2.z + quaternion3.w * quaternion2.w < 0f)
                {
                    quaternion3 = new Quaternion(-quaternion3.x, -quaternion3.y, -quaternion3.z, -quaternion3.w);
                }
                Quaternion quaternion4 = default(Quaternion);
                float      dx          = time2 - time;
                float      dx2         = time3 - time2;
                for (int i = 0; i < 4; i++)
                {
                    float dy   = quaternion2[i] - quaternion[i];
                    float dy2  = quaternion3[i] - quaternion2[i];
                    float num  = QuaternionCurveTangentCalculation.SafeDeltaDivide(dy, dx);
                    float num2 = QuaternionCurveTangentCalculation.SafeDeltaDivide(dy2, dx2);
                    quaternion4[i] = 0.5f * num + 0.5f * num2;
                }
                float      num3 = Mathf.Abs(time3 - time) * 0.01f;
                Quaternion q    = new Quaternion(quaternion2.x - quaternion4.x * num3, quaternion2.y - quaternion4.y * num3, quaternion2.z - quaternion4.z * num3, quaternion2.w - quaternion4.w * num3);
                Quaternion q2   = new Quaternion(quaternion2.x + quaternion4.x * num3, quaternion2.y + quaternion4.y * num3, quaternion2.z + quaternion4.z * num3, quaternion2.w + quaternion4.w * num3);
                Vector3    eulerFromQuaternion  = QuaternionCurveTangentCalculation.GetEulerFromQuaternion(q, vector);
                Vector3    eulerFromQuaternion2 = QuaternionCurveTangentCalculation.GetEulerFromQuaternion(q2, vector);
                result = ((eulerFromQuaternion2 - eulerFromQuaternion) / (num3 * 2f))[component];
            }
            return(result);
        }
Esempio n. 2
0
        public float CalculateSmoothTangent(int index, int component)
        {
            AnimationCurve curve = this.GetCurve(component);

            if (curve.length < 2)
            {
                return(0.0f);
            }
            if (index <= 0)
            {
                return(this.CalculateLinearTangent(curve[0], curve[1], component));
            }
            if (index >= curve.length - 1)
            {
                return(this.CalculateLinearTangent(curve[curve.length - 1], curve[curve.length - 2], component));
            }
            float      time1 = curve[index - 1].time;
            float      time2 = curve[index].time;
            float      time3 = curve[index + 1].time;
            Vector3    eulerCurvesDirectly1 = this.EvaluateEulerCurvesDirectly(time1);
            Vector3    eulerCurvesDirectly2 = this.EvaluateEulerCurvesDirectly(time2);
            Vector3    eulerCurvesDirectly3 = this.EvaluateEulerCurvesDirectly(time3);
            Quaternion quaternion1          = Quaternion.Euler(eulerCurvesDirectly1);
            Quaternion quaternion2          = Quaternion.Euler(eulerCurvesDirectly2);
            Quaternion quaternion3          = Quaternion.Euler(eulerCurvesDirectly3);

            if ((double)quaternion1.x * (double)quaternion2.x + (double)quaternion1.y * (double)quaternion2.y + (double)quaternion1.z * (double)quaternion2.z + (double)quaternion1.w * (double)quaternion2.w < 0.0)
            {
                quaternion1 = new Quaternion(-quaternion1.x, -quaternion1.y, -quaternion1.z, -quaternion1.w);
            }
            if ((double)quaternion3.x * (double)quaternion2.x + (double)quaternion3.y * (double)quaternion2.y + (double)quaternion3.z * (double)quaternion2.z + (double)quaternion3.w * (double)quaternion2.w < 0.0)
            {
                quaternion3 = new Quaternion(-quaternion3.x, -quaternion3.y, -quaternion3.z, -quaternion3.w);
            }
            Quaternion quaternion4 = new Quaternion();
            float      dx1         = time2 - time1;
            float      dx2         = time3 - time2;

            for (int index1 = 0; index1 < 4; ++index1)
            {
                float dy1  = quaternion2[index1] - quaternion1[index1];
                float dy2  = quaternion3[index1] - quaternion2[index1];
                float num1 = QuaternionCurveTangentCalculation.SafeDeltaDivide(dy1, dx1);
                float num2 = QuaternionCurveTangentCalculation.SafeDeltaDivide(dy2, dx2);
                quaternion4[index1] = (float)(0.5 * (double)num1 + 0.5 * (double)num2);
            }
            float      num = Mathf.Abs(time3 - time1) * 0.01f;
            Quaternion q1  = new Quaternion(quaternion2.x - quaternion4.x * num, quaternion2.y - quaternion4.y * num, quaternion2.z - quaternion4.z * num, quaternion2.w - quaternion4.w * num);
            Quaternion q2  = new Quaternion(quaternion2.x + quaternion4.x * num, quaternion2.y + quaternion4.y * num, quaternion2.z + quaternion4.z * num, quaternion2.w + quaternion4.w * num);
            Vector3    eulerFromQuaternion = QuaternionCurveTangentCalculation.GetEulerFromQuaternion(q1, eulerCurvesDirectly2);

            return(((QuaternionCurveTangentCalculation.GetEulerFromQuaternion(q2, eulerCurvesDirectly2) - eulerFromQuaternion) / (num * 2f))[component]);
        }