Matrix4.copyPosition() 是three.js中的一个方法,用于将一个4 x 4的矩阵中的位置(即第四列)复制到另一个矩阵中。
copyPosition(matrix: Matrix4): Matrix4
matrix: 源矩阵,类型为 Matrix4。返回一个新的矩阵,类型为 Matrix4,包含了源矩阵中的位置信息。
import { Matrix4, Vector3 } from 'three';
const matrix1 = new Matrix4().makeRotationX(Math.PI / 2);
const matrix2 = new Matrix4().makeTranslation(2, 3, 4);
matrix1.copyPosition(matrix2);
console.log(matrix1);
输出:
Matrix4 {
  elements: [
    1, 0, 0, 2,
    0, 0, 1, 3,
    0, -1, 0, 4,
    0, 0, 0, 1
  ]
}
可以看到,新的矩阵 matrix1 中的位置信息(第四列)与 matrix2 中的相同。
Matrix4.copyPosition() 方法只会拷贝位置信息,不会拷贝旋转、缩放等信息。如果需要拷贝全部信息,可以使用 Matrix4.copy() 方法。