Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Xiao CAI just learned the Transform class, its core part lies in the matrix transformation, and the matrix transformation is processed by Matrix4, and no matter how the translation rotation operation, or fundamentally a fourth order matrix operation; Matrix4: Matrix4: Matrix4: Matrix4

The basic structure

Matrix4(double arg0, … double arg15)

The default constructor for Matrix4 consists of 16 arguments, arranged from left to right and top to bottom in a fourth-order matrix;

The transform: Matrix4 (1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0).Copy the code

Matrix4.identity()

Matrix4.identity() initializes a Matrix4, which can be used to perform other matrix operations;

transform: Matrix4.identity(), transform: Matrix4.identity().. rotateZ(pi / 4),Copy the code

Matrix4.zero()

Matrix4.zero() initializes an empty matrix with all parameters of 0, and sets the specific change matrix based on that; All initializations start with matrix4.zero ();

transform: Matrix4.zero(), transform: Matrix4.zero().. setIdentity(),Copy the code

Matrix4.fromList()

Matrix4.fromlist () assigns values from the List to Matrix4(double arg0,… Double arg15) similar;

A List < double > List = [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0]. transform: Matrix4.fromList(list),Copy the code

Matrix4.copy()

Matrix4.copy() copies an existing Matrix4;

transform: Matrix4.copy(Matrix4.identity()),
Copy the code

Matrix4.columns()

Matrix4.columns() consists of four 4D column vectors;

import 'package:vector_math/vector_math_64.dart' as v; transform: Matrix4. Columns (v.V ector4 (1.0, 0.0, 0.0, 0.0), v.V ector4 (0.0, 1.0, 0.0, 0.0), v.V ector4 (0.0, 0.0, 1.0, 0.0), V.V ector4 (0.0, 0.0, 0.0, 1.0)),Copy the code

Matirx4.inverted()

May be inverted matrix (), inverted matrix (matrix coordinates are symmetric along the left diagonal);

transform: Matrix4.inverted(Matrix4.fromList(list)),
Copy the code

Matrix4.outer()

Matrix4.outer() = matrix4.outer ();

Transform: matrix4.outer (v.vector4 (1.0, 1.0, 1.0, 1.20), v.vector4. Identity ()), transform: matrix4.outer (v.vector4 (1.0, 1.0, 1.0, 1.20)), transform: Matrix4.outer(v.vector4.identity (), v.vector4 (1.0, 1.0, 1.0, 1.20)),Copy the code

Scale Scale constructor

Matrix4.diagonal3()

Matrix4.diagonal3() sets the scaling matrix via Vector3;

Matrix4.diagonal3(v.vector3 (2.0, 1.0, 1.0)), matrix4. diagonal3(v.vector3 ([2.0, 2.0, 2.0])),Copy the code

Two construction methods of Vector3 were analyzed, and the three parameters were scaled in x/Y/Z axis respectively.

factory Vector3(double x, double y, double z) => new Vector3.zero().. setValues(x, y, z); factory Vector3.array(List<double> array, [int offset = 0]) => new Vector3.zero().. copyFromArray(array, offset);Copy the code

Matirx4.diagonal3Values()

Matrix4.diagonal3values () is similar to extracting the above constructor and directly scaling the three parameters;

The transform: Matrix4 diagonal3Values (2.0, 1.0, 1.0),Copy the code

Diagonal3Values source analysis, found that the Matrix4 sit on the diagonal values corresponding to the X/Y/Z axis direction of the scale;

factory Matrix4.diagonal3Values(double x, double y, double z) => new Matrix4.zero() .. _m4storage [15] = 1.0.. _m4storage[10] = z .. _m4storage[5] = y .. _m4storage[0] = x;Copy the code

Transform translation construction method

Matrix4.translation()

Matrix4.translation also through Vector3 construction method of each parameter set matrix translation; Horizontal to the right is the positive X-axis, vertical downward is the positive Y-axis;

Transform: matrix4. translation(v.vector3 (10.0, 10.0, 10.0)), transform: Matrix4. Translation (v.V ector3. Array ([10.0, 10.0, 10.0])),Copy the code

Matrix4.translationValues()

Matrix4. TranslationValues matrix translation quantity direct assignment () will show;

The transform: Matrix4 translationValues (10.0, 10.0, 10.0),Copy the code

By analyzing translationValues source code, the first three columns of the fourth row in Matirx4 fourth-order matrix correspond to the offset of x/Y/Z axis respectively.

factory Matrix4.translationValues(double x, double y, double z) => new Matrix4.zero() .. setIdentity() .. setTranslationRaw(x, y, z); void setTranslationRaw(double x, double y, double z) { _m4storage[14] = z; _m4storage[13] = y; _m4storage[12] = x; }Copy the code

Rotation Rotation constructor

Matrix4.rotationX()

Matrix4.rotationx () rotates along the X-axis;

transform: Matrix4.rotationX(pi / 3),
Copy the code

Analysis of the source code, the fourth order matrix, index is 5/6/9/10 common operation rotation radian;

void setRotationX(double radians) { final double c = math.cos(radians); final double s = math.sin(radians); _m4storage [0] = 1.0; _m4storage [1] = 0.0; _m4storage [2] = 0.0; _m4storage [4] = 0.0; _m4storage[5] = c; _m4storage[6] = s; _m4storage [8] = 0.0; _m4storage[9] = -s; _m4storage[10] = c; _m4storage [3] = 0.0; _m4storage [7] = 0.0; _m4storage [11] = 0.0; }Copy the code

Matrix4.rotationY()

Matrix4.rotationy () rotates along the y axis;

transform: Matrix4.rotationY(pi / 3),
Copy the code

Analysis source code, the fourth order matrix, index is 0/2/8/10 common operation rotation radian;

void setRotationY(double radians) { final double c = math.cos(radians); final double s = math.sin(radians); _m4storage[0] = c; _m4storage [1] = 0.0; _m4storage[2] = -s; _m4storage [4] = 0.0; _m4storage [5] = 1.0; _m4storage [6] = 0.0; _m4storage[8] = s; _m4storage [9] = 0.0; _m4storage[10] = c; _m4storage [3] = 0.0; _m4storage [7] = 0.0; _m4storage [11] = 0.0; }Copy the code

Matrix4.rotationZ()

Matrix4.rotationz () rotates along the z-axis;

transform: Matrix4.rotationZ(pi / 3),
Copy the code

Analysis of the source code, the fourth order matrix, index is 0/1/4/5 joint operation rotation radian;

void setRotationZ(double radians) { final double c = math.cos(radians); final double s = math.sin(radians); _m4storage[0] = c; _m4storage[1] = s; _m4storage [2] = 0.0; _m4storage[4] = -s; _m4storage[5] = c; _m4storage [6] = 0.0; _m4storage [8] = 0.0; _m4storage [9] = 0.0; _m4storage [10] = 1.0; _m4storage [3] = 0.0; _m4storage [7] = 0.0; _m4storage [11] = 0.0; }Copy the code

Skew Angle cutting

Matrix4.skewX()

SkewX (), as I mentioned in my last blog post, is a diagonal cut along the X-axis;

transform: Matrix4.skewX(pi / 6),
Copy the code

Analysis of the source code, the fourth order matrix, the second row of the first column of elements corresponding to the tangent value, that is, the trigonometric function tan;

factory Matrix4.skewX(double alpha) {
    final Matrix4 m = new Matrix4.identity();
    m._m4storage[4] = math.tan(alpha);
    return m;
}
Copy the code

Matrix4.skewY()

SkewY () is an oblique cut along the Y-axis;

transform: Matrix4.skewY(pi / 6),
Copy the code

Analysis of the source code, the fourth order matrix, the first row of the second column elements corresponding to the tangent value, that is, the trigonometric function tan;

factory Matrix4.skewY(double beta) {
    final Matrix4 m = new Matrix4.identity();
    m._m4storage[1] = math.tan(beta);
    return m;
}
Copy the code

Matrix4.skew()

Skew () is an oblique cut along the x/y axis;

transform: Matrix4.skew(pi / 6, pi / 6),
Copy the code

Analysis of the source code, the fourth order matrix, the above two elements combined to show the effect;

factory Matrix4.skew(double alpha, double beta) {
    final Matrix4 m = new Matrix4.identity();
    m._m4storage[1] = math.tan(beta);
    m._m4storage[4] = math.tan(alpha);
    return m;
}
Copy the code

Composite construction

Matrix4.compose()

Matrix4.compose() can draw pan/rotate/scale combined operations;

Transform: Matrix4.compose(v.vector3 (10.0, 10.0, 10.0), v.quaternion.Random(math.random (10)), v.vector3 (1.5, 1.0, 1.0)),Copy the code

Analysis of the source code, three parameters corresponding to translation/rotation/scaling; Euler rotation is used in the amount of rotation, which is not well understood by xiaokai, but a construction method of V.quaternion. Random () is used in the test, which needs to be further explored.

factory Matrix4.compose( Vector3 translation, Quaternion rotation, Vector3 scale) => new Matrix4.zero() .. setFromTranslationRotationScale(translation, rotation, scale);Copy the code


Matirx4 involves a wide range of methods, there are many small dishes have not been studied, just tried some common construction methods, if there are mistakes, please give more guidance!

Source: Little Monk A Ce