You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让建筑物绕自身中心轴旋转,且修改旋转参数时位置不被重置?

问题根因

  • 你每次调用setPlacementTransform时传入的是全新的THREE.Matrix4实例,该方法是全量覆盖模型的变换矩阵,旧的位移/旋转属性会被直接清空,所以修改旋转后之前设置的位置就会恢复到初始状态。
  • 建筑物可以实现基于自身中心轴旋转,只需要调整矩阵组合顺序,增加锚点偏移处理即可。

修复代码

场景1:模型默认锚点已经在自身中心

直接按正确顺序组合旋转、位移矩阵后一次性设置即可:

// 生成旋转矩阵
const rotationMat = new THREE.Matrix4().makeRotationZ(angleRadian);
// 生成位移矩阵
const positionMat = new THREE.Matrix4().setPosition({
  x: this.valueX,
  y: this.valueY,
  z: this.valueZ,
});
// 矩阵右乘:先应用旋转,再应用位移
const finalTransformMat = positionMat.multiply(rotationMat);
// 一次性设置全量变换
this.viewer.model.setPlacementTransform(finalTransformMat);

场景2:模型默认锚点不在自身中心

需要先计算模型的中心偏移量,调整变换顺序实现绕自身中心旋转:

// 模型加载完成后仅计算一次中心偏移,无需每次修改变换都重复计算
if (!this.modelCenterOffset) {
  const modelBox = new THREE.Box3().setFromObject(this.viewer.model);
  this.modelCenterOffset = modelBox.getCenter(new THREE.Vector3()).negate();
}

// 偏移矩阵:将模型锚点移动到自身几何中心
const offsetMat = new THREE.Matrix4().setPosition(this.modelCenterOffset);
// 旋转矩阵
const rotationMat = new THREE.Matrix4().makeRotationZ(angleRadian);
// 位移矩阵
const positionMat = new THREE.Matrix4().setPosition({
  x: this.valueX,
  y: this.valueY,
  z: this.valueZ,
});

// 变换顺序:先偏移到中心 → 执行旋转 → 位移到目标坐标
const finalTransformMat = positionMat.multiply(rotationMat).multiply(offsetMat);
this.viewer.model.setPlacementTransform(finalTransformMat);

注意矩阵的相乘顺序不能颠倒,Three.js采用右乘规则,越靠右的变换越先作用到模型上,顺序错误会导致变换效果不符合预期。

内容的提问来源于stack exchange,提问作者surya kumara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 23:54:03