The geometric meaning of the Unity Shader matrix

        Matrix transformation: refers to the process in which we convert some data, such as points, direction vectors and even colors, in a certain way.

        Linear transformations: transformations that can preserve vector and scalar multiplications (scaling, rotation, skew, mirroring, orthographic projection)

         Affine Transformation: A transform type that combines linear and translational transformations. It can be represented by a 4x4 matrix, for which we need to extend the vector to a four-dimensional space, which is the homogeneous coordinate space. 

         Since a 3x3 matrix cannot represent a translation operation, we extend it to a 4x4 matrix to convert the original three-dimensional vector into a four-dimensional vector.


        Decompose the basic transformation matrix: the transformation matrix of pure translation, pure rotation and pure scaling is called the basic transformation matrix. M3x3 is used to represent rotation and scaling, t3x1 is used to represent translation, 01x3 is a zero matrix, and 1 is a scalar 1.

        Translation matrix : We use matrix multiplication to represent the translation transformation of a point. As shown in the figure below, it can be seen that x, y, and z are respectively increased by a position offset. (doesn't have any effect on direction vectors, since vectors don't have a position attribute)

        Scale matrix : scale the x, y, and z axes. If the scaling factor kx=ky=kz, we call such scaling uniform scaling, otherwise it is non-uniform scaling, non-uniform scaling will stretch or squeeze the model, change the angle and ratio related to the model, such as transforming the normal Yes, if there is non-uniform scaling, using the transformation matrix used to transform vertices directly will give wrong results. 

        Rotation matrix : rotate around the x-axis, y-axis or z-axis in space. In Unity, if multiple rotations with different axes are performed, we define a rotation order zxy.

        x-axis:

        y-axis: 

        z-axis: 

       Compound transformation: combine the above-mentioned translation, rotation and scaling to form a complex transformation process. Since matrix multiplication does not satisfy the commutative law, the order of matrix multiplication is very important. In most cases, the order of transformation we agree on is to scale first, then rotate, and finally translate.


        After learning the matrix transformation, we can use it to transform a point or direction vector from one coordinate space to another. We assume that Ap is the coordinate of the parent coordinate system obtained by Ac passing through the transformation matrix, and Bc is the coordinate of the child coordinate system obtained by Bp after passing the transformation matrix.

         After the matrix formula we learned before, we can finally deduce that Mc>p can actually be constructed by the vector representation of the origin of the coordinate space C in the coordinate space P and the coordinate axes, and put the three coordinate axes into the matrix in turn. For the first three columns, put the origin vector in the last column, and fill the last row with 0 and 1.

         And for the coordinate space transformation of the direction vector, because the vector has no position, the origin transformation of the coordinate space can be ignored, so we only translate the origin of the coordinate system without any effect on the vector, so we can directly use 3x3 matrix to represent:

        In Unity, there are the following common coordinate spaces:

         Therefore, there are these commonly used matrices in Unity, especially the MVP matrix, because it is a matrix that combines the M, V, and P matrices:


         In Unity Shader, the CG language is used, and CG uses the row-first method, that is, fill the matrix row by row, that is, float 3x3 M = float 3x3 (1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0, 9.0)

         If you need to fill the matrix column-major, you can use Matrix4x4.

 

Guess you like

Origin blog.csdn.net/weixin_45081191/article/details/129180199