Beispiel #1
0
        /**
         * Decomposes the current Matrix into :
         * - a scale vector3 passed as a reference to update,
         * - a rotation quaternion passed as a reference to update,
         * - a translation vector3 passed as a reference to update.
         * Returns the boolean `true`.
         */
        public bool decompose(BabylonVector3 scale, BabylonQuaternion rotation, BabylonVector3 translation)
        {
            translation.X = this.m[12];
            translation.Y = this.m[13];
            translation.Z = this.m[14];

            scale.X = (float)Math.Sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1] + this.m[2] * this.m[2]);
            scale.Y = (float)Math.Sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5] + this.m[6] * this.m[6]);
            scale.Z = (float)Math.Sqrt(this.m[8] * this.m[8] + this.m[9] * this.m[9] + this.m[10] * this.m[10]);

            if (this.determinant() <= 0)
            {
                scale.Y *= -1;
            }

            if (scale.X == 0 || scale.Y == 0 || scale.Z == 0)
            {
                rotation.X = 0;
                rotation.Y = 0;
                rotation.Z = 0;
                rotation.W = 1;
                return(false);
            }

            var matrix = new BabylonMatrix();

            BabylonMatrix.FromValuesToRef(
                this.m[0] / scale.X, this.m[1] / scale.X, this.m[2] / scale.X, 0,
                this.m[4] / scale.Y, this.m[5] / scale.Y, this.m[6] / scale.Y, 0,
                this.m[8] / scale.Z, this.m[9] / scale.Z, this.m[10] / scale.Z, 0,
                0, 0, 0, 1, matrix);

            BabylonQuaternion.FromRotationMatrixToRef(matrix, rotation);

            return(true);
        }
Beispiel #2
0
        public static float[] Interpolate(BabylonAnimation animation, BabylonAnimationKey fromKey, BabylonAnimationKey toKey, float frame)
        {
            switch (animation.property)
            {
            case "_matrix":
                var fromMatrix = new BabylonMatrix();
                fromMatrix.m = new List <float>(fromKey.values).ToArray();
                var toMatrix = new BabylonMatrix();
                toMatrix.m = new List <float>(toKey.values).ToArray();
                var fromPosition = new BabylonVector3();
                var fromRotation = new BabylonQuaternion();
                var fromScaling  = new BabylonVector3();
                var toPosition   = new BabylonVector3();
                var toRotation   = new BabylonQuaternion();
                var toScaling    = new BabylonVector3();

                fromMatrix.decompose(fromScaling, fromRotation, fromPosition);
                toMatrix.decompose(toScaling, toRotation, toPosition);

                var lerpFactor = MathUtilities.GetLerpFactor(fromKey.frame, toKey.frame, frame);

                var interpolatedKeyPosition = BabylonVector3.FromArray(MathUtilities.Lerp(fromPosition.ToArray(), toPosition.ToArray(), lerpFactor));
                var interpolatedKeyRotation = BabylonQuaternion.Slerp(fromRotation, toRotation, lerpFactor);
                var interpolatedKeyScaling  = BabylonVector3.FromArray(MathUtilities.Lerp(fromScaling.ToArray(), toScaling.ToArray(), lerpFactor));

                return(BabylonMatrix.Compose(interpolatedKeyScaling, interpolatedKeyRotation, interpolatedKeyPosition).m);

            case "rotationQuaternion":
                return(BabylonQuaternion.Slerp(BabylonQuaternion.FromArray(fromKey.values), BabylonQuaternion.FromArray(toKey.values), MathUtilities.GetLerpFactor(fromKey.frame, toKey.frame, frame)).ToArray());

            case "scaling":
            case "position":
            default:
                return(MathUtilities.Lerp(fromKey.values, toKey.values, MathUtilities.GetLerpFactor(fromKey.frame, toKey.frame, frame)));
            }
        }
Beispiel #3
0
        /**
         * Sets the passed matrix with the current inverted Matrix.
         * Returns the unmodified current Matrix.
         */
        public BabylonMatrix invertToRef(BabylonMatrix other)
        {
            var l1  = this.m[0];
            var l2  = this.m[1];
            var l3  = this.m[2];
            var l4  = this.m[3];
            var l5  = this.m[4];
            var l6  = this.m[5];
            var l7  = this.m[6];
            var l8  = this.m[7];
            var l9  = this.m[8];
            var l10 = this.m[9];
            var l11 = this.m[10];
            var l12 = this.m[11];
            var l13 = this.m[12];
            var l14 = this.m[13];
            var l15 = this.m[14];
            var l16 = this.m[15];
            var l17 = (l11 * l16) - (l12 * l15);
            var l18 = (l10 * l16) - (l12 * l14);
            var l19 = (l10 * l15) - (l11 * l14);
            var l20 = (l9 * l16) - (l12 * l13);
            var l21 = (l9 * l15) - (l11 * l13);
            var l22 = (l9 * l14) - (l10 * l13);
            var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
            var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
            var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
            var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
            var l27 = 1.0f / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
            var l28 = (l7 * l16) - (l8 * l15);
            var l29 = (l6 * l16) - (l8 * l14);
            var l30 = (l6 * l15) - (l7 * l14);
            var l31 = (l5 * l16) - (l8 * l13);
            var l32 = (l5 * l15) - (l7 * l13);
            var l33 = (l5 * l14) - (l6 * l13);
            var l34 = (l7 * l12) - (l8 * l11);
            var l35 = (l6 * l12) - (l8 * l10);
            var l36 = (l6 * l11) - (l7 * l10);
            var l37 = (l5 * l12) - (l8 * l9);
            var l38 = (l5 * l11) - (l7 * l9);
            var l39 = (l5 * l10) - (l6 * l9);

            other.m[0]  = l23 * l27;
            other.m[4]  = l24 * l27;
            other.m[8]  = l25 * l27;
            other.m[12] = l26 * l27;
            other.m[1]  = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
            other.m[5]  = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
            other.m[9]  = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
            other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
            other.m[2]  = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
            other.m[6]  = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
            other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
            other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
            other.m[3]  = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
            other.m[7]  = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
            other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
            other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;

            return(this);
        }
Beispiel #4
0
 /**
  * Sets the passed matrix "result" with the 16 passed floats.
  */
 public static void FromValuesToRef(float initialM11, float initialM12, float initialM13, float initialM14,
                                    float initialM21, float initialM22, float initialM23, float initialM24,
                                    float initialM31, float initialM32, float initialM33, float initialM34,
                                    float initialM41, float initialM42, float initialM43, float initialM44, BabylonMatrix result)
 {
     result.m[0]  = initialM11;
     result.m[1]  = initialM12;
     result.m[2]  = initialM13;
     result.m[3]  = initialM14;
     result.m[4]  = initialM21;
     result.m[5]  = initialM22;
     result.m[6]  = initialM23;
     result.m[7]  = initialM24;
     result.m[8]  = initialM31;
     result.m[9]  = initialM32;
     result.m[10] = initialM33;
     result.m[11] = initialM34;
     result.m[12] = initialM41;
     result.m[13] = initialM42;
     result.m[14] = initialM43;
     result.m[15] = initialM44;
 }
Beispiel #5
0
 /**
  * Sets the passed matrix "result" with the multiplication result of the current Matrix and the passed one.
  */
 public BabylonMatrix multiplyToRef(BabylonMatrix other, BabylonMatrix result)
 {
     this.multiplyToArray(other, result.m, 0);
     return(this);
 }
Beispiel #6
0
        /**
         * Update a Matrix with values composed by the passed scale (vector3), rotation (quaternion) and translation (vector3).
         */
        public static void ComposeToRef(BabylonVector3 scale, BabylonQuaternion rotation, BabylonVector3 translation, BabylonMatrix result)
        {
            var matrix1 = new BabylonMatrix();

            BabylonMatrix.FromValuesToRef(scale.X, 0, 0, 0,
                                          0, scale.Y, 0, 0,
                                          0, 0, scale.Z, 0,
                                          0, 0, 0, 1, matrix1);

            var matrix0 = new BabylonMatrix();

            rotation.toRotationMatrix(matrix0);
            matrix1.multiplyToRef(matrix0, result);

            result.setTranslation(translation);
        }