48 lines
2.0 KiB
Markdown
48 lines
2.0 KiB
Markdown
|
# 4×4 matrices
|
|||
|
|
|||
|
To create a 4×4 transformation matrix, use the constructors in [`float4x4`](xref:Unity.Mathematics.float4x4) to assign one value to all elements of the matrix, or individually set all 16 elements directly:
|
|||
|
|
|||
|
|
|||
|
```c#
|
|||
|
// Unity Mathematics example
|
|||
|
void Build4x4UnityMathematics()
|
|||
|
{
|
|||
|
var c0 = new float4(1.0f, 0.0f, 0.0f, 0.0f);
|
|||
|
var c1 = new float4(0.0f, 1.0f, 0.0f, 0.0f);
|
|||
|
var c2 = new float4(0.0f, 0.0f, 1.0f, 0.0f);
|
|||
|
var c3 = new float4(0.0f, 0.0f, 0.0f, 1.0f);
|
|||
|
var m = new float4x4(c0, c1, c2, c3);
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
## Multiplying a 4×4 matrix
|
|||
|
|
|||
|
Unity Mathematics and `UnityEngine` define the `*` operator differently. The `*` operator for [`float4x4`](xref:Unity.Mathematics.float4x4) implements componentwise multiplication. If you multiply a `float4x4` of all 1s with 0.5 on the diagonal, you get back the half identity because the upper and lower triangles of the matrix are multiplied by the respective zero entries from `f4x4_HalfIdentity`:
|
|||
|
|
|||
|
```c#
|
|||
|
// Unity Mathematics example
|
|||
|
void OperatorMultiply4x4UnityMathematics()
|
|||
|
{
|
|||
|
float4x4 result = f4x4_Ones * f4x4_HalfIdentity;
|
|||
|
// result:
|
|||
|
// 0.5, 0.0, 0.0, 0.0,
|
|||
|
// 0.0, 0.5, 0.0, 0.0,
|
|||
|
// 0.0, 0.0, 0.5, 0.0,
|
|||
|
// 0.0, 0.0, 0.0, 0.5
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
## Multiplying a 4×4 matrix and a 4D vector
|
|||
|
|
|||
|
Use the [`math.mul`](xref:Unity.Mathematics.math.mul*) method to multiply a 4×4 matrix and a 4D vector. If you supply a `float4x4` as the first parameter and a `float4` as the second, it performs a 4×4 matrix multiplication with a 4×1 column vector, which returns a 4×1 column vector as a `float4`.
|
|||
|
|
|||
|
`math.mul` can also multiply a 1×4 row vector by a 4×4 matrix to produce a 1×4 row vector by taking a `float4` as the first parameter and a `float4×4` as the second. Unity Mathematics stores the row vector in a `float4` and it isn't treated as a separate type.
|
|||
|
|
|||
|
```c#
|
|||
|
// Unity Mathematics example
|
|||
|
void Multiply4x4AndVector4UnityMathematics()
|
|||
|
{
|
|||
|
float4 result1 = math.mul(f4x4, f4); // 4x4 * 4x1 = 4x1
|
|||
|
float4 result2 = math.mul(f4, f4x4); // 1x4 * 4x4 = 1x4
|
|||
|
}
|
|||
|
```
|