经纬志
首页 / Cesium / API 文档

Matrix4

先看结论

4×4 变换矩阵,负责平移、旋转、缩放的组合。相机姿态、模型摆放、 局部参照系都由它描述。

最后验证 2026-11-03 · CesiumJS 1.121 · 更新 2026-10-28 · 引用本文
new Cesium.Matrix4(column0Row0, column1Row0, … , column3Row3)

构造参数

参数类型默认说明
16 个数值number0按列优先顺序传入:先第一列的四行,再第二列,依此类推
result(部分静态方法)Matrix4新建复用输出对象,减少 GC

常用静态成员与方法

成员返回说明
Matrix4.IDENTITYMatrix4单位矩阵,常用来重置变换
Matrix4.ZEROMatrix4全零矩阵
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)。