Matrix4
是 Three.js 中的一个类,表示 4x4 的矩阵。4x4 矩阵常用于 3D 图形学中的几何变换,如平移、缩放、旋转和投影等。Matrix4
在 Three.js 中的作用是对物体进行这些几何变换,尤其在3D场景中进行坐标转换时十分重要。
使用给定参数按行优先顺序创建一个 4x4 矩阵。如果未提供参数,构造函数会将 Matrix4 初始化为 4x4 单位矩阵。 Matrix4( n11 : Number, n12 : Number, n13 : Number, n14 : Number, n21 : Number, n22 : Number, n23 : Number, n24 : Number, n31 : Number, n32 : Number, n33 : Number, n34 : Number, n41 : Number, n42 : Number, n43 : Number, n44 : Number )
Matrix4 有一个属性三十七个方法
属性
- elements : Array 矩阵列优先column-major列表。
方法
- clone () : Matrix4 创建一个新的矩阵,元素elements与该矩阵相同。
- compose ( position : Vector3, quaternion : Quaternion, scale : Vector3 ) : this 设置将该对象位置 position,四元数quaternion 和 缩放scale 组合变换的矩阵。
const matrix = new THREE.Matrix4();
// 假设 matrix 经过了一系列变换
const position = new THREE.Vector3();
const quaternion = new THREE.Quaternion();
const scale = new THREE.Vector3();
matrix.decompose(position, quaternion, scale);
console.log(position); // 输出物体的位置信息
console.log(quaternion); // 输出物体的旋转信息
console.log(scale); // 输出物体的缩放比例
- copy ( m : Matrix4 ) : this 将矩阵m的元素elements复制到当前矩阵中。
- copyPosition ( m : Matrix4 ) : this 将给定矩阵 m : Matrix4 的平移分量拷贝到当前矩阵中。
- decompose ( position : Vector3, quaternion : Quaternion, scale : Vector3 ) : this 将矩阵分解到给定的平移position ,旋转 quaternion,缩放scale分量中。注意:并非所有矩阵都可以通过这种方式分解。 例如,如果一个对象有一个非均匀缩放的父对象,那么该对象的世界矩阵可能是不可分解的,这种方法可能不合适。 与 compose 相反 compose 是设置,这个是读取
- determinant () : Float 计算并返回矩阵的行列式determinant 。 基于这个的方法概述here。 行列式是矩阵的一个重要特性,通常用于判断矩阵是否可逆。如果矩阵的行列式不为零,则矩阵是可逆的;如果行列式为零,则矩阵不可逆。在 3D 计算中,行列式还可以用来判断一个变换是否会导致空间的翻转或压缩
- equals ( m : Matrix4 ) : Boolean 如果矩阵m 与当前矩阵所有对应元素相同则返回true。
- extractBasis ( xAxis : Vector3, yAxis : Vector3, zAxis : Vector3 ) : this 将矩阵的基向量basis提取到指定的3个轴向量中。 如果矩阵如下:
- extractRotation ( m : Matrix4 ) : this 将给定矩阵m的旋转分量提取到该矩阵的旋转分量中。
- fromArray ( array : Array, offset : Integer ) : this array - 用来存储设置元素数据的数组 offset - (可选参数) 数组的偏移量,默认值为 0。 使用基于列优先格式column-major的数组来设置该矩阵。
- invert () : this 将当前矩阵翻转为它的逆矩阵,使用 analytic method 解析方式。你不能对行或列为 0 的矩阵进行翻转,如果你尝试这样做,该方法将生成一个零矩阵。
- getMaxScaleOnAxis () : Float 获取3个轴方向的最大缩放值。
const matrix = new THREE.Matrix4();
matrix.makeScale(2, 3, 1); // 设置矩阵的缩放为 X: 2, Y: 3, Z: 1
const maxScale = matrix.getMaxScaleOnAxis(); // 获取三个轴上的最大缩放值
console.log(maxScale); // 输出:3,因为 Y 轴的缩放最大
- identity () : this 将当前矩阵重置为单位矩阵identity matrix。
const matrix = new THREE.Matrix4();
matrix.makeScale(2, 3, 1); // 设置缩放矩阵
matrix.identity(); // 重置为单位矩阵
console.log(matrix); // 输出单位矩阵 [1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]
- lookAt ( eye : Vector3, target : Vector3, up : Vector3 ) : this 构造一个旋转矩阵,从eye 指向 target,由向量 up 定向。- 常用于设置相机的视角,例如在 3D 场景中,让相机看向特定的物体。 也可以用于其他需要物体朝向特定目标的情况
const matrix = new THREE.Matrix4();
const eye = new THREE.Vector3(0, 0, 10); // 相机位置 (0, 0, 10)
const target = new THREE.Vector3(0, 0, 0); // 目标位置 (0, 0, 0)
const up = new THREE.Vector3(0, 1, 0); // 上方向 (0, 1, 0)
// 设置矩阵使其从 eye 位置看向 target 位置
matrix.lookAt(eye, target, up);
console.log(matrix);
- makeRotationAxis ( axis : Vector3, theta : Float ) : this axis — 旋转轴,需要被归一化。 theta — 旋转量(弧度)。 设置当前矩阵为围绕轴 axis 旋转量为 theta弧度。 这是一种有点争议但在数学上可以替代通过四元数Quaternions旋转的办法。 请参阅此处here的讨论。
const matrix = new THREE.Matrix4();
// 延自身 X Y 轴旋转 45 度
const axis = new THREE.Vector3(1, 1, 0).normalize(); // 自定义旋转轴 (1, 1, 0) 并单位化
const theta = Math.PI / 4; // 旋转角度 45 度(π/4 弧度)
// 创建一个绕指定轴旋转的矩阵
matrix.makeRotationAxis(axis, theta);
console.log(matrix);
- makeBasis ( xAxis : Vector3, yAxis : Vector3, zAxis : Vector3 ) : this 通过给定的三个向量设置该矩阵为基矩阵basis: 将四维矩阵中的三维矩阵设置为指定值
- makePerspective ( left : Float, right : Float, top : Float, bottom : Float, near : Float, far : Float ) : this 创建一个透视投影矩阵perspective projection。 在引擎内部由PerspectiveCamera.updateProjectionMatrix()使用。
- makeOrthographic ( left : Float, right : Float, top : Float, bottom : Float, near : Float, far : Float ) : this 创建一个正交投影矩阵orthographic projection。 在引擎内部由OrthographicCamera.updateProjectionMatrix()使用。
- makeRotationFromEuler ( euler : Euler ) : this 将传入的欧拉角转换为该矩阵的旋转分量(左上角的3x3矩阵)。 矩阵的其余部分被设为单位矩阵。根据欧拉角euler的旋转顺序order,总共有六种可能的结果。详见
- makeRotationFromQuaternion ( q : Quaternion ) : this 将这个矩阵的旋转分量设置为四元数q指定的旋转,如下链接所诉here。 矩阵的其余部分被设为单位矩阵。因此,给定四元数q = w + xi + yj + zk,得到的矩阵为:
- makeRotationX ( theta : Float ) : this 把该矩阵设置为绕x轴旋转弧度theta (θ)大小的矩阵。 结果如下:
- makeRotationY ( theta : Float ) : this 把该矩阵设置为绕Y轴旋转弧度theta (θ)大小的矩阵。 结果如下:
- makeRotationZ ( theta : Float ) : this 把该矩阵设置为绕z轴旋转弧度theta (θ)大小的矩阵。 结果如下:
- makeScale ( x : Float, y : Float, z : Float ) : this x - 在X轴方向的缩放比。 y - 在Y轴方向的缩放比。 z - 在Z轴方向的缩放比。 将这个矩阵设置为缩放变换:
- makeTranslation ( v : Vector3 ) : this 取传入参数v : Vector3中值设设置该矩阵为平移变换:
- makeTranslation ( x : Float, y : Float, z : Float ) : this 取传入参数x y z 中值设设置该矩阵为平移变换:
- multiply ( m : Matrix4 ) : this 将当前矩阵乘以矩阵m。
- multiplyMatrices ( a : Matrix4, b : Matrix4 ) : this 设置当前矩阵为矩阵a x 矩阵b。 计算方法同上
- multiplyScalar ( s : Float ) : this 当前矩阵所有的元素乘以该缩放值s
- premultiply ( m : Matrix4 ) : this 将矩阵m乘以当前矩阵。
- scale ( v : Vector3 ) : this 将该矩阵的列向量乘以对应向量v的分量。
object.scale(new Vector3(1.5, 2, 0.5));
- set ( n11 : Float, n12 : Float, n13 : Float, n14 : Float, n21 : Float, n22 : Float, n23 : Float, n24 : Float, n31 : Float, n32 : Float, n33 : Float, n34 : Float, n41 : Float, n42 : Float, n43 : Float, n44 : Float ) : this 以行优先的格式将传入的数值设置给该矩阵中的元素elements。
- setFromMatrix3 ( m : Matrix3 ) : this 根据参数 m 的值,设置当前矩阵左上 3x3 的矩阵值。
- setPosition ( v : Vector3 ) : this 取传入参数v : Vector3中值设置该矩阵的位置分量,不影响该矩阵的其余部分——即,如果该矩阵当前为:
- setPosition ( x : Float, y : Float, z : Float ) : this 取传入参数v : x y z中值设置该矩阵的位置分量,不影响该矩阵的其余部分——即,如果该矩阵当前为:
- toArray ( array : Array, offset : Integer ) : Array array - (可选参数) 存储矩阵元素的数组,如果未指定会创建一个新的数组。 offset - (可选参数) 存放矩阵元素数组的偏移量。
- transpose () : this 将该矩阵转置Transposes。