/// <summary> /// Creates a new matrix that performs translation, rotation and scale. /// </summary> /// <param name="translation">Offset to translate by.</param> /// <param name="rotation">Rotation quaternion.</param> /// <param name="scale">Non-uniform scale factors.</param> /// <returns>Matrix that performs scale, followed by rotation, followed by translation. </returns> public static Matrix4 TRS(Vector3 translation, Quaternion rotation, Vector3 scale) { Matrix3 rot3x3 = rotation.ToRotationMatrix(); Matrix4 mat = new Matrix4(); mat.m00 = scale.x * rot3x3.m00; mat.m01 = scale.y * rot3x3.m01; mat.m02 = scale.z * rot3x3.m02; mat.m03 = translation.x; mat.m10 = scale.x * rot3x3.m10; mat.m11 = scale.y * rot3x3.m11; mat.m12 = scale.z * rot3x3.m12; mat.m13 = translation.y; mat.m20 = scale.x * rot3x3.m20; mat.m21 = scale.y * rot3x3.m21; mat.m22 = scale.z * rot3x3.m22; mat.m23 = translation.z; // No projection term mat.m30 = 0; mat.m31 = 0; mat.m32 = 0; mat.m33 = 1; return(mat); }
/// <summary> /// Creates a rotation matrix from a quaternion rotation. /// </summary> /// <param name="quat">Quaternion to create the matrix from.</param> /// <returns>Rotation matrix containing the equivalent rotation of the provided quaternion.</returns> public static Matrix3 FromQuaternion(Quaternion quat) { return(quat.ToRotationMatrix()); }
/// <summary> /// Creates a new matrix that performs translation, rotation and scale. /// </summary> /// <param name="translation">Offset to translate by.</param> /// <param name="rotation">Rotation quaternion.</param> /// <param name="scale">Non-uniform scale factors.</param> /// <returns>Matrix that performs scale, followed by rotation, followed by translation. </returns> public static Matrix4 TRS(Vector3 translation, Quaternion rotation, Vector3 scale) { Matrix3 rot3x3 = rotation.ToRotationMatrix(); Matrix4 mat = new Matrix4(); mat.m00 = scale.x * rot3x3.m00; mat.m01 = scale.y * rot3x3.m01; mat.m02 = scale.z * rot3x3.m02; mat.m03 = translation.x; mat.m10 = scale.x * rot3x3.m10; mat.m11 = scale.y * rot3x3.m11; mat.m12 = scale.z * rot3x3.m12; mat.m13 = translation.y; mat.m20 = scale.x * rot3x3.m20; mat.m21 = scale.y * rot3x3.m21; mat.m22 = scale.z * rot3x3.m22; mat.m23 = translation.z; // No projection term mat.m30 = 0; mat.m31 = 0; mat.m32 = 0; mat.m33 = 1; return mat; }