[]
Represents a 4x4 mathematical matrix.
public struct Matrix4x4 : IEquatable<Matrix4x4>, IFormattable
Initializes a new instance of the Matrix4x4 struct.
public Matrix4x4(float M11, float M12, float M13, float M14, float M21, float M22, float M23, float M24, float M31, float M32, float M33, float M34, float M41, float M42, float M43, float M44)
M11
floatThe value to assign at row 1 column 1 of the matrix.
M12
floatThe value to assign at row 1 column 2 of the matrix.
M13
floatThe value to assign at row 1 column 3 of the matrix.
M14
floatThe value to assign at row 1 column 4 of the matrix.
M21
floatThe value to assign at row 2 column 1 of the matrix.
M22
floatThe value to assign at row 2 column 2 of the matrix.
M23
floatThe value to assign at row 2 column 3 of the matrix.
M24
floatThe value to assign at row 2 column 4 of the matrix.
M31
floatThe value to assign at row 3 column 1 of the matrix.
M32
floatThe value to assign at row 3 column 2 of the matrix.
M33
floatThe value to assign at row 3 column 3 of the matrix.
M34
floatThe value to assign at row 3 column 4 of the matrix.
M41
floatThe value to assign at row 4 column 1 of the matrix.
M42
floatThe value to assign at row 4 column 2 of the matrix.
M43
floatThe value to assign at row 4 column 3 of the matrix.
M44
floatThe value to assign at row 4 column 4 of the matrix.
Initializes a new instance of the Matrix4x4 struct.
public Matrix4x4(float value)
value
floatThe value that will be assigned to all components.
Initializes a new instance of the Matrix4x4 struct.
public Matrix4x4(float[] values)
values
float[]The values to assign to the components of the matrix. This must be an array with sixteen elements.
Thrown when values
is null
.
Thrown when values
contains more or less than sixteen elements.
The identity Matrix4x4.
public static readonly Matrix4x4 Identity
Value at row 1 column 1 of the matrix.
public float M11
Value at row 1 column 2 of the matrix.
public float M12
Value at row 1 column 3 of the matrix.
public float M13
Value at row 1 column 4 of the matrix.
public float M14
Value at row 2 column 1 of the matrix.
public float M21
Value at row 2 column 2 of the matrix.
public float M22
Value at row 2 column 3 of the matrix.
public float M23
Value at row 2 column 4 of the matrix.
public float M24
Value at row 3 column 1 of the matrix.
public float M31
Value at row 3 column 2 of the matrix.
public float M32
Value at row 3 column 3 of the matrix.
public float M33
Value at row 3 column 4 of the matrix.
public float M34
Value at row 4 column 1 of the matrix.
public float M41
Value at row 4 column 2 of the matrix.
public float M42
Value at row 4 column 3 of the matrix.
public float M43
Value at row 4 column 4 of the matrix.
public float M44
The size of the Matrix4x4 type, in bytes.
public const int SizeInBytes = 64
A Matrix4x4 with all of its components set to zero.
public static readonly Matrix4x4 Zero
Gets or sets the first column in the matrix; that is M11, M21, M31, and M41.
public Vector4 Column1 { get; set; }
Gets or sets the second column in the matrix; that is M12, M22, M32, and M42.
public Vector4 Column2 { get; set; }
Gets or sets the third column in the matrix; that is M13, M23, M33, and M43.
public Vector4 Column3 { get; set; }
Gets or sets the fourth column in the matrix; that is M14, M24, M34, and M44.
public Vector4 Column4 { get; set; }
Calculates the determinant of the matrix.
public float Determinant { get; }
The determinant of the matrix.
Gets a value indicating whether this instance is an identity matrix.
public bool IsIdentity { get; }
true
if this instance is an identity matrix; otherwise, false
.
Gets or sets the component at the specified index.
public float this[int row, int column] { get; set; }
row
intThe row of the matrix to access.
column
intThe column of the matrix to access.
The value of the matrix component, depending on the index.
Thrown when the row
or column
is out of the range [0, 3].
Gets or sets the component at the specified index.
public float this[int index] { get; set; }
index
intThe zero-based index of the component to access.
The value of the matrix component, depending on the index.
Thrown when the index
is out of the range [0, 15].
Gets or sets the first row in the matrix; that is M11, M12, M13, and M14.
public Vector4 Row1 { get; set; }
Gets or sets the second row in the matrix; that is M21, M22, M23, and M24.
public Vector4 Row2 { get; set; }
Gets or sets the third row in the matrix; that is M31, M32, M33, and M34.
public Vector4 Row3 { get; set; }
Gets or sets the fourth row in the matrix; that is M41, M42, M43, and M44.
public Vector4 Row4 { get; set; }
Gets or sets the scale of the matrix; that is M11, M22, and M33.
public Vector3 ScaleVector { get; set; }
Gets or sets the translation of the matrix; that is M41, M42, and M43.
public Vector3 TranslationVector { get; set; }
Determines the sum of two matrices.
public static Matrix4x4 Add(in Matrix4x4 left, in Matrix4x4 right)
The sum of the two matrices.
Creates a spherical billboard that rotates around a specified object position.
public static Matrix4x4 Billboard(Vector3 objectPosition, Vector3 cameraPosition, Vector3 cameraUpVector, Vector3 cameraForwardVector)
objectPosition
Vector3The position of the object that the billboard will rotate around.
cameraPosition
Vector3The position of the camera.
cameraUpVector
Vector3The up vector of the camera.
cameraForwardVector
Vector3The forward vector of the camera.
The created billboard.
Creates a cylindrical billboard that rotates around a specified axis.
public static Matrix4x4 ConstrainedBillboard(Vector3 objectPosition, Vector3 cameraPosition, Vector3 rotateAxis, Vector3 cameraForwardVector, Vector3 objectForwardVector)
objectPosition
Vector3The position of the object that the billboard will rotate around.
cameraPosition
Vector3The position of the camera.
rotateAxis
Vector3The axis to rotate the billboard around.
cameraForwardVector
Vector3The forward vector of the camera.
objectForwardVector
Vector3The forward vector of the object.
The billboard matrix.
Divides a matrix by the given value.
public static Matrix4x4 Divide(in Matrix4x4 left, float right)
left
Matrix4x4The matrix to divides.
right
floatThe amount by which to divides.
The resulting matrix.
Determines whether the specified Matrix4x4 is equal to this instance.
public bool Equals(Matrix4x4 other)
other
Matrix4x4Determines whether two instances of Matrix4x4 are equal.
public static bool Equals(in Matrix4x4 left, in Matrix4x4 right)
Determines whether the specified object is equal to this instance.
public override bool Equals(object value)
value
objectExchanges two columns in the matrix.
public void ExchangeColumns(int firstColumn, int secondColumn)
firstColumn
intThe first column to exchange. This is an index of the column starting at zero.
secondColumn
intThe second column to exchange. This is an index of the column starting at zero.
Exchanges two rows in the matrix.
public void ExchangeRows(int firstRow, int secondRow)
firstRow
intThe first row to exchange. This is an index of the row starting at zero.
secondRow
intThe second row to exchange. This is an index of the row starting at zero.
Returns a hash code for this instance.
public override int GetHashCode()
A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
Element-wise division of two matrices.
public static Matrix4x4 HadamardDivide(in Matrix4x4 left, in Matrix4x4 right)
The result of division of the two matrices.
Determines the Hadamard (element-wise) product of two matrices.
public static Matrix4x4 HadamardProduct(in Matrix4x4 left, in Matrix4x4 right)
The Hadamard (element-wise) product of the two matrices.
Calculates the inverse of the specified matrix.
public static Matrix4x4 Invert(in Matrix4x4 value)
value
Matrix4x4The matrix whose inverse is to be calculated.
The inverse of the specified matrix.
Performs a linear interpolation between two matrices based on the given weighting.
public static Matrix4x4 Lerp(in Matrix4x4 matrix1, in Matrix4x4 matrix2, float amount)
matrix1
Matrix4x4The first matrix.
matrix2
Matrix4x4The second matrix.
amount
floatA value between 0 and 1 that indicates the weight of matrix2.
The interpolated matrix.
Creates a view matrix.
public static Matrix4x4 LookAt(Vector3 cameraPosition, Vector3 cameraTarget, Vector3 cameraUpVector)
cameraPosition
Vector3The position of the camera.
cameraTarget
Vector3The target towards which the camera is pointing.
cameraUpVector
Vector3The direction that is "up" from the camera's point of view.
The view matrix.
Determines the product of two matrices.
public static Matrix4x4 Multiply(in Matrix4x4 left, in Matrix4x4 right)
The product of the two matrices.
Scales a matrix by the given value.
public static Matrix4x4 Multiply(in Matrix4x4 left, float right)
left
Matrix4x4The matrix to scale.
right
floatThe amount by which to scale.
The scaled matrix.
Negates a matrix.
public static Matrix4x4 Negate(in Matrix4x4 value)
value
Matrix4x4The matrix to be negated.
The negated matrix.
Negates a matrix.
public static void NegateRef(ref Matrix4x4 value)
value
Matrix4x4The matrix to be negated.
Creates an orthographic perspective matrix from the given view volume dimensions.
public static Matrix4x4 Orthographic(float width, float height, float zNearPlane, float zFarPlane)
width
floatThe width of the view volume.
height
floatThe height of the view volume.
zNearPlane
floatThe minimum Z-value of the view volume.
zFarPlane
floatThe maximum Z-value of the view volume.
The orthographic projection matrix.
Creates a customized orthographic projection matrix.
public static Matrix4x4 OrthographicOffCenter(float left, float right, float bottom, float top, float zNearPlane, float zFarPlane)
left
floatThe minimum X-value of the view volume.
right
floatThe maximum X-value of the view volume.
bottom
floatThe minimum Y-value of the view volume.
top
floatThe maximum Y-value of the view volume.
zNearPlane
floatThe minimum Z-value of the view volume.
zFarPlane
floatThe maximum Z-value of the view volume.
The orthographic projection matrix.
Creates a perspective projection matrix from the given view volume dimensions.
public static Matrix4x4 Perspective(float width, float height, float nearPlaneDistance, float farPlaneDistance)
width
floatThe width of the view volume at the near view plane.
height
floatThe height of the view volume at the near view plane.
nearPlaneDistance
floatThe distance to the near view plane.
farPlaneDistance
floatThe distance to the far view plane.
The perspective projection matrix.
Creates a perspective projection matrix based on a field of view, aspect ratio, and near and far view plane distances.
public static Matrix4x4 PerspectiveFieldOfView(float fieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance)
fieldOfView
floatThe field of view in the y direction, in radians.
aspectRatio
floatThe aspect ratio, defined as view space width divided by height.
nearPlaneDistance
floatThe distance to the near view plane.
farPlaneDistance
floatThe distance to the far view plane.
The perspective projection matrix.
Creates a customized perspective projection matrix.
public static Matrix4x4 PerspectiveOffCenter(float left, float right, float bottom, float top, float nearPlaneDistance, float farPlaneDistance)
left
floatThe minimum x-value of the view volume at the near view plane.
right
floatThe maximum x-value of the view volume at the near view plane.
bottom
floatThe minimum y-value of the view volume at the near view plane.
top
floatThe maximum y-value of the view volume at the near view plane.
nearPlaneDistance
floatThe distance to the near view plane.
farPlaneDistance
floatThe distance to the far view plane.
The perspective projection matrix.
Creates a matrix that reflects the coordinate system about a specified plane.
public static Matrix4x4 Reflection(Vector3 normal, float d)
normal
Vector3The plane's normal vector.
d
floatThe plane's distance from the origin along its normal vector.
A new matrix expressing the reflection.
Creates a matrix that rotates around an arbitary axis.
public static Matrix4x4 RotationAxis(Vector3 axis, float radians)
axis
Vector3The axis around which to rotate. This parameter is assumed to be normalized.
radians
floatAngle of rotation, in radians. Angles are measured clockwise when looking along the rotation axis toward the origin.
The created rotation matrix.
Creates a matrix for rotating points around the X axis from a center point.
public static Matrix4x4 RotationX(float radians, Vector3 centerPoint)
radians
floatThe amount, in radians, by which to rotate around the X axis.
centerPoint
Vector3The center point.
The rotation matrix.
Creates a matrix that rotates around the x-axis.
public static Matrix4x4 RotationX(float radians)
radians
floatAngle of rotation in radians. Angles are measured clockwise when looking along the rotation axis toward the origin.
The created rotation matrix.
Creates a matrix that rotates around the y-axis.
public static Matrix4x4 RotationY(float radians, Vector3 centerPoint)
radians
floatAngle of rotation in radians. Angles are measured clockwise when looking along the rotation axis toward the origin.
centerPoint
Vector3The center point.
The created rotation matrix.
Creates a matrix that rotates around the y-axis.
public static Matrix4x4 RotationY(float radians)
radians
floatAngle of rotation in radians. Angles are measured clockwise when looking along the rotation axis toward the origin.
The created rotation matrix.
Creates a matrix that rotates around the z-axis.
public static Matrix4x4 RotationZ(float radians, Vector3 centerPoint)
radians
floatAngle of rotation in radians. Angles are measured clockwise when looking along the rotation axis toward the origin.
centerPoint
Vector3The center point.
The created rotation matrix.
Creates a matrix that rotates around the z-axis.
public static Matrix4x4 RotationZ(float radians)
radians
floatAngle of rotation in radians. Angles are measured clockwise when looking along the rotation axis toward the origin.
The created rotation matrix.
Creates a matrix that scales along the x-axis, y-axis, and y-axis.
public static Matrix4x4 Scale(Vector3 scale)
scale
Vector3Scaling factor for all three axes.
The created scaling matrix.
Creates a matrix that scales along the x-axis, y-axis, and y-axis.
public static Matrix4x4 Scale(float xScale, float yScale, float zScale)
xScale
floatScaling factor that is applied along the x-axis.
yScale
floatScaling factor that is applied along the y-axis.
zScale
floatScaling factor that is applied along the z-axis.
The created scaling matrix.
Creates a matrix that uniformally scales along all three axis.
public static Matrix4x4 Scale(float scale)
scale
floatThe uniform scale that is applied along all axis.
The created scaling matrix.
Creates a matrix that flattens geometry into a specified plane as if casting a shadow from a specified light source.
public static Matrix4x4 Shadow(Vector3 lightDirection, Vector3 normal, float d)
lightDirection
Vector3The direction from which the light that will cast the shadow is coming.
normal
Vector3The plane's normal vector.
d
floatThe plane's distance from the origin along its normal vector.
A new matrix that can be used to flatten geometry onto the specified plane from the specified direction.
Creates a skew/shear matrix by means of a translation vector, a rotation vector, and a rotation angle.
public static Matrix4x4 Skew(Vector3 transVec, Vector3 rotationVec, float radians)
transVec
Vector3The translation vector
rotationVec
Vector3The rotation vector
radians
floatThe rotation angle, in radians.
The created skew/shear matrix.
Shearing is performed in the direction of translation vector, where translation vector and rotation vector define the shearing plane. The effect is such that the skewed rotation vector has the specified angle with rotation itself.
Determines the difference between two matrices.
public static Matrix4x4 Subtract(in Matrix4x4 left, in Matrix4x4 right)
The difference between the two matrices.
Creates an array containing the elements of the matrix.
public float[] ToArray()
A sixteen-element array containing the components of the matrix.
Returns a string that represents the current object.
public override string ToString()
Returns a string that represents this instance.
public string ToString(IFormatProvider formatProvider)
formatProvider
System.IFormatProviderThe format provider.
A string that represents this instance.
Returns a string that represents this instance.
public string ToString(string format, IFormatProvider formatProvider)
format
stringThe format.
formatProvider
System.IFormatProviderThe format provider.
A string that represents this instance.
Returns a string that represents this instance.
public string ToString(string format)
format
stringThe format.
A string that represents this instance.
Creates a translation matrix using the specified offsets.
public static Matrix4x4 Translation(Vector3 value)
value
Vector3The amount to translate in each axis.
The created translation matrix.
Creates a translation matrix using the specified offsets.
public static Matrix4x4 Translation(float x, float y, float z)
x
floatX-coordinate offset.
y
floatY-coordinate offset.
z
floatZ-coordinate offset.
The created translation matrix.
Calculates the transpose of the specified matrix.
public static Matrix4x4 Transpose(in Matrix4x4 value)
value
Matrix4x4The matrix whose transpose is to be calculated.
The transpose of the specified matrix.
Transposes the matrix.
public static void TransposeRef(ref Matrix4x4 value)
value
Matrix4x4The matrix whose transpose is to be calculated.
Creates a world matrix with the specified parameters.
public static Matrix4x4 World(Vector3 position, Vector3 forward, Vector3 up)
position
Vector3The position of the object (used in translation operations).
forward
Vector3The forward direction of the object.
up
Vector3The upward direction of the object. Its value is usually [0, 1, 0].
The world matrix.
Creates a rotation matrix from the specified yaw, pitch, and roll.
public static Matrix4x4 YawPitchRoll(float yaw, float pitch, float roll)
yaw
floatThe angle of rotation, in radians, around the Y axis.
pitch
floatThe angle of rotation, in radians, around the X axis.
roll
floatThe angle of rotation, in radians, around the Z axis.
The rotation matrix.
Adds two matricies.
public static Matrix4x4 operator +(in Matrix4x4 left, in Matrix4x4 right)
The sum of the two matricies.
Scales a matrix by a given value.
public static Matrix4x4 operator /(in Matrix4x4 left, float right)
left
Matrix4x4The matrix to scale.
right
floatThe amount by which to scale.
The scaled matrix.
Tests for equality between two objects.
public static bool operator ==(in Matrix4x4 left, in Matrix4x4 right)
true
if left
has the same value as right
; otherwise, false
.
Tests for inequality between two objects.
public static bool operator !=(in Matrix4x4 left, in Matrix4x4 right)
true
if left
has a different value than right
; otherwise, false
.
Multiplies two matricies.
public static Matrix4x4 operator *(in Matrix4x4 left, in Matrix4x4 right)
The product of the two matricies.
Scales a matrix by a given value.
public static Matrix4x4 operator *(in Matrix4x4 left, float right)
left
Matrix4x4The matrix to scale.
right
floatThe amount by which to scale.
The scaled matrix.
Scales a matrix by a given value.
public static Matrix4x4 operator *(float left, in Matrix4x4 right)
left
floatThe amount by which to scale.
right
Matrix4x4The matrix to scale.
The scaled matrix.
Subtracts two matricies.
public static Matrix4x4 operator -(in Matrix4x4 left, in Matrix4x4 right)
The difference between the two matricies.
Negates a matrix.
public static Matrix4x4 operator -(in Matrix4x4 value)
value
Matrix4x4The matrix to negate.
The negated matrix.