在 Autodesk Forge Viewer 中旋转模型

问题描述

您好,我正在使用查看器 API 开发一个应用程序。我有一个主模型。我在这个模型上加载其他模型。我可以移动和调整我稍后上传的这些模型的大小。我使用 makeRotationX,makeRotationY,makeRotationZ 函数实现了旋转。但是当我旋转加载的模型时,它会将其移动到起点。这可能是什么原因?例如,我将立方体添加到左侧,但它会移动到主要的 0,0 点并进行旋转。

first

我只是改变了旋转 Y 值,这就是结果。

second

代码

cubeModel.getModelTransform().makeRotationY(inputValue * Math.PI / 180);
                viewer.impl.invalidate(true,true,true)

解决方法

这是因为您使用 THREE.Matrix4 检索的模型的 model.getModelTransform() 变换可能已经包含一些变换(在这种特殊情况下,变换使红色立方体向左偏移)。并且 makeRotationY 方法不追加任何变换,它只是将矩阵重置为仅绕 Y 轴旋转的新变换。

相反,您想要做的是这样的:

let xform = model.getModelTransform().clone();
// modify the xform instead of resetting it,for example
let rotate = new THREE.Matrix4().makeRotationY(0.1);
let scale = new THREE.Matrix4().makeScale(0.1,0.5,0.1);
xform.premultiply(rotate);
xform.multiply(scale);
// since we cloned the matrix earlier (good practice),apply it back to the model
model.setModelTransform(xform);