By: Saxon
Introduction
In this article, we will learn several concepts: the translation matrix, the rotation matrix, and the scaling matrix. While learning these basic concepts, we will use some simple operations of mesh (mesh), mathematical operations, and 4x4 matrices. However, because Saxon is also a novice, the rigor of the article may not be very high, but also ask the great gods to advise.
Create a project
First create a unity project, Saxon named him "Matrix of China" (Chinese matrix), as shown in the basic configuration:
In order to facilitate the search, let us in the Assets directory to create a new three folders, named "Scripts", "Shader", "materials", this does not explain, everyone can understand it. As shown in the following:
Next, create a C # script in the Scripts folder named "Triangle", which is used to create a simple triangle. Then create an empty object under the Hierarchy panel named "Triangle". It then adds the previously created "Triangle" script to the object and adds two Mesh-related components to the object, as shown in:
OK, let's start with a code.
Triangle.cs
First let's take a look at the complete code and then step-by-step analysis:
Code parsing
I believe this code is not very difficult for everyone. The steps to create a grid are generally created by this way:
1. Assigning a value to a vertex array
2. Assigning a value to a triangular array
In the code, "vertices" is the so-called vertex array, "triangles" is called the triangle array, it is the function of the corresponding vertex index. Typically a triangle is made up of three vertices.
Okay, let's go back to the Inspector panel. Modify the properties of the "Triangle" script, as shown in:
PS: The triangular array index must start at 0, and then increment by 1 in order. And again, three vertices can form a triangle.
OK, now let's click Play to test it:
Okey, now our triangle is finished, and then we start playing with it.
Transformations in the script
First create a C # script named "Mytransform" and add this script for our "Triangle" object. The code is as follows:
Code parsing
This code is simple, declaring a 4x4 matrix, and then calling the Settrs function of the 4x4 matrix (T for Translate (PAN), R for Rotation (rotation), and S for scale (scaling). This function is used to set a translation, rotation, and scaling matrix. In the code, we pass in the position, rotation, and Localscale of the object, so that it is easy to observe the corresponding transformation matrix.
1 translation Matrix
This is the translation matrix, where (Tx,ty,tz) is the direction of the translation vector, some books are the Tx, Ty, Tz placed on the fourth row, when the Saxon test, unity translation matrix such as such. Let's do a simple test:
1. Change the Position of "Triangle" to (+/-)
2. Click the Play button to see the changes in the matrix properties:
The "E03", "E13", and "E23" of the red line in the picture correspond to the "Tx", "Ty", and "Tz" in the translation matrix above, which shows that unity is using the translation matrix in this way.
Next, we use the translation matrix to make a simple translation, and let's go back to the "Mytransform" script to add some code:
Code parsing
The code in the Start function was commented out earlier because it was used later in the article. In the code, a new variable of type Vector4 is added because the 4x4 matrix cannot be multiplied by a three-dimensional vector.
Then the start function initializes the vector and the matrix, the variable "V" holds the position of the current object, and the current matrix is the unit matrix (for matrix multiplication and the unit matrix I don't want to talk about, please everyone Baidu study).
Then find the corresponding translation in the matrix in the direction of "Tx", "Ty", "Tz", in the code, I let the object to the X-axis to translate 3 units, the Y-axis to translate 4 units, Z-axis direction of translation 5 units.
After performing the operation of the matrix transformation, the operation of the Matrix is as follows:
Finally, the calculated results are passed to the Position of the object. Then call the function in the Start function:
PS: Remember to set the Position of the "Triangle" object to (0,0,0). Now let's click Play to see if the result is right:
Well, the value is correct. You can also modify the parameters to play. At this point, the translation of the matrix is generally finished, or a little bit of an ignorant man can give Saxon to say.
Scaling matrix
This is the scaling matrix, where "Sx", "Sy", and "Sz" are the scaling factors on each axis. A scaling matrix is a matrix of matrix representation of object size transformations. If the scaling factor is less than 1, the object is reduced, and if it is greater than 1, the object expands, and if equal to 1 does not change.
Next we do a simple test that cancels the code in the Start function and then modifies the "Triangle" object's Scale property to (three-way), click Play to see the results:
Finding the corresponding scaling factor for each axis, the result is now correct.
Next, we use the scaling matrix to do a simple operation, let's go back to the "Mytransform" script to add some code:
Code parsing
This code does not go anywhere with the code that was previously panned. Comments are also given in the code. The main is the matrix scaling operation is not the same, the following figure is done, the scaling operation is relatively simple.
So there are:
Finally, modify the code of the Start function to:
Now click on the Play button to see if the result is correct:
Okey, it seems that the result is correct.
PS: When the scaling factor on each axis is equal, that is: Sx=sy=sz, it is evenly scaled.
Rotation matrix
is the so-called rotation matrix. In our practice, I use the rotation along the x-axis to do the practice.
Start with a simple test, let's modify the script, then set the object's rotation parameter to (45,0,0), and finally click on the Play button to see the results, as shown in:
Here I chose the angle of 45°, because the values of sin45° and cos45° are equal (although their values are not equal, but in fact they are equal, we can use an if statement), let's see if the result is right:
Okey, it seems to be right.
Next, we use the rotation matrix to do a simple operation, let's go back to the "Mytransform" script to add some code:
Because the picture is stitching, all a little awkward
Code parsing
First, a variable of type float "angle" is used to enter an angle that needs to be rotated, and then a variable "Axle" of type Emun (enum) is declared, which is used to select how to rotate.
Then the matrix is initialized in the "myrotation" function. The next judgment is to correspond to each axis of rotation (can be compared with the above rotation matrix, I believe everyone can understand it), in the judgment statement mainly used in three functions: Mathf.sin (), Mathf.cos (), Mathf.deg2rad (). The first two functions we all know what it is, the latter function is used for the radian angle, because the first two functions accept a radian-based value.
The next code is to do the matrix into a four-dollar operation, specifically, please fancy the formula:
Finally, the calculated value is passed to the Rotation of the object.
All right. Now let's go back to the object's Inspector panel to modify the "Angle" parameter and the AXLE option, then click on the Play button to test it (here Saxon choose to rotate 45° around the X-axis):
Perfect. As we expected, the guys can test the parameters themselves.
Finally, we send you a small tool, just pass in a transformation matrix to help you do the translation, rotation, zooming work, and then pass the value to the object's Position, Rotation, scale:
Next, use an example to demonstrate how the gadget is used.
Back in our scene, locate the "Triangle" object and modify the "Matrix" attribute of the "mytransform" script in the Inspector panel as shown in:
The red arrow represents the pan, the green arrow represents the zoom, the yellow arrow represents the rotation, the transformation matrix represents: The object shifts 3 units on the x axis, translates 4 units on the Y axis, translates 5 units on the z axis (position (3,4,5)), and the object rotates 90° around the x axis (Rotation ( 90,0,0)); The object is not scaled (1,1,1).
Then go back to the start function of the "mytransform" script and invoke the method in the tool class to complete the operation of the transformation matrix:
Finally click on the Play button to view the results:
Perfect. It looks exactly the same as we expected, and the guys can modify the parameters to test them themselves.
Well, this article is here. This part of the content is also Saxon recently learned, it is also a study notes, if there is any wrong place also ask the great God to teach advice.
Attached code:
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityengine; Public classTringle:monobehaviour {PrivateMesh mesh; Publicvector3[] vertices; Public int[] triangle; //Use this for initialization voidStart () {Mesh=NewMesh (); Mesh.vertices=vertices; Mesh.triangles=triangle; varMeshrender = getcomponent<meshrenderer> (); if(Meshrender = =NULL) Meshrender= This.gameobject.addcomponent<meshrenderer> (); varMeshfilter = getcomponent<meshfilter> (); if(Meshfilter = =NULL) Meshfilter= This.gameobject.addcomponent<meshfilter> (); Meshfilter.mesh=mesh; } //Update is called once per frame voidUpdate () {}}
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityengine; Public enumaxle{X, Y, Z,} Public classMytransform:monobehaviour { PublicAxle Axle; PublicVECTOR4 v4; Publicmatrix4x4 Matrix; //Use this for initialization voidStart () {//Matrix. Settrs (//Transform.position,//Transform.rotation,//Transform.localscale//); } voidMytranslate (floatXfloatYfloatz) {v4=NewVector4 (transform.position.x, TRANSFORM.POSITION.Y, Transform.position.z, 1 ); /*Identity * 1 0 0 0 * 0 1 0 0 * 0 0 1 0 * 0 0 0 1*/Matrix=matrix4x4.identity; //POSMATRIX.M03 =x; Matrix.m13=y; MATRIX.M23=Z; /*Pos Transform * 1 0 0 x * Pos.x 1 * pos.x + 0 * pos.x + 0 * pos.x + x * pos.x x * pos.x * 0 1 0 y * pos.y = = ... = = y * Pos.y * 0 0 1 Z * pos.z . Z * pos.z * 0 0 0 1 * 1 ... 1*/V4= Matrix *v4; Transform.position=NewVector3 (v4.x, V4.y, v4.z); } voidMyscale (floatXfloatYfloatz) {v4=NewVector4 (transform.localscale.x, Transform.localscale.y, Transform.localscale.z, 1 ); /*Identity * 1 0 0 0 * 0 1 0 0 * 0 0 1 0 * 0 0 0 1*/Matrix=matrix4x4.identity; Matrix.m00=x; MATRIX.M11=y; MATRIX.M22=Z; V4= Matrix *v4; Transform.localscale=NewVector3 (v4.x, V4.y, v4.z); } voidMyrotation (Axle Axle,floatangle) {Matrix=matrix4x4.identity; //Set Matrix if(Axle = =axle.x) {MATRIX.M11= Mathf.cos (Angle *Mathf.deg2rad); MATRIX.M22=-mathf.sin (Angle *Mathf.deg2rad); Matrix.m21= Mathf.sin (Angle *Mathf.deg2rad); MATRIX.M22= Mathf.cos (Angle *Mathf.deg2rad); } Else if(Axle = =axle.y) {matrix.m00= Mathf.cos (Angle *Mathf.deg2rad); MATRIX.M02= Mathf.sin (Angle *Mathf.deg2rad); MATRIX.M20=-mathf.sin (Angle *Mathf.deg2rad); MATRIX.M22= Mathf.cos (Angle *Mathf.deg2rad); } Else if(Axle = =axle.z) {matrix.m00= Mathf.cos (Angle *Mathf.deg2rad); MATRIX.M01=-mathf.sin (Angle *Mathf.deg2rad); MATRIX.M10= Mathf.sin (Angle *Mathf.deg2rad); MATRIX.M11= Mathf.cos (Angle *Mathf.deg2rad); } //To quaternion floatQW = mathf.sqrt (1f + matrix.m00 + MATRIX.M11, MATRIX.M22)/2; floatW =4*QW; floatQX = (MATRIX.M21-MATRIX.M12)/W; floatQY = (MATRIX.M02-MATRIX.M20)/W; floatQZ = (matrix.m10-matrix.m101)/W; Transform.rotation=Newquaternion (QX, qy, QZ, QW); }}
"Reprint" the translation, rotation, and scaling of matrices in unity