Matrix4.multiply()
Matrix4.multiply()
方法将传入的矩阵与当前矩阵进行乘法运算,并将结果存储在当前矩阵中。
该方法有一个参数:
matrix
:Matrix4
类型的参数,表示要与当前矩阵进行乘法运算的矩阵。该方法没有返回值。
在进行计算前,该方法会将传入的矩阵进行转置。然后,对于当前矩阵的每一列,都会将它与传入矩阵的每一行进行内积运算,从而得到新的矩阵。
multiply(matrix: Matrix4): this {
const ae = this.elements;
const be = matrix.elements;
const te = this.multiplyArray;
let a11 = ae[ 0 ], a12 = ae[ 4 ], a13 = ae[ 8 ], a14 = ae[ 12 ];
let a21 = ae[ 1 ], a22 = ae[ 5 ], a23 = ae[ 9 ], a24 = ae[ 13 ];
let a31 = ae[ 2 ], a32 = ae[ 6 ], a33 = ae[ 10 ], a34 = ae[ 14 ];
let a41 = ae[ 3 ], a42 = ae[ 7 ], a43 = ae[ 11 ], a44 = ae[ 15 ];
let b11 = be[ 0 ], b12 = be[ 4 ], b13 = be[ 8 ], b14 = be[ 12 ];
let b21 = be[ 1 ], b22 = be[ 5 ], b23 = be[ 9 ], b24 = be[ 13 ];
let b31 = be[ 2 ], b32 = be[ 6 ], b33 = be[ 10 ], b34 = be[ 14 ];
let b41 = be[ 3 ], b42 = be[ 7 ], b43 = be[ 11 ], b44 = be[ 15 ];
te[ 0 ] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
te[ 4 ] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
te[ 8 ] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
te[ 12 ] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
te[ 1 ] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
te[ 5 ] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
te[ 9 ] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
te[ 13 ] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
te[ 2 ] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
te[ 6 ] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
te[ 10 ] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
te[ 14 ] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
te[ 3 ] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
te[ 7 ] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
te[ 11 ] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
te[ 15 ] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
this.fromArray(te);
return this;
}
示例:
const m1 = new THREE.Matrix4().makeRotationY(Math.PI / 4); // 创建一个旋转矩阵
const m2 = new THREE.Matrix4().makeTranslation(2, 0, 0); // 创建一个平移矩阵
m1.multiply(m2); // 矩阵相乘,结果存储在 m1 中
console.log(m1.elements); // 打印新的矩阵
输出结果:
[
0.7071067811865476, 0, 0.7071067811865475, 2,
0 , 1, 0 , 0,
-0.7071067811865475, 0, 0.7071067811865476, 0,
0 , 0, 0 , 1
]
上述示例中,我们创建了一个旋转矩阵 m1
和一个平移矩阵 m2
,并将它们乘起来,存储在 m1
中。最终输出的 m1
矩阵表示了先进行了平移变换,再进行了旋转变换的结果。