示例#1
0
        public FMatrix ToMatrixWithScale()
        {
            var outMatrix = new FMatrix();

            outMatrix.M30 = Translation.X;
            outMatrix.M31 = Translation.Y;
            outMatrix.M32 = Translation.Z;

            var x2 = Rotation.X + Rotation.X;
            var y2 = Rotation.Y + Rotation.Y;
            var z2 = Rotation.Z + Rotation.Z;

            {
                var xx2 = Rotation.X * x2;
                var yy2 = Rotation.Y * y2;
                var zz2 = Rotation.Z * z2;

                outMatrix.M00 = (1.0f - (yy2 + zz2)) * Scale3D.X;
                outMatrix.M11 = (1.0f - (xx2 + zz2)) * Scale3D.Y;
                outMatrix.M22 = (1.0f - (xx2 + yy2)) * Scale3D.Z;
            }
            {
                var yz2 = Rotation.Y * z2;
                var wx2 = Rotation.W * x2;

                outMatrix.M21 = (yz2 - wx2) * Scale3D.Z;
                outMatrix.M12 = (yz2 + wx2) * Scale3D.Y;
            }
            {
                var xy2 = Rotation.X * y2;
                var wz2 = Rotation.W * z2;

                outMatrix.M10 = (xy2 - wz2) * Scale3D.Y;
                outMatrix.M01 = (xy2 + wz2) * Scale3D.X;
            }
            {
                var xz2 = Rotation.X * z2;
                var wy2 = Rotation.W * y2;

                outMatrix.M20 = (xz2 + wy2) * Scale3D.Z;
                outMatrix.M02 = (xz2 - wy2) * Scale3D.X;
            }

            outMatrix.M03 = 0.0f;
            outMatrix.M13 = 0.0f;
            outMatrix.M23 = 0.0f;
            outMatrix.M33 = 1.0f;

            return(outMatrix);
        }
示例#2
0
        public static FMatrix operator *(FMatrix a, FMatrix b)
        {
            var result = new FMatrix
            {
                M00 = a.M00 * b.M00 + a.M01 * b.M10 + a.M02 * b.M20 + a.M03 * b.M30,
                M01 = a.M00 * b.M01 + a.M01 * b.M11 + a.M02 * b.M21 + a.M03 * b.M31,
                M02 = a.M00 * b.M02 + a.M01 * b.M12 + a.M02 * b.M22 + a.M03 * b.M32,
                M03 = a.M00 * b.M03 + a.M01 * b.M13 + a.M02 * b.M23 + a.M03 * b.M33,
                M10 = a.M10 * b.M00 + a.M11 * b.M10 + a.M12 * b.M20 + a.M13 * b.M30,
                M11 = a.M10 * b.M01 + a.M11 * b.M11 + a.M12 * b.M21 + a.M13 * b.M31,
                M12 = a.M10 * b.M02 + a.M11 * b.M12 + a.M12 * b.M22 + a.M13 * b.M32,
                M13 = a.M10 * b.M03 + a.M11 * b.M13 + a.M12 * b.M23 + a.M13 * b.M33,
                M20 = a.M20 * b.M00 + a.M21 * b.M10 + a.M22 * b.M20 + a.M23 * b.M30,
                M21 = a.M20 * b.M01 + a.M21 * b.M11 + a.M22 * b.M21 + a.M23 * b.M31,
                M22 = a.M20 * b.M02 + a.M21 * b.M12 + a.M22 * b.M22 + a.M23 * b.M32,
                M23 = a.M20 * b.M03 + a.M21 * b.M13 + a.M22 * b.M23 + a.M23 * b.M33,
                M30 = a.M30 * b.M00 + a.M31 * b.M10 + a.M32 * b.M20 + a.M33 * b.M30,
                M31 = a.M30 * b.M01 + a.M31 * b.M11 + a.M32 * b.M21 + a.M33 * b.M31,
                M32 = a.M30 * b.M02 + a.M31 * b.M12 + a.M32 * b.M22 + a.M33 * b.M32,
                M33 = a.M30 * b.M03 + a.M31 * b.M13 + a.M32 * b.M23 + a.M33 * b.M33
            };

            return(result);
        }
示例#3
0
        public static void ConstructTransformFromMatrixWithDesiredScale(FMatrix aMatrix, FMatrix bMatrix, FVector desiredScale, ref FTransform outTransform)
        {
            // the goal of using M is to get the correct orientation
            // but for translation, we still need scale
            var m = aMatrix * bMatrix;

            m.RemoveScaling();

            // apply negative scale back to axes
            var signedScale = desiredScale.GetSignVector();

            m.SetAxis(0, signedScale.X * m.GetScaledAxis(EAxis.X));
            m.SetAxis(1, signedScale.Y * m.GetScaledAxis(EAxis.Y));
            m.SetAxis(2, signedScale.Z * m.GetScaledAxis(EAxis.Z));

            // @note: if you have negative with 0 scale, this will return rotation that is identity
            // since matrix loses that axes
            var rotation = new FQuat(m);

            rotation.Normalize();

            // set values back to output
            outTransform.Scale3D  = desiredScale;
            outTransform.Rotation = rotation;

            // technically I could calculate this using FTransform but then it does more quat multiplication
            // instead of using Scale in matrix multiplication
            // it's a question of between RemoveScaling vs using FTransform to move translation
            outTransform.Translation = m.GetOrigin();
        }