首页 > 解决方案 > ThreeJS 将一个网格世界位置旋转复制到另一个

问题描述

我有 3 个网格:cube(a)、tempSphere(b)、m_mesh(c)。我在一个组中有网格 a 和 b,a 作为父级。

我设置了组的位置旋转和位置,a 和 b 应该自行更新。现在我想将 c 设置为 b 的当前位置和旋转。这就是我所拥有的并且不起作用

const cube: Mesh = this.createCube(size, objectColor);
group.add(cube);
const homePos: THREE.Vector3 = this.getHomePosition(size, homePosition);
const tempSphere = this.createHomePosition(cube, homePosition);
group.add(tempSphere);

// set local position of group
group.position.x = meshPosition.x;
group.position.y = meshPosition.y;
group.position.z = meshPosition.z;
// set local rotation of group
const rad = rotationZ * Math.PI / 180;
group.rotation.z = rad;

// trying to update world matrix of group and tempSphere
group.updateMatrixWorld(true);
tempSphere.updateMatrix();
tempSphere.updateMatrixWorld(true);
tempSphere.updateWorldMatrix(true, true);
    
// get world matrix of tempSphere
let worldRotQuat: THREE.Quaternion;
let worldPos: THREE.Vector3;
tempSphere.getWorldQuaternion(worldRotQuat);
tempSphere.getWorldPosition(worldPos);

// set world matrix of tempSphere to outside mesh
if (this.transScaleTool.m_mesh !== null) {
  this.transScaleTool.m_mesh.matrixWorld.setRotationFromQuaternion(worldRotQuat);
  this.transScaleTool.m_mesh.matrixWorld.setPosition(worldPos);
}

标签: typescriptthree.js

解决方案


它应该是以下代码模式(假设c没有旋转的父级):

b.getWorldQuaternion( c.quaternion );

Object3D.getWorldQuaternion()自动更新世界矩阵,b因此不需要手动更新调用。


推荐阅读