【问题标题】:ThreeJS copy one mesh world position rotation to anotherThreeJS 将一个网格世界位置旋转复制到另一个
【发布时间】:2021-08-09 07:09:02
【问题描述】:

我有 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);
}

【问题讨论】:

    标签: typescript three.js


    【解决方案1】:

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

    b.getWorldQuaternion( c.quaternion );
    

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

    【讨论】:

      猜你喜欢
      • 2019-10-25
      • 2014-11-25
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      • 2019-03-29
      • 2017-09-07
      • 1970-01-01
      相关资源
      最近更新 更多