首页 / Cesium / API 文档
Matrix4
先看结论
4×4 变换矩阵,负责平移、旋转、缩放的组合。相机姿态、模型摆放、 局部参照系都由它描述。
new Cesium.Matrix4(column0Row0, column1Row0, … , column3Row3)
构造参数
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| 16 个数值 | number | 0 | 按列优先顺序传入:先第一列的四行,再第二列,依此类推 |
result(部分静态方法) | Matrix4 | 新建 | 复用输出对象,减少 GC |
常用静态成员与方法
| 成员 | 返回 | 说明 |
|---|---|---|
Matrix4.IDENTITY | Matrix4 | 单位矩阵,常用来重置变换 |
Matrix4.ZERO | Matrix4 | 全零矩阵 |
fromTranslation(translation, result) | Matrix4 | 纯平移,参数为 Cartesian3 |
fromRotationTranslation(rotation, translation, result) | Matrix4 | 旋转用 Matrix3,再加平移 |
fromTranslationQuaternionRotationScale(t, q, s, result) | Matrix4 | 平移 + 四元数旋转 + 缩放,摆模型最常用 |
fromScale(scale, result) | Matrix4 | 非均匀缩放 |
multiply(left, right, result) | Matrix4 | 矩阵相乘,顺序是先应用 right 再应用 left |
inverse(matrix, result) | Matrix4 | 求逆,行列式为 0 时返回 undefined |
inverseTransformation(matrix, result) | Matrix4 | 刚体变换的快速求逆,比通用 inverse 快 |
getTranslation(matrix, result) | Cartesian3 | 取平移分量 |
setTranslation(matrix, translation, result) | Matrix4 | 替换平移分量,保留旋转 |
getMaximumScale(matrix) | number | 最大缩放因子,判断矩阵是否退化时用 |
transpose(matrix, result) | Matrix4 | 转置 |
clone(matrix, result) / equals(a, b) | Matrix4 / boolean | 拷贝与相等判断 |
示例
var position = Cesium.Cartesian3.fromDegrees(116.391, 39.907, 0);
var hpr = new Cesium.HeadingPitchRoll(
Cesium.Math.toRadians(45),
Cesium.Math.toRadians(10),
0
);
// 局部变换:绕 z 轴转 hpr,再沿局部 x 轴偏移 2 米
var local = Cesium.Matrix4.fromRotationTranslation(
Cesium.Matrix3.fromQuaternion(Cesium.Quaternion.fromHeadingPitchRoll(hpr)),
new Cesium.Cartesian3(2, 0, 1)
);
// 东-北-天坐标系 × 局部变换,得到模型在地心系里的位置
var enu = Cesium.Transforms.eastNorthUpToFixedFrame(position);
var modelMatrix = Cesium.Matrix4.multiply(enu, local, new Cesium.Matrix4());
var model = Cesium.Model.fromGltf({
url: '/models/truck.glb',
modelMatrix: modelMatrix,
minimumPixelSize: 32
});
viewer.scene.primitives.add(model);
// 取回平移分量
console.log(Cesium.Matrix4.getTranslation(modelMatrix, new Cesium.Cartesian3()));
注意事项
- Cesium 的矩阵按列主序存储,手写 16 个参数时容易写成行主序,能够用
fromTranslation就不要手填。 multiply(left, right)的语义是left × right,先平移后旋转与先旋转后平移结果不同。- 矩阵对象默认会被静态方法写进
result,同一个 result 被多处引用时会互相覆盖。 inverse对奇异矩阵返回undefined,调用后要判空。
经纬志(2026)。《Matrix4》。https://www.jingweizhi.com/cesium/api/matrix4.html(访问日期 2026-11-03)。