我的本意是想让整块部件往x轴方向移动,但是当我获取他们的位置的时候,部分部件就会偏移原来的位置,移动到异常的地方
以下是我的部分代码:
这个是初始化代码
/* 连接入口 */
// 设置模型交互的方法
const setupModelInteraction = () => {
tree = viewer.model.getData().instanceTree;
const partNamesList = {
basePart: ["T1"],
Part: ["X1", "X2", "X3", "X5", "X6", "X12", "X13", "Z1", "Z2"]
}
const nodeIds = getModelNodeIds(partNamesList);
baseRodPivot = createPivot(nodeIds.T1NodeId);
// 获取除basePart外的所有部件名称
const nonBasePartNames = Object.keys(partNamesList)
.filter(key => key !== 'basePart') // 排除基础部件
.flatMap(key => partNamesList[key]); // 展平名称数组
// 为每个非基础部件创建辅助对象
const helperObjects = nonBasePartNames.map(name => createHelper(nodeIds[`${name}NodeId`], baseRodPivot.position));
//添加非基础件部件
addHelpersToPivot(baseRodPivot, helperObjects);
initializeRotation(helperObjects, nodeIds);
};
/* 运动入口 */
// 初始化旋转的方法,接收辅助对象和节点ID对象作为参数
const initializeRotation = (helperObjects, nodeIds) => {
const partSuffixes = {
linePart: {
x: ["X1", "X2", "X3", "X5", "X6", "X12", "X13"],
z: ["Z1", "Z2"]
},
};
// 获取辅助对象和节点ID的对应关系
const helperNodeIdPairs = getHelperNodeIdPairs(partSuffixes, helperObjects, nodeIds);
console.log(helperNodeIdPairs)
startRotationAnimation(helperNodeIdPairs);
};
上下文:(就是在applyHelpersTransformations这个函数之后就会出现问题)
// 应用辅助对象变换的方法,接收辅助对象和节点ID对象作为参数
const applyHelpersTransformations = (helperNodeIdPairs) => {
for (const pair of helperNodeIdPairs) {
assignTransformations(pair.helper, pair.id);
}
};
// 动画辅助对象的方法,接收一个包含基准点、辅助对象、X轴偏移量、Z轴偏移量的对象作为参数
const animateHelpers = ({ helperNodeIdPairs, xOffsetAmount, zOffsetAmount }) => {
const render = () => {
const positions = calculateHelpersNewPositions(helperNodeIdPairs, xOffsetAmount, zOffsetAmount);
updateHelpersPositions(helperNodeIdPairs, positions);
applyHelpersTransformations(helperNodeIdPairs);
viewer.impl.sceneUpdated(true);
requestAnimationFrame(render);
};
render();
};
核心代码:
// 辅助函数
const assignTransformations = (refererence_dummy, nodeId) => {
refererence_dummy.parent.updateMatrixWorld();
const position = new THREE.Vector3();
const rotation = new THREE.Quaternion();
const scale = new THREE.Vector3();
refererence_dummy.matrixWorld.decompose(position, rotation, scale);
tree.enumNodeFragments(nodeId, (frag) => {
const fragProxy = viewer.impl.getFragmentProxy(viewer.model, frag);
fragProxy.getAnimTransform();
fragProxy.position = position;
fragProxy.quaternion = rotation;
fragProxy.updateAnimTransform();
});
}
const findNodeIdbyName = (name) => {
let nodeList = Object.values(tree.nodeAccess.dbIdToIndex);
for (let i = 1, len = nodeList.length; i < len; ++i) {
let node_name = tree.getNodeName(nodeList[i]);
if (node_name === name) {
return nodeList[i];
}
}
return null;
}
const getFragmentWorldMatrixByNodeId = (nodeId) => {
let result = {
fragId: [],
matrix: [],
};
tree.enumNodeFragments(nodeId, (frag) => {
let fragProxy = viewer.impl.getFragmentProxy(viewer.model, frag);
let matrix = new THREE.Matrix4();
fragProxy.getWorldMatrix(matrix);
result.fragId.push(frag);
result.matrix.push(matrix);
});
return result;
}
我想要知道fusion360建出来的模型应该如何获取他们的位置,为什么这个代码获取出来的部分位置是错误的
用 fragProxy.setMatrix 方法,不是直接设置 position 和 quaternion
用 THREE.Matrix4 的 makeTranslation 方法来建一个平移矩阵: