Unity Shader Math Basics - Matrix

Definition of Matrix

A number table with m rows and n columns arranged by m×n numbers a is called a matrix with m rows and n columns, or m×n matrix for short. Referred to as:

This m×n number is called the element of matrix A, which is referred to as element for short. The number a is located in the i-th row and j-th column of matrix A, which is called the (i, j) element of matrix A, and the number a is (i, j) The matrix of ) elements can be denoted as (a) or (a)m×n, and the m×n matrix A is also denoted as Amn.

A matrix whose elements are real numbers is called a real matrix, and a matrix whose elements are complex numbers is called a complex matrix. A matrix with the number of rows and columns equal to n is called a matrix of order n or a square matrix of order n.

Matrix Basic Operations

addition

The addition of matrices satisfies the following operation laws (A, B, C are all of the same type):

It should be noted that addition is only possible between matrices of the same type

subtraction

multiplication

The multiplication of matrices satisfies the following operation laws :

Transpose

The matrix produced by exchanging the rows and columns of matrix A is called the transpose matrix of A (Aᵀ ), and this process is called matrix transposition.

    

Property 1: The transpose of matrix transpose is equal to the original matrix. (Mᵀ )ᵀ =M   

Property 2: The transpose of the matrix concatenation is equal to the transpose of the reverse concatenation of each matrix. (AB)ᵀ = Bᵀ Aᵀ                                    

matrix multiplication

The multiplication of two matrices can only be defined if the number of columns of the first matrix A and the number of rows of the other matrix B are equal. If A is an m×n matrix and B is an n×p matrix, their product C is an m×p matrix

The multiplication of matrices satisfies the following operation laws:

Associativity:  (AB)C=A(BC)

Left distributive law:  (A+B)C=AC+BC

Right distributive law:  C(A+B)=CA+CB

Matrix multiplication is not commutative.

special matrix

square matrix

Square matrices are referred to as square matrices, which refer to those matrices with the same number of rows and columns. The most commonly used in 3D rendering are 3X3 and 4X4 squares.

Some operations and properties of matrices are only available to square matrices. For example, diagonal elements. The diagonal elements of a square matrix refer to the elements whose row numbers and columns are equal. If the square matrix is ​​regarded as a square, those elements are arranged on the diagonal of the square. A matrix is ​​called a diagonal matrix if all but the diagonal elements are 0.

identity matrix

A special diagonal matrix is ​​the identity matrix, denoted by In.

Inverse matrix

Not all matrices have inverses, but they must be square.

Given a square matrix M, its inverse is represented by M⁻¹. The most important property of inverse matrices is that if we multiply M and M⁻¹, their result will be an identity matrix. MM⁻¹=M⁻¹M=I;

Property 1: The inverse matrix of the inverse matrix is ​​the original matrix itself.

Property 2: The inverse matrix of the identity matrix is ​​itself.

Property 3: The inverse of a transposed matrix is ​​the transpose of the inverse matrix. (MT)⁻¹=(M⁻¹)T

Property 4: The inverse matrix after matrix concatenation and multiplication is equal to the inverse matrix of each matrix inversely concatenated. (AB)⁻¹=B⁻¹A⁻¹

Orthogonal matrix

Orthogonality is a property of a matrix. If the product of a square matrix M and its transposed matrix is ​​the identity matrix, then we say that this matrix is ​​orthogonal. The reverse is also established. MMᵀ = Mᵀ M=I.

Geometric Meaning of Matrix

In the game world, transformations generally include rotation, scaling, and translation. Developers hope that given a point or vector, and then a transformation, a new point and vector can be obtained through a mathematical operation.

Transformation refers to the process of transforming some data, such as points, direction vectors and even colors, in some way. In the field of computer graphics, transformations are very important. Although the operations we can do with transformations are limited, these operations are enough to establish transformations as important in the field of graphics.

Linear transformation. Linear transformations are those that preserve vector addition and vector multiplication. Scaling is a linear transformation. Rotation is also a linear transformation. If we want to transform a three-dimensional vector, then only a 3X3 matrix can represent all linear transformations.

In addition to rotation and scaling, linear transformation also includes cross-cutting, mirroring, also known as orthogonal projection, etc.

But the general linear transformation is not enough, we consider the translation transformation is not a linear transformation.

This way we have an affine transformation . Affine transformation is a transformation type that combines linear transformation and translation transformation. Affine transformation can be represented by a 4X4 matrix. For this, we need to extend the vector to four-dimensional space, which is the homogeneous coordinate space.

Homogeneous coordinates

Since a 3X3 matrix cannot represent a translation operation, we need to extend it to a 4X4 matrix. For this, we also need to convert the original three-dimensional vector into a four-bit vector, which is what we call homogeneous coordinates.

Homogeneous coordinates are a four-dimensional vector.

translation matrix

scaling matrix

rotation matrix

compound transformation

Guess you like

Origin blog.csdn.net/f402455894/article/details/122558663